nx-sha512.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * SHA-512 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 "nx_csbcpb.h"
  14. #include "nx.h"
  15. struct sha512_state_be {
  16. __be64 state[SHA512_DIGEST_SIZE / 8];
  17. u64 count[2];
  18. u8 buf[SHA512_BLOCK_SIZE];
  19. };
  20. static int nx_crypto_ctx_sha512_init(struct crypto_tfm *tfm)
  21. {
  22. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(tfm);
  23. int err;
  24. err = nx_crypto_ctx_sha_init(tfm);
  25. if (err)
  26. return err;
  27. nx_ctx_init(nx_ctx, HCOP_FC_SHA);
  28. nx_ctx->ap = &nx_ctx->props[NX_PROPS_SHA512];
  29. NX_CPB_SET_DIGEST_SIZE(nx_ctx->csbcpb, NX_DS_SHA512);
  30. return 0;
  31. }
  32. static int nx_sha512_init(struct shash_desc *desc)
  33. {
  34. struct sha512_state_be *sctx = shash_desc_ctx(desc);
  35. memset(sctx, 0, sizeof *sctx);
  36. sctx->state[0] = __cpu_to_be64(SHA512_H0);
  37. sctx->state[1] = __cpu_to_be64(SHA512_H1);
  38. sctx->state[2] = __cpu_to_be64(SHA512_H2);
  39. sctx->state[3] = __cpu_to_be64(SHA512_H3);
  40. sctx->state[4] = __cpu_to_be64(SHA512_H4);
  41. sctx->state[5] = __cpu_to_be64(SHA512_H5);
  42. sctx->state[6] = __cpu_to_be64(SHA512_H6);
  43. sctx->state[7] = __cpu_to_be64(SHA512_H7);
  44. sctx->count[0] = 0;
  45. return 0;
  46. }
  47. static int nx_sha512_update(struct shash_desc *desc, const u8 *data,
  48. unsigned int len)
  49. {
  50. struct sha512_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, leftover = 0, 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[0] % SHA512_BLOCK_SIZE);
  60. spin_lock_irqsave(&nx_ctx->lock, irq_flags);
  61. /* 2 cases for total data len:
  62. * 1: < SHA512_BLOCK_SIZE: copy into state, return 0
  63. * 2: >= SHA512_BLOCK_SIZE: process X blocks, copy in leftover
  64. */
  65. total = (sctx->count[0] % SHA512_BLOCK_SIZE) + len;
  66. if (total < SHA512_BLOCK_SIZE) {
  67. memcpy(sctx->buf + buf_len, data, len);
  68. sctx->count[0] += len;
  69. goto out;
  70. }
  71. memcpy(csbcpb->cpb.sha512.message_digest, sctx->state, SHA512_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 = SHA512_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 != SHA512_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, max_sg_len);
  94. if (data_len != buf_len) {
  95. rc = -EINVAL;
  96. goto out;
  97. }
  98. used_sgs = in_sg - nx_ctx->in_sg;
  99. }
  100. /* to_process: SHA512_BLOCK_SIZE aligned chunk to be
  101. * processed in this iteration. This value is restricted
  102. * by sg list limits and number of sgs we already used
  103. * for leftover data. (see above)
  104. * In ideal case, we could allow NX_PAGE_SIZE * max_sg_len,
  105. * but because data may not be aligned, we need to account
  106. * for that too. */
  107. to_process = min_t(u64, total,
  108. (max_sg_len - 1 - used_sgs) * NX_PAGE_SIZE);
  109. to_process = to_process & ~(SHA512_BLOCK_SIZE - 1);
  110. data_len = to_process - buf_len;
  111. in_sg = nx_build_sg_list(in_sg, (u8 *) data,
  112. &data_len, max_sg_len);
  113. nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
  114. if (data_len != (to_process - buf_len)) {
  115. rc = -EINVAL;
  116. goto out;
  117. }
  118. to_process = data_len + buf_len;
  119. leftover = total - to_process;
  120. /*
  121. * we've hit the nx chip previously and we're updating
  122. * again, so copy over the partial digest.
  123. */
  124. memcpy(csbcpb->cpb.sha512.input_partial_digest,
  125. csbcpb->cpb.sha512.message_digest,
  126. SHA512_DIGEST_SIZE);
  127. if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
  128. rc = -EINVAL;
  129. goto out;
  130. }
  131. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, 0);
  132. if (rc)
  133. goto out;
  134. atomic_inc(&(nx_ctx->stats->sha512_ops));
  135. total -= to_process;
  136. data += to_process - buf_len;
  137. buf_len = 0;
  138. } while (leftover >= SHA512_BLOCK_SIZE);
  139. /* copy the leftover back into the state struct */
  140. if (leftover)
  141. memcpy(sctx->buf, data, leftover);
  142. sctx->count[0] += len;
  143. memcpy(sctx->state, csbcpb->cpb.sha512.message_digest, SHA512_DIGEST_SIZE);
  144. out:
  145. spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
  146. return rc;
  147. }
  148. static int nx_sha512_final(struct shash_desc *desc, u8 *out)
  149. {
  150. struct sha512_state_be *sctx = shash_desc_ctx(desc);
  151. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  152. struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
  153. struct nx_sg *in_sg, *out_sg;
  154. u32 max_sg_len;
  155. u64 count0;
  156. unsigned long irq_flags;
  157. int rc = 0;
  158. int len;
  159. spin_lock_irqsave(&nx_ctx->lock, irq_flags);
  160. max_sg_len = min_t(u64, nx_ctx->ap->sglen,
  161. nx_driver.of.max_sg_len/sizeof(struct nx_sg));
  162. max_sg_len = min_t(u64, max_sg_len,
  163. nx_ctx->ap->databytelen/NX_PAGE_SIZE);
  164. /* final is represented by continuing the operation and indicating that
  165. * this is not an intermediate operation */
  166. if (sctx->count[0] >= SHA512_BLOCK_SIZE) {
  167. /* we've hit the nx chip previously, now we're finalizing,
  168. * so copy over the partial digest */
  169. memcpy(csbcpb->cpb.sha512.input_partial_digest, sctx->state,
  170. SHA512_DIGEST_SIZE);
  171. NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
  172. NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
  173. } else {
  174. NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
  175. NX_CPB_FDM(csbcpb) &= ~NX_FDM_CONTINUATION;
  176. }
  177. NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
  178. count0 = sctx->count[0] * 8;
  179. csbcpb->cpb.sha512.message_bit_length_lo = count0;
  180. len = sctx->count[0] & (SHA512_BLOCK_SIZE - 1);
  181. in_sg = nx_build_sg_list(nx_ctx->in_sg, sctx->buf, &len,
  182. max_sg_len);
  183. if (len != (sctx->count[0] & (SHA512_BLOCK_SIZE - 1))) {
  184. rc = -EINVAL;
  185. goto out;
  186. }
  187. len = SHA512_DIGEST_SIZE;
  188. out_sg = nx_build_sg_list(nx_ctx->out_sg, out, &len,
  189. max_sg_len);
  190. nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
  191. nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
  192. if (!nx_ctx->op.outlen) {
  193. rc = -EINVAL;
  194. goto out;
  195. }
  196. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, 0);
  197. if (rc)
  198. goto out;
  199. atomic_inc(&(nx_ctx->stats->sha512_ops));
  200. atomic64_add(sctx->count[0], &(nx_ctx->stats->sha512_bytes));
  201. memcpy(out, csbcpb->cpb.sha512.message_digest, SHA512_DIGEST_SIZE);
  202. out:
  203. spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
  204. return rc;
  205. }
  206. static int nx_sha512_export(struct shash_desc *desc, void *out)
  207. {
  208. struct sha512_state_be *sctx = shash_desc_ctx(desc);
  209. memcpy(out, sctx, sizeof(*sctx));
  210. return 0;
  211. }
  212. static int nx_sha512_import(struct shash_desc *desc, const void *in)
  213. {
  214. struct sha512_state_be *sctx = shash_desc_ctx(desc);
  215. memcpy(sctx, in, sizeof(*sctx));
  216. return 0;
  217. }
  218. struct shash_alg nx_shash_sha512_alg = {
  219. .digestsize = SHA512_DIGEST_SIZE,
  220. .init = nx_sha512_init,
  221. .update = nx_sha512_update,
  222. .final = nx_sha512_final,
  223. .export = nx_sha512_export,
  224. .import = nx_sha512_import,
  225. .descsize = sizeof(struct sha512_state_be),
  226. .statesize = sizeof(struct sha512_state_be),
  227. .base = {
  228. .cra_name = "sha512",
  229. .cra_driver_name = "sha512-nx",
  230. .cra_priority = 300,
  231. .cra_blocksize = SHA512_BLOCK_SIZE,
  232. .cra_module = THIS_MODULE,
  233. .cra_ctxsize = sizeof(struct nx_crypto_ctx),
  234. .cra_init = nx_crypto_ctx_sha512_init,
  235. .cra_exit = nx_crypto_ctx_exit,
  236. }
  237. };