gcm.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  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[16];
  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. 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 skcipher_request *skreq = &pctx->u.skreq;
  190. struct scatterlist *dst;
  191. dst = req->src == req->dst ? pctx->src : pctx->dst;
  192. skcipher_request_set_tfm(skreq, ctx->ctr);
  193. skcipher_request_set_crypt(skreq, 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 skcipher_request *skreq = &pctx->u.skreq;
  399. u32 flags = aead_request_flags(req);
  400. crypto_gcm_init_common(req);
  401. crypto_gcm_init_crypt(req, req->cryptlen);
  402. skcipher_request_set_callback(skreq, flags, gcm_encrypt_done, req);
  403. return crypto_skcipher_encrypt(skreq) ?:
  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 skcipher_request *skreq = &pctx->u.skreq;
  430. struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
  431. crypto_gcm_init_crypt(req, gctx->cryptlen);
  432. skcipher_request_set_callback(skreq, flags, gcm_decrypt_done, req);
  433. return crypto_skcipher_decrypt(skreq) ?: 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_skcipher *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_skcipher2(&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 skcipher_request) +
  473. crypto_skcipher_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_skcipher(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 skcipher_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_skcipher2(&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_spawn_skcipher_alg(&ctx->ctr);
  539. /* We only support 16-byte blocks. */
  540. if (crypto_skcipher_alg_ivsize(ctr) != 16)
  541. goto out_put_ctr;
  542. /* Not a stream cipher? */
  543. err = -EINVAL;
  544. if (ctr->base.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->base.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 |
  554. ctr->base.cra_flags) & CRYPTO_ALG_ASYNC;
  555. inst->alg.base.cra_priority = (ghash->base.cra_priority +
  556. ctr->base.cra_priority) / 2;
  557. inst->alg.base.cra_blocksize = 1;
  558. inst->alg.base.cra_alignmask = ghash->base.cra_alignmask |
  559. ctr->base.cra_alignmask;
  560. inst->alg.base.cra_ctxsize = sizeof(struct crypto_gcm_ctx);
  561. inst->alg.ivsize = 12;
  562. inst->alg.chunksize = crypto_skcipher_alg_chunksize(ctr);
  563. inst->alg.maxauthsize = 16;
  564. inst->alg.init = crypto_gcm_init_tfm;
  565. inst->alg.exit = crypto_gcm_exit_tfm;
  566. inst->alg.setkey = crypto_gcm_setkey;
  567. inst->alg.setauthsize = crypto_gcm_setauthsize;
  568. inst->alg.encrypt = crypto_gcm_encrypt;
  569. inst->alg.decrypt = crypto_gcm_decrypt;
  570. inst->free = crypto_gcm_free;
  571. err = aead_register_instance(tmpl, inst);
  572. if (err)
  573. goto out_put_ctr;
  574. out_put_ghash:
  575. crypto_mod_put(ghash_alg);
  576. return err;
  577. out_put_ctr:
  578. crypto_drop_skcipher(&ctx->ctr);
  579. err_drop_ghash:
  580. crypto_drop_ahash(&ctx->ghash);
  581. err_free_inst:
  582. kfree(inst);
  583. goto out_put_ghash;
  584. }
  585. static int crypto_gcm_create(struct crypto_template *tmpl, struct rtattr **tb)
  586. {
  587. const char *cipher_name;
  588. char ctr_name[CRYPTO_MAX_ALG_NAME];
  589. char full_name[CRYPTO_MAX_ALG_NAME];
  590. cipher_name = crypto_attr_alg_name(tb[1]);
  591. if (IS_ERR(cipher_name))
  592. return PTR_ERR(cipher_name);
  593. if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)", cipher_name) >=
  594. CRYPTO_MAX_ALG_NAME)
  595. return -ENAMETOOLONG;
  596. if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "gcm(%s)", cipher_name) >=
  597. CRYPTO_MAX_ALG_NAME)
  598. return -ENAMETOOLONG;
  599. return crypto_gcm_create_common(tmpl, tb, full_name,
  600. ctr_name, "ghash");
  601. }
  602. static struct crypto_template crypto_gcm_tmpl = {
  603. .name = "gcm",
  604. .create = crypto_gcm_create,
  605. .module = THIS_MODULE,
  606. };
  607. static int crypto_gcm_base_create(struct crypto_template *tmpl,
  608. struct rtattr **tb)
  609. {
  610. const char *ctr_name;
  611. const char *ghash_name;
  612. char full_name[CRYPTO_MAX_ALG_NAME];
  613. ctr_name = crypto_attr_alg_name(tb[1]);
  614. if (IS_ERR(ctr_name))
  615. return PTR_ERR(ctr_name);
  616. ghash_name = crypto_attr_alg_name(tb[2]);
  617. if (IS_ERR(ghash_name))
  618. return PTR_ERR(ghash_name);
  619. if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "gcm_base(%s,%s)",
  620. ctr_name, ghash_name) >= CRYPTO_MAX_ALG_NAME)
  621. return -ENAMETOOLONG;
  622. return crypto_gcm_create_common(tmpl, tb, full_name,
  623. ctr_name, ghash_name);
  624. }
  625. static struct crypto_template crypto_gcm_base_tmpl = {
  626. .name = "gcm_base",
  627. .create = crypto_gcm_base_create,
  628. .module = THIS_MODULE,
  629. };
  630. static int crypto_rfc4106_setkey(struct crypto_aead *parent, const u8 *key,
  631. unsigned int keylen)
  632. {
  633. struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
  634. struct crypto_aead *child = ctx->child;
  635. int err;
  636. if (keylen < 4)
  637. return -EINVAL;
  638. keylen -= 4;
  639. memcpy(ctx->nonce, key + keylen, 4);
  640. crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  641. crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
  642. CRYPTO_TFM_REQ_MASK);
  643. err = crypto_aead_setkey(child, key, keylen);
  644. crypto_aead_set_flags(parent, crypto_aead_get_flags(child) &
  645. CRYPTO_TFM_RES_MASK);
  646. return err;
  647. }
  648. static int crypto_rfc4106_setauthsize(struct crypto_aead *parent,
  649. unsigned int authsize)
  650. {
  651. struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
  652. switch (authsize) {
  653. case 8:
  654. case 12:
  655. case 16:
  656. break;
  657. default:
  658. return -EINVAL;
  659. }
  660. return crypto_aead_setauthsize(ctx->child, authsize);
  661. }
  662. static struct aead_request *crypto_rfc4106_crypt(struct aead_request *req)
  663. {
  664. struct crypto_rfc4106_req_ctx *rctx = aead_request_ctx(req);
  665. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  666. struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(aead);
  667. struct aead_request *subreq = &rctx->subreq;
  668. struct crypto_aead *child = ctx->child;
  669. struct scatterlist *sg;
  670. u8 *iv = PTR_ALIGN((u8 *)(subreq + 1) + crypto_aead_reqsize(child),
  671. crypto_aead_alignmask(child) + 1);
  672. scatterwalk_map_and_copy(iv + 12, req->src, 0, req->assoclen - 8, 0);
  673. memcpy(iv, ctx->nonce, 4);
  674. memcpy(iv + 4, req->iv, 8);
  675. sg_init_table(rctx->src, 3);
  676. sg_set_buf(rctx->src, iv + 12, req->assoclen - 8);
  677. sg = scatterwalk_ffwd(rctx->src + 1, req->src, req->assoclen);
  678. if (sg != rctx->src + 1)
  679. sg_chain(rctx->src, 2, sg);
  680. if (req->src != req->dst) {
  681. sg_init_table(rctx->dst, 3);
  682. sg_set_buf(rctx->dst, iv + 12, req->assoclen - 8);
  683. sg = scatterwalk_ffwd(rctx->dst + 1, req->dst, req->assoclen);
  684. if (sg != rctx->dst + 1)
  685. sg_chain(rctx->dst, 2, sg);
  686. }
  687. aead_request_set_tfm(subreq, child);
  688. aead_request_set_callback(subreq, req->base.flags, req->base.complete,
  689. req->base.data);
  690. aead_request_set_crypt(subreq, rctx->src,
  691. req->src == req->dst ? rctx->src : rctx->dst,
  692. req->cryptlen, iv);
  693. aead_request_set_ad(subreq, req->assoclen - 8);
  694. return subreq;
  695. }
  696. static int crypto_rfc4106_encrypt(struct aead_request *req)
  697. {
  698. if (req->assoclen != 16 && req->assoclen != 20)
  699. return -EINVAL;
  700. req = crypto_rfc4106_crypt(req);
  701. return crypto_aead_encrypt(req);
  702. }
  703. static int crypto_rfc4106_decrypt(struct aead_request *req)
  704. {
  705. if (req->assoclen != 16 && req->assoclen != 20)
  706. return -EINVAL;
  707. req = crypto_rfc4106_crypt(req);
  708. return crypto_aead_decrypt(req);
  709. }
  710. static int crypto_rfc4106_init_tfm(struct crypto_aead *tfm)
  711. {
  712. struct aead_instance *inst = aead_alg_instance(tfm);
  713. struct crypto_aead_spawn *spawn = aead_instance_ctx(inst);
  714. struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(tfm);
  715. struct crypto_aead *aead;
  716. unsigned long align;
  717. aead = crypto_spawn_aead(spawn);
  718. if (IS_ERR(aead))
  719. return PTR_ERR(aead);
  720. ctx->child = aead;
  721. align = crypto_aead_alignmask(aead);
  722. align &= ~(crypto_tfm_ctx_alignment() - 1);
  723. crypto_aead_set_reqsize(
  724. tfm,
  725. sizeof(struct crypto_rfc4106_req_ctx) +
  726. ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) +
  727. align + 24);
  728. return 0;
  729. }
  730. static void crypto_rfc4106_exit_tfm(struct crypto_aead *tfm)
  731. {
  732. struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(tfm);
  733. crypto_free_aead(ctx->child);
  734. }
  735. static void crypto_rfc4106_free(struct aead_instance *inst)
  736. {
  737. crypto_drop_aead(aead_instance_ctx(inst));
  738. kfree(inst);
  739. }
  740. static int crypto_rfc4106_create(struct crypto_template *tmpl,
  741. struct rtattr **tb)
  742. {
  743. struct crypto_attr_type *algt;
  744. struct aead_instance *inst;
  745. struct crypto_aead_spawn *spawn;
  746. struct aead_alg *alg;
  747. const char *ccm_name;
  748. int err;
  749. algt = crypto_get_attr_type(tb);
  750. if (IS_ERR(algt))
  751. return PTR_ERR(algt);
  752. if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
  753. return -EINVAL;
  754. ccm_name = crypto_attr_alg_name(tb[1]);
  755. if (IS_ERR(ccm_name))
  756. return PTR_ERR(ccm_name);
  757. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  758. if (!inst)
  759. return -ENOMEM;
  760. spawn = aead_instance_ctx(inst);
  761. crypto_set_aead_spawn(spawn, aead_crypto_instance(inst));
  762. err = crypto_grab_aead(spawn, ccm_name, 0,
  763. crypto_requires_sync(algt->type, algt->mask));
  764. if (err)
  765. goto out_free_inst;
  766. alg = crypto_spawn_aead_alg(spawn);
  767. err = -EINVAL;
  768. /* Underlying IV size must be 12. */
  769. if (crypto_aead_alg_ivsize(alg) != 12)
  770. goto out_drop_alg;
  771. /* Not a stream cipher? */
  772. if (alg->base.cra_blocksize != 1)
  773. goto out_drop_alg;
  774. err = -ENAMETOOLONG;
  775. if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
  776. "rfc4106(%s)", alg->base.cra_name) >=
  777. CRYPTO_MAX_ALG_NAME ||
  778. snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  779. "rfc4106(%s)", alg->base.cra_driver_name) >=
  780. CRYPTO_MAX_ALG_NAME)
  781. goto out_drop_alg;
  782. inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
  783. inst->alg.base.cra_priority = alg->base.cra_priority;
  784. inst->alg.base.cra_blocksize = 1;
  785. inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
  786. inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4106_ctx);
  787. inst->alg.ivsize = 8;
  788. inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
  789. inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
  790. inst->alg.init = crypto_rfc4106_init_tfm;
  791. inst->alg.exit = crypto_rfc4106_exit_tfm;
  792. inst->alg.setkey = crypto_rfc4106_setkey;
  793. inst->alg.setauthsize = crypto_rfc4106_setauthsize;
  794. inst->alg.encrypt = crypto_rfc4106_encrypt;
  795. inst->alg.decrypt = crypto_rfc4106_decrypt;
  796. inst->free = crypto_rfc4106_free;
  797. err = aead_register_instance(tmpl, inst);
  798. if (err)
  799. goto out_drop_alg;
  800. out:
  801. return err;
  802. out_drop_alg:
  803. crypto_drop_aead(spawn);
  804. out_free_inst:
  805. kfree(inst);
  806. goto out;
  807. }
  808. static struct crypto_template crypto_rfc4106_tmpl = {
  809. .name = "rfc4106",
  810. .create = crypto_rfc4106_create,
  811. .module = THIS_MODULE,
  812. };
  813. static int crypto_rfc4543_setkey(struct crypto_aead *parent, const u8 *key,
  814. unsigned int keylen)
  815. {
  816. struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent);
  817. struct crypto_aead *child = ctx->child;
  818. int err;
  819. if (keylen < 4)
  820. return -EINVAL;
  821. keylen -= 4;
  822. memcpy(ctx->nonce, key + keylen, 4);
  823. crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  824. crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
  825. CRYPTO_TFM_REQ_MASK);
  826. err = crypto_aead_setkey(child, key, keylen);
  827. crypto_aead_set_flags(parent, crypto_aead_get_flags(child) &
  828. CRYPTO_TFM_RES_MASK);
  829. return err;
  830. }
  831. static int crypto_rfc4543_setauthsize(struct crypto_aead *parent,
  832. unsigned int authsize)
  833. {
  834. struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent);
  835. if (authsize != 16)
  836. return -EINVAL;
  837. return crypto_aead_setauthsize(ctx->child, authsize);
  838. }
  839. static int crypto_rfc4543_crypt(struct aead_request *req, bool enc)
  840. {
  841. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  842. struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
  843. struct crypto_rfc4543_req_ctx *rctx = aead_request_ctx(req);
  844. struct aead_request *subreq = &rctx->subreq;
  845. unsigned int authsize = crypto_aead_authsize(aead);
  846. u8 *iv = PTR_ALIGN((u8 *)(rctx + 1) + crypto_aead_reqsize(ctx->child),
  847. crypto_aead_alignmask(ctx->child) + 1);
  848. int err;
  849. if (req->src != req->dst) {
  850. err = crypto_rfc4543_copy_src_to_dst(req, enc);
  851. if (err)
  852. return err;
  853. }
  854. memcpy(iv, ctx->nonce, 4);
  855. memcpy(iv + 4, req->iv, 8);
  856. aead_request_set_tfm(subreq, ctx->child);
  857. aead_request_set_callback(subreq, req->base.flags,
  858. req->base.complete, req->base.data);
  859. aead_request_set_crypt(subreq, req->src, req->dst,
  860. enc ? 0 : authsize, iv);
  861. aead_request_set_ad(subreq, req->assoclen + req->cryptlen -
  862. subreq->cryptlen);
  863. return enc ? crypto_aead_encrypt(subreq) : crypto_aead_decrypt(subreq);
  864. }
  865. static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc)
  866. {
  867. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  868. struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
  869. unsigned int authsize = crypto_aead_authsize(aead);
  870. unsigned int nbytes = req->assoclen + req->cryptlen -
  871. (enc ? 0 : authsize);
  872. SKCIPHER_REQUEST_ON_STACK(nreq, ctx->null);
  873. skcipher_request_set_tfm(nreq, ctx->null);
  874. skcipher_request_set_callback(nreq, req->base.flags, NULL, NULL);
  875. skcipher_request_set_crypt(nreq, req->src, req->dst, nbytes, NULL);
  876. return crypto_skcipher_encrypt(nreq);
  877. }
  878. static int crypto_rfc4543_encrypt(struct aead_request *req)
  879. {
  880. return crypto_rfc4543_crypt(req, true);
  881. }
  882. static int crypto_rfc4543_decrypt(struct aead_request *req)
  883. {
  884. return crypto_rfc4543_crypt(req, false);
  885. }
  886. static int crypto_rfc4543_init_tfm(struct crypto_aead *tfm)
  887. {
  888. struct aead_instance *inst = aead_alg_instance(tfm);
  889. struct crypto_rfc4543_instance_ctx *ictx = aead_instance_ctx(inst);
  890. struct crypto_aead_spawn *spawn = &ictx->aead;
  891. struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm);
  892. struct crypto_aead *aead;
  893. struct crypto_skcipher *null;
  894. unsigned long align;
  895. int err = 0;
  896. aead = crypto_spawn_aead(spawn);
  897. if (IS_ERR(aead))
  898. return PTR_ERR(aead);
  899. null = crypto_get_default_null_skcipher2();
  900. err = PTR_ERR(null);
  901. if (IS_ERR(null))
  902. goto err_free_aead;
  903. ctx->child = aead;
  904. ctx->null = null;
  905. align = crypto_aead_alignmask(aead);
  906. align &= ~(crypto_tfm_ctx_alignment() - 1);
  907. crypto_aead_set_reqsize(
  908. tfm,
  909. sizeof(struct crypto_rfc4543_req_ctx) +
  910. ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) +
  911. align + 12);
  912. return 0;
  913. err_free_aead:
  914. crypto_free_aead(aead);
  915. return err;
  916. }
  917. static void crypto_rfc4543_exit_tfm(struct crypto_aead *tfm)
  918. {
  919. struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm);
  920. crypto_free_aead(ctx->child);
  921. crypto_put_default_null_skcipher2();
  922. }
  923. static void crypto_rfc4543_free(struct aead_instance *inst)
  924. {
  925. struct crypto_rfc4543_instance_ctx *ctx = aead_instance_ctx(inst);
  926. crypto_drop_aead(&ctx->aead);
  927. kfree(inst);
  928. }
  929. static int crypto_rfc4543_create(struct crypto_template *tmpl,
  930. struct rtattr **tb)
  931. {
  932. struct crypto_attr_type *algt;
  933. struct aead_instance *inst;
  934. struct crypto_aead_spawn *spawn;
  935. struct aead_alg *alg;
  936. struct crypto_rfc4543_instance_ctx *ctx;
  937. const char *ccm_name;
  938. int err;
  939. algt = crypto_get_attr_type(tb);
  940. if (IS_ERR(algt))
  941. return PTR_ERR(algt);
  942. if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
  943. return -EINVAL;
  944. ccm_name = crypto_attr_alg_name(tb[1]);
  945. if (IS_ERR(ccm_name))
  946. return PTR_ERR(ccm_name);
  947. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  948. if (!inst)
  949. return -ENOMEM;
  950. ctx = aead_instance_ctx(inst);
  951. spawn = &ctx->aead;
  952. crypto_set_aead_spawn(spawn, aead_crypto_instance(inst));
  953. err = crypto_grab_aead(spawn, ccm_name, 0,
  954. crypto_requires_sync(algt->type, algt->mask));
  955. if (err)
  956. goto out_free_inst;
  957. alg = crypto_spawn_aead_alg(spawn);
  958. err = -EINVAL;
  959. /* Underlying IV size must be 12. */
  960. if (crypto_aead_alg_ivsize(alg) != 12)
  961. goto out_drop_alg;
  962. /* Not a stream cipher? */
  963. if (alg->base.cra_blocksize != 1)
  964. goto out_drop_alg;
  965. err = -ENAMETOOLONG;
  966. if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
  967. "rfc4543(%s)", alg->base.cra_name) >=
  968. CRYPTO_MAX_ALG_NAME ||
  969. snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  970. "rfc4543(%s)", alg->base.cra_driver_name) >=
  971. CRYPTO_MAX_ALG_NAME)
  972. goto out_drop_alg;
  973. inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
  974. inst->alg.base.cra_priority = alg->base.cra_priority;
  975. inst->alg.base.cra_blocksize = 1;
  976. inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
  977. inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4543_ctx);
  978. inst->alg.ivsize = 8;
  979. inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
  980. inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
  981. inst->alg.init = crypto_rfc4543_init_tfm;
  982. inst->alg.exit = crypto_rfc4543_exit_tfm;
  983. inst->alg.setkey = crypto_rfc4543_setkey;
  984. inst->alg.setauthsize = crypto_rfc4543_setauthsize;
  985. inst->alg.encrypt = crypto_rfc4543_encrypt;
  986. inst->alg.decrypt = crypto_rfc4543_decrypt;
  987. inst->free = crypto_rfc4543_free,
  988. err = aead_register_instance(tmpl, inst);
  989. if (err)
  990. goto out_drop_alg;
  991. out:
  992. return err;
  993. out_drop_alg:
  994. crypto_drop_aead(spawn);
  995. out_free_inst:
  996. kfree(inst);
  997. goto out;
  998. }
  999. static struct crypto_template crypto_rfc4543_tmpl = {
  1000. .name = "rfc4543",
  1001. .create = crypto_rfc4543_create,
  1002. .module = THIS_MODULE,
  1003. };
  1004. static int __init crypto_gcm_module_init(void)
  1005. {
  1006. int err;
  1007. gcm_zeroes = kzalloc(sizeof(*gcm_zeroes), GFP_KERNEL);
  1008. if (!gcm_zeroes)
  1009. return -ENOMEM;
  1010. sg_init_one(&gcm_zeroes->sg, gcm_zeroes->buf, sizeof(gcm_zeroes->buf));
  1011. err = crypto_register_template(&crypto_gcm_base_tmpl);
  1012. if (err)
  1013. goto out;
  1014. err = crypto_register_template(&crypto_gcm_tmpl);
  1015. if (err)
  1016. goto out_undo_base;
  1017. err = crypto_register_template(&crypto_rfc4106_tmpl);
  1018. if (err)
  1019. goto out_undo_gcm;
  1020. err = crypto_register_template(&crypto_rfc4543_tmpl);
  1021. if (err)
  1022. goto out_undo_rfc4106;
  1023. return 0;
  1024. out_undo_rfc4106:
  1025. crypto_unregister_template(&crypto_rfc4106_tmpl);
  1026. out_undo_gcm:
  1027. crypto_unregister_template(&crypto_gcm_tmpl);
  1028. out_undo_base:
  1029. crypto_unregister_template(&crypto_gcm_base_tmpl);
  1030. out:
  1031. kfree(gcm_zeroes);
  1032. return err;
  1033. }
  1034. static void __exit crypto_gcm_module_exit(void)
  1035. {
  1036. kfree(gcm_zeroes);
  1037. crypto_unregister_template(&crypto_rfc4543_tmpl);
  1038. crypto_unregister_template(&crypto_rfc4106_tmpl);
  1039. crypto_unregister_template(&crypto_gcm_tmpl);
  1040. crypto_unregister_template(&crypto_gcm_base_tmpl);
  1041. }
  1042. module_init(crypto_gcm_module_init);
  1043. module_exit(crypto_gcm_module_exit);
  1044. MODULE_LICENSE("GPL");
  1045. MODULE_DESCRIPTION("Galois/Counter Mode");
  1046. MODULE_AUTHOR("Mikko Herranen <mh1@iki.fi>");
  1047. MODULE_ALIAS_CRYPTO("gcm_base");
  1048. MODULE_ALIAS_CRYPTO("rfc4106");
  1049. MODULE_ALIAS_CRYPTO("rfc4543");
  1050. MODULE_ALIAS_CRYPTO("gcm");