[CRYPTO] skcipher: Add givcrypt operations and givcipher type
[linux-2.6/libata-dev.git] / crypto / ablkcipher.c
blobe403d8137ecd01cdb81e05a62ab03547ee6dec0e
1 /*
2 * Asynchronous block chaining cipher operations.
3 *
4 * This is the asynchronous version of blkcipher.c indicating completion
5 * via a callback.
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)
12 * any later version.
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/slab.h>
22 #include <linux/seq_file.h>
24 #include "internal.h"
26 static int setkey_unaligned(struct crypto_ablkcipher *tfm, const u8 *key,
27 unsigned int keylen)
29 struct ablkcipher_alg *cipher = crypto_ablkcipher_alg(tfm);
30 unsigned long alignmask = crypto_ablkcipher_alignmask(tfm);
31 int ret;
32 u8 *buffer, *alignbuffer;
33 unsigned long absize;
35 absize = keylen + alignmask;
36 buffer = kmalloc(absize, GFP_ATOMIC);
37 if (!buffer)
38 return -ENOMEM;
40 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
41 memcpy(alignbuffer, key, keylen);
42 ret = cipher->setkey(tfm, alignbuffer, keylen);
43 memset(alignbuffer, 0, keylen);
44 kfree(buffer);
45 return ret;
48 static int setkey(struct crypto_ablkcipher *tfm, const u8 *key,
49 unsigned int keylen)
51 struct ablkcipher_alg *cipher = crypto_ablkcipher_alg(tfm);
52 unsigned long alignmask = crypto_ablkcipher_alignmask(tfm);
54 if (keylen < cipher->min_keysize || keylen > cipher->max_keysize) {
55 crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
56 return -EINVAL;
59 if ((unsigned long)key & alignmask)
60 return setkey_unaligned(tfm, key, keylen);
62 return cipher->setkey(tfm, key, keylen);
65 static unsigned int crypto_ablkcipher_ctxsize(struct crypto_alg *alg, u32 type,
66 u32 mask)
68 return alg->cra_ctxsize;
71 static int crypto_init_ablkcipher_ops(struct crypto_tfm *tfm, u32 type,
72 u32 mask)
74 struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
75 struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher;
77 if (alg->ivsize > PAGE_SIZE / 8)
78 return -EINVAL;
80 crt->setkey = setkey;
81 crt->encrypt = alg->encrypt;
82 crt->decrypt = alg->decrypt;
83 crt->ivsize = alg->ivsize;
85 return 0;
88 static void crypto_ablkcipher_show(struct seq_file *m, struct crypto_alg *alg)
89 __attribute__ ((unused));
90 static void crypto_ablkcipher_show(struct seq_file *m, struct crypto_alg *alg)
92 struct ablkcipher_alg *ablkcipher = &alg->cra_ablkcipher;
94 seq_printf(m, "type : ablkcipher\n");
95 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
96 seq_printf(m, "min keysize : %u\n", ablkcipher->min_keysize);
97 seq_printf(m, "max keysize : %u\n", ablkcipher->max_keysize);
98 seq_printf(m, "ivsize : %u\n", ablkcipher->ivsize);
101 const struct crypto_type crypto_ablkcipher_type = {
102 .ctxsize = crypto_ablkcipher_ctxsize,
103 .init = crypto_init_ablkcipher_ops,
104 #ifdef CONFIG_PROC_FS
105 .show = crypto_ablkcipher_show,
106 #endif
108 EXPORT_SYMBOL_GPL(crypto_ablkcipher_type);
110 static int no_givdecrypt(struct skcipher_givcrypt_request *req)
112 return -ENOSYS;
115 static int crypto_init_givcipher_ops(struct crypto_tfm *tfm, u32 type,
116 u32 mask)
118 struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
119 struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher;
121 if (alg->ivsize > PAGE_SIZE / 8)
122 return -EINVAL;
124 crt->setkey = setkey;
125 crt->encrypt = alg->encrypt;
126 crt->decrypt = alg->decrypt;
127 crt->givencrypt = alg->givencrypt;
128 crt->givdecrypt = alg->givdecrypt ?: no_givdecrypt;
129 crt->ivsize = alg->ivsize;
131 return 0;
134 static void crypto_givcipher_show(struct seq_file *m, struct crypto_alg *alg)
135 __attribute__ ((unused));
136 static void crypto_givcipher_show(struct seq_file *m, struct crypto_alg *alg)
138 struct ablkcipher_alg *ablkcipher = &alg->cra_ablkcipher;
140 seq_printf(m, "type : givcipher\n");
141 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
142 seq_printf(m, "min keysize : %u\n", ablkcipher->min_keysize);
143 seq_printf(m, "max keysize : %u\n", ablkcipher->max_keysize);
144 seq_printf(m, "ivsize : %u\n", ablkcipher->ivsize);
147 const struct crypto_type crypto_givcipher_type = {
148 .ctxsize = crypto_ablkcipher_ctxsize,
149 .init = crypto_init_givcipher_ops,
150 #ifdef CONFIG_PROC_FS
151 .show = crypto_givcipher_show,
152 #endif
154 EXPORT_SYMBOL_GPL(crypto_givcipher_type);
156 int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, const char *name,
157 u32 type, u32 mask)
159 struct crypto_alg *alg;
160 int err;
162 type = crypto_skcipher_type(type);
163 mask = crypto_skcipher_mask(mask);
165 alg = crypto_alg_mod_lookup(name, type, mask);
166 if (IS_ERR(alg))
167 return PTR_ERR(alg);
169 err = crypto_init_spawn(&spawn->base, alg, spawn->base.inst, mask);
170 crypto_mod_put(alg);
171 return err;
173 EXPORT_SYMBOL_GPL(crypto_grab_skcipher);
175 MODULE_LICENSE("GPL");
176 MODULE_DESCRIPTION("Asynchronous block chaining cipher type");