authenc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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. typedef u8 *(*authenc_ahash_t)(struct aead_request *req, unsigned int flags);
  25. struct authenc_instance_ctx {
  26. struct crypto_ahash_spawn auth;
  27. struct crypto_skcipher_spawn enc;
  28. };
  29. struct crypto_authenc_ctx {
  30. unsigned int reqoff;
  31. struct crypto_ahash *auth;
  32. struct crypto_ablkcipher *enc;
  33. };
  34. struct authenc_request_ctx {
  35. unsigned int cryptlen;
  36. struct scatterlist *sg;
  37. struct scatterlist asg[2];
  38. struct scatterlist cipher[2];
  39. crypto_completion_t complete;
  40. crypto_completion_t update_complete;
  41. char tail[];
  42. };
  43. static void authenc_request_complete(struct aead_request *req, int err)
  44. {
  45. if (err != -EINPROGRESS)
  46. aead_request_complete(req, err);
  47. }
  48. static int crypto_authenc_setkey(struct crypto_aead *authenc, const u8 *key,
  49. unsigned int keylen)
  50. {
  51. unsigned int authkeylen;
  52. unsigned int enckeylen;
  53. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  54. struct crypto_ahash *auth = ctx->auth;
  55. struct crypto_ablkcipher *enc = ctx->enc;
  56. struct rtattr *rta = (void *)key;
  57. struct crypto_authenc_key_param *param;
  58. int err = -EINVAL;
  59. if (!RTA_OK(rta, keylen))
  60. goto badkey;
  61. if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
  62. goto badkey;
  63. if (RTA_PAYLOAD(rta) < sizeof(*param))
  64. goto badkey;
  65. param = RTA_DATA(rta);
  66. enckeylen = be32_to_cpu(param->enckeylen);
  67. key += RTA_ALIGN(rta->rta_len);
  68. keylen -= RTA_ALIGN(rta->rta_len);
  69. if (keylen < enckeylen)
  70. goto badkey;
  71. authkeylen = keylen - enckeylen;
  72. crypto_ahash_clear_flags(auth, CRYPTO_TFM_REQ_MASK);
  73. crypto_ahash_set_flags(auth, crypto_aead_get_flags(authenc) &
  74. CRYPTO_TFM_REQ_MASK);
  75. err = crypto_ahash_setkey(auth, key, authkeylen);
  76. crypto_aead_set_flags(authenc, crypto_ahash_get_flags(auth) &
  77. CRYPTO_TFM_RES_MASK);
  78. if (err)
  79. goto out;
  80. crypto_ablkcipher_clear_flags(enc, CRYPTO_TFM_REQ_MASK);
  81. crypto_ablkcipher_set_flags(enc, crypto_aead_get_flags(authenc) &
  82. CRYPTO_TFM_REQ_MASK);
  83. err = crypto_ablkcipher_setkey(enc, key + authkeylen, enckeylen);
  84. crypto_aead_set_flags(authenc, crypto_ablkcipher_get_flags(enc) &
  85. CRYPTO_TFM_RES_MASK);
  86. out:
  87. return err;
  88. badkey:
  89. crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
  90. goto out;
  91. }
  92. static void authenc_geniv_ahash_update_done(struct crypto_async_request *areq,
  93. int err)
  94. {
  95. struct aead_request *req = areq->data;
  96. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  97. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  98. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  99. struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
  100. if (err)
  101. goto out;
  102. ahash_request_set_crypt(ahreq, areq_ctx->sg, ahreq->result,
  103. areq_ctx->cryptlen);
  104. ahash_request_set_callback(ahreq, aead_request_flags(req) &
  105. CRYPTO_TFM_REQ_MAY_SLEEP,
  106. areq_ctx->complete, req);
  107. err = crypto_ahash_finup(ahreq);
  108. if (err)
  109. goto out;
  110. scatterwalk_map_and_copy(ahreq->result, areq_ctx->sg,
  111. areq_ctx->cryptlen,
  112. crypto_aead_authsize(authenc), 1);
  113. out:
  114. authenc_request_complete(req, err);
  115. }
  116. static void authenc_geniv_ahash_done(struct crypto_async_request *areq, int err)
  117. {
  118. struct aead_request *req = areq->data;
  119. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  120. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  121. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  122. struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
  123. if (err)
  124. goto out;
  125. scatterwalk_map_and_copy(ahreq->result, areq_ctx->sg,
  126. areq_ctx->cryptlen,
  127. crypto_aead_authsize(authenc), 1);
  128. out:
  129. aead_request_complete(req, err);
  130. }
  131. static void authenc_verify_ahash_update_done(struct crypto_async_request *areq,
  132. int err)
  133. {
  134. u8 *ihash;
  135. unsigned int authsize;
  136. struct ablkcipher_request *abreq;
  137. struct aead_request *req = areq->data;
  138. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  139. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  140. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  141. struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
  142. unsigned int cryptlen = req->cryptlen;
  143. if (err)
  144. goto out;
  145. ahash_request_set_crypt(ahreq, areq_ctx->sg, ahreq->result,
  146. areq_ctx->cryptlen);
  147. ahash_request_set_callback(ahreq, aead_request_flags(req) &
  148. CRYPTO_TFM_REQ_MAY_SLEEP,
  149. areq_ctx->complete, req);
  150. err = crypto_ahash_finup(ahreq);
  151. if (err)
  152. goto out;
  153. authsize = crypto_aead_authsize(authenc);
  154. cryptlen -= authsize;
  155. ihash = ahreq->result + authsize;
  156. scatterwalk_map_and_copy(ihash, areq_ctx->sg, areq_ctx->cryptlen,
  157. authsize, 0);
  158. err = crypto_memneq(ihash, ahreq->result, authsize) ? -EBADMSG : 0;
  159. if (err)
  160. goto out;
  161. abreq = aead_request_ctx(req);
  162. ablkcipher_request_set_tfm(abreq, ctx->enc);
  163. ablkcipher_request_set_callback(abreq, aead_request_flags(req),
  164. req->base.complete, req->base.data);
  165. ablkcipher_request_set_crypt(abreq, req->src, req->dst,
  166. cryptlen, req->iv);
  167. err = crypto_ablkcipher_decrypt(abreq);
  168. out:
  169. authenc_request_complete(req, err);
  170. }
  171. static void authenc_verify_ahash_done(struct crypto_async_request *areq,
  172. int err)
  173. {
  174. u8 *ihash;
  175. unsigned int authsize;
  176. struct ablkcipher_request *abreq;
  177. struct aead_request *req = areq->data;
  178. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  179. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  180. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  181. struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
  182. unsigned int cryptlen = req->cryptlen;
  183. if (err)
  184. goto out;
  185. authsize = crypto_aead_authsize(authenc);
  186. cryptlen -= authsize;
  187. ihash = ahreq->result + authsize;
  188. scatterwalk_map_and_copy(ihash, areq_ctx->sg, areq_ctx->cryptlen,
  189. authsize, 0);
  190. err = crypto_memneq(ihash, ahreq->result, authsize) ? -EBADMSG : 0;
  191. if (err)
  192. goto out;
  193. abreq = aead_request_ctx(req);
  194. ablkcipher_request_set_tfm(abreq, ctx->enc);
  195. ablkcipher_request_set_callback(abreq, aead_request_flags(req),
  196. req->base.complete, req->base.data);
  197. ablkcipher_request_set_crypt(abreq, req->src, req->dst,
  198. cryptlen, req->iv);
  199. err = crypto_ablkcipher_decrypt(abreq);
  200. out:
  201. authenc_request_complete(req, err);
  202. }
  203. static u8 *crypto_authenc_ahash_fb(struct aead_request *req, unsigned int flags)
  204. {
  205. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  206. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  207. struct crypto_ahash *auth = ctx->auth;
  208. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  209. struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
  210. u8 *hash = areq_ctx->tail;
  211. int err;
  212. hash = (u8 *)ALIGN((unsigned long)hash + crypto_ahash_alignmask(auth),
  213. crypto_ahash_alignmask(auth) + 1);
  214. ahash_request_set_tfm(ahreq, auth);
  215. err = crypto_ahash_init(ahreq);
  216. if (err)
  217. return ERR_PTR(err);
  218. ahash_request_set_crypt(ahreq, req->assoc, hash, req->assoclen);
  219. ahash_request_set_callback(ahreq, aead_request_flags(req) & flags,
  220. areq_ctx->update_complete, req);
  221. err = crypto_ahash_update(ahreq);
  222. if (err)
  223. return ERR_PTR(err);
  224. ahash_request_set_crypt(ahreq, areq_ctx->sg, hash,
  225. areq_ctx->cryptlen);
  226. ahash_request_set_callback(ahreq, aead_request_flags(req) & flags,
  227. areq_ctx->complete, req);
  228. err = crypto_ahash_finup(ahreq);
  229. if (err)
  230. return ERR_PTR(err);
  231. return hash;
  232. }
  233. static u8 *crypto_authenc_ahash(struct aead_request *req, unsigned int flags)
  234. {
  235. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  236. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  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 + ctx->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, areq_ctx->sg, hash,
  246. areq_ctx->cryptlen);
  247. ahash_request_set_callback(ahreq, aead_request_flags(req) & flags,
  248. areq_ctx->complete, req);
  249. err = crypto_ahash_digest(ahreq);
  250. if (err)
  251. return ERR_PTR(err);
  252. return hash;
  253. }
  254. static int crypto_authenc_genicv(struct aead_request *req, u8 *iv,
  255. unsigned int flags)
  256. {
  257. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  258. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  259. struct scatterlist *dst = req->dst;
  260. struct scatterlist *assoc = req->assoc;
  261. struct scatterlist *cipher = areq_ctx->cipher;
  262. struct scatterlist *asg = areq_ctx->asg;
  263. unsigned int ivsize = crypto_aead_ivsize(authenc);
  264. unsigned int cryptlen = req->cryptlen;
  265. authenc_ahash_t authenc_ahash_fn = crypto_authenc_ahash_fb;
  266. struct page *dstp;
  267. u8 *vdst;
  268. u8 *hash;
  269. dstp = sg_page(dst);
  270. vdst = PageHighMem(dstp) ? NULL : page_address(dstp) + dst->offset;
  271. if (ivsize) {
  272. sg_init_table(cipher, 2);
  273. sg_set_buf(cipher, iv, ivsize);
  274. scatterwalk_crypto_chain(cipher, dst, vdst == iv + ivsize, 2);
  275. dst = cipher;
  276. cryptlen += ivsize;
  277. }
  278. if (req->assoclen && sg_is_last(assoc)) {
  279. authenc_ahash_fn = crypto_authenc_ahash;
  280. sg_init_table(asg, 2);
  281. sg_set_page(asg, sg_page(assoc), assoc->length, assoc->offset);
  282. scatterwalk_crypto_chain(asg, dst, 0, 2);
  283. dst = asg;
  284. cryptlen += req->assoclen;
  285. }
  286. areq_ctx->cryptlen = cryptlen;
  287. areq_ctx->sg = dst;
  288. areq_ctx->complete = authenc_geniv_ahash_done;
  289. areq_ctx->update_complete = authenc_geniv_ahash_update_done;
  290. hash = authenc_ahash_fn(req, flags);
  291. if (IS_ERR(hash))
  292. return PTR_ERR(hash);
  293. scatterwalk_map_and_copy(hash, dst, cryptlen,
  294. crypto_aead_authsize(authenc), 1);
  295. return 0;
  296. }
  297. static void crypto_authenc_encrypt_done(struct crypto_async_request *req,
  298. int err)
  299. {
  300. struct aead_request *areq = req->data;
  301. if (!err) {
  302. struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
  303. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  304. struct authenc_request_ctx *areq_ctx = aead_request_ctx(areq);
  305. struct ablkcipher_request *abreq = (void *)(areq_ctx->tail
  306. + ctx->reqoff);
  307. u8 *iv = (u8 *)abreq - crypto_ablkcipher_ivsize(ctx->enc);
  308. err = crypto_authenc_genicv(areq, iv, 0);
  309. }
  310. authenc_request_complete(areq, err);
  311. }
  312. static int crypto_authenc_encrypt(struct aead_request *req)
  313. {
  314. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  315. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  316. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  317. struct crypto_ablkcipher *enc = ctx->enc;
  318. struct scatterlist *dst = req->dst;
  319. unsigned int cryptlen = req->cryptlen;
  320. struct ablkcipher_request *abreq = (void *)(areq_ctx->tail
  321. + ctx->reqoff);
  322. u8 *iv = (u8 *)abreq - crypto_ablkcipher_ivsize(enc);
  323. int err;
  324. ablkcipher_request_set_tfm(abreq, enc);
  325. ablkcipher_request_set_callback(abreq, aead_request_flags(req),
  326. crypto_authenc_encrypt_done, req);
  327. ablkcipher_request_set_crypt(abreq, req->src, dst, cryptlen, req->iv);
  328. memcpy(iv, req->iv, crypto_aead_ivsize(authenc));
  329. err = crypto_ablkcipher_encrypt(abreq);
  330. if (err)
  331. return err;
  332. return crypto_authenc_genicv(req, iv, CRYPTO_TFM_REQ_MAY_SLEEP);
  333. }
  334. static void crypto_authenc_givencrypt_done(struct crypto_async_request *req,
  335. int err)
  336. {
  337. struct aead_request *areq = req->data;
  338. if (!err) {
  339. struct skcipher_givcrypt_request *greq = aead_request_ctx(areq);
  340. err = crypto_authenc_genicv(areq, greq->giv, 0);
  341. }
  342. authenc_request_complete(areq, err);
  343. }
  344. static int crypto_authenc_givencrypt(struct aead_givcrypt_request *req)
  345. {
  346. struct crypto_aead *authenc = aead_givcrypt_reqtfm(req);
  347. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  348. struct aead_request *areq = &req->areq;
  349. struct skcipher_givcrypt_request *greq = aead_request_ctx(areq);
  350. u8 *iv = req->giv;
  351. int err;
  352. skcipher_givcrypt_set_tfm(greq, ctx->enc);
  353. skcipher_givcrypt_set_callback(greq, aead_request_flags(areq),
  354. crypto_authenc_givencrypt_done, areq);
  355. skcipher_givcrypt_set_crypt(greq, areq->src, areq->dst, areq->cryptlen,
  356. areq->iv);
  357. skcipher_givcrypt_set_giv(greq, iv, req->seq);
  358. err = crypto_skcipher_givencrypt(greq);
  359. if (err)
  360. return err;
  361. return crypto_authenc_genicv(areq, iv, CRYPTO_TFM_REQ_MAY_SLEEP);
  362. }
  363. static int crypto_authenc_verify(struct aead_request *req,
  364. authenc_ahash_t authenc_ahash_fn)
  365. {
  366. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  367. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  368. u8 *ohash;
  369. u8 *ihash;
  370. unsigned int authsize;
  371. areq_ctx->complete = authenc_verify_ahash_done;
  372. areq_ctx->update_complete = authenc_verify_ahash_update_done;
  373. ohash = authenc_ahash_fn(req, CRYPTO_TFM_REQ_MAY_SLEEP);
  374. if (IS_ERR(ohash))
  375. return PTR_ERR(ohash);
  376. authsize = crypto_aead_authsize(authenc);
  377. ihash = ohash + authsize;
  378. scatterwalk_map_and_copy(ihash, areq_ctx->sg, areq_ctx->cryptlen,
  379. authsize, 0);
  380. return crypto_memneq(ihash, ohash, authsize) ? -EBADMSG : 0;
  381. }
  382. static int crypto_authenc_iverify(struct aead_request *req, u8 *iv,
  383. unsigned int cryptlen)
  384. {
  385. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  386. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  387. struct scatterlist *src = req->src;
  388. struct scatterlist *assoc = req->assoc;
  389. struct scatterlist *cipher = areq_ctx->cipher;
  390. struct scatterlist *asg = areq_ctx->asg;
  391. unsigned int ivsize = crypto_aead_ivsize(authenc);
  392. authenc_ahash_t authenc_ahash_fn = crypto_authenc_ahash_fb;
  393. struct page *srcp;
  394. u8 *vsrc;
  395. srcp = sg_page(src);
  396. vsrc = PageHighMem(srcp) ? NULL : page_address(srcp) + src->offset;
  397. if (ivsize) {
  398. sg_init_table(cipher, 2);
  399. sg_set_buf(cipher, iv, ivsize);
  400. scatterwalk_crypto_chain(cipher, src, vsrc == iv + ivsize, 2);
  401. src = cipher;
  402. cryptlen += ivsize;
  403. }
  404. if (req->assoclen && sg_is_last(assoc)) {
  405. authenc_ahash_fn = crypto_authenc_ahash;
  406. sg_init_table(asg, 2);
  407. sg_set_page(asg, sg_page(assoc), assoc->length, assoc->offset);
  408. scatterwalk_crypto_chain(asg, src, 0, 2);
  409. src = asg;
  410. cryptlen += req->assoclen;
  411. }
  412. areq_ctx->cryptlen = cryptlen;
  413. areq_ctx->sg = src;
  414. return crypto_authenc_verify(req, authenc_ahash_fn);
  415. }
  416. static int crypto_authenc_decrypt(struct aead_request *req)
  417. {
  418. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  419. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  420. struct ablkcipher_request *abreq = aead_request_ctx(req);
  421. unsigned int cryptlen = req->cryptlen;
  422. unsigned int authsize = crypto_aead_authsize(authenc);
  423. u8 *iv = req->iv;
  424. int err;
  425. if (cryptlen < authsize)
  426. return -EINVAL;
  427. cryptlen -= authsize;
  428. err = crypto_authenc_iverify(req, iv, cryptlen);
  429. if (err)
  430. return err;
  431. ablkcipher_request_set_tfm(abreq, ctx->enc);
  432. ablkcipher_request_set_callback(abreq, aead_request_flags(req),
  433. req->base.complete, req->base.data);
  434. ablkcipher_request_set_crypt(abreq, req->src, req->dst, cryptlen, iv);
  435. return crypto_ablkcipher_decrypt(abreq);
  436. }
  437. static int crypto_authenc_init_tfm(struct crypto_tfm *tfm)
  438. {
  439. struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
  440. struct authenc_instance_ctx *ictx = crypto_instance_ctx(inst);
  441. struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm);
  442. struct crypto_ahash *auth;
  443. struct crypto_ablkcipher *enc;
  444. int err;
  445. auth = crypto_spawn_ahash(&ictx->auth);
  446. if (IS_ERR(auth))
  447. return PTR_ERR(auth);
  448. enc = crypto_spawn_skcipher(&ictx->enc);
  449. err = PTR_ERR(enc);
  450. if (IS_ERR(enc))
  451. goto err_free_ahash;
  452. ctx->auth = auth;
  453. ctx->enc = enc;
  454. ctx->reqoff = ALIGN(2 * crypto_ahash_digestsize(auth) +
  455. crypto_ahash_alignmask(auth),
  456. crypto_ahash_alignmask(auth) + 1) +
  457. crypto_ablkcipher_ivsize(enc);
  458. tfm->crt_aead.reqsize = sizeof(struct authenc_request_ctx) +
  459. ctx->reqoff +
  460. max_t(unsigned int,
  461. crypto_ahash_reqsize(auth) +
  462. sizeof(struct ahash_request),
  463. sizeof(struct skcipher_givcrypt_request) +
  464. crypto_ablkcipher_reqsize(enc));
  465. return 0;
  466. err_free_ahash:
  467. crypto_free_ahash(auth);
  468. return err;
  469. }
  470. static void crypto_authenc_exit_tfm(struct crypto_tfm *tfm)
  471. {
  472. struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm);
  473. crypto_free_ahash(ctx->auth);
  474. crypto_free_ablkcipher(ctx->enc);
  475. }
  476. static struct crypto_instance *crypto_authenc_alloc(struct rtattr **tb)
  477. {
  478. struct crypto_attr_type *algt;
  479. struct crypto_instance *inst;
  480. struct hash_alg_common *auth;
  481. struct crypto_alg *auth_base;
  482. struct crypto_alg *enc;
  483. struct authenc_instance_ctx *ctx;
  484. const char *enc_name;
  485. int err;
  486. algt = crypto_get_attr_type(tb);
  487. err = PTR_ERR(algt);
  488. if (IS_ERR(algt))
  489. return ERR_PTR(err);
  490. if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
  491. return ERR_PTR(-EINVAL);
  492. auth = ahash_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH,
  493. CRYPTO_ALG_TYPE_AHASH_MASK);
  494. if (IS_ERR(auth))
  495. return ERR_CAST(auth);
  496. auth_base = &auth->base;
  497. enc_name = crypto_attr_alg_name(tb[2]);
  498. err = PTR_ERR(enc_name);
  499. if (IS_ERR(enc_name))
  500. goto out_put_auth;
  501. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  502. err = -ENOMEM;
  503. if (!inst)
  504. goto out_put_auth;
  505. ctx = crypto_instance_ctx(inst);
  506. err = crypto_init_ahash_spawn(&ctx->auth, auth, inst);
  507. if (err)
  508. goto err_free_inst;
  509. crypto_set_skcipher_spawn(&ctx->enc, inst);
  510. err = crypto_grab_skcipher(&ctx->enc, enc_name, 0,
  511. crypto_requires_sync(algt->type,
  512. algt->mask));
  513. if (err)
  514. goto err_drop_auth;
  515. enc = crypto_skcipher_spawn_alg(&ctx->enc);
  516. err = -ENAMETOOLONG;
  517. if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
  518. "authenc(%s,%s)", auth_base->cra_name, enc->cra_name) >=
  519. CRYPTO_MAX_ALG_NAME)
  520. goto err_drop_enc;
  521. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  522. "authenc(%s,%s)", auth_base->cra_driver_name,
  523. enc->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  524. goto err_drop_enc;
  525. inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD;
  526. inst->alg.cra_flags |= enc->cra_flags & CRYPTO_ALG_ASYNC;
  527. inst->alg.cra_priority = enc->cra_priority *
  528. 10 + auth_base->cra_priority;
  529. inst->alg.cra_blocksize = enc->cra_blocksize;
  530. inst->alg.cra_alignmask = auth_base->cra_alignmask | enc->cra_alignmask;
  531. inst->alg.cra_type = &crypto_aead_type;
  532. inst->alg.cra_aead.ivsize = enc->cra_ablkcipher.ivsize;
  533. inst->alg.cra_aead.maxauthsize = auth->digestsize;
  534. inst->alg.cra_ctxsize = sizeof(struct crypto_authenc_ctx);
  535. inst->alg.cra_init = crypto_authenc_init_tfm;
  536. inst->alg.cra_exit = crypto_authenc_exit_tfm;
  537. inst->alg.cra_aead.setkey = crypto_authenc_setkey;
  538. inst->alg.cra_aead.encrypt = crypto_authenc_encrypt;
  539. inst->alg.cra_aead.decrypt = crypto_authenc_decrypt;
  540. inst->alg.cra_aead.givencrypt = crypto_authenc_givencrypt;
  541. out:
  542. crypto_mod_put(auth_base);
  543. return inst;
  544. err_drop_enc:
  545. crypto_drop_skcipher(&ctx->enc);
  546. err_drop_auth:
  547. crypto_drop_ahash(&ctx->auth);
  548. err_free_inst:
  549. kfree(inst);
  550. out_put_auth:
  551. inst = ERR_PTR(err);
  552. goto out;
  553. }
  554. static void crypto_authenc_free(struct crypto_instance *inst)
  555. {
  556. struct authenc_instance_ctx *ctx = crypto_instance_ctx(inst);
  557. crypto_drop_skcipher(&ctx->enc);
  558. crypto_drop_ahash(&ctx->auth);
  559. kfree(inst);
  560. }
  561. static struct crypto_template crypto_authenc_tmpl = {
  562. .name = "authenc",
  563. .alloc = crypto_authenc_alloc,
  564. .free = crypto_authenc_free,
  565. .module = THIS_MODULE,
  566. };
  567. static int __init crypto_authenc_module_init(void)
  568. {
  569. return crypto_register_template(&crypto_authenc_tmpl);
  570. }
  571. static void __exit crypto_authenc_module_exit(void)
  572. {
  573. crypto_unregister_template(&crypto_authenc_tmpl);
  574. }
  575. module_init(crypto_authenc_module_init);
  576. module_exit(crypto_authenc_module_exit);
  577. MODULE_LICENSE("GPL");
  578. MODULE_DESCRIPTION("Simple AEAD wrapper for IPsec");