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.
 
 
 
 
 

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