Coverage Report

Created: 2024-11-21 07:03

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