xref: /freebsd-src/crypto/openssl/crypto/ec/curve448/eddsa.c (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1e71b7053SJung-uk Kim /*
2*b077aed3SPierre Pronchery  * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
3e71b7053SJung-uk Kim  * Copyright 2015-2016 Cryptography Research, Inc.
4e71b7053SJung-uk Kim  *
5*b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
6e71b7053SJung-uk Kim  * this file except in compliance with the License.  You can obtain a copy
7e71b7053SJung-uk Kim  * in the file LICENSE in the source distribution or at
8e71b7053SJung-uk Kim  * https://www.openssl.org/source/license.html
9e71b7053SJung-uk Kim  *
10e71b7053SJung-uk Kim  * Originally written by Mike Hamburg
11e71b7053SJung-uk Kim  */
12e71b7053SJung-uk Kim #include <string.h>
13e71b7053SJung-uk Kim #include <openssl/crypto.h>
14e71b7053SJung-uk Kim #include <openssl/evp.h>
15*b077aed3SPierre Pronchery #include "crypto/ecx.h"
1617f01e99SJung-uk Kim #include "curve448_local.h"
17e71b7053SJung-uk Kim #include "word.h"
18e71b7053SJung-uk Kim #include "ed448.h"
19e71b7053SJung-uk Kim #include "internal/numbers.h"
20e71b7053SJung-uk Kim 
21e71b7053SJung-uk Kim #define COFACTOR 4
22e71b7053SJung-uk Kim 
oneshot_hash(OSSL_LIB_CTX * ctx,uint8_t * out,size_t outlen,const uint8_t * in,size_t inlen,const char * propq)23*b077aed3SPierre Pronchery static c448_error_t oneshot_hash(OSSL_LIB_CTX *ctx, uint8_t *out, size_t outlen,
24*b077aed3SPierre Pronchery                                  const uint8_t *in, size_t inlen,
25*b077aed3SPierre Pronchery                                  const char *propq)
26e71b7053SJung-uk Kim {
27e71b7053SJung-uk Kim     EVP_MD_CTX *hashctx = EVP_MD_CTX_new();
28*b077aed3SPierre Pronchery     EVP_MD *shake256 = NULL;
29*b077aed3SPierre Pronchery     c448_error_t ret = C448_FAILURE;
30e71b7053SJung-uk Kim 
31e71b7053SJung-uk Kim     if (hashctx == NULL)
32e71b7053SJung-uk Kim         return C448_FAILURE;
33e71b7053SJung-uk Kim 
34*b077aed3SPierre Pronchery     shake256 = EVP_MD_fetch(ctx, "SHAKE256", propq);
35*b077aed3SPierre Pronchery     if (shake256 == NULL)
36*b077aed3SPierre Pronchery         goto err;
37e71b7053SJung-uk Kim 
38*b077aed3SPierre Pronchery     if (!EVP_DigestInit_ex(hashctx, shake256, NULL)
39*b077aed3SPierre Pronchery             || !EVP_DigestUpdate(hashctx, in, inlen)
40*b077aed3SPierre Pronchery             || !EVP_DigestFinalXOF(hashctx, out, outlen))
41*b077aed3SPierre Pronchery         goto err;
42*b077aed3SPierre Pronchery 
43*b077aed3SPierre Pronchery     ret = C448_SUCCESS;
44*b077aed3SPierre Pronchery  err:
45e71b7053SJung-uk Kim     EVP_MD_CTX_free(hashctx);
46*b077aed3SPierre Pronchery     EVP_MD_free(shake256);
47*b077aed3SPierre Pronchery     return ret;
48e71b7053SJung-uk Kim }
49e71b7053SJung-uk Kim 
clamp(uint8_t secret_scalar_ser[EDDSA_448_PRIVATE_BYTES])50e71b7053SJung-uk Kim static void clamp(uint8_t secret_scalar_ser[EDDSA_448_PRIVATE_BYTES])
51e71b7053SJung-uk Kim {
52e71b7053SJung-uk Kim     secret_scalar_ser[0] &= -COFACTOR;
53e71b7053SJung-uk Kim     secret_scalar_ser[EDDSA_448_PRIVATE_BYTES - 1] = 0;
54e71b7053SJung-uk Kim     secret_scalar_ser[EDDSA_448_PRIVATE_BYTES - 2] |= 0x80;
55e71b7053SJung-uk Kim }
56e71b7053SJung-uk Kim 
hash_init_with_dom(OSSL_LIB_CTX * ctx,EVP_MD_CTX * hashctx,uint8_t prehashed,uint8_t for_prehash,const uint8_t * context,size_t context_len,const char * propq)57*b077aed3SPierre Pronchery static c448_error_t hash_init_with_dom(OSSL_LIB_CTX *ctx, EVP_MD_CTX *hashctx,
58*b077aed3SPierre Pronchery                                        uint8_t prehashed,
59e71b7053SJung-uk Kim                                        uint8_t for_prehash,
60e71b7053SJung-uk Kim                                        const uint8_t *context,
61*b077aed3SPierre Pronchery                                        size_t context_len,
62*b077aed3SPierre Pronchery                                        const char *propq)
63e71b7053SJung-uk Kim {
6417f01e99SJung-uk Kim #ifdef CHARSET_EBCDIC
6517f01e99SJung-uk Kim     const char dom_s[] = {0x53, 0x69, 0x67, 0x45,
6617f01e99SJung-uk Kim                           0x64, 0x34, 0x34, 0x38, 0x00};
6717f01e99SJung-uk Kim #else
6817f01e99SJung-uk Kim     const char dom_s[] = "SigEd448";
6917f01e99SJung-uk Kim #endif
70e71b7053SJung-uk Kim     uint8_t dom[2];
71*b077aed3SPierre Pronchery     EVP_MD *shake256 = NULL;
72e71b7053SJung-uk Kim 
73e71b7053SJung-uk Kim     if (context_len > UINT8_MAX)
74e71b7053SJung-uk Kim         return C448_FAILURE;
75e71b7053SJung-uk Kim 
76e71b7053SJung-uk Kim     dom[0] = (uint8_t)(2 - (prehashed == 0 ? 1 : 0)
77e71b7053SJung-uk Kim                        - (for_prehash == 0 ? 1 : 0));
78e71b7053SJung-uk Kim     dom[1] = (uint8_t)context_len;
79e71b7053SJung-uk Kim 
80*b077aed3SPierre Pronchery     shake256 = EVP_MD_fetch(ctx, "SHAKE256", propq);
81*b077aed3SPierre Pronchery     if (shake256 == NULL)
82e71b7053SJung-uk Kim         return C448_FAILURE;
83e71b7053SJung-uk Kim 
84*b077aed3SPierre Pronchery     if (!EVP_DigestInit_ex(hashctx, shake256, NULL)
85*b077aed3SPierre Pronchery             || !EVP_DigestUpdate(hashctx, dom_s, strlen(dom_s))
86*b077aed3SPierre Pronchery             || !EVP_DigestUpdate(hashctx, dom, sizeof(dom))
87*b077aed3SPierre Pronchery             || !EVP_DigestUpdate(hashctx, context, context_len)) {
88*b077aed3SPierre Pronchery         EVP_MD_free(shake256);
89*b077aed3SPierre Pronchery         return C448_FAILURE;
90*b077aed3SPierre Pronchery     }
91*b077aed3SPierre Pronchery 
92*b077aed3SPierre Pronchery     EVP_MD_free(shake256);
93e71b7053SJung-uk Kim     return C448_SUCCESS;
94e71b7053SJung-uk Kim }
95e71b7053SJung-uk Kim 
96e71b7053SJung-uk Kim /* In this file because it uses the hash */
97*b077aed3SPierre Pronchery c448_error_t
ossl_c448_ed448_convert_private_key_to_x448(OSSL_LIB_CTX * ctx,uint8_t x[X448_PRIVATE_BYTES],const uint8_t ed[EDDSA_448_PRIVATE_BYTES],const char * propq)98*b077aed3SPierre Pronchery ossl_c448_ed448_convert_private_key_to_x448(
99*b077aed3SPierre Pronchery                             OSSL_LIB_CTX *ctx,
100e71b7053SJung-uk Kim                             uint8_t x[X448_PRIVATE_BYTES],
101*b077aed3SPierre Pronchery                             const uint8_t ed [EDDSA_448_PRIVATE_BYTES],
102*b077aed3SPierre Pronchery                             const char *propq)
103e71b7053SJung-uk Kim {
104e71b7053SJung-uk Kim     /* pass the private key through oneshot_hash function */
105e71b7053SJung-uk Kim     /* and keep the first X448_PRIVATE_BYTES bytes */
106*b077aed3SPierre Pronchery     return oneshot_hash(ctx, x, X448_PRIVATE_BYTES, ed,
107*b077aed3SPierre Pronchery                         EDDSA_448_PRIVATE_BYTES, propq);
108e71b7053SJung-uk Kim }
109e71b7053SJung-uk Kim 
110*b077aed3SPierre Pronchery c448_error_t
ossl_c448_ed448_derive_public_key(OSSL_LIB_CTX * ctx,uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],const uint8_t privkey[EDDSA_448_PRIVATE_BYTES],const char * propq)111*b077aed3SPierre Pronchery ossl_c448_ed448_derive_public_key(
112*b077aed3SPierre Pronchery                         OSSL_LIB_CTX *ctx,
113e71b7053SJung-uk Kim                         uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
114*b077aed3SPierre Pronchery                         const uint8_t privkey[EDDSA_448_PRIVATE_BYTES],
115*b077aed3SPierre Pronchery                         const char *propq)
116e71b7053SJung-uk Kim {
117e71b7053SJung-uk Kim     /* only this much used for keygen */
118e71b7053SJung-uk Kim     uint8_t secret_scalar_ser[EDDSA_448_PRIVATE_BYTES];
119e71b7053SJung-uk Kim     curve448_scalar_t secret_scalar;
120e71b7053SJung-uk Kim     unsigned int c;
121e71b7053SJung-uk Kim     curve448_point_t p;
122e71b7053SJung-uk Kim 
123*b077aed3SPierre Pronchery     if (!oneshot_hash(ctx, secret_scalar_ser, sizeof(secret_scalar_ser),
124*b077aed3SPierre Pronchery                       privkey,
125*b077aed3SPierre Pronchery                       EDDSA_448_PRIVATE_BYTES,
126*b077aed3SPierre Pronchery                       propq))
127e71b7053SJung-uk Kim         return C448_FAILURE;
128e71b7053SJung-uk Kim 
129e71b7053SJung-uk Kim     clamp(secret_scalar_ser);
130e71b7053SJung-uk Kim 
131*b077aed3SPierre Pronchery     ossl_curve448_scalar_decode_long(secret_scalar, secret_scalar_ser,
132e71b7053SJung-uk Kim                                      sizeof(secret_scalar_ser));
133e71b7053SJung-uk Kim 
134e71b7053SJung-uk Kim     /*
135e71b7053SJung-uk Kim      * Since we are going to mul_by_cofactor during encoding, divide by it
136e71b7053SJung-uk Kim      * here. However, the EdDSA base point is not the same as the decaf base
137e71b7053SJung-uk Kim      * point if the sigma isogeny is in use: the EdDSA base point is on
138e71b7053SJung-uk Kim      * Etwist_d/(1-d) and the decaf base point is on Etwist_d, and when
139e71b7053SJung-uk Kim      * converted it effectively picks up a factor of 2 from the isogenies.  So
140e71b7053SJung-uk Kim      * we might start at 2 instead of 1.
141e71b7053SJung-uk Kim      */
142e71b7053SJung-uk Kim     for (c = 1; c < C448_EDDSA_ENCODE_RATIO; c <<= 1)
143*b077aed3SPierre Pronchery         ossl_curve448_scalar_halve(secret_scalar, secret_scalar);
144e71b7053SJung-uk Kim 
145*b077aed3SPierre Pronchery     ossl_curve448_precomputed_scalarmul(p, ossl_curve448_precomputed_base,
146*b077aed3SPierre Pronchery                                         secret_scalar);
147e71b7053SJung-uk Kim 
148*b077aed3SPierre Pronchery     ossl_curve448_point_mul_by_ratio_and_encode_like_eddsa(pubkey, p);
149e71b7053SJung-uk Kim 
150e71b7053SJung-uk Kim     /* Cleanup */
151*b077aed3SPierre Pronchery     ossl_curve448_scalar_destroy(secret_scalar);
152*b077aed3SPierre Pronchery     ossl_curve448_point_destroy(p);
153e71b7053SJung-uk Kim     OPENSSL_cleanse(secret_scalar_ser, sizeof(secret_scalar_ser));
154e71b7053SJung-uk Kim 
155e71b7053SJung-uk Kim     return C448_SUCCESS;
156e71b7053SJung-uk Kim }
157e71b7053SJung-uk Kim 
158*b077aed3SPierre Pronchery c448_error_t
ossl_c448_ed448_sign(OSSL_LIB_CTX * ctx,uint8_t signature[EDDSA_448_SIGNATURE_BYTES],const uint8_t privkey[EDDSA_448_PRIVATE_BYTES],const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],const uint8_t * message,size_t message_len,uint8_t prehashed,const uint8_t * context,size_t context_len,const char * propq)159*b077aed3SPierre Pronchery ossl_c448_ed448_sign(OSSL_LIB_CTX *ctx,
160e71b7053SJung-uk Kim                      uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
161e71b7053SJung-uk Kim                      const uint8_t privkey[EDDSA_448_PRIVATE_BYTES],
162e71b7053SJung-uk Kim                      const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
163e71b7053SJung-uk Kim                      const uint8_t *message, size_t message_len,
164e71b7053SJung-uk Kim                      uint8_t prehashed, const uint8_t *context,
165*b077aed3SPierre Pronchery                      size_t context_len, const char *propq)
166e71b7053SJung-uk Kim {
167e71b7053SJung-uk Kim     curve448_scalar_t secret_scalar;
168e71b7053SJung-uk Kim     EVP_MD_CTX *hashctx = EVP_MD_CTX_new();
169e71b7053SJung-uk Kim     c448_error_t ret = C448_FAILURE;
170e71b7053SJung-uk Kim     curve448_scalar_t nonce_scalar;
171e71b7053SJung-uk Kim     uint8_t nonce_point[EDDSA_448_PUBLIC_BYTES] = { 0 };
172e71b7053SJung-uk Kim     unsigned int c;
173e71b7053SJung-uk Kim     curve448_scalar_t challenge_scalar;
174e71b7053SJung-uk Kim 
175e71b7053SJung-uk Kim     if (hashctx == NULL)
176e71b7053SJung-uk Kim         return C448_FAILURE;
177e71b7053SJung-uk Kim 
178e71b7053SJung-uk Kim     {
179e71b7053SJung-uk Kim         /*
180*b077aed3SPierre Pronchery          * Schedule the secret key, First EDDSA_448_PRIVATE_BYTES is serialized
181e71b7053SJung-uk Kim          * secret scalar,next EDDSA_448_PRIVATE_BYTES bytes is the seed.
182e71b7053SJung-uk Kim          */
183e71b7053SJung-uk Kim         uint8_t expanded[EDDSA_448_PRIVATE_BYTES * 2];
184e71b7053SJung-uk Kim 
185*b077aed3SPierre Pronchery         if (!oneshot_hash(ctx, expanded, sizeof(expanded), privkey,
186*b077aed3SPierre Pronchery                           EDDSA_448_PRIVATE_BYTES, propq))
187e71b7053SJung-uk Kim             goto err;
188e71b7053SJung-uk Kim         clamp(expanded);
189*b077aed3SPierre Pronchery         ossl_curve448_scalar_decode_long(secret_scalar, expanded,
190e71b7053SJung-uk Kim                                          EDDSA_448_PRIVATE_BYTES);
191e71b7053SJung-uk Kim 
192e71b7053SJung-uk Kim         /* Hash to create the nonce */
193*b077aed3SPierre Pronchery         if (!hash_init_with_dom(ctx, hashctx, prehashed, 0, context,
194*b077aed3SPierre Pronchery                                 context_len, propq)
195e71b7053SJung-uk Kim                 || !EVP_DigestUpdate(hashctx,
196e71b7053SJung-uk Kim                                      expanded + EDDSA_448_PRIVATE_BYTES,
197e71b7053SJung-uk Kim                                      EDDSA_448_PRIVATE_BYTES)
198e71b7053SJung-uk Kim                 || !EVP_DigestUpdate(hashctx, message, message_len)) {
199e71b7053SJung-uk Kim             OPENSSL_cleanse(expanded, sizeof(expanded));
200e71b7053SJung-uk Kim             goto err;
201e71b7053SJung-uk Kim         }
202e71b7053SJung-uk Kim         OPENSSL_cleanse(expanded, sizeof(expanded));
203e71b7053SJung-uk Kim     }
204e71b7053SJung-uk Kim 
205e71b7053SJung-uk Kim     /* Decode the nonce */
206e71b7053SJung-uk Kim     {
207e71b7053SJung-uk Kim         uint8_t nonce[2 * EDDSA_448_PRIVATE_BYTES];
208e71b7053SJung-uk Kim 
209e71b7053SJung-uk Kim         if (!EVP_DigestFinalXOF(hashctx, nonce, sizeof(nonce)))
210e71b7053SJung-uk Kim             goto err;
211*b077aed3SPierre Pronchery         ossl_curve448_scalar_decode_long(nonce_scalar, nonce, sizeof(nonce));
212e71b7053SJung-uk Kim         OPENSSL_cleanse(nonce, sizeof(nonce));
213e71b7053SJung-uk Kim     }
214e71b7053SJung-uk Kim 
215e71b7053SJung-uk Kim     {
216e71b7053SJung-uk Kim         /* Scalarmul to create the nonce-point */
217e71b7053SJung-uk Kim         curve448_scalar_t nonce_scalar_2;
218e71b7053SJung-uk Kim         curve448_point_t p;
219e71b7053SJung-uk Kim 
220*b077aed3SPierre Pronchery         ossl_curve448_scalar_halve(nonce_scalar_2, nonce_scalar);
221e71b7053SJung-uk Kim         for (c = 2; c < C448_EDDSA_ENCODE_RATIO; c <<= 1)
222*b077aed3SPierre Pronchery             ossl_curve448_scalar_halve(nonce_scalar_2, nonce_scalar_2);
223e71b7053SJung-uk Kim 
224*b077aed3SPierre Pronchery         ossl_curve448_precomputed_scalarmul(p, ossl_curve448_precomputed_base,
225e71b7053SJung-uk Kim                                             nonce_scalar_2);
226*b077aed3SPierre Pronchery         ossl_curve448_point_mul_by_ratio_and_encode_like_eddsa(nonce_point, p);
227*b077aed3SPierre Pronchery         ossl_curve448_point_destroy(p);
228*b077aed3SPierre Pronchery         ossl_curve448_scalar_destroy(nonce_scalar_2);
229e71b7053SJung-uk Kim     }
230e71b7053SJung-uk Kim 
231e71b7053SJung-uk Kim     {
232e71b7053SJung-uk Kim         uint8_t challenge[2 * EDDSA_448_PRIVATE_BYTES];
233e71b7053SJung-uk Kim 
234e71b7053SJung-uk Kim         /* Compute the challenge */
235*b077aed3SPierre Pronchery         if (!hash_init_with_dom(ctx, hashctx, prehashed, 0, context, context_len,
236*b077aed3SPierre Pronchery                                 propq)
237e71b7053SJung-uk Kim                 || !EVP_DigestUpdate(hashctx, nonce_point, sizeof(nonce_point))
238e71b7053SJung-uk Kim                 || !EVP_DigestUpdate(hashctx, pubkey, EDDSA_448_PUBLIC_BYTES)
239e71b7053SJung-uk Kim                 || !EVP_DigestUpdate(hashctx, message, message_len)
240e71b7053SJung-uk Kim                 || !EVP_DigestFinalXOF(hashctx, challenge, sizeof(challenge)))
241e71b7053SJung-uk Kim             goto err;
242e71b7053SJung-uk Kim 
243*b077aed3SPierre Pronchery         ossl_curve448_scalar_decode_long(challenge_scalar, challenge,
244e71b7053SJung-uk Kim                                          sizeof(challenge));
245e71b7053SJung-uk Kim         OPENSSL_cleanse(challenge, sizeof(challenge));
246e71b7053SJung-uk Kim     }
247e71b7053SJung-uk Kim 
248*b077aed3SPierre Pronchery     ossl_curve448_scalar_mul(challenge_scalar, challenge_scalar, secret_scalar);
249*b077aed3SPierre Pronchery     ossl_curve448_scalar_add(challenge_scalar, challenge_scalar, nonce_scalar);
250e71b7053SJung-uk Kim 
251e71b7053SJung-uk Kim     OPENSSL_cleanse(signature, EDDSA_448_SIGNATURE_BYTES);
252e71b7053SJung-uk Kim     memcpy(signature, nonce_point, sizeof(nonce_point));
253*b077aed3SPierre Pronchery     ossl_curve448_scalar_encode(&signature[EDDSA_448_PUBLIC_BYTES],
254e71b7053SJung-uk Kim                                 challenge_scalar);
255e71b7053SJung-uk Kim 
256*b077aed3SPierre Pronchery     ossl_curve448_scalar_destroy(secret_scalar);
257*b077aed3SPierre Pronchery     ossl_curve448_scalar_destroy(nonce_scalar);
258*b077aed3SPierre Pronchery     ossl_curve448_scalar_destroy(challenge_scalar);
259e71b7053SJung-uk Kim 
260e71b7053SJung-uk Kim     ret = C448_SUCCESS;
261e71b7053SJung-uk Kim  err:
262e71b7053SJung-uk Kim     EVP_MD_CTX_free(hashctx);
263e71b7053SJung-uk Kim     return ret;
264e71b7053SJung-uk Kim }
265e71b7053SJung-uk Kim 
266*b077aed3SPierre Pronchery c448_error_t
ossl_c448_ed448_sign_prehash(OSSL_LIB_CTX * ctx,uint8_t signature[EDDSA_448_SIGNATURE_BYTES],const uint8_t privkey[EDDSA_448_PRIVATE_BYTES],const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],const uint8_t hash[64],const uint8_t * context,size_t context_len,const char * propq)267*b077aed3SPierre Pronchery ossl_c448_ed448_sign_prehash(
268*b077aed3SPierre Pronchery                         OSSL_LIB_CTX *ctx,
269e71b7053SJung-uk Kim                         uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
270e71b7053SJung-uk Kim                         const uint8_t privkey[EDDSA_448_PRIVATE_BYTES],
271e71b7053SJung-uk Kim                         const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
272e71b7053SJung-uk Kim                         const uint8_t hash[64], const uint8_t *context,
273*b077aed3SPierre Pronchery                         size_t context_len, const char *propq)
274e71b7053SJung-uk Kim {
275*b077aed3SPierre Pronchery     return ossl_c448_ed448_sign(ctx, signature, privkey, pubkey, hash, 64, 1,
276*b077aed3SPierre Pronchery                                 context, context_len, propq);
277e71b7053SJung-uk Kim }
278e71b7053SJung-uk Kim 
279*b077aed3SPierre Pronchery c448_error_t
ossl_c448_ed448_verify(OSSL_LIB_CTX * ctx,const uint8_t signature[EDDSA_448_SIGNATURE_BYTES],const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],const uint8_t * message,size_t message_len,uint8_t prehashed,const uint8_t * context,uint8_t context_len,const char * propq)280*b077aed3SPierre Pronchery ossl_c448_ed448_verify(
281*b077aed3SPierre Pronchery                     OSSL_LIB_CTX *ctx,
282e71b7053SJung-uk Kim                     const uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
283e71b7053SJung-uk Kim                     const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
284e71b7053SJung-uk Kim                     const uint8_t *message, size_t message_len,
285e71b7053SJung-uk Kim                     uint8_t prehashed, const uint8_t *context,
286*b077aed3SPierre Pronchery                     uint8_t context_len, const char *propq)
287e71b7053SJung-uk Kim {
288e71b7053SJung-uk Kim     curve448_point_t pk_point, r_point;
2896935a639SJung-uk Kim     c448_error_t error;
290e71b7053SJung-uk Kim     curve448_scalar_t challenge_scalar;
291e71b7053SJung-uk Kim     curve448_scalar_t response_scalar;
2926935a639SJung-uk Kim     /* Order in little endian format */
2936935a639SJung-uk Kim     static const uint8_t order[] = {
2946935a639SJung-uk Kim         0xF3, 0x44, 0x58, 0xAB, 0x92, 0xC2, 0x78, 0x23, 0x55, 0x8F, 0xC5, 0x8D,
2956935a639SJung-uk Kim         0x72, 0xC2, 0x6C, 0x21, 0x90, 0x36, 0xD6, 0xAE, 0x49, 0xDB, 0x4E, 0xC4,
2966935a639SJung-uk Kim         0xE9, 0x23, 0xCA, 0x7C, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
2976935a639SJung-uk Kim         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
2986935a639SJung-uk Kim         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3F, 0x00
2996935a639SJung-uk Kim     };
3006935a639SJung-uk Kim     int i;
3016935a639SJung-uk Kim 
3026935a639SJung-uk Kim     /*
3036935a639SJung-uk Kim      * Check that s (second 57 bytes of the sig) is less than the order. Both
3046935a639SJung-uk Kim      * s and the order are in little-endian format. This can be done in
3056935a639SJung-uk Kim      * variable time, since if this is not the case the signature if publicly
3066935a639SJung-uk Kim      * invalid.
3076935a639SJung-uk Kim      */
3086935a639SJung-uk Kim     for (i = EDDSA_448_PUBLIC_BYTES - 1; i >= 0; i--) {
3096935a639SJung-uk Kim         if (signature[i + EDDSA_448_PUBLIC_BYTES] > order[i])
3106935a639SJung-uk Kim             return C448_FAILURE;
3116935a639SJung-uk Kim         if (signature[i + EDDSA_448_PUBLIC_BYTES] < order[i])
3126935a639SJung-uk Kim             break;
3136935a639SJung-uk Kim     }
3146935a639SJung-uk Kim     if (i < 0)
3156935a639SJung-uk Kim         return C448_FAILURE;
3166935a639SJung-uk Kim 
3176935a639SJung-uk Kim     error =
318*b077aed3SPierre Pronchery         ossl_curve448_point_decode_like_eddsa_and_mul_by_ratio(pk_point, pubkey);
319e71b7053SJung-uk Kim 
320e71b7053SJung-uk Kim     if (C448_SUCCESS != error)
321e71b7053SJung-uk Kim         return error;
322e71b7053SJung-uk Kim 
323e71b7053SJung-uk Kim     error =
324*b077aed3SPierre Pronchery         ossl_curve448_point_decode_like_eddsa_and_mul_by_ratio(r_point, signature);
325e71b7053SJung-uk Kim     if (C448_SUCCESS != error)
326e71b7053SJung-uk Kim         return error;
327e71b7053SJung-uk Kim 
328e71b7053SJung-uk Kim     {
329e71b7053SJung-uk Kim         /* Compute the challenge */
330e71b7053SJung-uk Kim         EVP_MD_CTX *hashctx = EVP_MD_CTX_new();
331e71b7053SJung-uk Kim         uint8_t challenge[2 * EDDSA_448_PRIVATE_BYTES];
332e71b7053SJung-uk Kim 
333e71b7053SJung-uk Kim         if (hashctx == NULL
334*b077aed3SPierre Pronchery                 || !hash_init_with_dom(ctx, hashctx, prehashed, 0, context,
335*b077aed3SPierre Pronchery                                        context_len, propq)
336e71b7053SJung-uk Kim                 || !EVP_DigestUpdate(hashctx, signature, EDDSA_448_PUBLIC_BYTES)
337e71b7053SJung-uk Kim                 || !EVP_DigestUpdate(hashctx, pubkey, EDDSA_448_PUBLIC_BYTES)
338e71b7053SJung-uk Kim                 || !EVP_DigestUpdate(hashctx, message, message_len)
339e71b7053SJung-uk Kim                 || !EVP_DigestFinalXOF(hashctx, challenge, sizeof(challenge))) {
340e71b7053SJung-uk Kim             EVP_MD_CTX_free(hashctx);
341e71b7053SJung-uk Kim             return C448_FAILURE;
342e71b7053SJung-uk Kim         }
343e71b7053SJung-uk Kim 
344e71b7053SJung-uk Kim         EVP_MD_CTX_free(hashctx);
345*b077aed3SPierre Pronchery         ossl_curve448_scalar_decode_long(challenge_scalar, challenge,
346e71b7053SJung-uk Kim                                          sizeof(challenge));
347e71b7053SJung-uk Kim         OPENSSL_cleanse(challenge, sizeof(challenge));
348e71b7053SJung-uk Kim     }
349*b077aed3SPierre Pronchery     ossl_curve448_scalar_sub(challenge_scalar, ossl_curve448_scalar_zero,
350e71b7053SJung-uk Kim                              challenge_scalar);
351e71b7053SJung-uk Kim 
352*b077aed3SPierre Pronchery     ossl_curve448_scalar_decode_long(response_scalar,
353e71b7053SJung-uk Kim                                      &signature[EDDSA_448_PUBLIC_BYTES],
354e71b7053SJung-uk Kim                                      EDDSA_448_PRIVATE_BYTES);
355e71b7053SJung-uk Kim 
356e71b7053SJung-uk Kim     /* pk_point = -c(x(P)) + (cx + k)G = kG */
357*b077aed3SPierre Pronchery     ossl_curve448_base_double_scalarmul_non_secret(pk_point,
358e71b7053SJung-uk Kim                                                    response_scalar,
359e71b7053SJung-uk Kim                                                    pk_point, challenge_scalar);
360*b077aed3SPierre Pronchery     return c448_succeed_if(ossl_curve448_point_eq(pk_point, r_point));
361e71b7053SJung-uk Kim }
362e71b7053SJung-uk Kim 
363*b077aed3SPierre Pronchery c448_error_t
ossl_c448_ed448_verify_prehash(OSSL_LIB_CTX * ctx,const uint8_t signature[EDDSA_448_SIGNATURE_BYTES],const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],const uint8_t hash[64],const uint8_t * context,uint8_t context_len,const char * propq)364*b077aed3SPierre Pronchery ossl_c448_ed448_verify_prehash(
365*b077aed3SPierre Pronchery                     OSSL_LIB_CTX *ctx,
366e71b7053SJung-uk Kim                     const uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
367e71b7053SJung-uk Kim                     const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
368e71b7053SJung-uk Kim                     const uint8_t hash[64], const uint8_t *context,
369*b077aed3SPierre Pronchery                     uint8_t context_len, const char *propq)
370e71b7053SJung-uk Kim {
371*b077aed3SPierre Pronchery     return ossl_c448_ed448_verify(ctx, signature, pubkey, hash, 64, 1, context,
372*b077aed3SPierre Pronchery                                   context_len, propq);
373e71b7053SJung-uk Kim }
374e71b7053SJung-uk Kim 
375*b077aed3SPierre Pronchery int
ossl_ed448_sign(OSSL_LIB_CTX * ctx,uint8_t * out_sig,const uint8_t * message,size_t message_len,const uint8_t public_key[57],const uint8_t private_key[57],const uint8_t * context,size_t context_len,const char * propq)376*b077aed3SPierre Pronchery ossl_ed448_sign(OSSL_LIB_CTX *ctx, uint8_t *out_sig, const uint8_t *message,
377*b077aed3SPierre Pronchery                 size_t message_len, const uint8_t public_key[57],
378*b077aed3SPierre Pronchery                 const uint8_t private_key[57], const uint8_t *context,
379*b077aed3SPierre Pronchery                 size_t context_len, const char *propq)
380e71b7053SJung-uk Kim {
381*b077aed3SPierre Pronchery     return ossl_c448_ed448_sign(ctx, out_sig, private_key, public_key, message,
382*b077aed3SPierre Pronchery                                 message_len, 0, context, context_len,
383*b077aed3SPierre Pronchery                                 propq) == C448_SUCCESS;
384e71b7053SJung-uk Kim }
385e71b7053SJung-uk Kim 
386*b077aed3SPierre Pronchery int
ossl_ed448_verify(OSSL_LIB_CTX * ctx,const uint8_t * message,size_t message_len,const uint8_t signature[114],const uint8_t public_key[57],const uint8_t * context,size_t context_len,const char * propq)387*b077aed3SPierre Pronchery ossl_ed448_verify(OSSL_LIB_CTX *ctx, const uint8_t *message, size_t message_len,
388e71b7053SJung-uk Kim                   const uint8_t signature[114], const uint8_t public_key[57],
389*b077aed3SPierre Pronchery                   const uint8_t *context, size_t context_len, const char *propq)
390e71b7053SJung-uk Kim {
391*b077aed3SPierre Pronchery     return ossl_c448_ed448_verify(ctx, signature, public_key, message,
392*b077aed3SPierre Pronchery                                   message_len, 0, context, (uint8_t)context_len,
393*b077aed3SPierre Pronchery                                   propq) == C448_SUCCESS;
394e71b7053SJung-uk Kim }
395e71b7053SJung-uk Kim 
396*b077aed3SPierre Pronchery int
ossl_ed448ph_sign(OSSL_LIB_CTX * ctx,uint8_t * out_sig,const uint8_t hash[64],const uint8_t public_key[57],const uint8_t private_key[57],const uint8_t * context,size_t context_len,const char * propq)397*b077aed3SPierre Pronchery ossl_ed448ph_sign(OSSL_LIB_CTX *ctx, uint8_t *out_sig, const uint8_t hash[64],
398e71b7053SJung-uk Kim                   const uint8_t public_key[57], const uint8_t private_key[57],
399*b077aed3SPierre Pronchery                   const uint8_t *context, size_t context_len, const char *propq)
400e71b7053SJung-uk Kim {
401*b077aed3SPierre Pronchery     return ossl_c448_ed448_sign_prehash(ctx, out_sig, private_key, public_key,
402*b077aed3SPierre Pronchery                                         hash, context, context_len,
403*b077aed3SPierre Pronchery                                         propq) == C448_SUCCESS;
404e71b7053SJung-uk Kim }
405e71b7053SJung-uk Kim 
406*b077aed3SPierre Pronchery int
ossl_ed448ph_verify(OSSL_LIB_CTX * ctx,const uint8_t hash[64],const uint8_t signature[114],const uint8_t public_key[57],const uint8_t * context,size_t context_len,const char * propq)407*b077aed3SPierre Pronchery ossl_ed448ph_verify(OSSL_LIB_CTX *ctx, const uint8_t hash[64],
408*b077aed3SPierre Pronchery                     const uint8_t signature[114], const uint8_t public_key[57],
409*b077aed3SPierre Pronchery                     const uint8_t *context, size_t context_len,
410*b077aed3SPierre Pronchery                     const char *propq)
411e71b7053SJung-uk Kim {
412*b077aed3SPierre Pronchery     return ossl_c448_ed448_verify_prehash(ctx, signature, public_key, hash,
413*b077aed3SPierre Pronchery                                           context, (uint8_t)context_len,
414*b077aed3SPierre Pronchery                                           propq) == C448_SUCCESS;
415e71b7053SJung-uk Kim }
416e71b7053SJung-uk Kim 
417*b077aed3SPierre Pronchery int
ossl_ed448_public_from_private(OSSL_LIB_CTX * ctx,uint8_t out_public_key[57],const uint8_t private_key[57],const char * propq)418*b077aed3SPierre Pronchery ossl_ed448_public_from_private(OSSL_LIB_CTX *ctx, uint8_t out_public_key[57],
419*b077aed3SPierre Pronchery                                const uint8_t private_key[57], const char *propq)
420e71b7053SJung-uk Kim {
421*b077aed3SPierre Pronchery     return ossl_c448_ed448_derive_public_key(ctx, out_public_key, private_key,
422*b077aed3SPierre Pronchery                                              propq) == C448_SUCCESS;
423e71b7053SJung-uk Kim }
424