Loading...
1/*
2 * FPU: Wrapper for blkcipher touching fpu
3 *
4 * Copyright (c) Intel Corp.
5 * Author: Huang Ying <ying.huang@intel.com>
6 *
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.
11 *
12 */
13
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>
21
22struct crypto_fpu_ctx {
23 struct crypto_blkcipher *child;
24};
25
26static int crypto_fpu_setkey(struct crypto_tfm *parent, const u8 *key,
27 unsigned int keylen)
28{
29 struct crypto_fpu_ctx *ctx = crypto_tfm_ctx(parent);
30 struct crypto_blkcipher *child = ctx->child;
31 int err;
32
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;
40}
41
42static int crypto_fpu_encrypt(struct blkcipher_desc *desc_in,
43 struct scatterlist *dst, struct scatterlist *src,
44 unsigned int nbytes)
45{
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,
53 };
54
55 kernel_fpu_begin();
56 err = crypto_blkcipher_crt(desc.tfm)->encrypt(&desc, dst, src, nbytes);
57 kernel_fpu_end();
58 return err;
59}
60
61static int crypto_fpu_decrypt(struct blkcipher_desc *desc_in,
62 struct scatterlist *dst, struct scatterlist *src,
63 unsigned int nbytes)
64{
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,
72 };
73
74 kernel_fpu_begin();
75 err = crypto_blkcipher_crt(desc.tfm)->decrypt(&desc, dst, src, nbytes);
76 kernel_fpu_end();
77 return err;
78}
79
80static int crypto_fpu_init_tfm(struct crypto_tfm *tfm)
81{
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;
86
87 cipher = crypto_spawn_blkcipher(spawn);
88 if (IS_ERR(cipher))
89 return PTR_ERR(cipher);
90
91 ctx->child = cipher;
92 return 0;
93}
94
95static void crypto_fpu_exit_tfm(struct crypto_tfm *tfm)
96{
97 struct crypto_fpu_ctx *ctx = crypto_tfm_ctx(tfm);
98 crypto_free_blkcipher(ctx->child);
99}
100
101static struct crypto_instance *crypto_fpu_alloc(struct rtattr **tb)
102{
103 struct crypto_instance *inst;
104 struct crypto_alg *alg;
105 int err;
106
107 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER);
108 if (err)
109 return ERR_PTR(err);
110
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);
115
116 inst = crypto_alloc_instance("fpu", alg);
117 if (IS_ERR(inst))
118 goto out_put_alg;
119
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;
134
135out_put_alg:
136 crypto_mod_put(alg);
137 return inst;
138}
139
140static void crypto_fpu_free(struct crypto_instance *inst)
141{
142 crypto_drop_spawn(crypto_instance_ctx(inst));
143 kfree(inst);
144}
145
146static struct crypto_template crypto_fpu_tmpl = {
147 .name = "fpu",
148 .alloc = crypto_fpu_alloc,
149 .free = crypto_fpu_free,
150 .module = THIS_MODULE,
151};
152
153int __init crypto_fpu_init(void)
154{
155 return crypto_register_template(&crypto_fpu_tmpl);
156}
157
158void __exit crypto_fpu_exit(void)
159{
160 crypto_unregister_template(&crypto_fpu_tmpl);
161}
1/*
2 * FPU: Wrapper for blkcipher touching fpu
3 *
4 * Copyright (c) Intel Corp.
5 * Author: Huang Ying <ying.huang@intel.com>
6 *
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.
11 *
12 */
13
14#include <crypto/internal/skcipher.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/fpu/api.h>
21
22struct crypto_fpu_ctx {
23 struct crypto_skcipher *child;
24};
25
26static int crypto_fpu_setkey(struct crypto_skcipher *parent, const u8 *key,
27 unsigned int keylen)
28{
29 struct crypto_fpu_ctx *ctx = crypto_skcipher_ctx(parent);
30 struct crypto_skcipher *child = ctx->child;
31 int err;
32
33 crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
34 crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(parent) &
35 CRYPTO_TFM_REQ_MASK);
36 err = crypto_skcipher_setkey(child, key, keylen);
37 crypto_skcipher_set_flags(parent, crypto_skcipher_get_flags(child) &
38 CRYPTO_TFM_RES_MASK);
39 return err;
40}
41
42static int crypto_fpu_encrypt(struct skcipher_request *req)
43{
44 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
45 struct crypto_fpu_ctx *ctx = crypto_skcipher_ctx(tfm);
46 struct crypto_skcipher *child = ctx->child;
47 SKCIPHER_REQUEST_ON_STACK(subreq, child);
48 int err;
49
50 skcipher_request_set_tfm(subreq, child);
51 skcipher_request_set_callback(subreq, 0, NULL, NULL);
52 skcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
53 req->iv);
54
55 kernel_fpu_begin();
56 err = crypto_skcipher_encrypt(subreq);
57 kernel_fpu_end();
58
59 skcipher_request_zero(subreq);
60 return err;
61}
62
63static int crypto_fpu_decrypt(struct skcipher_request *req)
64{
65 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
66 struct crypto_fpu_ctx *ctx = crypto_skcipher_ctx(tfm);
67 struct crypto_skcipher *child = ctx->child;
68 SKCIPHER_REQUEST_ON_STACK(subreq, child);
69 int err;
70
71 skcipher_request_set_tfm(subreq, child);
72 skcipher_request_set_callback(subreq, 0, NULL, NULL);
73 skcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
74 req->iv);
75
76 kernel_fpu_begin();
77 err = crypto_skcipher_decrypt(subreq);
78 kernel_fpu_end();
79
80 skcipher_request_zero(subreq);
81 return err;
82}
83
84static int crypto_fpu_init_tfm(struct crypto_skcipher *tfm)
85{
86 struct skcipher_instance *inst = skcipher_alg_instance(tfm);
87 struct crypto_fpu_ctx *ctx = crypto_skcipher_ctx(tfm);
88 struct crypto_skcipher_spawn *spawn;
89 struct crypto_skcipher *cipher;
90
91 spawn = skcipher_instance_ctx(inst);
92 cipher = crypto_spawn_skcipher(spawn);
93 if (IS_ERR(cipher))
94 return PTR_ERR(cipher);
95
96 ctx->child = cipher;
97
98 return 0;
99}
100
101static void crypto_fpu_exit_tfm(struct crypto_skcipher *tfm)
102{
103 struct crypto_fpu_ctx *ctx = crypto_skcipher_ctx(tfm);
104
105 crypto_free_skcipher(ctx->child);
106}
107
108static void crypto_fpu_free(struct skcipher_instance *inst)
109{
110 crypto_drop_skcipher(skcipher_instance_ctx(inst));
111 kfree(inst);
112}
113
114static int crypto_fpu_create(struct crypto_template *tmpl, struct rtattr **tb)
115{
116 struct crypto_skcipher_spawn *spawn;
117 struct skcipher_instance *inst;
118 struct crypto_attr_type *algt;
119 struct skcipher_alg *alg;
120 const char *cipher_name;
121 int err;
122
123 algt = crypto_get_attr_type(tb);
124 if (IS_ERR(algt))
125 return PTR_ERR(algt);
126
127 if ((algt->type ^ (CRYPTO_ALG_INTERNAL | CRYPTO_ALG_TYPE_SKCIPHER)) &
128 algt->mask)
129 return -EINVAL;
130
131 if (!(algt->mask & CRYPTO_ALG_INTERNAL))
132 return -EINVAL;
133
134 cipher_name = crypto_attr_alg_name(tb[1]);
135 if (IS_ERR(cipher_name))
136 return PTR_ERR(cipher_name);
137
138 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
139 if (!inst)
140 return -ENOMEM;
141
142 spawn = skcipher_instance_ctx(inst);
143
144 crypto_set_skcipher_spawn(spawn, skcipher_crypto_instance(inst));
145 err = crypto_grab_skcipher(spawn, cipher_name, CRYPTO_ALG_INTERNAL,
146 CRYPTO_ALG_INTERNAL | CRYPTO_ALG_ASYNC);
147 if (err)
148 goto out_free_inst;
149
150 alg = crypto_skcipher_spawn_alg(spawn);
151
152 err = crypto_inst_setname(skcipher_crypto_instance(inst), "fpu",
153 &alg->base);
154 if (err)
155 goto out_drop_skcipher;
156
157 inst->alg.base.cra_flags = CRYPTO_ALG_INTERNAL;
158 inst->alg.base.cra_priority = alg->base.cra_priority;
159 inst->alg.base.cra_blocksize = alg->base.cra_blocksize;
160 inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
161
162 inst->alg.ivsize = crypto_skcipher_alg_ivsize(alg);
163 inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg);
164 inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg);
165
166 inst->alg.base.cra_ctxsize = sizeof(struct crypto_fpu_ctx);
167
168 inst->alg.init = crypto_fpu_init_tfm;
169 inst->alg.exit = crypto_fpu_exit_tfm;
170
171 inst->alg.setkey = crypto_fpu_setkey;
172 inst->alg.encrypt = crypto_fpu_encrypt;
173 inst->alg.decrypt = crypto_fpu_decrypt;
174
175 inst->free = crypto_fpu_free;
176
177 err = skcipher_register_instance(tmpl, inst);
178 if (err)
179 goto out_drop_skcipher;
180
181out:
182 return err;
183
184out_drop_skcipher:
185 crypto_drop_skcipher(spawn);
186out_free_inst:
187 kfree(inst);
188 goto out;
189}
190
191static struct crypto_template crypto_fpu_tmpl = {
192 .name = "fpu",
193 .create = crypto_fpu_create,
194 .module = THIS_MODULE,
195};
196
197int __init crypto_fpu_init(void)
198{
199 return crypto_register_template(&crypto_fpu_tmpl);
200}
201
202void crypto_fpu_exit(void)
203{
204 crypto_unregister_template(&crypto_fpu_tmpl);
205}
206
207MODULE_ALIAS_CRYPTO("fpu");