nx-sha256.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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;
  49. int rc = 0;
  50. if (NX_CPB_FDM(csbcpb) & NX_FDM_CONTINUATION) {
  51. /* we've hit the nx chip previously and we're updating again,
  52. * so copy over the partial digest */
  53. memcpy(csbcpb->cpb.sha256.input_partial_digest,
  54. csbcpb->cpb.sha256.message_digest, SHA256_DIGEST_SIZE);
  55. }
  56. /* 2 cases for total data len:
  57. * 1: <= SHA256_BLOCK_SIZE: copy into state, return 0
  58. * 2: > SHA256_BLOCK_SIZE: process X blocks, copy in leftover
  59. */
  60. if (len + sctx->count <= SHA256_BLOCK_SIZE) {
  61. memcpy(sctx->buf + sctx->count, data, len);
  62. sctx->count += len;
  63. goto out;
  64. }
  65. /* to_process: the SHA256_BLOCK_SIZE data chunk to process in this
  66. * update */
  67. to_process = (sctx->count + len) & ~(SHA256_BLOCK_SIZE - 1);
  68. leftover = (sctx->count + len) & (SHA256_BLOCK_SIZE - 1);
  69. if (sctx->count) {
  70. in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *)sctx->buf,
  71. sctx->count, nx_ctx->ap->sglen);
  72. in_sg = nx_build_sg_list(in_sg, (u8 *)data,
  73. to_process - sctx->count,
  74. nx_ctx->ap->sglen);
  75. nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) *
  76. sizeof(struct nx_sg);
  77. } else {
  78. in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *)data,
  79. to_process, nx_ctx->ap->sglen);
  80. nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) *
  81. sizeof(struct nx_sg);
  82. }
  83. NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE;
  84. if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
  85. rc = -EINVAL;
  86. goto out;
  87. }
  88. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
  89. desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  90. if (rc)
  91. goto out;
  92. atomic_inc(&(nx_ctx->stats->sha256_ops));
  93. /* copy the leftover back into the state struct */
  94. memcpy(sctx->buf, data + len - leftover, leftover);
  95. sctx->count = leftover;
  96. csbcpb->cpb.sha256.message_bit_length += (u64)
  97. (csbcpb->cpb.sha256.spbc * 8);
  98. /* everything after the first update is continuation */
  99. NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
  100. out:
  101. return rc;
  102. }
  103. static int nx_sha256_final(struct shash_desc *desc, u8 *out)
  104. {
  105. struct sha256_state *sctx = shash_desc_ctx(desc);
  106. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  107. struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
  108. struct nx_sg *in_sg, *out_sg;
  109. int rc;
  110. if (NX_CPB_FDM(csbcpb) & NX_FDM_CONTINUATION) {
  111. /* we've hit the nx chip previously, now we're finalizing,
  112. * so copy over the partial digest */
  113. memcpy(csbcpb->cpb.sha256.input_partial_digest,
  114. csbcpb->cpb.sha256.message_digest, SHA256_DIGEST_SIZE);
  115. }
  116. /* final is represented by continuing the operation and indicating that
  117. * this is not an intermediate operation */
  118. NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
  119. csbcpb->cpb.sha256.message_bit_length += (u64)(sctx->count * 8);
  120. in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *)sctx->buf,
  121. sctx->count, nx_ctx->ap->sglen);
  122. out_sg = nx_build_sg_list(nx_ctx->out_sg, out, SHA256_DIGEST_SIZE,
  123. nx_ctx->ap->sglen);
  124. nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
  125. nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
  126. if (!nx_ctx->op.outlen) {
  127. rc = -EINVAL;
  128. goto out;
  129. }
  130. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
  131. desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  132. if (rc)
  133. goto out;
  134. atomic_inc(&(nx_ctx->stats->sha256_ops));
  135. atomic64_add(csbcpb->cpb.sha256.message_bit_length,
  136. &(nx_ctx->stats->sha256_bytes));
  137. memcpy(out, csbcpb->cpb.sha256.message_digest, SHA256_DIGEST_SIZE);
  138. out:
  139. return rc;
  140. }
  141. static int nx_sha256_export(struct shash_desc *desc, void *out)
  142. {
  143. struct sha256_state *sctx = shash_desc_ctx(desc);
  144. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  145. struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
  146. struct sha256_state *octx = out;
  147. octx->count = sctx->count +
  148. (csbcpb->cpb.sha256.message_bit_length / 8);
  149. memcpy(octx->buf, sctx->buf, sizeof(octx->buf));
  150. /* if no data has been processed yet, we need to export SHA256's
  151. * initial data, in case this context gets imported into a software
  152. * context */
  153. if (csbcpb->cpb.sha256.message_bit_length)
  154. memcpy(octx->state, csbcpb->cpb.sha256.message_digest,
  155. SHA256_DIGEST_SIZE);
  156. else {
  157. octx->state[0] = SHA256_H0;
  158. octx->state[1] = SHA256_H1;
  159. octx->state[2] = SHA256_H2;
  160. octx->state[3] = SHA256_H3;
  161. octx->state[4] = SHA256_H4;
  162. octx->state[5] = SHA256_H5;
  163. octx->state[6] = SHA256_H6;
  164. octx->state[7] = SHA256_H7;
  165. }
  166. return 0;
  167. }
  168. static int nx_sha256_import(struct shash_desc *desc, const void *in)
  169. {
  170. struct sha256_state *sctx = shash_desc_ctx(desc);
  171. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  172. struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
  173. const struct sha256_state *ictx = in;
  174. memcpy(sctx->buf, ictx->buf, sizeof(ictx->buf));
  175. sctx->count = ictx->count & 0x3f;
  176. csbcpb->cpb.sha256.message_bit_length = (ictx->count & ~0x3f) * 8;
  177. if (csbcpb->cpb.sha256.message_bit_length) {
  178. memcpy(csbcpb->cpb.sha256.message_digest, ictx->state,
  179. SHA256_DIGEST_SIZE);
  180. NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
  181. NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE;
  182. }
  183. return 0;
  184. }
  185. struct shash_alg nx_shash_sha256_alg = {
  186. .digestsize = SHA256_DIGEST_SIZE,
  187. .init = nx_sha256_init,
  188. .update = nx_sha256_update,
  189. .final = nx_sha256_final,
  190. .export = nx_sha256_export,
  191. .import = nx_sha256_import,
  192. .descsize = sizeof(struct sha256_state),
  193. .statesize = sizeof(struct sha256_state),
  194. .base = {
  195. .cra_name = "sha256",
  196. .cra_driver_name = "sha256-nx",
  197. .cra_priority = 300,
  198. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  199. .cra_blocksize = SHA256_BLOCK_SIZE,
  200. .cra_module = THIS_MODULE,
  201. .cra_ctxsize = sizeof(struct nx_crypto_ctx),
  202. .cra_init = nx_crypto_ctx_sha_init,
  203. .cra_exit = nx_crypto_ctx_exit,
  204. }
  205. };