sha256.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #ifndef SHA256_H_
  2. #define SHA256_H_
  3. #include <stddef.h>
  4. #include <stdint.h>
  5. /*
  6. * Use #defines in order to avoid namespace collisions with anyone else's
  7. * SHA256 code (e.g., the code in OpenSSL).
  8. */
  9. #define SHA256_Init libcperciva_SHA256_Init
  10. #define SHA256_Update libcperciva_SHA256_Update
  11. #define SHA256_Final libcperciva_SHA256_Final
  12. #define SHA256_Buf libcperciva_SHA256_Buf
  13. #define SHA256_CTX libcperciva_SHA256_CTX
  14. #define HMAC_SHA256_Init libcperciva_HMAC_SHA256_Init
  15. #define HMAC_SHA256_Update libcperciva_HMAC_SHA256_Update
  16. #define HMAC_SHA256_Final libcperciva_HMAC_SHA256_Final
  17. #define HMAC_SHA256_Buf libcperciva_HMAC_SHA256_Buf
  18. #define HMAC_SHA256_CTX libcperciva_HMAC_SHA256_CTX
  19. /* Context structure for SHA256 operations. */
  20. typedef struct {
  21. uint32_t state[8];
  22. uint64_t count;
  23. uint8_t buf[64];
  24. } SHA256_CTX;
  25. /**
  26. * SHA256_Init(ctx):
  27. * Initialize the SHA256 context ${ctx}.
  28. */
  29. void SHA256_Init(SHA256_CTX *);
  30. /**
  31. * SHA256_Update(ctx, in, len):
  32. * Input ${len} bytes from ${in} into the SHA256 context ${ctx}.
  33. */
  34. void SHA256_Update(SHA256_CTX *, const void *, size_t);
  35. /**
  36. * SHA256_Final(digest, ctx):
  37. * Output the SHA256 hash of the data input to the context ${ctx} into the
  38. * buffer ${digest}, and clear the context state.
  39. */
  40. void SHA256_Final(uint8_t[32], SHA256_CTX *);
  41. /**
  42. * SHA256_Buf(in, len, digest):
  43. * Compute the SHA256 hash of ${len} bytes from ${in} and write it to ${digest}.
  44. */
  45. void SHA256_Buf(const void *, size_t, uint8_t[32]);
  46. /* Context structure for HMAC-SHA256 operations. */
  47. typedef struct {
  48. SHA256_CTX ictx;
  49. SHA256_CTX octx;
  50. } HMAC_SHA256_CTX;
  51. /**
  52. * HMAC_SHA256_Init(ctx, K, Klen):
  53. * Initialize the HMAC-SHA256 context ${ctx} with ${Klen} bytes of key from
  54. * ${K}.
  55. */
  56. void HMAC_SHA256_Init(HMAC_SHA256_CTX *, const void *, size_t);
  57. /**
  58. * HMAC_SHA256_Update(ctx, in, len):
  59. * Input ${len} bytes from ${in} into the HMAC-SHA256 context ${ctx}.
  60. */
  61. void HMAC_SHA256_Update(HMAC_SHA256_CTX *, const void *, size_t);
  62. /**
  63. * HMAC_SHA256_Final(digest, ctx):
  64. * Output the HMAC-SHA256 of the data input to the context ${ctx} into the
  65. * buffer ${digest}, and clear the context state.
  66. */
  67. void HMAC_SHA256_Final(uint8_t[32], HMAC_SHA256_CTX *);
  68. /**
  69. * HMAC_SHA256_Buf(K, Klen, in, len, digest):
  70. * Compute the HMAC-SHA256 of ${len} bytes from ${in} using the key ${K} of
  71. * length ${Klen}, and write the result to ${digest}.
  72. */
  73. void HMAC_SHA256_Buf(const void *, size_t, const void *, size_t, uint8_t[32]);
  74. /**
  75. * PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen):
  76. * Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and
  77. * write the output to buf. The value dkLen must be at most 32 * (2^32 - 1).
  78. */
  79. void PBKDF2_SHA256(const uint8_t *, size_t, const uint8_t *, size_t,
  80. uint64_t, uint8_t *, size_t);
  81. #endif /* !SHA256_H_ */