2
0

ed448.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright 2015-2016 Cryptography Research, Inc.
  4. *
  5. * Licensed under the OpenSSL license (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 c448_ed448_derive_public_key(
  33. uint8_t pubkey [EDDSA_448_PUBLIC_BYTES],
  34. const uint8_t privkey [EDDSA_448_PRIVATE_BYTES]);
  35. /*
  36. * EdDSA signing.
  37. *
  38. * signature (out): The signature.
  39. * privkey (in): The private key.
  40. * pubkey (in): The public key.
  41. * message (in): The message to sign.
  42. * message_len (in): The length of the message.
  43. * prehashed (in): Nonzero if the message is actually the hash of something
  44. * you want to sign.
  45. * context (in): A "context" for this signature of up to 255 bytes.
  46. * context_len (in): Length of the context.
  47. *
  48. * For Ed25519, it is unsafe to use the same key for both prehashed and
  49. * non-prehashed messages, at least without some very careful protocol-level
  50. * disambiguation. For Ed448 it is safe.
  51. */
  52. c448_error_t c448_ed448_sign(
  53. uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
  54. const uint8_t privkey[EDDSA_448_PRIVATE_BYTES],
  55. const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
  56. const uint8_t *message, size_t message_len,
  57. uint8_t prehashed, const uint8_t *context,
  58. size_t context_len);
  59. /*
  60. * EdDSA signing with prehash.
  61. *
  62. * signature (out): The signature.
  63. * privkey (in): The private key.
  64. * pubkey (in): The public key.
  65. * hash (in): The hash of the message. This object will not be modified by the
  66. * call.
  67. * context (in): A "context" for this signature of up to 255 bytes. Must be the
  68. * same as what was used for the prehash.
  69. * context_len (in): Length of the context.
  70. *
  71. * For Ed25519, it is unsafe to use the same key for both prehashed and
  72. * non-prehashed messages, at least without some very careful protocol-level
  73. * disambiguation. For Ed448 it is safe.
  74. */
  75. c448_error_t c448_ed448_sign_prehash(
  76. uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
  77. const uint8_t privkey[EDDSA_448_PRIVATE_BYTES],
  78. const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
  79. const uint8_t hash[64],
  80. const uint8_t *context,
  81. size_t context_len);
  82. /*
  83. * EdDSA signature verification.
  84. *
  85. * Uses the standard (i.e. less-strict) verification formula.
  86. *
  87. * signature (in): The signature.
  88. * pubkey (in): The public key.
  89. * message (in): The message to verify.
  90. * message_len (in): The length of the message.
  91. * prehashed (in): Nonzero if the message is actually the hash of something you
  92. * want to verify.
  93. * context (in): A "context" for this signature of up to 255 bytes.
  94. * context_len (in): Length of the context.
  95. *
  96. * For Ed25519, it is unsafe to use the same key for both prehashed and
  97. * non-prehashed messages, at least without some very careful protocol-level
  98. * disambiguation. For Ed448 it is safe.
  99. */
  100. c448_error_t c448_ed448_verify(const uint8_t
  101. signature[EDDSA_448_SIGNATURE_BYTES],
  102. const uint8_t
  103. pubkey[EDDSA_448_PUBLIC_BYTES],
  104. const uint8_t *message, size_t message_len,
  105. uint8_t prehashed, const uint8_t *context,
  106. uint8_t context_len);
  107. /*
  108. * EdDSA signature verification.
  109. *
  110. * Uses the standard (i.e. less-strict) verification formula.
  111. *
  112. * signature (in): The signature.
  113. * pubkey (in): The public key.
  114. * hash (in): The hash of the message. This object will not be modified by the
  115. * call.
  116. * context (in): A "context" for this signature of up to 255 bytes. Must be the
  117. * same as what was used for the prehash.
  118. * context_len (in): Length of the context.
  119. *
  120. * For Ed25519, it is unsafe to use the same key for both prehashed and
  121. * non-prehashed messages, at least without some very careful protocol-level
  122. * disambiguation. For Ed448 it is safe.
  123. */
  124. c448_error_t c448_ed448_verify_prehash(
  125. const uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
  126. const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
  127. const uint8_t hash[64],
  128. const uint8_t *context,
  129. uint8_t context_len);
  130. /*
  131. * EdDSA point encoding. Used internally, exposed externally.
  132. * Multiplies by C448_EDDSA_ENCODE_RATIO first.
  133. *
  134. * The multiplication is required because the EdDSA encoding represents
  135. * the cofactor information, but the Decaf encoding ignores it (which
  136. * is the whole point). So if you decode from EdDSA and re-encode to
  137. * EdDSA, the cofactor info must get cleared, because the intermediate
  138. * representation doesn't track it.
  139. *
  140. * The way we handle this is to multiply by C448_EDDSA_DECODE_RATIO when
  141. * decoding, and by C448_EDDSA_ENCODE_RATIO when encoding. The product of
  142. * these ratios is always exactly the cofactor 4, so the cofactor ends up
  143. * cleared one way or another. But exactly how that shakes out depends on the
  144. * base points specified in RFC 8032.
  145. *
  146. * The upshot is that if you pass the Decaf/Ristretto base point to
  147. * this function, you will get C448_EDDSA_ENCODE_RATIO times the
  148. * EdDSA base point.
  149. *
  150. * enc (out): The encoded point.
  151. * p (in): The point.
  152. */
  153. void curve448_point_mul_by_ratio_and_encode_like_eddsa(
  154. uint8_t enc [EDDSA_448_PUBLIC_BYTES],
  155. const curve448_point_t p);
  156. /*
  157. * EdDSA point decoding. Multiplies by C448_EDDSA_DECODE_RATIO, and
  158. * ignores cofactor information.
  159. *
  160. * See notes on curve448_point_mul_by_ratio_and_encode_like_eddsa
  161. *
  162. * enc (out): The encoded point.
  163. * p (in): The point.
  164. */
  165. c448_error_t curve448_point_decode_like_eddsa_and_mul_by_ratio(
  166. curve448_point_t p,
  167. const uint8_t enc[EDDSA_448_PUBLIC_BYTES]);
  168. /*
  169. * EdDSA to ECDH private key conversion
  170. * Using the appropriate hash function, hash the EdDSA private key
  171. * and keep only the lower bytes to get the ECDH private key
  172. *
  173. * x (out): The ECDH private key as in RFC7748
  174. * ed (in): The EdDSA private key
  175. */
  176. c448_error_t c448_ed448_convert_private_key_to_x448(
  177. uint8_t x[X448_PRIVATE_BYTES],
  178. const uint8_t ed[EDDSA_448_PRIVATE_BYTES]);
  179. #endif /* OSSL_CRYPTO_EC_CURVE448_ED448_H */