You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

72 lines
2.0 KiB

  1. /**
  2. * @file decaf/shake.h
  3. * @copyright Public domain.
  4. * @author Mike Hamburg
  5. * @brief SHA2-512
  6. */
  7. #ifndef __DECAF_SHA512_H__
  8. #define __DECAF_SHA512_H__
  9. #include <stdint.h>
  10. #include <sys/types.h>
  11. #include <stdlib.h> /* for NULL */
  12. #include <decaf/common.h>
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. /** Hash context for SHA-512 */
  17. typedef struct decaf_sha512_ctx_s {
  18. /** @cond internal */
  19. uint64_t state[8];
  20. uint8_t block[128];
  21. uint64_t bytes_processed;
  22. /* @endcond */
  23. } decaf_sha512_ctx_s, decaf_sha512_ctx_t[1];
  24. /** Initialize a SHA-512 context. */
  25. void DECAF_API_VIS decaf_sha512_init(decaf_sha512_ctx_t ctx) DECAF_NONNULL;
  26. /** Update context by hashing part of a message. */
  27. void DECAF_API_VIS decaf_sha512_update(decaf_sha512_ctx_t ctx, const uint8_t *message, size_t message_len) DECAF_NONNULL;
  28. /** Finalize context and write out hash.
  29. * @param [inout] ctx The context. Will be destroyed and re-initialized on return.
  30. * @param [out] output Place to store the output hash.
  31. * @param [in] output_len Length in bytes of the output hash. Must between 0 and 64, inclusive.
  32. */
  33. void DECAF_API_VIS decaf_sha512_final(decaf_sha512_ctx_t ctx, uint8_t *output, size_t output_len) DECAF_NONNULL;
  34. /** Securely destroy a SHA512 context. */
  35. static inline void decaf_sha512_destroy(decaf_sha512_ctx_t ctx) {
  36. decaf_bzero(ctx,sizeof(*ctx));
  37. }
  38. /** Hash a message.
  39. * @param [out] output Place to store the output hash.
  40. * @param [in] output_len Length in bytes of the output hash. Must between 0 and 64, inclusive.
  41. * @param [in] message A message to hash.
  42. * @param [in] message_len Length in bytes of the input message.
  43. */
  44. static inline void decaf_sha512_hash(
  45. uint8_t *output,
  46. size_t output_len,
  47. const uint8_t *message,
  48. size_t message_len
  49. ) {
  50. decaf_sha512_ctx_t ctx;
  51. decaf_sha512_init(ctx);
  52. decaf_sha512_update(ctx,message,message_len);
  53. decaf_sha512_final(ctx,output,output_len);
  54. decaf_sha512_destroy(ctx);
  55. }
  56. #ifdef __cplusplus
  57. } /* extern "C" */
  58. #endif
  59. #endif /* __DECAF_SHA512_H__ */