summaryrefslogtreecommitdiff
path: root/FreeRTOS-Plus/Source/WolfSSL/wolfcrypt/src/ed25519.c
blob: ba0dcbe534e4309482bb03b193ab71b9281ebe3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/* ed25519.c
 *
 * Copyright (C) 2006-2015 wolfSSL Inc.
 *
 * This file is part of wolfSSL. (formerly known as CyaSSL)
 *
 * wolfSSL is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * wolfSSL is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */

 /* Based On Daniel J Bernstein's ed25519 Public Domain ref10 work. */

#ifdef HAVE_CONFIG_H
    #include <config.h>
#endif

/* in case user set HAVE_ED25519 there */
#include <wolfssl/wolfcrypt/settings.h>

#ifdef HAVE_ED25519

#include <wolfssl/wolfcrypt/ed25519.h>
#include <wolfssl/wolfcrypt/error-crypt.h>
#ifdef NO_INLINE
    #include <wolfssl/wolfcrypt/misc.h>
#else
    #include <wolfcrypt/src/misc.c>
#endif


/*
    generate an ed25519 key pair.
    returns 0 on success
 */
int wc_ed25519_make_key(RNG* rng, int keySz, ed25519_key* key)
{
    byte  az[64];
    int   ret;
    ge_p3 A;

    if (rng == NULL || key == NULL)
        return BAD_FUNC_ARG;

    /* ed25519 has 32 byte key sizes */
    if (keySz != ED25519_KEY_SIZE)
        return BAD_FUNC_ARG;

    ret = 0;
    ret |= wc_RNG_GenerateBlock(rng, key->k, 32);
    ret |= wc_Sha512Hash(key->k, 32, az);
    az[0] &= 248;
    az[31] &= 63;
    az[31] |= 64;

    ge_scalarmult_base(&A, az);
    ge_p3_tobytes(key->p, &A);
    XMEMMOVE(key->k + 32, key->p, 32);

    return ret;
}


/*
    in     contains the message to sign
    inlen  is the length of the message to sign
    out    is the buffer to write the signature
    outlen [in/out] input size of out buf
                    output gets set as the final length of out
    key    is the ed25519 key to use when signing
    return 0 on success
 */
int wc_ed25519_sign_msg(const byte* in, word32 inlen, byte* out,
                        word32 *outlen, ed25519_key* key)
{
    ge_p3  R;
    byte   nonce[SHA512_DIGEST_SIZE];
    byte   hram[SHA512_DIGEST_SIZE];
    byte   az[64];
    word32 sigSz;
    Sha512 sha;
    int    ret = 0;

    /* sanity check on arguments */
    if (in == NULL || out == NULL || outlen == NULL || key == NULL)
        return BAD_FUNC_ARG;

    /* check and set up out length */
    ret   = 0;
    sigSz = wc_ed25519_sig_size(key);
    if (*outlen < sigSz)
        return BAD_FUNC_ARG;
    *outlen = sigSz;

    /* step 1: create nonce to use where nonce is r in
       r = H(h_b, ... ,h_2b-1,M) */
    ret |= wc_Sha512Hash(key->k,32,az);
    az[0]  &= 248;
    az[31] &= 63;
    az[31] |= 64;
    ret |= wc_InitSha512(&sha);
    ret |= wc_Sha512Update(&sha, az + 32, 32);
    ret |= wc_Sha512Update(&sha, in, inlen);
    ret |= wc_Sha512Final(&sha, nonce);
    sc_reduce(nonce);

    /* step 2: computing R = rB where rB is the scalar multiplication of
       r and B */
    ge_scalarmult_base(&R,nonce);
    ge_p3_tobytes(out,&R);

    /* step 3: hash R + public key + message getting H(R,A,M) then
       creating S = (r + H(R,A,M)a) mod l */
    ret |= wc_InitSha512(&sha);
    ret |= wc_Sha512Update(&sha, out, 32);
    ret |= wc_Sha512Update(&sha, key->p, 32);
    ret |= wc_Sha512Update(&sha, in, inlen);
    ret |= wc_Sha512Final(&sha, hram);
    sc_reduce(hram);
    sc_muladd(out + 32, hram, az, nonce);

    return ret;
}


/*
   sig     is array of bytes containing the signature
   siglen  is the length of sig byte array
   msg     the array of bytes containing the message
   msglen  length of msg array
   stat    will be 1 on successful verify and 0 on unsuccessful
*/
int wc_ed25519_verify_msg(byte* sig, word32 siglen, const byte* msg,
                          word32 msglen, int* stat, ed25519_key* key)
{
    byte   rcheck[32];
    byte   h[SHA512_DIGEST_SIZE];
    ge_p3  A;
    ge_p2  R;
    word32 sigSz;
    int    ret;
    Sha512 sha;

    /* sanity check on arguments */
    if (sig == NULL || msg == NULL || stat == NULL || key == NULL)
        return BAD_FUNC_ARG;

    ret   = 0;
    *stat = 0;
    sigSz = wc_ed25519_size(key);

    /* check on basics needed to verify signature */
    if (siglen < sigSz)
        return BAD_FUNC_ARG;
    if (sig[63] & 224)
        return BAD_FUNC_ARG;

    /* uncompress A (public key), test if valid, and negate it */
    if (ge_frombytes_negate_vartime(&A, key->p) != 0)
        return BAD_FUNC_ARG;

    /* find H(R,A,M) and store it as h */
    ret |= wc_InitSha512(&sha);
    ret |= wc_Sha512Update(&sha, sig,    32);
    ret |= wc_Sha512Update(&sha, key->p, 32);
    ret |= wc_Sha512Update(&sha, msg,    msglen);
    ret |= wc_Sha512Final(&sha,  h);
    sc_reduce(h);

    /*
       Uses a fast single-signature verification SB = R + H(R,A,M)A becomes
       SB - H(R,A,M)A saving decompression of R
    */
    ret |= ge_double_scalarmult_vartime(&R, h, &A, sig + 32);
    ge_tobytes(rcheck, &R);

    /* comparison of R created to R in sig */
    ret  |= ConstantCompare(rcheck, sig, 32);

    *stat = (ret == 0)? 1: 0;

    return ret;
}


/* initialize information and memory for key */
int wc_ed25519_init(ed25519_key* key)
{
    if (key == NULL)
        return BAD_FUNC_ARG;

    XMEMSET(key, 0, sizeof(ed25519_key));

    return 0;
}


/* clear memory of key */
void wc_ed25519_free(ed25519_key* key)
{
    if (key == NULL)
        return;

    ForceZero(key, sizeof(ed25519_key));
}


/*
    outLen should contain the size of out buffer when input. outLen is than set
    to the final output length.
    returns 0 on success
 */
int wc_ed25519_export_public(ed25519_key* key, byte* out, word32* outLen)
{
    word32 keySz;

    /* sanity check on arguments */
    if (key == NULL || out == NULL || outLen == NULL)
        return BAD_FUNC_ARG;

    keySz = wc_ed25519_size(key);
    if (*outLen < keySz) {
        *outLen = keySz;
        return BUFFER_E;
    }
    *outLen = keySz;
    XMEMCPY(out, key->p, keySz);

    return 0;
}


/*
    Imports a compressed/uncompressed public key.
    in    the byte array containing the public key
    inLen the length of the byte array being passed in
    key   ed25519 key struct to put the public key in
 */
int wc_ed25519_import_public(const byte* in, word32 inLen, ed25519_key* key)
{
    word32 keySz;
    int    ret;

    /* sanity check on arguments */
    if (in == NULL || key == NULL)
        return BAD_FUNC_ARG;

    keySz = wc_ed25519_size(key);

    if (inLen < keySz)
        return BAD_FUNC_ARG;

    /* compressed prefix according to draft
       http://www.ietf.org/id/draft-koch-eddsa-for-openpgp-02.txt */
    if (in[0] == 0x40) {
        /* key is stored in compressed format so just copy in */
        XMEMCPY(key->p, (in + 1), keySz);
        return 0;
    }

    /* importing uncompressed public key */
    if (in[0] == 0x04) {
        /* pass in (x,y) and store compressed key */
        ret = ge_compress_key(key->p, (in+1), (in+1+keySz), keySz);
        return ret;
    }

    /* if not specified compressed or uncompressed check key size
       if key size is equal to compressed key size copy in key */
    if (inLen == keySz) {
        XMEMCPY(key->p, in, keySz);
        return 0;
    }

    /* bad public key format */
    return BAD_FUNC_ARG;
}


/*
    For importing a private key and its associated public key.
 */
int wc_ed25519_import_private_key(const byte* priv, word32 privSz,
                                const byte* pub, word32 pubSz, ed25519_key* key)
{
    word32 keySz;
    int    ret;

    /* sanity check on arguments */
    if (priv == NULL || pub == NULL || key == NULL)
        return BAD_FUNC_ARG;

    keySz = wc_ed25519_size(key);

    /* key size check */
    if (privSz < keySz || pubSz < keySz)
        return BAD_FUNC_ARG;

    XMEMCPY(key->k, priv, keySz);
    ret = wc_ed25519_import_public(pub, pubSz, key);
    XMEMCPY((key->k + keySz), key->p, keySz);

    return ret;
}


/*
    outLen should contain the size of out buffer when input. outLen is than set
    to the final output length.
    returns 0 on success
 */
int wc_ed25519_export_private_only(ed25519_key* key, byte* out, word32* outLen)
{
    word32 keySz;

    /* sanity checks on arguments */
    if (key == NULL || out == NULL || outLen == NULL)
        return BAD_FUNC_ARG;

    keySz = wc_ed25519_size(key);
    if (*outLen < keySz) {
        *outLen = keySz;
        return BUFFER_E;
    }
    *outLen = keySz;
    XMEMCPY(out, key->k, keySz);

    return 0;
}


/* is the compressed key size in bytes */
int wc_ed25519_size(ed25519_key* key)
{
    word32 keySz;

    if (key == NULL)
        return BAD_FUNC_ARG;

    keySz = ED25519_KEY_SIZE;

    return keySz;
}


/* returns the size of signature in bytes */
int wc_ed25519_sig_size(ed25519_key* key)
{
    word32 sigSz;

    if (key == NULL)
        return BAD_FUNC_ARG;

    sigSz = ED25519_SIG_SIZE;

    return sigSz;
}

#endif /* HAVE_ED25519 */