Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/ec/curve448/ed448.h

raw
1 /*
2 * Copyright 2017-2021 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 signature. */
25 #define EDDSA_448_SIGNATURE_BYTES (EDDSA_448_PUBLIC_BYTES + EDDSA_448_PRIVATE_BYTES)
26
27 /* EdDSA encoding ratio. */
28 #define C448_EDDSA_ENCODE_RATIO 4
29
30 /* EdDSA decoding ratio. */
31 #define C448_EDDSA_DECODE_RATIO (4 / 4)
32
33 /*
34 * EdDSA key generation. This function uses a different (non-Decaf) encoding.
35 *
36 * pubkey (out): The public key.
37 * privkey (in): The private key.
38 */
39 c448_error_t
40 ossl_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
64 ossl_c448_ed448_sign(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
90 ossl_c448_ed448_sign_prehash(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
118 ossl_c448_ed448_verify(OSSL_LIB_CTX *ctx,
119 const uint8_t
120 signature[EDDSA_448_SIGNATURE_BYTES],
121 const uint8_t
122 pubkey[EDDSA_448_PUBLIC_BYTES],
123 const uint8_t *message, size_t message_len,
124 uint8_t prehashed, const uint8_t *context,
125 uint8_t context_len,
126 const char *propq);
127
128 /*
129 * EdDSA signature verification.
130 *
131 * Uses the standard (i.e. less-strict) verification formula.
132 *
133 * signature (in): The signature.
134 * pubkey (in): The public key.
135 * hash (in): The hash of the message. This object will not be modified by the
136 * call.
137 * context (in): A "context" for this signature of up to 255 bytes. Must be the
138 * same as what was used for the prehash.
139 * context_len (in): Length of the context.
140 *
141 * For Ed25519, it is unsafe to use the same key for both prehashed and
142 * non-prehashed messages, at least without some very careful protocol-level
143 * disambiguation. For Ed448 it is safe.
144 */
145 c448_error_t
146 ossl_c448_ed448_verify_prehash(
147 OSSL_LIB_CTX *ctx,
148 const uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
149 const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
150 const uint8_t hash[64],
151 const uint8_t *context,
152 uint8_t context_len,
153 const char *propq);
154
155 /*
156 * EdDSA point encoding. Used internally, exposed externally.
157 * Multiplies by C448_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 we handle this is to multiply by C448_EDDSA_DECODE_RATIO when
166 * decoding, and by C448_EDDSA_ENCODE_RATIO when encoding. The product of
167 * these ratios is always exactly the cofactor 4, so the cofactor ends up
168 * cleared one way or another. But exactly how that shakes out depends on the
169 * base points specified in RFC 8032.
170 *
171 * The upshot is that if you pass the Decaf/Ristretto base point to
172 * this function, you will get C448_EDDSA_ENCODE_RATIO times the
173 * EdDSA base point.
174 *
175 * enc (out): The encoded point.
176 * p (in): The point.
177 */
178 void ossl_curve448_point_mul_by_ratio_and_encode_like_eddsa(
179 uint8_t enc[EDDSA_448_PUBLIC_BYTES],
180 const curve448_point_t p);
181
182 /*
183 * EdDSA point decoding. Multiplies by C448_EDDSA_DECODE_RATIO, and
184 * ignores cofactor information.
185 *
186 * See notes on curve448_point_mul_by_ratio_and_encode_like_eddsa
187 *
188 * enc (out): The encoded point.
189 * p (in): The point.
190 */
191 c448_error_t
192 ossl_curve448_point_decode_like_eddsa_and_mul_by_ratio(
193 curve448_point_t p,
194 const uint8_t enc[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 c448_error_t
205 ossl_c448_ed448_convert_private_key_to_x448(
206 OSSL_LIB_CTX *ctx,
207 uint8_t x[X448_PRIVATE_BYTES],
208 const uint8_t ed[EDDSA_448_PRIVATE_BYTES],
209 const char *propq);
210
211 #endif /* OSSL_CRYPTO_EC_CURVE448_ED448_H */