nx-sha256.c 7.9 KB

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