Some style fixes
[openssl.git] / crypto / ec / curve448 / eddsa.c
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 #include <openssl/crypto.h>
13 #include <openssl/evp.h>
14
15 #include "curve448_lcl.h"
16 #include "word.h"
17 #include "ed448.h"
18 #include <string.h>
19 #include "internal/numbers.h"
20
21 #define COFACTOR 4
22
23 static c448_error_t oneshot_hash(uint8_t *out, size_t outlen,
24                                  const uint8_t *in, size_t inlen)
25 {
26     EVP_MD_CTX *hashctx = EVP_MD_CTX_new();
27
28     if (hashctx == NULL)
29         return C448_FAILURE;
30
31     if (!EVP_DigestInit_ex(hashctx, EVP_shake256(), NULL)
32             || !EVP_DigestUpdate(hashctx, in, inlen)
33             || !EVP_DigestFinalXOF(hashctx, out, outlen)) {
34         EVP_MD_CTX_free(hashctx);
35         return C448_FAILURE;
36     }
37
38     EVP_MD_CTX_free(hashctx);
39     return C448_SUCCESS;
40 }
41
42 static void clamp(uint8_t secret_scalar_ser[EDDSA_448_PRIVATE_BYTES])
43 {
44     uint8_t hibit = (1 << 0) >> 1;
45
46     /* Blarg */
47     secret_scalar_ser[0] &= -COFACTOR;
48     if (hibit == 0) {
49         secret_scalar_ser[EDDSA_448_PRIVATE_BYTES - 1] = 0;
50         secret_scalar_ser[EDDSA_448_PRIVATE_BYTES - 2] |= 0x80;
51     } else {
52         secret_scalar_ser[EDDSA_448_PRIVATE_BYTES - 1] &= hibit - 1;
53         secret_scalar_ser[EDDSA_448_PRIVATE_BYTES - 1] |= hibit;
54     }
55 }
56
57 static c448_error_t hash_init_with_dom(EVP_MD_CTX *hashctx, uint8_t prehashed,
58                                        uint8_t for_prehash,
59                                        const uint8_t *context,
60                                        size_t context_len)
61 {
62     const char *dom_s = "SigEd448";
63     uint8_t dom[2];
64
65     dom[0] = 2 + word_is_zero(prehashed) + word_is_zero(for_prehash);
66     dom[1] = (uint8_t)context_len;
67
68     if (context_len > UINT8_MAX)
69         return C448_FAILURE;
70
71     if (!EVP_DigestInit_ex(hashctx, EVP_shake256(), NULL)
72             || !EVP_DigestUpdate(hashctx, dom_s, strlen(dom_s))
73             || !EVP_DigestUpdate(hashctx, dom, sizeof(dom))
74             || !EVP_DigestUpdate(hashctx, context, context_len))
75         return C448_FAILURE;
76
77     return C448_SUCCESS;
78 }
79
80 /* In this file because it uses the hash */
81 c448_error_t c448_ed448_convert_private_key_to_x448(
82                             uint8_t x[X448_PRIVATE_BYTES],
83                             const uint8_t ed [EDDSA_448_PRIVATE_BYTES])
84 {
85     /* pass the private key through oneshot_hash function */
86     /* and keep the first X448_PRIVATE_BYTES bytes */
87     return oneshot_hash(x, X448_PRIVATE_BYTES, ed,
88                         EDDSA_448_PRIVATE_BYTES);
89 }
90
91 c448_error_t c448_ed448_derive_public_key(
92                         uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
93                         const uint8_t privkey[EDDSA_448_PRIVATE_BYTES])
94 {
95     /* only this much used for keygen */
96     uint8_t secret_scalar_ser[EDDSA_448_PRIVATE_BYTES];
97     curve448_scalar_t secret_scalar;
98     unsigned int c;
99     curve448_point_t p;
100
101     if (!oneshot_hash(secret_scalar_ser, sizeof(secret_scalar_ser), privkey,
102                       EDDSA_448_PRIVATE_BYTES))
103         return C448_FAILURE;
104
105     clamp(secret_scalar_ser);
106
107     curve448_scalar_decode_long(secret_scalar, secret_scalar_ser,
108                                 sizeof(secret_scalar_ser));
109
110     /*
111      * Since we are going to mul_by_cofactor during encoding, divide by it
112      * here. However, the EdDSA base point is not the same as the decaf base
113      * point if the sigma isogeny is in use: the EdDSA base point is on
114      * Etwist_d/(1-d) and the decaf base point is on Etwist_d, and when
115      * converted it effectively picks up a factor of 2 from the isogenies.  So
116      * we might start at 2 instead of 1.
117      */
118     for (c = 1; c < C448_EDDSA_ENCODE_RATIO; c <<= 1)
119         curve448_scalar_halve(secret_scalar, secret_scalar);
120
121     curve448_precomputed_scalarmul(p, curve448_precomputed_base, secret_scalar);
122
123     curve448_point_mul_by_ratio_and_encode_like_eddsa(pubkey, p);
124
125     /* Cleanup */
126     curve448_scalar_destroy(secret_scalar);
127     curve448_point_destroy(p);
128     OPENSSL_cleanse(secret_scalar_ser, sizeof(secret_scalar_ser));
129
130     return C448_SUCCESS;
131 }
132
133 c448_error_t c448_ed448_sign(
134                         uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
135                         const uint8_t privkey[EDDSA_448_PRIVATE_BYTES],
136                         const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
137                         const uint8_t *message, size_t message_len,
138                         uint8_t prehashed, const uint8_t *context,
139                         size_t context_len)
140 {
141     curve448_scalar_t secret_scalar;
142     EVP_MD_CTX *hashctx = EVP_MD_CTX_new();
143     c448_error_t ret = C448_FAILURE;
144     curve448_scalar_t nonce_scalar;
145     uint8_t nonce_point[EDDSA_448_PUBLIC_BYTES] = { 0 };
146     unsigned int c;
147     curve448_scalar_t challenge_scalar;
148
149     if (hashctx == NULL)
150         return C448_FAILURE;
151
152     {
153         /*
154          * Schedule the secret key, First EDDSA_448_PRIVATE_BYTES is serialised
155          * secret scalar,next EDDSA_448_PRIVATE_BYTES bytes is the seed.
156          */
157         uint8_t expanded[EDDSA_448_PRIVATE_BYTES * 2];
158
159         if (!oneshot_hash(expanded, sizeof(expanded), privkey,
160                           EDDSA_448_PRIVATE_BYTES))
161             goto err;
162         clamp(expanded);
163         curve448_scalar_decode_long(secret_scalar, expanded,
164                                     EDDSA_448_PRIVATE_BYTES);
165
166         /* Hash to create the nonce */
167         if (!hash_init_with_dom(hashctx, prehashed, 0, context, context_len)
168                 || !EVP_DigestUpdate(hashctx,
169                                      expanded + EDDSA_448_PRIVATE_BYTES,
170                                      EDDSA_448_PRIVATE_BYTES)
171                 || !EVP_DigestUpdate(hashctx, message, message_len)) {
172                 OPENSSL_cleanse(expanded, sizeof(expanded));
173             goto err;
174         }
175         OPENSSL_cleanse(expanded, sizeof(expanded));
176     }
177
178     /* Decode the nonce */
179     {
180         uint8_t nonce[2 * EDDSA_448_PRIVATE_BYTES];
181
182         if (!EVP_DigestFinalXOF(hashctx, nonce, sizeof(nonce)))
183             goto err;
184         curve448_scalar_decode_long(nonce_scalar, nonce, sizeof(nonce));
185         OPENSSL_cleanse(nonce, sizeof(nonce));
186     }
187
188     {
189         /* Scalarmul to create the nonce-point */
190         curve448_scalar_t nonce_scalar_2;
191         curve448_point_t p;
192
193         curve448_scalar_halve(nonce_scalar_2, nonce_scalar);
194         for (c = 2; c < C448_EDDSA_ENCODE_RATIO; c <<= 1) {
195             curve448_scalar_halve(nonce_scalar_2, nonce_scalar_2);
196         }
197
198         curve448_precomputed_scalarmul(p, curve448_precomputed_base,
199                                        nonce_scalar_2);
200         curve448_point_mul_by_ratio_and_encode_like_eddsa(nonce_point, p);
201         curve448_point_destroy(p);
202         curve448_scalar_destroy(nonce_scalar_2);
203     }
204
205     {
206         uint8_t challenge[2 * EDDSA_448_PRIVATE_BYTES];
207
208         /* Compute the challenge */
209         if (!hash_init_with_dom(hashctx, prehashed, 0, context, context_len)
210             || !EVP_DigestUpdate(hashctx, nonce_point, sizeof(nonce_point))
211             || !EVP_DigestUpdate(hashctx, pubkey, EDDSA_448_PUBLIC_BYTES)
212             || !EVP_DigestUpdate(hashctx, message, message_len)
213             || !EVP_DigestFinalXOF(hashctx, challenge, sizeof(challenge)))
214             goto err;
215
216         curve448_scalar_decode_long(challenge_scalar, challenge,
217                                     sizeof(challenge));
218         OPENSSL_cleanse(challenge, sizeof(challenge));
219     }
220
221     curve448_scalar_mul(challenge_scalar, challenge_scalar, secret_scalar);
222     curve448_scalar_add(challenge_scalar, challenge_scalar, nonce_scalar);
223
224     OPENSSL_cleanse(signature, EDDSA_448_SIGNATURE_BYTES);
225     memcpy(signature, nonce_point, sizeof(nonce_point));
226     curve448_scalar_encode(&signature[EDDSA_448_PUBLIC_BYTES],
227                            challenge_scalar);
228
229     curve448_scalar_destroy(secret_scalar);
230     curve448_scalar_destroy(nonce_scalar);
231     curve448_scalar_destroy(challenge_scalar);
232
233     ret = C448_SUCCESS;
234  err:
235     EVP_MD_CTX_free(hashctx);
236     return ret;
237 }
238
239 c448_error_t c448_ed448_sign_prehash(
240                         uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
241                         const uint8_t privkey[EDDSA_448_PRIVATE_BYTES],
242                         const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
243                         const uint8_t hash[64], const uint8_t *context,
244                         size_t context_len)
245 {
246     return c448_ed448_sign(signature, privkey, pubkey, hash, 64, 1, context,
247                            context_len);
248 }
249
250 c448_error_t c448_ed448_verify(
251                     const uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
252                     const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
253                     const uint8_t *message, size_t message_len,
254                     uint8_t prehashed, const uint8_t *context,
255                     uint8_t context_len)
256 {
257     curve448_point_t pk_point, r_point;
258     c448_error_t error =
259         curve448_point_decode_like_eddsa_and_mul_by_ratio(pk_point, pubkey);
260     curve448_scalar_t challenge_scalar;
261     curve448_scalar_t response_scalar;
262     unsigned int c;
263
264     if (C448_SUCCESS != error)
265         return error;
266
267     error =
268         curve448_point_decode_like_eddsa_and_mul_by_ratio(r_point, signature);
269     if (C448_SUCCESS != error)
270         return error;
271
272     {
273         /* Compute the challenge */
274         EVP_MD_CTX *hashctx = EVP_MD_CTX_new();
275         uint8_t challenge[2 * EDDSA_448_PRIVATE_BYTES];
276
277         if (hashctx == NULL
278                 || !hash_init_with_dom(hashctx, prehashed, 0, context,
279                                        context_len)
280                 || !EVP_DigestUpdate(hashctx, signature, EDDSA_448_PUBLIC_BYTES)
281                 || !EVP_DigestUpdate(hashctx, pubkey, EDDSA_448_PUBLIC_BYTES)
282                 || !EVP_DigestUpdate(hashctx, message, message_len)
283                 || !EVP_DigestFinalXOF(hashctx, challenge, sizeof(challenge))) {
284             EVP_MD_CTX_free(hashctx);
285             return C448_FAILURE;
286         }
287
288         EVP_MD_CTX_free(hashctx);
289         curve448_scalar_decode_long(challenge_scalar, challenge,
290                                     sizeof(challenge));
291         OPENSSL_cleanse(challenge, sizeof(challenge));
292     }
293     curve448_scalar_sub(challenge_scalar, curve448_scalar_zero,
294                         challenge_scalar);
295
296     curve448_scalar_decode_long(response_scalar,
297                                 &signature[EDDSA_448_PUBLIC_BYTES],
298                                 EDDSA_448_PRIVATE_BYTES);
299
300     for (c = 1; c < C448_EDDSA_DECODE_RATIO; c <<= 1)
301         curve448_scalar_add(response_scalar, response_scalar, response_scalar);
302
303     /* pk_point = -c(x(P)) + (cx + k)G = kG */
304     curve448_base_double_scalarmul_non_secret(pk_point,
305                                               response_scalar,
306                                               pk_point, challenge_scalar);
307     return c448_succeed_if(curve448_point_eq(pk_point, r_point));
308 }
309
310 c448_error_t c448_ed448_verify_prehash(
311                     const uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
312                     const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
313                     const uint8_t hash[64], const uint8_t *context,
314                     uint8_t context_len)
315 {
316     c448_error_t ret;
317
318     ret = c448_ed448_verify(signature, pubkey, hash, 64, 1, context,
319                             context_len);
320
321     return ret;
322 }
323
324 int ED448_sign(uint8_t *out_sig, const uint8_t *message, size_t message_len,
325                const uint8_t public_key[57], const uint8_t private_key[57],
326                const uint8_t *context, size_t context_len)
327 {
328
329     return c448_ed448_sign(out_sig, private_key, public_key, message,
330                            message_len, 0, context, context_len)
331         == C448_SUCCESS;
332 }
333
334 int ED448_verify(const uint8_t *message, size_t message_len,
335                  const uint8_t signature[114], const uint8_t public_key[57],
336                  const uint8_t *context, size_t context_len)
337 {
338     return c448_ed448_verify(signature, public_key, message, message_len, 0,
339                              context, (uint8_t)context_len) == C448_SUCCESS;
340 }
341
342 int ED448ph_sign(uint8_t *out_sig, const uint8_t hash[64],
343                  const uint8_t public_key[57], const uint8_t private_key[57],
344                  const uint8_t *context, size_t context_len)
345 {
346     return c448_ed448_sign_prehash(out_sig, private_key, public_key, hash,
347                                    context, context_len) == C448_SUCCESS;
348
349 }
350
351 int ED448ph_verify(const uint8_t hash[64], const uint8_t signature[114],
352                    const uint8_t public_key[57], const uint8_t *context,
353                    size_t context_len)
354 {
355     return c448_ed448_verify_prehash(signature, public_key, hash, context,
356                                      (uint8_t)context_len) == C448_SUCCESS;
357 }
358
359 int ED448_public_from_private(uint8_t out_public_key[57],
360                               const uint8_t private_key[57])
361 {
362     return c448_ed448_derive_public_key(out_public_key, private_key)
363         == C448_SUCCESS;
364 }