authenc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. /*
  55. * RTA_OK() didn't align the rtattr's payload when validating that it
  56. * fits in the buffer. Yet, the keys should start on the next 4-byte
  57. * aligned boundary. To avoid confusion, require that the rtattr
  58. * payload be exactly the param struct, which has a 4-byte aligned size.
  59. */
  60. if (RTA_PAYLOAD(rta) != sizeof(*param))
  61. return -EINVAL;
  62. BUILD_BUG_ON(sizeof(*param) % RTA_ALIGNTO);
  63. param = RTA_DATA(rta);
  64. keys->enckeylen = be32_to_cpu(param->enckeylen);
  65. key += rta->rta_len;
  66. keylen -= rta->rta_len;
  67. if (keylen < keys->enckeylen)
  68. return -EINVAL;
  69. keys->authkeylen = keylen - keys->enckeylen;
  70. keys->authkey = key;
  71. keys->enckey = key + keys->authkeylen;
  72. return 0;
  73. }
  74. EXPORT_SYMBOL_GPL(crypto_authenc_extractkeys);
  75. static int crypto_authenc_setkey(struct crypto_aead *authenc, const u8 *key,
  76. unsigned int keylen)
  77. {
  78. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  79. struct crypto_ahash *auth = ctx->auth;
  80. struct crypto_ablkcipher *enc = ctx->enc;
  81. struct crypto_authenc_keys keys;
  82. int err = -EINVAL;
  83. if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
  84. goto badkey;
  85. crypto_ahash_clear_flags(auth, CRYPTO_TFM_REQ_MASK);
  86. crypto_ahash_set_flags(auth, crypto_aead_get_flags(authenc) &
  87. CRYPTO_TFM_REQ_MASK);
  88. err = crypto_ahash_setkey(auth, keys.authkey, keys.authkeylen);
  89. crypto_aead_set_flags(authenc, crypto_ahash_get_flags(auth) &
  90. CRYPTO_TFM_RES_MASK);
  91. if (err)
  92. goto out;
  93. crypto_ablkcipher_clear_flags(enc, CRYPTO_TFM_REQ_MASK);
  94. crypto_ablkcipher_set_flags(enc, crypto_aead_get_flags(authenc) &
  95. CRYPTO_TFM_REQ_MASK);
  96. err = crypto_ablkcipher_setkey(enc, keys.enckey, keys.enckeylen);
  97. crypto_aead_set_flags(authenc, crypto_ablkcipher_get_flags(enc) &
  98. CRYPTO_TFM_RES_MASK);
  99. out:
  100. memzero_explicit(&keys, sizeof(keys));
  101. return err;
  102. badkey:
  103. crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
  104. goto out;
  105. }
  106. static void authenc_geniv_ahash_done(struct crypto_async_request *areq, int err)
  107. {
  108. struct aead_request *req = areq->data;
  109. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  110. struct aead_instance *inst = aead_alg_instance(authenc);
  111. struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
  112. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  113. struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
  114. if (err)
  115. goto out;
  116. scatterwalk_map_and_copy(ahreq->result, req->dst,
  117. req->assoclen + req->cryptlen,
  118. crypto_aead_authsize(authenc), 1);
  119. out:
  120. aead_request_complete(req, err);
  121. }
  122. static int crypto_authenc_genicv(struct aead_request *req, unsigned int flags)
  123. {
  124. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  125. struct aead_instance *inst = aead_alg_instance(authenc);
  126. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  127. struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
  128. struct crypto_ahash *auth = ctx->auth;
  129. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  130. struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
  131. u8 *hash = areq_ctx->tail;
  132. int err;
  133. hash = (u8 *)ALIGN((unsigned long)hash + crypto_ahash_alignmask(auth),
  134. crypto_ahash_alignmask(auth) + 1);
  135. ahash_request_set_tfm(ahreq, auth);
  136. ahash_request_set_crypt(ahreq, req->dst, hash,
  137. req->assoclen + req->cryptlen);
  138. ahash_request_set_callback(ahreq, flags,
  139. authenc_geniv_ahash_done, req);
  140. err = crypto_ahash_digest(ahreq);
  141. if (err)
  142. return err;
  143. scatterwalk_map_and_copy(hash, req->dst, req->assoclen + req->cryptlen,
  144. crypto_aead_authsize(authenc), 1);
  145. return 0;
  146. }
  147. static void crypto_authenc_encrypt_done(struct crypto_async_request *req,
  148. int err)
  149. {
  150. struct aead_request *areq = req->data;
  151. if (err)
  152. goto out;
  153. err = crypto_authenc_genicv(areq, 0);
  154. out:
  155. authenc_request_complete(areq, err);
  156. }
  157. static int crypto_authenc_copy_assoc(struct aead_request *req)
  158. {
  159. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  160. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  161. struct blkcipher_desc desc = {
  162. .tfm = ctx->null,
  163. };
  164. return crypto_blkcipher_encrypt(&desc, req->dst, req->src,
  165. req->assoclen);
  166. }
  167. static int crypto_authenc_encrypt(struct aead_request *req)
  168. {
  169. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  170. struct aead_instance *inst = aead_alg_instance(authenc);
  171. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  172. struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
  173. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  174. struct crypto_ablkcipher *enc = ctx->enc;
  175. unsigned int cryptlen = req->cryptlen;
  176. struct ablkcipher_request *abreq = (void *)(areq_ctx->tail +
  177. ictx->reqoff);
  178. struct scatterlist *src, *dst;
  179. int err;
  180. sg_init_table(areq_ctx->src, 2);
  181. src = scatterwalk_ffwd(areq_ctx->src, req->src, req->assoclen);
  182. dst = src;
  183. if (req->src != req->dst) {
  184. err = crypto_authenc_copy_assoc(req);
  185. if (err)
  186. return err;
  187. sg_init_table(areq_ctx->dst, 2);
  188. dst = scatterwalk_ffwd(areq_ctx->dst, req->dst, req->assoclen);
  189. }
  190. ablkcipher_request_set_tfm(abreq, enc);
  191. ablkcipher_request_set_callback(abreq, aead_request_flags(req),
  192. crypto_authenc_encrypt_done, req);
  193. ablkcipher_request_set_crypt(abreq, src, dst, cryptlen, req->iv);
  194. err = crypto_ablkcipher_encrypt(abreq);
  195. if (err)
  196. return err;
  197. return crypto_authenc_genicv(req, aead_request_flags(req));
  198. }
  199. static int crypto_authenc_decrypt_tail(struct aead_request *req,
  200. unsigned int flags)
  201. {
  202. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  203. struct aead_instance *inst = aead_alg_instance(authenc);
  204. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  205. struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
  206. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  207. struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
  208. struct ablkcipher_request *abreq = (void *)(areq_ctx->tail +
  209. ictx->reqoff);
  210. unsigned int authsize = crypto_aead_authsize(authenc);
  211. u8 *ihash = ahreq->result + authsize;
  212. struct scatterlist *src, *dst;
  213. scatterwalk_map_and_copy(ihash, req->src, ahreq->nbytes, authsize, 0);
  214. if (crypto_memneq(ihash, ahreq->result, authsize))
  215. return -EBADMSG;
  216. sg_init_table(areq_ctx->src, 2);
  217. src = scatterwalk_ffwd(areq_ctx->src, req->src, req->assoclen);
  218. dst = src;
  219. if (req->src != req->dst) {
  220. sg_init_table(areq_ctx->dst, 2);
  221. dst = scatterwalk_ffwd(areq_ctx->dst, req->dst, req->assoclen);
  222. }
  223. ablkcipher_request_set_tfm(abreq, ctx->enc);
  224. ablkcipher_request_set_callback(abreq, aead_request_flags(req),
  225. req->base.complete, req->base.data);
  226. ablkcipher_request_set_crypt(abreq, src, dst,
  227. req->cryptlen - authsize, req->iv);
  228. return crypto_ablkcipher_decrypt(abreq);
  229. }
  230. static void authenc_verify_ahash_done(struct crypto_async_request *areq,
  231. int err)
  232. {
  233. struct aead_request *req = areq->data;
  234. if (err)
  235. goto out;
  236. err = crypto_authenc_decrypt_tail(req, 0);
  237. out:
  238. authenc_request_complete(req, err);
  239. }
  240. static int crypto_authenc_decrypt(struct aead_request *req)
  241. {
  242. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  243. unsigned int authsize = crypto_aead_authsize(authenc);
  244. struct aead_instance *inst = aead_alg_instance(authenc);
  245. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  246. struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
  247. struct crypto_ahash *auth = ctx->auth;
  248. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  249. struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
  250. u8 *hash = areq_ctx->tail;
  251. int err;
  252. hash = (u8 *)ALIGN((unsigned long)hash + crypto_ahash_alignmask(auth),
  253. crypto_ahash_alignmask(auth) + 1);
  254. ahash_request_set_tfm(ahreq, auth);
  255. ahash_request_set_crypt(ahreq, req->src, hash,
  256. req->assoclen + req->cryptlen - authsize);
  257. ahash_request_set_callback(ahreq, aead_request_flags(req),
  258. authenc_verify_ahash_done, req);
  259. err = crypto_ahash_digest(ahreq);
  260. if (err)
  261. return err;
  262. return crypto_authenc_decrypt_tail(req, aead_request_flags(req));
  263. }
  264. static int crypto_authenc_init_tfm(struct crypto_aead *tfm)
  265. {
  266. struct aead_instance *inst = aead_alg_instance(tfm);
  267. struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
  268. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(tfm);
  269. struct crypto_ahash *auth;
  270. struct crypto_ablkcipher *enc;
  271. struct crypto_blkcipher *null;
  272. int err;
  273. auth = crypto_spawn_ahash(&ictx->auth);
  274. if (IS_ERR(auth))
  275. return PTR_ERR(auth);
  276. enc = crypto_spawn_skcipher(&ictx->enc);
  277. err = PTR_ERR(enc);
  278. if (IS_ERR(enc))
  279. goto err_free_ahash;
  280. null = crypto_get_default_null_skcipher();
  281. err = PTR_ERR(null);
  282. if (IS_ERR(null))
  283. goto err_free_skcipher;
  284. ctx->auth = auth;
  285. ctx->enc = enc;
  286. ctx->null = null;
  287. crypto_aead_set_reqsize(
  288. tfm,
  289. sizeof(struct authenc_request_ctx) +
  290. ictx->reqoff +
  291. max_t(unsigned int,
  292. crypto_ahash_reqsize(auth) +
  293. sizeof(struct ahash_request),
  294. sizeof(struct ablkcipher_request) +
  295. crypto_ablkcipher_reqsize(enc)));
  296. return 0;
  297. err_free_skcipher:
  298. crypto_free_ablkcipher(enc);
  299. err_free_ahash:
  300. crypto_free_ahash(auth);
  301. return err;
  302. }
  303. static void crypto_authenc_exit_tfm(struct crypto_aead *tfm)
  304. {
  305. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(tfm);
  306. crypto_free_ahash(ctx->auth);
  307. crypto_free_ablkcipher(ctx->enc);
  308. crypto_put_default_null_skcipher();
  309. }
  310. static void crypto_authenc_free(struct aead_instance *inst)
  311. {
  312. struct authenc_instance_ctx *ctx = aead_instance_ctx(inst);
  313. crypto_drop_skcipher(&ctx->enc);
  314. crypto_drop_ahash(&ctx->auth);
  315. kfree(inst);
  316. }
  317. static int crypto_authenc_create(struct crypto_template *tmpl,
  318. struct rtattr **tb)
  319. {
  320. struct crypto_attr_type *algt;
  321. struct aead_instance *inst;
  322. struct hash_alg_common *auth;
  323. struct crypto_alg *auth_base;
  324. struct crypto_alg *enc;
  325. struct authenc_instance_ctx *ctx;
  326. const char *enc_name;
  327. int err;
  328. algt = crypto_get_attr_type(tb);
  329. if (IS_ERR(algt))
  330. return PTR_ERR(algt);
  331. if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
  332. return -EINVAL;
  333. auth = ahash_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH,
  334. CRYPTO_ALG_TYPE_AHASH_MASK);
  335. if (IS_ERR(auth))
  336. return PTR_ERR(auth);
  337. auth_base = &auth->base;
  338. enc_name = crypto_attr_alg_name(tb[2]);
  339. err = PTR_ERR(enc_name);
  340. if (IS_ERR(enc_name))
  341. goto out_put_auth;
  342. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  343. err = -ENOMEM;
  344. if (!inst)
  345. goto out_put_auth;
  346. ctx = aead_instance_ctx(inst);
  347. err = crypto_init_ahash_spawn(&ctx->auth, auth,
  348. aead_crypto_instance(inst));
  349. if (err)
  350. goto err_free_inst;
  351. crypto_set_skcipher_spawn(&ctx->enc, aead_crypto_instance(inst));
  352. err = crypto_grab_skcipher(&ctx->enc, enc_name, 0,
  353. crypto_requires_sync(algt->type,
  354. algt->mask));
  355. if (err)
  356. goto err_drop_auth;
  357. enc = crypto_skcipher_spawn_alg(&ctx->enc);
  358. ctx->reqoff = ALIGN(2 * auth->digestsize + auth_base->cra_alignmask,
  359. auth_base->cra_alignmask + 1);
  360. err = -ENAMETOOLONG;
  361. if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
  362. "authenc(%s,%s)", auth_base->cra_name, enc->cra_name) >=
  363. CRYPTO_MAX_ALG_NAME)
  364. goto err_drop_enc;
  365. if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  366. "authenc(%s,%s)", auth_base->cra_driver_name,
  367. enc->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  368. goto err_drop_enc;
  369. inst->alg.base.cra_flags = enc->cra_flags & CRYPTO_ALG_ASYNC;
  370. inst->alg.base.cra_priority = enc->cra_priority * 10 +
  371. auth_base->cra_priority;
  372. inst->alg.base.cra_blocksize = enc->cra_blocksize;
  373. inst->alg.base.cra_alignmask = auth_base->cra_alignmask |
  374. enc->cra_alignmask;
  375. inst->alg.base.cra_ctxsize = sizeof(struct crypto_authenc_ctx);
  376. inst->alg.ivsize = enc->cra_ablkcipher.ivsize;
  377. inst->alg.maxauthsize = auth->digestsize;
  378. inst->alg.init = crypto_authenc_init_tfm;
  379. inst->alg.exit = crypto_authenc_exit_tfm;
  380. inst->alg.setkey = crypto_authenc_setkey;
  381. inst->alg.encrypt = crypto_authenc_encrypt;
  382. inst->alg.decrypt = crypto_authenc_decrypt;
  383. inst->free = crypto_authenc_free;
  384. err = aead_register_instance(tmpl, inst);
  385. if (err)
  386. goto err_drop_enc;
  387. out:
  388. crypto_mod_put(auth_base);
  389. return err;
  390. err_drop_enc:
  391. crypto_drop_skcipher(&ctx->enc);
  392. err_drop_auth:
  393. crypto_drop_ahash(&ctx->auth);
  394. err_free_inst:
  395. kfree(inst);
  396. out_put_auth:
  397. goto out;
  398. }
  399. static struct crypto_template crypto_authenc_tmpl = {
  400. .name = "authenc",
  401. .create = crypto_authenc_create,
  402. .module = THIS_MODULE,
  403. };
  404. static int __init crypto_authenc_module_init(void)
  405. {
  406. return crypto_register_template(&crypto_authenc_tmpl);
  407. }
  408. static void __exit crypto_authenc_module_exit(void)
  409. {
  410. crypto_unregister_template(&crypto_authenc_tmpl);
  411. }
  412. module_init(crypto_authenc_module_init);
  413. module_exit(crypto_authenc_module_exit);
  414. MODULE_LICENSE("GPL");
  415. MODULE_DESCRIPTION("Simple AEAD wrapper for IPsec");
  416. MODULE_ALIAS_CRYPTO("authenc");