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/err.h>
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/rtnetlink.h>
22 #include <linux/sched.h>
23 #include <linux/slab.h>
24 #include <linux/seq_file.h>
28 static int setkey_unaligned(struct crypto_ablkcipher *tfm, const u8 *key,
31 struct ablkcipher_alg *cipher = crypto_ablkcipher_alg(tfm);
32 unsigned long alignmask = crypto_ablkcipher_alignmask(tfm);
34 u8 *buffer, *alignbuffer;
37 absize = keylen + alignmask;
38 buffer = kmalloc(absize, GFP_ATOMIC);
42 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
43 memcpy(alignbuffer, key, keylen);
44 ret = cipher->setkey(tfm, alignbuffer, keylen);
45 memset(alignbuffer, 0, keylen);
50 static int setkey(struct crypto_ablkcipher *tfm, const u8 *key,
53 struct ablkcipher_alg *cipher = crypto_ablkcipher_alg(tfm);
54 unsigned long alignmask = crypto_ablkcipher_alignmask(tfm);
56 if (keylen < cipher->min_keysize || keylen > cipher->max_keysize) {
57 crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
61 if ((unsigned long)key & alignmask)
62 return setkey_unaligned(tfm, key, keylen);
64 return cipher->setkey(tfm, key, keylen);
67 static unsigned int crypto_ablkcipher_ctxsize(struct crypto_alg *alg, u32 type,
70 return alg->cra_ctxsize;
73 int skcipher_null_givencrypt(struct skcipher_givcrypt_request *req)
75 return crypto_ablkcipher_encrypt(&req->creq);
78 int skcipher_null_givdecrypt(struct skcipher_givcrypt_request *req)
80 return crypto_ablkcipher_decrypt(&req->creq);
83 static int crypto_init_ablkcipher_ops(struct crypto_tfm *tfm, u32 type,
86 struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
87 struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher;
89 if (alg->ivsize > PAGE_SIZE / 8)
93 crt->encrypt = alg->encrypt;
94 crt->decrypt = alg->decrypt;
96 crt->givencrypt = skcipher_null_givencrypt;
97 crt->givdecrypt = skcipher_null_givdecrypt;
99 crt->base = __crypto_ablkcipher_cast(tfm);
100 crt->ivsize = alg->ivsize;
105 static void crypto_ablkcipher_show(struct seq_file *m, struct crypto_alg *alg)
106 __attribute__ ((unused));
107 static void crypto_ablkcipher_show(struct seq_file *m, struct crypto_alg *alg)
109 struct ablkcipher_alg *ablkcipher = &alg->cra_ablkcipher;
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);
116 seq_printf(m, "geniv : %s\n", ablkcipher->geniv ?: "<default>");
119 const 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,
126 EXPORT_SYMBOL_GPL(crypto_ablkcipher_type);
128 static int no_givdecrypt(struct skcipher_givcrypt_request *req)
133 static int crypto_init_givcipher_ops(struct crypto_tfm *tfm, u32 type,
136 struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
137 struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher;
139 if (alg->ivsize > PAGE_SIZE / 8)
142 crt->setkey = tfm->__crt_alg->cra_flags & CRYPTO_ALG_GENIV ?
143 alg->setkey : setkey;
144 crt->encrypt = alg->encrypt;
145 crt->decrypt = alg->decrypt;
146 crt->givencrypt = alg->givencrypt;
147 crt->givdecrypt = alg->givdecrypt ?: no_givdecrypt;
148 crt->base = __crypto_ablkcipher_cast(tfm);
149 crt->ivsize = alg->ivsize;
154 static void crypto_givcipher_show(struct seq_file *m, struct crypto_alg *alg)
155 __attribute__ ((unused));
156 static void crypto_givcipher_show(struct seq_file *m, struct crypto_alg *alg)
158 struct ablkcipher_alg *ablkcipher = &alg->cra_ablkcipher;
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);
165 seq_printf(m, "geniv : %s\n", ablkcipher->geniv ?: "<built-in>");
168 const 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,
175 EXPORT_SYMBOL_GPL(crypto_givcipher_type);
177 const char *crypto_default_geniv(const struct crypto_alg *alg)
179 return alg->cra_flags & CRYPTO_ALG_ASYNC ? "eseqiv" : "chainiv";
182 static int crypto_givcipher_default(struct crypto_alg *alg, u32 type, u32 mask)
184 struct rtattr *tb[3];
187 struct crypto_attr_type data;
191 struct crypto_attr_alg data;
193 struct crypto_template *tmpl;
194 struct crypto_instance *inst;
195 struct crypto_alg *larval;
199 larval = crypto_larval_lookup(alg->cra_driver_name,
200 CRYPTO_ALG_TYPE_GIVCIPHER,
201 CRYPTO_ALG_TYPE_MASK);
202 err = PTR_ERR(larval);
207 if (!crypto_is_larval(larval))
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;
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);
225 if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
226 CRYPTO_ALG_TYPE_BLKCIPHER)
227 geniv = alg->cra_blkcipher.geniv;
229 geniv = alg->cra_ablkcipher.geniv;
232 geniv = crypto_default_geniv(alg);
234 tmpl = crypto_lookup_template(geniv);
239 inst = tmpl->alloc(tb);
244 if ((err = crypto_register_instance(tmpl, inst))) {
249 /* Redo the lookup to use the instance we just registered. */
253 crypto_tmpl_put(tmpl);
255 crypto_larval_kill(larval);
257 crypto_mod_put(larval);
263 static struct crypto_alg *crypto_lookup_skcipher(const char *name, u32 type,
266 struct crypto_alg *alg;
268 alg = crypto_alg_mod_lookup(name, type, mask);
272 if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
273 CRYPTO_ALG_TYPE_GIVCIPHER)
276 if (!((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
277 CRYPTO_ALG_TYPE_BLKCIPHER ? alg->cra_blkcipher.ivsize :
278 alg->cra_ablkcipher.ivsize))
281 return ERR_PTR(crypto_givcipher_default(alg, type, mask));
284 int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, const char *name,
287 struct crypto_alg *alg;
290 type = crypto_skcipher_type(type);
291 mask = crypto_skcipher_mask(mask);
293 alg = crypto_lookup_skcipher(name, type, mask);
297 err = crypto_init_spawn(&spawn->base, alg, spawn->base.inst, mask);
301 EXPORT_SYMBOL_GPL(crypto_grab_skcipher);
303 struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
306 struct crypto_tfm *tfm;
309 type = crypto_skcipher_type(type);
310 mask = crypto_skcipher_mask(mask);
313 struct crypto_alg *alg;
315 alg = crypto_lookup_skcipher(alg_name, type, mask);
321 tfm = __crypto_alloc_tfm(alg, type, mask);
323 return __crypto_ablkcipher_cast(tfm);
331 if (signal_pending(current)) {
339 EXPORT_SYMBOL_GPL(crypto_alloc_ablkcipher);
341 MODULE_LICENSE("GPL");
342 MODULE_DESCRIPTION("Asynchronous block chaining cipher type");