nx-sha256.c 7.2 KB

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