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.
 
 
 
 
 

372 lines
12 KiB

  1. /* Copyright (c) 2014 Cryptography Research, Inc.
  2. * Released under the MIT License. See LICENSE.txt for license information.
  3. */
  4. /**
  5. * @file goldilocks.h
  6. * @author Mike Hamburg
  7. * @brief Goldilocks high-level functions.
  8. */
  9. #ifndef __GOLDILOCKS_H__
  10. #define __GOLDILOCKS_H__ 1
  11. #include <stdint.h>
  12. #ifndef GOLDI_IMPLEMENT_PRECOMPUTED_KEYS
  13. /** If nonzero, implement precomputation for verify and ECDH. */
  14. #define GOLDI_IMPLEMENT_PRECOMPUTED_KEYS 1
  15. #endif
  16. /** The size of the Goldilocks field, in bits. */
  17. #define GOLDI_FIELD_BITS 448
  18. /** The size of the Goldilocks scalars, in bits. */
  19. #define GOLDI_SCALAR_BITS 446
  20. /** The same size, in bytes. */
  21. #define GOLDI_FIELD_BYTES (GOLDI_FIELD_BITS/8)
  22. /** The size of a Goldilocks public key, in bytes. */
  23. #define GOLDI_PUBLIC_KEY_BYTES GOLDI_FIELD_BYTES
  24. /** The extra bytes in a Goldilocks private key for the symmetric key. */
  25. #define GOLDI_SYMKEY_BYTES 32
  26. /** The size of a shared secret. */
  27. #define GOLDI_SHARED_SECRET_BYTES 64
  28. /** The size of a Goldilocks private key, in bytes. */
  29. #define GOLDI_PRIVATE_KEY_BYTES (2*GOLDI_FIELD_BYTES + GOLDI_SYMKEY_BYTES)
  30. /** The size of a Goldilocks signature, in bytes. */
  31. #define GOLDI_SIGNATURE_BYTES (2*GOLDI_FIELD_BYTES)
  32. /**
  33. * @brief Serialized form of a Goldilocks public key.
  34. *
  35. * @warning This isn't even my final form!
  36. */
  37. struct goldilocks_public_key_t {
  38. uint8_t opaque[GOLDI_PUBLIC_KEY_BYTES]; /**< Serialized data. */
  39. };
  40. /**
  41. * @brief Serialized form of a Goldilocks private key.
  42. *
  43. * Contains 56 bytes of actual private key, 56 bytes of
  44. * public key, and 32 bytes of symmetric key for randomization.
  45. *
  46. * @warning This isn't even my final form!
  47. */
  48. struct goldilocks_private_key_t {
  49. uint8_t opaque[GOLDI_PRIVATE_KEY_BYTES]; /**< Serialized data. */
  50. };
  51. #ifdef __cplusplus
  52. extern "C" {
  53. #endif
  54. /** @brief No error. */
  55. static const int GOLDI_EOK = 0;
  56. /** @brief Error: your key or other state is corrupt. */
  57. static const int GOLDI_ECORRUPT = 44801;
  58. /** @brief Error: other party's key is corrupt. */
  59. static const int GOLDI_EINVAL = 44802;
  60. /** @brief Error: not enough entropy. */
  61. static const int GOLDI_ENODICE = 44804;
  62. /** @brief Error: you need to initialize the library first. */
  63. static const int GOLDI_EUNINIT = 44805;
  64. /** @brief Error: called init() but we are already initialized. */
  65. static const int GOLDI_EALREADYINIT = 44805;
  66. /**
  67. * @brief Initialize Goldilocks' precomputed tables and
  68. * random number generator. This function must be called before
  69. * any of the other Goldilocks routines (except
  70. * goldilocks_shared_secret in the current version) and should be
  71. * called only once per process.
  72. *
  73. * There is currently no way to tear down this state. It is possible
  74. * that a future version of this library will not require this function.
  75. *
  76. * @retval GOLDI_EOK Success.
  77. * @retval GOLDI_EALREADYINIT Already initialized.
  78. * @retval GOLDI_ECORRUPT Memory is corrupted, or another thread is already init'ing.
  79. * @retval Nonzero An error occurred.
  80. */
  81. int
  82. goldilocks_init (void)
  83. __attribute__((warn_unused_result,visibility ("default")));
  84. /**
  85. * @brief Generate a new random keypair.
  86. * @param [out] privkey The generated private key.
  87. * @param [out] pubkey The generated public key.
  88. *
  89. * @warning This isn't even my final form!
  90. *
  91. * @retval GOLDI_EOK Success.
  92. * @retval GOLDI_ENODICE Insufficient entropy.
  93. * @retval GOLDI_EUNINIT You must call goldilocks_init() first.
  94. */
  95. int
  96. goldilocks_keygen (
  97. struct goldilocks_private_key_t *privkey,
  98. struct goldilocks_public_key_t *pubkey
  99. ) __attribute__((warn_unused_result,nonnull(1,2),visibility ("default")));
  100. /**
  101. * @brief Derive a key from its compressed form.
  102. * @param [out] privkey The derived private key.
  103. * @param [in] proto The compressed or proto-key, which must be 32 random bytes.
  104. *
  105. * @warning This isn't even my final form!
  106. *
  107. * @retval GOLDI_EOK Success.
  108. * @retval GOLDI_EUNINIT You must call goldilocks_init() first.
  109. */
  110. int
  111. goldilocks_derive_private_key (
  112. struct goldilocks_private_key_t *privkey,
  113. const unsigned char proto[GOLDI_SYMKEY_BYTES]
  114. ) __attribute__((nonnull(1,2),visibility ("default")));
  115. /**
  116. * @brief Compress a private key (by copying out the proto-key)
  117. * @param [out] proto The proto-key.
  118. * @param [in] privkey The private key.
  119. *
  120. * @warning This isn't even my final form!
  121. * @todo test.
  122. *
  123. * @retval GOLDI_EOK Success.
  124. * @retval GOLDI_EUNINIT You must call goldilocks_init() first.
  125. */
  126. void
  127. goldilocks_underive_private_key (
  128. unsigned char proto[GOLDI_SYMKEY_BYTES],
  129. const struct goldilocks_private_key_t *privkey
  130. ) __attribute__((nonnull(1,2),visibility ("default")));
  131. /**
  132. * @brief Extract the public key from a private key.
  133. *
  134. * This is essentially a memcpy from the public part of the privkey.
  135. *
  136. * @param [out] pubkey The extracted private key.
  137. * @param [in] privkey The private key.
  138. *
  139. * @retval GOLDI_EOK Success.
  140. * @retval GOLDI_ECORRUPT The private key is corrupt.
  141. */
  142. int
  143. goldilocks_private_to_public (
  144. struct goldilocks_public_key_t *pubkey,
  145. const struct goldilocks_private_key_t *privkey
  146. ) __attribute__((nonnull(1,2),visibility ("default")));
  147. /**
  148. * @brief Generate a Diffie-Hellman shared secret in constant time.
  149. *
  150. * This function uses some compile-time flags whose merit remains to
  151. * be decided.
  152. *
  153. * If the flag EXPERIMENT_ECDH_OBLITERATE_CT is set, prepend 40 bytes
  154. * of zeros to the secret before hashing. In the case that the other
  155. * party's key is detectably corrupt, instead the symmetric part
  156. * of the secret key is used to produce a pseudorandom value.
  157. *
  158. * If EXPERIMENT_ECDH_STIR_IN_PUBKEYS is set, the sum and product of
  159. * the two parties' public keys is prepended to the hash.
  160. *
  161. * In the current version, this function can safely be run even without
  162. * goldilocks_init(). But this property is not guaranteed for future
  163. * versions, so call it anyway.
  164. *
  165. * @warning This isn't even my final form!
  166. *
  167. * @param [out] shared The shared secret established with the other party.
  168. * @param [in] my_privkey My private key.
  169. * @param [in] your_pubkey The other party's public key.
  170. *
  171. * @retval GOLDI_EOK Success.
  172. * @retval GOLDI_ECORRUPT My key is corrupt.
  173. * @retval GOLDI_EINVAL The other party's key is corrupt.
  174. * @retval GOLDI_EUNINIT You must call goldilocks_init() first.
  175. */
  176. int
  177. goldilocks_shared_secret (
  178. uint8_t shared[GOLDI_SHARED_SECRET_BYTES],
  179. const struct goldilocks_private_key_t *my_privkey,
  180. const struct goldilocks_public_key_t *your_pubkey
  181. ) __attribute__((warn_unused_result,nonnull(1,2,3),visibility ("default")));
  182. #ifdef GOLDI_IMPLEMENT_SIGNATURES
  183. /**
  184. * @brief Sign a message.
  185. *
  186. * The signature is deterministic, using the symmetric secret found in the
  187. * secret key to form a nonce.
  188. *
  189. * The technique used in signing is a modified Schnorr system, like EdDSA.
  190. *
  191. * @warning This isn't even my final form!
  192. *
  193. * @param [out] signature_out Space for the output signature.
  194. * @param [in] message The message to be signed.
  195. * @param [in] message_len The length of the message to be signed.
  196. * @param [in] privkey My private key.
  197. *
  198. * @retval GOLDI_EOK Success.
  199. * @retval GOLDI_ECORRUPT My key is corrupt.
  200. * @retval GOLDI_EUNINIT You must call goldilocks_init() first.
  201. */
  202. int
  203. goldilocks_sign (
  204. uint8_t signature_out[GOLDI_SIGNATURE_BYTES],
  205. const uint8_t *message,
  206. uint64_t message_len,
  207. const struct goldilocks_private_key_t *privkey
  208. ) __attribute__((nonnull(1,2,4),visibility ("default")));
  209. /**
  210. * @brief Verify a signature.
  211. *
  212. * This function is fairly strict. It will correctly detect when
  213. * the signature has the wrong cofactor component, or when the sig
  214. * values aren't less than p or q.
  215. *
  216. * Currently this function does not detect when the public key is weird,
  217. * eg 0, has cofactor, etc. As a result, a party with a bogus public
  218. * key could create signatures that succeed on some systems and fail on
  219. * others.
  220. *
  221. * @warning This isn't even my final form!
  222. *
  223. * @param [in] signature The signature.
  224. * @param [in] message The message to be verified.
  225. * @param [in] message_len The length of the message to be verified.
  226. * @param [in] pubkey The signer's public key.
  227. *
  228. * @retval GOLDI_EOK Success.
  229. * @retval GOLDI_EINVAL The public key or signature is corrupt.
  230. * @retval GOLDI_EUNINIT You must call goldilocks_init() first.
  231. */
  232. int
  233. goldilocks_verify (
  234. const uint8_t signature[GOLDI_SIGNATURE_BYTES],
  235. const uint8_t *message,
  236. uint64_t message_len,
  237. const struct goldilocks_public_key_t *pubkey
  238. ) __attribute__((warn_unused_result,nonnull(1,2,4),visibility ("default")));
  239. #endif
  240. #if GOLDI_IMPLEMENT_PRECOMPUTED_KEYS
  241. /** A public key which has been expanded by precomputation for higher speed. */
  242. struct goldilocks_precomputed_public_key_t;
  243. /**
  244. * @brief Expand a public key by precomputation.
  245. *
  246. * @todo Give actual error returns, instead of ambiguous NULL.
  247. *
  248. * @warning This isn't even my final form!
  249. *
  250. * @param [in] pub The public key.
  251. * @retval NULL We ran out of memory, or the
  252. */
  253. struct goldilocks_precomputed_public_key_t *
  254. goldilocks_precompute_public_key (
  255. const struct goldilocks_public_key_t *pub
  256. ) __attribute__((warn_unused_result,nonnull(1),visibility ("default")));
  257. /**
  258. * @brief Overwrite an expanded public key with zeros, then destroy it.
  259. *
  260. * If the input is NULL, this function does nothing.
  261. *
  262. * @param [in] precom The public key.
  263. */
  264. void
  265. goldilocks_destroy_precomputed_public_key (
  266. struct goldilocks_precomputed_public_key_t *precom
  267. ) __attribute__((visibility ("default")));
  268. /**
  269. * @brief Verify a signature.
  270. *
  271. * This function is fairly strict. It will correctly detect when
  272. * the signature has the wrong cofactor component, or when the sig
  273. * values aren't less than p or q.
  274. *
  275. * @warning This isn't even my final form!
  276. *
  277. * @param [in] signature The signature.
  278. * @param [in] message The message to be verified.
  279. * @param [in] message_len The length of the message to be verified.
  280. * @param [in] pubkey The signer's public key, expanded by precomputation.
  281. *
  282. * @retval GOLDI_EOK Success.
  283. * @retval GOLDI_EINVAL The public key or signature is corrupt.
  284. * @retval GOLDI_EUNINIT You must call goldilocks_init() first.
  285. */
  286. int
  287. goldilocks_verify_precomputed (
  288. const uint8_t signature[GOLDI_SIGNATURE_BYTES],
  289. const uint8_t *message,
  290. uint64_t message_len,
  291. const struct goldilocks_precomputed_public_key_t *pubkey
  292. ) __attribute__((warn_unused_result,nonnull(1,2,4),visibility ("default")));
  293. /**
  294. * @brief Generate a Diffie-Hellman shared secret in constant time.
  295. * Uses a precomputation on the other party's public key for efficiency.
  296. *
  297. * This function uses some compile-time flags whose merit remains to
  298. * be decided.
  299. *
  300. * If the flag EXPERIMENT_ECDH_OBLITERATE_CT is set, prepend 40 bytes
  301. * of zeros to the secret before hashing. In the case that the other
  302. * party's key is detectably corrupt, instead the symmetric part
  303. * of the secret key is used to produce a pseudorandom value.
  304. *
  305. * If EXPERIMENT_ECDH_STIR_IN_PUBKEYS is set, the sum and product of
  306. * the two parties' public keys is prepended to the hash.
  307. *
  308. * In the current version, this function can safely be run even without
  309. * goldilocks_init(). But this property is not guaranteed for future
  310. * versions, so call it anyway.
  311. *
  312. * @warning This isn't even my final form!
  313. *
  314. * @param [out] shared The shared secret established with the other party.
  315. * @param [in] my_privkey My private key.
  316. * @param [in] your_pubkey The other party's precomputed public key.
  317. *
  318. * @retval GOLDI_EOK Success.
  319. * @retval GOLDI_ECORRUPT My key is corrupt.
  320. * @retval GOLDI_EINVAL The other party's key is corrupt.
  321. * @retval GOLDI_EUNINIT You must call goldilocks_init() first.
  322. */
  323. int
  324. goldilocks_shared_secret_precomputed (
  325. uint8_t shared[GOLDI_SHARED_SECRET_BYTES],
  326. const struct goldilocks_private_key_t *my_privkey,
  327. const struct goldilocks_precomputed_public_key_t *your_pubkey
  328. ) __attribute__((warn_unused_result,nonnull(1,2,3),visibility ("default")));
  329. #endif /* GOLDI_IMPLEMENT_PRECOMPUTED_KEYS */
  330. #ifdef __cplusplus
  331. }; /* extern "C" */
  332. #endif
  333. #endif /* __GOLDILOCKS_H__ */