nx-aes-gcm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /**
  2. * AES GCM routines supporting the Power 7+ Nest Accelerators driver
  3. *
  4. * Copyright (C) 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/aead.h>
  22. #include <crypto/aes.h>
  23. #include <crypto/algapi.h>
  24. #include <crypto/scatterwalk.h>
  25. #include <linux/module.h>
  26. #include <linux/types.h>
  27. #include <asm/vio.h>
  28. #include "nx_csbcpb.h"
  29. #include "nx.h"
  30. static int gcm_aes_nx_set_key(struct crypto_aead *tfm,
  31. const u8 *in_key,
  32. unsigned int key_len)
  33. {
  34. struct nx_crypto_ctx *nx_ctx = crypto_aead_ctx(tfm);
  35. struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
  36. struct nx_csbcpb *csbcpb_aead = nx_ctx->csbcpb_aead;
  37. nx_ctx_init(nx_ctx, HCOP_FC_AES);
  38. switch (key_len) {
  39. case AES_KEYSIZE_128:
  40. NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_128);
  41. NX_CPB_SET_KEY_SIZE(csbcpb_aead, NX_KS_AES_128);
  42. nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_128];
  43. break;
  44. case AES_KEYSIZE_192:
  45. NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_192);
  46. NX_CPB_SET_KEY_SIZE(csbcpb_aead, NX_KS_AES_192);
  47. nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_192];
  48. break;
  49. case AES_KEYSIZE_256:
  50. NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_256);
  51. NX_CPB_SET_KEY_SIZE(csbcpb_aead, NX_KS_AES_256);
  52. nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_256];
  53. break;
  54. default:
  55. return -EINVAL;
  56. }
  57. csbcpb->cpb.hdr.mode = NX_MODE_AES_GCM;
  58. memcpy(csbcpb->cpb.aes_gcm.key, in_key, key_len);
  59. csbcpb_aead->cpb.hdr.mode = NX_MODE_AES_GCA;
  60. memcpy(csbcpb_aead->cpb.aes_gca.key, in_key, key_len);
  61. return 0;
  62. }
  63. static int gcm4106_aes_nx_set_key(struct crypto_aead *tfm,
  64. const u8 *in_key,
  65. unsigned int key_len)
  66. {
  67. struct nx_crypto_ctx *nx_ctx = crypto_aead_ctx(tfm);
  68. char *nonce = nx_ctx->priv.gcm.nonce;
  69. int rc;
  70. if (key_len < 4)
  71. return -EINVAL;
  72. key_len -= 4;
  73. rc = gcm_aes_nx_set_key(tfm, in_key, key_len);
  74. if (rc)
  75. goto out;
  76. memcpy(nonce, in_key + key_len, 4);
  77. out:
  78. return rc;
  79. }
  80. static int gcm4106_aes_nx_setauthsize(struct crypto_aead *tfm,
  81. unsigned int authsize)
  82. {
  83. switch (authsize) {
  84. case 8:
  85. case 12:
  86. case 16:
  87. break;
  88. default:
  89. return -EINVAL;
  90. }
  91. return 0;
  92. }
  93. static int nx_gca(struct nx_crypto_ctx *nx_ctx,
  94. struct aead_request *req,
  95. u8 *out,
  96. unsigned int assoclen)
  97. {
  98. int rc;
  99. struct nx_csbcpb *csbcpb_aead = nx_ctx->csbcpb_aead;
  100. struct scatter_walk walk;
  101. struct nx_sg *nx_sg = nx_ctx->in_sg;
  102. unsigned int nbytes = assoclen;
  103. unsigned int processed = 0, to_process;
  104. unsigned int max_sg_len;
  105. if (nbytes <= AES_BLOCK_SIZE) {
  106. scatterwalk_start(&walk, req->src);
  107. scatterwalk_copychunks(out, &walk, nbytes, SCATTERWALK_FROM_SG);
  108. scatterwalk_done(&walk, SCATTERWALK_FROM_SG, 0);
  109. return 0;
  110. }
  111. NX_CPB_FDM(csbcpb_aead) &= ~NX_FDM_CONTINUATION;
  112. /* page_limit: number of sg entries that fit on one page */
  113. max_sg_len = min_t(u64, nx_driver.of.max_sg_len/sizeof(struct nx_sg),
  114. nx_ctx->ap->sglen);
  115. max_sg_len = min_t(u64, max_sg_len,
  116. nx_ctx->ap->databytelen/NX_PAGE_SIZE);
  117. do {
  118. /*
  119. * to_process: the data chunk to process in this update.
  120. * This value is bound by sg list limits.
  121. */
  122. to_process = min_t(u64, nbytes - processed,
  123. nx_ctx->ap->databytelen);
  124. to_process = min_t(u64, to_process,
  125. NX_PAGE_SIZE * (max_sg_len - 1));
  126. nx_sg = nx_walk_and_build(nx_ctx->in_sg, max_sg_len,
  127. req->src, processed, &to_process);
  128. if ((to_process + processed) < nbytes)
  129. NX_CPB_FDM(csbcpb_aead) |= NX_FDM_INTERMEDIATE;
  130. else
  131. NX_CPB_FDM(csbcpb_aead) &= ~NX_FDM_INTERMEDIATE;
  132. nx_ctx->op_aead.inlen = (nx_ctx->in_sg - nx_sg)
  133. * sizeof(struct nx_sg);
  134. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op_aead,
  135. req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  136. if (rc)
  137. return rc;
  138. memcpy(csbcpb_aead->cpb.aes_gca.in_pat,
  139. csbcpb_aead->cpb.aes_gca.out_pat,
  140. AES_BLOCK_SIZE);
  141. NX_CPB_FDM(csbcpb_aead) |= NX_FDM_CONTINUATION;
  142. atomic_inc(&(nx_ctx->stats->aes_ops));
  143. atomic64_add(assoclen, &(nx_ctx->stats->aes_bytes));
  144. processed += to_process;
  145. } while (processed < nbytes);
  146. memcpy(out, csbcpb_aead->cpb.aes_gca.out_pat, AES_BLOCK_SIZE);
  147. return rc;
  148. }
  149. static int gmac(struct aead_request *req, struct blkcipher_desc *desc,
  150. unsigned int assoclen)
  151. {
  152. int rc;
  153. struct nx_crypto_ctx *nx_ctx =
  154. crypto_aead_ctx(crypto_aead_reqtfm(req));
  155. struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
  156. struct nx_sg *nx_sg;
  157. unsigned int nbytes = assoclen;
  158. unsigned int processed = 0, to_process;
  159. unsigned int max_sg_len;
  160. /* Set GMAC mode */
  161. csbcpb->cpb.hdr.mode = NX_MODE_AES_GMAC;
  162. NX_CPB_FDM(csbcpb) &= ~NX_FDM_CONTINUATION;
  163. /* page_limit: number of sg entries that fit on one page */
  164. max_sg_len = min_t(u64, nx_driver.of.max_sg_len/sizeof(struct nx_sg),
  165. nx_ctx->ap->sglen);
  166. max_sg_len = min_t(u64, max_sg_len,
  167. nx_ctx->ap->databytelen/NX_PAGE_SIZE);
  168. /* Copy IV */
  169. memcpy(csbcpb->cpb.aes_gcm.iv_or_cnt, desc->info, AES_BLOCK_SIZE);
  170. do {
  171. /*
  172. * to_process: the data chunk to process in this update.
  173. * This value is bound by sg list limits.
  174. */
  175. to_process = min_t(u64, nbytes - processed,
  176. nx_ctx->ap->databytelen);
  177. to_process = min_t(u64, to_process,
  178. NX_PAGE_SIZE * (max_sg_len - 1));
  179. nx_sg = nx_walk_and_build(nx_ctx->in_sg, max_sg_len,
  180. req->src, processed, &to_process);
  181. if ((to_process + processed) < nbytes)
  182. NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE;
  183. else
  184. NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
  185. nx_ctx->op.inlen = (nx_ctx->in_sg - nx_sg)
  186. * sizeof(struct nx_sg);
  187. csbcpb->cpb.aes_gcm.bit_length_data = 0;
  188. csbcpb->cpb.aes_gcm.bit_length_aad = 8 * nbytes;
  189. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
  190. req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  191. if (rc)
  192. goto out;
  193. memcpy(csbcpb->cpb.aes_gcm.in_pat_or_aad,
  194. csbcpb->cpb.aes_gcm.out_pat_or_mac, AES_BLOCK_SIZE);
  195. memcpy(csbcpb->cpb.aes_gcm.in_s0,
  196. csbcpb->cpb.aes_gcm.out_s0, AES_BLOCK_SIZE);
  197. NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
  198. atomic_inc(&(nx_ctx->stats->aes_ops));
  199. atomic64_add(assoclen, &(nx_ctx->stats->aes_bytes));
  200. processed += to_process;
  201. } while (processed < nbytes);
  202. out:
  203. /* Restore GCM mode */
  204. csbcpb->cpb.hdr.mode = NX_MODE_AES_GCM;
  205. return rc;
  206. }
  207. static int gcm_empty(struct aead_request *req, struct blkcipher_desc *desc,
  208. int enc)
  209. {
  210. int rc;
  211. struct nx_crypto_ctx *nx_ctx =
  212. crypto_aead_ctx(crypto_aead_reqtfm(req));
  213. struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
  214. char out[AES_BLOCK_SIZE];
  215. struct nx_sg *in_sg, *out_sg;
  216. int len;
  217. /* For scenarios where the input message is zero length, AES CTR mode
  218. * may be used. Set the source data to be a single block (16B) of all
  219. * zeros, and set the input IV value to be the same as the GMAC IV
  220. * value. - nx_wb 4.8.1.3 */
  221. /* Change to ECB mode */
  222. csbcpb->cpb.hdr.mode = NX_MODE_AES_ECB;
  223. memcpy(csbcpb->cpb.aes_ecb.key, csbcpb->cpb.aes_gcm.key,
  224. sizeof(csbcpb->cpb.aes_ecb.key));
  225. if (enc)
  226. NX_CPB_FDM(csbcpb) |= NX_FDM_ENDE_ENCRYPT;
  227. else
  228. NX_CPB_FDM(csbcpb) &= ~NX_FDM_ENDE_ENCRYPT;
  229. len = AES_BLOCK_SIZE;
  230. /* Encrypt the counter/IV */
  231. in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *) desc->info,
  232. &len, nx_ctx->ap->sglen);
  233. if (len != AES_BLOCK_SIZE)
  234. return -EINVAL;
  235. len = sizeof(out);
  236. out_sg = nx_build_sg_list(nx_ctx->out_sg, (u8 *) out, &len,
  237. nx_ctx->ap->sglen);
  238. if (len != sizeof(out))
  239. return -EINVAL;
  240. nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
  241. nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
  242. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
  243. desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  244. if (rc)
  245. goto out;
  246. atomic_inc(&(nx_ctx->stats->aes_ops));
  247. /* Copy out the auth tag */
  248. memcpy(csbcpb->cpb.aes_gcm.out_pat_or_mac, out,
  249. crypto_aead_authsize(crypto_aead_reqtfm(req)));
  250. out:
  251. /* Restore XCBC mode */
  252. csbcpb->cpb.hdr.mode = NX_MODE_AES_GCM;
  253. /*
  254. * ECB key uses the same region that GCM AAD and counter, so it's safe
  255. * to just fill it with zeroes.
  256. */
  257. memset(csbcpb->cpb.aes_ecb.key, 0, sizeof(csbcpb->cpb.aes_ecb.key));
  258. return rc;
  259. }
  260. static int gcm_aes_nx_crypt(struct aead_request *req, int enc,
  261. unsigned int assoclen)
  262. {
  263. struct nx_crypto_ctx *nx_ctx =
  264. crypto_aead_ctx(crypto_aead_reqtfm(req));
  265. struct nx_gcm_rctx *rctx = aead_request_ctx(req);
  266. struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
  267. struct blkcipher_desc desc;
  268. unsigned int nbytes = req->cryptlen;
  269. unsigned int processed = 0, to_process;
  270. unsigned long irq_flags;
  271. int rc = -EINVAL;
  272. spin_lock_irqsave(&nx_ctx->lock, irq_flags);
  273. desc.info = rctx->iv;
  274. /* initialize the counter */
  275. *(u32 *)(desc.info + NX_GCM_CTR_OFFSET) = 1;
  276. if (nbytes == 0) {
  277. if (assoclen == 0)
  278. rc = gcm_empty(req, &desc, enc);
  279. else
  280. rc = gmac(req, &desc, assoclen);
  281. if (rc)
  282. goto out;
  283. else
  284. goto mac;
  285. }
  286. /* Process associated data */
  287. csbcpb->cpb.aes_gcm.bit_length_aad = assoclen * 8;
  288. if (assoclen) {
  289. rc = nx_gca(nx_ctx, req, csbcpb->cpb.aes_gcm.in_pat_or_aad,
  290. assoclen);
  291. if (rc)
  292. goto out;
  293. }
  294. /* Set flags for encryption */
  295. NX_CPB_FDM(csbcpb) &= ~NX_FDM_CONTINUATION;
  296. if (enc) {
  297. NX_CPB_FDM(csbcpb) |= NX_FDM_ENDE_ENCRYPT;
  298. } else {
  299. NX_CPB_FDM(csbcpb) &= ~NX_FDM_ENDE_ENCRYPT;
  300. nbytes -= crypto_aead_authsize(crypto_aead_reqtfm(req));
  301. }
  302. do {
  303. to_process = nbytes - processed;
  304. csbcpb->cpb.aes_gcm.bit_length_data = nbytes * 8;
  305. rc = nx_build_sg_lists(nx_ctx, &desc, req->dst,
  306. req->src, &to_process,
  307. processed + req->assoclen,
  308. csbcpb->cpb.aes_gcm.iv_or_cnt);
  309. if (rc)
  310. goto out;
  311. if ((to_process + processed) < nbytes)
  312. NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE;
  313. else
  314. NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
  315. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
  316. req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  317. if (rc)
  318. goto out;
  319. memcpy(desc.info, csbcpb->cpb.aes_gcm.out_cnt, AES_BLOCK_SIZE);
  320. memcpy(csbcpb->cpb.aes_gcm.in_pat_or_aad,
  321. csbcpb->cpb.aes_gcm.out_pat_or_mac, AES_BLOCK_SIZE);
  322. memcpy(csbcpb->cpb.aes_gcm.in_s0,
  323. csbcpb->cpb.aes_gcm.out_s0, AES_BLOCK_SIZE);
  324. NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
  325. atomic_inc(&(nx_ctx->stats->aes_ops));
  326. atomic64_add(csbcpb->csb.processed_byte_count,
  327. &(nx_ctx->stats->aes_bytes));
  328. processed += to_process;
  329. } while (processed < nbytes);
  330. mac:
  331. if (enc) {
  332. /* copy out the auth tag */
  333. scatterwalk_map_and_copy(
  334. csbcpb->cpb.aes_gcm.out_pat_or_mac,
  335. req->dst, req->assoclen + nbytes,
  336. crypto_aead_authsize(crypto_aead_reqtfm(req)),
  337. SCATTERWALK_TO_SG);
  338. } else {
  339. u8 *itag = nx_ctx->priv.gcm.iauth_tag;
  340. u8 *otag = csbcpb->cpb.aes_gcm.out_pat_or_mac;
  341. scatterwalk_map_and_copy(
  342. itag, req->src, req->assoclen + nbytes,
  343. crypto_aead_authsize(crypto_aead_reqtfm(req)),
  344. SCATTERWALK_FROM_SG);
  345. rc = crypto_memneq(itag, otag,
  346. crypto_aead_authsize(crypto_aead_reqtfm(req))) ?
  347. -EBADMSG : 0;
  348. }
  349. out:
  350. spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
  351. return rc;
  352. }
  353. static int gcm_aes_nx_encrypt(struct aead_request *req)
  354. {
  355. struct nx_gcm_rctx *rctx = aead_request_ctx(req);
  356. char *iv = rctx->iv;
  357. memcpy(iv, req->iv, 12);
  358. return gcm_aes_nx_crypt(req, 1, req->assoclen);
  359. }
  360. static int gcm_aes_nx_decrypt(struct aead_request *req)
  361. {
  362. struct nx_gcm_rctx *rctx = aead_request_ctx(req);
  363. char *iv = rctx->iv;
  364. memcpy(iv, req->iv, 12);
  365. return gcm_aes_nx_crypt(req, 0, req->assoclen);
  366. }
  367. static int gcm4106_aes_nx_encrypt(struct aead_request *req)
  368. {
  369. struct nx_crypto_ctx *nx_ctx =
  370. crypto_aead_ctx(crypto_aead_reqtfm(req));
  371. struct nx_gcm_rctx *rctx = aead_request_ctx(req);
  372. char *iv = rctx->iv;
  373. char *nonce = nx_ctx->priv.gcm.nonce;
  374. memcpy(iv, nonce, NX_GCM4106_NONCE_LEN);
  375. memcpy(iv + NX_GCM4106_NONCE_LEN, req->iv, 8);
  376. if (req->assoclen < 8)
  377. return -EINVAL;
  378. return gcm_aes_nx_crypt(req, 1, req->assoclen - 8);
  379. }
  380. static int gcm4106_aes_nx_decrypt(struct aead_request *req)
  381. {
  382. struct nx_crypto_ctx *nx_ctx =
  383. crypto_aead_ctx(crypto_aead_reqtfm(req));
  384. struct nx_gcm_rctx *rctx = aead_request_ctx(req);
  385. char *iv = rctx->iv;
  386. char *nonce = nx_ctx->priv.gcm.nonce;
  387. memcpy(iv, nonce, NX_GCM4106_NONCE_LEN);
  388. memcpy(iv + NX_GCM4106_NONCE_LEN, req->iv, 8);
  389. if (req->assoclen < 8)
  390. return -EINVAL;
  391. return gcm_aes_nx_crypt(req, 0, req->assoclen - 8);
  392. }
  393. /* tell the block cipher walk routines that this is a stream cipher by
  394. * setting cra_blocksize to 1. Even using blkcipher_walk_virt_block
  395. * during encrypt/decrypt doesn't solve this problem, because it calls
  396. * blkcipher_walk_done under the covers, which doesn't use walk->blocksize,
  397. * but instead uses this tfm->blocksize. */
  398. struct aead_alg nx_gcm_aes_alg = {
  399. .base = {
  400. .cra_name = "gcm(aes)",
  401. .cra_driver_name = "gcm-aes-nx",
  402. .cra_priority = 300,
  403. .cra_blocksize = 1,
  404. .cra_ctxsize = sizeof(struct nx_crypto_ctx),
  405. .cra_module = THIS_MODULE,
  406. },
  407. .init = nx_crypto_ctx_aes_gcm_init,
  408. .exit = nx_crypto_ctx_aead_exit,
  409. .ivsize = 12,
  410. .maxauthsize = AES_BLOCK_SIZE,
  411. .setkey = gcm_aes_nx_set_key,
  412. .encrypt = gcm_aes_nx_encrypt,
  413. .decrypt = gcm_aes_nx_decrypt,
  414. };
  415. struct aead_alg nx_gcm4106_aes_alg = {
  416. .base = {
  417. .cra_name = "rfc4106(gcm(aes))",
  418. .cra_driver_name = "rfc4106-gcm-aes-nx",
  419. .cra_priority = 300,
  420. .cra_blocksize = 1,
  421. .cra_ctxsize = sizeof(struct nx_crypto_ctx),
  422. .cra_module = THIS_MODULE,
  423. },
  424. .init = nx_crypto_ctx_aes_gcm_init,
  425. .exit = nx_crypto_ctx_aead_exit,
  426. .ivsize = 8,
  427. .maxauthsize = AES_BLOCK_SIZE,
  428. .setkey = gcm4106_aes_nx_set_key,
  429. .setauthsize = gcm4106_aes_nx_setauthsize,
  430. .encrypt = gcm4106_aes_nx_encrypt,
  431. .decrypt = gcm4106_aes_nx_decrypt,
  432. };