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.
 
 
 
 
 

482 lines
15 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_common.h"
  16. /** @cond internal */
  17. #define API_VIS __attribute__((visibility("default")))
  18. #define WARN_UNUSED __attribute__((warn_unused_result))
  19. #define NONNULL1 __attribute__((nonnull(1)))
  20. #define NONNULL2 __attribute__((nonnull(1,2)))
  21. #define NONNULL13 __attribute__((nonnull(1,3)))
  22. #define NONNULL3 __attribute__((nonnull(1,2,3)))
  23. /** @endcond */
  24. /* TODO: different containing structs for each primitive? */
  25. #ifndef INTERNAL_SPONGE_STRUCT
  26. /** Sponge container object for the various primitives. */
  27. typedef struct keccak_sponge_s {
  28. /** @cond internal */
  29. uint64_t opaque[26];
  30. /** @endcond */
  31. } keccak_sponge_t[1];
  32. struct kparams_s;
  33. #endif
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. /**
  38. * @brief Initialize a sponge context object.
  39. * @param [out] sponge The object to initialize.
  40. * @param [in] params The sponge's parameter description.
  41. */
  42. void sponge_init (
  43. keccak_sponge_t sponge,
  44. const struct kparams_s *params
  45. ) API_VIS;
  46. /**
  47. * @brief Absorb data into a SHA3 or SHAKE hash context.
  48. * @param [inout] sponge The context.
  49. * @param [in] in The input data.
  50. * @param [in] len The input data's length in bytes.
  51. */
  52. void sha3_update (
  53. struct keccak_sponge_s * __restrict__ sponge,
  54. const uint8_t *in,
  55. size_t len
  56. ) API_VIS;
  57. /**
  58. * @brief Squeeze output data from a SHA3 or SHAKE hash context.
  59. * This does not destroy or re-initialize the hash context, and
  60. * sha3 output can be called more times.
  61. *
  62. * @param [inout] sponge The context.
  63. * @param [out] out The output data.
  64. * @param [in] len The requested output data length in bytes.
  65. */
  66. void sha3_output (
  67. keccak_sponge_t sponge,
  68. uint8_t * __restrict__ out,
  69. size_t len
  70. ) API_VIS;
  71. /**
  72. * @brief Return the default output length of the sponge construction,
  73. * for the purpose of C++ default operators.
  74. *
  75. * Returns n/8 for SHA3-n and 2n/8 for SHAKE-n.
  76. *
  77. * @param [inout] sponge The context.
  78. */
  79. size_t sponge_default_output_bytes (
  80. const keccak_sponge_t sponge
  81. ) API_VIS;
  82. /**
  83. * @brief Destroy a SHA3 or SHAKE sponge context by overwriting it with 0.
  84. * @param [out] sponge The context.
  85. */
  86. void sponge_destroy (
  87. keccak_sponge_t sponge
  88. ) API_VIS;
  89. /**
  90. * @brief Hash (in) to (out)
  91. * @param [in] in The input data.
  92. * @param [in] inlen The length of the input data.
  93. * @param [out] out A buffer for the output data.
  94. * @param [in] outlen The length of the output data.
  95. * @param [in] params The parameters of the sponge hash.
  96. */
  97. void sponge_hash (
  98. const uint8_t *in,
  99. size_t inlen,
  100. uint8_t *out,
  101. size_t outlen,
  102. const struct kparams_s *params
  103. ) API_VIS;
  104. /* FUTURE: expand/doxygenate individual SHAKE/SHA3 instances? */
  105. /** @cond internal */
  106. #define DECSHAKE(n) \
  107. extern const struct kparams_s SHAKE##n##_params_s API_VIS; \
  108. static inline void NONNULL1 shake##n##_init(keccak_sponge_t sponge) { \
  109. sponge_init(sponge, &SHAKE##n##_params_s); \
  110. } \
  111. static inline void NONNULL2 shake##n##_update(keccak_sponge_t sponge, const uint8_t *in, size_t inlen ) { \
  112. sha3_update(sponge, in, inlen); \
  113. } \
  114. static inline void NONNULL2 shake##n##_final(keccak_sponge_t sponge, uint8_t *out, size_t outlen ) { \
  115. sha3_output(sponge, out, outlen); \
  116. sponge_init(sponge, &SHAKE##n##_params_s); \
  117. } \
  118. static inline void NONNULL13 shake##n##_hash(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen) { \
  119. sponge_hash(in,inlen,out,outlen,&SHAKE##n##_params_s); \
  120. } \
  121. static inline void NONNULL1 shake##n##_destroy( keccak_sponge_t sponge ) { \
  122. sponge_destroy(sponge); \
  123. }
  124. #define DECSHA3(n) \
  125. extern const struct kparams_s SHA3_##n##_params_s API_VIS; \
  126. static inline void NONNULL1 sha3_##n##_init(keccak_sponge_t sponge) { \
  127. sponge_init(sponge, &SHA3_##n##_params_s); \
  128. } \
  129. static inline void NONNULL2 sha3_##n##_update(keccak_sponge_t sponge, const uint8_t *in, size_t inlen ) { \
  130. sha3_update(sponge, in, inlen); \
  131. } \
  132. static inline void NONNULL2 sha3_##n##_final(keccak_sponge_t sponge, uint8_t *out, size_t outlen ) { \
  133. sha3_output(sponge, out, outlen); \
  134. sponge_init(sponge, &SHA3_##n##_params_s); \
  135. } \
  136. static inline void NONNULL13 sha3_##n##_hash(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen) { \
  137. sponge_hash(in,inlen,out,outlen,&SHA3_##n##_params_s); \
  138. } \
  139. static inline void NONNULL1 sha3_##n##_destroy( keccak_sponge_t sponge ) { \
  140. sponge_destroy(sponge); \
  141. }
  142. /** @endcond */
  143. DECSHAKE(128)
  144. DECSHAKE(256)
  145. DECSHA3(224)
  146. DECSHA3(256)
  147. DECSHA3(384)
  148. DECSHA3(512)
  149. /**
  150. * @brief Initialize a sponge-based CSPRNG from a buffer.
  151. *
  152. * @param [out] sponge The sponge object.
  153. * @param [in] in The initial data.
  154. * @param [in] len The length of the initial data.
  155. * @param [in] deterministic If zero, allow RNG to stir in nondeterministic
  156. * data from RDRAND or RDTSC.
  157. */
  158. void spongerng_init_from_buffer (
  159. keccak_sponge_t sponge,
  160. const uint8_t * __restrict__ in,
  161. size_t len,
  162. int deterministic
  163. ) NONNULL2 API_VIS;
  164. /* FIXME!! This interface has the opposite retval convention from other functions
  165. * in the library. (0=success). Should they be harmonized?
  166. */
  167. /**
  168. * @brief Initialize a sponge-based CSPRNG from a file.
  169. *
  170. * @param [out] sponge The sponge object.
  171. * @param [in] file A name of a file containing initial data.
  172. * @param [in] len The length of the initial data. Must be positive.
  173. * @param [in] deterministic If zero, allow RNG to stir in nondeterministic
  174. * data from RDRAND or RDTSC.
  175. *
  176. * @retval 0 Success.
  177. * @retval positive An error has occurred, and this was the errno.
  178. * @retval -1 An unknown error has occurred.
  179. * @retval -2 len was 0.
  180. */
  181. int spongerng_init_from_file (
  182. keccak_sponge_t sponge,
  183. const char *file,
  184. size_t len,
  185. int deterministic
  186. ) NONNULL2 API_VIS WARN_UNUSED;
  187. /* FIXME!! This interface has the opposite retval convention from other functions
  188. * in the library. (0=success). Should they be harmonized?
  189. */
  190. /**
  191. * @brief Initialize a nondeterministic sponge-based CSPRNG from /dev/urandom.
  192. *
  193. * @param [out] sponge The sponge object.
  194. *
  195. * @retval 0 Success.
  196. * @retval positive An error has occurred, and this was the errno.
  197. * @retval -1 An unknown error has occurred.
  198. */
  199. int spongerng_init_from_dev_urandom (
  200. keccak_sponge_t sponge
  201. ) API_VIS WARN_UNUSED;
  202. /**
  203. * @brief Output bytes from a sponge-based CSPRNG.
  204. *
  205. * @param [inout] sponge The sponge object.
  206. * @param [out] out The output buffer.
  207. * @param [in] len The output buffer's length.
  208. */
  209. void spongerng_next (
  210. keccak_sponge_t sponge,
  211. uint8_t * __restrict__ out,
  212. size_t len
  213. ) API_VIS;
  214. /**
  215. * @brief Stir entropy data into a sponge-based CSPRNG from a buffer.
  216. *
  217. * @param [out] sponge The sponge object.
  218. * @param [in] in The entropy data.
  219. * @param [in] len The length of the initial data.
  220. */
  221. void spongerng_stir (
  222. keccak_sponge_t sponge,
  223. const uint8_t * __restrict__ in,
  224. size_t len
  225. ) NONNULL2 API_VIS;
  226. extern const struct kparams_s STROBE_128 API_VIS;
  227. extern const struct kparams_s STROBE_256 API_VIS;
  228. extern const struct kparams_s STROBE_KEYED_128 API_VIS;
  229. extern const struct kparams_s STROBE_KEYED_256 API_VIS;
  230. /** TODO: remove this restriction?? */
  231. #define STROBE_MAX_AUTH_BYTES 255
  232. /** TODO: check "more" flags? */
  233. /**
  234. * @brief Initialize Strobe protocol context.
  235. * @param [out] The initialized strobe object.
  236. * @param [in] Strobe parameter descriptor
  237. * @param [in] am_client Nonzero if this party
  238. * is the client.
  239. */
  240. void strobe_init (
  241. keccak_sponge_t sponge,
  242. const struct kparams_s *params,
  243. uint8_t am_client
  244. ) NONNULL2 API_VIS;
  245. /**
  246. * @brief Send plaintext in strobe context.
  247. * @param [inout] The initialized strobe object.
  248. * @param [in] Strobe parameter descriptor
  249. * @param [in] in The plaintext.
  250. * @param [in] len The length of the plaintext.
  251. * @param [in] iSent Nonzero if this side of exchange sent the plaintext.
  252. * @param [in] more Nonzero if this is a continuation.
  253. * @retval DECAF_SUCCESS The operation applied successfully.
  254. * @retval DECAF_FAILURE The operation applied, but is dangerous
  255. * because it breaks the usual flow (by doing keyed operations
  256. * before a key is specified, or by specifying more when the previous
  257. * operation didn't match).
  258. */
  259. decaf_bool_t strobe_plaintext (
  260. keccak_sponge_t sponge,
  261. const unsigned char *in,
  262. size_t len,
  263. uint8_t iSent,
  264. uint8_t more
  265. ) NONNULL2 API_VIS;
  266. /**
  267. * @brief Report authenticated data in strobe context.
  268. * @param [inout] The initialized strobe object.
  269. * @param [in] Strobe parameter descriptor
  270. * @param [in] in The plaintext.
  271. * @param [in] len The length of the ad.
  272. * @param [in] more Nonzero if this is a continuation.
  273. * @retval DECAF_SUCCESS The operation applied successfully.
  274. * @retval DECAF_FAILURE The operation applied, but is dangerous
  275. * because it breaks the usual flow (by doing keyed operations
  276. * before a key is specified, or by specifying more when the previous
  277. * operation didn't match).
  278. */
  279. decaf_bool_t strobe_ad (
  280. keccak_sponge_t sponge,
  281. const unsigned char *in,
  282. size_t len,
  283. uint8_t more
  284. ) NONNULL2 API_VIS;
  285. /**
  286. * @brief Set nonce in strobe context.
  287. * @param [inout] The initialized strobe object.
  288. * @param [in] Strobe parameter descriptor
  289. * @param [in] in The nonce.
  290. * @param [in] len The length of the nonce.
  291. * @param [in] more Nonzero if this is a continuation.
  292. * @retval DECAF_SUCCESS The operation applied successfully.
  293. * @retval DECAF_FAILURE The operation applied, but is dangerous
  294. * because it breaks the usual flow (by doing keyed operations
  295. * before a key is specified, or by specifying more when the previous
  296. * operation didn't match).
  297. */
  298. decaf_bool_t strobe_nonce (
  299. keccak_sponge_t sponge,
  300. const unsigned char *in,
  301. size_t len,
  302. uint8_t more
  303. ) NONNULL2 API_VIS;
  304. /**
  305. * @brief Set key in strobe context.
  306. * @param [inout] The initialized strobe object.
  307. * @param [in] Strobe parameter descriptor
  308. * @param [in] in The key.
  309. * @param [in] len The length of the key.
  310. * @param [in] more Nonzero if this is a continuation.
  311. */
  312. decaf_bool_t strobe_key (
  313. keccak_sponge_t sponge,
  314. const unsigned char *in,
  315. size_t len,
  316. uint8_t more
  317. ) NONNULL2 API_VIS;
  318. /**
  319. * @brief Produce an authenticator.
  320. * @param [inout] strobe The Strobe protocol context
  321. * @param [out] out The authenticator
  322. * @param len The length, which must be no more than
  323. * @todo 32?
  324. * @retval DECAF_SUCCESS The operation applied successfully.
  325. * @retval DECAF_FAILURE The operation applied, but is dangerous
  326. * because it breaks the usual flow (by doing keyed operations
  327. * before a key is specified, or by specifying more when the previous
  328. * operation didn't match).
  329. */
  330. decaf_bool_t strobe_produce_auth (
  331. keccak_sponge_t sponge,
  332. unsigned char *out,
  333. size_t len
  334. ) NONNULL2 API_VIS;
  335. /**
  336. * @brief Encrypt bytes from in to out.
  337. * @warning Doesn't produce an auth tag (TODO?)
  338. * @param [inout] strobe The Strobe protocol context.
  339. * @param [in] in The plaintext.
  340. * @param [out] out The ciphertext.
  341. * @param [in] len The length of plaintext and ciphertext.
  342. * @param [in] more This is a continuation.
  343. * @retval DECAF_SUCCESS The operation applied successfully.
  344. * @retval DECAF_FAILURE The operation applied, but is dangerous
  345. * because it breaks the usual flow (by doing keyed operations
  346. * before a key is specified, or by specifying more when the previous
  347. * operation didn't match).
  348. */
  349. decaf_bool_t strobe_encrypt (
  350. keccak_sponge_t sponge,
  351. unsigned char *out,
  352. const unsigned char *in,
  353. size_t len,
  354. uint8_t more
  355. ) NONNULL3 API_VIS;
  356. /**
  357. * @brief Decrypt bytes from in to out.
  358. * @warning Doesn't check an auth tag (TODO?)
  359. * @param [inout] strobe The Strobe protocol context.
  360. * @param [in] in The ciphertext.
  361. * @param [out] out The plaintext.
  362. * @param [in] len The length of plaintext and ciphertext.
  363. * @param [in] more This is a continuation.
  364. * @retval DECAF_SUCCESS The operation applied successfully.
  365. * @retval DECAF_FAILURE The operation applied, but is dangerous
  366. * because it breaks the usual flow (by doing keyed operations
  367. * before a key is specified, or by specifying more when the previous
  368. * operation didn't match).
  369. */
  370. decaf_bool_t strobe_decrypt (
  371. keccak_sponge_t sponge,
  372. unsigned char *out,
  373. const unsigned char *in,
  374. size_t len,
  375. uint8_t more
  376. ) NONNULL3 API_VIS;
  377. /**
  378. * @brief Produce a session-bound pseudorandom value.
  379. *
  380. * @warning This "prng" value is NOT suitable for
  381. * refreshing forward secrecy! It's to replace things
  382. * like TCP session hash.
  383. *
  384. * @todo Figure out how to treat this wrt anti-rollback.
  385. *
  386. * @param [inout] strobe The Strobe protocol context
  387. * @param [out] out The authenticator
  388. * @param len The length.
  389. *
  390. * @retval DECAF_SUCCESS The operation applied successfully.
  391. * @retval DECAF_FAILURE The operation applied, but is dangerous
  392. * because it breaks the usual flow (by doing keyed operations
  393. * before a key is specified, or by specifying more when the previous
  394. * operation didn't match).
  395. */
  396. decaf_bool_t strobe_prng (
  397. keccak_sponge_t sponge,
  398. unsigned char *out,
  399. size_t len,
  400. uint8_t more
  401. ) NONNULL2 API_VIS;
  402. /**
  403. * @brief Verify an authenticator.
  404. * @param [inout] strobe The Strobe protocol context
  405. * @param [in] in The authenticator
  406. * @param len The length, which must be no more than
  407. * @todo 32?
  408. * @retval DECAF_SUCCESS The operation applied successfully.
  409. * @retval DECAF_FAILURE The operation failed because of a
  410. * bad validator (or because you aren't keyed)
  411. */
  412. decaf_bool_t strobe_verify_auth (
  413. keccak_sponge_t sponge,
  414. const unsigned char *in,
  415. size_t len
  416. ) WARN_UNUSED NONNULL2 API_VIS;
  417. /**
  418. * @brief Respecify Strobe protocol object's crypto.
  419. * @param [inout] The initialized strobe context.
  420. * @param [in] Strobe parameter descriptor
  421. * @param [in] am_client Nonzero if this party
  422. * is the client.
  423. * @retval DECAF_SUCCESS The operation applied successfully.
  424. * @retval DECAF_FAILURE The operation failed because of a
  425. * bad validator (or because you aren't keyed)
  426. */
  427. decaf_bool_t strobe_respec (
  428. keccak_sponge_t sponge,
  429. const struct kparams_s *params
  430. ) NONNULL2 API_VIS;
  431. #ifdef __cplusplus
  432. } /* extern "C" */
  433. #endif
  434. #undef API_VIS
  435. #undef WARN_UNUSED
  436. #undef NONNULL1
  437. #undef NONNULL13
  438. #undef NONNULL2
  439. #undef NONNULL3
  440. #endif /* __SHAKE_H__ */