authencesn.c 23 KB

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