[CRYPTO] salsa20_generic: Fix multi-page processing
[linux-2.6/verdex.git] / crypto / gcm.c
blob73565d607ee74832a2691d832c5ee0f6018825ae
1 /*
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.
9 */
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>
20 #include "internal.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 {
32 u32 bytes;
33 u32 flags;
34 struct gf128mul_4k *gf128;
35 u8 buffer[16];
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;
46 static void crypto_gcm_ghash_init(struct crypto_gcm_ghash_ctx *ctx, u32 flags,
47 struct gf128mul_4k *gf128)
49 ctx->bytes = 0;
50 ctx->flags = flags;
51 ctx->gf128 = 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;
60 if (ctx->bytes) {
61 int n = min(srclen, ctx->bytes);
62 u8 *pos = dst + (16 - ctx->bytes);
64 ctx->bytes -= n;
65 srclen -= n;
67 while (n--)
68 *pos++ ^= *src++;
70 if (!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);
77 src += 16;
78 srclen -= 16;
81 if (srclen) {
82 ctx->bytes = 16 - srclen;
83 while (srclen--)
84 *dst++ ^= *src++;
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;
92 u8 *src;
93 int n;
95 if (!len)
96 return;
98 scatterwalk_start(&walk, sg);
100 while (len) {
101 n = scatterwalk_clamp(&walk, len);
103 if (!n) {
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);
111 len -= n;
113 scatterwalk_unmap(src, 0);
114 scatterwalk_advance(&walk, n);
115 scatterwalk_done(&walk, 0, len);
116 if (len)
117 crypto_yield(ctx->flags);
121 static void crypto_gcm_ghash_flush(struct crypto_gcm_ghash_ctx *ctx)
123 u8 *dst = ctx->buffer;
125 if (ctx->bytes) {
126 u8 *tmp = dst + (16 - ctx->bytes);
128 while (ctx->bytes--)
129 *tmp++ ^= 0;
131 gf128mul_4k_lle((be128 *)dst, ctx->gf128);
134 ctx->bytes = 0;
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;
142 u128 lengths;
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;
165 u8 counterblock[16];
167 if (iv == NULL)
168 memset(counterblock, 0, 12);
169 else
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,
183 unsigned int keylen)
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);
190 int err = 0;
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);
197 if (err)
198 goto out;
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);
204 if (err)
205 goto out;
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)
213 err = -ENOMEM;
215 out:
216 return err;
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 *,
223 int))
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;
232 int err = 0;
234 ablkcipher_request_set_tfm(ablk_req, ctx->ctr);
235 ablkcipher_request_set_callback(ablk_req, aead_request_flags(req),
236 done, req);
237 ablkcipher_request_set_crypt(ablk_req, req->src, req->dst,
238 cryptlen, counter);
240 err = crypto_gcm_encrypt_counter(aead, auth_tag, 0, req->iv);
241 if (err)
242 goto out;
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);
252 out:
253 return err;
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,
265 auth_tag);
267 scatterwalk_map_and_copy(auth_tag, req->dst, req->cryptlen,
268 crypto_aead_authsize(aead), 1);
269 return 0;
272 static void crypto_gcm_encrypt_done(struct crypto_async_request *areq, int err)
274 struct aead_request *req = areq->data;
276 if (!err)
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;
286 int err = 0;
288 err = crypto_gcm_init_crypt(abreq, req, req->cryptlen,
289 crypto_gcm_encrypt_done);
290 if (err)
291 return err;
293 if (req->cryptlen) {
294 err = crypto_ablkcipher_encrypt(abreq);
295 if (err)
296 return err;
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);
317 int err;
319 if (cryptlen < authsize)
320 return -EINVAL;
321 cryptlen -= authsize;
323 err = crypto_gcm_init_crypt(abreq, req, cryptlen,
324 crypto_gcm_decrypt_done);
325 if (err)
326 return err;
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))
333 return -EBADMSG;
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;
344 unsigned long align;
345 int err;
347 ctr = crypto_spawn_ablkcipher(&ictx->ctr);
348 err = PTR_ERR(ctr);
349 if (IS_ERR(ctr))
350 return err;
352 ctx->ctr = ctr;
353 ctx->gf128 = NULL;
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);
362 return 0;
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;
381 int err;
382 char ctr_name[CRYPTO_MAX_ALG_NAME];
384 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD);
385 if (err)
386 return ERR_PTR(err);
388 cipher = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_CIPHER,
389 CRYPTO_ALG_TYPE_MASK);
391 inst = ERR_PTR(PTR_ERR(cipher));
392 if (IS_ERR(cipher))
393 return inst;
395 inst = ERR_PTR(ENAMETOOLONG);
396 if (snprintf(
397 ctr_name, CRYPTO_MAX_ALG_NAME,
398 "ctr(%s,0,16,4)", cipher->cra_name) >= CRYPTO_MAX_ALG_NAME)
399 return inst;
401 ctr = crypto_alg_mod_lookup(ctr_name, CRYPTO_ALG_TYPE_BLKCIPHER,
402 CRYPTO_ALG_TYPE_MASK);
404 if (IS_ERR(ctr))
405 return ERR_PTR(PTR_ERR(ctr));
407 if (cipher->cra_blocksize != 16)
408 goto out_put_ctr;
410 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
411 err = -ENOMEM;
412 if (!inst)
413 goto out_put_ctr;
415 err = -ENAMETOOLONG;
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)
420 goto err_free_inst;
423 ctx = crypto_instance_ctx(inst);
424 err = crypto_init_spawn(&ctx->ctr, ctr, inst, CRYPTO_ALG_TYPE_MASK);
425 if (err)
426 goto err_free_inst;
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;
442 out:
443 crypto_mod_put(ctr);
444 return inst;
445 err_free_inst:
446 kfree(inst);
447 out_put_ctr:
448 inst = ERR_PTR(err);
449 goto out;
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);
457 kfree(inst);
460 static struct crypto_template crypto_gcm_tmpl = {
461 .name = "gcm",
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>");