nx-aes-xcbc.c 10.0 KB

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