]>
Commit | Line | Data |
---|---|---|
1308e022 | 1 | /* |
3c2bdd7d | 2 | * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved. |
1308e022 | 3 | * Copyright 2015-2016 Cryptography Research, Inc. |
7324473f | 4 | * |
a7f182b7 | 5 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
1308e022 MC |
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 | |
7324473f | 9 | * |
1308e022 | 10 | * Originally written by Mike Hamburg |
7324473f MC |
11 | */ |
12 | ||
ae4186b0 DMSP |
13 | #ifndef OSSL_CRYPTO_EC_CURVE448_ED448_H |
14 | # define OSSL_CRYPTO_EC_CURVE448_ED448_H | |
7324473f | 15 | |
205fd638 | 16 | # include "point_448.h" |
7324473f | 17 | |
8d55f844 | 18 | /* Number of bytes in an EdDSA public key. */ |
db90b274 | 19 | # define EDDSA_448_PUBLIC_BYTES 57 |
7324473f | 20 | |
8d55f844 | 21 | /* Number of bytes in an EdDSA private key. */ |
db90b274 | 22 | # define EDDSA_448_PRIVATE_BYTES EDDSA_448_PUBLIC_BYTES |
7324473f | 23 | |
8d55f844 | 24 | /* Number of bytes in an EdDSA private key. */ |
db90b274 MC |
25 | # define EDDSA_448_SIGNATURE_BYTES (EDDSA_448_PUBLIC_BYTES + \ |
26 | EDDSA_448_PRIVATE_BYTES) | |
7324473f | 27 | |
8d55f844 | 28 | /* EdDSA encoding ratio. */ |
db90b274 | 29 | # define C448_EDDSA_ENCODE_RATIO 4 |
7324473f | 30 | |
8d55f844 | 31 | /* EdDSA decoding ratio. */ |
db90b274 | 32 | # define C448_EDDSA_DECODE_RATIO (4 / 4) |
7324473f | 33 | |
8d55f844 MC |
34 | /* |
35 | * EdDSA key generation. This function uses a different (non-Decaf) encoding. | |
7324473f | 36 | * |
8d55f844 MC |
37 | * pubkey (out): The public key. |
38 | * privkey (in): The private key. | |
205fd638 | 39 | */ |
054d43ff SL |
40 | c448_error_t |
41 | ossl_c448_ed448_derive_public_key( | |
42 | OSSL_LIB_CTX *ctx, | |
43 | uint8_t pubkey [EDDSA_448_PUBLIC_BYTES], | |
44 | const uint8_t privkey [EDDSA_448_PRIVATE_BYTES], | |
45 | const char *propq); | |
8d55f844 MC |
46 | |
47 | /* | |
48 | * EdDSA signing. | |
49 | * | |
50 | * signature (out): The signature. | |
51 | * privkey (in): The private key. | |
52 | * pubkey (in): The public key. | |
53 | * message (in): The message to sign. | |
54 | * message_len (in): The length of the message. | |
55 | * prehashed (in): Nonzero if the message is actually the hash of something | |
56 | * you want to sign. | |
57 | * context (in): A "context" for this signature of up to 255 bytes. | |
58 | * context_len (in): Length of the context. | |
59 | * | |
60 | * For Ed25519, it is unsafe to use the same key for both prehashed and | |
61 | * non-prehashed messages, at least without some very careful protocol-level | |
53ef3252 | 62 | * disambiguation. For Ed448 it is safe. |
205fd638 | 63 | */ |
054d43ff SL |
64 | c448_error_t |
65 | ossl_c448_ed448_sign(OSSL_LIB_CTX *ctx, | |
66 | uint8_t signature[EDDSA_448_SIGNATURE_BYTES], | |
67 | const uint8_t privkey[EDDSA_448_PRIVATE_BYTES], | |
68 | const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES], | |
69 | const uint8_t *message, size_t message_len, | |
70 | uint8_t prehashed, const uint8_t *context, | |
71 | size_t context_len, | |
72 | const char *propq); | |
8d55f844 MC |
73 | |
74 | /* | |
75 | * EdDSA signing with prehash. | |
76 | * | |
77 | * signature (out): The signature. | |
78 | * privkey (in): The private key. | |
79 | * pubkey (in): The public key. | |
80 | * hash (in): The hash of the message. This object will not be modified by the | |
81 | * call. | |
82 | * context (in): A "context" for this signature of up to 255 bytes. Must be the | |
83 | * same as what was used for the prehash. | |
84 | * context_len (in): Length of the context. | |
85 | * | |
86 | * For Ed25519, it is unsafe to use the same key for both prehashed and | |
87 | * non-prehashed messages, at least without some very careful protocol-level | |
53ef3252 | 88 | * disambiguation. For Ed448 it is safe. |
205fd638 | 89 | */ |
054d43ff SL |
90 | c448_error_t |
91 | ossl_c448_ed448_sign_prehash(OSSL_LIB_CTX *ctx, | |
92 | uint8_t signature[EDDSA_448_SIGNATURE_BYTES], | |
93 | const uint8_t privkey[EDDSA_448_PRIVATE_BYTES], | |
94 | const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES], | |
95 | const uint8_t hash[64], | |
96 | const uint8_t *context, | |
97 | size_t context_len, | |
98 | const char *propq); | |
8d55f844 MC |
99 | |
100 | /* | |
101 | * EdDSA signature verification. | |
7324473f MC |
102 | * |
103 | * Uses the standard (i.e. less-strict) verification formula. | |
104 | * | |
8d55f844 MC |
105 | * signature (in): The signature. |
106 | * pubkey (in): The public key. | |
107 | * message (in): The message to verify. | |
108 | * message_len (in): The length of the message. | |
109 | * prehashed (in): Nonzero if the message is actually the hash of something you | |
110 | * want to verify. | |
111 | * context (in): A "context" for this signature of up to 255 bytes. | |
112 | * context_len (in): Length of the context. | |
113 | * | |
114 | * For Ed25519, it is unsafe to use the same key for both prehashed and | |
115 | * non-prehashed messages, at least without some very careful protocol-level | |
68b20c00 | 116 | * disambiguation. For Ed448 it is safe. |
7324473f | 117 | */ |
054d43ff SL |
118 | c448_error_t |
119 | ossl_c448_ed448_verify(OSSL_LIB_CTX *ctx, | |
120 | const uint8_t | |
121 | signature[EDDSA_448_SIGNATURE_BYTES], | |
122 | const uint8_t | |
123 | pubkey[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 | const char *propq); | |
7324473f | 128 | |
8d55f844 MC |
129 | /* |
130 | * EdDSA signature verification. | |
7324473f MC |
131 | * |
132 | * Uses the standard (i.e. less-strict) verification formula. | |
133 | * | |
8d55f844 MC |
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 | |
53ef3252 | 144 | * disambiguation. For Ed448 it is safe. |
7324473f | 145 | */ |
054d43ff SL |
146 | c448_error_t |
147 | ossl_c448_ed448_verify_prehash( | |
148 | OSSL_LIB_CTX *ctx, | |
149 | const uint8_t signature[EDDSA_448_SIGNATURE_BYTES], | |
150 | const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES], | |
151 | const uint8_t hash[64], | |
152 | const uint8_t *context, | |
153 | uint8_t context_len, | |
154 | const char *propq); | |
8d55f844 MC |
155 | |
156 | /* | |
157 | * EdDSA point encoding. Used internally, exposed externally. | |
db90b274 | 158 | * Multiplies by C448_EDDSA_ENCODE_RATIO first. |
7324473f MC |
159 | * |
160 | * The multiplication is required because the EdDSA encoding represents | |
161 | * the cofactor information, but the Decaf encoding ignores it (which | |
162 | * is the whole point). So if you decode from EdDSA and re-encode to | |
163 | * EdDSA, the cofactor info must get cleared, because the intermediate | |
164 | * representation doesn't track it. | |
165 | * | |
db90b274 MC |
166 | * The way we handle this is to multiply by C448_EDDSA_DECODE_RATIO when |
167 | * decoding, and by C448_EDDSA_ENCODE_RATIO when encoding. The product of | |
aeeef83c MC |
168 | * these ratios is always exactly the cofactor 4, so the cofactor ends up |
169 | * cleared one way or another. But exactly how that shakes out depends on the | |
170 | * base points specified in RFC 8032. | |
7324473f MC |
171 | * |
172 | * The upshot is that if you pass the Decaf/Ristretto base point to | |
db90b274 | 173 | * this function, you will get C448_EDDSA_ENCODE_RATIO times the |
7324473f MC |
174 | * EdDSA base point. |
175 | * | |
8d55f844 MC |
176 | * enc (out): The encoded point. |
177 | * p (in): The point. | |
205fd638 | 178 | */ |
054d43ff SL |
179 | void |
180 | ossl_curve448_point_mul_by_ratio_and_encode_like_eddsa( | |
db90b274 | 181 | uint8_t enc [EDDSA_448_PUBLIC_BYTES], |
8d55f844 | 182 | const curve448_point_t p); |
7324473f | 183 | |
8d55f844 | 184 | /* |
db90b274 | 185 | * EdDSA point decoding. Multiplies by C448_EDDSA_DECODE_RATIO, and |
8d55f844 | 186 | * ignores cofactor information. |
7324473f | 187 | * |
e7772577 | 188 | * See notes on curve448_point_mul_by_ratio_and_encode_like_eddsa |
7324473f | 189 | * |
8d55f844 MC |
190 | * enc (out): The encoded point. |
191 | * p (in): The point. | |
205fd638 | 192 | */ |
054d43ff SL |
193 | c448_error_t |
194 | ossl_curve448_point_decode_like_eddsa_and_mul_by_ratio( | |
8d55f844 | 195 | curve448_point_t p, |
db90b274 | 196 | const uint8_t enc[EDDSA_448_PUBLIC_BYTES]); |
8d55f844 | 197 | |
8d55f844 MC |
198 | /* |
199 | * EdDSA to ECDH private key conversion | |
7324473f MC |
200 | * Using the appropriate hash function, hash the EdDSA private key |
201 | * and keep only the lower bytes to get the ECDH private key | |
202 | * | |
8d55f844 MC |
203 | * x (out): The ECDH private key as in RFC7748 |
204 | * ed (in): The EdDSA private key | |
7324473f | 205 | */ |
054d43ff SL |
206 | c448_error_t |
207 | ossl_c448_ed448_convert_private_key_to_x448( | |
208 | OSSL_LIB_CTX *ctx, | |
209 | uint8_t x[X448_PRIVATE_BYTES], | |
210 | const uint8_t ed[EDDSA_448_PRIVATE_BYTES], | |
211 | const char *propq); | |
7324473f | 212 | |
ae4186b0 | 213 | #endif /* OSSL_CRYPTO_EC_CURVE448_ED448_H */ |