Coverage Report

Created: 2026-10-31 07:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wolfssl-sp-math-all-8bit/wolfcrypt/src/eccsi.c
Line
Count
Source
1
/* eccsi.c
2
 *
3
 * Copyright (C) 2006-2026 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
#include <wolfssl/wolfcrypt/libwolfssl_sources.h>
23
24
#ifdef NO_INLINE
25
    #include <wolfssl/wolfcrypt/misc.h>
26
#else
27
    #define WOLFSSL_MISC_INCLUDED
28
    #include <wolfcrypt/src/misc.c>
29
#endif
30
31
#ifdef WOLFCRYPT_HAVE_ECCSI
32
33
#include <wolfssl/wolfcrypt/eccsi.h>
34
#include <wolfssl/wolfcrypt/asn_public.h>
35
#ifdef WOLFSSL_HAVE_SP_ECC
36
    #include <wolfssl/wolfcrypt/sp.h>
37
#endif
38
39
#ifndef WOLFSSL_HAVE_ECC_KEY_GET_PRIV
40
    /* FIPS build has replaced ecc.h. */
41
    #define wc_ecc_key_get_priv(key)  (&((key)->k))
42
    #define ecc_get_k_raw(key)        (&((key)->k))
43
    #define ecc_blind_k_rng(key, rng) 0
44
    #define ecc_forcezero_k(key)      mp_forcezero(&((key)->k))
45
    #define WOLFSSL_HAVE_ECC_KEY_GET_PRIV
46
#endif
47
48
#ifndef ecc_get_k_raw
49
    /* FIPS replacement of ecc.h predates ecc_get_k_raw but is new enough to
50
     * define WOLFSSL_HAVE_ECC_KEY_GET_PRIV, so the block above was skipped. */
51
    #define ecc_get_k_raw(key)        (key)->k
52
#endif
53
54
#if defined(HAVE_FIPS) && FIPS_VERSION3_LT(7,0,0)
55
    #ifndef ecc_forcezero_k
56
        /* FIPS ecc.h predates ecc_forcezero_k(). */
57
        #ifdef WOLFSSL_ECC_BLIND_K
58
            #define ecc_forcezero_k(key)                                  \
59
                do {                                                      \
60
                    mp_forcezero((key)->k);                               \
61
                    mp_forcezero((key)->kb);                              \
62
                    mp_forcezero((key)->ku);                              \
63
                } while (0)
64
        #else
65
            #define ecc_forcezero_k(key)      mp_forcezero((key)->k)
66
        #endif
67
    #endif
68
#endif
69
70
/**
71
 * Initialize the components of the ECCSI key and use the specified curve.
72
 *
73
 * Must be called before performing any operations.
74
 * Free the ECCSI key with wc_FreeEccsiKey() when no longer needed.
75
 *
76
 * @param  [in]  key    ECCSI key to initialize.
77
 * @param  [in]  heap   Heap hint.
78
 * @param  [in]  devId  Device identifier.
79
 *                      Use INVALID_DEVID when no device used.
80
 * @return  0 on success.
81
 * @return  BAD_FUNC_ARG when key is NULL.
82
 * @return  MEMORY_E when dynamic memory allocation fails.
83
 */
84
int wc_InitEccsiKey_ex(EccsiKey* key, int keySz, int curveId, void* heap,
85
        int devId)
86
120
{
87
120
    int err = 0;
88
120
    EccsiKeyParams* params = NULL;
89
90
120
    if (key == NULL) {
91
0
        err = BAD_FUNC_ARG;
92
0
    }
93
94
120
    if (err == 0) {
95
120
        XMEMSET(key, 0, sizeof(*key));
96
120
        key->heap = heap;
97
120
        params = &key->params;
98
99
120
        err = wc_ecc_init_ex(&key->ecc, heap, devId);
100
120
    }
101
120
    if (err == 0) {
102
120
        err = wc_ecc_init_ex(&key->pubkey, heap, devId);
103
120
    }
104
120
    if (err == 0) {
105
120
        key->pvt = wc_ecc_new_point_h(heap);
106
120
        if (key->pvt == NULL) {
107
6
            err = MEMORY_E;
108
6
        }
109
120
    }
110
120
    if (err == 0) {
111
114
        err = mp_init_multi(&params->order,
112
114
#ifdef WOLFCRYPT_ECCSI_CLIENT
113
114
                &params->a, &params->b, &params->prime, &key->tmp, &key->ssk
114
#else
115
                NULL, NULL, NULL, NULL, NULL
116
#endif
117
114
                );
118
114
    }
119
120
    if (err == 0) {
120
114
        err = wc_ecc_set_curve(&key->ecc, keySz, curveId);
121
114
    }
122
120
    if (err == 0) {
123
114
        err = wc_ecc_set_curve(&key->pubkey, keySz, curveId);
124
114
    }
125
126
120
    if (err != 0) {
127
6
        wc_FreeEccsiKey(key);
128
6
    }
129
130
120
    return err;
131
120
}
132
133
/**
134
 * Initialize the components of the ECCSI key.
135
 * Default curve used: NIST_P256 (ECC_SECP256R1)
136
 *
137
 * Must be called before performing any operations.
138
 * Free the ECCSI key with wc_FreeEccsiKey() when no longer needed.
139
 *
140
 * @param  [in]  key    ECCSI key to initialize.
141
 * @param  [in]  heap   Heap hint.
142
 * @param  [in]  devId  Device identifier.
143
 *                      Use INVALID_DEVID when no device used.
144
 * @return  0 on success.
145
 * @return  BAD_FUNC_ARG when key is NULL.
146
 * @return  MEMORY_E when dynamic memory allocation fails.
147
 */
148
int wc_InitEccsiKey(EccsiKey* key, void* heap, int devId)
149
0
{
150
0
    return wc_InitEccsiKey_ex(key, 32, ECC_SECP256R1, heap, devId);
151
0
}
152
153
/**
154
 * Frees memory associated with components of the ECCIS key.
155
 *
156
 * Must be called when finished with the ECCIS key.
157
 *
158
 * @param  [in]  key  ECCIS key.
159
 */
160
void wc_FreeEccsiKey(EccsiKey* key)
161
120
{
162
120
    if (key != NULL) {
163
120
        EccsiKeyParams* params = &key->params;
164
165
120
        wc_ecc_del_point_h(params->base, key->heap);
166
120
#ifdef WOLFCRYPT_ECCSI_CLIENT
167
120
        mp_free(&key->ssk);
168
120
        mp_free(&key->tmp);
169
120
        mp_free(&params->prime);
170
120
        mp_free(&params->b);
171
120
        mp_free(&params->a);
172
120
#endif
173
120
        mp_free(&params->order);
174
120
        wc_ecc_del_point_h(key->pvt, key->heap);
175
120
        wc_ecc_free(&key->pubkey);
176
120
        wc_ecc_free(&key->ecc);
177
120
        XMEMSET(key, 0, sizeof(*key));
178
120
    }
179
120
}
180
181
/*
182
 * Order, as a hex string in the ECC object, loaded into mp_int in key.
183
 * Flags that the order is available so it isn't loaded multiple times.
184
 *
185
 * @param  [in]  key  ECCSI key.
186
 * @return  0 on success.
187
 * @return  MEMORY_E when dynamic memory allocation fails.
188
 */
189
static int eccsi_load_order(EccsiKey* key)
190
0
{
191
0
    int err = 0;
192
193
0
    if (!key->params.haveOrder) {
194
0
        err = mp_read_radix(&key->params.order, key->ecc.dp->order,
195
0
                MP_RADIX_HEX);
196
0
        if (err == 0) {
197
0
            key->params.haveOrder = 1;
198
0
        }
199
0
    }
200
201
0
    return err;
202
0
}
203
204
#ifdef WOLFCRYPT_ECCSI_CLIENT
205
/*
206
 * Parameters, as a hex strings in the ECC object, loaded into mp_ints in key.
207
 *
208
 * Parameters loaded: order, A, B, prime.
209
 * Flags that each parameter is available so they aren't loaded multiple times.
210
 *
211
 * @param  [in]  key  ECCSI key.
212
 * @return  0 on success.
213
 * @return  MEMORY_E when dynamic memory allocation fails.
214
 */
215
static int eccsi_load_ecc_params(EccsiKey* key)
216
0
{
217
0
    int err = 0;
218
0
    EccsiKeyParams* params = &key->params;
219
220
0
    err = eccsi_load_order(key);
221
0
    if ((err == 0) && (!params->haveA)) {
222
0
        err = mp_read_radix(&params->a, key->ecc.dp->Af, MP_RADIX_HEX);
223
0
        if (err == 0) {
224
0
            params->haveA = 1;
225
0
        }
226
0
    }
227
0
    if ((err == 0) && (!params->haveB)) {
228
0
        err = mp_read_radix(&params->b, key->ecc.dp->Bf, MP_RADIX_HEX);
229
0
        if (err == 0) {
230
0
            params->haveB = 1;
231
0
        }
232
0
    }
233
0
    if ((err == 0) && (!params->havePrime)) {
234
0
        err = mp_read_radix(&params->prime, key->ecc.dp->prime, MP_RADIX_HEX);
235
0
        if (err == 0) {
236
0
            params->havePrime = 1;
237
0
        }
238
0
    }
239
240
0
    return err;
241
0
}
242
#endif /* WOLFCRYPT_ECCSI_CLIENT */
243
244
/*
245
 * Get the base point, hex encoded in the ECC object, as an ecc_point.
246
 *
247
 * Flags that base is available so it isn't loaded multiple times.
248
249
 * @param  [in]   key   ECCSI key.
250
 * @param  [out]  base  Base point of curve.
251
 * @return  0 on success.
252
 * @return  MEMORY_E when dynamic memory allocation fails.
253
 */
254
static int eccsi_load_base(EccsiKey* key)
255
0
{
256
0
    int err = 0;
257
0
    EccsiKeyParams* params = &key->params;
258
259
0
    if (!params->haveBase) {
260
0
        if (params->base == NULL) {
261
0
            params->base = wc_ecc_new_point_h(key->heap);
262
0
            if (params->base == NULL) {
263
0
                err = MEMORY_E;
264
0
            }
265
0
        }
266
0
        if (err == 0) {
267
0
            err = mp_read_radix(params->base->x, key->ecc.dp->Gx, MP_RADIX_HEX);
268
0
        }
269
0
        if (err == 0) {
270
0
            err = mp_read_radix(params->base->y, key->ecc.dp->Gy, MP_RADIX_HEX);
271
0
        }
272
0
        if (err == 0) {
273
0
            err = mp_set(params->base->z, 1);
274
0
        }
275
0
        if (err == 0) {
276
0
            params->haveBase = 1;
277
0
        }
278
0
    }
279
280
0
    return err;
281
0
}
282
283
/*
284
 * Encode the base point of the curve.
285
 *
286
 * Base point is hex encoded in the ECC object or cached as an ECC point from
287
 * previous load calls.
288
 *
289
 * @param  [in]   key     ECCSI key.
290
 * @param  [out]  data    Buffer to encode base point into.
291
 * @param  [out]  dataSz  Length of base point in bytes.
292
 * @return  0 on success.
293
 * @return  MEMORY_E when dynamic memory allocation fails.
294
 * @return  Other -ve value when an internal operation fails.
295
 */
296
static int eccsi_encode_base(EccsiKey* key, byte* data, word32* dataSz)
297
0
{
298
0
    int err;
299
0
    int idx = wc_ecc_get_curve_idx(key->ecc.dp->id);
300
301
0
    err = eccsi_load_base(key);
302
0
    if (err == 0) {
303
0
        err = wc_ecc_export_point_der(idx, key->params.base, data, dataSz);
304
0
    }
305
306
0
    return err;
307
0
}
308
309
#ifndef WOLFSSL_HAVE_SP_ECC
310
/*
311
 * Convert the KPAK to montgomery form.
312
 *
313
 * The KPAK is needed in Montgomery form for verification.
314
 *
315
 * @param  [in]  key      ECCSI key.
316
 * @return  0 on success.
317
 * @return  MP_MEM or MEMORY_E when dynamic memory allocation fails.
318
 * @return  Other -ve value when an internal operation fails.
319
 */
320
static int eccsi_kpak_to_mont(EccsiKey* key)
321
0
{
322
0
    int err = 0;
323
0
    ecc_point* kpak = &key->ecc.pubkey;
324
0
    mp_int* mu = &key->tmp;
325
0
    mp_int* prime = &key->params.prime;
326
327
0
    if (!key->kpakMont) {
328
0
        err = mp_montgomery_calc_normalization(mu, prime);
329
0
        if (err == 0) {
330
0
            err = mp_mulmod(kpak->x, mu, prime, kpak->x);
331
0
        }
332
0
        if (err == 0) {
333
0
            err = mp_mulmod(kpak->y, mu, prime, kpak->y);
334
0
        }
335
0
        if (err == 0) {
336
0
            err = mp_mulmod(kpak->z, mu, prime, kpak->z);
337
0
        }
338
0
        if (err == 0) {
339
0
            key->kpakMont = 1;
340
0
        }
341
0
    }
342
343
0
    return err;
344
0
}
345
#endif
346
347
/*
348
 * Convert the KPAK from montgomery form.
349
 *
350
 * The KPAK is needed in Montgomery form for verification.
351
 *
352
 * @param  [in]  key      ECCSI key.
353
 * @return  0 on success.
354
 * @return  MP_MEM or MEMORY_E when dynamic memory allocation fails.
355
 * @return  Other -ve value when an internal operation fails.
356
 */
357
static int eccsi_kpak_from_mont(EccsiKey* key)
358
0
{
359
0
    int err = 0;
360
0
    ecc_point* kpak = &key->ecc.pubkey;
361
0
    mp_digit mp;
362
0
    mp_int* prime = &key->params.prime;
363
364
0
    if (key->kpakMont) {
365
0
        err = mp_montgomery_setup(prime, &mp);
366
0
        if (err == 0) {
367
0
            err = mp_montgomery_reduce(kpak->x, prime, mp);
368
0
        }
369
0
        if (err == 0) {
370
0
            err = mp_montgomery_reduce(kpak->y, prime, mp);
371
0
        }
372
0
        if (err == 0) {
373
0
            err = mp_montgomery_reduce(kpak->z, prime, mp);
374
0
        }
375
0
        if (err == 0) {
376
0
            key->kpakMont = 0;
377
0
        }
378
0
    }
379
380
0
    return err;
381
0
}
382
383
/*
384
 * Compute HS = hash( G | KPAK | ID | PVT )
385
 *
386
 * Use when making a (SSK,PVT) pair, signing and verifying.
387
 *
388
 * @param  [in]   key       ECCSI key.
389
 * @param  [in]   hashType  Type of hash algorithm. e.g. WC_SHA256
390
 * @param  [in]   id        Identity to create hash from.
391
 * @param  [in]   idSz      Length of identity in bytes.
392
 * @param  [in]   pvt       Public Validation Token (PVT) as an ECC point.
393
 * @param  [out]  hash      Buffer to hold hash data.
394
 * @param  [out]  hashSz    Length of hash data in bytes.
395
 * @return  0 on success.
396
 * @return  MEMORY_E when dynamic memory allocation fails.
397
 * @return  Other -ve value when an internal operation fails.
398
 */
399
static int eccsi_compute_hs(EccsiKey* key, enum wc_HashType hashType,
400
        const byte* id, word32 idSz, ecc_point* pvt, byte* hash, byte* hashSz)
401
0
{
402
0
    int err;
403
0
    word32 dataSz = 0;
404
0
    int idx = wc_ecc_get_curve_idx(key->ecc.dp->id);
405
0
    ecc_point* kpak = &key->ecc.pubkey;
406
0
    int hash_inited = 0;
407
408
    /* HS = hash( G | KPAK | ID | PVT ) */
409
0
    err = wc_HashInit_ex(&key->hash, hashType, key->heap, INVALID_DEVID);
410
0
    if (err == 0) {
411
0
        hash_inited = 1;
412
        /* Base Point - G */
413
0
        dataSz = sizeof(key->data);
414
0
        err = eccsi_encode_base(key, key->data, &dataSz);
415
0
    }
416
0
    if (err == 0) {
417
0
        err = wc_HashUpdate(&key->hash, hashType, key->data, dataSz);
418
0
    }
419
0
    if (err == 0) {
420
0
        err = eccsi_kpak_from_mont(key);
421
0
    }
422
0
    if (err == 0) {
423
0
        dataSz = sizeof(key->data);
424
        /* KPAK - public key */
425
0
        err = wc_ecc_export_point_der(idx, kpak, key->data, &dataSz);
426
0
    }
427
0
    if (err == 0) {
428
0
        err = wc_HashUpdate(&key->hash, hashType, key->data, dataSz);
429
0
    }
430
0
    if (err == 0) {
431
        /* Id - Signer's ID */
432
0
        err = wc_HashUpdate(&key->hash, hashType, id, idSz);
433
0
    }
434
0
    if (err == 0) {
435
0
        dataSz = sizeof(key->data);
436
        /* PVT - Public Validation Token */
437
0
        err = wc_ecc_export_point_der(idx, pvt, key->data, &dataSz);
438
0
    }
439
0
    if (err == 0) {
440
        /* PVT - Public Validation Token */
441
0
        err = wc_HashUpdate(&key->hash, hashType, key->data, dataSz);
442
0
    }
443
0
    if (err == 0) {
444
0
        err = wc_HashFinal(&key->hash, hashType, hash);
445
0
    }
446
447
0
    if (err == 0) {
448
0
        *hashSz = (byte)wc_HashGetDigestSize(hashType);
449
0
    }
450
451
0
    if (hash_inited) {
452
0
        (void)wc_HashFree(&key->hash, hashType);
453
0
    }
454
455
0
    return err;
456
0
}
457
458
#ifdef WOLFCRYPT_ECCSI_KMS
459
/**
460
 * Generate KMS Secret Auth Key (KSAK) and KMS Public Auth Key (KPAK).
461
 *
462
 * RFC 6507, Section 4.2
463
 *
464
 * Called when establishing a new KMS.\n
465
 * KSAK must be kept secret while KPAK is required by clients for signing
466
 * and verifying.\n
467
 * Export key using wc_ExportEccsiKey(), once generated, to reuse the key.\n
468
 * Export KPAK using wc_ExportEccsiPublicKey(), once generate to send to
469
 * clients.
470
 *
471
 * Creates a random private key and multiplies it by the base point to calculate
472
 * the public key.
473
 *
474
 * @param  [in]  key      ECCSI key.
475
 * @param  [in]  rng      Random number generator.
476
 * @return  0 on success.
477
 * @return  BAD_FUNC_ARG when key or rng is NULL.
478
 * @return  MP_MEM or MEMORY_E when dynamic memory allocation fails.
479
 * @return  Other -ve value when an internal operation fails.
480
 */
481
int wc_MakeEccsiKey(EccsiKey* key, WC_RNG* rng)
482
0
{
483
0
    int err = 0;
484
485
0
    if ((key == NULL) || (rng == NULL)) {
486
0
        err = BAD_FUNC_ARG;
487
0
    }
488
489
0
    if (err == 0) {
490
0
        err = wc_ecc_make_key_ex(rng, key->ecc.dp->size, &key->ecc,
491
0
                key->ecc.dp->id);
492
#ifdef WOLFSSL_ASYNC_CRYPT
493
        /* ECCSI has no asynchronous API, so the caller cannot resume a pending
494
         * key generation - complete it here. The key->pubkey sites in
495
         * eccsi_make_pair() and eccsi_gen_sig() need no wait: each is preceded
496
         * by wc_ecc_free(&key->pubkey), which clears the marker that
497
         * _ecc_make_key_ex() gates its pending path on. */
498
        err = wc_AsyncWait(err, &key->ecc.asyncDev, WC_ASYNC_FLAG_NONE);
499
#endif
500
0
    }
501
502
0
    return err;
503
0
}
504
505
/*
506
 * Encode a point into a buffer.
507
 *
508
 * X and y ordinate of point concatenated. Each number is zero padded tosize.
509
 * Descriptor byte (0x04) is prepended when not raw.
510
 *
511
 * @param  [in]      point    ECC point to encode.
512
 * @param  [in]      size     Size of prime in bytes - maximum ordinate length.
513
 * @param  [out]     data     Buffer to hold encoded data.
514
 *                            NULL when needing length of encoded data.
515
 * @param  [in,out]  sz       In, the size of the buffer in bytes.
516
 *                            Out, the size of the encoded data in bytes.
517
 * @param  [in]      raw      On 0, prepend descriptor byte.
518
 *                            On 1, only include ordinates.
519
 * @return  0 on success.
520
 * @return  BAD_FUNC_ARG when key or sz is NULL.
521
 * @return  LENGTH_ONLY_E when data is NULL - sz will hold the size in bytes of
522
 *          the encoded data.
523
 * @return  BUFFER_E when size of buffer is too small.
524
 */
525
static int eccsi_encode_point(ecc_point* point, word32 size, byte* data,
526
        word32* sz, int raw)
527
0
{
528
0
    int err = 0;
529
530
0
    if (data == NULL) {
531
0
        *sz = size * 2 + !raw;
532
0
        err = WC_NO_ERR_TRACE(LENGTH_ONLY_E);
533
0
    }
534
0
    if ((err == 0) && (*sz < size * 2 + !raw)) {
535
0
        err = BUFFER_E;
536
0
    }
537
538
0
    if (err == 0) {
539
0
        if (!raw) {
540
0
            data[0] = 0x04;
541
0
            data++;
542
0
        }
543
544
        /* Write out the point's x ordinate into key size bytes. */
545
0
        err = mp_to_unsigned_bin_len(point->x, data, (int)size);
546
0
    }
547
0
    if (err == 0) {
548
0
        data += size;
549
        /* Write out the point's y ordinate into key size bytes. */
550
0
        err = mp_to_unsigned_bin_len(point->y, data, (int)size);
551
0
    }
552
0
    if (err == 0) {
553
0
        *sz = size * 2 + !raw;
554
0
    }
555
556
0
    return err;
557
0
}
558
559
/*
560
 * Decode the data into an ECC point.
561
 *
562
 * X and y ordinate of point concatenated. Each number is zero padded to
563
 * key size. Supports prepended descriptor byte (0x04).
564
 *
565
 * @param  [out]  point  ECC point to encode.
566
 * @param  [in]   size   Size of prime in bytes - maximum ordinate length.
567
 * @param  [in]   data   Encoded public key.
568
 * @param  [in]   sz     Size of the encoded public key in bytes.
569
 * @return  0 on success.
570
 * @return  BAD_FUNC_ARG when key or z is NULL.
571
 * @return  BUFFER_E when size of data is not equal to the expected size.
572
 * @return  ASN_PARSE_E when format byte is invalid.
573
 * @return  MP_MEM or MEMORY_E when dynamic memory allocation fails.
574
 */
575
static int eccsi_decode_point(ecc_point* point, word32 size, const byte* data,
576
        word32 sz)
577
114
{
578
114
    int err = 0;
579
580
114
    if ((sz != size * 2) && (sz != size * 2 + 1)) {
581
0
        err = BUFFER_E;
582
0
    }
583
584
114
    if ((err == 0) && (sz & 1)) {
585
0
        if (data[0] != 0x04) {
586
0
            err = ASN_PARSE_E;
587
0
        }
588
0
        data++;
589
0
    }
590
591
114
    if (err == 0) {
592
        /* Read the public key point's x value from key size bytes. */
593
114
        err = mp_read_unsigned_bin(point->x, data, size);
594
114
    }
595
114
    if (err == 0) {
596
114
        data += size;
597
        /* Read the public key point's y value from key size bytes. */
598
114
        err = mp_read_unsigned_bin(point->y, data, size);
599
114
    }
600
114
    if (err == 0) {
601
114
        err = mp_set(point->z, 1);
602
114
    }
603
604
114
    return err;
605
114
}
606
607
/*
608
 * Encode the ECCSI key.
609
 *
610
 * Encodes the private key as big-endian bytes of fixed length.
611
 * Encodes the public key x and y ordinates as big-endian bytes of fixed length.
612
 *
613
 * @param  [in]      key   ECCSI key.
614
 * @param  [out]     data  Buffer to hold encoded ECCSI key.
615
 * @return  0 on success.
616
 * @return  MEMORY_E when dynamic memory allocation fails (WOLFSSL_SMALL_STACK).
617
 */
618
static int eccsi_encode_key(EccsiKey* key, byte* data)
619
0
{
620
0
    int err;
621
0
    word32 sz = (word32)key->ecc.dp->size * 2;
622
623
    /* Write out the secret value into key size bytes. */
624
0
    err = mp_to_unsigned_bin_len(wc_ecc_key_get_priv(&key->ecc), data,
625
0
        key->ecc.dp->size);
626
0
    if (err == 0) {
627
0
        data += key->ecc.dp->size;
628
        /* Write the public key. */
629
0
        err = eccsi_encode_point(&key->ecc.pubkey, (word32)key->ecc.dp->size,
630
0
                data, &sz, 1);
631
0
    }
632
633
0
    return err;
634
0
}
635
636
/**
637
 * Export the ECCSI key as encoded public/private ECC key.
638
 *
639
 * Use when saving the KMS key pair.
640
 *
641
 * Private key, x ordinate of public key and y ordinate of public key
642
 * concatenated. Each number is zero padded to key size.
643
 *
644
 * @param  [in]      key   ECCSI key.
645
 * @param  [out]     data  Buffer to hold encoded ECCSI key.
646
 *                         NULL when requesting required length.
647
 * @param  [in,out]  sz    On in, size of buffer in bytes.
648
 *                         On out, size of encoded ECCSI key in bytes.
649
 * @return  0 on success.
650
 * @return  BAD_FUNC_ARG when key or sz is NULL
651
 * @return  BAD_STATE_E when no key to export.
652
 * @return  LENGTH_ONLY_E when data is NULL - sz is set.
653
 * @return  BUFFER_E when the buffer passed in is too small.
654
 * @return  MEMORY_E when dynamic memory allocation fails (WOLFSSL_SMALL_STACK).
655
 */
656
int wc_ExportEccsiKey(EccsiKey* key, byte* data, word32* sz)
657
0
{
658
0
    int err = 0;
659
660
0
    if ((key == NULL) || (sz == NULL)) {
661
0
        err = BAD_FUNC_ARG;
662
0
    }
663
664
0
    if ((err == 0) && (key->ecc.type != ECC_PRIVATEKEY)) {
665
0
        err = BAD_STATE_E;
666
0
    }
667
668
0
    if (err == 0) {
669
0
        if (data == NULL) {
670
0
            *sz = (word32)(key->ecc.dp->size * 3);
671
0
            err = WC_NO_ERR_TRACE(LENGTH_ONLY_E);
672
0
        }
673
0
        else if (*sz < (word32)key->ecc.dp->size * 3) {
674
0
            err = BUFFER_E;
675
0
        }
676
0
        else {
677
0
            *sz = (word32)(key->ecc.dp->size * 3);
678
0
        }
679
0
    }
680
0
    if (err == 0) {
681
0
        err = eccsi_kpak_from_mont(key);
682
0
    }
683
0
    if (err == 0) {
684
        /* Encode key */
685
0
        err = eccsi_encode_key(key, data);
686
0
    }
687
688
0
    return err;
689
0
}
690
691
/*
692
 * Import the ECCSI key as encoded public/private ECC key.
693
 *
694
 * Decodes the private key as big-endian bytes of fixed length.
695
 * Decodes the public key x and y ordinates as big-endian bytes of fixed length.
696
 *
697
 * @param  [in]  key   ECCSI key.
698
 * @param  [in]  data  Buffer holding encoded ECCSI key.
699
 * @return  0 on success.
700
 * @return  MP_MEM or MEMORY_E when dynamic memory allocation fails.
701
 */
702
static int eccsi_decode_key(EccsiKey* key, const byte* data)
703
114
{
704
114
    int err;
705
706
    /* Read the secret value from key size bytes. */
707
114
    err = mp_read_unsigned_bin(ecc_get_k_raw(&key->ecc), data,
708
114
        (word32)key->ecc.dp->size);
709
114
    if (err == 0) {
710
114
        err = ecc_blind_k_rng(&key->ecc, NULL);
711
114
    }
712
114
    if (err == 0) {
713
114
        data += key->ecc.dp->size;
714
        /* Read public key. */
715
114
        err = eccsi_decode_point(&key->ecc.pubkey, (word32)key->ecc.dp->size,
716
114
                data, (word32)(key->ecc.dp->size * 2));
717
114
    }
718
719
114
    return err;
720
114
}
721
722
/**
723
 * Import the ECCSI key as encoded public/private ECC key.
724
 *
725
 * Use when restoring the KMS key pair.
726
 *
727
 * Private key, x ordinate of public key and y ordinate of public key
728
 * concatenated. Each number is zero padded to key size.
729
 *
730
 * @param  [in]  key   ECCSI key.
731
 * @param  [in]  data  Buffer holding encoded ECCSI key.
732
 * @param  [in]  sz    Size of encoded ECCSI key in bytes.
733
 * @return  0 on success.
734
 * @return  BAD_FUNC_ARG when key or data is NULL.
735
 * @return  BUFFER_E when size of data is not equal to the expected size.
736
 * @return  MP_MEM or MEMORY_E when dynamic memory allocation fails.
737
 */
738
int wc_ImportEccsiKey(EccsiKey* key, const byte* data, word32 sz)
739
114
{
740
114
    int err = 0;
741
742
114
    if ((key == NULL) || (data == NULL)) {
743
0
        err = BAD_FUNC_ARG;
744
0
    }
745
114
    if ((err == 0) && (sz != (word32)key->ecc.dp->size * 3)) {
746
0
        err = BUFFER_E;
747
0
    }
748
749
114
    if (err == 0) {
750
114
        key->kpakMont = 0;
751
752
        /* Decode key */
753
114
        err = eccsi_decode_key(key, data);
754
114
    }
755
114
    if (err == 0) {
756
114
        key->ecc.type = ECC_PRIVATEKEY;
757
114
    }
758
759
114
    return err;
760
114
}
761
762
/**
763
 * Export the ECCSI private key.
764
 *
765
 * Use when saving the KMS key.
766
 *
767
 * Private key is zero padded to key size.
768
 *
769
 * @param  [in]      key   ECCSI key.
770
 * @param  [out]     data  Buffer to hold encoded ECCSI private key.
771
 *                         NULL when requesting required length.
772
 * @param  [in,out]  sz    On in, size of buffer in bytes.
773
 *                         On out, size of encoded ECCSI private key in bytes.
774
 * @return  0 on success.
775
 * @return  BAD_FUNC_ARG when key or sz is NULL
776
 * @return  BAD_STATE_E when no key to export.
777
 * @return  LENGTH_ONLY_E when data is NULL - sz is set.
778
 * @return  BUFFER_E when the buffer passed in is too small.
779
 * @return  MEMORY_E when dynamic memory allocation fails (WOLFSSL_SMALL_STACK).
780
 */
781
int wc_ExportEccsiPrivateKey(EccsiKey* key, byte* data, word32* sz)
782
0
{
783
0
    int err = 0;
784
785
0
    if ((key == NULL) || (sz == NULL)) {
786
0
        err = BAD_FUNC_ARG;
787
0
    }
788
789
0
    if ((err == 0) && (key->ecc.type != ECC_PRIVATEKEY)) {
790
0
        err = BAD_STATE_E;
791
0
    }
792
793
0
    if (err == 0) {
794
0
        if (data == NULL) {
795
0
            *sz = (word32)key->ecc.dp->size;
796
0
            err = WC_NO_ERR_TRACE(LENGTH_ONLY_E);
797
0
        }
798
0
        else if (*sz < (word32)key->ecc.dp->size) {
799
0
            err = BUFFER_E;
800
0
        }
801
0
        else {
802
0
            *sz = (word32)key->ecc.dp->size;
803
0
        }
804
0
    }
805
0
    if (err == 0) {
806
0
        err = mp_to_unsigned_bin_len(wc_ecc_key_get_priv(&key->ecc), data,
807
0
            key->ecc.dp->size);
808
0
    }
809
810
0
    return err;
811
0
}
812
813
/**
814
 * Import the ECCSI private key.
815
 *
816
 * Use when restoring the KMS key pair.
817
 *
818
 * Private key is zero padded to key size.
819
 *
820
 * @param  [in]  key   ECCSI key.
821
 * @param  [in]  data  Buffer holding encoded ECCSI private key.
822
 * @param  [in]  sz    Size of encoded ECCSI private key in bytes.
823
 * @return  0 on success.
824
 * @return  BAD_FUNC_ARG when key or data is NULL.
825
 * @return  BUFFER_E when size of data is not equal to the expected size.
826
 * @return  MP_MEM or MEMORY_E when dynamic memory allocation fails.
827
 */
828
int wc_ImportEccsiPrivateKey(EccsiKey* key, const byte* data, word32 sz)
829
0
{
830
0
    int err = 0;
831
832
0
    if ((key == NULL) || (data == NULL)) {
833
0
        err = BAD_FUNC_ARG;
834
0
    }
835
0
    if ((err == 0) && (sz != (word32)key->ecc.dp->size)) {
836
0
        err = BUFFER_E;
837
0
    }
838
839
0
    if (err == 0) {
840
0
        err = mp_read_unsigned_bin(ecc_get_k_raw(&key->ecc), data,
841
0
            (word32)key->ecc.dp->size);
842
0
    }
843
0
    if (err == 0) {
844
0
        err = ecc_blind_k_rng(&key->ecc, NULL);
845
0
    }
846
847
0
    return err;
848
0
}
849
850
/**
851
 * Export the KMS Public Auth Key (KPAK) from the ECCSI object.
852
 *
853
 * KPAK is required by all clients in order to perform cryptographic operations.
854
 *
855
 * X and y ordinate of public key concatenated. Each number is zero padded to
856
 * key size.
857
 * Descriptor byte (0x04) is prepended when not raw.
858
 *
859
 * @param  [in]      key      ECCSI key.
860
 * @param  [out]     data     Buffer to hold the encoded public key.
861
 * @param  [in,out]  sz       On in, size of buffer in bytes.
862
 *                            On out, length of encoded public key in bytes.
863
 * @param  [in]      raw   On 0, prepend descriptor byte.
864
 *                         On 1, only include ordinates.
865
 * @return  0 on success.
866
 * @return  BAD_FUNC_ARG when key or sz is NULL.
867
 * @return  LENGTH_ONLY_E when data is NULL - sz is set.
868
 * @return  BUFFER_E when the buffer passed in is too small.
869
 */
870
int wc_ExportEccsiPublicKey(EccsiKey* key, byte* data, word32* sz, int raw)
871
0
{
872
0
    int err = 0;
873
874
0
    if ((key == NULL) || (sz == NULL)) {
875
0
        err = BAD_FUNC_ARG;
876
0
    }
877
0
    if ((err == 0) && (key->ecc.type != ECC_PRIVATEKEY) &&
878
0
            (key->ecc.type != ECC_PUBLICKEY)) {
879
0
        err = BAD_STATE_E;
880
0
    }
881
882
0
    if ((err == 0) && (data != NULL)) {
883
0
        err = eccsi_kpak_from_mont(key);
884
0
    }
885
0
    if (err == 0) {
886
        /* Write out public key. */
887
0
        err = eccsi_encode_point(&key->ecc.pubkey, (word32)key->ecc.dp->size,
888
0
            data, sz, raw);
889
0
    }
890
891
0
    return err;
892
0
}
893
894
/*
895
 * Generates an (SSK, PVT) Pair - signing key pair.
896
 *
897
 * RFC 6507, Section 5.1.1
898
 *
899
 * @param  [in]   key       ECCSI key.
900
 * @param  [in]   rng       Random number generator.
901
 * @param  [in]   hashType  Type of hash algorithm. e.g. WC_SHA256
902
 * @param  [in]   id        Identity to create hash from.
903
 * @param  [in]   idSz      Length of identity in bytes.
904
 * @param  [out]  ssk       Secret Signing Key as an MP integer.
905
 * @param  [out]  pvt       Public Validation Token (PVT) as an ECC point.
906
 * @return  0 on success.
907
 * @return  MP_MEM or MEMORY_E when dynamic memory allocation fails.
908
 * @return  Other -ve value when an internal operation fails.
909
 */
910
static int eccsi_make_pair(EccsiKey* key, WC_RNG* rng,
911
        enum wc_HashType hashType, const byte* id, word32 idSz, mp_int* ssk,
912
        ecc_point* pvt)
913
0
{
914
0
    int err = 0;
915
0
    byte hashSz = 0;
916
0
    int genTryCnt = 0;
917
918
0
    do {
919
        /* Don't infinitely make pairs when random number generator fails. */
920
0
        if ((++genTryCnt) > ECCSI_MAX_GEN_COUNT) {
921
0
            err = RNG_FAILURE_E;
922
0
        }
923
924
0
        if (err == 0) {
925
0
            wc_ecc_free(&key->pubkey);
926
927
            /* Step 1 and 2: Generate ephemeral key - v, PVT = [v]G */
928
0
            err = wc_ecc_make_key_ex(rng, key->ecc.dp->size, &key->pubkey,
929
0
                    key->ecc.dp->id);
930
0
        }
931
0
        if (err == 0) {
932
0
            err = wc_ecc_copy_point(&key->pubkey.pubkey, pvt);
933
0
        }
934
935
        /* Step 3: Compute HS */
936
0
        if (err == 0) {
937
0
            hashSz = (byte)sizeof(key->data);
938
0
            err = eccsi_compute_hs(key, hashType, id, idSz, pvt, key->data,
939
0
                    &hashSz);
940
0
        }
941
942
        /* Step 4: Compute SSK = ( KSAK + HS * v ) modulo q */
943
0
        if (err == 0) {
944
0
            err = mp_read_unsigned_bin(ssk, key->data, hashSz);
945
0
        }
946
0
        if (err == 0) {
947
0
            err = mp_mulmod(ssk, wc_ecc_key_get_priv(&key->pubkey),
948
0
                &key->params.order, ssk);
949
0
        }
950
0
        if (err == 0) {
951
0
            err = mp_addmod(ssk, wc_ecc_key_get_priv(&key->ecc),
952
0
                &key->params.order, ssk);
953
0
        }
954
0
    }
955
0
    while ((err == 0) && (mp_iszero(ssk) ||
956
0
            (mp_cmp(ssk, wc_ecc_key_get_priv(&key->ecc)) == MP_EQ)));
957
    /* Step 5: ensure SSK and HS are non-zero (code lines above) */
958
959
    /* Step 6: Copy out SSK (done during calc) and PVT. Erase v */
960
0
    ecc_forcezero_k(&key->pubkey);
961
962
0
    return err;
963
0
}
964
965
/**
966
 * Generates an (SSK, PVT) Pair - signing key pair.
967
 *
968
 * RFC 6507, Section 5.1.1
969
 *
970
 * ID should include information to indicate a revocation date.\n
971
 * SSK must be zeroized after sending to client.\n
972
 * SSK is sent to signing client only.\n
973
 * PVT is sent to all client types.
974
 *
975
 * @param  [in]   key       ECCSI key.
976
 * @param  [in]   rng       Random number generator.
977
 * @param  [in]   hashType  Type of hash algorithm. e.g. WC_SHA256
978
 * @param  [in]   id        Identity to create hash from.
979
 * @param  [in]   idSz      Length of identity in bytes.
980
 * @param  [out]  ssk       Secret Signing Key as an MP integer.
981
 * @param  [out]  pvt       Public Validation Token (PVT) as an ECC point.
982
 * @return  0 on success.
983
 * @return  BAD_FUNC_ARG when key, rng, id, ssk or pvt is NULL.
984
 * @return  BAD_STATE_E when curve not set (key not set).
985
 * @return  MP_MEM or MEMORY_E when dynamic memory allocation fails.
986
 * @return  Other -ve value when an internal operation fails.
987
 */
988
int wc_MakeEccsiPair(EccsiKey* key, WC_RNG* rng, enum wc_HashType hashType,
989
        const byte* id, word32 idSz, mp_int* ssk, ecc_point* pvt)
990
0
{
991
0
    int err = 0;
992
993
0
    if ((key == NULL) || (rng == NULL) || (id == NULL) || (ssk == NULL) ||
994
0
            (pvt == NULL)) {
995
0
        err = BAD_FUNC_ARG;
996
0
    }
997
0
    if ((err == 0) && (key->ecc.type != ECC_PRIVATEKEY)) {
998
0
        err = BAD_STATE_E;
999
0
    }
1000
1001
0
    if (err == 0) {
1002
0
        err = eccsi_load_order(key);
1003
0
    }
1004
0
    if (err == 0) {
1005
0
        err = eccsi_make_pair(key, rng, hashType, id, idSz, ssk, pvt);
1006
0
    }
1007
1008
0
    return err;
1009
0
}
1010
1011
/**
1012
 * Encode the SSK and PVT into a buffer.
1013
 *
1014
 * SSK and PVT required by client signing messages.
1015
 *
1016
 * @param  [in]      key   ECCSI key.
1017
 * @param  [in]      ssk   Secret Signing Key as an MP integer.
1018
 * @param  [in]      pvt   Public Validation Token (PVT) as an ECC point.
1019
 * @param  [out]     data  Buffer to encode key pair into.
1020
 * @param  [in,out]  sz    In, size of buffer in bytes.
1021
 *                         Out, size of encoded pair data in bytes.
1022
 * @return  0 on success.
1023
 * @return  BAD_FUNC_ARG when key, ssk, pvt or sz is NULL.
1024
 * @return  MP_MEM or MEMORY_E when dynamic memory allocation fails.
1025
 * @return  LENGTH_ONLY_E when data is NULL - sz is set.
1026
 */
1027
int wc_EncodeEccsiPair(const EccsiKey* key, mp_int* ssk, ecc_point* pvt,
1028
        byte* data, word32* sz)
1029
0
{
1030
0
    int err = 0;
1031
1032
0
    if ((key == NULL) || (ssk == NULL) || (pvt == NULL) || (sz == NULL)) {
1033
0
        err = BAD_FUNC_ARG;
1034
0
    }
1035
1036
0
    if ((err == 0) && (data == NULL)) {
1037
0
        *sz = (word32)(key->ecc.dp->size * 3);
1038
0
        err = WC_NO_ERR_TRACE(LENGTH_ONLY_E);
1039
0
    }
1040
0
    if ((err == 0) && (*sz < (word32)(key->ecc.dp->size * 3))) {
1041
0
        err = BUFFER_E;
1042
0
    }
1043
1044
0
    if (err == 0) {
1045
0
        err = mp_to_unsigned_bin_len(ssk, data, key->ecc.dp->size);
1046
0
    }
1047
0
    if (err == 0) {
1048
0
        data += key->ecc.dp->size;
1049
        /* Write out the PVT's x ordinate into key size bytes. */
1050
0
        err = mp_to_unsigned_bin_len(pvt->x, data, key->ecc.dp->size);
1051
0
    }
1052
0
    if (err == 0) {
1053
0
        data += key->ecc.dp->size;
1054
        /* Write out the PVT's y ordinate into key size bytes. */
1055
0
        err = mp_to_unsigned_bin_len(pvt->y, data, key->ecc.dp->size);
1056
0
    }
1057
0
    if (err == 0) {
1058
0
        *sz = (word32)(key->ecc.dp->size * 3);
1059
0
    }
1060
1061
0
    return err;
1062
0
}
1063
1064
/**
1065
 * Encode the Secret Signing Key (SSK).
1066
 *
1067
 * Use when saving the key pair.
1068
 *
1069
 * SSK is zero padded to key size.
1070
 *
1071
 * @param  [in]      key   ECCSI key.
1072
 * @param  [in]      ssk   Secret Signing Key as an MP integer.
1073
 * @param  [out]     data  Buffer to hold encoded SSK.
1074
 *                         NULL when requesting required length.
1075
 * @param  [in,out]  sz    On in, size of buffer in bytes.
1076
 *                         On out, size of encoded ECCSI key in bytes.
1077
 * @return  0 on success.
1078
 * @return  BAD_FUNC_ARG when key, ssk or sz is NULL
1079
 * @return  BAD_STATE_E when no key to export.
1080
 * @return  LENGTH_ONLY_E when data is NULL - sz is set.
1081
 * @return  BUFFER_E when the buffer passed in is too small.
1082
 * @return  MEMORY_E when dynamic memory allocation fails (WOLFSSL_SMALL_STACK).
1083
 */
1084
int wc_EncodeEccsiSsk(const EccsiKey* key, mp_int* ssk, byte* data, word32* sz)
1085
0
{
1086
0
    int err = 0;
1087
1088
0
    if ((key == NULL) || (ssk == NULL) || (sz == NULL)) {
1089
0
        err = BAD_FUNC_ARG;
1090
0
    }
1091
1092
0
    if ((err == 0) && (key->ecc.type != ECC_PRIVATEKEY)) {
1093
0
        err = BAD_STATE_E;
1094
0
    }
1095
1096
0
    if (err == 0) {
1097
0
        if (data == NULL) {
1098
0
            *sz = (word32)key->ecc.dp->size;
1099
0
            err = WC_NO_ERR_TRACE(LENGTH_ONLY_E);
1100
0
        }
1101
0
        else if (*sz < (word32)key->ecc.dp->size) {
1102
0
            err = BUFFER_E;
1103
0
        }
1104
0
        else {
1105
0
            *sz = (word32)key->ecc.dp->size;
1106
0
        }
1107
0
    }
1108
0
    if (err == 0) {
1109
0
        err = mp_to_unsigned_bin_len(ssk, data, key->ecc.dp->size);
1110
0
    }
1111
1112
0
    return err;
1113
0
}
1114
1115
/**
1116
 * Decode the Secret Signing Key (SSK).
1117
 *
1118
 * Use when restoring the key pair.
1119
 *
1120
 * SSK is zero padded to key size.
1121
 *
1122
 * @param  [in]   key   ECCSI key.
1123
 * @param  [in]   data  Buffer holding encoded ECCSI key.
1124
 * @param  [in]   sz    Size of encoded ECCSI key in bytes.
1125
 * @param  [out]  ssk   Secret Signing Key as an MP integer.
1126
 * @return  0 on success.
1127
 * @return  BAD_FUNC_ARG when key, data or ssk is NULL.
1128
 * @return  BUFFER_E when size of data is not equal to the expected size.
1129
 * @return  MP_MEM or MEMORY_E when dynamic memory allocation fails.
1130
 */
1131
int wc_DecodeEccsiSsk(const EccsiKey* key, const byte* data, word32 sz,
1132
        mp_int* ssk)
1133
0
{
1134
0
    int err = 0;
1135
1136
0
    if ((key == NULL) || (data == NULL) || (ssk == NULL)) {
1137
0
        err = BAD_FUNC_ARG;
1138
0
    }
1139
0
    if ((err == 0) && (sz != (word32)key->ecc.dp->size)) {
1140
0
        err = BUFFER_E;
1141
0
    }
1142
1143
0
    if (err == 0) {
1144
0
        err = mp_read_unsigned_bin(ssk, data, (word32)key->ecc.dp->size);
1145
0
    }
1146
1147
0
    return err;
1148
0
}
1149
1150
/**
1151
 * Encode the PVT into a buffer.
1152
 *
1153
 * PVT required by client verifying messages.
1154
 *
1155
 * X and y ordinate of public key concatenated. Each number is zero padded to
1156
 * key size.
1157
 * Descriptor byte (0x04) is prepended when not raw.
1158
 *
1159
 * @param  [in]      key   ECCSI key.
1160
 * @param  [in]      pvt   Public Validation Token (PVT) as an ECC point.
1161
 * @param  [out]     data  Buffer to encode key pair into.
1162
 * @param  [in,out]  sz    In, size of buffer in bytes.
1163
 *                         Out, size of encoded pair data in bytes.
1164
 * @param  [in]      raw   On 0, prepend descriptor byte.
1165
 *                         On 1, only include ordinates.
1166
 * @return  0 on success.
1167
 * @return  BAD_FUNC_ARG when key, pvt or sz is NULL.
1168
 * @return  BAD_STATE_E when PVT has not been set.
1169
 * @return  MP_MEM or MEMORY_E when dynamic memory allocation fails.
1170
 * @return  LENGTH_ONLY_E when data is NULL - sz is set.
1171
 */
1172
int wc_EncodeEccsiPvt(const EccsiKey* key, ecc_point* pvt, byte* data,
1173
        word32* sz, int raw)
1174
0
{
1175
0
    int err = 0;
1176
1177
0
    if ((key == NULL) || (pvt == NULL) || (sz == NULL)) {
1178
0
        err = BAD_FUNC_ARG;
1179
0
    }
1180
1181
0
    if (err == 0) {
1182
0
        err = eccsi_encode_point(pvt, (word32)key->ecc.dp->size, data, sz, raw);
1183
0
    }
1184
1185
0
    return err;
1186
0
}
1187
1188
#endif /* WOLFCRYPT_ECCSI_KMS */
1189
1190
#ifdef WOLFCRYPT_ECCSI_CLIENT
1191
/**
1192
 * Decode the SSK and PVT data into separate variables.
1193
 *
1194
 * A signing client decodes the data so that it can validate the pair and sign.
1195
 *
1196
 * @param  [in]   key   ECCSI key.
1197
 * @param  [in]   data  Buffer holding key pair data.
1198
 * @param  [in]   sz    Size of data in bytes.
1199
 * @param  [out]  ssk   Secret Signing Key as an MP integer.
1200
 * @param  [out]  pvt   Public Validation Token (PVT) as an ECC point.
1201
 * @return  0 on success.
1202
 * @return  BAD_FUNC_ARG when key, data, ssk or pvt is NULL.
1203
 * @return  LENGTH_ONLY_E when data is NULL - sz is set.
1204
 * @return  BUFFER_E when size of data is not equal to the expected size.
1205
 * @return  MP_MEM or MEMORY_E when dynamic memory allocation fails.
1206
 */
1207
int wc_DecodeEccsiPair(const EccsiKey* key, const byte* data, word32 sz,
1208
        mp_int* ssk, ecc_point* pvt)
1209
0
{
1210
0
    int err = 0;
1211
1212
0
    if ((key == NULL) || (data == NULL) || (ssk == NULL) || (pvt == NULL)) {
1213
0
        err = BAD_FUNC_ARG;
1214
0
    }
1215
0
    if ((err == 0) && (sz != (word32)(key->ecc.dp->size * 3))) {
1216
0
        err = BUFFER_E;
1217
0
    }
1218
1219
0
    if (err == 0) {
1220
        /* Read the SSK value from key size bytes. */
1221
0
        err = mp_read_unsigned_bin(ssk, data, (word32)key->ecc.dp->size);
1222
0
    }
1223
0
    if (err == 0) {
1224
0
        data += key->ecc.dp->size;
1225
        /* Read the PVT's x value from key size bytes. */
1226
0
        err = mp_read_unsigned_bin(pvt->x, data, (word32)key->ecc.dp->size);
1227
0
    }
1228
0
    if (err == 0) {
1229
0
        data += key->ecc.dp->size;
1230
        /* Read the PVT's y value from key size bytes. */
1231
0
        err = mp_read_unsigned_bin(pvt->y, data, (word32)key->ecc.dp->size);
1232
0
    }
1233
0
    if (err == 0) {
1234
0
        err = mp_set(pvt->z, 1);
1235
0
    }
1236
1237
0
    return err;
1238
0
}
1239
1240
/**
1241
 * Decode the PVT data into an ECC point.
1242
 *
1243
 * A verifying client decodes the data so that it can verify a message.
1244
 *
1245
 * X and y ordinate of public key concatenated. Each number is zero padded to
1246
 * key size.
1247
 * Descriptor byte (0x04) is prepended when not raw.
1248
 *
1249
 * @param  [in]   key   ECCSI key.
1250
 * @param  [in]   data  Buffer holding PVT data.
1251
 * @param  [in]   sz    Size of data in bytes.
1252
 * @param  [out]  pvt   Public Validation Token (PVT) as an ECC point.
1253
 * @return  0 on success.
1254
 * @return  BAD_FUNC_ARG when key, data, ssk or pvt is NULL.
1255
 * @return  BUFFER_E when size of data is not equal to the expected size.
1256
 * @return  ASN_PARSE_E when format byte is invalid.
1257
 * @return  MP_MEM or MEMORY_E when dynamic memory allocation fails.
1258
 */
1259
int wc_DecodeEccsiPvt(const EccsiKey* key, const byte* data, word32 sz,
1260
        ecc_point* pvt)
1261
0
{
1262
0
    int err = 0;
1263
1264
0
    if ((key == NULL) || (data == NULL) || (pvt == NULL)) {
1265
0
        err = BAD_FUNC_ARG;
1266
0
    }
1267
1268
0
    if (err == 0) {
1269
0
        err = eccsi_decode_point(pvt, (word32)key->ecc.dp->size, data, sz);
1270
0
    }
1271
1272
0
    return err;
1273
0
}
1274
1275
/**
1276
 * Decode the PVT data, from a signature, into an ECC point.
1277
 *
1278
 * A verifying client decodes the data so that it can calculate the identity
1279
 * hash.
1280
 *
1281
 * X and y ordinate of public key concatenated. Each number is zero padded to
1282
 * key size.
1283
 * Descriptor byte (0x04) is prepended when not raw.
1284
 *
1285
 * @param  [in]   key   ECCSI key.
1286
 * @param  [in]   sig   Buffer holding signature data.
1287
 * @param  [in]   sz    Size of data in bytes.
1288
 * @param  [out]  pvt   Public Validation Token (PVT) as an ECC point.
1289
 * @return  0 on success.
1290
 * @return  BAD_FUNC_ARG when key, data, ssk or pvt is NULL.
1291
 * @return  BUFFER_E when size of data is not equal to the expected size.
1292
 * @return  ASN_PARSE_E when format byte is invalid.
1293
 * @return  MP_MEM or MEMORY_E when dynamic memory allocation fails.
1294
 */
1295
int wc_DecodeEccsiPvtFromSig(const EccsiKey* key, const byte* sig, word32 sz,
1296
        ecc_point* pvt)
1297
0
{
1298
0
    int err = 0;
1299
1300
0
    if ((key == NULL) || (sig == NULL) || (pvt == NULL)) {
1301
0
        err = BAD_FUNC_ARG;
1302
0
    }
1303
1304
0
    if (err == 0) {
1305
0
        word32 rSz = (word32)(key->ecc.dp->size * 2);
1306
0
        err = eccsi_decode_point(pvt, (word32)key->ecc.dp->size, sig + rSz,
1307
0
                sz - rSz);
1308
0
    }
1309
1310
0
    return err;
1311
0
}
1312
1313
/**
1314
 * Import the KMS Public Auth Key (KPAK) into the ECCSI object.
1315
 *
1316
 * Clients import the KPAK to perform cryptographic operations.
1317
 *
1318
 * X and y ordinate of public key concatenated. Each number is zero padded to
1319
 * key size.
1320
 * Descriptor byte (0x04) is prepended when not raw.
1321
 *
1322
 * @param  [in]  key      ECCSI key.
1323
 * @param  [in]  data     Encoded public key as an array of bytes.
1324
 * @param  [in]  sz       Length of encoded KPAK in bytes.
1325
 * @param  [in]  trusted  1 when public key is trusted.
1326
 *                        0 when validation is required to be performed.
1327
 * @return  0 on success.
1328
 * @return  BAD_FUNC_ARG when key or data is NULL.
1329
 * @return  BUFFER_E when size of data is not equal to the expected size.
1330
 * @return  ASN_PARSE_E when format byte is invalid.
1331
 * @return  ECC_OUT_OF_RANGE_E when point is invalid.
1332
 * @return  ECC_INF_E when point is at infinity and invalid.
1333
 * @return  MP_MEM or MEMORY_E when dynamic memory allocation fails.
1334
 */
1335
int wc_ImportEccsiPublicKey(EccsiKey* key, const byte* data, word32 sz,
1336
        int trusted)
1337
0
{
1338
0
    int err = 0;
1339
1340
0
    if ((key == NULL) || (data == NULL)) {
1341
0
        err = BAD_FUNC_ARG;
1342
0
    }
1343
1344
0
    if (err == 0) {
1345
0
        key->kpakMont = 0;
1346
1347
        /* Read the public key. */
1348
0
        err = eccsi_decode_point(&key->ecc.pubkey, (word32)key->ecc.dp->size,
1349
0
                data, sz);
1350
0
    }
1351
0
    if (err == 0) {
1352
0
        key->ecc.type = ECC_PUBLICKEY;
1353
0
    }
1354
0
    if ((err == 0) && (!trusted)) {
1355
0
       err = wc_ecc_check_key(&key->ecc);
1356
0
    }
1357
1358
0
    return err;
1359
0
}
1360
1361
/*
1362
 * Scalar multiply the base point of the curve and add a point.
1363
 *
1364
 * @param  [in]   key   ECCSI key.
1365
 * @param  [in]   n     MP integer representing scalar to multiply by.
1366
 * @param  [in]   a     ECC point to add.
1367
 * @param  [out]  res   ECC point representation of the resulting point.
1368
 * @param  [in]   mp    Montgomery reduction multiplier.
1369
 * @param  [in]   map   0 indicates to leave in projective representation.
1370
 *                      1 indicates map projective point to affine.
1371
 * @return  0 on success.
1372
 * @return  MEMORY_E when dynamic memory allocation fails.
1373
 * @return  Other -ve value when an internal operation fails.
1374
 */
1375
static int eccsi_mulmod_base_add(EccsiKey* key, const mp_int* n,
1376
        ecc_point* a, ecc_point* res, mp_digit mp, int map)
1377
0
{
1378
0
    int err = 0;
1379
1380
#if defined(WOLFSSL_HAVE_SP_ECC) && !defined(WOLFSSL_SP_NO_256)
1381
    if ((key->ecc.idx != ECC_CUSTOM_IDX) &&
1382
            (ecc_sets[key->ecc.idx].id == ECC_SECP256R1)) {
1383
        err = sp_ecc_mulmod_base_add_256(n, a, 1, res, map, key->heap);
1384
    }
1385
    else
1386
#endif
1387
0
#ifndef WOLFSSL_SP_MATH
1388
0
    {
1389
0
        EccsiKeyParams* params = &key->params;
1390
0
        err = wc_ecc_mulmod(n, params->base, params->base, &params->a,
1391
0
                &params->prime, 0);
1392
0
        key->params.haveBase = 0;
1393
0
        if (err == 0) {
1394
0
            err = ecc_projective_add_point(params->base, a, res, &params->a,
1395
0
                    &params->prime, mp);
1396
0
        }
1397
0
        if ((err == 0) && map) {
1398
0
            err = ecc_map(res, &params->prime, mp);
1399
0
        }
1400
0
    }
1401
#else
1402
    {
1403
        err = NOT_COMPILED_IN;
1404
    }
1405
    (void)key;
1406
    (void)n;
1407
    (void)a;
1408
    (void)res;
1409
    (void)mp;
1410
    (void)map;
1411
#endif
1412
1413
0
    return err;
1414
0
}
1415
1416
/*
1417
 * Scalar multiply a point on the curve.
1418
 *
1419
 * @param  [in]   key    ECCSI key.
1420
 * @param  [in]   n      MP integer representing scalar to multiply by.
1421
 * @param  [in]   point  ECC point representation of a point on the curve.
1422
 * @param  [out]  res    ECC point representation of the resulting point.
1423
 * @param  [in]   map    0 indicates to leave in projective representation.
1424
 *                       1 indicates map projective point to affine.
1425
 * @return  0 on success.
1426
 * @return  MEMORY_E when dynamic memory allocation fails.
1427
 * @return  Other -ve value when an internal operation fails.
1428
 */
1429
static int eccsi_mulmod_point(EccsiKey* key, const mp_int* n, ecc_point* point,
1430
        ecc_point* res, int map)
1431
0
{
1432
0
    int err;
1433
1434
#if defined(WOLFSSL_HAVE_SP_ECC) && !defined(WOLFSSL_SP_NO_256)
1435
    if ((key->ecc.idx != ECC_CUSTOM_IDX) &&
1436
            (ecc_sets[key->ecc.idx].id == ECC_SECP256R1)) {
1437
        err = sp_ecc_mulmod_256(n, point, res, map, key->heap);
1438
    }
1439
    else
1440
#endif
1441
0
    {
1442
0
        EccsiKeyParams* params = &key->params;
1443
1444
0
        err = wc_ecc_mulmod(n, point, res, &params->a, &params->prime, map);
1445
0
    }
1446
1447
0
    return err;
1448
0
}
1449
1450
/*
1451
 * Scalar multiply a point on the curve and add a.
1452
 *
1453
 * @param  [in]   key    ECCSI key.
1454
 * @param  [in]   n      MP integer representing scalar to multiply by.
1455
 * @param  [in]   point  ECC point representation of a point on the curve.
1456
 * @param  [in]   a      ECC point to add.
1457
 * @param  [out]  res    ECC point representation of the resulting point.
1458
 * @param  [in]   mp     Montgomery reduction multiplier.
1459
 * @param  [in]   map    0 indicates to leave in projective representation.
1460
 *                       1 indicates map projective point to affine.
1461
 * @return  0 on success.
1462
 * @return  MEMORY_E when dynamic memory allocation fails.
1463
 * @return  Other -ve value when an internal operation fails.
1464
 */
1465
static int eccsi_mulmod_point_add(EccsiKey* key, const mp_int* n,
1466
        ecc_point* point, ecc_point* a, ecc_point* res, mp_digit mp, int map)
1467
0
{
1468
#if defined(WOLFSSL_HAVE_SP_ECC) && !defined(WOLFSSL_SP_NO_256)
1469
    int err = WC_NO_ERR_TRACE(NOT_COMPILED_IN);
1470
1471
    if ((key->ecc.idx != ECC_CUSTOM_IDX) &&
1472
            (ecc_sets[key->ecc.idx].id == ECC_SECP256R1)) {
1473
        err = sp_ecc_mulmod_add_256(n, point, a, 0, res, map, key->heap);
1474
    }
1475
1476
    (void)mp;
1477
1478
    return err;
1479
#else
1480
0
    int err;
1481
0
    EccsiKeyParams* params = &key->params;
1482
1483
0
    err = wc_ecc_mulmod(n, point, res, &params->a, &params->prime, 0);
1484
0
    if (err == 0) {
1485
0
        err = ecc_projective_add_point(res, a, res, &key->params.a,
1486
0
                &params->prime, mp);
1487
0
    }
1488
0
    if ((err == 0) && map) {
1489
0
        err = ecc_map(res, &params->prime, mp);
1490
0
    }
1491
1492
0
    return err;
1493
0
#endif
1494
0
}
1495
1496
/**
1497
 * Validate an (SSV, PVT) Pair.
1498
 *
1499
 * RFC 6507, Section 5.1.2
1500
 *
1501
 * A signing client should validate the key pair before first use.
1502
 *
1503
 * @param  [in]   key       ECCSI key.
1504
 * @param  [in]   hashType  Type of hash algorithm. e.g. WC_SHA256
1505
 * @param  [in]   id        Identity to create hash from.
1506
 * @param  [in]   idSz      Length of identity in bytes.
1507
 * @param  [in]   ssk       Secret Signing Key as an MP integer.
1508
 * @param  [in]   pvt       Public Validation Token (PVT) as an ECC point.
1509
 * @param  [out]  valid     1 when pair is valid and 0 otherwise.
1510
 * @return  0 on success.
1511
 * @return  BAD_FUNC_ARG when key, id, ssk, pvt or valid is NULL.
1512
 * @return  BAD_STATE_E when curve not set (key not set).
1513
 * @return  MP_MEM or MEMORY_E when dynamic memory allocation fails.
1514
 * @return  IS_POINT_E when point is not on the curve.
1515
 * @return  Other -ve value when an internal operation fails.
1516
 */
1517
int wc_ValidateEccsiPair(EccsiKey* key, enum wc_HashType hashType,
1518
        const byte* id, word32 idSz, const mp_int* ssk, ecc_point* pvt,
1519
        int* valid)
1520
0
{
1521
0
    int err = 0;
1522
0
    ecc_point* res = NULL;
1523
0
    mp_int* hs = NULL;
1524
0
    mp_digit mp = 0;
1525
0
    byte hashSz = 0;
1526
0
    EccsiKeyParams* params = NULL;
1527
1528
0
    if ((key == NULL) || (id == NULL) || (ssk == NULL) || (pvt == NULL) ||
1529
0
            (valid == NULL)) {
1530
0
        err = BAD_FUNC_ARG;
1531
0
    }
1532
1533
0
    if ((err == 0) && (key->ecc.type != ECC_PRIVATEKEY) &&
1534
0
            (key->ecc.type != ECC_PUBLICKEY)) {
1535
0
        err = BAD_STATE_E;
1536
0
    }
1537
1538
0
    if (err != 0)
1539
0
        return err;
1540
1541
0
    params = &key->params;
1542
0
    hs = &key->tmp;
1543
0
    res = &key->pubkey.pubkey;
1544
1545
0
    err = eccsi_load_base(key);
1546
1547
0
    if (err == 0) {
1548
0
       err = eccsi_load_ecc_params(key);
1549
0
    }
1550
0
    if (err == 0) {
1551
0
        err = mp_montgomery_setup(&params->prime, &mp);
1552
0
    }
1553
1554
    /* Step 1: Validate PVT is on curve */
1555
0
    if (err == 0) {
1556
0
        err = wc_ecc_is_point(pvt, &params->a, &params->b, &params->prime);
1557
0
        if (err == -1) {
1558
0
            err = IS_POINT_E;
1559
0
        }
1560
0
    }
1561
1562
    /* Step 2: Compute HS = hash( G | KPAK | ID | PVT ) */
1563
0
    if (err == 0) {
1564
0
        hashSz = (byte)sizeof(key->data);
1565
        /* Converts KPAK from mont. */
1566
0
        err = eccsi_compute_hs(key, hashType, id, idSz, pvt, key->data,
1567
0
                &hashSz);
1568
0
    }
1569
1570
    /* Step 3: Validate that KPAK = [SSK]G - [HS]PVT */
1571
0
    if (err == 0) {
1572
0
        err = mp_read_unsigned_bin(hs, key->data, hashSz);
1573
0
    }
1574
    /* [HS]PVT */
1575
0
    if (err == 0) {
1576
0
        err = eccsi_mulmod_point(key, hs, pvt, res, 0);
1577
0
    }
1578
    /* -[HS]PVT */
1579
0
    if (err == 0) {
1580
0
        err = mp_sub(&params->prime, res->y, res->y);
1581
0
    }
1582
    /* [SSK]G + -[HS]PVT */
1583
0
    if (err == 0) {
1584
0
        err = eccsi_mulmod_base_add(key, ssk, res, res, mp, 1);
1585
0
    }
1586
0
    if (valid != NULL) {
1587
0
        *valid = (err == 0);
1588
0
        if (err == 0) {
1589
0
            ecc_point* kpak = &key->ecc.pubkey;
1590
            /* Compare KPAK and [SSK]G + -[HS]PVT */
1591
0
            *valid = (wc_ecc_cmp_point(res, kpak) == MP_EQ);
1592
0
        }
1593
0
    }
1594
1595
0
    return err;
1596
0
}
1597
1598
/**
1599
 * Validate Public Validation Token (PVT) is on the curve.
1600
 *
1601
 * RFC 6507, Section 5.1.2, Step 1
1602
 *
1603
 * A verifying client should validate the PVT before first use.
1604
 *
1605
 * @param  [in]   key       ECCSI key.
1606
 * @param  [in]   pvt       Public Validation Token (PVT) as an ECC point.
1607
 * @param  [out]  valid     1 when PVT is valid and 0 otherwise.
1608
 * @return  0 on success.
1609
 * @return  BAD_FUNC_ARG when key, pvt or valid is NULL.
1610
 * @return  BAD_STATE_E when curve not set (key not set).
1611
 * @return  MP_MEM or MEMORY_E when dynamic memory allocation fails.
1612
 * @return  Other -ve value when an internal operation fails.
1613
 */
1614
int wc_ValidateEccsiPvt(EccsiKey* key, const ecc_point* pvt, int* valid)
1615
0
{
1616
0
    int err = 0;
1617
1618
0
    if ((key == NULL)| (pvt == NULL) || (valid == NULL)) {
1619
0
        err = BAD_FUNC_ARG;
1620
0
    }
1621
1622
0
    if (err == 0) {
1623
0
        err = wc_ecc_set_curve(&key->pubkey, key->ecc.dp->size,
1624
0
                key->ecc.dp->id);
1625
0
    }
1626
0
    if (err == 0) {
1627
0
        err = wc_ecc_copy_point(pvt, &key->pubkey.pubkey);
1628
0
    }
1629
0
    if (err == 0) {
1630
0
        *valid = (wc_ecc_check_key(&key->pubkey) == 0);
1631
0
    }
1632
1633
0
    return err;
1634
0
}
1635
1636
/**
1637
 * Creates the Hash of the ID and PVT with the ECCSI key.
1638
 *
1639
 * The hash ID is required as input to the sign and verify operations.\n
1640
 * Signing clients may cache this value.
1641
 *
1642
 * RFC 6507, Section 5.2.1, Step 3
1643
 *
1644
 * Set the calculated hash internally for use.
1645
 *
1646
 * @param  [in]   key       ECCSI key.
1647
 * @param  [in]   hashType  Type of hash algorithm. e.g. WC_SHA256
1648
 * @param  [in]   id        Identity to create hash from.
1649
 * @param  [in]   idSz      Length of identity in bytes.
1650
 * @param  [in]   pvt       Public Validation Token (PVT) as an ECC point.
1651
 * @param  [out]  hash      Buffer to hold hash result.
1652
 * @param  [out]  hashSz    Length of hash data in bytes.
1653
 * @return  0 on success.
1654
 * @return  BAD_FUNC_ARG when key, id, pvt, hash or hashSz is NULL.
1655
 * @return  BAD_FUNC_ARG when hash size doesn't match curve size.
1656
 * @return  BAD_STATE_E when public key not set.
1657
 * @return  MEMORY_E when dynamic memory allocation fails.
1658
 * @return  Other -ve value when an internal operation fails.
1659
 */
1660
int wc_HashEccsiId(EccsiKey* key, enum wc_HashType hashType, const byte* id,
1661
        word32 idSz, ecc_point* pvt, byte* hash, byte* hashSz)
1662
0
{
1663
0
    int err = 0;
1664
0
    int dgstSz = -1;
1665
0
    int curveSz = -1;
1666
1667
0
    if ((key == NULL) || (id == NULL) || (pvt == NULL) || (hash == NULL) ||
1668
0
            (hashSz == NULL)) {
1669
0
        err = BAD_FUNC_ARG;
1670
0
    }
1671
0
    if ((err == 0) && (key->ecc.type != ECC_PRIVATEKEY) &&
1672
0
            (key->ecc.type != ECC_PUBLICKEY)) {
1673
0
        err = BAD_STATE_E;
1674
0
    }
1675
    /* Ensure digest output size matches curve size (RFC 6507 4.1). */
1676
0
    if (err == 0) {
1677
0
        dgstSz = wc_HashGetDigestSize(hashType);
1678
0
        if (dgstSz < 0) {
1679
0
            err = dgstSz;
1680
0
        }
1681
0
    }
1682
0
    if (err == 0) {
1683
0
        curveSz = wc_ecc_get_curve_size_from_id(key->ecc.dp->id);
1684
0
        if (curveSz < 0) {
1685
0
            err = curveSz;
1686
0
        }
1687
0
    }
1688
0
    if ((err == 0) && (dgstSz != curveSz)) {
1689
0
        err = BAD_FUNC_ARG;
1690
0
    }
1691
    /* Load the curve parameters for operations */
1692
0
    if (err == 0) {
1693
0
       err = eccsi_load_ecc_params(key);
1694
0
    }
1695
0
    if (err == 0) {
1696
0
        err = eccsi_compute_hs(key, hashType, id, idSz, pvt, hash, hashSz);
1697
0
    }
1698
0
    if (err == 0) {
1699
0
        XMEMCPY(key->idHash, hash, *hashSz);
1700
0
        key->idHashSz = *hashSz;
1701
0
    }
1702
1703
0
    return err;
1704
0
}
1705
1706
/**
1707
 * Set the identity hash for use with signing/verification.
1708
 *
1709
 * @param  [in]  key     ECCSI key.
1710
 * @param  [in]  hash    Buffer with hash of identity.
1711
 * @param  [in]  hashSz  Length of hash data in bytes.
1712
 * @return  0 on success.
1713
 * @return  BAD_FUNC_ARG when key or hash is NULL, or hashSz is greater than
1714
 *          WC_MAX_DIGEST_SIZE.
1715
 */
1716
int wc_SetEccsiHash(EccsiKey* key, const byte* hash, byte hashSz)
1717
0
{
1718
0
    int err = 0;
1719
1720
0
    if ((key == NULL) || (hash == NULL) || (hashSz > WC_MAX_DIGEST_SIZE)) {
1721
0
        err = BAD_FUNC_ARG;
1722
0
    }
1723
0
    if (err == 0) {
1724
0
        XMEMCPY(key->idHash, hash, hashSz);
1725
0
        key->idHashSz = hashSz;
1726
0
    }
1727
1728
0
    return err;
1729
0
}
1730
1731
/**
1732
 * Set an (SSV, PVT) Pair for signing.
1733
 *
1734
 * @param  [in]   key  ECCSI key.
1735
 * @param  [in]   ssk  Secret Signing Key as an MP integer.
1736
 * @param  [in]   pvt  Public Validation Token (PVT) as an ECC point.
1737
 * @return  0 on success.
1738
 * @return  BAD_FUNC_ARG when key, ssk or pvt is NULL.
1739
 * @return  MP math errors when copy fails
1740
 */
1741
int wc_SetEccsiPair(EccsiKey* key, const mp_int* ssk, const ecc_point* pvt)
1742
0
{
1743
0
    int err = 0;
1744
1745
0
    if ((key == NULL) || (ssk == NULL) || (pvt == NULL)) {
1746
0
        err = BAD_FUNC_ARG;
1747
0
    }
1748
1749
0
    if (err == 0) {
1750
0
        err = mp_copy(ssk, &key->ssk);
1751
0
    }
1752
1753
0
    if (err == 0) {
1754
0
        err = wc_ecc_copy_point(pvt, key->pvt);
1755
0
    }
1756
1757
0
    return err;
1758
0
}
1759
1760
#ifdef ECCSI_ORDER_MORE_BITS_THAN_PRIME
1761
/*
1762
 * Fit the number to the maximum number of bytes.
1763
 *
1764
 * If the number is too big then subtract from order.
1765
 * RFC 6507, Section 5.2.1, Note at end.
1766
 * This should only happen when order is larger than prime in bits.
1767
 *
1768
 * @param  [in]   a      MP integer to fix.
1769
 * @param  [in]   order  MP integer representing order of curve.
1770
 * @param  [in]   m      Maximum number of bytes to encode into.
1771
 * @param  [out]  r      MP integer that is the result after fixing.
1772
 * @return  0 on success.
1773
 * @return  MEMORY_E when dynamic memory allocation fails.
1774
 */
1775
static int eccsi_fit_to_octets(const mp_int* a, mp_int* order, int m,
1776
        mp_int* r)
1777
{
1778
    int err;
1779
1780
    if (mp_count_bits(a) > m * 8) {
1781
        err = mp_sub(order, (mp_int*)a, r);
1782
    }
1783
    else
1784
    {
1785
        err = mp_copy(a, r);
1786
    }
1787
1788
    return err;
1789
}
1790
#else
1791
/*
1792
 * Fit the number to the maximum number of bytes.
1793
 *
1794
 * If the number is too big then subtract from order.
1795
 * RFC 6507, Section 5.2.1, Note at end.
1796
 * This should only happen when order is larger than prime in bits.
1797
 *
1798
 * @param  [in]   a      MP integer to fix.
1799
 * @param  [in]   order  MP integer representing order of curve.
1800
 * @param  [in]   m      Maximum number of bytes to encode into.
1801
 * @param  [out]  r      MP integer that is the result after fixing.
1802
 * @return  0 on success.
1803
 * @return  MEMORY_E when dynamic memory allocation fails.
1804
 */
1805
static int eccsi_fit_to_octets(const mp_int* a, const mp_int* order, int m,
1806
        mp_int* r)
1807
0
{
1808
0
    (void)order;
1809
0
    (void)m;
1810
1811
    /* Duplicate line to stop static analyzer complaining. */
1812
0
    return mp_copy(a, r);
1813
0
}
1814
#endif
1815
1816
/*
1817
 * Compute the HE = hash( HS | r | M ), hash value of signature.
1818
 *
1819
 * Partial result required for signing and verification.
1820
 *
1821
 * @param  [in]   key       ECCSI key.
1822
 * @param  [in]   hashType  Type of hash algorithm. e.g. WC_SHA256
1823
 * @param  [in]   r         MP integer that is the first signature element.
1824
 * @param  [in]   msg       Message of signature.
1825
 * @param  [in]   msgSz     Length of message in bytes.
1826
 * @param  [out]  he        Signature hash.
1827
 * @param  [out]  heSz      Length of signature hash in bytes
1828
 * @return  0 on success.
1829
 * @return  MEMORY_E when dynamic memory allocation fails.
1830
 * @return  Other -ve value when an internal operation fails.
1831
 */
1832
static int eccsi_compute_he(EccsiKey* key, enum wc_HashType hashType,
1833
        mp_int* r, const byte* msg, word32 msgSz, byte* he, word32* heSz)
1834
0
{
1835
0
    int err = 0;
1836
0
    word32 dataSz = (word32)key->ecc.dp->size;
1837
0
    int hash_inited = 0;
1838
1839
    /* HE = hash( HS | r | M ) */
1840
0
    err = wc_HashInit_ex(&key->hash, hashType, key->heap, INVALID_DEVID);
1841
0
    if (err == 0) {
1842
0
        hash_inited = 1;
1843
        /* HS */
1844
0
        err = wc_HashUpdate(&key->hash, hashType, key->idHash, key->idHashSz);
1845
0
    }
1846
0
    if (err == 0) {
1847
0
        err = mp_to_unsigned_bin_len(r, key->data, (int)dataSz);
1848
0
    }
1849
0
    if (err == 0) {
1850
        /* r */
1851
0
        err = wc_HashUpdate(&key->hash, hashType, key->data, dataSz);
1852
0
    }
1853
0
    if (err == 0) {
1854
        /* M */
1855
0
        err = wc_HashUpdate(&key->hash, hashType, msg, msgSz);
1856
0
    }
1857
0
    if (err == 0) {
1858
0
        err = wc_HashFinal(&key->hash, hashType, he);
1859
0
    }
1860
0
    if (err == 0) {
1861
0
        *heSz = (word32)wc_HashGetDigestSize(hashType);
1862
0
    }
1863
1864
0
    if (hash_inited) {
1865
0
        (void)wc_HashFree(&key->hash, hashType);
1866
0
    }
1867
1868
0
    return err;
1869
0
}
1870
1871
/*
1872
 * Encode the signature = ( r | s | PVT )
1873
 *
1874
 * @param  [in]   key    ECCSI key.
1875
 * @param  [in]   r      MP integer that is the first signature element.
1876
 * @param  [in]   s      MP integer that is the second signature element.
1877
 * @param  [in]   pvt    ECC point representing Public Validation Token.
1878
 * @param  [out]  sig    Signature of message.
1879
 * @param  [out]  sigSz  Length of signature in bytes.
1880
 */
1881
static int eccsi_encode_sig(const EccsiKey* key, mp_int* r, mp_int* s,
1882
        byte* sig, word32* sigSz)
1883
0
{
1884
0
    int err;
1885
0
    word32 sz = (word32)key->ecc.dp->size;
1886
1887
0
    err = mp_to_unsigned_bin_len(r, sig, (int)sz);
1888
0
    if (err == 0) {
1889
0
        err = mp_to_unsigned_bin_len(s, sig + sz, (int)sz);
1890
0
    }
1891
0
    if (err == 0) {
1892
0
        *sigSz = (word32)(key->ecc.dp->size * 2 + 1);
1893
0
        err = wc_ecc_export_point_der(wc_ecc_get_curve_idx(key->ecc.dp->id),
1894
0
                 key->pvt, sig + sz * 2, sigSz);
1895
0
    }
1896
0
    if (err == 0) {
1897
0
        *sigSz = sz * 4 + 1;
1898
0
    }
1899
1900
0
    return err;
1901
0
}
1902
1903
/*
1904
 * Sign the ECCSI hash (of ID with the key) to two mp_int objects: r and s.
1905
 *
1906
 * RFC 6507, Section 5.2.1, Steps 1 to 4
1907
 *
1908
 * @param  [in]   key       ECCSI key.
1909
 * @param  [in]   rng       Random number generator.
1910
 * @param  [in]   hashType  Type of hash algorithm. e.g. WC_SHA256
1911
 * @param  [in]   msg       Message to sign.
1912
 * @param  [in]   msgSz     Length of message in bytes.
1913
 * @param  [out]  r         First big number integer part of signature.
1914
 * @param  [out]  s         Second big number integer part of signature.
1915
 * @return  0 on success.
1916
 * @return  MEMORY_E when dynamic memory allocation fails.
1917
 * @return  Other -ve value when an internal operation fails.
1918
 */
1919
static int eccsi_gen_sig(EccsiKey* key, WC_RNG* rng, enum wc_HashType hashType,
1920
        const byte* msg, word32 msgSz, mp_int* r, mp_int* s)
1921
0
{
1922
0
    int err = 0;
1923
0
    int sz = key->ecc.dp->size;
1924
0
    word32 heSz = 0;
1925
0
    const mp_int* jx = NULL;
1926
0
    mp_int* he = &key->tmp;
1927
0
    int genTryCnt = 0;
1928
1929
0
    do {
1930
        /* Don't infinitely gen sigs when random number generator fails. */
1931
0
        if ((++genTryCnt) > ECCSI_MAX_GEN_COUNT) {
1932
0
            err = RNG_FAILURE_E;
1933
0
        }
1934
1935
0
        if (err == 0) {
1936
0
            wc_ecc_free(&key->pubkey);
1937
1938
            /* Step 1 and 2: Generate ephemeral key - j, J = [j]G, r = Jx */
1939
0
            err = wc_ecc_make_key_ex(rng, sz, &key->pubkey, key->ecc.dp->id);
1940
0
        }
1941
0
        if (err == 0) {
1942
0
            jx = key->pubkey.pubkey.x;
1943
0
            err = eccsi_fit_to_octets(jx, &key->params.order, sz, r);
1944
0
        }
1945
1946
        /* Step 3: Compute HE = hash( HS | r | M ) */
1947
0
        if (err == 0) {
1948
0
            err = eccsi_compute_he(key, hashType, r, msg, msgSz, key->data,
1949
0
                    &heSz);
1950
0
        }
1951
1952
        /* Step 4: Verify that HE + r * SSK is non-zero modulo q */
1953
0
        if (err == 0) {
1954
0
            err = mp_read_unsigned_bin(he, key->data, heSz);
1955
0
        }
1956
        /* s' = r * SSK */
1957
0
        if (err == 0) {
1958
0
            err = mp_mulmod(r, &key->ssk, &key->params.order, s);
1959
0
        }
1960
        /* s' = HE + r * SSK */
1961
0
        if (err == 0) {
1962
0
            err = mp_addmod(he, s, &key->params.order, s);
1963
0
        }
1964
0
    }
1965
0
    while ((err == 0) && (mp_iszero(s) || (mp_cmp(s, he) == MP_EQ)));
1966
1967
0
    return err;
1968
0
}
1969
1970
1971
/**
1972
 * Sign the ECCSI hash (of ID with the key).
1973
 *
1974
 * RFC 6507, Section 5.2.1
1975
 *
1976
 * Must have imported KPAK using wc_ImportEccsiPublicKey() before calling.\n
1977
 * Use wc_HashEccsiId() to calculate the hash and wc_SetEccsiHash() to set
1978
 * the identity hash to use.
1979
 *
1980
 * @param  [in]   key       ECCSI key.
1981
 * @param  [in]   rng       Random number generator.
1982
 * @param  [in]   hashType  Type of hash algorithm. e.g. WC_SHA256
1983
 * @param  [in]   msg       Message to sign.
1984
 * @param  [in]   msgSz     Length of message in bytes.
1985
 * @param  [out]  sig       Signature of message.
1986
 * @param  [out]  sigSz     Length of signature in bytes.
1987
 * @return  0 on success.
1988
 * @return  BAD_FUNC_ARG when key, rng, msg or sigSz is NULL.
1989
 * @return  BAD_STATE_E when the curve or id hash has not been set (no key set).
1990
 * @return  LENGTH_ONLY_E when sig is NULL - sigSz is set.
1991
 * @return  MEMORY_E when dynamic memory allocation fails.
1992
 * @return  Other -ve value when an internal operation fails.
1993
 */
1994
int wc_SignEccsiHash(EccsiKey* key, WC_RNG* rng, enum wc_HashType hashType,
1995
        const byte* msg, word32 msgSz, byte* sig, word32* sigSz)
1996
0
{
1997
0
    int err = 0;
1998
0
    mp_int* r = NULL;
1999
0
    mp_int* s = NULL;
2000
0
    mp_int* j = NULL;
2001
0
    word32 sz = 0;
2002
2003
0
    if ((key == NULL) || (rng == NULL) || (msg == NULL) || (sigSz == NULL)) {
2004
0
        err = BAD_FUNC_ARG;
2005
0
    }
2006
0
    if ((err == 0) && (key->ecc.type != ECC_PUBLICKEY) &&
2007
0
            (key->ecc.type != ECC_PRIVATEKEY)) {
2008
0
        err = BAD_STATE_E;
2009
0
    }
2010
0
    if ((err == 0) && (sig != NULL) && (key->idHashSz == 0)) {
2011
0
        err = BAD_STATE_E;
2012
0
    }
2013
2014
0
    if (err == 0)  {
2015
0
        sz = (word32)key->ecc.dp->size;
2016
0
        if (sig == NULL) {
2017
0
            *sigSz = sz * 4 + 1;
2018
0
            err = WC_NO_ERR_TRACE(LENGTH_ONLY_E);
2019
0
        }
2020
0
    }
2021
0
    if ((err == 0) && (*sigSz < sz * 4 + 1)) {
2022
0
        err = BAD_FUNC_ARG;
2023
0
    }
2024
2025
0
    if (err == 0) {
2026
0
        r = key->pubkey.pubkey.y;
2027
0
        s = key->pubkey.pubkey.z;
2028
2029
0
        err = eccsi_load_order(key);
2030
0
    }
2031
2032
0
    if (err == 0) {
2033
        /* Steps 1 to 4. */
2034
0
        err = eccsi_gen_sig(key, rng, hashType, msg, msgSz, r, s);
2035
0
    }
2036
2037
    /* Step 5: s' = ( (( HE + r * SSK )^-1) * j ) modulo q, erase j */
2038
0
    if (err == 0) {
2039
0
        err = mp_invmod(s, &key->params.order, s);
2040
0
    }
2041
0
    if (err == 0) {
2042
0
        j = wc_ecc_key_get_priv(&key->pubkey);
2043
0
        err = mp_mulmod(s, j, &key->params.order, s);
2044
        /* Erase j on the failure path too. */
2045
0
        ecc_forcezero_k(&key->pubkey);
2046
0
    }
2047
0
    if (err == 0) {
2048
        /* Step 6: s = s' fitted */
2049
0
        err = eccsi_fit_to_octets(s, &key->params.order, (int)sz, s);
2050
0
    }
2051
2052
    /* Step 7: Output Signature = ( r | s | PVT ) */
2053
0
    if (err == 0) {
2054
0
        err = eccsi_encode_sig(key, r, s, sig, sigSz);
2055
0
    }
2056
2057
0
    return err;
2058
0
}
2059
2060
/*
2061
 * Decode the s part of the signature = ( r | s | PVT )
2062
 *
2063
 * @param  [in]   key    ECCSI key.
2064
 * @param  [in]   sig    Signature of message.
2065
 * @param  [in]   sigSz  Length of signature in bytes.
2066
 * @param  [out]  s      MP integer that is the second signature element.
2067
 * @return  0 on success.
2068
 * @return  MEMORY_E when dynamic memory allocation fails.
2069
 * @return  Other -ve value when an internal operation fails.
2070
 */
2071
static int eccsi_decode_sig_s(const EccsiKey* key, const byte* sig,
2072
        word32 sigSz, mp_int* s)
2073
0
{
2074
0
    int err = 0;
2075
0
    word32 sz = (word32)key->ecc.dp->size;
2076
2077
0
    if (sigSz != sz * 4 + 1) {
2078
0
        err = BAD_FUNC_ARG;
2079
0
    }
2080
2081
0
    if (err == 0) {
2082
0
        err = mp_read_unsigned_bin(s, sig + sz, sz);
2083
0
    }
2084
2085
0
    return err;
2086
0
}
2087
2088
/*
2089
 * Decode the r and pvt part of the signature = ( r | s | PVT )
2090
 *
2091
 * @param  [in]   key    ECCSI key.
2092
 * @param  [in]   sig    Signature of message.
2093
 * @param  [in]   sigSz  Length of signature in bytes.
2094
 * @param  [out]  r      MP integer that is the first signature element.
2095
 * @param  [out]  pvt    ECC point representing Public Validation Token.
2096
 * @return  0 on success.
2097
 * @return  MEMORY_E when dynamic memory allocation fails.
2098
 * @return  Other -ve value when an internal operation fails.
2099
 */
2100
static int eccsi_decode_sig_r_pvt(const EccsiKey* key, const byte* sig,
2101
        word32 sigSz, mp_int* r, ecc_point* pvt)
2102
0
{
2103
0
    int err = 0;
2104
0
    word32 sz = (word32)key->ecc.dp->size;
2105
2106
0
    if (sigSz != sz * 4 + 1) {
2107
0
        err = BAD_FUNC_ARG;
2108
0
    }
2109
2110
0
    if (err == 0) {
2111
0
        err = mp_read_unsigned_bin(r, sig, sz);
2112
0
    }
2113
0
    if (err == 0) {
2114
        /* must free previous public point otherwise wc_ecc_import_point_der
2115
         * could leak memory */
2116
0
        mp_clear(pvt->x);
2117
0
        mp_clear(pvt->y);
2118
0
        mp_clear(pvt->z);
2119
2120
0
        err = wc_ecc_import_point_der(sig + sz * 2, sz * 2 + 1,
2121
0
                wc_ecc_get_curve_idx(key->ecc.dp->id), pvt);
2122
0
    }
2123
2124
0
    return err;
2125
0
}
2126
2127
/*
2128
 * Calculate Y point as part of verification process.
2129
 *
2130
 * Y = [HS]PVT + KPAK
2131
 *
2132
 * @param  [in]   key      ECCSI key.
2133
 * @param  [in]   pvt      ECC point representing Public Validation Token.
2134
 * @param  [in]   mp       Montgomery reduction multiplier.
2135
 * @param  [out]  y        ECC point representing calculated value Y.
2136
 * @return  0 on success.
2137
 * @return  MEMORY_E when dynamic memory allocation fails.
2138
 * @return  Other value when an an internal operation fails.
2139
 */
2140
static int eccsi_calc_y(EccsiKey* key, ecc_point* pvt, mp_digit mp,
2141
        ecc_point* y)
2142
0
{
2143
0
    int err;
2144
0
    mp_int* hs = &key->tmp;
2145
2146
0
#ifndef WOLFSSL_HAVE_SP_ECC
2147
0
    err = eccsi_kpak_to_mont(key);
2148
    /* Need KPAK in montgomery form. */
2149
0
    if (err == 0) {
2150
0
        err = mp_read_unsigned_bin(hs, key->idHash, key->idHashSz);
2151
0
    }
2152
#else
2153
    err = mp_read_unsigned_bin(hs, key->idHash, key->idHashSz);
2154
#endif
2155
0
    if (err == 0)
2156
0
    {
2157
        /* [HS]PVT + KPAK */
2158
0
        ecc_point* kpak = &key->ecc.pubkey;
2159
0
        err = eccsi_mulmod_point_add(key, hs, pvt, kpak, y, mp, 1);
2160
0
    }
2161
2162
0
    return err;
2163
0
}
2164
2165
/*
2166
 * Calculate J point as part of verification process.
2167
 *
2168
 * J = [s]( [HE]G + [r]Y )
2169
 *
2170
 * @param  [in]   key    ECCSI key.
2171
 * @param  [in]   hem    MP int representation of HE = Hash (hs, r and message).
2172
 * @param  [in]   sig    Signature of message.
2173
 * @param  [in]   sigSz  Length of signature in bytes.
2174
 * @param  [in]   y      ECC point representing [r]Y.
2175
 * @param  [in]   mp     Montgomery reduction multiplier.
2176
 * @param  [out]  j      ECC point representing calculated value J.
2177
 * @return  0 on success.
2178
 * @return  MEMORY_E when dynamic memory allocation fails.
2179
 * @return  Other value when an an internal operation fails.
2180
 */
2181
static int eccsi_calc_j(EccsiKey* key, const mp_int* hem, const byte* sig,
2182
        word32 sigSz, ecc_point* y, mp_digit mp, ecc_point* j)
2183
0
{
2184
0
    int err;
2185
0
    mp_int* s = &key->tmp;
2186
2187
    /* [HE]G + [r]Y */
2188
0
    err = eccsi_mulmod_base_add(key, hem, y, j, mp, 1);
2189
0
    if (err == 0) {
2190
0
        err = eccsi_decode_sig_s(key, sig, sigSz, s);
2191
0
    }
2192
    /* Validate s is in [1, q-1]: reject zero or out-of-range second signature
2193
     * component.  With s=0, [s](...) yields the point at infinity whose
2194
     * affine x-coordinate is 0, making the final mp_cmp(0,0) accept any
2195
     * forged signature. */
2196
0
    if (err == 0) {
2197
0
        if (mp_iszero(s)) {
2198
0
            err = MP_ZERO_E;
2199
0
        }
2200
0
        else if (mp_cmp(s, &key->params.order) != MP_LT) {
2201
0
            err = ECC_OUT_OF_RANGE_E;
2202
0
        }
2203
0
    }
2204
    /* [s]( [HE]G + [r]Y ) */
2205
0
    if (err == 0) {
2206
0
        err = eccsi_mulmod_point(key, s, j, j, 1);
2207
0
    }
2208
2209
0
    return err;
2210
0
}
2211
2212
/**
2213
 * Verify the ECCSI hash (of ID with the key).
2214
 *
2215
 * RFC 6507, Section 5.2.2
2216
 *
2217
 * Must have imported KPAK using wc_ImportEccsiPublicKey() before calling.\n
2218
 * Use wc_HashEccsiId() to calculate the hash and wc_SetEccsiHash() to set
2219
 * the identity hash to use.
2220
 *
2221
 * @param  [in]   key       ECCSI key.
2222
 * @param  [in]   hashType  Type of hash algorithm. e.g. WC_SHA256
2223
 * @param  [in]   msg       Message to verify.
2224
 * @param  [in]   msgSz     Length of message in bytes.
2225
 * @param  [in]   sig       Signature of message.
2226
 * @param  [in]   sigSz     Length of signature in bytes.
2227
 * @param  [out]  verified  1 when the signature was verified and 0 otherwise.
2228
 * @return  0 on success.
2229
 * @return  BAD_FUNC_ARG when key, hash, msg, sig or ret is NULL.
2230
 * @return  BAD_STATE_E when the curve or id hash has not been set (no key set).
2231
 * @return  MEMORY_E when dynamic memory allocation fails.
2232
 * @return  Other value when an an internal operation fails.
2233
 */
2234
int wc_VerifyEccsiHash(EccsiKey* key, enum wc_HashType hashType,
2235
        const byte* msg, word32 msgSz, const byte* sig, word32 sigSz,
2236
        int* verified)
2237
0
{
2238
0
    int err = 0;
2239
0
    byte* he = NULL;
2240
0
    word32 heSz = 0;
2241
0
    mp_int* r = NULL;
2242
0
    mp_int* jx = NULL;
2243
0
    mp_int* hem = NULL;
2244
0
    ecc_point* pvt = NULL;
2245
0
    ecc_point* y = NULL;
2246
0
    ecc_point* j = NULL;
2247
0
    mp_digit mp = 0;
2248
0
    EccsiKeyParams* params = NULL;
2249
2250
0
    if ((key == NULL) || (msg == NULL) || (sig == NULL) || (verified == NULL)) {
2251
0
        err = BAD_FUNC_ARG;
2252
0
    }
2253
0
    if ((err == 0) && (key->ecc.type != ECC_PRIVATEKEY) &&
2254
0
            (key->ecc.type != ECC_PUBLICKEY)) {
2255
0
        err = BAD_STATE_E;
2256
0
    }
2257
0
    if ((err == 0) && (key->idHashSz == 0)) {
2258
0
        err = BAD_STATE_E;
2259
0
    }
2260
2261
0
    if (err != 0)
2262
0
        return err;
2263
2264
    /* Decode the signature into components. */
2265
0
    r = wc_ecc_key_get_priv(&key->pubkey);
2266
0
    pvt = &key->pubkey.pubkey;
2267
0
    err = eccsi_decode_sig_r_pvt(key, sig, sigSz, r, pvt);
2268
2269
    /* Load the curve parameters for operations */
2270
0
    if (err == 0) {
2271
0
        err = eccsi_load_base(key);
2272
0
    }
2273
0
    if (err == 0) {
2274
0
        err = eccsi_load_ecc_params(key);
2275
0
    }
2276
0
    if (err == 0) {
2277
0
        params = &key->params;
2278
0
        err = mp_montgomery_setup(&params->prime, &mp);
2279
0
    }
2280
2281
    /* Validate r is in [1, q-1]: reject zero or out-of-range first signature
2282
     * component before any scalar multiplication takes place.
2283
     * Without this check, r=0 causes J_x=0 and the final mp_cmp(0,0)==MP_EQ
2284
     * comparison accepts the forged signature unconditionally. */
2285
0
    if (err == 0) {
2286
0
        if (mp_iszero(r)) {
2287
0
            err = MP_ZERO_E;
2288
0
        }
2289
0
        else if (mp_cmp(r, &params->order) != MP_LT) {
2290
0
            err = ECC_OUT_OF_RANGE_E;
2291
0
        }
2292
0
    }
2293
2294
    /* Step 1: Validate PVT is on curve */
2295
0
    if (err == 0) {
2296
0
        err = wc_ecc_is_point(pvt, &params->a, &params->b, &params->prime);
2297
0
    }
2298
2299
    /* Step 2: Compute HS = hash( G | KPAK | ID | PVT )
2300
     * HS is key->idHash, key->idHashSz */
2301
2302
    /* Step 3: Compute HE = hash( HS | r | M ) */
2303
0
    if (err == 0) {
2304
0
        he = key->data;
2305
0
        err = eccsi_compute_he(key, hashType, r, msg, msgSz, he, &heSz);
2306
0
    }
2307
2308
    /* Step 4: Y = [HS]PVT + KPAK */
2309
0
    if (err == 0) {
2310
0
        y = pvt;
2311
0
        err = eccsi_calc_y(key, pvt, mp, y);
2312
0
    }
2313
2314
    /* Step 5: Compute J = [s]( [HE]G + [r]Y ) */
2315
    /* [r]Y */
2316
0
    if (err == 0) {
2317
0
        hem = &key->tmp;
2318
0
        err = mp_read_unsigned_bin(hem, he, heSz);
2319
0
    }
2320
0
    if (err == 0) {
2321
0
        err = eccsi_mulmod_point(key, r, y, y, 0);
2322
0
    }
2323
0
    if (err == 0) {
2324
0
        j = params->base;
2325
0
        err = eccsi_calc_j(key, hem, sig, sigSz, y, mp, j);
2326
0
        key->params.haveBase = 0;
2327
0
    }
2328
2329
    /* Defense-in-depth: reject J = point at infinity before the final
2330
     * comparison. Catches any future path that might reach this point
2331
     * with a neutral-element result (e.g. s = 0 mod q for a non-zero
2332
     * encoded s). */
2333
0
    if (err == 0) {
2334
0
        if (wc_ecc_point_is_at_infinity(j)) {
2335
0
            err = ECC_INF_E;
2336
0
        }
2337
0
    }
2338
2339
    /* Step 6: Jx fitting, compare with r */
2340
0
    if (err == 0) {
2341
0
        jx = &key->tmp;
2342
0
        err = eccsi_fit_to_octets(j->x, &params->order, key->ecc.dp->size, jx);
2343
0
    }
2344
2345
0
    if (verified != NULL) {
2346
0
        *verified = ((err == 0) && (mp_cmp(jx, r) == MP_EQ));
2347
0
    }
2348
2349
0
    return err;
2350
0
}
2351
#endif /* WOLFCRYPT_ECCSI_CLIENT */
2352
2353
#endif /* WOLFCRYPT_HAVE_ECCSI */
2354