sha2.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Common values for SHA-2 algorithms
  4. */
  5. #ifndef _CRYPTO_SHA2_H
  6. #define _CRYPTO_SHA2_H
  7. #include <linux/types.h>
  8. #define SHA224_DIGEST_SIZE 28
  9. #define SHA224_BLOCK_SIZE 64
  10. #define SHA256_DIGEST_SIZE 32
  11. #define SHA256_BLOCK_SIZE 64
  12. #define SHA384_DIGEST_SIZE 48
  13. #define SHA384_BLOCK_SIZE 128
  14. #define SHA512_DIGEST_SIZE 64
  15. #define SHA512_BLOCK_SIZE 128
  16. #define SHA224_H0 0xc1059ed8UL
  17. #define SHA224_H1 0x367cd507UL
  18. #define SHA224_H2 0x3070dd17UL
  19. #define SHA224_H3 0xf70e5939UL
  20. #define SHA224_H4 0xffc00b31UL
  21. #define SHA224_H5 0x68581511UL
  22. #define SHA224_H6 0x64f98fa7UL
  23. #define SHA224_H7 0xbefa4fa4UL
  24. #define SHA256_H0 0x6a09e667UL
  25. #define SHA256_H1 0xbb67ae85UL
  26. #define SHA256_H2 0x3c6ef372UL
  27. #define SHA256_H3 0xa54ff53aUL
  28. #define SHA256_H4 0x510e527fUL
  29. #define SHA256_H5 0x9b05688cUL
  30. #define SHA256_H6 0x1f83d9abUL
  31. #define SHA256_H7 0x5be0cd19UL
  32. #define SHA384_H0 0xcbbb9d5dc1059ed8ULL
  33. #define SHA384_H1 0x629a292a367cd507ULL
  34. #define SHA384_H2 0x9159015a3070dd17ULL
  35. #define SHA384_H3 0x152fecd8f70e5939ULL
  36. #define SHA384_H4 0x67332667ffc00b31ULL
  37. #define SHA384_H5 0x8eb44a8768581511ULL
  38. #define SHA384_H6 0xdb0c2e0d64f98fa7ULL
  39. #define SHA384_H7 0x47b5481dbefa4fa4ULL
  40. #define SHA512_H0 0x6a09e667f3bcc908ULL
  41. #define SHA512_H1 0xbb67ae8584caa73bULL
  42. #define SHA512_H2 0x3c6ef372fe94f82bULL
  43. #define SHA512_H3 0xa54ff53a5f1d36f1ULL
  44. #define SHA512_H4 0x510e527fade682d1ULL
  45. #define SHA512_H5 0x9b05688c2b3e6c1fULL
  46. #define SHA512_H6 0x1f83d9abfb41bd6bULL
  47. #define SHA512_H7 0x5be0cd19137e2179ULL
  48. extern const u8 sha224_zero_message_hash[SHA224_DIGEST_SIZE];
  49. extern const u8 sha256_zero_message_hash[SHA256_DIGEST_SIZE];
  50. extern const u8 sha384_zero_message_hash[SHA384_DIGEST_SIZE];
  51. extern const u8 sha512_zero_message_hash[SHA512_DIGEST_SIZE];
  52. struct sha256_state {
  53. u32 state[SHA256_DIGEST_SIZE / 4];
  54. u64 count;
  55. u8 buf[SHA256_BLOCK_SIZE];
  56. };
  57. struct sha512_state {
  58. u64 state[SHA512_DIGEST_SIZE / 8];
  59. u64 count[2];
  60. u8 buf[SHA512_BLOCK_SIZE];
  61. };
  62. struct shash_desc;
  63. extern int crypto_sha256_update(struct shash_desc *desc, const u8 *data,
  64. unsigned int len);
  65. extern int crypto_sha256_finup(struct shash_desc *desc, const u8 *data,
  66. unsigned int len, u8 *hash);
  67. extern int crypto_sha512_update(struct shash_desc *desc, const u8 *data,
  68. unsigned int len);
  69. extern int crypto_sha512_finup(struct shash_desc *desc, const u8 *data,
  70. unsigned int len, u8 *hash);
  71. /*
  72. * Stand-alone implementation of the SHA256 algorithm. It is designed to
  73. * have as little dependencies as possible so it can be used in the
  74. * kexec_file purgatory. In other cases you should generally use the
  75. * hash APIs from include/crypto/hash.h. Especially when hashing large
  76. * amounts of data as those APIs may be hw-accelerated.
  77. *
  78. * For details see lib/crypto/sha256.c
  79. */
  80. static inline void sha256_init(struct sha256_state *sctx)
  81. {
  82. sctx->state[0] = SHA256_H0;
  83. sctx->state[1] = SHA256_H1;
  84. sctx->state[2] = SHA256_H2;
  85. sctx->state[3] = SHA256_H3;
  86. sctx->state[4] = SHA256_H4;
  87. sctx->state[5] = SHA256_H5;
  88. sctx->state[6] = SHA256_H6;
  89. sctx->state[7] = SHA256_H7;
  90. sctx->count = 0;
  91. }
  92. void sha256_update(struct sha256_state *sctx, const u8 *data, unsigned int len);
  93. void sha256_final(struct sha256_state *sctx, u8 *out);
  94. void sha256(const u8 *data, unsigned int len, u8 *out);
  95. static inline void sha224_init(struct sha256_state *sctx)
  96. {
  97. sctx->state[0] = SHA224_H0;
  98. sctx->state[1] = SHA224_H1;
  99. sctx->state[2] = SHA224_H2;
  100. sctx->state[3] = SHA224_H3;
  101. sctx->state[4] = SHA224_H4;
  102. sctx->state[5] = SHA224_H5;
  103. sctx->state[6] = SHA224_H6;
  104. sctx->state[7] = SHA224_H7;
  105. sctx->count = 0;
  106. }
  107. void sha224_update(struct sha256_state *sctx, const u8 *data, unsigned int len);
  108. void sha224_final(struct sha256_state *sctx, u8 *out);
  109. #endif /* _CRYPTO_SHA2_H */