^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Cryptographic API.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Null algorithms, aka Much Ado About Nothing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * These are needed for IPsec, and may be useful in general for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * testing & debugging.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * The null cipher is compliant with RFC2410.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <crypto/null.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <crypto/internal/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <crypto/internal/skcipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static DEFINE_MUTEX(crypto_default_null_skcipher_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static struct crypto_sync_skcipher *crypto_default_null_skcipher;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static int crypto_default_null_skcipher_refcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static int null_compress(struct crypto_tfm *tfm, const u8 *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) unsigned int slen, u8 *dst, unsigned int *dlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) if (slen > *dlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) memcpy(dst, src, slen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) *dlen = slen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static int null_init(struct shash_desc *desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static int null_update(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static int null_final(struct shash_desc *desc, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static int null_digest(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) unsigned int len, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static int null_hash_setkey(struct crypto_shash *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) { return 0; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static int null_skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) { return 0; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static int null_setkey(struct crypto_tfm *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) { return 0; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static void null_crypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) memcpy(dst, src, NULL_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) static int null_skcipher_crypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) struct skcipher_walk walk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) err = skcipher_walk_virt(&walk, req, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) while (walk.nbytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (walk.src.virt.addr != walk.dst.virt.addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) memcpy(walk.dst.virt.addr, walk.src.virt.addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) walk.nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) err = skcipher_walk_done(&walk, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) static struct shash_alg digest_null = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) .digestsize = NULL_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) .setkey = null_hash_setkey,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) .init = null_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) .update = null_update,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) .finup = null_digest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) .digest = null_digest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) .final = null_final,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) .base = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) .cra_name = "digest_null",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) .cra_driver_name = "digest_null-generic",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) .cra_blocksize = NULL_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) .cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static struct skcipher_alg skcipher_null = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) .base.cra_name = "ecb(cipher_null)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) .base.cra_driver_name = "ecb-cipher_null",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) .base.cra_priority = 100,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) .base.cra_blocksize = NULL_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) .base.cra_ctxsize = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) .base.cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) .min_keysize = NULL_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) .max_keysize = NULL_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) .ivsize = NULL_IV_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) .setkey = null_skcipher_setkey,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) .encrypt = null_skcipher_crypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) .decrypt = null_skcipher_crypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static struct crypto_alg null_algs[] = { {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) .cra_name = "cipher_null",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) .cra_driver_name = "cipher_null-generic",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) .cra_blocksize = NULL_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) .cra_ctxsize = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) .cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) .cra_u = { .cipher = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) .cia_min_keysize = NULL_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) .cia_max_keysize = NULL_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) .cia_setkey = null_setkey,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) .cia_encrypt = null_crypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) .cia_decrypt = null_crypt } }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) .cra_name = "compress_null",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) .cra_driver_name = "compress_null-generic",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) .cra_blocksize = NULL_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) .cra_ctxsize = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) .cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) .cra_u = { .compress = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) .coa_compress = null_compress,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) .coa_decompress = null_compress } }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) } };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) MODULE_ALIAS_CRYPTO("compress_null");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) MODULE_ALIAS_CRYPTO("digest_null");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) MODULE_ALIAS_CRYPTO("cipher_null");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) struct crypto_sync_skcipher *crypto_get_default_null_skcipher(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) struct crypto_sync_skcipher *tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) mutex_lock(&crypto_default_null_skcipher_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) tfm = crypto_default_null_skcipher;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (!tfm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) tfm = crypto_alloc_sync_skcipher("ecb(cipher_null)", 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (IS_ERR(tfm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) crypto_default_null_skcipher = tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) crypto_default_null_skcipher_refcnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) mutex_unlock(&crypto_default_null_skcipher_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) EXPORT_SYMBOL_GPL(crypto_get_default_null_skcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) void crypto_put_default_null_skcipher(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) mutex_lock(&crypto_default_null_skcipher_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (!--crypto_default_null_skcipher_refcnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) crypto_free_sync_skcipher(crypto_default_null_skcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) crypto_default_null_skcipher = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) mutex_unlock(&crypto_default_null_skcipher_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) EXPORT_SYMBOL_GPL(crypto_put_default_null_skcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) static int __init crypto_null_mod_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) ret = crypto_register_algs(null_algs, ARRAY_SIZE(null_algs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) ret = crypto_register_shash(&digest_null);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) goto out_unregister_algs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) ret = crypto_register_skcipher(&skcipher_null);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) goto out_unregister_shash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) out_unregister_shash:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) crypto_unregister_shash(&digest_null);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) out_unregister_algs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) crypto_unregister_algs(null_algs, ARRAY_SIZE(null_algs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) static void __exit crypto_null_mod_fini(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) crypto_unregister_algs(null_algs, ARRAY_SIZE(null_algs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) crypto_unregister_shash(&digest_null);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) crypto_unregister_skcipher(&skcipher_null);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) subsys_initcall(crypto_null_mod_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) module_exit(crypto_null_mod_fini);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) MODULE_DESCRIPTION("Null Cryptographic Algorithms");