1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * 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. 9 * 10 * The null cipher is compliant with RFC2410. 11 * 12 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> 13 */ 14 15 #include <crypto/null.h> 16 #include <crypto/internal/hash.h> 17 #include <crypto/internal/skcipher.h> 18 #include <linux/init.h> 19 #include <linux/module.h> 20 #include <linux/spinlock.h> 21 #include <linux/string.h> 22 23 static DEFINE_SPINLOCK(crypto_default_null_skcipher_lock); 24 static struct crypto_sync_skcipher *crypto_default_null_skcipher; 25 static int crypto_default_null_skcipher_refcnt; 26 27 static int null_compress(struct crypto_tfm *tfm, const u8 *src, 28 unsigned int slen, u8 *dst, unsigned int *dlen) 29 { 30 if (slen > *dlen) 31 return -EINVAL; 32 memcpy(dst, src, slen); 33 *dlen = slen; 34 return 0; 35 } 36 37 static int null_init(struct shash_desc *desc) 38 { 39 return 0; 40 } 41 42 static int null_update(struct shash_desc *desc, const u8 *data, 43 unsigned int len) 44 { 45 return 0; 46 } 47 48 static int null_final(struct shash_desc *desc, u8 *out) 49 { 50 return 0; 51 } 52 53 static int null_digest(struct shash_desc *desc, const u8 *data, 54 unsigned int len, u8 *out) 55 { 56 return 0; 57 } 58 59 static int null_hash_setkey(struct crypto_shash *tfm, const u8 *key, 60 unsigned int keylen) 61 { return 0; } 62 63 static int null_skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key, 64 unsigned int keylen) 65 { return 0; } 66 67 static int null_setkey(struct crypto_tfm *tfm, const u8 *key, 68 unsigned int keylen) 69 { return 0; } 70 71 static void null_crypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) 72 { 73 memcpy(dst, src, NULL_BLOCK_SIZE); 74 } 75 76 static int null_skcipher_crypt(struct skcipher_request *req) 77 { 78 struct skcipher_walk walk; 79 int err; 80 81 err = skcipher_walk_virt(&walk, req, false); 82 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); 87 err = skcipher_walk_done(&walk, 0); 88 } 89 90 return err; 91 } 92 93 static 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", 103 .cra_driver_name = "digest_null-generic", 104 .cra_blocksize = NULL_BLOCK_SIZE, 105 .cra_module = THIS_MODULE, 106 } 107 }; 108 109 static 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 124 static struct crypto_alg null_algs[] = { { 125 .cra_name = "cipher_null", 126 .cra_driver_name = "cipher_null-generic", 127 .cra_flags = CRYPTO_ALG_TYPE_CIPHER, 128 .cra_blocksize = NULL_BLOCK_SIZE, 129 .cra_ctxsize = 0, 130 .cra_module = THIS_MODULE, 131 .cra_u = { .cipher = { 132 .cia_min_keysize = NULL_KEY_SIZE, 133 .cia_max_keysize = NULL_KEY_SIZE, 134 .cia_setkey = null_setkey, 135 .cia_encrypt = null_crypt, 136 .cia_decrypt = null_crypt } } 137 }, { 138 .cra_name = "compress_null", 139 .cra_driver_name = "compress_null-generic", 140 .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 } }; 148 149 MODULE_ALIAS_CRYPTO("compress_null"); 150 MODULE_ALIAS_CRYPTO("digest_null"); 151 MODULE_ALIAS_CRYPTO("cipher_null"); 152 153 struct crypto_sync_skcipher *crypto_get_default_null_skcipher(void) 154 { 155 struct crypto_sync_skcipher *ntfm = NULL; 156 struct crypto_sync_skcipher *tfm; 157 158 spin_lock_bh(&crypto_default_null_skcipher_lock); 159 tfm = crypto_default_null_skcipher; 160 161 if (!tfm) { 162 spin_unlock_bh(&crypto_default_null_skcipher_lock); 163 164 ntfm = crypto_alloc_sync_skcipher("ecb(cipher_null)", 0, 0); 165 if (IS_ERR(ntfm)) 166 return ntfm; 167 168 spin_lock_bh(&crypto_default_null_skcipher_lock); 169 tfm = crypto_default_null_skcipher; 170 if (!tfm) { 171 tfm = ntfm; 172 ntfm = NULL; 173 crypto_default_null_skcipher = tfm; 174 } 175 } 176 177 crypto_default_null_skcipher_refcnt++; 178 spin_unlock_bh(&crypto_default_null_skcipher_lock); 179 180 crypto_free_sync_skcipher(ntfm); 181 182 return tfm; 183 } 184 EXPORT_SYMBOL_GPL(crypto_get_default_null_skcipher); 185 186 void crypto_put_default_null_skcipher(void) 187 { 188 struct crypto_sync_skcipher *tfm = NULL; 189 190 spin_lock_bh(&crypto_default_null_skcipher_lock); 191 if (!--crypto_default_null_skcipher_refcnt) { 192 tfm = crypto_default_null_skcipher; 193 crypto_default_null_skcipher = NULL; 194 } 195 spin_unlock_bh(&crypto_default_null_skcipher_lock); 196 197 crypto_free_sync_skcipher(tfm); 198 } 199 EXPORT_SYMBOL_GPL(crypto_put_default_null_skcipher); 200 201 static int __init crypto_null_mod_init(void) 202 { 203 int ret = 0; 204 205 ret = crypto_register_algs(null_algs, ARRAY_SIZE(null_algs)); 206 if (ret < 0) 207 goto out; 208 209 ret = crypto_register_shash(&digest_null); 210 if (ret < 0) 211 goto out_unregister_algs; 212 213 ret = crypto_register_skcipher(&skcipher_null); 214 if (ret < 0) 215 goto out_unregister_shash; 216 217 return 0; 218 219 out_unregister_shash: 220 crypto_unregister_shash(&digest_null); 221 out_unregister_algs: 222 crypto_unregister_algs(null_algs, ARRAY_SIZE(null_algs)); 223 out: 224 return ret; 225 } 226 227 static void __exit crypto_null_mod_fini(void) 228 { 229 crypto_unregister_algs(null_algs, ARRAY_SIZE(null_algs)); 230 crypto_unregister_shash(&digest_null); 231 crypto_unregister_skcipher(&skcipher_null); 232 } 233 234 subsys_initcall(crypto_null_mod_init); 235 module_exit(crypto_null_mod_fini); 236 237 MODULE_LICENSE("GPL"); 238 MODULE_DESCRIPTION("Null Cryptographic Algorithms"); 239