authencesn.c 15 KB

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