blob: 43fc8fb9f978a0b7a5bbe27cffb4c36e5ecad409 [file] [log] [blame]
Herbert Xub5b7f082007-04-16 20:48:54 +10001/*
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
Herbert Xu378f4f52007-12-17 20:07:31 +080016#include <crypto/internal/skcipher.h>
17#include <linux/err.h>
Herbert Xub5b7f082007-04-16 20:48:54 +100018#include <linux/init.h>
Herbert Xu791b4d52007-08-23 16:23:01 +080019#include <linux/kernel.h>
Herbert Xub5b7f082007-04-16 20:48:54 +100020#include <linux/module.h>
Herbert Xub9c55aa2007-12-04 12:46:48 +110021#include <linux/rtnetlink.h>
22#include <linux/sched.h>
Herbert Xu791b4d52007-08-23 16:23:01 +080023#include <linux/slab.h>
Herbert Xub5b7f082007-04-16 20:48:54 +100024#include <linux/seq_file.h>
25
Herbert Xu378f4f52007-12-17 20:07:31 +080026#include "internal.h"
27
Herbert Xu791b4d52007-08-23 16:23:01 +080028static int setkey_unaligned(struct crypto_ablkcipher *tfm, const u8 *key,
29 unsigned int keylen)
Sebastian Siewiorca7c3932007-05-19 19:51:21 +100030{
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);
Sebastian Siewior06817172007-08-03 20:33:47 +080045 memset(alignbuffer, 0, keylen);
Sebastian Siewiorca7c3932007-05-19 19:51:21 +100046 kfree(buffer);
47 return ret;
48}
49
Herbert Xub5b7f082007-04-16 20:48:54 +100050static int setkey(struct crypto_ablkcipher *tfm, const u8 *key,
51 unsigned int keylen)
52{
53 struct ablkcipher_alg *cipher = crypto_ablkcipher_alg(tfm);
Sebastian Siewiorca7c3932007-05-19 19:51:21 +100054 unsigned long alignmask = crypto_ablkcipher_alignmask(tfm);
Herbert Xub5b7f082007-04-16 20:48:54 +100055
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
Sebastian Siewiorca7c3932007-05-19 19:51:21 +100061 if ((unsigned long)key & alignmask)
62 return setkey_unaligned(tfm, key, keylen);
63
Herbert Xub5b7f082007-04-16 20:48:54 +100064 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
Herbert Xub9c55aa2007-12-04 12:46:48 +110073int 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
Herbert Xub5b7f082007-04-16 20:48:54 +100083static 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;
Herbert Xub9c55aa2007-12-04 12:46:48 +110095 if (!alg->ivsize) {
96 crt->givencrypt = skcipher_null_givencrypt;
97 crt->givdecrypt = skcipher_null_givdecrypt;
98 }
Herbert Xuecfc4322007-12-05 21:08:36 +110099 crt->base = __crypto_ablkcipher_cast(tfm);
Herbert Xub5b7f082007-04-16 20:48:54 +1000100 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");
Herbert Xu189ed662007-12-14 22:29:37 +0800112 seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
113 "yes" : "no");
Herbert Xub5b7f082007-04-16 20:48:54 +1000114 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
115 seq_printf(m, "min keysize : %u\n", ablkcipher->min_keysize);
116 seq_printf(m, "max keysize : %u\n", ablkcipher->max_keysize);
117 seq_printf(m, "ivsize : %u\n", ablkcipher->ivsize);
Herbert Xu23508e12007-11-27 21:33:24 +0800118 seq_printf(m, "geniv : %s\n", ablkcipher->geniv ?: "<default>");
Herbert Xub5b7f082007-04-16 20:48:54 +1000119}
120
121const struct crypto_type crypto_ablkcipher_type = {
122 .ctxsize = crypto_ablkcipher_ctxsize,
123 .init = crypto_init_ablkcipher_ops,
124#ifdef CONFIG_PROC_FS
125 .show = crypto_ablkcipher_show,
126#endif
127};
128EXPORT_SYMBOL_GPL(crypto_ablkcipher_type);
129
Herbert Xu61da88e2007-12-17 21:51:27 +0800130static int no_givdecrypt(struct skcipher_givcrypt_request *req)
131{
132 return -ENOSYS;
133}
134
135static int crypto_init_givcipher_ops(struct crypto_tfm *tfm, u32 type,
136 u32 mask)
137{
138 struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
139 struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher;
140
141 if (alg->ivsize > PAGE_SIZE / 8)
142 return -EINVAL;
143
Herbert Xuecfc4322007-12-05 21:08:36 +1100144 crt->setkey = tfm->__crt_alg->cra_flags & CRYPTO_ALG_GENIV ?
145 alg->setkey : setkey;
Herbert Xu61da88e2007-12-17 21:51:27 +0800146 crt->encrypt = alg->encrypt;
147 crt->decrypt = alg->decrypt;
148 crt->givencrypt = alg->givencrypt;
149 crt->givdecrypt = alg->givdecrypt ?: no_givdecrypt;
Herbert Xuecfc4322007-12-05 21:08:36 +1100150 crt->base = __crypto_ablkcipher_cast(tfm);
Herbert Xu61da88e2007-12-17 21:51:27 +0800151 crt->ivsize = alg->ivsize;
152
153 return 0;
154}
155
156static void crypto_givcipher_show(struct seq_file *m, struct crypto_alg *alg)
157 __attribute__ ((unused));
158static void crypto_givcipher_show(struct seq_file *m, struct crypto_alg *alg)
159{
160 struct ablkcipher_alg *ablkcipher = &alg->cra_ablkcipher;
161
162 seq_printf(m, "type : givcipher\n");
Herbert Xu189ed662007-12-14 22:29:37 +0800163 seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
164 "yes" : "no");
Herbert Xu61da88e2007-12-17 21:51:27 +0800165 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
166 seq_printf(m, "min keysize : %u\n", ablkcipher->min_keysize);
167 seq_printf(m, "max keysize : %u\n", ablkcipher->max_keysize);
168 seq_printf(m, "ivsize : %u\n", ablkcipher->ivsize);
Herbert Xu23508e12007-11-27 21:33:24 +0800169 seq_printf(m, "geniv : %s\n", ablkcipher->geniv ?: "<built-in>");
Herbert Xu61da88e2007-12-17 21:51:27 +0800170}
171
172const struct crypto_type crypto_givcipher_type = {
173 .ctxsize = crypto_ablkcipher_ctxsize,
174 .init = crypto_init_givcipher_ops,
175#ifdef CONFIG_PROC_FS
176 .show = crypto_givcipher_show,
177#endif
178};
179EXPORT_SYMBOL_GPL(crypto_givcipher_type);
180
Herbert Xuecfc4322007-12-05 21:08:36 +1100181const char *crypto_default_geniv(const struct crypto_alg *alg)
182{
183 return alg->cra_flags & CRYPTO_ALG_ASYNC ? "eseqiv" : "chainiv";
184}
185
Herbert Xub9c55aa2007-12-04 12:46:48 +1100186static int crypto_givcipher_default(struct crypto_alg *alg, u32 type, u32 mask)
187{
188 struct rtattr *tb[3];
189 struct {
190 struct rtattr attr;
191 struct crypto_attr_type data;
192 } ptype;
193 struct {
194 struct rtattr attr;
195 struct crypto_attr_alg data;
196 } palg;
197 struct crypto_template *tmpl;
198 struct crypto_instance *inst;
199 struct crypto_alg *larval;
200 const char *geniv;
201 int err;
202
203 larval = crypto_larval_lookup(alg->cra_driver_name,
Herbert Xu435578a2009-06-25 14:46:31 +0800204 (type & ~CRYPTO_ALG_TYPE_MASK) |
Herbert Xub9c55aa2007-12-04 12:46:48 +1100205 CRYPTO_ALG_TYPE_GIVCIPHER,
Herbert Xu435578a2009-06-25 14:46:31 +0800206 mask | CRYPTO_ALG_TYPE_MASK);
Herbert Xub9c55aa2007-12-04 12:46:48 +1100207 err = PTR_ERR(larval);
208 if (IS_ERR(larval))
209 goto out;
210
211 err = -EAGAIN;
212 if (!crypto_is_larval(larval))
213 goto drop_larval;
214
215 ptype.attr.rta_len = sizeof(ptype);
216 ptype.attr.rta_type = CRYPTOA_TYPE;
217 ptype.data.type = type | CRYPTO_ALG_GENIV;
218 /* GENIV tells the template that we're making a default geniv. */
219 ptype.data.mask = mask | CRYPTO_ALG_GENIV;
220 tb[0] = &ptype.attr;
221
222 palg.attr.rta_len = sizeof(palg);
223 palg.attr.rta_type = CRYPTOA_ALG;
224 /* Must use the exact name to locate ourselves. */
225 memcpy(palg.data.name, alg->cra_driver_name, CRYPTO_MAX_ALG_NAME);
226 tb[1] = &palg.attr;
227
228 tb[2] = NULL;
229
230 if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
231 CRYPTO_ALG_TYPE_BLKCIPHER)
232 geniv = alg->cra_blkcipher.geniv;
233 else
234 geniv = alg->cra_ablkcipher.geniv;
235
236 if (!geniv)
237 geniv = crypto_default_geniv(alg);
238
239 tmpl = crypto_lookup_template(geniv);
240 err = -ENOENT;
241 if (!tmpl)
242 goto kill_larval;
243
244 inst = tmpl->alloc(tb);
245 err = PTR_ERR(inst);
246 if (IS_ERR(inst))
247 goto put_tmpl;
248
249 if ((err = crypto_register_instance(tmpl, inst))) {
250 tmpl->free(inst);
251 goto put_tmpl;
252 }
253
254 /* Redo the lookup to use the instance we just registered. */
255 err = -EAGAIN;
256
257put_tmpl:
258 crypto_tmpl_put(tmpl);
259kill_larval:
260 crypto_larval_kill(larval);
261drop_larval:
262 crypto_mod_put(larval);
263out:
264 crypto_mod_put(alg);
265 return err;
266}
267
268static struct crypto_alg *crypto_lookup_skcipher(const char *name, u32 type,
269 u32 mask)
270{
271 struct crypto_alg *alg;
272
273 alg = crypto_alg_mod_lookup(name, type, mask);
274 if (IS_ERR(alg))
275 return alg;
276
277 if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
278 CRYPTO_ALG_TYPE_GIVCIPHER)
279 return alg;
280
281 if (!((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
282 CRYPTO_ALG_TYPE_BLKCIPHER ? alg->cra_blkcipher.ivsize :
283 alg->cra_ablkcipher.ivsize))
284 return alg;
285
Herbert Xub170a132009-02-18 20:33:55 +0800286 crypto_mod_put(alg);
287 alg = crypto_alg_mod_lookup(name, type | CRYPTO_ALG_TESTED,
288 mask & ~CRYPTO_ALG_TESTED);
289 if (IS_ERR(alg))
290 return alg;
291
292 if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
293 CRYPTO_ALG_TYPE_GIVCIPHER) {
294 if ((alg->cra_flags ^ type ^ ~mask) & CRYPTO_ALG_TESTED) {
295 crypto_mod_put(alg);
296 alg = ERR_PTR(-ENOENT);
297 }
298 return alg;
299 }
300
301 BUG_ON(!((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
302 CRYPTO_ALG_TYPE_BLKCIPHER ? alg->cra_blkcipher.ivsize :
303 alg->cra_ablkcipher.ivsize));
304
Herbert Xub9c55aa2007-12-04 12:46:48 +1100305 return ERR_PTR(crypto_givcipher_default(alg, type, mask));
306}
307
Herbert Xu378f4f52007-12-17 20:07:31 +0800308int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, const char *name,
309 u32 type, u32 mask)
310{
311 struct crypto_alg *alg;
312 int err;
313
314 type = crypto_skcipher_type(type);
315 mask = crypto_skcipher_mask(mask);
316
Herbert Xub9c55aa2007-12-04 12:46:48 +1100317 alg = crypto_lookup_skcipher(name, type, mask);
Herbert Xu378f4f52007-12-17 20:07:31 +0800318 if (IS_ERR(alg))
319 return PTR_ERR(alg);
320
321 err = crypto_init_spawn(&spawn->base, alg, spawn->base.inst, mask);
322 crypto_mod_put(alg);
323 return err;
324}
325EXPORT_SYMBOL_GPL(crypto_grab_skcipher);
326
Herbert Xub9c55aa2007-12-04 12:46:48 +1100327struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
328 u32 type, u32 mask)
329{
330 struct crypto_tfm *tfm;
331 int err;
332
333 type = crypto_skcipher_type(type);
334 mask = crypto_skcipher_mask(mask);
335
336 for (;;) {
337 struct crypto_alg *alg;
338
339 alg = crypto_lookup_skcipher(alg_name, type, mask);
340 if (IS_ERR(alg)) {
341 err = PTR_ERR(alg);
342 goto err;
343 }
344
345 tfm = __crypto_alloc_tfm(alg, type, mask);
346 if (!IS_ERR(tfm))
347 return __crypto_ablkcipher_cast(tfm);
348
349 crypto_mod_put(alg);
350 err = PTR_ERR(tfm);
351
352err:
353 if (err != -EAGAIN)
354 break;
355 if (signal_pending(current)) {
356 err = -EINTR;
357 break;
358 }
359 }
360
361 return ERR_PTR(err);
362}
363EXPORT_SYMBOL_GPL(crypto_alloc_ablkcipher);