chacha20poly1305.c 19 KB

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