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.
 
 
 
 
 

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