authenc.c 20 KB

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