Commit | Line | Data |
---|---|---|
c9af70fb | 1 | /* |
1da177e4 LT |
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. | |
c9af70fb | 8 | * |
1da177e4 LT |
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 | */ | |
3631c650 | 19 | |
72567258 | 20 | #include <crypto/null.h> |
d35d2454 | 21 | #include <crypto/internal/hash.h> |
3631c650 | 22 | #include <crypto/internal/skcipher.h> |
1da177e4 LT |
23 | #include <linux/init.h> |
24 | #include <linux/module.h> | |
25 | #include <linux/mm.h> | |
d0856009 | 26 | #include <linux/string.h> |
1da177e4 | 27 | |
33023463 HX |
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; | |
a0129733 HX |
31 | static struct crypto_skcipher *crypto_default_null_skcipher2; |
32 | static int crypto_default_null_skcipher2_refcnt; | |
33023463 | 33 | |
6c2bb98b HX |
34 | static int null_compress(struct crypto_tfm *tfm, const u8 *src, |
35 | unsigned int slen, u8 *dst, unsigned int *dlen) | |
d0856009 PM |
36 | { |
37 | if (slen > *dlen) | |
38 | return -EINVAL; | |
39 | memcpy(dst, src, slen); | |
40 | *dlen = slen; | |
41 | return 0; | |
42 | } | |
1da177e4 | 43 | |
d35d2454 HX |
44 | static int null_init(struct shash_desc *desc) |
45 | { | |
46 | return 0; | |
47 | } | |
1da177e4 | 48 | |
d35d2454 HX |
49 | static int null_update(struct shash_desc *desc, const u8 *data, |
50 | unsigned int len) | |
51 | { | |
52 | return 0; | |
53 | } | |
1da177e4 | 54 | |
d35d2454 HX |
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; } | |
1da177e4 | 69 | |
6c2bb98b | 70 | static int null_setkey(struct crypto_tfm *tfm, const u8 *key, |
560c06ae | 71 | unsigned int keylen) |
1da177e4 LT |
72 | { return 0; } |
73 | ||
6c2bb98b | 74 | static void null_crypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) |
d0856009 PM |
75 | { |
76 | memcpy(dst, src, NULL_BLOCK_SIZE); | |
77 | } | |
1da177e4 | 78 | |
3631c650 HX |
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 | ||
d35d2454 HX |
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 | } | |
1da177e4 LT |
113 | }; |
114 | ||
70a03bff | 115 | static struct crypto_alg null_algs[3] = { { |
1da177e4 LT |
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, | |
1da177e4 LT |
121 | .cra_u = { .cipher = { |
122 | .cia_min_keysize = NULL_KEY_SIZE, | |
123 | .cia_max_keysize = NULL_KEY_SIZE, | |
124 | .cia_setkey = null_setkey, | |
d0856009 PM |
125 | .cia_encrypt = null_crypt, |
126 | .cia_decrypt = null_crypt } } | |
70a03bff | 127 | }, { |
3631c650 HX |
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, | |
3631c650 HX |
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 } } | |
70a03bff JK |
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 | } }; | |
3631c650 | 153 | |
5d26a105 KC |
154 | MODULE_ALIAS_CRYPTO("compress_null"); |
155 | MODULE_ALIAS_CRYPTO("digest_null"); | |
156 | MODULE_ALIAS_CRYPTO("cipher_null"); | |
1da177e4 | 157 | |
33023463 HX |
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 | ||
a0129733 HX |
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 | ||
3af5b90b | 229 | static int __init crypto_null_mod_init(void) |
1da177e4 LT |
230 | { |
231 | int ret = 0; | |
c9af70fb | 232 | |
70a03bff | 233 | ret = crypto_register_algs(null_algs, ARRAY_SIZE(null_algs)); |
1da177e4 LT |
234 | if (ret < 0) |
235 | goto out; | |
236 | ||
d35d2454 | 237 | ret = crypto_register_shash(&digest_null); |
3631c650 | 238 | if (ret < 0) |
70a03bff | 239 | goto out_unregister_algs; |
1da177e4 | 240 | |
70a03bff | 241 | return 0; |
1da177e4 | 242 | |
70a03bff JK |
243 | out_unregister_algs: |
244 | crypto_unregister_algs(null_algs, ARRAY_SIZE(null_algs)); | |
c9af70fb | 245 | out: |
1da177e4 LT |
246 | return ret; |
247 | } | |
248 | ||
3af5b90b | 249 | static void __exit crypto_null_mod_fini(void) |
1da177e4 | 250 | { |
d35d2454 | 251 | crypto_unregister_shash(&digest_null); |
70a03bff | 252 | crypto_unregister_algs(null_algs, ARRAY_SIZE(null_algs)); |
1da177e4 LT |
253 | } |
254 | ||
3af5b90b KB |
255 | module_init(crypto_null_mod_init); |
256 | module_exit(crypto_null_mod_fini); | |
1da177e4 LT |
257 | |
258 | MODULE_LICENSE("GPL"); | |
259 | MODULE_DESCRIPTION("Null Cryptographic Algorithms"); |