gcm.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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/scatterwalk.h>
  14. #include <linux/completion.h>
  15. #include <linux/err.h>
  16. #include <linux/init.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. struct gcm_instance_ctx {
  21. struct crypto_skcipher_spawn ctr;
  22. };
  23. struct crypto_gcm_ctx {
  24. struct crypto_ablkcipher *ctr;
  25. struct gf128mul_4k *gf128;
  26. };
  27. struct crypto_rfc4106_ctx {
  28. struct crypto_aead *child;
  29. u8 nonce[4];
  30. };
  31. struct crypto_gcm_ghash_ctx {
  32. u32 bytes;
  33. u32 flags;
  34. struct gf128mul_4k *gf128;
  35. u8 buffer[16];
  36. };
  37. struct crypto_gcm_req_priv_ctx {
  38. u8 auth_tag[16];
  39. u8 iauth_tag[16];
  40. struct scatterlist src[2];
  41. struct scatterlist dst[2];
  42. struct crypto_gcm_ghash_ctx ghash;
  43. struct ablkcipher_request abreq;
  44. };
  45. struct crypto_gcm_setkey_result {
  46. int err;
  47. struct completion completion;
  48. };
  49. static inline struct crypto_gcm_req_priv_ctx *crypto_gcm_reqctx(
  50. struct aead_request *req)
  51. {
  52. unsigned long align = crypto_aead_alignmask(crypto_aead_reqtfm(req));
  53. return (void *)PTR_ALIGN((u8 *)aead_request_ctx(req), align + 1);
  54. }
  55. static void crypto_gcm_ghash_init(struct crypto_gcm_ghash_ctx *ctx, u32 flags,
  56. struct gf128mul_4k *gf128)
  57. {
  58. ctx->bytes = 0;
  59. ctx->flags = flags;
  60. ctx->gf128 = gf128;
  61. memset(ctx->buffer, 0, 16);
  62. }
  63. static void crypto_gcm_ghash_update(struct crypto_gcm_ghash_ctx *ctx,
  64. const u8 *src, unsigned int srclen)
  65. {
  66. u8 *dst = ctx->buffer;
  67. if (ctx->bytes) {
  68. int n = min(srclen, ctx->bytes);
  69. u8 *pos = dst + (16 - ctx->bytes);
  70. ctx->bytes -= n;
  71. srclen -= n;
  72. while (n--)
  73. *pos++ ^= *src++;
  74. if (!ctx->bytes)
  75. gf128mul_4k_lle((be128 *)dst, ctx->gf128);
  76. }
  77. while (srclen >= 16) {
  78. crypto_xor(dst, src, 16);
  79. gf128mul_4k_lle((be128 *)dst, ctx->gf128);
  80. src += 16;
  81. srclen -= 16;
  82. }
  83. if (srclen) {
  84. ctx->bytes = 16 - srclen;
  85. while (srclen--)
  86. *dst++ ^= *src++;
  87. }
  88. }
  89. static void crypto_gcm_ghash_update_sg(struct crypto_gcm_ghash_ctx *ctx,
  90. struct scatterlist *sg, int len)
  91. {
  92. struct scatter_walk walk;
  93. u8 *src;
  94. int n;
  95. if (!len)
  96. return;
  97. scatterwalk_start(&walk, sg);
  98. while (len) {
  99. n = scatterwalk_clamp(&walk, len);
  100. if (!n) {
  101. scatterwalk_start(&walk, scatterwalk_sg_next(walk.sg));
  102. n = scatterwalk_clamp(&walk, len);
  103. }
  104. src = scatterwalk_map(&walk, 0);
  105. crypto_gcm_ghash_update(ctx, src, n);
  106. len -= n;
  107. scatterwalk_unmap(src, 0);
  108. scatterwalk_advance(&walk, n);
  109. scatterwalk_done(&walk, 0, len);
  110. if (len)
  111. crypto_yield(ctx->flags);
  112. }
  113. }
  114. static void crypto_gcm_ghash_flush(struct crypto_gcm_ghash_ctx *ctx)
  115. {
  116. u8 *dst = ctx->buffer;
  117. if (ctx->bytes) {
  118. u8 *tmp = dst + (16 - ctx->bytes);
  119. while (ctx->bytes--)
  120. *tmp++ ^= 0;
  121. gf128mul_4k_lle((be128 *)dst, ctx->gf128);
  122. }
  123. ctx->bytes = 0;
  124. }
  125. static void crypto_gcm_ghash_final_xor(struct crypto_gcm_ghash_ctx *ctx,
  126. unsigned int authlen,
  127. unsigned int cryptlen, u8 *dst)
  128. {
  129. u8 *buf = ctx->buffer;
  130. u128 lengths;
  131. lengths.a = cpu_to_be64(authlen * 8);
  132. lengths.b = cpu_to_be64(cryptlen * 8);
  133. crypto_gcm_ghash_flush(ctx);
  134. crypto_xor(buf, (u8 *)&lengths, 16);
  135. gf128mul_4k_lle((be128 *)buf, ctx->gf128);
  136. crypto_xor(dst, buf, 16);
  137. }
  138. static void crypto_gcm_setkey_done(struct crypto_async_request *req, int err)
  139. {
  140. struct crypto_gcm_setkey_result *result = req->data;
  141. if (err == -EINPROGRESS)
  142. return;
  143. result->err = err;
  144. complete(&result->completion);
  145. }
  146. static int crypto_gcm_setkey(struct crypto_aead *aead, const u8 *key,
  147. unsigned int keylen)
  148. {
  149. struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
  150. struct crypto_ablkcipher *ctr = ctx->ctr;
  151. struct {
  152. be128 hash;
  153. u8 iv[8];
  154. struct crypto_gcm_setkey_result result;
  155. struct scatterlist sg[1];
  156. struct ablkcipher_request req;
  157. } *data;
  158. int err;
  159. crypto_ablkcipher_clear_flags(ctr, CRYPTO_TFM_REQ_MASK);
  160. crypto_ablkcipher_set_flags(ctr, crypto_aead_get_flags(aead) &
  161. CRYPTO_TFM_REQ_MASK);
  162. err = crypto_ablkcipher_setkey(ctr, key, keylen);
  163. if (err)
  164. return err;
  165. crypto_aead_set_flags(aead, crypto_ablkcipher_get_flags(ctr) &
  166. CRYPTO_TFM_RES_MASK);
  167. data = kzalloc(sizeof(*data) + crypto_ablkcipher_reqsize(ctr),
  168. GFP_KERNEL);
  169. if (!data)
  170. return -ENOMEM;
  171. init_completion(&data->result.completion);
  172. sg_init_one(data->sg, &data->hash, sizeof(data->hash));
  173. ablkcipher_request_set_tfm(&data->req, ctr);
  174. ablkcipher_request_set_callback(&data->req, CRYPTO_TFM_REQ_MAY_SLEEP |
  175. CRYPTO_TFM_REQ_MAY_BACKLOG,
  176. crypto_gcm_setkey_done,
  177. &data->result);
  178. ablkcipher_request_set_crypt(&data->req, data->sg, data->sg,
  179. sizeof(data->hash), data->iv);
  180. err = crypto_ablkcipher_encrypt(&data->req);
  181. if (err == -EINPROGRESS || err == -EBUSY) {
  182. err = wait_for_completion_interruptible(
  183. &data->result.completion);
  184. if (!err)
  185. err = data->result.err;
  186. }
  187. if (err)
  188. goto out;
  189. if (ctx->gf128 != NULL)
  190. gf128mul_free_4k(ctx->gf128);
  191. ctx->gf128 = gf128mul_init_4k_lle(&data->hash);
  192. if (ctx->gf128 == NULL)
  193. err = -ENOMEM;
  194. out:
  195. kfree(data);
  196. return err;
  197. }
  198. static int crypto_gcm_setauthsize(struct crypto_aead *tfm,
  199. unsigned int authsize)
  200. {
  201. switch (authsize) {
  202. case 4:
  203. case 8:
  204. case 12:
  205. case 13:
  206. case 14:
  207. case 15:
  208. case 16:
  209. break;
  210. default:
  211. return -EINVAL;
  212. }
  213. return 0;
  214. }
  215. static void crypto_gcm_init_crypt(struct ablkcipher_request *ablk_req,
  216. struct aead_request *req,
  217. unsigned int cryptlen)
  218. {
  219. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  220. struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
  221. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  222. u32 flags = req->base.tfm->crt_flags;
  223. struct crypto_gcm_ghash_ctx *ghash = &pctx->ghash;
  224. struct scatterlist *dst;
  225. __be32 counter = cpu_to_be32(1);
  226. memset(pctx->auth_tag, 0, sizeof(pctx->auth_tag));
  227. memcpy(req->iv + 12, &counter, 4);
  228. sg_init_table(pctx->src, 2);
  229. sg_set_buf(pctx->src, pctx->auth_tag, sizeof(pctx->auth_tag));
  230. scatterwalk_sg_chain(pctx->src, 2, req->src);
  231. dst = pctx->src;
  232. if (req->src != req->dst) {
  233. sg_init_table(pctx->dst, 2);
  234. sg_set_buf(pctx->dst, pctx->auth_tag, sizeof(pctx->auth_tag));
  235. scatterwalk_sg_chain(pctx->dst, 2, req->dst);
  236. dst = pctx->dst;
  237. }
  238. ablkcipher_request_set_tfm(ablk_req, ctx->ctr);
  239. ablkcipher_request_set_crypt(ablk_req, pctx->src, dst,
  240. cryptlen + sizeof(pctx->auth_tag),
  241. req->iv);
  242. crypto_gcm_ghash_init(ghash, flags, ctx->gf128);
  243. crypto_gcm_ghash_update_sg(ghash, req->assoc, req->assoclen);
  244. crypto_gcm_ghash_flush(ghash);
  245. }
  246. static int crypto_gcm_hash(struct aead_request *req)
  247. {
  248. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  249. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  250. u8 *auth_tag = pctx->auth_tag;
  251. struct crypto_gcm_ghash_ctx *ghash = &pctx->ghash;
  252. crypto_gcm_ghash_update_sg(ghash, req->dst, req->cryptlen);
  253. crypto_gcm_ghash_final_xor(ghash, req->assoclen, req->cryptlen,
  254. auth_tag);
  255. scatterwalk_map_and_copy(auth_tag, req->dst, req->cryptlen,
  256. crypto_aead_authsize(aead), 1);
  257. return 0;
  258. }
  259. static void crypto_gcm_encrypt_done(struct crypto_async_request *areq, int err)
  260. {
  261. struct aead_request *req = areq->data;
  262. if (!err)
  263. err = crypto_gcm_hash(req);
  264. aead_request_complete(req, err);
  265. }
  266. static int crypto_gcm_encrypt(struct aead_request *req)
  267. {
  268. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  269. struct ablkcipher_request *abreq = &pctx->abreq;
  270. int err;
  271. crypto_gcm_init_crypt(abreq, req, req->cryptlen);
  272. ablkcipher_request_set_callback(abreq, aead_request_flags(req),
  273. crypto_gcm_encrypt_done, req);
  274. err = crypto_ablkcipher_encrypt(abreq);
  275. if (err)
  276. return err;
  277. return crypto_gcm_hash(req);
  278. }
  279. static int crypto_gcm_verify(struct aead_request *req)
  280. {
  281. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  282. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  283. struct crypto_gcm_ghash_ctx *ghash = &pctx->ghash;
  284. u8 *auth_tag = pctx->auth_tag;
  285. u8 *iauth_tag = pctx->iauth_tag;
  286. unsigned int authsize = crypto_aead_authsize(aead);
  287. unsigned int cryptlen = req->cryptlen - authsize;
  288. crypto_gcm_ghash_final_xor(ghash, req->assoclen, cryptlen, auth_tag);
  289. authsize = crypto_aead_authsize(aead);
  290. scatterwalk_map_and_copy(iauth_tag, req->src, cryptlen, authsize, 0);
  291. return memcmp(iauth_tag, auth_tag, authsize) ? -EBADMSG : 0;
  292. }
  293. static void crypto_gcm_decrypt_done(struct crypto_async_request *areq, int err)
  294. {
  295. struct aead_request *req = areq->data;
  296. if (!err)
  297. err = crypto_gcm_verify(req);
  298. aead_request_complete(req, err);
  299. }
  300. static int crypto_gcm_decrypt(struct aead_request *req)
  301. {
  302. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  303. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  304. struct ablkcipher_request *abreq = &pctx->abreq;
  305. struct crypto_gcm_ghash_ctx *ghash = &pctx->ghash;
  306. unsigned int cryptlen = req->cryptlen;
  307. unsigned int authsize = crypto_aead_authsize(aead);
  308. int err;
  309. if (cryptlen < authsize)
  310. return -EINVAL;
  311. cryptlen -= authsize;
  312. crypto_gcm_init_crypt(abreq, req, cryptlen);
  313. ablkcipher_request_set_callback(abreq, aead_request_flags(req),
  314. crypto_gcm_decrypt_done, req);
  315. crypto_gcm_ghash_update_sg(ghash, req->src, cryptlen);
  316. err = crypto_ablkcipher_decrypt(abreq);
  317. if (err)
  318. return err;
  319. return crypto_gcm_verify(req);
  320. }
  321. static int crypto_gcm_init_tfm(struct crypto_tfm *tfm)
  322. {
  323. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  324. struct gcm_instance_ctx *ictx = crypto_instance_ctx(inst);
  325. struct crypto_gcm_ctx *ctx = crypto_tfm_ctx(tfm);
  326. struct crypto_ablkcipher *ctr;
  327. unsigned long align;
  328. int err;
  329. ctr = crypto_spawn_skcipher(&ictx->ctr);
  330. err = PTR_ERR(ctr);
  331. if (IS_ERR(ctr))
  332. return err;
  333. ctx->ctr = ctr;
  334. ctx->gf128 = NULL;
  335. align = crypto_tfm_alg_alignmask(tfm);
  336. align &= ~(crypto_tfm_ctx_alignment() - 1);
  337. tfm->crt_aead.reqsize = align +
  338. sizeof(struct crypto_gcm_req_priv_ctx) +
  339. crypto_ablkcipher_reqsize(ctr);
  340. return 0;
  341. }
  342. static void crypto_gcm_exit_tfm(struct crypto_tfm *tfm)
  343. {
  344. struct crypto_gcm_ctx *ctx = crypto_tfm_ctx(tfm);
  345. if (ctx->gf128 != NULL)
  346. gf128mul_free_4k(ctx->gf128);
  347. crypto_free_ablkcipher(ctx->ctr);
  348. }
  349. static struct crypto_instance *crypto_gcm_alloc_common(struct rtattr **tb,
  350. const char *full_name,
  351. const char *ctr_name)
  352. {
  353. struct crypto_attr_type *algt;
  354. struct crypto_instance *inst;
  355. struct crypto_alg *ctr;
  356. struct gcm_instance_ctx *ctx;
  357. int err;
  358. algt = crypto_get_attr_type(tb);
  359. err = PTR_ERR(algt);
  360. if (IS_ERR(algt))
  361. return ERR_PTR(err);
  362. if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
  363. return ERR_PTR(-EINVAL);
  364. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  365. if (!inst)
  366. return ERR_PTR(-ENOMEM);
  367. ctx = crypto_instance_ctx(inst);
  368. crypto_set_skcipher_spawn(&ctx->ctr, inst);
  369. err = crypto_grab_skcipher(&ctx->ctr, ctr_name, 0,
  370. crypto_requires_sync(algt->type,
  371. algt->mask));
  372. if (err)
  373. goto err_free_inst;
  374. ctr = crypto_skcipher_spawn_alg(&ctx->ctr);
  375. /* We only support 16-byte blocks. */
  376. if (ctr->cra_ablkcipher.ivsize != 16)
  377. goto out_put_ctr;
  378. /* Not a stream cipher? */
  379. err = -EINVAL;
  380. if (ctr->cra_blocksize != 1)
  381. goto out_put_ctr;
  382. err = -ENAMETOOLONG;
  383. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  384. "gcm_base(%s)", ctr->cra_driver_name) >=
  385. CRYPTO_MAX_ALG_NAME)
  386. goto out_put_ctr;
  387. memcpy(inst->alg.cra_name, full_name, CRYPTO_MAX_ALG_NAME);
  388. inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD;
  389. inst->alg.cra_flags |= ctr->cra_flags & CRYPTO_ALG_ASYNC;
  390. inst->alg.cra_priority = ctr->cra_priority;
  391. inst->alg.cra_blocksize = 1;
  392. inst->alg.cra_alignmask = ctr->cra_alignmask | (__alignof__(u64) - 1);
  393. inst->alg.cra_type = &crypto_aead_type;
  394. inst->alg.cra_aead.ivsize = 16;
  395. inst->alg.cra_aead.maxauthsize = 16;
  396. inst->alg.cra_ctxsize = sizeof(struct crypto_gcm_ctx);
  397. inst->alg.cra_init = crypto_gcm_init_tfm;
  398. inst->alg.cra_exit = crypto_gcm_exit_tfm;
  399. inst->alg.cra_aead.setkey = crypto_gcm_setkey;
  400. inst->alg.cra_aead.setauthsize = crypto_gcm_setauthsize;
  401. inst->alg.cra_aead.encrypt = crypto_gcm_encrypt;
  402. inst->alg.cra_aead.decrypt = crypto_gcm_decrypt;
  403. out:
  404. return inst;
  405. out_put_ctr:
  406. crypto_drop_skcipher(&ctx->ctr);
  407. err_free_inst:
  408. kfree(inst);
  409. inst = ERR_PTR(err);
  410. goto out;
  411. }
  412. static struct crypto_instance *crypto_gcm_alloc(struct rtattr **tb)
  413. {
  414. int err;
  415. const char *cipher_name;
  416. char ctr_name[CRYPTO_MAX_ALG_NAME];
  417. char full_name[CRYPTO_MAX_ALG_NAME];
  418. cipher_name = crypto_attr_alg_name(tb[1]);
  419. err = PTR_ERR(cipher_name);
  420. if (IS_ERR(cipher_name))
  421. return ERR_PTR(err);
  422. if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)", cipher_name) >=
  423. CRYPTO_MAX_ALG_NAME)
  424. return ERR_PTR(-ENAMETOOLONG);
  425. if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "gcm(%s)", cipher_name) >=
  426. CRYPTO_MAX_ALG_NAME)
  427. return ERR_PTR(-ENAMETOOLONG);
  428. return crypto_gcm_alloc_common(tb, full_name, ctr_name);
  429. }
  430. static void crypto_gcm_free(struct crypto_instance *inst)
  431. {
  432. struct gcm_instance_ctx *ctx = crypto_instance_ctx(inst);
  433. crypto_drop_skcipher(&ctx->ctr);
  434. kfree(inst);
  435. }
  436. static struct crypto_template crypto_gcm_tmpl = {
  437. .name = "gcm",
  438. .alloc = crypto_gcm_alloc,
  439. .free = crypto_gcm_free,
  440. .module = THIS_MODULE,
  441. };
  442. static struct crypto_instance *crypto_gcm_base_alloc(struct rtattr **tb)
  443. {
  444. int err;
  445. const char *ctr_name;
  446. char full_name[CRYPTO_MAX_ALG_NAME];
  447. ctr_name = crypto_attr_alg_name(tb[1]);
  448. err = PTR_ERR(ctr_name);
  449. if (IS_ERR(ctr_name))
  450. return ERR_PTR(err);
  451. if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "gcm_base(%s)",
  452. ctr_name) >= CRYPTO_MAX_ALG_NAME)
  453. return ERR_PTR(-ENAMETOOLONG);
  454. return crypto_gcm_alloc_common(tb, full_name, ctr_name);
  455. }
  456. static struct crypto_template crypto_gcm_base_tmpl = {
  457. .name = "gcm_base",
  458. .alloc = crypto_gcm_base_alloc,
  459. .free = crypto_gcm_free,
  460. .module = THIS_MODULE,
  461. };
  462. static int crypto_rfc4106_setkey(struct crypto_aead *parent, const u8 *key,
  463. unsigned int keylen)
  464. {
  465. struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
  466. struct crypto_aead *child = ctx->child;
  467. int err;
  468. if (keylen < 4)
  469. return -EINVAL;
  470. keylen -= 4;
  471. memcpy(ctx->nonce, key + keylen, 4);
  472. crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  473. crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
  474. CRYPTO_TFM_REQ_MASK);
  475. err = crypto_aead_setkey(child, key, keylen);
  476. crypto_aead_set_flags(parent, crypto_aead_get_flags(child) &
  477. CRYPTO_TFM_RES_MASK);
  478. return err;
  479. }
  480. static int crypto_rfc4106_setauthsize(struct crypto_aead *parent,
  481. unsigned int authsize)
  482. {
  483. struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
  484. switch (authsize) {
  485. case 8:
  486. case 12:
  487. case 16:
  488. break;
  489. default:
  490. return -EINVAL;
  491. }
  492. return crypto_aead_setauthsize(ctx->child, authsize);
  493. }
  494. static struct aead_request *crypto_rfc4106_crypt(struct aead_request *req)
  495. {
  496. struct aead_request *subreq = aead_request_ctx(req);
  497. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  498. struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(aead);
  499. struct crypto_aead *child = ctx->child;
  500. u8 *iv = PTR_ALIGN((u8 *)(subreq + 1) + crypto_aead_reqsize(child),
  501. crypto_aead_alignmask(child) + 1);
  502. memcpy(iv, ctx->nonce, 4);
  503. memcpy(iv + 4, req->iv, 8);
  504. aead_request_set_tfm(subreq, child);
  505. aead_request_set_callback(subreq, req->base.flags, req->base.complete,
  506. req->base.data);
  507. aead_request_set_crypt(subreq, req->src, req->dst, req->cryptlen, iv);
  508. aead_request_set_assoc(subreq, req->assoc, req->assoclen);
  509. return subreq;
  510. }
  511. static int crypto_rfc4106_encrypt(struct aead_request *req)
  512. {
  513. req = crypto_rfc4106_crypt(req);
  514. return crypto_aead_encrypt(req);
  515. }
  516. static int crypto_rfc4106_decrypt(struct aead_request *req)
  517. {
  518. req = crypto_rfc4106_crypt(req);
  519. return crypto_aead_decrypt(req);
  520. }
  521. static int crypto_rfc4106_init_tfm(struct crypto_tfm *tfm)
  522. {
  523. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  524. struct crypto_aead_spawn *spawn = crypto_instance_ctx(inst);
  525. struct crypto_rfc4106_ctx *ctx = crypto_tfm_ctx(tfm);
  526. struct crypto_aead *aead;
  527. unsigned long align;
  528. aead = crypto_spawn_aead(spawn);
  529. if (IS_ERR(aead))
  530. return PTR_ERR(aead);
  531. ctx->child = aead;
  532. align = crypto_aead_alignmask(aead);
  533. align &= ~(crypto_tfm_ctx_alignment() - 1);
  534. tfm->crt_aead.reqsize = sizeof(struct aead_request) +
  535. ALIGN(crypto_aead_reqsize(aead),
  536. crypto_tfm_ctx_alignment()) +
  537. align + 16;
  538. return 0;
  539. }
  540. static void crypto_rfc4106_exit_tfm(struct crypto_tfm *tfm)
  541. {
  542. struct crypto_rfc4106_ctx *ctx = crypto_tfm_ctx(tfm);
  543. crypto_free_aead(ctx->child);
  544. }
  545. static struct crypto_instance *crypto_rfc4106_alloc(struct rtattr **tb)
  546. {
  547. struct crypto_attr_type *algt;
  548. struct crypto_instance *inst;
  549. struct crypto_aead_spawn *spawn;
  550. struct crypto_alg *alg;
  551. const char *ccm_name;
  552. int err;
  553. algt = crypto_get_attr_type(tb);
  554. err = PTR_ERR(algt);
  555. if (IS_ERR(algt))
  556. return ERR_PTR(err);
  557. if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
  558. return ERR_PTR(-EINVAL);
  559. ccm_name = crypto_attr_alg_name(tb[1]);
  560. err = PTR_ERR(ccm_name);
  561. if (IS_ERR(ccm_name))
  562. return ERR_PTR(err);
  563. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  564. if (!inst)
  565. return ERR_PTR(-ENOMEM);
  566. spawn = crypto_instance_ctx(inst);
  567. crypto_set_aead_spawn(spawn, inst);
  568. err = crypto_grab_aead(spawn, ccm_name, 0,
  569. crypto_requires_sync(algt->type, algt->mask));
  570. if (err)
  571. goto out_free_inst;
  572. alg = crypto_aead_spawn_alg(spawn);
  573. err = -EINVAL;
  574. /* We only support 16-byte blocks. */
  575. if (alg->cra_aead.ivsize != 16)
  576. goto out_drop_alg;
  577. /* Not a stream cipher? */
  578. if (alg->cra_blocksize != 1)
  579. goto out_drop_alg;
  580. err = -ENAMETOOLONG;
  581. if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
  582. "rfc4106(%s)", alg->cra_name) >= CRYPTO_MAX_ALG_NAME ||
  583. snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  584. "rfc4106(%s)", alg->cra_driver_name) >=
  585. CRYPTO_MAX_ALG_NAME)
  586. goto out_drop_alg;
  587. inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD;
  588. inst->alg.cra_flags |= alg->cra_flags & CRYPTO_ALG_ASYNC;
  589. inst->alg.cra_priority = alg->cra_priority;
  590. inst->alg.cra_blocksize = 1;
  591. inst->alg.cra_alignmask = alg->cra_alignmask;
  592. inst->alg.cra_type = &crypto_nivaead_type;
  593. inst->alg.cra_aead.ivsize = 8;
  594. inst->alg.cra_aead.maxauthsize = 16;
  595. inst->alg.cra_ctxsize = sizeof(struct crypto_rfc4106_ctx);
  596. inst->alg.cra_init = crypto_rfc4106_init_tfm;
  597. inst->alg.cra_exit = crypto_rfc4106_exit_tfm;
  598. inst->alg.cra_aead.setkey = crypto_rfc4106_setkey;
  599. inst->alg.cra_aead.setauthsize = crypto_rfc4106_setauthsize;
  600. inst->alg.cra_aead.encrypt = crypto_rfc4106_encrypt;
  601. inst->alg.cra_aead.decrypt = crypto_rfc4106_decrypt;
  602. inst->alg.cra_aead.geniv = "seqiv";
  603. out:
  604. return inst;
  605. out_drop_alg:
  606. crypto_drop_aead(spawn);
  607. out_free_inst:
  608. kfree(inst);
  609. inst = ERR_PTR(err);
  610. goto out;
  611. }
  612. static void crypto_rfc4106_free(struct crypto_instance *inst)
  613. {
  614. crypto_drop_spawn(crypto_instance_ctx(inst));
  615. kfree(inst);
  616. }
  617. static struct crypto_template crypto_rfc4106_tmpl = {
  618. .name = "rfc4106",
  619. .alloc = crypto_rfc4106_alloc,
  620. .free = crypto_rfc4106_free,
  621. .module = THIS_MODULE,
  622. };
  623. static int __init crypto_gcm_module_init(void)
  624. {
  625. int err;
  626. err = crypto_register_template(&crypto_gcm_base_tmpl);
  627. if (err)
  628. goto out;
  629. err = crypto_register_template(&crypto_gcm_tmpl);
  630. if (err)
  631. goto out_undo_base;
  632. err = crypto_register_template(&crypto_rfc4106_tmpl);
  633. if (err)
  634. goto out_undo_gcm;
  635. out:
  636. return err;
  637. out_undo_gcm:
  638. crypto_unregister_template(&crypto_gcm_tmpl);
  639. out_undo_base:
  640. crypto_unregister_template(&crypto_gcm_base_tmpl);
  641. goto out;
  642. }
  643. static void __exit crypto_gcm_module_exit(void)
  644. {
  645. crypto_unregister_template(&crypto_rfc4106_tmpl);
  646. crypto_unregister_template(&crypto_gcm_tmpl);
  647. crypto_unregister_template(&crypto_gcm_base_tmpl);
  648. }
  649. module_init(crypto_gcm_module_init);
  650. module_exit(crypto_gcm_module_exit);
  651. MODULE_LICENSE("GPL");
  652. MODULE_DESCRIPTION("Galois/Counter Mode");
  653. MODULE_AUTHOR("Mikko Herranen <mh1@iki.fi>");
  654. MODULE_ALIAS("gcm_base");
  655. MODULE_ALIAS("rfc4106");