Richard Hartmann | c9af70f | 2010-02-16 20:31:54 +0800 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Cryptographic API. |
| 3 | * |
| 4 | * Null algorithms, aka Much Ado About Nothing. |
| 5 | * |
| 6 | * These are needed for IPsec, and may be useful in general for |
| 7 | * testing & debugging. |
Richard Hartmann | c9af70f | 2010-02-16 20:31:54 +0800 | [diff] [blame] | 8 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * The null cipher is compliant with RFC2410. |
| 10 | * |
| 11 | * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | */ |
Herbert Xu | 3631c65 | 2007-12-13 22:28:59 +0800 | [diff] [blame] | 19 | |
Horia Geanta | 7256725 | 2014-03-14 17:46:50 +0200 | [diff] [blame] | 20 | #include <crypto/null.h> |
Herbert Xu | d35d245 | 2008-11-08 08:09:56 +0800 | [diff] [blame] | 21 | #include <crypto/internal/hash.h> |
Herbert Xu | 3631c65 | 2007-12-13 22:28:59 +0800 | [diff] [blame] | 22 | #include <crypto/internal/skcipher.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/init.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/mm.h> |
Patrick McHardy | d085600 | 2005-05-16 21:53:41 -0700 | [diff] [blame] | 26 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Herbert Xu | 3302346 | 2015-05-21 15:11:09 +0800 | [diff] [blame] | 28 | static DEFINE_MUTEX(crypto_default_null_skcipher_lock); |
| 29 | static struct crypto_blkcipher *crypto_default_null_skcipher; |
| 30 | static int crypto_default_null_skcipher_refcnt; |
Herbert Xu | a012973 | 2016-07-12 13:17:32 +0800 | [diff] [blame] | 31 | static struct crypto_skcipher *crypto_default_null_skcipher2; |
| 32 | static int crypto_default_null_skcipher2_refcnt; |
Herbert Xu | 3302346 | 2015-05-21 15:11:09 +0800 | [diff] [blame] | 33 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 34 | static int null_compress(struct crypto_tfm *tfm, const u8 *src, |
| 35 | unsigned int slen, u8 *dst, unsigned int *dlen) |
Patrick McHardy | d085600 | 2005-05-16 21:53:41 -0700 | [diff] [blame] | 36 | { |
| 37 | if (slen > *dlen) |
| 38 | return -EINVAL; |
| 39 | memcpy(dst, src, slen); |
| 40 | *dlen = slen; |
| 41 | return 0; |
| 42 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Herbert Xu | d35d245 | 2008-11-08 08:09:56 +0800 | [diff] [blame] | 44 | static int null_init(struct shash_desc *desc) |
| 45 | { |
| 46 | return 0; |
| 47 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Herbert Xu | d35d245 | 2008-11-08 08:09:56 +0800 | [diff] [blame] | 49 | static int null_update(struct shash_desc *desc, const u8 *data, |
| 50 | unsigned int len) |
| 51 | { |
| 52 | return 0; |
| 53 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Herbert Xu | d35d245 | 2008-11-08 08:09:56 +0800 | [diff] [blame] | 55 | static int null_final(struct shash_desc *desc, u8 *out) |
| 56 | { |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | static int null_digest(struct shash_desc *desc, const u8 *data, |
| 61 | unsigned int len, u8 *out) |
| 62 | { |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | static int null_hash_setkey(struct crypto_shash *tfm, const u8 *key, |
| 67 | unsigned int keylen) |
| 68 | { return 0; } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 70 | static int null_setkey(struct crypto_tfm *tfm, const u8 *key, |
Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 71 | unsigned int keylen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | { return 0; } |
| 73 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 74 | static void null_crypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) |
Patrick McHardy | d085600 | 2005-05-16 21:53:41 -0700 | [diff] [blame] | 75 | { |
| 76 | memcpy(dst, src, NULL_BLOCK_SIZE); |
| 77 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
Herbert Xu | 3631c65 | 2007-12-13 22:28:59 +0800 | [diff] [blame] | 79 | static int skcipher_null_crypt(struct blkcipher_desc *desc, |
| 80 | struct scatterlist *dst, |
| 81 | struct scatterlist *src, unsigned int nbytes) |
| 82 | { |
| 83 | struct blkcipher_walk walk; |
| 84 | int err; |
| 85 | |
| 86 | blkcipher_walk_init(&walk, dst, src, nbytes); |
| 87 | err = blkcipher_walk_virt(desc, &walk); |
| 88 | |
| 89 | while (walk.nbytes) { |
| 90 | if (walk.src.virt.addr != walk.dst.virt.addr) |
| 91 | memcpy(walk.dst.virt.addr, walk.src.virt.addr, |
| 92 | walk.nbytes); |
| 93 | err = blkcipher_walk_done(desc, &walk, 0); |
| 94 | } |
| 95 | |
| 96 | return err; |
| 97 | } |
| 98 | |
Herbert Xu | d35d245 | 2008-11-08 08:09:56 +0800 | [diff] [blame] | 99 | static struct shash_alg digest_null = { |
| 100 | .digestsize = NULL_DIGEST_SIZE, |
| 101 | .setkey = null_hash_setkey, |
| 102 | .init = null_init, |
| 103 | .update = null_update, |
| 104 | .finup = null_digest, |
| 105 | .digest = null_digest, |
| 106 | .final = null_final, |
| 107 | .base = { |
| 108 | .cra_name = "digest_null", |
| 109 | .cra_flags = CRYPTO_ALG_TYPE_SHASH, |
| 110 | .cra_blocksize = NULL_BLOCK_SIZE, |
| 111 | .cra_module = THIS_MODULE, |
| 112 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | }; |
| 114 | |
Jussi Kivilinna | 70a03bf | 2012-07-11 14:20:00 +0300 | [diff] [blame] | 115 | static struct crypto_alg null_algs[3] = { { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | .cra_name = "cipher_null", |
| 117 | .cra_flags = CRYPTO_ALG_TYPE_CIPHER, |
| 118 | .cra_blocksize = NULL_BLOCK_SIZE, |
| 119 | .cra_ctxsize = 0, |
| 120 | .cra_module = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | .cra_u = { .cipher = { |
| 122 | .cia_min_keysize = NULL_KEY_SIZE, |
| 123 | .cia_max_keysize = NULL_KEY_SIZE, |
| 124 | .cia_setkey = null_setkey, |
Patrick McHardy | d085600 | 2005-05-16 21:53:41 -0700 | [diff] [blame] | 125 | .cia_encrypt = null_crypt, |
| 126 | .cia_decrypt = null_crypt } } |
Jussi Kivilinna | 70a03bf | 2012-07-11 14:20:00 +0300 | [diff] [blame] | 127 | }, { |
Herbert Xu | 3631c65 | 2007-12-13 22:28:59 +0800 | [diff] [blame] | 128 | .cra_name = "ecb(cipher_null)", |
| 129 | .cra_driver_name = "ecb-cipher_null", |
| 130 | .cra_priority = 100, |
| 131 | .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, |
| 132 | .cra_blocksize = NULL_BLOCK_SIZE, |
| 133 | .cra_type = &crypto_blkcipher_type, |
| 134 | .cra_ctxsize = 0, |
| 135 | .cra_module = THIS_MODULE, |
Herbert Xu | 3631c65 | 2007-12-13 22:28:59 +0800 | [diff] [blame] | 136 | .cra_u = { .blkcipher = { |
| 137 | .min_keysize = NULL_KEY_SIZE, |
| 138 | .max_keysize = NULL_KEY_SIZE, |
| 139 | .ivsize = NULL_IV_SIZE, |
| 140 | .setkey = null_setkey, |
| 141 | .encrypt = skcipher_null_crypt, |
| 142 | .decrypt = skcipher_null_crypt } } |
Jussi Kivilinna | 70a03bf | 2012-07-11 14:20:00 +0300 | [diff] [blame] | 143 | }, { |
| 144 | .cra_name = "compress_null", |
| 145 | .cra_flags = CRYPTO_ALG_TYPE_COMPRESS, |
| 146 | .cra_blocksize = NULL_BLOCK_SIZE, |
| 147 | .cra_ctxsize = 0, |
| 148 | .cra_module = THIS_MODULE, |
| 149 | .cra_u = { .compress = { |
| 150 | .coa_compress = null_compress, |
| 151 | .coa_decompress = null_compress } } |
| 152 | } }; |
Herbert Xu | 3631c65 | 2007-12-13 22:28:59 +0800 | [diff] [blame] | 153 | |
Kees Cook | 5d26a10 | 2014-11-20 17:05:53 -0800 | [diff] [blame] | 154 | MODULE_ALIAS_CRYPTO("compress_null"); |
| 155 | MODULE_ALIAS_CRYPTO("digest_null"); |
| 156 | MODULE_ALIAS_CRYPTO("cipher_null"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Herbert Xu | 3302346 | 2015-05-21 15:11:09 +0800 | [diff] [blame] | 158 | struct crypto_blkcipher *crypto_get_default_null_skcipher(void) |
| 159 | { |
| 160 | struct crypto_blkcipher *tfm; |
| 161 | |
| 162 | mutex_lock(&crypto_default_null_skcipher_lock); |
| 163 | tfm = crypto_default_null_skcipher; |
| 164 | |
| 165 | if (!tfm) { |
| 166 | tfm = crypto_alloc_blkcipher("ecb(cipher_null)", 0, 0); |
| 167 | if (IS_ERR(tfm)) |
| 168 | goto unlock; |
| 169 | |
| 170 | crypto_default_null_skcipher = tfm; |
| 171 | } |
| 172 | |
| 173 | crypto_default_null_skcipher_refcnt++; |
| 174 | |
| 175 | unlock: |
| 176 | mutex_unlock(&crypto_default_null_skcipher_lock); |
| 177 | |
| 178 | return tfm; |
| 179 | } |
| 180 | EXPORT_SYMBOL_GPL(crypto_get_default_null_skcipher); |
| 181 | |
| 182 | void crypto_put_default_null_skcipher(void) |
| 183 | { |
| 184 | mutex_lock(&crypto_default_null_skcipher_lock); |
| 185 | if (!--crypto_default_null_skcipher_refcnt) { |
| 186 | crypto_free_blkcipher(crypto_default_null_skcipher); |
| 187 | crypto_default_null_skcipher = NULL; |
| 188 | } |
| 189 | mutex_unlock(&crypto_default_null_skcipher_lock); |
| 190 | } |
| 191 | EXPORT_SYMBOL_GPL(crypto_put_default_null_skcipher); |
| 192 | |
Herbert Xu | a012973 | 2016-07-12 13:17:32 +0800 | [diff] [blame] | 193 | struct crypto_skcipher *crypto_get_default_null_skcipher2(void) |
| 194 | { |
| 195 | struct crypto_skcipher *tfm; |
| 196 | |
| 197 | mutex_lock(&crypto_default_null_skcipher_lock); |
| 198 | tfm = crypto_default_null_skcipher2; |
| 199 | |
| 200 | if (!tfm) { |
| 201 | tfm = crypto_alloc_skcipher("ecb(cipher_null)", |
| 202 | 0, CRYPTO_ALG_ASYNC); |
| 203 | if (IS_ERR(tfm)) |
| 204 | goto unlock; |
| 205 | |
| 206 | crypto_default_null_skcipher2 = tfm; |
| 207 | } |
| 208 | |
| 209 | crypto_default_null_skcipher2_refcnt++; |
| 210 | |
| 211 | unlock: |
| 212 | mutex_unlock(&crypto_default_null_skcipher_lock); |
| 213 | |
| 214 | return tfm; |
| 215 | } |
| 216 | EXPORT_SYMBOL_GPL(crypto_get_default_null_skcipher2); |
| 217 | |
| 218 | void crypto_put_default_null_skcipher2(void) |
| 219 | { |
| 220 | mutex_lock(&crypto_default_null_skcipher_lock); |
| 221 | if (!--crypto_default_null_skcipher2_refcnt) { |
| 222 | crypto_free_skcipher(crypto_default_null_skcipher2); |
| 223 | crypto_default_null_skcipher2 = NULL; |
| 224 | } |
| 225 | mutex_unlock(&crypto_default_null_skcipher_lock); |
| 226 | } |
| 227 | EXPORT_SYMBOL_GPL(crypto_put_default_null_skcipher2); |
| 228 | |
Kamalesh Babulal | 3af5b90 | 2008-04-05 21:00:57 +0800 | [diff] [blame] | 229 | static int __init crypto_null_mod_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | { |
| 231 | int ret = 0; |
Richard Hartmann | c9af70f | 2010-02-16 20:31:54 +0800 | [diff] [blame] | 232 | |
Jussi Kivilinna | 70a03bf | 2012-07-11 14:20:00 +0300 | [diff] [blame] | 233 | ret = crypto_register_algs(null_algs, ARRAY_SIZE(null_algs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | if (ret < 0) |
| 235 | goto out; |
| 236 | |
Herbert Xu | d35d245 | 2008-11-08 08:09:56 +0800 | [diff] [blame] | 237 | ret = crypto_register_shash(&digest_null); |
Herbert Xu | 3631c65 | 2007-12-13 22:28:59 +0800 | [diff] [blame] | 238 | if (ret < 0) |
Jussi Kivilinna | 70a03bf | 2012-07-11 14:20:00 +0300 | [diff] [blame] | 239 | goto out_unregister_algs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
Jussi Kivilinna | 70a03bf | 2012-07-11 14:20:00 +0300 | [diff] [blame] | 241 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
Jussi Kivilinna | 70a03bf | 2012-07-11 14:20:00 +0300 | [diff] [blame] | 243 | out_unregister_algs: |
| 244 | crypto_unregister_algs(null_algs, ARRAY_SIZE(null_algs)); |
Richard Hartmann | c9af70f | 2010-02-16 20:31:54 +0800 | [diff] [blame] | 245 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | return ret; |
| 247 | } |
| 248 | |
Kamalesh Babulal | 3af5b90 | 2008-04-05 21:00:57 +0800 | [diff] [blame] | 249 | static void __exit crypto_null_mod_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | { |
Herbert Xu | d35d245 | 2008-11-08 08:09:56 +0800 | [diff] [blame] | 251 | crypto_unregister_shash(&digest_null); |
Jussi Kivilinna | 70a03bf | 2012-07-11 14:20:00 +0300 | [diff] [blame] | 252 | crypto_unregister_algs(null_algs, ARRAY_SIZE(null_algs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Kamalesh Babulal | 3af5b90 | 2008-04-05 21:00:57 +0800 | [diff] [blame] | 255 | module_init(crypto_null_mod_init); |
| 256 | module_exit(crypto_null_mod_fini); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
| 258 | MODULE_LICENSE("GPL"); |
| 259 | MODULE_DESCRIPTION("Null Cryptographic Algorithms"); |