nx-aes-xcbc.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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;
  76. int rc = 0;
  77. if (NX_CPB_FDM(csbcpb) & NX_FDM_CONTINUATION) {
  78. /* we've hit the nx chip previously and we're updating again,
  79. * so copy over the partial digest */
  80. memcpy(csbcpb->cpb.aes_xcbc.cv,
  81. csbcpb->cpb.aes_xcbc.out_cv_mac, AES_BLOCK_SIZE);
  82. }
  83. /* 2 cases for total data len:
  84. * 1: <= AES_BLOCK_SIZE: copy into state, return 0
  85. * 2: > AES_BLOCK_SIZE: process X blocks, copy in leftover
  86. */
  87. if (len + sctx->count <= AES_BLOCK_SIZE) {
  88. memcpy(sctx->buffer + sctx->count, data, len);
  89. sctx->count += len;
  90. goto out;
  91. }
  92. /* to_process: the AES_BLOCK_SIZE data chunk to process in this
  93. * update */
  94. to_process = (sctx->count + len) & ~(AES_BLOCK_SIZE - 1);
  95. leftover = (sctx->count + len) & (AES_BLOCK_SIZE - 1);
  96. /* the hardware will not accept a 0 byte operation for this algorithm
  97. * and the operation MUST be finalized to be correct. So if we happen
  98. * to get an update that falls on a block sized boundary, we must
  99. * save off the last block to finalize with later. */
  100. if (!leftover) {
  101. to_process -= AES_BLOCK_SIZE;
  102. leftover = AES_BLOCK_SIZE;
  103. }
  104. if (sctx->count) {
  105. in_sg = nx_build_sg_list(nx_ctx->in_sg, sctx->buffer,
  106. sctx->count, nx_ctx->ap->sglen);
  107. in_sg = nx_build_sg_list(in_sg, (u8 *)data,
  108. to_process - sctx->count,
  109. nx_ctx->ap->sglen);
  110. nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) *
  111. sizeof(struct nx_sg);
  112. } else {
  113. in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *)data, to_process,
  114. nx_ctx->ap->sglen);
  115. nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) *
  116. sizeof(struct nx_sg);
  117. }
  118. NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE;
  119. if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
  120. rc = -EINVAL;
  121. goto out;
  122. }
  123. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
  124. desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  125. if (rc)
  126. goto out;
  127. atomic_inc(&(nx_ctx->stats->aes_ops));
  128. /* copy the leftover back into the state struct */
  129. memcpy(sctx->buffer, data + len - leftover, leftover);
  130. sctx->count = leftover;
  131. /* everything after the first update is continuation */
  132. NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
  133. out:
  134. return rc;
  135. }
  136. static int nx_xcbc_final(struct shash_desc *desc, u8 *out)
  137. {
  138. struct xcbc_state *sctx = shash_desc_ctx(desc);
  139. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  140. struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
  141. struct nx_sg *in_sg, *out_sg;
  142. int rc = 0;
  143. if (NX_CPB_FDM(csbcpb) & NX_FDM_CONTINUATION) {
  144. /* we've hit the nx chip previously, now we're finalizing,
  145. * so copy over the partial digest */
  146. memcpy(csbcpb->cpb.aes_xcbc.cv,
  147. csbcpb->cpb.aes_xcbc.out_cv_mac, AES_BLOCK_SIZE);
  148. } else if (sctx->count == 0) {
  149. /* we've never seen an update, so this is a 0 byte op. The
  150. * hardware cannot handle a 0 byte op, so just copy out the
  151. * known 0 byte result. This is cheaper than allocating a
  152. * software context to do a 0 byte op */
  153. u8 data[] = { 0x75, 0xf0, 0x25, 0x1d, 0x52, 0x8a, 0xc0, 0x1c,
  154. 0x45, 0x73, 0xdf, 0xd5, 0x84, 0xd7, 0x9f, 0x29 };
  155. memcpy(out, data, sizeof(data));
  156. goto out;
  157. }
  158. /* final is represented by continuing the operation and indicating that
  159. * this is not an intermediate operation */
  160. NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
  161. in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *)sctx->buffer,
  162. sctx->count, nx_ctx->ap->sglen);
  163. out_sg = nx_build_sg_list(nx_ctx->out_sg, out, AES_BLOCK_SIZE,
  164. nx_ctx->ap->sglen);
  165. nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
  166. nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
  167. if (!nx_ctx->op.outlen) {
  168. rc = -EINVAL;
  169. goto out;
  170. }
  171. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
  172. desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  173. if (rc)
  174. goto out;
  175. atomic_inc(&(nx_ctx->stats->aes_ops));
  176. memcpy(out, csbcpb->cpb.aes_xcbc.out_cv_mac, AES_BLOCK_SIZE);
  177. out:
  178. return rc;
  179. }
  180. struct shash_alg nx_shash_aes_xcbc_alg = {
  181. .digestsize = AES_BLOCK_SIZE,
  182. .init = nx_xcbc_init,
  183. .update = nx_xcbc_update,
  184. .final = nx_xcbc_final,
  185. .setkey = nx_xcbc_set_key,
  186. .descsize = sizeof(struct xcbc_state),
  187. .statesize = sizeof(struct xcbc_state),
  188. .base = {
  189. .cra_name = "xcbc(aes)",
  190. .cra_driver_name = "xcbc-aes-nx",
  191. .cra_priority = 300,
  192. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  193. .cra_blocksize = AES_BLOCK_SIZE,
  194. .cra_module = THIS_MODULE,
  195. .cra_ctxsize = sizeof(struct nx_crypto_ctx),
  196. .cra_init = nx_crypto_ctx_aes_xcbc_init,
  197. .cra_exit = nx_crypto_ctx_exit,
  198. }
  199. };