nx-sha512.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. int rc = 0;
  51. /* 2 cases for total data len:
  52. * 1: < SHA512_BLOCK_SIZE: copy into state, return 0
  53. * 2: >= SHA512_BLOCK_SIZE: process X blocks, copy in leftover
  54. */
  55. total = sctx->count[0] + len;
  56. if (total < SHA512_BLOCK_SIZE) {
  57. memcpy(sctx->buf + sctx->count[0], data, len);
  58. sctx->count[0] += len;
  59. goto out;
  60. }
  61. in_sg = nx_ctx->in_sg;
  62. max_sg_len = min_t(u32, nx_driver.of.max_sg_len/sizeof(struct nx_sg),
  63. nx_ctx->ap->sglen);
  64. do {
  65. /*
  66. * to_process: the SHA512_BLOCK_SIZE data chunk to process in
  67. * this update. This value is also restricted by the sg list
  68. * limits.
  69. */
  70. to_process = min_t(u64, total, nx_ctx->ap->databytelen);
  71. to_process = min_t(u64, to_process,
  72. NX_PAGE_SIZE * (max_sg_len - 1));
  73. to_process = to_process & ~(SHA512_BLOCK_SIZE - 1);
  74. leftover = total - to_process;
  75. if (sctx->count[0]) {
  76. in_sg = nx_build_sg_list(nx_ctx->in_sg,
  77. (u8 *) sctx->buf,
  78. sctx->count[0], max_sg_len);
  79. }
  80. in_sg = nx_build_sg_list(in_sg, (u8 *) data,
  81. to_process - sctx->count[0],
  82. max_sg_len);
  83. nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) *
  84. sizeof(struct nx_sg);
  85. if (NX_CPB_FDM(csbcpb) & NX_FDM_CONTINUATION) {
  86. /*
  87. * we've hit the nx chip previously and we're updating
  88. * again, so copy over the partial digest.
  89. */
  90. memcpy(csbcpb->cpb.sha512.input_partial_digest,
  91. csbcpb->cpb.sha512.message_digest,
  92. SHA512_DIGEST_SIZE);
  93. }
  94. NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE;
  95. if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
  96. rc = -EINVAL;
  97. goto out;
  98. }
  99. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
  100. desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  101. if (rc)
  102. goto out;
  103. atomic_inc(&(nx_ctx->stats->sha512_ops));
  104. spbc_bits = csbcpb->cpb.sha512.spbc * 8;
  105. csbcpb->cpb.sha512.message_bit_length_lo += spbc_bits;
  106. if (csbcpb->cpb.sha512.message_bit_length_lo < spbc_bits)
  107. csbcpb->cpb.sha512.message_bit_length_hi++;
  108. /* everything after the first update is continuation */
  109. NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
  110. total -= to_process;
  111. data += to_process;
  112. sctx->count[0] = 0;
  113. in_sg = nx_ctx->in_sg;
  114. } while (leftover >= SHA512_BLOCK_SIZE);
  115. /* copy the leftover back into the state struct */
  116. if (leftover)
  117. memcpy(sctx->buf, data, leftover);
  118. sctx->count[0] = leftover;
  119. out:
  120. return rc;
  121. }
  122. static int nx_sha512_final(struct shash_desc *desc, u8 *out)
  123. {
  124. struct sha512_state *sctx = shash_desc_ctx(desc);
  125. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  126. struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
  127. struct nx_sg *in_sg, *out_sg;
  128. u32 max_sg_len;
  129. u64 count0;
  130. int rc;
  131. max_sg_len = min_t(u32, nx_driver.of.max_sg_len, nx_ctx->ap->sglen);
  132. if (NX_CPB_FDM(csbcpb) & NX_FDM_CONTINUATION) {
  133. /* we've hit the nx chip previously, now we're finalizing,
  134. * so copy over the partial digest */
  135. memcpy(csbcpb->cpb.sha512.input_partial_digest,
  136. csbcpb->cpb.sha512.message_digest, SHA512_DIGEST_SIZE);
  137. }
  138. /* final is represented by continuing the operation and indicating that
  139. * this is not an intermediate operation */
  140. NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
  141. count0 = sctx->count[0] * 8;
  142. csbcpb->cpb.sha512.message_bit_length_lo += count0;
  143. if (csbcpb->cpb.sha512.message_bit_length_lo < count0)
  144. csbcpb->cpb.sha512.message_bit_length_hi++;
  145. in_sg = nx_build_sg_list(nx_ctx->in_sg, sctx->buf, sctx->count[0],
  146. max_sg_len);
  147. out_sg = nx_build_sg_list(nx_ctx->out_sg, out, SHA512_DIGEST_SIZE,
  148. max_sg_len);
  149. nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
  150. nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
  151. if (!nx_ctx->op.outlen) {
  152. rc = -EINVAL;
  153. goto out;
  154. }
  155. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
  156. desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  157. if (rc)
  158. goto out;
  159. atomic_inc(&(nx_ctx->stats->sha512_ops));
  160. atomic64_add(csbcpb->cpb.sha512.message_bit_length_lo / 8,
  161. &(nx_ctx->stats->sha512_bytes));
  162. memcpy(out, csbcpb->cpb.sha512.message_digest, SHA512_DIGEST_SIZE);
  163. out:
  164. return rc;
  165. }
  166. static int nx_sha512_export(struct shash_desc *desc, void *out)
  167. {
  168. struct sha512_state *sctx = shash_desc_ctx(desc);
  169. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  170. struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
  171. struct sha512_state *octx = out;
  172. /* move message_bit_length (128 bits) into count and convert its value
  173. * to bytes */
  174. octx->count[0] = csbcpb->cpb.sha512.message_bit_length_lo >> 3 |
  175. ((csbcpb->cpb.sha512.message_bit_length_hi & 7) << 61);
  176. octx->count[1] = csbcpb->cpb.sha512.message_bit_length_hi >> 3;
  177. octx->count[0] += sctx->count[0];
  178. if (octx->count[0] < sctx->count[0])
  179. octx->count[1]++;
  180. memcpy(octx->buf, sctx->buf, sizeof(octx->buf));
  181. /* if no data has been processed yet, we need to export SHA512's
  182. * initial data, in case this context gets imported into a software
  183. * context */
  184. if (csbcpb->cpb.sha512.message_bit_length_hi ||
  185. csbcpb->cpb.sha512.message_bit_length_lo)
  186. memcpy(octx->state, csbcpb->cpb.sha512.message_digest,
  187. SHA512_DIGEST_SIZE);
  188. else {
  189. octx->state[0] = SHA512_H0;
  190. octx->state[1] = SHA512_H1;
  191. octx->state[2] = SHA512_H2;
  192. octx->state[3] = SHA512_H3;
  193. octx->state[4] = SHA512_H4;
  194. octx->state[5] = SHA512_H5;
  195. octx->state[6] = SHA512_H6;
  196. octx->state[7] = SHA512_H7;
  197. }
  198. return 0;
  199. }
  200. static int nx_sha512_import(struct shash_desc *desc, const void *in)
  201. {
  202. struct sha512_state *sctx = shash_desc_ctx(desc);
  203. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  204. struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
  205. const struct sha512_state *ictx = in;
  206. memcpy(sctx->buf, ictx->buf, sizeof(ictx->buf));
  207. sctx->count[0] = ictx->count[0] & 0x3f;
  208. csbcpb->cpb.sha512.message_bit_length_lo = (ictx->count[0] & ~0x3f)
  209. << 3;
  210. csbcpb->cpb.sha512.message_bit_length_hi = ictx->count[1] << 3 |
  211. ictx->count[0] >> 61;
  212. if (csbcpb->cpb.sha512.message_bit_length_hi ||
  213. csbcpb->cpb.sha512.message_bit_length_lo) {
  214. memcpy(csbcpb->cpb.sha512.message_digest, ictx->state,
  215. SHA512_DIGEST_SIZE);
  216. NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
  217. NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE;
  218. }
  219. return 0;
  220. }
  221. struct shash_alg nx_shash_sha512_alg = {
  222. .digestsize = SHA512_DIGEST_SIZE,
  223. .init = nx_sha512_init,
  224. .update = nx_sha512_update,
  225. .final = nx_sha512_final,
  226. .export = nx_sha512_export,
  227. .import = nx_sha512_import,
  228. .descsize = sizeof(struct sha512_state),
  229. .statesize = sizeof(struct sha512_state),
  230. .base = {
  231. .cra_name = "sha512",
  232. .cra_driver_name = "sha512-nx",
  233. .cra_priority = 300,
  234. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  235. .cra_blocksize = SHA512_BLOCK_SIZE,
  236. .cra_module = THIS_MODULE,
  237. .cra_ctxsize = sizeof(struct nx_crypto_ctx),
  238. .cra_init = nx_crypto_ctx_sha_init,
  239. .cra_exit = nx_crypto_ctx_exit,
  240. }
  241. };