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.
 
 
 
 
 

370 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 private key, 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 ()
  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. /**
  183. * @brief Sign a message.
  184. *
  185. * The signature is deterministic, using the symmetric secret found in the
  186. * secret key to form a nonce.
  187. *
  188. * The technique used in signing is a modified Schnorr system, like EdDSA.
  189. *
  190. * @warning This isn't even my final form!
  191. *
  192. * @param [out] signature_out Space for the output signature.
  193. * @param [in] message The message to be signed.
  194. * @param [in] message_len The length of the message to be signed.
  195. * @param [in] privkey My private key.
  196. *
  197. * @retval GOLDI_EOK Success.
  198. * @retval GOLDI_ECORRUPT My key is corrupt.
  199. * @retval GOLDI_EUNINIT You must call goldilocks_init() first.
  200. */
  201. int
  202. goldilocks_sign (
  203. uint8_t signature_out[GOLDI_SIGNATURE_BYTES],
  204. const uint8_t *message,
  205. uint64_t message_len,
  206. const struct goldilocks_private_key_t *privkey
  207. ) __attribute__((nonnull(1,2,4),visibility ("default")));
  208. /**
  209. * @brief Verify a signature.
  210. *
  211. * This function is fairly strict. It will correctly detect when
  212. * the signature has the wrong cofactor component, or when the sig
  213. * values aren't less than p or q.
  214. *
  215. * Currently this function does not detect when the public key is weird,
  216. * eg 0, has cofactor, etc. As a result, a party with a bogus public
  217. * key could create signatures that succeed on some systems and fail on
  218. * others.
  219. *
  220. * @warning This isn't even my final form!
  221. *
  222. * @param [in] signature The signature.
  223. * @param [in] message The message to be verified.
  224. * @param [in] message_len The length of the message to be verified.
  225. * @param [in] pubkey The signer's public key.
  226. *
  227. * @retval GOLDI_EOK Success.
  228. * @retval GOLDI_EINVAL The public key or signature is corrupt.
  229. * @retval GOLDI_EUNINIT You must call goldilocks_init() first.
  230. */
  231. int
  232. goldilocks_verify (
  233. const uint8_t signature[GOLDI_SIGNATURE_BYTES],
  234. const uint8_t *message,
  235. uint64_t message_len,
  236. const struct goldilocks_public_key_t *pubkey
  237. ) __attribute__((warn_unused_result,nonnull(1,2,4),visibility ("default")));
  238. #if GOLDI_IMPLEMENT_PRECOMPUTED_KEYS
  239. /** A public key which has been expanded by precomputation for higher speed. */
  240. struct goldilocks_precomputed_public_key_t;
  241. /**
  242. * @brief Expand a public key by precomputation.
  243. *
  244. * @todo Give actual error returns, instead of ambiguous NULL.
  245. *
  246. * @warning This isn't even my final form!
  247. *
  248. * @param [in] pub The public key.
  249. * @retval NULL We ran out of memory, or the
  250. */
  251. struct goldilocks_precomputed_public_key_t *
  252. goldilocks_precompute_public_key (
  253. const struct goldilocks_public_key_t *pub
  254. ) __attribute__((warn_unused_result,nonnull(1),visibility ("default")));
  255. /**
  256. * @brief Overwrite an expanded public key with zeros, then destroy it.
  257. *
  258. * If the input is NULL, this function does nothing.
  259. *
  260. * @param [in] precom The public key.
  261. */
  262. void
  263. goldilocks_destroy_precomputed_public_key (
  264. struct goldilocks_precomputed_public_key_t *precom
  265. ) __attribute__((visibility ("default")));
  266. /**
  267. * @brief Verify a signature.
  268. *
  269. * This function is fairly strict. It will correctly detect when
  270. * the signature has the wrong cofactor component, or when the sig
  271. * values aren't less than p or q.
  272. *
  273. * @warning This isn't even my final form!
  274. *
  275. * @param [in] signature The signature.
  276. * @param [in] message The message to be verified.
  277. * @param [in] message_len The length of the message to be verified.
  278. * @param [in] pubkey The signer's public key, expanded by precomputation.
  279. *
  280. * @retval GOLDI_EOK Success.
  281. * @retval GOLDI_EINVAL The public key or signature is corrupt.
  282. * @retval GOLDI_EUNINIT You must call goldilocks_init() first.
  283. */
  284. int
  285. goldilocks_verify_precomputed (
  286. const uint8_t signature[GOLDI_SIGNATURE_BYTES],
  287. const uint8_t *message,
  288. uint64_t message_len,
  289. const struct goldilocks_precomputed_public_key_t *pubkey
  290. ) __attribute__((warn_unused_result,nonnull(1,2,4),visibility ("default")));
  291. /**
  292. * @brief Generate a Diffie-Hellman shared secret in constant time.
  293. * Uses a precomputation on the other party's public key for efficiency.
  294. *
  295. * This function uses some compile-time flags whose merit remains to
  296. * be decided.
  297. *
  298. * If the flag EXPERIMENT_ECDH_OBLITERATE_CT is set, prepend 40 bytes
  299. * of zeros to the secret before hashing. In the case that the other
  300. * party's key is detectably corrupt, instead the symmetric part
  301. * of the secret key is used to produce a pseudorandom value.
  302. *
  303. * If EXPERIMENT_ECDH_STIR_IN_PUBKEYS is set, the sum and product of
  304. * the two parties' public keys is prepended to the hash.
  305. *
  306. * In the current version, this function can safely be run even without
  307. * goldilocks_init(). But this property is not guaranteed for future
  308. * versions, so call it anyway.
  309. *
  310. * @warning This isn't even my final form!
  311. *
  312. * @param [out] shared The shared secret established with the other party.
  313. * @param [in] my_privkey My private key.
  314. * @param [in] your_pubkey The other party's precomputed public key.
  315. *
  316. * @retval GOLDI_EOK Success.
  317. * @retval GOLDI_ECORRUPT My key is corrupt.
  318. * @retval GOLDI_EINVAL The other party's key is corrupt.
  319. * @retval GOLDI_EUNINIT You must call goldilocks_init() first.
  320. */
  321. int
  322. goldilocks_shared_secret_precomputed (
  323. uint8_t shared[GOLDI_SHARED_SECRET_BYTES],
  324. const struct goldilocks_private_key_t *my_privkey,
  325. const struct goldilocks_precomputed_public_key_t *your_pubkey
  326. ) __attribute__((warn_unused_result,nonnull(1,2,3),visibility ("default")));
  327. #endif /* GOLDI_IMPLEMENT_PRECOMPUTED_KEYS */
  328. #ifdef __cplusplus
  329. }; /* extern "C" */
  330. #endif
  331. #endif /* __GOLDILOCKS_H__ */