authenc.c 13 KB

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