summaryrefslogtreecommitdiff
path: root/FreeRTOS-Plus/Source/WolfSSL/wolfcrypt/src/curve448.c
blob: 135f2380e68a879245bb18916a0b2c7c6671a1c0 (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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/* curve448.c
 *
 * Copyright (C) 2006-2020 wolfSSL Inc.
 *
 * This file is part of wolfSSL.
 *
 * 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-1335, USA
 */

/* Implemented to: RFC 7748 */

/* Based On Daniel J Bernstein's curve25519 Public Domain ref10 work.
 * Reworked for curve448 by Sean Parkinson.
 */

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

#include <wolfssl/wolfcrypt/settings.h>

#ifdef HAVE_CURVE448

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


/* Make a new curve448 private/public key.
 *
 * rng      [in]  Random number generator.
 * keysize  [in]  Size of the key to generate.
 * key      [in]  Curve448 key object.
 * returns BAD_FUNC_ARG when rng or key are NULL,
 *         ECC_BAD_ARG_E when keysize is not CURVE448_KEY_SIZE,
 *         0 otherwise.
 */
int wc_curve448_make_key(WC_RNG* rng, int keysize, curve448_key* key)
{
    unsigned char basepoint[CURVE448_KEY_SIZE] = {5};
    int  ret = 0;

    if ((key == NULL) || (rng == NULL)) {
        ret = BAD_FUNC_ARG;
    }

    /* currently only a key size of 56 bytes is used */
    if ((ret == 0) && (keysize != CURVE448_KEY_SIZE)) {
        ret = ECC_BAD_ARG_E;
    }

    if (ret == 0) {
        fe448_init();

        /* random number for private key */
        ret = wc_RNG_GenerateBlock(rng, key->k, keysize);
    }
    if (ret == 0) {
        /* Clamp the private key */
        key->k[0] &= 0xfc;
        key->k[CURVE448_KEY_SIZE-1] |= 0x80;

        /* compute public key */
        ret = curve448(key->p, key->k, basepoint);
        if (ret != 0) {
            ForceZero(key->k, keysize);
            ForceZero(key->p, keysize);
        }
    }

    return ret;
}

#ifdef HAVE_CURVE448_SHARED_SECRET

/* Calculate the shared secret from the private key and peer's public key.
 * Calculation over curve448.
 * Secret encoded big-endian.
 *
 * private_key  [in]      Curve448 private key.
 * public_key   [in]      Curve448 public key.
 * out          [in]      Array to hold shared secret.
 * outLen       [in/out]  On in, the number of bytes in array.
 *                        On out, the number bytes put into array.
 * returns BAD_FUNC_ARG when a parameter is NULL or outLen is less than
 *         CURVE448_KEY_SIZE,
 *         0 otherwise.
 */
int wc_curve448_shared_secret(curve448_key* private_key,
                              curve448_key* public_key,
                              byte* out, word32* outLen)
{
    return wc_curve448_shared_secret_ex(private_key, public_key, out, outLen,
                                        EC448_BIG_ENDIAN);
}

/* Calculate the shared secret from the private key and peer's public key.
 * Calculation over curve448.
 *
 * private_key  [in]      Curve448 private key.
 * public_key   [in]      Curve448 public key.
 * out          [in]      Array to hold shared secret.
 * outLen       [in/out]  On in, the number of bytes in array.
 *                        On out, the number bytes put into array.
 * endian       [in]      Endianness to use when encoding number in array.
 * returns BAD_FUNC_ARG when a parameter is NULL or outLen is less than
 *         CURVE448_PUB_KEY_SIZE,
 *         0 otherwise.
 */
int wc_curve448_shared_secret_ex(curve448_key* private_key,
                                 curve448_key* public_key,
                                 byte* out, word32* outLen, int endian)
{
    unsigned char o[CURVE448_PUB_KEY_SIZE];
    int ret = 0;
    int i;

    /* sanity check */
    if ((private_key == NULL) || (public_key == NULL) || (out == NULL) ||
                        (outLen == NULL) || (*outLen < CURVE448_PUB_KEY_SIZE)) {
        ret = BAD_FUNC_ARG;
    }

    if (ret == 0) {
        ret = curve448(o, private_key->k, public_key->p);
    }
    if (ret == 0) {
        if (endian == EC448_BIG_ENDIAN) {
            /* put shared secret key in Big Endian format */
            for (i = 0; i < CURVE448_PUB_KEY_SIZE; i++) {
                 out[i] = o[CURVE448_PUB_KEY_SIZE - i -1];
            }
        }
        else {
            /* put shared secret key in Little Endian format */
            XMEMCPY(out, o, CURVE448_PUB_KEY_SIZE);
        }

        *outLen = CURVE448_PUB_KEY_SIZE;
    }

    ForceZero(o, CURVE448_PUB_KEY_SIZE);

    return ret;
}

#endif /* HAVE_CURVE448_SHARED_SECRET */

#ifdef HAVE_CURVE448_KEY_EXPORT

/* Export the curve448 public key.
 * Public key encoded big-endian.
 *
 * key     [in]      Curve448 public key.
 * out     [in]      Array to hold public key.
 * outLen  [in/out]  On in, the number of bytes in array.
 *                   On out, the number bytes put into array.
 * returns BAD_FUNC_ARG when a parameter is NULL,
 *         ECC_BAD_ARG_E when outLen is less than CURVE448_PUB_KEY_SIZE,
 *         0 otherwise.
 */
int wc_curve448_export_public(curve448_key* key, byte* out, word32* outLen)
{
    return wc_curve448_export_public_ex(key, out, outLen, EC448_BIG_ENDIAN);
}

/* Export the curve448 public key.
 *
 * key     [in]      Curve448 public key.
 * out     [in]      Array to hold public key.
 * outLen  [in/out]  On in, the number of bytes in array.
 *                   On out, the number bytes put into array.
 * endian  [in]      Endianness to use when encoding number in array.
 * returns BAD_FUNC_ARG when a parameter is NULL,
 *         ECC_BAD_ARG_E when outLen is less than CURVE448_PUB_KEY_SIZE,
 *         0 otherwise.
 */
int wc_curve448_export_public_ex(curve448_key* key, byte* out, word32* outLen,
                                 int endian)
{
    int ret = 0;
    int i;

    if ((key == NULL) || (out == NULL) || (outLen == NULL)) {
        ret = BAD_FUNC_ARG;
    }

    /* check and set outgoing key size */
    if ((ret == 0) && (*outLen < CURVE448_PUB_KEY_SIZE)) {
        *outLen = CURVE448_PUB_KEY_SIZE;
        ret = ECC_BAD_ARG_E;
    }
    if (ret == 0) {
        *outLen = CURVE448_PUB_KEY_SIZE;

        if (endian == EC448_BIG_ENDIAN) {
            /* read keys in Big Endian format */
            for (i = 0; i < CURVE448_PUB_KEY_SIZE; i++) {
                out[i] = key->p[CURVE448_PUB_KEY_SIZE - i - 1];
            }
        }
        else {
            XMEMCPY(out, key->p, CURVE448_PUB_KEY_SIZE);
        }
    }

    return ret;
}

#endif /* HAVE_CURVE448_KEY_EXPORT */

#ifdef HAVE_CURVE448_KEY_IMPORT

/* Import a curve448 public key from a byte array.
 * Public key encoded in big-endian.
 *
 * in      [in]  Array holding public key.
 * inLen   [in]  Number of bytes of data in array.
 * key     [in]  Curve448 public key.
 * returns BAD_FUNC_ARG when a parameter is NULL,
 *         ECC_BAD_ARG_E when inLen is less than CURVE448_PUB_KEY_SIZE,
 *         0 otherwise.
 */
int wc_curve448_import_public(const byte* in, word32 inLen, curve448_key* key)
{
    return wc_curve448_import_public_ex(in, inLen, key, EC448_BIG_ENDIAN);
}

/* Import a curve448 public key from a byte array.
 *
 * in      [in]  Array holding public key.
 * inLen   [in]  Number of bytes of data in array.
 * key     [in]  Curve448 public key.
 * endian  [in]  Endianness of encoded number in byte array.
 * returns BAD_FUNC_ARG when a parameter is NULL,
 *         ECC_BAD_ARG_E when inLen is less than CURVE448_PUB_KEY_SIZE,
 *         0 otherwise.
 */
int wc_curve448_import_public_ex(const byte* in, word32 inLen,
                                 curve448_key* key, int endian)
{
    int ret = 0;
    int i;

    /* sanity check */
    if ((key == NULL) || (in == NULL)) {
        ret = BAD_FUNC_ARG;
    }

    /* check size of incoming keys */
    if ((ret == 0) && (inLen != CURVE448_PUB_KEY_SIZE)) {
       ret = ECC_BAD_ARG_E;
    }

    if (ret == 0) {
        if (endian == EC448_BIG_ENDIAN) {
            /* read keys in Big Endian format */
            for (i = 0; i < CURVE448_PUB_KEY_SIZE; i++) {
                key->p[i] = in[CURVE448_PUB_KEY_SIZE - i - 1];
            }
        }
        else
            XMEMCPY(key->p, in, inLen);
    }

    return ret;
}

/* Check the public key value (big or little endian)
 *
 * pub     [in]  Public key bytes.
 * pubSz   [in]  Size of public key in bytes.
 * endian  [in]  Public key bytes passed in as big-endian or little-endian.
 * returns BAD_FUNC_ARGS when pub is NULL,
 *         ECC_BAD_ARG_E when key length is not 56 bytes, public key value is
 *         zero or one;
 *         BUFFER_E when size of public key is zero;
 *         0 otherwise.
 */
int wc_curve448_check_public(const byte* pub, word32 pubSz, int endian)
{
    int ret = 0;
    word32 i;

    if (pub == NULL) {
        ret = BAD_FUNC_ARG;
    }

    /* Check for empty key data */
    if ((ret == 0) && (pubSz == 0)) {
        ret = BUFFER_E;
    }

    /* Check key length */
    if ((ret == 0) && (pubSz != CURVE448_PUB_KEY_SIZE)) {
        ret = ECC_BAD_ARG_E;
    }

    if (ret == 0) {
        if (endian == EC448_LITTLE_ENDIAN) {
            /* Check for value of zero or one */
            for (i = pubSz - 1; i > 0; i--) {
                if (pub[i] != 0) {
                    break;
                }
            }
            if ((i == 0) && (pub[0] == 0 || pub[0] == 1)) {
                return ECC_BAD_ARG_E;
            }
        }
        else {
            /* Check for value of zero or one */
            for (i = 0; i < pubSz-1; i++) {
                if (pub[i] != 0) {
                    break;
                }
            }
            if ((i == pubSz - 1) && (pub[i] == 0 || pub[i] == 1)) {
                ret = ECC_BAD_ARG_E;
            }
        }
    }

    return ret;
}

#endif /* HAVE_CURVE448_KEY_IMPORT */


#ifdef HAVE_CURVE448_KEY_EXPORT

/* Export the curve448 private key raw form.
 * Private key encoded big-endian.
 *
 * key     [in]      Curve448 private key.
 * out     [in]      Array to hold private key.
 * outLen  [in/out]  On in, the number of bytes in array.
 *                   On out, the number bytes put into array.
 * returns BAD_FUNC_ARG when a parameter is NULL,
 *         ECC_BAD_ARG_E when outLen is less than CURVE448_KEY_SIZE,
 *         0 otherwise.
 */
int wc_curve448_export_private_raw(curve448_key* key, byte* out, word32* outLen)
{
    return wc_curve448_export_private_raw_ex(key, out, outLen,
                                             EC448_BIG_ENDIAN);
}

/* Export the curve448 private key raw form.
 *
 * key     [in]      Curve448 private key.
 * out     [in]      Array to hold private key.
 * outLen  [in/out]  On in, the number of bytes in array.
 *                   On out, the number bytes put into array.
 * endian  [in]      Endianness to use when encoding number in array.
 * returns BAD_FUNC_ARG when a parameter is NULL,
 *         ECC_BAD_ARG_E when outLen is less than CURVE448_KEY_SIZE,
 *         0 otherwise.
 */
int wc_curve448_export_private_raw_ex(curve448_key* key, byte* out,
                                      word32* outLen, int endian)
{
    int ret = 0;
    int i;

    /* sanity check */
    if ((key == NULL) || (out == NULL) || (outLen == NULL)) {
        ret = BAD_FUNC_ARG;
    }

    /* check size of outgoing buffer */
    if ((ret == 0) && (*outLen < CURVE448_KEY_SIZE)) {
        *outLen = CURVE448_KEY_SIZE;
        ret = ECC_BAD_ARG_E;
    }
    if (ret == 0) {
        *outLen = CURVE448_KEY_SIZE;

        if (endian == EC448_BIG_ENDIAN) {
            /* put the key in Big Endian format */
            for (i = 0; i < CURVE448_KEY_SIZE; i++) {
                out[i] = key->k[CURVE448_KEY_SIZE - i - 1];
            }
        }
        else {
            XMEMCPY(out, key->k, CURVE448_KEY_SIZE);
        }
    }

    return ret;
}

/* Export the curve448 private and public keys in raw form.
 * Private and public key encoded big-endian.
 *
 * key     [in]      Curve448 private key.
 * priv    [in]      Array to hold private key.
 * privSz  [in/out]  On in, the number of bytes in private key array.
 *                   On out, the number bytes put into private key array.
 * pub     [in]      Array to hold public key.
 * pubSz   [in/out]  On in, the number of bytes in public key array.
 *                   On out, the number bytes put into public key array.
 * returns BAD_FUNC_ARG when a parameter is NULL,
 *         ECC_BAD_ARG_E when privSz is less than CURVE448_KEY_SIZE or pubSz is
 *         less than CURVE448_PUB_KEY_SIZE,
 *         0 otherwise.
 */
int wc_curve448_export_key_raw(curve448_key* key, byte* priv, word32 *privSz,
                               byte* pub, word32 *pubSz)
{
    return wc_curve448_export_key_raw_ex(key, priv, privSz, pub, pubSz,
                                         EC448_BIG_ENDIAN);
}

/* Export the curve448 private and public keys in raw form.
 *
 * key     [in]      Curve448 private key.
 * priv    [in]      Array to hold private key.
 * privSz  [in/out]  On in, the number of bytes in private key array.
 *                   On out, the number bytes put into private key array.
 * pub     [in]      Array to hold public key.
 * pubSz   [in/out]  On in, the number of bytes in public key array.
 *                   On out, the number bytes put into public key array.
 * endian  [in]      Endianness to use when encoding number in array.
 * returns BAD_FUNC_ARG when a parameter is NULL,
 *         ECC_BAD_ARG_E when privSz is less than CURVE448_KEY_SIZE or pubSz is
 *         less than CURVE448_PUB_KEY_SIZE,
 *         0 otherwise.
 */
int wc_curve448_export_key_raw_ex(curve448_key* key, byte* priv, word32 *privSz,
                                  byte* pub, word32 *pubSz, int endian)
{
    int ret;

    /* export private part */
    ret = wc_curve448_export_private_raw_ex(key, priv, privSz, endian);
    if (ret == 0) {
        /* export public part */
        ret = wc_curve448_export_public_ex(key, pub, pubSz, endian);
    }

    return ret;
}

#endif /* HAVE_CURVE448_KEY_EXPORT */

#ifdef HAVE_CURVE448_KEY_IMPORT

/* Import curve448 private and public keys from a byte arrays.
 * Private and public keys encoded in big-endian.
 *
 * piv     [in]  Array holding private key.
 * privSz  [in]  Number of bytes of data in private key array.
 * pub     [in]  Array holding public key.
 * pubSz   [in]  Number of bytes of data in public key array.
 * key     [in]  Curve448 private/public key.
 * returns BAD_FUNC_ARG when a parameter is NULL,
 *         ECC_BAD_ARG_E when privSz is less than CURVE448_KEY_SIZE or pubSz is
 *         less than CURVE448_PUB_KEY_SIZE,
 *         0 otherwise.
 */
int wc_curve448_import_private_raw(const byte* priv, word32 privSz,
                                   const byte* pub, word32 pubSz,
                                   curve448_key* key)
{
    return wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz, key,
                                             EC448_BIG_ENDIAN);
}

/* Import curve448 private and public keys from a byte arrays.
 *
 * piv     [in]  Array holding private key.
 * privSz  [in]  Number of bytes of data in private key array.
 * pub     [in]  Array holding public key.
 * pubSz   [in]  Number of bytes of data in public key array.
 * key     [in]  Curve448 private/public key.
 * endian  [in]  Endianness of encoded numbers in byte arrays.
 * returns BAD_FUNC_ARG when a parameter is NULL,
 *         ECC_BAD_ARG_E when privSz is less than CURVE448_KEY_SIZE or pubSz is
 *         less than CURVE448_PUB_KEY_SIZE,
 *         0 otherwise.
 */
int wc_curve448_import_private_raw_ex(const byte* priv, word32 privSz,
                                      const byte* pub, word32 pubSz,
                                      curve448_key* key, int endian)
{
    int ret;

    /* import private part */
    ret = wc_curve448_import_private_ex(priv, privSz, key, endian);
    if (ret == 0) {
        /* import public part */
        return wc_curve448_import_public_ex(pub, pubSz, key, endian);
    }

    return ret;
}

/* Import curve448 private key from a byte array.
 * Private key encoded in big-endian.
 *
 * piv     [in]  Array holding private key.
 * privSz  [in]  Number of bytes of data in private key array.
 * key     [in]  Curve448 private/public key.
 * returns BAD_FUNC_ARG when a parameter is NULL,
 *         ECC_BAD_ARG_E when privSz is less than CURVE448_KEY_SIZE,
 *         0 otherwise.
 */
int wc_curve448_import_private(const byte* priv, word32 privSz,
                               curve448_key* key)
{
    return wc_curve448_import_private_ex(priv, privSz, key, EC448_BIG_ENDIAN);
}

/* Import curve448 private key from a byte array.
 *
 * piv     [in]  Array holding private key.
 * privSz  [in]  Number of bytes of data in private key array.
 * key     [in]  Curve448 private/public key.
 * endian  [in]  Endianness of encoded number in byte array.
 * returns BAD_FUNC_ARG when a parameter is NULL,
 *         ECC_BAD_ARG_E when privSz is less than CURVE448_KEY_SIZE,
 *         0 otherwise.
 */
int wc_curve448_import_private_ex(const byte* priv, word32 privSz,
                                  curve448_key* key, int endian)
{
    int ret = 0;
    int i;

    /* sanity check */
    if ((key == NULL) || (priv == NULL)) {
        ret = BAD_FUNC_ARG;
    }

    /* check size of incoming keys */
    if ((ret == 0) && ((int)privSz != CURVE448_KEY_SIZE)) {
        ret = ECC_BAD_ARG_E;
    }

    if (ret == 0) {
        if (endian == EC448_BIG_ENDIAN) {
            /* read the key in Big Endian format */
            for (i = 0; i < CURVE448_KEY_SIZE; i++) {
                key->k[i] = priv[CURVE448_KEY_SIZE - i - 1];
            }
        }
        else {
            XMEMCPY(key->k, priv, CURVE448_KEY_SIZE);
        }

        /* Clamp the key */
        key->k[0] &= 0xfc;
        key->k[CURVE448_KEY_SIZE-1] |= 0x80;
    }

    return ret;
}

#endif /* HAVE_CURVE448_KEY_IMPORT */


/* Initialize the curve448 key.
 *
 * key  [in]  Curve448 key object.
 * returns BAD_FUNC_ARG when key is NULL,
 *         0 otherwise.
 */
int wc_curve448_init(curve448_key* key)
{
    int ret = 0;

    if (key == NULL) {
       ret = BAD_FUNC_ARG;
    }

    if (ret == 0) {
        XMEMSET(key, 0, sizeof(*key));

        fe448_init();
    }

    return ret;
}


/* Clears the curve448 key data.
 *
 * key  [in]  Curve448 key object.
 */
void wc_curve448_free(curve448_key* key)
{
   if (key != NULL) {
       ForceZero(key->p, sizeof(key->p));
       ForceZero(key->k, sizeof(key->k));
   }
}


/* Get the curve448 key's size.
 *
 * key  [in]  Curve448 key object.
 * returns 0 if key is NULL,
 *         CURVE448_KEY_SIZE otherwise.
 */
int wc_curve448_size(curve448_key* key)
{
    int ret = 0;

    if (key != NULL) {
        ret = CURVE448_KEY_SIZE;
    }

    return ret;
}

#endif /* HAVE_CURVE448 */