162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Common values for SHA-2 algorithms
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#ifndef _CRYPTO_SHA2_H
762306a36Sopenharmony_ci#define _CRYPTO_SHA2_H
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include <linux/types.h>
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#define SHA224_DIGEST_SIZE	28
1262306a36Sopenharmony_ci#define SHA224_BLOCK_SIZE	64
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#define SHA256_DIGEST_SIZE      32
1562306a36Sopenharmony_ci#define SHA256_BLOCK_SIZE       64
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#define SHA384_DIGEST_SIZE      48
1862306a36Sopenharmony_ci#define SHA384_BLOCK_SIZE       128
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci#define SHA512_DIGEST_SIZE      64
2162306a36Sopenharmony_ci#define SHA512_BLOCK_SIZE       128
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci#define SHA224_H0	0xc1059ed8UL
2462306a36Sopenharmony_ci#define SHA224_H1	0x367cd507UL
2562306a36Sopenharmony_ci#define SHA224_H2	0x3070dd17UL
2662306a36Sopenharmony_ci#define SHA224_H3	0xf70e5939UL
2762306a36Sopenharmony_ci#define SHA224_H4	0xffc00b31UL
2862306a36Sopenharmony_ci#define SHA224_H5	0x68581511UL
2962306a36Sopenharmony_ci#define SHA224_H6	0x64f98fa7UL
3062306a36Sopenharmony_ci#define SHA224_H7	0xbefa4fa4UL
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci#define SHA256_H0	0x6a09e667UL
3362306a36Sopenharmony_ci#define SHA256_H1	0xbb67ae85UL
3462306a36Sopenharmony_ci#define SHA256_H2	0x3c6ef372UL
3562306a36Sopenharmony_ci#define SHA256_H3	0xa54ff53aUL
3662306a36Sopenharmony_ci#define SHA256_H4	0x510e527fUL
3762306a36Sopenharmony_ci#define SHA256_H5	0x9b05688cUL
3862306a36Sopenharmony_ci#define SHA256_H6	0x1f83d9abUL
3962306a36Sopenharmony_ci#define SHA256_H7	0x5be0cd19UL
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci#define SHA384_H0	0xcbbb9d5dc1059ed8ULL
4262306a36Sopenharmony_ci#define SHA384_H1	0x629a292a367cd507ULL
4362306a36Sopenharmony_ci#define SHA384_H2	0x9159015a3070dd17ULL
4462306a36Sopenharmony_ci#define SHA384_H3	0x152fecd8f70e5939ULL
4562306a36Sopenharmony_ci#define SHA384_H4	0x67332667ffc00b31ULL
4662306a36Sopenharmony_ci#define SHA384_H5	0x8eb44a8768581511ULL
4762306a36Sopenharmony_ci#define SHA384_H6	0xdb0c2e0d64f98fa7ULL
4862306a36Sopenharmony_ci#define SHA384_H7	0x47b5481dbefa4fa4ULL
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci#define SHA512_H0	0x6a09e667f3bcc908ULL
5162306a36Sopenharmony_ci#define SHA512_H1	0xbb67ae8584caa73bULL
5262306a36Sopenharmony_ci#define SHA512_H2	0x3c6ef372fe94f82bULL
5362306a36Sopenharmony_ci#define SHA512_H3	0xa54ff53a5f1d36f1ULL
5462306a36Sopenharmony_ci#define SHA512_H4	0x510e527fade682d1ULL
5562306a36Sopenharmony_ci#define SHA512_H5	0x9b05688c2b3e6c1fULL
5662306a36Sopenharmony_ci#define SHA512_H6	0x1f83d9abfb41bd6bULL
5762306a36Sopenharmony_ci#define SHA512_H7	0x5be0cd19137e2179ULL
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ciextern const u8 sha224_zero_message_hash[SHA224_DIGEST_SIZE];
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ciextern const u8 sha256_zero_message_hash[SHA256_DIGEST_SIZE];
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ciextern const u8 sha384_zero_message_hash[SHA384_DIGEST_SIZE];
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ciextern const u8 sha512_zero_message_hash[SHA512_DIGEST_SIZE];
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_cistruct sha256_state {
6862306a36Sopenharmony_ci	u32 state[SHA256_DIGEST_SIZE / 4];
6962306a36Sopenharmony_ci	u64 count;
7062306a36Sopenharmony_ci	u8 buf[SHA256_BLOCK_SIZE];
7162306a36Sopenharmony_ci};
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_cistruct sha512_state {
7462306a36Sopenharmony_ci	u64 state[SHA512_DIGEST_SIZE / 8];
7562306a36Sopenharmony_ci	u64 count[2];
7662306a36Sopenharmony_ci	u8 buf[SHA512_BLOCK_SIZE];
7762306a36Sopenharmony_ci};
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_cistruct shash_desc;
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ciextern int crypto_sha256_update(struct shash_desc *desc, const u8 *data,
8262306a36Sopenharmony_ci			      unsigned int len);
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ciextern int crypto_sha256_finup(struct shash_desc *desc, const u8 *data,
8562306a36Sopenharmony_ci			       unsigned int len, u8 *hash);
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ciextern int crypto_sha512_update(struct shash_desc *desc, const u8 *data,
8862306a36Sopenharmony_ci			      unsigned int len);
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ciextern int crypto_sha512_finup(struct shash_desc *desc, const u8 *data,
9162306a36Sopenharmony_ci			       unsigned int len, u8 *hash);
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci/*
9462306a36Sopenharmony_ci * Stand-alone implementation of the SHA256 algorithm. It is designed to
9562306a36Sopenharmony_ci * have as little dependencies as possible so it can be used in the
9662306a36Sopenharmony_ci * kexec_file purgatory. In other cases you should generally use the
9762306a36Sopenharmony_ci * hash APIs from include/crypto/hash.h. Especially when hashing large
9862306a36Sopenharmony_ci * amounts of data as those APIs may be hw-accelerated.
9962306a36Sopenharmony_ci *
10062306a36Sopenharmony_ci * For details see lib/crypto/sha256.c
10162306a36Sopenharmony_ci */
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_cistatic inline void sha256_init(struct sha256_state *sctx)
10462306a36Sopenharmony_ci{
10562306a36Sopenharmony_ci	sctx->state[0] = SHA256_H0;
10662306a36Sopenharmony_ci	sctx->state[1] = SHA256_H1;
10762306a36Sopenharmony_ci	sctx->state[2] = SHA256_H2;
10862306a36Sopenharmony_ci	sctx->state[3] = SHA256_H3;
10962306a36Sopenharmony_ci	sctx->state[4] = SHA256_H4;
11062306a36Sopenharmony_ci	sctx->state[5] = SHA256_H5;
11162306a36Sopenharmony_ci	sctx->state[6] = SHA256_H6;
11262306a36Sopenharmony_ci	sctx->state[7] = SHA256_H7;
11362306a36Sopenharmony_ci	sctx->count = 0;
11462306a36Sopenharmony_ci}
11562306a36Sopenharmony_civoid sha256_update(struct sha256_state *sctx, const u8 *data, unsigned int len);
11662306a36Sopenharmony_civoid sha256_final(struct sha256_state *sctx, u8 *out);
11762306a36Sopenharmony_civoid sha256(const u8 *data, unsigned int len, u8 *out);
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_cistatic inline void sha224_init(struct sha256_state *sctx)
12062306a36Sopenharmony_ci{
12162306a36Sopenharmony_ci	sctx->state[0] = SHA224_H0;
12262306a36Sopenharmony_ci	sctx->state[1] = SHA224_H1;
12362306a36Sopenharmony_ci	sctx->state[2] = SHA224_H2;
12462306a36Sopenharmony_ci	sctx->state[3] = SHA224_H3;
12562306a36Sopenharmony_ci	sctx->state[4] = SHA224_H4;
12662306a36Sopenharmony_ci	sctx->state[5] = SHA224_H5;
12762306a36Sopenharmony_ci	sctx->state[6] = SHA224_H6;
12862306a36Sopenharmony_ci	sctx->state[7] = SHA224_H7;
12962306a36Sopenharmony_ci	sctx->count = 0;
13062306a36Sopenharmony_ci}
13162306a36Sopenharmony_ci/* Simply use sha256_update as it is equivalent to sha224_update. */
13262306a36Sopenharmony_civoid sha224_final(struct sha256_state *sctx, u8 *out);
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci#endif /* _CRYPTO_SHA2_H */
135