nx-sha512.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. int len;
  32. int rc;
  33. nx_ctx_init(nx_ctx, HCOP_FC_SHA);
  34. memset(sctx, 0, sizeof *sctx);
  35. nx_ctx->ap = &nx_ctx->props[NX_PROPS_SHA512];
  36. NX_CPB_SET_DIGEST_SIZE(nx_ctx->csbcpb, NX_DS_SHA512);
  37. len = SHA512_DIGEST_SIZE;
  38. rc = nx_sha_build_sg_list(nx_ctx, nx_ctx->out_sg,
  39. &nx_ctx->op.outlen,
  40. &len,
  41. (u8 *)sctx->state,
  42. NX_DS_SHA512);
  43. if (rc || len != SHA512_DIGEST_SIZE)
  44. goto out;
  45. sctx->state[0] = __cpu_to_be64(SHA512_H0);
  46. sctx->state[1] = __cpu_to_be64(SHA512_H1);
  47. sctx->state[2] = __cpu_to_be64(SHA512_H2);
  48. sctx->state[3] = __cpu_to_be64(SHA512_H3);
  49. sctx->state[4] = __cpu_to_be64(SHA512_H4);
  50. sctx->state[5] = __cpu_to_be64(SHA512_H5);
  51. sctx->state[6] = __cpu_to_be64(SHA512_H6);
  52. sctx->state[7] = __cpu_to_be64(SHA512_H7);
  53. sctx->count[0] = 0;
  54. out:
  55. return 0;
  56. }
  57. static int nx_sha512_update(struct shash_desc *desc, const u8 *data,
  58. unsigned int len)
  59. {
  60. struct sha512_state *sctx = shash_desc_ctx(desc);
  61. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  62. struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
  63. u64 to_process, leftover = 0, total;
  64. unsigned long irq_flags;
  65. int rc = 0;
  66. int data_len;
  67. u64 buf_len = (sctx->count[0] % SHA512_BLOCK_SIZE);
  68. spin_lock_irqsave(&nx_ctx->lock, irq_flags);
  69. /* 2 cases for total data len:
  70. * 1: < SHA512_BLOCK_SIZE: copy into state, return 0
  71. * 2: >= SHA512_BLOCK_SIZE: process X blocks, copy in leftover
  72. */
  73. total = (sctx->count[0] % SHA512_BLOCK_SIZE) + len;
  74. if (total < SHA512_BLOCK_SIZE) {
  75. memcpy(sctx->buf + buf_len, data, len);
  76. sctx->count[0] += len;
  77. goto out;
  78. }
  79. memcpy(csbcpb->cpb.sha512.message_digest, sctx->state, SHA512_DIGEST_SIZE);
  80. NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE;
  81. NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
  82. do {
  83. /*
  84. * to_process: the SHA512_BLOCK_SIZE data chunk to process in
  85. * this update. This value is also restricted by the sg list
  86. * limits.
  87. */
  88. to_process = total - leftover;
  89. to_process = to_process & ~(SHA512_BLOCK_SIZE - 1);
  90. leftover = total - to_process;
  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_SHA512);
  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_SHA512);
  107. if (rc || data_len != (to_process - buf_len))
  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.sha512.input_partial_digest,
  116. csbcpb->cpb.sha512.message_digest,
  117. SHA512_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->sha512_ops));
  127. total -= to_process;
  128. data += to_process - buf_len;
  129. buf_len = 0;
  130. } while (leftover >= SHA512_BLOCK_SIZE);
  131. /* copy the leftover back into the state struct */
  132. if (leftover)
  133. memcpy(sctx->buf, data, leftover);
  134. sctx->count[0] += len;
  135. memcpy(sctx->state, csbcpb->cpb.sha512.message_digest, SHA512_DIGEST_SIZE);
  136. out:
  137. spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
  138. return rc;
  139. }
  140. static int nx_sha512_final(struct shash_desc *desc, u8 *out)
  141. {
  142. struct sha512_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. u64 count0;
  146. unsigned long irq_flags;
  147. int rc;
  148. int len;
  149. spin_lock_irqsave(&nx_ctx->lock, irq_flags);
  150. /* final is represented by continuing the operation and indicating that
  151. * this is not an intermediate operation */
  152. if (sctx->count[0] >= SHA512_BLOCK_SIZE) {
  153. /* we've hit the nx chip previously, now we're finalizing,
  154. * so copy over the partial digest */
  155. memcpy(csbcpb->cpb.sha512.input_partial_digest, sctx->state,
  156. SHA512_DIGEST_SIZE);
  157. NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
  158. NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
  159. } else {
  160. NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
  161. NX_CPB_FDM(csbcpb) &= ~NX_FDM_CONTINUATION;
  162. }
  163. NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
  164. count0 = sctx->count[0] * 8;
  165. csbcpb->cpb.sha512.message_bit_length_lo = count0;
  166. len = sctx->count[0] & (SHA512_BLOCK_SIZE - 1);
  167. rc = nx_sha_build_sg_list(nx_ctx, nx_ctx->in_sg,
  168. &nx_ctx->op.inlen,
  169. &len,
  170. (u8 *)sctx->buf,
  171. NX_DS_SHA512);
  172. if (rc || len != (sctx->count[0] & (SHA512_BLOCK_SIZE - 1)))
  173. goto out;
  174. len = SHA512_DIGEST_SIZE;
  175. rc = nx_sha_build_sg_list(nx_ctx, nx_ctx->out_sg,
  176. &nx_ctx->op.outlen,
  177. &len,
  178. out,
  179. NX_DS_SHA512);
  180. if (rc)
  181. goto out;
  182. if (!nx_ctx->op.outlen) {
  183. rc = -EINVAL;
  184. goto out;
  185. }
  186. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
  187. desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  188. if (rc)
  189. goto out;
  190. atomic_inc(&(nx_ctx->stats->sha512_ops));
  191. atomic64_add(sctx->count[0], &(nx_ctx->stats->sha512_bytes));
  192. memcpy(out, csbcpb->cpb.sha512.message_digest, SHA512_DIGEST_SIZE);
  193. out:
  194. spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
  195. return rc;
  196. }
  197. static int nx_sha512_export(struct shash_desc *desc, void *out)
  198. {
  199. struct sha512_state *sctx = shash_desc_ctx(desc);
  200. memcpy(out, sctx, sizeof(*sctx));
  201. return 0;
  202. }
  203. static int nx_sha512_import(struct shash_desc *desc, const void *in)
  204. {
  205. struct sha512_state *sctx = shash_desc_ctx(desc);
  206. memcpy(sctx, in, sizeof(*sctx));
  207. return 0;
  208. }
  209. struct shash_alg nx_shash_sha512_alg = {
  210. .digestsize = SHA512_DIGEST_SIZE,
  211. .init = nx_sha512_init,
  212. .update = nx_sha512_update,
  213. .final = nx_sha512_final,
  214. .export = nx_sha512_export,
  215. .import = nx_sha512_import,
  216. .descsize = sizeof(struct sha512_state),
  217. .statesize = sizeof(struct sha512_state),
  218. .base = {
  219. .cra_name = "sha512",
  220. .cra_driver_name = "sha512-nx",
  221. .cra_priority = 300,
  222. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  223. .cra_blocksize = SHA512_BLOCK_SIZE,
  224. .cra_module = THIS_MODULE,
  225. .cra_ctxsize = sizeof(struct nx_crypto_ctx),
  226. .cra_init = nx_crypto_ctx_sha_init,
  227. .cra_exit = nx_crypto_ctx_exit,
  228. }
  229. };