gcm.c 37 KB

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