authenc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. * Authenc: Simple AEAD wrapper for IPsec
  3. *
  4. * Copyright (c) 2007-2015 Herbert Xu <herbert@gondor.apana.org.au>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. *
  11. */
  12. #include <crypto/internal/aead.h>
  13. #include <crypto/internal/hash.h>
  14. #include <crypto/internal/skcipher.h>
  15. #include <crypto/authenc.h>
  16. #include <crypto/null.h>
  17. #include <crypto/scatterwalk.h>
  18. #include <linux/err.h>
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/rtnetlink.h>
  23. #include <linux/slab.h>
  24. #include <linux/spinlock.h>
  25. struct authenc_instance_ctx {
  26. struct crypto_ahash_spawn auth;
  27. struct crypto_skcipher_spawn enc;
  28. unsigned int reqoff;
  29. };
  30. struct crypto_authenc_ctx {
  31. struct crypto_ahash *auth;
  32. struct crypto_ablkcipher *enc;
  33. struct crypto_blkcipher *null;
  34. };
  35. struct authenc_request_ctx {
  36. struct scatterlist src[2];
  37. struct scatterlist dst[2];
  38. char tail[];
  39. };
  40. static void authenc_request_complete(struct aead_request *req, int err)
  41. {
  42. if (err != -EINPROGRESS)
  43. aead_request_complete(req, err);
  44. }
  45. int crypto_authenc_extractkeys(struct crypto_authenc_keys *keys, const u8 *key,
  46. unsigned int keylen)
  47. {
  48. struct rtattr *rta = (struct rtattr *)key;
  49. struct crypto_authenc_key_param *param;
  50. if (!RTA_OK(rta, keylen))
  51. return -EINVAL;
  52. if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
  53. return -EINVAL;
  54. if (RTA_PAYLOAD(rta) < sizeof(*param))
  55. return -EINVAL;
  56. param = RTA_DATA(rta);
  57. keys->enckeylen = be32_to_cpu(param->enckeylen);
  58. key += RTA_ALIGN(rta->rta_len);
  59. keylen -= RTA_ALIGN(rta->rta_len);
  60. if (keylen < keys->enckeylen)
  61. return -EINVAL;
  62. keys->authkeylen = keylen - keys->enckeylen;
  63. keys->authkey = key;
  64. keys->enckey = key + keys->authkeylen;
  65. return 0;
  66. }
  67. EXPORT_SYMBOL_GPL(crypto_authenc_extractkeys);
  68. static int crypto_authenc_setkey(struct crypto_aead *authenc, const u8 *key,
  69. unsigned int keylen)
  70. {
  71. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  72. struct crypto_ahash *auth = ctx->auth;
  73. struct crypto_ablkcipher *enc = ctx->enc;
  74. struct crypto_authenc_keys keys;
  75. int err = -EINVAL;
  76. if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
  77. goto badkey;
  78. crypto_ahash_clear_flags(auth, CRYPTO_TFM_REQ_MASK);
  79. crypto_ahash_set_flags(auth, crypto_aead_get_flags(authenc) &
  80. CRYPTO_TFM_REQ_MASK);
  81. err = crypto_ahash_setkey(auth, keys.authkey, keys.authkeylen);
  82. crypto_aead_set_flags(authenc, crypto_ahash_get_flags(auth) &
  83. CRYPTO_TFM_RES_MASK);
  84. if (err)
  85. goto out;
  86. crypto_ablkcipher_clear_flags(enc, CRYPTO_TFM_REQ_MASK);
  87. crypto_ablkcipher_set_flags(enc, crypto_aead_get_flags(authenc) &
  88. CRYPTO_TFM_REQ_MASK);
  89. err = crypto_ablkcipher_setkey(enc, keys.enckey, keys.enckeylen);
  90. crypto_aead_set_flags(authenc, crypto_ablkcipher_get_flags(enc) &
  91. CRYPTO_TFM_RES_MASK);
  92. out:
  93. return err;
  94. badkey:
  95. crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
  96. goto out;
  97. }
  98. static void authenc_geniv_ahash_done(struct crypto_async_request *areq, int err)
  99. {
  100. struct aead_request *req = areq->data;
  101. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  102. struct aead_instance *inst = aead_alg_instance(authenc);
  103. struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
  104. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  105. struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
  106. if (err)
  107. goto out;
  108. scatterwalk_map_and_copy(ahreq->result, req->dst,
  109. req->assoclen + req->cryptlen,
  110. crypto_aead_authsize(authenc), 1);
  111. out:
  112. aead_request_complete(req, err);
  113. }
  114. static int crypto_authenc_genicv(struct aead_request *req, unsigned int flags)
  115. {
  116. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  117. struct aead_instance *inst = aead_alg_instance(authenc);
  118. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  119. struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
  120. struct crypto_ahash *auth = ctx->auth;
  121. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  122. struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
  123. u8 *hash = areq_ctx->tail;
  124. int err;
  125. hash = (u8 *)ALIGN((unsigned long)hash + crypto_ahash_alignmask(auth),
  126. crypto_ahash_alignmask(auth) + 1);
  127. ahash_request_set_tfm(ahreq, auth);
  128. ahash_request_set_crypt(ahreq, req->dst, hash,
  129. req->assoclen + req->cryptlen);
  130. ahash_request_set_callback(ahreq, flags,
  131. authenc_geniv_ahash_done, req);
  132. err = crypto_ahash_digest(ahreq);
  133. if (err)
  134. return err;
  135. scatterwalk_map_and_copy(hash, req->dst, req->assoclen + req->cryptlen,
  136. crypto_aead_authsize(authenc), 1);
  137. return 0;
  138. }
  139. static void crypto_authenc_encrypt_done(struct crypto_async_request *req,
  140. int err)
  141. {
  142. struct aead_request *areq = req->data;
  143. if (err)
  144. goto out;
  145. err = crypto_authenc_genicv(areq, 0);
  146. out:
  147. authenc_request_complete(areq, err);
  148. }
  149. static int crypto_authenc_copy_assoc(struct aead_request *req)
  150. {
  151. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  152. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  153. struct blkcipher_desc desc = {
  154. .tfm = ctx->null,
  155. };
  156. return crypto_blkcipher_encrypt(&desc, req->dst, req->src,
  157. req->assoclen);
  158. }
  159. static int crypto_authenc_encrypt(struct aead_request *req)
  160. {
  161. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  162. struct aead_instance *inst = aead_alg_instance(authenc);
  163. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  164. struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
  165. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  166. struct crypto_ablkcipher *enc = ctx->enc;
  167. unsigned int cryptlen = req->cryptlen;
  168. struct ablkcipher_request *abreq = (void *)(areq_ctx->tail +
  169. ictx->reqoff);
  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, req->assoclen);
  174. dst = src;
  175. if (req->src != req->dst) {
  176. err = crypto_authenc_copy_assoc(req);
  177. if (err)
  178. return err;
  179. sg_init_table(areq_ctx->dst, 2);
  180. dst = scatterwalk_ffwd(areq_ctx->dst, req->dst, req->assoclen);
  181. }
  182. ablkcipher_request_set_tfm(abreq, enc);
  183. ablkcipher_request_set_callback(abreq, aead_request_flags(req),
  184. crypto_authenc_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_genicv(req, aead_request_flags(req));
  190. }
  191. static int crypto_authenc_decrypt_tail(struct aead_request *req,
  192. unsigned int flags)
  193. {
  194. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  195. struct aead_instance *inst = aead_alg_instance(authenc);
  196. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  197. struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
  198. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  199. struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
  200. struct ablkcipher_request *abreq = (void *)(areq_ctx->tail +
  201. ictx->reqoff);
  202. unsigned int authsize = crypto_aead_authsize(authenc);
  203. u8 *ihash = ahreq->result + authsize;
  204. struct scatterlist *src, *dst;
  205. scatterwalk_map_and_copy(ihash, req->src, ahreq->nbytes, authsize, 0);
  206. if (crypto_memneq(ihash, ahreq->result, authsize))
  207. return -EBADMSG;
  208. sg_init_table(areq_ctx->src, 2);
  209. src = scatterwalk_ffwd(areq_ctx->src, req->src, req->assoclen);
  210. dst = src;
  211. if (req->src != req->dst) {
  212. sg_init_table(areq_ctx->dst, 2);
  213. dst = scatterwalk_ffwd(areq_ctx->dst, req->dst, req->assoclen);
  214. }
  215. ablkcipher_request_set_tfm(abreq, ctx->enc);
  216. ablkcipher_request_set_callback(abreq, aead_request_flags(req),
  217. req->base.complete, req->base.data);
  218. ablkcipher_request_set_crypt(abreq, src, dst,
  219. req->cryptlen - authsize, req->iv);
  220. return crypto_ablkcipher_decrypt(abreq);
  221. }
  222. static void authenc_verify_ahash_done(struct crypto_async_request *areq,
  223. int err)
  224. {
  225. struct aead_request *req = areq->data;
  226. if (err)
  227. goto out;
  228. err = crypto_authenc_decrypt_tail(req, 0);
  229. out:
  230. authenc_request_complete(req, err);
  231. }
  232. static int crypto_authenc_decrypt(struct aead_request *req)
  233. {
  234. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  235. unsigned int authsize = crypto_aead_authsize(authenc);
  236. struct aead_instance *inst = aead_alg_instance(authenc);
  237. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  238. struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
  239. struct crypto_ahash *auth = ctx->auth;
  240. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  241. struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
  242. u8 *hash = areq_ctx->tail;
  243. int err;
  244. hash = (u8 *)ALIGN((unsigned long)hash + crypto_ahash_alignmask(auth),
  245. crypto_ahash_alignmask(auth) + 1);
  246. ahash_request_set_tfm(ahreq, auth);
  247. ahash_request_set_crypt(ahreq, req->src, hash,
  248. req->assoclen + req->cryptlen - authsize);
  249. ahash_request_set_callback(ahreq, aead_request_flags(req),
  250. authenc_verify_ahash_done, req);
  251. err = crypto_ahash_digest(ahreq);
  252. if (err)
  253. return err;
  254. return crypto_authenc_decrypt_tail(req, aead_request_flags(req));
  255. }
  256. static int crypto_authenc_init_tfm(struct crypto_aead *tfm)
  257. {
  258. struct aead_instance *inst = aead_alg_instance(tfm);
  259. struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
  260. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(tfm);
  261. struct crypto_ahash *auth;
  262. struct crypto_ablkcipher *enc;
  263. struct crypto_blkcipher *null;
  264. int err;
  265. auth = crypto_spawn_ahash(&ictx->auth);
  266. if (IS_ERR(auth))
  267. return PTR_ERR(auth);
  268. enc = crypto_spawn_skcipher(&ictx->enc);
  269. err = PTR_ERR(enc);
  270. if (IS_ERR(enc))
  271. goto err_free_ahash;
  272. null = crypto_get_default_null_skcipher();
  273. err = PTR_ERR(null);
  274. if (IS_ERR(null))
  275. goto err_free_skcipher;
  276. ctx->auth = auth;
  277. ctx->enc = enc;
  278. ctx->null = null;
  279. crypto_aead_set_reqsize(
  280. tfm,
  281. sizeof(struct authenc_request_ctx) +
  282. ictx->reqoff +
  283. max_t(unsigned int,
  284. crypto_ahash_reqsize(auth) +
  285. sizeof(struct ahash_request),
  286. sizeof(struct ablkcipher_request) +
  287. crypto_ablkcipher_reqsize(enc)));
  288. return 0;
  289. err_free_skcipher:
  290. crypto_free_ablkcipher(enc);
  291. err_free_ahash:
  292. crypto_free_ahash(auth);
  293. return err;
  294. }
  295. static void crypto_authenc_exit_tfm(struct crypto_aead *tfm)
  296. {
  297. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(tfm);
  298. crypto_free_ahash(ctx->auth);
  299. crypto_free_ablkcipher(ctx->enc);
  300. crypto_put_default_null_skcipher();
  301. }
  302. static void crypto_authenc_free(struct aead_instance *inst)
  303. {
  304. struct authenc_instance_ctx *ctx = aead_instance_ctx(inst);
  305. crypto_drop_skcipher(&ctx->enc);
  306. crypto_drop_ahash(&ctx->auth);
  307. kfree(inst);
  308. }
  309. static int crypto_authenc_create(struct crypto_template *tmpl,
  310. struct rtattr **tb)
  311. {
  312. struct crypto_attr_type *algt;
  313. struct aead_instance *inst;
  314. struct hash_alg_common *auth;
  315. struct crypto_alg *auth_base;
  316. struct crypto_alg *enc;
  317. struct authenc_instance_ctx *ctx;
  318. const char *enc_name;
  319. int err;
  320. algt = crypto_get_attr_type(tb);
  321. if (IS_ERR(algt))
  322. return PTR_ERR(algt);
  323. if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
  324. return -EINVAL;
  325. auth = ahash_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH,
  326. CRYPTO_ALG_TYPE_AHASH_MASK);
  327. if (IS_ERR(auth))
  328. return PTR_ERR(auth);
  329. auth_base = &auth->base;
  330. enc_name = crypto_attr_alg_name(tb[2]);
  331. err = PTR_ERR(enc_name);
  332. if (IS_ERR(enc_name))
  333. goto out_put_auth;
  334. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  335. err = -ENOMEM;
  336. if (!inst)
  337. goto out_put_auth;
  338. ctx = aead_instance_ctx(inst);
  339. err = crypto_init_ahash_spawn(&ctx->auth, auth,
  340. aead_crypto_instance(inst));
  341. if (err)
  342. goto err_free_inst;
  343. crypto_set_skcipher_spawn(&ctx->enc, aead_crypto_instance(inst));
  344. err = crypto_grab_skcipher(&ctx->enc, enc_name, 0,
  345. crypto_requires_sync(algt->type,
  346. algt->mask));
  347. if (err)
  348. goto err_drop_auth;
  349. enc = crypto_skcipher_spawn_alg(&ctx->enc);
  350. ctx->reqoff = ALIGN(2 * auth->digestsize + auth_base->cra_alignmask,
  351. auth_base->cra_alignmask + 1);
  352. err = -ENAMETOOLONG;
  353. if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
  354. "authenc(%s,%s)", auth_base->cra_name, enc->cra_name) >=
  355. CRYPTO_MAX_ALG_NAME)
  356. goto err_drop_enc;
  357. if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  358. "authenc(%s,%s)", auth_base->cra_driver_name,
  359. enc->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  360. goto err_drop_enc;
  361. inst->alg.base.cra_flags = enc->cra_flags & CRYPTO_ALG_ASYNC;
  362. inst->alg.base.cra_priority = enc->cra_priority * 10 +
  363. auth_base->cra_priority;
  364. inst->alg.base.cra_blocksize = enc->cra_blocksize;
  365. inst->alg.base.cra_alignmask = auth_base->cra_alignmask |
  366. enc->cra_alignmask;
  367. inst->alg.base.cra_ctxsize = sizeof(struct crypto_authenc_ctx);
  368. inst->alg.ivsize = enc->cra_ablkcipher.ivsize;
  369. inst->alg.maxauthsize = auth->digestsize;
  370. inst->alg.init = crypto_authenc_init_tfm;
  371. inst->alg.exit = crypto_authenc_exit_tfm;
  372. inst->alg.setkey = crypto_authenc_setkey;
  373. inst->alg.encrypt = crypto_authenc_encrypt;
  374. inst->alg.decrypt = crypto_authenc_decrypt;
  375. inst->free = crypto_authenc_free;
  376. err = aead_register_instance(tmpl, inst);
  377. if (err)
  378. goto err_drop_enc;
  379. out:
  380. crypto_mod_put(auth_base);
  381. return err;
  382. err_drop_enc:
  383. crypto_drop_skcipher(&ctx->enc);
  384. err_drop_auth:
  385. crypto_drop_ahash(&ctx->auth);
  386. err_free_inst:
  387. kfree(inst);
  388. out_put_auth:
  389. goto out;
  390. }
  391. static struct crypto_template crypto_authenc_tmpl = {
  392. .name = "authenc",
  393. .create = crypto_authenc_create,
  394. .module = THIS_MODULE,
  395. };
  396. static int __init crypto_authenc_module_init(void)
  397. {
  398. return crypto_register_template(&crypto_authenc_tmpl);
  399. }
  400. static void __exit crypto_authenc_module_exit(void)
  401. {
  402. crypto_unregister_template(&crypto_authenc_tmpl);
  403. }
  404. module_init(crypto_authenc_module_init);
  405. module_exit(crypto_authenc_module_exit);
  406. MODULE_LICENSE("GPL");
  407. MODULE_DESCRIPTION("Simple AEAD wrapper for IPsec");
  408. MODULE_ALIAS_CRYPTO("authenc");