gcm.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  1. /*
  2. * GCM: Galois/Counter Mode.
  3. *
  4. * Copyright (c) 2007 Nokia Siemens Networks - Mikko Herranen <mh1@iki.fi>
  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 version 2 as published
  8. * by the Free Software Foundation.
  9. */
  10. #include <crypto/gf128mul.h>
  11. #include <crypto/internal/aead.h>
  12. #include <crypto/internal/skcipher.h>
  13. #include <crypto/internal/hash.h>
  14. #include <crypto/scatterwalk.h>
  15. #include <crypto/hash.h>
  16. #include "internal.h"
  17. #include <linux/completion.h>
  18. #include <linux/err.h>
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/slab.h>
  23. struct gcm_instance_ctx {
  24. struct crypto_skcipher_spawn ctr;
  25. struct crypto_ahash_spawn ghash;
  26. };
  27. struct crypto_gcm_ctx {
  28. struct crypto_ablkcipher *ctr;
  29. struct crypto_ahash *ghash;
  30. };
  31. struct crypto_rfc4106_ctx {
  32. struct crypto_aead *child;
  33. u8 nonce[4];
  34. };
  35. struct crypto_rfc4543_instance_ctx {
  36. struct crypto_aead_spawn aead;
  37. struct crypto_skcipher_spawn null;
  38. };
  39. struct crypto_rfc4543_ctx {
  40. struct crypto_aead *child;
  41. struct crypto_blkcipher *null;
  42. u8 nonce[4];
  43. };
  44. struct crypto_rfc4543_req_ctx {
  45. u8 auth_tag[16];
  46. struct scatterlist cipher[1];
  47. struct scatterlist payload[2];
  48. struct scatterlist assoc[2];
  49. struct aead_request subreq;
  50. };
  51. struct crypto_gcm_ghash_ctx {
  52. unsigned int cryptlen;
  53. struct scatterlist *src;
  54. void (*complete)(struct aead_request *req, int err);
  55. };
  56. struct crypto_gcm_req_priv_ctx {
  57. u8 auth_tag[16];
  58. u8 iauth_tag[16];
  59. struct scatterlist src[2];
  60. struct scatterlist dst[2];
  61. struct crypto_gcm_ghash_ctx ghash_ctx;
  62. union {
  63. struct ahash_request ahreq;
  64. struct ablkcipher_request abreq;
  65. } u;
  66. };
  67. struct crypto_gcm_setkey_result {
  68. int err;
  69. struct completion completion;
  70. };
  71. static void *gcm_zeroes;
  72. static inline struct crypto_gcm_req_priv_ctx *crypto_gcm_reqctx(
  73. struct aead_request *req)
  74. {
  75. unsigned long align = crypto_aead_alignmask(crypto_aead_reqtfm(req));
  76. return (void *)PTR_ALIGN((u8 *)aead_request_ctx(req), align + 1);
  77. }
  78. static void crypto_gcm_setkey_done(struct crypto_async_request *req, int err)
  79. {
  80. struct crypto_gcm_setkey_result *result = req->data;
  81. if (err == -EINPROGRESS)
  82. return;
  83. result->err = err;
  84. complete(&result->completion);
  85. }
  86. static int crypto_gcm_setkey(struct crypto_aead *aead, const u8 *key,
  87. unsigned int keylen)
  88. {
  89. struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
  90. struct crypto_ahash *ghash = ctx->ghash;
  91. struct crypto_ablkcipher *ctr = ctx->ctr;
  92. struct {
  93. be128 hash;
  94. u8 iv[8];
  95. struct crypto_gcm_setkey_result result;
  96. struct scatterlist sg[1];
  97. struct ablkcipher_request req;
  98. } *data;
  99. int err;
  100. crypto_ablkcipher_clear_flags(ctr, CRYPTO_TFM_REQ_MASK);
  101. crypto_ablkcipher_set_flags(ctr, crypto_aead_get_flags(aead) &
  102. CRYPTO_TFM_REQ_MASK);
  103. err = crypto_ablkcipher_setkey(ctr, key, keylen);
  104. if (err)
  105. return err;
  106. crypto_aead_set_flags(aead, crypto_ablkcipher_get_flags(ctr) &
  107. CRYPTO_TFM_RES_MASK);
  108. data = kzalloc(sizeof(*data) + crypto_ablkcipher_reqsize(ctr),
  109. GFP_KERNEL);
  110. if (!data)
  111. return -ENOMEM;
  112. init_completion(&data->result.completion);
  113. sg_init_one(data->sg, &data->hash, sizeof(data->hash));
  114. ablkcipher_request_set_tfm(&data->req, ctr);
  115. ablkcipher_request_set_callback(&data->req, CRYPTO_TFM_REQ_MAY_SLEEP |
  116. CRYPTO_TFM_REQ_MAY_BACKLOG,
  117. crypto_gcm_setkey_done,
  118. &data->result);
  119. ablkcipher_request_set_crypt(&data->req, data->sg, data->sg,
  120. sizeof(data->hash), data->iv);
  121. err = crypto_ablkcipher_encrypt(&data->req);
  122. if (err == -EINPROGRESS || err == -EBUSY) {
  123. err = wait_for_completion_interruptible(
  124. &data->result.completion);
  125. if (!err)
  126. err = data->result.err;
  127. }
  128. if (err)
  129. goto out;
  130. crypto_ahash_clear_flags(ghash, CRYPTO_TFM_REQ_MASK);
  131. crypto_ahash_set_flags(ghash, crypto_aead_get_flags(aead) &
  132. CRYPTO_TFM_REQ_MASK);
  133. err = crypto_ahash_setkey(ghash, (u8 *)&data->hash, sizeof(be128));
  134. crypto_aead_set_flags(aead, crypto_ahash_get_flags(ghash) &
  135. CRYPTO_TFM_RES_MASK);
  136. out:
  137. kfree(data);
  138. return err;
  139. }
  140. static int crypto_gcm_setauthsize(struct crypto_aead *tfm,
  141. unsigned int authsize)
  142. {
  143. switch (authsize) {
  144. case 4:
  145. case 8:
  146. case 12:
  147. case 13:
  148. case 14:
  149. case 15:
  150. case 16:
  151. break;
  152. default:
  153. return -EINVAL;
  154. }
  155. return 0;
  156. }
  157. static void crypto_gcm_init_crypt(struct ablkcipher_request *ablk_req,
  158. struct aead_request *req,
  159. unsigned int cryptlen)
  160. {
  161. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  162. struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
  163. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  164. struct scatterlist *dst;
  165. __be32 counter = cpu_to_be32(1);
  166. memset(pctx->auth_tag, 0, sizeof(pctx->auth_tag));
  167. memcpy(req->iv + 12, &counter, 4);
  168. sg_init_table(pctx->src, 2);
  169. sg_set_buf(pctx->src, pctx->auth_tag, sizeof(pctx->auth_tag));
  170. scatterwalk_sg_chain(pctx->src, 2, req->src);
  171. dst = pctx->src;
  172. if (req->src != req->dst) {
  173. sg_init_table(pctx->dst, 2);
  174. sg_set_buf(pctx->dst, pctx->auth_tag, sizeof(pctx->auth_tag));
  175. scatterwalk_sg_chain(pctx->dst, 2, req->dst);
  176. dst = pctx->dst;
  177. }
  178. ablkcipher_request_set_tfm(ablk_req, ctx->ctr);
  179. ablkcipher_request_set_crypt(ablk_req, pctx->src, dst,
  180. cryptlen + sizeof(pctx->auth_tag),
  181. req->iv);
  182. }
  183. static inline unsigned int gcm_remain(unsigned int len)
  184. {
  185. len &= 0xfU;
  186. return len ? 16 - len : 0;
  187. }
  188. static void gcm_hash_len_done(struct crypto_async_request *areq, int err);
  189. static void gcm_hash_final_done(struct crypto_async_request *areq, int err);
  190. static int gcm_hash_update(struct aead_request *req,
  191. struct crypto_gcm_req_priv_ctx *pctx,
  192. crypto_completion_t complete,
  193. struct scatterlist *src,
  194. unsigned int len)
  195. {
  196. struct ahash_request *ahreq = &pctx->u.ahreq;
  197. ahash_request_set_callback(ahreq, aead_request_flags(req),
  198. complete, req);
  199. ahash_request_set_crypt(ahreq, src, NULL, len);
  200. return crypto_ahash_update(ahreq);
  201. }
  202. static int gcm_hash_remain(struct aead_request *req,
  203. struct crypto_gcm_req_priv_ctx *pctx,
  204. unsigned int remain,
  205. crypto_completion_t complete)
  206. {
  207. struct ahash_request *ahreq = &pctx->u.ahreq;
  208. ahash_request_set_callback(ahreq, aead_request_flags(req),
  209. complete, req);
  210. sg_init_one(pctx->src, gcm_zeroes, remain);
  211. ahash_request_set_crypt(ahreq, pctx->src, NULL, remain);
  212. return crypto_ahash_update(ahreq);
  213. }
  214. static int gcm_hash_len(struct aead_request *req,
  215. struct crypto_gcm_req_priv_ctx *pctx)
  216. {
  217. struct ahash_request *ahreq = &pctx->u.ahreq;
  218. struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
  219. u128 lengths;
  220. lengths.a = cpu_to_be64(req->assoclen * 8);
  221. lengths.b = cpu_to_be64(gctx->cryptlen * 8);
  222. memcpy(pctx->iauth_tag, &lengths, 16);
  223. sg_init_one(pctx->src, pctx->iauth_tag, 16);
  224. ahash_request_set_callback(ahreq, aead_request_flags(req),
  225. gcm_hash_len_done, req);
  226. ahash_request_set_crypt(ahreq, pctx->src,
  227. NULL, sizeof(lengths));
  228. return crypto_ahash_update(ahreq);
  229. }
  230. static int gcm_hash_final(struct aead_request *req,
  231. struct crypto_gcm_req_priv_ctx *pctx)
  232. {
  233. struct ahash_request *ahreq = &pctx->u.ahreq;
  234. ahash_request_set_callback(ahreq, aead_request_flags(req),
  235. gcm_hash_final_done, req);
  236. ahash_request_set_crypt(ahreq, NULL, pctx->iauth_tag, 0);
  237. return crypto_ahash_final(ahreq);
  238. }
  239. static void __gcm_hash_final_done(struct aead_request *req, int err)
  240. {
  241. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  242. struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
  243. if (!err)
  244. crypto_xor(pctx->auth_tag, pctx->iauth_tag, 16);
  245. gctx->complete(req, err);
  246. }
  247. static void gcm_hash_final_done(struct crypto_async_request *areq, int err)
  248. {
  249. struct aead_request *req = areq->data;
  250. __gcm_hash_final_done(req, err);
  251. }
  252. static void __gcm_hash_len_done(struct aead_request *req, int err)
  253. {
  254. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  255. if (!err) {
  256. err = gcm_hash_final(req, pctx);
  257. if (err == -EINPROGRESS || err == -EBUSY)
  258. return;
  259. }
  260. __gcm_hash_final_done(req, err);
  261. }
  262. static void gcm_hash_len_done(struct crypto_async_request *areq, int err)
  263. {
  264. struct aead_request *req = areq->data;
  265. __gcm_hash_len_done(req, err);
  266. }
  267. static void __gcm_hash_crypt_remain_done(struct aead_request *req, int err)
  268. {
  269. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  270. if (!err) {
  271. err = gcm_hash_len(req, pctx);
  272. if (err == -EINPROGRESS || err == -EBUSY)
  273. return;
  274. }
  275. __gcm_hash_len_done(req, err);
  276. }
  277. static void gcm_hash_crypt_remain_done(struct crypto_async_request *areq,
  278. int err)
  279. {
  280. struct aead_request *req = areq->data;
  281. __gcm_hash_crypt_remain_done(req, err);
  282. }
  283. static void __gcm_hash_crypt_done(struct aead_request *req, int err)
  284. {
  285. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  286. struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
  287. unsigned int remain;
  288. if (!err) {
  289. remain = gcm_remain(gctx->cryptlen);
  290. BUG_ON(!remain);
  291. err = gcm_hash_remain(req, pctx, remain,
  292. gcm_hash_crypt_remain_done);
  293. if (err == -EINPROGRESS || err == -EBUSY)
  294. return;
  295. }
  296. __gcm_hash_crypt_remain_done(req, err);
  297. }
  298. static void gcm_hash_crypt_done(struct crypto_async_request *areq, int err)
  299. {
  300. struct aead_request *req = areq->data;
  301. __gcm_hash_crypt_done(req, err);
  302. }
  303. static void __gcm_hash_assoc_remain_done(struct aead_request *req, int err)
  304. {
  305. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  306. struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
  307. crypto_completion_t complete;
  308. unsigned int remain = 0;
  309. if (!err && gctx->cryptlen) {
  310. remain = gcm_remain(gctx->cryptlen);
  311. complete = remain ? gcm_hash_crypt_done :
  312. gcm_hash_crypt_remain_done;
  313. err = gcm_hash_update(req, pctx, complete,
  314. gctx->src, gctx->cryptlen);
  315. if (err == -EINPROGRESS || err == -EBUSY)
  316. return;
  317. }
  318. if (remain)
  319. __gcm_hash_crypt_done(req, err);
  320. else
  321. __gcm_hash_crypt_remain_done(req, err);
  322. }
  323. static void gcm_hash_assoc_remain_done(struct crypto_async_request *areq,
  324. int err)
  325. {
  326. struct aead_request *req = areq->data;
  327. __gcm_hash_assoc_remain_done(req, err);
  328. }
  329. static void __gcm_hash_assoc_done(struct aead_request *req, int err)
  330. {
  331. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  332. unsigned int remain;
  333. if (!err) {
  334. remain = gcm_remain(req->assoclen);
  335. BUG_ON(!remain);
  336. err = gcm_hash_remain(req, pctx, remain,
  337. gcm_hash_assoc_remain_done);
  338. if (err == -EINPROGRESS || err == -EBUSY)
  339. return;
  340. }
  341. __gcm_hash_assoc_remain_done(req, err);
  342. }
  343. static void gcm_hash_assoc_done(struct crypto_async_request *areq, int err)
  344. {
  345. struct aead_request *req = areq->data;
  346. __gcm_hash_assoc_done(req, err);
  347. }
  348. static void __gcm_hash_init_done(struct aead_request *req, int err)
  349. {
  350. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  351. crypto_completion_t complete;
  352. unsigned int remain = 0;
  353. if (!err && req->assoclen) {
  354. remain = gcm_remain(req->assoclen);
  355. complete = remain ? gcm_hash_assoc_done :
  356. gcm_hash_assoc_remain_done;
  357. err = gcm_hash_update(req, pctx, complete,
  358. req->assoc, req->assoclen);
  359. if (err == -EINPROGRESS || err == -EBUSY)
  360. return;
  361. }
  362. if (remain)
  363. __gcm_hash_assoc_done(req, err);
  364. else
  365. __gcm_hash_assoc_remain_done(req, err);
  366. }
  367. static void gcm_hash_init_done(struct crypto_async_request *areq, int err)
  368. {
  369. struct aead_request *req = areq->data;
  370. __gcm_hash_init_done(req, err);
  371. }
  372. static int gcm_hash(struct aead_request *req,
  373. struct crypto_gcm_req_priv_ctx *pctx)
  374. {
  375. struct ahash_request *ahreq = &pctx->u.ahreq;
  376. struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
  377. struct crypto_gcm_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  378. unsigned int remain;
  379. crypto_completion_t complete;
  380. int err;
  381. ahash_request_set_tfm(ahreq, ctx->ghash);
  382. ahash_request_set_callback(ahreq, aead_request_flags(req),
  383. gcm_hash_init_done, req);
  384. err = crypto_ahash_init(ahreq);
  385. if (err)
  386. return err;
  387. remain = gcm_remain(req->assoclen);
  388. complete = remain ? gcm_hash_assoc_done : gcm_hash_assoc_remain_done;
  389. err = gcm_hash_update(req, pctx, complete, req->assoc, req->assoclen);
  390. if (err)
  391. return err;
  392. if (remain) {
  393. err = gcm_hash_remain(req, pctx, remain,
  394. gcm_hash_assoc_remain_done);
  395. if (err)
  396. return err;
  397. }
  398. remain = gcm_remain(gctx->cryptlen);
  399. complete = remain ? gcm_hash_crypt_done : gcm_hash_crypt_remain_done;
  400. err = gcm_hash_update(req, pctx, complete, gctx->src, gctx->cryptlen);
  401. if (err)
  402. return err;
  403. if (remain) {
  404. err = gcm_hash_remain(req, pctx, remain,
  405. gcm_hash_crypt_remain_done);
  406. if (err)
  407. return err;
  408. }
  409. err = gcm_hash_len(req, pctx);
  410. if (err)
  411. return err;
  412. err = gcm_hash_final(req, pctx);
  413. if (err)
  414. return err;
  415. return 0;
  416. }
  417. static void gcm_enc_copy_hash(struct aead_request *req,
  418. struct crypto_gcm_req_priv_ctx *pctx)
  419. {
  420. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  421. u8 *auth_tag = pctx->auth_tag;
  422. scatterwalk_map_and_copy(auth_tag, req->dst, req->cryptlen,
  423. crypto_aead_authsize(aead), 1);
  424. }
  425. static void gcm_enc_hash_done(struct aead_request *req, int err)
  426. {
  427. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  428. if (!err)
  429. gcm_enc_copy_hash(req, pctx);
  430. aead_request_complete(req, err);
  431. }
  432. static void gcm_encrypt_done(struct crypto_async_request *areq, int err)
  433. {
  434. struct aead_request *req = areq->data;
  435. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  436. if (!err) {
  437. err = gcm_hash(req, pctx);
  438. if (err == -EINPROGRESS || err == -EBUSY)
  439. return;
  440. else if (!err) {
  441. crypto_xor(pctx->auth_tag, pctx->iauth_tag, 16);
  442. gcm_enc_copy_hash(req, pctx);
  443. }
  444. }
  445. aead_request_complete(req, err);
  446. }
  447. static int crypto_gcm_encrypt(struct aead_request *req)
  448. {
  449. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  450. struct ablkcipher_request *abreq = &pctx->u.abreq;
  451. struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
  452. int err;
  453. crypto_gcm_init_crypt(abreq, req, req->cryptlen);
  454. ablkcipher_request_set_callback(abreq, aead_request_flags(req),
  455. gcm_encrypt_done, req);
  456. gctx->src = req->dst;
  457. gctx->cryptlen = req->cryptlen;
  458. gctx->complete = gcm_enc_hash_done;
  459. err = crypto_ablkcipher_encrypt(abreq);
  460. if (err)
  461. return err;
  462. err = gcm_hash(req, pctx);
  463. if (err)
  464. return err;
  465. crypto_xor(pctx->auth_tag, pctx->iauth_tag, 16);
  466. gcm_enc_copy_hash(req, pctx);
  467. return 0;
  468. }
  469. static int crypto_gcm_verify(struct aead_request *req,
  470. struct crypto_gcm_req_priv_ctx *pctx)
  471. {
  472. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  473. u8 *auth_tag = pctx->auth_tag;
  474. u8 *iauth_tag = pctx->iauth_tag;
  475. unsigned int authsize = crypto_aead_authsize(aead);
  476. unsigned int cryptlen = req->cryptlen - authsize;
  477. crypto_xor(auth_tag, iauth_tag, 16);
  478. scatterwalk_map_and_copy(iauth_tag, req->src, cryptlen, authsize, 0);
  479. return memcmp(iauth_tag, auth_tag, authsize) ? -EBADMSG : 0;
  480. }
  481. static void gcm_decrypt_done(struct crypto_async_request *areq, int err)
  482. {
  483. struct aead_request *req = areq->data;
  484. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  485. if (!err)
  486. err = crypto_gcm_verify(req, pctx);
  487. aead_request_complete(req, err);
  488. }
  489. static void gcm_dec_hash_done(struct aead_request *req, int err)
  490. {
  491. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  492. struct ablkcipher_request *abreq = &pctx->u.abreq;
  493. struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
  494. if (!err) {
  495. ablkcipher_request_set_callback(abreq, aead_request_flags(req),
  496. gcm_decrypt_done, req);
  497. crypto_gcm_init_crypt(abreq, req, gctx->cryptlen);
  498. err = crypto_ablkcipher_decrypt(abreq);
  499. if (err == -EINPROGRESS || err == -EBUSY)
  500. return;
  501. else if (!err)
  502. err = crypto_gcm_verify(req, pctx);
  503. }
  504. aead_request_complete(req, err);
  505. }
  506. static int crypto_gcm_decrypt(struct aead_request *req)
  507. {
  508. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  509. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  510. struct ablkcipher_request *abreq = &pctx->u.abreq;
  511. struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
  512. unsigned int authsize = crypto_aead_authsize(aead);
  513. unsigned int cryptlen = req->cryptlen;
  514. int err;
  515. if (cryptlen < authsize)
  516. return -EINVAL;
  517. cryptlen -= authsize;
  518. gctx->src = req->src;
  519. gctx->cryptlen = cryptlen;
  520. gctx->complete = gcm_dec_hash_done;
  521. err = gcm_hash(req, pctx);
  522. if (err)
  523. return err;
  524. ablkcipher_request_set_callback(abreq, aead_request_flags(req),
  525. gcm_decrypt_done, req);
  526. crypto_gcm_init_crypt(abreq, req, cryptlen);
  527. err = crypto_ablkcipher_decrypt(abreq);
  528. if (err)
  529. return err;
  530. return crypto_gcm_verify(req, pctx);
  531. }
  532. static int crypto_gcm_init_tfm(struct crypto_tfm *tfm)
  533. {
  534. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  535. struct gcm_instance_ctx *ictx = crypto_instance_ctx(inst);
  536. struct crypto_gcm_ctx *ctx = crypto_tfm_ctx(tfm);
  537. struct crypto_ablkcipher *ctr;
  538. struct crypto_ahash *ghash;
  539. unsigned long align;
  540. int err;
  541. ghash = crypto_spawn_ahash(&ictx->ghash);
  542. if (IS_ERR(ghash))
  543. return PTR_ERR(ghash);
  544. ctr = crypto_spawn_skcipher(&ictx->ctr);
  545. err = PTR_ERR(ctr);
  546. if (IS_ERR(ctr))
  547. goto err_free_hash;
  548. ctx->ctr = ctr;
  549. ctx->ghash = ghash;
  550. align = crypto_tfm_alg_alignmask(tfm);
  551. align &= ~(crypto_tfm_ctx_alignment() - 1);
  552. tfm->crt_aead.reqsize = align +
  553. offsetof(struct crypto_gcm_req_priv_ctx, u) +
  554. max(sizeof(struct ablkcipher_request) +
  555. crypto_ablkcipher_reqsize(ctr),
  556. sizeof(struct ahash_request) +
  557. crypto_ahash_reqsize(ghash));
  558. return 0;
  559. err_free_hash:
  560. crypto_free_ahash(ghash);
  561. return err;
  562. }
  563. static void crypto_gcm_exit_tfm(struct crypto_tfm *tfm)
  564. {
  565. struct crypto_gcm_ctx *ctx = crypto_tfm_ctx(tfm);
  566. crypto_free_ahash(ctx->ghash);
  567. crypto_free_ablkcipher(ctx->ctr);
  568. }
  569. static struct crypto_instance *crypto_gcm_alloc_common(struct rtattr **tb,
  570. const char *full_name,
  571. const char *ctr_name,
  572. const char *ghash_name)
  573. {
  574. struct crypto_attr_type *algt;
  575. struct crypto_instance *inst;
  576. struct crypto_alg *ctr;
  577. struct crypto_alg *ghash_alg;
  578. struct ahash_alg *ghash_ahash_alg;
  579. struct gcm_instance_ctx *ctx;
  580. int err;
  581. algt = crypto_get_attr_type(tb);
  582. if (IS_ERR(algt))
  583. return ERR_CAST(algt);
  584. if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
  585. return ERR_PTR(-EINVAL);
  586. ghash_alg = crypto_find_alg(ghash_name, &crypto_ahash_type,
  587. CRYPTO_ALG_TYPE_HASH,
  588. CRYPTO_ALG_TYPE_AHASH_MASK);
  589. if (IS_ERR(ghash_alg))
  590. return ERR_CAST(ghash_alg);
  591. err = -ENOMEM;
  592. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  593. if (!inst)
  594. goto out_put_ghash;
  595. ctx = crypto_instance_ctx(inst);
  596. ghash_ahash_alg = container_of(ghash_alg, struct ahash_alg, halg.base);
  597. err = crypto_init_ahash_spawn(&ctx->ghash, &ghash_ahash_alg->halg,
  598. inst);
  599. if (err)
  600. goto err_free_inst;
  601. crypto_set_skcipher_spawn(&ctx->ctr, inst);
  602. err = crypto_grab_skcipher(&ctx->ctr, ctr_name, 0,
  603. crypto_requires_sync(algt->type,
  604. algt->mask));
  605. if (err)
  606. goto err_drop_ghash;
  607. ctr = crypto_skcipher_spawn_alg(&ctx->ctr);
  608. /* We only support 16-byte blocks. */
  609. if (ctr->cra_ablkcipher.ivsize != 16)
  610. goto out_put_ctr;
  611. /* Not a stream cipher? */
  612. err = -EINVAL;
  613. if (ctr->cra_blocksize != 1)
  614. goto out_put_ctr;
  615. err = -ENAMETOOLONG;
  616. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  617. "gcm_base(%s,%s)", ctr->cra_driver_name,
  618. ghash_alg->cra_driver_name) >=
  619. CRYPTO_MAX_ALG_NAME)
  620. goto out_put_ctr;
  621. memcpy(inst->alg.cra_name, full_name, CRYPTO_MAX_ALG_NAME);
  622. inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD;
  623. inst->alg.cra_flags |= ctr->cra_flags & CRYPTO_ALG_ASYNC;
  624. inst->alg.cra_priority = ctr->cra_priority;
  625. inst->alg.cra_blocksize = 1;
  626. inst->alg.cra_alignmask = ctr->cra_alignmask | (__alignof__(u64) - 1);
  627. inst->alg.cra_type = &crypto_aead_type;
  628. inst->alg.cra_aead.ivsize = 16;
  629. inst->alg.cra_aead.maxauthsize = 16;
  630. inst->alg.cra_ctxsize = sizeof(struct crypto_gcm_ctx);
  631. inst->alg.cra_init = crypto_gcm_init_tfm;
  632. inst->alg.cra_exit = crypto_gcm_exit_tfm;
  633. inst->alg.cra_aead.setkey = crypto_gcm_setkey;
  634. inst->alg.cra_aead.setauthsize = crypto_gcm_setauthsize;
  635. inst->alg.cra_aead.encrypt = crypto_gcm_encrypt;
  636. inst->alg.cra_aead.decrypt = crypto_gcm_decrypt;
  637. out:
  638. crypto_mod_put(ghash_alg);
  639. return inst;
  640. out_put_ctr:
  641. crypto_drop_skcipher(&ctx->ctr);
  642. err_drop_ghash:
  643. crypto_drop_ahash(&ctx->ghash);
  644. err_free_inst:
  645. kfree(inst);
  646. out_put_ghash:
  647. inst = ERR_PTR(err);
  648. goto out;
  649. }
  650. static struct crypto_instance *crypto_gcm_alloc(struct rtattr **tb)
  651. {
  652. const char *cipher_name;
  653. char ctr_name[CRYPTO_MAX_ALG_NAME];
  654. char full_name[CRYPTO_MAX_ALG_NAME];
  655. cipher_name = crypto_attr_alg_name(tb[1]);
  656. if (IS_ERR(cipher_name))
  657. return ERR_CAST(cipher_name);
  658. if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)", cipher_name) >=
  659. CRYPTO_MAX_ALG_NAME)
  660. return ERR_PTR(-ENAMETOOLONG);
  661. if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "gcm(%s)", cipher_name) >=
  662. CRYPTO_MAX_ALG_NAME)
  663. return ERR_PTR(-ENAMETOOLONG);
  664. return crypto_gcm_alloc_common(tb, full_name, ctr_name, "ghash");
  665. }
  666. static void crypto_gcm_free(struct crypto_instance *inst)
  667. {
  668. struct gcm_instance_ctx *ctx = crypto_instance_ctx(inst);
  669. crypto_drop_skcipher(&ctx->ctr);
  670. crypto_drop_ahash(&ctx->ghash);
  671. kfree(inst);
  672. }
  673. static struct crypto_template crypto_gcm_tmpl = {
  674. .name = "gcm",
  675. .alloc = crypto_gcm_alloc,
  676. .free = crypto_gcm_free,
  677. .module = THIS_MODULE,
  678. };
  679. static struct crypto_instance *crypto_gcm_base_alloc(struct rtattr **tb)
  680. {
  681. const char *ctr_name;
  682. const char *ghash_name;
  683. char full_name[CRYPTO_MAX_ALG_NAME];
  684. ctr_name = crypto_attr_alg_name(tb[1]);
  685. if (IS_ERR(ctr_name))
  686. return ERR_CAST(ctr_name);
  687. ghash_name = crypto_attr_alg_name(tb[2]);
  688. if (IS_ERR(ghash_name))
  689. return ERR_CAST(ghash_name);
  690. if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "gcm_base(%s,%s)",
  691. ctr_name, ghash_name) >= CRYPTO_MAX_ALG_NAME)
  692. return ERR_PTR(-ENAMETOOLONG);
  693. return crypto_gcm_alloc_common(tb, full_name, ctr_name, ghash_name);
  694. }
  695. static struct crypto_template crypto_gcm_base_tmpl = {
  696. .name = "gcm_base",
  697. .alloc = crypto_gcm_base_alloc,
  698. .free = crypto_gcm_free,
  699. .module = THIS_MODULE,
  700. };
  701. static int crypto_rfc4106_setkey(struct crypto_aead *parent, const u8 *key,
  702. unsigned int keylen)
  703. {
  704. struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
  705. struct crypto_aead *child = ctx->child;
  706. int err;
  707. if (keylen < 4)
  708. return -EINVAL;
  709. keylen -= 4;
  710. memcpy(ctx->nonce, key + keylen, 4);
  711. crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  712. crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
  713. CRYPTO_TFM_REQ_MASK);
  714. err = crypto_aead_setkey(child, key, keylen);
  715. crypto_aead_set_flags(parent, crypto_aead_get_flags(child) &
  716. CRYPTO_TFM_RES_MASK);
  717. return err;
  718. }
  719. static int crypto_rfc4106_setauthsize(struct crypto_aead *parent,
  720. unsigned int authsize)
  721. {
  722. struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
  723. switch (authsize) {
  724. case 8:
  725. case 12:
  726. case 16:
  727. break;
  728. default:
  729. return -EINVAL;
  730. }
  731. return crypto_aead_setauthsize(ctx->child, authsize);
  732. }
  733. static struct aead_request *crypto_rfc4106_crypt(struct aead_request *req)
  734. {
  735. struct aead_request *subreq = aead_request_ctx(req);
  736. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  737. struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(aead);
  738. struct crypto_aead *child = ctx->child;
  739. u8 *iv = PTR_ALIGN((u8 *)(subreq + 1) + crypto_aead_reqsize(child),
  740. crypto_aead_alignmask(child) + 1);
  741. memcpy(iv, ctx->nonce, 4);
  742. memcpy(iv + 4, req->iv, 8);
  743. aead_request_set_tfm(subreq, child);
  744. aead_request_set_callback(subreq, req->base.flags, req->base.complete,
  745. req->base.data);
  746. aead_request_set_crypt(subreq, req->src, req->dst, req->cryptlen, iv);
  747. aead_request_set_assoc(subreq, req->assoc, req->assoclen);
  748. return subreq;
  749. }
  750. static int crypto_rfc4106_encrypt(struct aead_request *req)
  751. {
  752. req = crypto_rfc4106_crypt(req);
  753. return crypto_aead_encrypt(req);
  754. }
  755. static int crypto_rfc4106_decrypt(struct aead_request *req)
  756. {
  757. req = crypto_rfc4106_crypt(req);
  758. return crypto_aead_decrypt(req);
  759. }
  760. static int crypto_rfc4106_init_tfm(struct crypto_tfm *tfm)
  761. {
  762. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  763. struct crypto_aead_spawn *spawn = crypto_instance_ctx(inst);
  764. struct crypto_rfc4106_ctx *ctx = crypto_tfm_ctx(tfm);
  765. struct crypto_aead *aead;
  766. unsigned long align;
  767. aead = crypto_spawn_aead(spawn);
  768. if (IS_ERR(aead))
  769. return PTR_ERR(aead);
  770. ctx->child = aead;
  771. align = crypto_aead_alignmask(aead);
  772. align &= ~(crypto_tfm_ctx_alignment() - 1);
  773. tfm->crt_aead.reqsize = sizeof(struct aead_request) +
  774. ALIGN(crypto_aead_reqsize(aead),
  775. crypto_tfm_ctx_alignment()) +
  776. align + 16;
  777. return 0;
  778. }
  779. static void crypto_rfc4106_exit_tfm(struct crypto_tfm *tfm)
  780. {
  781. struct crypto_rfc4106_ctx *ctx = crypto_tfm_ctx(tfm);
  782. crypto_free_aead(ctx->child);
  783. }
  784. static struct crypto_instance *crypto_rfc4106_alloc(struct rtattr **tb)
  785. {
  786. struct crypto_attr_type *algt;
  787. struct crypto_instance *inst;
  788. struct crypto_aead_spawn *spawn;
  789. struct crypto_alg *alg;
  790. const char *ccm_name;
  791. int err;
  792. algt = crypto_get_attr_type(tb);
  793. if (IS_ERR(algt))
  794. return ERR_CAST(algt);
  795. if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
  796. return ERR_PTR(-EINVAL);
  797. ccm_name = crypto_attr_alg_name(tb[1]);
  798. if (IS_ERR(ccm_name))
  799. return ERR_CAST(ccm_name);
  800. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  801. if (!inst)
  802. return ERR_PTR(-ENOMEM);
  803. spawn = crypto_instance_ctx(inst);
  804. crypto_set_aead_spawn(spawn, inst);
  805. err = crypto_grab_aead(spawn, ccm_name, 0,
  806. crypto_requires_sync(algt->type, algt->mask));
  807. if (err)
  808. goto out_free_inst;
  809. alg = crypto_aead_spawn_alg(spawn);
  810. err = -EINVAL;
  811. /* We only support 16-byte blocks. */
  812. if (alg->cra_aead.ivsize != 16)
  813. goto out_drop_alg;
  814. /* Not a stream cipher? */
  815. if (alg->cra_blocksize != 1)
  816. goto out_drop_alg;
  817. err = -ENAMETOOLONG;
  818. if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
  819. "rfc4106(%s)", alg->cra_name) >= CRYPTO_MAX_ALG_NAME ||
  820. snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  821. "rfc4106(%s)", alg->cra_driver_name) >=
  822. CRYPTO_MAX_ALG_NAME)
  823. goto out_drop_alg;
  824. inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD;
  825. inst->alg.cra_flags |= alg->cra_flags & CRYPTO_ALG_ASYNC;
  826. inst->alg.cra_priority = alg->cra_priority;
  827. inst->alg.cra_blocksize = 1;
  828. inst->alg.cra_alignmask = alg->cra_alignmask;
  829. inst->alg.cra_type = &crypto_nivaead_type;
  830. inst->alg.cra_aead.ivsize = 8;
  831. inst->alg.cra_aead.maxauthsize = 16;
  832. inst->alg.cra_ctxsize = sizeof(struct crypto_rfc4106_ctx);
  833. inst->alg.cra_init = crypto_rfc4106_init_tfm;
  834. inst->alg.cra_exit = crypto_rfc4106_exit_tfm;
  835. inst->alg.cra_aead.setkey = crypto_rfc4106_setkey;
  836. inst->alg.cra_aead.setauthsize = crypto_rfc4106_setauthsize;
  837. inst->alg.cra_aead.encrypt = crypto_rfc4106_encrypt;
  838. inst->alg.cra_aead.decrypt = crypto_rfc4106_decrypt;
  839. inst->alg.cra_aead.geniv = "seqiv";
  840. out:
  841. return inst;
  842. out_drop_alg:
  843. crypto_drop_aead(spawn);
  844. out_free_inst:
  845. kfree(inst);
  846. inst = ERR_PTR(err);
  847. goto out;
  848. }
  849. static void crypto_rfc4106_free(struct crypto_instance *inst)
  850. {
  851. crypto_drop_spawn(crypto_instance_ctx(inst));
  852. kfree(inst);
  853. }
  854. static struct crypto_template crypto_rfc4106_tmpl = {
  855. .name = "rfc4106",
  856. .alloc = crypto_rfc4106_alloc,
  857. .free = crypto_rfc4106_free,
  858. .module = THIS_MODULE,
  859. };
  860. static inline struct crypto_rfc4543_req_ctx *crypto_rfc4543_reqctx(
  861. struct aead_request *req)
  862. {
  863. unsigned long align = crypto_aead_alignmask(crypto_aead_reqtfm(req));
  864. return (void *)PTR_ALIGN((u8 *)aead_request_ctx(req), align + 1);
  865. }
  866. static int crypto_rfc4543_setkey(struct crypto_aead *parent, const u8 *key,
  867. unsigned int keylen)
  868. {
  869. struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent);
  870. struct crypto_aead *child = ctx->child;
  871. int err;
  872. if (keylen < 4)
  873. return -EINVAL;
  874. keylen -= 4;
  875. memcpy(ctx->nonce, key + keylen, 4);
  876. crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  877. crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
  878. CRYPTO_TFM_REQ_MASK);
  879. err = crypto_aead_setkey(child, key, keylen);
  880. crypto_aead_set_flags(parent, crypto_aead_get_flags(child) &
  881. CRYPTO_TFM_RES_MASK);
  882. return err;
  883. }
  884. static int crypto_rfc4543_setauthsize(struct crypto_aead *parent,
  885. unsigned int authsize)
  886. {
  887. struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent);
  888. if (authsize != 16)
  889. return -EINVAL;
  890. return crypto_aead_setauthsize(ctx->child, authsize);
  891. }
  892. static void crypto_rfc4543_done(struct crypto_async_request *areq, int err)
  893. {
  894. struct aead_request *req = areq->data;
  895. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  896. struct crypto_rfc4543_req_ctx *rctx = crypto_rfc4543_reqctx(req);
  897. if (!err) {
  898. scatterwalk_map_and_copy(rctx->auth_tag, req->dst,
  899. req->cryptlen,
  900. crypto_aead_authsize(aead), 1);
  901. }
  902. aead_request_complete(req, err);
  903. }
  904. static struct aead_request *crypto_rfc4543_crypt(struct aead_request *req,
  905. bool enc)
  906. {
  907. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  908. struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
  909. struct crypto_rfc4543_req_ctx *rctx = crypto_rfc4543_reqctx(req);
  910. struct aead_request *subreq = &rctx->subreq;
  911. struct scatterlist *src = req->src;
  912. struct scatterlist *cipher = rctx->cipher;
  913. struct scatterlist *payload = rctx->payload;
  914. struct scatterlist *assoc = rctx->assoc;
  915. unsigned int authsize = crypto_aead_authsize(aead);
  916. unsigned int assoclen = req->assoclen;
  917. struct page *srcp;
  918. u8 *vsrc;
  919. u8 *iv = PTR_ALIGN((u8 *)(rctx + 1) + crypto_aead_reqsize(ctx->child),
  920. crypto_aead_alignmask(ctx->child) + 1);
  921. memcpy(iv, ctx->nonce, 4);
  922. memcpy(iv + 4, req->iv, 8);
  923. /* construct cipher/plaintext */
  924. if (enc)
  925. memset(rctx->auth_tag, 0, authsize);
  926. else
  927. scatterwalk_map_and_copy(rctx->auth_tag, src,
  928. req->cryptlen - authsize,
  929. authsize, 0);
  930. sg_init_one(cipher, rctx->auth_tag, authsize);
  931. /* construct the aad */
  932. srcp = sg_page(src);
  933. vsrc = PageHighMem(srcp) ? NULL : page_address(srcp) + src->offset;
  934. sg_init_table(payload, 2);
  935. sg_set_buf(payload, req->iv, 8);
  936. scatterwalk_crypto_chain(payload, src, vsrc == req->iv + 8, 2);
  937. assoclen += 8 + req->cryptlen - (enc ? 0 : authsize);
  938. sg_init_table(assoc, 2);
  939. sg_set_page(assoc, sg_page(req->assoc), req->assoc->length,
  940. req->assoc->offset);
  941. scatterwalk_crypto_chain(assoc, payload, 0, 2);
  942. aead_request_set_tfm(subreq, ctx->child);
  943. aead_request_set_callback(subreq, req->base.flags, crypto_rfc4543_done,
  944. req);
  945. aead_request_set_crypt(subreq, cipher, cipher, enc ? 0 : authsize, iv);
  946. aead_request_set_assoc(subreq, assoc, assoclen);
  947. return subreq;
  948. }
  949. static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc)
  950. {
  951. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  952. struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
  953. unsigned int authsize = crypto_aead_authsize(aead);
  954. unsigned int nbytes = req->cryptlen - (enc ? 0 : authsize);
  955. struct blkcipher_desc desc = {
  956. .tfm = ctx->null,
  957. };
  958. return crypto_blkcipher_encrypt(&desc, req->dst, req->src, nbytes);
  959. }
  960. static int crypto_rfc4543_encrypt(struct aead_request *req)
  961. {
  962. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  963. struct crypto_rfc4543_req_ctx *rctx = crypto_rfc4543_reqctx(req);
  964. struct aead_request *subreq;
  965. int err;
  966. if (req->src != req->dst) {
  967. err = crypto_rfc4543_copy_src_to_dst(req, true);
  968. if (err)
  969. return err;
  970. }
  971. subreq = crypto_rfc4543_crypt(req, true);
  972. err = crypto_aead_encrypt(subreq);
  973. if (err)
  974. return err;
  975. scatterwalk_map_and_copy(rctx->auth_tag, req->dst, req->cryptlen,
  976. crypto_aead_authsize(aead), 1);
  977. return 0;
  978. }
  979. static int crypto_rfc4543_decrypt(struct aead_request *req)
  980. {
  981. int err;
  982. if (req->src != req->dst) {
  983. err = crypto_rfc4543_copy_src_to_dst(req, false);
  984. if (err)
  985. return err;
  986. }
  987. req = crypto_rfc4543_crypt(req, false);
  988. return crypto_aead_decrypt(req);
  989. }
  990. static int crypto_rfc4543_init_tfm(struct crypto_tfm *tfm)
  991. {
  992. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  993. struct crypto_rfc4543_instance_ctx *ictx = crypto_instance_ctx(inst);
  994. struct crypto_aead_spawn *spawn = &ictx->aead;
  995. struct crypto_rfc4543_ctx *ctx = crypto_tfm_ctx(tfm);
  996. struct crypto_aead *aead;
  997. struct crypto_blkcipher *null;
  998. unsigned long align;
  999. int err = 0;
  1000. aead = crypto_spawn_aead(spawn);
  1001. if (IS_ERR(aead))
  1002. return PTR_ERR(aead);
  1003. null = crypto_spawn_blkcipher(&ictx->null.base);
  1004. err = PTR_ERR(null);
  1005. if (IS_ERR(null))
  1006. goto err_free_aead;
  1007. ctx->child = aead;
  1008. ctx->null = null;
  1009. align = crypto_aead_alignmask(aead);
  1010. align &= ~(crypto_tfm_ctx_alignment() - 1);
  1011. tfm->crt_aead.reqsize = sizeof(struct crypto_rfc4543_req_ctx) +
  1012. ALIGN(crypto_aead_reqsize(aead),
  1013. crypto_tfm_ctx_alignment()) +
  1014. align + 16;
  1015. return 0;
  1016. err_free_aead:
  1017. crypto_free_aead(aead);
  1018. return err;
  1019. }
  1020. static void crypto_rfc4543_exit_tfm(struct crypto_tfm *tfm)
  1021. {
  1022. struct crypto_rfc4543_ctx *ctx = crypto_tfm_ctx(tfm);
  1023. crypto_free_aead(ctx->child);
  1024. crypto_free_blkcipher(ctx->null);
  1025. }
  1026. static struct crypto_instance *crypto_rfc4543_alloc(struct rtattr **tb)
  1027. {
  1028. struct crypto_attr_type *algt;
  1029. struct crypto_instance *inst;
  1030. struct crypto_aead_spawn *spawn;
  1031. struct crypto_alg *alg;
  1032. struct crypto_rfc4543_instance_ctx *ctx;
  1033. const char *ccm_name;
  1034. int err;
  1035. algt = crypto_get_attr_type(tb);
  1036. if (IS_ERR(algt))
  1037. return ERR_CAST(algt);
  1038. if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
  1039. return ERR_PTR(-EINVAL);
  1040. ccm_name = crypto_attr_alg_name(tb[1]);
  1041. if (IS_ERR(ccm_name))
  1042. return ERR_CAST(ccm_name);
  1043. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  1044. if (!inst)
  1045. return ERR_PTR(-ENOMEM);
  1046. ctx = crypto_instance_ctx(inst);
  1047. spawn = &ctx->aead;
  1048. crypto_set_aead_spawn(spawn, inst);
  1049. err = crypto_grab_aead(spawn, ccm_name, 0,
  1050. crypto_requires_sync(algt->type, algt->mask));
  1051. if (err)
  1052. goto out_free_inst;
  1053. alg = crypto_aead_spawn_alg(spawn);
  1054. crypto_set_skcipher_spawn(&ctx->null, inst);
  1055. err = crypto_grab_skcipher(&ctx->null, "ecb(cipher_null)", 0,
  1056. CRYPTO_ALG_ASYNC);
  1057. if (err)
  1058. goto out_drop_alg;
  1059. crypto_skcipher_spawn_alg(&ctx->null);
  1060. err = -EINVAL;
  1061. /* We only support 16-byte blocks. */
  1062. if (alg->cra_aead.ivsize != 16)
  1063. goto out_drop_ecbnull;
  1064. /* Not a stream cipher? */
  1065. if (alg->cra_blocksize != 1)
  1066. goto out_drop_ecbnull;
  1067. err = -ENAMETOOLONG;
  1068. if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
  1069. "rfc4543(%s)", alg->cra_name) >= CRYPTO_MAX_ALG_NAME ||
  1070. snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  1071. "rfc4543(%s)", alg->cra_driver_name) >=
  1072. CRYPTO_MAX_ALG_NAME)
  1073. goto out_drop_ecbnull;
  1074. inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD;
  1075. inst->alg.cra_flags |= alg->cra_flags & CRYPTO_ALG_ASYNC;
  1076. inst->alg.cra_priority = alg->cra_priority;
  1077. inst->alg.cra_blocksize = 1;
  1078. inst->alg.cra_alignmask = alg->cra_alignmask;
  1079. inst->alg.cra_type = &crypto_nivaead_type;
  1080. inst->alg.cra_aead.ivsize = 8;
  1081. inst->alg.cra_aead.maxauthsize = 16;
  1082. inst->alg.cra_ctxsize = sizeof(struct crypto_rfc4543_ctx);
  1083. inst->alg.cra_init = crypto_rfc4543_init_tfm;
  1084. inst->alg.cra_exit = crypto_rfc4543_exit_tfm;
  1085. inst->alg.cra_aead.setkey = crypto_rfc4543_setkey;
  1086. inst->alg.cra_aead.setauthsize = crypto_rfc4543_setauthsize;
  1087. inst->alg.cra_aead.encrypt = crypto_rfc4543_encrypt;
  1088. inst->alg.cra_aead.decrypt = crypto_rfc4543_decrypt;
  1089. inst->alg.cra_aead.geniv = "seqiv";
  1090. out:
  1091. return inst;
  1092. out_drop_ecbnull:
  1093. crypto_drop_skcipher(&ctx->null);
  1094. out_drop_alg:
  1095. crypto_drop_aead(spawn);
  1096. out_free_inst:
  1097. kfree(inst);
  1098. inst = ERR_PTR(err);
  1099. goto out;
  1100. }
  1101. static void crypto_rfc4543_free(struct crypto_instance *inst)
  1102. {
  1103. struct crypto_rfc4543_instance_ctx *ctx = crypto_instance_ctx(inst);
  1104. crypto_drop_aead(&ctx->aead);
  1105. crypto_drop_skcipher(&ctx->null);
  1106. kfree(inst);
  1107. }
  1108. static struct crypto_template crypto_rfc4543_tmpl = {
  1109. .name = "rfc4543",
  1110. .alloc = crypto_rfc4543_alloc,
  1111. .free = crypto_rfc4543_free,
  1112. .module = THIS_MODULE,
  1113. };
  1114. static int __init crypto_gcm_module_init(void)
  1115. {
  1116. int err;
  1117. gcm_zeroes = kzalloc(16, GFP_KERNEL);
  1118. if (!gcm_zeroes)
  1119. return -ENOMEM;
  1120. err = crypto_register_template(&crypto_gcm_base_tmpl);
  1121. if (err)
  1122. goto out;
  1123. err = crypto_register_template(&crypto_gcm_tmpl);
  1124. if (err)
  1125. goto out_undo_base;
  1126. err = crypto_register_template(&crypto_rfc4106_tmpl);
  1127. if (err)
  1128. goto out_undo_gcm;
  1129. err = crypto_register_template(&crypto_rfc4543_tmpl);
  1130. if (err)
  1131. goto out_undo_rfc4106;
  1132. return 0;
  1133. out_undo_rfc4106:
  1134. crypto_unregister_template(&crypto_rfc4106_tmpl);
  1135. out_undo_gcm:
  1136. crypto_unregister_template(&crypto_gcm_tmpl);
  1137. out_undo_base:
  1138. crypto_unregister_template(&crypto_gcm_base_tmpl);
  1139. out:
  1140. kfree(gcm_zeroes);
  1141. return err;
  1142. }
  1143. static void __exit crypto_gcm_module_exit(void)
  1144. {
  1145. kfree(gcm_zeroes);
  1146. crypto_unregister_template(&crypto_rfc4543_tmpl);
  1147. crypto_unregister_template(&crypto_rfc4106_tmpl);
  1148. crypto_unregister_template(&crypto_gcm_tmpl);
  1149. crypto_unregister_template(&crypto_gcm_base_tmpl);
  1150. }
  1151. module_init(crypto_gcm_module_init);
  1152. module_exit(crypto_gcm_module_exit);
  1153. MODULE_LICENSE("GPL");
  1154. MODULE_DESCRIPTION("Galois/Counter Mode");
  1155. MODULE_AUTHOR("Mikko Herranen <mh1@iki.fi>");
  1156. MODULE_ALIAS("gcm_base");
  1157. MODULE_ALIAS("rfc4106");
  1158. MODULE_ALIAS("rfc4543");