authenc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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 int crypto_authenc_setkey(struct crypto_aead *authenc, const u8 *key,
  44. unsigned int keylen)
  45. {
  46. unsigned int authkeylen;
  47. unsigned int enckeylen;
  48. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  49. struct crypto_ahash *auth = ctx->auth;
  50. struct crypto_ablkcipher *enc = ctx->enc;
  51. struct rtattr *rta = (void *)key;
  52. struct crypto_authenc_key_param *param;
  53. int err = -EINVAL;
  54. if (!RTA_OK(rta, keylen))
  55. goto badkey;
  56. if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
  57. goto badkey;
  58. if (RTA_PAYLOAD(rta) < sizeof(*param))
  59. goto badkey;
  60. param = RTA_DATA(rta);
  61. enckeylen = be32_to_cpu(param->enckeylen);
  62. key += RTA_ALIGN(rta->rta_len);
  63. keylen -= RTA_ALIGN(rta->rta_len);
  64. if (keylen < enckeylen)
  65. goto badkey;
  66. authkeylen = keylen - enckeylen;
  67. crypto_ahash_clear_flags(auth, CRYPTO_TFM_REQ_MASK);
  68. crypto_ahash_set_flags(auth, crypto_aead_get_flags(authenc) &
  69. CRYPTO_TFM_REQ_MASK);
  70. err = crypto_ahash_setkey(auth, key, authkeylen);
  71. crypto_aead_set_flags(authenc, crypto_ahash_get_flags(auth) &
  72. CRYPTO_TFM_RES_MASK);
  73. if (err)
  74. goto out;
  75. crypto_ablkcipher_clear_flags(enc, CRYPTO_TFM_REQ_MASK);
  76. crypto_ablkcipher_set_flags(enc, crypto_aead_get_flags(authenc) &
  77. CRYPTO_TFM_REQ_MASK);
  78. err = crypto_ablkcipher_setkey(enc, key + authkeylen, enckeylen);
  79. crypto_aead_set_flags(authenc, crypto_ablkcipher_get_flags(enc) &
  80. CRYPTO_TFM_RES_MASK);
  81. out:
  82. return err;
  83. badkey:
  84. crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
  85. goto out;
  86. }
  87. static void authenc_chain(struct scatterlist *head, struct scatterlist *sg,
  88. int chain)
  89. {
  90. if (chain) {
  91. head->length += sg->length;
  92. sg = scatterwalk_sg_next(sg);
  93. }
  94. if (sg)
  95. scatterwalk_sg_chain(head, 2, sg);
  96. else
  97. sg_mark_end(head);
  98. }
  99. static void authenc_geniv_ahash_update_done(struct crypto_async_request *areq,
  100. int err)
  101. {
  102. struct aead_request *req = areq->data;
  103. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  104. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  105. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  106. struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
  107. if (err)
  108. goto out;
  109. ahash_request_set_crypt(ahreq, areq_ctx->sg, ahreq->result,
  110. areq_ctx->cryptlen);
  111. ahash_request_set_callback(ahreq, aead_request_flags(req) &
  112. CRYPTO_TFM_REQ_MAY_SLEEP,
  113. areq_ctx->complete, req);
  114. err = crypto_ahash_finup(ahreq);
  115. if (err)
  116. goto out;
  117. scatterwalk_map_and_copy(ahreq->result, areq_ctx->sg,
  118. areq_ctx->cryptlen,
  119. crypto_aead_authsize(authenc), 1);
  120. out:
  121. aead_request_complete(req, err);
  122. }
  123. static void authenc_geniv_ahash_done(struct crypto_async_request *areq, int err)
  124. {
  125. struct aead_request *req = areq->data;
  126. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  127. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  128. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  129. struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
  130. if (err)
  131. goto out;
  132. scatterwalk_map_and_copy(ahreq->result, areq_ctx->sg,
  133. areq_ctx->cryptlen,
  134. crypto_aead_authsize(authenc), 1);
  135. out:
  136. aead_request_complete(req, err);
  137. }
  138. static void authenc_verify_ahash_update_done(struct crypto_async_request *areq,
  139. int err)
  140. {
  141. u8 *ihash;
  142. unsigned int authsize;
  143. struct ablkcipher_request *abreq;
  144. struct aead_request *req = areq->data;
  145. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  146. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  147. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  148. struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
  149. if (err)
  150. goto out;
  151. ahash_request_set_crypt(ahreq, areq_ctx->sg, ahreq->result,
  152. areq_ctx->cryptlen);
  153. ahash_request_set_callback(ahreq, aead_request_flags(req) &
  154. CRYPTO_TFM_REQ_MAY_SLEEP,
  155. areq_ctx->complete, req);
  156. err = crypto_ahash_finup(ahreq);
  157. if (err)
  158. goto out;
  159. authsize = crypto_aead_authsize(authenc);
  160. ihash = ahreq->result + authsize;
  161. scatterwalk_map_and_copy(ihash, areq_ctx->sg, areq_ctx->cryptlen,
  162. authsize, 0);
  163. err = memcmp(ihash, ahreq->result, authsize) ? -EBADMSG: 0;
  164. if (err)
  165. goto out;
  166. abreq = aead_request_ctx(req);
  167. ablkcipher_request_set_tfm(abreq, ctx->enc);
  168. ablkcipher_request_set_callback(abreq, aead_request_flags(req),
  169. req->base.complete, req->base.data);
  170. ablkcipher_request_set_crypt(abreq, req->src, req->dst,
  171. req->cryptlen, req->iv);
  172. err = crypto_ablkcipher_decrypt(abreq);
  173. out:
  174. aead_request_complete(req, err);
  175. }
  176. static void authenc_verify_ahash_done(struct crypto_async_request *areq,
  177. int err)
  178. {
  179. u8 *ihash;
  180. unsigned int authsize;
  181. struct ablkcipher_request *abreq;
  182. struct aead_request *req = areq->data;
  183. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  184. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  185. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  186. struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
  187. if (err)
  188. goto out;
  189. authsize = crypto_aead_authsize(authenc);
  190. ihash = ahreq->result + authsize;
  191. scatterwalk_map_and_copy(ihash, areq_ctx->sg, areq_ctx->cryptlen,
  192. authsize, 0);
  193. err = memcmp(ihash, ahreq->result, authsize) ? -EBADMSG: 0;
  194. if (err)
  195. goto out;
  196. abreq = aead_request_ctx(req);
  197. ablkcipher_request_set_tfm(abreq, ctx->enc);
  198. ablkcipher_request_set_callback(abreq, aead_request_flags(req),
  199. req->base.complete, req->base.data);
  200. ablkcipher_request_set_crypt(abreq, req->src, req->dst,
  201. req->cryptlen, req->iv);
  202. err = crypto_ablkcipher_decrypt(abreq);
  203. out:
  204. aead_request_complete(req, err);
  205. }
  206. static u8 *crypto_authenc_ahash_fb(struct aead_request *req, unsigned int flags)
  207. {
  208. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  209. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  210. struct crypto_ahash *auth = ctx->auth;
  211. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  212. struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
  213. u8 *hash = areq_ctx->tail;
  214. int err;
  215. hash = (u8 *)ALIGN((unsigned long)hash + crypto_ahash_alignmask(auth),
  216. crypto_ahash_alignmask(auth) + 1);
  217. ahash_request_set_tfm(ahreq, auth);
  218. err = crypto_ahash_init(ahreq);
  219. if (err)
  220. return ERR_PTR(err);
  221. ahash_request_set_crypt(ahreq, req->assoc, hash, req->assoclen);
  222. ahash_request_set_callback(ahreq, aead_request_flags(req) & flags,
  223. areq_ctx->update_complete, req);
  224. err = crypto_ahash_update(ahreq);
  225. if (err)
  226. return ERR_PTR(err);
  227. ahash_request_set_crypt(ahreq, areq_ctx->sg, hash,
  228. areq_ctx->cryptlen);
  229. ahash_request_set_callback(ahreq, aead_request_flags(req) & flags,
  230. areq_ctx->complete, req);
  231. err = crypto_ahash_finup(ahreq);
  232. if (err)
  233. return ERR_PTR(err);
  234. return hash;
  235. }
  236. static u8 *crypto_authenc_ahash(struct aead_request *req, unsigned int flags)
  237. {
  238. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  239. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  240. struct crypto_ahash *auth = ctx->auth;
  241. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  242. struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
  243. u8 *hash = areq_ctx->tail;
  244. int err;
  245. hash = (u8 *)ALIGN((unsigned long)hash + crypto_ahash_alignmask(auth),
  246. crypto_ahash_alignmask(auth) + 1);
  247. ahash_request_set_tfm(ahreq, auth);
  248. ahash_request_set_crypt(ahreq, areq_ctx->sg, hash,
  249. areq_ctx->cryptlen);
  250. ahash_request_set_callback(ahreq, aead_request_flags(req) & flags,
  251. areq_ctx->complete, req);
  252. err = crypto_ahash_digest(ahreq);
  253. if (err)
  254. return ERR_PTR(err);
  255. return hash;
  256. }
  257. static int crypto_authenc_genicv(struct aead_request *req, u8 *iv,
  258. unsigned int flags)
  259. {
  260. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  261. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  262. struct scatterlist *dst = req->dst;
  263. struct scatterlist *assoc = req->assoc;
  264. struct scatterlist *cipher = areq_ctx->cipher;
  265. struct scatterlist *asg = areq_ctx->asg;
  266. unsigned int ivsize = crypto_aead_ivsize(authenc);
  267. unsigned int cryptlen = req->cryptlen;
  268. authenc_ahash_t authenc_ahash_fn = crypto_authenc_ahash_fb;
  269. struct page *dstp;
  270. u8 *vdst;
  271. u8 *hash;
  272. dstp = sg_page(dst);
  273. vdst = PageHighMem(dstp) ? NULL : page_address(dstp) + dst->offset;
  274. if (ivsize) {
  275. sg_init_table(cipher, 2);
  276. sg_set_buf(cipher, iv, ivsize);
  277. authenc_chain(cipher, dst, vdst == iv + ivsize);
  278. dst = cipher;
  279. cryptlen += ivsize;
  280. }
  281. if (sg_is_last(assoc)) {
  282. authenc_ahash_fn = crypto_authenc_ahash;
  283. sg_init_table(asg, 2);
  284. sg_set_page(asg, sg_page(assoc), assoc->length, assoc->offset);
  285. authenc_chain(asg, dst, 0);
  286. dst = asg;
  287. cryptlen += req->assoclen;
  288. }
  289. areq_ctx->cryptlen = cryptlen;
  290. areq_ctx->sg = dst;
  291. areq_ctx->complete = authenc_geniv_ahash_done;
  292. areq_ctx->update_complete = authenc_geniv_ahash_update_done;
  293. hash = authenc_ahash_fn(req, flags);
  294. if (IS_ERR(hash))
  295. return PTR_ERR(hash);
  296. scatterwalk_map_and_copy(hash, dst, cryptlen,
  297. crypto_aead_authsize(authenc), 1);
  298. return 0;
  299. }
  300. static void crypto_authenc_encrypt_done(struct crypto_async_request *req,
  301. int err)
  302. {
  303. struct aead_request *areq = req->data;
  304. if (!err) {
  305. struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
  306. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  307. struct ablkcipher_request *abreq = aead_request_ctx(areq);
  308. u8 *iv = (u8 *)(abreq + 1) +
  309. crypto_ablkcipher_reqsize(ctx->enc);
  310. err = crypto_authenc_genicv(areq, iv, 0);
  311. }
  312. aead_request_complete(areq, err);
  313. }
  314. static int crypto_authenc_encrypt(struct aead_request *req)
  315. {
  316. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  317. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  318. struct ablkcipher_request *abreq = aead_request_ctx(req);
  319. struct crypto_ablkcipher *enc = ctx->enc;
  320. struct scatterlist *dst = req->dst;
  321. unsigned int cryptlen = req->cryptlen;
  322. u8 *iv = (u8 *)(abreq + 1) + crypto_ablkcipher_reqsize(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. aead_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->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 memcmp(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. authenc_chain(cipher, src, vsrc == iv + ivsize);
  401. src = cipher;
  402. cryptlen += ivsize;
  403. }
  404. if (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. authenc_chain(asg, src, 0);
  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. ctx->reqoff = ALIGN(2 * crypto_ahash_digestsize(auth) +
  449. crypto_ahash_alignmask(auth),
  450. crypto_ahash_alignmask(auth) + 1);
  451. enc = crypto_spawn_skcipher(&ictx->enc);
  452. err = PTR_ERR(enc);
  453. if (IS_ERR(enc))
  454. goto err_free_ahash;
  455. ctx->auth = auth;
  456. ctx->enc = enc;
  457. tfm->crt_aead.reqsize = max_t(unsigned int,
  458. crypto_ahash_reqsize(auth) + ctx->reqoff +
  459. sizeof(struct authenc_request_ctx) +
  460. sizeof(struct ahash_request),
  461. sizeof(struct skcipher_givcrypt_request) +
  462. crypto_ablkcipher_reqsize(enc) +
  463. crypto_ablkcipher_ivsize(enc));
  464. return 0;
  465. err_free_ahash:
  466. crypto_free_ahash(auth);
  467. return err;
  468. }
  469. static void crypto_authenc_exit_tfm(struct crypto_tfm *tfm)
  470. {
  471. struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm);
  472. crypto_free_ahash(ctx->auth);
  473. crypto_free_ablkcipher(ctx->enc);
  474. }
  475. static struct crypto_instance *crypto_authenc_alloc(struct rtattr **tb)
  476. {
  477. struct crypto_attr_type *algt;
  478. struct crypto_instance *inst;
  479. struct hash_alg_common *auth;
  480. struct crypto_alg *auth_base;
  481. struct crypto_alg *enc;
  482. struct authenc_instance_ctx *ctx;
  483. const char *enc_name;
  484. int err;
  485. algt = crypto_get_attr_type(tb);
  486. err = PTR_ERR(algt);
  487. if (IS_ERR(algt))
  488. return ERR_PTR(err);
  489. if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
  490. return ERR_PTR(-EINVAL);
  491. auth = ahash_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH,
  492. CRYPTO_ALG_TYPE_AHASH_MASK);
  493. if (IS_ERR(auth))
  494. return ERR_PTR(PTR_ERR(auth));
  495. auth_base = &auth->base;
  496. enc_name = crypto_attr_alg_name(tb[2]);
  497. err = PTR_ERR(enc_name);
  498. if (IS_ERR(enc_name))
  499. goto out_put_auth;
  500. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  501. err = -ENOMEM;
  502. if (!inst)
  503. goto out_put_auth;
  504. ctx = crypto_instance_ctx(inst);
  505. err = crypto_init_ahash_spawn(&ctx->auth, auth, inst);
  506. if (err)
  507. goto err_free_inst;
  508. crypto_set_skcipher_spawn(&ctx->enc, inst);
  509. err = crypto_grab_skcipher(&ctx->enc, enc_name, 0,
  510. crypto_requires_sync(algt->type,
  511. algt->mask));
  512. if (err)
  513. goto err_drop_auth;
  514. enc = crypto_skcipher_spawn_alg(&ctx->enc);
  515. err = -ENAMETOOLONG;
  516. if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
  517. "authenc(%s,%s)", auth_base->cra_name, enc->cra_name) >=
  518. CRYPTO_MAX_ALG_NAME)
  519. goto err_drop_enc;
  520. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  521. "authenc(%s,%s)", auth_base->cra_driver_name,
  522. enc->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  523. goto err_drop_enc;
  524. inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD;
  525. inst->alg.cra_flags |= enc->cra_flags & CRYPTO_ALG_ASYNC;
  526. inst->alg.cra_priority = enc->cra_priority *
  527. 10 + auth_base->cra_priority;
  528. inst->alg.cra_blocksize = enc->cra_blocksize;
  529. inst->alg.cra_alignmask = auth_base->cra_alignmask | enc->cra_alignmask;
  530. inst->alg.cra_type = &crypto_aead_type;
  531. inst->alg.cra_aead.ivsize = enc->cra_ablkcipher.ivsize;
  532. inst->alg.cra_aead.maxauthsize = auth->digestsize;
  533. inst->alg.cra_ctxsize = sizeof(struct crypto_authenc_ctx);
  534. inst->alg.cra_init = crypto_authenc_init_tfm;
  535. inst->alg.cra_exit = crypto_authenc_exit_tfm;
  536. inst->alg.cra_aead.setkey = crypto_authenc_setkey;
  537. inst->alg.cra_aead.encrypt = crypto_authenc_encrypt;
  538. inst->alg.cra_aead.decrypt = crypto_authenc_decrypt;
  539. inst->alg.cra_aead.givencrypt = crypto_authenc_givencrypt;
  540. out:
  541. crypto_mod_put(auth_base);
  542. return inst;
  543. err_drop_enc:
  544. crypto_drop_skcipher(&ctx->enc);
  545. err_drop_auth:
  546. crypto_drop_ahash(&ctx->auth);
  547. err_free_inst:
  548. kfree(inst);
  549. out_put_auth:
  550. inst = ERR_PTR(err);
  551. goto out;
  552. }
  553. static void crypto_authenc_free(struct crypto_instance *inst)
  554. {
  555. struct authenc_instance_ctx *ctx = crypto_instance_ctx(inst);
  556. crypto_drop_skcipher(&ctx->enc);
  557. crypto_drop_ahash(&ctx->auth);
  558. kfree(inst);
  559. }
  560. static struct crypto_template crypto_authenc_tmpl = {
  561. .name = "authenc",
  562. .alloc = crypto_authenc_alloc,
  563. .free = crypto_authenc_free,
  564. .module = THIS_MODULE,
  565. };
  566. static int __init crypto_authenc_module_init(void)
  567. {
  568. return crypto_register_template(&crypto_authenc_tmpl);
  569. }
  570. static void __exit crypto_authenc_module_exit(void)
  571. {
  572. crypto_unregister_template(&crypto_authenc_tmpl);
  573. }
  574. module_init(crypto_authenc_module_init);
  575. module_exit(crypto_authenc_module_exit);
  576. MODULE_LICENSE("GPL");
  577. MODULE_DESCRIPTION("Simple AEAD wrapper for IPsec");