0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <crypto/null.h>
0016 #include <crypto/internal/hash.h>
0017 #include <crypto/internal/skcipher.h>
0018 #include <linux/init.h>
0019 #include <linux/module.h>
0020 #include <linux/mm.h>
0021 #include <linux/string.h>
0022
0023 static DEFINE_MUTEX(crypto_default_null_skcipher_lock);
0024 static struct crypto_sync_skcipher *crypto_default_null_skcipher;
0025 static int crypto_default_null_skcipher_refcnt;
0026
0027 static int null_compress(struct crypto_tfm *tfm, const u8 *src,
0028 unsigned int slen, u8 *dst, unsigned int *dlen)
0029 {
0030 if (slen > *dlen)
0031 return -EINVAL;
0032 memcpy(dst, src, slen);
0033 *dlen = slen;
0034 return 0;
0035 }
0036
0037 static int null_init(struct shash_desc *desc)
0038 {
0039 return 0;
0040 }
0041
0042 static int null_update(struct shash_desc *desc, const u8 *data,
0043 unsigned int len)
0044 {
0045 return 0;
0046 }
0047
0048 static int null_final(struct shash_desc *desc, u8 *out)
0049 {
0050 return 0;
0051 }
0052
0053 static int null_digest(struct shash_desc *desc, const u8 *data,
0054 unsigned int len, u8 *out)
0055 {
0056 return 0;
0057 }
0058
0059 static int null_hash_setkey(struct crypto_shash *tfm, const u8 *key,
0060 unsigned int keylen)
0061 { return 0; }
0062
0063 static int null_skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key,
0064 unsigned int keylen)
0065 { return 0; }
0066
0067 static int null_setkey(struct crypto_tfm *tfm, const u8 *key,
0068 unsigned int keylen)
0069 { return 0; }
0070
0071 static void null_crypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
0072 {
0073 memcpy(dst, src, NULL_BLOCK_SIZE);
0074 }
0075
0076 static int null_skcipher_crypt(struct skcipher_request *req)
0077 {
0078 struct skcipher_walk walk;
0079 int err;
0080
0081 err = skcipher_walk_virt(&walk, req, false);
0082
0083 while (walk.nbytes) {
0084 if (walk.src.virt.addr != walk.dst.virt.addr)
0085 memcpy(walk.dst.virt.addr, walk.src.virt.addr,
0086 walk.nbytes);
0087 err = skcipher_walk_done(&walk, 0);
0088 }
0089
0090 return err;
0091 }
0092
0093 static struct shash_alg digest_null = {
0094 .digestsize = NULL_DIGEST_SIZE,
0095 .setkey = null_hash_setkey,
0096 .init = null_init,
0097 .update = null_update,
0098 .finup = null_digest,
0099 .digest = null_digest,
0100 .final = null_final,
0101 .base = {
0102 .cra_name = "digest_null",
0103 .cra_driver_name = "digest_null-generic",
0104 .cra_blocksize = NULL_BLOCK_SIZE,
0105 .cra_module = THIS_MODULE,
0106 }
0107 };
0108
0109 static struct skcipher_alg skcipher_null = {
0110 .base.cra_name = "ecb(cipher_null)",
0111 .base.cra_driver_name = "ecb-cipher_null",
0112 .base.cra_priority = 100,
0113 .base.cra_blocksize = NULL_BLOCK_SIZE,
0114 .base.cra_ctxsize = 0,
0115 .base.cra_module = THIS_MODULE,
0116 .min_keysize = NULL_KEY_SIZE,
0117 .max_keysize = NULL_KEY_SIZE,
0118 .ivsize = NULL_IV_SIZE,
0119 .setkey = null_skcipher_setkey,
0120 .encrypt = null_skcipher_crypt,
0121 .decrypt = null_skcipher_crypt,
0122 };
0123
0124 static struct crypto_alg null_algs[] = { {
0125 .cra_name = "cipher_null",
0126 .cra_driver_name = "cipher_null-generic",
0127 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
0128 .cra_blocksize = NULL_BLOCK_SIZE,
0129 .cra_ctxsize = 0,
0130 .cra_module = THIS_MODULE,
0131 .cra_u = { .cipher = {
0132 .cia_min_keysize = NULL_KEY_SIZE,
0133 .cia_max_keysize = NULL_KEY_SIZE,
0134 .cia_setkey = null_setkey,
0135 .cia_encrypt = null_crypt,
0136 .cia_decrypt = null_crypt } }
0137 }, {
0138 .cra_name = "compress_null",
0139 .cra_driver_name = "compress_null-generic",
0140 .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
0141 .cra_blocksize = NULL_BLOCK_SIZE,
0142 .cra_ctxsize = 0,
0143 .cra_module = THIS_MODULE,
0144 .cra_u = { .compress = {
0145 .coa_compress = null_compress,
0146 .coa_decompress = null_compress } }
0147 } };
0148
0149 MODULE_ALIAS_CRYPTO("compress_null");
0150 MODULE_ALIAS_CRYPTO("digest_null");
0151 MODULE_ALIAS_CRYPTO("cipher_null");
0152
0153 struct crypto_sync_skcipher *crypto_get_default_null_skcipher(void)
0154 {
0155 struct crypto_sync_skcipher *tfm;
0156
0157 mutex_lock(&crypto_default_null_skcipher_lock);
0158 tfm = crypto_default_null_skcipher;
0159
0160 if (!tfm) {
0161 tfm = crypto_alloc_sync_skcipher("ecb(cipher_null)", 0, 0);
0162 if (IS_ERR(tfm))
0163 goto unlock;
0164
0165 crypto_default_null_skcipher = tfm;
0166 }
0167
0168 crypto_default_null_skcipher_refcnt++;
0169
0170 unlock:
0171 mutex_unlock(&crypto_default_null_skcipher_lock);
0172
0173 return tfm;
0174 }
0175 EXPORT_SYMBOL_GPL(crypto_get_default_null_skcipher);
0176
0177 void crypto_put_default_null_skcipher(void)
0178 {
0179 mutex_lock(&crypto_default_null_skcipher_lock);
0180 if (!--crypto_default_null_skcipher_refcnt) {
0181 crypto_free_sync_skcipher(crypto_default_null_skcipher);
0182 crypto_default_null_skcipher = NULL;
0183 }
0184 mutex_unlock(&crypto_default_null_skcipher_lock);
0185 }
0186 EXPORT_SYMBOL_GPL(crypto_put_default_null_skcipher);
0187
0188 static int __init crypto_null_mod_init(void)
0189 {
0190 int ret = 0;
0191
0192 ret = crypto_register_algs(null_algs, ARRAY_SIZE(null_algs));
0193 if (ret < 0)
0194 goto out;
0195
0196 ret = crypto_register_shash(&digest_null);
0197 if (ret < 0)
0198 goto out_unregister_algs;
0199
0200 ret = crypto_register_skcipher(&skcipher_null);
0201 if (ret < 0)
0202 goto out_unregister_shash;
0203
0204 return 0;
0205
0206 out_unregister_shash:
0207 crypto_unregister_shash(&digest_null);
0208 out_unregister_algs:
0209 crypto_unregister_algs(null_algs, ARRAY_SIZE(null_algs));
0210 out:
0211 return ret;
0212 }
0213
0214 static void __exit crypto_null_mod_fini(void)
0215 {
0216 crypto_unregister_algs(null_algs, ARRAY_SIZE(null_algs));
0217 crypto_unregister_shash(&digest_null);
0218 crypto_unregister_skcipher(&skcipher_null);
0219 }
0220
0221 subsys_initcall(crypto_null_mod_init);
0222 module_exit(crypto_null_mod_fini);
0223
0224 MODULE_LICENSE("GPL");
0225 MODULE_DESCRIPTION("Null Cryptographic Algorithms");