gcm.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  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. crypto_completion_t complete;
  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 crypto_async_request *areq,
  224. int err)
  225. {
  226. struct aead_request *req = areq->data;
  227. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  228. struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
  229. if (!err)
  230. crypto_xor(pctx->auth_tag, pctx->iauth_tag, 16);
  231. gctx->complete(areq, err);
  232. }
  233. static void gcm_hash_len_done(struct crypto_async_request *areq,
  234. int err)
  235. {
  236. struct aead_request *req = areq->data;
  237. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  238. if (!err) {
  239. err = gcm_hash_final(req, pctx);
  240. if (err == -EINPROGRESS || err == -EBUSY)
  241. return;
  242. }
  243. gcm_hash_final_done(areq, err);
  244. }
  245. static void gcm_hash_crypt_remain_done(struct crypto_async_request *areq,
  246. int err)
  247. {
  248. struct aead_request *req = areq->data;
  249. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  250. if (!err) {
  251. err = gcm_hash_len(req, pctx);
  252. if (err == -EINPROGRESS || err == -EBUSY)
  253. return;
  254. }
  255. gcm_hash_len_done(areq, err);
  256. }
  257. static void gcm_hash_crypt_done(struct crypto_async_request *areq,
  258. int err)
  259. {
  260. struct aead_request *req = areq->data;
  261. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  262. struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
  263. unsigned int remain;
  264. if (!err) {
  265. remain = gcm_remain(gctx->cryptlen);
  266. BUG_ON(!remain);
  267. err = gcm_hash_remain(req, pctx, remain,
  268. gcm_hash_crypt_remain_done);
  269. if (err == -EINPROGRESS || err == -EBUSY)
  270. return;
  271. }
  272. gcm_hash_crypt_remain_done(areq, err);
  273. }
  274. static void gcm_hash_assoc_remain_done(struct crypto_async_request *areq,
  275. int err)
  276. {
  277. struct aead_request *req = areq->data;
  278. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  279. struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
  280. crypto_completion_t complete;
  281. unsigned int remain = 0;
  282. if (!err && gctx->cryptlen) {
  283. remain = gcm_remain(gctx->cryptlen);
  284. complete = remain ? gcm_hash_crypt_done :
  285. gcm_hash_crypt_remain_done;
  286. err = gcm_hash_update(req, pctx, complete,
  287. gctx->src, gctx->cryptlen);
  288. if (err == -EINPROGRESS || err == -EBUSY)
  289. return;
  290. }
  291. if (remain)
  292. gcm_hash_crypt_done(areq, err);
  293. else
  294. gcm_hash_crypt_remain_done(areq, err);
  295. }
  296. static void gcm_hash_assoc_done(struct crypto_async_request *areq,
  297. int err)
  298. {
  299. struct aead_request *req = areq->data;
  300. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  301. unsigned int remain;
  302. if (!err) {
  303. remain = gcm_remain(req->assoclen);
  304. BUG_ON(!remain);
  305. err = gcm_hash_remain(req, pctx, remain,
  306. gcm_hash_assoc_remain_done);
  307. if (err == -EINPROGRESS || err == -EBUSY)
  308. return;
  309. }
  310. gcm_hash_assoc_remain_done(areq, err);
  311. }
  312. static void gcm_hash_init_done(struct crypto_async_request *areq,
  313. int err)
  314. {
  315. struct aead_request *req = areq->data;
  316. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  317. crypto_completion_t complete;
  318. unsigned int remain = 0;
  319. if (!err && req->assoclen) {
  320. remain = gcm_remain(req->assoclen);
  321. complete = remain ? gcm_hash_assoc_done :
  322. gcm_hash_assoc_remain_done;
  323. err = gcm_hash_update(req, pctx, complete,
  324. req->assoc, req->assoclen);
  325. if (err == -EINPROGRESS || err == -EBUSY)
  326. return;
  327. }
  328. if (remain)
  329. gcm_hash_assoc_done(areq, err);
  330. else
  331. gcm_hash_assoc_remain_done(areq, err);
  332. }
  333. static int gcm_hash(struct aead_request *req,
  334. struct crypto_gcm_req_priv_ctx *pctx)
  335. {
  336. struct ahash_request *ahreq = &pctx->u.ahreq;
  337. struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
  338. struct crypto_gcm_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  339. unsigned int remain;
  340. crypto_completion_t complete;
  341. int err;
  342. ahash_request_set_tfm(ahreq, ctx->ghash);
  343. ahash_request_set_callback(ahreq, aead_request_flags(req),
  344. gcm_hash_init_done, req);
  345. err = crypto_ahash_init(ahreq);
  346. if (err)
  347. return err;
  348. remain = gcm_remain(req->assoclen);
  349. complete = remain ? gcm_hash_assoc_done : gcm_hash_assoc_remain_done;
  350. err = gcm_hash_update(req, pctx, complete, req->assoc, req->assoclen);
  351. if (err)
  352. return err;
  353. if (remain) {
  354. err = gcm_hash_remain(req, pctx, remain,
  355. gcm_hash_assoc_remain_done);
  356. if (err)
  357. return err;
  358. }
  359. remain = gcm_remain(gctx->cryptlen);
  360. complete = remain ? gcm_hash_crypt_done : gcm_hash_crypt_remain_done;
  361. err = gcm_hash_update(req, pctx, complete, gctx->src, gctx->cryptlen);
  362. if (err)
  363. return err;
  364. if (remain) {
  365. err = gcm_hash_remain(req, pctx, remain,
  366. gcm_hash_crypt_remain_done);
  367. if (err)
  368. return err;
  369. }
  370. err = gcm_hash_len(req, pctx);
  371. if (err)
  372. return err;
  373. err = gcm_hash_final(req, pctx);
  374. if (err)
  375. return err;
  376. return 0;
  377. }
  378. static void gcm_enc_copy_hash(struct aead_request *req,
  379. struct crypto_gcm_req_priv_ctx *pctx)
  380. {
  381. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  382. u8 *auth_tag = pctx->auth_tag;
  383. scatterwalk_map_and_copy(auth_tag, req->dst, req->cryptlen,
  384. crypto_aead_authsize(aead), 1);
  385. }
  386. static void gcm_enc_hash_done(struct crypto_async_request *areq,
  387. int err)
  388. {
  389. struct aead_request *req = areq->data;
  390. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  391. if (!err)
  392. gcm_enc_copy_hash(req, pctx);
  393. aead_request_complete(req, err);
  394. }
  395. static void gcm_encrypt_done(struct crypto_async_request *areq,
  396. int err)
  397. {
  398. struct aead_request *req = areq->data;
  399. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  400. if (!err) {
  401. err = gcm_hash(req, pctx);
  402. if (err == -EINPROGRESS || err == -EBUSY)
  403. return;
  404. }
  405. gcm_enc_hash_done(areq, err);
  406. }
  407. static int crypto_gcm_encrypt(struct aead_request *req)
  408. {
  409. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  410. struct ablkcipher_request *abreq = &pctx->u.abreq;
  411. struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
  412. int err;
  413. crypto_gcm_init_crypt(abreq, req, req->cryptlen);
  414. ablkcipher_request_set_callback(abreq, aead_request_flags(req),
  415. gcm_encrypt_done, req);
  416. gctx->src = req->dst;
  417. gctx->cryptlen = req->cryptlen;
  418. gctx->complete = gcm_enc_hash_done;
  419. err = crypto_ablkcipher_encrypt(abreq);
  420. if (err)
  421. return err;
  422. err = gcm_hash(req, pctx);
  423. if (err)
  424. return err;
  425. crypto_xor(pctx->auth_tag, pctx->iauth_tag, 16);
  426. gcm_enc_copy_hash(req, pctx);
  427. return 0;
  428. }
  429. static int crypto_gcm_verify(struct aead_request *req,
  430. struct crypto_gcm_req_priv_ctx *pctx)
  431. {
  432. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  433. u8 *auth_tag = pctx->auth_tag;
  434. u8 *iauth_tag = pctx->iauth_tag;
  435. unsigned int authsize = crypto_aead_authsize(aead);
  436. unsigned int cryptlen = req->cryptlen - authsize;
  437. crypto_xor(auth_tag, iauth_tag, 16);
  438. scatterwalk_map_and_copy(iauth_tag, req->src, cryptlen, authsize, 0);
  439. return memcmp(iauth_tag, auth_tag, authsize) ? -EBADMSG : 0;
  440. }
  441. static void gcm_decrypt_done(struct crypto_async_request *areq, int err)
  442. {
  443. struct aead_request *req = areq->data;
  444. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  445. if (!err)
  446. err = crypto_gcm_verify(req, pctx);
  447. aead_request_complete(req, err);
  448. }
  449. static void gcm_dec_hash_done(struct crypto_async_request *areq, int err)
  450. {
  451. struct aead_request *req = areq->data;
  452. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  453. struct ablkcipher_request *abreq = &pctx->u.abreq;
  454. struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
  455. if (!err) {
  456. ablkcipher_request_set_callback(abreq, aead_request_flags(req),
  457. gcm_decrypt_done, req);
  458. crypto_gcm_init_crypt(abreq, req, gctx->cryptlen);
  459. err = crypto_ablkcipher_decrypt(abreq);
  460. if (err == -EINPROGRESS || err == -EBUSY)
  461. return;
  462. }
  463. gcm_decrypt_done(areq, err);
  464. }
  465. static int crypto_gcm_decrypt(struct aead_request *req)
  466. {
  467. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  468. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  469. struct ablkcipher_request *abreq = &pctx->u.abreq;
  470. struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
  471. unsigned int authsize = crypto_aead_authsize(aead);
  472. unsigned int cryptlen = req->cryptlen;
  473. int err;
  474. if (cryptlen < authsize)
  475. return -EINVAL;
  476. cryptlen -= authsize;
  477. gctx->src = req->src;
  478. gctx->cryptlen = cryptlen;
  479. gctx->complete = gcm_dec_hash_done;
  480. err = gcm_hash(req, pctx);
  481. if (err)
  482. return err;
  483. ablkcipher_request_set_callback(abreq, aead_request_flags(req),
  484. gcm_decrypt_done, req);
  485. crypto_gcm_init_crypt(abreq, req, cryptlen);
  486. err = crypto_ablkcipher_decrypt(abreq);
  487. if (err)
  488. return err;
  489. return crypto_gcm_verify(req, pctx);
  490. }
  491. static int crypto_gcm_init_tfm(struct crypto_tfm *tfm)
  492. {
  493. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  494. struct gcm_instance_ctx *ictx = crypto_instance_ctx(inst);
  495. struct crypto_gcm_ctx *ctx = crypto_tfm_ctx(tfm);
  496. struct crypto_ablkcipher *ctr;
  497. struct crypto_ahash *ghash;
  498. unsigned long align;
  499. int err;
  500. ghash = crypto_spawn_ahash(&ictx->ghash);
  501. if (IS_ERR(ghash))
  502. return PTR_ERR(ghash);
  503. ctr = crypto_spawn_skcipher(&ictx->ctr);
  504. err = PTR_ERR(ctr);
  505. if (IS_ERR(ctr))
  506. goto err_free_hash;
  507. ctx->ctr = ctr;
  508. ctx->ghash = ghash;
  509. align = crypto_tfm_alg_alignmask(tfm);
  510. align &= ~(crypto_tfm_ctx_alignment() - 1);
  511. tfm->crt_aead.reqsize = align +
  512. offsetof(struct crypto_gcm_req_priv_ctx, u) +
  513. max(sizeof(struct ablkcipher_request) +
  514. crypto_ablkcipher_reqsize(ctr),
  515. sizeof(struct ahash_request) +
  516. crypto_ahash_reqsize(ghash));
  517. return 0;
  518. err_free_hash:
  519. crypto_free_ahash(ghash);
  520. return err;
  521. }
  522. static void crypto_gcm_exit_tfm(struct crypto_tfm *tfm)
  523. {
  524. struct crypto_gcm_ctx *ctx = crypto_tfm_ctx(tfm);
  525. crypto_free_ahash(ctx->ghash);
  526. crypto_free_ablkcipher(ctx->ctr);
  527. }
  528. static struct crypto_instance *crypto_gcm_alloc_common(struct rtattr **tb,
  529. const char *full_name,
  530. const char *ctr_name,
  531. const char *ghash_name)
  532. {
  533. struct crypto_attr_type *algt;
  534. struct crypto_instance *inst;
  535. struct crypto_alg *ctr;
  536. struct crypto_alg *ghash_alg;
  537. struct ahash_alg *ghash_ahash_alg;
  538. struct gcm_instance_ctx *ctx;
  539. int err;
  540. algt = crypto_get_attr_type(tb);
  541. err = PTR_ERR(algt);
  542. if (IS_ERR(algt))
  543. return ERR_PTR(err);
  544. if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
  545. return ERR_PTR(-EINVAL);
  546. ghash_alg = crypto_find_alg(ghash_name, &crypto_ahash_type,
  547. CRYPTO_ALG_TYPE_HASH,
  548. CRYPTO_ALG_TYPE_AHASH_MASK);
  549. err = PTR_ERR(ghash_alg);
  550. if (IS_ERR(ghash_alg))
  551. return ERR_PTR(err);
  552. err = -ENOMEM;
  553. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  554. if (!inst)
  555. goto out_put_ghash;
  556. ctx = crypto_instance_ctx(inst);
  557. ghash_ahash_alg = container_of(ghash_alg, struct ahash_alg, halg.base);
  558. err = crypto_init_ahash_spawn(&ctx->ghash, &ghash_ahash_alg->halg,
  559. inst);
  560. if (err)
  561. goto err_free_inst;
  562. crypto_set_skcipher_spawn(&ctx->ctr, inst);
  563. err = crypto_grab_skcipher(&ctx->ctr, ctr_name, 0,
  564. crypto_requires_sync(algt->type,
  565. algt->mask));
  566. if (err)
  567. goto err_drop_ghash;
  568. ctr = crypto_skcipher_spawn_alg(&ctx->ctr);
  569. /* We only support 16-byte blocks. */
  570. if (ctr->cra_ablkcipher.ivsize != 16)
  571. goto out_put_ctr;
  572. /* Not a stream cipher? */
  573. err = -EINVAL;
  574. if (ctr->cra_blocksize != 1)
  575. goto out_put_ctr;
  576. err = -ENAMETOOLONG;
  577. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  578. "gcm_base(%s,%s)", ctr->cra_driver_name,
  579. ghash_alg->cra_driver_name) >=
  580. CRYPTO_MAX_ALG_NAME)
  581. goto out_put_ctr;
  582. memcpy(inst->alg.cra_name, full_name, CRYPTO_MAX_ALG_NAME);
  583. inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD;
  584. inst->alg.cra_flags |= ctr->cra_flags & CRYPTO_ALG_ASYNC;
  585. inst->alg.cra_priority = ctr->cra_priority;
  586. inst->alg.cra_blocksize = 1;
  587. inst->alg.cra_alignmask = ctr->cra_alignmask | (__alignof__(u64) - 1);
  588. inst->alg.cra_type = &crypto_aead_type;
  589. inst->alg.cra_aead.ivsize = 16;
  590. inst->alg.cra_aead.maxauthsize = 16;
  591. inst->alg.cra_ctxsize = sizeof(struct crypto_gcm_ctx);
  592. inst->alg.cra_init = crypto_gcm_init_tfm;
  593. inst->alg.cra_exit = crypto_gcm_exit_tfm;
  594. inst->alg.cra_aead.setkey = crypto_gcm_setkey;
  595. inst->alg.cra_aead.setauthsize = crypto_gcm_setauthsize;
  596. inst->alg.cra_aead.encrypt = crypto_gcm_encrypt;
  597. inst->alg.cra_aead.decrypt = crypto_gcm_decrypt;
  598. out:
  599. crypto_mod_put(ghash_alg);
  600. return inst;
  601. out_put_ctr:
  602. crypto_drop_skcipher(&ctx->ctr);
  603. err_drop_ghash:
  604. crypto_drop_ahash(&ctx->ghash);
  605. err_free_inst:
  606. kfree(inst);
  607. out_put_ghash:
  608. inst = ERR_PTR(err);
  609. goto out;
  610. }
  611. static struct crypto_instance *crypto_gcm_alloc(struct rtattr **tb)
  612. {
  613. int err;
  614. const char *cipher_name;
  615. char ctr_name[CRYPTO_MAX_ALG_NAME];
  616. char full_name[CRYPTO_MAX_ALG_NAME];
  617. cipher_name = crypto_attr_alg_name(tb[1]);
  618. err = PTR_ERR(cipher_name);
  619. if (IS_ERR(cipher_name))
  620. return ERR_PTR(err);
  621. if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)", cipher_name) >=
  622. CRYPTO_MAX_ALG_NAME)
  623. return ERR_PTR(-ENAMETOOLONG);
  624. if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "gcm(%s)", cipher_name) >=
  625. CRYPTO_MAX_ALG_NAME)
  626. return ERR_PTR(-ENAMETOOLONG);
  627. return crypto_gcm_alloc_common(tb, full_name, ctr_name, "ghash");
  628. }
  629. static void crypto_gcm_free(struct crypto_instance *inst)
  630. {
  631. struct gcm_instance_ctx *ctx = crypto_instance_ctx(inst);
  632. crypto_drop_skcipher(&ctx->ctr);
  633. crypto_drop_ahash(&ctx->ghash);
  634. kfree(inst);
  635. }
  636. static struct crypto_template crypto_gcm_tmpl = {
  637. .name = "gcm",
  638. .alloc = crypto_gcm_alloc,
  639. .free = crypto_gcm_free,
  640. .module = THIS_MODULE,
  641. };
  642. static struct crypto_instance *crypto_gcm_base_alloc(struct rtattr **tb)
  643. {
  644. int err;
  645. const char *ctr_name;
  646. const char *ghash_name;
  647. char full_name[CRYPTO_MAX_ALG_NAME];
  648. ctr_name = crypto_attr_alg_name(tb[1]);
  649. err = PTR_ERR(ctr_name);
  650. if (IS_ERR(ctr_name))
  651. return ERR_PTR(err);
  652. ghash_name = crypto_attr_alg_name(tb[2]);
  653. err = PTR_ERR(ghash_name);
  654. if (IS_ERR(ghash_name))
  655. return ERR_PTR(err);
  656. if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "gcm_base(%s,%s)",
  657. ctr_name, ghash_name) >= CRYPTO_MAX_ALG_NAME)
  658. return ERR_PTR(-ENAMETOOLONG);
  659. return crypto_gcm_alloc_common(tb, full_name, ctr_name, ghash_name);
  660. }
  661. static struct crypto_template crypto_gcm_base_tmpl = {
  662. .name = "gcm_base",
  663. .alloc = crypto_gcm_base_alloc,
  664. .free = crypto_gcm_free,
  665. .module = THIS_MODULE,
  666. };
  667. static int crypto_rfc4106_setkey(struct crypto_aead *parent, const u8 *key,
  668. unsigned int keylen)
  669. {
  670. struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
  671. struct crypto_aead *child = ctx->child;
  672. int err;
  673. if (keylen < 4)
  674. return -EINVAL;
  675. keylen -= 4;
  676. memcpy(ctx->nonce, key + keylen, 4);
  677. crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  678. crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
  679. CRYPTO_TFM_REQ_MASK);
  680. err = crypto_aead_setkey(child, key, keylen);
  681. crypto_aead_set_flags(parent, crypto_aead_get_flags(child) &
  682. CRYPTO_TFM_RES_MASK);
  683. return err;
  684. }
  685. static int crypto_rfc4106_setauthsize(struct crypto_aead *parent,
  686. unsigned int authsize)
  687. {
  688. struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
  689. switch (authsize) {
  690. case 8:
  691. case 12:
  692. case 16:
  693. break;
  694. default:
  695. return -EINVAL;
  696. }
  697. return crypto_aead_setauthsize(ctx->child, authsize);
  698. }
  699. static struct aead_request *crypto_rfc4106_crypt(struct aead_request *req)
  700. {
  701. struct aead_request *subreq = aead_request_ctx(req);
  702. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  703. struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(aead);
  704. struct crypto_aead *child = ctx->child;
  705. u8 *iv = PTR_ALIGN((u8 *)(subreq + 1) + crypto_aead_reqsize(child),
  706. crypto_aead_alignmask(child) + 1);
  707. memcpy(iv, ctx->nonce, 4);
  708. memcpy(iv + 4, req->iv, 8);
  709. aead_request_set_tfm(subreq, child);
  710. aead_request_set_callback(subreq, req->base.flags, req->base.complete,
  711. req->base.data);
  712. aead_request_set_crypt(subreq, req->src, req->dst, req->cryptlen, iv);
  713. aead_request_set_assoc(subreq, req->assoc, req->assoclen);
  714. return subreq;
  715. }
  716. static int crypto_rfc4106_encrypt(struct aead_request *req)
  717. {
  718. req = crypto_rfc4106_crypt(req);
  719. return crypto_aead_encrypt(req);
  720. }
  721. static int crypto_rfc4106_decrypt(struct aead_request *req)
  722. {
  723. req = crypto_rfc4106_crypt(req);
  724. return crypto_aead_decrypt(req);
  725. }
  726. static int crypto_rfc4106_init_tfm(struct crypto_tfm *tfm)
  727. {
  728. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  729. struct crypto_aead_spawn *spawn = crypto_instance_ctx(inst);
  730. struct crypto_rfc4106_ctx *ctx = crypto_tfm_ctx(tfm);
  731. struct crypto_aead *aead;
  732. unsigned long align;
  733. aead = crypto_spawn_aead(spawn);
  734. if (IS_ERR(aead))
  735. return PTR_ERR(aead);
  736. ctx->child = aead;
  737. align = crypto_aead_alignmask(aead);
  738. align &= ~(crypto_tfm_ctx_alignment() - 1);
  739. tfm->crt_aead.reqsize = sizeof(struct aead_request) +
  740. ALIGN(crypto_aead_reqsize(aead),
  741. crypto_tfm_ctx_alignment()) +
  742. align + 16;
  743. return 0;
  744. }
  745. static void crypto_rfc4106_exit_tfm(struct crypto_tfm *tfm)
  746. {
  747. struct crypto_rfc4106_ctx *ctx = crypto_tfm_ctx(tfm);
  748. crypto_free_aead(ctx->child);
  749. }
  750. static struct crypto_instance *crypto_rfc4106_alloc(struct rtattr **tb)
  751. {
  752. struct crypto_attr_type *algt;
  753. struct crypto_instance *inst;
  754. struct crypto_aead_spawn *spawn;
  755. struct crypto_alg *alg;
  756. const char *ccm_name;
  757. int err;
  758. algt = crypto_get_attr_type(tb);
  759. err = PTR_ERR(algt);
  760. if (IS_ERR(algt))
  761. return ERR_PTR(err);
  762. if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
  763. return ERR_PTR(-EINVAL);
  764. ccm_name = crypto_attr_alg_name(tb[1]);
  765. err = PTR_ERR(ccm_name);
  766. if (IS_ERR(ccm_name))
  767. return ERR_PTR(err);
  768. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  769. if (!inst)
  770. return ERR_PTR(-ENOMEM);
  771. spawn = crypto_instance_ctx(inst);
  772. crypto_set_aead_spawn(spawn, inst);
  773. err = crypto_grab_aead(spawn, ccm_name, 0,
  774. crypto_requires_sync(algt->type, algt->mask));
  775. if (err)
  776. goto out_free_inst;
  777. alg = crypto_aead_spawn_alg(spawn);
  778. err = -EINVAL;
  779. /* We only support 16-byte blocks. */
  780. if (alg->cra_aead.ivsize != 16)
  781. goto out_drop_alg;
  782. /* Not a stream cipher? */
  783. if (alg->cra_blocksize != 1)
  784. goto out_drop_alg;
  785. err = -ENAMETOOLONG;
  786. if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
  787. "rfc4106(%s)", alg->cra_name) >= CRYPTO_MAX_ALG_NAME ||
  788. snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  789. "rfc4106(%s)", alg->cra_driver_name) >=
  790. CRYPTO_MAX_ALG_NAME)
  791. goto out_drop_alg;
  792. inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD;
  793. inst->alg.cra_flags |= alg->cra_flags & CRYPTO_ALG_ASYNC;
  794. inst->alg.cra_priority = alg->cra_priority;
  795. inst->alg.cra_blocksize = 1;
  796. inst->alg.cra_alignmask = alg->cra_alignmask;
  797. inst->alg.cra_type = &crypto_nivaead_type;
  798. inst->alg.cra_aead.ivsize = 8;
  799. inst->alg.cra_aead.maxauthsize = 16;
  800. inst->alg.cra_ctxsize = sizeof(struct crypto_rfc4106_ctx);
  801. inst->alg.cra_init = crypto_rfc4106_init_tfm;
  802. inst->alg.cra_exit = crypto_rfc4106_exit_tfm;
  803. inst->alg.cra_aead.setkey = crypto_rfc4106_setkey;
  804. inst->alg.cra_aead.setauthsize = crypto_rfc4106_setauthsize;
  805. inst->alg.cra_aead.encrypt = crypto_rfc4106_encrypt;
  806. inst->alg.cra_aead.decrypt = crypto_rfc4106_decrypt;
  807. inst->alg.cra_aead.geniv = "seqiv";
  808. out:
  809. return inst;
  810. out_drop_alg:
  811. crypto_drop_aead(spawn);
  812. out_free_inst:
  813. kfree(inst);
  814. inst = ERR_PTR(err);
  815. goto out;
  816. }
  817. static void crypto_rfc4106_free(struct crypto_instance *inst)
  818. {
  819. crypto_drop_spawn(crypto_instance_ctx(inst));
  820. kfree(inst);
  821. }
  822. static struct crypto_template crypto_rfc4106_tmpl = {
  823. .name = "rfc4106",
  824. .alloc = crypto_rfc4106_alloc,
  825. .free = crypto_rfc4106_free,
  826. .module = THIS_MODULE,
  827. };
  828. static int __init crypto_gcm_module_init(void)
  829. {
  830. int err;
  831. gcm_zeroes = kzalloc(16, GFP_KERNEL);
  832. if (!gcm_zeroes)
  833. return -ENOMEM;
  834. err = crypto_register_template(&crypto_gcm_base_tmpl);
  835. if (err)
  836. goto out;
  837. err = crypto_register_template(&crypto_gcm_tmpl);
  838. if (err)
  839. goto out_undo_base;
  840. err = crypto_register_template(&crypto_rfc4106_tmpl);
  841. if (err)
  842. goto out_undo_gcm;
  843. return 0;
  844. out_undo_gcm:
  845. crypto_unregister_template(&crypto_gcm_tmpl);
  846. out_undo_base:
  847. crypto_unregister_template(&crypto_gcm_base_tmpl);
  848. out:
  849. kfree(gcm_zeroes);
  850. return err;
  851. }
  852. static void __exit crypto_gcm_module_exit(void)
  853. {
  854. kfree(gcm_zeroes);
  855. crypto_unregister_template(&crypto_rfc4106_tmpl);
  856. crypto_unregister_template(&crypto_gcm_tmpl);
  857. crypto_unregister_template(&crypto_gcm_base_tmpl);
  858. }
  859. module_init(crypto_gcm_module_init);
  860. module_exit(crypto_gcm_module_exit);
  861. MODULE_LICENSE("GPL");
  862. MODULE_DESCRIPTION("Galois/Counter Mode");
  863. MODULE_AUTHOR("Mikko Herranen <mh1@iki.fi>");
  864. MODULE_ALIAS("gcm_base");
  865. MODULE_ALIAS("rfc4106");