f710dc5618362724e41a1480dd7c32d3a467d955
[openssl.git] / crypto / ec / curve448 / ed448.h
1 /*
2  * Copyright 2017 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
13 #ifndef __DECAF_ED448_H__
14 # define __DECAF_ED448_H__ 1
15
16 # include "point_448.h"
17
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21
22 /* Number of bytes in an EdDSA public key. */
23 # define DECAF_EDDSA_448_PUBLIC_BYTES 57
24
25 /* Number of bytes in an EdDSA private key. */
26 # define DECAF_EDDSA_448_PRIVATE_BYTES DECAF_EDDSA_448_PUBLIC_BYTES
27
28 /* Number of bytes in an EdDSA private key. */
29 # define DECAF_EDDSA_448_SIGNATURE_BYTES (DECAF_EDDSA_448_PUBLIC_BYTES + \
30                                           DECAF_EDDSA_448_PRIVATE_BYTES)
31
32 /* Does EdDSA support non-contextual signatures? */
33 # define DECAF_EDDSA_448_SUPPORTS_CONTEXTLESS_SIGS 0
34
35 /* EdDSA encoding ratio. */
36 # define DECAF_448_EDDSA_ENCODE_RATIO 4
37
38 /* EdDSA decoding ratio. */
39 # define DECAF_448_EDDSA_DECODE_RATIO (4 / 4)
40
41 /*
42  * EdDSA key generation.  This function uses a different (non-Decaf) encoding.
43  *
44  * pubkey (out): The public key.
45  * privkey (in): The private key.
46  */
47 decaf_error_t decaf_ed448_derive_public_key(
48                         uint8_t pubkey [DECAF_EDDSA_448_PUBLIC_BYTES],
49                         const uint8_t privkey [DECAF_EDDSA_448_PRIVATE_BYTES]);
50
51 /*
52  * EdDSA signing.
53  *
54  * signature (out): The signature.
55  * privkey (in): The private key.
56  * pubkey (in):  The public key.
57  * message (in):  The message to sign.
58  * message_len (in):  The length of the message.
59  * prehashed (in):  Nonzero if the message is actually the hash of something
60  *                  you want to sign.
61  * context (in):  A "context" for this signature of up to 255 bytes.
62  * context_len (in):  Length of the context.
63  *
64  * For Ed25519, it is unsafe to use the same key for both prehashed and
65  * non-prehashed messages, at least without some very careful protocol-level
66  * disambiguation.  For Ed448 it is safe.  The C++ wrapper is designed to make
67  * it harder to screw this up, but this C code gives you no seat belt.
68  */
69 decaf_error_t decaf_ed448_sign(
70                         uint8_t signature[DECAF_EDDSA_448_SIGNATURE_BYTES],
71                         const uint8_t privkey[DECAF_EDDSA_448_PRIVATE_BYTES],
72                         const uint8_t pubkey[DECAF_EDDSA_448_PUBLIC_BYTES],
73                         const uint8_t *message, size_t message_len,
74                         uint8_t prehashed, const uint8_t *context,
75                         size_t context_len)
76                         __attribute__ ((nonnull(1, 2, 3)));
77
78 /*
79  * EdDSA signing with prehash.
80  *
81  * signature (out): The signature.
82  * privkey (in): The private key.
83  * pubkey (in): The public key.
84  * hash (in): The hash of the message.  This object will not be modified by the
85  *            call.
86  * context (in): A "context" for this signature of up to 255 bytes.  Must be the
87  *               same as what was used for the prehash.
88  * context_len (in): Length of the context.
89  *
90  * For Ed25519, it is unsafe to use the same key for both prehashed and
91  * non-prehashed messages, at least without some very careful protocol-level
92  * disambiguation.  For Ed448 it is safe.  The C++ wrapper is designed to make
93  * it harder to screw this up, but this C code gives you no seat belt.
94  */
95 decaf_error_t decaf_ed448_sign_prehash(
96                         uint8_t signature[DECAF_EDDSA_448_SIGNATURE_BYTES],
97                         const uint8_t privkey[DECAF_EDDSA_448_PRIVATE_BYTES],
98                         const uint8_t pubkey[DECAF_EDDSA_448_PUBLIC_BYTES],
99                         const uint8_t hash[64],
100                         const uint8_t *context,
101                         size_t context_len)
102                         __attribute__ ((nonnull(1, 2, 3, 4)));
103
104 /*
105  * EdDSA signature verification.
106  *
107  * Uses the standard (i.e. less-strict) verification formula.
108  *
109  * signature (in): The signature.
110  * pubkey (in): The public key.
111  * message (in): The message to verify.
112  * message_len (in): The length of the message.
113  * prehashed (in): Nonzero if the message is actually the hash of something you
114  *                 want to verify.
115  * context (in): A "context" for this signature of up to 255 bytes.
116  * context_len (in): Length of the context.
117  *
118  * For Ed25519, it is unsafe to use the same key for both prehashed and
119  * non-prehashed messages, at least without some very careful protocol-level
120  * disambiguation.  For Ed448 it is safe.  The C++ wrapper is designed to make
121  * it harder to screw this up, but this C code gives you no seat belt.
122  */
123 decaf_error_t decaf_ed448_verify(const uint8_t
124                                  signature[DECAF_EDDSA_448_SIGNATURE_BYTES],
125                                  const uint8_t
126                                  pubkey[DECAF_EDDSA_448_PUBLIC_BYTES],
127                                  const uint8_t *message, size_t message_len,
128                                  uint8_t prehashed, const uint8_t *context,
129                                  uint8_t context_len)
130                                  __attribute__ ((nonnull(1, 2)));
131
132 /*
133  * EdDSA signature verification.
134  *
135  * Uses the standard (i.e. less-strict) verification formula.
136  *
137  * signature (in): The signature.
138  * pubkey (in): The public key.
139  * hash (in): The hash of the message.  This object will not be modified by the
140  *            call.
141  * context (in): A "context" for this signature of up to 255 bytes.  Must be the
142  *               same as what was used for the prehash.
143  * context_len (in): Length of the context.
144  *
145  * For Ed25519, it is unsafe to use the same key for both prehashed and
146  * non-prehashed messages, at least without some very careful protocol-level
147  * disambiguation.  For Ed448 it is safe.  The C++ wrapper is designed to make
148  * it harder to screw this up, but this C code gives you no seat belt.
149  */
150 decaf_error_t decaf_ed448_verify_prehash(
151                     const uint8_t signature[DECAF_EDDSA_448_SIGNATURE_BYTES],
152                     const uint8_t pubkey[DECAF_EDDSA_448_PUBLIC_BYTES],
153                     const uint8_t hash[64],
154                     const uint8_t *context,
155                     uint8_t context_len)
156                     __attribute__ ((nonnull(1, 2)));
157
158 /*
159  * EdDSA point encoding.  Used internally, exposed externally.
160  * Multiplies by DECAF_448_EDDSA_ENCODE_RATIO first.
161  *
162  * The multiplication is required because the EdDSA encoding represents
163  * the cofactor information, but the Decaf encoding ignores it (which
164  * is the whole point).  So if you decode from EdDSA and re-encode to
165  * EdDSA, the cofactor info must get cleared, because the intermediate
166  * representation doesn't track it.
167  *
168  * The way libdecaf handles this is to multiply by
169  * DECAF_448_EDDSA_DECODE_RATIO when decoding, and by
170  * DECAF_448_EDDSA_ENCODE_RATIO when encoding.  The product of these
171  * ratios is always exactly the cofactor 4, so the cofactor
172  * ends up cleared one way or another.  But exactly how that shakes
173  * out depends on the base points specified in RFC 8032.
174  *
175  * The upshot is that if you pass the Decaf/Ristretto base point to
176  * this function, you will get DECAF_448_EDDSA_ENCODE_RATIO times the
177  * EdDSA base point.
178  *
179  * enc (out): The encoded point.
180  * p (in): The point.
181  */
182 void curve448_point_mul_by_ratio_and_encode_like_eddsa(
183                                     uint8_t enc [DECAF_EDDSA_448_PUBLIC_BYTES],
184                                     const curve448_point_t p);
185
186 /*
187  * EdDSA point decoding.  Multiplies by DECAF_448_EDDSA_DECODE_RATIO, and
188  * ignores cofactor information.
189  *
190  * See notes on curve448_point_mul_by_ratio_and_encode_like_eddsa
191  *
192  * enc (out): The encoded point.
193  * p (in): The point.
194  */
195 decaf_error_t curve448_point_decode_like_eddsa_and_mul_by_ratio(
196                             curve448_point_t p,
197                             const uint8_t enc[DECAF_EDDSA_448_PUBLIC_BYTES]);
198
199 /*
200  * EdDSA to ECDH public key conversion
201  * Deserialize the point to get y on Edwards curve,
202  * Convert it to u coordinate on Montgomery curve.
203  *
204  * This function does not check that the public key being converted is a valid
205  * EdDSA public key (FUTURE?)
206  *
207  * x (out): The ECDH public key as in RFC7748(point on Montgomery curve)
208  * ed (in): The EdDSA public key(point on Edwards curve)
209  */
210 void decaf_ed448_convert_public_key_to_x448(
211                                 uint8_t x[DECAF_X448_PUBLIC_BYTES],
212                                 const uint8_t ed[DECAF_EDDSA_448_PUBLIC_BYTES]);
213
214 /*
215  * EdDSA to ECDH private key conversion
216  * Using the appropriate hash function, hash the EdDSA private key
217  * and keep only the lower bytes to get the ECDH private key
218  *
219  * x (out): The ECDH private key as in RFC7748
220  * ed (in): The EdDSA private key
221  */
222 decaf_error_t decaf_ed448_convert_private_key_to_x448(
223                             uint8_t x[DECAF_X448_PRIVATE_BYTES],
224                             const uint8_t ed[DECAF_EDDSA_448_PRIVATE_BYTES]);
225
226 #ifdef __cplusplus
227 } /* extern "C" */
228 #endif
229
230 #endif                          /* __DECAF_ED448_H__ */