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