hmacsha256.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #ifndef _HMAC_SHA_256_H_
  2. #define _HMAC_SHA_256_H_
  3. #define SHA256_BLOCKLEN 64ul //size of message block buffer
  4. #define SHA256_DIGESTLEN 32ul //size of digest in uint8_t
  5. #define SHA256_DIGESTINT 8ul //size of digest in uint32_t
  6. #include <stdint.h>
  7. #if defined(__GNUC__)
  8. #pragma GCC diagnostic push
  9. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  10. #elif defined(_MSC_VER)
  11. #pragma warning(disable : 4996)
  12. #endif
  13. typedef struct sha256_ctx_t
  14. {
  15. uint64_t len; // processed message length
  16. uint32_t h[SHA256_DIGESTINT]; // hash state
  17. uint8_t buf[SHA256_BLOCKLEN]; // message block buffer
  18. } SHA256_CTX;
  19. void sha256_init(SHA256_CTX* ctx);
  20. void sha256_update(SHA256_CTX* ctx, const uint8_t* m, uint32_t mlen);
  21. // resets state: calls sha256_init
  22. void sha256_final(SHA256_CTX* ctx, uint8_t* md);
  23. typedef struct hmac_sha256_ctx_t
  24. {
  25. uint8_t buf[SHA256_BLOCKLEN]; // key block buffer, not needed after init
  26. uint32_t h_inner[SHA256_DIGESTINT];
  27. uint32_t h_outer[SHA256_DIGESTINT];
  28. SHA256_CTX sha;
  29. } HMAC_SHA256_CTX;
  30. void hmac_sha256_init(HMAC_SHA256_CTX* hmac, const uint8_t* key, uint32_t keylen);
  31. void hmac_sha256_update(HMAC_SHA256_CTX* hmac, const uint8_t* m, uint32_t mlen);
  32. void hmac_sha256_final(HMAC_SHA256_CTX* hmac, uint8_t* md);
  33. void pbkdf2_sha256(HMAC_SHA256_CTX* ctx, const uint8_t* key, uint32_t keylen, const uint8_t* salt, uint32_t saltlen, uint32_t rounds, uint8_t* dk, uint32_t dklen);
  34. #endif // _HMAC_SHA_256_H_