Coverage Report

Created: 2026-01-06 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wolfssl-sp-math-all-8bit/wolfcrypt/src/curve448.c
Line
Count
Source
1
/* curve448.c
2
 *
3
 * Copyright (C) 2006-2025 wolfSSL Inc.
4
 *
5
 * This file is part of wolfSSL.
6
 *
7
 * wolfSSL is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * wolfSSL is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20
 */
21
22
/* Implemented to: RFC 7748 */
23
24
/* Based On Daniel J Bernstein's curve25519 Public Domain ref10 work.
25
 * Reworked for curve448 by Sean Parkinson.
26
 */
27
28
#include <wolfssl/wolfcrypt/libwolfssl_sources.h>
29
30
#ifdef HAVE_CURVE448
31
32
#include <wolfssl/wolfcrypt/curve448.h>
33
#ifdef NO_INLINE
34
    #include <wolfssl/wolfcrypt/misc.h>
35
#else
36
    #define WOLFSSL_MISC_INCLUDED
37
    #include <wolfcrypt/src/misc.c>
38
#endif
39
40
int wc_curve448_make_pub(int public_size, byte* pub, int private_size,
41
    const byte* priv)
42
1.03k
{
43
1.03k
    int ret;
44
1.03k
    unsigned char basepoint[CURVE448_KEY_SIZE] = {5};
45
46
1.03k
    if ((pub == NULL) || (priv == NULL)) {
47
0
        return ECC_BAD_ARG_E;
48
0
    }
49
1.03k
    if ((public_size  != CURVE448_PUB_KEY_SIZE) ||
50
1.03k
        (private_size != CURVE448_KEY_SIZE)) {
51
0
        return ECC_BAD_ARG_E;
52
0
    }
53
54
1.03k
    fe448_init();
55
56
    /* compute public key */
57
1.03k
    ret = curve448(pub, priv, basepoint);
58
59
1.03k
    return ret;
60
1.03k
}
61
62
63
/* Make a new curve448 private/public key.
64
 *
65
 * rng      [in]  Random number generator.
66
 * keysize  [in]  Size of the key to generate.
67
 * key      [in]  Curve448 key object.
68
 * returns BAD_FUNC_ARG when rng or key are NULL,
69
 *         ECC_BAD_ARG_E when keysize is not CURVE448_KEY_SIZE,
70
 *         0 otherwise.
71
 */
72
int wc_curve448_make_key(WC_RNG* rng, int keysize, curve448_key* key)
73
1.19k
{
74
1.19k
    int  ret = 0;
75
76
1.19k
    if ((key == NULL) || (rng == NULL)) {
77
0
        ret = BAD_FUNC_ARG;
78
0
    }
79
80
    /* currently only a key size of 56 bytes is used */
81
1.19k
    if ((ret == 0) && (keysize != CURVE448_KEY_SIZE)) {
82
0
        ret = ECC_BAD_ARG_E;
83
0
    }
84
85
1.19k
    if (ret == 0) {
86
        /* random number for private key */
87
1.19k
        ret = wc_RNG_GenerateBlock(rng, key->k, (word32)keysize);
88
1.19k
    }
89
1.19k
    if (ret == 0) {
90
1.03k
        key->privSet = 1;
91
92
        /* clamp private */
93
1.03k
        key->k[0] &= 0xfc;
94
1.03k
        key->k[CURVE448_KEY_SIZE-1] |= 0x80;
95
96
        /* compute public */
97
1.03k
        ret = wc_curve448_make_pub((int)sizeof(key->p), key->p,
98
1.03k
                                   (int)sizeof(key->k), key->k);
99
1.03k
        if (ret == 0) {
100
1.03k
            key->pubSet = 1;
101
1.03k
        }
102
0
        else {
103
0
            ForceZero(key->k, sizeof(key->k));
104
0
            XMEMSET(key->p, 0, sizeof(key->p));
105
0
        }
106
1.03k
    }
107
108
1.19k
    return ret;
109
1.19k
}
110
111
#ifdef HAVE_CURVE448_SHARED_SECRET
112
113
/* Calculate the shared secret from the private key and peer's public key.
114
 * Calculation over curve448.
115
 * Secret encoded big-endian.
116
 *
117
 * private_key  [in]      Curve448 private key.
118
 * public_key   [in]      Curve448 public key.
119
 * out          [in]      Array to hold shared secret.
120
 * outLen       [in/out]  On in, the number of bytes in array.
121
 *                        On out, the number bytes put into array.
122
 * returns BAD_FUNC_ARG when a parameter is NULL or outLen is less than
123
 *         CURVE448_KEY_SIZE,
124
 *         0 otherwise.
125
 */
126
int wc_curve448_shared_secret(curve448_key* private_key,
127
                              curve448_key* public_key,
128
                              byte* out, word32* outLen)
129
0
{
130
0
    return wc_curve448_shared_secret_ex(private_key, public_key, out, outLen,
131
0
                                        EC448_BIG_ENDIAN);
132
0
}
133
134
/* Calculate the shared secret from the private key and peer's public key.
135
 * Calculation over curve448.
136
 *
137
 * private_key  [in]      Curve448 private key.
138
 * public_key   [in]      Curve448 public key.
139
 * out          [in]      Array to hold shared secret.
140
 * outLen       [in/out]  On in, the number of bytes in array.
141
 *                        On out, the number bytes put into array.
142
 * endian       [in]      Endianness to use when encoding number in array.
143
 * returns BAD_FUNC_ARG when a parameter is NULL or outLen is less than
144
 *         CURVE448_PUB_KEY_SIZE,
145
 *         0 otherwise.
146
 */
147
int wc_curve448_shared_secret_ex(curve448_key* private_key,
148
                                 curve448_key* public_key,
149
                                 byte* out, word32* outLen, int endian)
150
38
{
151
38
    unsigned char o[CURVE448_PUB_KEY_SIZE];
152
38
    int ret = 0;
153
38
    int i;
154
155
    /* sanity check */
156
38
    if ((private_key == NULL) || (public_key == NULL) || (out == NULL) ||
157
38
                        (outLen == NULL) || (*outLen < CURVE448_PUB_KEY_SIZE)) {
158
0
        ret = BAD_FUNC_ARG;
159
0
    }
160
    /* make sure we have a populated private and public key */
161
38
    if (ret == 0 && (!private_key->privSet || !public_key->pubSet)) {
162
0
        ret = ECC_BAD_ARG_E;
163
0
    }
164
165
38
    if (ret == 0) {
166
38
        ret = curve448(o, private_key->k, public_key->p);
167
38
    }
168
#ifdef WOLFSSL_ECDHX_SHARED_NOT_ZERO
169
    if (ret == 0) {
170
        byte t = 0;
171
        for (i = 0; i < CURVE448_PUB_KEY_SIZE; i++) {
172
            t |= o[i];
173
        }
174
        if (t == 0) {
175
            ret = ECC_OUT_OF_RANGE_E;
176
        }
177
    }
178
#endif
179
38
    if (ret == 0) {
180
38
        if (endian == EC448_BIG_ENDIAN) {
181
            /* put shared secret key in Big Endian format */
182
0
            for (i = 0; i < CURVE448_PUB_KEY_SIZE; i++) {
183
0
                 out[i] = o[CURVE448_PUB_KEY_SIZE - i -1];
184
0
            }
185
0
        }
186
38
        else {
187
            /* put shared secret key in Little Endian format */
188
38
            XMEMCPY(out, o, CURVE448_PUB_KEY_SIZE);
189
38
        }
190
191
38
        *outLen = CURVE448_PUB_KEY_SIZE;
192
38
    }
193
194
38
    ForceZero(o, CURVE448_PUB_KEY_SIZE);
195
196
38
    return ret;
197
38
}
198
199
#endif /* HAVE_CURVE448_SHARED_SECRET */
200
201
#ifdef HAVE_CURVE448_KEY_EXPORT
202
203
/* Export the curve448 public key.
204
 * Public key encoded big-endian.
205
 *
206
 * key     [in]      Curve448 public key.
207
 * out     [in]      Array to hold public key.
208
 * outLen  [in/out]  On in, the number of bytes in array.
209
 *                   On out, the number bytes put into array.
210
 * returns BAD_FUNC_ARG when a parameter is NULL,
211
 *         ECC_BAD_ARG_E when outLen is less than CURVE448_PUB_KEY_SIZE,
212
 *         0 otherwise.
213
 */
214
int wc_curve448_export_public(curve448_key* key, byte* out, word32* outLen)
215
1
{
216
1
    return wc_curve448_export_public_ex(key, out, outLen, EC448_BIG_ENDIAN);
217
1
}
218
219
/* Export the curve448 public key.
220
 *
221
 * key     [in]      Curve448 public key.
222
 * out     [in]      Array to hold public key.
223
 * outLen  [in/out]  On in, the number of bytes in array.
224
 *                   On out, the number bytes put into array.
225
 * endian  [in]      Endianness to use when encoding number in array.
226
 * returns BAD_FUNC_ARG when a parameter is NULL,
227
 *         ECC_BAD_ARG_E when outLen is less than CURVE448_PUB_KEY_SIZE,
228
 *         0 otherwise.
229
 */
230
int wc_curve448_export_public_ex(curve448_key* key, byte* out, word32* outLen,
231
                                 int endian)
232
808
{
233
808
    int ret = 0;
234
235
808
    if ((key == NULL) || (out == NULL) || (outLen == NULL)) {
236
101
        ret = BAD_FUNC_ARG;
237
101
    }
238
239
    /* check and set outgoing key size */
240
808
    if ((ret == 0) && (*outLen < CURVE448_PUB_KEY_SIZE)) {
241
16
        *outLen = CURVE448_PUB_KEY_SIZE;
242
16
        ret = ECC_BAD_ARG_E;
243
16
    }
244
808
    if (ret == 0) {
245
        /* calculate public if missing */
246
691
        if (!key->pubSet) {
247
0
            ret = wc_curve448_make_pub((int)sizeof(key->p), key->p,
248
0
                                       (int)sizeof(key->k), key->k);
249
0
            key->pubSet = (ret == 0);
250
0
        }
251
691
    }
252
808
    if (ret == 0) {
253
691
        *outLen = CURVE448_PUB_KEY_SIZE;
254
691
        if (endian == EC448_BIG_ENDIAN) {
255
1
            int i;
256
            /* read keys in Big Endian format */
257
57
            for (i = 0; i < CURVE448_PUB_KEY_SIZE; i++) {
258
56
                out[i] = key->p[CURVE448_PUB_KEY_SIZE - i - 1];
259
56
            }
260
1
        }
261
690
        else {
262
690
            XMEMCPY(out, key->p, CURVE448_PUB_KEY_SIZE);
263
690
        }
264
691
    }
265
266
808
    return ret;
267
808
}
268
269
#endif /* HAVE_CURVE448_KEY_EXPORT */
270
271
#ifdef HAVE_CURVE448_KEY_IMPORT
272
273
/* Import a curve448 public key from a byte array.
274
 * Public key encoded in big-endian.
275
 *
276
 * in      [in]  Array holding public key.
277
 * inLen   [in]  Number of bytes of data in array.
278
 * key     [in]  Curve448 public key.
279
 * returns BAD_FUNC_ARG when a parameter is NULL,
280
 *         ECC_BAD_ARG_E when inLen is less than CURVE448_PUB_KEY_SIZE,
281
 *         0 otherwise.
282
 */
283
int wc_curve448_import_public(const byte* in, word32 inLen, curve448_key* key)
284
21
{
285
21
    return wc_curve448_import_public_ex(in, inLen, key, EC448_BIG_ENDIAN);
286
21
}
287
288
/* Import a curve448 public key from a byte array.
289
 *
290
 * in      [in]  Array holding public key.
291
 * inLen   [in]  Number of bytes of data in array.
292
 * key     [in]  Curve448 public key.
293
 * endian  [in]  Endianness of encoded number in byte array.
294
 * returns BAD_FUNC_ARG when a parameter is NULL,
295
 *         ECC_BAD_ARG_E when inLen is less than CURVE448_PUB_KEY_SIZE,
296
 *         0 otherwise.
297
 */
298
int wc_curve448_import_public_ex(const byte* in, word32 inLen,
299
                                 curve448_key* key, int endian)
300
140
{
301
140
    int ret = 0;
302
303
    /* sanity check */
304
140
    if ((key == NULL) || (in == NULL)) {
305
1
        ret = BAD_FUNC_ARG;
306
1
    }
307
308
    /* check size of incoming keys */
309
140
    if ((ret == 0) && (inLen != CURVE448_PUB_KEY_SIZE)) {
310
20
       ret = ECC_BAD_ARG_E;
311
20
    }
312
313
140
    if (ret == 0) {
314
119
        if (endian == EC448_BIG_ENDIAN) {
315
1
            int i;
316
            /* read keys in Big Endian format */
317
57
            for (i = 0; i < CURVE448_PUB_KEY_SIZE; i++) {
318
56
                key->p[i] = in[CURVE448_PUB_KEY_SIZE - i - 1];
319
56
            }
320
1
        }
321
118
        else
322
118
            XMEMCPY(key->p, in, inLen);
323
119
        key->pubSet = 1;
324
119
    }
325
326
140
    return ret;
327
140
}
328
329
/* Check the public key value (big or little endian)
330
 *
331
 * pub     [in]  Public key bytes.
332
 * pubSz   [in]  Size of public key in bytes.
333
 * endian  [in]  Public key bytes passed in as big-endian or little-endian.
334
 * returns BAD_FUNC_ARGS when pub is NULL,
335
 *         ECC_BAD_ARG_E when key length is not 56 bytes, public key value is
336
 *         zero or one;
337
 *         BUFFER_E when size of public key is zero;
338
 *         0 otherwise.
339
 */
340
int wc_curve448_check_public(const byte* pub, word32 pubSz, int endian)
341
893
{
342
893
    int ret = 0;
343
344
893
    if (pub == NULL) {
345
0
        ret = BAD_FUNC_ARG;
346
0
    }
347
348
    /* Check for empty key data */
349
893
    if ((ret == 0) && (pubSz == 0)) {
350
5
        ret = BUFFER_E;
351
5
    }
352
353
    /* Check key length */
354
893
    if ((ret == 0) && (pubSz != CURVE448_PUB_KEY_SIZE)) {
355
97
        ret = ECC_BAD_ARG_E;
356
97
    }
357
358
893
    if (ret == 0) {
359
791
        word32 i;
360
361
791
        if (endian == EC448_LITTLE_ENDIAN) {
362
            /* Check for value of zero or one */
363
1.69k
            for (i = CURVE448_PUB_KEY_SIZE - 1; i > 0; i--) {
364
1.67k
                if (pub[i] != 0) {
365
186
                    break;
366
186
                }
367
1.67k
            }
368
207
            if ((i == 0) && (pub[0] == 0 || pub[0] == 1)) {
369
8
                return ECC_BAD_ARG_E;
370
8
            }
371
            /* Check for order-1 or higher */
372
2.91k
            for (i = CURVE448_PUB_KEY_SIZE - 1; i > 28; i--) {
373
2.82k
                if (pub[i] != 0xff) {
374
111
                    break;
375
111
                }
376
2.82k
            }
377
199
            if ((i == 28) && (pub[i] == 0xff)) {
378
5
                return ECC_BAD_ARG_E;
379
5
            }
380
194
            if ((i == 28) && (pub[i] == 0xfe)) {
381
802
                for (--i; i > 0; i--) {
382
783
                    if (pub[i] != 0xff) {
383
37
                        break;
384
37
                    }
385
783
                }
386
56
                if ((i == 0) && (pub[i] >= 0xfe)) {
387
4
                    return ECC_BAD_ARG_E;
388
4
                }
389
56
            }
390
194
        }
391
584
        else {
392
            /* Check for value of zero or one */
393
1.30k
            for (i = 0; i < CURVE448_PUB_KEY_SIZE-1; i++) {
394
1.29k
                if (pub[i] != 0) {
395
575
                    break;
396
575
                }
397
1.29k
            }
398
584
            if ((i == CURVE448_PUB_KEY_SIZE - 1) &&
399
9
                (pub[i] == 0 || pub[i] == 1)) {
400
2
                ret = ECC_BAD_ARG_E;
401
2
            }
402
            /* Check for order-1 or higher */
403
1.24k
            for (i = 0; i < 27; i++) {
404
1.22k
                if (pub[i] != 0xff) {
405
565
                    break;
406
565
                }
407
1.22k
            }
408
584
            if ((i == 27) && (pub[i] == 0xff)) {
409
2
                return ECC_BAD_ARG_E;
410
2
            }
411
582
            if ((i == 27) && (pub[i] == 0xfe)) {
412
9
                for (++i; i < CURVE448_PUB_KEY_SIZE - 1; i--) {
413
9
                    if (pub[i] != 0xff) {
414
8
                        break;
415
8
                    }
416
9
                }
417
8
                if ((i == CURVE448_PUB_KEY_SIZE) && (pub[i] >= 0xfe)) {
418
0
                    return ECC_BAD_ARG_E;
419
0
                }
420
8
            }
421
582
        }
422
791
    }
423
424
874
    return ret;
425
893
}
426
427
#endif /* HAVE_CURVE448_KEY_IMPORT */
428
429
430
#ifdef HAVE_CURVE448_KEY_EXPORT
431
432
/* Export the curve448 private key raw form.
433
 * Private key encoded big-endian.
434
 *
435
 * key     [in]      Curve448 private key.
436
 * out     [in]      Array to hold private key.
437
 * outLen  [in/out]  On in, the number of bytes in array.
438
 *                   On out, the number bytes put into array.
439
 * returns BAD_FUNC_ARG when a parameter is NULL,
440
 *         ECC_BAD_ARG_E when outLen is less than CURVE448_KEY_SIZE,
441
 *         0 otherwise.
442
 */
443
int wc_curve448_export_private_raw(curve448_key* key, byte* out, word32* outLen)
444
0
{
445
0
    return wc_curve448_export_private_raw_ex(key, out, outLen,
446
0
                                             EC448_BIG_ENDIAN);
447
0
}
448
449
/* Export the curve448 private key raw form.
450
 *
451
 * key     [in]      Curve448 private key.
452
 * out     [in]      Array to hold private key.
453
 * outLen  [in/out]  On in, the number of bytes in array.
454
 *                   On out, the number bytes put into array.
455
 * endian  [in]      Endianness to use when encoding number in array.
456
 * returns BAD_FUNC_ARG when a parameter is NULL,
457
 *         ECC_BAD_ARG_E when outLen is less than CURVE448_KEY_SIZE,
458
 *         0 otherwise.
459
 */
460
int wc_curve448_export_private_raw_ex(curve448_key* key, byte* out,
461
                                      word32* outLen, int endian)
462
508
{
463
508
    int ret = 0;
464
465
    /* sanity check */
466
508
    if ((key == NULL) || (out == NULL) || (outLen == NULL)) {
467
140
        ret = BAD_FUNC_ARG;
468
140
    }
469
470
    /* check size of outgoing buffer */
471
508
    if ((ret == 0) && (*outLen < CURVE448_KEY_SIZE)) {
472
29
        *outLen = CURVE448_KEY_SIZE;
473
29
        ret = ECC_BAD_ARG_E;
474
29
    }
475
508
    if (ret == 0) {
476
339
        *outLen = CURVE448_KEY_SIZE;
477
478
339
        if (endian == EC448_BIG_ENDIAN) {
479
0
            int i;
480
            /* put the key in Big Endian format */
481
0
            for (i = 0; i < CURVE448_KEY_SIZE; i++) {
482
0
                out[i] = key->k[CURVE448_KEY_SIZE - i - 1];
483
0
            }
484
0
        }
485
339
        else {
486
339
            XMEMCPY(out, key->k, CURVE448_KEY_SIZE);
487
339
        }
488
339
    }
489
490
508
    return ret;
491
508
}
492
493
/* Export the curve448 private and public keys in raw form.
494
 * Private and public key encoded big-endian.
495
 *
496
 * key     [in]      Curve448 private key.
497
 * priv    [in]      Array to hold private key.
498
 * privSz  [in/out]  On in, the number of bytes in private key array.
499
 *                   On out, the number bytes put into private key array.
500
 * pub     [in]      Array to hold public key.
501
 * pubSz   [in/out]  On in, the number of bytes in public key array.
502
 *                   On out, the number bytes put into public key array.
503
 * returns BAD_FUNC_ARG when a parameter is NULL,
504
 *         ECC_BAD_ARG_E when privSz is less than CURVE448_KEY_SIZE or pubSz is
505
 *         less than CURVE448_PUB_KEY_SIZE,
506
 *         0 otherwise.
507
 */
508
int wc_curve448_export_key_raw(curve448_key* key, byte* priv, word32 *privSz,
509
                               byte* pub, word32 *pubSz)
510
0
{
511
0
    return wc_curve448_export_key_raw_ex(key, priv, privSz, pub, pubSz,
512
0
                                         EC448_BIG_ENDIAN);
513
0
}
514
515
/* Export the curve448 private and public keys in raw form.
516
 *
517
 * key     [in]      Curve448 private key.
518
 * priv    [in]      Array to hold private key.
519
 * privSz  [in/out]  On in, the number of bytes in private key array.
520
 *                   On out, the number bytes put into private key array.
521
 * pub     [in]      Array to hold public key.
522
 * pubSz   [in/out]  On in, the number of bytes in public key array.
523
 *                   On out, the number bytes put into public key array.
524
 * endian  [in]      Endianness to use when encoding number in array.
525
 * returns BAD_FUNC_ARG when a parameter is NULL,
526
 *         ECC_BAD_ARG_E when privSz is less than CURVE448_KEY_SIZE or pubSz is
527
 *         less than CURVE448_PUB_KEY_SIZE,
528
 *         0 otherwise.
529
 */
530
int wc_curve448_export_key_raw_ex(curve448_key* key, byte* priv, word32 *privSz,
531
                                  byte* pub, word32 *pubSz, int endian)
532
0
{
533
0
    int ret;
534
535
    /* export private part */
536
0
    ret = wc_curve448_export_private_raw_ex(key, priv, privSz, endian);
537
0
    if (ret == 0) {
538
        /* export public part */
539
0
        ret = wc_curve448_export_public_ex(key, pub, pubSz, endian);
540
0
    }
541
542
0
    return ret;
543
0
}
544
545
#endif /* HAVE_CURVE448_KEY_EXPORT */
546
547
#ifdef HAVE_CURVE448_KEY_IMPORT
548
549
/* Import curve448 private and public keys from a byte arrays.
550
 * Private and public keys encoded in big-endian.
551
 *
552
 * piv     [in]  Array holding private key.
553
 * privSz  [in]  Number of bytes of data in private key array.
554
 * pub     [in]  Array holding public key.
555
 * pubSz   [in]  Number of bytes of data in public key array.
556
 * key     [in]  Curve448 private/public key.
557
 * returns BAD_FUNC_ARG when a parameter is NULL,
558
 *         ECC_BAD_ARG_E when privSz is less than CURVE448_KEY_SIZE or pubSz is
559
 *         less than CURVE448_PUB_KEY_SIZE,
560
 *         0 otherwise.
561
 */
562
int wc_curve448_import_private_raw(const byte* priv, word32 privSz,
563
                                   const byte* pub, word32 pubSz,
564
                                   curve448_key* key)
565
26
{
566
26
    return wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz, key,
567
26
                                             EC448_BIG_ENDIAN);
568
26
}
569
570
/* Import curve448 private and public keys from a byte arrays.
571
 *
572
 * piv     [in]  Array holding private key.
573
 * privSz  [in]  Number of bytes of data in private key array.
574
 * pub     [in]  Array holding public key.
575
 * pubSz   [in]  Number of bytes of data in public key array.
576
 * key     [in]  Curve448 private/public key.
577
 * endian  [in]  Endianness of encoded numbers in byte arrays.
578
 * returns BAD_FUNC_ARG when a parameter is NULL,
579
 *         ECC_BAD_ARG_E when privSz is less than CURVE448_KEY_SIZE or pubSz is
580
 *         less than CURVE448_PUB_KEY_SIZE,
581
 *         0 otherwise.
582
 */
583
int wc_curve448_import_private_raw_ex(const byte* priv, word32 privSz,
584
                                      const byte* pub, word32 pubSz,
585
                                      curve448_key* key, int endian)
586
26
{
587
26
    int ret;
588
589
    /* import private part */
590
26
    ret = wc_curve448_import_private_ex(priv, privSz, key, endian);
591
26
    if (ret == 0) {
592
        /* import public part */
593
1
        return wc_curve448_import_public_ex(pub, pubSz, key, endian);
594
1
    }
595
596
25
    return ret;
597
26
}
598
599
/* Import curve448 private key from a byte array.
600
 * Private key encoded in big-endian.
601
 *
602
 * piv     [in]  Array holding private key.
603
 * privSz  [in]  Number of bytes of data in private key array.
604
 * key     [in]  Curve448 private/public key.
605
 * returns BAD_FUNC_ARG when a parameter is NULL,
606
 *         ECC_BAD_ARG_E when privSz is less than CURVE448_KEY_SIZE,
607
 *         0 otherwise.
608
 */
609
int wc_curve448_import_private(const byte* priv, word32 privSz,
610
                               curve448_key* key)
611
0
{
612
0
    return wc_curve448_import_private_ex(priv, privSz, key, EC448_BIG_ENDIAN);
613
0
}
614
615
/* Import curve448 private key from a byte array.
616
 *
617
 * piv     [in]  Array holding private key.
618
 * privSz  [in]  Number of bytes of data in private key array.
619
 * key     [in]  Curve448 private/public key.
620
 * endian  [in]  Endianness of encoded number in byte array.
621
 * returns BAD_FUNC_ARG when a parameter is NULL,
622
 *         ECC_BAD_ARG_E when privSz is less than CURVE448_KEY_SIZE,
623
 *         0 otherwise.
624
 */
625
int wc_curve448_import_private_ex(const byte* priv, word32 privSz,
626
                                  curve448_key* key, int endian)
627
26
{
628
26
    int ret = 0;
629
630
    /* sanity check */
631
26
    if ((key == NULL) || (priv == NULL)) {
632
0
        ret = BAD_FUNC_ARG;
633
0
    }
634
635
    /* check size of incoming keys */
636
26
    if ((ret == 0) && ((int)privSz != CURVE448_KEY_SIZE)) {
637
25
        ret = ECC_BAD_ARG_E;
638
25
    }
639
640
26
    if (ret == 0) {
641
1
        if (endian == EC448_BIG_ENDIAN) {
642
1
            int i;
643
            /* read the key in Big Endian format */
644
57
            for (i = 0; i < CURVE448_KEY_SIZE; i++) {
645
56
                key->k[i] = priv[CURVE448_KEY_SIZE - i - 1];
646
56
            }
647
1
        }
648
0
        else {
649
0
            XMEMCPY(key->k, priv, CURVE448_KEY_SIZE);
650
0
        }
651
652
        /* Clamp the key */
653
1
        key->k[0] &= 0xfc;
654
1
        key->k[CURVE448_KEY_SIZE-1] |= 0x80;
655
656
1
        key->privSet = 1;
657
1
    }
658
659
26
    return ret;
660
26
}
661
662
#endif /* HAVE_CURVE448_KEY_IMPORT */
663
664
665
/* Initialize the curve448 key.
666
 *
667
 * key  [in]  Curve448 key object.
668
 * returns BAD_FUNC_ARG when key is NULL,
669
 *         0 otherwise.
670
 */
671
int wc_curve448_init(curve448_key* key)
672
1.39k
{
673
1.39k
    int ret = 0;
674
675
1.39k
    if (key == NULL) {
676
0
       ret = BAD_FUNC_ARG;
677
0
    }
678
679
1.39k
    if (ret == 0) {
680
1.39k
        XMEMSET(key, 0, sizeof(*key));
681
682
1.39k
        fe448_init();
683
684
    #ifdef WOLFSSL_CHECK_MEM_ZERO
685
        wc_MemZero_Add("wc_curve448_init key->k", &key->k, CURVE448_KEY_SIZE);
686
    #endif
687
1.39k
    }
688
689
1.39k
    return ret;
690
1.39k
}
691
692
693
/* Clears the curve448 key data.
694
 *
695
 * key  [in]  Curve448 key object.
696
 */
697
void wc_curve448_free(curve448_key* key)
698
1.41k
{
699
1.41k
    if (key != NULL) {
700
1.39k
        ForceZero(key->k, sizeof(key->k));
701
1.39k
        XMEMSET(key->p, 0, sizeof(key->p));
702
1.39k
        key->pubSet = 0;
703
1.39k
        key->privSet = 0;
704
    #ifdef WOLFSSL_CHECK_MEM_ZERO
705
        wc_MemZero_Check(key, sizeof(curve448_key));
706
    #endif
707
1.39k
    }
708
1.41k
}
709
710
711
/* Get the curve448 key's size.
712
 *
713
 * key  [in]  Curve448 key object.
714
 * returns 0 if key is NULL,
715
 *         CURVE448_KEY_SIZE otherwise.
716
 */
717
int wc_curve448_size(curve448_key* key)
718
0
{
719
0
    int ret = 0;
720
721
0
    if (key != NULL) {
722
0
        ret = CURVE448_KEY_SIZE;
723
0
    }
724
725
0
    return ret;
726
0
}
727
728
#endif /* HAVE_CURVE448 */
729