Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Common values for SHA-2 algorithms
0004  */
0005 
0006 #ifndef _CRYPTO_SHA2_H
0007 #define _CRYPTO_SHA2_H
0008 
0009 #include <linux/types.h>
0010 
0011 #define SHA224_DIGEST_SIZE  28
0012 #define SHA224_BLOCK_SIZE   64
0013 
0014 #define SHA256_DIGEST_SIZE      32
0015 #define SHA256_BLOCK_SIZE       64
0016 
0017 #define SHA384_DIGEST_SIZE      48
0018 #define SHA384_BLOCK_SIZE       128
0019 
0020 #define SHA512_DIGEST_SIZE      64
0021 #define SHA512_BLOCK_SIZE       128
0022 
0023 #define SHA224_H0   0xc1059ed8UL
0024 #define SHA224_H1   0x367cd507UL
0025 #define SHA224_H2   0x3070dd17UL
0026 #define SHA224_H3   0xf70e5939UL
0027 #define SHA224_H4   0xffc00b31UL
0028 #define SHA224_H5   0x68581511UL
0029 #define SHA224_H6   0x64f98fa7UL
0030 #define SHA224_H7   0xbefa4fa4UL
0031 
0032 #define SHA256_H0   0x6a09e667UL
0033 #define SHA256_H1   0xbb67ae85UL
0034 #define SHA256_H2   0x3c6ef372UL
0035 #define SHA256_H3   0xa54ff53aUL
0036 #define SHA256_H4   0x510e527fUL
0037 #define SHA256_H5   0x9b05688cUL
0038 #define SHA256_H6   0x1f83d9abUL
0039 #define SHA256_H7   0x5be0cd19UL
0040 
0041 #define SHA384_H0   0xcbbb9d5dc1059ed8ULL
0042 #define SHA384_H1   0x629a292a367cd507ULL
0043 #define SHA384_H2   0x9159015a3070dd17ULL
0044 #define SHA384_H3   0x152fecd8f70e5939ULL
0045 #define SHA384_H4   0x67332667ffc00b31ULL
0046 #define SHA384_H5   0x8eb44a8768581511ULL
0047 #define SHA384_H6   0xdb0c2e0d64f98fa7ULL
0048 #define SHA384_H7   0x47b5481dbefa4fa4ULL
0049 
0050 #define SHA512_H0   0x6a09e667f3bcc908ULL
0051 #define SHA512_H1   0xbb67ae8584caa73bULL
0052 #define SHA512_H2   0x3c6ef372fe94f82bULL
0053 #define SHA512_H3   0xa54ff53a5f1d36f1ULL
0054 #define SHA512_H4   0x510e527fade682d1ULL
0055 #define SHA512_H5   0x9b05688c2b3e6c1fULL
0056 #define SHA512_H6   0x1f83d9abfb41bd6bULL
0057 #define SHA512_H7   0x5be0cd19137e2179ULL
0058 
0059 extern const u8 sha224_zero_message_hash[SHA224_DIGEST_SIZE];
0060 
0061 extern const u8 sha256_zero_message_hash[SHA256_DIGEST_SIZE];
0062 
0063 extern const u8 sha384_zero_message_hash[SHA384_DIGEST_SIZE];
0064 
0065 extern const u8 sha512_zero_message_hash[SHA512_DIGEST_SIZE];
0066 
0067 struct sha256_state {
0068     u32 state[SHA256_DIGEST_SIZE / 4];
0069     u64 count;
0070     u8 buf[SHA256_BLOCK_SIZE];
0071 };
0072 
0073 struct sha512_state {
0074     u64 state[SHA512_DIGEST_SIZE / 8];
0075     u64 count[2];
0076     u8 buf[SHA512_BLOCK_SIZE];
0077 };
0078 
0079 struct shash_desc;
0080 
0081 extern int crypto_sha256_update(struct shash_desc *desc, const u8 *data,
0082                   unsigned int len);
0083 
0084 extern int crypto_sha256_finup(struct shash_desc *desc, const u8 *data,
0085                    unsigned int len, u8 *hash);
0086 
0087 extern int crypto_sha512_update(struct shash_desc *desc, const u8 *data,
0088                   unsigned int len);
0089 
0090 extern int crypto_sha512_finup(struct shash_desc *desc, const u8 *data,
0091                    unsigned int len, u8 *hash);
0092 
0093 /*
0094  * Stand-alone implementation of the SHA256 algorithm. It is designed to
0095  * have as little dependencies as possible so it can be used in the
0096  * kexec_file purgatory. In other cases you should generally use the
0097  * hash APIs from include/crypto/hash.h. Especially when hashing large
0098  * amounts of data as those APIs may be hw-accelerated.
0099  *
0100  * For details see lib/crypto/sha256.c
0101  */
0102 
0103 static inline void sha256_init(struct sha256_state *sctx)
0104 {
0105     sctx->state[0] = SHA256_H0;
0106     sctx->state[1] = SHA256_H1;
0107     sctx->state[2] = SHA256_H2;
0108     sctx->state[3] = SHA256_H3;
0109     sctx->state[4] = SHA256_H4;
0110     sctx->state[5] = SHA256_H5;
0111     sctx->state[6] = SHA256_H6;
0112     sctx->state[7] = SHA256_H7;
0113     sctx->count = 0;
0114 }
0115 void sha256_update(struct sha256_state *sctx, const u8 *data, unsigned int len);
0116 void sha256_final(struct sha256_state *sctx, u8 *out);
0117 void sha256(const u8 *data, unsigned int len, u8 *out);
0118 
0119 static inline void sha224_init(struct sha256_state *sctx)
0120 {
0121     sctx->state[0] = SHA224_H0;
0122     sctx->state[1] = SHA224_H1;
0123     sctx->state[2] = SHA224_H2;
0124     sctx->state[3] = SHA224_H3;
0125     sctx->state[4] = SHA224_H4;
0126     sctx->state[5] = SHA224_H5;
0127     sctx->state[6] = SHA224_H6;
0128     sctx->state[7] = SHA224_H7;
0129     sctx->count = 0;
0130 }
0131 void sha224_update(struct sha256_state *sctx, const u8 *data, unsigned int len);
0132 void sha224_final(struct sha256_state *sctx, u8 *out);
0133 
0134 #endif /* _CRYPTO_SHA2_H */