1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Cryptographic API.
4 *
5 * Null algorithms, aka Much Ado About Nothing.
6 *
7 * These are needed for IPsec, and may be useful in general for
8 * testing & debugging.
9 *
10 * The null cipher is compliant with RFC2410.
11 *
12 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
13 */
14
15 #include <crypto/null.h>
16 #include <crypto/internal/hash.h>
17 #include <crypto/internal/skcipher.h>
18 #include <crypto/scatterwalk.h>
19 #include <linux/init.h>
20 #include <linux/module.h>
21 #include <linux/string.h>
22
null_init(struct shash_desc * desc)23 static int null_init(struct shash_desc *desc)
24 {
25 return 0;
26 }
27
null_update(struct shash_desc * desc,const u8 * data,unsigned int len)28 static int null_update(struct shash_desc *desc, const u8 *data,
29 unsigned int len)
30 {
31 return 0;
32 }
33
null_final(struct shash_desc * desc,u8 * out)34 static int null_final(struct shash_desc *desc, u8 *out)
35 {
36 return 0;
37 }
38
null_digest(struct shash_desc * desc,const u8 * data,unsigned int len,u8 * out)39 static int null_digest(struct shash_desc *desc, const u8 *data,
40 unsigned int len, u8 *out)
41 {
42 return 0;
43 }
44
null_hash_setkey(struct crypto_shash * tfm,const u8 * key,unsigned int keylen)45 static int null_hash_setkey(struct crypto_shash *tfm, const u8 *key,
46 unsigned int keylen)
47 { return 0; }
48
null_skcipher_setkey(struct crypto_skcipher * tfm,const u8 * key,unsigned int keylen)49 static int null_skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key,
50 unsigned int keylen)
51 { return 0; }
52
null_setkey(struct crypto_tfm * tfm,const u8 * key,unsigned int keylen)53 static int null_setkey(struct crypto_tfm *tfm, const u8 *key,
54 unsigned int keylen)
55 { return 0; }
56
null_crypt(struct crypto_tfm * tfm,u8 * dst,const u8 * src)57 static void null_crypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
58 {
59 memcpy(dst, src, NULL_BLOCK_SIZE);
60 }
61
null_skcipher_crypt(struct skcipher_request * req)62 static int null_skcipher_crypt(struct skcipher_request *req)
63 {
64 if (req->src != req->dst)
65 memcpy_sglist(req->dst, req->src, req->cryptlen);
66 return 0;
67 }
68
69 static struct shash_alg digest_null = {
70 .digestsize = NULL_DIGEST_SIZE,
71 .setkey = null_hash_setkey,
72 .init = null_init,
73 .update = null_update,
74 .finup = null_digest,
75 .digest = null_digest,
76 .final = null_final,
77 .base = {
78 .cra_name = "digest_null",
79 .cra_driver_name = "digest_null-generic",
80 .cra_blocksize = NULL_BLOCK_SIZE,
81 .cra_module = THIS_MODULE,
82 }
83 };
84
85 static struct skcipher_alg skcipher_null = {
86 .base.cra_name = "ecb(cipher_null)",
87 .base.cra_driver_name = "ecb-cipher_null",
88 .base.cra_priority = 100,
89 .base.cra_blocksize = NULL_BLOCK_SIZE,
90 .base.cra_ctxsize = 0,
91 .base.cra_module = THIS_MODULE,
92 .min_keysize = NULL_KEY_SIZE,
93 .max_keysize = NULL_KEY_SIZE,
94 .ivsize = NULL_IV_SIZE,
95 .setkey = null_skcipher_setkey,
96 .encrypt = null_skcipher_crypt,
97 .decrypt = null_skcipher_crypt,
98 };
99
100 static struct crypto_alg cipher_null = {
101 .cra_name = "cipher_null",
102 .cra_driver_name = "cipher_null-generic",
103 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
104 .cra_blocksize = NULL_BLOCK_SIZE,
105 .cra_ctxsize = 0,
106 .cra_module = THIS_MODULE,
107 .cra_u = { .cipher = {
108 .cia_min_keysize = NULL_KEY_SIZE,
109 .cia_max_keysize = NULL_KEY_SIZE,
110 .cia_setkey = null_setkey,
111 .cia_encrypt = null_crypt,
112 .cia_decrypt = null_crypt } }
113 };
114
115 MODULE_ALIAS_CRYPTO("digest_null");
116 MODULE_ALIAS_CRYPTO("cipher_null");
117
crypto_null_mod_init(void)118 static int __init crypto_null_mod_init(void)
119 {
120 int ret = 0;
121
122 ret = crypto_register_alg(&cipher_null);
123 if (ret < 0)
124 goto out;
125
126 ret = crypto_register_shash(&digest_null);
127 if (ret < 0)
128 goto out_unregister_algs;
129
130 ret = crypto_register_skcipher(&skcipher_null);
131 if (ret < 0)
132 goto out_unregister_shash;
133
134 return 0;
135
136 out_unregister_shash:
137 crypto_unregister_shash(&digest_null);
138 out_unregister_algs:
139 crypto_unregister_alg(&cipher_null);
140 out:
141 return ret;
142 }
143
crypto_null_mod_fini(void)144 static void __exit crypto_null_mod_fini(void)
145 {
146 crypto_unregister_alg(&cipher_null);
147 crypto_unregister_shash(&digest_null);
148 crypto_unregister_skcipher(&skcipher_null);
149 }
150
151 module_init(crypto_null_mod_init);
152 module_exit(crypto_null_mod_fini);
153
154 MODULE_LICENSE("GPL");
155 MODULE_DESCRIPTION("Null Cryptographic Algorithms");
156