-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathwp_ecx_kmgmt.c
More file actions
3594 lines (3229 loc) · 112 KB
/
wp_ecx_kmgmt.c
File metadata and controls
3594 lines (3229 loc) · 112 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* wp_ecx_kmgmt.c
*
* Copyright (C) 2006-2025 wolfSSL Inc.
*
* This file is part of wolfProvider.
*
* wolfProvider is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfProvider is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with wolfProvider. If not, see <http://www.gnu.org/licenses/>.
*/
#include <openssl/err.h>
#include <openssl/proverr.h>
#include <openssl/core_dispatch.h>
#include <openssl/core_names.h>
#include <openssl/core_object.h>
#include <openssl/params.h>
#include <openssl/ec.h>
#include <openssl/obj_mac.h>
#include <openssl/evp.h>
#include <wolfprovider/settings.h>
#include <wolfprovider/alg_funcs.h>
#if defined(WP_HAVE_X25519) || defined(WP_HAVE_ED25519) || \
defined(WP_HAVE_X448) || defined(WP_HAVE_ED448)
/** Supported selections (key parts) in this key manager for ECX. */
#define WP_ECX_POSSIBLE_SELECTIONS \
(OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS)
/** Curve25519 for ECDH. */
#define WP_KEY_TYPE_X25519 1
/** Curve448 for ECDH. */
#define WP_KEY_TYPE_X448 2
/** Ed25519 for ECDSA. */
#define WP_KEY_TYPE_ED25519 3
/** Ed448 for ECDSA. */
#define WP_KEY_TYPE_ED448 4
/** Maximum key size. Used for exporting when comparing keys. */
#ifdef WP_HAVE_ED448
#define WP_MAX_KEY_SIZE ED448_KEY_SIZE
#elif defined(WP_HAVE_X448)
#define WP_MAX_KEY_SIZE CURVE448_KEY_SIZE
#elif defined(WP_HAVE_ED25519)
#define WP_MAX_KEY_SIZE ED25519_KEY_SIZE
#else
#define WP_MAX_KEY_SIZE CURVE25519_KEYSIZE
#endif
#ifdef WP_HAVE_X25519
#define ECX_LITTLE_ENDIAN EC25519_LITTLE_ENDIAN
#elif defined(WP_HAVE_X448)
#define ECX_LITTLE_ENDIAN EC448_LITTLE_ENDIAN
#else
#define ECX_LITTLE_ENDIAN 0
#endif
/** Type for function that initializes a wolfSSL key. */
typedef int (*WP_ECX_INIT)(void* key);
/** Type for function that frees a wolfSSL key. */
typedef void (*WP_ECX_FREE)(void* key);
/** Type for function that generates a wolfSSL key. */
typedef int (*WP_ECX_MAKE_KEY)(WC_RNG* rmg, int keySize, void* key);
/** Type for function that imports a public key into wolfSSL key. */
typedef int (*WP_ECX_IMPORT_PUB)(const byte* in, word32 inLen, void* key,
int endian);
/** Type for function that exports a public key from a wolfSSL key. */
typedef int (*WP_ECX_EXPORT_PUB)(void* key, const byte* out, word32* outLen,
int endian);
/** Type for function that imports a private key into wolfSSL key. */
typedef int (*WP_ECX_IMPORT_PRIV)(const byte* priv, word32 privLen, void* key,
int endian);
/** Type for function that exports a private key from a wolfSSL key. */
typedef int (*WP_ECX_EXPORT_PRIV)(void* key, const byte* out, word32* outLen);
/** Type for a wolfSSL function that checks a public key. */
typedef int (*WP_ECX_CHECK_PUB)(const byte* pub, word32 pubLen, int endian);
/** Type for a wolfSSL function that checks a key. */
typedef int (*WP_ECX_CHECK_KEY)(void* key);
/**
* ECX data and wolfSSL functions.
*/
typedef struct wp_EcxData {
/** Type of key. */
int keyType;
/** Cryptographic length in bits. */
int bits;
/** Length of curve in bytes. */
int len;
/* wolfSSL APIs. */
/** Initialize wolfSSL key object. */
WP_ECX_INIT initKey;
/** Free wolfSSL key object. */
WP_ECX_FREE freeKey;
/** wolfSSL make key. */
WP_ECX_MAKE_KEY makeKey;
/** Import public key into wolfSSL key object. */
WP_ECX_IMPORT_PUB importPub;
/** Export public key from wolfSSL key object. */
WP_ECX_EXPORT_PUB exportPub;
/** Import private key into wolfSSL key object. */
WP_ECX_IMPORT_PRIV importPriv;
/** Export private key from wolfSSL key object. */
WP_ECX_EXPORT_PRIV exportPriv;
/** wolfSSL check of public key value. */
WP_ECX_CHECK_PUB checkPub;
/** wolfSSL check of public/private key. */
WP_ECX_CHECK_KEY checkKey;
} wp_EcxData;
/**
* ECX key.
*/
struct wp_Ecx {
/** wolfSSL key - see data field for type. */
union {
#ifdef WP_HAVE_X25519
/** Curve25519 key object for ECDH. */
curve25519_key x25519;
#endif
#ifdef WP_HAVE_X448
/** Curve448 key object for ECDH. */
curve448_key x448;
#endif
#ifdef WP_HAVE_ED25519
/** Ed25519 key object for ECDSA. */
ed25519_key ed25519;
#endif
#ifdef WP_HAVE_ED448
/** Ed448 key object for ECDSA. */
ed448_key ed448;
#endif
} key;
/** Data including method table that operates on a wolfSSL key. */
const wp_EcxData* data;
/** Unclamped values to restore on export. */
byte unclamped[2];
#ifndef WP_SINGLE_THREADED
/** Mutex for reference count updating. */
wolfSSL_Mutex mutex;
#endif
/** Count of references to this object. */
int refCnt;
/** Provider context - for duplicating key. */
WOLFPROV_CTX* provCtx;
/** Include public key in ASN.1 encoding of private key. */
int includePublic;
/** Public key available. */
unsigned int hasPub:1;
/** Private key available. */
unsigned int hasPriv:1;
/* Private key was imported and has been clamped */
unsigned int clamped:1;
};
/**
* ECX key generation context.
*/
typedef struct wp_EcxGenCtx {
/** wolfSSL random number generator. */
WC_RNG rng;
/** Data including method table that operates on a wolfSSL key. */
const wp_EcxData* data;
/** Provider context - used when creating an ECX key. */
WOLFPROV_CTX* provCtx;
/** The parts of a ECX key to generate. */
int selection;
/** Name of group. */
const char* name;
} wp_EcxGenCtx;
/* Prototype for ECX generation initialization. */
static int wp_ecx_gen_set_params(wp_EcxGenCtx* ctx, const OSSL_PARAM params[]);
static size_t wp_ecx_export_keypair_alloc_size(wp_Ecx* ecx, int priv);
/*
* ECX key
*/
/**
* Increment reference count for key.
*
* Used in key generation, signing/verify and key exchange.
*
* @param [in, out] ecx ECX key object.
* @return 1 on success.
* @return 0 when multi-threaded and locking fails.
*/
int wp_ecx_up_ref(wp_Ecx* ecx)
{
#ifndef WP_SINGLE_THREADED
int ok = 1;
int rc;
WOLFPROV_ENTER(WP_LOG_COMP_KE, "wp_ecx_up_ref");
rc = wc_LockMutex(&ecx->mutex);
if (rc < 0) {
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "wc_LockMutex", rc);
ok = 0;
}
if (ok) {
ecx->refCnt++;
wc_UnLockMutex(&ecx->mutex);
}
WOLFPROV_LEAVE(WP_LOG_COMP_KE, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
#else
WOLFPROV_ENTER(WP_LOG_COMP_KE, "wp_ecx_up_ref");
ecx->refCnt++;
WOLFPROV_LEAVE(WP_LOG_COMP_KE, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), 1);
return 1;
#endif
}
/**
* Get the wolfSSL object from the ECX key object.
*
* @param [in] ecx ECX key object.
* @return Pointer to wolfSSL object.
*/
void* wp_ecx_get_key(wp_Ecx* ecx)
{
return (void*)&ecx->key;
}
/**
* Get the mutex object from the ECX key object.
*
* @param [in] ecx ECX key object.
* @return Pointer to wolfSSL mutex object.
*/
wolfSSL_Mutex* wp_ecx_get_mutex(wp_Ecx* ecx)
{
return &ecx->mutex;
}
/**
* Create a new ECX key object. Base function.
*
* @param [in] provCtx Provider context.
* @param [in] data wolfSSL data for curve.
* @return New ECX key object on success.
* @return NULL on failure.
*/
static wp_Ecx* wp_ecx_new(WOLFPROV_CTX* provCtx, const wp_EcxData* data)
{
wp_Ecx* ecx = NULL;
if (wolfssl_prov_is_running()) {
ecx = (wp_Ecx*)OPENSSL_zalloc(sizeof(*ecx));
}
if (ecx != NULL) {
int ok = 1;
int rc;
rc = (*data->initKey)((void*)&ecx->key);
if (rc != 0) {
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "initKey", rc);
ok = 0;
}
#ifndef SINGLE_THREADED
if (ok) {
rc = wc_InitMutex(&ecx->mutex);
if (rc != 0) {
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "wc_InitMutex", rc);
(*data->freeKey)(&ecx->key);
ok = 0;
}
}
#endif
if (ok) {
ecx->provCtx = provCtx;
ecx->refCnt = 1;
ecx->data = data;
}
if (!ok) {
OPENSSL_free(ecx);
ecx = NULL;
}
}
return ecx;
}
/**
* Dispose of ECX key object.
*
* @param [in, out] ecx ECX key object.
*/
void wp_ecx_free(wp_Ecx* ecx)
{
if (ecx != NULL) {
int cnt;
#ifndef WP_SINGLE_THREADED
int rc;
rc = wc_LockMutex(&ecx->mutex);
if (rc < 0) {
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "wc_LockMutex", rc);
}
cnt = --ecx->refCnt;
if (rc == 0) {
wc_UnLockMutex(&ecx->mutex);
}
#else
cnt = --ecx->refCnt;
#endif
if (cnt == 0) {
#ifndef WP_SINGLE_THREADED
wc_FreeMutex(&ecx->mutex);
#endif
(*ecx->data->freeKey)((void*)&ecx->key);
OPENSSL_free(ecx);
}
}
}
/**
* Duplicate specific parts of a ECX key object.
*
* @param [in] src Source ECX key object.
* @param [in] selection Parts of key to include.
* @return NULL on failure.
* @return New ECX key object on success.
*/
static wp_Ecx* wp_ecx_dup(const wp_Ecx* src, int selection)
{
wp_Ecx* dst = NULL;
(void)selection;
if (wolfssl_prov_is_running()) {
/* Create a new ecx object. */
dst = wp_ecx_new(src->provCtx, src->data);
}
if (dst != NULL) {
XMEMCPY(&dst->key, &src->key, sizeof(src->key));
dst->includePublic = src->includePublic;
dst->hasPub = src->hasPub;
dst->hasPriv = src->hasPriv;
}
return dst;
}
/**
* Load the ECX key.
*
* Return the ECX key object taken out of the reference.
*
* @param [in, out] pEcx Pointer to a ECX key object.
* @param [in] size Size of data structure that is the ECX key object.
* Unused.
* @return NULL when no ECX key object at reference.
* @return ECX key object from reference on success.
*/
static const wp_Ecx* wp_ecx_load(const wp_Ecx** pEcx, size_t size)
{
const wp_Ecx* ecx = *pEcx;
/* TODO: validate the object is a wp_Ecx? */
(void)size;
*pEcx = NULL;
return ecx;
}
/**
* Return an array of supported settable parameters for the ECX key.
*
* @param [in] provCtx Provider context object. Unused.
* @return Array of parameters with data type.
*/
static const OSSL_PARAM* wp_ecx_settable_params(WOLFPROV_CTX* provCtx)
{
/**
* Supported settable parameters for ECX key.
*/
static const OSSL_PARAM wp_ecx_supported_settable_params[] = {
OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
OSSL_PARAM_END
};
(void)provCtx;
return wp_ecx_supported_settable_params;
}
/**
* Set the ECX key parameters.
*
* @param [in, out] ecx ECX key object.
* @param [in] params Array of parameters and values.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_ecx_set_params(wp_Ecx* ecx, const OSSL_PARAM params[])
{
int ok = 1;
unsigned char* data = NULL;
size_t len;
WOLFPROV_ENTER(WP_LOG_COMP_KE, "wp_ecx_set_params");
if (!wp_params_get_octet_string_ptr(params,
OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, &data, &len)) {
ok = 0;
}
if (ok && (data != NULL)) {
int rc = (*ecx->data->importPub)(data, (word32)len, (void*)&ecx->key,
ECX_LITTLE_ENDIAN);
if (rc != 0) {
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "importPub", rc);
ok = 0;
}
if (ok) {
ecx->hasPub = 1;
}
}
WOLFPROV_LEAVE(WP_LOG_COMP_KE, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Return an array of supported gettable parameters for the ECX key object.
*
* @param [in] provCtx Provider context object. Unused.
* @return Array of parameters with data type.
*/
static const OSSL_PARAM* wp_ecx_gettable_params(WOLFPROV_CTX* provCtx)
{
/**
* Supported gettable parameters for ECX key object.
*/
static const OSSL_PARAM wp_ecx_supported_gettable_params[] = {
OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0),
OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
OSSL_PARAM_END
};
(void)provCtx;
return wp_ecx_supported_gettable_params;
}
/**
* Get the security bits for an ECX key.
*
* @param [in] ecx ECX key object.
* @return Security bits on success.
* @return 0 on failure.
*/
static int wp_ecx_get_security_bits(wp_Ecx* ecx)
{
int bits = 0;
if (ecx->data->bits >= 456) {
bits = 224;
}
else if (ecx->data->bits >= 256) {
bits = 128;
}
return bits;
}
/**
* Get the encoded public key into parameters.
*
* @param [in] ecx ECX key object.
* @param [in, out] params Array of parameters and values.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_ecx_get_params_enc_pub_key(wp_Ecx* ecx, OSSL_PARAM params[],
const char* key)
{
int ok = 1;
OSSL_PARAM* p;
WOLFPROV_ENTER(WP_LOG_COMP_KE, "wp_ecx_get_params_enc_pub_key");
p = OSSL_PARAM_locate(params, key);
if (p != NULL) {
word32 outLen = (word32)p->return_size;
if (p->data == NULL) {
outLen = ecx->data->len;
}
else {
int rc = (*ecx->data->exportPub)((void*)&ecx->key, p->data,
&outLen, ECX_LITTLE_ENDIAN);
if (rc != 0) {
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "exportPub", rc);
ok = 0;
}
}
p->return_size = outLen;
}
WOLFPROV_LEAVE(WP_LOG_COMP_KE, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Get the encoded private key into parameters.
*
* @param [in] ecx ECX key object.
* @param [in, out] params Array of parameters and values.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_ecx_get_params_priv_key(wp_Ecx* ecx, OSSL_PARAM params[])
{
int ok = 1;
OSSL_PARAM* p;
WOLFPROV_ENTER(WP_LOG_COMP_KE, "wp_ecx_get_params_priv_key");
p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_PRIV_KEY);
if (p != NULL) {
word32 outLen = (word32)p->return_size;
if (p->data == NULL) {
outLen = ecx->data->len;
}
else {
PRIVATE_KEY_UNLOCK();
int rc = (*ecx->data->exportPriv)((void*)&ecx->key, p->data,
&outLen);
PRIVATE_KEY_LOCK();
if (rc != 0) {
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "exportPriv", rc);
ok = 0;
}
if (ok && ecx->clamped) {
if ((outLen < 2) || (p->data_size < outLen)) {
ok = 0;
}
else {
((unsigned char*)p->data)[0 ] = ecx->unclamped[0];
((unsigned char*)p->data)[outLen - 1] = ecx->unclamped[1];
}
}
}
p->return_size = outLen;
}
WOLFPROV_LEAVE(WP_LOG_COMP_KE, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Get the ECX key parameters.
*
* @param [in] ecx ECX key object.
* @param [in, out] params Array of parameters and values.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_ecx_get_params(wp_Ecx* ecx, OSSL_PARAM params[])
{
int ok = 1;
OSSL_PARAM* p;
WOLFPROV_ENTER(WP_LOG_COMP_KE, "wp_ecx_get_params");
p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE);
if (p != NULL) {
if (!OSSL_PARAM_set_int(p, (int)wp_ecx_export_keypair_alloc_size(ecx, 1))) {
ok = 0;
}
}
if (ok) {
p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS);
if ((p != NULL) && !OSSL_PARAM_set_int(p, ecx->data->bits)) {
ok = 0;
}
}
if (ok) {
p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS);
if ((p != NULL) && (!OSSL_PARAM_set_int(p,
wp_ecx_get_security_bits(ecx)))) {
ok = 0;
}
}
if (ok && (!wp_ecx_get_params_enc_pub_key(ecx, params,
OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY))) {
ok = 0;
}
if (ok && (!wp_ecx_get_params_enc_pub_key(ecx, params,
OSSL_PKEY_PARAM_PUB_KEY))) {
ok = 0;
}
if (ok && (!wp_ecx_get_params_priv_key(ecx, params))) {
ok = 0;
}
WOLFPROV_LEAVE(WP_LOG_COMP_KE, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Check ECX key object has the components required.
*
* @param [in] ecx ECX key object.
* @param [in] selection Parts of key required.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_ecx_has(const wp_Ecx* ecx, int selection)
{
int ok = 1;
WOLFPROV_ENTER(WP_LOG_COMP_KE, "wp_ecx_has");
if (!wolfssl_prov_is_running()) {
ok = 0;
}
if (ecx == NULL) {
ok = 0;
}
if (ok && ((selection & WP_ECX_POSSIBLE_SELECTIONS) != 0)) {
if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
ok &= ecx->hasPub;
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
ok &= ecx->hasPriv;
}
WOLFPROV_LEAVE(WP_LOG_COMP_KE, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Check that two ECX key objects' private keys match.
*
* @param [in] ecx1 First ECX key object.
* @param [in] ecx2 Second ECX key object.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_ecx_match_priv_key(const wp_Ecx* ecx1, const wp_Ecx* ecx2)
{
int ok = 1;
int rc;
unsigned char key1[WP_MAX_KEY_SIZE];
word32 len1;
unsigned char key2[WP_MAX_KEY_SIZE];
word32 len2;
WOLFPROV_ENTER(WP_LOG_COMP_KE, "wp_ecx_match_priv_key");
XMEMSET(key1, 0, sizeof(key1));
XMEMSET(key2, 0, sizeof(key2));
ok &= ecx1->hasPriv && ecx2->hasPriv;
if (ok) {
len1 = ecx1->data->len;
PRIVATE_KEY_UNLOCK();
rc = (*ecx1->data->exportPriv)((void*)&ecx1->key, key1, &len1);
PRIVATE_KEY_LOCK();
if (rc != 0) {
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "exportPriv", rc);
ok = 0;
}
}
if (ok) {
len2 = ecx2->data->len;
PRIVATE_KEY_UNLOCK();
rc = (*ecx2->data->exportPriv)((void*)&ecx2->key, key2, &len2);
PRIVATE_KEY_LOCK();
if (rc != 0) {
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "exportPriv", rc);
ok = 0;
}
}
if (ok && (len1 != len2)) {
ok = 0;
}
if (ok && (XMEMCMP(key1, key2, len1) != 0)) {
ok = 0;
}
WOLFPROV_LEAVE(WP_LOG_COMP_KE, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Check that two ECX key objects' public keys match.
*
* @param [in] ecx1 First ECX key object.
* @param [in] ecx2 Second ECX key object.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_ecx_match_pub_key(const wp_Ecx* ecx1, const wp_Ecx* ecx2)
{
int ok = 1;
int rc;
unsigned char key1[WP_MAX_KEY_SIZE];
word32 len1;
unsigned char key2[WP_MAX_KEY_SIZE];
word32 len2;
WOLFPROV_ENTER(WP_LOG_COMP_KE, "wp_ecx_match_pub_key");
XMEMSET(key1, 0, sizeof(key1));
XMEMSET(key2, 0, sizeof(key2));
ok &= ecx1->hasPub && ecx2->hasPub;
if (ok) {
len1 = ecx1->data->len;
rc = (*ecx1->data->exportPub)((void*)&ecx1->key, key1, &len1,
ECX_LITTLE_ENDIAN);
if (rc != 0) {
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "exportPub", rc);
ok = 0;
}
}
if (ok) {
len2 = ecx2->data->len;
rc = (*ecx2->data->exportPub)((void*)&ecx2->key, key2, &len2,
ECX_LITTLE_ENDIAN);
if (rc != 0) {
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "exportPub", rc);
ok = 0;
}
}
if (ok && (len1 != len2)) {
ok = 0;
}
if (ok && (XMEMCMP(key1, key2, len1) != 0)) {
ok = 0;
}
WOLFPROV_LEAVE(WP_LOG_COMP_KE, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Check that two ECX key objects match for the components specified.
*
* @param [in] ecx1 First ECX key object.
* @param [in] ecx2 Second ECX key object.
* @param [in] selection Parts of key to match.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_ecx_match(const wp_Ecx* ecx1, const wp_Ecx* ecx2, int selection)
{
int ok = 1;
WOLFPROV_ENTER(WP_LOG_COMP_KE, "wp_ecx_match");
if (!wolfssl_prov_is_running()) {
ok = 0;
}
if (ok && (selection != 0)) {
/* Check for domain parameters, private key and public key. */
ok &= ecx1->data->keyType == ecx2->data->keyType;
}
if (ok && ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) &&
(!wp_ecx_match_priv_key(ecx1, ecx2))) {
ok = 0;
}
if (ok && ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) &&
(!wp_ecx_match_pub_key(ecx1, ecx2))) {
ok = 0;
}
WOLFPROV_LEAVE(WP_LOG_COMP_KE, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
#if defined(WP_HAVE_X25519) || defined(WP_HAVE_X448)
/**
* Validate the ECX public key - X25519 and X448 only.
*
* @param [in] ecx ECX key object.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_ecx_validate_pub_key(const wp_Ecx* ecx)
{
int ok = 1;
int rc;
unsigned char key[WP_MAX_KEY_SIZE] = {0};
word32 len = ecx->data->len;
WOLFPROV_ENTER(WP_LOG_COMP_KE, "wp_ecx_validate_pub_key");
ok &= ecx->hasPub;
if (ok) {
/* Get the public key out. */
rc = (*ecx->data->exportPub)((void*)&ecx->key, key, &len,
ECX_LITTLE_ENDIAN);
if (rc != 0) {
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "exportPub", rc);
ok = 0;
}
}
if (ok) {
/* Check the public key is valid. */
rc = (*ecx->data->checkPub)(key, len, ECX_LITTLE_ENDIAN);
if (rc != 0) {
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "checkPub", rc);
ok = 0;
}
}
WOLFPROV_LEAVE(WP_LOG_COMP_KE, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Validate the ECX key - X25519 and X448 only.
*
* @param [in] ecx ECX key object.
* @param [in] selection Parts of key to validate.
* @param [in] checkType How thorough to check key. Values:
* OSSL_KEYMGMT_VALIDATE_FULL_CHECK or
* OSSL_KEYMGMT_VALIDATE_QUICK_CHECK.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_ecx_x_validate(const wp_Ecx* ecx, int selection, int checkType)
{
int ok = 1;
WOLFPROV_ENTER(WP_LOG_COMP_KE, "wp_ecx_x_validate");
(void)checkType;
if (ok && ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) &&
(!wp_ecx_validate_pub_key(ecx))) {
ok = 0;
}
if (ok && ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)) {
ok &= ecx->hasPriv;
/* Nothing to do. The private key is valid as it has been clamped. */
}
WOLFPROV_LEAVE(WP_LOG_COMP_KE, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
#endif
#if defined(WP_HAVE_ED25519) || defined(WP_HAVE_ED448)
/**
* Validate the ECX key - Ed25519 and Ed448 only.
*
* @param [in] ecx ECX key object.
* @param [in] selection Parts of key to validate.
* @param [in] checkType How thorough to check key. Values:
* OSSL_KEYMGMT_VALIDATE_FULL_CHECK or
* OSSL_KEYMGMT_VALIDATE_QUICK_CHECK.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_ecx_ed_validate(const wp_Ecx* ecx, int selection, int checkType)
{
int ok = 1;
WOLFPROV_ENTER(WP_LOG_COMP_KE, "wp_ecx_ed_validate");
(void)checkType;
if (ok && ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)) {
ok &= ecx->hasPub;
/* Nothing to do. The public key is validated on import. */
}
if (ok && ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)) {
ok &= ecx->hasPriv;
/* Nothing to do. The private key is validated on import. */
}
if (ok && ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) ==
OSSL_KEYMGMT_SELECT_KEYPAIR)) {
int rc = (*ecx->data->checkKey)((void*)&ecx->key);
if (rc != 0) {
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "checkKey", rc);
ok = 0;
}
}
WOLFPROV_LEAVE(WP_LOG_COMP_KE, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
#endif
/**
* Import the key into ECX key object from parameters.
*
* @param [in, out] ecx ECX key object.
* @param [in] selection Parts of key to import.
* @param [in] params Array of parameters and values.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_ecx_import(wp_Ecx* ecx, int selection, const OSSL_PARAM params[])
{
int ok = 1;
int rc;
unsigned char* privData = NULL;
unsigned char* pubData = NULL;
size_t len;
WOLFPROV_ENTER(WP_LOG_COMP_KE, "wp_ecx_import");
if ((!wolfssl_prov_is_running()) || (ecx == NULL)) {
ok = 0;
}
if (ok && ((selection & WP_ECX_POSSIBLE_SELECTIONS) == 0)) {
ok = 0;
}
if (ok && ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)) {
if (!wp_params_get_octet_string_ptr(params, OSSL_PKEY_PARAM_PRIV_KEY,
&privData, &len)) {
ok = 0;
}
if (ok && (privData != NULL)) {
ecx->unclamped[0] = privData[0];
ecx->unclamped[1] = privData[len - 1];
rc = (*ecx->data->importPriv)(privData, (word32)len, (void*)&ecx->key,
ECX_LITTLE_ENDIAN);
if (rc != 0) {
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "importPriv", rc);
ok = 0;
}
if (ok) {
ecx->hasPriv = 1;
ecx->hasPub = 1;
ecx->clamped = 1;
}
}
}
if (ok && ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)) {
if (!wp_params_get_octet_string_ptr(params, OSSL_PKEY_PARAM_PUB_KEY,
&pubData, &len)) {
ok = 0;
}
if (ok && (pubData != NULL)) {
rc = (*ecx->data->importPub)(pubData, (word32)len, (void*)&ecx->key,
ECX_LITTLE_ENDIAN);
if (rc != 0) {
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "importPub", rc);
ok = 0;
}
if (ok) {
ecx->hasPub = 1;
}
}
}
if (ok && (privData == NULL) && (pubData == NULL)) {
ok = 0;
}
WOLFPROV_LEAVE(WP_LOG_COMP_KE, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/** ECX private key parameters. */
#define WP_ECX_PRIVATE_KEY_PARAMS \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
/** ECX public key parameters. */
#define WP_ECX_PUBLIC_KEY_PARAMS \
OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0)
/**
* Table of key parameters for difference selections.
*/
static const OSSL_PARAM wp_ecx_key_params[] = {
/* 0 */
OSSL_PARAM_END,
/* 1 */