nx-sha256.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /**
  2. * SHA-256 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_sha256_init(struct shash_desc *desc)
  28. {
  29. struct sha256_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_SHA256];
  35. NX_CPB_SET_DIGEST_SIZE(nx_ctx->csbcpb, NX_DS_SHA256);
  36. out_sg = nx_build_sg_list(nx_ctx->out_sg, (u8 *)sctx->state,
  37. SHA256_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_sha256_update(struct shash_desc *desc, const u8 *data,
  42. unsigned int len)
  43. {
  44. struct sha256_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;
  49. u32 max_sg_len;
  50. int rc = 0;
  51. /* 2 cases for total data len:
  52. * 1: < SHA256_BLOCK_SIZE: copy into state, return 0
  53. * 2: >= SHA256_BLOCK_SIZE: process X blocks, copy in leftover
  54. */
  55. total = sctx->count + len;
  56. if (total < SHA256_BLOCK_SIZE) {
  57. memcpy(sctx->buf + sctx->count, data, len);
  58. sctx->count += 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 SHA256_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 & ~(SHA256_BLOCK_SIZE - 1);
  74. leftover = total - to_process;
  75. if (sctx->count) {
  76. in_sg = nx_build_sg_list(nx_ctx->in_sg,
  77. (u8 *) sctx->buf,
  78. sctx->count, max_sg_len);
  79. }
  80. in_sg = nx_build_sg_list(in_sg, (u8 *) data,
  81. to_process - sctx->count,
  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.sha256.input_partial_digest,
  91. csbcpb->cpb.sha256.message_digest,
  92. SHA256_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->sha256_ops));
  104. csbcpb->cpb.sha256.message_bit_length += (u64)
  105. (csbcpb->cpb.sha256.spbc * 8);
  106. /* everything after the first update is continuation */
  107. NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
  108. total -= to_process;
  109. data += to_process;
  110. sctx->count = 0;
  111. in_sg = nx_ctx->in_sg;
  112. } while (leftover >= SHA256_BLOCK_SIZE);
  113. /* copy the leftover back into the state struct */
  114. if (leftover)
  115. memcpy(sctx->buf, data, leftover);
  116. sctx->count = leftover;
  117. out:
  118. return rc;
  119. }
  120. static int nx_sha256_final(struct shash_desc *desc, u8 *out)
  121. {
  122. struct sha256_state *sctx = shash_desc_ctx(desc);
  123. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  124. struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
  125. struct nx_sg *in_sg, *out_sg;
  126. u32 max_sg_len;
  127. int rc;
  128. max_sg_len = min_t(u32, nx_driver.of.max_sg_len, nx_ctx->ap->sglen);
  129. if (NX_CPB_FDM(csbcpb) & NX_FDM_CONTINUATION) {
  130. /* we've hit the nx chip previously, now we're finalizing,
  131. * so copy over the partial digest */
  132. memcpy(csbcpb->cpb.sha256.input_partial_digest,
  133. csbcpb->cpb.sha256.message_digest, SHA256_DIGEST_SIZE);
  134. }
  135. /* final is represented by continuing the operation and indicating that
  136. * this is not an intermediate operation */
  137. NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
  138. csbcpb->cpb.sha256.message_bit_length += (u64)(sctx->count * 8);
  139. in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *)sctx->buf,
  140. sctx->count, max_sg_len);
  141. out_sg = nx_build_sg_list(nx_ctx->out_sg, out, SHA256_DIGEST_SIZE,
  142. max_sg_len);
  143. nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
  144. nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
  145. if (!nx_ctx->op.outlen) {
  146. rc = -EINVAL;
  147. goto out;
  148. }
  149. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
  150. desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  151. if (rc)
  152. goto out;
  153. atomic_inc(&(nx_ctx->stats->sha256_ops));
  154. atomic64_add(csbcpb->cpb.sha256.message_bit_length / 8,
  155. &(nx_ctx->stats->sha256_bytes));
  156. memcpy(out, csbcpb->cpb.sha256.message_digest, SHA256_DIGEST_SIZE);
  157. out:
  158. return rc;
  159. }
  160. static int nx_sha256_export(struct shash_desc *desc, void *out)
  161. {
  162. struct sha256_state *sctx = shash_desc_ctx(desc);
  163. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  164. struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
  165. struct sha256_state *octx = out;
  166. octx->count = sctx->count +
  167. (csbcpb->cpb.sha256.message_bit_length / 8);
  168. memcpy(octx->buf, sctx->buf, sizeof(octx->buf));
  169. /* if no data has been processed yet, we need to export SHA256's
  170. * initial data, in case this context gets imported into a software
  171. * context */
  172. if (csbcpb->cpb.sha256.message_bit_length)
  173. memcpy(octx->state, csbcpb->cpb.sha256.message_digest,
  174. SHA256_DIGEST_SIZE);
  175. else {
  176. octx->state[0] = SHA256_H0;
  177. octx->state[1] = SHA256_H1;
  178. octx->state[2] = SHA256_H2;
  179. octx->state[3] = SHA256_H3;
  180. octx->state[4] = SHA256_H4;
  181. octx->state[5] = SHA256_H5;
  182. octx->state[6] = SHA256_H6;
  183. octx->state[7] = SHA256_H7;
  184. }
  185. return 0;
  186. }
  187. static int nx_sha256_import(struct shash_desc *desc, const void *in)
  188. {
  189. struct sha256_state *sctx = shash_desc_ctx(desc);
  190. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  191. struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
  192. const struct sha256_state *ictx = in;
  193. memcpy(sctx->buf, ictx->buf, sizeof(ictx->buf));
  194. sctx->count = ictx->count & 0x3f;
  195. csbcpb->cpb.sha256.message_bit_length = (ictx->count & ~0x3f) * 8;
  196. if (csbcpb->cpb.sha256.message_bit_length) {
  197. memcpy(csbcpb->cpb.sha256.message_digest, ictx->state,
  198. SHA256_DIGEST_SIZE);
  199. NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
  200. NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE;
  201. }
  202. return 0;
  203. }
  204. struct shash_alg nx_shash_sha256_alg = {
  205. .digestsize = SHA256_DIGEST_SIZE,
  206. .init = nx_sha256_init,
  207. .update = nx_sha256_update,
  208. .final = nx_sha256_final,
  209. .export = nx_sha256_export,
  210. .import = nx_sha256_import,
  211. .descsize = sizeof(struct sha256_state),
  212. .statesize = sizeof(struct sha256_state),
  213. .base = {
  214. .cra_name = "sha256",
  215. .cra_driver_name = "sha256-nx",
  216. .cra_priority = 300,
  217. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  218. .cra_blocksize = SHA256_BLOCK_SIZE,
  219. .cra_module = THIS_MODULE,
  220. .cra_ctxsize = sizeof(struct nx_crypto_ctx),
  221. .cra_init = nx_crypto_ctx_sha_init,
  222. .cra_exit = nx_crypto_ctx_exit,
  223. }
  224. };