ed448.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright 2015-2016 Cryptography Research, Inc.
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. *
  10. * Originally written by Mike Hamburg
  11. */
  12. #ifndef OSSL_CRYPTO_EC_CURVE448_ED448_H
  13. # define OSSL_CRYPTO_EC_CURVE448_ED448_H
  14. # include "point_448.h"
  15. /* Number of bytes in an EdDSA public key. */
  16. # define EDDSA_448_PUBLIC_BYTES 57
  17. /* Number of bytes in an EdDSA private key. */
  18. # define EDDSA_448_PRIVATE_BYTES EDDSA_448_PUBLIC_BYTES
  19. /* Number of bytes in an EdDSA private key. */
  20. # define EDDSA_448_SIGNATURE_BYTES (EDDSA_448_PUBLIC_BYTES + \
  21. EDDSA_448_PRIVATE_BYTES)
  22. /* EdDSA encoding ratio. */
  23. # define C448_EDDSA_ENCODE_RATIO 4
  24. /* EdDSA decoding ratio. */
  25. # define C448_EDDSA_DECODE_RATIO (4 / 4)
  26. /*
  27. * EdDSA key generation. This function uses a different (non-Decaf) encoding.
  28. *
  29. * pubkey (out): The public key.
  30. * privkey (in): The private key.
  31. */
  32. c448_error_t
  33. ossl_c448_ed448_derive_public_key(
  34. OSSL_LIB_CTX *ctx,
  35. uint8_t pubkey [EDDSA_448_PUBLIC_BYTES],
  36. const uint8_t privkey [EDDSA_448_PRIVATE_BYTES],
  37. const char *propq);
  38. /*
  39. * EdDSA signing.
  40. *
  41. * signature (out): The signature.
  42. * privkey (in): The private key.
  43. * pubkey (in): The public key.
  44. * message (in): The message to sign.
  45. * message_len (in): The length of the message.
  46. * prehashed (in): Nonzero if the message is actually the hash of something
  47. * you want to sign.
  48. * context (in): A "context" for this signature of up to 255 bytes.
  49. * context_len (in): Length of the context.
  50. *
  51. * For Ed25519, it is unsafe to use the same key for both prehashed and
  52. * non-prehashed messages, at least without some very careful protocol-level
  53. * disambiguation. For Ed448 it is safe.
  54. */
  55. c448_error_t
  56. ossl_c448_ed448_sign(OSSL_LIB_CTX *ctx,
  57. uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
  58. const uint8_t privkey[EDDSA_448_PRIVATE_BYTES],
  59. const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
  60. const uint8_t *message, size_t message_len,
  61. uint8_t prehashed, const uint8_t *context,
  62. size_t context_len,
  63. const char *propq);
  64. /*
  65. * EdDSA signing with prehash.
  66. *
  67. * signature (out): The signature.
  68. * privkey (in): The private key.
  69. * pubkey (in): The public key.
  70. * hash (in): The hash of the message. This object will not be modified by the
  71. * call.
  72. * context (in): A "context" for this signature of up to 255 bytes. Must be the
  73. * same as what was used for the prehash.
  74. * context_len (in): Length of the context.
  75. *
  76. * For Ed25519, it is unsafe to use the same key for both prehashed and
  77. * non-prehashed messages, at least without some very careful protocol-level
  78. * disambiguation. For Ed448 it is safe.
  79. */
  80. c448_error_t
  81. ossl_c448_ed448_sign_prehash(OSSL_LIB_CTX *ctx,
  82. uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
  83. const uint8_t privkey[EDDSA_448_PRIVATE_BYTES],
  84. const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
  85. const uint8_t hash[64],
  86. const uint8_t *context,
  87. size_t context_len,
  88. const char *propq);
  89. /*
  90. * EdDSA signature verification.
  91. *
  92. * Uses the standard (i.e. less-strict) verification formula.
  93. *
  94. * signature (in): The signature.
  95. * pubkey (in): The public key.
  96. * message (in): The message to verify.
  97. * message_len (in): The length of the message.
  98. * prehashed (in): Nonzero if the message is actually the hash of something you
  99. * want to verify.
  100. * context (in): A "context" for this signature of up to 255 bytes.
  101. * context_len (in): Length of the context.
  102. *
  103. * For Ed25519, it is unsafe to use the same key for both prehashed and
  104. * non-prehashed messages, at least without some very careful protocol-level
  105. * disambiguation. For Ed448 it is safe.
  106. */
  107. c448_error_t
  108. ossl_c448_ed448_verify(OSSL_LIB_CTX *ctx,
  109. const uint8_t
  110. signature[EDDSA_448_SIGNATURE_BYTES],
  111. const uint8_t
  112. pubkey[EDDSA_448_PUBLIC_BYTES],
  113. const uint8_t *message, size_t message_len,
  114. uint8_t prehashed, const uint8_t *context,
  115. uint8_t context_len,
  116. const char *propq);
  117. /*
  118. * EdDSA signature verification.
  119. *
  120. * Uses the standard (i.e. less-strict) verification formula.
  121. *
  122. * signature (in): The signature.
  123. * pubkey (in): The public key.
  124. * hash (in): The hash of the message. This object will not be modified by the
  125. * call.
  126. * context (in): A "context" for this signature of up to 255 bytes. Must be the
  127. * same as what was used for the prehash.
  128. * context_len (in): Length of the context.
  129. *
  130. * For Ed25519, it is unsafe to use the same key for both prehashed and
  131. * non-prehashed messages, at least without some very careful protocol-level
  132. * disambiguation. For Ed448 it is safe.
  133. */
  134. c448_error_t
  135. ossl_c448_ed448_verify_prehash(
  136. OSSL_LIB_CTX *ctx,
  137. const uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
  138. const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
  139. const uint8_t hash[64],
  140. const uint8_t *context,
  141. uint8_t context_len,
  142. const char *propq);
  143. /*
  144. * EdDSA point encoding. Used internally, exposed externally.
  145. * Multiplies by C448_EDDSA_ENCODE_RATIO first.
  146. *
  147. * The multiplication is required because the EdDSA encoding represents
  148. * the cofactor information, but the Decaf encoding ignores it (which
  149. * is the whole point). So if you decode from EdDSA and re-encode to
  150. * EdDSA, the cofactor info must get cleared, because the intermediate
  151. * representation doesn't track it.
  152. *
  153. * The way we handle this is to multiply by C448_EDDSA_DECODE_RATIO when
  154. * decoding, and by C448_EDDSA_ENCODE_RATIO when encoding. The product of
  155. * these ratios is always exactly the cofactor 4, so the cofactor ends up
  156. * cleared one way or another. But exactly how that shakes out depends on the
  157. * base points specified in RFC 8032.
  158. *
  159. * The upshot is that if you pass the Decaf/Ristretto base point to
  160. * this function, you will get C448_EDDSA_ENCODE_RATIO times the
  161. * EdDSA base point.
  162. *
  163. * enc (out): The encoded point.
  164. * p (in): The point.
  165. */
  166. void
  167. ossl_curve448_point_mul_by_ratio_and_encode_like_eddsa(
  168. uint8_t enc [EDDSA_448_PUBLIC_BYTES],
  169. const curve448_point_t p);
  170. /*
  171. * EdDSA point decoding. Multiplies by C448_EDDSA_DECODE_RATIO, and
  172. * ignores cofactor information.
  173. *
  174. * See notes on curve448_point_mul_by_ratio_and_encode_like_eddsa
  175. *
  176. * enc (out): The encoded point.
  177. * p (in): The point.
  178. */
  179. c448_error_t
  180. ossl_curve448_point_decode_like_eddsa_and_mul_by_ratio(
  181. curve448_point_t p,
  182. const uint8_t enc[EDDSA_448_PUBLIC_BYTES]);
  183. /*
  184. * EdDSA to ECDH private key conversion
  185. * Using the appropriate hash function, hash the EdDSA private key
  186. * and keep only the lower bytes to get the ECDH private key
  187. *
  188. * x (out): The ECDH private key as in RFC7748
  189. * ed (in): The EdDSA private key
  190. */
  191. c448_error_t
  192. ossl_c448_ed448_convert_private_key_to_x448(
  193. OSSL_LIB_CTX *ctx,
  194. uint8_t x[X448_PRIVATE_BYTES],
  195. const uint8_t ed[EDDSA_448_PRIVATE_BYTES],
  196. const char *propq);
  197. #endif /* OSSL_CRYPTO_EC_CURVE448_ED448_H */