authencesn.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. /*
  2. * authencesn.c - AEAD wrapper for IPsec with extended sequence numbers,
  3. * derived from authenc.c
  4. *
  5. * Copyright (C) 2010 secunet Security Networks AG
  6. * Copyright (C) 2010 Steffen Klassert <steffen.klassert@secunet.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the Free
  10. * Software Foundation; either version 2 of the License, or (at your option)
  11. * any later version.
  12. *
  13. */
  14. #include <crypto/internal/aead.h>
  15. #include <crypto/internal/hash.h>
  16. #include <crypto/internal/skcipher.h>
  17. #include <crypto/authenc.h>
  18. #include <crypto/scatterwalk.h>
  19. #include <linux/err.h>
  20. #include <linux/init.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/rtnetlink.h>
  24. #include <linux/slab.h>
  25. #include <linux/spinlock.h>
  26. struct authenc_esn_instance_ctx {
  27. struct crypto_ahash_spawn auth;
  28. struct crypto_skcipher_spawn enc;
  29. };
  30. struct crypto_authenc_esn_ctx {
  31. unsigned int reqoff;
  32. struct crypto_ahash *auth;
  33. struct crypto_ablkcipher *enc;
  34. };
  35. struct authenc_esn_request_ctx {
  36. unsigned int cryptlen;
  37. unsigned int headlen;
  38. unsigned int trailen;
  39. struct scatterlist *sg;
  40. struct scatterlist hsg[2];
  41. struct scatterlist tsg[1];
  42. struct scatterlist cipher[2];
  43. crypto_completion_t complete;
  44. crypto_completion_t update_complete;
  45. crypto_completion_t update_complete2;
  46. char tail[];
  47. };
  48. static void authenc_esn_request_complete(struct aead_request *req, int err)
  49. {
  50. if (err != -EINPROGRESS)
  51. aead_request_complete(req, err);
  52. }
  53. static int crypto_authenc_esn_setkey(struct crypto_aead *authenc_esn, const u8 *key,
  54. unsigned int keylen)
  55. {
  56. struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
  57. struct crypto_ahash *auth = ctx->auth;
  58. struct crypto_ablkcipher *enc = ctx->enc;
  59. struct crypto_authenc_keys keys;
  60. int err = -EINVAL;
  61. if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
  62. goto badkey;
  63. crypto_ahash_clear_flags(auth, CRYPTO_TFM_REQ_MASK);
  64. crypto_ahash_set_flags(auth, crypto_aead_get_flags(authenc_esn) &
  65. CRYPTO_TFM_REQ_MASK);
  66. err = crypto_ahash_setkey(auth, keys.authkey, keys.authkeylen);
  67. crypto_aead_set_flags(authenc_esn, crypto_ahash_get_flags(auth) &
  68. CRYPTO_TFM_RES_MASK);
  69. if (err)
  70. goto out;
  71. crypto_ablkcipher_clear_flags(enc, CRYPTO_TFM_REQ_MASK);
  72. crypto_ablkcipher_set_flags(enc, crypto_aead_get_flags(authenc_esn) &
  73. CRYPTO_TFM_REQ_MASK);
  74. err = crypto_ablkcipher_setkey(enc, keys.enckey, keys.enckeylen);
  75. crypto_aead_set_flags(authenc_esn, crypto_ablkcipher_get_flags(enc) &
  76. CRYPTO_TFM_RES_MASK);
  77. out:
  78. return err;
  79. badkey:
  80. crypto_aead_set_flags(authenc_esn, CRYPTO_TFM_RES_BAD_KEY_LEN);
  81. goto out;
  82. }
  83. static void authenc_esn_geniv_ahash_update_done(struct crypto_async_request *areq,
  84. int err)
  85. {
  86. struct aead_request *req = areq->data;
  87. struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
  88. struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
  89. struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
  90. struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
  91. if (err)
  92. goto out;
  93. ahash_request_set_crypt(ahreq, areq_ctx->sg, ahreq->result,
  94. areq_ctx->cryptlen);
  95. ahash_request_set_callback(ahreq, aead_request_flags(req) &
  96. CRYPTO_TFM_REQ_MAY_SLEEP,
  97. areq_ctx->update_complete2, req);
  98. err = crypto_ahash_update(ahreq);
  99. if (err)
  100. goto out;
  101. ahash_request_set_crypt(ahreq, areq_ctx->tsg, ahreq->result,
  102. areq_ctx->trailen);
  103. ahash_request_set_callback(ahreq, aead_request_flags(req) &
  104. CRYPTO_TFM_REQ_MAY_SLEEP,
  105. areq_ctx->complete, req);
  106. err = crypto_ahash_finup(ahreq);
  107. if (err)
  108. goto out;
  109. scatterwalk_map_and_copy(ahreq->result, areq_ctx->sg,
  110. areq_ctx->cryptlen,
  111. crypto_aead_authsize(authenc_esn), 1);
  112. out:
  113. authenc_esn_request_complete(req, err);
  114. }
  115. static void authenc_esn_geniv_ahash_update_done2(struct crypto_async_request *areq,
  116. int err)
  117. {
  118. struct aead_request *req = areq->data;
  119. struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
  120. struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
  121. struct authenc_esn_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. ahash_request_set_crypt(ahreq, areq_ctx->tsg, ahreq->result,
  126. areq_ctx->trailen);
  127. ahash_request_set_callback(ahreq, aead_request_flags(req) &
  128. CRYPTO_TFM_REQ_MAY_SLEEP,
  129. areq_ctx->complete, req);
  130. err = crypto_ahash_finup(ahreq);
  131. if (err)
  132. goto out;
  133. scatterwalk_map_and_copy(ahreq->result, areq_ctx->sg,
  134. areq_ctx->cryptlen,
  135. crypto_aead_authsize(authenc_esn), 1);
  136. out:
  137. authenc_esn_request_complete(req, err);
  138. }
  139. static void authenc_esn_geniv_ahash_done(struct crypto_async_request *areq,
  140. int err)
  141. {
  142. struct aead_request *req = areq->data;
  143. struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
  144. struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
  145. struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
  146. struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
  147. if (err)
  148. goto out;
  149. scatterwalk_map_and_copy(ahreq->result, areq_ctx->sg,
  150. areq_ctx->cryptlen,
  151. crypto_aead_authsize(authenc_esn), 1);
  152. out:
  153. aead_request_complete(req, err);
  154. }
  155. static void authenc_esn_verify_ahash_update_done(struct crypto_async_request *areq,
  156. int err)
  157. {
  158. u8 *ihash;
  159. unsigned int authsize;
  160. struct ablkcipher_request *abreq;
  161. struct aead_request *req = areq->data;
  162. struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
  163. struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
  164. struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
  165. struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
  166. unsigned int cryptlen = req->cryptlen;
  167. if (err)
  168. goto out;
  169. ahash_request_set_crypt(ahreq, areq_ctx->sg, ahreq->result,
  170. areq_ctx->cryptlen);
  171. ahash_request_set_callback(ahreq,
  172. aead_request_flags(req) &
  173. CRYPTO_TFM_REQ_MAY_SLEEP,
  174. areq_ctx->update_complete2, req);
  175. err = crypto_ahash_update(ahreq);
  176. if (err)
  177. goto out;
  178. ahash_request_set_crypt(ahreq, areq_ctx->tsg, ahreq->result,
  179. areq_ctx->trailen);
  180. ahash_request_set_callback(ahreq, aead_request_flags(req) &
  181. CRYPTO_TFM_REQ_MAY_SLEEP,
  182. areq_ctx->complete, req);
  183. err = crypto_ahash_finup(ahreq);
  184. if (err)
  185. goto out;
  186. authsize = crypto_aead_authsize(authenc_esn);
  187. cryptlen -= authsize;
  188. ihash = ahreq->result + authsize;
  189. scatterwalk_map_and_copy(ihash, areq_ctx->sg, areq_ctx->cryptlen,
  190. authsize, 0);
  191. err = crypto_memneq(ihash, ahreq->result, authsize) ? -EBADMSG : 0;
  192. if (err)
  193. goto out;
  194. abreq = aead_request_ctx(req);
  195. ablkcipher_request_set_tfm(abreq, ctx->enc);
  196. ablkcipher_request_set_callback(abreq, aead_request_flags(req),
  197. req->base.complete, req->base.data);
  198. ablkcipher_request_set_crypt(abreq, req->src, req->dst,
  199. cryptlen, req->iv);
  200. err = crypto_ablkcipher_decrypt(abreq);
  201. out:
  202. authenc_esn_request_complete(req, err);
  203. }
  204. static void authenc_esn_verify_ahash_update_done2(struct crypto_async_request *areq,
  205. int err)
  206. {
  207. u8 *ihash;
  208. unsigned int authsize;
  209. struct ablkcipher_request *abreq;
  210. struct aead_request *req = areq->data;
  211. struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
  212. struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
  213. struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
  214. struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
  215. unsigned int cryptlen = req->cryptlen;
  216. if (err)
  217. goto out;
  218. ahash_request_set_crypt(ahreq, areq_ctx->tsg, ahreq->result,
  219. areq_ctx->trailen);
  220. ahash_request_set_callback(ahreq, aead_request_flags(req) &
  221. CRYPTO_TFM_REQ_MAY_SLEEP,
  222. areq_ctx->complete, req);
  223. err = crypto_ahash_finup(ahreq);
  224. if (err)
  225. goto out;
  226. authsize = crypto_aead_authsize(authenc_esn);
  227. cryptlen -= authsize;
  228. ihash = ahreq->result + authsize;
  229. scatterwalk_map_and_copy(ihash, areq_ctx->sg, areq_ctx->cryptlen,
  230. authsize, 0);
  231. err = crypto_memneq(ihash, ahreq->result, authsize) ? -EBADMSG : 0;
  232. if (err)
  233. goto out;
  234. abreq = aead_request_ctx(req);
  235. ablkcipher_request_set_tfm(abreq, ctx->enc);
  236. ablkcipher_request_set_callback(abreq, aead_request_flags(req),
  237. req->base.complete, req->base.data);
  238. ablkcipher_request_set_crypt(abreq, req->src, req->dst,
  239. cryptlen, req->iv);
  240. err = crypto_ablkcipher_decrypt(abreq);
  241. out:
  242. authenc_esn_request_complete(req, err);
  243. }
  244. static void authenc_esn_verify_ahash_done(struct crypto_async_request *areq,
  245. int err)
  246. {
  247. u8 *ihash;
  248. unsigned int authsize;
  249. struct ablkcipher_request *abreq;
  250. struct aead_request *req = areq->data;
  251. struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
  252. struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
  253. struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
  254. struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
  255. unsigned int cryptlen = req->cryptlen;
  256. if (err)
  257. goto out;
  258. authsize = crypto_aead_authsize(authenc_esn);
  259. cryptlen -= authsize;
  260. ihash = ahreq->result + authsize;
  261. scatterwalk_map_and_copy(ihash, areq_ctx->sg, areq_ctx->cryptlen,
  262. authsize, 0);
  263. err = crypto_memneq(ihash, ahreq->result, authsize) ? -EBADMSG : 0;
  264. if (err)
  265. goto out;
  266. abreq = aead_request_ctx(req);
  267. ablkcipher_request_set_tfm(abreq, ctx->enc);
  268. ablkcipher_request_set_callback(abreq, aead_request_flags(req),
  269. req->base.complete, req->base.data);
  270. ablkcipher_request_set_crypt(abreq, req->src, req->dst,
  271. cryptlen, req->iv);
  272. err = crypto_ablkcipher_decrypt(abreq);
  273. out:
  274. authenc_esn_request_complete(req, err);
  275. }
  276. static u8 *crypto_authenc_esn_ahash(struct aead_request *req,
  277. unsigned int flags)
  278. {
  279. struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
  280. struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
  281. struct crypto_ahash *auth = ctx->auth;
  282. struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
  283. struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
  284. u8 *hash = areq_ctx->tail;
  285. int err;
  286. hash = (u8 *)ALIGN((unsigned long)hash + crypto_ahash_alignmask(auth),
  287. crypto_ahash_alignmask(auth) + 1);
  288. ahash_request_set_tfm(ahreq, auth);
  289. err = crypto_ahash_init(ahreq);
  290. if (err)
  291. return ERR_PTR(err);
  292. ahash_request_set_crypt(ahreq, areq_ctx->hsg, hash, areq_ctx->headlen);
  293. ahash_request_set_callback(ahreq, aead_request_flags(req) & flags,
  294. areq_ctx->update_complete, req);
  295. err = crypto_ahash_update(ahreq);
  296. if (err)
  297. return ERR_PTR(err);
  298. ahash_request_set_crypt(ahreq, areq_ctx->sg, hash, areq_ctx->cryptlen);
  299. ahash_request_set_callback(ahreq, aead_request_flags(req) & flags,
  300. areq_ctx->update_complete2, req);
  301. err = crypto_ahash_update(ahreq);
  302. if (err)
  303. return ERR_PTR(err);
  304. ahash_request_set_crypt(ahreq, areq_ctx->tsg, hash,
  305. areq_ctx->trailen);
  306. ahash_request_set_callback(ahreq, aead_request_flags(req) & flags,
  307. areq_ctx->complete, req);
  308. err = crypto_ahash_finup(ahreq);
  309. if (err)
  310. return ERR_PTR(err);
  311. return hash;
  312. }
  313. static int crypto_authenc_esn_genicv(struct aead_request *req, u8 *iv,
  314. unsigned int flags)
  315. {
  316. struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
  317. struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
  318. struct scatterlist *dst = req->dst;
  319. struct scatterlist *assoc = req->assoc;
  320. struct scatterlist *cipher = areq_ctx->cipher;
  321. struct scatterlist *hsg = areq_ctx->hsg;
  322. struct scatterlist *tsg = areq_ctx->tsg;
  323. struct scatterlist *assoc1;
  324. struct scatterlist *assoc2;
  325. unsigned int ivsize = crypto_aead_ivsize(authenc_esn);
  326. unsigned int cryptlen = req->cryptlen;
  327. struct page *dstp;
  328. u8 *vdst;
  329. u8 *hash;
  330. dstp = sg_page(dst);
  331. vdst = PageHighMem(dstp) ? NULL : page_address(dstp) + dst->offset;
  332. if (ivsize) {
  333. sg_init_table(cipher, 2);
  334. sg_set_buf(cipher, iv, ivsize);
  335. scatterwalk_crypto_chain(cipher, dst, vdst == iv + ivsize, 2);
  336. dst = cipher;
  337. cryptlen += ivsize;
  338. }
  339. if (sg_is_last(assoc))
  340. return -EINVAL;
  341. assoc1 = assoc + 1;
  342. if (sg_is_last(assoc1))
  343. return -EINVAL;
  344. assoc2 = assoc + 2;
  345. if (!sg_is_last(assoc2))
  346. return -EINVAL;
  347. sg_init_table(hsg, 2);
  348. sg_set_page(hsg, sg_page(assoc), assoc->length, assoc->offset);
  349. sg_set_page(hsg + 1, sg_page(assoc2), assoc2->length, assoc2->offset);
  350. sg_init_table(tsg, 1);
  351. sg_set_page(tsg, sg_page(assoc1), assoc1->length, assoc1->offset);
  352. areq_ctx->cryptlen = cryptlen;
  353. areq_ctx->headlen = assoc->length + assoc2->length;
  354. areq_ctx->trailen = assoc1->length;
  355. areq_ctx->sg = dst;
  356. areq_ctx->complete = authenc_esn_geniv_ahash_done;
  357. areq_ctx->update_complete = authenc_esn_geniv_ahash_update_done;
  358. areq_ctx->update_complete2 = authenc_esn_geniv_ahash_update_done2;
  359. hash = crypto_authenc_esn_ahash(req, flags);
  360. if (IS_ERR(hash))
  361. return PTR_ERR(hash);
  362. scatterwalk_map_and_copy(hash, dst, cryptlen,
  363. crypto_aead_authsize(authenc_esn), 1);
  364. return 0;
  365. }
  366. static void crypto_authenc_esn_encrypt_done(struct crypto_async_request *req,
  367. int err)
  368. {
  369. struct aead_request *areq = req->data;
  370. if (!err) {
  371. struct crypto_aead *authenc_esn = crypto_aead_reqtfm(areq);
  372. struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
  373. struct ablkcipher_request *abreq = aead_request_ctx(areq);
  374. u8 *iv = (u8 *)(abreq + 1) +
  375. crypto_ablkcipher_reqsize(ctx->enc);
  376. err = crypto_authenc_esn_genicv(areq, iv, 0);
  377. }
  378. authenc_esn_request_complete(areq, err);
  379. }
  380. static int crypto_authenc_esn_encrypt(struct aead_request *req)
  381. {
  382. struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
  383. struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
  384. struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
  385. struct crypto_ablkcipher *enc = ctx->enc;
  386. struct scatterlist *dst = req->dst;
  387. unsigned int cryptlen = req->cryptlen;
  388. struct ablkcipher_request *abreq = (void *)(areq_ctx->tail
  389. + ctx->reqoff);
  390. u8 *iv = (u8 *)abreq - crypto_ablkcipher_ivsize(enc);
  391. int err;
  392. ablkcipher_request_set_tfm(abreq, enc);
  393. ablkcipher_request_set_callback(abreq, aead_request_flags(req),
  394. crypto_authenc_esn_encrypt_done, req);
  395. ablkcipher_request_set_crypt(abreq, req->src, dst, cryptlen, req->iv);
  396. memcpy(iv, req->iv, crypto_aead_ivsize(authenc_esn));
  397. err = crypto_ablkcipher_encrypt(abreq);
  398. if (err)
  399. return err;
  400. return crypto_authenc_esn_genicv(req, iv, CRYPTO_TFM_REQ_MAY_SLEEP);
  401. }
  402. static void crypto_authenc_esn_givencrypt_done(struct crypto_async_request *req,
  403. int err)
  404. {
  405. struct aead_request *areq = req->data;
  406. if (!err) {
  407. struct skcipher_givcrypt_request *greq = aead_request_ctx(areq);
  408. err = crypto_authenc_esn_genicv(areq, greq->giv, 0);
  409. }
  410. authenc_esn_request_complete(areq, err);
  411. }
  412. static int crypto_authenc_esn_givencrypt(struct aead_givcrypt_request *req)
  413. {
  414. struct crypto_aead *authenc_esn = aead_givcrypt_reqtfm(req);
  415. struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
  416. struct aead_request *areq = &req->areq;
  417. struct skcipher_givcrypt_request *greq = aead_request_ctx(areq);
  418. u8 *iv = req->giv;
  419. int err;
  420. skcipher_givcrypt_set_tfm(greq, ctx->enc);
  421. skcipher_givcrypt_set_callback(greq, aead_request_flags(areq),
  422. crypto_authenc_esn_givencrypt_done, areq);
  423. skcipher_givcrypt_set_crypt(greq, areq->src, areq->dst, areq->cryptlen,
  424. areq->iv);
  425. skcipher_givcrypt_set_giv(greq, iv, req->seq);
  426. err = crypto_skcipher_givencrypt(greq);
  427. if (err)
  428. return err;
  429. return crypto_authenc_esn_genicv(areq, iv, CRYPTO_TFM_REQ_MAY_SLEEP);
  430. }
  431. static int crypto_authenc_esn_verify(struct aead_request *req)
  432. {
  433. struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
  434. struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
  435. u8 *ohash;
  436. u8 *ihash;
  437. unsigned int authsize;
  438. areq_ctx->complete = authenc_esn_verify_ahash_done;
  439. areq_ctx->update_complete = authenc_esn_verify_ahash_update_done;
  440. ohash = crypto_authenc_esn_ahash(req, CRYPTO_TFM_REQ_MAY_SLEEP);
  441. if (IS_ERR(ohash))
  442. return PTR_ERR(ohash);
  443. authsize = crypto_aead_authsize(authenc_esn);
  444. ihash = ohash + authsize;
  445. scatterwalk_map_and_copy(ihash, areq_ctx->sg, areq_ctx->cryptlen,
  446. authsize, 0);
  447. return crypto_memneq(ihash, ohash, authsize) ? -EBADMSG : 0;
  448. }
  449. static int crypto_authenc_esn_iverify(struct aead_request *req, u8 *iv,
  450. unsigned int cryptlen)
  451. {
  452. struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
  453. struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
  454. struct scatterlist *src = req->src;
  455. struct scatterlist *assoc = req->assoc;
  456. struct scatterlist *cipher = areq_ctx->cipher;
  457. struct scatterlist *hsg = areq_ctx->hsg;
  458. struct scatterlist *tsg = areq_ctx->tsg;
  459. struct scatterlist *assoc1;
  460. struct scatterlist *assoc2;
  461. unsigned int ivsize = crypto_aead_ivsize(authenc_esn);
  462. struct page *srcp;
  463. u8 *vsrc;
  464. srcp = sg_page(src);
  465. vsrc = PageHighMem(srcp) ? NULL : page_address(srcp) + src->offset;
  466. if (ivsize) {
  467. sg_init_table(cipher, 2);
  468. sg_set_buf(cipher, iv, ivsize);
  469. scatterwalk_crypto_chain(cipher, src, vsrc == iv + ivsize, 2);
  470. src = cipher;
  471. cryptlen += ivsize;
  472. }
  473. if (sg_is_last(assoc))
  474. return -EINVAL;
  475. assoc1 = assoc + 1;
  476. if (sg_is_last(assoc1))
  477. return -EINVAL;
  478. assoc2 = assoc + 2;
  479. if (!sg_is_last(assoc2))
  480. return -EINVAL;
  481. sg_init_table(hsg, 2);
  482. sg_set_page(hsg, sg_page(assoc), assoc->length, assoc->offset);
  483. sg_set_page(hsg + 1, sg_page(assoc2), assoc2->length, assoc2->offset);
  484. sg_init_table(tsg, 1);
  485. sg_set_page(tsg, sg_page(assoc1), assoc1->length, assoc1->offset);
  486. areq_ctx->cryptlen = cryptlen;
  487. areq_ctx->headlen = assoc->length + assoc2->length;
  488. areq_ctx->trailen = assoc1->length;
  489. areq_ctx->sg = src;
  490. areq_ctx->complete = authenc_esn_verify_ahash_done;
  491. areq_ctx->update_complete = authenc_esn_verify_ahash_update_done;
  492. areq_ctx->update_complete2 = authenc_esn_verify_ahash_update_done2;
  493. return crypto_authenc_esn_verify(req);
  494. }
  495. static int crypto_authenc_esn_decrypt(struct aead_request *req)
  496. {
  497. struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
  498. struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
  499. struct ablkcipher_request *abreq = aead_request_ctx(req);
  500. unsigned int cryptlen = req->cryptlen;
  501. unsigned int authsize = crypto_aead_authsize(authenc_esn);
  502. u8 *iv = req->iv;
  503. int err;
  504. if (cryptlen < authsize)
  505. return -EINVAL;
  506. cryptlen -= authsize;
  507. err = crypto_authenc_esn_iverify(req, iv, cryptlen);
  508. if (err)
  509. return err;
  510. ablkcipher_request_set_tfm(abreq, ctx->enc);
  511. ablkcipher_request_set_callback(abreq, aead_request_flags(req),
  512. req->base.complete, req->base.data);
  513. ablkcipher_request_set_crypt(abreq, req->src, req->dst, cryptlen, iv);
  514. return crypto_ablkcipher_decrypt(abreq);
  515. }
  516. static int crypto_authenc_esn_init_tfm(struct crypto_tfm *tfm)
  517. {
  518. struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
  519. struct authenc_esn_instance_ctx *ictx = crypto_instance_ctx(inst);
  520. struct crypto_authenc_esn_ctx *ctx = crypto_tfm_ctx(tfm);
  521. struct crypto_ahash *auth;
  522. struct crypto_ablkcipher *enc;
  523. int err;
  524. auth = crypto_spawn_ahash(&ictx->auth);
  525. if (IS_ERR(auth))
  526. return PTR_ERR(auth);
  527. enc = crypto_spawn_skcipher(&ictx->enc);
  528. err = PTR_ERR(enc);
  529. if (IS_ERR(enc))
  530. goto err_free_ahash;
  531. ctx->auth = auth;
  532. ctx->enc = enc;
  533. ctx->reqoff = ALIGN(2 * crypto_ahash_digestsize(auth) +
  534. crypto_ahash_alignmask(auth),
  535. crypto_ahash_alignmask(auth) + 1) +
  536. crypto_ablkcipher_ivsize(enc);
  537. crypto_aead_set_reqsize(__crypto_aead_cast(tfm),
  538. sizeof(struct authenc_esn_request_ctx) +
  539. ctx->reqoff +
  540. max_t(unsigned int,
  541. crypto_ahash_reqsize(auth) +
  542. sizeof(struct ahash_request),
  543. sizeof(struct skcipher_givcrypt_request) +
  544. crypto_ablkcipher_reqsize(enc)));
  545. return 0;
  546. err_free_ahash:
  547. crypto_free_ahash(auth);
  548. return err;
  549. }
  550. static void crypto_authenc_esn_exit_tfm(struct crypto_tfm *tfm)
  551. {
  552. struct crypto_authenc_esn_ctx *ctx = crypto_tfm_ctx(tfm);
  553. crypto_free_ahash(ctx->auth);
  554. crypto_free_ablkcipher(ctx->enc);
  555. }
  556. static struct crypto_instance *crypto_authenc_esn_alloc(struct rtattr **tb)
  557. {
  558. struct crypto_attr_type *algt;
  559. struct crypto_instance *inst;
  560. struct hash_alg_common *auth;
  561. struct crypto_alg *auth_base;
  562. struct crypto_alg *enc;
  563. struct authenc_esn_instance_ctx *ctx;
  564. const char *enc_name;
  565. int err;
  566. algt = crypto_get_attr_type(tb);
  567. if (IS_ERR(algt))
  568. return ERR_CAST(algt);
  569. if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
  570. return ERR_PTR(-EINVAL);
  571. auth = ahash_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH,
  572. CRYPTO_ALG_TYPE_AHASH_MASK);
  573. if (IS_ERR(auth))
  574. return ERR_CAST(auth);
  575. auth_base = &auth->base;
  576. enc_name = crypto_attr_alg_name(tb[2]);
  577. err = PTR_ERR(enc_name);
  578. if (IS_ERR(enc_name))
  579. goto out_put_auth;
  580. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  581. err = -ENOMEM;
  582. if (!inst)
  583. goto out_put_auth;
  584. ctx = crypto_instance_ctx(inst);
  585. err = crypto_init_ahash_spawn(&ctx->auth, auth, inst);
  586. if (err)
  587. goto err_free_inst;
  588. crypto_set_skcipher_spawn(&ctx->enc, inst);
  589. err = crypto_grab_skcipher(&ctx->enc, enc_name, 0,
  590. crypto_requires_sync(algt->type,
  591. algt->mask));
  592. if (err)
  593. goto err_drop_auth;
  594. enc = crypto_skcipher_spawn_alg(&ctx->enc);
  595. err = -ENAMETOOLONG;
  596. if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
  597. "authencesn(%s,%s)", auth_base->cra_name, enc->cra_name) >=
  598. CRYPTO_MAX_ALG_NAME)
  599. goto err_drop_enc;
  600. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  601. "authencesn(%s,%s)", auth_base->cra_driver_name,
  602. enc->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  603. goto err_drop_enc;
  604. inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD;
  605. inst->alg.cra_flags |= enc->cra_flags & CRYPTO_ALG_ASYNC;
  606. inst->alg.cra_priority = enc->cra_priority *
  607. 10 + auth_base->cra_priority;
  608. inst->alg.cra_blocksize = enc->cra_blocksize;
  609. inst->alg.cra_alignmask = auth_base->cra_alignmask | enc->cra_alignmask;
  610. inst->alg.cra_type = &crypto_aead_type;
  611. inst->alg.cra_aead.ivsize = enc->cra_ablkcipher.ivsize;
  612. inst->alg.cra_aead.maxauthsize = auth->digestsize;
  613. inst->alg.cra_ctxsize = sizeof(struct crypto_authenc_esn_ctx);
  614. inst->alg.cra_init = crypto_authenc_esn_init_tfm;
  615. inst->alg.cra_exit = crypto_authenc_esn_exit_tfm;
  616. inst->alg.cra_aead.setkey = crypto_authenc_esn_setkey;
  617. inst->alg.cra_aead.encrypt = crypto_authenc_esn_encrypt;
  618. inst->alg.cra_aead.decrypt = crypto_authenc_esn_decrypt;
  619. inst->alg.cra_aead.givencrypt = crypto_authenc_esn_givencrypt;
  620. out:
  621. crypto_mod_put(auth_base);
  622. return inst;
  623. err_drop_enc:
  624. crypto_drop_skcipher(&ctx->enc);
  625. err_drop_auth:
  626. crypto_drop_ahash(&ctx->auth);
  627. err_free_inst:
  628. kfree(inst);
  629. out_put_auth:
  630. inst = ERR_PTR(err);
  631. goto out;
  632. }
  633. static void crypto_authenc_esn_free(struct crypto_instance *inst)
  634. {
  635. struct authenc_esn_instance_ctx *ctx = crypto_instance_ctx(inst);
  636. crypto_drop_skcipher(&ctx->enc);
  637. crypto_drop_ahash(&ctx->auth);
  638. kfree(inst);
  639. }
  640. static struct crypto_template crypto_authenc_esn_tmpl = {
  641. .name = "authencesn",
  642. .alloc = crypto_authenc_esn_alloc,
  643. .free = crypto_authenc_esn_free,
  644. .module = THIS_MODULE,
  645. };
  646. static int __init crypto_authenc_esn_module_init(void)
  647. {
  648. return crypto_register_template(&crypto_authenc_esn_tmpl);
  649. }
  650. static void __exit crypto_authenc_esn_module_exit(void)
  651. {
  652. crypto_unregister_template(&crypto_authenc_esn_tmpl);
  653. }
  654. module_init(crypto_authenc_esn_module_init);
  655. module_exit(crypto_authenc_esn_module_exit);
  656. MODULE_LICENSE("GPL");
  657. MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
  658. MODULE_DESCRIPTION("AEAD wrapper for IPsec with extended sequence numbers");
  659. MODULE_ALIAS_CRYPTO("authencesn");