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.
 
 
 
 
 

172 lines
4.8 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 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. /**
  43. * @brief Initialize Goldilocks' precomputed tables and
  44. * random number generator.
  45. * @retval GOLDI_EOK Success.
  46. * @retval Nonzero An error occurred.
  47. */
  48. int
  49. goldilocks_init();
  50. /**
  51. * @brief Generate a new random keypair.
  52. * @param [out] privkey The generated private key.
  53. * @param [out] pubkey The generated public key.
  54. *
  55. * @warning This isn't even my final form!
  56. *
  57. * @retval GOLDI_EOK Success.
  58. * @retval GOLDI_ENODICE Insufficient entropy.
  59. */
  60. int
  61. goldilocks_keygen (
  62. struct goldilocks_private_key_t *privkey,
  63. struct goldilocks_public_key_t *pubkey
  64. ) __attribute__((warn_unused_result));
  65. /**
  66. * @brief Generate a Diffie-Hellman shared secret in constant time.
  67. *
  68. * This function uses some compile-time flags whose merit remains to
  69. * be decided.
  70. *
  71. * If the flag EXPERIMENT_ECDH_OBLITERATE_CT is set, prepend 40 bytes
  72. * of zeros to the secret before hashing. In the case that the other
  73. * party's key is detectably corrupt, instead the symmetric part
  74. * of the secret key is used to produce a pseudorandom value.
  75. *
  76. * If EXPERIMENT_ECDH_STIR_IN_PUBKEYS is set, the sum and product of
  77. * the two parties' public keys is prepended to the hash.
  78. *
  79. * @warning This isn't even my final form!
  80. *
  81. * @param [out] shared The shared secret established with the other party.
  82. * @param [in] my_privkey My private key.
  83. * @param [in] your_pubkey The other party's public key.
  84. *
  85. * @retval GOLDI_EOK Success.
  86. * @retval GOLDI_ECORRUPT My key is corrupt.
  87. * @retval GOLDI_EINVAL The other party's key is corrupt.
  88. */
  89. int
  90. goldilocks_shared_secret (
  91. uint8_t shared[64],
  92. const struct goldilocks_private_key_t *my_privkey,
  93. const struct goldilocks_public_key_t *your_pubkey
  94. ) __attribute__((warn_unused_result));
  95. /**
  96. * @brief Sign a message.
  97. *
  98. * The signature is deterministic, using the symmetric secret found in the
  99. * secret key to form a nonce.
  100. *
  101. * The technique used in signing is a modified Schnorr system, like EdDSA.
  102. *
  103. * @warning This isn't even my final form!
  104. * @warning This function contains endian bugs. (TODO)
  105. *
  106. * @param [out] signature_out Space for the output signature.
  107. * @param [in] message The message to be signed.
  108. * @param [in] message_len The length of the message to be signed.
  109. * @param [in] privkey My private key.
  110. *
  111. * @retval GOLDI_EOK Success.
  112. * @retval GOLDI_ECORRUPT My key is corrupt.
  113. */
  114. int
  115. goldilocks_sign (
  116. uint8_t signature_out[56*2],
  117. const uint8_t *message,
  118. uint64_t message_len,
  119. const struct goldilocks_private_key_t *privkey
  120. );
  121. /**
  122. * @brief Verify a signature.
  123. *
  124. * This function is fairly strict. It will correctly detect when
  125. * the signature has the wrong cofactor companent. Once deserialization
  126. * of numbers is strictified (TODO) it will limit the response to being
  127. * less than q as well.
  128. *
  129. * Currently this function does not detect when the public key is weird,
  130. * eg 0, has cofactor, etc. As a result, a party with a bogus public
  131. * key could create signatures that succeed on some systems and fail on
  132. * others.
  133. *
  134. * @warning This isn't even my final form!
  135. * @warning This function contains endian bugs. (TODO)
  136. *
  137. * @param [out] signature_out The signature.
  138. * @param [in] message The message to be verified.
  139. * @param [in] message_len The length of the message to be verified.
  140. * @param [in] pubkey The signer's public key.
  141. *
  142. * @retval GOLDI_EOK Success.
  143. * @retval GOLDI_EINVAL The public key or signature is corrupt.
  144. */
  145. int
  146. goldilocks_verify (
  147. const uint8_t signature[56*2],
  148. const uint8_t *message,
  149. uint64_t message_len,
  150. const struct goldilocks_public_key_t *pubkey
  151. ) __attribute__((warn_unused_result));
  152. #ifdef __cplusplus
  153. }; /* extern "C" */
  154. #endif
  155. #endif /* __GOLDILOCKS_H__ */