authenc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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 authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  319. struct crypto_ablkcipher *enc = ctx->enc;
  320. struct scatterlist *dst = req->dst;
  321. unsigned int cryptlen = req->cryptlen;
  322. struct ablkcipher_request *abreq = (void *)(areq_ctx->tail
  323. + ctx->reqoff);
  324. u8 *iv = (u8 *)abreq - crypto_ablkcipher_ivsize(enc);
  325. int err;
  326. ablkcipher_request_set_tfm(abreq, enc);
  327. ablkcipher_request_set_callback(abreq, aead_request_flags(req),
  328. crypto_authenc_encrypt_done, req);
  329. ablkcipher_request_set_crypt(abreq, req->src, dst, cryptlen, req->iv);
  330. memcpy(iv, req->iv, crypto_aead_ivsize(authenc));
  331. err = crypto_ablkcipher_encrypt(abreq);
  332. if (err)
  333. return err;
  334. return crypto_authenc_genicv(req, iv, CRYPTO_TFM_REQ_MAY_SLEEP);
  335. }
  336. static void crypto_authenc_givencrypt_done(struct crypto_async_request *req,
  337. int err)
  338. {
  339. struct aead_request *areq = req->data;
  340. if (!err) {
  341. struct skcipher_givcrypt_request *greq = aead_request_ctx(areq);
  342. err = crypto_authenc_genicv(areq, greq->giv, 0);
  343. }
  344. aead_request_complete(areq, err);
  345. }
  346. static int crypto_authenc_givencrypt(struct aead_givcrypt_request *req)
  347. {
  348. struct crypto_aead *authenc = aead_givcrypt_reqtfm(req);
  349. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  350. struct aead_request *areq = &req->areq;
  351. struct skcipher_givcrypt_request *greq = aead_request_ctx(areq);
  352. u8 *iv = req->giv;
  353. int err;
  354. skcipher_givcrypt_set_tfm(greq, ctx->enc);
  355. skcipher_givcrypt_set_callback(greq, aead_request_flags(areq),
  356. crypto_authenc_givencrypt_done, areq);
  357. skcipher_givcrypt_set_crypt(greq, areq->src, areq->dst, areq->cryptlen,
  358. areq->iv);
  359. skcipher_givcrypt_set_giv(greq, iv, req->seq);
  360. err = crypto_skcipher_givencrypt(greq);
  361. if (err)
  362. return err;
  363. return crypto_authenc_genicv(areq, iv, CRYPTO_TFM_REQ_MAY_SLEEP);
  364. }
  365. static int crypto_authenc_verify(struct aead_request *req,
  366. authenc_ahash_t authenc_ahash_fn)
  367. {
  368. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  369. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  370. u8 *ohash;
  371. u8 *ihash;
  372. unsigned int authsize;
  373. areq_ctx->complete = authenc_verify_ahash_done;
  374. areq_ctx->update_complete = authenc_verify_ahash_update_done;
  375. ohash = authenc_ahash_fn(req, CRYPTO_TFM_REQ_MAY_SLEEP);
  376. if (IS_ERR(ohash))
  377. return PTR_ERR(ohash);
  378. authsize = crypto_aead_authsize(authenc);
  379. ihash = ohash + authsize;
  380. scatterwalk_map_and_copy(ihash, areq_ctx->sg, areq_ctx->cryptlen,
  381. authsize, 0);
  382. return memcmp(ihash, ohash, authsize) ? -EBADMSG : 0;
  383. }
  384. static int crypto_authenc_iverify(struct aead_request *req, u8 *iv,
  385. unsigned int cryptlen)
  386. {
  387. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  388. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  389. struct scatterlist *src = req->src;
  390. struct scatterlist *assoc = req->assoc;
  391. struct scatterlist *cipher = areq_ctx->cipher;
  392. struct scatterlist *asg = areq_ctx->asg;
  393. unsigned int ivsize = crypto_aead_ivsize(authenc);
  394. authenc_ahash_t authenc_ahash_fn = crypto_authenc_ahash_fb;
  395. struct page *srcp;
  396. u8 *vsrc;
  397. srcp = sg_page(src);
  398. vsrc = PageHighMem(srcp) ? NULL : page_address(srcp) + src->offset;
  399. if (ivsize) {
  400. sg_init_table(cipher, 2);
  401. sg_set_buf(cipher, iv, ivsize);
  402. authenc_chain(cipher, src, vsrc == iv + ivsize);
  403. src = cipher;
  404. cryptlen += ivsize;
  405. }
  406. if (sg_is_last(assoc)) {
  407. authenc_ahash_fn = crypto_authenc_ahash;
  408. sg_init_table(asg, 2);
  409. sg_set_page(asg, sg_page(assoc), assoc->length, assoc->offset);
  410. authenc_chain(asg, src, 0);
  411. src = asg;
  412. cryptlen += req->assoclen;
  413. }
  414. areq_ctx->cryptlen = cryptlen;
  415. areq_ctx->sg = src;
  416. return crypto_authenc_verify(req, authenc_ahash_fn);
  417. }
  418. static int crypto_authenc_decrypt(struct aead_request *req)
  419. {
  420. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  421. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  422. struct ablkcipher_request *abreq = aead_request_ctx(req);
  423. unsigned int cryptlen = req->cryptlen;
  424. unsigned int authsize = crypto_aead_authsize(authenc);
  425. u8 *iv = req->iv;
  426. int err;
  427. if (cryptlen < authsize)
  428. return -EINVAL;
  429. cryptlen -= authsize;
  430. err = crypto_authenc_iverify(req, iv, cryptlen);
  431. if (err)
  432. return err;
  433. ablkcipher_request_set_tfm(abreq, ctx->enc);
  434. ablkcipher_request_set_callback(abreq, aead_request_flags(req),
  435. req->base.complete, req->base.data);
  436. ablkcipher_request_set_crypt(abreq, req->src, req->dst, cryptlen, iv);
  437. return crypto_ablkcipher_decrypt(abreq);
  438. }
  439. static int crypto_authenc_init_tfm(struct crypto_tfm *tfm)
  440. {
  441. struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
  442. struct authenc_instance_ctx *ictx = crypto_instance_ctx(inst);
  443. struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm);
  444. struct crypto_ahash *auth;
  445. struct crypto_ablkcipher *enc;
  446. int err;
  447. auth = crypto_spawn_ahash(&ictx->auth);
  448. if (IS_ERR(auth))
  449. return PTR_ERR(auth);
  450. enc = crypto_spawn_skcipher(&ictx->enc);
  451. err = PTR_ERR(enc);
  452. if (IS_ERR(enc))
  453. goto err_free_ahash;
  454. ctx->auth = auth;
  455. ctx->enc = enc;
  456. ctx->reqoff = ALIGN(2 * crypto_ahash_digestsize(auth) +
  457. crypto_ahash_alignmask(auth),
  458. crypto_ahash_alignmask(auth) + 1) +
  459. crypto_ablkcipher_ivsize(enc);
  460. tfm->crt_aead.reqsize = sizeof(struct authenc_request_ctx) +
  461. ctx->reqoff +
  462. max_t(unsigned int,
  463. crypto_ahash_reqsize(auth) +
  464. sizeof(struct ahash_request),
  465. sizeof(struct skcipher_givcrypt_request) +
  466. crypto_ablkcipher_reqsize(enc));
  467. return 0;
  468. err_free_ahash:
  469. crypto_free_ahash(auth);
  470. return err;
  471. }
  472. static void crypto_authenc_exit_tfm(struct crypto_tfm *tfm)
  473. {
  474. struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm);
  475. crypto_free_ahash(ctx->auth);
  476. crypto_free_ablkcipher(ctx->enc);
  477. }
  478. static struct crypto_instance *crypto_authenc_alloc(struct rtattr **tb)
  479. {
  480. struct crypto_attr_type *algt;
  481. struct crypto_instance *inst;
  482. struct hash_alg_common *auth;
  483. struct crypto_alg *auth_base;
  484. struct crypto_alg *enc;
  485. struct authenc_instance_ctx *ctx;
  486. const char *enc_name;
  487. int err;
  488. algt = crypto_get_attr_type(tb);
  489. err = PTR_ERR(algt);
  490. if (IS_ERR(algt))
  491. return ERR_PTR(err);
  492. if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
  493. return ERR_PTR(-EINVAL);
  494. auth = ahash_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH,
  495. CRYPTO_ALG_TYPE_AHASH_MASK);
  496. if (IS_ERR(auth))
  497. return ERR_PTR(PTR_ERR(auth));
  498. auth_base = &auth->base;
  499. enc_name = crypto_attr_alg_name(tb[2]);
  500. err = PTR_ERR(enc_name);
  501. if (IS_ERR(enc_name))
  502. goto out_put_auth;
  503. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  504. err = -ENOMEM;
  505. if (!inst)
  506. goto out_put_auth;
  507. ctx = crypto_instance_ctx(inst);
  508. err = crypto_init_ahash_spawn(&ctx->auth, auth, inst);
  509. if (err)
  510. goto err_free_inst;
  511. crypto_set_skcipher_spawn(&ctx->enc, inst);
  512. err = crypto_grab_skcipher(&ctx->enc, enc_name, 0,
  513. crypto_requires_sync(algt->type,
  514. algt->mask));
  515. if (err)
  516. goto err_drop_auth;
  517. enc = crypto_skcipher_spawn_alg(&ctx->enc);
  518. err = -ENAMETOOLONG;
  519. if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
  520. "authenc(%s,%s)", auth_base->cra_name, enc->cra_name) >=
  521. CRYPTO_MAX_ALG_NAME)
  522. goto err_drop_enc;
  523. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  524. "authenc(%s,%s)", auth_base->cra_driver_name,
  525. enc->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  526. goto err_drop_enc;
  527. inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD;
  528. inst->alg.cra_flags |= enc->cra_flags & CRYPTO_ALG_ASYNC;
  529. inst->alg.cra_priority = enc->cra_priority *
  530. 10 + auth_base->cra_priority;
  531. inst->alg.cra_blocksize = enc->cra_blocksize;
  532. inst->alg.cra_alignmask = auth_base->cra_alignmask | enc->cra_alignmask;
  533. inst->alg.cra_type = &crypto_aead_type;
  534. inst->alg.cra_aead.ivsize = enc->cra_ablkcipher.ivsize;
  535. inst->alg.cra_aead.maxauthsize = auth->digestsize;
  536. inst->alg.cra_ctxsize = sizeof(struct crypto_authenc_ctx);
  537. inst->alg.cra_init = crypto_authenc_init_tfm;
  538. inst->alg.cra_exit = crypto_authenc_exit_tfm;
  539. inst->alg.cra_aead.setkey = crypto_authenc_setkey;
  540. inst->alg.cra_aead.encrypt = crypto_authenc_encrypt;
  541. inst->alg.cra_aead.decrypt = crypto_authenc_decrypt;
  542. inst->alg.cra_aead.givencrypt = crypto_authenc_givencrypt;
  543. out:
  544. crypto_mod_put(auth_base);
  545. return inst;
  546. err_drop_enc:
  547. crypto_drop_skcipher(&ctx->enc);
  548. err_drop_auth:
  549. crypto_drop_ahash(&ctx->auth);
  550. err_free_inst:
  551. kfree(inst);
  552. out_put_auth:
  553. inst = ERR_PTR(err);
  554. goto out;
  555. }
  556. static void crypto_authenc_free(struct crypto_instance *inst)
  557. {
  558. struct authenc_instance_ctx *ctx = crypto_instance_ctx(inst);
  559. crypto_drop_skcipher(&ctx->enc);
  560. crypto_drop_ahash(&ctx->auth);
  561. kfree(inst);
  562. }
  563. static struct crypto_template crypto_authenc_tmpl = {
  564. .name = "authenc",
  565. .alloc = crypto_authenc_alloc,
  566. .free = crypto_authenc_free,
  567. .module = THIS_MODULE,
  568. };
  569. static int __init crypto_authenc_module_init(void)
  570. {
  571. return crypto_register_template(&crypto_authenc_tmpl);
  572. }
  573. static void __exit crypto_authenc_module_exit(void)
  574. {
  575. crypto_unregister_template(&crypto_authenc_tmpl);
  576. }
  577. module_init(crypto_authenc_module_init);
  578. module_exit(crypto_authenc_module_exit);
  579. MODULE_LICENSE("GPL");
  580. MODULE_DESCRIPTION("Simple AEAD wrapper for IPsec");