nx-sha256.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 *out_sg;
  61. u64 to_process = 0, leftover, total;
  62. unsigned long irq_flags;
  63. int rc = 0;
  64. int data_len;
  65. u32 max_sg_len;
  66. u64 buf_len = (sctx->count % SHA256_BLOCK_SIZE);
  67. spin_lock_irqsave(&nx_ctx->lock, irq_flags);
  68. /* 2 cases for total data len:
  69. * 1: < SHA256_BLOCK_SIZE: copy into state, return 0
  70. * 2: >= SHA256_BLOCK_SIZE: process X blocks, copy in leftover
  71. */
  72. total = (sctx->count % SHA256_BLOCK_SIZE) + len;
  73. if (total < SHA256_BLOCK_SIZE) {
  74. memcpy(sctx->buf + buf_len, data, len);
  75. sctx->count += len;
  76. goto out;
  77. }
  78. memcpy(csbcpb->cpb.sha256.message_digest, sctx->state, SHA256_DIGEST_SIZE);
  79. NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE;
  80. NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
  81. max_sg_len = min_t(u64, nx_ctx->ap->sglen,
  82. nx_driver.of.max_sg_len/sizeof(struct nx_sg));
  83. max_sg_len = min_t(u64, max_sg_len,
  84. nx_ctx->ap->databytelen/NX_PAGE_SIZE);
  85. data_len = SHA256_DIGEST_SIZE;
  86. out_sg = nx_build_sg_list(nx_ctx->out_sg, (u8 *)sctx->state,
  87. &data_len, max_sg_len);
  88. nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
  89. if (data_len != SHA256_DIGEST_SIZE) {
  90. rc = -EINVAL;
  91. goto out;
  92. }
  93. do {
  94. int used_sgs = 0;
  95. struct nx_sg *in_sg = nx_ctx->in_sg;
  96. if (buf_len) {
  97. data_len = buf_len;
  98. in_sg = nx_build_sg_list(in_sg,
  99. (u8 *) sctx->buf,
  100. &data_len,
  101. max_sg_len);
  102. if (data_len != buf_len) {
  103. rc = -EINVAL;
  104. goto out;
  105. }
  106. used_sgs = in_sg - nx_ctx->in_sg;
  107. }
  108. /* to_process: SHA256_BLOCK_SIZE aligned chunk to be
  109. * processed in this iteration. This value is restricted
  110. * by sg list limits and number of sgs we already used
  111. * for leftover data. (see above)
  112. * In ideal case, we could allow NX_PAGE_SIZE * max_sg_len,
  113. * but because data may not be aligned, we need to account
  114. * for that too. */
  115. to_process = min_t(u64, total,
  116. (max_sg_len - 1 - used_sgs) * NX_PAGE_SIZE);
  117. to_process = to_process & ~(SHA256_BLOCK_SIZE - 1);
  118. data_len = to_process - buf_len;
  119. in_sg = nx_build_sg_list(in_sg, (u8 *) data,
  120. &data_len, max_sg_len);
  121. nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
  122. to_process = data_len + buf_len;
  123. leftover = total - to_process;
  124. /*
  125. * we've hit the nx chip previously and we're updating
  126. * again, so copy over the partial digest.
  127. */
  128. memcpy(csbcpb->cpb.sha256.input_partial_digest,
  129. csbcpb->cpb.sha256.message_digest,
  130. SHA256_DIGEST_SIZE);
  131. if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
  132. rc = -EINVAL;
  133. goto out;
  134. }
  135. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
  136. desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  137. if (rc)
  138. goto out;
  139. atomic_inc(&(nx_ctx->stats->sha256_ops));
  140. total -= to_process;
  141. data += to_process - buf_len;
  142. buf_len = 0;
  143. } while (leftover >= SHA256_BLOCK_SIZE);
  144. /* copy the leftover back into the state struct */
  145. if (leftover)
  146. memcpy(sctx->buf, data, leftover);
  147. sctx->count += len;
  148. memcpy(sctx->state, csbcpb->cpb.sha256.message_digest, SHA256_DIGEST_SIZE);
  149. out:
  150. spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
  151. return rc;
  152. }
  153. static int nx_sha256_final(struct shash_desc *desc, u8 *out)
  154. {
  155. struct sha256_state *sctx = shash_desc_ctx(desc);
  156. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  157. struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
  158. struct nx_sg *in_sg, *out_sg;
  159. unsigned long irq_flags;
  160. u32 max_sg_len;
  161. int rc = 0;
  162. int len;
  163. spin_lock_irqsave(&nx_ctx->lock, irq_flags);
  164. max_sg_len = min_t(u64, nx_ctx->ap->sglen,
  165. nx_driver.of.max_sg_len/sizeof(struct nx_sg));
  166. max_sg_len = min_t(u64, max_sg_len,
  167. nx_ctx->ap->databytelen/NX_PAGE_SIZE);
  168. /* final is represented by continuing the operation and indicating that
  169. * this is not an intermediate operation */
  170. if (sctx->count >= SHA256_BLOCK_SIZE) {
  171. /* we've hit the nx chip previously, now we're finalizing,
  172. * so copy over the partial digest */
  173. memcpy(csbcpb->cpb.sha256.input_partial_digest, sctx->state, SHA256_DIGEST_SIZE);
  174. NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
  175. NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
  176. } else {
  177. NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
  178. NX_CPB_FDM(csbcpb) &= ~NX_FDM_CONTINUATION;
  179. }
  180. csbcpb->cpb.sha256.message_bit_length = (u64) (sctx->count * 8);
  181. len = sctx->count & (SHA256_BLOCK_SIZE - 1);
  182. in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *) sctx->buf,
  183. &len, max_sg_len);
  184. if (len != (sctx->count & (SHA256_BLOCK_SIZE - 1))) {
  185. rc = -EINVAL;
  186. goto out;
  187. }
  188. len = SHA256_DIGEST_SIZE;
  189. out_sg = nx_build_sg_list(nx_ctx->out_sg, out, &len, max_sg_len);
  190. if (len != SHA256_DIGEST_SIZE) {
  191. rc = -EINVAL;
  192. goto out;
  193. }
  194. nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
  195. nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
  196. if (!nx_ctx->op.outlen) {
  197. rc = -EINVAL;
  198. goto out;
  199. }
  200. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
  201. desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  202. if (rc)
  203. goto out;
  204. atomic_inc(&(nx_ctx->stats->sha256_ops));
  205. atomic64_add(sctx->count, &(nx_ctx->stats->sha256_bytes));
  206. memcpy(out, csbcpb->cpb.sha256.message_digest, SHA256_DIGEST_SIZE);
  207. out:
  208. spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
  209. return rc;
  210. }
  211. static int nx_sha256_export(struct shash_desc *desc, void *out)
  212. {
  213. struct sha256_state *sctx = shash_desc_ctx(desc);
  214. memcpy(out, sctx, sizeof(*sctx));
  215. return 0;
  216. }
  217. static int nx_sha256_import(struct shash_desc *desc, const void *in)
  218. {
  219. struct sha256_state *sctx = shash_desc_ctx(desc);
  220. memcpy(sctx, in, sizeof(*sctx));
  221. return 0;
  222. }
  223. struct shash_alg nx_shash_sha256_alg = {
  224. .digestsize = SHA256_DIGEST_SIZE,
  225. .init = nx_sha256_init,
  226. .update = nx_sha256_update,
  227. .final = nx_sha256_final,
  228. .export = nx_sha256_export,
  229. .import = nx_sha256_import,
  230. .descsize = sizeof(struct sha256_state),
  231. .statesize = sizeof(struct sha256_state),
  232. .base = {
  233. .cra_name = "sha256",
  234. .cra_driver_name = "sha256-nx",
  235. .cra_priority = 300,
  236. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  237. .cra_blocksize = SHA256_BLOCK_SIZE,
  238. .cra_module = THIS_MODULE,
  239. .cra_ctxsize = sizeof(struct nx_crypto_ctx),
  240. .cra_init = nx_crypto_ctx_sha256_init,
  241. .cra_exit = nx_crypto_ctx_exit,
  242. }
  243. };