blob: 5b84b0f7cc178fcd9499465add7ae0fcf7a8c75b [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Richard Hartmannc9af70f2010-02-16 20:31:54 +08002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Cryptographic API.
4 *
5 * Null algorithms, aka Much Ado About Nothing.
6 *
7 * These are needed for IPsec, and may be useful in general for
8 * testing & debugging.
Richard Hartmannc9af70f2010-02-16 20:31:54 +08009 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * The null cipher is compliant with RFC2410.
11 *
12 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
Herbert Xu3631c652007-12-13 22:28:59 +080014
Horia Geanta72567252014-03-14 17:46:50 +020015#include <crypto/null.h>
Herbert Xud35d2452008-11-08 08:09:56 +080016#include <crypto/internal/hash.h>
Herbert Xu3631c652007-12-13 22:28:59 +080017#include <crypto/internal/skcipher.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/init.h>
19#include <linux/module.h>
20#include <linux/mm.h>
Patrick McHardyd0856002005-05-16 21:53:41 -070021#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Herbert Xu33023462015-05-21 15:11:09 +080023static DEFINE_MUTEX(crypto_default_null_skcipher_lock);
Kees Cook8d605392018-09-18 19:10:51 -070024static struct crypto_sync_skcipher *crypto_default_null_skcipher;
Herbert Xu33023462015-05-21 15:11:09 +080025static int crypto_default_null_skcipher_refcnt;
26
Herbert Xu6c2bb982006-05-16 22:09:29 +100027static int null_compress(struct crypto_tfm *tfm, const u8 *src,
28 unsigned int slen, u8 *dst, unsigned int *dlen)
Patrick McHardyd0856002005-05-16 21:53:41 -070029{
30 if (slen > *dlen)
31 return -EINVAL;
32 memcpy(dst, src, slen);
33 *dlen = slen;
34 return 0;
35}
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Herbert Xud35d2452008-11-08 08:09:56 +080037static int null_init(struct shash_desc *desc)
38{
39 return 0;
40}
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Herbert Xud35d2452008-11-08 08:09:56 +080042static int null_update(struct shash_desc *desc, const u8 *data,
43 unsigned int len)
44{
45 return 0;
46}
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Herbert Xud35d2452008-11-08 08:09:56 +080048static int null_final(struct shash_desc *desc, u8 *out)
49{
50 return 0;
51}
52
53static int null_digest(struct shash_desc *desc, const u8 *data,
54 unsigned int len, u8 *out)
55{
56 return 0;
57}
58
59static int null_hash_setkey(struct crypto_shash *tfm, const u8 *key,
60 unsigned int keylen)
61{ return 0; }
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Eric Biggers31d40c22019-01-03 20:16:24 -080063static int null_skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key,
64 unsigned int keylen)
65{ return 0; }
66
Herbert Xu6c2bb982006-05-16 22:09:29 +100067static int null_setkey(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +100068 unsigned int keylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{ return 0; }
70
Herbert Xu6c2bb982006-05-16 22:09:29 +100071static void null_crypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
Patrick McHardyd0856002005-05-16 21:53:41 -070072{
73 memcpy(dst, src, NULL_BLOCK_SIZE);
74}
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Eric Biggers31d40c22019-01-03 20:16:24 -080076static int null_skcipher_crypt(struct skcipher_request *req)
Herbert Xu3631c652007-12-13 22:28:59 +080077{
Eric Biggers31d40c22019-01-03 20:16:24 -080078 struct skcipher_walk walk;
Herbert Xu3631c652007-12-13 22:28:59 +080079 int err;
80
Eric Biggers31d40c22019-01-03 20:16:24 -080081 err = skcipher_walk_virt(&walk, req, false);
Herbert Xu3631c652007-12-13 22:28:59 +080082
83 while (walk.nbytes) {
84 if (walk.src.virt.addr != walk.dst.virt.addr)
85 memcpy(walk.dst.virt.addr, walk.src.virt.addr,
86 walk.nbytes);
Eric Biggers31d40c22019-01-03 20:16:24 -080087 err = skcipher_walk_done(&walk, 0);
Herbert Xu3631c652007-12-13 22:28:59 +080088 }
89
90 return err;
91}
92
Herbert Xud35d2452008-11-08 08:09:56 +080093static struct shash_alg digest_null = {
94 .digestsize = NULL_DIGEST_SIZE,
95 .setkey = null_hash_setkey,
96 .init = null_init,
97 .update = null_update,
98 .finup = null_digest,
99 .digest = null_digest,
100 .final = null_final,
101 .base = {
102 .cra_name = "digest_null",
Eric Biggersd6ebf522019-06-02 22:40:57 -0700103 .cra_driver_name = "digest_null-generic",
Herbert Xud35d2452008-11-08 08:09:56 +0800104 .cra_blocksize = NULL_BLOCK_SIZE,
105 .cra_module = THIS_MODULE,
106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107};
108
Eric Biggers31d40c22019-01-03 20:16:24 -0800109static struct skcipher_alg skcipher_null = {
110 .base.cra_name = "ecb(cipher_null)",
111 .base.cra_driver_name = "ecb-cipher_null",
112 .base.cra_priority = 100,
113 .base.cra_blocksize = NULL_BLOCK_SIZE,
114 .base.cra_ctxsize = 0,
115 .base.cra_module = THIS_MODULE,
116 .min_keysize = NULL_KEY_SIZE,
117 .max_keysize = NULL_KEY_SIZE,
118 .ivsize = NULL_IV_SIZE,
119 .setkey = null_skcipher_setkey,
120 .encrypt = null_skcipher_crypt,
121 .decrypt = null_skcipher_crypt,
122};
123
124static struct crypto_alg null_algs[] = { {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 .cra_name = "cipher_null",
Eric Biggersd6ebf522019-06-02 22:40:57 -0700126 .cra_driver_name = "cipher_null-generic",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
128 .cra_blocksize = NULL_BLOCK_SIZE,
129 .cra_ctxsize = 0,
130 .cra_module = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 .cra_u = { .cipher = {
132 .cia_min_keysize = NULL_KEY_SIZE,
133 .cia_max_keysize = NULL_KEY_SIZE,
134 .cia_setkey = null_setkey,
Patrick McHardyd0856002005-05-16 21:53:41 -0700135 .cia_encrypt = null_crypt,
136 .cia_decrypt = null_crypt } }
Jussi Kivilinna70a03bf2012-07-11 14:20:00 +0300137}, {
Jussi Kivilinna70a03bf2012-07-11 14:20:00 +0300138 .cra_name = "compress_null",
Eric Biggersd6ebf522019-06-02 22:40:57 -0700139 .cra_driver_name = "compress_null-generic",
Jussi Kivilinna70a03bf2012-07-11 14:20:00 +0300140 .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
141 .cra_blocksize = NULL_BLOCK_SIZE,
142 .cra_ctxsize = 0,
143 .cra_module = THIS_MODULE,
144 .cra_u = { .compress = {
145 .coa_compress = null_compress,
146 .coa_decompress = null_compress } }
147} };
Herbert Xu3631c652007-12-13 22:28:59 +0800148
Kees Cook5d26a102014-11-20 17:05:53 -0800149MODULE_ALIAS_CRYPTO("compress_null");
150MODULE_ALIAS_CRYPTO("digest_null");
151MODULE_ALIAS_CRYPTO("cipher_null");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Kees Cook8d605392018-09-18 19:10:51 -0700153struct crypto_sync_skcipher *crypto_get_default_null_skcipher(void)
Herbert Xu33023462015-05-21 15:11:09 +0800154{
Kees Cook8d605392018-09-18 19:10:51 -0700155 struct crypto_sync_skcipher *tfm;
Herbert Xu33023462015-05-21 15:11:09 +0800156
157 mutex_lock(&crypto_default_null_skcipher_lock);
158 tfm = crypto_default_null_skcipher;
159
160 if (!tfm) {
Kees Cook8d605392018-09-18 19:10:51 -0700161 tfm = crypto_alloc_sync_skcipher("ecb(cipher_null)", 0, 0);
Herbert Xu33023462015-05-21 15:11:09 +0800162 if (IS_ERR(tfm))
163 goto unlock;
164
165 crypto_default_null_skcipher = tfm;
166 }
167
168 crypto_default_null_skcipher_refcnt++;
169
170unlock:
171 mutex_unlock(&crypto_default_null_skcipher_lock);
172
173 return tfm;
174}
175EXPORT_SYMBOL_GPL(crypto_get_default_null_skcipher);
176
177void crypto_put_default_null_skcipher(void)
178{
179 mutex_lock(&crypto_default_null_skcipher_lock);
180 if (!--crypto_default_null_skcipher_refcnt) {
Kees Cook8d605392018-09-18 19:10:51 -0700181 crypto_free_sync_skcipher(crypto_default_null_skcipher);
Herbert Xu33023462015-05-21 15:11:09 +0800182 crypto_default_null_skcipher = NULL;
183 }
184 mutex_unlock(&crypto_default_null_skcipher_lock);
185}
186EXPORT_SYMBOL_GPL(crypto_put_default_null_skcipher);
187
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800188static int __init crypto_null_mod_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 int ret = 0;
Richard Hartmannc9af70f2010-02-16 20:31:54 +0800191
Jussi Kivilinna70a03bf2012-07-11 14:20:00 +0300192 ret = crypto_register_algs(null_algs, ARRAY_SIZE(null_algs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (ret < 0)
194 goto out;
195
Herbert Xud35d2452008-11-08 08:09:56 +0800196 ret = crypto_register_shash(&digest_null);
Herbert Xu3631c652007-12-13 22:28:59 +0800197 if (ret < 0)
Jussi Kivilinna70a03bf2012-07-11 14:20:00 +0300198 goto out_unregister_algs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Eric Biggers31d40c22019-01-03 20:16:24 -0800200 ret = crypto_register_skcipher(&skcipher_null);
201 if (ret < 0)
202 goto out_unregister_shash;
203
Jussi Kivilinna70a03bf2012-07-11 14:20:00 +0300204 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Eric Biggers31d40c22019-01-03 20:16:24 -0800206out_unregister_shash:
207 crypto_unregister_shash(&digest_null);
Jussi Kivilinna70a03bf2012-07-11 14:20:00 +0300208out_unregister_algs:
209 crypto_unregister_algs(null_algs, ARRAY_SIZE(null_algs));
Richard Hartmannc9af70f2010-02-16 20:31:54 +0800210out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return ret;
212}
213
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800214static void __exit crypto_null_mod_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Jussi Kivilinna70a03bf2012-07-11 14:20:00 +0300216 crypto_unregister_algs(null_algs, ARRAY_SIZE(null_algs));
Eric Biggers31d40c22019-01-03 20:16:24 -0800217 crypto_unregister_shash(&digest_null);
218 crypto_unregister_skcipher(&skcipher_null);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
Eric Biggersc4741b22019-04-11 21:57:42 -0700221subsys_initcall(crypto_null_mod_init);
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800222module_exit(crypto_null_mod_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224MODULE_LICENSE("GPL");
225MODULE_DESCRIPTION("Null Cryptographic Algorithms");