nx-sha256.c 8.3 KB

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