chacha20poly1305.c 20 KB

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