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_ablkcipher *enc;
  36. struct crypto_blkcipher *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_ablkcipher *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_ablkcipher_clear_flags(enc, CRYPTO_TFM_REQ_MASK);
  74. crypto_ablkcipher_set_flags(enc, crypto_aead_get_flags(authenc_esn) &
  75. CRYPTO_TFM_REQ_MASK);
  76. err = crypto_ablkcipher_setkey(enc, keys.enckey, keys.enckeylen);
  77. crypto_aead_set_flags(authenc_esn, crypto_ablkcipher_get_flags(enc) &
  78. CRYPTO_TFM_RES_MASK);
  79. out:
  80. memzero_explicit(&keys, sizeof(keys));
  81. return err;
  82. badkey:
  83. crypto_aead_set_flags(authenc_esn, CRYPTO_TFM_RES_BAD_KEY_LEN);
  84. goto out;
  85. }
  86. static int crypto_authenc_esn_genicv_tail(struct aead_request *req,
  87. unsigned int flags)
  88. {
  89. struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
  90. struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
  91. struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
  92. struct crypto_ahash *auth = ctx->auth;
  93. u8 *hash = PTR_ALIGN((u8 *)areq_ctx->tail,
  94. crypto_ahash_alignmask(auth) + 1);
  95. unsigned int authsize = crypto_aead_authsize(authenc_esn);
  96. unsigned int assoclen = req->assoclen;
  97. unsigned int cryptlen = req->cryptlen;
  98. struct scatterlist *dst = req->dst;
  99. u32 tmp[2];
  100. /* Move high-order bits of sequence number back. */
  101. scatterwalk_map_and_copy(tmp, dst, 4, 4, 0);
  102. scatterwalk_map_and_copy(tmp + 1, dst, assoclen + cryptlen, 4, 0);
  103. scatterwalk_map_and_copy(tmp, dst, 0, 8, 1);
  104. scatterwalk_map_and_copy(hash, dst, assoclen + cryptlen, authsize, 1);
  105. return 0;
  106. }
  107. static void authenc_esn_geniv_ahash_done(struct crypto_async_request *areq,
  108. int err)
  109. {
  110. struct aead_request *req = areq->data;
  111. err = err ?: crypto_authenc_esn_genicv_tail(req, 0);
  112. aead_request_complete(req, err);
  113. }
  114. static int crypto_authenc_esn_genicv(struct aead_request *req,
  115. unsigned int flags)
  116. {
  117. struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
  118. struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
  119. struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
  120. struct crypto_ahash *auth = ctx->auth;
  121. u8 *hash = PTR_ALIGN((u8 *)areq_ctx->tail,
  122. crypto_ahash_alignmask(auth) + 1);
  123. struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
  124. unsigned int authsize = crypto_aead_authsize(authenc_esn);
  125. unsigned int assoclen = req->assoclen;
  126. unsigned int cryptlen = req->cryptlen;
  127. struct scatterlist *dst = req->dst;
  128. u32 tmp[2];
  129. if (!authsize)
  130. return 0;
  131. /* Move high-order bits of sequence number to the end. */
  132. scatterwalk_map_and_copy(tmp, dst, 0, 8, 0);
  133. scatterwalk_map_and_copy(tmp, dst, 4, 4, 1);
  134. scatterwalk_map_and_copy(tmp + 1, dst, assoclen + cryptlen, 4, 1);
  135. sg_init_table(areq_ctx->dst, 2);
  136. dst = scatterwalk_ffwd(areq_ctx->dst, dst, 4);
  137. ahash_request_set_tfm(ahreq, auth);
  138. ahash_request_set_crypt(ahreq, dst, hash, assoclen + cryptlen);
  139. ahash_request_set_callback(ahreq, flags,
  140. authenc_esn_geniv_ahash_done, req);
  141. return crypto_ahash_digest(ahreq) ?:
  142. crypto_authenc_esn_genicv_tail(req, aead_request_flags(req));
  143. }
  144. static void crypto_authenc_esn_encrypt_done(struct crypto_async_request *req,
  145. int err)
  146. {
  147. struct aead_request *areq = req->data;
  148. if (!err)
  149. err = crypto_authenc_esn_genicv(areq, 0);
  150. authenc_esn_request_complete(areq, err);
  151. }
  152. static int crypto_authenc_esn_copy(struct aead_request *req, unsigned int len)
  153. {
  154. struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
  155. struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
  156. struct blkcipher_desc desc = {
  157. .tfm = ctx->null,
  158. };
  159. return crypto_blkcipher_encrypt(&desc, req->dst, req->src, len);
  160. }
  161. static int crypto_authenc_esn_encrypt(struct aead_request *req)
  162. {
  163. struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
  164. struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
  165. struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
  166. struct ablkcipher_request *abreq = (void *)(areq_ctx->tail
  167. + ctx->reqoff);
  168. struct crypto_ablkcipher *enc = ctx->enc;
  169. unsigned int assoclen = req->assoclen;
  170. unsigned int cryptlen = req->cryptlen;
  171. struct scatterlist *src, *dst;
  172. int err;
  173. sg_init_table(areq_ctx->src, 2);
  174. src = scatterwalk_ffwd(areq_ctx->src, req->src, assoclen);
  175. dst = src;
  176. if (req->src != req->dst) {
  177. err = crypto_authenc_esn_copy(req, assoclen);
  178. if (err)
  179. return err;
  180. sg_init_table(areq_ctx->dst, 2);
  181. dst = scatterwalk_ffwd(areq_ctx->dst, req->dst, assoclen);
  182. }
  183. ablkcipher_request_set_tfm(abreq, enc);
  184. ablkcipher_request_set_callback(abreq, aead_request_flags(req),
  185. crypto_authenc_esn_encrypt_done, req);
  186. ablkcipher_request_set_crypt(abreq, src, dst, cryptlen, req->iv);
  187. err = crypto_ablkcipher_encrypt(abreq);
  188. if (err)
  189. return err;
  190. return crypto_authenc_esn_genicv(req, aead_request_flags(req));
  191. }
  192. static int crypto_authenc_esn_decrypt_tail(struct aead_request *req,
  193. unsigned int flags)
  194. {
  195. struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
  196. unsigned int authsize = crypto_aead_authsize(authenc_esn);
  197. struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
  198. struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
  199. struct ablkcipher_request *abreq = (void *)(areq_ctx->tail
  200. + ctx->reqoff);
  201. struct crypto_ahash *auth = ctx->auth;
  202. u8 *ohash = PTR_ALIGN((u8 *)areq_ctx->tail,
  203. crypto_ahash_alignmask(auth) + 1);
  204. unsigned int cryptlen = req->cryptlen - authsize;
  205. unsigned int assoclen = req->assoclen;
  206. struct scatterlist *dst = req->dst;
  207. u8 *ihash = ohash + crypto_ahash_digestsize(auth);
  208. u32 tmp[2];
  209. if (!authsize)
  210. goto decrypt;
  211. /* Move high-order bits of sequence number back. */
  212. scatterwalk_map_and_copy(tmp, dst, 4, 4, 0);
  213. scatterwalk_map_and_copy(tmp + 1, dst, assoclen + cryptlen, 4, 0);
  214. scatterwalk_map_and_copy(tmp, dst, 0, 8, 1);
  215. if (crypto_memneq(ihash, ohash, authsize))
  216. return -EBADMSG;
  217. decrypt:
  218. sg_init_table(areq_ctx->dst, 2);
  219. dst = scatterwalk_ffwd(areq_ctx->dst, dst, assoclen);
  220. ablkcipher_request_set_tfm(abreq, ctx->enc);
  221. ablkcipher_request_set_callback(abreq, flags,
  222. req->base.complete, req->base.data);
  223. ablkcipher_request_set_crypt(abreq, dst, dst, cryptlen, req->iv);
  224. return crypto_ablkcipher_decrypt(abreq);
  225. }
  226. static void authenc_esn_verify_ahash_done(struct crypto_async_request *areq,
  227. int err)
  228. {
  229. struct aead_request *req = areq->data;
  230. err = err ?: crypto_authenc_esn_decrypt_tail(req, 0);
  231. authenc_esn_request_complete(req, err);
  232. }
  233. static int crypto_authenc_esn_decrypt(struct aead_request *req)
  234. {
  235. struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
  236. struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
  237. struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
  238. struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
  239. unsigned int authsize = crypto_aead_authsize(authenc_esn);
  240. struct crypto_ahash *auth = ctx->auth;
  241. u8 *ohash = PTR_ALIGN((u8 *)areq_ctx->tail,
  242. crypto_ahash_alignmask(auth) + 1);
  243. unsigned int assoclen = req->assoclen;
  244. unsigned int cryptlen = req->cryptlen;
  245. u8 *ihash = ohash + crypto_ahash_digestsize(auth);
  246. struct scatterlist *dst = req->dst;
  247. u32 tmp[2];
  248. int err;
  249. cryptlen -= authsize;
  250. if (req->src != dst) {
  251. err = crypto_authenc_esn_copy(req, assoclen + cryptlen);
  252. if (err)
  253. return err;
  254. }
  255. scatterwalk_map_and_copy(ihash, req->src, assoclen + cryptlen,
  256. authsize, 0);
  257. if (!authsize)
  258. goto tail;
  259. /* Move high-order bits of sequence number to the end. */
  260. scatterwalk_map_and_copy(tmp, dst, 0, 8, 0);
  261. scatterwalk_map_and_copy(tmp, dst, 4, 4, 1);
  262. scatterwalk_map_and_copy(tmp + 1, dst, assoclen + cryptlen, 4, 1);
  263. sg_init_table(areq_ctx->dst, 2);
  264. dst = scatterwalk_ffwd(areq_ctx->dst, dst, 4);
  265. ahash_request_set_tfm(ahreq, auth);
  266. ahash_request_set_crypt(ahreq, dst, ohash, assoclen + cryptlen);
  267. ahash_request_set_callback(ahreq, aead_request_flags(req),
  268. authenc_esn_verify_ahash_done, req);
  269. err = crypto_ahash_digest(ahreq);
  270. if (err)
  271. return err;
  272. tail:
  273. return crypto_authenc_esn_decrypt_tail(req, aead_request_flags(req));
  274. }
  275. static int crypto_authenc_esn_init_tfm(struct crypto_aead *tfm)
  276. {
  277. struct aead_instance *inst = aead_alg_instance(tfm);
  278. struct authenc_esn_instance_ctx *ictx = aead_instance_ctx(inst);
  279. struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(tfm);
  280. struct crypto_ahash *auth;
  281. struct crypto_ablkcipher *enc;
  282. struct crypto_blkcipher *null;
  283. int err;
  284. auth = crypto_spawn_ahash(&ictx->auth);
  285. if (IS_ERR(auth))
  286. return PTR_ERR(auth);
  287. enc = crypto_spawn_skcipher(&ictx->enc);
  288. err = PTR_ERR(enc);
  289. if (IS_ERR(enc))
  290. goto err_free_ahash;
  291. null = crypto_get_default_null_skcipher();
  292. err = PTR_ERR(null);
  293. if (IS_ERR(null))
  294. goto err_free_skcipher;
  295. ctx->auth = auth;
  296. ctx->enc = enc;
  297. ctx->null = null;
  298. ctx->reqoff = ALIGN(2 * crypto_ahash_digestsize(auth),
  299. crypto_ahash_alignmask(auth) + 1);
  300. crypto_aead_set_reqsize(
  301. tfm,
  302. sizeof(struct authenc_esn_request_ctx) +
  303. ctx->reqoff +
  304. max_t(unsigned int,
  305. crypto_ahash_reqsize(auth) +
  306. sizeof(struct ahash_request),
  307. sizeof(struct skcipher_givcrypt_request) +
  308. crypto_ablkcipher_reqsize(enc)));
  309. return 0;
  310. err_free_skcipher:
  311. crypto_free_ablkcipher(enc);
  312. err_free_ahash:
  313. crypto_free_ahash(auth);
  314. return err;
  315. }
  316. static void crypto_authenc_esn_exit_tfm(struct crypto_aead *tfm)
  317. {
  318. struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(tfm);
  319. crypto_free_ahash(ctx->auth);
  320. crypto_free_ablkcipher(ctx->enc);
  321. crypto_put_default_null_skcipher();
  322. }
  323. static void crypto_authenc_esn_free(struct aead_instance *inst)
  324. {
  325. struct authenc_esn_instance_ctx *ctx = aead_instance_ctx(inst);
  326. crypto_drop_skcipher(&ctx->enc);
  327. crypto_drop_ahash(&ctx->auth);
  328. kfree(inst);
  329. }
  330. static int crypto_authenc_esn_create(struct crypto_template *tmpl,
  331. struct rtattr **tb)
  332. {
  333. struct crypto_attr_type *algt;
  334. struct aead_instance *inst;
  335. struct hash_alg_common *auth;
  336. struct crypto_alg *auth_base;
  337. struct crypto_alg *enc;
  338. struct authenc_esn_instance_ctx *ctx;
  339. const char *enc_name;
  340. int err;
  341. algt = crypto_get_attr_type(tb);
  342. if (IS_ERR(algt))
  343. return PTR_ERR(algt);
  344. if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
  345. return -EINVAL;
  346. auth = ahash_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH,
  347. CRYPTO_ALG_TYPE_AHASH_MASK);
  348. if (IS_ERR(auth))
  349. return PTR_ERR(auth);
  350. auth_base = &auth->base;
  351. enc_name = crypto_attr_alg_name(tb[2]);
  352. err = PTR_ERR(enc_name);
  353. if (IS_ERR(enc_name))
  354. goto out_put_auth;
  355. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  356. err = -ENOMEM;
  357. if (!inst)
  358. goto out_put_auth;
  359. ctx = aead_instance_ctx(inst);
  360. err = crypto_init_ahash_spawn(&ctx->auth, auth,
  361. aead_crypto_instance(inst));
  362. if (err)
  363. goto err_free_inst;
  364. crypto_set_skcipher_spawn(&ctx->enc, aead_crypto_instance(inst));
  365. err = crypto_grab_skcipher(&ctx->enc, enc_name, 0,
  366. crypto_requires_sync(algt->type,
  367. algt->mask));
  368. if (err)
  369. goto err_drop_auth;
  370. enc = crypto_skcipher_spawn_alg(&ctx->enc);
  371. err = -ENAMETOOLONG;
  372. if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
  373. "authencesn(%s,%s)", auth_base->cra_name,
  374. enc->cra_name) >= CRYPTO_MAX_ALG_NAME)
  375. goto err_drop_enc;
  376. if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  377. "authencesn(%s,%s)", auth_base->cra_driver_name,
  378. enc->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  379. goto err_drop_enc;
  380. inst->alg.base.cra_flags = enc->cra_flags & CRYPTO_ALG_ASYNC;
  381. inst->alg.base.cra_priority = enc->cra_priority * 10 +
  382. auth_base->cra_priority;
  383. inst->alg.base.cra_blocksize = enc->cra_blocksize;
  384. inst->alg.base.cra_alignmask = auth_base->cra_alignmask |
  385. enc->cra_alignmask;
  386. inst->alg.base.cra_ctxsize = sizeof(struct crypto_authenc_esn_ctx);
  387. inst->alg.ivsize = enc->cra_ablkcipher.ivsize;
  388. inst->alg.maxauthsize = auth->digestsize;
  389. inst->alg.init = crypto_authenc_esn_init_tfm;
  390. inst->alg.exit = crypto_authenc_esn_exit_tfm;
  391. inst->alg.setkey = crypto_authenc_esn_setkey;
  392. inst->alg.setauthsize = crypto_authenc_esn_setauthsize;
  393. inst->alg.encrypt = crypto_authenc_esn_encrypt;
  394. inst->alg.decrypt = crypto_authenc_esn_decrypt;
  395. inst->free = crypto_authenc_esn_free,
  396. err = aead_register_instance(tmpl, inst);
  397. if (err)
  398. goto err_drop_enc;
  399. out:
  400. crypto_mod_put(auth_base);
  401. return err;
  402. err_drop_enc:
  403. crypto_drop_skcipher(&ctx->enc);
  404. err_drop_auth:
  405. crypto_drop_ahash(&ctx->auth);
  406. err_free_inst:
  407. kfree(inst);
  408. out_put_auth:
  409. goto out;
  410. }
  411. static struct crypto_template crypto_authenc_esn_tmpl = {
  412. .name = "authencesn",
  413. .create = crypto_authenc_esn_create,
  414. .module = THIS_MODULE,
  415. };
  416. static int __init crypto_authenc_esn_module_init(void)
  417. {
  418. return crypto_register_template(&crypto_authenc_esn_tmpl);
  419. }
  420. static void __exit crypto_authenc_esn_module_exit(void)
  421. {
  422. crypto_unregister_template(&crypto_authenc_esn_tmpl);
  423. }
  424. module_init(crypto_authenc_esn_module_init);
  425. module_exit(crypto_authenc_esn_module_exit);
  426. MODULE_LICENSE("GPL");
  427. MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
  428. MODULE_DESCRIPTION("AEAD wrapper for IPsec with extended sequence numbers");
  429. MODULE_ALIAS_CRYPTO("authencesn");