summaryrefslogtreecommitdiff
path: root/wolfcrypt/src/ed448.c
blob: 125ee3852fa763c166d976f91748a4021bd27025 (plain) (blame)
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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
/* ed448.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 8032 */

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

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

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

#ifdef HAVE_ED448

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

#if defined(HAVE_ED448_SIGN) || defined(HAVE_ED448_VERIFY)
/* Size of context bytes to use with hash when signing and verifying. */
#define ED448CTX_SIZE    8
/* Context to pass to hash when signing and verifying. */
static const byte ed448Ctx[ED448CTX_SIZE+1] = "SigEd448";
#endif

/* Derive the public key for the private key.
 *
 * key       [in]  Ed448 key object.
 * pubKey    [in]  Byte array to hold te public key.
 * pubKeySz  [in]  Size of the array in bytes.
 * returns BAD_FUNC_ARG when key is NULL or pubKeySz is not equal to
 *         ED448_PUB_KEY_SIZE,
 *         other -ve value on hash failure,
 *         0 otherwise.
 */
int wc_ed448_make_public(ed448_key* key, unsigned char* pubKey, word32 pubKeySz)
{
    int   ret = 0;
    byte  az[ED448_PRV_KEY_SIZE];
    ge448_p2 A;

    if ((key == NULL) || (pubKeySz != ED448_PUB_KEY_SIZE)) {
        ret = BAD_FUNC_ARG;
    }

    if (ret == 0) {
        ret = wc_Shake256Hash(key->k, ED448_KEY_SIZE, az, sizeof(az));
    }
    if (ret == 0) {
        /* apply clamp */
        az[0]  &= 0xfc;
        az[55] |= 0x80;
        az[56]  = 0x00;

        ge448_scalarmult_base(&A, az);
        ge448_to_bytes(pubKey, &A);
    }

    return ret;
}

/* Make a new ed448 private/public key.
 *
 * rng      [in]  Random number generator.
 * keysize  [in]  Size of the key to generate.
 * key      [in]  Ed448 key object.
 * returns BAD_FUNC_ARG when rng or key is NULL or keySz is not equal to
 *         ED448_KEY_SIZE,
 *         other -ve value on random number or hash failure,
 *         0 otherwise.
 */
int wc_ed448_make_key(WC_RNG* rng, int keySz, ed448_key* key)
{
    int ret = 0;

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

    /* ed448 has 57 byte key sizes */
    if ((ret == 0) && (keySz != ED448_KEY_SIZE)) {
        ret = BAD_FUNC_ARG;
    }

    if (ret == 0) {
        ret = wc_RNG_GenerateBlock(rng, key->k, ED448_KEY_SIZE);
    }
    if (ret == 0) {
        ret = wc_ed448_make_public(key, key->p, ED448_PUB_KEY_SIZE);
        if (ret != 0) {
            ForceZero(key->k, ED448_KEY_SIZE);
        }
    }
    if (ret == 0) {
        /* put public key after private key, on the same buffer */
        XMEMMOVE(key->k + ED448_KEY_SIZE, key->p, ED448_PUB_KEY_SIZE);

        key->pubKeySet = 1;
    }

    return ret;
}


#ifdef HAVE_ED448_SIGN
/* Sign the message using the ed448 private key.
 *
 *  in          [in]      Message to sign.
 *  inLen       [in]      Length of the message in bytes.
 *  out         [in]      Buffer to write signature into.
 *  outLen      [in/out]  On in, size of buffer.
 *                        On out, the length of the signature in bytes.
 *  key         [in]      Ed448 key to use when signing
 *  type        [in]      Type of signature to perform: Ed448 or Ed448ph
 *  context     [in]      Context of signing.
 *  contextLen  [in]      Length of context in bytes.
 *  returns BAD_FUNC_ARG when a parameter is NULL or contextLen is zero when and
 *          context is not NULL or public key not set,
 *          BUFFER_E when outLen is less than ED448_SIG_SIZE,
 *          other -ve values when hash fails,
 *          0 otherwise.
 */
static int ed448_sign_msg(const byte* in, word32 inLen, byte* out,
                          word32 *outLen, ed448_key* key, byte type,
                          const byte* context, byte contextLen)
{
    ge448_p2 R;
    byte     nonce[ED448_SIG_SIZE];
    byte     hram[ED448_SIG_SIZE];
    byte     az[ED448_PRV_KEY_SIZE];
    wc_Shake sha;
    int      ret = 0;

    /* sanity check on arguments */
    if ((in == NULL) || (out == NULL) || (outLen == NULL) || (key == NULL) ||
                                     ((context == NULL) && (contextLen != 0))) {
        ret = BAD_FUNC_ARG;
    }
    if ((ret == 0) && (!key->pubKeySet)) {
        ret = BAD_FUNC_ARG;
    }

    /* check and set up out length */
    if ((ret == 0) && (*outLen < ED448_SIG_SIZE)) {
        *outLen = ED448_SIG_SIZE;
        ret = BUFFER_E;
    }

    if (ret == 0) {
        *outLen = ED448_SIG_SIZE;

        /* step 1: create nonce to use where nonce is r in
           r = H(h_b, ... ,h_2b-1,M) */
        ret = wc_Shake256Hash(key->k, ED448_KEY_SIZE, az, sizeof(az));
    }
    if (ret == 0) {
        /* apply clamp */
        az[0]  &= 0xfc;
        az[55] |= 0x80;
        az[56]  = 0x00;

        ret = wc_InitShake256(&sha, NULL, INVALID_DEVID);
        if (ret == 0) {
            ret = wc_Shake256_Update(&sha, ed448Ctx, ED448CTX_SIZE);
        }
        if (ret == 0) {
            ret = wc_Shake256_Update(&sha, &type, sizeof(type));
        }
        if (ret == 0) {
            ret = wc_Shake256_Update(&sha, &contextLen, sizeof(contextLen));
        }
        if (ret == 0 && context != NULL) {
            ret = wc_Shake256_Update(&sha, context, contextLen);
        }
        if (ret == 0) {
            ret = wc_Shake256_Update(&sha, az + ED448_KEY_SIZE, ED448_KEY_SIZE);
        }
        if (ret == 0) {
            ret = wc_Shake256_Update(&sha, in, inLen);
        }
        if (ret == 0) {
            ret = wc_Shake256_Final(&sha, nonce, sizeof(nonce));
        }
        wc_Shake256_Free(&sha);
    }
    if (ret == 0) {
        sc448_reduce(nonce);

        /* step 2: computing R = rB where rB is the scalar multiplication of
           r and B */
        ge448_scalarmult_base(&R,nonce);
        ge448_to_bytes(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_InitShake256(&sha, NULL, INVALID_DEVID);
        if (ret == 0) {
            ret = wc_Shake256_Update(&sha, ed448Ctx, ED448CTX_SIZE);
            if (ret == 0) {
                ret = wc_Shake256_Update(&sha, &type, sizeof(type));
            }
            if (ret == 0) {
                ret = wc_Shake256_Update(&sha, &contextLen, sizeof(contextLen));
            }
            if (ret == 0 && context != NULL) {
                ret = wc_Shake256_Update(&sha, context, contextLen);
            }
            if (ret == 0) {
                ret = wc_Shake256_Update(&sha, out, ED448_SIG_SIZE/2);
            }
            if (ret == 0) {
                ret = wc_Shake256_Update(&sha, key->p, ED448_PUB_KEY_SIZE);
            }
            if (ret == 0) {
                ret = wc_Shake256_Update(&sha, in, inLen);
            }
            if (ret == 0) {
                ret = wc_Shake256_Final(&sha, hram, sizeof(hram));
            }
            wc_Shake256_Free(&sha);
        }
    }

    if (ret == 0) {
        sc448_reduce(hram);
        sc448_muladd(out + (ED448_SIG_SIZE/2), hram, az, nonce);
    }

    return ret;
}

/* Sign the message using the ed448 private key.
 * Signature type is Ed448.
 *
 *  in          [in]      Message to sign.
 *  inLen       [in]      Length of the message in bytes.
 *  out         [in]      Buffer to write signature into.
 *  outLen      [in/out]  On in, size of buffer.
 *                        On out, the length of the signature in bytes.
 *  key         [in]      Ed448 key to use when signing
 *  context     [in]      Context of signing.
 *  contextLen  [in]      Length of context in bytes.
 *  returns BAD_FUNC_ARG when a parameter is NULL or contextLen is zero when and
 *          context is not NULL or public key not set,
 *          BUFFER_E when outLen is less than ED448_SIG_SIZE,
 *          other -ve values when hash fails,
 *          0 otherwise.
 */
int wc_ed448_sign_msg(const byte* in, word32 inLen, byte* out, word32 *outLen,
                      ed448_key* key, const byte* context, byte contextLen)
{
    return ed448_sign_msg(in, inLen, out, outLen, key, Ed448, context,
                                                                    contextLen);
}

/* Sign the hash using the ed448 private key.
 * Signature type is Ed448ph.
 *
 *  hash        [in]      Hash of message to sign.
 *  hashLen     [in]      Length of hash of message in bytes.
 *  out         [in]      Buffer to write signature into.
 *  outLen      [in/out]  On in, size of buffer.
 *                        On out, the length of the signature in bytes.
 *  key         [in]      Ed448 key to use when signing
 *  context     [in]      Context of signing.
 *  contextLen  [in]      Length of context in bytes.
 *  returns BAD_FUNC_ARG when a parameter is NULL or contextLen is zero when and
 *          context is not NULL or public key not set,
 *          BUFFER_E when outLen is less than ED448_SIG_SIZE,
 *          other -ve values when hash fails,
 *          0 otherwise.
 */
int wc_ed448ph_sign_hash(const byte* hash, word32 hashLen, byte* out,
                         word32 *outLen, ed448_key* key,
                         const byte* context, byte contextLen)
{
    return ed448_sign_msg(hash, hashLen, out, outLen, key, Ed448ph, context,
                                                                    contextLen);
}

/* Sign the message using the ed448 private key.
 * Signature type is Ed448ph.
 *
 *  in          [in]      Message to sign.
 *  inLen       [in]      Length of the message to sign in bytes.
 *  out         [in]      Buffer to write signature into.
 *  outLen      [in/out]  On in, size of buffer.
 *                        On out, the length of the signature in bytes.
 *  key         [in]      Ed448 key to use when signing
 *  context     [in]      Context of signing.
 *  contextLen  [in]      Length of context in bytes.
 *  returns BAD_FUNC_ARG when a parameter is NULL or contextLen is zero when and
 *          context is not NULL or public key not set,
 *          BUFFER_E when outLen is less than ED448_SIG_SIZE,
 *          other -ve values when hash fails,
 *          0 otherwise.
 */
int wc_ed448ph_sign_msg(const byte* in, word32 inLen, byte* out, word32 *outLen,
                        ed448_key* key, const byte* context, byte contextLen)
{
    int  ret = 0;
    byte hash[64];

    ret = wc_Shake256Hash(in, inLen, hash, sizeof(hash));
    if (ret == 0) {
        ret = wc_ed448ph_sign_hash(hash, sizeof(hash), out, outLen, key,
                                                           context, contextLen);
    }

    return ret;
}
#endif /* HAVE_ED448_SIGN */

#ifdef HAVE_ED448_VERIFY

/* Verify the message using the ed448 public key.
 *
 *  sig         [in]  Signature to verify.
 *  sigLen      [in]  Size of signature in bytes.
 *  msg         [in]  Message to verify.
 *  msgLen      [in]  Length of the message in bytes.
 *  key         [in]  Ed448 key to use to verify.
 *  type        [in]  Type of signature to verify: Ed448 or Ed448ph
 *  context     [in]  Context of verification.
 *  contextLen  [in]  Length of context in bytes.
 *  returns BAD_FUNC_ARG when a parameter is NULL or contextLen is zero when and
 *          context is not NULL or public key not set,
 *          BUFFER_E when sigLen is less than ED448_SIG_SIZE,
 *          other -ve values when hash fails,
 *          0 otherwise.
 */
static int ed448_verify_msg(const byte* sig, word32 sigLen, const byte* msg,
                            word32 msgLen, int* res, ed448_key* key,
                            byte type, const byte* context, byte contextLen)
{
    byte     rcheck[ED448_KEY_SIZE];
    byte     h[ED448_SIG_SIZE];
    ge448_p2 A;
    ge448_p2 R;
    int      ret = 0;
    wc_Shake sha;

    /* sanity check on arguments */
    if ((sig == NULL) || (msg == NULL) || (res == NULL) || (key == NULL) ||
                                     ((context == NULL) && (contextLen != 0))) {
        ret = BAD_FUNC_ARG;
    }

    if (ret == 0) {
        /* set verification failed by default */
        *res = 0;

        /* check on basics needed to verify signature */
        if (sigLen < ED448_SIG_SIZE) {
            ret = BAD_FUNC_ARG;
        }
    }

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

    if (ret == 0) {
        /* find H(R,A,M) and store it as h */
        ret  = wc_InitShake256(&sha, NULL, INVALID_DEVID);
        if (ret == 0) {
            ret = wc_Shake256_Update(&sha, ed448Ctx, ED448CTX_SIZE);
            if (ret == 0) {
                ret = wc_Shake256_Update(&sha, &type, sizeof(type));
            }
            if (ret == 0) {
                ret = wc_Shake256_Update(&sha, &contextLen, sizeof(contextLen));
            }
            if (ret == 0 && context != NULL) {
                ret = wc_Shake256_Update(&sha, context, contextLen);
            }
            if (ret == 0) {
                ret = wc_Shake256_Update(&sha, sig, ED448_SIG_SIZE/2);
            }
            if (ret == 0) {
                ret = wc_Shake256_Update(&sha, key->p, ED448_PUB_KEY_SIZE);
            }
            if (ret == 0) {
                ret = wc_Shake256_Update(&sha, msg, msgLen);
            }
            if (ret == 0) {
                ret = wc_Shake256_Final(&sha,  h, sizeof(h));
            }
            wc_Shake256_Free(&sha);
        }
    }
    if (ret == 0) {
        sc448_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 = ge448_double_scalarmult_vartime(&R, h, &A,
                                                      sig + (ED448_SIG_SIZE/2));
    }

    if (ret == 0) {
        ge448_to_bytes(rcheck, &R);

        /* comparison of R created to R in sig */
        if (ConstantCompare(rcheck, sig, ED448_SIG_SIZE/2) != 0) {
            ret = SIG_VERIFY_E;
        }
        else {
            /* set the verification status */
            *res = 1;
        }
    }

    return ret;
}

/* Verify the message using the ed448 public key.
 * Signature type is Ed448.
 *
 *  sig         [in]  Signature to verify.
 *  sigLen      [in]  Size of signature in bytes.
 *  msg         [in]  Message to verify.
 *  msgLen      [in]  Length of the message in bytes.
 *  key         [in]  Ed448 key to use to verify.
 *  context     [in]  Context of verification.
 *  contextLen  [in]  Length of context in bytes.
 *  returns BAD_FUNC_ARG when a parameter is NULL or contextLen is zero when and
 *          context is not NULL or public key not set,
 *          BUFFER_E when sigLen is less than ED448_SIG_SIZE,
 *          other -ve values when hash fails,
 *          0 otherwise.
 */
int wc_ed448_verify_msg(const byte* sig, word32 sigLen, const byte* msg,
                        word32 msgLen, int* res, ed448_key* key,
                        const byte* context, byte contextLen)
{
    return ed448_verify_msg(sig, sigLen, msg, msgLen, res, key, Ed448,
                                                           context, contextLen);
}

/* Verify the hash using the ed448 public key.
 * Signature type is Ed448ph.
 *
 *  sig         [in]  Signature to verify.
 *  sigLen      [in]  Size of signature in bytes.
 *  hash        [in]  Hash of message to verify.
 *  hashLen     [in]  Length of the hash in bytes.
 *  key         [in]  Ed448 key to use to verify.
 *  context     [in]  Context of verification.
 *  contextLen  [in]  Length of context in bytes.
 *  returns BAD_FUNC_ARG when a parameter is NULL or contextLen is zero when and
 *          context is not NULL or public key not set,
 *          BUFFER_E when sigLen is less than ED448_SIG_SIZE,
 *          other -ve values when hash fails,
 *          0 otherwise.
 */
int wc_ed448ph_verify_hash(const byte* sig, word32 sigLen, const byte* hash,
                           word32 hashLen, int* res, ed448_key* key,
                           const byte* context, byte contextLen)
{
    return ed448_verify_msg(sig, sigLen, hash, hashLen, res, key, Ed448ph,
                                                           context, contextLen);
}

/* Verify the message using the ed448 public key.
 * Signature type is Ed448ph.
 *
 *  sig         [in]  Signature to verify.
 *  sigLen      [in]  Size of signature in bytes.
 *  msg         [in]  Message to verify.
 *  msgLen      [in]  Length of the message in bytes.
 *  key         [in]  Ed448 key to use to verify.
 *  context     [in]  Context of verification.
 *  contextLen  [in]  Length of context in bytes.
 *  returns BAD_FUNC_ARG when a parameter is NULL or contextLen is zero when and
 *          context is not NULL or public key not set,
 *          BUFFER_E when sigLen is less than ED448_SIG_SIZE,
 *          other -ve values when hash fails,
 *          0 otherwise.
 */
int wc_ed448ph_verify_msg(const byte* sig, word32 sigLen, const byte* msg,
                          word32 msgLen, int* res, ed448_key* key,
                          const byte* context, byte contextLen)
{
    int  ret = 0;
    byte hash[64];

    ret = wc_Shake256Hash(msg, msgLen, hash, sizeof(hash));
    if (ret == 0) {
        ret = wc_ed448ph_verify_hash(sig, sigLen, hash, sizeof(hash), res, key,
                                                           context, contextLen);
    }

    return ret;
}
#endif /* HAVE_ED448_VERIFY */

/* Initialize the ed448 private/public key.
 *
 * key  [in]  Ed448 key.
 * returns BAD_FUNC_ARG when key is NULL
 */
int wc_ed448_init(ed448_key* key)
{
    int ret = 0;

    if (key == NULL) {
        ret = BAD_FUNC_ARG;
    }
    else {
        XMEMSET(key, 0, sizeof(ed448_key));

        fe448_init();
    }

    return ret;
}


/* Clears the ed448 key data
 *
 * key  [in]  Ed448 key.
 */
void wc_ed448_free(ed448_key* key)
{
    if (key != NULL) {
        ForceZero(key, sizeof(ed448_key));
    }
}


#ifdef HAVE_ED448_KEY_EXPORT

/* Export the ed448 public key.
 *
 * key     [in]      Ed448 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 ED448_PUB_KEY_SIZE,
 *         0 otherwise.
 */
int wc_ed448_export_public(ed448_key* key, byte* out, word32* outLen)
{
    int ret = 0;

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

    if ((ret == 0) && (*outLen < ED448_PUB_KEY_SIZE)) {
        *outLen = ED448_PUB_KEY_SIZE;
        ret = BUFFER_E;
    }

    if (ret == 0) {
        *outLen = ED448_PUB_KEY_SIZE;
        XMEMCPY(out, key->p, ED448_PUB_KEY_SIZE);
    }

    return ret;
}

#endif /* HAVE_ED448_KEY_EXPORT */


#ifdef HAVE_ED448_KEY_IMPORT
/* Import a compressed or uncompressed ed448 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]  Ed448 public key.
 * returns BAD_FUNC_ARG when a parameter is NULL or key format is not supported,
 *         0 otherwise.
 */
int wc_ed448_import_public(const byte* in, word32 inLen, ed448_key* key)
{
    int ret = 0;

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

    if (ret == 0) {
        /* compressed prefix according to draft
         * https://tools.ietf.org/html/draft-ietf-openpgp-rfc4880bis-06 */
        if (in[0] == 0x40 && inLen > ED448_PUB_KEY_SIZE) {
            /* key is stored in compressed format so just copy in */
            XMEMCPY(key->p, (in + 1), ED448_PUB_KEY_SIZE);
            key->pubKeySet = 1;
        }
        /* importing uncompressed public key */
        else if (in[0] == 0x04 && inLen > 2*ED448_PUB_KEY_SIZE) {
            /* pass in (x,y) and store compressed key */
            ret = ge448_compress_key(key->p, in+1, in+1+ED448_PUB_KEY_SIZE);
            if (ret == 0)
                key->pubKeySet = 1;
        }
        else if (inLen == ED448_PUB_KEY_SIZE) {
            /* if not specified compressed or uncompressed check key size
             * if key size is equal to compressed key size copy in key */
            XMEMCPY(key->p, in, ED448_PUB_KEY_SIZE);
            key->pubKeySet = 1;
        }
        else {
            /* bad public key format */
            ret = BAD_FUNC_ARG;
        }
    }

    return ret;
}


/* Import an ed448 private key from a byte array.
 *
 * priv    [in]  Array holding private key.
 * privSz  [in]  Number of bytes of data in array.
 * key     [in]  Ed448 private key.
 * returns BAD_FUNC_ARG when a parameter is NULL or privSz is less than
 *         ED448_KEY_SIZE,
 *         0 otherwise.
 */
int wc_ed448_import_private_only(const byte* priv, word32 privSz,
                                 ed448_key* key)
{
    int ret = 0;

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

    /* key size check */
    if ((ret == 0) && (privSz < ED448_KEY_SIZE)) {
        ret = BAD_FUNC_ARG;
    }

    if (ret == 0) {
        XMEMCPY(key->k, priv, ED448_KEY_SIZE);
    }

    return ret;
}

/* Import an ed448 private and public keys from a byte arrays.
 *
 * priv    [in]  Array holding private key.
 * privSz  [in]  Number of bytes of data in private key array.
 * pub     [in]  Array holding private key.
 * pubSz   [in]  Number of bytes of data in public key array.
 * key     [in]  Ed448 private/public key.
 * returns BAD_FUNC_ARG when a parameter is NULL or privSz is less than
 *         ED448_KEY_SIZE or pubSz is less than ED448_PUB_KEY_SIZE,
 *         0 otherwise.
 */
int wc_ed448_import_private_key(const byte* priv, word32 privSz,
                                const byte* pub, word32 pubSz, ed448_key* key)
{
    int ret = 0;

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

    /* key size check */
    if ((ret == 0) && (privSz < ED448_KEY_SIZE || pubSz < ED448_PUB_KEY_SIZE)) {
        ret = BAD_FUNC_ARG;
    }

    if (ret == 0) {
        /* import public key */
        ret = wc_ed448_import_public(pub, pubSz, key);
    }
    if (ret == 0) {
        /* make the private key (priv + pub) */
        XMEMCPY(key->k, priv, ED448_KEY_SIZE);
        XMEMCPY(key->k + ED448_KEY_SIZE, key->p, ED448_PUB_KEY_SIZE);
    }

    return ret;
}

#endif /* HAVE_ED448_KEY_IMPORT */


#ifdef HAVE_ED448_KEY_EXPORT

/* Export the ed448 private key.
 *
 * key     [in]      Ed448 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 ED448_KEY_SIZE,
 *         0 otherwise.
 */
int wc_ed448_export_private_only(ed448_key* key, byte* out, word32* outLen)
{
    int ret = 0;

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

    if ((ret == 0) && (*outLen < ED448_KEY_SIZE)) {
        *outLen = ED448_KEY_SIZE;
        ret = BUFFER_E;
    }

    if (ret == 0) {
        *outLen = ED448_KEY_SIZE;
        XMEMCPY(out, key->k, ED448_KEY_SIZE);
    }

    return ret;
}

/* Export the ed448 private and public key.
 *
 * key     [in]      Ed448 private/public key.
 * out     [in]      Array to hold private and 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,
 *         BUFFER_E when outLen is less than ED448_PRV_KEY_SIZE,
 *         0 otherwise.
 */
int wc_ed448_export_private(ed448_key* key, byte* out, word32* outLen)
{
    int ret = 0;

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

    if ((ret == 0) && (*outLen < ED448_PRV_KEY_SIZE)) {
        *outLen = ED448_PRV_KEY_SIZE;
        ret = BUFFER_E;
    }

    if (ret == 0) {
        *outLen = ED448_PRV_KEY_SIZE;
        XMEMCPY(out, key->k, ED448_PRV_KEY_SIZE);
     }

    return ret;
}

/* Export the ed448 private and public key.
 *
 * key     [in]      Ed448 private/public key.
 * priv    [in]      Array to hold private key.
 * privSz  [in/out]  On in, the number of bytes in 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 array.
 * returns BAD_FUNC_ARG when a parameter is NULL,
 *         BUFFER_E when privSz is less than ED448_PRV_KEY_SIZE or pubSz is less
 *         than ED448_PUB_KEY_SIZE,
 *         0 otherwise.
 */
int wc_ed448_export_key(ed448_key* key, byte* priv, word32 *privSz,
                        byte* pub, word32 *pubSz)
{
    int ret = 0;

    /* export 'full' private part */
    ret = wc_ed448_export_private(key, priv, privSz);
    if (ret == 0) {
        /* export public part */
        ret = wc_ed448_export_public(key, pub, pubSz);
    }

    return ret;
}

#endif /* HAVE_ED448_KEY_EXPORT */

/* Check the public key of the ed448 key matches the private key.
 *
 * key     [in]      Ed448 private/public key.
 * returns BAD_FUNC_ARG when key is NULL,
 *         PUBLIC_KEY_E when the public key is not set or doesn't match,
 *         other -ve value on hash failure,
 *         0 otherwise.
 */
int wc_ed448_check_key(ed448_key* key)
{
    int ret = 0;
    unsigned char pubKey[ED448_PUB_KEY_SIZE];

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

    if (!key->pubKeySet) {
        ret = PUBLIC_KEY_E;
    }
    if (ret == 0) {
        ret = wc_ed448_make_public(key, pubKey, sizeof(pubKey));
    }
    if ((ret == 0) && (XMEMCMP(pubKey, key->p, ED448_PUB_KEY_SIZE) != 0)) {
        ret = PUBLIC_KEY_E;
    }

    return ret;
}

/* Returns the size of an ed448 private key.
 *
 * key     [in]      Ed448 private/public key.
 * returns BAD_FUNC_ARG when key is NULL,
 *         ED448_KEY_SIZE otherwise.
 */
int wc_ed448_size(ed448_key* key)
{
    int ret = ED448_KEY_SIZE;

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

    return ret;
}

/* Returns the size of an ed448 private plus public key.
 *
 * key     [in]      Ed448 private/public key.
 * returns BAD_FUNC_ARG when key is NULL,
 *         ED448_PRV_KEY_SIZE otherwise.
 */
int wc_ed448_priv_size(ed448_key* key)
{
    int ret = ED448_PRV_KEY_SIZE;

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

    return ret;
}

/* Returns the size of an ed448 public key.
 *
 * key     [in]      Ed448 private/public key.
 * returns BAD_FUNC_ARG when key is NULL,
 *         ED448_PUB_KEY_SIZE otherwise.
 */
int wc_ed448_pub_size(ed448_key* key)
{
    int ret = ED448_PUB_KEY_SIZE;

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

    return ret;
}

/* Returns the size of an ed448 signature.
 *
 * key     [in]      Ed448 private/public key.
 * returns BAD_FUNC_ARG when key is NULL,
 *         ED448_SIG_SIZE otherwise.
 */
int wc_ed448_sig_size(ed448_key* key)
{
    int ret = ED448_SIG_SIZE;

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

    return ret;
}

#endif /* HAVE_ED448 */