authencesn.c 15 KB

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