nx-sha256.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 <asm/byteorder.h>
  26. #include "nx_csbcpb.h"
  27. #include "nx.h"
  28. static int nx_sha256_init(struct shash_desc *desc)
  29. {
  30. struct sha256_state *sctx = shash_desc_ctx(desc);
  31. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  32. struct nx_sg *out_sg;
  33. int len;
  34. u32 max_sg_len;
  35. nx_ctx_init(nx_ctx, HCOP_FC_SHA);
  36. memset(sctx, 0, sizeof *sctx);
  37. nx_ctx->ap = &nx_ctx->props[NX_PROPS_SHA256];
  38. NX_CPB_SET_DIGEST_SIZE(nx_ctx->csbcpb, NX_DS_SHA256);
  39. max_sg_len = min_t(u64, nx_ctx->ap->sglen,
  40. nx_driver.of.max_sg_len/sizeof(struct nx_sg));
  41. max_sg_len = min_t(u64, max_sg_len,
  42. nx_ctx->ap->databytelen/NX_PAGE_SIZE);
  43. len = SHA256_DIGEST_SIZE;
  44. out_sg = nx_build_sg_list(nx_ctx->out_sg, (u8 *)sctx->state,
  45. &len, max_sg_len);
  46. nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
  47. if (len != SHA256_DIGEST_SIZE)
  48. return -EINVAL;
  49. sctx->state[0] = __cpu_to_be32(SHA256_H0);
  50. sctx->state[1] = __cpu_to_be32(SHA256_H1);
  51. sctx->state[2] = __cpu_to_be32(SHA256_H2);
  52. sctx->state[3] = __cpu_to_be32(SHA256_H3);
  53. sctx->state[4] = __cpu_to_be32(SHA256_H4);
  54. sctx->state[5] = __cpu_to_be32(SHA256_H5);
  55. sctx->state[6] = __cpu_to_be32(SHA256_H6);
  56. sctx->state[7] = __cpu_to_be32(SHA256_H7);
  57. sctx->count = 0;
  58. return 0;
  59. }
  60. static int nx_sha256_update(struct shash_desc *desc, const u8 *data,
  61. unsigned int len)
  62. {
  63. struct sha256_state *sctx = shash_desc_ctx(desc);
  64. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  65. struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
  66. struct nx_sg *in_sg;
  67. u64 to_process = 0, leftover, total;
  68. unsigned long irq_flags;
  69. int rc = 0;
  70. int data_len;
  71. u32 max_sg_len;
  72. u64 buf_len = (sctx->count % SHA256_BLOCK_SIZE);
  73. spin_lock_irqsave(&nx_ctx->lock, irq_flags);
  74. /* 2 cases for total data len:
  75. * 1: < SHA256_BLOCK_SIZE: copy into state, return 0
  76. * 2: >= SHA256_BLOCK_SIZE: process X blocks, copy in leftover
  77. */
  78. total = (sctx->count % SHA256_BLOCK_SIZE) + len;
  79. if (total < SHA256_BLOCK_SIZE) {
  80. memcpy(sctx->buf + buf_len, data, len);
  81. sctx->count += len;
  82. goto out;
  83. }
  84. memcpy(csbcpb->cpb.sha256.message_digest, sctx->state, SHA256_DIGEST_SIZE);
  85. NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE;
  86. NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
  87. in_sg = nx_ctx->in_sg;
  88. max_sg_len = min_t(u64, nx_ctx->ap->sglen,
  89. nx_driver.of.max_sg_len/sizeof(struct nx_sg));
  90. max_sg_len = min_t(u64, max_sg_len,
  91. nx_ctx->ap->databytelen/NX_PAGE_SIZE);
  92. do {
  93. /*
  94. * to_process: the SHA256_BLOCK_SIZE data chunk to process in
  95. * this update. This value is also restricted by the sg list
  96. * limits.
  97. */
  98. to_process = total - to_process;
  99. to_process = to_process & ~(SHA256_BLOCK_SIZE - 1);
  100. if (buf_len) {
  101. data_len = buf_len;
  102. in_sg = nx_build_sg_list(nx_ctx->in_sg,
  103. (u8 *) sctx->buf,
  104. &data_len,
  105. max_sg_len);
  106. if (data_len != buf_len) {
  107. rc = -EINVAL;
  108. goto out;
  109. }
  110. }
  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,
  129. desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  130. if (rc)
  131. goto out;
  132. atomic_inc(&(nx_ctx->stats->sha256_ops));
  133. total -= to_process;
  134. data += to_process - buf_len;
  135. buf_len = 0;
  136. } while (leftover >= SHA256_BLOCK_SIZE);
  137. /* copy the leftover back into the state struct */
  138. if (leftover)
  139. memcpy(sctx->buf, data, leftover);
  140. sctx->count += len;
  141. memcpy(sctx->state, csbcpb->cpb.sha256.message_digest, SHA256_DIGEST_SIZE);
  142. out:
  143. spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
  144. return rc;
  145. }
  146. static int nx_sha256_final(struct shash_desc *desc, u8 *out)
  147. {
  148. struct sha256_state *sctx = shash_desc_ctx(desc);
  149. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  150. struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
  151. struct nx_sg *in_sg, *out_sg;
  152. unsigned long irq_flags;
  153. u32 max_sg_len;
  154. int rc = 0;
  155. int len;
  156. spin_lock_irqsave(&nx_ctx->lock, irq_flags);
  157. max_sg_len = min_t(u64, nx_ctx->ap->sglen,
  158. nx_driver.of.max_sg_len/sizeof(struct nx_sg));
  159. max_sg_len = min_t(u64, max_sg_len,
  160. nx_ctx->ap->databytelen/NX_PAGE_SIZE);
  161. /* final is represented by continuing the operation and indicating that
  162. * this is not an intermediate operation */
  163. if (sctx->count >= SHA256_BLOCK_SIZE) {
  164. /* we've hit the nx chip previously, now we're finalizing,
  165. * so copy over the partial digest */
  166. memcpy(csbcpb->cpb.sha256.input_partial_digest, sctx->state, SHA256_DIGEST_SIZE);
  167. NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
  168. NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
  169. } else {
  170. NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
  171. NX_CPB_FDM(csbcpb) &= ~NX_FDM_CONTINUATION;
  172. }
  173. csbcpb->cpb.sha256.message_bit_length = (u64) (sctx->count * 8);
  174. len = sctx->count & (SHA256_BLOCK_SIZE - 1);
  175. in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *) sctx->buf,
  176. &len, max_sg_len);
  177. if (len != (sctx->count & (SHA256_BLOCK_SIZE - 1))) {
  178. rc = -EINVAL;
  179. goto out;
  180. }
  181. len = SHA256_DIGEST_SIZE;
  182. out_sg = nx_build_sg_list(nx_ctx->out_sg, out, &len, max_sg_len);
  183. if (len != SHA256_DIGEST_SIZE) {
  184. rc = -EINVAL;
  185. goto out;
  186. }
  187. nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
  188. nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
  189. if (!nx_ctx->op.outlen) {
  190. rc = -EINVAL;
  191. goto out;
  192. }
  193. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
  194. desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  195. if (rc)
  196. goto out;
  197. atomic_inc(&(nx_ctx->stats->sha256_ops));
  198. atomic64_add(sctx->count, &(nx_ctx->stats->sha256_bytes));
  199. memcpy(out, csbcpb->cpb.sha256.message_digest, SHA256_DIGEST_SIZE);
  200. out:
  201. spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
  202. return rc;
  203. }
  204. static int nx_sha256_export(struct shash_desc *desc, void *out)
  205. {
  206. struct sha256_state *sctx = shash_desc_ctx(desc);
  207. memcpy(out, sctx, sizeof(*sctx));
  208. return 0;
  209. }
  210. static int nx_sha256_import(struct shash_desc *desc, const void *in)
  211. {
  212. struct sha256_state *sctx = shash_desc_ctx(desc);
  213. memcpy(sctx, in, sizeof(*sctx));
  214. return 0;
  215. }
  216. struct shash_alg nx_shash_sha256_alg = {
  217. .digestsize = SHA256_DIGEST_SIZE,
  218. .init = nx_sha256_init,
  219. .update = nx_sha256_update,
  220. .final = nx_sha256_final,
  221. .export = nx_sha256_export,
  222. .import = nx_sha256_import,
  223. .descsize = sizeof(struct sha256_state),
  224. .statesize = sizeof(struct sha256_state),
  225. .base = {
  226. .cra_name = "sha256",
  227. .cra_driver_name = "sha256-nx",
  228. .cra_priority = 300,
  229. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  230. .cra_blocksize = SHA256_BLOCK_SIZE,
  231. .cra_module = THIS_MODULE,
  232. .cra_ctxsize = sizeof(struct nx_crypto_ctx),
  233. .cra_init = nx_crypto_ctx_sha_init,
  234. .cra_exit = nx_crypto_ctx_exit,
  235. }
  236. };