18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Cryptographic API. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Null algorithms, aka Much Ado About Nothing. 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * These are needed for IPsec, and may be useful in general for 88c2ecf20Sopenharmony_ci * testing & debugging. 98c2ecf20Sopenharmony_ci * 108c2ecf20Sopenharmony_ci * The null cipher is compliant with RFC2410. 118c2ecf20Sopenharmony_ci * 128c2ecf20Sopenharmony_ci * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> 138c2ecf20Sopenharmony_ci */ 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#include <crypto/null.h> 168c2ecf20Sopenharmony_ci#include <crypto/internal/hash.h> 178c2ecf20Sopenharmony_ci#include <crypto/internal/skcipher.h> 188c2ecf20Sopenharmony_ci#include <linux/init.h> 198c2ecf20Sopenharmony_ci#include <linux/module.h> 208c2ecf20Sopenharmony_ci#include <linux/mm.h> 218c2ecf20Sopenharmony_ci#include <linux/string.h> 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(crypto_default_null_skcipher_lock); 248c2ecf20Sopenharmony_cistatic struct crypto_sync_skcipher *crypto_default_null_skcipher; 258c2ecf20Sopenharmony_cistatic int crypto_default_null_skcipher_refcnt; 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_cistatic int null_compress(struct crypto_tfm *tfm, const u8 *src, 288c2ecf20Sopenharmony_ci unsigned int slen, u8 *dst, unsigned int *dlen) 298c2ecf20Sopenharmony_ci{ 308c2ecf20Sopenharmony_ci if (slen > *dlen) 318c2ecf20Sopenharmony_ci return -EINVAL; 328c2ecf20Sopenharmony_ci memcpy(dst, src, slen); 338c2ecf20Sopenharmony_ci *dlen = slen; 348c2ecf20Sopenharmony_ci return 0; 358c2ecf20Sopenharmony_ci} 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_cistatic int null_init(struct shash_desc *desc) 388c2ecf20Sopenharmony_ci{ 398c2ecf20Sopenharmony_ci return 0; 408c2ecf20Sopenharmony_ci} 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_cistatic int null_update(struct shash_desc *desc, const u8 *data, 438c2ecf20Sopenharmony_ci unsigned int len) 448c2ecf20Sopenharmony_ci{ 458c2ecf20Sopenharmony_ci return 0; 468c2ecf20Sopenharmony_ci} 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_cistatic int null_final(struct shash_desc *desc, u8 *out) 498c2ecf20Sopenharmony_ci{ 508c2ecf20Sopenharmony_ci return 0; 518c2ecf20Sopenharmony_ci} 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_cistatic int null_digest(struct shash_desc *desc, const u8 *data, 548c2ecf20Sopenharmony_ci unsigned int len, u8 *out) 558c2ecf20Sopenharmony_ci{ 568c2ecf20Sopenharmony_ci return 0; 578c2ecf20Sopenharmony_ci} 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_cistatic int null_hash_setkey(struct crypto_shash *tfm, const u8 *key, 608c2ecf20Sopenharmony_ci unsigned int keylen) 618c2ecf20Sopenharmony_ci{ return 0; } 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_cistatic int null_skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key, 648c2ecf20Sopenharmony_ci unsigned int keylen) 658c2ecf20Sopenharmony_ci{ return 0; } 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_cistatic int null_setkey(struct crypto_tfm *tfm, const u8 *key, 688c2ecf20Sopenharmony_ci unsigned int keylen) 698c2ecf20Sopenharmony_ci{ return 0; } 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_cistatic void null_crypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) 728c2ecf20Sopenharmony_ci{ 738c2ecf20Sopenharmony_ci memcpy(dst, src, NULL_BLOCK_SIZE); 748c2ecf20Sopenharmony_ci} 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_cistatic int null_skcipher_crypt(struct skcipher_request *req) 778c2ecf20Sopenharmony_ci{ 788c2ecf20Sopenharmony_ci struct skcipher_walk walk; 798c2ecf20Sopenharmony_ci int err; 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci err = skcipher_walk_virt(&walk, req, false); 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci while (walk.nbytes) { 848c2ecf20Sopenharmony_ci if (walk.src.virt.addr != walk.dst.virt.addr) 858c2ecf20Sopenharmony_ci memcpy(walk.dst.virt.addr, walk.src.virt.addr, 868c2ecf20Sopenharmony_ci walk.nbytes); 878c2ecf20Sopenharmony_ci err = skcipher_walk_done(&walk, 0); 888c2ecf20Sopenharmony_ci } 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci return err; 918c2ecf20Sopenharmony_ci} 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_cistatic struct shash_alg digest_null = { 948c2ecf20Sopenharmony_ci .digestsize = NULL_DIGEST_SIZE, 958c2ecf20Sopenharmony_ci .setkey = null_hash_setkey, 968c2ecf20Sopenharmony_ci .init = null_init, 978c2ecf20Sopenharmony_ci .update = null_update, 988c2ecf20Sopenharmony_ci .finup = null_digest, 998c2ecf20Sopenharmony_ci .digest = null_digest, 1008c2ecf20Sopenharmony_ci .final = null_final, 1018c2ecf20Sopenharmony_ci .base = { 1028c2ecf20Sopenharmony_ci .cra_name = "digest_null", 1038c2ecf20Sopenharmony_ci .cra_driver_name = "digest_null-generic", 1048c2ecf20Sopenharmony_ci .cra_blocksize = NULL_BLOCK_SIZE, 1058c2ecf20Sopenharmony_ci .cra_module = THIS_MODULE, 1068c2ecf20Sopenharmony_ci } 1078c2ecf20Sopenharmony_ci}; 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_cistatic struct skcipher_alg skcipher_null = { 1108c2ecf20Sopenharmony_ci .base.cra_name = "ecb(cipher_null)", 1118c2ecf20Sopenharmony_ci .base.cra_driver_name = "ecb-cipher_null", 1128c2ecf20Sopenharmony_ci .base.cra_priority = 100, 1138c2ecf20Sopenharmony_ci .base.cra_blocksize = NULL_BLOCK_SIZE, 1148c2ecf20Sopenharmony_ci .base.cra_ctxsize = 0, 1158c2ecf20Sopenharmony_ci .base.cra_module = THIS_MODULE, 1168c2ecf20Sopenharmony_ci .min_keysize = NULL_KEY_SIZE, 1178c2ecf20Sopenharmony_ci .max_keysize = NULL_KEY_SIZE, 1188c2ecf20Sopenharmony_ci .ivsize = NULL_IV_SIZE, 1198c2ecf20Sopenharmony_ci .setkey = null_skcipher_setkey, 1208c2ecf20Sopenharmony_ci .encrypt = null_skcipher_crypt, 1218c2ecf20Sopenharmony_ci .decrypt = null_skcipher_crypt, 1228c2ecf20Sopenharmony_ci}; 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_cistatic struct crypto_alg null_algs[] = { { 1258c2ecf20Sopenharmony_ci .cra_name = "cipher_null", 1268c2ecf20Sopenharmony_ci .cra_driver_name = "cipher_null-generic", 1278c2ecf20Sopenharmony_ci .cra_flags = CRYPTO_ALG_TYPE_CIPHER, 1288c2ecf20Sopenharmony_ci .cra_blocksize = NULL_BLOCK_SIZE, 1298c2ecf20Sopenharmony_ci .cra_ctxsize = 0, 1308c2ecf20Sopenharmony_ci .cra_module = THIS_MODULE, 1318c2ecf20Sopenharmony_ci .cra_u = { .cipher = { 1328c2ecf20Sopenharmony_ci .cia_min_keysize = NULL_KEY_SIZE, 1338c2ecf20Sopenharmony_ci .cia_max_keysize = NULL_KEY_SIZE, 1348c2ecf20Sopenharmony_ci .cia_setkey = null_setkey, 1358c2ecf20Sopenharmony_ci .cia_encrypt = null_crypt, 1368c2ecf20Sopenharmony_ci .cia_decrypt = null_crypt } } 1378c2ecf20Sopenharmony_ci}, { 1388c2ecf20Sopenharmony_ci .cra_name = "compress_null", 1398c2ecf20Sopenharmony_ci .cra_driver_name = "compress_null-generic", 1408c2ecf20Sopenharmony_ci .cra_flags = CRYPTO_ALG_TYPE_COMPRESS, 1418c2ecf20Sopenharmony_ci .cra_blocksize = NULL_BLOCK_SIZE, 1428c2ecf20Sopenharmony_ci .cra_ctxsize = 0, 1438c2ecf20Sopenharmony_ci .cra_module = THIS_MODULE, 1448c2ecf20Sopenharmony_ci .cra_u = { .compress = { 1458c2ecf20Sopenharmony_ci .coa_compress = null_compress, 1468c2ecf20Sopenharmony_ci .coa_decompress = null_compress } } 1478c2ecf20Sopenharmony_ci} }; 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("compress_null"); 1508c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("digest_null"); 1518c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("cipher_null"); 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_cistruct crypto_sync_skcipher *crypto_get_default_null_skcipher(void) 1548c2ecf20Sopenharmony_ci{ 1558c2ecf20Sopenharmony_ci struct crypto_sync_skcipher *tfm; 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci mutex_lock(&crypto_default_null_skcipher_lock); 1588c2ecf20Sopenharmony_ci tfm = crypto_default_null_skcipher; 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci if (!tfm) { 1618c2ecf20Sopenharmony_ci tfm = crypto_alloc_sync_skcipher("ecb(cipher_null)", 0, 0); 1628c2ecf20Sopenharmony_ci if (IS_ERR(tfm)) 1638c2ecf20Sopenharmony_ci goto unlock; 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci crypto_default_null_skcipher = tfm; 1668c2ecf20Sopenharmony_ci } 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci crypto_default_null_skcipher_refcnt++; 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ciunlock: 1718c2ecf20Sopenharmony_ci mutex_unlock(&crypto_default_null_skcipher_lock); 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci return tfm; 1748c2ecf20Sopenharmony_ci} 1758c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(crypto_get_default_null_skcipher); 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_civoid crypto_put_default_null_skcipher(void) 1788c2ecf20Sopenharmony_ci{ 1798c2ecf20Sopenharmony_ci mutex_lock(&crypto_default_null_skcipher_lock); 1808c2ecf20Sopenharmony_ci if (!--crypto_default_null_skcipher_refcnt) { 1818c2ecf20Sopenharmony_ci crypto_free_sync_skcipher(crypto_default_null_skcipher); 1828c2ecf20Sopenharmony_ci crypto_default_null_skcipher = NULL; 1838c2ecf20Sopenharmony_ci } 1848c2ecf20Sopenharmony_ci mutex_unlock(&crypto_default_null_skcipher_lock); 1858c2ecf20Sopenharmony_ci} 1868c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(crypto_put_default_null_skcipher); 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_cistatic int __init crypto_null_mod_init(void) 1898c2ecf20Sopenharmony_ci{ 1908c2ecf20Sopenharmony_ci int ret = 0; 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci ret = crypto_register_algs(null_algs, ARRAY_SIZE(null_algs)); 1938c2ecf20Sopenharmony_ci if (ret < 0) 1948c2ecf20Sopenharmony_ci goto out; 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_ci ret = crypto_register_shash(&digest_null); 1978c2ecf20Sopenharmony_ci if (ret < 0) 1988c2ecf20Sopenharmony_ci goto out_unregister_algs; 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci ret = crypto_register_skcipher(&skcipher_null); 2018c2ecf20Sopenharmony_ci if (ret < 0) 2028c2ecf20Sopenharmony_ci goto out_unregister_shash; 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci return 0; 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ciout_unregister_shash: 2078c2ecf20Sopenharmony_ci crypto_unregister_shash(&digest_null); 2088c2ecf20Sopenharmony_ciout_unregister_algs: 2098c2ecf20Sopenharmony_ci crypto_unregister_algs(null_algs, ARRAY_SIZE(null_algs)); 2108c2ecf20Sopenharmony_ciout: 2118c2ecf20Sopenharmony_ci return ret; 2128c2ecf20Sopenharmony_ci} 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_cistatic void __exit crypto_null_mod_fini(void) 2158c2ecf20Sopenharmony_ci{ 2168c2ecf20Sopenharmony_ci crypto_unregister_algs(null_algs, ARRAY_SIZE(null_algs)); 2178c2ecf20Sopenharmony_ci crypto_unregister_shash(&digest_null); 2188c2ecf20Sopenharmony_ci crypto_unregister_skcipher(&skcipher_null); 2198c2ecf20Sopenharmony_ci} 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_cisubsys_initcall(crypto_null_mod_init); 2228c2ecf20Sopenharmony_cimodule_exit(crypto_null_mod_fini); 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 2258c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Null Cryptographic Algorithms"); 226