chacha20poly1305.c 19 KB

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