More style fixes to Curve448 code based on review feedback
[openssl.git] / crypto / ec / curve448 / ed448.h
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
13 #ifndef HEADER_ED448_H
14 # define HEADER_ED448_H
15
16 # include "point_448.h"
17
18 /* Number of bytes in an EdDSA public key. */
19 # define EDDSA_448_PUBLIC_BYTES 57
20
21 /* Number of bytes in an EdDSA private key. */
22 # define EDDSA_448_PRIVATE_BYTES EDDSA_448_PUBLIC_BYTES
23
24 /* Number of bytes in an EdDSA private key. */
25 # define EDDSA_448_SIGNATURE_BYTES (EDDSA_448_PUBLIC_BYTES + \
26                                     EDDSA_448_PRIVATE_BYTES)
27
28 /* EdDSA encoding ratio. */
29 # define C448_EDDSA_ENCODE_RATIO 4
30
31 /* EdDSA decoding ratio. */
32 # define C448_EDDSA_DECODE_RATIO (4 / 4)
33
34 /*
35  * EdDSA key generation.  This function uses a different (non-Decaf) encoding.
36  *
37  * pubkey (out): The public key.
38  * privkey (in): The private key.
39  */
40 c448_error_t c448_ed448_derive_public_key(
41                         uint8_t pubkey [EDDSA_448_PUBLIC_BYTES],
42                         const uint8_t privkey [EDDSA_448_PRIVATE_BYTES]);
43
44 /*
45  * EdDSA signing.
46  *
47  * signature (out): The signature.
48  * privkey (in): The private key.
49  * pubkey (in):  The public key.
50  * message (in):  The message to sign.
51  * message_len (in):  The length of the message.
52  * prehashed (in):  Nonzero if the message is actually the hash of something
53  *                  you want to sign.
54  * context (in):  A "context" for this signature of up to 255 bytes.
55  * context_len (in):  Length of the context.
56  *
57  * For Ed25519, it is unsafe to use the same key for both prehashed and
58  * non-prehashed messages, at least without some very careful protocol-level
59  * disambiguation.  For Ed448 it is safe.  The C++ wrapper is designed to make
60  * it harder to screw this up, but this C code gives you no seat belt.
61  */
62 c448_error_t c448_ed448_sign(
63                         uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
64                         const uint8_t privkey[EDDSA_448_PRIVATE_BYTES],
65                         const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
66                         const uint8_t *message, size_t message_len,
67                         uint8_t prehashed, const uint8_t *context,
68                         size_t context_len);
69
70 /*
71  * EdDSA signing with prehash.
72  *
73  * signature (out): The signature.
74  * privkey (in): The private key.
75  * pubkey (in): The public key.
76  * hash (in): The hash of the message.  This object will not be modified by the
77  *            call.
78  * context (in): A "context" for this signature of up to 255 bytes.  Must be the
79  *               same as what was used for the prehash.
80  * context_len (in): Length of the context.
81  *
82  * For Ed25519, it is unsafe to use the same key for both prehashed and
83  * non-prehashed messages, at least without some very careful protocol-level
84  * disambiguation.  For Ed448 it is safe.  The C++ wrapper is designed to make
85  * it harder to screw this up, but this C code gives you no seat belt.
86  */
87 c448_error_t c448_ed448_sign_prehash(
88                         uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
89                         const uint8_t privkey[EDDSA_448_PRIVATE_BYTES],
90                         const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
91                         const uint8_t hash[64],
92                         const uint8_t *context,
93                         size_t context_len);
94
95 /*
96  * EdDSA signature verification.
97  *
98  * Uses the standard (i.e. less-strict) verification formula.
99  *
100  * signature (in): The signature.
101  * pubkey (in): The public key.
102  * message (in): The message to verify.
103  * message_len (in): The length of the message.
104  * prehashed (in): Nonzero if the message is actually the hash of something you
105  *                 want to verify.
106  * context (in): A "context" for this signature of up to 255 bytes.
107  * context_len (in): Length of the context.
108  *
109  * For Ed25519, it is unsafe to use the same key for both prehashed and
110  * non-prehashed messages, at least without some very careful protocol-level
111  * disambiguation.  For Ed448 it is safe.
112  */
113 c448_error_t c448_ed448_verify(const uint8_t
114                                  signature[EDDSA_448_SIGNATURE_BYTES],
115                                  const uint8_t
116                                  pubkey[EDDSA_448_PUBLIC_BYTES],
117                                  const uint8_t *message, size_t message_len,
118                                  uint8_t prehashed, const uint8_t *context,
119                                  uint8_t context_len);
120
121 /*
122  * EdDSA signature verification.
123  *
124  * Uses the standard (i.e. less-strict) verification formula.
125  *
126  * signature (in): The signature.
127  * pubkey (in): The public key.
128  * hash (in): The hash of the message.  This object will not be modified by the
129  *            call.
130  * context (in): A "context" for this signature of up to 255 bytes.  Must be the
131  *               same as what was used for the prehash.
132  * context_len (in): Length of the context.
133  *
134  * For Ed25519, it is unsafe to use the same key for both prehashed and
135  * non-prehashed messages, at least without some very careful protocol-level
136  * disambiguation.  For Ed448 it is safe.  The C++ wrapper is designed to make
137  * it harder to screw this up, but this C code gives you no seat belt.
138  */
139 c448_error_t c448_ed448_verify_prehash(
140                     const uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
141                     const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
142                     const uint8_t hash[64],
143                     const uint8_t *context,
144                     uint8_t context_len);
145
146 /*
147  * EdDSA point encoding.  Used internally, exposed externally.
148  * Multiplies by C448_EDDSA_ENCODE_RATIO first.
149  *
150  * The multiplication is required because the EdDSA encoding represents
151  * the cofactor information, but the Decaf encoding ignores it (which
152  * is the whole point).  So if you decode from EdDSA and re-encode to
153  * EdDSA, the cofactor info must get cleared, because the intermediate
154  * representation doesn't track it.
155  *
156  * The way we handle this is to multiply by C448_EDDSA_DECODE_RATIO when
157  * decoding, and by C448_EDDSA_ENCODE_RATIO when encoding.  The product of
158  * these ratios is always exactly the cofactor 4, so the cofactor ends up
159  * cleared one way or another.  But exactly how that shakes out depends on the
160  * base points specified in RFC 8032.
161  *
162  * The upshot is that if you pass the Decaf/Ristretto base point to
163  * this function, you will get C448_EDDSA_ENCODE_RATIO times the
164  * EdDSA base point.
165  *
166  * enc (out): The encoded point.
167  * p (in): The point.
168  */
169 void curve448_point_mul_by_ratio_and_encode_like_eddsa(
170                                     uint8_t enc [EDDSA_448_PUBLIC_BYTES],
171                                     const curve448_point_t p);
172
173 /*
174  * EdDSA point decoding.  Multiplies by C448_EDDSA_DECODE_RATIO, and
175  * ignores cofactor information.
176  *
177  * See notes on curve448_point_mul_by_ratio_and_encode_like_eddsa
178  *
179  * enc (out): The encoded point.
180  * p (in): The point.
181  */
182 c448_error_t curve448_point_decode_like_eddsa_and_mul_by_ratio(
183                             curve448_point_t p,
184                             const uint8_t enc[EDDSA_448_PUBLIC_BYTES]);
185
186 /*
187  * EdDSA to ECDH private key conversion
188  * Using the appropriate hash function, hash the EdDSA private key
189  * and keep only the lower bytes to get the ECDH private key
190  *
191  * x (out): The ECDH private key as in RFC7748
192  * ed (in): The EdDSA private key
193  */
194 c448_error_t c448_ed448_convert_private_key_to_x448(
195                             uint8_t x[X448_PRIVATE_BYTES],
196                             const uint8_t ed[EDDSA_448_PRIVATE_BYTES]);
197
198 #endif                          /* HEADER_ED448_H */