authencesn.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /*
  2. * authencesn.c - AEAD wrapper for IPsec with extended sequence numbers,
  3. * derived from authenc.c
  4. *
  5. * Copyright (C) 2010 secunet Security Networks AG
  6. * Copyright (C) 2010 Steffen Klassert <steffen.klassert@secunet.com>
  7. * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the Free
  11. * Software Foundation; either version 2 of the License, or (at your option)
  12. * any later version.
  13. *
  14. */
  15. #include <crypto/internal/aead.h>
  16. #include <crypto/internal/hash.h>
  17. #include <crypto/internal/skcipher.h>
  18. #include <crypto/authenc.h>
  19. #include <crypto/null.h>
  20. #include <crypto/scatterwalk.h>
  21. #include <linux/err.h>
  22. #include <linux/init.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/rtnetlink.h>
  26. #include <linux/slab.h>
  27. #include <linux/spinlock.h>
  28. struct authenc_esn_instance_ctx {
  29. struct crypto_ahash_spawn auth;
  30. struct crypto_skcipher_spawn enc;
  31. };
  32. struct crypto_authenc_esn_ctx {
  33. unsigned int reqoff;
  34. struct crypto_ahash *auth;
  35. struct crypto_skcipher *enc;
  36. struct crypto_skcipher *null;
  37. };
  38. struct authenc_esn_request_ctx {
  39. struct scatterlist src[2];
  40. struct scatterlist dst[2];
  41. char tail[];
  42. };
  43. static void authenc_esn_request_complete(struct aead_request *req, int err)
  44. {
  45. if (err != -EINPROGRESS)
  46. aead_request_complete(req, err);
  47. }
  48. static int crypto_authenc_esn_setauthsize(struct crypto_aead *authenc_esn,
  49. unsigned int authsize)
  50. {
  51. if (authsize > 0 && authsize < 4)
  52. return -EINVAL;
  53. return 0;
  54. }
  55. static int crypto_authenc_esn_setkey(struct crypto_aead *authenc_esn, const u8 *key,
  56. unsigned int keylen)
  57. {
  58. struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
  59. struct crypto_ahash *auth = ctx->auth;
  60. struct crypto_skcipher *enc = ctx->enc;
  61. struct crypto_authenc_keys keys;
  62. int err = -EINVAL;
  63. if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
  64. goto badkey;
  65. crypto_ahash_clear_flags(auth, CRYPTO_TFM_REQ_MASK);
  66. crypto_ahash_set_flags(auth, crypto_aead_get_flags(authenc_esn) &
  67. CRYPTO_TFM_REQ_MASK);
  68. err = crypto_ahash_setkey(auth, keys.authkey, keys.authkeylen);
  69. crypto_aead_set_flags(authenc_esn, crypto_ahash_get_flags(auth) &
  70. CRYPTO_TFM_RES_MASK);
  71. if (err)
  72. goto out;
  73. crypto_skcipher_clear_flags(enc, CRYPTO_TFM_REQ_MASK);
  74. crypto_skcipher_set_flags(enc, crypto_aead_get_flags(authenc_esn) &
  75. CRYPTO_TFM_REQ_MASK);
  76. err = crypto_skcipher_setkey(enc, keys.enckey, keys.enckeylen);
  77. crypto_aead_set_flags(authenc_esn, crypto_skcipher_get_flags(enc) &
  78. CRYPTO_TFM_RES_MASK);
  79. out:
  80. return err;
  81. badkey:
  82. crypto_aead_set_flags(authenc_esn, CRYPTO_TFM_RES_BAD_KEY_LEN);
  83. goto out;
  84. }
  85. static int crypto_authenc_esn_genicv_tail(struct aead_request *req,
  86. unsigned int flags)
  87. {
  88. struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
  89. struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
  90. struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
  91. struct crypto_ahash *auth = ctx->auth;
  92. u8 *hash = PTR_ALIGN((u8 *)areq_ctx->tail,
  93. crypto_ahash_alignmask(auth) + 1);
  94. unsigned int authsize = crypto_aead_authsize(authenc_esn);
  95. unsigned int assoclen = req->assoclen;
  96. unsigned int cryptlen = req->cryptlen;
  97. struct scatterlist *dst = req->dst;
  98. u32 tmp[2];
  99. /* Move high-order bits of sequence number back. */
  100. scatterwalk_map_and_copy(tmp, dst, 4, 4, 0);
  101. scatterwalk_map_and_copy(tmp + 1, dst, assoclen + cryptlen, 4, 0);
  102. scatterwalk_map_and_copy(tmp, dst, 0, 8, 1);
  103. scatterwalk_map_and_copy(hash, dst, assoclen + cryptlen, authsize, 1);
  104. return 0;
  105. }
  106. static void authenc_esn_geniv_ahash_done(struct crypto_async_request *areq,
  107. int err)
  108. {
  109. struct aead_request *req = areq->data;
  110. err = err ?: crypto_authenc_esn_genicv_tail(req, 0);
  111. aead_request_complete(req, err);
  112. }
  113. static int crypto_authenc_esn_genicv(struct aead_request *req,
  114. unsigned int flags)
  115. {
  116. struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
  117. struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
  118. struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
  119. struct crypto_ahash *auth = ctx->auth;
  120. u8 *hash = PTR_ALIGN((u8 *)areq_ctx->tail,
  121. crypto_ahash_alignmask(auth) + 1);
  122. struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
  123. unsigned int authsize = crypto_aead_authsize(authenc_esn);
  124. unsigned int assoclen = req->assoclen;
  125. unsigned int cryptlen = req->cryptlen;
  126. struct scatterlist *dst = req->dst;
  127. u32 tmp[2];
  128. if (!authsize)
  129. return 0;
  130. /* Move high-order bits of sequence number to the end. */
  131. scatterwalk_map_and_copy(tmp, dst, 0, 8, 0);
  132. scatterwalk_map_and_copy(tmp, dst, 4, 4, 1);
  133. scatterwalk_map_and_copy(tmp + 1, dst, assoclen + cryptlen, 4, 1);
  134. sg_init_table(areq_ctx->dst, 2);
  135. dst = scatterwalk_ffwd(areq_ctx->dst, dst, 4);
  136. ahash_request_set_tfm(ahreq, auth);
  137. ahash_request_set_crypt(ahreq, dst, hash, assoclen + cryptlen);
  138. ahash_request_set_callback(ahreq, flags,
  139. authenc_esn_geniv_ahash_done, req);
  140. return crypto_ahash_digest(ahreq) ?:
  141. crypto_authenc_esn_genicv_tail(req, aead_request_flags(req));
  142. }
  143. static void crypto_authenc_esn_encrypt_done(struct crypto_async_request *req,
  144. int err)
  145. {
  146. struct aead_request *areq = req->data;
  147. if (!err)
  148. err = crypto_authenc_esn_genicv(areq, 0);
  149. authenc_esn_request_complete(areq, err);
  150. }
  151. static int crypto_authenc_esn_copy(struct aead_request *req, unsigned int len)
  152. {
  153. struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
  154. struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
  155. SKCIPHER_REQUEST_ON_STACK(skreq, ctx->null);
  156. skcipher_request_set_tfm(skreq, ctx->null);
  157. skcipher_request_set_callback(skreq, aead_request_flags(req),
  158. NULL, NULL);
  159. skcipher_request_set_crypt(skreq, req->src, req->dst, len, NULL);
  160. return crypto_skcipher_encrypt(skreq);
  161. }
  162. static int crypto_authenc_esn_encrypt(struct aead_request *req)
  163. {
  164. struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
  165. struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
  166. struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
  167. struct skcipher_request *skreq = (void *)(areq_ctx->tail +
  168. ctx->reqoff);
  169. struct crypto_skcipher *enc = ctx->enc;
  170. unsigned int assoclen = req->assoclen;
  171. unsigned int cryptlen = req->cryptlen;
  172. struct scatterlist *src, *dst;
  173. int err;
  174. sg_init_table(areq_ctx->src, 2);
  175. src = scatterwalk_ffwd(areq_ctx->src, req->src, assoclen);
  176. dst = src;
  177. if (req->src != req->dst) {
  178. err = crypto_authenc_esn_copy(req, assoclen);
  179. if (err)
  180. return err;
  181. sg_init_table(areq_ctx->dst, 2);
  182. dst = scatterwalk_ffwd(areq_ctx->dst, req->dst, assoclen);
  183. }
  184. skcipher_request_set_tfm(skreq, enc);
  185. skcipher_request_set_callback(skreq, aead_request_flags(req),
  186. crypto_authenc_esn_encrypt_done, req);
  187. skcipher_request_set_crypt(skreq, src, dst, cryptlen, req->iv);
  188. err = crypto_skcipher_encrypt(skreq);
  189. if (err)
  190. return err;
  191. return crypto_authenc_esn_genicv(req, aead_request_flags(req));
  192. }
  193. static int crypto_authenc_esn_decrypt_tail(struct aead_request *req,
  194. unsigned int flags)
  195. {
  196. struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
  197. unsigned int authsize = crypto_aead_authsize(authenc_esn);
  198. struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
  199. struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
  200. struct skcipher_request *skreq = (void *)(areq_ctx->tail +
  201. ctx->reqoff);
  202. struct crypto_ahash *auth = ctx->auth;
  203. u8 *ohash = PTR_ALIGN((u8 *)areq_ctx->tail,
  204. crypto_ahash_alignmask(auth) + 1);
  205. unsigned int cryptlen = req->cryptlen - authsize;
  206. unsigned int assoclen = req->assoclen;
  207. struct scatterlist *dst = req->dst;
  208. u8 *ihash = ohash + crypto_ahash_digestsize(auth);
  209. u32 tmp[2];
  210. /* Move high-order bits of sequence number back. */
  211. scatterwalk_map_and_copy(tmp, dst, 4, 4, 0);
  212. scatterwalk_map_and_copy(tmp + 1, dst, assoclen + cryptlen, 4, 0);
  213. scatterwalk_map_and_copy(tmp, dst, 0, 8, 1);
  214. if (crypto_memneq(ihash, ohash, authsize))
  215. return -EBADMSG;
  216. sg_init_table(areq_ctx->dst, 2);
  217. dst = scatterwalk_ffwd(areq_ctx->dst, dst, assoclen);
  218. skcipher_request_set_tfm(skreq, ctx->enc);
  219. skcipher_request_set_callback(skreq, flags,
  220. req->base.complete, req->base.data);
  221. skcipher_request_set_crypt(skreq, dst, dst, cryptlen, req->iv);
  222. return crypto_skcipher_decrypt(skreq);
  223. }
  224. static void authenc_esn_verify_ahash_done(struct crypto_async_request *areq,
  225. int err)
  226. {
  227. struct aead_request *req = areq->data;
  228. err = err ?: crypto_authenc_esn_decrypt_tail(req, 0);
  229. aead_request_complete(req, err);
  230. }
  231. static int crypto_authenc_esn_decrypt(struct aead_request *req)
  232. {
  233. struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
  234. struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
  235. struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
  236. struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
  237. unsigned int authsize = crypto_aead_authsize(authenc_esn);
  238. struct crypto_ahash *auth = ctx->auth;
  239. u8 *ohash = PTR_ALIGN((u8 *)areq_ctx->tail,
  240. crypto_ahash_alignmask(auth) + 1);
  241. unsigned int assoclen = req->assoclen;
  242. unsigned int cryptlen = req->cryptlen;
  243. u8 *ihash = ohash + crypto_ahash_digestsize(auth);
  244. struct scatterlist *dst = req->dst;
  245. u32 tmp[2];
  246. int err;
  247. cryptlen -= authsize;
  248. if (req->src != dst) {
  249. err = crypto_authenc_esn_copy(req, assoclen + cryptlen);
  250. if (err)
  251. return err;
  252. }
  253. scatterwalk_map_and_copy(ihash, req->src, assoclen + cryptlen,
  254. authsize, 0);
  255. if (!authsize)
  256. goto tail;
  257. /* Move high-order bits of sequence number to the end. */
  258. scatterwalk_map_and_copy(tmp, dst, 0, 8, 0);
  259. scatterwalk_map_and_copy(tmp, dst, 4, 4, 1);
  260. scatterwalk_map_and_copy(tmp + 1, dst, assoclen + cryptlen, 4, 1);
  261. sg_init_table(areq_ctx->dst, 2);
  262. dst = scatterwalk_ffwd(areq_ctx->dst, dst, 4);
  263. ahash_request_set_tfm(ahreq, auth);
  264. ahash_request_set_crypt(ahreq, dst, ohash, assoclen + cryptlen);
  265. ahash_request_set_callback(ahreq, aead_request_flags(req),
  266. authenc_esn_verify_ahash_done, req);
  267. err = crypto_ahash_digest(ahreq);
  268. if (err)
  269. return err;
  270. tail:
  271. return crypto_authenc_esn_decrypt_tail(req, aead_request_flags(req));
  272. }
  273. static int crypto_authenc_esn_init_tfm(struct crypto_aead *tfm)
  274. {
  275. struct aead_instance *inst = aead_alg_instance(tfm);
  276. struct authenc_esn_instance_ctx *ictx = aead_instance_ctx(inst);
  277. struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(tfm);
  278. struct crypto_ahash *auth;
  279. struct crypto_skcipher *enc;
  280. struct crypto_skcipher *null;
  281. int err;
  282. auth = crypto_spawn_ahash(&ictx->auth);
  283. if (IS_ERR(auth))
  284. return PTR_ERR(auth);
  285. enc = crypto_spawn_skcipher(&ictx->enc);
  286. err = PTR_ERR(enc);
  287. if (IS_ERR(enc))
  288. goto err_free_ahash;
  289. null = crypto_get_default_null_skcipher2();
  290. err = PTR_ERR(null);
  291. if (IS_ERR(null))
  292. goto err_free_skcipher;
  293. ctx->auth = auth;
  294. ctx->enc = enc;
  295. ctx->null = null;
  296. ctx->reqoff = ALIGN(2 * crypto_ahash_digestsize(auth),
  297. crypto_ahash_alignmask(auth) + 1);
  298. crypto_aead_set_reqsize(
  299. tfm,
  300. sizeof(struct authenc_esn_request_ctx) +
  301. ctx->reqoff +
  302. max_t(unsigned int,
  303. crypto_ahash_reqsize(auth) +
  304. sizeof(struct ahash_request),
  305. sizeof(struct skcipher_request) +
  306. crypto_skcipher_reqsize(enc)));
  307. return 0;
  308. err_free_skcipher:
  309. crypto_free_skcipher(enc);
  310. err_free_ahash:
  311. crypto_free_ahash(auth);
  312. return err;
  313. }
  314. static void crypto_authenc_esn_exit_tfm(struct crypto_aead *tfm)
  315. {
  316. struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(tfm);
  317. crypto_free_ahash(ctx->auth);
  318. crypto_free_skcipher(ctx->enc);
  319. crypto_put_default_null_skcipher2();
  320. }
  321. static void crypto_authenc_esn_free(struct aead_instance *inst)
  322. {
  323. struct authenc_esn_instance_ctx *ctx = aead_instance_ctx(inst);
  324. crypto_drop_skcipher(&ctx->enc);
  325. crypto_drop_ahash(&ctx->auth);
  326. kfree(inst);
  327. }
  328. static int crypto_authenc_esn_create(struct crypto_template *tmpl,
  329. struct rtattr **tb)
  330. {
  331. struct crypto_attr_type *algt;
  332. struct aead_instance *inst;
  333. struct hash_alg_common *auth;
  334. struct crypto_alg *auth_base;
  335. struct skcipher_alg *enc;
  336. struct authenc_esn_instance_ctx *ctx;
  337. const char *enc_name;
  338. int err;
  339. algt = crypto_get_attr_type(tb);
  340. if (IS_ERR(algt))
  341. return PTR_ERR(algt);
  342. if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
  343. return -EINVAL;
  344. auth = ahash_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH,
  345. CRYPTO_ALG_TYPE_AHASH_MASK |
  346. crypto_requires_sync(algt->type, algt->mask));
  347. if (IS_ERR(auth))
  348. return PTR_ERR(auth);
  349. auth_base = &auth->base;
  350. enc_name = crypto_attr_alg_name(tb[2]);
  351. err = PTR_ERR(enc_name);
  352. if (IS_ERR(enc_name))
  353. goto out_put_auth;
  354. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  355. err = -ENOMEM;
  356. if (!inst)
  357. goto out_put_auth;
  358. ctx = aead_instance_ctx(inst);
  359. err = crypto_init_ahash_spawn(&ctx->auth, auth,
  360. aead_crypto_instance(inst));
  361. if (err)
  362. goto err_free_inst;
  363. crypto_set_skcipher_spawn(&ctx->enc, aead_crypto_instance(inst));
  364. err = crypto_grab_skcipher(&ctx->enc, enc_name, 0,
  365. crypto_requires_sync(algt->type,
  366. algt->mask));
  367. if (err)
  368. goto err_drop_auth;
  369. enc = crypto_spawn_skcipher_alg(&ctx->enc);
  370. err = -ENAMETOOLONG;
  371. if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
  372. "authencesn(%s,%s)", auth_base->cra_name,
  373. enc->base.cra_name) >= CRYPTO_MAX_ALG_NAME)
  374. goto err_drop_enc;
  375. if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  376. "authencesn(%s,%s)", auth_base->cra_driver_name,
  377. enc->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  378. goto err_drop_enc;
  379. inst->alg.base.cra_flags = (auth_base->cra_flags |
  380. enc->base.cra_flags) & CRYPTO_ALG_ASYNC;
  381. inst->alg.base.cra_priority = enc->base.cra_priority * 10 +
  382. auth_base->cra_priority;
  383. inst->alg.base.cra_blocksize = enc->base.cra_blocksize;
  384. inst->alg.base.cra_alignmask = auth_base->cra_alignmask |
  385. enc->base.cra_alignmask;
  386. inst->alg.base.cra_ctxsize = sizeof(struct crypto_authenc_esn_ctx);
  387. inst->alg.ivsize = crypto_skcipher_alg_ivsize(enc);
  388. inst->alg.chunksize = crypto_skcipher_alg_chunksize(enc);
  389. inst->alg.maxauthsize = auth->digestsize;
  390. inst->alg.init = crypto_authenc_esn_init_tfm;
  391. inst->alg.exit = crypto_authenc_esn_exit_tfm;
  392. inst->alg.setkey = crypto_authenc_esn_setkey;
  393. inst->alg.setauthsize = crypto_authenc_esn_setauthsize;
  394. inst->alg.encrypt = crypto_authenc_esn_encrypt;
  395. inst->alg.decrypt = crypto_authenc_esn_decrypt;
  396. inst->free = crypto_authenc_esn_free,
  397. err = aead_register_instance(tmpl, inst);
  398. if (err)
  399. goto err_drop_enc;
  400. out:
  401. crypto_mod_put(auth_base);
  402. return err;
  403. err_drop_enc:
  404. crypto_drop_skcipher(&ctx->enc);
  405. err_drop_auth:
  406. crypto_drop_ahash(&ctx->auth);
  407. err_free_inst:
  408. kfree(inst);
  409. out_put_auth:
  410. goto out;
  411. }
  412. static struct crypto_template crypto_authenc_esn_tmpl = {
  413. .name = "authencesn",
  414. .create = crypto_authenc_esn_create,
  415. .module = THIS_MODULE,
  416. };
  417. static int __init crypto_authenc_esn_module_init(void)
  418. {
  419. return crypto_register_template(&crypto_authenc_esn_tmpl);
  420. }
  421. static void __exit crypto_authenc_esn_module_exit(void)
  422. {
  423. crypto_unregister_template(&crypto_authenc_esn_tmpl);
  424. }
  425. module_init(crypto_authenc_esn_module_init);
  426. module_exit(crypto_authenc_esn_module_exit);
  427. MODULE_LICENSE("GPL");
  428. MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
  429. MODULE_DESCRIPTION("AEAD wrapper for IPsec with extended sequence numbers");
  430. MODULE_ALIAS_CRYPTO("authencesn");