authenc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*
  2. * Authenc: Simple AEAD wrapper for IPsec
  3. *
  4. * Copyright (c) 2007 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/aead.h>
  13. #include <crypto/internal/hash.h>
  14. #include <crypto/internal/skcipher.h>
  15. #include <crypto/authenc.h>
  16. #include <crypto/scatterwalk.h>
  17. #include <linux/err.h>
  18. #include <linux/init.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/rtnetlink.h>
  22. #include <linux/slab.h>
  23. #include <linux/spinlock.h>
  24. struct authenc_instance_ctx {
  25. struct crypto_spawn auth;
  26. struct crypto_skcipher_spawn enc;
  27. };
  28. struct crypto_authenc_ctx {
  29. spinlock_t auth_lock;
  30. struct crypto_hash *auth;
  31. struct crypto_ablkcipher *enc;
  32. };
  33. static int crypto_authenc_setkey(struct crypto_aead *authenc, const u8 *key,
  34. unsigned int keylen)
  35. {
  36. unsigned int authkeylen;
  37. unsigned int enckeylen;
  38. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  39. struct crypto_hash *auth = ctx->auth;
  40. struct crypto_ablkcipher *enc = ctx->enc;
  41. struct rtattr *rta = (void *)key;
  42. struct crypto_authenc_key_param *param;
  43. int err = -EINVAL;
  44. if (!RTA_OK(rta, keylen))
  45. goto badkey;
  46. if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
  47. goto badkey;
  48. if (RTA_PAYLOAD(rta) < sizeof(*param))
  49. goto badkey;
  50. param = RTA_DATA(rta);
  51. enckeylen = be32_to_cpu(param->enckeylen);
  52. key += RTA_ALIGN(rta->rta_len);
  53. keylen -= RTA_ALIGN(rta->rta_len);
  54. if (keylen < enckeylen)
  55. goto badkey;
  56. authkeylen = keylen - enckeylen;
  57. crypto_hash_clear_flags(auth, CRYPTO_TFM_REQ_MASK);
  58. crypto_hash_set_flags(auth, crypto_aead_get_flags(authenc) &
  59. CRYPTO_TFM_REQ_MASK);
  60. err = crypto_hash_setkey(auth, key, authkeylen);
  61. crypto_aead_set_flags(authenc, crypto_hash_get_flags(auth) &
  62. CRYPTO_TFM_RES_MASK);
  63. if (err)
  64. goto out;
  65. crypto_ablkcipher_clear_flags(enc, CRYPTO_TFM_REQ_MASK);
  66. crypto_ablkcipher_set_flags(enc, crypto_aead_get_flags(authenc) &
  67. CRYPTO_TFM_REQ_MASK);
  68. err = crypto_ablkcipher_setkey(enc, key + authkeylen, enckeylen);
  69. crypto_aead_set_flags(authenc, crypto_ablkcipher_get_flags(enc) &
  70. CRYPTO_TFM_RES_MASK);
  71. out:
  72. return err;
  73. badkey:
  74. crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
  75. goto out;
  76. }
  77. static void authenc_chain(struct scatterlist *head, struct scatterlist *sg,
  78. int chain)
  79. {
  80. if (chain) {
  81. head->length += sg->length;
  82. sg = scatterwalk_sg_next(sg);
  83. }
  84. if (sg)
  85. scatterwalk_sg_chain(head, 2, sg);
  86. else
  87. sg_mark_end(head);
  88. }
  89. static u8 *crypto_authenc_hash(struct aead_request *req, unsigned int flags,
  90. struct scatterlist *cipher,
  91. unsigned int cryptlen)
  92. {
  93. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  94. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  95. struct crypto_hash *auth = ctx->auth;
  96. struct hash_desc desc = {
  97. .tfm = auth,
  98. .flags = aead_request_flags(req) & flags,
  99. };
  100. u8 *hash = aead_request_ctx(req);
  101. int err;
  102. hash = (u8 *)ALIGN((unsigned long)hash + crypto_hash_alignmask(auth),
  103. crypto_hash_alignmask(auth) + 1);
  104. spin_lock_bh(&ctx->auth_lock);
  105. err = crypto_hash_init(&desc);
  106. if (err)
  107. goto auth_unlock;
  108. err = crypto_hash_update(&desc, req->assoc, req->assoclen);
  109. if (err)
  110. goto auth_unlock;
  111. err = crypto_hash_update(&desc, cipher, cryptlen);
  112. if (err)
  113. goto auth_unlock;
  114. err = crypto_hash_final(&desc, hash);
  115. auth_unlock:
  116. spin_unlock_bh(&ctx->auth_lock);
  117. if (err)
  118. return ERR_PTR(err);
  119. return hash;
  120. }
  121. static int crypto_authenc_genicv(struct aead_request *req, u8 *iv,
  122. unsigned int flags)
  123. {
  124. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  125. struct scatterlist *dst = req->dst;
  126. struct scatterlist cipher[2];
  127. struct page *dstp;
  128. unsigned int ivsize = crypto_aead_ivsize(authenc);
  129. unsigned int cryptlen;
  130. u8 *vdst;
  131. u8 *hash;
  132. dstp = sg_page(dst);
  133. vdst = PageHighMem(dstp) ? NULL : page_address(dstp) + dst->offset;
  134. sg_init_table(cipher, 2);
  135. sg_set_buf(cipher, iv, ivsize);
  136. authenc_chain(cipher, dst, vdst == iv + ivsize);
  137. cryptlen = req->cryptlen + ivsize;
  138. hash = crypto_authenc_hash(req, flags, cipher, cryptlen);
  139. if (IS_ERR(hash))
  140. return PTR_ERR(hash);
  141. scatterwalk_map_and_copy(hash, cipher, cryptlen,
  142. crypto_aead_authsize(authenc), 1);
  143. return 0;
  144. }
  145. static void crypto_authenc_encrypt_done(struct crypto_async_request *req,
  146. int err)
  147. {
  148. struct aead_request *areq = req->data;
  149. if (!err) {
  150. struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
  151. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  152. struct ablkcipher_request *abreq = aead_request_ctx(areq);
  153. u8 *iv = (u8 *)(abreq + 1) +
  154. crypto_ablkcipher_reqsize(ctx->enc);
  155. err = crypto_authenc_genicv(areq, iv, 0);
  156. }
  157. aead_request_complete(areq, err);
  158. }
  159. static int crypto_authenc_encrypt(struct aead_request *req)
  160. {
  161. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  162. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  163. struct ablkcipher_request *abreq = aead_request_ctx(req);
  164. struct crypto_ablkcipher *enc = ctx->enc;
  165. struct scatterlist *dst = req->dst;
  166. unsigned int cryptlen = req->cryptlen;
  167. u8 *iv = (u8 *)(abreq + 1) + crypto_ablkcipher_reqsize(enc);
  168. int err;
  169. ablkcipher_request_set_tfm(abreq, enc);
  170. ablkcipher_request_set_callback(abreq, aead_request_flags(req),
  171. crypto_authenc_encrypt_done, req);
  172. ablkcipher_request_set_crypt(abreq, req->src, dst, cryptlen, req->iv);
  173. memcpy(iv, req->iv, crypto_aead_ivsize(authenc));
  174. err = crypto_ablkcipher_encrypt(abreq);
  175. if (err)
  176. return err;
  177. return crypto_authenc_genicv(req, iv, CRYPTO_TFM_REQ_MAY_SLEEP);
  178. }
  179. static void crypto_authenc_givencrypt_done(struct crypto_async_request *req,
  180. int err)
  181. {
  182. struct aead_request *areq = req->data;
  183. if (!err) {
  184. struct skcipher_givcrypt_request *greq = aead_request_ctx(areq);
  185. err = crypto_authenc_genicv(areq, greq->giv, 0);
  186. }
  187. aead_request_complete(areq, err);
  188. }
  189. static int crypto_authenc_givencrypt(struct aead_givcrypt_request *req)
  190. {
  191. struct crypto_aead *authenc = aead_givcrypt_reqtfm(req);
  192. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  193. struct aead_request *areq = &req->areq;
  194. struct skcipher_givcrypt_request *greq = aead_request_ctx(areq);
  195. u8 *iv = req->giv;
  196. int err;
  197. skcipher_givcrypt_set_tfm(greq, ctx->enc);
  198. skcipher_givcrypt_set_callback(greq, aead_request_flags(areq),
  199. crypto_authenc_givencrypt_done, areq);
  200. skcipher_givcrypt_set_crypt(greq, areq->src, areq->dst, areq->cryptlen,
  201. areq->iv);
  202. skcipher_givcrypt_set_giv(greq, iv, req->seq);
  203. err = crypto_skcipher_givencrypt(greq);
  204. if (err)
  205. return err;
  206. return crypto_authenc_genicv(areq, iv, CRYPTO_TFM_REQ_MAY_SLEEP);
  207. }
  208. static int crypto_authenc_verify(struct aead_request *req,
  209. struct scatterlist *cipher,
  210. unsigned int cryptlen)
  211. {
  212. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  213. u8 *ohash;
  214. u8 *ihash;
  215. unsigned int authsize;
  216. ohash = crypto_authenc_hash(req, CRYPTO_TFM_REQ_MAY_SLEEP, cipher,
  217. cryptlen);
  218. if (IS_ERR(ohash))
  219. return PTR_ERR(ohash);
  220. authsize = crypto_aead_authsize(authenc);
  221. ihash = ohash + authsize;
  222. scatterwalk_map_and_copy(ihash, cipher, cryptlen, authsize, 0);
  223. return memcmp(ihash, ohash, authsize) ? -EBADMSG: 0;
  224. }
  225. static int crypto_authenc_iverify(struct aead_request *req, u8 *iv,
  226. unsigned int cryptlen)
  227. {
  228. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  229. struct scatterlist *src = req->src;
  230. struct scatterlist cipher[2];
  231. struct page *srcp;
  232. unsigned int ivsize = crypto_aead_ivsize(authenc);
  233. u8 *vsrc;
  234. srcp = sg_page(src);
  235. vsrc = PageHighMem(srcp) ? NULL : page_address(srcp) + src->offset;
  236. sg_init_table(cipher, 2);
  237. sg_set_buf(cipher, iv, ivsize);
  238. authenc_chain(cipher, src, vsrc == iv + ivsize);
  239. return crypto_authenc_verify(req, cipher, cryptlen + ivsize);
  240. }
  241. static int crypto_authenc_decrypt(struct aead_request *req)
  242. {
  243. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  244. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  245. struct ablkcipher_request *abreq = aead_request_ctx(req);
  246. unsigned int cryptlen = req->cryptlen;
  247. unsigned int authsize = crypto_aead_authsize(authenc);
  248. u8 *iv = req->iv;
  249. int err;
  250. if (cryptlen < authsize)
  251. return -EINVAL;
  252. cryptlen -= authsize;
  253. err = crypto_authenc_iverify(req, iv, cryptlen);
  254. if (err)
  255. return err;
  256. ablkcipher_request_set_tfm(abreq, ctx->enc);
  257. ablkcipher_request_set_callback(abreq, aead_request_flags(req),
  258. req->base.complete, req->base.data);
  259. ablkcipher_request_set_crypt(abreq, req->src, req->dst, cryptlen, iv);
  260. return crypto_ablkcipher_decrypt(abreq);
  261. }
  262. static int crypto_authenc_init_tfm(struct crypto_tfm *tfm)
  263. {
  264. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  265. struct authenc_instance_ctx *ictx = crypto_instance_ctx(inst);
  266. struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm);
  267. struct crypto_hash *auth;
  268. struct crypto_ablkcipher *enc;
  269. int err;
  270. auth = crypto_spawn_hash(&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_hash;
  277. ctx->auth = auth;
  278. ctx->enc = enc;
  279. tfm->crt_aead.reqsize = max_t(unsigned int,
  280. (crypto_hash_alignmask(auth) &
  281. ~(crypto_tfm_ctx_alignment() - 1)) +
  282. crypto_hash_digestsize(auth) * 2,
  283. sizeof(struct skcipher_givcrypt_request) +
  284. crypto_ablkcipher_reqsize(enc) +
  285. crypto_ablkcipher_ivsize(enc));
  286. spin_lock_init(&ctx->auth_lock);
  287. return 0;
  288. err_free_hash:
  289. crypto_free_hash(auth);
  290. return err;
  291. }
  292. static void crypto_authenc_exit_tfm(struct crypto_tfm *tfm)
  293. {
  294. struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm);
  295. crypto_free_hash(ctx->auth);
  296. crypto_free_ablkcipher(ctx->enc);
  297. }
  298. static struct crypto_instance *crypto_authenc_alloc(struct rtattr **tb)
  299. {
  300. struct crypto_attr_type *algt;
  301. struct crypto_instance *inst;
  302. struct crypto_alg *auth;
  303. struct crypto_alg *enc;
  304. struct authenc_instance_ctx *ctx;
  305. const char *enc_name;
  306. int err;
  307. algt = crypto_get_attr_type(tb);
  308. err = PTR_ERR(algt);
  309. if (IS_ERR(algt))
  310. return ERR_PTR(err);
  311. if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
  312. return ERR_PTR(-EINVAL);
  313. auth = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH,
  314. CRYPTO_ALG_TYPE_HASH_MASK);
  315. if (IS_ERR(auth))
  316. return ERR_PTR(PTR_ERR(auth));
  317. enc_name = crypto_attr_alg_name(tb[2]);
  318. err = PTR_ERR(enc_name);
  319. if (IS_ERR(enc_name))
  320. goto out_put_auth;
  321. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  322. err = -ENOMEM;
  323. if (!inst)
  324. goto out_put_auth;
  325. ctx = crypto_instance_ctx(inst);
  326. err = crypto_init_spawn(&ctx->auth, auth, inst, CRYPTO_ALG_TYPE_MASK);
  327. if (err)
  328. goto err_free_inst;
  329. crypto_set_skcipher_spawn(&ctx->enc, inst);
  330. err = crypto_grab_skcipher(&ctx->enc, enc_name, 0,
  331. crypto_requires_sync(algt->type,
  332. algt->mask));
  333. if (err)
  334. goto err_drop_auth;
  335. enc = crypto_skcipher_spawn_alg(&ctx->enc);
  336. err = -ENAMETOOLONG;
  337. if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
  338. "authenc(%s,%s)", auth->cra_name, enc->cra_name) >=
  339. CRYPTO_MAX_ALG_NAME)
  340. goto err_drop_enc;
  341. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  342. "authenc(%s,%s)", auth->cra_driver_name,
  343. enc->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  344. goto err_drop_enc;
  345. inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD;
  346. inst->alg.cra_flags |= enc->cra_flags & CRYPTO_ALG_ASYNC;
  347. inst->alg.cra_priority = enc->cra_priority * 10 + auth->cra_priority;
  348. inst->alg.cra_blocksize = enc->cra_blocksize;
  349. inst->alg.cra_alignmask = auth->cra_alignmask | enc->cra_alignmask;
  350. inst->alg.cra_type = &crypto_aead_type;
  351. inst->alg.cra_aead.ivsize = enc->cra_ablkcipher.ivsize;
  352. inst->alg.cra_aead.maxauthsize = auth->cra_type == &crypto_hash_type ?
  353. auth->cra_hash.digestsize :
  354. auth->cra_type ?
  355. __crypto_shash_alg(auth)->digestsize :
  356. auth->cra_digest.dia_digestsize;
  357. inst->alg.cra_ctxsize = sizeof(struct crypto_authenc_ctx);
  358. inst->alg.cra_init = crypto_authenc_init_tfm;
  359. inst->alg.cra_exit = crypto_authenc_exit_tfm;
  360. inst->alg.cra_aead.setkey = crypto_authenc_setkey;
  361. inst->alg.cra_aead.encrypt = crypto_authenc_encrypt;
  362. inst->alg.cra_aead.decrypt = crypto_authenc_decrypt;
  363. inst->alg.cra_aead.givencrypt = crypto_authenc_givencrypt;
  364. out:
  365. crypto_mod_put(auth);
  366. return inst;
  367. err_drop_enc:
  368. crypto_drop_skcipher(&ctx->enc);
  369. err_drop_auth:
  370. crypto_drop_spawn(&ctx->auth);
  371. err_free_inst:
  372. kfree(inst);
  373. out_put_auth:
  374. inst = ERR_PTR(err);
  375. goto out;
  376. }
  377. static void crypto_authenc_free(struct crypto_instance *inst)
  378. {
  379. struct authenc_instance_ctx *ctx = crypto_instance_ctx(inst);
  380. crypto_drop_skcipher(&ctx->enc);
  381. crypto_drop_spawn(&ctx->auth);
  382. kfree(inst);
  383. }
  384. static struct crypto_template crypto_authenc_tmpl = {
  385. .name = "authenc",
  386. .alloc = crypto_authenc_alloc,
  387. .free = crypto_authenc_free,
  388. .module = THIS_MODULE,
  389. };
  390. static int __init crypto_authenc_module_init(void)
  391. {
  392. return crypto_register_template(&crypto_authenc_tmpl);
  393. }
  394. static void __exit crypto_authenc_module_exit(void)
  395. {
  396. crypto_unregister_template(&crypto_authenc_tmpl);
  397. }
  398. module_init(crypto_authenc_module_init);
  399. module_exit(crypto_authenc_module_exit);
  400. MODULE_LICENSE("GPL");
  401. MODULE_DESCRIPTION("Simple AEAD wrapper for IPsec");