xref: /linux/crypto/sha256.c (revision e0cd3716910385ba1ccbd433c860516cf806fc71)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Crypto API support for SHA-224, SHA-256, HMAC-SHA224, and HMAC-SHA256
4  *
5  * Copyright (c) Jean-Luc Cooke <jlcooke@certainkey.com>
6  * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
7  * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
8  * SHA224 Support Copyright 2007 Intel Corporation <jonathan.lynch@intel.com>
9  * Copyright 2025 Google LLC
10  */
11 #include <crypto/internal/hash.h>
12 #include <crypto/sha2.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 
16 /* SHA-224 */
17 
18 const u8 sha224_zero_message_hash[SHA224_DIGEST_SIZE] = {
19 	0xd1, 0x4a, 0x02, 0x8c, 0x2a, 0x3a, 0x2b, 0xc9, 0x47,
20 	0x61, 0x02, 0xbb, 0x28, 0x82, 0x34, 0xc4, 0x15, 0xa2,
21 	0xb0, 0x1f, 0x82, 0x8e, 0xa6, 0x2a, 0xc5, 0xb3, 0xe4,
22 	0x2f
23 };
24 EXPORT_SYMBOL_GPL(sha224_zero_message_hash);
25 
26 #define SHA224_CTX(desc) ((struct sha224_ctx *)shash_desc_ctx(desc))
27 
28 static int crypto_sha224_init(struct shash_desc *desc)
29 {
30 	sha224_init(SHA224_CTX(desc));
31 	return 0;
32 }
33 
34 static int crypto_sha224_update(struct shash_desc *desc,
35 				const u8 *data, unsigned int len)
36 {
37 	sha224_update(SHA224_CTX(desc), data, len);
38 	return 0;
39 }
40 
41 static int crypto_sha224_final(struct shash_desc *desc, u8 *out)
42 {
43 	sha224_final(SHA224_CTX(desc), out);
44 	return 0;
45 }
46 
47 static int crypto_sha224_digest(struct shash_desc *desc,
48 				const u8 *data, unsigned int len, u8 *out)
49 {
50 	sha224(data, len, out);
51 	return 0;
52 }
53 
54 /* SHA-256 */
55 
56 const u8 sha256_zero_message_hash[SHA256_DIGEST_SIZE] = {
57 	0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14,
58 	0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
59 	0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
60 	0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
61 };
62 EXPORT_SYMBOL_GPL(sha256_zero_message_hash);
63 
64 #define SHA256_CTX(desc) ((struct sha256_ctx *)shash_desc_ctx(desc))
65 
66 static int crypto_sha256_init(struct shash_desc *desc)
67 {
68 	sha256_init(SHA256_CTX(desc));
69 	return 0;
70 }
71 
72 static int crypto_sha256_update(struct shash_desc *desc,
73 				const u8 *data, unsigned int len)
74 {
75 	sha256_update(SHA256_CTX(desc), data, len);
76 	return 0;
77 }
78 
79 static int crypto_sha256_final(struct shash_desc *desc, u8 *out)
80 {
81 	sha256_final(SHA256_CTX(desc), out);
82 	return 0;
83 }
84 
85 static int crypto_sha256_digest(struct shash_desc *desc,
86 				const u8 *data, unsigned int len, u8 *out)
87 {
88 	sha256(data, len, out);
89 	return 0;
90 }
91 
92 /* HMAC-SHA224 */
93 
94 #define HMAC_SHA224_KEY(tfm) ((struct hmac_sha224_key *)crypto_shash_ctx(tfm))
95 #define HMAC_SHA224_CTX(desc) ((struct hmac_sha224_ctx *)shash_desc_ctx(desc))
96 
97 static int crypto_hmac_sha224_setkey(struct crypto_shash *tfm,
98 				     const u8 *raw_key, unsigned int keylen)
99 {
100 	hmac_sha224_preparekey(HMAC_SHA224_KEY(tfm), raw_key, keylen);
101 	return 0;
102 }
103 
104 static int crypto_hmac_sha224_init(struct shash_desc *desc)
105 {
106 	hmac_sha224_init(HMAC_SHA224_CTX(desc), HMAC_SHA224_KEY(desc->tfm));
107 	return 0;
108 }
109 
110 static int crypto_hmac_sha224_update(struct shash_desc *desc,
111 				     const u8 *data, unsigned int len)
112 {
113 	hmac_sha224_update(HMAC_SHA224_CTX(desc), data, len);
114 	return 0;
115 }
116 
117 static int crypto_hmac_sha224_final(struct shash_desc *desc, u8 *out)
118 {
119 	hmac_sha224_final(HMAC_SHA224_CTX(desc), out);
120 	return 0;
121 }
122 
123 static int crypto_hmac_sha224_digest(struct shash_desc *desc,
124 				     const u8 *data, unsigned int len,
125 				     u8 *out)
126 {
127 	hmac_sha224(HMAC_SHA224_KEY(desc->tfm), data, len, out);
128 	return 0;
129 }
130 
131 /* HMAC-SHA256 */
132 
133 #define HMAC_SHA256_KEY(tfm) ((struct hmac_sha256_key *)crypto_shash_ctx(tfm))
134 #define HMAC_SHA256_CTX(desc) ((struct hmac_sha256_ctx *)shash_desc_ctx(desc))
135 
136 static int crypto_hmac_sha256_setkey(struct crypto_shash *tfm,
137 				     const u8 *raw_key, unsigned int keylen)
138 {
139 	hmac_sha256_preparekey(HMAC_SHA256_KEY(tfm), raw_key, keylen);
140 	return 0;
141 }
142 
143 static int crypto_hmac_sha256_init(struct shash_desc *desc)
144 {
145 	hmac_sha256_init(HMAC_SHA256_CTX(desc), HMAC_SHA256_KEY(desc->tfm));
146 	return 0;
147 }
148 
149 static int crypto_hmac_sha256_update(struct shash_desc *desc,
150 				     const u8 *data, unsigned int len)
151 {
152 	hmac_sha256_update(HMAC_SHA256_CTX(desc), data, len);
153 	return 0;
154 }
155 
156 static int crypto_hmac_sha256_final(struct shash_desc *desc, u8 *out)
157 {
158 	hmac_sha256_final(HMAC_SHA256_CTX(desc), out);
159 	return 0;
160 }
161 
162 static int crypto_hmac_sha256_digest(struct shash_desc *desc,
163 				     const u8 *data, unsigned int len,
164 				     u8 *out)
165 {
166 	hmac_sha256(HMAC_SHA256_KEY(desc->tfm), data, len, out);
167 	return 0;
168 }
169 
170 /* Algorithm definitions */
171 
172 static struct shash_alg algs[] = {
173 	{
174 		.base.cra_name		= "sha224",
175 		.base.cra_driver_name	= "sha224-lib",
176 		.base.cra_priority	= 300,
177 		.base.cra_blocksize	= SHA224_BLOCK_SIZE,
178 		.base.cra_module	= THIS_MODULE,
179 		.digestsize		= SHA224_DIGEST_SIZE,
180 		.init			= crypto_sha224_init,
181 		.update			= crypto_sha224_update,
182 		.final			= crypto_sha224_final,
183 		.digest			= crypto_sha224_digest,
184 		.descsize		= sizeof(struct sha224_ctx),
185 	},
186 	{
187 		.base.cra_name		= "sha256",
188 		.base.cra_driver_name	= "sha256-lib",
189 		.base.cra_priority	= 300,
190 		.base.cra_blocksize	= SHA256_BLOCK_SIZE,
191 		.base.cra_module	= THIS_MODULE,
192 		.digestsize		= SHA256_DIGEST_SIZE,
193 		.init			= crypto_sha256_init,
194 		.update			= crypto_sha256_update,
195 		.final			= crypto_sha256_final,
196 		.digest			= crypto_sha256_digest,
197 		.descsize		= sizeof(struct sha256_ctx),
198 	},
199 	{
200 		.base.cra_name		= "hmac(sha224)",
201 		.base.cra_driver_name	= "hmac-sha224-lib",
202 		.base.cra_priority	= 300,
203 		.base.cra_blocksize	= SHA224_BLOCK_SIZE,
204 		.base.cra_ctxsize	= sizeof(struct hmac_sha224_key),
205 		.base.cra_module	= THIS_MODULE,
206 		.digestsize		= SHA224_DIGEST_SIZE,
207 		.setkey			= crypto_hmac_sha224_setkey,
208 		.init			= crypto_hmac_sha224_init,
209 		.update			= crypto_hmac_sha224_update,
210 		.final			= crypto_hmac_sha224_final,
211 		.digest			= crypto_hmac_sha224_digest,
212 		.descsize		= sizeof(struct hmac_sha224_ctx),
213 	},
214 	{
215 		.base.cra_name		= "hmac(sha256)",
216 		.base.cra_driver_name	= "hmac-sha256-lib",
217 		.base.cra_priority	= 300,
218 		.base.cra_blocksize	= SHA256_BLOCK_SIZE,
219 		.base.cra_ctxsize	= sizeof(struct hmac_sha256_key),
220 		.base.cra_module	= THIS_MODULE,
221 		.digestsize		= SHA256_DIGEST_SIZE,
222 		.setkey			= crypto_hmac_sha256_setkey,
223 		.init			= crypto_hmac_sha256_init,
224 		.update			= crypto_hmac_sha256_update,
225 		.final			= crypto_hmac_sha256_final,
226 		.digest			= crypto_hmac_sha256_digest,
227 		.descsize		= sizeof(struct hmac_sha256_ctx),
228 	},
229 };
230 
231 static int __init crypto_sha256_mod_init(void)
232 {
233 	return crypto_register_shashes(algs, ARRAY_SIZE(algs));
234 }
235 module_init(crypto_sha256_mod_init);
236 
237 static void __exit crypto_sha256_mod_exit(void)
238 {
239 	crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
240 }
241 module_exit(crypto_sha256_mod_exit);
242 
243 MODULE_LICENSE("GPL");
244 MODULE_DESCRIPTION("Crypto API support for SHA-224, SHA-256, HMAC-SHA224, and HMAC-SHA256");
245 
246 MODULE_ALIAS_CRYPTO("sha224");
247 MODULE_ALIAS_CRYPTO("sha224-lib");
248 MODULE_ALIAS_CRYPTO("sha256");
249 MODULE_ALIAS_CRYPTO("sha256-lib");
250 MODULE_ALIAS_CRYPTO("hmac(sha224)");
251 MODULE_ALIAS_CRYPTO("hmac-sha224-lib");
252 MODULE_ALIAS_CRYPTO("hmac(sha256)");
253 MODULE_ALIAS_CRYPTO("hmac-sha256-lib");
254