^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Crypto API wrapper for the generic SHA256 code from lib/crypto/sha256.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) Jean-Luc Cooke <jlcooke@certainkey.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * SHA224 Support Copyright 2007 Intel Corporation <jonathan.lynch@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <crypto/internal/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <crypto/sha.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <crypto/sha256_base.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <asm/byteorder.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) const u8 sha224_zero_message_hash[SHA224_DIGEST_SIZE] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) 0xd1, 0x4a, 0x02, 0x8c, 0x2a, 0x3a, 0x2b, 0xc9, 0x47,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) 0x61, 0x02, 0xbb, 0x28, 0x82, 0x34, 0xc4, 0x15, 0xa2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) 0xb0, 0x1f, 0x82, 0x8e, 0xa6, 0x2a, 0xc5, 0xb3, 0xe4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) 0x2f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) EXPORT_SYMBOL_GPL(sha224_zero_message_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) const u8 sha256_zero_message_hash[SHA256_DIGEST_SIZE] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) EXPORT_SYMBOL_GPL(sha256_zero_message_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static int crypto_sha256_init(struct shash_desc *desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) sha256_init(shash_desc_ctx(desc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static int crypto_sha224_init(struct shash_desc *desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) sha224_init(shash_desc_ctx(desc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) int crypto_sha256_update(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) sha256_update(shash_desc_ctx(desc), data, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) EXPORT_SYMBOL(crypto_sha256_update);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static int crypto_sha256_final(struct shash_desc *desc, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (crypto_shash_digestsize(desc->tfm) == SHA224_DIGEST_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) sha224_final(shash_desc_ctx(desc), out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) sha256_final(shash_desc_ctx(desc), out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) int crypto_sha256_finup(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) unsigned int len, u8 *hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) sha256_update(shash_desc_ctx(desc), data, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return crypto_sha256_final(desc, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) EXPORT_SYMBOL(crypto_sha256_finup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) static struct shash_alg sha256_algs[2] = { {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) .digestsize = SHA256_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) .init = crypto_sha256_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) .update = crypto_sha256_update,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) .final = crypto_sha256_final,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) .finup = crypto_sha256_finup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) .descsize = sizeof(struct sha256_state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) .base = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) .cra_name = "sha256",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) .cra_driver_name= "sha256-generic",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) .cra_priority = 100,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) .cra_blocksize = SHA256_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) .cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) .digestsize = SHA224_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) .init = crypto_sha224_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) .update = crypto_sha256_update,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) .final = crypto_sha256_final,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) .finup = crypto_sha256_finup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) .descsize = sizeof(struct sha256_state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) .base = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) .cra_name = "sha224",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) .cra_driver_name= "sha224-generic",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) .cra_priority = 100,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) .cra_blocksize = SHA224_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) .cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) } };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static int __init sha256_generic_mod_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return crypto_register_shashes(sha256_algs, ARRAY_SIZE(sha256_algs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static void __exit sha256_generic_mod_fini(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) crypto_unregister_shashes(sha256_algs, ARRAY_SIZE(sha256_algs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) subsys_initcall(sha256_generic_mod_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) module_exit(sha256_generic_mod_fini);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) MODULE_DESCRIPTION("SHA-224 and SHA-256 Secure Hash Algorithm");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) MODULE_ALIAS_CRYPTO("sha224");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) MODULE_ALIAS_CRYPTO("sha224-generic");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) MODULE_ALIAS_CRYPTO("sha256");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) MODULE_ALIAS_CRYPTO("sha256-generic");