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