Coverage Report

Created: 2023-02-22 06:39

/src/wolfssl/wolfcrypt/src/curve448.c
Line
Count
Source (jump to first uncovered line)
1
/* curve448.c
2
 *
3
 * Copyright (C) 2006-2022 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
0
{
48
0
    int ret;
49
0
    unsigned char basepoint[CURVE448_KEY_SIZE] = {5};
50
51
0
    if ((pub == NULL) || (priv == NULL)) {
52
0
        return ECC_BAD_ARG_E;
53
0
    }
54
0
    if ((public_size  != CURVE448_PUB_KEY_SIZE) ||
55
0
        (private_size != CURVE448_KEY_SIZE)) {
56
0
        return ECC_BAD_ARG_E;
57
0
    }
58
59
0
    fe448_init();
60
61
    /* compute public key */
62
0
    ret = curve448(pub, priv, basepoint);
63
64
0
    return ret;
65
0
}
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
0
{
79
0
    int  ret = 0;
80
81
0
    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
0
    if ((ret == 0) && (keysize != CURVE448_KEY_SIZE)) {
87
0
        ret = ECC_BAD_ARG_E;
88
0
    }
89
90
0
    if (ret == 0) {
91
        /* random number for private key */
92
0
        ret = wc_RNG_GenerateBlock(rng, key->k, keysize);
93
0
    }
94
0
    if (ret == 0) {
95
0
        key->privSet = 1;
96
97
        /* clamp private */
98
0
        key->k[0] &= 0xfc;
99
0
        key->k[CURVE448_KEY_SIZE-1] |= 0x80;
100
101
        /* compute public */
102
0
        ret = wc_curve448_make_pub((int)sizeof(key->p), key->p,
103
0
                                   (int)sizeof(key->k), key->k);
104
0
        if (ret == 0) {
105
0
            key->pubSet = 1;
106
0
        }
107
0
        else {
108
0
            ForceZero(key->k, sizeof(key->k));
109
0
            XMEMSET(key->p, 0, sizeof(key->p));
110
0
        }
111
0
    }
112
113
0
    return ret;
114
0
}
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
0
{
238
0
    int ret = 0;
239
0
    int i;
240
241
0
    if ((key == NULL) || (out == NULL) || (outLen == NULL)) {
242
0
        ret = BAD_FUNC_ARG;
243
0
    }
244
245
    /* check and set outgoing key size */
246
0
    if ((ret == 0) && (*outLen < CURVE448_PUB_KEY_SIZE)) {
247
0
        *outLen = CURVE448_PUB_KEY_SIZE;
248
0
        ret = ECC_BAD_ARG_E;
249
0
    }
250
0
    if (ret == 0) {
251
        /* calculate public if missing */
252
0
        if (!key->pubSet) {
253
0
            ret = wc_curve448_make_pub((int)sizeof(key->p), key->p,
254
0
                                       (int)sizeof(key->k), key->k);
255
0
            key->pubSet = (ret == 0);
256
0
        }
257
0
    }
258
0
    if (ret == 0) {
259
0
        *outLen = CURVE448_PUB_KEY_SIZE;
260
0
        if (endian == EC448_BIG_ENDIAN) {
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
0
        else {
267
0
            XMEMCPY(out, key->p, CURVE448_PUB_KEY_SIZE);
268
0
        }
269
0
    }
270
271
0
    return ret;
272
0
}
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
0
    int i;
308
309
    /* sanity check */
310
0
    if ((key == NULL) || (in == NULL)) {
311
0
        ret = BAD_FUNC_ARG;
312
0
    }
313
314
    /* check size of incoming keys */
315
0
    if ((ret == 0) && (inLen != CURVE448_PUB_KEY_SIZE)) {
316
0
       ret = ECC_BAD_ARG_E;
317
0
    }
318
319
0
    if (ret == 0) {
320
0
        if (endian == EC448_BIG_ENDIAN) {
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
0
{
347
0
    int ret = 0;
348
0
    word32 i;
349
350
0
    if (pub == NULL) {
351
0
        ret = BAD_FUNC_ARG;
352
0
    }
353
354
    /* Check for empty key data */
355
0
    if ((ret == 0) && (pubSz == 0)) {
356
0
        ret = BUFFER_E;
357
0
    }
358
359
    /* Check key length */
360
0
    if ((ret == 0) && (pubSz != CURVE448_PUB_KEY_SIZE)) {
361
0
        ret = ECC_BAD_ARG_E;
362
0
    }
363
364
0
    if (ret == 0) {
365
0
        if (endian == EC448_LITTLE_ENDIAN) {
366
            /* Check for value of zero or one */
367
0
            for (i = CURVE448_PUB_KEY_SIZE - 1; i > 0; i--) {
368
0
                if (pub[i] != 0) {
369
0
                    break;
370
0
                }
371
0
            }
372
0
            if ((i == 0) && (pub[0] == 0 || pub[0] == 1)) {
373
0
                return ECC_BAD_ARG_E;
374
0
            }
375
            /* Check for order-1 or higher */
376
0
            for (i = CURVE448_PUB_KEY_SIZE - 1; i > 28; i--) {
377
0
                if (pub[i] != 0xff) {
378
0
                    break;
379
0
                }
380
0
            }
381
0
            if ((i == 28) && (pub[i] == 0xff)) {
382
0
                return ECC_BAD_ARG_E;
383
0
            }
384
0
            if ((i == 28) && (pub[i] == 0xfe)) {
385
0
                for (--i; i > 0; i--) {
386
0
                    if (pub[i] != 0xff) {
387
0
                        break;
388
0
                    }
389
0
                }
390
0
                if ((i == 0) && (pub[i] >= 0xfe)) {
391
0
                    return ECC_BAD_ARG_E;
392
0
                }
393
0
            }
394
0
        }
395
0
        else {
396
            /* Check for value of zero or one */
397
0
            for (i = 0; i < CURVE448_PUB_KEY_SIZE-1; i++) {
398
0
                if (pub[i] != 0) {
399
0
                    break;
400
0
                }
401
0
            }
402
0
            if ((i == CURVE448_PUB_KEY_SIZE - 1) &&
403
0
                (pub[i] == 0 || pub[i] == 1)) {
404
0
                ret = ECC_BAD_ARG_E;
405
0
            }
406
            /* Check for order-1 or higher */
407
0
            for (i = 0; i < 27; i++) {
408
0
                if (pub[i] != 0xff) {
409
0
                    break;
410
0
                }
411
0
            }
412
0
            if ((i == 27) && (pub[i] == 0xff)) {
413
0
                return ECC_BAD_ARG_E;
414
0
            }
415
0
            if ((i == 27) && (pub[i] == 0xfe)) {
416
0
                for (++i; i < CURVE448_PUB_KEY_SIZE - 1; i--) {
417
0
                    if (pub[i] != 0xff) {
418
0
                        break;
419
0
                    }
420
0
                }
421
0
                if ((i == CURVE448_PUB_KEY_SIZE) && (pub[i] >= 0xfe)) {
422
0
                    return ECC_BAD_ARG_E;
423
0
                }
424
0
            }
425
0
        }
426
0
    }
427
428
0
    return ret;
429
0
}
430
431
#endif /* HAVE_CURVE448_KEY_IMPORT */
432
433
434
#ifdef HAVE_CURVE448_KEY_EXPORT
435
436
/* Export the curve448 private key raw form.
437
 * Private key encoded big-endian.
438
 *
439
 * key     [in]      Curve448 private key.
440
 * out     [in]      Array to hold private key.
441
 * outLen  [in/out]  On in, the number of bytes in array.
442
 *                   On out, the number bytes put into array.
443
 * returns BAD_FUNC_ARG when a parameter is NULL,
444
 *         ECC_BAD_ARG_E when outLen is less than CURVE448_KEY_SIZE,
445
 *         0 otherwise.
446
 */
447
int wc_curve448_export_private_raw(curve448_key* key, byte* out, word32* outLen)
448
0
{
449
0
    return wc_curve448_export_private_raw_ex(key, out, outLen,
450
0
                                             EC448_BIG_ENDIAN);
451
0
}
452
453
/* Export the curve448 private key raw form.
454
 *
455
 * key     [in]      Curve448 private key.
456
 * out     [in]      Array to hold private key.
457
 * outLen  [in/out]  On in, the number of bytes in array.
458
 *                   On out, the number bytes put into array.
459
 * endian  [in]      Endianness to use when encoding number in array.
460
 * returns BAD_FUNC_ARG when a parameter is NULL,
461
 *         ECC_BAD_ARG_E when outLen is less than CURVE448_KEY_SIZE,
462
 *         0 otherwise.
463
 */
464
int wc_curve448_export_private_raw_ex(curve448_key* key, byte* out,
465
                                      word32* outLen, int endian)
466
0
{
467
0
    int ret = 0;
468
0
    int i;
469
470
    /* sanity check */
471
0
    if ((key == NULL) || (out == NULL) || (outLen == NULL)) {
472
0
        ret = BAD_FUNC_ARG;
473
0
    }
474
475
    /* check size of outgoing buffer */
476
0
    if ((ret == 0) && (*outLen < CURVE448_KEY_SIZE)) {
477
0
        *outLen = CURVE448_KEY_SIZE;
478
0
        ret = ECC_BAD_ARG_E;
479
0
    }
480
0
    if (ret == 0) {
481
0
        *outLen = CURVE448_KEY_SIZE;
482
483
0
        if (endian == EC448_BIG_ENDIAN) {
484
            /* put the key in Big Endian format */
485
0
            for (i = 0; i < CURVE448_KEY_SIZE; i++) {
486
0
                out[i] = key->k[CURVE448_KEY_SIZE - i - 1];
487
0
            }
488
0
        }
489
0
        else {
490
0
            XMEMCPY(out, key->k, CURVE448_KEY_SIZE);
491
0
        }
492
0
    }
493
494
0
    return ret;
495
0
}
496
497
/* Export the curve448 private and public keys in raw form.
498
 * Private and public key encoded big-endian.
499
 *
500
 * key     [in]      Curve448 private key.
501
 * priv    [in]      Array to hold private key.
502
 * privSz  [in/out]  On in, the number of bytes in private key array.
503
 *                   On out, the number bytes put into private key array.
504
 * pub     [in]      Array to hold public key.
505
 * pubSz   [in/out]  On in, the number of bytes in public key array.
506
 *                   On out, the number bytes put into public key array.
507
 * returns BAD_FUNC_ARG when a parameter is NULL,
508
 *         ECC_BAD_ARG_E when privSz is less than CURVE448_KEY_SIZE or pubSz is
509
 *         less than CURVE448_PUB_KEY_SIZE,
510
 *         0 otherwise.
511
 */
512
int wc_curve448_export_key_raw(curve448_key* key, byte* priv, word32 *privSz,
513
                               byte* pub, word32 *pubSz)
514
0
{
515
0
    return wc_curve448_export_key_raw_ex(key, priv, privSz, pub, pubSz,
516
0
                                         EC448_BIG_ENDIAN);
517
0
}
518
519
/* Export the curve448 private and public keys in raw form.
520
 *
521
 * key     [in]      Curve448 private key.
522
 * priv    [in]      Array to hold private key.
523
 * privSz  [in/out]  On in, the number of bytes in private key array.
524
 *                   On out, the number bytes put into private key array.
525
 * pub     [in]      Array to hold public key.
526
 * pubSz   [in/out]  On in, the number of bytes in public key array.
527
 *                   On out, the number bytes put into public key array.
528
 * endian  [in]      Endianness to use when encoding number in array.
529
 * returns BAD_FUNC_ARG when a parameter is NULL,
530
 *         ECC_BAD_ARG_E when privSz is less than CURVE448_KEY_SIZE or pubSz is
531
 *         less than CURVE448_PUB_KEY_SIZE,
532
 *         0 otherwise.
533
 */
534
int wc_curve448_export_key_raw_ex(curve448_key* key, byte* priv, word32 *privSz,
535
                                  byte* pub, word32 *pubSz, int endian)
536
0
{
537
0
    int ret;
538
539
    /* export private part */
540
0
    ret = wc_curve448_export_private_raw_ex(key, priv, privSz, endian);
541
0
    if (ret == 0) {
542
        /* export public part */
543
0
        ret = wc_curve448_export_public_ex(key, pub, pubSz, endian);
544
0
    }
545
546
0
    return ret;
547
0
}
548
549
#endif /* HAVE_CURVE448_KEY_EXPORT */
550
551
#ifdef HAVE_CURVE448_KEY_IMPORT
552
553
/* Import curve448 private and public keys from a byte arrays.
554
 * Private and public keys encoded in big-endian.
555
 *
556
 * piv     [in]  Array holding private key.
557
 * privSz  [in]  Number of bytes of data in private key array.
558
 * pub     [in]  Array holding public key.
559
 * pubSz   [in]  Number of bytes of data in public key array.
560
 * key     [in]  Curve448 private/public key.
561
 * returns BAD_FUNC_ARG when a parameter is NULL,
562
 *         ECC_BAD_ARG_E when privSz is less than CURVE448_KEY_SIZE or pubSz is
563
 *         less than CURVE448_PUB_KEY_SIZE,
564
 *         0 otherwise.
565
 */
566
int wc_curve448_import_private_raw(const byte* priv, word32 privSz,
567
                                   const byte* pub, word32 pubSz,
568
                                   curve448_key* key)
569
0
{
570
0
    return wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz, key,
571
0
                                             EC448_BIG_ENDIAN);
572
0
}
573
574
/* Import curve448 private and public keys from a byte arrays.
575
 *
576
 * piv     [in]  Array holding private key.
577
 * privSz  [in]  Number of bytes of data in private key array.
578
 * pub     [in]  Array holding public key.
579
 * pubSz   [in]  Number of bytes of data in public key array.
580
 * key     [in]  Curve448 private/public key.
581
 * endian  [in]  Endianness of encoded numbers in byte arrays.
582
 * returns BAD_FUNC_ARG when a parameter is NULL,
583
 *         ECC_BAD_ARG_E when privSz is less than CURVE448_KEY_SIZE or pubSz is
584
 *         less than CURVE448_PUB_KEY_SIZE,
585
 *         0 otherwise.
586
 */
587
int wc_curve448_import_private_raw_ex(const byte* priv, word32 privSz,
588
                                      const byte* pub, word32 pubSz,
589
                                      curve448_key* key, int endian)
590
0
{
591
0
    int ret;
592
593
    /* import private part */
594
0
    ret = wc_curve448_import_private_ex(priv, privSz, key, endian);
595
0
    if (ret == 0) {
596
        /* import public part */
597
0
        return wc_curve448_import_public_ex(pub, pubSz, key, endian);
598
0
    }
599
600
0
    return ret;
601
0
}
602
603
/* Import curve448 private key from a byte array.
604
 * Private key encoded in big-endian.
605
 *
606
 * piv     [in]  Array holding private key.
607
 * privSz  [in]  Number of bytes of data in private key array.
608
 * key     [in]  Curve448 private/public key.
609
 * returns BAD_FUNC_ARG when a parameter is NULL,
610
 *         ECC_BAD_ARG_E when privSz is less than CURVE448_KEY_SIZE,
611
 *         0 otherwise.
612
 */
613
int wc_curve448_import_private(const byte* priv, word32 privSz,
614
                               curve448_key* key)
615
0
{
616
0
    return wc_curve448_import_private_ex(priv, privSz, key, EC448_BIG_ENDIAN);
617
0
}
618
619
/* Import curve448 private key from a byte array.
620
 *
621
 * piv     [in]  Array holding private key.
622
 * privSz  [in]  Number of bytes of data in private key array.
623
 * key     [in]  Curve448 private/public key.
624
 * endian  [in]  Endianness of encoded number in byte array.
625
 * returns BAD_FUNC_ARG when a parameter is NULL,
626
 *         ECC_BAD_ARG_E when privSz is less than CURVE448_KEY_SIZE,
627
 *         0 otherwise.
628
 */
629
int wc_curve448_import_private_ex(const byte* priv, word32 privSz,
630
                                  curve448_key* key, int endian)
631
0
{
632
0
    int ret = 0;
633
0
    int i;
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
            /* read the key in Big Endian format */
648
0
            for (i = 0; i < CURVE448_KEY_SIZE; i++) {
649
0
                key->k[i] = priv[CURVE448_KEY_SIZE - i - 1];
650
0
            }
651
0
        }
652
0
        else {
653
0
            XMEMCPY(key->k, priv, CURVE448_KEY_SIZE);
654
0
        }
655
656
        /* Clamp the key */
657
0
        key->k[0] &= 0xfc;
658
0
        key->k[CURVE448_KEY_SIZE-1] |= 0x80;
659
660
0
        key->privSet = 1;
661
0
    }
662
663
0
    return ret;
664
0
}
665
666
#endif /* HAVE_CURVE448_KEY_IMPORT */
667
668
669
/* Initialize the curve448 key.
670
 *
671
 * key  [in]  Curve448 key object.
672
 * returns BAD_FUNC_ARG when key is NULL,
673
 *         0 otherwise.
674
 */
675
int wc_curve448_init(curve448_key* key)
676
0
{
677
0
    int ret = 0;
678
679
0
    if (key == NULL) {
680
0
       ret = BAD_FUNC_ARG;
681
0
    }
682
683
0
    if (ret == 0) {
684
0
        XMEMSET(key, 0, sizeof(*key));
685
686
0
        fe448_init();
687
688
    #ifdef WOLFSSL_CHECK_MEM_ZERO
689
        wc_MemZero_Add("wc_curve448_init key->k", &key->k, CURVE448_KEY_SIZE);
690
    #endif
691
0
    }
692
693
0
    return ret;
694
0
}
695
696
697
/* Clears the curve448 key data.
698
 *
699
 * key  [in]  Curve448 key object.
700
 */
701
void wc_curve448_free(curve448_key* key)
702
0
{
703
0
    if (key != NULL) {
704
0
        ForceZero(key->k, sizeof(key->k));
705
0
        XMEMSET(key->p, 0, sizeof(key->p));
706
0
        key->pubSet = 0;
707
0
        key->privSet = 0;
708
    #ifdef WOLFSSL_CHECK_MEM_ZERO
709
        wc_MemZero_Check(key, sizeof(curve448_key));
710
    #endif
711
0
    }
712
0
}
713
714
715
/* Get the curve448 key's size.
716
 *
717
 * key  [in]  Curve448 key object.
718
 * returns 0 if key is NULL,
719
 *         CURVE448_KEY_SIZE otherwise.
720
 */
721
int wc_curve448_size(curve448_key* key)
722
0
{
723
0
    int ret = 0;
724
725
0
    if (key != NULL) {
726
0
        ret = CURVE448_KEY_SIZE;
727
0
    }
728
729
0
    return ret;
730
0
}
731
732
#endif /* HAVE_CURVE448 */
733