authenc.c 20 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 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 = memcmp(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 = memcmp(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 (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 ablkcipher_request *abreq = aead_request_ctx(areq);
  305. u8 *iv = (u8 *)(abreq + 1) +
  306. crypto_ablkcipher_reqsize(ctx->enc);
  307. err = crypto_authenc_genicv(areq, iv, 0);
  308. }
  309. authenc_request_complete(areq, err);
  310. }
  311. static int crypto_authenc_encrypt(struct aead_request *req)
  312. {
  313. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  314. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  315. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  316. struct crypto_ablkcipher *enc = ctx->enc;
  317. struct scatterlist *dst = req->dst;
  318. unsigned int cryptlen = req->cryptlen;
  319. struct ablkcipher_request *abreq = (void *)(areq_ctx->tail
  320. + ctx->reqoff);
  321. u8 *iv = (u8 *)abreq - crypto_ablkcipher_ivsize(enc);
  322. int err;
  323. ablkcipher_request_set_tfm(abreq, enc);
  324. ablkcipher_request_set_callback(abreq, aead_request_flags(req),
  325. crypto_authenc_encrypt_done, req);
  326. ablkcipher_request_set_crypt(abreq, req->src, dst, cryptlen, req->iv);
  327. memcpy(iv, req->iv, crypto_aead_ivsize(authenc));
  328. err = crypto_ablkcipher_encrypt(abreq);
  329. if (err)
  330. return err;
  331. return crypto_authenc_genicv(req, iv, CRYPTO_TFM_REQ_MAY_SLEEP);
  332. }
  333. static void crypto_authenc_givencrypt_done(struct crypto_async_request *req,
  334. int err)
  335. {
  336. struct aead_request *areq = req->data;
  337. if (!err) {
  338. struct skcipher_givcrypt_request *greq = aead_request_ctx(areq);
  339. err = crypto_authenc_genicv(areq, greq->giv, 0);
  340. }
  341. authenc_request_complete(areq, err);
  342. }
  343. static int crypto_authenc_givencrypt(struct aead_givcrypt_request *req)
  344. {
  345. struct crypto_aead *authenc = aead_givcrypt_reqtfm(req);
  346. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  347. struct aead_request *areq = &req->areq;
  348. struct skcipher_givcrypt_request *greq = aead_request_ctx(areq);
  349. u8 *iv = req->giv;
  350. int err;
  351. skcipher_givcrypt_set_tfm(greq, ctx->enc);
  352. skcipher_givcrypt_set_callback(greq, aead_request_flags(areq),
  353. crypto_authenc_givencrypt_done, areq);
  354. skcipher_givcrypt_set_crypt(greq, areq->src, areq->dst, areq->cryptlen,
  355. areq->iv);
  356. skcipher_givcrypt_set_giv(greq, iv, req->seq);
  357. err = crypto_skcipher_givencrypt(greq);
  358. if (err)
  359. return err;
  360. return crypto_authenc_genicv(areq, iv, CRYPTO_TFM_REQ_MAY_SLEEP);
  361. }
  362. static int crypto_authenc_verify(struct aead_request *req,
  363. authenc_ahash_t authenc_ahash_fn)
  364. {
  365. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  366. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  367. u8 *ohash;
  368. u8 *ihash;
  369. unsigned int authsize;
  370. areq_ctx->complete = authenc_verify_ahash_done;
  371. areq_ctx->update_complete = authenc_verify_ahash_update_done;
  372. ohash = authenc_ahash_fn(req, CRYPTO_TFM_REQ_MAY_SLEEP);
  373. if (IS_ERR(ohash))
  374. return PTR_ERR(ohash);
  375. authsize = crypto_aead_authsize(authenc);
  376. ihash = ohash + authsize;
  377. scatterwalk_map_and_copy(ihash, areq_ctx->sg, areq_ctx->cryptlen,
  378. authsize, 0);
  379. return memcmp(ihash, ohash, authsize) ? -EBADMSG : 0;
  380. }
  381. static int crypto_authenc_iverify(struct aead_request *req, u8 *iv,
  382. unsigned int cryptlen)
  383. {
  384. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  385. struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
  386. struct scatterlist *src = req->src;
  387. struct scatterlist *assoc = req->assoc;
  388. struct scatterlist *cipher = areq_ctx->cipher;
  389. struct scatterlist *asg = areq_ctx->asg;
  390. unsigned int ivsize = crypto_aead_ivsize(authenc);
  391. authenc_ahash_t authenc_ahash_fn = crypto_authenc_ahash_fb;
  392. struct page *srcp;
  393. u8 *vsrc;
  394. srcp = sg_page(src);
  395. vsrc = PageHighMem(srcp) ? NULL : page_address(srcp) + src->offset;
  396. if (ivsize) {
  397. sg_init_table(cipher, 2);
  398. sg_set_buf(cipher, iv, ivsize);
  399. scatterwalk_crypto_chain(cipher, src, vsrc == iv + ivsize, 2);
  400. src = cipher;
  401. cryptlen += ivsize;
  402. }
  403. if (sg_is_last(assoc)) {
  404. authenc_ahash_fn = crypto_authenc_ahash;
  405. sg_init_table(asg, 2);
  406. sg_set_page(asg, sg_page(assoc), assoc->length, assoc->offset);
  407. scatterwalk_crypto_chain(asg, src, 0, 2);
  408. src = asg;
  409. cryptlen += req->assoclen;
  410. }
  411. areq_ctx->cryptlen = cryptlen;
  412. areq_ctx->sg = src;
  413. return crypto_authenc_verify(req, authenc_ahash_fn);
  414. }
  415. static int crypto_authenc_decrypt(struct aead_request *req)
  416. {
  417. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  418. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  419. struct ablkcipher_request *abreq = aead_request_ctx(req);
  420. unsigned int cryptlen = req->cryptlen;
  421. unsigned int authsize = crypto_aead_authsize(authenc);
  422. u8 *iv = req->iv;
  423. int err;
  424. if (cryptlen < authsize)
  425. return -EINVAL;
  426. cryptlen -= authsize;
  427. err = crypto_authenc_iverify(req, iv, cryptlen);
  428. if (err)
  429. return err;
  430. ablkcipher_request_set_tfm(abreq, ctx->enc);
  431. ablkcipher_request_set_callback(abreq, aead_request_flags(req),
  432. req->base.complete, req->base.data);
  433. ablkcipher_request_set_crypt(abreq, req->src, req->dst, cryptlen, iv);
  434. return crypto_ablkcipher_decrypt(abreq);
  435. }
  436. static int crypto_authenc_init_tfm(struct crypto_tfm *tfm)
  437. {
  438. struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
  439. struct authenc_instance_ctx *ictx = crypto_instance_ctx(inst);
  440. struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm);
  441. struct crypto_ahash *auth;
  442. struct crypto_ablkcipher *enc;
  443. int err;
  444. auth = crypto_spawn_ahash(&ictx->auth);
  445. if (IS_ERR(auth))
  446. return PTR_ERR(auth);
  447. enc = crypto_spawn_skcipher(&ictx->enc);
  448. err = PTR_ERR(enc);
  449. if (IS_ERR(enc))
  450. goto err_free_ahash;
  451. ctx->auth = auth;
  452. ctx->enc = enc;
  453. ctx->reqoff = ALIGN(2 * crypto_ahash_digestsize(auth) +
  454. crypto_ahash_alignmask(auth),
  455. crypto_ahash_alignmask(auth) + 1) +
  456. crypto_ablkcipher_ivsize(enc);
  457. tfm->crt_aead.reqsize = sizeof(struct authenc_request_ctx) +
  458. ctx->reqoff +
  459. max_t(unsigned int,
  460. crypto_ahash_reqsize(auth) +
  461. sizeof(struct ahash_request),
  462. sizeof(struct skcipher_givcrypt_request) +
  463. crypto_ablkcipher_reqsize(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_CAST(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");