authenc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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_skcipher *enc;
  33. struct crypto_skcipher *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_skcipher *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_skcipher_clear_flags(enc, CRYPTO_TFM_REQ_MASK);
  94. crypto_skcipher_set_flags(enc, crypto_aead_get_flags(authenc) &
  95. CRYPTO_TFM_REQ_MASK);
  96. err = crypto_skcipher_setkey(enc, keys.enckey, keys.enckeylen);
  97. crypto_aead_set_flags(authenc, crypto_skcipher_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. SKCIPHER_REQUEST_ON_STACK(skreq, ctx->null);
  162. skcipher_request_set_tfm(skreq, ctx->null);
  163. skcipher_request_set_callback(skreq, aead_request_flags(req),
  164. NULL, NULL);
  165. skcipher_request_set_crypt(skreq, req->src, req->dst, req->assoclen,
  166. NULL);
  167. return crypto_skcipher_encrypt(skreq);
  168. }
  169. static int crypto_authenc_encrypt(struct aead_request *req)
  170. {
  171. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  172. struct aead_instance *inst = aead_alg_instance(authenc);
  173. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  174. struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
  175. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  176. struct crypto_skcipher *enc = ctx->enc;
  177. unsigned int cryptlen = req->cryptlen;
  178. struct skcipher_request *skreq = (void *)(areq_ctx->tail +
  179. ictx->reqoff);
  180. struct scatterlist *src, *dst;
  181. int err;
  182. src = scatterwalk_ffwd(areq_ctx->src, req->src, req->assoclen);
  183. dst = src;
  184. if (req->src != req->dst) {
  185. err = crypto_authenc_copy_assoc(req);
  186. if (err)
  187. return err;
  188. dst = scatterwalk_ffwd(areq_ctx->dst, req->dst, req->assoclen);
  189. }
  190. skcipher_request_set_tfm(skreq, enc);
  191. skcipher_request_set_callback(skreq, aead_request_flags(req),
  192. crypto_authenc_encrypt_done, req);
  193. skcipher_request_set_crypt(skreq, src, dst, cryptlen, req->iv);
  194. err = crypto_skcipher_encrypt(skreq);
  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 skcipher_request *skreq = (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. src = scatterwalk_ffwd(areq_ctx->src, req->src, req->assoclen);
  217. dst = src;
  218. if (req->src != req->dst)
  219. dst = scatterwalk_ffwd(areq_ctx->dst, req->dst, req->assoclen);
  220. skcipher_request_set_tfm(skreq, ctx->enc);
  221. skcipher_request_set_callback(skreq, aead_request_flags(req),
  222. req->base.complete, req->base.data);
  223. skcipher_request_set_crypt(skreq, src, dst,
  224. req->cryptlen - authsize, req->iv);
  225. return crypto_skcipher_decrypt(skreq);
  226. }
  227. static void authenc_verify_ahash_done(struct crypto_async_request *areq,
  228. int err)
  229. {
  230. struct aead_request *req = areq->data;
  231. if (err)
  232. goto out;
  233. err = crypto_authenc_decrypt_tail(req, 0);
  234. out:
  235. authenc_request_complete(req, err);
  236. }
  237. static int crypto_authenc_decrypt(struct aead_request *req)
  238. {
  239. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  240. unsigned int authsize = crypto_aead_authsize(authenc);
  241. struct aead_instance *inst = aead_alg_instance(authenc);
  242. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  243. struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
  244. struct crypto_ahash *auth = ctx->auth;
  245. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  246. struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
  247. u8 *hash = areq_ctx->tail;
  248. int err;
  249. hash = (u8 *)ALIGN((unsigned long)hash + crypto_ahash_alignmask(auth),
  250. crypto_ahash_alignmask(auth) + 1);
  251. ahash_request_set_tfm(ahreq, auth);
  252. ahash_request_set_crypt(ahreq, req->src, hash,
  253. req->assoclen + req->cryptlen - authsize);
  254. ahash_request_set_callback(ahreq, aead_request_flags(req),
  255. authenc_verify_ahash_done, req);
  256. err = crypto_ahash_digest(ahreq);
  257. if (err)
  258. return err;
  259. return crypto_authenc_decrypt_tail(req, aead_request_flags(req));
  260. }
  261. static int crypto_authenc_init_tfm(struct crypto_aead *tfm)
  262. {
  263. struct aead_instance *inst = aead_alg_instance(tfm);
  264. struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
  265. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(tfm);
  266. struct crypto_ahash *auth;
  267. struct crypto_skcipher *enc;
  268. struct crypto_skcipher *null;
  269. int err;
  270. auth = crypto_spawn_ahash(&ictx->auth);
  271. if (IS_ERR(auth))
  272. return PTR_ERR(auth);
  273. enc = crypto_spawn_skcipher(&ictx->enc);
  274. err = PTR_ERR(enc);
  275. if (IS_ERR(enc))
  276. goto err_free_ahash;
  277. null = crypto_get_default_null_skcipher();
  278. err = PTR_ERR(null);
  279. if (IS_ERR(null))
  280. goto err_free_skcipher;
  281. ctx->auth = auth;
  282. ctx->enc = enc;
  283. ctx->null = null;
  284. crypto_aead_set_reqsize(
  285. tfm,
  286. sizeof(struct authenc_request_ctx) +
  287. ictx->reqoff +
  288. max_t(unsigned int,
  289. crypto_ahash_reqsize(auth) +
  290. sizeof(struct ahash_request),
  291. sizeof(struct skcipher_request) +
  292. crypto_skcipher_reqsize(enc)));
  293. return 0;
  294. err_free_skcipher:
  295. crypto_free_skcipher(enc);
  296. err_free_ahash:
  297. crypto_free_ahash(auth);
  298. return err;
  299. }
  300. static void crypto_authenc_exit_tfm(struct crypto_aead *tfm)
  301. {
  302. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(tfm);
  303. crypto_free_ahash(ctx->auth);
  304. crypto_free_skcipher(ctx->enc);
  305. crypto_put_default_null_skcipher();
  306. }
  307. static void crypto_authenc_free(struct aead_instance *inst)
  308. {
  309. struct authenc_instance_ctx *ctx = aead_instance_ctx(inst);
  310. crypto_drop_skcipher(&ctx->enc);
  311. crypto_drop_ahash(&ctx->auth);
  312. kfree(inst);
  313. }
  314. static int crypto_authenc_create(struct crypto_template *tmpl,
  315. struct rtattr **tb)
  316. {
  317. struct crypto_attr_type *algt;
  318. struct aead_instance *inst;
  319. struct hash_alg_common *auth;
  320. struct crypto_alg *auth_base;
  321. struct skcipher_alg *enc;
  322. struct authenc_instance_ctx *ctx;
  323. const char *enc_name;
  324. int err;
  325. algt = crypto_get_attr_type(tb);
  326. if (IS_ERR(algt))
  327. return PTR_ERR(algt);
  328. if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
  329. return -EINVAL;
  330. auth = ahash_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH,
  331. CRYPTO_ALG_TYPE_AHASH_MASK |
  332. crypto_requires_sync(algt->type, algt->mask));
  333. if (IS_ERR(auth))
  334. return PTR_ERR(auth);
  335. auth_base = &auth->base;
  336. enc_name = crypto_attr_alg_name(tb[2]);
  337. err = PTR_ERR(enc_name);
  338. if (IS_ERR(enc_name))
  339. goto out_put_auth;
  340. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  341. err = -ENOMEM;
  342. if (!inst)
  343. goto out_put_auth;
  344. ctx = aead_instance_ctx(inst);
  345. err = crypto_init_ahash_spawn(&ctx->auth, auth,
  346. aead_crypto_instance(inst));
  347. if (err)
  348. goto err_free_inst;
  349. crypto_set_skcipher_spawn(&ctx->enc, aead_crypto_instance(inst));
  350. err = crypto_grab_skcipher(&ctx->enc, enc_name, 0,
  351. crypto_requires_sync(algt->type,
  352. algt->mask));
  353. if (err)
  354. goto err_drop_auth;
  355. enc = crypto_spawn_skcipher_alg(&ctx->enc);
  356. ctx->reqoff = ALIGN(2 * auth->digestsize + auth_base->cra_alignmask,
  357. auth_base->cra_alignmask + 1);
  358. err = -ENAMETOOLONG;
  359. if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
  360. "authenc(%s,%s)", auth_base->cra_name,
  361. enc->base.cra_name) >=
  362. CRYPTO_MAX_ALG_NAME)
  363. goto err_drop_enc;
  364. if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  365. "authenc(%s,%s)", auth_base->cra_driver_name,
  366. enc->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  367. goto err_drop_enc;
  368. inst->alg.base.cra_flags = (auth_base->cra_flags |
  369. enc->base.cra_flags) & CRYPTO_ALG_ASYNC;
  370. inst->alg.base.cra_priority = enc->base.cra_priority * 10 +
  371. auth_base->cra_priority;
  372. inst->alg.base.cra_blocksize = enc->base.cra_blocksize;
  373. inst->alg.base.cra_alignmask = auth_base->cra_alignmask |
  374. enc->base.cra_alignmask;
  375. inst->alg.base.cra_ctxsize = sizeof(struct crypto_authenc_ctx);
  376. inst->alg.ivsize = crypto_skcipher_alg_ivsize(enc);
  377. inst->alg.chunksize = crypto_skcipher_alg_chunksize(enc);
  378. inst->alg.maxauthsize = auth->digestsize;
  379. inst->alg.init = crypto_authenc_init_tfm;
  380. inst->alg.exit = crypto_authenc_exit_tfm;
  381. inst->alg.setkey = crypto_authenc_setkey;
  382. inst->alg.encrypt = crypto_authenc_encrypt;
  383. inst->alg.decrypt = crypto_authenc_decrypt;
  384. inst->free = crypto_authenc_free;
  385. err = aead_register_instance(tmpl, inst);
  386. if (err)
  387. goto err_drop_enc;
  388. out:
  389. crypto_mod_put(auth_base);
  390. return err;
  391. err_drop_enc:
  392. crypto_drop_skcipher(&ctx->enc);
  393. err_drop_auth:
  394. crypto_drop_ahash(&ctx->auth);
  395. err_free_inst:
  396. kfree(inst);
  397. out_put_auth:
  398. goto out;
  399. }
  400. static struct crypto_template crypto_authenc_tmpl = {
  401. .name = "authenc",
  402. .create = crypto_authenc_create,
  403. .module = THIS_MODULE,
  404. };
  405. static int __init crypto_authenc_module_init(void)
  406. {
  407. return crypto_register_template(&crypto_authenc_tmpl);
  408. }
  409. static void __exit crypto_authenc_module_exit(void)
  410. {
  411. crypto_unregister_template(&crypto_authenc_tmpl);
  412. }
  413. module_init(crypto_authenc_module_init);
  414. module_exit(crypto_authenc_module_exit);
  415. MODULE_LICENSE("GPL");
  416. MODULE_DESCRIPTION("Simple AEAD wrapper for IPsec");
  417. MODULE_ALIAS_CRYPTO("authenc");