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.
 
 
 
 
 

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