nx-aes-xcbc.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. /*
  51. * Based on RFC 3566, for a zero-length message:
  52. *
  53. * n = 1
  54. * K1 = E(K, 0x01010101010101010101010101010101)
  55. * K3 = E(K, 0x03030303030303030303030303030303)
  56. * E[0] = 0x00000000000000000000000000000000
  57. * M[1] = 0x80000000000000000000000000000000 (0 length message with padding)
  58. * E[1] = (K1, M[1] ^ E[0] ^ K3)
  59. * Tag = M[1]
  60. */
  61. static int nx_xcbc_empty(struct shash_desc *desc, u8 *out)
  62. {
  63. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  64. struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
  65. struct nx_sg *in_sg, *out_sg;
  66. u8 keys[2][AES_BLOCK_SIZE];
  67. u8 key[32];
  68. int rc = 0;
  69. /* Change to ECB mode */
  70. csbcpb->cpb.hdr.mode = NX_MODE_AES_ECB;
  71. memcpy(key, csbcpb->cpb.aes_xcbc.key, AES_BLOCK_SIZE);
  72. memcpy(csbcpb->cpb.aes_ecb.key, key, AES_BLOCK_SIZE);
  73. NX_CPB_FDM(csbcpb) |= NX_FDM_ENDE_ENCRYPT;
  74. /* K1 and K3 base patterns */
  75. memset(keys[0], 0x01, sizeof(keys[0]));
  76. memset(keys[1], 0x03, sizeof(keys[1]));
  77. /* Generate K1 and K3 encrypting the patterns */
  78. in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *) keys, sizeof(keys),
  79. nx_ctx->ap->sglen);
  80. out_sg = nx_build_sg_list(nx_ctx->out_sg, (u8 *) keys, sizeof(keys),
  81. nx_ctx->ap->sglen);
  82. nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
  83. nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
  84. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
  85. desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  86. if (rc)
  87. goto out;
  88. atomic_inc(&(nx_ctx->stats->aes_ops));
  89. /* XOr K3 with the padding for a 0 length message */
  90. keys[1][0] ^= 0x80;
  91. /* Encrypt the final result */
  92. memcpy(csbcpb->cpb.aes_ecb.key, keys[0], AES_BLOCK_SIZE);
  93. in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *) keys[1], sizeof(keys[1]),
  94. nx_ctx->ap->sglen);
  95. out_sg = nx_build_sg_list(nx_ctx->out_sg, out, AES_BLOCK_SIZE,
  96. nx_ctx->ap->sglen);
  97. nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
  98. nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
  99. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
  100. desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  101. if (rc)
  102. goto out;
  103. atomic_inc(&(nx_ctx->stats->aes_ops));
  104. out:
  105. /* Restore XCBC mode */
  106. csbcpb->cpb.hdr.mode = NX_MODE_AES_XCBC_MAC;
  107. memcpy(csbcpb->cpb.aes_xcbc.key, key, AES_BLOCK_SIZE);
  108. NX_CPB_FDM(csbcpb) &= ~NX_FDM_ENDE_ENCRYPT;
  109. return rc;
  110. }
  111. static int nx_xcbc_init(struct shash_desc *desc)
  112. {
  113. struct xcbc_state *sctx = shash_desc_ctx(desc);
  114. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  115. struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
  116. struct nx_sg *out_sg;
  117. nx_ctx_init(nx_ctx, HCOP_FC_AES);
  118. memset(sctx, 0, sizeof *sctx);
  119. NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_128);
  120. csbcpb->cpb.hdr.mode = NX_MODE_AES_XCBC_MAC;
  121. memcpy(csbcpb->cpb.aes_xcbc.key, nx_ctx->priv.xcbc.key, AES_BLOCK_SIZE);
  122. memset(nx_ctx->priv.xcbc.key, 0, sizeof *nx_ctx->priv.xcbc.key);
  123. out_sg = nx_build_sg_list(nx_ctx->out_sg, (u8 *)sctx->state,
  124. AES_BLOCK_SIZE, nx_ctx->ap->sglen);
  125. nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
  126. return 0;
  127. }
  128. static int nx_xcbc_update(struct shash_desc *desc,
  129. const u8 *data,
  130. unsigned int len)
  131. {
  132. struct xcbc_state *sctx = shash_desc_ctx(desc);
  133. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  134. struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
  135. struct nx_sg *in_sg;
  136. u32 to_process, leftover, total;
  137. u32 max_sg_len;
  138. unsigned long irq_flags;
  139. int rc = 0;
  140. spin_lock_irqsave(&nx_ctx->lock, irq_flags);
  141. total = sctx->count + len;
  142. /* 2 cases for total data len:
  143. * 1: <= AES_BLOCK_SIZE: copy into state, return 0
  144. * 2: > AES_BLOCK_SIZE: process X blocks, copy in leftover
  145. */
  146. if (total <= AES_BLOCK_SIZE) {
  147. memcpy(sctx->buffer + sctx->count, data, len);
  148. sctx->count += len;
  149. goto out;
  150. }
  151. in_sg = nx_ctx->in_sg;
  152. max_sg_len = min_t(u32, nx_driver.of.max_sg_len/sizeof(struct nx_sg),
  153. nx_ctx->ap->sglen);
  154. do {
  155. /* to_process: the AES_BLOCK_SIZE data chunk to process in this
  156. * update */
  157. to_process = min_t(u64, total, nx_ctx->ap->databytelen);
  158. to_process = min_t(u64, to_process,
  159. NX_PAGE_SIZE * (max_sg_len - 1));
  160. to_process = to_process & ~(AES_BLOCK_SIZE - 1);
  161. leftover = total - to_process;
  162. /* the hardware will not accept a 0 byte operation for this
  163. * algorithm and the operation MUST be finalized to be correct.
  164. * So if we happen to get an update that falls on a block sized
  165. * boundary, we must save off the last block to finalize with
  166. * later. */
  167. if (!leftover) {
  168. to_process -= AES_BLOCK_SIZE;
  169. leftover = AES_BLOCK_SIZE;
  170. }
  171. if (sctx->count) {
  172. in_sg = nx_build_sg_list(nx_ctx->in_sg,
  173. (u8 *) sctx->buffer,
  174. sctx->count,
  175. max_sg_len);
  176. }
  177. in_sg = nx_build_sg_list(in_sg,
  178. (u8 *) data,
  179. to_process - sctx->count,
  180. max_sg_len);
  181. nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) *
  182. sizeof(struct nx_sg);
  183. /* we've hit the nx chip previously and we're updating again,
  184. * so copy over the partial digest */
  185. if (NX_CPB_FDM(csbcpb) & NX_FDM_CONTINUATION) {
  186. memcpy(csbcpb->cpb.aes_xcbc.cv,
  187. csbcpb->cpb.aes_xcbc.out_cv_mac,
  188. AES_BLOCK_SIZE);
  189. }
  190. NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE;
  191. if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
  192. rc = -EINVAL;
  193. goto out;
  194. }
  195. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
  196. desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  197. if (rc)
  198. goto out;
  199. atomic_inc(&(nx_ctx->stats->aes_ops));
  200. /* everything after the first update is continuation */
  201. NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
  202. total -= to_process;
  203. data += to_process - sctx->count;
  204. sctx->count = 0;
  205. in_sg = nx_ctx->in_sg;
  206. } while (leftover > AES_BLOCK_SIZE);
  207. /* copy the leftover back into the state struct */
  208. memcpy(sctx->buffer, data, leftover);
  209. sctx->count = leftover;
  210. out:
  211. spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
  212. return rc;
  213. }
  214. static int nx_xcbc_final(struct shash_desc *desc, u8 *out)
  215. {
  216. struct xcbc_state *sctx = shash_desc_ctx(desc);
  217. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  218. struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
  219. struct nx_sg *in_sg, *out_sg;
  220. unsigned long irq_flags;
  221. int rc = 0;
  222. spin_lock_irqsave(&nx_ctx->lock, irq_flags);
  223. if (NX_CPB_FDM(csbcpb) & NX_FDM_CONTINUATION) {
  224. /* we've hit the nx chip previously, now we're finalizing,
  225. * so copy over the partial digest */
  226. memcpy(csbcpb->cpb.aes_xcbc.cv,
  227. csbcpb->cpb.aes_xcbc.out_cv_mac, AES_BLOCK_SIZE);
  228. } else if (sctx->count == 0) {
  229. /*
  230. * we've never seen an update, so this is a 0 byte op. The
  231. * hardware cannot handle a 0 byte op, so just ECB to
  232. * generate the hash.
  233. */
  234. rc = nx_xcbc_empty(desc, out);
  235. goto out;
  236. }
  237. /* final is represented by continuing the operation and indicating that
  238. * this is not an intermediate operation */
  239. NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
  240. in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *)sctx->buffer,
  241. sctx->count, nx_ctx->ap->sglen);
  242. out_sg = nx_build_sg_list(nx_ctx->out_sg, out, AES_BLOCK_SIZE,
  243. nx_ctx->ap->sglen);
  244. nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
  245. nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
  246. if (!nx_ctx->op.outlen) {
  247. rc = -EINVAL;
  248. goto out;
  249. }
  250. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
  251. desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  252. if (rc)
  253. goto out;
  254. atomic_inc(&(nx_ctx->stats->aes_ops));
  255. memcpy(out, csbcpb->cpb.aes_xcbc.out_cv_mac, AES_BLOCK_SIZE);
  256. out:
  257. spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
  258. return rc;
  259. }
  260. struct shash_alg nx_shash_aes_xcbc_alg = {
  261. .digestsize = AES_BLOCK_SIZE,
  262. .init = nx_xcbc_init,
  263. .update = nx_xcbc_update,
  264. .final = nx_xcbc_final,
  265. .setkey = nx_xcbc_set_key,
  266. .descsize = sizeof(struct xcbc_state),
  267. .statesize = sizeof(struct xcbc_state),
  268. .base = {
  269. .cra_name = "xcbc(aes)",
  270. .cra_driver_name = "xcbc-aes-nx",
  271. .cra_priority = 300,
  272. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  273. .cra_blocksize = AES_BLOCK_SIZE,
  274. .cra_module = THIS_MODULE,
  275. .cra_ctxsize = sizeof(struct nx_crypto_ctx),
  276. .cra_init = nx_crypto_ctx_aes_xcbc_init,
  277. .cra_exit = nx_crypto_ctx_exit,
  278. }
  279. };