nx-sha512.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /**
  2. * SHA-512 routines supporting the Power 7+ Nest Accelerators driver
  3. *
  4. * Copyright (C) 2011-2012 International Business Machines Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 only.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. *
  19. * Author: Kent Yoder <yoder1@us.ibm.com>
  20. */
  21. #include <crypto/internal/hash.h>
  22. #include <crypto/sha.h>
  23. #include <linux/module.h>
  24. #include <asm/vio.h>
  25. #include "nx_csbcpb.h"
  26. #include "nx.h"
  27. static int nx_sha512_init(struct shash_desc *desc)
  28. {
  29. struct sha512_state *sctx = shash_desc_ctx(desc);
  30. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  31. struct nx_sg *out_sg;
  32. nx_ctx_init(nx_ctx, HCOP_FC_SHA);
  33. memset(sctx, 0, sizeof *sctx);
  34. nx_ctx->ap = &nx_ctx->props[NX_PROPS_SHA512];
  35. NX_CPB_SET_DIGEST_SIZE(nx_ctx->csbcpb, NX_DS_SHA512);
  36. out_sg = nx_build_sg_list(nx_ctx->out_sg, (u8 *)sctx->state,
  37. SHA512_DIGEST_SIZE, nx_ctx->ap->sglen);
  38. nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
  39. return 0;
  40. }
  41. static int nx_sha512_update(struct shash_desc *desc, const u8 *data,
  42. unsigned int len)
  43. {
  44. struct sha512_state *sctx = shash_desc_ctx(desc);
  45. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  46. struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
  47. struct nx_sg *in_sg;
  48. u64 to_process, leftover, total, spbc_bits;
  49. u32 max_sg_len;
  50. unsigned long irq_flags;
  51. int rc = 0;
  52. spin_lock_irqsave(&nx_ctx->lock, irq_flags);
  53. /* 2 cases for total data len:
  54. * 1: < SHA512_BLOCK_SIZE: copy into state, return 0
  55. * 2: >= SHA512_BLOCK_SIZE: process X blocks, copy in leftover
  56. */
  57. total = sctx->count[0] + len;
  58. if (total < SHA512_BLOCK_SIZE) {
  59. memcpy(sctx->buf + sctx->count[0], data, len);
  60. sctx->count[0] += len;
  61. goto out;
  62. }
  63. in_sg = nx_ctx->in_sg;
  64. max_sg_len = min_t(u32, nx_driver.of.max_sg_len/sizeof(struct nx_sg),
  65. nx_ctx->ap->sglen);
  66. do {
  67. /*
  68. * to_process: the SHA512_BLOCK_SIZE data chunk to process in
  69. * this update. This value is also restricted by the sg list
  70. * limits.
  71. */
  72. to_process = min_t(u64, total, nx_ctx->ap->databytelen);
  73. to_process = min_t(u64, to_process,
  74. NX_PAGE_SIZE * (max_sg_len - 1));
  75. to_process = to_process & ~(SHA512_BLOCK_SIZE - 1);
  76. leftover = total - to_process;
  77. if (sctx->count[0]) {
  78. in_sg = nx_build_sg_list(nx_ctx->in_sg,
  79. (u8 *) sctx->buf,
  80. sctx->count[0], max_sg_len);
  81. }
  82. in_sg = nx_build_sg_list(in_sg, (u8 *) data,
  83. to_process - sctx->count[0],
  84. max_sg_len);
  85. nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) *
  86. sizeof(struct nx_sg);
  87. if (NX_CPB_FDM(csbcpb) & NX_FDM_CONTINUATION) {
  88. /*
  89. * we've hit the nx chip previously and we're updating
  90. * again, so copy over the partial digest.
  91. */
  92. memcpy(csbcpb->cpb.sha512.input_partial_digest,
  93. csbcpb->cpb.sha512.message_digest,
  94. SHA512_DIGEST_SIZE);
  95. }
  96. NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE;
  97. if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
  98. rc = -EINVAL;
  99. goto out;
  100. }
  101. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
  102. desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  103. if (rc)
  104. goto out;
  105. atomic_inc(&(nx_ctx->stats->sha512_ops));
  106. spbc_bits = csbcpb->cpb.sha512.spbc * 8;
  107. csbcpb->cpb.sha512.message_bit_length_lo += spbc_bits;
  108. if (csbcpb->cpb.sha512.message_bit_length_lo < spbc_bits)
  109. csbcpb->cpb.sha512.message_bit_length_hi++;
  110. /* everything after the first update is continuation */
  111. NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
  112. total -= to_process;
  113. data += to_process - sctx->count[0];
  114. sctx->count[0] = 0;
  115. in_sg = nx_ctx->in_sg;
  116. } while (leftover >= SHA512_BLOCK_SIZE);
  117. /* copy the leftover back into the state struct */
  118. if (leftover)
  119. memcpy(sctx->buf, data, leftover);
  120. sctx->count[0] = leftover;
  121. out:
  122. spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
  123. return rc;
  124. }
  125. static int nx_sha512_final(struct shash_desc *desc, u8 *out)
  126. {
  127. struct sha512_state *sctx = shash_desc_ctx(desc);
  128. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  129. struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
  130. struct nx_sg *in_sg, *out_sg;
  131. u32 max_sg_len;
  132. u64 count0;
  133. unsigned long irq_flags;
  134. int rc;
  135. spin_lock_irqsave(&nx_ctx->lock, irq_flags);
  136. max_sg_len = min_t(u32, nx_driver.of.max_sg_len, nx_ctx->ap->sglen);
  137. if (NX_CPB_FDM(csbcpb) & NX_FDM_CONTINUATION) {
  138. /* we've hit the nx chip previously, now we're finalizing,
  139. * so copy over the partial digest */
  140. memcpy(csbcpb->cpb.sha512.input_partial_digest,
  141. csbcpb->cpb.sha512.message_digest, SHA512_DIGEST_SIZE);
  142. }
  143. /* final is represented by continuing the operation and indicating that
  144. * this is not an intermediate operation */
  145. NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
  146. count0 = sctx->count[0] * 8;
  147. csbcpb->cpb.sha512.message_bit_length_lo += count0;
  148. if (csbcpb->cpb.sha512.message_bit_length_lo < count0)
  149. csbcpb->cpb.sha512.message_bit_length_hi++;
  150. in_sg = nx_build_sg_list(nx_ctx->in_sg, sctx->buf, sctx->count[0],
  151. max_sg_len);
  152. out_sg = nx_build_sg_list(nx_ctx->out_sg, out, SHA512_DIGEST_SIZE,
  153. max_sg_len);
  154. nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
  155. nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
  156. if (!nx_ctx->op.outlen) {
  157. rc = -EINVAL;
  158. goto out;
  159. }
  160. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
  161. desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  162. if (rc)
  163. goto out;
  164. atomic_inc(&(nx_ctx->stats->sha512_ops));
  165. atomic64_add(csbcpb->cpb.sha512.message_bit_length_lo / 8,
  166. &(nx_ctx->stats->sha512_bytes));
  167. memcpy(out, csbcpb->cpb.sha512.message_digest, SHA512_DIGEST_SIZE);
  168. out:
  169. spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
  170. return rc;
  171. }
  172. static int nx_sha512_export(struct shash_desc *desc, void *out)
  173. {
  174. struct sha512_state *sctx = shash_desc_ctx(desc);
  175. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  176. struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
  177. struct sha512_state *octx = out;
  178. unsigned long irq_flags;
  179. spin_lock_irqsave(&nx_ctx->lock, irq_flags);
  180. /* move message_bit_length (128 bits) into count and convert its value
  181. * to bytes */
  182. octx->count[0] = csbcpb->cpb.sha512.message_bit_length_lo >> 3 |
  183. ((csbcpb->cpb.sha512.message_bit_length_hi & 7) << 61);
  184. octx->count[1] = csbcpb->cpb.sha512.message_bit_length_hi >> 3;
  185. octx->count[0] += sctx->count[0];
  186. if (octx->count[0] < sctx->count[0])
  187. octx->count[1]++;
  188. memcpy(octx->buf, sctx->buf, sizeof(octx->buf));
  189. /* if no data has been processed yet, we need to export SHA512's
  190. * initial data, in case this context gets imported into a software
  191. * context */
  192. if (csbcpb->cpb.sha512.message_bit_length_hi ||
  193. csbcpb->cpb.sha512.message_bit_length_lo)
  194. memcpy(octx->state, csbcpb->cpb.sha512.message_digest,
  195. SHA512_DIGEST_SIZE);
  196. else {
  197. octx->state[0] = SHA512_H0;
  198. octx->state[1] = SHA512_H1;
  199. octx->state[2] = SHA512_H2;
  200. octx->state[3] = SHA512_H3;
  201. octx->state[4] = SHA512_H4;
  202. octx->state[5] = SHA512_H5;
  203. octx->state[6] = SHA512_H6;
  204. octx->state[7] = SHA512_H7;
  205. }
  206. spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
  207. return 0;
  208. }
  209. static int nx_sha512_import(struct shash_desc *desc, const void *in)
  210. {
  211. struct sha512_state *sctx = shash_desc_ctx(desc);
  212. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  213. struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
  214. const struct sha512_state *ictx = in;
  215. unsigned long irq_flags;
  216. spin_lock_irqsave(&nx_ctx->lock, irq_flags);
  217. memcpy(sctx->buf, ictx->buf, sizeof(ictx->buf));
  218. sctx->count[0] = ictx->count[0] & 0x3f;
  219. csbcpb->cpb.sha512.message_bit_length_lo = (ictx->count[0] & ~0x3f)
  220. << 3;
  221. csbcpb->cpb.sha512.message_bit_length_hi = ictx->count[1] << 3 |
  222. ictx->count[0] >> 61;
  223. if (csbcpb->cpb.sha512.message_bit_length_hi ||
  224. csbcpb->cpb.sha512.message_bit_length_lo) {
  225. memcpy(csbcpb->cpb.sha512.message_digest, ictx->state,
  226. SHA512_DIGEST_SIZE);
  227. NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
  228. NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE;
  229. }
  230. spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
  231. return 0;
  232. }
  233. struct shash_alg nx_shash_sha512_alg = {
  234. .digestsize = SHA512_DIGEST_SIZE,
  235. .init = nx_sha512_init,
  236. .update = nx_sha512_update,
  237. .final = nx_sha512_final,
  238. .export = nx_sha512_export,
  239. .import = nx_sha512_import,
  240. .descsize = sizeof(struct sha512_state),
  241. .statesize = sizeof(struct sha512_state),
  242. .base = {
  243. .cra_name = "sha512",
  244. .cra_driver_name = "sha512-nx",
  245. .cra_priority = 300,
  246. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  247. .cra_blocksize = SHA512_BLOCK_SIZE,
  248. .cra_module = THIS_MODULE,
  249. .cra_ctxsize = sizeof(struct nx_crypto_ctx),
  250. .cra_init = nx_crypto_ctx_sha_init,
  251. .cra_exit = nx_crypto_ctx_exit,
  252. }
  253. };