18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Common values for SHA algorithms 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci#ifndef _CRYPTO_SHA_H 78c2ecf20Sopenharmony_ci#define _CRYPTO_SHA_H 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#include <linux/types.h> 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#define SHA1_DIGEST_SIZE 20 128c2ecf20Sopenharmony_ci#define SHA1_BLOCK_SIZE 64 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#define SHA224_DIGEST_SIZE 28 158c2ecf20Sopenharmony_ci#define SHA224_BLOCK_SIZE 64 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#define SHA256_DIGEST_SIZE 32 188c2ecf20Sopenharmony_ci#define SHA256_BLOCK_SIZE 64 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci#define SHA384_DIGEST_SIZE 48 218c2ecf20Sopenharmony_ci#define SHA384_BLOCK_SIZE 128 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci#define SHA512_DIGEST_SIZE 64 248c2ecf20Sopenharmony_ci#define SHA512_BLOCK_SIZE 128 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci#define SHA1_H0 0x67452301UL 278c2ecf20Sopenharmony_ci#define SHA1_H1 0xefcdab89UL 288c2ecf20Sopenharmony_ci#define SHA1_H2 0x98badcfeUL 298c2ecf20Sopenharmony_ci#define SHA1_H3 0x10325476UL 308c2ecf20Sopenharmony_ci#define SHA1_H4 0xc3d2e1f0UL 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci#define SHA224_H0 0xc1059ed8UL 338c2ecf20Sopenharmony_ci#define SHA224_H1 0x367cd507UL 348c2ecf20Sopenharmony_ci#define SHA224_H2 0x3070dd17UL 358c2ecf20Sopenharmony_ci#define SHA224_H3 0xf70e5939UL 368c2ecf20Sopenharmony_ci#define SHA224_H4 0xffc00b31UL 378c2ecf20Sopenharmony_ci#define SHA224_H5 0x68581511UL 388c2ecf20Sopenharmony_ci#define SHA224_H6 0x64f98fa7UL 398c2ecf20Sopenharmony_ci#define SHA224_H7 0xbefa4fa4UL 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci#define SHA256_H0 0x6a09e667UL 428c2ecf20Sopenharmony_ci#define SHA256_H1 0xbb67ae85UL 438c2ecf20Sopenharmony_ci#define SHA256_H2 0x3c6ef372UL 448c2ecf20Sopenharmony_ci#define SHA256_H3 0xa54ff53aUL 458c2ecf20Sopenharmony_ci#define SHA256_H4 0x510e527fUL 468c2ecf20Sopenharmony_ci#define SHA256_H5 0x9b05688cUL 478c2ecf20Sopenharmony_ci#define SHA256_H6 0x1f83d9abUL 488c2ecf20Sopenharmony_ci#define SHA256_H7 0x5be0cd19UL 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci#define SHA384_H0 0xcbbb9d5dc1059ed8ULL 518c2ecf20Sopenharmony_ci#define SHA384_H1 0x629a292a367cd507ULL 528c2ecf20Sopenharmony_ci#define SHA384_H2 0x9159015a3070dd17ULL 538c2ecf20Sopenharmony_ci#define SHA384_H3 0x152fecd8f70e5939ULL 548c2ecf20Sopenharmony_ci#define SHA384_H4 0x67332667ffc00b31ULL 558c2ecf20Sopenharmony_ci#define SHA384_H5 0x8eb44a8768581511ULL 568c2ecf20Sopenharmony_ci#define SHA384_H6 0xdb0c2e0d64f98fa7ULL 578c2ecf20Sopenharmony_ci#define SHA384_H7 0x47b5481dbefa4fa4ULL 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci#define SHA512_H0 0x6a09e667f3bcc908ULL 608c2ecf20Sopenharmony_ci#define SHA512_H1 0xbb67ae8584caa73bULL 618c2ecf20Sopenharmony_ci#define SHA512_H2 0x3c6ef372fe94f82bULL 628c2ecf20Sopenharmony_ci#define SHA512_H3 0xa54ff53a5f1d36f1ULL 638c2ecf20Sopenharmony_ci#define SHA512_H4 0x510e527fade682d1ULL 648c2ecf20Sopenharmony_ci#define SHA512_H5 0x9b05688c2b3e6c1fULL 658c2ecf20Sopenharmony_ci#define SHA512_H6 0x1f83d9abfb41bd6bULL 668c2ecf20Sopenharmony_ci#define SHA512_H7 0x5be0cd19137e2179ULL 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ciextern const u8 sha1_zero_message_hash[SHA1_DIGEST_SIZE]; 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ciextern const u8 sha224_zero_message_hash[SHA224_DIGEST_SIZE]; 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ciextern const u8 sha256_zero_message_hash[SHA256_DIGEST_SIZE]; 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ciextern const u8 sha384_zero_message_hash[SHA384_DIGEST_SIZE]; 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ciextern const u8 sha512_zero_message_hash[SHA512_DIGEST_SIZE]; 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_cistruct sha1_state { 798c2ecf20Sopenharmony_ci u32 state[SHA1_DIGEST_SIZE / 4]; 808c2ecf20Sopenharmony_ci u64 count; 818c2ecf20Sopenharmony_ci u8 buffer[SHA1_BLOCK_SIZE]; 828c2ecf20Sopenharmony_ci}; 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_cistruct sha256_state { 858c2ecf20Sopenharmony_ci u32 state[SHA256_DIGEST_SIZE / 4]; 868c2ecf20Sopenharmony_ci u64 count; 878c2ecf20Sopenharmony_ci u8 buf[SHA256_BLOCK_SIZE]; 888c2ecf20Sopenharmony_ci}; 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_cistruct sha512_state { 918c2ecf20Sopenharmony_ci u64 state[SHA512_DIGEST_SIZE / 8]; 928c2ecf20Sopenharmony_ci u64 count[2]; 938c2ecf20Sopenharmony_ci u8 buf[SHA512_BLOCK_SIZE]; 948c2ecf20Sopenharmony_ci}; 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_cistruct shash_desc; 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ciextern int crypto_sha1_update(struct shash_desc *desc, const u8 *data, 998c2ecf20Sopenharmony_ci unsigned int len); 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ciextern int crypto_sha1_finup(struct shash_desc *desc, const u8 *data, 1028c2ecf20Sopenharmony_ci unsigned int len, u8 *hash); 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ciextern int crypto_sha256_update(struct shash_desc *desc, const u8 *data, 1058c2ecf20Sopenharmony_ci unsigned int len); 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ciextern int crypto_sha256_finup(struct shash_desc *desc, const u8 *data, 1088c2ecf20Sopenharmony_ci unsigned int len, u8 *hash); 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ciextern int crypto_sha512_update(struct shash_desc *desc, const u8 *data, 1118c2ecf20Sopenharmony_ci unsigned int len); 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ciextern int crypto_sha512_finup(struct shash_desc *desc, const u8 *data, 1148c2ecf20Sopenharmony_ci unsigned int len, u8 *hash); 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci/* 1178c2ecf20Sopenharmony_ci * An implementation of SHA-1's compression function. Don't use in new code! 1188c2ecf20Sopenharmony_ci * You shouldn't be using SHA-1, and even if you *have* to use SHA-1, this isn't 1198c2ecf20Sopenharmony_ci * the correct way to hash something with SHA-1 (use crypto_shash instead). 1208c2ecf20Sopenharmony_ci */ 1218c2ecf20Sopenharmony_ci#define SHA1_DIGEST_WORDS (SHA1_DIGEST_SIZE / 4) 1228c2ecf20Sopenharmony_ci#define SHA1_WORKSPACE_WORDS 16 1238c2ecf20Sopenharmony_civoid sha1_init(__u32 *buf); 1248c2ecf20Sopenharmony_civoid sha1_transform(__u32 *digest, const char *data, __u32 *W); 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci/* 1278c2ecf20Sopenharmony_ci * Stand-alone implementation of the SHA256 algorithm. It is designed to 1288c2ecf20Sopenharmony_ci * have as little dependencies as possible so it can be used in the 1298c2ecf20Sopenharmony_ci * kexec_file purgatory. In other cases you should generally use the 1308c2ecf20Sopenharmony_ci * hash APIs from include/crypto/hash.h. Especially when hashing large 1318c2ecf20Sopenharmony_ci * amounts of data as those APIs may be hw-accelerated. 1328c2ecf20Sopenharmony_ci * 1338c2ecf20Sopenharmony_ci * For details see lib/crypto/sha256.c 1348c2ecf20Sopenharmony_ci */ 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_cistatic inline void sha256_init(struct sha256_state *sctx) 1378c2ecf20Sopenharmony_ci{ 1388c2ecf20Sopenharmony_ci sctx->state[0] = SHA256_H0; 1398c2ecf20Sopenharmony_ci sctx->state[1] = SHA256_H1; 1408c2ecf20Sopenharmony_ci sctx->state[2] = SHA256_H2; 1418c2ecf20Sopenharmony_ci sctx->state[3] = SHA256_H3; 1428c2ecf20Sopenharmony_ci sctx->state[4] = SHA256_H4; 1438c2ecf20Sopenharmony_ci sctx->state[5] = SHA256_H5; 1448c2ecf20Sopenharmony_ci sctx->state[6] = SHA256_H6; 1458c2ecf20Sopenharmony_ci sctx->state[7] = SHA256_H7; 1468c2ecf20Sopenharmony_ci sctx->count = 0; 1478c2ecf20Sopenharmony_ci} 1488c2ecf20Sopenharmony_civoid sha256_update(struct sha256_state *sctx, const u8 *data, unsigned int len); 1498c2ecf20Sopenharmony_civoid sha256_final(struct sha256_state *sctx, u8 *out); 1508c2ecf20Sopenharmony_civoid sha256(const u8 *data, unsigned int len, u8 *out); 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_cistatic inline void sha224_init(struct sha256_state *sctx) 1538c2ecf20Sopenharmony_ci{ 1548c2ecf20Sopenharmony_ci sctx->state[0] = SHA224_H0; 1558c2ecf20Sopenharmony_ci sctx->state[1] = SHA224_H1; 1568c2ecf20Sopenharmony_ci sctx->state[2] = SHA224_H2; 1578c2ecf20Sopenharmony_ci sctx->state[3] = SHA224_H3; 1588c2ecf20Sopenharmony_ci sctx->state[4] = SHA224_H4; 1598c2ecf20Sopenharmony_ci sctx->state[5] = SHA224_H5; 1608c2ecf20Sopenharmony_ci sctx->state[6] = SHA224_H6; 1618c2ecf20Sopenharmony_ci sctx->state[7] = SHA224_H7; 1628c2ecf20Sopenharmony_ci sctx->count = 0; 1638c2ecf20Sopenharmony_ci} 1648c2ecf20Sopenharmony_civoid sha224_update(struct sha256_state *sctx, const u8 *data, unsigned int len); 1658c2ecf20Sopenharmony_civoid sha224_final(struct sha256_state *sctx, u8 *out); 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci#endif 168