chacha20poly1305.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. /*
  2. * ChaCha20-Poly1305 AEAD, RFC7539
  3. *
  4. * Copyright (C) 2015 Martin Willi
  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; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <crypto/internal/aead.h>
  12. #include <crypto/internal/hash.h>
  13. #include <crypto/internal/skcipher.h>
  14. #include <crypto/scatterwalk.h>
  15. #include <linux/err.h>
  16. #include <linux/init.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include "internal.h"
  20. #define POLY1305_BLOCK_SIZE 16
  21. #define POLY1305_DIGEST_SIZE 16
  22. #define POLY1305_KEY_SIZE 32
  23. #define CHACHA20_KEY_SIZE 32
  24. #define CHACHA20_IV_SIZE 16
  25. #define CHACHAPOLY_IV_SIZE 12
  26. struct chachapoly_instance_ctx {
  27. struct crypto_skcipher_spawn chacha;
  28. struct crypto_ahash_spawn poly;
  29. unsigned int saltlen;
  30. };
  31. struct chachapoly_ctx {
  32. struct crypto_ablkcipher *chacha;
  33. struct crypto_ahash *poly;
  34. /* key bytes we use for the ChaCha20 IV */
  35. unsigned int saltlen;
  36. u8 salt[];
  37. };
  38. struct poly_req {
  39. /* zero byte padding for AD/ciphertext, as needed */
  40. u8 pad[POLY1305_BLOCK_SIZE];
  41. /* tail data with AD/ciphertext lengths */
  42. struct {
  43. __le64 assoclen;
  44. __le64 cryptlen;
  45. } tail;
  46. struct scatterlist src[1];
  47. struct ahash_request req; /* must be last member */
  48. };
  49. struct chacha_req {
  50. u8 iv[CHACHA20_IV_SIZE];
  51. struct scatterlist src[1];
  52. struct ablkcipher_request req; /* must be last member */
  53. };
  54. struct chachapoly_req_ctx {
  55. /* the key we generate for Poly1305 using Chacha20 */
  56. u8 key[POLY1305_KEY_SIZE];
  57. /* calculated Poly1305 tag */
  58. u8 tag[POLY1305_DIGEST_SIZE];
  59. /* length of data to en/decrypt, without ICV */
  60. unsigned int cryptlen;
  61. union {
  62. struct poly_req poly;
  63. struct chacha_req chacha;
  64. } u;
  65. };
  66. static inline void async_done_continue(struct aead_request *req, int err,
  67. int (*cont)(struct aead_request *))
  68. {
  69. if (!err)
  70. err = cont(req);
  71. if (err != -EINPROGRESS && err != -EBUSY)
  72. aead_request_complete(req, err);
  73. }
  74. static void chacha_iv(u8 *iv, struct aead_request *req, u32 icb)
  75. {
  76. struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
  77. __le32 leicb = cpu_to_le32(icb);
  78. memcpy(iv, &leicb, sizeof(leicb));
  79. memcpy(iv + sizeof(leicb), ctx->salt, ctx->saltlen);
  80. memcpy(iv + sizeof(leicb) + ctx->saltlen, req->iv,
  81. CHACHA20_IV_SIZE - sizeof(leicb) - ctx->saltlen);
  82. }
  83. static int poly_verify_tag(struct aead_request *req)
  84. {
  85. struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
  86. u8 tag[sizeof(rctx->tag)];
  87. scatterwalk_map_and_copy(tag, req->src, rctx->cryptlen, sizeof(tag), 0);
  88. if (crypto_memneq(tag, rctx->tag, sizeof(tag)))
  89. return -EBADMSG;
  90. return 0;
  91. }
  92. static int poly_copy_tag(struct aead_request *req)
  93. {
  94. struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
  95. scatterwalk_map_and_copy(rctx->tag, req->dst, rctx->cryptlen,
  96. sizeof(rctx->tag), 1);
  97. return 0;
  98. }
  99. static void chacha_decrypt_done(struct crypto_async_request *areq, int err)
  100. {
  101. async_done_continue(areq->data, err, poly_verify_tag);
  102. }
  103. static int chacha_decrypt(struct aead_request *req)
  104. {
  105. struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
  106. struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
  107. struct chacha_req *creq = &rctx->u.chacha;
  108. int err;
  109. chacha_iv(creq->iv, req, 1);
  110. ablkcipher_request_set_callback(&creq->req, aead_request_flags(req),
  111. chacha_decrypt_done, req);
  112. ablkcipher_request_set_tfm(&creq->req, ctx->chacha);
  113. ablkcipher_request_set_crypt(&creq->req, req->src, req->dst,
  114. rctx->cryptlen, creq->iv);
  115. err = crypto_ablkcipher_decrypt(&creq->req);
  116. if (err)
  117. return err;
  118. return poly_verify_tag(req);
  119. }
  120. static int poly_tail_continue(struct aead_request *req)
  121. {
  122. struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
  123. if (rctx->cryptlen == req->cryptlen) /* encrypting */
  124. return poly_copy_tag(req);
  125. return chacha_decrypt(req);
  126. }
  127. static void poly_tail_done(struct crypto_async_request *areq, int err)
  128. {
  129. async_done_continue(areq->data, err, poly_tail_continue);
  130. }
  131. static int poly_tail(struct aead_request *req)
  132. {
  133. struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
  134. struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
  135. struct poly_req *preq = &rctx->u.poly;
  136. __le64 len;
  137. int err;
  138. sg_init_table(preq->src, 1);
  139. len = cpu_to_le64(req->assoclen);
  140. memcpy(&preq->tail.assoclen, &len, sizeof(len));
  141. len = cpu_to_le64(rctx->cryptlen);
  142. memcpy(&preq->tail.cryptlen, &len, sizeof(len));
  143. sg_set_buf(preq->src, &preq->tail, sizeof(preq->tail));
  144. ahash_request_set_callback(&preq->req, aead_request_flags(req),
  145. poly_tail_done, req);
  146. ahash_request_set_tfm(&preq->req, ctx->poly);
  147. ahash_request_set_crypt(&preq->req, preq->src,
  148. rctx->tag, sizeof(preq->tail));
  149. err = crypto_ahash_finup(&preq->req);
  150. if (err)
  151. return err;
  152. return poly_tail_continue(req);
  153. }
  154. static void poly_cipherpad_done(struct crypto_async_request *areq, int err)
  155. {
  156. async_done_continue(areq->data, err, poly_tail);
  157. }
  158. static int poly_cipherpad(struct aead_request *req)
  159. {
  160. struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
  161. struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
  162. struct poly_req *preq = &rctx->u.poly;
  163. unsigned int padlen, bs = POLY1305_BLOCK_SIZE;
  164. int err;
  165. padlen = (bs - (rctx->cryptlen % bs)) % bs;
  166. memset(preq->pad, 0, sizeof(preq->pad));
  167. sg_init_table(preq->src, 1);
  168. sg_set_buf(preq->src, &preq->pad, padlen);
  169. ahash_request_set_callback(&preq->req, aead_request_flags(req),
  170. poly_cipherpad_done, req);
  171. ahash_request_set_tfm(&preq->req, ctx->poly);
  172. ahash_request_set_crypt(&preq->req, preq->src, NULL, padlen);
  173. err = crypto_ahash_update(&preq->req);
  174. if (err)
  175. return err;
  176. return poly_tail(req);
  177. }
  178. static void poly_cipher_done(struct crypto_async_request *areq, int err)
  179. {
  180. async_done_continue(areq->data, err, poly_cipherpad);
  181. }
  182. static int poly_cipher(struct aead_request *req)
  183. {
  184. struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
  185. struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
  186. struct poly_req *preq = &rctx->u.poly;
  187. struct scatterlist *crypt = req->src;
  188. int err;
  189. if (rctx->cryptlen == req->cryptlen) /* encrypting */
  190. crypt = req->dst;
  191. ahash_request_set_callback(&preq->req, aead_request_flags(req),
  192. poly_cipher_done, req);
  193. ahash_request_set_tfm(&preq->req, ctx->poly);
  194. ahash_request_set_crypt(&preq->req, crypt, NULL, rctx->cryptlen);
  195. err = crypto_ahash_update(&preq->req);
  196. if (err)
  197. return err;
  198. return poly_cipherpad(req);
  199. }
  200. static void poly_adpad_done(struct crypto_async_request *areq, int err)
  201. {
  202. async_done_continue(areq->data, err, poly_cipher);
  203. }
  204. static int poly_adpad(struct aead_request *req)
  205. {
  206. struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
  207. struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
  208. struct poly_req *preq = &rctx->u.poly;
  209. unsigned int padlen, bs = POLY1305_BLOCK_SIZE;
  210. int err;
  211. padlen = (bs - (req->assoclen % bs)) % bs;
  212. memset(preq->pad, 0, sizeof(preq->pad));
  213. sg_init_table(preq->src, 1);
  214. sg_set_buf(preq->src, preq->pad, padlen);
  215. ahash_request_set_callback(&preq->req, aead_request_flags(req),
  216. poly_adpad_done, req);
  217. ahash_request_set_tfm(&preq->req, ctx->poly);
  218. ahash_request_set_crypt(&preq->req, preq->src, NULL, padlen);
  219. err = crypto_ahash_update(&preq->req);
  220. if (err)
  221. return err;
  222. return poly_cipher(req);
  223. }
  224. static void poly_ad_done(struct crypto_async_request *areq, int err)
  225. {
  226. async_done_continue(areq->data, err, poly_adpad);
  227. }
  228. static int poly_ad(struct aead_request *req)
  229. {
  230. struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
  231. struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
  232. struct poly_req *preq = &rctx->u.poly;
  233. int err;
  234. ahash_request_set_callback(&preq->req, aead_request_flags(req),
  235. poly_ad_done, req);
  236. ahash_request_set_tfm(&preq->req, ctx->poly);
  237. ahash_request_set_crypt(&preq->req, req->assoc, NULL, req->assoclen);
  238. err = crypto_ahash_update(&preq->req);
  239. if (err)
  240. return err;
  241. return poly_adpad(req);
  242. }
  243. static void poly_setkey_done(struct crypto_async_request *areq, int err)
  244. {
  245. async_done_continue(areq->data, err, poly_ad);
  246. }
  247. static int poly_setkey(struct aead_request *req)
  248. {
  249. struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
  250. struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
  251. struct poly_req *preq = &rctx->u.poly;
  252. int err;
  253. sg_init_table(preq->src, 1);
  254. sg_set_buf(preq->src, rctx->key, sizeof(rctx->key));
  255. ahash_request_set_callback(&preq->req, aead_request_flags(req),
  256. poly_setkey_done, req);
  257. ahash_request_set_tfm(&preq->req, ctx->poly);
  258. ahash_request_set_crypt(&preq->req, preq->src, NULL, sizeof(rctx->key));
  259. err = crypto_ahash_update(&preq->req);
  260. if (err)
  261. return err;
  262. return poly_ad(req);
  263. }
  264. static void poly_init_done(struct crypto_async_request *areq, int err)
  265. {
  266. async_done_continue(areq->data, err, poly_setkey);
  267. }
  268. static int poly_init(struct aead_request *req)
  269. {
  270. struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
  271. struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
  272. struct poly_req *preq = &rctx->u.poly;
  273. int err;
  274. ahash_request_set_callback(&preq->req, aead_request_flags(req),
  275. poly_init_done, req);
  276. ahash_request_set_tfm(&preq->req, ctx->poly);
  277. err = crypto_ahash_init(&preq->req);
  278. if (err)
  279. return err;
  280. return poly_setkey(req);
  281. }
  282. static void poly_genkey_done(struct crypto_async_request *areq, int err)
  283. {
  284. async_done_continue(areq->data, err, poly_init);
  285. }
  286. static int poly_genkey(struct aead_request *req)
  287. {
  288. struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
  289. struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
  290. struct chacha_req *creq = &rctx->u.chacha;
  291. int err;
  292. sg_init_table(creq->src, 1);
  293. memset(rctx->key, 0, sizeof(rctx->key));
  294. sg_set_buf(creq->src, rctx->key, sizeof(rctx->key));
  295. chacha_iv(creq->iv, req, 0);
  296. ablkcipher_request_set_callback(&creq->req, aead_request_flags(req),
  297. poly_genkey_done, req);
  298. ablkcipher_request_set_tfm(&creq->req, ctx->chacha);
  299. ablkcipher_request_set_crypt(&creq->req, creq->src, creq->src,
  300. POLY1305_KEY_SIZE, creq->iv);
  301. err = crypto_ablkcipher_decrypt(&creq->req);
  302. if (err)
  303. return err;
  304. return poly_init(req);
  305. }
  306. static void chacha_encrypt_done(struct crypto_async_request *areq, int err)
  307. {
  308. async_done_continue(areq->data, err, poly_genkey);
  309. }
  310. static int chacha_encrypt(struct aead_request *req)
  311. {
  312. struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
  313. struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
  314. struct chacha_req *creq = &rctx->u.chacha;
  315. int err;
  316. chacha_iv(creq->iv, req, 1);
  317. ablkcipher_request_set_callback(&creq->req, aead_request_flags(req),
  318. chacha_encrypt_done, req);
  319. ablkcipher_request_set_tfm(&creq->req, ctx->chacha);
  320. ablkcipher_request_set_crypt(&creq->req, req->src, req->dst,
  321. req->cryptlen, creq->iv);
  322. err = crypto_ablkcipher_encrypt(&creq->req);
  323. if (err)
  324. return err;
  325. return poly_genkey(req);
  326. }
  327. static int chachapoly_encrypt(struct aead_request *req)
  328. {
  329. struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
  330. rctx->cryptlen = req->cryptlen;
  331. /* encrypt call chain:
  332. * - chacha_encrypt/done()
  333. * - poly_genkey/done()
  334. * - poly_init/done()
  335. * - poly_setkey/done()
  336. * - poly_ad/done()
  337. * - poly_adpad/done()
  338. * - poly_cipher/done()
  339. * - poly_cipherpad/done()
  340. * - poly_tail/done/continue()
  341. * - poly_copy_tag()
  342. */
  343. return chacha_encrypt(req);
  344. }
  345. static int chachapoly_decrypt(struct aead_request *req)
  346. {
  347. struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
  348. if (req->cryptlen < POLY1305_DIGEST_SIZE)
  349. return -EINVAL;
  350. rctx->cryptlen = req->cryptlen - POLY1305_DIGEST_SIZE;
  351. /* decrypt call chain:
  352. * - poly_genkey/done()
  353. * - poly_init/done()
  354. * - poly_setkey/done()
  355. * - poly_ad/done()
  356. * - poly_adpad/done()
  357. * - poly_cipher/done()
  358. * - poly_cipherpad/done()
  359. * - poly_tail/done/continue()
  360. * - chacha_decrypt/done()
  361. * - poly_verify_tag()
  362. */
  363. return poly_genkey(req);
  364. }
  365. static int chachapoly_setkey(struct crypto_aead *aead, const u8 *key,
  366. unsigned int keylen)
  367. {
  368. struct chachapoly_ctx *ctx = crypto_aead_ctx(aead);
  369. int err;
  370. if (keylen != ctx->saltlen + CHACHA20_KEY_SIZE)
  371. return -EINVAL;
  372. keylen -= ctx->saltlen;
  373. memcpy(ctx->salt, key + keylen, ctx->saltlen);
  374. crypto_ablkcipher_clear_flags(ctx->chacha, CRYPTO_TFM_REQ_MASK);
  375. crypto_ablkcipher_set_flags(ctx->chacha, crypto_aead_get_flags(aead) &
  376. CRYPTO_TFM_REQ_MASK);
  377. err = crypto_ablkcipher_setkey(ctx->chacha, key, keylen);
  378. crypto_aead_set_flags(aead, crypto_ablkcipher_get_flags(ctx->chacha) &
  379. CRYPTO_TFM_RES_MASK);
  380. return err;
  381. }
  382. static int chachapoly_setauthsize(struct crypto_aead *tfm,
  383. unsigned int authsize)
  384. {
  385. if (authsize != POLY1305_DIGEST_SIZE)
  386. return -EINVAL;
  387. return 0;
  388. }
  389. static int chachapoly_init(struct crypto_tfm *tfm)
  390. {
  391. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  392. struct chachapoly_instance_ctx *ictx = crypto_instance_ctx(inst);
  393. struct chachapoly_ctx *ctx = crypto_tfm_ctx(tfm);
  394. struct crypto_ablkcipher *chacha;
  395. struct crypto_ahash *poly;
  396. unsigned long align;
  397. poly = crypto_spawn_ahash(&ictx->poly);
  398. if (IS_ERR(poly))
  399. return PTR_ERR(poly);
  400. chacha = crypto_spawn_skcipher(&ictx->chacha);
  401. if (IS_ERR(chacha)) {
  402. crypto_free_ahash(poly);
  403. return PTR_ERR(chacha);
  404. }
  405. ctx->chacha = chacha;
  406. ctx->poly = poly;
  407. ctx->saltlen = ictx->saltlen;
  408. align = crypto_tfm_alg_alignmask(tfm);
  409. align &= ~(crypto_tfm_ctx_alignment() - 1);
  410. crypto_aead_set_reqsize(__crypto_aead_cast(tfm),
  411. align + offsetof(struct chachapoly_req_ctx, u) +
  412. max(offsetof(struct chacha_req, req) +
  413. sizeof(struct ablkcipher_request) +
  414. crypto_ablkcipher_reqsize(chacha),
  415. offsetof(struct poly_req, req) +
  416. sizeof(struct ahash_request) +
  417. crypto_ahash_reqsize(poly)));
  418. return 0;
  419. }
  420. static void chachapoly_exit(struct crypto_tfm *tfm)
  421. {
  422. struct chachapoly_ctx *ctx = crypto_tfm_ctx(tfm);
  423. crypto_free_ahash(ctx->poly);
  424. crypto_free_ablkcipher(ctx->chacha);
  425. }
  426. static struct crypto_instance *chachapoly_alloc(struct rtattr **tb,
  427. const char *name,
  428. unsigned int ivsize)
  429. {
  430. struct crypto_attr_type *algt;
  431. struct crypto_instance *inst;
  432. struct crypto_alg *chacha;
  433. struct crypto_alg *poly;
  434. struct ahash_alg *poly_ahash;
  435. struct chachapoly_instance_ctx *ctx;
  436. const char *chacha_name, *poly_name;
  437. int err;
  438. if (ivsize > CHACHAPOLY_IV_SIZE)
  439. return ERR_PTR(-EINVAL);
  440. algt = crypto_get_attr_type(tb);
  441. if (IS_ERR(algt))
  442. return ERR_CAST(algt);
  443. if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
  444. return ERR_PTR(-EINVAL);
  445. chacha_name = crypto_attr_alg_name(tb[1]);
  446. if (IS_ERR(chacha_name))
  447. return ERR_CAST(chacha_name);
  448. poly_name = crypto_attr_alg_name(tb[2]);
  449. if (IS_ERR(poly_name))
  450. return ERR_CAST(poly_name);
  451. poly = crypto_find_alg(poly_name, &crypto_ahash_type,
  452. CRYPTO_ALG_TYPE_HASH,
  453. CRYPTO_ALG_TYPE_AHASH_MASK);
  454. if (IS_ERR(poly))
  455. return ERR_CAST(poly);
  456. err = -ENOMEM;
  457. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  458. if (!inst)
  459. goto out_put_poly;
  460. ctx = crypto_instance_ctx(inst);
  461. ctx->saltlen = CHACHAPOLY_IV_SIZE - ivsize;
  462. poly_ahash = container_of(poly, struct ahash_alg, halg.base);
  463. err = crypto_init_ahash_spawn(&ctx->poly, &poly_ahash->halg, inst);
  464. if (err)
  465. goto err_free_inst;
  466. crypto_set_skcipher_spawn(&ctx->chacha, inst);
  467. err = crypto_grab_skcipher(&ctx->chacha, chacha_name, 0,
  468. crypto_requires_sync(algt->type,
  469. algt->mask));
  470. if (err)
  471. goto err_drop_poly;
  472. chacha = crypto_skcipher_spawn_alg(&ctx->chacha);
  473. err = -EINVAL;
  474. /* Need 16-byte IV size, including Initial Block Counter value */
  475. if (chacha->cra_ablkcipher.ivsize != CHACHA20_IV_SIZE)
  476. goto out_drop_chacha;
  477. /* Not a stream cipher? */
  478. if (chacha->cra_blocksize != 1)
  479. goto out_drop_chacha;
  480. err = -ENAMETOOLONG;
  481. if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
  482. "%s(%s,%s)", name, chacha_name,
  483. poly_name) >= CRYPTO_MAX_ALG_NAME)
  484. goto out_drop_chacha;
  485. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  486. "%s(%s,%s)", name, chacha->cra_driver_name,
  487. poly->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  488. goto out_drop_chacha;
  489. inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD;
  490. inst->alg.cra_flags |= (chacha->cra_flags |
  491. poly->cra_flags) & CRYPTO_ALG_ASYNC;
  492. inst->alg.cra_priority = (chacha->cra_priority +
  493. poly->cra_priority) / 2;
  494. inst->alg.cra_blocksize = 1;
  495. inst->alg.cra_alignmask = chacha->cra_alignmask | poly->cra_alignmask;
  496. inst->alg.cra_type = &crypto_nivaead_type;
  497. inst->alg.cra_aead.ivsize = ivsize;
  498. inst->alg.cra_aead.maxauthsize = POLY1305_DIGEST_SIZE;
  499. inst->alg.cra_ctxsize = sizeof(struct chachapoly_ctx) + ctx->saltlen;
  500. inst->alg.cra_init = chachapoly_init;
  501. inst->alg.cra_exit = chachapoly_exit;
  502. inst->alg.cra_aead.encrypt = chachapoly_encrypt;
  503. inst->alg.cra_aead.decrypt = chachapoly_decrypt;
  504. inst->alg.cra_aead.setkey = chachapoly_setkey;
  505. inst->alg.cra_aead.setauthsize = chachapoly_setauthsize;
  506. inst->alg.cra_aead.geniv = "seqiv";
  507. out:
  508. crypto_mod_put(poly);
  509. return inst;
  510. out_drop_chacha:
  511. crypto_drop_skcipher(&ctx->chacha);
  512. err_drop_poly:
  513. crypto_drop_ahash(&ctx->poly);
  514. err_free_inst:
  515. kfree(inst);
  516. out_put_poly:
  517. inst = ERR_PTR(err);
  518. goto out;
  519. }
  520. static struct crypto_instance *rfc7539_alloc(struct rtattr **tb)
  521. {
  522. return chachapoly_alloc(tb, "rfc7539", 12);
  523. }
  524. static struct crypto_instance *rfc7539esp_alloc(struct rtattr **tb)
  525. {
  526. return chachapoly_alloc(tb, "rfc7539esp", 8);
  527. }
  528. static void chachapoly_free(struct crypto_instance *inst)
  529. {
  530. struct chachapoly_instance_ctx *ctx = crypto_instance_ctx(inst);
  531. crypto_drop_skcipher(&ctx->chacha);
  532. crypto_drop_ahash(&ctx->poly);
  533. kfree(inst);
  534. }
  535. static struct crypto_template rfc7539_tmpl = {
  536. .name = "rfc7539",
  537. .alloc = rfc7539_alloc,
  538. .free = chachapoly_free,
  539. .module = THIS_MODULE,
  540. };
  541. static struct crypto_template rfc7539esp_tmpl = {
  542. .name = "rfc7539esp",
  543. .alloc = rfc7539esp_alloc,
  544. .free = chachapoly_free,
  545. .module = THIS_MODULE,
  546. };
  547. static int __init chacha20poly1305_module_init(void)
  548. {
  549. int err;
  550. err = crypto_register_template(&rfc7539_tmpl);
  551. if (err)
  552. return err;
  553. err = crypto_register_template(&rfc7539esp_tmpl);
  554. if (err)
  555. crypto_unregister_template(&rfc7539_tmpl);
  556. return err;
  557. }
  558. static void __exit chacha20poly1305_module_exit(void)
  559. {
  560. crypto_unregister_template(&rfc7539esp_tmpl);
  561. crypto_unregister_template(&rfc7539_tmpl);
  562. }
  563. module_init(chacha20poly1305_module_init);
  564. module_exit(chacha20poly1305_module_exit);
  565. MODULE_LICENSE("GPL");
  566. MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
  567. MODULE_DESCRIPTION("ChaCha20-Poly1305 AEAD");
  568. MODULE_ALIAS_CRYPTO("chacha20poly1305");
  569. MODULE_ALIAS_CRYPTO("rfc7539");
  570. MODULE_ALIAS_CRYPTO("rfc7539esp");