nx-sha512.c 8.0 KB

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