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