crypto: null - Add new default null skcipher
[cascardo/linux.git] / crypto / crypto_null.c
1 /*
2  * Cryptographic API.
3  *
4  * Null algorithms, aka Much Ado About Nothing.
5  *
6  * These are needed for IPsec, and may be useful in general for
7  * testing & debugging.
8  *
9  * The null cipher is compliant with RFC2410.
10  *
11  * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  */
19
20 #include <crypto/null.h>
21 #include <crypto/internal/hash.h>
22 #include <crypto/internal/skcipher.h>
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/mm.h>
26 #include <linux/string.h>
27
28 static DEFINE_MUTEX(crypto_default_null_skcipher_lock);
29 static struct crypto_blkcipher *crypto_default_null_skcipher;
30 static int crypto_default_null_skcipher_refcnt;
31 static struct crypto_skcipher *crypto_default_null_skcipher2;
32 static int crypto_default_null_skcipher2_refcnt;
33
34 static int null_compress(struct crypto_tfm *tfm, const u8 *src,
35                          unsigned int slen, u8 *dst, unsigned int *dlen)
36 {
37         if (slen > *dlen)
38                 return -EINVAL;
39         memcpy(dst, src, slen);
40         *dlen = slen;
41         return 0;
42 }
43
44 static int null_init(struct shash_desc *desc)
45 {
46         return 0;
47 }
48
49 static int null_update(struct shash_desc *desc, const u8 *data,
50                        unsigned int len)
51 {
52         return 0;
53 }
54
55 static int null_final(struct shash_desc *desc, u8 *out)
56 {
57         return 0;
58 }
59
60 static int null_digest(struct shash_desc *desc, const u8 *data,
61                        unsigned int len, u8 *out)
62 {
63         return 0;
64 }
65
66 static int null_hash_setkey(struct crypto_shash *tfm, const u8 *key,
67                             unsigned int keylen)
68 { return 0; }
69
70 static int null_setkey(struct crypto_tfm *tfm, const u8 *key,
71                        unsigned int keylen)
72 { return 0; }
73
74 static void null_crypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
75 {
76         memcpy(dst, src, NULL_BLOCK_SIZE);
77 }
78
79 static int skcipher_null_crypt(struct blkcipher_desc *desc,
80                                struct scatterlist *dst,
81                                struct scatterlist *src, unsigned int nbytes)
82 {
83         struct blkcipher_walk walk;
84         int err;
85
86         blkcipher_walk_init(&walk, dst, src, nbytes);
87         err = blkcipher_walk_virt(desc, &walk);
88
89         while (walk.nbytes) {
90                 if (walk.src.virt.addr != walk.dst.virt.addr)
91                         memcpy(walk.dst.virt.addr, walk.src.virt.addr,
92                                walk.nbytes);
93                 err = blkcipher_walk_done(desc, &walk, 0);
94         }
95
96         return err;
97 }
98
99 static struct shash_alg digest_null = {
100         .digestsize             =       NULL_DIGEST_SIZE,
101         .setkey                 =       null_hash_setkey,
102         .init                   =       null_init,
103         .update                 =       null_update,
104         .finup                  =       null_digest,
105         .digest                 =       null_digest,
106         .final                  =       null_final,
107         .base                   =       {
108                 .cra_name               =       "digest_null",
109                 .cra_flags              =       CRYPTO_ALG_TYPE_SHASH,
110                 .cra_blocksize          =       NULL_BLOCK_SIZE,
111                 .cra_module             =       THIS_MODULE,
112         }
113 };
114
115 static struct crypto_alg null_algs[3] = { {
116         .cra_name               =       "cipher_null",
117         .cra_flags              =       CRYPTO_ALG_TYPE_CIPHER,
118         .cra_blocksize          =       NULL_BLOCK_SIZE,
119         .cra_ctxsize            =       0,
120         .cra_module             =       THIS_MODULE,
121         .cra_u                  =       { .cipher = {
122         .cia_min_keysize        =       NULL_KEY_SIZE,
123         .cia_max_keysize        =       NULL_KEY_SIZE,
124         .cia_setkey             =       null_setkey,
125         .cia_encrypt            =       null_crypt,
126         .cia_decrypt            =       null_crypt } }
127 }, {
128         .cra_name               =       "ecb(cipher_null)",
129         .cra_driver_name        =       "ecb-cipher_null",
130         .cra_priority           =       100,
131         .cra_flags              =       CRYPTO_ALG_TYPE_BLKCIPHER,
132         .cra_blocksize          =       NULL_BLOCK_SIZE,
133         .cra_type               =       &crypto_blkcipher_type,
134         .cra_ctxsize            =       0,
135         .cra_module             =       THIS_MODULE,
136         .cra_u                  =       { .blkcipher = {
137         .min_keysize            =       NULL_KEY_SIZE,
138         .max_keysize            =       NULL_KEY_SIZE,
139         .ivsize                 =       NULL_IV_SIZE,
140         .setkey                 =       null_setkey,
141         .encrypt                =       skcipher_null_crypt,
142         .decrypt                =       skcipher_null_crypt } }
143 }, {
144         .cra_name               =       "compress_null",
145         .cra_flags              =       CRYPTO_ALG_TYPE_COMPRESS,
146         .cra_blocksize          =       NULL_BLOCK_SIZE,
147         .cra_ctxsize            =       0,
148         .cra_module             =       THIS_MODULE,
149         .cra_u                  =       { .compress = {
150         .coa_compress           =       null_compress,
151         .coa_decompress         =       null_compress } }
152 } };
153
154 MODULE_ALIAS_CRYPTO("compress_null");
155 MODULE_ALIAS_CRYPTO("digest_null");
156 MODULE_ALIAS_CRYPTO("cipher_null");
157
158 struct crypto_blkcipher *crypto_get_default_null_skcipher(void)
159 {
160         struct crypto_blkcipher *tfm;
161
162         mutex_lock(&crypto_default_null_skcipher_lock);
163         tfm = crypto_default_null_skcipher;
164
165         if (!tfm) {
166                 tfm = crypto_alloc_blkcipher("ecb(cipher_null)", 0, 0);
167                 if (IS_ERR(tfm))
168                         goto unlock;
169
170                 crypto_default_null_skcipher = tfm;
171         }
172
173         crypto_default_null_skcipher_refcnt++;
174
175 unlock:
176         mutex_unlock(&crypto_default_null_skcipher_lock);
177
178         return tfm;
179 }
180 EXPORT_SYMBOL_GPL(crypto_get_default_null_skcipher);
181
182 void crypto_put_default_null_skcipher(void)
183 {
184         mutex_lock(&crypto_default_null_skcipher_lock);
185         if (!--crypto_default_null_skcipher_refcnt) {
186                 crypto_free_blkcipher(crypto_default_null_skcipher);
187                 crypto_default_null_skcipher = NULL;
188         }
189         mutex_unlock(&crypto_default_null_skcipher_lock);
190 }
191 EXPORT_SYMBOL_GPL(crypto_put_default_null_skcipher);
192
193 struct crypto_skcipher *crypto_get_default_null_skcipher2(void)
194 {
195         struct crypto_skcipher *tfm;
196
197         mutex_lock(&crypto_default_null_skcipher_lock);
198         tfm = crypto_default_null_skcipher2;
199
200         if (!tfm) {
201                 tfm = crypto_alloc_skcipher("ecb(cipher_null)",
202                                             0, CRYPTO_ALG_ASYNC);
203                 if (IS_ERR(tfm))
204                         goto unlock;
205
206                 crypto_default_null_skcipher2 = tfm;
207         }
208
209         crypto_default_null_skcipher2_refcnt++;
210
211 unlock:
212         mutex_unlock(&crypto_default_null_skcipher_lock);
213
214         return tfm;
215 }
216 EXPORT_SYMBOL_GPL(crypto_get_default_null_skcipher2);
217
218 void crypto_put_default_null_skcipher2(void)
219 {
220         mutex_lock(&crypto_default_null_skcipher_lock);
221         if (!--crypto_default_null_skcipher2_refcnt) {
222                 crypto_free_skcipher(crypto_default_null_skcipher2);
223                 crypto_default_null_skcipher2 = NULL;
224         }
225         mutex_unlock(&crypto_default_null_skcipher_lock);
226 }
227 EXPORT_SYMBOL_GPL(crypto_put_default_null_skcipher2);
228
229 static int __init crypto_null_mod_init(void)
230 {
231         int ret = 0;
232
233         ret = crypto_register_algs(null_algs, ARRAY_SIZE(null_algs));
234         if (ret < 0)
235                 goto out;
236
237         ret = crypto_register_shash(&digest_null);
238         if (ret < 0)
239                 goto out_unregister_algs;
240
241         return 0;
242
243 out_unregister_algs:
244         crypto_unregister_algs(null_algs, ARRAY_SIZE(null_algs));
245 out:
246         return ret;
247 }
248
249 static void __exit crypto_null_mod_fini(void)
250 {
251         crypto_unregister_shash(&digest_null);
252         crypto_unregister_algs(null_algs, ARRAY_SIZE(null_algs));
253 }
254
255 module_init(crypto_null_mod_init);
256 module_exit(crypto_null_mod_fini);
257
258 MODULE_LICENSE("GPL");
259 MODULE_DESCRIPTION("Null Cryptographic Algorithms");