18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Crypto API wrapper for the generic SHA256 code from lib/crypto/sha256.c 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (c) Jean-Luc Cooke <jlcooke@certainkey.com> 68c2ecf20Sopenharmony_ci * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk> 78c2ecf20Sopenharmony_ci * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> 88c2ecf20Sopenharmony_ci * SHA224 Support Copyright 2007 Intel Corporation <jonathan.lynch@intel.com> 98c2ecf20Sopenharmony_ci */ 108c2ecf20Sopenharmony_ci#include <crypto/internal/hash.h> 118c2ecf20Sopenharmony_ci#include <linux/init.h> 128c2ecf20Sopenharmony_ci#include <linux/module.h> 138c2ecf20Sopenharmony_ci#include <linux/mm.h> 148c2ecf20Sopenharmony_ci#include <linux/types.h> 158c2ecf20Sopenharmony_ci#include <crypto/sha.h> 168c2ecf20Sopenharmony_ci#include <crypto/sha256_base.h> 178c2ecf20Sopenharmony_ci#include <asm/byteorder.h> 188c2ecf20Sopenharmony_ci#include <asm/unaligned.h> 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ciconst u8 sha224_zero_message_hash[SHA224_DIGEST_SIZE] = { 218c2ecf20Sopenharmony_ci 0xd1, 0x4a, 0x02, 0x8c, 0x2a, 0x3a, 0x2b, 0xc9, 0x47, 228c2ecf20Sopenharmony_ci 0x61, 0x02, 0xbb, 0x28, 0x82, 0x34, 0xc4, 0x15, 0xa2, 238c2ecf20Sopenharmony_ci 0xb0, 0x1f, 0x82, 0x8e, 0xa6, 0x2a, 0xc5, 0xb3, 0xe4, 248c2ecf20Sopenharmony_ci 0x2f 258c2ecf20Sopenharmony_ci}; 268c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sha224_zero_message_hash); 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ciconst u8 sha256_zero_message_hash[SHA256_DIGEST_SIZE] = { 298c2ecf20Sopenharmony_ci 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 308c2ecf20Sopenharmony_ci 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, 318c2ecf20Sopenharmony_ci 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 328c2ecf20Sopenharmony_ci 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 338c2ecf20Sopenharmony_ci}; 348c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sha256_zero_message_hash); 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_cistatic int crypto_sha256_init(struct shash_desc *desc) 378c2ecf20Sopenharmony_ci{ 388c2ecf20Sopenharmony_ci sha256_init(shash_desc_ctx(desc)); 398c2ecf20Sopenharmony_ci return 0; 408c2ecf20Sopenharmony_ci} 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_cistatic int crypto_sha224_init(struct shash_desc *desc) 438c2ecf20Sopenharmony_ci{ 448c2ecf20Sopenharmony_ci sha224_init(shash_desc_ctx(desc)); 458c2ecf20Sopenharmony_ci return 0; 468c2ecf20Sopenharmony_ci} 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ciint crypto_sha256_update(struct shash_desc *desc, const u8 *data, 498c2ecf20Sopenharmony_ci unsigned int len) 508c2ecf20Sopenharmony_ci{ 518c2ecf20Sopenharmony_ci sha256_update(shash_desc_ctx(desc), data, len); 528c2ecf20Sopenharmony_ci return 0; 538c2ecf20Sopenharmony_ci} 548c2ecf20Sopenharmony_ciEXPORT_SYMBOL(crypto_sha256_update); 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_cistatic int crypto_sha256_final(struct shash_desc *desc, u8 *out) 578c2ecf20Sopenharmony_ci{ 588c2ecf20Sopenharmony_ci if (crypto_shash_digestsize(desc->tfm) == SHA224_DIGEST_SIZE) 598c2ecf20Sopenharmony_ci sha224_final(shash_desc_ctx(desc), out); 608c2ecf20Sopenharmony_ci else 618c2ecf20Sopenharmony_ci sha256_final(shash_desc_ctx(desc), out); 628c2ecf20Sopenharmony_ci return 0; 638c2ecf20Sopenharmony_ci} 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ciint crypto_sha256_finup(struct shash_desc *desc, const u8 *data, 668c2ecf20Sopenharmony_ci unsigned int len, u8 *hash) 678c2ecf20Sopenharmony_ci{ 688c2ecf20Sopenharmony_ci sha256_update(shash_desc_ctx(desc), data, len); 698c2ecf20Sopenharmony_ci return crypto_sha256_final(desc, hash); 708c2ecf20Sopenharmony_ci} 718c2ecf20Sopenharmony_ciEXPORT_SYMBOL(crypto_sha256_finup); 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_cistatic struct shash_alg sha256_algs[2] = { { 748c2ecf20Sopenharmony_ci .digestsize = SHA256_DIGEST_SIZE, 758c2ecf20Sopenharmony_ci .init = crypto_sha256_init, 768c2ecf20Sopenharmony_ci .update = crypto_sha256_update, 778c2ecf20Sopenharmony_ci .final = crypto_sha256_final, 788c2ecf20Sopenharmony_ci .finup = crypto_sha256_finup, 798c2ecf20Sopenharmony_ci .descsize = sizeof(struct sha256_state), 808c2ecf20Sopenharmony_ci .base = { 818c2ecf20Sopenharmony_ci .cra_name = "sha256", 828c2ecf20Sopenharmony_ci .cra_driver_name= "sha256-generic", 838c2ecf20Sopenharmony_ci .cra_priority = 100, 848c2ecf20Sopenharmony_ci .cra_blocksize = SHA256_BLOCK_SIZE, 858c2ecf20Sopenharmony_ci .cra_module = THIS_MODULE, 868c2ecf20Sopenharmony_ci } 878c2ecf20Sopenharmony_ci}, { 888c2ecf20Sopenharmony_ci .digestsize = SHA224_DIGEST_SIZE, 898c2ecf20Sopenharmony_ci .init = crypto_sha224_init, 908c2ecf20Sopenharmony_ci .update = crypto_sha256_update, 918c2ecf20Sopenharmony_ci .final = crypto_sha256_final, 928c2ecf20Sopenharmony_ci .finup = crypto_sha256_finup, 938c2ecf20Sopenharmony_ci .descsize = sizeof(struct sha256_state), 948c2ecf20Sopenharmony_ci .base = { 958c2ecf20Sopenharmony_ci .cra_name = "sha224", 968c2ecf20Sopenharmony_ci .cra_driver_name= "sha224-generic", 978c2ecf20Sopenharmony_ci .cra_priority = 100, 988c2ecf20Sopenharmony_ci .cra_blocksize = SHA224_BLOCK_SIZE, 998c2ecf20Sopenharmony_ci .cra_module = THIS_MODULE, 1008c2ecf20Sopenharmony_ci } 1018c2ecf20Sopenharmony_ci} }; 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_cistatic int __init sha256_generic_mod_init(void) 1048c2ecf20Sopenharmony_ci{ 1058c2ecf20Sopenharmony_ci return crypto_register_shashes(sha256_algs, ARRAY_SIZE(sha256_algs)); 1068c2ecf20Sopenharmony_ci} 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_cistatic void __exit sha256_generic_mod_fini(void) 1098c2ecf20Sopenharmony_ci{ 1108c2ecf20Sopenharmony_ci crypto_unregister_shashes(sha256_algs, ARRAY_SIZE(sha256_algs)); 1118c2ecf20Sopenharmony_ci} 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_cisubsys_initcall(sha256_generic_mod_init); 1148c2ecf20Sopenharmony_cimodule_exit(sha256_generic_mod_fini); 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 1178c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("SHA-224 and SHA-256 Secure Hash Algorithm"); 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("sha224"); 1208c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("sha224-generic"); 1218c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("sha256"); 1228c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("sha256-generic"); 123