gcm.c 35 KB

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