nx-aes-xcbc.c 10 KB

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