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.
 
 
 
 
 

247 lines
10 KiB

  1. /** @brief A group of prime order p, based on $(iso_to). */
  2. #include <decaf/point_$(gf_bits).h>
  3. #include <decaf/shake.h>
  4. #include <decaf/sha512.h>
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. /** Number of bytes in an EdDSA public key. */
  9. #define DECAF_EDDSA_$(gf_shortname)_PUBLIC_BYTES $((gf_bits)//8 + 1)
  10. /** Number of bytes in an EdDSA private key. */
  11. #define DECAF_EDDSA_$(gf_shortname)_PRIVATE_BYTES DECAF_EDDSA_$(gf_shortname)_PUBLIC_BYTES
  12. /** Number of bytes in an EdDSA private key. */
  13. #define DECAF_EDDSA_$(gf_shortname)_SIGNATURE_BYTES (DECAF_EDDSA_$(gf_shortname)_PUBLIC_BYTES + DECAF_EDDSA_$(gf_shortname)_PRIVATE_BYTES)
  14. /** Does EdDSA support non-contextual signatures? */
  15. #if defined _MSC_VER /* Different syntax for exposing API */
  16. #define DECAF_EDDSA_$(gf_shortname)_SUPPORTS_CONTEXTLESS_SIGS $(eddsa_no_context)
  17. $("extern const DECAF_API_VIS uint8_t * const DECAF_ED" + gf_shortname + "_NO_CONTEXT;\n" if eddsa_no_context else "")
  18. #else
  19. #define DECAF_EDDSA_$(gf_shortname)_SUPPORTS_CONTEXTLESS_SIGS $(eddsa_no_context)
  20. $("DECAF_API_VIS extern const uint8_t * const DECAF_ED" + gf_shortname + "_NO_CONTEXT;\n" if eddsa_no_context else "")
  21. #endif
  22. /** Prehash context (raw), because each EdDSA instance has a different prehash. */
  23. #define decaf_ed$(gf_shortname)_prehash_ctx_s decaf_$(eddsa_hash)_ctx_s
  24. /** Prehash context, array[1] form. */
  25. #define decaf_ed$(gf_shortname)_prehash_ctx_t decaf_$(eddsa_hash)_ctx_t
  26. /** Prehash update. */
  27. #define decaf_ed$(gf_shortname)_prehash_update decaf_$(eddsa_hash)_update
  28. /** Prehash destroy. */
  29. #define decaf_ed$(gf_shortname)_prehash_destroy decaf_$(eddsa_hash)_destroy
  30. /** EdDSA encoding ratio. */
  31. #define $(C_NS)_EDDSA_ENCODE_RATIO $(eddsa_encode_ratio)
  32. /** EdDSA decoding ratio. */
  33. #define $(C_NS)_EDDSA_DECODE_RATIO ($(cofactor) / $(eddsa_encode_ratio))
  34. /**
  35. * @brief EdDSA key generation. This function uses a different (non-Decaf)
  36. * encoding.
  37. *
  38. * @param [out] pubkey The public key.
  39. * @param [in] privkey The private key.
  40. */
  41. void DECAF_API_VIS decaf_ed$(gf_shortname)_derive_public_key (
  42. uint8_t pubkey[DECAF_EDDSA_$(gf_shortname)_PUBLIC_BYTES],
  43. const uint8_t privkey[DECAF_EDDSA_$(gf_shortname)_PRIVATE_BYTES]
  44. ) DECAF_NONNULL DECAF_NOINLINE;
  45. /**
  46. * @brief EdDSA signing.
  47. *
  48. * @param [out] signature The signature.
  49. * @param [in] privkey The private key.
  50. * @param [in] pubkey The public key.
  51. * @param [in] message The message to sign.
  52. * @param [in] message_len The length of the message.
  53. * @param [in] prehashed Nonzero if the message is actually the hash of something you want to sign.
  54. * @param [in] context A "context" for this signature of up to 255 bytes.
  55. * @param [in] context_len Length of the context.
  56. *
  57. * @warning For Ed25519, it is unsafe to use the same key for both prehashed and non-prehashed
  58. * messages, at least without some very careful protocol-level disambiguation. For Ed448 it is
  59. * safe. The C++ wrapper is designed to make it harder to screw this up, but this C code gives
  60. * you no seat belt.
  61. */
  62. void DECAF_API_VIS decaf_ed$(gf_shortname)_sign (
  63. uint8_t signature[DECAF_EDDSA_$(gf_shortname)_SIGNATURE_BYTES],
  64. const uint8_t privkey[DECAF_EDDSA_$(gf_shortname)_PRIVATE_BYTES],
  65. const uint8_t pubkey[DECAF_EDDSA_$(gf_shortname)_PUBLIC_BYTES],
  66. const uint8_t *message,
  67. size_t message_len,
  68. uint8_t prehashed,
  69. const uint8_t *context,
  70. uint8_t context_len
  71. ) __attribute__((nonnull(1,2,3))) DECAF_NOINLINE;
  72. /**
  73. * @brief EdDSA signing with prehash.
  74. *
  75. * @param [out] signature The signature.
  76. * @param [in] privkey The private key.
  77. * @param [in] pubkey The public key.
  78. * @param [in] hash The hash of the message. This object will not be modified by the call.
  79. * @param [in] context A "context" for this signature of up to 255 bytes. Must be the same as what was used for the prehash.
  80. * @param [in] context_len Length of the context.
  81. *
  82. * @warning For Ed25519, it is unsafe to use the same key for both prehashed and non-prehashed
  83. * messages, at least without some very careful protocol-level disambiguation. For Ed448 it is
  84. * safe. The C++ wrapper is designed to make it harder to screw this up, but this C code gives
  85. * you no seat belt.
  86. */
  87. void DECAF_API_VIS decaf_ed$(gf_shortname)_sign_prehash (
  88. uint8_t signature[DECAF_EDDSA_$(gf_shortname)_SIGNATURE_BYTES],
  89. const uint8_t privkey[DECAF_EDDSA_$(gf_shortname)_PRIVATE_BYTES],
  90. const uint8_t pubkey[DECAF_EDDSA_$(gf_shortname)_PUBLIC_BYTES],
  91. const decaf_ed$(gf_shortname)_prehash_ctx_t hash,
  92. const uint8_t *context,
  93. uint8_t context_len
  94. ) __attribute__((nonnull(1,2,3,4))) DECAF_NOINLINE;
  95. /**
  96. * @brief Prehash initialization, with contexts if supported.
  97. *
  98. * @param [out] hash The hash object to be initialized.
  99. */
  100. void DECAF_API_VIS decaf_ed$(gf_shortname)_prehash_init (
  101. decaf_ed$(gf_shortname)_prehash_ctx_t hash
  102. ) __attribute__((nonnull(1))) DECAF_NOINLINE;
  103. /**
  104. * @brief EdDSA signature verification.
  105. *
  106. * Uses the standard (i.e. less-strict) verification formula.
  107. *
  108. * @param [in] signature The signature.
  109. * @param [in] pubkey The public key.
  110. * @param [in] message The message to verify.
  111. * @param [in] message_len The length of the message.
  112. * @param [in] prehashed Nonzero if the message is actually the hash of something you want to verify.
  113. * @param [in] context A "context" for this signature of up to 255 bytes.
  114. * @param [in] context_len Length of the context.
  115. *
  116. * @warning For Ed25519, it is unsafe to use the same key for both prehashed and non-prehashed
  117. * messages, at least without some very careful protocol-level disambiguation. For Ed448 it is
  118. * safe. The C++ wrapper is designed to make it harder to screw this up, but this C code gives
  119. * you no seat belt.
  120. */
  121. decaf_error_t DECAF_API_VIS decaf_ed$(gf_shortname)_verify (
  122. const uint8_t signature[DECAF_EDDSA_$(gf_shortname)_SIGNATURE_BYTES],
  123. const uint8_t pubkey[DECAF_EDDSA_$(gf_shortname)_PUBLIC_BYTES],
  124. const uint8_t *message,
  125. size_t message_len,
  126. uint8_t prehashed,
  127. const uint8_t *context,
  128. uint8_t context_len
  129. ) __attribute__((nonnull(1,2))) DECAF_NOINLINE;
  130. /**
  131. * @brief EdDSA signature verification.
  132. *
  133. * Uses the standard (i.e. less-strict) verification formula.
  134. *
  135. * @param [in] signature The signature.
  136. * @param [in] pubkey The public key.
  137. * @param [in] hash The hash of the message. This object will not be modified by the call.
  138. * @param [in] context A "context" for this signature of up to 255 bytes. Must be the same as what was used for the prehash.
  139. * @param [in] context_len Length of the context.
  140. *
  141. * @warning For Ed25519, it is unsafe to use the same key for both prehashed and non-prehashed
  142. * messages, at least without some very careful protocol-level disambiguation. For Ed448 it is
  143. * safe. The C++ wrapper is designed to make it harder to screw this up, but this C code gives
  144. * you no seat belt.
  145. */
  146. decaf_error_t DECAF_API_VIS decaf_ed$(gf_shortname)_verify_prehash (
  147. const uint8_t signature[DECAF_EDDSA_$(gf_shortname)_SIGNATURE_BYTES],
  148. const uint8_t pubkey[DECAF_EDDSA_$(gf_shortname)_PUBLIC_BYTES],
  149. const decaf_ed$(gf_shortname)_prehash_ctx_t hash,
  150. const uint8_t *context,
  151. uint8_t context_len
  152. ) __attribute__((nonnull(1,2))) DECAF_NOINLINE;
  153. /**
  154. * @brief EdDSA point encoding. Used internally, exposed externally.
  155. * Multiplies by $(C_NS)_EDDSA_ENCODE_RATIO first.
  156. *
  157. * The multiplication is required because the EdDSA encoding represents
  158. * the cofactor information, but the Decaf encoding ignores it (which
  159. * is the whole point). So if you decode from EdDSA and re-encode to
  160. * EdDSA, the cofactor info must get cleared, because the intermediate
  161. * representation doesn't track it.
  162. *
  163. * The way libdecaf handles this is to multiply by
  164. * $(C_NS)_EDDSA_DECODE_RATIO when decoding, and by
  165. * $(C_NS)_EDDSA_ENCODE_RATIO when encoding. The product of these
  166. * ratios is always exactly the cofactor $(cofactor), so the cofactor
  167. * ends up cleared one way or another. But exactly how that shakes
  168. * out depends on the base points specified in RFC 8032.
  169. *
  170. * The upshot is that if you pass the Decaf/Ristretto base point to
  171. * this function, you will get $(C_NS)_EDDSA_ENCODE_RATIO times the
  172. * EdDSA base point.
  173. *
  174. * @param [out] enc The encoded point.
  175. * @param [in] p The point.
  176. */
  177. void DECAF_API_VIS $(c_ns)_point_mul_by_ratio_and_encode_like_eddsa (
  178. uint8_t enc[DECAF_EDDSA_$(gf_shortname)_PUBLIC_BYTES],
  179. const $(c_ns)_point_t p
  180. ) DECAF_NONNULL DECAF_NOINLINE;
  181. /**
  182. * @brief EdDSA point decoding. Multiplies by $(C_NS)_EDDSA_DECODE_RATIO,
  183. * and ignores cofactor information.
  184. *
  185. * See notes on $(c_ns)_point_mul_by_ratio_and_encode_like_eddsa
  186. *
  187. * @param [out] enc The encoded point.
  188. * @param [in] p The point.
  189. */
  190. decaf_error_t DECAF_API_VIS $(c_ns)_point_decode_like_eddsa_and_mul_by_ratio (
  191. $(c_ns)_point_t p,
  192. const uint8_t enc[DECAF_EDDSA_$(gf_shortname)_PUBLIC_BYTES]
  193. ) DECAF_NONNULL DECAF_NOINLINE;
  194. /**
  195. * @brief EdDSA to ECDH public key conversion
  196. * Deserialize the point to get y on Edwards curve,
  197. * Convert it to u coordinate on Montgomery curve.
  198. *
  199. * @warning This function does not check that the public key being converted
  200. * is a valid EdDSA public key (FUTURE?)
  201. *
  202. * @param[out] x The ECDH public key as in RFC7748(point on Montgomery curve)
  203. * @param[in] ed The EdDSA public key(point on Edwards curve)
  204. */
  205. void DECAF_API_VIS decaf_ed$(gf_shortname)_convert_public_key_to_x$(gf_shortname) (
  206. uint8_t x[DECAF_X$(gf_shortname)_PUBLIC_BYTES],
  207. const uint8_t ed[DECAF_EDDSA_$(gf_shortname)_PUBLIC_BYTES]
  208. ) DECAF_NONNULL DECAF_NOINLINE;
  209. /**
  210. * @brief EdDSA to ECDH private key conversion
  211. * Using the appropriate hash function, hash the EdDSA private key
  212. * and keep only the lower bytes to get the ECDH private key
  213. *
  214. * @param[out] x The ECDH private key as in RFC7748
  215. * @param[in] ed The EdDSA private key
  216. */
  217. void DECAF_API_VIS decaf_ed$(gf_shortname)_convert_private_key_to_x$(gf_shortname) (
  218. uint8_t x[DECAF_X$(gf_shortname)_PRIVATE_BYTES],
  219. const uint8_t ed[DECAF_EDDSA_$(gf_shortname)_PRIVATE_BYTES]
  220. ) DECAF_NONNULL DECAF_NOINLINE;
  221. #ifdef __cplusplus
  222. } /* extern "C" */
  223. #endif