Back to home page

LXR

 
 

    


0001 /*
0002  * Cryptographic API.
0003  *
0004  * Null algorithms, aka Much Ado About Nothing.
0005  *
0006  * These are needed for IPsec, and may be useful in general for
0007  * testing & debugging.
0008  *
0009  * The null cipher is compliant with RFC2410.
0010  *
0011  * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
0012  *
0013  * This program is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  */
0019 
0020 #include <crypto/null.h>
0021 #include <crypto/internal/hash.h>
0022 #include <crypto/internal/skcipher.h>
0023 #include <linux/init.h>
0024 #include <linux/module.h>
0025 #include <linux/mm.h>
0026 #include <linux/string.h>
0027 
0028 static DEFINE_MUTEX(crypto_default_null_skcipher_lock);
0029 static struct crypto_skcipher *crypto_default_null_skcipher;
0030 static int crypto_default_null_skcipher_refcnt;
0031 
0032 static int null_compress(struct crypto_tfm *tfm, const u8 *src,
0033              unsigned int slen, u8 *dst, unsigned int *dlen)
0034 {
0035     if (slen > *dlen)
0036         return -EINVAL;
0037     memcpy(dst, src, slen);
0038     *dlen = slen;
0039     return 0;
0040 }
0041 
0042 static int null_init(struct shash_desc *desc)
0043 {
0044     return 0;
0045 }
0046 
0047 static int null_update(struct shash_desc *desc, const u8 *data,
0048                unsigned int len)
0049 {
0050     return 0;
0051 }
0052 
0053 static int null_final(struct shash_desc *desc, u8 *out)
0054 {
0055     return 0;
0056 }
0057 
0058 static int null_digest(struct shash_desc *desc, const u8 *data,
0059                unsigned int len, u8 *out)
0060 {
0061     return 0;
0062 }
0063 
0064 static int null_hash_setkey(struct crypto_shash *tfm, const u8 *key,
0065                 unsigned int keylen)
0066 { return 0; }
0067 
0068 static int null_setkey(struct crypto_tfm *tfm, const u8 *key,
0069                unsigned int keylen)
0070 { return 0; }
0071 
0072 static void null_crypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
0073 {
0074     memcpy(dst, src, NULL_BLOCK_SIZE);
0075 }
0076 
0077 static int skcipher_null_crypt(struct blkcipher_desc *desc,
0078                    struct scatterlist *dst,
0079                    struct scatterlist *src, unsigned int nbytes)
0080 {
0081     struct blkcipher_walk walk;
0082     int err;
0083 
0084     blkcipher_walk_init(&walk, dst, src, nbytes);
0085     err = blkcipher_walk_virt(desc, &walk);
0086 
0087     while (walk.nbytes) {
0088         if (walk.src.virt.addr != walk.dst.virt.addr)
0089             memcpy(walk.dst.virt.addr, walk.src.virt.addr,
0090                    walk.nbytes);
0091         err = blkcipher_walk_done(desc, &walk, 0);
0092     }
0093 
0094     return err;
0095 }
0096 
0097 static struct shash_alg digest_null = {
0098     .digestsize     =   NULL_DIGEST_SIZE,
0099     .setkey         =   null_hash_setkey,
0100     .init           =   null_init,
0101     .update         =   null_update,
0102     .finup          =   null_digest,
0103     .digest         =   null_digest,
0104     .final          =   null_final,
0105     .base           =   {
0106         .cra_name       =   "digest_null",
0107         .cra_flags      =   CRYPTO_ALG_TYPE_SHASH,
0108         .cra_blocksize      =   NULL_BLOCK_SIZE,
0109         .cra_module     =   THIS_MODULE,
0110     }
0111 };
0112 
0113 static struct crypto_alg null_algs[3] = { {
0114     .cra_name       =   "cipher_null",
0115     .cra_flags      =   CRYPTO_ALG_TYPE_CIPHER,
0116     .cra_blocksize      =   NULL_BLOCK_SIZE,
0117     .cra_ctxsize        =   0,
0118     .cra_module     =   THIS_MODULE,
0119     .cra_u          =   { .cipher = {
0120     .cia_min_keysize    =   NULL_KEY_SIZE,
0121     .cia_max_keysize    =   NULL_KEY_SIZE,
0122     .cia_setkey     =   null_setkey,
0123     .cia_encrypt        =   null_crypt,
0124     .cia_decrypt        =   null_crypt } }
0125 }, {
0126     .cra_name       =   "ecb(cipher_null)",
0127     .cra_driver_name    =   "ecb-cipher_null",
0128     .cra_priority       =   100,
0129     .cra_flags      =   CRYPTO_ALG_TYPE_BLKCIPHER,
0130     .cra_blocksize      =   NULL_BLOCK_SIZE,
0131     .cra_type       =   &crypto_blkcipher_type,
0132     .cra_ctxsize        =   0,
0133     .cra_module     =   THIS_MODULE,
0134     .cra_u          =   { .blkcipher = {
0135     .min_keysize        =   NULL_KEY_SIZE,
0136     .max_keysize        =   NULL_KEY_SIZE,
0137     .ivsize         =   NULL_IV_SIZE,
0138     .setkey         =   null_setkey,
0139     .encrypt        =   skcipher_null_crypt,
0140     .decrypt        =   skcipher_null_crypt } }
0141 }, {
0142     .cra_name       =   "compress_null",
0143     .cra_flags      =   CRYPTO_ALG_TYPE_COMPRESS,
0144     .cra_blocksize      =   NULL_BLOCK_SIZE,
0145     .cra_ctxsize        =   0,
0146     .cra_module     =   THIS_MODULE,
0147     .cra_u          =   { .compress = {
0148     .coa_compress       =   null_compress,
0149     .coa_decompress     =   null_compress } }
0150 } };
0151 
0152 MODULE_ALIAS_CRYPTO("compress_null");
0153 MODULE_ALIAS_CRYPTO("digest_null");
0154 MODULE_ALIAS_CRYPTO("cipher_null");
0155 
0156 struct crypto_skcipher *crypto_get_default_null_skcipher(void)
0157 {
0158     struct crypto_skcipher *tfm;
0159 
0160     mutex_lock(&crypto_default_null_skcipher_lock);
0161     tfm = crypto_default_null_skcipher;
0162 
0163     if (!tfm) {
0164         tfm = crypto_alloc_skcipher("ecb(cipher_null)",
0165                         0, CRYPTO_ALG_ASYNC);
0166         if (IS_ERR(tfm))
0167             goto unlock;
0168 
0169         crypto_default_null_skcipher = tfm;
0170     }
0171 
0172     crypto_default_null_skcipher_refcnt++;
0173 
0174 unlock:
0175     mutex_unlock(&crypto_default_null_skcipher_lock);
0176 
0177     return tfm;
0178 }
0179 EXPORT_SYMBOL_GPL(crypto_get_default_null_skcipher);
0180 
0181 void crypto_put_default_null_skcipher(void)
0182 {
0183     mutex_lock(&crypto_default_null_skcipher_lock);
0184     if (!--crypto_default_null_skcipher_refcnt) {
0185         crypto_free_skcipher(crypto_default_null_skcipher);
0186         crypto_default_null_skcipher = NULL;
0187     }
0188     mutex_unlock(&crypto_default_null_skcipher_lock);
0189 }
0190 EXPORT_SYMBOL_GPL(crypto_put_default_null_skcipher);
0191 
0192 static int __init crypto_null_mod_init(void)
0193 {
0194     int ret = 0;
0195 
0196     ret = crypto_register_algs(null_algs, ARRAY_SIZE(null_algs));
0197     if (ret < 0)
0198         goto out;
0199 
0200     ret = crypto_register_shash(&digest_null);
0201     if (ret < 0)
0202         goto out_unregister_algs;
0203 
0204     return 0;
0205 
0206 out_unregister_algs:
0207     crypto_unregister_algs(null_algs, ARRAY_SIZE(null_algs));
0208 out:
0209     return ret;
0210 }
0211 
0212 static void __exit crypto_null_mod_fini(void)
0213 {
0214     crypto_unregister_shash(&digest_null);
0215     crypto_unregister_algs(null_algs, ARRAY_SIZE(null_algs));
0216 }
0217 
0218 module_init(crypto_null_mod_init);
0219 module_exit(crypto_null_mod_fini);
0220 
0221 MODULE_LICENSE("GPL");
0222 MODULE_DESCRIPTION("Null Cryptographic Algorithms");