authenc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. if (ivsize) {
  135. sg_init_table(cipher, 2);
  136. sg_set_buf(cipher, iv, ivsize);
  137. authenc_chain(cipher, dst, vdst == iv + ivsize);
  138. dst = cipher;
  139. }
  140. cryptlen = req->cryptlen + ivsize;
  141. hash = crypto_authenc_hash(req, flags, dst, cryptlen);
  142. if (IS_ERR(hash))
  143. return PTR_ERR(hash);
  144. scatterwalk_map_and_copy(hash, dst, cryptlen,
  145. crypto_aead_authsize(authenc), 1);
  146. return 0;
  147. }
  148. static void crypto_authenc_encrypt_done(struct crypto_async_request *req,
  149. int err)
  150. {
  151. struct aead_request *areq = req->data;
  152. if (!err) {
  153. struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
  154. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  155. struct ablkcipher_request *abreq = aead_request_ctx(areq);
  156. u8 *iv = (u8 *)(abreq + 1) +
  157. crypto_ablkcipher_reqsize(ctx->enc);
  158. err = crypto_authenc_genicv(areq, iv, 0);
  159. }
  160. aead_request_complete(areq, err);
  161. }
  162. static int crypto_authenc_encrypt(struct aead_request *req)
  163. {
  164. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  165. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  166. struct ablkcipher_request *abreq = aead_request_ctx(req);
  167. struct crypto_ablkcipher *enc = ctx->enc;
  168. struct scatterlist *dst = req->dst;
  169. unsigned int cryptlen = req->cryptlen;
  170. u8 *iv = (u8 *)(abreq + 1) + crypto_ablkcipher_reqsize(enc);
  171. int err;
  172. ablkcipher_request_set_tfm(abreq, enc);
  173. ablkcipher_request_set_callback(abreq, aead_request_flags(req),
  174. crypto_authenc_encrypt_done, req);
  175. ablkcipher_request_set_crypt(abreq, req->src, dst, cryptlen, req->iv);
  176. memcpy(iv, req->iv, crypto_aead_ivsize(authenc));
  177. err = crypto_ablkcipher_encrypt(abreq);
  178. if (err)
  179. return err;
  180. return crypto_authenc_genicv(req, iv, CRYPTO_TFM_REQ_MAY_SLEEP);
  181. }
  182. static void crypto_authenc_givencrypt_done(struct crypto_async_request *req,
  183. int err)
  184. {
  185. struct aead_request *areq = req->data;
  186. if (!err) {
  187. struct skcipher_givcrypt_request *greq = aead_request_ctx(areq);
  188. err = crypto_authenc_genicv(areq, greq->giv, 0);
  189. }
  190. aead_request_complete(areq, err);
  191. }
  192. static int crypto_authenc_givencrypt(struct aead_givcrypt_request *req)
  193. {
  194. struct crypto_aead *authenc = aead_givcrypt_reqtfm(req);
  195. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  196. struct aead_request *areq = &req->areq;
  197. struct skcipher_givcrypt_request *greq = aead_request_ctx(areq);
  198. u8 *iv = req->giv;
  199. int err;
  200. skcipher_givcrypt_set_tfm(greq, ctx->enc);
  201. skcipher_givcrypt_set_callback(greq, aead_request_flags(areq),
  202. crypto_authenc_givencrypt_done, areq);
  203. skcipher_givcrypt_set_crypt(greq, areq->src, areq->dst, areq->cryptlen,
  204. areq->iv);
  205. skcipher_givcrypt_set_giv(greq, iv, req->seq);
  206. err = crypto_skcipher_givencrypt(greq);
  207. if (err)
  208. return err;
  209. return crypto_authenc_genicv(areq, iv, CRYPTO_TFM_REQ_MAY_SLEEP);
  210. }
  211. static int crypto_authenc_verify(struct aead_request *req,
  212. struct scatterlist *cipher,
  213. unsigned int cryptlen)
  214. {
  215. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  216. u8 *ohash;
  217. u8 *ihash;
  218. unsigned int authsize;
  219. ohash = crypto_authenc_hash(req, CRYPTO_TFM_REQ_MAY_SLEEP, cipher,
  220. cryptlen);
  221. if (IS_ERR(ohash))
  222. return PTR_ERR(ohash);
  223. authsize = crypto_aead_authsize(authenc);
  224. ihash = ohash + authsize;
  225. scatterwalk_map_and_copy(ihash, cipher, cryptlen, authsize, 0);
  226. return memcmp(ihash, ohash, authsize) ? -EBADMSG: 0;
  227. }
  228. static int crypto_authenc_iverify(struct aead_request *req, u8 *iv,
  229. unsigned int cryptlen)
  230. {
  231. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  232. struct scatterlist *src = req->src;
  233. struct scatterlist cipher[2];
  234. struct page *srcp;
  235. unsigned int ivsize = crypto_aead_ivsize(authenc);
  236. u8 *vsrc;
  237. srcp = sg_page(src);
  238. vsrc = PageHighMem(srcp) ? NULL : page_address(srcp) + src->offset;
  239. if (ivsize) {
  240. sg_init_table(cipher, 2);
  241. sg_set_buf(cipher, iv, ivsize);
  242. authenc_chain(cipher, src, vsrc == iv + ivsize);
  243. src = cipher;
  244. }
  245. return crypto_authenc_verify(req, src, cryptlen + ivsize);
  246. }
  247. static int crypto_authenc_decrypt(struct aead_request *req)
  248. {
  249. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  250. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  251. struct ablkcipher_request *abreq = aead_request_ctx(req);
  252. unsigned int cryptlen = req->cryptlen;
  253. unsigned int authsize = crypto_aead_authsize(authenc);
  254. u8 *iv = req->iv;
  255. int err;
  256. if (cryptlen < authsize)
  257. return -EINVAL;
  258. cryptlen -= authsize;
  259. err = crypto_authenc_iverify(req, iv, cryptlen);
  260. if (err)
  261. return err;
  262. ablkcipher_request_set_tfm(abreq, ctx->enc);
  263. ablkcipher_request_set_callback(abreq, aead_request_flags(req),
  264. req->base.complete, req->base.data);
  265. ablkcipher_request_set_crypt(abreq, req->src, req->dst, cryptlen, iv);
  266. return crypto_ablkcipher_decrypt(abreq);
  267. }
  268. static int crypto_authenc_init_tfm(struct crypto_tfm *tfm)
  269. {
  270. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  271. struct authenc_instance_ctx *ictx = crypto_instance_ctx(inst);
  272. struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm);
  273. struct crypto_hash *auth;
  274. struct crypto_ablkcipher *enc;
  275. int err;
  276. auth = crypto_spawn_hash(&ictx->auth);
  277. if (IS_ERR(auth))
  278. return PTR_ERR(auth);
  279. enc = crypto_spawn_skcipher(&ictx->enc);
  280. err = PTR_ERR(enc);
  281. if (IS_ERR(enc))
  282. goto err_free_hash;
  283. ctx->auth = auth;
  284. ctx->enc = enc;
  285. tfm->crt_aead.reqsize = max_t(unsigned int,
  286. (crypto_hash_alignmask(auth) &
  287. ~(crypto_tfm_ctx_alignment() - 1)) +
  288. crypto_hash_digestsize(auth) * 2,
  289. sizeof(struct skcipher_givcrypt_request) +
  290. crypto_ablkcipher_reqsize(enc) +
  291. crypto_ablkcipher_ivsize(enc));
  292. spin_lock_init(&ctx->auth_lock);
  293. return 0;
  294. err_free_hash:
  295. crypto_free_hash(auth);
  296. return err;
  297. }
  298. static void crypto_authenc_exit_tfm(struct crypto_tfm *tfm)
  299. {
  300. struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm);
  301. crypto_free_hash(ctx->auth);
  302. crypto_free_ablkcipher(ctx->enc);
  303. }
  304. static struct crypto_instance *crypto_authenc_alloc(struct rtattr **tb)
  305. {
  306. struct crypto_attr_type *algt;
  307. struct crypto_instance *inst;
  308. struct crypto_alg *auth;
  309. struct crypto_alg *enc;
  310. struct authenc_instance_ctx *ctx;
  311. const char *enc_name;
  312. int err;
  313. algt = crypto_get_attr_type(tb);
  314. err = PTR_ERR(algt);
  315. if (IS_ERR(algt))
  316. return ERR_PTR(err);
  317. if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
  318. return ERR_PTR(-EINVAL);
  319. auth = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH,
  320. CRYPTO_ALG_TYPE_HASH_MASK);
  321. if (IS_ERR(auth))
  322. return ERR_PTR(PTR_ERR(auth));
  323. enc_name = crypto_attr_alg_name(tb[2]);
  324. err = PTR_ERR(enc_name);
  325. if (IS_ERR(enc_name))
  326. goto out_put_auth;
  327. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  328. err = -ENOMEM;
  329. if (!inst)
  330. goto out_put_auth;
  331. ctx = crypto_instance_ctx(inst);
  332. err = crypto_init_spawn(&ctx->auth, auth, inst, CRYPTO_ALG_TYPE_MASK);
  333. if (err)
  334. goto err_free_inst;
  335. crypto_set_skcipher_spawn(&ctx->enc, inst);
  336. err = crypto_grab_skcipher(&ctx->enc, enc_name, 0,
  337. crypto_requires_sync(algt->type,
  338. algt->mask));
  339. if (err)
  340. goto err_drop_auth;
  341. enc = crypto_skcipher_spawn_alg(&ctx->enc);
  342. err = -ENAMETOOLONG;
  343. if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
  344. "authenc(%s,%s)", auth->cra_name, enc->cra_name) >=
  345. CRYPTO_MAX_ALG_NAME)
  346. goto err_drop_enc;
  347. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  348. "authenc(%s,%s)", auth->cra_driver_name,
  349. enc->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  350. goto err_drop_enc;
  351. inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD;
  352. inst->alg.cra_flags |= enc->cra_flags & CRYPTO_ALG_ASYNC;
  353. inst->alg.cra_priority = enc->cra_priority * 10 + auth->cra_priority;
  354. inst->alg.cra_blocksize = enc->cra_blocksize;
  355. inst->alg.cra_alignmask = auth->cra_alignmask | enc->cra_alignmask;
  356. inst->alg.cra_type = &crypto_aead_type;
  357. inst->alg.cra_aead.ivsize = enc->cra_ablkcipher.ivsize;
  358. inst->alg.cra_aead.maxauthsize = auth->cra_type == &crypto_hash_type ?
  359. auth->cra_hash.digestsize :
  360. auth->cra_type ?
  361. __crypto_shash_alg(auth)->digestsize :
  362. auth->cra_digest.dia_digestsize;
  363. inst->alg.cra_ctxsize = sizeof(struct crypto_authenc_ctx);
  364. inst->alg.cra_init = crypto_authenc_init_tfm;
  365. inst->alg.cra_exit = crypto_authenc_exit_tfm;
  366. inst->alg.cra_aead.setkey = crypto_authenc_setkey;
  367. inst->alg.cra_aead.encrypt = crypto_authenc_encrypt;
  368. inst->alg.cra_aead.decrypt = crypto_authenc_decrypt;
  369. inst->alg.cra_aead.givencrypt = crypto_authenc_givencrypt;
  370. out:
  371. crypto_mod_put(auth);
  372. return inst;
  373. err_drop_enc:
  374. crypto_drop_skcipher(&ctx->enc);
  375. err_drop_auth:
  376. crypto_drop_spawn(&ctx->auth);
  377. err_free_inst:
  378. kfree(inst);
  379. out_put_auth:
  380. inst = ERR_PTR(err);
  381. goto out;
  382. }
  383. static void crypto_authenc_free(struct crypto_instance *inst)
  384. {
  385. struct authenc_instance_ctx *ctx = crypto_instance_ctx(inst);
  386. crypto_drop_skcipher(&ctx->enc);
  387. crypto_drop_spawn(&ctx->auth);
  388. kfree(inst);
  389. }
  390. static struct crypto_template crypto_authenc_tmpl = {
  391. .name = "authenc",
  392. .alloc = crypto_authenc_alloc,
  393. .free = crypto_authenc_free,
  394. .module = THIS_MODULE,
  395. };
  396. static int __init crypto_authenc_module_init(void)
  397. {
  398. return crypto_register_template(&crypto_authenc_tmpl);
  399. }
  400. static void __exit crypto_authenc_module_exit(void)
  401. {
  402. crypto_unregister_template(&crypto_authenc_tmpl);
  403. }
  404. module_init(crypto_authenc_module_init);
  405. module_exit(crypto_authenc_module_exit);
  406. MODULE_LICENSE("GPL");
  407. MODULE_DESCRIPTION("Simple AEAD wrapper for IPsec");