blob: 0d341ddecd542f5205383d2451218d074edae683 [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",
Herbert Xud35d2452008-11-08 08:09:56 +0800103 .cra_blocksize = NULL_BLOCK_SIZE,
104 .cra_module = THIS_MODULE,
105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106};
107
Eric Biggers31d40c22019-01-03 20:16:24 -0800108static struct skcipher_alg skcipher_null = {
109 .base.cra_name = "ecb(cipher_null)",
110 .base.cra_driver_name = "ecb-cipher_null",
111 .base.cra_priority = 100,
112 .base.cra_blocksize = NULL_BLOCK_SIZE,
113 .base.cra_ctxsize = 0,
114 .base.cra_module = THIS_MODULE,
115 .min_keysize = NULL_KEY_SIZE,
116 .max_keysize = NULL_KEY_SIZE,
117 .ivsize = NULL_IV_SIZE,
118 .setkey = null_skcipher_setkey,
119 .encrypt = null_skcipher_crypt,
120 .decrypt = null_skcipher_crypt,
121};
122
123static struct crypto_alg null_algs[] = { {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 .cra_name = "cipher_null",
125 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
126 .cra_blocksize = NULL_BLOCK_SIZE,
127 .cra_ctxsize = 0,
128 .cra_module = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 .cra_u = { .cipher = {
130 .cia_min_keysize = NULL_KEY_SIZE,
131 .cia_max_keysize = NULL_KEY_SIZE,
132 .cia_setkey = null_setkey,
Patrick McHardyd0856002005-05-16 21:53:41 -0700133 .cia_encrypt = null_crypt,
134 .cia_decrypt = null_crypt } }
Jussi Kivilinna70a03bf2012-07-11 14:20:00 +0300135}, {
Jussi Kivilinna70a03bf2012-07-11 14:20:00 +0300136 .cra_name = "compress_null",
137 .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
138 .cra_blocksize = NULL_BLOCK_SIZE,
139 .cra_ctxsize = 0,
140 .cra_module = THIS_MODULE,
141 .cra_u = { .compress = {
142 .coa_compress = null_compress,
143 .coa_decompress = null_compress } }
144} };
Herbert Xu3631c652007-12-13 22:28:59 +0800145
Kees Cook5d26a102014-11-20 17:05:53 -0800146MODULE_ALIAS_CRYPTO("compress_null");
147MODULE_ALIAS_CRYPTO("digest_null");
148MODULE_ALIAS_CRYPTO("cipher_null");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Kees Cook8d605392018-09-18 19:10:51 -0700150struct crypto_sync_skcipher *crypto_get_default_null_skcipher(void)
Herbert Xu33023462015-05-21 15:11:09 +0800151{
Kees Cook8d605392018-09-18 19:10:51 -0700152 struct crypto_sync_skcipher *tfm;
Herbert Xu33023462015-05-21 15:11:09 +0800153
154 mutex_lock(&crypto_default_null_skcipher_lock);
155 tfm = crypto_default_null_skcipher;
156
157 if (!tfm) {
Kees Cook8d605392018-09-18 19:10:51 -0700158 tfm = crypto_alloc_sync_skcipher("ecb(cipher_null)", 0, 0);
Herbert Xu33023462015-05-21 15:11:09 +0800159 if (IS_ERR(tfm))
160 goto unlock;
161
162 crypto_default_null_skcipher = tfm;
163 }
164
165 crypto_default_null_skcipher_refcnt++;
166
167unlock:
168 mutex_unlock(&crypto_default_null_skcipher_lock);
169
170 return tfm;
171}
172EXPORT_SYMBOL_GPL(crypto_get_default_null_skcipher);
173
174void crypto_put_default_null_skcipher(void)
175{
176 mutex_lock(&crypto_default_null_skcipher_lock);
177 if (!--crypto_default_null_skcipher_refcnt) {
Kees Cook8d605392018-09-18 19:10:51 -0700178 crypto_free_sync_skcipher(crypto_default_null_skcipher);
Herbert Xu33023462015-05-21 15:11:09 +0800179 crypto_default_null_skcipher = NULL;
180 }
181 mutex_unlock(&crypto_default_null_skcipher_lock);
182}
183EXPORT_SYMBOL_GPL(crypto_put_default_null_skcipher);
184
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800185static int __init crypto_null_mod_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 int ret = 0;
Richard Hartmannc9af70f2010-02-16 20:31:54 +0800188
Jussi Kivilinna70a03bf2012-07-11 14:20:00 +0300189 ret = crypto_register_algs(null_algs, ARRAY_SIZE(null_algs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (ret < 0)
191 goto out;
192
Herbert Xud35d2452008-11-08 08:09:56 +0800193 ret = crypto_register_shash(&digest_null);
Herbert Xu3631c652007-12-13 22:28:59 +0800194 if (ret < 0)
Jussi Kivilinna70a03bf2012-07-11 14:20:00 +0300195 goto out_unregister_algs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Eric Biggers31d40c22019-01-03 20:16:24 -0800197 ret = crypto_register_skcipher(&skcipher_null);
198 if (ret < 0)
199 goto out_unregister_shash;
200
Jussi Kivilinna70a03bf2012-07-11 14:20:00 +0300201 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Eric Biggers31d40c22019-01-03 20:16:24 -0800203out_unregister_shash:
204 crypto_unregister_shash(&digest_null);
Jussi Kivilinna70a03bf2012-07-11 14:20:00 +0300205out_unregister_algs:
206 crypto_unregister_algs(null_algs, ARRAY_SIZE(null_algs));
Richard Hartmannc9af70f2010-02-16 20:31:54 +0800207out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return ret;
209}
210
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800211static void __exit crypto_null_mod_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
Jussi Kivilinna70a03bf2012-07-11 14:20:00 +0300213 crypto_unregister_algs(null_algs, ARRAY_SIZE(null_algs));
Eric Biggers31d40c22019-01-03 20:16:24 -0800214 crypto_unregister_shash(&digest_null);
215 crypto_unregister_skcipher(&skcipher_null);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
Eric Biggersc4741b22019-04-11 21:57:42 -0700218subsys_initcall(crypto_null_mod_init);
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800219module_exit(crypto_null_mod_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221MODULE_LICENSE("GPL");
222MODULE_DESCRIPTION("Null Cryptographic Algorithms");