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.
 
 
 
 
 

227 lines
6.6 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. /* TODO: unify with other headers (maybe all into one??); add nonnull attributes */
  16. #define API_VIS __attribute__((visibility("default")))
  17. #define WARN_UNUSED __attribute__((warn_unused_result))
  18. /* TODO: different containing structs for each primitive? */
  19. #ifndef INTERNAL_SPONGE_STRUCT
  20. typedef struct keccak_sponge_s {
  21. uint64_t opaque[26];
  22. } keccak_sponge_t[1];
  23. struct kparams_s;
  24. #endif
  25. /**
  26. * @brief Initialize a sponge context object.
  27. * @param [out] sponge The object to initialize.
  28. * @param [in] params The sponge's parameter description.
  29. */
  30. void sponge_init (
  31. keccak_sponge_t sponge,
  32. const struct kparams_s *params
  33. ) API_VIS;
  34. /**
  35. * @brief Absorb data into a SHA3 or SHAKE hash context.
  36. * @param [inout] sponge The context.
  37. * @param [in] in The input data.
  38. * @param [in] len The input data's length in bytes.
  39. */
  40. void sha3_update (
  41. struct keccak_sponge_s * __restrict__ sponge,
  42. const uint8_t *in,
  43. size_t len
  44. ) API_VIS;
  45. /**
  46. * @brief Squeeze output data from a SHA3 or SHAKE hash context.
  47. * This does not destroy or re-initialize the hash context, and
  48. * sha3 output can be called more times.
  49. *
  50. * @param [inout] sponge The context.
  51. * @param [out] in The output data.
  52. * @param [in] len The requested output data length in bytes.
  53. */
  54. void sha3_output (
  55. keccak_sponge_t sponge,
  56. uint8_t * __restrict__ out,
  57. size_t len
  58. ) API_VIS;
  59. /**
  60. * @brief Destroy a SHA3 or SHAKE sponge context by overwriting it with 0.
  61. * @param [out] sponge The context.
  62. */
  63. void sponge_destroy (
  64. keccak_sponge_t sponge
  65. ) API_VIS;
  66. /**
  67. * @brief Hash (in) to (out)
  68. * @param [in] in The input data.
  69. * @param [in] inlen The length of the input data.
  70. * @param [out] out A buffer for the output data.
  71. * @param [in] outlen The length of the output data.
  72. * @param [in] params The parameters of the sponge hash.
  73. */
  74. void sponge_hash (
  75. const uint8_t *in,
  76. size_t inlen,
  77. uint8_t *out,
  78. size_t outlen,
  79. const struct kparams_s *params
  80. ) API_VIS;
  81. /* TODO: expand/doxygenate individual SHAKE/SHA3 instances? */
  82. #define DECSHAKE(n) \
  83. extern const struct kparams_s *SHAKE##n##_params API_VIS; \
  84. static inline void shake##n##_init(keccak_sponge_t sponge) { \
  85. sponge_init(sponge, SHAKE##n##_params); \
  86. } \
  87. static inline void shake##n##_update(keccak_sponge_t sponge, const uint8_t *in, size_t inlen ) { \
  88. sha3_update(sponge, in, inlen); \
  89. } \
  90. static inline void shake##n##_final(keccak_sponge_t sponge, uint8_t *out, size_t outlen ) { \
  91. sha3_output(sponge, out, outlen); \
  92. sponge_init(sponge, SHAKE##n##_params); \
  93. } \
  94. static inline void shake##n##_hash(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen) { \
  95. sponge_hash(in,inlen,out,outlen,SHAKE##n##_params); \
  96. } \
  97. static inline void shake##n##_destroy( keccak_sponge_t sponge ) { \
  98. sponge_destroy(sponge); \
  99. }
  100. #define DECSHA3(n) \
  101. extern const struct kparams_s *SHA3_##n##_params API_VIS; \
  102. static inline void sha3_##n##_init(keccak_sponge_t sponge) { \
  103. sponge_init(sponge, SHA3_##n##_params); \
  104. } \
  105. static inline void sha3_##n##_update(keccak_sponge_t sponge, const uint8_t *in, size_t inlen ) { \
  106. sha3_update(sponge, in, inlen); \
  107. } \
  108. static inline void sha3_##n##_final(keccak_sponge_t sponge, uint8_t *out, size_t outlen ) { \
  109. sha3_output(sponge, out, outlen); \
  110. sponge_init(sponge, SHA3_##n##_params); \
  111. } \
  112. static inline void sha3_##n##_hash(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen) { \
  113. sponge_hash(in,inlen,out,outlen,SHA3_##n##_params); \
  114. } \
  115. static inline void sha3_##n##_destroy( keccak_sponge_t sponge ) { \
  116. sponge_destroy(sponge); \
  117. }
  118. DECSHAKE(128)
  119. DECSHAKE(256)
  120. DECSHA3(224)
  121. DECSHA3(256)
  122. DECSHA3(384)
  123. DECSHA3(512)
  124. /**
  125. * @brief Initialize a sponge-based CSPRNG from a buffer.
  126. *
  127. * @param [out] sponge The sponge object.
  128. * @param [in] in The initial data.
  129. * @param [in] len The length of the initial data.
  130. * @param [in] deterministic If zero, allow RNG to stir in nondeterministic
  131. * data from RDRAND or RDTSC.
  132. */
  133. void spongerng_init_from_buffer (
  134. keccak_sponge_t sponge,
  135. const uint8_t * __restrict__ in,
  136. size_t len,
  137. int deterministic
  138. ) API_VIS;
  139. /* FIXME!! This interface has the opposite retval convention from other functions
  140. * in the library. (0=success). Should they be harmonized?
  141. */
  142. /**
  143. * @brief Initialize a sponge-based CSPRNG from a file.
  144. *
  145. * @param [out] sponge The sponge object.
  146. * @param [in] file A name of a file containing initial data.
  147. * @param [in] len The length of the initial data. Must be positive.
  148. * @param [in] deterministic If zero, allow RNG to stir in nondeterministic
  149. * data from RDRAND or RDTSC.
  150. *
  151. * @retval 0 Success.
  152. * @retval positive An error has occurred, and this was the errno.
  153. * @retval -1 An unknown error has occurred.
  154. * @retval -2 len was 0.
  155. */
  156. int spongerng_init_from_file (
  157. keccak_sponge_t sponge,
  158. const char *file,
  159. size_t len,
  160. int deterministic
  161. ) API_VIS WARN_UNUSED;
  162. /* FIXME!! This interface has the opposite retval convention from other functions
  163. * in the library. (0=success). Should they be harmonized?
  164. */
  165. /**
  166. * @brief Initialize a nondeterministic sponge-based CSPRNG from /dev/urandom.
  167. *
  168. * @param [out] sponge The sponge object.
  169. *
  170. * @retval 0 Success.
  171. * @retval positive An error has occurred, and this was the errno.
  172. * @retval -1 An unknown error has occurred.
  173. */
  174. int spongerng_init_from_dev_urandom (
  175. keccak_sponge_t sponge
  176. ) API_VIS WARN_UNUSED;
  177. /**
  178. * @brief Output bytes from a sponge-based CSPRNG.
  179. *
  180. * @param [inout] sponge The sponge object.
  181. * @param [out] out The output buffer.
  182. * @param [in] out The output buffer's length.
  183. */
  184. void spongerng_next (
  185. keccak_sponge_t sponge,
  186. uint8_t * __restrict__ out,
  187. size_t len
  188. ) API_VIS;
  189. /**
  190. * @brief Stir entropy data into a sponge-based CSPRNG from a buffer.
  191. *
  192. * @param [out] sponge The sponge object.
  193. * @param [in] in The entropy data.
  194. * @param [in] len The length of the initial data.
  195. */
  196. void spongerng_stir (
  197. keccak_sponge_t sponge,
  198. const uint8_t * __restrict__ in,
  199. size_t len
  200. ) API_VIS;
  201. #undef API_VIS
  202. #undef WARN_UNUSED
  203. #endif /* __SHAKE_H__ */