authenc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. memzero_explicit(&keys, sizeof(keys));
  94. return err;
  95. badkey:
  96. crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
  97. goto out;
  98. }
  99. static void authenc_geniv_ahash_done(struct crypto_async_request *areq, int err)
  100. {
  101. struct aead_request *req = areq->data;
  102. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  103. struct aead_instance *inst = aead_alg_instance(authenc);
  104. struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
  105. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  106. struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
  107. if (err)
  108. goto out;
  109. scatterwalk_map_and_copy(ahreq->result, req->dst,
  110. req->assoclen + req->cryptlen,
  111. crypto_aead_authsize(authenc), 1);
  112. out:
  113. aead_request_complete(req, err);
  114. }
  115. static int crypto_authenc_genicv(struct aead_request *req, unsigned int flags)
  116. {
  117. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  118. struct aead_instance *inst = aead_alg_instance(authenc);
  119. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  120. struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
  121. struct crypto_ahash *auth = ctx->auth;
  122. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  123. struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
  124. u8 *hash = areq_ctx->tail;
  125. int err;
  126. hash = (u8 *)ALIGN((unsigned long)hash + crypto_ahash_alignmask(auth),
  127. crypto_ahash_alignmask(auth) + 1);
  128. ahash_request_set_tfm(ahreq, auth);
  129. ahash_request_set_crypt(ahreq, req->dst, hash,
  130. req->assoclen + req->cryptlen);
  131. ahash_request_set_callback(ahreq, flags,
  132. authenc_geniv_ahash_done, req);
  133. err = crypto_ahash_digest(ahreq);
  134. if (err)
  135. return err;
  136. scatterwalk_map_and_copy(hash, req->dst, req->assoclen + req->cryptlen,
  137. crypto_aead_authsize(authenc), 1);
  138. return 0;
  139. }
  140. static void crypto_authenc_encrypt_done(struct crypto_async_request *req,
  141. int err)
  142. {
  143. struct aead_request *areq = req->data;
  144. if (err)
  145. goto out;
  146. err = crypto_authenc_genicv(areq, 0);
  147. out:
  148. authenc_request_complete(areq, err);
  149. }
  150. static int crypto_authenc_copy_assoc(struct aead_request *req)
  151. {
  152. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  153. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  154. SKCIPHER_REQUEST_ON_STACK(skreq, ctx->null);
  155. skcipher_request_set_tfm(skreq, ctx->null);
  156. skcipher_request_set_callback(skreq, aead_request_flags(req),
  157. NULL, NULL);
  158. skcipher_request_set_crypt(skreq, req->src, req->dst, req->assoclen,
  159. NULL);
  160. return crypto_skcipher_encrypt(skreq);
  161. }
  162. static int crypto_authenc_encrypt(struct aead_request *req)
  163. {
  164. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  165. struct aead_instance *inst = aead_alg_instance(authenc);
  166. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  167. struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
  168. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  169. struct crypto_skcipher *enc = ctx->enc;
  170. unsigned int cryptlen = req->cryptlen;
  171. struct skcipher_request *skreq = (void *)(areq_ctx->tail +
  172. ictx->reqoff);
  173. struct scatterlist *src, *dst;
  174. int err;
  175. src = scatterwalk_ffwd(areq_ctx->src, req->src, req->assoclen);
  176. dst = src;
  177. if (req->src != req->dst) {
  178. err = crypto_authenc_copy_assoc(req);
  179. if (err)
  180. return err;
  181. dst = scatterwalk_ffwd(areq_ctx->dst, req->dst, req->assoclen);
  182. }
  183. skcipher_request_set_tfm(skreq, enc);
  184. skcipher_request_set_callback(skreq, aead_request_flags(req),
  185. crypto_authenc_encrypt_done, req);
  186. skcipher_request_set_crypt(skreq, src, dst, cryptlen, req->iv);
  187. err = crypto_skcipher_encrypt(skreq);
  188. if (err)
  189. return err;
  190. return crypto_authenc_genicv(req, aead_request_flags(req));
  191. }
  192. static int crypto_authenc_decrypt_tail(struct aead_request *req,
  193. unsigned int flags)
  194. {
  195. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  196. struct aead_instance *inst = aead_alg_instance(authenc);
  197. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  198. struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
  199. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  200. struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
  201. struct skcipher_request *skreq = (void *)(areq_ctx->tail +
  202. ictx->reqoff);
  203. unsigned int authsize = crypto_aead_authsize(authenc);
  204. u8 *ihash = ahreq->result + authsize;
  205. struct scatterlist *src, *dst;
  206. scatterwalk_map_and_copy(ihash, req->src, ahreq->nbytes, authsize, 0);
  207. if (crypto_memneq(ihash, ahreq->result, authsize))
  208. return -EBADMSG;
  209. src = scatterwalk_ffwd(areq_ctx->src, req->src, req->assoclen);
  210. dst = src;
  211. if (req->src != req->dst)
  212. dst = scatterwalk_ffwd(areq_ctx->dst, req->dst, req->assoclen);
  213. skcipher_request_set_tfm(skreq, ctx->enc);
  214. skcipher_request_set_callback(skreq, aead_request_flags(req),
  215. req->base.complete, req->base.data);
  216. skcipher_request_set_crypt(skreq, src, dst,
  217. req->cryptlen - authsize, req->iv);
  218. return crypto_skcipher_decrypt(skreq);
  219. }
  220. static void authenc_verify_ahash_done(struct crypto_async_request *areq,
  221. int err)
  222. {
  223. struct aead_request *req = areq->data;
  224. if (err)
  225. goto out;
  226. err = crypto_authenc_decrypt_tail(req, 0);
  227. out:
  228. authenc_request_complete(req, err);
  229. }
  230. static int crypto_authenc_decrypt(struct aead_request *req)
  231. {
  232. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  233. unsigned int authsize = crypto_aead_authsize(authenc);
  234. struct aead_instance *inst = aead_alg_instance(authenc);
  235. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  236. struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
  237. struct crypto_ahash *auth = ctx->auth;
  238. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  239. struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
  240. u8 *hash = areq_ctx->tail;
  241. int err;
  242. hash = (u8 *)ALIGN((unsigned long)hash + crypto_ahash_alignmask(auth),
  243. crypto_ahash_alignmask(auth) + 1);
  244. ahash_request_set_tfm(ahreq, auth);
  245. ahash_request_set_crypt(ahreq, req->src, hash,
  246. req->assoclen + req->cryptlen - authsize);
  247. ahash_request_set_callback(ahreq, aead_request_flags(req),
  248. authenc_verify_ahash_done, req);
  249. err = crypto_ahash_digest(ahreq);
  250. if (err)
  251. return err;
  252. return crypto_authenc_decrypt_tail(req, aead_request_flags(req));
  253. }
  254. static int crypto_authenc_init_tfm(struct crypto_aead *tfm)
  255. {
  256. struct aead_instance *inst = aead_alg_instance(tfm);
  257. struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
  258. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(tfm);
  259. struct crypto_ahash *auth;
  260. struct crypto_skcipher *enc;
  261. struct crypto_skcipher *null;
  262. int err;
  263. auth = crypto_spawn_ahash(&ictx->auth);
  264. if (IS_ERR(auth))
  265. return PTR_ERR(auth);
  266. enc = crypto_spawn_skcipher2(&ictx->enc);
  267. err = PTR_ERR(enc);
  268. if (IS_ERR(enc))
  269. goto err_free_ahash;
  270. null = crypto_get_default_null_skcipher2();
  271. err = PTR_ERR(null);
  272. if (IS_ERR(null))
  273. goto err_free_skcipher;
  274. ctx->auth = auth;
  275. ctx->enc = enc;
  276. ctx->null = null;
  277. crypto_aead_set_reqsize(
  278. tfm,
  279. sizeof(struct authenc_request_ctx) +
  280. ictx->reqoff +
  281. max_t(unsigned int,
  282. crypto_ahash_reqsize(auth) +
  283. sizeof(struct ahash_request),
  284. sizeof(struct skcipher_request) +
  285. crypto_skcipher_reqsize(enc)));
  286. return 0;
  287. err_free_skcipher:
  288. crypto_free_skcipher(enc);
  289. err_free_ahash:
  290. crypto_free_ahash(auth);
  291. return err;
  292. }
  293. static void crypto_authenc_exit_tfm(struct crypto_aead *tfm)
  294. {
  295. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(tfm);
  296. crypto_free_ahash(ctx->auth);
  297. crypto_free_skcipher(ctx->enc);
  298. crypto_put_default_null_skcipher2();
  299. }
  300. static void crypto_authenc_free(struct aead_instance *inst)
  301. {
  302. struct authenc_instance_ctx *ctx = aead_instance_ctx(inst);
  303. crypto_drop_skcipher(&ctx->enc);
  304. crypto_drop_ahash(&ctx->auth);
  305. kfree(inst);
  306. }
  307. static int crypto_authenc_create(struct crypto_template *tmpl,
  308. struct rtattr **tb)
  309. {
  310. struct crypto_attr_type *algt;
  311. struct aead_instance *inst;
  312. struct hash_alg_common *auth;
  313. struct crypto_alg *auth_base;
  314. struct skcipher_alg *enc;
  315. struct authenc_instance_ctx *ctx;
  316. const char *enc_name;
  317. int err;
  318. algt = crypto_get_attr_type(tb);
  319. if (IS_ERR(algt))
  320. return PTR_ERR(algt);
  321. if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
  322. return -EINVAL;
  323. auth = ahash_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH,
  324. CRYPTO_ALG_TYPE_AHASH_MASK |
  325. crypto_requires_sync(algt->type, algt->mask));
  326. if (IS_ERR(auth))
  327. return PTR_ERR(auth);
  328. auth_base = &auth->base;
  329. enc_name = crypto_attr_alg_name(tb[2]);
  330. err = PTR_ERR(enc_name);
  331. if (IS_ERR(enc_name))
  332. goto out_put_auth;
  333. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  334. err = -ENOMEM;
  335. if (!inst)
  336. goto out_put_auth;
  337. ctx = aead_instance_ctx(inst);
  338. err = crypto_init_ahash_spawn(&ctx->auth, auth,
  339. aead_crypto_instance(inst));
  340. if (err)
  341. goto err_free_inst;
  342. crypto_set_skcipher_spawn(&ctx->enc, aead_crypto_instance(inst));
  343. err = crypto_grab_skcipher2(&ctx->enc, enc_name, 0,
  344. crypto_requires_sync(algt->type,
  345. algt->mask));
  346. if (err)
  347. goto err_drop_auth;
  348. enc = crypto_spawn_skcipher_alg(&ctx->enc);
  349. ctx->reqoff = ALIGN(2 * auth->digestsize + auth_base->cra_alignmask,
  350. auth_base->cra_alignmask + 1);
  351. err = -ENAMETOOLONG;
  352. if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
  353. "authenc(%s,%s)", auth_base->cra_name,
  354. enc->base.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->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  360. goto err_drop_enc;
  361. inst->alg.base.cra_flags = (auth_base->cra_flags |
  362. enc->base.cra_flags) & CRYPTO_ALG_ASYNC;
  363. inst->alg.base.cra_priority = enc->base.cra_priority * 10 +
  364. auth_base->cra_priority;
  365. inst->alg.base.cra_blocksize = enc->base.cra_blocksize;
  366. inst->alg.base.cra_alignmask = auth_base->cra_alignmask |
  367. enc->base.cra_alignmask;
  368. inst->alg.base.cra_ctxsize = sizeof(struct crypto_authenc_ctx);
  369. inst->alg.ivsize = crypto_skcipher_alg_ivsize(enc);
  370. inst->alg.chunksize = crypto_skcipher_alg_chunksize(enc);
  371. inst->alg.maxauthsize = auth->digestsize;
  372. inst->alg.init = crypto_authenc_init_tfm;
  373. inst->alg.exit = crypto_authenc_exit_tfm;
  374. inst->alg.setkey = crypto_authenc_setkey;
  375. inst->alg.encrypt = crypto_authenc_encrypt;
  376. inst->alg.decrypt = crypto_authenc_decrypt;
  377. inst->free = crypto_authenc_free;
  378. err = aead_register_instance(tmpl, inst);
  379. if (err)
  380. goto err_drop_enc;
  381. out:
  382. crypto_mod_put(auth_base);
  383. return err;
  384. err_drop_enc:
  385. crypto_drop_skcipher(&ctx->enc);
  386. err_drop_auth:
  387. crypto_drop_ahash(&ctx->auth);
  388. err_free_inst:
  389. kfree(inst);
  390. out_put_auth:
  391. goto out;
  392. }
  393. static struct crypto_template crypto_authenc_tmpl = {
  394. .name = "authenc",
  395. .create = crypto_authenc_create,
  396. .module = THIS_MODULE,
  397. };
  398. static int __init crypto_authenc_module_init(void)
  399. {
  400. return crypto_register_template(&crypto_authenc_tmpl);
  401. }
  402. static void __exit crypto_authenc_module_exit(void)
  403. {
  404. crypto_unregister_template(&crypto_authenc_tmpl);
  405. }
  406. module_init(crypto_authenc_module_init);
  407. module_exit(crypto_authenc_module_exit);
  408. MODULE_LICENSE("GPL");
  409. MODULE_DESCRIPTION("Simple AEAD wrapper for IPsec");
  410. MODULE_ALIAS_CRYPTO("authenc");