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 <linux/err.h>
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
19 #include "scatterwalk.h"
21 struct gcm_instance_ctx
{
22 struct crypto_spawn ctr
;
25 struct crypto_gcm_ctx
{
26 struct crypto_ablkcipher
*ctr
;
27 struct gf128mul_4k
*gf128
;
30 struct crypto_gcm_ghash_ctx
{
33 struct gf128mul_4k
*gf128
;
37 struct crypto_gcm_req_priv_ctx
{
40 struct crypto_gcm_ghash_ctx ghash
;
43 static void crypto_gcm_ghash_init(struct crypto_gcm_ghash_ctx
*ctx
, u32 flags
,
44 struct gf128mul_4k
*gf128
)
49 memset(ctx
->buffer
, 0, 16);
52 static void crypto_gcm_ghash_update(struct crypto_gcm_ghash_ctx
*ctx
,
53 const u8
*src
, unsigned int srclen
)
55 u8
*dst
= ctx
->buffer
;
58 int n
= min(srclen
, ctx
->bytes
);
59 u8
*pos
= dst
+ (16 - ctx
->bytes
);
68 gf128mul_4k_lle((be128
*)dst
, ctx
->gf128
);
71 while (srclen
>= 16) {
72 crypto_xor(dst
, src
, 16);
73 gf128mul_4k_lle((be128
*)dst
, ctx
->gf128
);
79 ctx
->bytes
= 16 - srclen
;
85 static void crypto_gcm_ghash_update_sg(struct crypto_gcm_ghash_ctx
*ctx
,
86 struct scatterlist
*sg
, int len
)
88 struct scatter_walk walk
;
92 scatterwalk_start(&walk
, sg
);
95 n
= scatterwalk_clamp(&walk
, len
);
98 scatterwalk_start(&walk
, sg_next(walk
.sg
));
99 n
= scatterwalk_clamp(&walk
, len
);
102 src
= scatterwalk_map(&walk
, 0);
104 crypto_gcm_ghash_update(ctx
, src
, n
);
107 scatterwalk_unmap(src
, 0);
108 scatterwalk_advance(&walk
, n
);
109 scatterwalk_done(&walk
, 0, len
);
111 crypto_yield(ctx
->flags
);
115 static void crypto_gcm_ghash_flush(struct crypto_gcm_ghash_ctx
*ctx
)
117 u8
*dst
= ctx
->buffer
;
120 u8
*tmp
= dst
+ (16 - ctx
->bytes
);
125 gf128mul_4k_lle((be128
*)dst
, ctx
->gf128
);
131 static void crypto_gcm_ghash_final_xor(struct crypto_gcm_ghash_ctx
*ctx
,
132 unsigned int authlen
,
133 unsigned int cryptlen
, u8
*dst
)
135 u8
*buf
= ctx
->buffer
;
138 lengths
.a
= cpu_to_be64(authlen
* 8);
139 lengths
.b
= cpu_to_be64(cryptlen
* 8);
141 crypto_gcm_ghash_flush(ctx
);
142 crypto_xor(buf
, (u8
*)&lengths
, 16);
143 gf128mul_4k_lle((be128
*)buf
, ctx
->gf128
);
144 crypto_xor(dst
, buf
, 16);
147 static inline void crypto_gcm_set_counter(u8
*counterblock
, u32 value
)
149 *((u32
*)&counterblock
[12]) = cpu_to_be32(value
);
152 static int crypto_gcm_encrypt_counter(struct crypto_aead
*aead
, u8
*block
,
153 u32 value
, const u8
*iv
)
155 struct crypto_gcm_ctx
*ctx
= crypto_aead_ctx(aead
);
156 struct crypto_ablkcipher
*ctr
= ctx
->ctr
;
157 struct ablkcipher_request req
;
158 struct scatterlist sg
;
162 memset(counterblock
, 0, 12);
164 memcpy(counterblock
, iv
, 12);
166 crypto_gcm_set_counter(counterblock
, value
);
168 sg_init_one(&sg
, block
, 16);
169 ablkcipher_request_set_tfm(&req
, ctr
);
170 ablkcipher_request_set_crypt(&req
, &sg
, &sg
, 16, counterblock
);
171 ablkcipher_request_set_callback(&req
, 0, NULL
, NULL
);
172 memset(block
, 0, 16);
173 return crypto_ablkcipher_encrypt(&req
);
176 static int crypto_gcm_setkey(struct crypto_aead
*aead
, const u8
*key
,
179 struct crypto_gcm_ctx
*ctx
= crypto_aead_ctx(aead
);
180 struct crypto_ablkcipher
*ctr
= ctx
->ctr
;
181 int alignmask
= crypto_ablkcipher_alignmask(ctr
);
182 u8 alignbuf
[16+alignmask
];
183 u8
*hash
= (u8
*)ALIGN((unsigned long)alignbuf
, alignmask
+1);
186 crypto_ablkcipher_clear_flags(ctr
, CRYPTO_TFM_REQ_MASK
);
187 crypto_ablkcipher_set_flags(ctr
, crypto_aead_get_flags(aead
) &
188 CRYPTO_TFM_REQ_MASK
);
190 err
= crypto_ablkcipher_setkey(ctr
, key
, keylen
);
194 crypto_aead_set_flags(aead
, crypto_ablkcipher_get_flags(ctr
) &
195 CRYPTO_TFM_RES_MASK
);
197 err
= crypto_gcm_encrypt_counter(aead
, hash
, -1, NULL
);
201 if (ctx
->gf128
!= NULL
)
202 gf128mul_free_4k(ctx
->gf128
);
204 ctx
->gf128
= gf128mul_init_4k_lle((be128
*)hash
);
206 if (ctx
->gf128
== NULL
)
213 static int crypto_gcm_init_crypt(struct ablkcipher_request
*ablk_req
,
214 struct aead_request
*req
,
215 void (*done
)(struct crypto_async_request
*,
218 struct crypto_aead
*aead
= crypto_aead_reqtfm(req
);
219 struct crypto_gcm_ctx
*ctx
= crypto_aead_ctx(aead
);
220 struct crypto_gcm_req_priv_ctx
*pctx
= aead_request_ctx(req
);
221 u32 flags
= req
->base
.tfm
->crt_flags
;
222 u8
*auth_tag
= pctx
->auth_tag
;
223 u8
*counter
= pctx
->counter
;
224 struct crypto_gcm_ghash_ctx
*ghash
= &pctx
->ghash
;
227 ablkcipher_request_set_tfm(ablk_req
, ctx
->ctr
);
228 ablkcipher_request_set_callback(ablk_req
, aead_request_flags(req
),
230 ablkcipher_request_set_crypt(ablk_req
, req
->src
, req
->dst
,
231 req
->cryptlen
, counter
);
233 err
= crypto_gcm_encrypt_counter(aead
, auth_tag
, 0, req
->iv
);
237 memcpy(counter
, req
->iv
, 12);
238 crypto_gcm_set_counter(counter
, 1);
240 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
);
251 static void crypto_gcm_encrypt_done(struct crypto_async_request
*areq
, int err
)
253 struct aead_request
*req
= areq
->data
;
254 struct crypto_gcm_req_priv_ctx
*pctx
= aead_request_ctx(req
);
255 u8
*auth_tag
= pctx
->auth_tag
;
256 struct crypto_gcm_ghash_ctx
*ghash
= &pctx
->ghash
;
258 crypto_gcm_ghash_update_sg(ghash
, req
->dst
, req
->cryptlen
);
259 crypto_gcm_ghash_final_xor(ghash
, req
->assoclen
, req
->cryptlen
,
262 aead_request_complete(req
, err
);
265 static int crypto_gcm_encrypt(struct aead_request
*req
)
267 struct ablkcipher_request abreq
;
268 struct crypto_gcm_req_priv_ctx
*pctx
= aead_request_ctx(req
);
269 u8
*auth_tag
= pctx
->auth_tag
;
270 struct crypto_gcm_ghash_ctx
*ghash
= &pctx
->ghash
;
273 err
= crypto_gcm_init_crypt(&abreq
, req
, crypto_gcm_encrypt_done
);
278 err
= crypto_ablkcipher_encrypt(&abreq
);
282 crypto_gcm_ghash_update_sg(ghash
, req
->dst
, req
->cryptlen
);
285 crypto_gcm_ghash_final_xor(ghash
, req
->assoclen
, req
->cryptlen
,
291 static void crypto_gcm_decrypt_done(struct crypto_async_request
*areq
, int err
)
293 aead_request_complete(areq
->data
, err
);
296 static int crypto_gcm_decrypt(struct aead_request
*req
)
298 struct ablkcipher_request abreq
;
299 struct crypto_gcm_req_priv_ctx
*pctx
= aead_request_ctx(req
);
300 u8
*auth_tag
= pctx
->auth_tag
;
301 struct crypto_gcm_ghash_ctx
*ghash
= &pctx
->ghash
;
308 memcpy(tag
, auth_tag
, 16);
309 err
= crypto_gcm_init_crypt(&abreq
, req
, crypto_gcm_decrypt_done
);
313 crypto_gcm_ghash_update_sg(ghash
, req
->src
, req
->cryptlen
);
314 crypto_gcm_ghash_final_xor(ghash
, req
->assoclen
, req
->cryptlen
,
317 if (memcmp(tag
, auth_tag
, 16))
320 return crypto_ablkcipher_decrypt(&abreq
);
323 static int crypto_gcm_init_tfm(struct crypto_tfm
*tfm
)
325 struct crypto_instance
*inst
= (void *)tfm
->__crt_alg
;
326 struct gcm_instance_ctx
*ictx
= crypto_instance_ctx(inst
);
327 struct crypto_gcm_ctx
*ctx
= crypto_tfm_ctx(tfm
);
328 struct crypto_ablkcipher
*ctr
;
332 ctr
= crypto_spawn_ablkcipher(&ictx
->ctr
);
340 align
= max_t(unsigned long, crypto_ablkcipher_alignmask(ctr
),
341 __alignof__(u32
) - 1);
342 align
&= ~(crypto_tfm_ctx_alignment() - 1);
343 tfm
->crt_aead
.reqsize
= align
+ sizeof(struct crypto_gcm_req_priv_ctx
);
348 static void crypto_gcm_exit_tfm(struct crypto_tfm
*tfm
)
350 struct crypto_gcm_ctx
*ctx
= crypto_tfm_ctx(tfm
);
352 if (ctx
->gf128
!= NULL
)
353 gf128mul_free_4k(ctx
->gf128
);
355 crypto_free_ablkcipher(ctx
->ctr
);
358 static struct crypto_instance
*crypto_gcm_alloc(struct rtattr
**tb
)
360 struct crypto_instance
*inst
;
361 struct crypto_alg
*ctr
;
362 struct crypto_alg
*cipher
;
363 struct gcm_instance_ctx
*ctx
;
365 char ctr_name
[CRYPTO_MAX_ALG_NAME
];
367 err
= crypto_check_attr_type(tb
, CRYPTO_ALG_TYPE_AEAD
);
371 cipher
= crypto_attr_alg(tb
[1], CRYPTO_ALG_TYPE_CIPHER
,
372 CRYPTO_ALG_TYPE_MASK
);
374 inst
= ERR_PTR(PTR_ERR(cipher
));
378 inst
= ERR_PTR(ENAMETOOLONG
);
380 ctr_name
, CRYPTO_MAX_ALG_NAME
,
381 "ctr(%s,0,16,4)", cipher
->cra_name
) >= CRYPTO_MAX_ALG_NAME
)
384 ctr
= crypto_alg_mod_lookup(ctr_name
, CRYPTO_ALG_TYPE_BLKCIPHER
,
385 CRYPTO_ALG_TYPE_MASK
);
388 return ERR_PTR(PTR_ERR(ctr
));
390 if (cipher
->cra_blocksize
!= 16)
393 inst
= kzalloc(sizeof(*inst
) + sizeof(*ctx
), GFP_KERNEL
);
399 if (snprintf(inst
->alg
.cra_name
, CRYPTO_MAX_ALG_NAME
,
400 "gcm(%s)", cipher
->cra_name
) >= CRYPTO_MAX_ALG_NAME
||
401 snprintf(inst
->alg
.cra_driver_name
, CRYPTO_MAX_ALG_NAME
,
402 "gcm(%s)", cipher
->cra_driver_name
) >= CRYPTO_MAX_ALG_NAME
)
406 ctx
= crypto_instance_ctx(inst
);
407 err
= crypto_init_spawn(&ctx
->ctr
, ctr
, inst
, CRYPTO_ALG_TYPE_MASK
);
411 inst
->alg
.cra_flags
= CRYPTO_ALG_TYPE_AEAD
| CRYPTO_ALG_ASYNC
;
412 inst
->alg
.cra_priority
= ctr
->cra_priority
;
413 inst
->alg
.cra_blocksize
= 16;
414 inst
->alg
.cra_alignmask
= __alignof__(u32
) - 1;
415 inst
->alg
.cra_type
= &crypto_aead_type
;
416 inst
->alg
.cra_aead
.ivsize
= 12;
417 inst
->alg
.cra_aead
.authsize
= 16;
418 inst
->alg
.cra_ctxsize
= sizeof(struct crypto_gcm_ctx
);
419 inst
->alg
.cra_init
= crypto_gcm_init_tfm
;
420 inst
->alg
.cra_exit
= crypto_gcm_exit_tfm
;
421 inst
->alg
.cra_aead
.setkey
= crypto_gcm_setkey
;
422 inst
->alg
.cra_aead
.encrypt
= crypto_gcm_encrypt
;
423 inst
->alg
.cra_aead
.decrypt
= crypto_gcm_decrypt
;
435 static void crypto_gcm_free(struct crypto_instance
*inst
)
437 struct gcm_instance_ctx
*ctx
= crypto_instance_ctx(inst
);
439 crypto_drop_spawn(&ctx
->ctr
);
443 static struct crypto_template crypto_gcm_tmpl
= {
445 .alloc
= crypto_gcm_alloc
,
446 .free
= crypto_gcm_free
,
447 .module
= THIS_MODULE
,
450 static int __init
crypto_gcm_module_init(void)
452 return crypto_register_template(&crypto_gcm_tmpl
);
455 static void __exit
crypto_gcm_module_exit(void)
457 crypto_unregister_template(&crypto_gcm_tmpl
);
460 module_init(crypto_gcm_module_init
);
461 module_exit(crypto_gcm_module_exit
);
463 MODULE_LICENSE("GPL");
464 MODULE_DESCRIPTION("Galois/Counter Mode");
465 MODULE_AUTHOR("Mikko Herranen <mh1@iki.fi>");