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.
 
 
 
 
 

381 lines
12 KiB

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