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