authenc.c 14 KB

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