keccak-tiny.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #ifndef KECCAK_FIPS202_H
  2. #define KECCAK_FIPS202_H
  3. #define __STDC_WANT_LIB_EXT1__ 1
  4. #include <stdint.h>
  5. #include <stdlib.h>
  6. #define KECCAK_MAX_RATE 200
  7. /* Calculate the rate (block size) from the security target. */
  8. #define KECCAK_RATE(bits) (KECCAK_MAX_RATE - (bits / 4))
  9. /* The internal structure of a FIPS202 hash/xof instance. Most callers
  10. * should treat this as an opaque structure.
  11. */
  12. typedef struct keccak_state {
  13. uint8_t a[KECCAK_MAX_RATE];
  14. size_t rate;
  15. uint8_t delim;
  16. uint8_t block[KECCAK_MAX_RATE];
  17. size_t offset;
  18. uint8_t finalized : 1;
  19. } keccak_state;
  20. /* Initialize a Keccak instance suitable for SHA-3 hash functions. */
  21. int keccak_digest_init(keccak_state *s, size_t bits);
  22. /* Feed more data into the SHA-3 hash instance. */
  23. int keccak_digest_update(keccak_state *s, const uint8_t *buf, size_t len);
  24. /* Calculate the SHA-3 hash digest. The state is unmodified to support
  25. * calculating multiple/rolling digests.
  26. */
  27. int keccak_digest_sum(const keccak_state *s, uint8_t *out, size_t outlen);
  28. /* Initialize a Keccak instance suitable for XOFs (SHAKE-128/256). */
  29. int keccak_xof_init(keccak_state *s, size_t bits);
  30. /* Absorb more data into the XOF. Must not be called after a squeeze call. */
  31. int keccak_xof_absorb(keccak_state *s, const uint8_t *buf, size_t len);
  32. /* Squeeze data out of the XOF. Must not attempt to absorb additional data,
  33. * after a squeeze has been called.
  34. */
  35. int keccak_xof_squeeze(keccak_state *s, uint8_t *out, size_t outlen);
  36. /* Clone an existing hash/XOF instance. */
  37. void keccak_clone(keccak_state *out, const keccak_state *in);
  38. /* Cleanse sensitive data from a given hash instance. */
  39. void keccak_cleanse(keccak_state *s);
  40. #define decshake(bits) \
  41. int shake##bits(uint8_t*, size_t, const uint8_t*, size_t);
  42. #define decsha3(bits) \
  43. int sha3_##bits(uint8_t*, size_t, const uint8_t*, size_t);
  44. decshake(128)
  45. decshake(256)
  46. decsha3(224)
  47. decsha3(256)
  48. decsha3(384)
  49. decsha3(512)
  50. #endif