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