nx-sha512.c 8.2 KB

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