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.
 
 
 
 
 

137 lines
4.0 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. #define API_VIS __attribute__((visibility("default")))
  15. #ifndef INTERNAL_SPONGE_STRUCT
  16. typedef struct keccak_sponge_s {
  17. uint64_t opaque[26];
  18. } keccak_sponge_t[1];
  19. struct kparams_s;
  20. #endif
  21. /**
  22. * @brief Initialize a sponge context object.
  23. * @param [out] sponge The object to initialize.
  24. * @param [in] params The sponge's parameter description.
  25. */
  26. void sponge_init (
  27. keccak_sponge_t sponge,
  28. const struct kparams_s *params
  29. ) API_VIS;
  30. /**
  31. * @brief Absorb data into a SHA3 or SHAKE hash context.
  32. * @param [inout] sponge The context.
  33. * @param [in] in The input data.
  34. * @param [in] len The input data's length in bytes.
  35. */
  36. void sha3_update (
  37. struct keccak_sponge_s * __restrict__ sponge,
  38. const uint8_t *in,
  39. size_t len
  40. ) API_VIS;
  41. /**
  42. * @brief Squeeze output data from a SHA3 or SHAKE hash context.
  43. * This does not destroy or re-initialize the hash context, and
  44. * sha3 output can be called more times.
  45. *
  46. * @param [inout] sponge The context.
  47. * @param [out] in The output data.
  48. * @param [in] len The requested output data length in bytes.
  49. */
  50. void sha3_output (
  51. keccak_sponge_t sponge,
  52. uint8_t * __restrict__ out,
  53. size_t len
  54. ) API_VIS;
  55. /**
  56. * @brief Destroy a SHA3 or SHAKE sponge context by overwriting it with 0.
  57. * @param [out] sponge The context.
  58. */
  59. void sponge_destroy (
  60. keccak_sponge_t sponge
  61. ) API_VIS;
  62. /**
  63. * @brief Hash (in) to (out)
  64. * @param [in] in The input data.
  65. * @param [in] inlen The length of the input data.
  66. * @param [out] out A buffer for the output data.
  67. * @param [in] outlen The length of the output data.
  68. * @param [in] params The parameters of the sponge hash.
  69. */
  70. void sponge_hash (
  71. const uint8_t *in,
  72. size_t inlen,
  73. uint8_t *out,
  74. size_t outlen,
  75. const struct kparams_s *params
  76. ) API_VIS;
  77. /* TODO: expand/doxygenate individual SHAKE/SHA3 instances? */
  78. #define DECSHAKE(n) \
  79. extern const struct kparams_s *SHAKE##n##_params API_VIS; \
  80. static inline void shake##n##_init(keccak_sponge_t sponge) { \
  81. sponge_init(sponge, SHAKE##n##_params); \
  82. } \
  83. static inline void shake##n##_update(keccak_sponge_t sponge, const uint8_t *in, size_t inlen ) { \
  84. sha3_update(sponge, in, inlen); \
  85. } \
  86. static inline void shake##n##_final(keccak_sponge_t sponge, uint8_t *out, size_t outlen ) { \
  87. sha3_output(sponge, out, outlen); \
  88. sponge_init(sponge, SHAKE##n##_params); \
  89. } \
  90. static inline void shake##n##_hash(const uint8_t *in, size_t inlen, uint8_t *out, size_t outlen ) { \
  91. sponge_hash(in,inlen,out,outlen,SHAKE##n##_params); \
  92. } \
  93. static inline void shake##n##_destroy( keccak_sponge_t sponge ) { \
  94. sponge_destroy(sponge); \
  95. }
  96. #define DECSHA3(n) \
  97. extern const struct kparams_s *SHA3_##n##_params API_VIS; \
  98. static inline void sha3_##n##_init(keccak_sponge_t sponge) { \
  99. sponge_init(sponge, SHA3_##n##_params); \
  100. } \
  101. static inline void sha3_##n##_update(keccak_sponge_t sponge, const uint8_t *in, size_t inlen ) { \
  102. sha3_update(sponge, in, inlen); \
  103. } \
  104. static inline void sha3_##n##_final(keccak_sponge_t sponge, uint8_t *out, size_t outlen ) { \
  105. sha3_output(sponge, out, outlen); \
  106. sponge_init(sponge, SHA3_##n##_params); \
  107. } \
  108. static inline void sha3_##n##_hash(const uint8_t *in, size_t inlen, uint8_t *out, size_t outlen ) { \
  109. sponge_hash(in,inlen,out,outlen,SHA3_##n##_params); \
  110. } \
  111. static inline void sha3_##n##_destroy( keccak_sponge_t sponge ) { \
  112. sponge_destroy(sponge); \
  113. }
  114. DECSHAKE(128)
  115. DECSHAKE(256)
  116. DECSHA3(224)
  117. DECSHA3(256)
  118. DECSHA3(384)
  119. DECSHA3(512)
  120. #undef API_VIS
  121. #endif /* __SHAKE_H__ */