12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * Cryptographic API.
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Null algorithms, aka Much Ado About Nothing.
61da177e4SLinus Torvalds *
71da177e4SLinus Torvalds * These are needed for IPsec, and may be useful in general for
81da177e4SLinus Torvalds * testing & debugging.
91da177e4SLinus Torvalds *
101da177e4SLinus Torvalds * The null cipher is compliant with RFC2410.
111da177e4SLinus Torvalds *
121da177e4SLinus Torvalds * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
131da177e4SLinus Torvalds */
143631c650SHerbert Xu
1572567258SHoria Geanta #include <crypto/null.h>
16d35d2454SHerbert Xu #include <crypto/internal/hash.h>
173631c650SHerbert Xu #include <crypto/internal/skcipher.h>
181da177e4SLinus Torvalds #include <linux/init.h>
191da177e4SLinus Torvalds #include <linux/module.h>
201da177e4SLinus Torvalds #include <linux/mm.h>
21d0856009SPatrick McHardy #include <linux/string.h>
221da177e4SLinus Torvalds
2333023463SHerbert Xu static DEFINE_MUTEX(crypto_default_null_skcipher_lock);
248d605398SKees Cook static struct crypto_sync_skcipher *crypto_default_null_skcipher;
2533023463SHerbert Xu static int crypto_default_null_skcipher_refcnt;
2633023463SHerbert Xu
null_compress(struct crypto_tfm * tfm,const u8 * src,unsigned int slen,u8 * dst,unsigned int * dlen)276c2bb98bSHerbert Xu static int null_compress(struct crypto_tfm *tfm, const u8 *src,
286c2bb98bSHerbert Xu unsigned int slen, u8 *dst, unsigned int *dlen)
29d0856009SPatrick McHardy {
30d0856009SPatrick McHardy if (slen > *dlen)
31d0856009SPatrick McHardy return -EINVAL;
32d0856009SPatrick McHardy memcpy(dst, src, slen);
33d0856009SPatrick McHardy *dlen = slen;
34d0856009SPatrick McHardy return 0;
35d0856009SPatrick McHardy }
361da177e4SLinus Torvalds
null_init(struct shash_desc * desc)37d35d2454SHerbert Xu static int null_init(struct shash_desc *desc)
38d35d2454SHerbert Xu {
39d35d2454SHerbert Xu return 0;
40d35d2454SHerbert Xu }
411da177e4SLinus Torvalds
null_update(struct shash_desc * desc,const u8 * data,unsigned int len)42d35d2454SHerbert Xu static int null_update(struct shash_desc *desc, const u8 *data,
436c2bb98bSHerbert Xu unsigned int len)
44d35d2454SHerbert Xu {
45d35d2454SHerbert Xu return 0;
46d35d2454SHerbert Xu }
471da177e4SLinus Torvalds
null_final(struct shash_desc * desc,u8 * out)48d35d2454SHerbert Xu static int null_final(struct shash_desc *desc, u8 *out)
49d35d2454SHerbert Xu {
50d35d2454SHerbert Xu return 0;
51d35d2454SHerbert Xu }
52d35d2454SHerbert Xu
null_digest(struct shash_desc * desc,const u8 * data,unsigned int len,u8 * out)53d35d2454SHerbert Xu static int null_digest(struct shash_desc *desc, const u8 *data,
54d35d2454SHerbert Xu unsigned int len, u8 *out)
55d35d2454SHerbert Xu {
56d35d2454SHerbert Xu return 0;
57d35d2454SHerbert Xu }
58d35d2454SHerbert Xu
null_hash_setkey(struct crypto_shash * tfm,const u8 * key,unsigned int keylen)59d35d2454SHerbert Xu static int null_hash_setkey(struct crypto_shash *tfm, const u8 *key,
60d35d2454SHerbert Xu unsigned int keylen)
61d35d2454SHerbert Xu { return 0; }
621da177e4SLinus Torvalds
null_skcipher_setkey(struct crypto_skcipher * tfm,const u8 * key,unsigned int keylen)6331d40c20SEric Biggers static int null_skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key,
6431d40c20SEric Biggers unsigned int keylen)
6531d40c20SEric Biggers { return 0; }
6631d40c20SEric Biggers
null_setkey(struct crypto_tfm * tfm,const u8 * key,unsigned int keylen)676c2bb98bSHerbert Xu static int null_setkey(struct crypto_tfm *tfm, const u8 *key,
68560c06aeSHerbert Xu unsigned int keylen)
691da177e4SLinus Torvalds { return 0; }
701da177e4SLinus Torvalds
null_crypt(struct crypto_tfm * tfm,u8 * dst,const u8 * src)716c2bb98bSHerbert Xu static void null_crypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
72d0856009SPatrick McHardy {
73d0856009SPatrick McHardy memcpy(dst, src, NULL_BLOCK_SIZE);
74d0856009SPatrick McHardy }
751da177e4SLinus Torvalds
null_skcipher_crypt(struct skcipher_request * req)7631d40c20SEric Biggers static int null_skcipher_crypt(struct skcipher_request *req)
773631c650SHerbert Xu {
7831d40c20SEric Biggers struct skcipher_walk walk;
793631c650SHerbert Xu int err;
803631c650SHerbert Xu
8131d40c20SEric Biggers err = skcipher_walk_virt(&walk, req, false);
823631c650SHerbert Xu
833631c650SHerbert Xu while (walk.nbytes) {
843631c650SHerbert Xu if (walk.src.virt.addr != walk.dst.virt.addr)
853631c650SHerbert Xu memcpy(walk.dst.virt.addr, walk.src.virt.addr,
863631c650SHerbert Xu walk.nbytes);
8731d40c20SEric Biggers err = skcipher_walk_done(&walk, 0);
883631c650SHerbert Xu }
893631c650SHerbert Xu
903631c650SHerbert Xu return err;
913631c650SHerbert Xu }
923631c650SHerbert Xu
93d35d2454SHerbert Xu static struct shash_alg digest_null = {
94d35d2454SHerbert Xu .digestsize = NULL_DIGEST_SIZE,
95d35d2454SHerbert Xu .setkey = null_hash_setkey,
96d35d2454SHerbert Xu .init = null_init,
97d35d2454SHerbert Xu .update = null_update,
98d35d2454SHerbert Xu .finup = null_digest,
99d35d2454SHerbert Xu .digest = null_digest,
100d35d2454SHerbert Xu .final = null_final,
101d35d2454SHerbert Xu .base = {
1021da177e4SLinus Torvalds .cra_name = "digest_null",
103*d6ebf528SEric Biggers .cra_driver_name = "digest_null-generic",
1041da177e4SLinus Torvalds .cra_blocksize = NULL_BLOCK_SIZE,
1051da177e4SLinus Torvalds .cra_module = THIS_MODULE,
106d35d2454SHerbert Xu }
1071da177e4SLinus Torvalds };
1081da177e4SLinus Torvalds
10931d40c20SEric Biggers static struct skcipher_alg skcipher_null = {
11031d40c20SEric Biggers .base.cra_name = "ecb(cipher_null)",
11131d40c20SEric Biggers .base.cra_driver_name = "ecb-cipher_null",
11231d40c20SEric Biggers .base.cra_priority = 100,
11331d40c20SEric Biggers .base.cra_blocksize = NULL_BLOCK_SIZE,
11431d40c20SEric Biggers .base.cra_ctxsize = 0,
11531d40c20SEric Biggers .base.cra_module = THIS_MODULE,
11631d40c20SEric Biggers .min_keysize = NULL_KEY_SIZE,
11731d40c20SEric Biggers .max_keysize = NULL_KEY_SIZE,
11831d40c20SEric Biggers .ivsize = NULL_IV_SIZE,
11931d40c20SEric Biggers .setkey = null_skcipher_setkey,
12031d40c20SEric Biggers .encrypt = null_skcipher_crypt,
12131d40c20SEric Biggers .decrypt = null_skcipher_crypt,
12231d40c20SEric Biggers };
12331d40c20SEric Biggers
12431d40c20SEric Biggers static struct crypto_alg null_algs[] = { {
1251da177e4SLinus Torvalds .cra_name = "cipher_null",
126*d6ebf528SEric Biggers .cra_driver_name = "cipher_null-generic",
1271da177e4SLinus Torvalds .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
1281da177e4SLinus Torvalds .cra_blocksize = NULL_BLOCK_SIZE,
1291da177e4SLinus Torvalds .cra_ctxsize = 0,
1301da177e4SLinus Torvalds .cra_module = THIS_MODULE,
1311da177e4SLinus Torvalds .cra_u = { .cipher = {
1321da177e4SLinus Torvalds .cia_min_keysize = NULL_KEY_SIZE,
1331da177e4SLinus Torvalds .cia_max_keysize = NULL_KEY_SIZE,
1341da177e4SLinus Torvalds .cia_setkey = null_setkey,
135d0856009SPatrick McHardy .cia_encrypt = null_crypt,
136d0856009SPatrick McHardy .cia_decrypt = null_crypt } }
13770a03bffSJussi Kivilinna }, {
13870a03bffSJussi Kivilinna .cra_name = "compress_null",
139*d6ebf528SEric Biggers .cra_driver_name = "compress_null-generic",
14070a03bffSJussi Kivilinna .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
14170a03bffSJussi Kivilinna .cra_blocksize = NULL_BLOCK_SIZE,
14270a03bffSJussi Kivilinna .cra_ctxsize = 0,
14370a03bffSJussi Kivilinna .cra_module = THIS_MODULE,
14470a03bffSJussi Kivilinna .cra_u = { .compress = {
14570a03bffSJussi Kivilinna .coa_compress = null_compress,
14670a03bffSJussi Kivilinna .coa_decompress = null_compress } }
14770a03bffSJussi Kivilinna } };
1483631c650SHerbert Xu
1495d26a105SKees Cook MODULE_ALIAS_CRYPTO("compress_null");
1505d26a105SKees Cook MODULE_ALIAS_CRYPTO("digest_null");
1515d26a105SKees Cook MODULE_ALIAS_CRYPTO("cipher_null");
1521da177e4SLinus Torvalds
crypto_get_default_null_skcipher(void)1538d605398SKees Cook struct crypto_sync_skcipher *crypto_get_default_null_skcipher(void)
15433023463SHerbert Xu {
1558d605398SKees Cook struct crypto_sync_skcipher *tfm;
15633023463SHerbert Xu
15733023463SHerbert Xu mutex_lock(&crypto_default_null_skcipher_lock);
15833023463SHerbert Xu tfm = crypto_default_null_skcipher;
15933023463SHerbert Xu
16033023463SHerbert Xu if (!tfm) {
1618d605398SKees Cook tfm = crypto_alloc_sync_skcipher("ecb(cipher_null)", 0, 0);
16233023463SHerbert Xu if (IS_ERR(tfm))
16333023463SHerbert Xu goto unlock;
16433023463SHerbert Xu
16533023463SHerbert Xu crypto_default_null_skcipher = tfm;
16633023463SHerbert Xu }
16733023463SHerbert Xu
16833023463SHerbert Xu crypto_default_null_skcipher_refcnt++;
16933023463SHerbert Xu
17033023463SHerbert Xu unlock:
17133023463SHerbert Xu mutex_unlock(&crypto_default_null_skcipher_lock);
17233023463SHerbert Xu
17333023463SHerbert Xu return tfm;
17433023463SHerbert Xu }
17533023463SHerbert Xu EXPORT_SYMBOL_GPL(crypto_get_default_null_skcipher);
17633023463SHerbert Xu
crypto_put_default_null_skcipher(void)17733023463SHerbert Xu void crypto_put_default_null_skcipher(void)
17833023463SHerbert Xu {
17933023463SHerbert Xu mutex_lock(&crypto_default_null_skcipher_lock);
18033023463SHerbert Xu if (!--crypto_default_null_skcipher_refcnt) {
1818d605398SKees Cook crypto_free_sync_skcipher(crypto_default_null_skcipher);
18233023463SHerbert Xu crypto_default_null_skcipher = NULL;
18333023463SHerbert Xu }
18433023463SHerbert Xu mutex_unlock(&crypto_default_null_skcipher_lock);
18533023463SHerbert Xu }
18633023463SHerbert Xu EXPORT_SYMBOL_GPL(crypto_put_default_null_skcipher);
18733023463SHerbert Xu
crypto_null_mod_init(void)1883af5b90bSKamalesh Babulal static int __init crypto_null_mod_init(void)
1891da177e4SLinus Torvalds {
1901da177e4SLinus Torvalds int ret = 0;
1911da177e4SLinus Torvalds
19270a03bffSJussi Kivilinna ret = crypto_register_algs(null_algs, ARRAY_SIZE(null_algs));
1931da177e4SLinus Torvalds if (ret < 0)
1941da177e4SLinus Torvalds goto out;
1951da177e4SLinus Torvalds
196d35d2454SHerbert Xu ret = crypto_register_shash(&digest_null);
1973631c650SHerbert Xu if (ret < 0)
19870a03bffSJussi Kivilinna goto out_unregister_algs;
1991da177e4SLinus Torvalds
20031d40c20SEric Biggers ret = crypto_register_skcipher(&skcipher_null);
20131d40c20SEric Biggers if (ret < 0)
20231d40c20SEric Biggers goto out_unregister_shash;
20331d40c20SEric Biggers
20470a03bffSJussi Kivilinna return 0;
2051da177e4SLinus Torvalds
20631d40c20SEric Biggers out_unregister_shash:
20731d40c20SEric Biggers crypto_unregister_shash(&digest_null);
20870a03bffSJussi Kivilinna out_unregister_algs:
20970a03bffSJussi Kivilinna crypto_unregister_algs(null_algs, ARRAY_SIZE(null_algs));
2101da177e4SLinus Torvalds out:
2111da177e4SLinus Torvalds return ret;
2121da177e4SLinus Torvalds }
2131da177e4SLinus Torvalds
crypto_null_mod_fini(void)2143af5b90bSKamalesh Babulal static void __exit crypto_null_mod_fini(void)
2151da177e4SLinus Torvalds {
21670a03bffSJussi Kivilinna crypto_unregister_algs(null_algs, ARRAY_SIZE(null_algs));
21731d40c20SEric Biggers crypto_unregister_shash(&digest_null);
21831d40c20SEric Biggers crypto_unregister_skcipher(&skcipher_null);
2191da177e4SLinus Torvalds }
2201da177e4SLinus Torvalds
221c4741b23SEric Biggers subsys_initcall(crypto_null_mod_init);
2223af5b90bSKamalesh Babulal module_exit(crypto_null_mod_fini);
2231da177e4SLinus Torvalds
2241da177e4SLinus Torvalds MODULE_LICENSE("GPL");
2251da177e4SLinus Torvalds MODULE_DESCRIPTION("Null Cryptographic Algorithms");
226