gcm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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/algapi.h>
  11. #include <crypto/gf128mul.h>
  12. #include <linux/err.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include "scatterwalk.h"
  18. struct gcm_instance_ctx {
  19. struct crypto_spawn ctr;
  20. };
  21. struct crypto_gcm_ctx {
  22. struct crypto_ablkcipher *ctr;
  23. struct gf128mul_4k *gf128;
  24. };
  25. struct crypto_gcm_ghash_ctx {
  26. u32 bytes;
  27. u32 flags;
  28. struct gf128mul_4k *gf128;
  29. u8 buffer[16];
  30. };
  31. struct crypto_gcm_req_priv_ctx {
  32. u8 auth_tag[16];
  33. u8 iauth_tag[16];
  34. u8 counter[16];
  35. struct crypto_gcm_ghash_ctx ghash;
  36. };
  37. static void crypto_gcm_ghash_init(struct crypto_gcm_ghash_ctx *ctx, u32 flags,
  38. struct gf128mul_4k *gf128)
  39. {
  40. ctx->bytes = 0;
  41. ctx->flags = flags;
  42. ctx->gf128 = gf128;
  43. memset(ctx->buffer, 0, 16);
  44. }
  45. static void crypto_gcm_ghash_update(struct crypto_gcm_ghash_ctx *ctx,
  46. const u8 *src, unsigned int srclen)
  47. {
  48. u8 *dst = ctx->buffer;
  49. if (ctx->bytes) {
  50. int n = min(srclen, ctx->bytes);
  51. u8 *pos = dst + (16 - ctx->bytes);
  52. ctx->bytes -= n;
  53. srclen -= n;
  54. while (n--)
  55. *pos++ ^= *src++;
  56. if (!ctx->bytes)
  57. gf128mul_4k_lle((be128 *)dst, ctx->gf128);
  58. }
  59. while (srclen >= 16) {
  60. crypto_xor(dst, src, 16);
  61. gf128mul_4k_lle((be128 *)dst, ctx->gf128);
  62. src += 16;
  63. srclen -= 16;
  64. }
  65. if (srclen) {
  66. ctx->bytes = 16 - srclen;
  67. while (srclen--)
  68. *dst++ ^= *src++;
  69. }
  70. }
  71. static void crypto_gcm_ghash_update_sg(struct crypto_gcm_ghash_ctx *ctx,
  72. struct scatterlist *sg, int len)
  73. {
  74. struct scatter_walk walk;
  75. u8 *src;
  76. int n;
  77. if (!len)
  78. return;
  79. scatterwalk_start(&walk, sg);
  80. while (len) {
  81. n = scatterwalk_clamp(&walk, len);
  82. if (!n) {
  83. scatterwalk_start(&walk, sg_next(walk.sg));
  84. n = scatterwalk_clamp(&walk, len);
  85. }
  86. src = scatterwalk_map(&walk, 0);
  87. crypto_gcm_ghash_update(ctx, src, n);
  88. len -= n;
  89. scatterwalk_unmap(src, 0);
  90. scatterwalk_advance(&walk, n);
  91. scatterwalk_done(&walk, 0, len);
  92. if (len)
  93. crypto_yield(ctx->flags);
  94. }
  95. }
  96. static void crypto_gcm_ghash_flush(struct crypto_gcm_ghash_ctx *ctx)
  97. {
  98. u8 *dst = ctx->buffer;
  99. if (ctx->bytes) {
  100. u8 *tmp = dst + (16 - ctx->bytes);
  101. while (ctx->bytes--)
  102. *tmp++ ^= 0;
  103. gf128mul_4k_lle((be128 *)dst, ctx->gf128);
  104. }
  105. ctx->bytes = 0;
  106. }
  107. static void crypto_gcm_ghash_final_xor(struct crypto_gcm_ghash_ctx *ctx,
  108. unsigned int authlen,
  109. unsigned int cryptlen, u8 *dst)
  110. {
  111. u8 *buf = ctx->buffer;
  112. u128 lengths;
  113. lengths.a = cpu_to_be64(authlen * 8);
  114. lengths.b = cpu_to_be64(cryptlen * 8);
  115. crypto_gcm_ghash_flush(ctx);
  116. crypto_xor(buf, (u8 *)&lengths, 16);
  117. gf128mul_4k_lle((be128 *)buf, ctx->gf128);
  118. crypto_xor(dst, buf, 16);
  119. }
  120. static inline void crypto_gcm_set_counter(u8 *counterblock, u32 value)
  121. {
  122. *((u32 *)&counterblock[12]) = cpu_to_be32(value);
  123. }
  124. static int crypto_gcm_encrypt_counter(struct crypto_aead *aead, u8 *block,
  125. u32 value, const u8 *iv)
  126. {
  127. struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
  128. struct crypto_ablkcipher *ctr = ctx->ctr;
  129. struct ablkcipher_request req;
  130. struct scatterlist sg;
  131. u8 counterblock[16];
  132. if (iv == NULL)
  133. memset(counterblock, 0, 12);
  134. else
  135. memcpy(counterblock, iv, 12);
  136. crypto_gcm_set_counter(counterblock, value);
  137. sg_init_one(&sg, block, 16);
  138. ablkcipher_request_set_tfm(&req, ctr);
  139. ablkcipher_request_set_crypt(&req, &sg, &sg, 16, counterblock);
  140. ablkcipher_request_set_callback(&req, 0, NULL, NULL);
  141. memset(block, 0, 16);
  142. return crypto_ablkcipher_encrypt(&req);
  143. }
  144. static int crypto_gcm_setkey(struct crypto_aead *aead, const u8 *key,
  145. unsigned int keylen)
  146. {
  147. struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
  148. struct crypto_ablkcipher *ctr = ctx->ctr;
  149. int alignmask = crypto_ablkcipher_alignmask(ctr);
  150. u8 alignbuf[16+alignmask];
  151. u8 *hash = (u8 *)ALIGN((unsigned long)alignbuf, alignmask+1);
  152. int err = 0;
  153. crypto_ablkcipher_clear_flags(ctr, CRYPTO_TFM_REQ_MASK);
  154. crypto_ablkcipher_set_flags(ctr, crypto_aead_get_flags(aead) &
  155. CRYPTO_TFM_REQ_MASK);
  156. err = crypto_ablkcipher_setkey(ctr, key, keylen);
  157. if (err)
  158. goto out;
  159. crypto_aead_set_flags(aead, crypto_ablkcipher_get_flags(ctr) &
  160. CRYPTO_TFM_RES_MASK);
  161. err = crypto_gcm_encrypt_counter(aead, hash, -1, NULL);
  162. if (err)
  163. goto out;
  164. if (ctx->gf128 != NULL)
  165. gf128mul_free_4k(ctx->gf128);
  166. ctx->gf128 = gf128mul_init_4k_lle((be128 *)hash);
  167. if (ctx->gf128 == NULL)
  168. err = -ENOMEM;
  169. out:
  170. return err;
  171. }
  172. static int crypto_gcm_init_crypt(struct ablkcipher_request *ablk_req,
  173. struct aead_request *req,
  174. unsigned int cryptlen,
  175. void (*done)(struct crypto_async_request *,
  176. int))
  177. {
  178. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  179. struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
  180. struct crypto_gcm_req_priv_ctx *pctx = aead_request_ctx(req);
  181. u32 flags = req->base.tfm->crt_flags;
  182. u8 *auth_tag = pctx->auth_tag;
  183. u8 *counter = pctx->counter;
  184. struct crypto_gcm_ghash_ctx *ghash = &pctx->ghash;
  185. int err = 0;
  186. ablkcipher_request_set_tfm(ablk_req, ctx->ctr);
  187. ablkcipher_request_set_callback(ablk_req, aead_request_flags(req),
  188. done, req);
  189. ablkcipher_request_set_crypt(ablk_req, req->src, req->dst,
  190. cryptlen, counter);
  191. err = crypto_gcm_encrypt_counter(aead, auth_tag, 0, req->iv);
  192. if (err)
  193. goto out;
  194. memcpy(counter, req->iv, 12);
  195. crypto_gcm_set_counter(counter, 1);
  196. crypto_gcm_ghash_init(ghash, flags, ctx->gf128);
  197. crypto_gcm_ghash_update_sg(ghash, req->assoc, req->assoclen);
  198. crypto_gcm_ghash_flush(ghash);
  199. out:
  200. return err;
  201. }
  202. static int crypto_gcm_hash(struct aead_request *req)
  203. {
  204. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  205. struct crypto_gcm_req_priv_ctx *pctx = aead_request_ctx(req);
  206. u8 *auth_tag = pctx->auth_tag;
  207. struct crypto_gcm_ghash_ctx *ghash = &pctx->ghash;
  208. crypto_gcm_ghash_update_sg(ghash, req->dst, req->cryptlen);
  209. crypto_gcm_ghash_final_xor(ghash, req->assoclen, req->cryptlen,
  210. auth_tag);
  211. scatterwalk_map_and_copy(auth_tag, req->dst, req->cryptlen,
  212. crypto_aead_authsize(aead), 1);
  213. return 0;
  214. }
  215. static void crypto_gcm_encrypt_done(struct crypto_async_request *areq, int err)
  216. {
  217. struct aead_request *req = areq->data;
  218. if (!err)
  219. err = crypto_gcm_hash(req);
  220. aead_request_complete(req, err);
  221. }
  222. static int crypto_gcm_encrypt(struct aead_request *req)
  223. {
  224. struct ablkcipher_request abreq;
  225. int err = 0;
  226. err = crypto_gcm_init_crypt(&abreq, req, req->cryptlen,
  227. crypto_gcm_encrypt_done);
  228. if (err)
  229. return err;
  230. if (req->cryptlen) {
  231. err = crypto_ablkcipher_encrypt(&abreq);
  232. if (err)
  233. return err;
  234. }
  235. return crypto_gcm_hash(req);
  236. }
  237. static void crypto_gcm_decrypt_done(struct crypto_async_request *areq, int err)
  238. {
  239. aead_request_complete(areq->data, err);
  240. }
  241. static int crypto_gcm_decrypt(struct aead_request *req)
  242. {
  243. struct ablkcipher_request abreq;
  244. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  245. struct crypto_gcm_req_priv_ctx *pctx = aead_request_ctx(req);
  246. u8 *auth_tag = pctx->auth_tag;
  247. u8 *iauth_tag = pctx->iauth_tag;
  248. struct crypto_gcm_ghash_ctx *ghash = &pctx->ghash;
  249. unsigned int cryptlen = req->cryptlen;
  250. unsigned int authsize = crypto_aead_authsize(aead);
  251. int err;
  252. if (cryptlen < authsize)
  253. return -EINVAL;
  254. cryptlen -= authsize;
  255. err = crypto_gcm_init_crypt(&abreq, req, cryptlen,
  256. crypto_gcm_decrypt_done);
  257. if (err)
  258. return err;
  259. crypto_gcm_ghash_update_sg(ghash, req->src, cryptlen);
  260. crypto_gcm_ghash_final_xor(ghash, req->assoclen, cryptlen, auth_tag);
  261. scatterwalk_map_and_copy(iauth_tag, req->src, cryptlen, authsize, 0);
  262. if (memcmp(iauth_tag, auth_tag, authsize))
  263. return -EINVAL;
  264. return crypto_ablkcipher_decrypt(&abreq);
  265. }
  266. static int crypto_gcm_init_tfm(struct crypto_tfm *tfm)
  267. {
  268. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  269. struct gcm_instance_ctx *ictx = crypto_instance_ctx(inst);
  270. struct crypto_gcm_ctx *ctx = crypto_tfm_ctx(tfm);
  271. struct crypto_ablkcipher *ctr;
  272. unsigned long align;
  273. int err;
  274. ctr = crypto_spawn_ablkcipher(&ictx->ctr);
  275. err = PTR_ERR(ctr);
  276. if (IS_ERR(ctr))
  277. return err;
  278. ctx->ctr = ctr;
  279. ctx->gf128 = NULL;
  280. align = max_t(unsigned long, crypto_ablkcipher_alignmask(ctr),
  281. __alignof__(u32) - 1);
  282. align &= ~(crypto_tfm_ctx_alignment() - 1);
  283. tfm->crt_aead.reqsize = align + sizeof(struct crypto_gcm_req_priv_ctx);
  284. return 0;
  285. }
  286. static void crypto_gcm_exit_tfm(struct crypto_tfm *tfm)
  287. {
  288. struct crypto_gcm_ctx *ctx = crypto_tfm_ctx(tfm);
  289. if (ctx->gf128 != NULL)
  290. gf128mul_free_4k(ctx->gf128);
  291. crypto_free_ablkcipher(ctx->ctr);
  292. }
  293. static struct crypto_instance *crypto_gcm_alloc(struct rtattr **tb)
  294. {
  295. struct crypto_instance *inst;
  296. struct crypto_alg *ctr;
  297. struct crypto_alg *cipher;
  298. struct gcm_instance_ctx *ctx;
  299. int err;
  300. char ctr_name[CRYPTO_MAX_ALG_NAME];
  301. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD);
  302. if (err)
  303. return ERR_PTR(err);
  304. cipher = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_CIPHER,
  305. CRYPTO_ALG_TYPE_MASK);
  306. inst = ERR_PTR(PTR_ERR(cipher));
  307. if (IS_ERR(cipher))
  308. return inst;
  309. inst = ERR_PTR(ENAMETOOLONG);
  310. if (snprintf(
  311. ctr_name, CRYPTO_MAX_ALG_NAME,
  312. "ctr(%s,0,16,4)", cipher->cra_name) >= CRYPTO_MAX_ALG_NAME)
  313. return inst;
  314. ctr = crypto_alg_mod_lookup(ctr_name, CRYPTO_ALG_TYPE_BLKCIPHER,
  315. CRYPTO_ALG_TYPE_MASK);
  316. if (IS_ERR(ctr))
  317. return ERR_PTR(PTR_ERR(ctr));
  318. if (cipher->cra_blocksize != 16)
  319. goto out_put_ctr;
  320. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  321. err = -ENOMEM;
  322. if (!inst)
  323. goto out_put_ctr;
  324. err = -ENAMETOOLONG;
  325. if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
  326. "gcm(%s)", cipher->cra_name) >= CRYPTO_MAX_ALG_NAME ||
  327. snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  328. "gcm(%s)", cipher->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  329. goto err_free_inst;
  330. ctx = crypto_instance_ctx(inst);
  331. err = crypto_init_spawn(&ctx->ctr, ctr, inst, CRYPTO_ALG_TYPE_MASK);
  332. if (err)
  333. goto err_free_inst;
  334. inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC;
  335. inst->alg.cra_priority = ctr->cra_priority;
  336. inst->alg.cra_blocksize = 16;
  337. inst->alg.cra_alignmask = __alignof__(u32) - 1;
  338. inst->alg.cra_type = &crypto_aead_type;
  339. inst->alg.cra_aead.ivsize = 12;
  340. inst->alg.cra_aead.maxauthsize = 16;
  341. inst->alg.cra_ctxsize = sizeof(struct crypto_gcm_ctx);
  342. inst->alg.cra_init = crypto_gcm_init_tfm;
  343. inst->alg.cra_exit = crypto_gcm_exit_tfm;
  344. inst->alg.cra_aead.setkey = crypto_gcm_setkey;
  345. inst->alg.cra_aead.encrypt = crypto_gcm_encrypt;
  346. inst->alg.cra_aead.decrypt = crypto_gcm_decrypt;
  347. out:
  348. crypto_mod_put(ctr);
  349. return inst;
  350. err_free_inst:
  351. kfree(inst);
  352. out_put_ctr:
  353. inst = ERR_PTR(err);
  354. goto out;
  355. }
  356. static void crypto_gcm_free(struct crypto_instance *inst)
  357. {
  358. struct gcm_instance_ctx *ctx = crypto_instance_ctx(inst);
  359. crypto_drop_spawn(&ctx->ctr);
  360. kfree(inst);
  361. }
  362. static struct crypto_template crypto_gcm_tmpl = {
  363. .name = "gcm",
  364. .alloc = crypto_gcm_alloc,
  365. .free = crypto_gcm_free,
  366. .module = THIS_MODULE,
  367. };
  368. static int __init crypto_gcm_module_init(void)
  369. {
  370. return crypto_register_template(&crypto_gcm_tmpl);
  371. }
  372. static void __exit crypto_gcm_module_exit(void)
  373. {
  374. crypto_unregister_template(&crypto_gcm_tmpl);
  375. }
  376. module_init(crypto_gcm_module_init);
  377. module_exit(crypto_gcm_module_exit);
  378. MODULE_LICENSE("GPL");
  379. MODULE_DESCRIPTION("Galois/Counter Mode");
  380. MODULE_AUTHOR("Mikko Herranen <mh1@iki.fi>");