nx-aes-xcbc.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /**
  2. * AES XCBC 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/aes.h>
  23. #include <crypto/algapi.h>
  24. #include <linux/module.h>
  25. #include <linux/types.h>
  26. #include <linux/crypto.h>
  27. #include <asm/vio.h>
  28. #include "nx_csbcpb.h"
  29. #include "nx.h"
  30. struct xcbc_state {
  31. u8 state[AES_BLOCK_SIZE];
  32. unsigned int count;
  33. u8 buffer[AES_BLOCK_SIZE];
  34. };
  35. static int nx_xcbc_set_key(struct crypto_shash *desc,
  36. const u8 *in_key,
  37. unsigned int key_len)
  38. {
  39. struct nx_crypto_ctx *nx_ctx = crypto_shash_ctx(desc);
  40. switch (key_len) {
  41. case AES_KEYSIZE_128:
  42. nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_128];
  43. break;
  44. default:
  45. return -EINVAL;
  46. }
  47. memcpy(nx_ctx->priv.xcbc.key, in_key, key_len);
  48. return 0;
  49. }
  50. static int nx_xcbc_init(struct shash_desc *desc)
  51. {
  52. struct xcbc_state *sctx = shash_desc_ctx(desc);
  53. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  54. struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
  55. struct nx_sg *out_sg;
  56. nx_ctx_init(nx_ctx, HCOP_FC_AES);
  57. memset(sctx, 0, sizeof *sctx);
  58. NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_128);
  59. csbcpb->cpb.hdr.mode = NX_MODE_AES_XCBC_MAC;
  60. memcpy(csbcpb->cpb.aes_xcbc.key, nx_ctx->priv.xcbc.key, AES_BLOCK_SIZE);
  61. memset(nx_ctx->priv.xcbc.key, 0, sizeof *nx_ctx->priv.xcbc.key);
  62. out_sg = nx_build_sg_list(nx_ctx->out_sg, (u8 *)sctx->state,
  63. AES_BLOCK_SIZE, nx_ctx->ap->sglen);
  64. nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
  65. return 0;
  66. }
  67. static int nx_xcbc_update(struct shash_desc *desc,
  68. const u8 *data,
  69. unsigned int len)
  70. {
  71. struct xcbc_state *sctx = shash_desc_ctx(desc);
  72. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  73. struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
  74. struct nx_sg *in_sg;
  75. u32 to_process, leftover, total;
  76. u32 max_sg_len;
  77. unsigned long irq_flags;
  78. int rc = 0;
  79. spin_lock_irqsave(&nx_ctx->lock, irq_flags);
  80. total = sctx->count + len;
  81. /* 2 cases for total data len:
  82. * 1: <= AES_BLOCK_SIZE: copy into state, return 0
  83. * 2: > AES_BLOCK_SIZE: process X blocks, copy in leftover
  84. */
  85. if (total <= AES_BLOCK_SIZE) {
  86. memcpy(sctx->buffer + sctx->count, data, len);
  87. sctx->count += len;
  88. goto out;
  89. }
  90. in_sg = nx_ctx->in_sg;
  91. max_sg_len = min_t(u32, nx_driver.of.max_sg_len/sizeof(struct nx_sg),
  92. nx_ctx->ap->sglen);
  93. do {
  94. /* to_process: the AES_BLOCK_SIZE data chunk to process in this
  95. * update */
  96. to_process = min_t(u64, total, nx_ctx->ap->databytelen);
  97. to_process = min_t(u64, to_process,
  98. NX_PAGE_SIZE * (max_sg_len - 1));
  99. to_process = to_process & ~(AES_BLOCK_SIZE - 1);
  100. leftover = total - to_process;
  101. /* the hardware will not accept a 0 byte operation for this
  102. * algorithm and the operation MUST be finalized to be correct.
  103. * So if we happen to get an update that falls on a block sized
  104. * boundary, we must save off the last block to finalize with
  105. * later. */
  106. if (!leftover) {
  107. to_process -= AES_BLOCK_SIZE;
  108. leftover = AES_BLOCK_SIZE;
  109. }
  110. if (sctx->count) {
  111. in_sg = nx_build_sg_list(nx_ctx->in_sg,
  112. (u8 *) sctx->buffer,
  113. sctx->count,
  114. max_sg_len);
  115. }
  116. in_sg = nx_build_sg_list(in_sg,
  117. (u8 *) data,
  118. to_process - sctx->count,
  119. max_sg_len);
  120. nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) *
  121. sizeof(struct nx_sg);
  122. /* we've hit the nx chip previously and we're updating again,
  123. * so copy over the partial digest */
  124. if (NX_CPB_FDM(csbcpb) & NX_FDM_CONTINUATION) {
  125. memcpy(csbcpb->cpb.aes_xcbc.cv,
  126. csbcpb->cpb.aes_xcbc.out_cv_mac,
  127. AES_BLOCK_SIZE);
  128. }
  129. NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE;
  130. if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
  131. rc = -EINVAL;
  132. goto out;
  133. }
  134. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
  135. desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  136. if (rc)
  137. goto out;
  138. atomic_inc(&(nx_ctx->stats->aes_ops));
  139. /* everything after the first update is continuation */
  140. NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
  141. total -= to_process;
  142. data += to_process - sctx->count;
  143. sctx->count = 0;
  144. in_sg = nx_ctx->in_sg;
  145. } while (leftover > AES_BLOCK_SIZE);
  146. /* copy the leftover back into the state struct */
  147. memcpy(sctx->buffer, data, leftover);
  148. sctx->count = leftover;
  149. out:
  150. spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
  151. return rc;
  152. }
  153. static int nx_xcbc_final(struct shash_desc *desc, u8 *out)
  154. {
  155. struct xcbc_state *sctx = shash_desc_ctx(desc);
  156. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  157. struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
  158. struct nx_sg *in_sg, *out_sg;
  159. unsigned long irq_flags;
  160. int rc = 0;
  161. spin_lock_irqsave(&nx_ctx->lock, irq_flags);
  162. if (NX_CPB_FDM(csbcpb) & NX_FDM_CONTINUATION) {
  163. /* we've hit the nx chip previously, now we're finalizing,
  164. * so copy over the partial digest */
  165. memcpy(csbcpb->cpb.aes_xcbc.cv,
  166. csbcpb->cpb.aes_xcbc.out_cv_mac, AES_BLOCK_SIZE);
  167. } else if (sctx->count == 0) {
  168. /* we've never seen an update, so this is a 0 byte op. The
  169. * hardware cannot handle a 0 byte op, so just copy out the
  170. * known 0 byte result. This is cheaper than allocating a
  171. * software context to do a 0 byte op */
  172. u8 data[] = { 0x75, 0xf0, 0x25, 0x1d, 0x52, 0x8a, 0xc0, 0x1c,
  173. 0x45, 0x73, 0xdf, 0xd5, 0x84, 0xd7, 0x9f, 0x29 };
  174. memcpy(out, data, sizeof(data));
  175. goto out;
  176. }
  177. /* final is represented by continuing the operation and indicating that
  178. * this is not an intermediate operation */
  179. NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
  180. in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *)sctx->buffer,
  181. sctx->count, nx_ctx->ap->sglen);
  182. out_sg = nx_build_sg_list(nx_ctx->out_sg, out, AES_BLOCK_SIZE,
  183. nx_ctx->ap->sglen);
  184. nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
  185. nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
  186. if (!nx_ctx->op.outlen) {
  187. rc = -EINVAL;
  188. goto out;
  189. }
  190. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
  191. desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  192. if (rc)
  193. goto out;
  194. atomic_inc(&(nx_ctx->stats->aes_ops));
  195. memcpy(out, csbcpb->cpb.aes_xcbc.out_cv_mac, AES_BLOCK_SIZE);
  196. out:
  197. spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
  198. return rc;
  199. }
  200. struct shash_alg nx_shash_aes_xcbc_alg = {
  201. .digestsize = AES_BLOCK_SIZE,
  202. .init = nx_xcbc_init,
  203. .update = nx_xcbc_update,
  204. .final = nx_xcbc_final,
  205. .setkey = nx_xcbc_set_key,
  206. .descsize = sizeof(struct xcbc_state),
  207. .statesize = sizeof(struct xcbc_state),
  208. .base = {
  209. .cra_name = "xcbc(aes)",
  210. .cra_driver_name = "xcbc-aes-nx",
  211. .cra_priority = 300,
  212. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  213. .cra_blocksize = AES_BLOCK_SIZE,
  214. .cra_module = THIS_MODULE,
  215. .cra_ctxsize = sizeof(struct nx_crypto_ctx),
  216. .cra_init = nx_crypto_ctx_aes_xcbc_init,
  217. .cra_exit = nx_crypto_ctx_exit,
  218. }
  219. };