^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Common values for SHA algorithms
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #ifndef _CRYPTO_SHA_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #define _CRYPTO_SHA_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #define SHA1_DIGEST_SIZE 20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #define SHA1_BLOCK_SIZE 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #define SHA224_DIGEST_SIZE 28
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define SHA224_BLOCK_SIZE 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define SHA256_DIGEST_SIZE 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define SHA256_BLOCK_SIZE 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define SHA384_DIGEST_SIZE 48
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define SHA384_BLOCK_SIZE 128
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define SHA512_DIGEST_SIZE 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define SHA512_BLOCK_SIZE 128
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define SHA1_H0 0x67452301UL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define SHA1_H1 0xefcdab89UL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define SHA1_H2 0x98badcfeUL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define SHA1_H3 0x10325476UL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define SHA1_H4 0xc3d2e1f0UL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define SHA224_H0 0xc1059ed8UL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define SHA224_H1 0x367cd507UL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define SHA224_H2 0x3070dd17UL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #define SHA224_H3 0xf70e5939UL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #define SHA224_H4 0xffc00b31UL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #define SHA224_H5 0x68581511UL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #define SHA224_H6 0x64f98fa7UL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #define SHA224_H7 0xbefa4fa4UL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #define SHA256_H0 0x6a09e667UL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #define SHA256_H1 0xbb67ae85UL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #define SHA256_H2 0x3c6ef372UL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #define SHA256_H3 0xa54ff53aUL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #define SHA256_H4 0x510e527fUL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #define SHA256_H5 0x9b05688cUL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #define SHA256_H6 0x1f83d9abUL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #define SHA256_H7 0x5be0cd19UL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #define SHA384_H0 0xcbbb9d5dc1059ed8ULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #define SHA384_H1 0x629a292a367cd507ULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #define SHA384_H2 0x9159015a3070dd17ULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #define SHA384_H3 0x152fecd8f70e5939ULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #define SHA384_H4 0x67332667ffc00b31ULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #define SHA384_H5 0x8eb44a8768581511ULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #define SHA384_H6 0xdb0c2e0d64f98fa7ULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #define SHA384_H7 0x47b5481dbefa4fa4ULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #define SHA512_H0 0x6a09e667f3bcc908ULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #define SHA512_H1 0xbb67ae8584caa73bULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #define SHA512_H2 0x3c6ef372fe94f82bULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #define SHA512_H3 0xa54ff53a5f1d36f1ULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) #define SHA512_H4 0x510e527fade682d1ULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #define SHA512_H5 0x9b05688c2b3e6c1fULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) #define SHA512_H6 0x1f83d9abfb41bd6bULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #define SHA512_H7 0x5be0cd19137e2179ULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) extern const u8 sha1_zero_message_hash[SHA1_DIGEST_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) extern const u8 sha224_zero_message_hash[SHA224_DIGEST_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) extern const u8 sha256_zero_message_hash[SHA256_DIGEST_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) extern const u8 sha384_zero_message_hash[SHA384_DIGEST_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) extern const u8 sha512_zero_message_hash[SHA512_DIGEST_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) struct sha1_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) u32 state[SHA1_DIGEST_SIZE / 4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) u64 count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) u8 buffer[SHA1_BLOCK_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) struct sha256_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) u32 state[SHA256_DIGEST_SIZE / 4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) u64 count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) u8 buf[SHA256_BLOCK_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct sha512_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) u64 state[SHA512_DIGEST_SIZE / 8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) u64 count[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) u8 buf[SHA512_BLOCK_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) struct shash_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) extern int crypto_sha1_update(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) unsigned int len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) extern int crypto_sha1_finup(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) unsigned int len, u8 *hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) extern int crypto_sha256_update(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) unsigned int len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) extern int crypto_sha256_finup(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) unsigned int len, u8 *hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) extern int crypto_sha512_update(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) unsigned int len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) extern int crypto_sha512_finup(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) unsigned int len, u8 *hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * An implementation of SHA-1's compression function. Don't use in new code!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * You shouldn't be using SHA-1, and even if you *have* to use SHA-1, this isn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * the correct way to hash something with SHA-1 (use crypto_shash instead).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) #define SHA1_DIGEST_WORDS (SHA1_DIGEST_SIZE / 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) #define SHA1_WORKSPACE_WORDS 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) void sha1_init(__u32 *buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) void sha1_transform(__u32 *digest, const char *data, __u32 *W);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * Stand-alone implementation of the SHA256 algorithm. It is designed to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * have as little dependencies as possible so it can be used in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * kexec_file purgatory. In other cases you should generally use the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * hash APIs from include/crypto/hash.h. Especially when hashing large
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * amounts of data as those APIs may be hw-accelerated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * For details see lib/crypto/sha256.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static inline void sha256_init(struct sha256_state *sctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) sctx->state[0] = SHA256_H0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) sctx->state[1] = SHA256_H1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) sctx->state[2] = SHA256_H2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) sctx->state[3] = SHA256_H3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) sctx->state[4] = SHA256_H4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) sctx->state[5] = SHA256_H5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) sctx->state[6] = SHA256_H6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) sctx->state[7] = SHA256_H7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) sctx->count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) void sha256_update(struct sha256_state *sctx, const u8 *data, unsigned int len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) void sha256_final(struct sha256_state *sctx, u8 *out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) void sha256(const u8 *data, unsigned int len, u8 *out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static inline void sha224_init(struct sha256_state *sctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) sctx->state[0] = SHA224_H0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) sctx->state[1] = SHA224_H1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) sctx->state[2] = SHA224_H2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) sctx->state[3] = SHA224_H3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) sctx->state[4] = SHA224_H4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) sctx->state[5] = SHA224_H5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) sctx->state[6] = SHA224_H6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) sctx->state[7] = SHA224_H7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) sctx->count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) void sha224_update(struct sha256_state *sctx, const u8 *data, unsigned int len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) void sha224_final(struct sha256_state *sctx, u8 *out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) #endif