gcm.c 33 KB

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