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.
 
 
 
 
 

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