2 * Asynchronous block chaining cipher operations.
4 * This is the asynchronous version of blkcipher.c indicating completion
7 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
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)
16 #include <crypto/internal/skcipher.h>
17 #include <linux/cpumask.h>
18 #include <linux/err.h>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/rtnetlink.h>
23 #include <linux/sched.h>
24 #include <linux/slab.h>
25 #include <linux/seq_file.h>
29 static const char *skcipher_default_geniv __read_mostly;
31 static int setkey_unaligned(struct crypto_ablkcipher *tfm, const u8 *key,
34 struct ablkcipher_alg *cipher = crypto_ablkcipher_alg(tfm);
35 unsigned long alignmask = crypto_ablkcipher_alignmask(tfm);
37 u8 *buffer, *alignbuffer;
40 absize = keylen + alignmask;
41 buffer = kmalloc(absize, GFP_ATOMIC);
45 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
46 memcpy(alignbuffer, key, keylen);
47 ret = cipher->setkey(tfm, alignbuffer, keylen);
48 memset(alignbuffer, 0, keylen);
53 static int setkey(struct crypto_ablkcipher *tfm, const u8 *key,
56 struct ablkcipher_alg *cipher = crypto_ablkcipher_alg(tfm);
57 unsigned long alignmask = crypto_ablkcipher_alignmask(tfm);
59 if (keylen < cipher->min_keysize || keylen > cipher->max_keysize) {
60 crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
64 if ((unsigned long)key & alignmask)
65 return setkey_unaligned(tfm, key, keylen);
67 return cipher->setkey(tfm, key, keylen);
70 static unsigned int crypto_ablkcipher_ctxsize(struct crypto_alg *alg, u32 type,
73 return alg->cra_ctxsize;
76 int skcipher_null_givencrypt(struct skcipher_givcrypt_request *req)
78 return crypto_ablkcipher_encrypt(&req->creq);
81 int skcipher_null_givdecrypt(struct skcipher_givcrypt_request *req)
83 return crypto_ablkcipher_decrypt(&req->creq);
86 static int crypto_init_ablkcipher_ops(struct crypto_tfm *tfm, u32 type,
89 struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
90 struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher;
92 if (alg->ivsize > PAGE_SIZE / 8)
96 crt->encrypt = alg->encrypt;
97 crt->decrypt = alg->decrypt;
99 crt->givencrypt = skcipher_null_givencrypt;
100 crt->givdecrypt = skcipher_null_givdecrypt;
102 crt->base = __crypto_ablkcipher_cast(tfm);
103 crt->ivsize = alg->ivsize;
108 static void crypto_ablkcipher_show(struct seq_file *m, struct crypto_alg *alg)
109 __attribute__ ((unused));
110 static void crypto_ablkcipher_show(struct seq_file *m, struct crypto_alg *alg)
112 struct ablkcipher_alg *ablkcipher = &alg->cra_ablkcipher;
114 seq_printf(m, "type : ablkcipher\n");
115 seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
117 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
118 seq_printf(m, "min keysize : %u\n", ablkcipher->min_keysize);
119 seq_printf(m, "max keysize : %u\n", ablkcipher->max_keysize);
120 seq_printf(m, "ivsize : %u\n", ablkcipher->ivsize);
121 seq_printf(m, "geniv : %s\n", ablkcipher->geniv ?: "<default>");
124 const struct crypto_type crypto_ablkcipher_type = {
125 .ctxsize = crypto_ablkcipher_ctxsize,
126 .init = crypto_init_ablkcipher_ops,
127 #ifdef CONFIG_PROC_FS
128 .show = crypto_ablkcipher_show,
131 EXPORT_SYMBOL_GPL(crypto_ablkcipher_type);
133 static int no_givdecrypt(struct skcipher_givcrypt_request *req)
138 static int crypto_init_givcipher_ops(struct crypto_tfm *tfm, u32 type,
141 struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
142 struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher;
144 if (alg->ivsize > PAGE_SIZE / 8)
147 crt->setkey = tfm->__crt_alg->cra_flags & CRYPTO_ALG_GENIV ?
148 alg->setkey : setkey;
149 crt->encrypt = alg->encrypt;
150 crt->decrypt = alg->decrypt;
151 crt->givencrypt = alg->givencrypt;
152 crt->givdecrypt = alg->givdecrypt ?: no_givdecrypt;
153 crt->base = __crypto_ablkcipher_cast(tfm);
154 crt->ivsize = alg->ivsize;
159 static void crypto_givcipher_show(struct seq_file *m, struct crypto_alg *alg)
160 __attribute__ ((unused));
161 static void crypto_givcipher_show(struct seq_file *m, struct crypto_alg *alg)
163 struct ablkcipher_alg *ablkcipher = &alg->cra_ablkcipher;
165 seq_printf(m, "type : givcipher\n");
166 seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
168 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
169 seq_printf(m, "min keysize : %u\n", ablkcipher->min_keysize);
170 seq_printf(m, "max keysize : %u\n", ablkcipher->max_keysize);
171 seq_printf(m, "ivsize : %u\n", ablkcipher->ivsize);
172 seq_printf(m, "geniv : %s\n", ablkcipher->geniv ?: "<built-in>");
175 const struct crypto_type crypto_givcipher_type = {
176 .ctxsize = crypto_ablkcipher_ctxsize,
177 .init = crypto_init_givcipher_ops,
178 #ifdef CONFIG_PROC_FS
179 .show = crypto_givcipher_show,
182 EXPORT_SYMBOL_GPL(crypto_givcipher_type);
184 const char *crypto_default_geniv(const struct crypto_alg *alg)
186 return alg->cra_flags & CRYPTO_ALG_ASYNC ?
187 "eseqiv" : skcipher_default_geniv;
190 static int crypto_givcipher_default(struct crypto_alg *alg, u32 type, u32 mask)
192 struct rtattr *tb[3];
195 struct crypto_attr_type data;
199 struct crypto_attr_alg data;
201 struct crypto_template *tmpl;
202 struct crypto_instance *inst;
203 struct crypto_alg *larval;
207 larval = crypto_larval_lookup(alg->cra_driver_name,
208 (type & ~CRYPTO_ALG_TYPE_MASK) |
209 CRYPTO_ALG_TYPE_GIVCIPHER,
210 mask | CRYPTO_ALG_TYPE_MASK);
211 err = PTR_ERR(larval);
216 if (!crypto_is_larval(larval))
219 ptype.attr.rta_len = sizeof(ptype);
220 ptype.attr.rta_type = CRYPTOA_TYPE;
221 ptype.data.type = type | CRYPTO_ALG_GENIV;
222 /* GENIV tells the template that we're making a default geniv. */
223 ptype.data.mask = mask | CRYPTO_ALG_GENIV;
226 palg.attr.rta_len = sizeof(palg);
227 palg.attr.rta_type = CRYPTOA_ALG;
228 /* Must use the exact name to locate ourselves. */
229 memcpy(palg.data.name, alg->cra_driver_name, CRYPTO_MAX_ALG_NAME);
234 if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
235 CRYPTO_ALG_TYPE_BLKCIPHER)
236 geniv = alg->cra_blkcipher.geniv;
238 geniv = alg->cra_ablkcipher.geniv;
241 geniv = crypto_default_geniv(alg);
243 tmpl = crypto_lookup_template(geniv);
248 inst = tmpl->alloc(tb);
253 if ((err = crypto_register_instance(tmpl, inst))) {
258 /* Redo the lookup to use the instance we just registered. */
262 crypto_tmpl_put(tmpl);
264 crypto_larval_kill(larval);
266 crypto_mod_put(larval);
272 static struct crypto_alg *crypto_lookup_skcipher(const char *name, u32 type,
275 struct crypto_alg *alg;
277 alg = crypto_alg_mod_lookup(name, type, mask);
281 if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
282 CRYPTO_ALG_TYPE_GIVCIPHER)
285 if (!((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
286 CRYPTO_ALG_TYPE_BLKCIPHER ? alg->cra_blkcipher.ivsize :
287 alg->cra_ablkcipher.ivsize))
291 alg = crypto_alg_mod_lookup(name, type | CRYPTO_ALG_TESTED,
292 mask & ~CRYPTO_ALG_TESTED);
296 if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
297 CRYPTO_ALG_TYPE_GIVCIPHER) {
298 if ((alg->cra_flags ^ type ^ ~mask) & CRYPTO_ALG_TESTED) {
300 alg = ERR_PTR(-ENOENT);
305 BUG_ON(!((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
306 CRYPTO_ALG_TYPE_BLKCIPHER ? alg->cra_blkcipher.ivsize :
307 alg->cra_ablkcipher.ivsize));
309 return ERR_PTR(crypto_givcipher_default(alg, type, mask));
312 int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, const char *name,
315 struct crypto_alg *alg;
318 type = crypto_skcipher_type(type);
319 mask = crypto_skcipher_mask(mask);
321 alg = crypto_lookup_skcipher(name, type, mask);
325 err = crypto_init_spawn(&spawn->base, alg, spawn->base.inst, mask);
329 EXPORT_SYMBOL_GPL(crypto_grab_skcipher);
331 struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
334 struct crypto_tfm *tfm;
337 type = crypto_skcipher_type(type);
338 mask = crypto_skcipher_mask(mask);
341 struct crypto_alg *alg;
343 alg = crypto_lookup_skcipher(alg_name, type, mask);
349 tfm = __crypto_alloc_tfm(alg, type, mask);
351 return __crypto_ablkcipher_cast(tfm);
359 if (signal_pending(current)) {
367 EXPORT_SYMBOL_GPL(crypto_alloc_ablkcipher);
369 static int __init skcipher_module_init(void)
371 skcipher_default_geniv = num_possible_cpus() > 1 ?
372 "eseqiv" : "chainiv";
376 static void skcipher_module_exit(void)
380 module_init(skcipher_module_init);
381 module_exit(skcipher_module_exit);