nx-aes-gcm.c 14 KB

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