crypto_null.c 5.32 KB
Newer Older
1
/*
Linus Torvalds's avatar
Linus Torvalds committed
2 3 4 5 6 7
 * Cryptographic API.
 *
 * Null algorithms, aka Much Ado About Nothing.
 *
 * These are needed for IPsec, and may be useful in general for
 * testing & debugging.
8
 *
Linus Torvalds's avatar
Linus Torvalds committed
9 10 11 12 13 14 15 16 17 18
 * The null cipher is compliant with RFC2410.
 *
 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 */
19

20
#include <crypto/null.h>
21
#include <crypto/internal/hash.h>
22
#include <crypto/internal/skcipher.h>
Linus Torvalds's avatar
Linus Torvalds committed
23 24 25
#include <linux/init.h>
#include <linux/module.h>
#include <linux/mm.h>
26
#include <linux/string.h>
Linus Torvalds's avatar
Linus Torvalds committed
27

28
static DEFINE_MUTEX(crypto_default_null_skcipher_lock);
29
static struct crypto_skcipher *crypto_default_null_skcipher;
30 31
static int crypto_default_null_skcipher_refcnt;

32 33
static int null_compress(struct crypto_tfm *tfm, const u8 *src,
			 unsigned int slen, u8 *dst, unsigned int *dlen)
34 35 36 37 38 39 40
{
	if (slen > *dlen)
		return -EINVAL;
	memcpy(dst, src, slen);
	*dlen = slen;
	return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
41

42 43 44 45
static int null_init(struct shash_desc *desc)
{
	return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
46

47 48 49 50 51
static int null_update(struct shash_desc *desc, const u8 *data,
		       unsigned int len)
{
	return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
52

53 54 55 56 57 58 59 60 61 62 63 64 65 66
static int null_final(struct shash_desc *desc, u8 *out)
{
	return 0;
}

static int null_digest(struct shash_desc *desc, const u8 *data,
		       unsigned int len, u8 *out)
{
	return 0;
}

static int null_hash_setkey(struct crypto_shash *tfm, const u8 *key,
			    unsigned int keylen)
{ return 0; }
Linus Torvalds's avatar
Linus Torvalds committed
67

68
static int null_setkey(struct crypto_tfm *tfm, const u8 *key,
69
		       unsigned int keylen)
Linus Torvalds's avatar
Linus Torvalds committed
70 71
{ return 0; }

72
static void null_crypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
73 74 75
{
	memcpy(dst, src, NULL_BLOCK_SIZE);
}
Linus Torvalds's avatar
Linus Torvalds committed
76

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
static int skcipher_null_crypt(struct blkcipher_desc *desc,
			       struct scatterlist *dst,
			       struct scatterlist *src, unsigned int nbytes)
{
	struct blkcipher_walk walk;
	int err;

	blkcipher_walk_init(&walk, dst, src, nbytes);
	err = blkcipher_walk_virt(desc, &walk);

	while (walk.nbytes) {
		if (walk.src.virt.addr != walk.dst.virt.addr)
			memcpy(walk.dst.virt.addr, walk.src.virt.addr,
			       walk.nbytes);
		err = blkcipher_walk_done(desc, &walk, 0);
	}

	return err;
}

97 98 99 100 101 102 103 104 105 106 107 108 109 110
static struct shash_alg digest_null = {
	.digestsize		=	NULL_DIGEST_SIZE,
	.setkey   		=	null_hash_setkey,
	.init   		=	null_init,
	.update 		=	null_update,
	.finup 			=	null_digest,
	.digest 		=	null_digest,
	.final  		=	null_final,
	.base			=	{
		.cra_name		=	"digest_null",
		.cra_flags		=	CRYPTO_ALG_TYPE_SHASH,
		.cra_blocksize		=	NULL_BLOCK_SIZE,
		.cra_module		=	THIS_MODULE,
	}
Linus Torvalds's avatar
Linus Torvalds committed
111 112
};

113
static struct crypto_alg null_algs[3] = { {
Linus Torvalds's avatar
Linus Torvalds committed
114 115 116 117 118 119 120 121 122
	.cra_name		=	"cipher_null",
	.cra_flags		=	CRYPTO_ALG_TYPE_CIPHER,
	.cra_blocksize		=	NULL_BLOCK_SIZE,
	.cra_ctxsize		=	0,
	.cra_module		=	THIS_MODULE,
	.cra_u			=	{ .cipher = {
	.cia_min_keysize	=	NULL_KEY_SIZE,
	.cia_max_keysize	=	NULL_KEY_SIZE,
	.cia_setkey		= 	null_setkey,
123 124
	.cia_encrypt		=	null_crypt,
	.cia_decrypt		=	null_crypt } }
125
}, {
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
	.cra_name		=	"ecb(cipher_null)",
	.cra_driver_name	=	"ecb-cipher_null",
	.cra_priority		=	100,
	.cra_flags		=	CRYPTO_ALG_TYPE_BLKCIPHER,
	.cra_blocksize		=	NULL_BLOCK_SIZE,
	.cra_type		=	&crypto_blkcipher_type,
	.cra_ctxsize		=	0,
	.cra_module		=	THIS_MODULE,
	.cra_u			=	{ .blkcipher = {
	.min_keysize		=	NULL_KEY_SIZE,
	.max_keysize		=	NULL_KEY_SIZE,
	.ivsize			=	NULL_IV_SIZE,
	.setkey			= 	null_setkey,
	.encrypt		=	skcipher_null_crypt,
	.decrypt		=	skcipher_null_crypt } }
141 142 143 144 145 146 147 148 149 150
}, {
	.cra_name		=	"compress_null",
	.cra_flags		=	CRYPTO_ALG_TYPE_COMPRESS,
	.cra_blocksize		=	NULL_BLOCK_SIZE,
	.cra_ctxsize		=	0,
	.cra_module		=	THIS_MODULE,
	.cra_u			=	{ .compress = {
	.coa_compress		=	null_compress,
	.coa_decompress		=	null_compress } }
} };
151

152 153 154
MODULE_ALIAS_CRYPTO("compress_null");
MODULE_ALIAS_CRYPTO("digest_null");
MODULE_ALIAS_CRYPTO("cipher_null");
Linus Torvalds's avatar
Linus Torvalds committed
155

156
struct crypto_skcipher *crypto_get_default_null_skcipher(void)
157
{
158
	struct crypto_skcipher *tfm;
159 160 161 162 163

	mutex_lock(&crypto_default_null_skcipher_lock);
	tfm = crypto_default_null_skcipher;

	if (!tfm) {
164 165
		tfm = crypto_alloc_skcipher("ecb(cipher_null)",
					    0, CRYPTO_ALG_ASYNC);
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
		if (IS_ERR(tfm))
			goto unlock;

		crypto_default_null_skcipher = tfm;
	}

	crypto_default_null_skcipher_refcnt++;

unlock:
	mutex_unlock(&crypto_default_null_skcipher_lock);

	return tfm;
}
EXPORT_SYMBOL_GPL(crypto_get_default_null_skcipher);

void crypto_put_default_null_skcipher(void)
{
	mutex_lock(&crypto_default_null_skcipher_lock);
	if (!--crypto_default_null_skcipher_refcnt) {
185
		crypto_free_skcipher(crypto_default_null_skcipher);
186 187 188 189 190 191
		crypto_default_null_skcipher = NULL;
	}
	mutex_unlock(&crypto_default_null_skcipher_lock);
}
EXPORT_SYMBOL_GPL(crypto_put_default_null_skcipher);

192
static int __init crypto_null_mod_init(void)
Linus Torvalds's avatar
Linus Torvalds committed
193 194
{
	int ret = 0;
195

196
	ret = crypto_register_algs(null_algs, ARRAY_SIZE(null_algs));
Linus Torvalds's avatar
Linus Torvalds committed
197 198 199
	if (ret < 0)
		goto out;

200
	ret = crypto_register_shash(&digest_null);
201
	if (ret < 0)
202
		goto out_unregister_algs;
Linus Torvalds's avatar
Linus Torvalds committed
203

204
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
205

206 207
out_unregister_algs:
	crypto_unregister_algs(null_algs, ARRAY_SIZE(null_algs));
208
out:
Linus Torvalds's avatar
Linus Torvalds committed
209 210 211
	return ret;
}

212
static void __exit crypto_null_mod_fini(void)
Linus Torvalds's avatar
Linus Torvalds committed
213
{
214
	crypto_unregister_shash(&digest_null);
215
	crypto_unregister_algs(null_algs, ARRAY_SIZE(null_algs));
Linus Torvalds's avatar
Linus Torvalds committed
216 217
}

218 219
module_init(crypto_null_mod_init);
module_exit(crypto_null_mod_fini);
Linus Torvalds's avatar
Linus Torvalds committed
220 221 222

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Null Cryptographic Algorithms");