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.
 
 
 
 
 

242 lines
6.9 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 Destroy a SHA3 or SHAKE sponge context by overwriting it with 0.
  69. * @param [out] sponge The context.
  70. */
  71. void sponge_destroy (
  72. keccak_sponge_t sponge
  73. ) API_VIS;
  74. /**
  75. * @brief Hash (in) to (out)
  76. * @param [in] in The input data.
  77. * @param [in] inlen The length of the input data.
  78. * @param [out] out A buffer for the output data.
  79. * @param [in] outlen The length of the output data.
  80. * @param [in] params The parameters of the sponge hash.
  81. */
  82. void sponge_hash (
  83. const uint8_t *in,
  84. size_t inlen,
  85. uint8_t *out,
  86. size_t outlen,
  87. const struct kparams_s *params
  88. ) API_VIS;
  89. /* TODO: expand/doxygenate individual SHAKE/SHA3 instances? */
  90. /** @cond internal */
  91. #define DECSHAKE(n) \
  92. extern const struct kparams_s SHAKE##n##_params_s API_VIS; \
  93. static inline void shake##n##_init(keccak_sponge_t sponge) { \
  94. sponge_init(sponge, &SHAKE##n##_params_s); \
  95. } \
  96. static inline void shake##n##_update(keccak_sponge_t sponge, const uint8_t *in, size_t inlen ) { \
  97. sha3_update(sponge, in, inlen); \
  98. } \
  99. static inline void shake##n##_final(keccak_sponge_t sponge, uint8_t *out, size_t outlen ) { \
  100. sha3_output(sponge, out, outlen); \
  101. sponge_init(sponge, &SHAKE##n##_params_s); \
  102. } \
  103. static inline void shake##n##_hash(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen) { \
  104. sponge_hash(in,inlen,out,outlen,&SHAKE##n##_params_s); \
  105. } \
  106. static inline void shake##n##_destroy( keccak_sponge_t sponge ) { \
  107. sponge_destroy(sponge); \
  108. }
  109. #define DECSHA3(n) \
  110. extern const struct kparams_s SHA3_##n##_params_s API_VIS; \
  111. static inline void sha3_##n##_init(keccak_sponge_t sponge) { \
  112. sponge_init(sponge, &SHA3_##n##_params_s); \
  113. } \
  114. static inline void sha3_##n##_update(keccak_sponge_t sponge, const uint8_t *in, size_t inlen ) { \
  115. sha3_update(sponge, in, inlen); \
  116. } \
  117. static inline void sha3_##n##_final(keccak_sponge_t sponge, uint8_t *out, size_t outlen ) { \
  118. sha3_output(sponge, out, outlen); \
  119. sponge_init(sponge, &SHA3_##n##_params_s); \
  120. } \
  121. static inline void sha3_##n##_hash(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen) { \
  122. sponge_hash(in,inlen,out,outlen,&SHA3_##n##_params_s); \
  123. } \
  124. static inline void sha3_##n##_destroy( keccak_sponge_t sponge ) { \
  125. sponge_destroy(sponge); \
  126. }
  127. /** @endcond */
  128. DECSHAKE(128)
  129. DECSHAKE(256)
  130. DECSHA3(224)
  131. DECSHA3(256)
  132. DECSHA3(384)
  133. DECSHA3(512)
  134. /**
  135. * @brief Initialize a sponge-based CSPRNG from a buffer.
  136. *
  137. * @param [out] sponge The sponge object.
  138. * @param [in] in The initial data.
  139. * @param [in] len The length of the initial data.
  140. * @param [in] deterministic If zero, allow RNG to stir in nondeterministic
  141. * data from RDRAND or RDTSC.
  142. */
  143. void spongerng_init_from_buffer (
  144. keccak_sponge_t sponge,
  145. const uint8_t * __restrict__ in,
  146. size_t len,
  147. int deterministic
  148. ) API_VIS;
  149. /* FIXME!! This interface has the opposite retval convention from other functions
  150. * in the library. (0=success). Should they be harmonized?
  151. */
  152. /**
  153. * @brief Initialize a sponge-based CSPRNG from a file.
  154. *
  155. * @param [out] sponge The sponge object.
  156. * @param [in] file A name of a file containing initial data.
  157. * @param [in] len The length of the initial data. Must be positive.
  158. * @param [in] deterministic If zero, allow RNG to stir in nondeterministic
  159. * data from RDRAND or RDTSC.
  160. *
  161. * @retval 0 Success.
  162. * @retval positive An error has occurred, and this was the errno.
  163. * @retval -1 An unknown error has occurred.
  164. * @retval -2 len was 0.
  165. */
  166. int spongerng_init_from_file (
  167. keccak_sponge_t sponge,
  168. const char *file,
  169. size_t len,
  170. int deterministic
  171. ) API_VIS WARN_UNUSED;
  172. /* FIXME!! This interface has the opposite retval convention from other functions
  173. * in the library. (0=success). Should they be harmonized?
  174. */
  175. /**
  176. * @brief Initialize a nondeterministic sponge-based CSPRNG from /dev/urandom.
  177. *
  178. * @param [out] sponge The sponge object.
  179. *
  180. * @retval 0 Success.
  181. * @retval positive An error has occurred, and this was the errno.
  182. * @retval -1 An unknown error has occurred.
  183. */
  184. int spongerng_init_from_dev_urandom (
  185. keccak_sponge_t sponge
  186. ) API_VIS WARN_UNUSED;
  187. /**
  188. * @brief Output bytes from a sponge-based CSPRNG.
  189. *
  190. * @param [inout] sponge The sponge object.
  191. * @param [out] out The output buffer.
  192. * @param [in] len The output buffer's length.
  193. */
  194. void spongerng_next (
  195. keccak_sponge_t sponge,
  196. uint8_t * __restrict__ out,
  197. size_t len
  198. ) API_VIS;
  199. /**
  200. * @brief Stir entropy data into a sponge-based CSPRNG from a buffer.
  201. *
  202. * @param [out] sponge The sponge object.
  203. * @param [in] in The entropy data.
  204. * @param [in] len The length of the initial data.
  205. */
  206. void spongerng_stir (
  207. keccak_sponge_t sponge,
  208. const uint8_t * __restrict__ in,
  209. size_t len
  210. ) API_VIS;
  211. #ifdef __cplusplus
  212. } /* extern "C" */
  213. #endif
  214. #undef API_VIS
  215. #undef WARN_UNUSED
  216. #endif /* __SHAKE_H__ */