gcm.c 33 KB

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