nx-sha256.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * SHA-256 routines supporting the Power 7+ Nest Accelerators driver
  4. *
  5. * Copyright (C) 2011-2012 International Business Machines Inc.
  6. *
  7. * Author: Kent Yoder <yoder1@us.ibm.com>
  8. */
  9. #include <crypto/internal/hash.h>
  10. #include <crypto/sha2.h>
  11. #include <linux/module.h>
  12. #include <asm/vio.h>
  13. #include <asm/byteorder.h>
  14. #include "nx_csbcpb.h"
  15. #include "nx.h"
  16. struct sha256_state_be {
  17. __be32 state[SHA256_DIGEST_SIZE / 4];
  18. u64 count;
  19. u8 buf[SHA256_BLOCK_SIZE];
  20. };
  21. static int nx_crypto_ctx_sha256_init(struct crypto_tfm *tfm)
  22. {
  23. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(tfm);
  24. int err;
  25. err = nx_crypto_ctx_sha_init(tfm);
  26. if (err)
  27. return err;
  28. nx_ctx_init(nx_ctx, HCOP_FC_SHA);
  29. nx_ctx->ap = &nx_ctx->props[NX_PROPS_SHA256];
  30. NX_CPB_SET_DIGEST_SIZE(nx_ctx->csbcpb, NX_DS_SHA256);
  31. return 0;
  32. }
  33. static int nx_sha256_init(struct shash_desc *desc) {
  34. struct sha256_state_be *sctx = shash_desc_ctx(desc);
  35. memset(sctx, 0, sizeof *sctx);
  36. sctx->state[0] = __cpu_to_be32(SHA256_H0);
  37. sctx->state[1] = __cpu_to_be32(SHA256_H1);
  38. sctx->state[2] = __cpu_to_be32(SHA256_H2);
  39. sctx->state[3] = __cpu_to_be32(SHA256_H3);
  40. sctx->state[4] = __cpu_to_be32(SHA256_H4);
  41. sctx->state[5] = __cpu_to_be32(SHA256_H5);
  42. sctx->state[6] = __cpu_to_be32(SHA256_H6);
  43. sctx->state[7] = __cpu_to_be32(SHA256_H7);
  44. sctx->count = 0;
  45. return 0;
  46. }
  47. static int nx_sha256_update(struct shash_desc *desc, const u8 *data,
  48. unsigned int len)
  49. {
  50. struct sha256_state_be *sctx = shash_desc_ctx(desc);
  51. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  52. struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
  53. struct nx_sg *out_sg;
  54. u64 to_process = 0, leftover, total;
  55. unsigned long irq_flags;
  56. int rc = 0;
  57. int data_len;
  58. u32 max_sg_len;
  59. u64 buf_len = (sctx->count % SHA256_BLOCK_SIZE);
  60. spin_lock_irqsave(&nx_ctx->lock, irq_flags);
  61. /* 2 cases for total data len:
  62. * 1: < SHA256_BLOCK_SIZE: copy into state, return 0
  63. * 2: >= SHA256_BLOCK_SIZE: process X blocks, copy in leftover
  64. */
  65. total = (sctx->count % SHA256_BLOCK_SIZE) + len;
  66. if (total < SHA256_BLOCK_SIZE) {
  67. memcpy(sctx->buf + buf_len, data, len);
  68. sctx->count += len;
  69. goto out;
  70. }
  71. memcpy(csbcpb->cpb.sha256.message_digest, sctx->state, SHA256_DIGEST_SIZE);
  72. NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE;
  73. NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
  74. max_sg_len = min_t(u64, nx_ctx->ap->sglen,
  75. nx_driver.of.max_sg_len/sizeof(struct nx_sg));
  76. max_sg_len = min_t(u64, max_sg_len,
  77. nx_ctx->ap->databytelen/NX_PAGE_SIZE);
  78. data_len = SHA256_DIGEST_SIZE;
  79. out_sg = nx_build_sg_list(nx_ctx->out_sg, (u8 *)sctx->state,
  80. &data_len, max_sg_len);
  81. nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
  82. if (data_len != SHA256_DIGEST_SIZE) {
  83. rc = -EINVAL;
  84. goto out;
  85. }
  86. do {
  87. int used_sgs = 0;
  88. struct nx_sg *in_sg = nx_ctx->in_sg;
  89. if (buf_len) {
  90. data_len = buf_len;
  91. in_sg = nx_build_sg_list(in_sg,
  92. (u8 *) sctx->buf,
  93. &data_len,
  94. max_sg_len);
  95. if (data_len != buf_len) {
  96. rc = -EINVAL;
  97. goto out;
  98. }
  99. used_sgs = in_sg - nx_ctx->in_sg;
  100. }
  101. /* to_process: SHA256_BLOCK_SIZE aligned chunk to be
  102. * processed in this iteration. This value is restricted
  103. * by sg list limits and number of sgs we already used
  104. * for leftover data. (see above)
  105. * In ideal case, we could allow NX_PAGE_SIZE * max_sg_len,
  106. * but because data may not be aligned, we need to account
  107. * for that too. */
  108. to_process = min_t(u64, total,
  109. (max_sg_len - 1 - used_sgs) * NX_PAGE_SIZE);
  110. to_process = to_process & ~(SHA256_BLOCK_SIZE - 1);
  111. data_len = to_process - buf_len;
  112. in_sg = nx_build_sg_list(in_sg, (u8 *) data,
  113. &data_len, max_sg_len);
  114. nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
  115. to_process = data_len + buf_len;
  116. leftover = total - to_process;
  117. /*
  118. * we've hit the nx chip previously and we're updating
  119. * again, so copy over the partial digest.
  120. */
  121. memcpy(csbcpb->cpb.sha256.input_partial_digest,
  122. csbcpb->cpb.sha256.message_digest,
  123. SHA256_DIGEST_SIZE);
  124. if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
  125. rc = -EINVAL;
  126. goto out;
  127. }
  128. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, 0);
  129. if (rc)
  130. goto out;
  131. atomic_inc(&(nx_ctx->stats->sha256_ops));
  132. total -= to_process;
  133. data += to_process - buf_len;
  134. buf_len = 0;
  135. } while (leftover >= SHA256_BLOCK_SIZE);
  136. /* copy the leftover back into the state struct */
  137. if (leftover)
  138. memcpy(sctx->buf, data, leftover);
  139. sctx->count += len;
  140. memcpy(sctx->state, csbcpb->cpb.sha256.message_digest, SHA256_DIGEST_SIZE);
  141. out:
  142. spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
  143. return rc;
  144. }
  145. static int nx_sha256_final(struct shash_desc *desc, u8 *out)
  146. {
  147. struct sha256_state_be *sctx = shash_desc_ctx(desc);
  148. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  149. struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
  150. struct nx_sg *in_sg, *out_sg;
  151. unsigned long irq_flags;
  152. u32 max_sg_len;
  153. int rc = 0;
  154. int len;
  155. spin_lock_irqsave(&nx_ctx->lock, irq_flags);
  156. max_sg_len = min_t(u64, nx_ctx->ap->sglen,
  157. nx_driver.of.max_sg_len/sizeof(struct nx_sg));
  158. max_sg_len = min_t(u64, max_sg_len,
  159. nx_ctx->ap->databytelen/NX_PAGE_SIZE);
  160. /* final is represented by continuing the operation and indicating that
  161. * this is not an intermediate operation */
  162. if (sctx->count >= SHA256_BLOCK_SIZE) {
  163. /* we've hit the nx chip previously, now we're finalizing,
  164. * so copy over the partial digest */
  165. memcpy(csbcpb->cpb.sha256.input_partial_digest, sctx->state, SHA256_DIGEST_SIZE);
  166. NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
  167. NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
  168. } else {
  169. NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
  170. NX_CPB_FDM(csbcpb) &= ~NX_FDM_CONTINUATION;
  171. }
  172. csbcpb->cpb.sha256.message_bit_length = (u64) (sctx->count * 8);
  173. len = sctx->count & (SHA256_BLOCK_SIZE - 1);
  174. in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *) sctx->buf,
  175. &len, max_sg_len);
  176. if (len != (sctx->count & (SHA256_BLOCK_SIZE - 1))) {
  177. rc = -EINVAL;
  178. goto out;
  179. }
  180. len = SHA256_DIGEST_SIZE;
  181. out_sg = nx_build_sg_list(nx_ctx->out_sg, out, &len, max_sg_len);
  182. if (len != SHA256_DIGEST_SIZE) {
  183. rc = -EINVAL;
  184. goto out;
  185. }
  186. nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
  187. nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
  188. if (!nx_ctx->op.outlen) {
  189. rc = -EINVAL;
  190. goto out;
  191. }
  192. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, 0);
  193. if (rc)
  194. goto out;
  195. atomic_inc(&(nx_ctx->stats->sha256_ops));
  196. atomic64_add(sctx->count, &(nx_ctx->stats->sha256_bytes));
  197. memcpy(out, csbcpb->cpb.sha256.message_digest, SHA256_DIGEST_SIZE);
  198. out:
  199. spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
  200. return rc;
  201. }
  202. static int nx_sha256_export(struct shash_desc *desc, void *out)
  203. {
  204. struct sha256_state_be *sctx = shash_desc_ctx(desc);
  205. memcpy(out, sctx, sizeof(*sctx));
  206. return 0;
  207. }
  208. static int nx_sha256_import(struct shash_desc *desc, const void *in)
  209. {
  210. struct sha256_state_be *sctx = shash_desc_ctx(desc);
  211. memcpy(sctx, in, sizeof(*sctx));
  212. return 0;
  213. }
  214. struct shash_alg nx_shash_sha256_alg = {
  215. .digestsize = SHA256_DIGEST_SIZE,
  216. .init = nx_sha256_init,
  217. .update = nx_sha256_update,
  218. .final = nx_sha256_final,
  219. .export = nx_sha256_export,
  220. .import = nx_sha256_import,
  221. .descsize = sizeof(struct sha256_state_be),
  222. .statesize = sizeof(struct sha256_state_be),
  223. .base = {
  224. .cra_name = "sha256",
  225. .cra_driver_name = "sha256-nx",
  226. .cra_priority = 300,
  227. .cra_blocksize = SHA256_BLOCK_SIZE,
  228. .cra_module = THIS_MODULE,
  229. .cra_ctxsize = sizeof(struct nx_crypto_ctx),
  230. .cra_init = nx_crypto_ctx_sha256_init,
  231. .cra_exit = nx_crypto_ctx_exit,
  232. }
  233. };