2 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright 2015-2016 Cryptography Research, Inc.
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
10 * Originally written by Mike Hamburg
13 #ifndef __DECAF_ED448_H__
14 # define __DECAF_ED448_H__ 1
16 # include "point_448.h"
22 /** Number of bytes in an EdDSA public key. */
23 # define DECAF_EDDSA_448_PUBLIC_BYTES 57
25 /** Number of bytes in an EdDSA private key. */
26 # define DECAF_EDDSA_448_PRIVATE_BYTES DECAF_EDDSA_448_PUBLIC_BYTES
28 /** Number of bytes in an EdDSA private key. */
29 # define DECAF_EDDSA_448_SIGNATURE_BYTES (DECAF_EDDSA_448_PUBLIC_BYTES + DECAF_EDDSA_448_PRIVATE_BYTES)
31 /** Does EdDSA support non-contextual signatures? */
32 # define DECAF_EDDSA_448_SUPPORTS_CONTEXTLESS_SIGS 0
34 /** EdDSA encoding ratio. */
35 # define DECAF_448_EDDSA_ENCODE_RATIO 4
37 /** EdDSA decoding ratio. */
38 # define DECAF_448_EDDSA_DECODE_RATIO (4 / 4)
41 * @brief EdDSA key generation. This function uses a different (non-Decaf)
44 * @param [out] pubkey The public key.
45 * @param [in] privkey The private key.
47 decaf_error_t decaf_ed448_derive_public_key(uint8_t
49 [DECAF_EDDSA_448_PUBLIC_BYTES],
52 [DECAF_EDDSA_448_PRIVATE_BYTES]
56 * @brief EdDSA signing.
58 * @param [out] signature The signature.
59 * @param [in] privkey The private key.
60 * @param [in] pubkey The public key.
61 * @param [in] message The message to sign.
62 * @param [in] message_len The length of the message.
63 * @param [in] prehashed Nonzero if the message is actually the hash of something you want to sign.
64 * @param [in] context A "context" for this signature of up to 255 bytes.
65 * @param [in] context_len Length of the context.
67 * @warning For Ed25519, it is unsafe to use the same key for both prehashed and non-prehashed
68 * messages, at least without some very careful protocol-level disambiguation. For Ed448 it is
69 * safe. The C++ wrapper is designed to make it harder to screw this up, but this C code gives
72 decaf_error_t decaf_ed448_sign(uint8_t
73 signature[DECAF_EDDSA_448_SIGNATURE_BYTES],
75 privkey[DECAF_EDDSA_448_PRIVATE_BYTES],
77 pubkey[DECAF_EDDSA_448_PUBLIC_BYTES],
78 const uint8_t *message, size_t message_len,
79 uint8_t prehashed, const uint8_t *context,
81 __attribute__ ((nonnull(1, 2, 3)));
84 * @brief EdDSA signing with prehash.
86 * @param [out] signature The signature.
87 * @param [in] privkey The private key.
88 * @param [in] pubkey The public key.
89 * @param [in] hash The hash of the message. This object will not be modified by the call.
90 * @param [in] context A "context" for this signature of up to 255 bytes. Must be the same as what was used for the prehash.
91 * @param [in] context_len Length of the context.
93 * @warning For Ed25519, it is unsafe to use the same key for both prehashed and non-prehashed
94 * messages, at least without some very careful protocol-level disambiguation. For Ed448 it is
95 * safe. The C++ wrapper is designed to make it harder to screw this up, but this C code gives
98 decaf_error_t decaf_ed448_sign_prehash(uint8_t
100 [DECAF_EDDSA_448_SIGNATURE_BYTES],
102 privkey[DECAF_EDDSA_448_PRIVATE_BYTES],
104 pubkey[DECAF_EDDSA_448_PUBLIC_BYTES],
105 const uint8_t hash[64],
106 const uint8_t *context,
108 __attribute__ ((nonnull(1, 2, 3, 4)));
111 * @brief EdDSA signature verification.
113 * Uses the standard (i.e. less-strict) verification formula.
115 * @param [in] signature The signature.
116 * @param [in] pubkey The public key.
117 * @param [in] message The message to verify.
118 * @param [in] message_len The length of the message.
119 * @param [in] prehashed Nonzero if the message is actually the hash of something you want to verify.
120 * @param [in] context A "context" for this signature of up to 255 bytes.
121 * @param [in] context_len Length of the context.
123 * @warning For Ed25519, it is unsafe to use the same key for both prehashed and non-prehashed
124 * messages, at least without some very careful protocol-level disambiguation. For Ed448 it is
125 * safe. The C++ wrapper is designed to make it harder to screw this up, but this C code gives
128 decaf_error_t decaf_ed448_verify(const uint8_t
129 signature[DECAF_EDDSA_448_SIGNATURE_BYTES],
131 pubkey[DECAF_EDDSA_448_PUBLIC_BYTES],
132 const uint8_t *message, size_t message_len,
133 uint8_t prehashed, const uint8_t *context,
135 __attribute__ ((nonnull(1, 2)));
138 * @brief EdDSA signature verification.
140 * Uses the standard (i.e. less-strict) verification formula.
142 * @param [in] signature The signature.
143 * @param [in] pubkey The public key.
144 * @param [in] hash The hash of the message. This object will not be modified by the call.
145 * @param [in] context A "context" for this signature of up to 255 bytes. Must be the same as what was used for the prehash.
146 * @param [in] context_len Length of the context.
148 * @warning For Ed25519, it is unsafe to use the same key for both prehashed and non-prehashed
149 * messages, at least without some very careful protocol-level disambiguation. For Ed448 it is
150 * safe. The C++ wrapper is designed to make it harder to screw this up, but this C code gives
153 decaf_error_t decaf_ed448_verify_prehash(const uint8_t
155 [DECAF_EDDSA_448_SIGNATURE_BYTES],
157 pubkey[DECAF_EDDSA_448_PUBLIC_BYTES],
158 const uint8_t hash[64],
159 const uint8_t *context,
161 __attribute__ ((nonnull(1, 2)));
164 * @brief EdDSA point encoding. Used internally, exposed externally.
165 * Multiplies by DECAF_448_EDDSA_ENCODE_RATIO first.
167 * The multiplication is required because the EdDSA encoding represents
168 * the cofactor information, but the Decaf encoding ignores it (which
169 * is the whole point). So if you decode from EdDSA and re-encode to
170 * EdDSA, the cofactor info must get cleared, because the intermediate
171 * representation doesn't track it.
173 * The way libdecaf handles this is to multiply by
174 * DECAF_448_EDDSA_DECODE_RATIO when decoding, and by
175 * DECAF_448_EDDSA_ENCODE_RATIO when encoding. The product of these
176 * ratios is always exactly the cofactor 4, so the cofactor
177 * ends up cleared one way or another. But exactly how that shakes
178 * out depends on the base points specified in RFC 8032.
180 * The upshot is that if you pass the Decaf/Ristretto base point to
181 * this function, you will get DECAF_448_EDDSA_ENCODE_RATIO times the
184 * @param [out] enc The encoded point.
185 * @param [in] p The point.
187 void curve448_point_mul_by_ratio_and_encode_like_eddsa(uint8_t
189 [DECAF_EDDSA_448_PUBLIC_BYTES],
190 const curve448_point_t
194 * @brief EdDSA point decoding. Multiplies by DECAF_448_EDDSA_DECODE_RATIO,
195 * and ignores cofactor information.
197 * See notes on curve448_point_mul_by_ratio_and_encode_like_eddsa
199 * @param [out] enc The encoded point.
200 * @param [in] p The point.
202 decaf_error_t curve448_point_decode_like_eddsa_and_mul_by_ratio(curve448_point_t
206 [DECAF_EDDSA_448_PUBLIC_BYTES]
210 * @brief EdDSA to ECDH public key conversion
211 * Deserialize the point to get y on Edwards curve,
212 * Convert it to u coordinate on Montgomery curve.
214 * @warning This function does not check that the public key being converted
215 * is a valid EdDSA public key (FUTURE?)
217 * @param[out] x The ECDH public key as in RFC7748(point on Montgomery curve)
218 * @param[in] ed The EdDSA public key(point on Edwards curve)
220 void decaf_ed448_convert_public_key_to_x448(uint8_t x[DECAF_X448_PUBLIC_BYTES],
222 ed[DECAF_EDDSA_448_PUBLIC_BYTES]
226 * @brief EdDSA to ECDH private key conversion
227 * Using the appropriate hash function, hash the EdDSA private key
228 * and keep only the lower bytes to get the ECDH private key
230 * @param[out] x The ECDH private key as in RFC7748
231 * @param[in] ed The EdDSA private key
233 decaf_error_t decaf_ed448_convert_private_key_to_x448(uint8_t
235 [DECAF_X448_PRIVATE_BYTES],
238 [DECAF_EDDSA_448_PRIVATE_BYTES]
245 #endif /* __DECAF_ED448_H__ */