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.
 
 
 
 
 

211 lines
6.3 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. /**
  13. * @brief Serialized form of a Goldilocks public key.
  14. *
  15. * @warning This isn't even my final form!
  16. */
  17. struct goldilocks_public_key_t {
  18. uint8_t opaque[56]; /**< Serialized data. */
  19. };
  20. /**
  21. * @brief Serialized form of a Goldilocks private key.
  22. *
  23. * Contains 56 bytes of actual private key, 56 bytes of
  24. * public key, and 32 bytes of symmetric key for randomization.
  25. *
  26. * @warning This isn't even my final form!
  27. */
  28. struct goldilocks_private_key_t {
  29. uint8_t opaque[144]; /**< Serialized data. */
  30. };
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. /** @brief No error. */
  35. static const int GOLDI_EOK = 0;
  36. /** @brief Error: your key or other state is corrupt. */
  37. static const int GOLDI_ECORRUPT = 44801;
  38. /** @brief Error: other party's key is corrupt. */
  39. static const int GOLDI_EINVAL = 44802;
  40. /** @brief Error: not enough entropy. */
  41. static const int GOLDI_ENODICE = 44804;
  42. /** @brief Error: you need to initialize the library first. */
  43. static const int GOLDI_EUNINIT = 44805;
  44. /** @brief Error: called init() but we are already initialized. */
  45. static const int GOLDI_EALREADYINIT = 44805;
  46. /**
  47. * @brief Initialize Goldilocks' precomputed tables and
  48. * random number generator. This function must be called before
  49. * any of the other Goldilocks routines (except
  50. * goldilocks_shared_secret in the current version) and should be
  51. * called only once per process.
  52. *
  53. * There is currently no way to tear down this state. It is possible
  54. * that a future version of this library will not require this function.
  55. *
  56. * @retval GOLDI_EOK Success.
  57. * @retval GOLDI_EALREADYINIT Already initialized.
  58. * @retval GOLDI_ECORRUPT Memory is corrupted, or another thread is already init'ing.
  59. * @retval Nonzero An error occurred.
  60. */
  61. int
  62. goldilocks_init ()
  63. __attribute__((warn_unused_result));
  64. /**
  65. * @brief Generate a new random keypair.
  66. * @param [out] privkey The generated private key.
  67. * @param [out] pubkey The generated public key.
  68. *
  69. * @warning This isn't even my final form!
  70. *
  71. * @retval GOLDI_EOK Success.
  72. * @retval GOLDI_ENODICE Insufficient entropy.
  73. * @retval GOLDI_EUNINIT You must call goldilocks_init() first.
  74. */
  75. int
  76. goldilocks_keygen (
  77. struct goldilocks_private_key_t *privkey,
  78. struct goldilocks_public_key_t *pubkey
  79. ) __attribute__((warn_unused_result,nonnull(1,2)));
  80. /**
  81. * @brief Extract the public key from a private key.
  82. *
  83. * This is essentially a memcpy from the public part of the privkey.
  84. *
  85. * @param [out] pubkey The extracted private key.
  86. * @param [in] privkey The private key.
  87. *
  88. * @retval GOLDI_EOK Success.
  89. * @retval GOLDI_ECORRUPT The private key is corrupt.
  90. */
  91. int
  92. goldilocks_private_to_public (
  93. struct goldilocks_public_key_t *pubkey,
  94. const struct goldilocks_private_key_t *privkey
  95. ) __attribute__((nonnull(1,2)));
  96. /**
  97. * @brief Generate a Diffie-Hellman shared secret in constant time.
  98. *
  99. * This function uses some compile-time flags whose merit remains to
  100. * be decided.
  101. *
  102. * If the flag EXPERIMENT_ECDH_OBLITERATE_CT is set, prepend 40 bytes
  103. * of zeros to the secret before hashing. In the case that the other
  104. * party's key is detectably corrupt, instead the symmetric part
  105. * of the secret key is used to produce a pseudorandom value.
  106. *
  107. * If EXPERIMENT_ECDH_STIR_IN_PUBKEYS is set, the sum and product of
  108. * the two parties' public keys is prepended to the hash.
  109. *
  110. * In the current version, this function can safely be run even without
  111. * goldilocks_init(). But this property is not guaranteed for future
  112. * versions, so call it anyway.
  113. *
  114. * @warning This isn't even my final form!
  115. *
  116. * @param [out] shared The shared secret established with the other party.
  117. * @param [in] my_privkey My private key.
  118. * @param [in] your_pubkey The other party's public key.
  119. *
  120. * @retval GOLDI_EOK Success.
  121. * @retval GOLDI_ECORRUPT My key is corrupt.
  122. * @retval GOLDI_EINVAL The other party's key is corrupt.
  123. * @retval GOLDI_EUNINIT You must call goldilocks_init() first.
  124. */
  125. int
  126. goldilocks_shared_secret (
  127. uint8_t shared[64],
  128. const struct goldilocks_private_key_t *my_privkey,
  129. const struct goldilocks_public_key_t *your_pubkey
  130. ) __attribute__((warn_unused_result,nonnull(1,2,3)));
  131. /**
  132. * @brief Sign a message.
  133. *
  134. * The signature is deterministic, using the symmetric secret found in the
  135. * secret key to form a nonce.
  136. *
  137. * The technique used in signing is a modified Schnorr system, like EdDSA.
  138. *
  139. * @warning This isn't even my final form!
  140. *
  141. * @param [out] signature_out Space for the output signature.
  142. * @param [in] message The message to be signed.
  143. * @param [in] message_len The length of the message to be signed.
  144. * @param [in] privkey My private key.
  145. *
  146. * @retval GOLDI_EOK Success.
  147. * @retval GOLDI_ECORRUPT My key is corrupt.
  148. * @retval GOLDI_EUNINIT You must call goldilocks_init() first.
  149. */
  150. int
  151. goldilocks_sign (
  152. uint8_t signature_out[56*2],
  153. const uint8_t *message,
  154. uint64_t message_len,
  155. const struct goldilocks_private_key_t *privkey
  156. ) __attribute__((nonnull(1,2,4)));
  157. /**
  158. * @brief Verify a signature.
  159. *
  160. * This function is fairly strict. It will correctly detect when
  161. * the signature has the wrong cofactor component, or when the sig
  162. * values aren't less than p or q.
  163. *
  164. * Currently this function does not detect when the public key is weird,
  165. * eg 0, has cofactor, etc. As a result, a party with a bogus public
  166. * key could create signatures that succeed on some systems and fail on
  167. * others.
  168. *
  169. * @warning This isn't even my final form!
  170. *
  171. * @param [in] signature The signature.
  172. * @param [in] message The message to be verified.
  173. * @param [in] message_len The length of the message to be verified.
  174. * @param [in] pubkey The signer's public key.
  175. *
  176. * @retval GOLDI_EOK Success.
  177. * @retval GOLDI_EINVAL The public key or signature is corrupt.
  178. * @retval GOLDI_EUNINIT You must call goldilocks_init() first.
  179. */
  180. int
  181. goldilocks_verify (
  182. const uint8_t signature[56*2],
  183. const uint8_t *message,
  184. uint64_t message_len,
  185. const struct goldilocks_public_key_t *pubkey
  186. ) __attribute__((warn_unused_result,nonnull(1,2,4)));
  187. #ifdef __cplusplus
  188. }; /* extern "C" */
  189. #endif
  190. #endif /* __GOLDILOCKS_H__ */