authenc.c 20 KB

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