xref: /openbmc/linux/crypto/sha256_generic.c (revision 08c327f6)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2ad5d2789SSebastian Siewior /*
308c327f6SHans de Goede  * Crypto API wrapper for the generic SHA256 code from lib/crypto/sha256.c
4ad5d2789SSebastian Siewior  *
5ad5d2789SSebastian Siewior  * Copyright (c) Jean-Luc Cooke <jlcooke@certainkey.com>
6ad5d2789SSebastian Siewior  * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
7ad5d2789SSebastian Siewior  * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
8cd12fb90SJonathan Lynch  * SHA224 Support Copyright 2007 Intel Corporation <jonathan.lynch@intel.com>
9ad5d2789SSebastian Siewior  */
1050e109b5SAdrian-Ken Rueegsegger #include <crypto/internal/hash.h>
11ad5d2789SSebastian Siewior #include <linux/init.h>
12ad5d2789SSebastian Siewior #include <linux/module.h>
13ad5d2789SSebastian Siewior #include <linux/mm.h>
14ad5d2789SSebastian Siewior #include <linux/types.h>
155265eeb2SJan Glauber #include <crypto/sha.h>
1608c327f6SHans de Goede #include <crypto/sha256.h>
17a2e5ba4fSArd Biesheuvel #include <crypto/sha256_base.h>
18ad5d2789SSebastian Siewior #include <asm/byteorder.h>
19be34c4efSDavid S. Miller #include <asm/unaligned.h>
20ad5d2789SSebastian Siewior 
210c4c78deSLABBE Corentin const u8 sha224_zero_message_hash[SHA224_DIGEST_SIZE] = {
220c4c78deSLABBE Corentin 	0xd1, 0x4a, 0x02, 0x8c, 0x2a, 0x3a, 0x2b, 0xc9, 0x47,
230c4c78deSLABBE Corentin 	0x61, 0x02, 0xbb, 0x28, 0x82, 0x34, 0xc4, 0x15, 0xa2,
240c4c78deSLABBE Corentin 	0xb0, 0x1f, 0x82, 0x8e, 0xa6, 0x2a, 0xc5, 0xb3, 0xe4,
250c4c78deSLABBE Corentin 	0x2f
260c4c78deSLABBE Corentin };
270c4c78deSLABBE Corentin EXPORT_SYMBOL_GPL(sha224_zero_message_hash);
280c4c78deSLABBE Corentin 
290c4c78deSLABBE Corentin const u8 sha256_zero_message_hash[SHA256_DIGEST_SIZE] = {
300c4c78deSLABBE Corentin 	0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14,
310c4c78deSLABBE Corentin 	0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
320c4c78deSLABBE Corentin 	0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
330c4c78deSLABBE Corentin 	0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
340c4c78deSLABBE Corentin };
350c4c78deSLABBE Corentin EXPORT_SYMBOL_GPL(sha256_zero_message_hash);
360c4c78deSLABBE Corentin 
3708c327f6SHans de Goede static int crypto_sha256_init(struct shash_desc *desc)
38ad5d2789SSebastian Siewior {
3908c327f6SHans de Goede 	return sha256_init(shash_desc_ctx(desc));
40ad5d2789SSebastian Siewior }
41ad5d2789SSebastian Siewior 
4208c327f6SHans de Goede static int crypto_sha224_init(struct shash_desc *desc)
43ad5d2789SSebastian Siewior {
4408c327f6SHans de Goede 	return sha224_init(shash_desc_ctx(desc));
45ad5d2789SSebastian Siewior }
46ad5d2789SSebastian Siewior 
4735d2c9d0STim Chen int crypto_sha256_update(struct shash_desc *desc, const u8 *data,
48ad5d2789SSebastian Siewior 			  unsigned int len)
49ad5d2789SSebastian Siewior {
5008c327f6SHans de Goede 	return sha256_update(shash_desc_ctx(desc), data, len);
51ad5d2789SSebastian Siewior }
5235d2c9d0STim Chen EXPORT_SYMBOL(crypto_sha256_update);
53ad5d2789SSebastian Siewior 
5408c327f6SHans de Goede static int crypto_sha256_final(struct shash_desc *desc, u8 *out)
55ad5d2789SSebastian Siewior {
5608c327f6SHans de Goede 	if (crypto_shash_digestsize(desc->tfm) == SHA224_DIGEST_SIZE)
5708c327f6SHans de Goede 		return sha224_final(shash_desc_ctx(desc), out);
5808c327f6SHans de Goede 	else
5908c327f6SHans de Goede 		return sha256_final(shash_desc_ctx(desc), out);
60ad5d2789SSebastian Siewior }
61ad5d2789SSebastian Siewior 
62a2e5ba4fSArd Biesheuvel int crypto_sha256_finup(struct shash_desc *desc, const u8 *data,
63a2e5ba4fSArd Biesheuvel 			unsigned int len, u8 *hash)
64cd12fb90SJonathan Lynch {
6508c327f6SHans de Goede 	sha256_update(shash_desc_ctx(desc), data, len);
6608c327f6SHans de Goede 	return crypto_sha256_final(desc, hash);
67cd12fb90SJonathan Lynch }
68a2e5ba4fSArd Biesheuvel EXPORT_SYMBOL(crypto_sha256_finup);
699b2fda7bSHerbert Xu 
706aeb49bcSJussi Kivilinna static struct shash_alg sha256_algs[2] = { {
7150e109b5SAdrian-Ken Rueegsegger 	.digestsize	=	SHA256_DIGEST_SIZE,
7208c327f6SHans de Goede 	.init		=	crypto_sha256_init,
7335d2c9d0STim Chen 	.update		=	crypto_sha256_update,
7408c327f6SHans de Goede 	.final		=	crypto_sha256_final,
75a2e5ba4fSArd Biesheuvel 	.finup		=	crypto_sha256_finup,
769b2fda7bSHerbert Xu 	.descsize	=	sizeof(struct sha256_state),
7750e109b5SAdrian-Ken Rueegsegger 	.base		=	{
78ad5d2789SSebastian Siewior 		.cra_name	=	"sha256",
79ad5d2789SSebastian Siewior 		.cra_driver_name=	"sha256-generic",
80b73b7ac0SEric Biggers 		.cra_priority	=	100,
815265eeb2SJan Glauber 		.cra_blocksize	=	SHA256_BLOCK_SIZE,
82ad5d2789SSebastian Siewior 		.cra_module	=	THIS_MODULE,
8350e109b5SAdrian-Ken Rueegsegger 	}
846aeb49bcSJussi Kivilinna }, {
8550e109b5SAdrian-Ken Rueegsegger 	.digestsize	=	SHA224_DIGEST_SIZE,
8608c327f6SHans de Goede 	.init		=	crypto_sha224_init,
8735d2c9d0STim Chen 	.update		=	crypto_sha256_update,
8808c327f6SHans de Goede 	.final		=	crypto_sha256_final,
89a2e5ba4fSArd Biesheuvel 	.finup		=	crypto_sha256_finup,
909b2fda7bSHerbert Xu 	.descsize	=	sizeof(struct sha256_state),
9150e109b5SAdrian-Ken Rueegsegger 	.base		=	{
92cd12fb90SJonathan Lynch 		.cra_name	=	"sha224",
93cd12fb90SJonathan Lynch 		.cra_driver_name=	"sha224-generic",
94b73b7ac0SEric Biggers 		.cra_priority	=	100,
95cd12fb90SJonathan Lynch 		.cra_blocksize	=	SHA224_BLOCK_SIZE,
96cd12fb90SJonathan Lynch 		.cra_module	=	THIS_MODULE,
9750e109b5SAdrian-Ken Rueegsegger 	}
986aeb49bcSJussi Kivilinna } };
99cd12fb90SJonathan Lynch 
1003af5b90bSKamalesh Babulal static int __init sha256_generic_mod_init(void)
101ad5d2789SSebastian Siewior {
1026aeb49bcSJussi Kivilinna 	return crypto_register_shashes(sha256_algs, ARRAY_SIZE(sha256_algs));
103ad5d2789SSebastian Siewior }
104ad5d2789SSebastian Siewior 
1053af5b90bSKamalesh Babulal static void __exit sha256_generic_mod_fini(void)
106ad5d2789SSebastian Siewior {
1076aeb49bcSJussi Kivilinna 	crypto_unregister_shashes(sha256_algs, ARRAY_SIZE(sha256_algs));
108ad5d2789SSebastian Siewior }
109ad5d2789SSebastian Siewior 
110c4741b23SEric Biggers subsys_initcall(sha256_generic_mod_init);
1113af5b90bSKamalesh Babulal module_exit(sha256_generic_mod_fini);
112ad5d2789SSebastian Siewior 
113ad5d2789SSebastian Siewior MODULE_LICENSE("GPL");
114cd12fb90SJonathan Lynch MODULE_DESCRIPTION("SHA-224 and SHA-256 Secure Hash Algorithm");
115ad5d2789SSebastian Siewior 
1165d26a105SKees Cook MODULE_ALIAS_CRYPTO("sha224");
1173e14dcf7SMathias Krause MODULE_ALIAS_CRYPTO("sha224-generic");
1185d26a105SKees Cook MODULE_ALIAS_CRYPTO("sha256");
1193e14dcf7SMathias Krause MODULE_ALIAS_CRYPTO("sha256-generic");
120