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.
 
 
 
 
 

340 lines
9.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. #include "decaf.h" /* TODO: orly? */
  16. /* TODO: unify with other headers (maybe all into one??); add nonnull attributes */
  17. /** @cond internal */
  18. #define API_VIS __attribute__((visibility("default")))
  19. #define WARN_UNUSED __attribute__((warn_unused_result))
  20. /** @endcond */
  21. /* TODO: different containing structs for each primitive? */
  22. #ifndef INTERNAL_SPONGE_STRUCT
  23. /** Sponge container object for the various primitives. */
  24. typedef struct keccak_sponge_s {
  25. /** @cond internal */
  26. uint64_t opaque[26];
  27. /** @endcond */
  28. } keccak_sponge_t[1];
  29. struct kparams_s;
  30. typedef struct { uint64_t opaque; } strobe_params_t[1];
  31. #endif
  32. typedef struct strobe_s {
  33. keccak_sponge_t sponge;
  34. strobe_params_t params;
  35. } strobe_s, strobe_t[1];
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. /**
  40. * @brief Initialize a sponge context object.
  41. * @param [out] sponge The object to initialize.
  42. * @param [in] params The sponge's parameter description.
  43. */
  44. void sponge_init (
  45. keccak_sponge_t sponge,
  46. const struct kparams_s *params
  47. ) API_VIS;
  48. /**
  49. * @brief Absorb data into a SHA3 or SHAKE hash context.
  50. * @param [inout] sponge The context.
  51. * @param [in] in The input data.
  52. * @param [in] len The input data's length in bytes.
  53. */
  54. void sha3_update (
  55. struct keccak_sponge_s * __restrict__ sponge,
  56. const uint8_t *in,
  57. size_t len
  58. ) API_VIS;
  59. /**
  60. * @brief Squeeze output data from a SHA3 or SHAKE hash context.
  61. * This does not destroy or re-initialize the hash context, and
  62. * sha3 output can be called more times.
  63. *
  64. * @param [inout] sponge The context.
  65. * @param [out] out The output data.
  66. * @param [in] len The requested output data length in bytes.
  67. */
  68. void sha3_output (
  69. keccak_sponge_t sponge,
  70. uint8_t * __restrict__ out,
  71. size_t len
  72. ) API_VIS;
  73. /**
  74. * @brief Return the default output length of the sponge construction,
  75. * for the purpose of C++ default operators.
  76. *
  77. * Returns n/8 for SHA3-n and 2n/8 for SHAKE-n.
  78. *
  79. * @param [inout] sponge The context.
  80. */
  81. size_t sponge_default_output_bytes (
  82. const keccak_sponge_t sponge
  83. ) API_VIS;
  84. /**
  85. * @brief Destroy a SHA3 or SHAKE sponge context by overwriting it with 0.
  86. * @param [out] sponge The context.
  87. */
  88. void sponge_destroy (
  89. keccak_sponge_t sponge
  90. ) API_VIS;
  91. /**
  92. * @brief Hash (in) to (out)
  93. * @param [in] in The input data.
  94. * @param [in] inlen The length of the input data.
  95. * @param [out] out A buffer for the output data.
  96. * @param [in] outlen The length of the output data.
  97. * @param [in] params The parameters of the sponge hash.
  98. */
  99. void sponge_hash (
  100. const uint8_t *in,
  101. size_t inlen,
  102. uint8_t *out,
  103. size_t outlen,
  104. const struct kparams_s *params
  105. ) API_VIS;
  106. /* TODO: expand/doxygenate individual SHAKE/SHA3 instances? */
  107. /** @cond internal */
  108. #define DECSHAKE(n) \
  109. extern const struct kparams_s SHAKE##n##_params_s API_VIS; \
  110. static inline void shake##n##_init(keccak_sponge_t sponge) { \
  111. sponge_init(sponge, &SHAKE##n##_params_s); \
  112. } \
  113. static inline void shake##n##_update(keccak_sponge_t sponge, const uint8_t *in, size_t inlen ) { \
  114. sha3_update(sponge, in, inlen); \
  115. } \
  116. static inline void shake##n##_final(keccak_sponge_t sponge, uint8_t *out, size_t outlen ) { \
  117. sha3_output(sponge, out, outlen); \
  118. sponge_init(sponge, &SHAKE##n##_params_s); \
  119. } \
  120. static inline void shake##n##_hash(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen) { \
  121. sponge_hash(in,inlen,out,outlen,&SHAKE##n##_params_s); \
  122. } \
  123. static inline void shake##n##_destroy( keccak_sponge_t sponge ) { \
  124. sponge_destroy(sponge); \
  125. }
  126. #define DECSHA3(n) \
  127. extern const struct kparams_s SHA3_##n##_params_s API_VIS; \
  128. static inline void sha3_##n##_init(keccak_sponge_t sponge) { \
  129. sponge_init(sponge, &SHA3_##n##_params_s); \
  130. } \
  131. static inline void sha3_##n##_update(keccak_sponge_t sponge, const uint8_t *in, size_t inlen ) { \
  132. sha3_update(sponge, in, inlen); \
  133. } \
  134. static inline void sha3_##n##_final(keccak_sponge_t sponge, uint8_t *out, size_t outlen ) { \
  135. sha3_output(sponge, out, outlen); \
  136. sponge_init(sponge, &SHA3_##n##_params_s); \
  137. } \
  138. static inline void sha3_##n##_hash(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen) { \
  139. sponge_hash(in,inlen,out,outlen,&SHA3_##n##_params_s); \
  140. } \
  141. static inline void sha3_##n##_destroy( keccak_sponge_t sponge ) { \
  142. sponge_destroy(sponge); \
  143. }
  144. /** @endcond */
  145. DECSHAKE(128)
  146. DECSHAKE(256)
  147. DECSHA3(224)
  148. DECSHA3(256)
  149. DECSHA3(384)
  150. DECSHA3(512)
  151. /**
  152. * @brief Initialize a sponge-based CSPRNG from a buffer.
  153. *
  154. * @param [out] sponge The sponge object.
  155. * @param [in] in The initial data.
  156. * @param [in] len The length of the initial data.
  157. * @param [in] deterministic If zero, allow RNG to stir in nondeterministic
  158. * data from RDRAND or RDTSC.
  159. */
  160. void spongerng_init_from_buffer (
  161. keccak_sponge_t sponge,
  162. const uint8_t * __restrict__ in,
  163. size_t len,
  164. int deterministic
  165. ) API_VIS;
  166. /* FIXME!! This interface has the opposite retval convention from other functions
  167. * in the library. (0=success). Should they be harmonized?
  168. */
  169. /**
  170. * @brief Initialize a sponge-based CSPRNG from a file.
  171. *
  172. * @param [out] sponge The sponge object.
  173. * @param [in] file A name of a file containing initial data.
  174. * @param [in] len The length of the initial data. Must be positive.
  175. * @param [in] deterministic If zero, allow RNG to stir in nondeterministic
  176. * data from RDRAND or RDTSC.
  177. *
  178. * @retval 0 Success.
  179. * @retval positive An error has occurred, and this was the errno.
  180. * @retval -1 An unknown error has occurred.
  181. * @retval -2 len was 0.
  182. */
  183. int spongerng_init_from_file (
  184. keccak_sponge_t sponge,
  185. const char *file,
  186. size_t len,
  187. int deterministic
  188. ) API_VIS WARN_UNUSED;
  189. /* FIXME!! This interface has the opposite retval convention from other functions
  190. * in the library. (0=success). Should they be harmonized?
  191. */
  192. /**
  193. * @brief Initialize a nondeterministic sponge-based CSPRNG from /dev/urandom.
  194. *
  195. * @param [out] sponge The sponge object.
  196. *
  197. * @retval 0 Success.
  198. * @retval positive An error has occurred, and this was the errno.
  199. * @retval -1 An unknown error has occurred.
  200. */
  201. int spongerng_init_from_dev_urandom (
  202. keccak_sponge_t sponge
  203. ) API_VIS WARN_UNUSED;
  204. /**
  205. * @brief Output bytes from a sponge-based CSPRNG.
  206. *
  207. * @param [inout] sponge The sponge object.
  208. * @param [out] out The output buffer.
  209. * @param [in] len The output buffer's length.
  210. */
  211. void spongerng_next (
  212. keccak_sponge_t sponge,
  213. uint8_t * __restrict__ out,
  214. size_t len
  215. ) API_VIS;
  216. /**
  217. * @brief Stir entropy data into a sponge-based CSPRNG from a buffer.
  218. *
  219. * @param [out] sponge The sponge object.
  220. * @param [in] in The entropy data.
  221. * @param [in] len The length of the initial data.
  222. */
  223. void spongerng_stir (
  224. keccak_sponge_t sponge,
  225. const uint8_t * __restrict__ in,
  226. size_t len
  227. ) API_VIS;
  228. /**
  229. * @brief Initialize Strobe protocol context.
  230. * @param [out] The initialized strobe object.
  231. * @param [in] Strobe parameter descriptor
  232. * @param [in] am_client Nonzero if this party
  233. * is the client.
  234. */
  235. void strobe_init(
  236. strobe_t strobe,
  237. const struct kparams_s *params,
  238. uint8_t am_client
  239. );
  240. /**
  241. * @brief Produce an authenticator.
  242. * @param [inout] strobe The Strobe protocol context
  243. * @param [out] out The authenticator
  244. * @param len The length, which must be no more than
  245. * @todo 32?
  246. */
  247. void strobe_produce_auth (
  248. strobe_t strobe,
  249. unsigned char *out,
  250. size_t len
  251. );
  252. /**
  253. * @brief Produce a session-bound pseudorandom value.
  254. *
  255. * @warning This "prng" value is NOT suitable for
  256. * refreshing forward secrecy! It's to replace things
  257. * like TCP session hash.
  258. *
  259. * @todo Figure out how to treat this wrt anti-rollback.
  260. *
  261. * @param [inout] strobe The Strobe protocol context
  262. * @param [out] out The authenticator
  263. * @param len The length.
  264. */
  265. void strobe_prng (
  266. strobe_t strobe,
  267. unsigned char *out,
  268. size_t len
  269. );
  270. /**
  271. * @brief Verify an authenticator.
  272. * @param [inout] strobe The Strobe protocol context
  273. * @param [in] in The authenticator
  274. * @param len The length, which must be no more than
  275. * @todo 32?
  276. */
  277. decaf_bool_t strobe_verify_auth (
  278. strobe_t strobe,
  279. const unsigned char *in,
  280. size_t len
  281. );
  282. /**
  283. * @brief Respecify Strobe protocol object's crypto.
  284. * @param [inout] The initialized strobe context.
  285. * @param [in] Strobe parameter descriptor
  286. * @param [in] am_client Nonzero if this party
  287. * is the client.
  288. */
  289. void strobe_respec (
  290. strobe_t strobe,
  291. const struct kparams_s *params
  292. );
  293. /**
  294. * @brief Destroy a Strobe context.
  295. * @param [out] strobe The object to destroy.
  296. */
  297. void strobe_destroy (
  298. strobe_t strobe
  299. );
  300. #ifdef __cplusplus
  301. } /* extern "C" */
  302. #endif
  303. #undef API_VIS
  304. #undef WARN_UNUSED
  305. #endif /* __SHAKE_H__ */