nx-sha512.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /**
  2. * SHA-512 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_sha512_init(struct shash_desc *desc)
  28. {
  29. struct sha512_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_SHA512];
  35. NX_CPB_SET_DIGEST_SIZE(nx_ctx->csbcpb, NX_DS_SHA512);
  36. out_sg = nx_build_sg_list(nx_ctx->out_sg, (u8 *)sctx->state,
  37. SHA512_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_sha512_update(struct shash_desc *desc, const u8 *data,
  42. unsigned int len)
  43. {
  44. struct sha512_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, spbc_bits;
  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.sha512.input_partial_digest,
  54. csbcpb->cpb.sha512.message_digest, SHA512_DIGEST_SIZE);
  55. }
  56. /* 2 cases for total data len:
  57. * 1: <= SHA512_BLOCK_SIZE: copy into state, return 0
  58. * 2: > SHA512_BLOCK_SIZE: process X blocks, copy in leftover
  59. */
  60. if ((u64)len + sctx->count[0] <= SHA512_BLOCK_SIZE) {
  61. memcpy(sctx->buf + sctx->count[0], data, len);
  62. sctx->count[0] += len;
  63. goto out;
  64. }
  65. /* to_process: the SHA512_BLOCK_SIZE data chunk to process in this
  66. * update */
  67. to_process = (sctx->count[0] + len) & ~(SHA512_BLOCK_SIZE - 1);
  68. leftover = (sctx->count[0] + len) & (SHA512_BLOCK_SIZE - 1);
  69. if (sctx->count[0]) {
  70. in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *)sctx->buf,
  71. sctx->count[0], nx_ctx->ap->sglen);
  72. in_sg = nx_build_sg_list(in_sg, (u8 *)data,
  73. to_process - sctx->count[0],
  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->sha512_ops));
  93. /* copy the leftover back into the state struct */
  94. memcpy(sctx->buf, data + len - leftover, leftover);
  95. sctx->count[0] = leftover;
  96. spbc_bits = csbcpb->cpb.sha512.spbc * 8;
  97. csbcpb->cpb.sha512.message_bit_length_lo += spbc_bits;
  98. if (csbcpb->cpb.sha512.message_bit_length_lo < spbc_bits)
  99. csbcpb->cpb.sha512.message_bit_length_hi++;
  100. /* everything after the first update is continuation */
  101. NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
  102. out:
  103. return rc;
  104. }
  105. static int nx_sha512_final(struct shash_desc *desc, u8 *out)
  106. {
  107. struct sha512_state *sctx = shash_desc_ctx(desc);
  108. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  109. struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
  110. struct nx_sg *in_sg, *out_sg;
  111. u64 count0;
  112. int rc;
  113. if (NX_CPB_FDM(csbcpb) & NX_FDM_CONTINUATION) {
  114. /* we've hit the nx chip previously, now we're finalizing,
  115. * so copy over the partial digest */
  116. memcpy(csbcpb->cpb.sha512.input_partial_digest,
  117. csbcpb->cpb.sha512.message_digest, SHA512_DIGEST_SIZE);
  118. }
  119. /* final is represented by continuing the operation and indicating that
  120. * this is not an intermediate operation */
  121. NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
  122. count0 = sctx->count[0] * 8;
  123. csbcpb->cpb.sha512.message_bit_length_lo += count0;
  124. if (csbcpb->cpb.sha512.message_bit_length_lo < count0)
  125. csbcpb->cpb.sha512.message_bit_length_hi++;
  126. in_sg = nx_build_sg_list(nx_ctx->in_sg, sctx->buf, sctx->count[0],
  127. nx_ctx->ap->sglen);
  128. out_sg = nx_build_sg_list(nx_ctx->out_sg, out, SHA512_DIGEST_SIZE,
  129. nx_ctx->ap->sglen);
  130. nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
  131. nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
  132. if (!nx_ctx->op.outlen) {
  133. rc = -EINVAL;
  134. goto out;
  135. }
  136. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
  137. desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  138. if (rc)
  139. goto out;
  140. atomic_inc(&(nx_ctx->stats->sha512_ops));
  141. atomic64_add(csbcpb->cpb.sha512.message_bit_length_lo,
  142. &(nx_ctx->stats->sha512_bytes));
  143. memcpy(out, csbcpb->cpb.sha512.message_digest, SHA512_DIGEST_SIZE);
  144. out:
  145. return rc;
  146. }
  147. static int nx_sha512_export(struct shash_desc *desc, void *out)
  148. {
  149. struct sha512_state *sctx = shash_desc_ctx(desc);
  150. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  151. struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
  152. struct sha512_state *octx = out;
  153. /* move message_bit_length (128 bits) into count and convert its value
  154. * to bytes */
  155. octx->count[0] = csbcpb->cpb.sha512.message_bit_length_lo >> 3 |
  156. ((csbcpb->cpb.sha512.message_bit_length_hi & 7) << 61);
  157. octx->count[1] = csbcpb->cpb.sha512.message_bit_length_hi >> 3;
  158. octx->count[0] += sctx->count[0];
  159. if (octx->count[0] < sctx->count[0])
  160. octx->count[1]++;
  161. memcpy(octx->buf, sctx->buf, sizeof(octx->buf));
  162. /* if no data has been processed yet, we need to export SHA512's
  163. * initial data, in case this context gets imported into a software
  164. * context */
  165. if (csbcpb->cpb.sha512.message_bit_length_hi ||
  166. csbcpb->cpb.sha512.message_bit_length_lo)
  167. memcpy(octx->state, csbcpb->cpb.sha512.message_digest,
  168. SHA512_DIGEST_SIZE);
  169. else {
  170. octx->state[0] = SHA512_H0;
  171. octx->state[1] = SHA512_H1;
  172. octx->state[2] = SHA512_H2;
  173. octx->state[3] = SHA512_H3;
  174. octx->state[4] = SHA512_H4;
  175. octx->state[5] = SHA512_H5;
  176. octx->state[6] = SHA512_H6;
  177. octx->state[7] = SHA512_H7;
  178. }
  179. return 0;
  180. }
  181. static int nx_sha512_import(struct shash_desc *desc, const void *in)
  182. {
  183. struct sha512_state *sctx = shash_desc_ctx(desc);
  184. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  185. struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
  186. const struct sha512_state *ictx = in;
  187. memcpy(sctx->buf, ictx->buf, sizeof(ictx->buf));
  188. sctx->count[0] = ictx->count[0] & 0x3f;
  189. csbcpb->cpb.sha512.message_bit_length_lo = (ictx->count[0] & ~0x3f)
  190. << 3;
  191. csbcpb->cpb.sha512.message_bit_length_hi = ictx->count[1] << 3 |
  192. ictx->count[0] >> 61;
  193. if (csbcpb->cpb.sha512.message_bit_length_hi ||
  194. csbcpb->cpb.sha512.message_bit_length_lo) {
  195. memcpy(csbcpb->cpb.sha512.message_digest, ictx->state,
  196. SHA512_DIGEST_SIZE);
  197. NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
  198. NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE;
  199. }
  200. return 0;
  201. }
  202. struct shash_alg nx_shash_sha512_alg = {
  203. .digestsize = SHA512_DIGEST_SIZE,
  204. .init = nx_sha512_init,
  205. .update = nx_sha512_update,
  206. .final = nx_sha512_final,
  207. .export = nx_sha512_export,
  208. .import = nx_sha512_import,
  209. .descsize = sizeof(struct sha512_state),
  210. .statesize = sizeof(struct sha512_state),
  211. .base = {
  212. .cra_name = "sha512",
  213. .cra_driver_name = "sha512-nx",
  214. .cra_priority = 300,
  215. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  216. .cra_blocksize = SHA512_BLOCK_SIZE,
  217. .cra_module = THIS_MODULE,
  218. .cra_ctxsize = sizeof(struct nx_crypto_ctx),
  219. .cra_init = nx_crypto_ctx_sha_init,
  220. .cra_exit = nx_crypto_ctx_exit,
  221. }
  222. };