nx-sha512.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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_crypto_ctx_sha512_init(struct crypto_tfm *tfm)
  28. {
  29. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(tfm);
  30. int err;
  31. err = nx_crypto_ctx_sha_init(tfm);
  32. if (err)
  33. return err;
  34. nx_ctx_init(nx_ctx, HCOP_FC_SHA);
  35. nx_ctx->ap = &nx_ctx->props[NX_PROPS_SHA512];
  36. NX_CPB_SET_DIGEST_SIZE(nx_ctx->csbcpb, NX_DS_SHA512);
  37. return 0;
  38. }
  39. static int nx_sha512_init(struct shash_desc *desc)
  40. {
  41. struct sha512_state *sctx = shash_desc_ctx(desc);
  42. memset(sctx, 0, sizeof *sctx);
  43. sctx->state[0] = __cpu_to_be64(SHA512_H0);
  44. sctx->state[1] = __cpu_to_be64(SHA512_H1);
  45. sctx->state[2] = __cpu_to_be64(SHA512_H2);
  46. sctx->state[3] = __cpu_to_be64(SHA512_H3);
  47. sctx->state[4] = __cpu_to_be64(SHA512_H4);
  48. sctx->state[5] = __cpu_to_be64(SHA512_H5);
  49. sctx->state[6] = __cpu_to_be64(SHA512_H6);
  50. sctx->state[7] = __cpu_to_be64(SHA512_H7);
  51. sctx->count[0] = 0;
  52. return 0;
  53. }
  54. static int nx_sha512_update(struct shash_desc *desc, const u8 *data,
  55. unsigned int len)
  56. {
  57. struct sha512_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, leftover = 0, 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[0] % SHA512_BLOCK_SIZE);
  67. spin_lock_irqsave(&nx_ctx->lock, irq_flags);
  68. /* 2 cases for total data len:
  69. * 1: < SHA512_BLOCK_SIZE: copy into state, return 0
  70. * 2: >= SHA512_BLOCK_SIZE: process X blocks, copy in leftover
  71. */
  72. total = (sctx->count[0] % SHA512_BLOCK_SIZE) + len;
  73. if (total < SHA512_BLOCK_SIZE) {
  74. memcpy(sctx->buf + buf_len, data, len);
  75. sctx->count[0] += len;
  76. goto out;
  77. }
  78. memcpy(csbcpb->cpb.sha512.message_digest, sctx->state, SHA512_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 = SHA512_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 != SHA512_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, max_sg_len);
  101. if (data_len != buf_len) {
  102. rc = -EINVAL;
  103. goto out;
  104. }
  105. used_sgs = in_sg - nx_ctx->in_sg;
  106. }
  107. /* to_process: SHA512_BLOCK_SIZE aligned chunk to be
  108. * processed in this iteration. This value is restricted
  109. * by sg list limits and number of sgs we already used
  110. * for leftover data. (see above)
  111. * In ideal case, we could allow NX_PAGE_SIZE * max_sg_len,
  112. * but because data may not be aligned, we need to account
  113. * for that too. */
  114. to_process = min_t(u64, total,
  115. (max_sg_len - 1 - used_sgs) * NX_PAGE_SIZE);
  116. to_process = to_process & ~(SHA512_BLOCK_SIZE - 1);
  117. data_len = to_process - buf_len;
  118. in_sg = nx_build_sg_list(in_sg, (u8 *) data,
  119. &data_len, max_sg_len);
  120. nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
  121. if (data_len != (to_process - buf_len)) {
  122. rc = -EINVAL;
  123. goto out;
  124. }
  125. to_process = data_len + buf_len;
  126. leftover = total - to_process;
  127. /*
  128. * we've hit the nx chip previously and we're updating
  129. * again, so copy over the partial digest.
  130. */
  131. memcpy(csbcpb->cpb.sha512.input_partial_digest,
  132. csbcpb->cpb.sha512.message_digest,
  133. SHA512_DIGEST_SIZE);
  134. if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
  135. rc = -EINVAL;
  136. goto out;
  137. }
  138. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
  139. desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  140. if (rc)
  141. goto out;
  142. atomic_inc(&(nx_ctx->stats->sha512_ops));
  143. total -= to_process;
  144. data += to_process - buf_len;
  145. buf_len = 0;
  146. } while (leftover >= SHA512_BLOCK_SIZE);
  147. /* copy the leftover back into the state struct */
  148. if (leftover)
  149. memcpy(sctx->buf, data, leftover);
  150. sctx->count[0] += len;
  151. memcpy(sctx->state, csbcpb->cpb.sha512.message_digest, SHA512_DIGEST_SIZE);
  152. out:
  153. spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
  154. return rc;
  155. }
  156. static int nx_sha512_final(struct shash_desc *desc, u8 *out)
  157. {
  158. struct sha512_state *sctx = shash_desc_ctx(desc);
  159. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  160. struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
  161. struct nx_sg *in_sg, *out_sg;
  162. u32 max_sg_len;
  163. u64 count0;
  164. unsigned long irq_flags;
  165. int rc = 0;
  166. int len;
  167. spin_lock_irqsave(&nx_ctx->lock, irq_flags);
  168. max_sg_len = min_t(u64, nx_ctx->ap->sglen,
  169. nx_driver.of.max_sg_len/sizeof(struct nx_sg));
  170. max_sg_len = min_t(u64, max_sg_len,
  171. nx_ctx->ap->databytelen/NX_PAGE_SIZE);
  172. /* final is represented by continuing the operation and indicating that
  173. * this is not an intermediate operation */
  174. if (sctx->count[0] >= SHA512_BLOCK_SIZE) {
  175. /* we've hit the nx chip previously, now we're finalizing,
  176. * so copy over the partial digest */
  177. memcpy(csbcpb->cpb.sha512.input_partial_digest, sctx->state,
  178. SHA512_DIGEST_SIZE);
  179. NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
  180. NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
  181. } else {
  182. NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
  183. NX_CPB_FDM(csbcpb) &= ~NX_FDM_CONTINUATION;
  184. }
  185. NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
  186. count0 = sctx->count[0] * 8;
  187. csbcpb->cpb.sha512.message_bit_length_lo = count0;
  188. len = sctx->count[0] & (SHA512_BLOCK_SIZE - 1);
  189. in_sg = nx_build_sg_list(nx_ctx->in_sg, sctx->buf, &len,
  190. max_sg_len);
  191. if (len != (sctx->count[0] & (SHA512_BLOCK_SIZE - 1))) {
  192. rc = -EINVAL;
  193. goto out;
  194. }
  195. len = SHA512_DIGEST_SIZE;
  196. out_sg = nx_build_sg_list(nx_ctx->out_sg, out, &len,
  197. max_sg_len);
  198. nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
  199. nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
  200. if (!nx_ctx->op.outlen) {
  201. rc = -EINVAL;
  202. goto out;
  203. }
  204. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
  205. desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  206. if (rc)
  207. goto out;
  208. atomic_inc(&(nx_ctx->stats->sha512_ops));
  209. atomic64_add(sctx->count[0], &(nx_ctx->stats->sha512_bytes));
  210. memcpy(out, csbcpb->cpb.sha512.message_digest, SHA512_DIGEST_SIZE);
  211. out:
  212. spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
  213. return rc;
  214. }
  215. static int nx_sha512_export(struct shash_desc *desc, void *out)
  216. {
  217. struct sha512_state *sctx = shash_desc_ctx(desc);
  218. memcpy(out, sctx, sizeof(*sctx));
  219. return 0;
  220. }
  221. static int nx_sha512_import(struct shash_desc *desc, const void *in)
  222. {
  223. struct sha512_state *sctx = shash_desc_ctx(desc);
  224. memcpy(sctx, in, sizeof(*sctx));
  225. return 0;
  226. }
  227. struct shash_alg nx_shash_sha512_alg = {
  228. .digestsize = SHA512_DIGEST_SIZE,
  229. .init = nx_sha512_init,
  230. .update = nx_sha512_update,
  231. .final = nx_sha512_final,
  232. .export = nx_sha512_export,
  233. .import = nx_sha512_import,
  234. .descsize = sizeof(struct sha512_state),
  235. .statesize = sizeof(struct sha512_state),
  236. .base = {
  237. .cra_name = "sha512",
  238. .cra_driver_name = "sha512-nx",
  239. .cra_priority = 300,
  240. .cra_blocksize = SHA512_BLOCK_SIZE,
  241. .cra_module = THIS_MODULE,
  242. .cra_ctxsize = sizeof(struct nx_crypto_ctx),
  243. .cra_init = nx_crypto_ctx_sha512_init,
  244. .cra_exit = nx_crypto_ctx_exit,
  245. }
  246. };