gcm.c 27 KB

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