blob: c3f683910d07294dcf704f4fbd300e557464828f [file] [log] [blame]
Richard Hartmannc9af70f2010-02-16 20:31:54 +08001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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.
Richard Hartmannc9af70f2010-02-16 20:31:54 +08008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * 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 */
Herbert Xu3631c652007-12-13 22:28:59 +080019
Horia Geanta72567252014-03-14 17:46:50 +020020#include <crypto/null.h>
Herbert Xud35d2452008-11-08 08:09:56 +080021#include <crypto/internal/hash.h>
Herbert Xu3631c652007-12-13 22:28:59 +080022#include <crypto/internal/skcipher.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/module.h>
25#include <linux/mm.h>
Patrick McHardyd0856002005-05-16 21:53:41 -070026#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Herbert Xu33023462015-05-21 15:11:09 +080028static DEFINE_MUTEX(crypto_default_null_skcipher_lock);
29static struct crypto_blkcipher *crypto_default_null_skcipher;
30static int crypto_default_null_skcipher_refcnt;
Herbert Xua0129732016-07-12 13:17:32 +080031static struct crypto_skcipher *crypto_default_null_skcipher2;
32static int crypto_default_null_skcipher2_refcnt;
Herbert Xu33023462015-05-21 15:11:09 +080033
Herbert Xu6c2bb982006-05-16 22:09:29 +100034static int null_compress(struct crypto_tfm *tfm, const u8 *src,
35 unsigned int slen, u8 *dst, unsigned int *dlen)
Patrick McHardyd0856002005-05-16 21:53:41 -070036{
37 if (slen > *dlen)
38 return -EINVAL;
39 memcpy(dst, src, slen);
40 *dlen = slen;
41 return 0;
42}
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Herbert Xud35d2452008-11-08 08:09:56 +080044static int null_init(struct shash_desc *desc)
45{
46 return 0;
47}
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Herbert Xud35d2452008-11-08 08:09:56 +080049static int null_update(struct shash_desc *desc, const u8 *data,
50 unsigned int len)
51{
52 return 0;
53}
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Herbert Xud35d2452008-11-08 08:09:56 +080055static int null_final(struct shash_desc *desc, u8 *out)
56{
57 return 0;
58}
59
60static int null_digest(struct shash_desc *desc, const u8 *data,
61 unsigned int len, u8 *out)
62{
63 return 0;
64}
65
66static int null_hash_setkey(struct crypto_shash *tfm, const u8 *key,
67 unsigned int keylen)
68{ return 0; }
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Herbert Xu6c2bb982006-05-16 22:09:29 +100070static int null_setkey(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +100071 unsigned int keylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{ return 0; }
73
Herbert Xu6c2bb982006-05-16 22:09:29 +100074static void null_crypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
Patrick McHardyd0856002005-05-16 21:53:41 -070075{
76 memcpy(dst, src, NULL_BLOCK_SIZE);
77}
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Herbert Xu3631c652007-12-13 22:28:59 +080079static 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
Herbert Xud35d2452008-11-08 08:09:56 +080099static 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113};
114
Jussi Kivilinna70a03bf2012-07-11 14:20:00 +0300115static struct crypto_alg null_algs[3] = { {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 .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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 .cra_u = { .cipher = {
122 .cia_min_keysize = NULL_KEY_SIZE,
123 .cia_max_keysize = NULL_KEY_SIZE,
124 .cia_setkey = null_setkey,
Patrick McHardyd0856002005-05-16 21:53:41 -0700125 .cia_encrypt = null_crypt,
126 .cia_decrypt = null_crypt } }
Jussi Kivilinna70a03bf2012-07-11 14:20:00 +0300127}, {
Herbert Xu3631c652007-12-13 22:28:59 +0800128 .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,
Herbert Xu3631c652007-12-13 22:28:59 +0800136 .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 } }
Jussi Kivilinna70a03bf2012-07-11 14:20:00 +0300143}, {
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} };
Herbert Xu3631c652007-12-13 22:28:59 +0800153
Kees Cook5d26a102014-11-20 17:05:53 -0800154MODULE_ALIAS_CRYPTO("compress_null");
155MODULE_ALIAS_CRYPTO("digest_null");
156MODULE_ALIAS_CRYPTO("cipher_null");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Herbert Xu33023462015-05-21 15:11:09 +0800158struct 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
175unlock:
176 mutex_unlock(&crypto_default_null_skcipher_lock);
177
178 return tfm;
179}
180EXPORT_SYMBOL_GPL(crypto_get_default_null_skcipher);
181
182void 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}
191EXPORT_SYMBOL_GPL(crypto_put_default_null_skcipher);
192
Herbert Xua0129732016-07-12 13:17:32 +0800193struct 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
211unlock:
212 mutex_unlock(&crypto_default_null_skcipher_lock);
213
214 return tfm;
215}
216EXPORT_SYMBOL_GPL(crypto_get_default_null_skcipher2);
217
218void 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}
227EXPORT_SYMBOL_GPL(crypto_put_default_null_skcipher2);
228
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800229static int __init crypto_null_mod_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 int ret = 0;
Richard Hartmannc9af70f2010-02-16 20:31:54 +0800232
Jussi Kivilinna70a03bf2012-07-11 14:20:00 +0300233 ret = crypto_register_algs(null_algs, ARRAY_SIZE(null_algs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if (ret < 0)
235 goto out;
236
Herbert Xud35d2452008-11-08 08:09:56 +0800237 ret = crypto_register_shash(&digest_null);
Herbert Xu3631c652007-12-13 22:28:59 +0800238 if (ret < 0)
Jussi Kivilinna70a03bf2012-07-11 14:20:00 +0300239 goto out_unregister_algs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Jussi Kivilinna70a03bf2012-07-11 14:20:00 +0300241 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Jussi Kivilinna70a03bf2012-07-11 14:20:00 +0300243out_unregister_algs:
244 crypto_unregister_algs(null_algs, ARRAY_SIZE(null_algs));
Richard Hartmannc9af70f2010-02-16 20:31:54 +0800245out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return ret;
247}
248
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800249static void __exit crypto_null_mod_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Herbert Xud35d2452008-11-08 08:09:56 +0800251 crypto_unregister_shash(&digest_null);
Jussi Kivilinna70a03bf2012-07-11 14:20:00 +0300252 crypto_unregister_algs(null_algs, ARRAY_SIZE(null_algs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800255module_init(crypto_null_mod_init);
256module_exit(crypto_null_mod_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258MODULE_LICENSE("GPL");
259MODULE_DESCRIPTION("Null Cryptographic Algorithms");