authenc.c 20 KB

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