netfilter: avoid double free in nf_reinject
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / x86 / crypto / fpu.c
blob98d7a188f46b0744ea53942eeab09d609f7d4847
1 /*
2 * FPU: Wrapper for blkcipher touching fpu
4 * Copyright (c) Intel Corp.
5 * Author: Huang Ying <ying.huang@intel.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
14 #include <crypto/algapi.h>
15 #include <linux/err.h>
16 #include <linux/init.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <asm/i387.h>
22 struct crypto_fpu_ctx {
23 struct crypto_blkcipher *child;
26 static int crypto_fpu_setkey(struct crypto_tfm *parent, const u8 *key,
27 unsigned int keylen)
29 struct crypto_fpu_ctx *ctx = crypto_tfm_ctx(parent);
30 struct crypto_blkcipher *child = ctx->child;
31 int err;
33 crypto_blkcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
34 crypto_blkcipher_set_flags(child, crypto_tfm_get_flags(parent) &
35 CRYPTO_TFM_REQ_MASK);
36 err = crypto_blkcipher_setkey(child, key, keylen);
37 crypto_tfm_set_flags(parent, crypto_blkcipher_get_flags(child) &
38 CRYPTO_TFM_RES_MASK);
39 return err;
42 static int crypto_fpu_encrypt(struct blkcipher_desc *desc_in,
43 struct scatterlist *dst, struct scatterlist *src,
44 unsigned int nbytes)
46 int err;
47 struct crypto_fpu_ctx *ctx = crypto_blkcipher_ctx(desc_in->tfm);
48 struct crypto_blkcipher *child = ctx->child;
49 struct blkcipher_desc desc = {
50 .tfm = child,
51 .info = desc_in->info,
52 .flags = desc_in->flags & ~CRYPTO_TFM_REQ_MAY_SLEEP,
55 kernel_fpu_begin();
56 err = crypto_blkcipher_crt(desc.tfm)->encrypt(&desc, dst, src, nbytes);
57 kernel_fpu_end();
58 return err;
61 static int crypto_fpu_decrypt(struct blkcipher_desc *desc_in,
62 struct scatterlist *dst, struct scatterlist *src,
63 unsigned int nbytes)
65 int err;
66 struct crypto_fpu_ctx *ctx = crypto_blkcipher_ctx(desc_in->tfm);
67 struct crypto_blkcipher *child = ctx->child;
68 struct blkcipher_desc desc = {
69 .tfm = child,
70 .info = desc_in->info,
71 .flags = desc_in->flags & ~CRYPTO_TFM_REQ_MAY_SLEEP,
74 kernel_fpu_begin();
75 err = crypto_blkcipher_crt(desc.tfm)->decrypt(&desc, dst, src, nbytes);
76 kernel_fpu_end();
77 return err;
80 static int crypto_fpu_init_tfm(struct crypto_tfm *tfm)
82 struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
83 struct crypto_spawn *spawn = crypto_instance_ctx(inst);
84 struct crypto_fpu_ctx *ctx = crypto_tfm_ctx(tfm);
85 struct crypto_blkcipher *cipher;
87 cipher = crypto_spawn_blkcipher(spawn);
88 if (IS_ERR(cipher))
89 return PTR_ERR(cipher);
91 ctx->child = cipher;
92 return 0;
95 static void crypto_fpu_exit_tfm(struct crypto_tfm *tfm)
97 struct crypto_fpu_ctx *ctx = crypto_tfm_ctx(tfm);
98 crypto_free_blkcipher(ctx->child);
101 static struct crypto_instance *crypto_fpu_alloc(struct rtattr **tb)
103 struct crypto_instance *inst;
104 struct crypto_alg *alg;
105 int err;
107 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER);
108 if (err)
109 return ERR_PTR(err);
111 alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_BLKCIPHER,
112 CRYPTO_ALG_TYPE_MASK);
113 if (IS_ERR(alg))
114 return ERR_CAST(alg);
116 inst = crypto_alloc_instance("fpu", alg);
117 if (IS_ERR(inst))
118 goto out_put_alg;
120 inst->alg.cra_flags = alg->cra_flags;
121 inst->alg.cra_priority = alg->cra_priority;
122 inst->alg.cra_blocksize = alg->cra_blocksize;
123 inst->alg.cra_alignmask = alg->cra_alignmask;
124 inst->alg.cra_type = alg->cra_type;
125 inst->alg.cra_blkcipher.ivsize = alg->cra_blkcipher.ivsize;
126 inst->alg.cra_blkcipher.min_keysize = alg->cra_blkcipher.min_keysize;
127 inst->alg.cra_blkcipher.max_keysize = alg->cra_blkcipher.max_keysize;
128 inst->alg.cra_ctxsize = sizeof(struct crypto_fpu_ctx);
129 inst->alg.cra_init = crypto_fpu_init_tfm;
130 inst->alg.cra_exit = crypto_fpu_exit_tfm;
131 inst->alg.cra_blkcipher.setkey = crypto_fpu_setkey;
132 inst->alg.cra_blkcipher.encrypt = crypto_fpu_encrypt;
133 inst->alg.cra_blkcipher.decrypt = crypto_fpu_decrypt;
135 out_put_alg:
136 crypto_mod_put(alg);
137 return inst;
140 static void crypto_fpu_free(struct crypto_instance *inst)
142 crypto_drop_spawn(crypto_instance_ctx(inst));
143 kfree(inst);
146 static struct crypto_template crypto_fpu_tmpl = {
147 .name = "fpu",
148 .alloc = crypto_fpu_alloc,
149 .free = crypto_fpu_free,
150 .module = THIS_MODULE,
153 int __init crypto_fpu_init(void)
155 return crypto_register_template(&crypto_fpu_tmpl);
158 void __exit crypto_fpu_exit(void)
160 crypto_unregister_template(&crypto_fpu_tmpl);