2 * GCM: Galois/Counter Mode.
4 * Copyright (c) 2007 Nokia Siemens Networks - Mikko Herranen <mh1@iki.fi>
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.
11 #include <crypto/algapi.h>
12 #include <crypto/gf128mul.h>
13 #include <crypto/scatterwalk.h>
14 #include <linux/err.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
22 struct gcm_instance_ctx {
23 struct crypto_spawn ctr;
26 struct crypto_gcm_ctx {
27 struct crypto_ablkcipher *ctr;
28 struct gf128mul_4k *gf128;
31 struct crypto_gcm_ghash_ctx {
34 struct gf128mul_4k *gf128;
38 struct crypto_gcm_req_priv_ctx {
42 struct crypto_gcm_ghash_ctx ghash;
43 struct ablkcipher_request abreq;
46 static void crypto_gcm_ghash_init(struct crypto_gcm_ghash_ctx *ctx, u32 flags,
47 struct gf128mul_4k *gf128)
52 memset(ctx->buffer, 0, 16);
55 static void crypto_gcm_ghash_update(struct crypto_gcm_ghash_ctx *ctx,
56 const u8 *src, unsigned int srclen)
58 u8 *dst = ctx->buffer;
61 int n = min(srclen, ctx->bytes);
62 u8 *pos = dst + (16 - ctx->bytes);
71 gf128mul_4k_lle((be128 *)dst, ctx->gf128);
74 while (srclen >= 16) {
75 crypto_xor(dst, src, 16);
76 gf128mul_4k_lle((be128 *)dst, ctx->gf128);
82 ctx->bytes = 16 - srclen;
88 static void crypto_gcm_ghash_update_sg(struct crypto_gcm_ghash_ctx *ctx,
89 struct scatterlist *sg, int len)
91 struct scatter_walk walk;
98 scatterwalk_start(&walk, sg);
101 n = scatterwalk_clamp(&walk, len);
104 scatterwalk_start(&walk, scatterwalk_sg_next(walk.sg));
105 n = scatterwalk_clamp(&walk, len);
108 src = scatterwalk_map(&walk, 0);
110 crypto_gcm_ghash_update(ctx, src, n);
113 scatterwalk_unmap(src, 0);
114 scatterwalk_advance(&walk, n);
115 scatterwalk_done(&walk, 0, len);
117 crypto_yield(ctx->flags);
121 static void crypto_gcm_ghash_flush(struct crypto_gcm_ghash_ctx *ctx)
123 u8 *dst = ctx->buffer;
126 u8 *tmp = dst + (16 - ctx->bytes);
131 gf128mul_4k_lle((be128 *)dst, ctx->gf128);
137 static void crypto_gcm_ghash_final_xor(struct crypto_gcm_ghash_ctx *ctx,
138 unsigned int authlen,
139 unsigned int cryptlen, u8 *dst)
141 u8 *buf = ctx->buffer;
144 lengths.a = cpu_to_be64(authlen * 8);
145 lengths.b = cpu_to_be64(cryptlen * 8);
147 crypto_gcm_ghash_flush(ctx);
148 crypto_xor(buf, (u8 *)&lengths, 16);
149 gf128mul_4k_lle((be128 *)buf, ctx->gf128);
150 crypto_xor(dst, buf, 16);
153 static inline void crypto_gcm_set_counter(u8 *counterblock, u32 value)
155 *((u32 *)&counterblock[12]) = cpu_to_be32(value);
158 static int crypto_gcm_encrypt_counter(struct crypto_aead *aead, u8 *block,
159 u32 value, const u8 *iv)
161 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
162 struct crypto_ablkcipher *ctr = ctx->ctr;
163 struct ablkcipher_request req;
164 struct scatterlist sg;
168 memset(counterblock, 0, 12);
170 memcpy(counterblock, iv, 12);
172 crypto_gcm_set_counter(counterblock, value);
174 sg_init_one(&sg, block, 16);
175 ablkcipher_request_set_tfm(&req, ctr);
176 ablkcipher_request_set_crypt(&req, &sg, &sg, 16, counterblock);
177 ablkcipher_request_set_callback(&req, 0, NULL, NULL);
178 memset(block, 0, 16);
179 return crypto_ablkcipher_encrypt(&req);
182 static int crypto_gcm_setkey(struct crypto_aead *aead, const u8 *key,
185 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
186 struct crypto_ablkcipher *ctr = ctx->ctr;
187 int alignmask = crypto_ablkcipher_alignmask(ctr);
188 u8 alignbuf[16+alignmask];
189 u8 *hash = (u8 *)ALIGN((unsigned long)alignbuf, alignmask+1);
192 crypto_ablkcipher_clear_flags(ctr, CRYPTO_TFM_REQ_MASK);
193 crypto_ablkcipher_set_flags(ctr, crypto_aead_get_flags(aead) &
194 CRYPTO_TFM_REQ_MASK);
196 err = crypto_ablkcipher_setkey(ctr, key, keylen);
200 crypto_aead_set_flags(aead, crypto_ablkcipher_get_flags(ctr) &
201 CRYPTO_TFM_RES_MASK);
203 err = crypto_gcm_encrypt_counter(aead, hash, -1, NULL);
207 if (ctx->gf128 != NULL)
208 gf128mul_free_4k(ctx->gf128);
210 ctx->gf128 = gf128mul_init_4k_lle((be128 *)hash);
212 if (ctx->gf128 == NULL)
219 static int crypto_gcm_init_crypt(struct ablkcipher_request *ablk_req,
220 struct aead_request *req,
221 unsigned int cryptlen,
222 void (*done)(struct crypto_async_request *,
225 struct crypto_aead *aead = crypto_aead_reqtfm(req);
226 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
227 struct crypto_gcm_req_priv_ctx *pctx = aead_request_ctx(req);
228 u32 flags = req->base.tfm->crt_flags;
229 u8 *auth_tag = pctx->auth_tag;
230 u8 *counter = pctx->counter;
231 struct crypto_gcm_ghash_ctx *ghash = &pctx->ghash;
234 ablkcipher_request_set_tfm(ablk_req, ctx->ctr);
235 ablkcipher_request_set_callback(ablk_req, aead_request_flags(req),
237 ablkcipher_request_set_crypt(ablk_req, req->src, req->dst,
240 err = crypto_gcm_encrypt_counter(aead, auth_tag, 0, req->iv);
244 memcpy(counter, req->iv, 12);
245 crypto_gcm_set_counter(counter, 1);
247 crypto_gcm_ghash_init(ghash, flags, ctx->gf128);
249 crypto_gcm_ghash_update_sg(ghash, req->assoc, req->assoclen);
250 crypto_gcm_ghash_flush(ghash);
256 static int crypto_gcm_hash(struct aead_request *req)
258 struct crypto_aead *aead = crypto_aead_reqtfm(req);
259 struct crypto_gcm_req_priv_ctx *pctx = aead_request_ctx(req);
260 u8 *auth_tag = pctx->auth_tag;
261 struct crypto_gcm_ghash_ctx *ghash = &pctx->ghash;
263 crypto_gcm_ghash_update_sg(ghash, req->dst, req->cryptlen);
264 crypto_gcm_ghash_final_xor(ghash, req->assoclen, req->cryptlen,
267 scatterwalk_map_and_copy(auth_tag, req->dst, req->cryptlen,
268 crypto_aead_authsize(aead), 1);
272 static void crypto_gcm_encrypt_done(struct crypto_async_request *areq, int err)
274 struct aead_request *req = areq->data;
277 err = crypto_gcm_hash(req);
279 aead_request_complete(req, err);
282 static int crypto_gcm_encrypt(struct aead_request *req)
284 struct crypto_gcm_req_priv_ctx *pctx = aead_request_ctx(req);
285 struct ablkcipher_request *abreq = &pctx->abreq;
288 err = crypto_gcm_init_crypt(abreq, req, req->cryptlen,
289 crypto_gcm_encrypt_done);
294 err = crypto_ablkcipher_encrypt(abreq);
299 return crypto_gcm_hash(req);
302 static void crypto_gcm_decrypt_done(struct crypto_async_request *areq, int err)
304 aead_request_complete(areq->data, err);
307 static int crypto_gcm_decrypt(struct aead_request *req)
309 struct crypto_aead *aead = crypto_aead_reqtfm(req);
310 struct crypto_gcm_req_priv_ctx *pctx = aead_request_ctx(req);
311 struct ablkcipher_request *abreq = &pctx->abreq;
312 u8 *auth_tag = pctx->auth_tag;
313 u8 *iauth_tag = pctx->iauth_tag;
314 struct crypto_gcm_ghash_ctx *ghash = &pctx->ghash;
315 unsigned int cryptlen = req->cryptlen;
316 unsigned int authsize = crypto_aead_authsize(aead);
319 if (cryptlen < authsize)
321 cryptlen -= authsize;
323 err = crypto_gcm_init_crypt(abreq, req, cryptlen,
324 crypto_gcm_decrypt_done);
328 crypto_gcm_ghash_update_sg(ghash, req->src, cryptlen);
329 crypto_gcm_ghash_final_xor(ghash, req->assoclen, cryptlen, auth_tag);
331 scatterwalk_map_and_copy(iauth_tag, req->src, cryptlen, authsize, 0);
332 if (memcmp(iauth_tag, auth_tag, authsize))
335 return crypto_ablkcipher_decrypt(abreq);
338 static int crypto_gcm_init_tfm(struct crypto_tfm *tfm)
340 struct crypto_instance *inst = (void *)tfm->__crt_alg;
341 struct gcm_instance_ctx *ictx = crypto_instance_ctx(inst);
342 struct crypto_gcm_ctx *ctx = crypto_tfm_ctx(tfm);
343 struct crypto_ablkcipher *ctr;
347 ctr = crypto_spawn_ablkcipher(&ictx->ctr);
355 align = max_t(unsigned long, crypto_ablkcipher_alignmask(ctr),
356 __alignof__(u32) - 1);
357 align &= ~(crypto_tfm_ctx_alignment() - 1);
358 tfm->crt_aead.reqsize = align +
359 sizeof(struct crypto_gcm_req_priv_ctx) +
360 crypto_ablkcipher_reqsize(ctr);
365 static void crypto_gcm_exit_tfm(struct crypto_tfm *tfm)
367 struct crypto_gcm_ctx *ctx = crypto_tfm_ctx(tfm);
369 if (ctx->gf128 != NULL)
370 gf128mul_free_4k(ctx->gf128);
372 crypto_free_ablkcipher(ctx->ctr);
375 static struct crypto_instance *crypto_gcm_alloc(struct rtattr **tb)
377 struct crypto_instance *inst;
378 struct crypto_alg *ctr;
379 struct crypto_alg *cipher;
380 struct gcm_instance_ctx *ctx;
382 char ctr_name[CRYPTO_MAX_ALG_NAME];
384 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD);
388 cipher = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_CIPHER,
389 CRYPTO_ALG_TYPE_MASK);
391 inst = ERR_PTR(PTR_ERR(cipher));
395 inst = ERR_PTR(ENAMETOOLONG);
397 ctr_name, CRYPTO_MAX_ALG_NAME,
398 "ctr(%s,0,16,4)", cipher->cra_name) >= CRYPTO_MAX_ALG_NAME)
401 ctr = crypto_alg_mod_lookup(ctr_name, CRYPTO_ALG_TYPE_BLKCIPHER,
402 CRYPTO_ALG_TYPE_MASK);
405 return ERR_PTR(PTR_ERR(ctr));
407 if (cipher->cra_blocksize != 16)
410 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
416 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
417 "gcm(%s)", cipher->cra_name) >= CRYPTO_MAX_ALG_NAME ||
418 snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
419 "gcm(%s)", cipher->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
423 ctx = crypto_instance_ctx(inst);
424 err = crypto_init_spawn(&ctx->ctr, ctr, inst, CRYPTO_ALG_TYPE_MASK);
428 inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC;
429 inst->alg.cra_priority = ctr->cra_priority;
430 inst->alg.cra_blocksize = 16;
431 inst->alg.cra_alignmask = __alignof__(u32) - 1;
432 inst->alg.cra_type = &crypto_aead_type;
433 inst->alg.cra_aead.ivsize = 12;
434 inst->alg.cra_aead.maxauthsize = 16;
435 inst->alg.cra_ctxsize = sizeof(struct crypto_gcm_ctx);
436 inst->alg.cra_init = crypto_gcm_init_tfm;
437 inst->alg.cra_exit = crypto_gcm_exit_tfm;
438 inst->alg.cra_aead.setkey = crypto_gcm_setkey;
439 inst->alg.cra_aead.encrypt = crypto_gcm_encrypt;
440 inst->alg.cra_aead.decrypt = crypto_gcm_decrypt;
452 static void crypto_gcm_free(struct crypto_instance *inst)
454 struct gcm_instance_ctx *ctx = crypto_instance_ctx(inst);
456 crypto_drop_spawn(&ctx->ctr);
460 static struct crypto_template crypto_gcm_tmpl = {
462 .alloc = crypto_gcm_alloc,
463 .free = crypto_gcm_free,
464 .module = THIS_MODULE,
467 static int __init crypto_gcm_module_init(void)
469 return crypto_register_template(&crypto_gcm_tmpl);
472 static void __exit crypto_gcm_module_exit(void)
474 crypto_unregister_template(&crypto_gcm_tmpl);
477 module_init(crypto_gcm_module_init);
478 module_exit(crypto_gcm_module_exit);
480 MODULE_LICENSE("GPL");
481 MODULE_DESCRIPTION("Galois/Counter Mode");
482 MODULE_AUTHOR("Mikko Herranen <mh1@iki.fi>");