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.
 
 
 
 
 

136 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. */
  69. void sponge_hash (
  70. const uint8_t *in,
  71. size_t inlen,
  72. uint8_t *out,
  73. size_t outlen,
  74. const struct kparams_s *params
  75. ) API_VIS;
  76. /* TODO: expand/doxygenate individual SHAKE/SHA3 instances? */
  77. #define DECSHAKE(n) \
  78. extern const struct kparams_s *SHAKE##n##_params API_VIS; \
  79. static inline void shake##n##_init(keccak_sponge_t sponge) { \
  80. sponge_init(sponge, SHAKE##n##_params); \
  81. } \
  82. static inline void shake##n##_update(keccak_sponge_t sponge, const uint8_t *in, size_t inlen ) { \
  83. sha3_update(sponge, in, inlen); \
  84. } \
  85. static inline void shake##n##_final(keccak_sponge_t sponge, uint8_t *out, size_t outlen ) { \
  86. sha3_output(sponge, out, outlen); \
  87. sponge_init(sponge, SHAKE##n##_params); \
  88. } \
  89. static inline void shake##n##_hash(const uint8_t *in, size_t inlen, uint8_t *out, size_t outlen ) { \
  90. sponge_hash(in,inlen,out,outlen,SHAKE##n##_params); \
  91. } \
  92. static inline void shake##n##_destroy( keccak_sponge_t sponge ) { \
  93. sponge_destroy(sponge); \
  94. }
  95. #define DECSHA3(n) \
  96. extern const struct kparams_s *SHA3_##n##_params API_VIS; \
  97. static inline void sha3_##n##_init(keccak_sponge_t sponge) { \
  98. sponge_init(sponge, SHA3_##n##_params); \
  99. } \
  100. static inline void sha3_##n##_update(keccak_sponge_t sponge, const uint8_t *in, size_t inlen ) { \
  101. sha3_update(sponge, in, inlen); \
  102. } \
  103. static inline void sha3_##n##_final(keccak_sponge_t sponge, uint8_t *out, size_t outlen ) { \
  104. sha3_output(sponge, out, outlen); \
  105. sponge_init(sponge, SHA3_##n##_params); \
  106. } \
  107. static inline void sha3_##n##_hash(const uint8_t *in, size_t inlen, uint8_t *out, size_t outlen ) { \
  108. sponge_hash(in,inlen,out,outlen,SHA3_##n##_params); \
  109. } \
  110. static inline void sha3_##n##_destroy( keccak_sponge_t sponge ) { \
  111. sponge_destroy(sponge); \
  112. }
  113. DECSHAKE(128)
  114. DECSHAKE(256)
  115. DECSHA3(224)
  116. DECSHA3(256)
  117. DECSHA3(384)
  118. DECSHA3(512)
  119. #undef API_VIS
  120. #endif /* __SHAKE_H__ */