]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - crypto/ablkcipher.c
[CRYPTO] chainiv: Avoid lock spinning where possible
[mirror_ubuntu-artful-kernel.git] / crypto / ablkcipher.c
CommitLineData
b5b7f088
HX
1/*
2 * Asynchronous block chaining cipher operations.
3 *
4 * This is the asynchronous version of blkcipher.c indicating completion
5 * via a callback.
6 *
7 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
12 * any later version.
13 *
14 */
15
378f4f51
HX
16#include <crypto/internal/skcipher.h>
17#include <linux/err.h>
b5b7f088 18#include <linux/init.h>
791b4d5f 19#include <linux/kernel.h>
b5b7f088 20#include <linux/module.h>
b9c55aa4
HX
21#include <linux/rtnetlink.h>
22#include <linux/sched.h>
791b4d5f 23#include <linux/slab.h>
b5b7f088
HX
24#include <linux/seq_file.h>
25
378f4f51
HX
26#include "internal.h"
27
791b4d5f
HX
28static int setkey_unaligned(struct crypto_ablkcipher *tfm, const u8 *key,
29 unsigned int keylen)
ca7c3938
SS
30{
31 struct ablkcipher_alg *cipher = crypto_ablkcipher_alg(tfm);
32 unsigned long alignmask = crypto_ablkcipher_alignmask(tfm);
33 int ret;
34 u8 *buffer, *alignbuffer;
35 unsigned long absize;
36
37 absize = keylen + alignmask;
38 buffer = kmalloc(absize, GFP_ATOMIC);
39 if (!buffer)
40 return -ENOMEM;
41
42 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
43 memcpy(alignbuffer, key, keylen);
44 ret = cipher->setkey(tfm, alignbuffer, keylen);
06817176 45 memset(alignbuffer, 0, keylen);
ca7c3938
SS
46 kfree(buffer);
47 return ret;
48}
49
b5b7f088
HX
50static int setkey(struct crypto_ablkcipher *tfm, const u8 *key,
51 unsigned int keylen)
52{
53 struct ablkcipher_alg *cipher = crypto_ablkcipher_alg(tfm);
ca7c3938 54 unsigned long alignmask = crypto_ablkcipher_alignmask(tfm);
b5b7f088
HX
55
56 if (keylen < cipher->min_keysize || keylen > cipher->max_keysize) {
57 crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
58 return -EINVAL;
59 }
60
ca7c3938
SS
61 if ((unsigned long)key & alignmask)
62 return setkey_unaligned(tfm, key, keylen);
63
b5b7f088
HX
64 return cipher->setkey(tfm, key, keylen);
65}
66
67static unsigned int crypto_ablkcipher_ctxsize(struct crypto_alg *alg, u32 type,
68 u32 mask)
69{
70 return alg->cra_ctxsize;
71}
72
b9c55aa4
HX
73int skcipher_null_givencrypt(struct skcipher_givcrypt_request *req)
74{
75 return crypto_ablkcipher_encrypt(&req->creq);
76}
77
78int skcipher_null_givdecrypt(struct skcipher_givcrypt_request *req)
79{
80 return crypto_ablkcipher_decrypt(&req->creq);
81}
82
b5b7f088
HX
83static int crypto_init_ablkcipher_ops(struct crypto_tfm *tfm, u32 type,
84 u32 mask)
85{
86 struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
87 struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher;
88
89 if (alg->ivsize > PAGE_SIZE / 8)
90 return -EINVAL;
91
92 crt->setkey = setkey;
93 crt->encrypt = alg->encrypt;
94 crt->decrypt = alg->decrypt;
b9c55aa4
HX
95 if (!alg->ivsize) {
96 crt->givencrypt = skcipher_null_givencrypt;
97 crt->givdecrypt = skcipher_null_givdecrypt;
98 }
ecfc4329 99 crt->base = __crypto_ablkcipher_cast(tfm);
b5b7f088
HX
100 crt->ivsize = alg->ivsize;
101
102 return 0;
103}
104
105static void crypto_ablkcipher_show(struct seq_file *m, struct crypto_alg *alg)
106 __attribute__ ((unused));
107static void crypto_ablkcipher_show(struct seq_file *m, struct crypto_alg *alg)
108{
109 struct ablkcipher_alg *ablkcipher = &alg->cra_ablkcipher;
110
111 seq_printf(m, "type : ablkcipher\n");
112 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
113 seq_printf(m, "min keysize : %u\n", ablkcipher->min_keysize);
114 seq_printf(m, "max keysize : %u\n", ablkcipher->max_keysize);
115 seq_printf(m, "ivsize : %u\n", ablkcipher->ivsize);
23508e11 116 seq_printf(m, "geniv : %s\n", ablkcipher->geniv ?: "<default>");
b5b7f088
HX
117}
118
119const struct crypto_type crypto_ablkcipher_type = {
120 .ctxsize = crypto_ablkcipher_ctxsize,
121 .init = crypto_init_ablkcipher_ops,
122#ifdef CONFIG_PROC_FS
123 .show = crypto_ablkcipher_show,
124#endif
125};
126EXPORT_SYMBOL_GPL(crypto_ablkcipher_type);
127
61da88e2
HX
128static int no_givdecrypt(struct skcipher_givcrypt_request *req)
129{
130 return -ENOSYS;
131}
132
133static int crypto_init_givcipher_ops(struct crypto_tfm *tfm, u32 type,
134 u32 mask)
135{
136 struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
137 struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher;
138
139 if (alg->ivsize > PAGE_SIZE / 8)
140 return -EINVAL;
141
ecfc4329
HX
142 crt->setkey = tfm->__crt_alg->cra_flags & CRYPTO_ALG_GENIV ?
143 alg->setkey : setkey;
61da88e2
HX
144 crt->encrypt = alg->encrypt;
145 crt->decrypt = alg->decrypt;
146 crt->givencrypt = alg->givencrypt;
147 crt->givdecrypt = alg->givdecrypt ?: no_givdecrypt;
ecfc4329 148 crt->base = __crypto_ablkcipher_cast(tfm);
61da88e2
HX
149 crt->ivsize = alg->ivsize;
150
151 return 0;
152}
153
154static void crypto_givcipher_show(struct seq_file *m, struct crypto_alg *alg)
155 __attribute__ ((unused));
156static void crypto_givcipher_show(struct seq_file *m, struct crypto_alg *alg)
157{
158 struct ablkcipher_alg *ablkcipher = &alg->cra_ablkcipher;
159
160 seq_printf(m, "type : givcipher\n");
161 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
162 seq_printf(m, "min keysize : %u\n", ablkcipher->min_keysize);
163 seq_printf(m, "max keysize : %u\n", ablkcipher->max_keysize);
164 seq_printf(m, "ivsize : %u\n", ablkcipher->ivsize);
23508e11 165 seq_printf(m, "geniv : %s\n", ablkcipher->geniv ?: "<built-in>");
61da88e2
HX
166}
167
168const struct crypto_type crypto_givcipher_type = {
169 .ctxsize = crypto_ablkcipher_ctxsize,
170 .init = crypto_init_givcipher_ops,
171#ifdef CONFIG_PROC_FS
172 .show = crypto_givcipher_show,
173#endif
174};
175EXPORT_SYMBOL_GPL(crypto_givcipher_type);
176
ecfc4329
HX
177const char *crypto_default_geniv(const struct crypto_alg *alg)
178{
179 return alg->cra_flags & CRYPTO_ALG_ASYNC ? "eseqiv" : "chainiv";
180}
181
b9c55aa4
HX
182static int crypto_givcipher_default(struct crypto_alg *alg, u32 type, u32 mask)
183{
184 struct rtattr *tb[3];
185 struct {
186 struct rtattr attr;
187 struct crypto_attr_type data;
188 } ptype;
189 struct {
190 struct rtattr attr;
191 struct crypto_attr_alg data;
192 } palg;
193 struct crypto_template *tmpl;
194 struct crypto_instance *inst;
195 struct crypto_alg *larval;
196 const char *geniv;
197 int err;
198
199 larval = crypto_larval_lookup(alg->cra_driver_name,
200 CRYPTO_ALG_TYPE_GIVCIPHER,
201 CRYPTO_ALG_TYPE_MASK);
202 err = PTR_ERR(larval);
203 if (IS_ERR(larval))
204 goto out;
205
206 err = -EAGAIN;
207 if (!crypto_is_larval(larval))
208 goto drop_larval;
209
210 ptype.attr.rta_len = sizeof(ptype);
211 ptype.attr.rta_type = CRYPTOA_TYPE;
212 ptype.data.type = type | CRYPTO_ALG_GENIV;
213 /* GENIV tells the template that we're making a default geniv. */
214 ptype.data.mask = mask | CRYPTO_ALG_GENIV;
215 tb[0] = &ptype.attr;
216
217 palg.attr.rta_len = sizeof(palg);
218 palg.attr.rta_type = CRYPTOA_ALG;
219 /* Must use the exact name to locate ourselves. */
220 memcpy(palg.data.name, alg->cra_driver_name, CRYPTO_MAX_ALG_NAME);
221 tb[1] = &palg.attr;
222
223 tb[2] = NULL;
224
225 if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
226 CRYPTO_ALG_TYPE_BLKCIPHER)
227 geniv = alg->cra_blkcipher.geniv;
228 else
229 geniv = alg->cra_ablkcipher.geniv;
230
231 if (!geniv)
232 geniv = crypto_default_geniv(alg);
233
234 tmpl = crypto_lookup_template(geniv);
235 err = -ENOENT;
236 if (!tmpl)
237 goto kill_larval;
238
239 inst = tmpl->alloc(tb);
240 err = PTR_ERR(inst);
241 if (IS_ERR(inst))
242 goto put_tmpl;
243
244 if ((err = crypto_register_instance(tmpl, inst))) {
245 tmpl->free(inst);
246 goto put_tmpl;
247 }
248
249 /* Redo the lookup to use the instance we just registered. */
250 err = -EAGAIN;
251
252put_tmpl:
253 crypto_tmpl_put(tmpl);
254kill_larval:
255 crypto_larval_kill(larval);
256drop_larval:
257 crypto_mod_put(larval);
258out:
259 crypto_mod_put(alg);
260 return err;
261}
262
263static struct crypto_alg *crypto_lookup_skcipher(const char *name, u32 type,
264 u32 mask)
265{
266 struct crypto_alg *alg;
267
268 alg = crypto_alg_mod_lookup(name, type, mask);
269 if (IS_ERR(alg))
270 return alg;
271
272 if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
273 CRYPTO_ALG_TYPE_GIVCIPHER)
274 return alg;
275
276 if (!((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
277 CRYPTO_ALG_TYPE_BLKCIPHER ? alg->cra_blkcipher.ivsize :
278 alg->cra_ablkcipher.ivsize))
279 return alg;
280
281 return ERR_PTR(crypto_givcipher_default(alg, type, mask));
282}
283
378f4f51
HX
284int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, const char *name,
285 u32 type, u32 mask)
286{
287 struct crypto_alg *alg;
288 int err;
289
290 type = crypto_skcipher_type(type);
291 mask = crypto_skcipher_mask(mask);
292
b9c55aa4 293 alg = crypto_lookup_skcipher(name, type, mask);
378f4f51
HX
294 if (IS_ERR(alg))
295 return PTR_ERR(alg);
296
297 err = crypto_init_spawn(&spawn->base, alg, spawn->base.inst, mask);
298 crypto_mod_put(alg);
299 return err;
300}
301EXPORT_SYMBOL_GPL(crypto_grab_skcipher);
302
b9c55aa4
HX
303struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
304 u32 type, u32 mask)
305{
306 struct crypto_tfm *tfm;
307 int err;
308
309 type = crypto_skcipher_type(type);
310 mask = crypto_skcipher_mask(mask);
311
312 for (;;) {
313 struct crypto_alg *alg;
314
315 alg = crypto_lookup_skcipher(alg_name, type, mask);
316 if (IS_ERR(alg)) {
317 err = PTR_ERR(alg);
318 goto err;
319 }
320
321 tfm = __crypto_alloc_tfm(alg, type, mask);
322 if (!IS_ERR(tfm))
323 return __crypto_ablkcipher_cast(tfm);
324
325 crypto_mod_put(alg);
326 err = PTR_ERR(tfm);
327
328err:
329 if (err != -EAGAIN)
330 break;
331 if (signal_pending(current)) {
332 err = -EINTR;
333 break;
334 }
335 }
336
337 return ERR_PTR(err);
338}
339EXPORT_SYMBOL_GPL(crypto_alloc_ablkcipher);
340
b5b7f088
HX
341MODULE_LICENSE("GPL");
342MODULE_DESCRIPTION("Asynchronous block chaining cipher type");