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.
 
 
 
 
 

133 lines
3.9 KiB

  1. /**
  2. * @file shake.h
  3. * @copyright
  4. * Based on CC0 code by David Leon Gil, 2015 \n
  5. * Copyright (c) 2015 Cryptography Research, Inc. \n
  6. * Released under the MIT License. See LICENSE.txt for license information.
  7. * @author Mike Hamburg
  8. * @brief SHA-3-n and SHAKE-n instances.
  9. * @warning EXPERIMENTAL! The names, parameter orders etc are likely to change.
  10. */
  11. #ifndef __SHAKE_H__
  12. #define __SHAKE_H__
  13. #include <stdint.h>
  14. #ifndef INTERNAL_SPONGE_STRUCT
  15. typedef struct keccak_sponge_s {
  16. uint64_t opaque[26];
  17. } keccak_sponge_t[1];
  18. struct kparams_s;
  19. #endif
  20. /**
  21. * @brief Initialize a sponge context object.
  22. * @param [out] sponge The object to initialize.
  23. * @param [in] params The sponge's parameter description.
  24. */
  25. void sponge_init (
  26. keccak_sponge_t sponge,
  27. const struct kparams_s *params
  28. );
  29. /**
  30. * @brief Absorb data into a SHA3 or SHAKE hash context.
  31. * @param [inout] sponge The context.
  32. * @param [in] in The input data.
  33. * @param [in] len The input data's length in bytes.
  34. */
  35. void sha3_update (
  36. struct keccak_sponge_s * __restrict__ sponge,
  37. const uint8_t *in,
  38. size_t len
  39. );
  40. /**
  41. * @brief Squeeze output data from a SHA3 or SHAKE hash context.
  42. * This does not destroy or re-initialize the hash context, and
  43. * sha3 output can be called more times.
  44. *
  45. * @param [inout] sponge The context.
  46. * @param [out] in The output data.
  47. * @param [in] len The requested output data length in bytes.
  48. */
  49. void sha3_output (
  50. keccak_sponge_t sponge,
  51. uint8_t * __restrict__ out,
  52. size_t len
  53. );
  54. /**
  55. * @brief Destroy a SHA3 or SHAKE sponge context by overwriting it with 0.
  56. * @param [out] sponge The context.
  57. */
  58. void sponge_destroy (
  59. keccak_sponge_t sponge
  60. );
  61. /**
  62. * @brief Hash (in) to (out)
  63. * @param [in] in The input data.
  64. * @param [in] inlen The length of the input data.
  65. * @param [out] out A buffer for the output data.
  66. * @param [in] outlen The length of the output data.
  67. */
  68. void sponge_hash (
  69. const uint8_t *in,
  70. size_t inlen,
  71. uint8_t *out,
  72. size_t outlen,
  73. const struct kparams_s *params
  74. );
  75. /* TODO: expand/doxygenate individual SHAKE/SHA3 instances? */
  76. #define DECSHAKE(n) \
  77. extern const struct kparams_s *SHAKE##n##_params; \
  78. static inline void shake##n##_init(keccak_sponge_t sponge) { \
  79. sponge_init(sponge, SHAKE##n##_params); \
  80. } \
  81. static inline void shake##n##_update(keccak_sponge_t sponge, const uint8_t *in, size_t inlen ) { \
  82. sha3_update(sponge, in, inlen); \
  83. } \
  84. static inline void shake##n##_final(keccak_sponge_t sponge, uint8_t *out, size_t outlen ) { \
  85. sha3_output(sponge, out, outlen); \
  86. sponge_init(sponge, SHAKE##n##_params); \
  87. } \
  88. static inline void shake##n##_hash(const uint8_t *in, size_t inlen, uint8_t *out, size_t outlen ) { \
  89. sponge_hash(in,inlen,out,outlen,SHAKE##n##_params); \
  90. } \
  91. static inline void shake##n##_destroy( keccak_sponge_t sponge ) { \
  92. sponge_destroy(sponge); \
  93. }
  94. #define DECSHA3(n) \
  95. extern const struct kparams_s *SHA3_##n##_params; \
  96. static inline void sha3_##n##_init(keccak_sponge_t sponge) { \
  97. sponge_init(sponge, SHA3_##n##_params); \
  98. } \
  99. static inline void sha3_##n##_update(keccak_sponge_t sponge, const uint8_t *in, size_t inlen ) { \
  100. sha3_update(sponge, in, inlen); \
  101. } \
  102. static inline void sha3_##n##_final(keccak_sponge_t sponge, uint8_t *out, size_t outlen ) { \
  103. sha3_output(sponge, out, outlen); \
  104. sponge_init(sponge, SHA3_##n##_params); \
  105. } \
  106. static inline void sha3_##n##_hash(const uint8_t *in, size_t inlen, uint8_t *out, size_t outlen ) { \
  107. sponge_hash(in,inlen,out,outlen,SHA3_##n##_params); \
  108. } \
  109. static inline void sha3_##n##_destroy( keccak_sponge_t sponge ) { \
  110. sponge_destroy(sponge); \
  111. }
  112. DECSHAKE(128)
  113. DECSHAKE(256)
  114. DECSHA3(224)
  115. DECSHA3(256)
  116. DECSHA3(384)
  117. DECSHA3(512)
  118. #endif /* __SHAKE_H__ */