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.
 
 
 
 
 

483 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_128 API_VIS;
  228. extern const struct kparams_s STROBE_256 API_VIS;
  229. extern const struct kparams_s STROBE_KEYED_128 API_VIS;
  230. extern const struct kparams_s STROBE_KEYED_256 API_VIS;
  231. /** TODO: remove this restriction?? */
  232. #define STROBE_MAX_AUTH_BYTES 255
  233. /** TODO: check "more" flags? */
  234. /**
  235. * @brief Initialize Strobe protocol context.
  236. * @param [out] The initialized strobe object.
  237. * @param [in] Strobe parameter descriptor
  238. * @param [in] am_client Nonzero if this party
  239. * is the client.
  240. */
  241. void strobe_init (
  242. keccak_sponge_t sponge,
  243. const struct kparams_s *params,
  244. uint8_t am_client
  245. ) NONNULL2 API_VIS;
  246. /**
  247. * @brief Send plaintext in strobe context.
  248. * @param [inout] The initialized strobe object.
  249. * @param [in] Strobe parameter descriptor
  250. * @param [in] in The plaintext.
  251. * @param [in] len The length of the plaintext.
  252. * @param [in] iSent Nonzero if this side of exchange sent the plaintext.
  253. * @param [in] more Nonzero if this is a continuation.
  254. * @retval DECAF_SUCCESS The operation applied successfully.
  255. * @retval DECAF_FAILURE The operation applied, but is dangerous
  256. * because it breaks the usual flow (by doing keyed operations
  257. * before a key is specified, or by specifying more when the previous
  258. * operation didn't match).
  259. */
  260. decaf_bool_t strobe_plaintext (
  261. keccak_sponge_t sponge,
  262. const unsigned char *in,
  263. size_t len,
  264. uint8_t iSent,
  265. uint8_t more
  266. ) NONNULL2 API_VIS;
  267. /**
  268. * @brief Report authenticated data in strobe context.
  269. * @param [inout] The initialized strobe object.
  270. * @param [in] Strobe parameter descriptor
  271. * @param [in] in The plaintext.
  272. * @param [in] len The length of the ad.
  273. * @param [in] more Nonzero if this is a continuation.
  274. * @retval DECAF_SUCCESS The operation applied successfully.
  275. * @retval DECAF_FAILURE The operation applied, but is dangerous
  276. * because it breaks the usual flow (by doing keyed operations
  277. * before a key is specified, or by specifying more when the previous
  278. * operation didn't match).
  279. */
  280. decaf_bool_t strobe_ad (
  281. keccak_sponge_t sponge,
  282. const unsigned char *in,
  283. size_t len,
  284. uint8_t more
  285. ) NONNULL2 API_VIS;
  286. /**
  287. * @brief Set nonce in strobe context.
  288. * @param [inout] The initialized strobe object.
  289. * @param [in] Strobe parameter descriptor
  290. * @param [in] in The nonce.
  291. * @param [in] len The length of the nonce.
  292. * @param [in] more Nonzero if this is a continuation.
  293. * @retval DECAF_SUCCESS The operation applied successfully.
  294. * @retval DECAF_FAILURE The operation applied, but is dangerous
  295. * because it breaks the usual flow (by doing keyed operations
  296. * before a key is specified, or by specifying more when the previous
  297. * operation didn't match).
  298. */
  299. decaf_bool_t strobe_nonce (
  300. keccak_sponge_t sponge,
  301. const unsigned char *in,
  302. size_t len,
  303. uint8_t more
  304. ) NONNULL2 API_VIS;
  305. /**
  306. * @brief Set key in strobe context.
  307. * @param [inout] The initialized strobe object.
  308. * @param [in] Strobe parameter descriptor
  309. * @param [in] in The key.
  310. * @param [in] len The length of the key.
  311. * @param [in] more Nonzero if this is a continuation.
  312. */
  313. decaf_bool_t strobe_key (
  314. keccak_sponge_t sponge,
  315. const unsigned char *in,
  316. size_t len,
  317. uint8_t more
  318. ) NONNULL2 API_VIS;
  319. /**
  320. * @brief Produce an authenticator.
  321. * @param [inout] strobe The Strobe protocol context
  322. * @param [out] out The authenticator
  323. * @param len The length, which must be no more than
  324. * @todo 32?
  325. * @retval DECAF_SUCCESS The operation applied successfully.
  326. * @retval DECAF_FAILURE The operation applied, but is dangerous
  327. * because it breaks the usual flow (by doing keyed operations
  328. * before a key is specified, or by specifying more when the previous
  329. * operation didn't match).
  330. */
  331. decaf_bool_t strobe_produce_auth (
  332. keccak_sponge_t sponge,
  333. unsigned char *out,
  334. size_t len
  335. ) NONNULL2 API_VIS;
  336. /**
  337. * @brief Encrypt bytes from in to out.
  338. * @warning Doesn't produce an auth tag (TODO?)
  339. * @param [inout] strobe The Strobe protocol context.
  340. * @param [in] in The plaintext.
  341. * @param [out] out The ciphertext.
  342. * @param [in] len The length of plaintext and ciphertext.
  343. * @param [in] more This is a continuation.
  344. * @retval DECAF_SUCCESS The operation applied successfully.
  345. * @retval DECAF_FAILURE The operation applied, but is dangerous
  346. * because it breaks the usual flow (by doing keyed operations
  347. * before a key is specified, or by specifying more when the previous
  348. * operation didn't match).
  349. */
  350. decaf_bool_t strobe_encrypt (
  351. keccak_sponge_t sponge,
  352. unsigned char *out,
  353. const unsigned char *in,
  354. size_t len,
  355. uint8_t more
  356. ) NONNULL3 API_VIS;
  357. /**
  358. * @brief Decrypt bytes from in to out.
  359. * @warning Doesn't check an auth tag (TODO?)
  360. * @param [inout] strobe The Strobe protocol context.
  361. * @param [in] in The ciphertext.
  362. * @param [out] out The plaintext.
  363. * @param [in] len The length of plaintext and ciphertext.
  364. * @param [in] more This is a continuation.
  365. * @retval DECAF_SUCCESS The operation applied successfully.
  366. * @retval DECAF_FAILURE The operation applied, but is dangerous
  367. * because it breaks the usual flow (by doing keyed operations
  368. * before a key is specified, or by specifying more when the previous
  369. * operation didn't match).
  370. */
  371. decaf_bool_t strobe_decrypt (
  372. keccak_sponge_t sponge,
  373. unsigned char *out,
  374. const unsigned char *in,
  375. size_t len,
  376. uint8_t more
  377. ) NONNULL3 API_VIS;
  378. /**
  379. * @brief Produce a session-bound pseudorandom value.
  380. *
  381. * @warning This "prng" value is NOT suitable for
  382. * refreshing forward secrecy! It's to replace things
  383. * like TCP session hash.
  384. *
  385. * @todo Figure out how to treat this wrt anti-rollback.
  386. *
  387. * @param [inout] strobe The Strobe protocol context
  388. * @param [out] out The authenticator
  389. * @param len The length.
  390. *
  391. * @retval DECAF_SUCCESS The operation applied successfully.
  392. * @retval DECAF_FAILURE The operation applied, but is dangerous
  393. * because it breaks the usual flow (by doing keyed operations
  394. * before a key is specified, or by specifying more when the previous
  395. * operation didn't match).
  396. */
  397. decaf_bool_t strobe_prng (
  398. keccak_sponge_t sponge,
  399. unsigned char *out,
  400. size_t len,
  401. uint8_t more
  402. ) NONNULL2 API_VIS;
  403. /**
  404. * @brief Verify an authenticator.
  405. * @param [inout] strobe The Strobe protocol context
  406. * @param [in] in The authenticator
  407. * @param len The length, which must be no more than
  408. * @todo 32?
  409. * @retval DECAF_SUCCESS The operation applied successfully.
  410. * @retval DECAF_FAILURE The operation failed because of a
  411. * bad validator (or because you aren't keyed)
  412. */
  413. decaf_bool_t strobe_verify_auth (
  414. keccak_sponge_t sponge,
  415. const unsigned char *in,
  416. size_t len
  417. ) WARN_UNUSED NONNULL2 API_VIS;
  418. /**
  419. * @brief Respecify Strobe protocol object's crypto.
  420. * @param [inout] The initialized strobe context.
  421. * @param [in] Strobe parameter descriptor
  422. * @param [in] am_client Nonzero if this party
  423. * is the client.
  424. * @retval DECAF_SUCCESS The operation applied successfully.
  425. * @retval DECAF_FAILURE The operation failed because of a
  426. * bad validator (or because you aren't keyed)
  427. */
  428. decaf_bool_t strobe_respec (
  429. keccak_sponge_t sponge,
  430. const struct kparams_s *params
  431. ) NONNULL2 API_VIS;
  432. #ifdef __cplusplus
  433. } /* extern "C" */
  434. #endif
  435. #undef API_VIS
  436. #undef WARN_UNUSED
  437. #undef NONNULL1
  438. #undef NONNULL13
  439. #undef NONNULL2
  440. #undef NONNULL3
  441. #endif /* __SHAKE_H__ */