1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Crypto API wrapper for the generic SHA256 code from lib/crypto/sha256.c
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>
10 #include <crypto/internal/hash.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <crypto/sha.h>
16 #include <crypto/sha256.h>
17 #include <crypto/sha256_base.h>
18 #include <asm/byteorder.h>
19 #include <asm/unaligned.h>
21 const u8 sha224_zero_message_hash[SHA224_DIGEST_SIZE] = {
22 0xd1, 0x4a, 0x02, 0x8c, 0x2a, 0x3a, 0x2b, 0xc9, 0x47,
23 0x61, 0x02, 0xbb, 0x28, 0x82, 0x34, 0xc4, 0x15, 0xa2,
24 0xb0, 0x1f, 0x82, 0x8e, 0xa6, 0x2a, 0xc5, 0xb3, 0xe4,
27 EXPORT_SYMBOL_GPL(sha224_zero_message_hash);
29 const u8 sha256_zero_message_hash[SHA256_DIGEST_SIZE] = {
30 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14,
31 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
32 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
33 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
35 EXPORT_SYMBOL_GPL(sha256_zero_message_hash);
37 static int crypto_sha256_init(struct shash_desc *desc)
39 return sha256_init(shash_desc_ctx(desc));
42 static int crypto_sha224_init(struct shash_desc *desc)
44 return sha224_init(shash_desc_ctx(desc));
47 int crypto_sha256_update(struct shash_desc *desc, const u8 *data,
50 return sha256_update(shash_desc_ctx(desc), data, len);
52 EXPORT_SYMBOL(crypto_sha256_update);
54 static int crypto_sha256_final(struct shash_desc *desc, u8 *out)
56 if (crypto_shash_digestsize(desc->tfm) == SHA224_DIGEST_SIZE)
57 return sha224_final(shash_desc_ctx(desc), out);
59 return sha256_final(shash_desc_ctx(desc), out);
62 int crypto_sha256_finup(struct shash_desc *desc, const u8 *data,
63 unsigned int len, u8 *hash)
65 sha256_update(shash_desc_ctx(desc), data, len);
66 return crypto_sha256_final(desc, hash);
68 EXPORT_SYMBOL(crypto_sha256_finup);
70 static struct shash_alg sha256_algs[2] = { {
71 .digestsize = SHA256_DIGEST_SIZE,
72 .init = crypto_sha256_init,
73 .update = crypto_sha256_update,
74 .final = crypto_sha256_final,
75 .finup = crypto_sha256_finup,
76 .descsize = sizeof(struct sha256_state),
79 .cra_driver_name= "sha256-generic",
81 .cra_blocksize = SHA256_BLOCK_SIZE,
82 .cra_module = THIS_MODULE,
85 .digestsize = SHA224_DIGEST_SIZE,
86 .init = crypto_sha224_init,
87 .update = crypto_sha256_update,
88 .final = crypto_sha256_final,
89 .finup = crypto_sha256_finup,
90 .descsize = sizeof(struct sha256_state),
93 .cra_driver_name= "sha224-generic",
95 .cra_blocksize = SHA224_BLOCK_SIZE,
96 .cra_module = THIS_MODULE,
100 static int __init sha256_generic_mod_init(void)
102 return crypto_register_shashes(sha256_algs, ARRAY_SIZE(sha256_algs));
105 static void __exit sha256_generic_mod_fini(void)
107 crypto_unregister_shashes(sha256_algs, ARRAY_SIZE(sha256_algs));
110 subsys_initcall(sha256_generic_mod_init);
111 module_exit(sha256_generic_mod_fini);
113 MODULE_LICENSE("GPL");
114 MODULE_DESCRIPTION("SHA-224 and SHA-256 Secure Hash Algorithm");
116 MODULE_ALIAS_CRYPTO("sha224");
117 MODULE_ALIAS_CRYPTO("sha224-generic");
118 MODULE_ALIAS_CRYPTO("sha256");
119 MODULE_ALIAS_CRYPTO("sha256-generic");