-
Notifications
You must be signed in to change notification settings - Fork 970
Expand file tree
/
Copy pathstsafe.c
More file actions
1921 lines (1688 loc) · 62 KB
/
stsafe.c
File metadata and controls
1921 lines (1688 loc) · 62 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
/* stsafe.c
*
* Copyright (C) 2006-2026 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL 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.
*
* wolfSSL 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <wolfssl/wolfcrypt/types.h>
#include <wolfssl/wolfcrypt/port/st/stsafe.h>
#include <wolfssl/wolfcrypt/logging.h>
#ifndef NO_ASN
#include <wolfssl/wolfcrypt/asn.h>
#endif
#ifndef STSAFE_INTERFACE_PRINTF
#define STSAFE_INTERFACE_PRINTF(...) WC_DO_NOTHING
#endif
/* Combined STSAFE macro - set in stsafe.h when either A100/A120 is defined */
#ifdef WOLFSSL_STSAFE
/* ========================================================================== */
/* Internal Implementation (when NOT using external stsafe_interface.h) */
/* ========================================================================== */
/* When WOLFSSL_STSAFE_INTERFACE_EXTERNAL is defined, all internal
* implementation is skipped and the customer provides their own
* stsafe_interface.h with custom implementations. This maintains
* backwards compatibility with older integration approaches. */
#ifndef WOLFSSL_STSAFE_INTERFACE_EXTERNAL
/* ========================================================================== */
/* SDK-Specific Includes */
/* ========================================================================== */
#ifdef WOLFSSL_STSAFEA120
/* STSELib includes for A120 */
#include "stse_platform_generic.h"
#include "stselib.h"
#else /* WOLFSSL_STSAFEA100 */
/* Legacy STSAFE-A1xx SDK includes */
#include <stsafe_a_types.h>
#include <stsafe_a_configuration.h>
#include <stsafe_a_basic.h>
#include <stsafe_a_tools.h>
#include <stsafe_a_administrative.h>
#include <stsafe_a_general_purpose.h>
#include <stsafe_a_private_public_key.h>
#include <stsafe_a_data_partition.h>
#endif
/* ========================================================================== */
/* Global State */
/* ========================================================================== */
#ifdef WOLFSSL_STSAFEA120
/* STSELib handler */
static stse_Handler_t g_stse_handler;
static int g_stse_initialized = 0;
#else /* WOLFSSL_STSAFEA100 */
/* Legacy SDK handle */
static void* g_stsafe_handle = NULL;
/* Host MAC and Cipher Keys for secure communication */
/* NOTE: These are example keys
* - real implementations should store securely */
#ifndef STSAFE_HOST_KEY_MAC
static const uint8_t g_host_mac_key[16] = {
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
};
#endif
#ifndef STSAFE_HOST_KEY_CIPHER
static const uint8_t g_host_cipher_key[16] = {
0x11, 0x11, 0x22, 0x22, 0x33, 0x33, 0x44, 0x44,
0x55, 0x55, 0x66, 0x66, 0x77, 0x77, 0x88, 0x88
};
#endif
#endif
/* Current curve mode for signing operations */
static stsafe_curve_id_t g_stsafe_curve_mode = STSAFE_DEFAULT_CURVE;
/* ========================================================================== */
/* Internal Helper Functions */
/* ========================================================================== */
/**
* \brief Helper macros to store/retrieve slot number in devCtx
* \details Slot number is stored directly in devCtx as void* to avoid
* dynamic memory allocation. Slot values are small (0, 1, 0xFF)
* so safe to cast to/from void*.
*/
#define STSAFE_SLOT_TO_DEVCXT(slot) ((void*)(uintptr_t)(slot))
#define STSAFE_DEVCXT_TO_SLOT(devCtx) ((stsafe_slot_t)(uintptr_t)(devCtx))
/**
* \brief Get key size in bytes for a given curve
*/
static int stsafe_get_key_size(stsafe_curve_id_t curve_id)
{
switch (curve_id) {
case STSAFE_ECC_CURVE_P256:
#if defined(HAVE_ECC_BRAINPOOL) && defined(STSE_CONF_ECC_BRAINPOOL_P_256)
case STSAFE_ECC_CURVE_BP256:
#endif
return 32;
case STSAFE_ECC_CURVE_P384:
#if defined(HAVE_ECC_BRAINPOOL) && defined(STSE_CONF_ECC_BRAINPOOL_P_384)
case STSAFE_ECC_CURVE_BP384:
#endif
return 48;
default:
break;
}
return 0;
}
/**
* \brief Convert wolfSSL ECC curve ID to STSAFE curve ID
*/
static stsafe_curve_id_t stsafe_get_ecc_curve_id(int ecc_curve)
{
switch (ecc_curve) {
case ECC_SECP256R1:
return STSAFE_ECC_CURVE_P256;
case ECC_SECP384R1:
return STSAFE_ECC_CURVE_P384;
#if defined(HAVE_ECC_BRAINPOOL) && defined(STSE_CONF_ECC_BRAINPOOL_P_256)
case ECC_BRAINPOOLP256R1:
return STSAFE_ECC_CURVE_BP256;
#endif
#if defined(HAVE_ECC_BRAINPOOL) && defined(STSE_CONF_ECC_BRAINPOOL_P_384)
case ECC_BRAINPOOLP384R1:
return STSAFE_ECC_CURVE_BP384;
#endif
default:
break;
}
return STSAFE_DEFAULT_CURVE;
}
/**
* \brief Convert STSAFE curve ID to wolfSSL ECC curve ID
*/
#if !defined(WOLFCRYPT_ONLY) && defined(HAVE_PK_CALLBACKS)
static int stsafe_get_ecc_curve(stsafe_curve_id_t curve_id)
{
switch (curve_id) {
case STSAFE_ECC_CURVE_P256:
return ECC_SECP256R1;
case STSAFE_ECC_CURVE_P384:
return ECC_SECP384R1;
#if defined(HAVE_ECC_BRAINPOOL) && defined(STSE_CONF_ECC_BRAINPOOL_P_256)
case STSAFE_ECC_CURVE_BP256:
return ECC_BRAINPOOLP256R1;
#endif
#if defined(HAVE_ECC_BRAINPOOL) && defined(STSE_CONF_ECC_BRAINPOOL_P_384)
case STSAFE_ECC_CURVE_BP384:
return ECC_BRAINPOOLP384R1;
#endif
default:
break;
}
return ECC_SECP256R1;
}
#endif
/**
* \brief Get current curve mode for signing
*/
static stsafe_curve_id_t stsafe_get_curve_mode(void)
{
return g_stsafe_curve_mode;
}
/**
* \brief Set current curve mode for signing
*/
static int stsafe_set_curve_mode(stsafe_curve_id_t curve_id)
{
g_stsafe_curve_mode = curve_id;
return 0;
}
/* Unused function workaround for some compilers */
#ifdef __GNUC__
__attribute__((unused))
#endif
static void stsafe_unused_funcs(void)
{
#if !defined(WOLFCRYPT_ONLY) && defined(HAVE_PK_CALLBACKS)
(void)stsafe_get_ecc_curve;
#endif
(void)stsafe_set_curve_mode;
}
/* ========================================================================== */
/* Internal Interface Functions - SDK Specific Implementations */
/* ========================================================================== */
#ifdef WOLFSSL_STSAFEA120
/* -------------------------------------------------------------------------- */
/* STSELib (A120) Implementation */
/* -------------------------------------------------------------------------- */
/**
* \brief Initialize STSAFE-A120 device using STSELib
*/
int stsafe_interface_init(void)
{
int rc = 0;
stse_ReturnCode_t ret;
if (g_stse_initialized) {
return 0; /* Already initialized */
}
/* Set default handler values */
ret = stse_set_default_handler_value(&g_stse_handler);
if (ret != STSE_OK) {
STSAFE_INTERFACE_PRINTF("stse_set_default_handler_value error: %d\n",
ret);
rc = -1;
}
if (rc == 0) {
/* Configure for STSAFE-A120 on I2C bus 1 */
g_stse_handler.device_type = STSAFE_A120;
#ifdef STSAFE_I2C_BUS
g_stse_handler.io.busID = STSAFE_I2C_BUS;
#else
g_stse_handler.io.busID = 1;
#endif
g_stse_handler.io.BusSpeed = 400; /* 400 kHz */
/* Initialize STSELib - this sets up I2C communication */
ret = stse_init(&g_stse_handler);
if (ret != STSE_OK) {
STSAFE_INTERFACE_PRINTF("stse_init error: %d\n", ret);
rc = -1;
}
}
if (rc == 0) {
g_stse_initialized = 1;
#ifdef USE_STSAFE_VERBOSE
WOLFSSL_MSG("STSAFE-A120 (STSELib) initialized");
#endif
}
return rc;
}
/**
* \brief Generate ECC key pair on STSAFE-A120
* \details Uses dedicated key slot for persistent keys (typically slot 0 or 1).
* For ephemeral ECDHE keys, use stsafe_create_ecdhe_key() instead.
*
* Note: For ECDH operations on persistent slots, the key must be generated
* with appropriate usage settings. Per ST FAE: slot 0xFF with usage_limit=1
* is recommended for ephemeral ECDH (key establishment mode).
*
* \return STSAFE_A_OK on success.
* \return Other value on failure.
*/
static int stsafe_create_key(stsafe_slot_t slot, stsafe_curve_id_t curve_id,
uint8_t* pPubKeyRaw)
{
int rc = STSAFE_A_OK;
stse_ReturnCode_t ret;
if (pPubKeyRaw == NULL) {
return BAD_FUNC_ARG;
}
/* Generate key pair - public key is X||Y concatenated
* Note: stse_generate_ecc_key_pair expects stse_ecc_key_type_t,
* but stsafe_curve_id_t values match stse_ecc_key_type_t enum values.
*
* For persistent keys: usage_limit=255 allows multiple operations (signing)
* For ephemeral keys (slot 0xFF): usage_limit=1 for key establishment mode
*/
ret = stse_generate_ecc_key_pair(&g_stse_handler, slot,
(stse_ecc_key_type_t)curve_id,
STSAFE_PERSISTENT_KEY_USAGE_LIMIT,
pPubKeyRaw);
if (ret != STSE_OK) {
STSAFE_INTERFACE_PRINTF("stse_generate_ecc_key_pair error: %d\n", ret);
rc = (int)ret;
}
return rc;
}
/**
* \brief Generate ECDHE ephemeral key pair on STSAFE-A120
* \details Uses stse_generate_ecc_key_pair() with slot 0xFF (ephemeral slot)
* and usage_limit=1 for key establishment mode.
* Per ST FAE recommendation: slot 0xFF must be used with mode of
* operation = key establishment and usage limit = 1 for ECDH operations.
* Public key is returned in X||Y format.
*
* \return STSAFE_A_OK on success.
* \return Other value on failure.
*/
static int stsafe_create_ecdhe_key(stsafe_curve_id_t curve_id,
uint8_t* pPubKeyRaw)
{
int rc = STSAFE_A_OK;
stse_ReturnCode_t ret;
if (pPubKeyRaw == NULL) {
return BAD_FUNC_ARG;
}
/* Generate ephemeral key pair in slot 0xFF with usage_limit=1
* This configures the key for key establishment mode */
ret = stse_generate_ecc_key_pair(&g_stse_handler,
STSAFE_KEY_SLOT_EPHEMERAL, /* slot 0xFF */
(stse_ecc_key_type_t)curve_id,
STSAFE_EPHEMERAL_KEY_USAGE_LIMIT, /* usage_limit = 1 */
pPubKeyRaw);
if (ret != STSE_OK) {
STSAFE_INTERFACE_PRINTF("stse_generate_ecc_key_pair (ephemeral) error: %d\n", ret);
rc = (int)ret;
}
return rc;
}
/**
* \brief ECDSA sign using STSAFE-A120
*
* \return STSAFE_A_OK on success.
* \return Other value on failure.
*/
static int stsafe_sign(stsafe_slot_t slot, stsafe_curve_id_t curve_id,
uint8_t* pHash, uint8_t* pSigRS)
{
int rc = STSAFE_A_OK;
stse_ReturnCode_t ret;
int key_sz = stsafe_get_key_size(curve_id);
if (pHash == NULL || pSigRS == NULL) {
return BAD_FUNC_ARG;
}
/* Sign hash - output is R || S concatenated */
ret = stse_ecc_generate_signature(&g_stse_handler, slot, curve_id,
pHash, (uint16_t)key_sz, pSigRS);
if (ret != STSE_OK) {
STSAFE_INTERFACE_PRINTF("stse_ecc_generate_signature error: %d\n", ret);
rc = (int)ret;
}
return rc;
}
/**
* \brief ECDSA verify using STSAFE-A120
*
* \return STSAFE_A_OK on success.
* \return Other value on failure.
*/
static int stsafe_verify(stsafe_curve_id_t curve_id, uint8_t* pHash,
uint8_t* pSigRS, uint8_t* pPubKeyX, uint8_t* pPubKeyY,
int32_t* pResult)
{
int rc = STSAFE_A_OK;
stse_ReturnCode_t ret;
int key_sz = stsafe_get_key_size(curve_id);
uint8_t pubKey[STSAFE_MAX_PUBKEY_RAW_LEN];
uint8_t validity = 0;
if (pHash == NULL || pSigRS == NULL || pPubKeyX == NULL ||
pPubKeyY == NULL || pResult == NULL) {
return BAD_FUNC_ARG;
}
/* Combine X and Y into single buffer (X||Y) */
XMEMCPY(pubKey, pPubKeyX, key_sz);
XMEMCPY(pubKey + key_sz, pPubKeyY, key_sz);
/* Verify signature - pMessage is the hash, pSignature is R||S */
ret = stse_ecc_verify_signature(&g_stse_handler, curve_id,
pubKey, /* public key X||Y */
pSigRS, /* signature R||S */
pHash, /* message (hash) */
(uint16_t)key_sz, /* message length */
0, /* eddsa_variant (0 for non-EdDSA) */
&validity);
if (ret != STSE_OK) {
STSAFE_INTERFACE_PRINTF("stse_ecc_verify_signature error: %d\n", ret);
*pResult = 0;
rc = (int)ret;
}
if (rc == STSAFE_A_OK) {
*pResult = (validity != 0) ? 1 : 0;
}
return rc;
}
/**
* \brief ECDH shared secret using STSAFE-A120
*
* \return STSAFE_A_OK on success.
* \return Other value on failure.
*/
static int stsafe_shared_secret(stsafe_slot_t slot, stsafe_curve_id_t curve_id,
uint8_t* pPubKeyX, uint8_t* pPubKeyY,
uint8_t* pSharedSecret,
int32_t* pSharedSecretLen)
{
int rc = STSAFE_A_OK;
stse_ReturnCode_t ret;
int key_sz = stsafe_get_key_size(curve_id);
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
uint8_t* peerPubKey = NULL;
#else
uint8_t peerPubKey[STSAFE_MAX_PUBKEY_RAW_LEN];
#endif
if (pPubKeyX == NULL || pPubKeyY == NULL || pSharedSecret == NULL ||
pSharedSecretLen == NULL) {
return BAD_FUNC_ARG;
}
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
peerPubKey = (uint8_t*)XMALLOC(STSAFE_MAX_PUBKEY_RAW_LEN, NULL,
DYNAMIC_TYPE_TMP_BUFFER);
if (peerPubKey == NULL) {
return MEMORY_E;
}
#endif
/* Combine peer X and Y (X||Y format) */
XMEMCPY(peerPubKey, pPubKeyX, key_sz);
XMEMCPY(peerPubKey + key_sz, pPubKeyY, key_sz);
/* Compute shared secret
* Note: stse_ecc_establish_shared_secret expects stse_ecc_key_type_t.
* For STSAFE-A120, stsafe_curve_id_t values match stse_ecc_key_type_t enum values:
* STSAFE_ECC_CURVE_P256 (0) = STSE_ECC_KT_NIST_P_256 (0)
* STSAFE_ECC_CURVE_P384 (1) = STSE_ECC_KT_NIST_P_384 (1) */
ret = stse_ecc_establish_shared_secret(&g_stse_handler, slot,
(stse_ecc_key_type_t)curve_id, peerPubKey, pSharedSecret);
if (ret != STSE_OK) {
STSAFE_INTERFACE_PRINTF("stse_ecc_establish_shared_secret error: %d (slot: %d, curve_id: %d)\n",
ret, slot, curve_id);
rc = (int)ret;
}
if (rc == STSAFE_A_OK) {
*pSharedSecretLen = (int32_t)key_sz;
}
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
XFREE(peerPubKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return rc;
}
/**
* \brief Read device certificate from STSAFE-A120
*
* \return STSAFE_A_OK on success.
* \return Other value on failure.
*/
static int stsafe_read_certificate(uint8_t** ppCert, uint32_t* pCertLen)
{
#ifdef WOLFSSL_NO_MALLOC
/* Certificate reading requires dynamic allocation */
(void)ppCert;
(void)pCertLen;
return NOT_COMPILED_IN;
#else
int rc = STSAFE_A_OK;
stse_ReturnCode_t ret;
uint16_t certLen = 0;
uint8_t certZone = 0; /* Certificate zone 0 */
/* First, get certificate size */
ret = stse_get_device_certificate_size(&g_stse_handler, certZone, &certLen);
if (ret != STSE_OK) {
STSAFE_INTERFACE_PRINTF("stse_get_device_certificate_size error: %d\n",
ret);
rc = (int)ret;
}
else if (certLen == 0) {
/* Certificate size is 0 - invalid certificate data */
STSAFE_INTERFACE_PRINTF("stse_get_device_certificate_size returned zero length\n");
rc = ASN_PARSE_E;
}
/* Allocate buffer */
if (rc == STSAFE_A_OK) {
*ppCert = (uint8_t*)XMALLOC(certLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (*ppCert == NULL) {
rc = MEMORY_E;
}
}
/* Read certificate */
if (rc == STSAFE_A_OK) {
ret = stse_get_device_certificate(&g_stse_handler, certZone, certLen,
*ppCert);
if (ret != STSE_OK) {
XFREE(*ppCert, NULL, DYNAMIC_TYPE_TMP_BUFFER);
*ppCert = NULL;
STSAFE_INTERFACE_PRINTF("stse_get_device_certificate error: %d\n",
ret);
rc = (int)ret;
}
}
if (rc == STSAFE_A_OK) {
*pCertLen = certLen;
}
return rc;
#endif /* WOLFSSL_NO_MALLOC */
}
#if !defined(WC_NO_RNG) && defined(USE_STSAFE_RNG_SEED)
/**
* \brief Get random bytes from STSAFE-A120
*/
static int stsafe_get_random(uint8_t* pRandom, uint32_t size)
{
int rc;
stse_ReturnCode_t ret;
uint16_t len = (size > 0xFFFF) ? 0xFFFF : (uint16_t)size;
ret = stse_generate_random(&g_stse_handler, pRandom, len);
if (ret != STSE_OK) {
rc = -1;
}
else {
rc = (int)len;
}
return rc;
}
#endif
#else /* WOLFSSL_STSAFEA100 */
/* -------------------------------------------------------------------------- */
/* Legacy STSAFE-A1xx SDK (A100/A110) Implementation */
/* -------------------------------------------------------------------------- */
/**
* \brief Set host keys for secure communication
*/
static void stsafe_set_host_keys(void* handle)
{
StSafeA_SetHostMacKey(handle, g_host_mac_key);
StSafeA_SetHostCipherKey(handle, g_host_cipher_key);
}
/**
* \brief Check and initialize host keys
*/
static int stsafe_check_host_keys(void* handle)
{
uint8_t status_code;
StSafeA_HostKeySlotBuffer* pHostKeySlot;
status_code = StSafeA_HostKeySlotQuery(handle, &pHostKeySlot,
STSAFE_A_NO_MAC);
if (status_code == STSAFE_A_OK && !pHostKeySlot->HostKeyPresenceFlag) {
/* Host keys not set, initialize them */
uint8_t hostKeys[32];
XMEMCPY(hostKeys, g_host_mac_key, 16);
XMEMCPY(hostKeys + 16, g_host_cipher_key, 16);
status_code = StSafeA_PutAttribute(handle, STSAFE_A_HOST_KEY_SLOT_TAG,
hostKeys, sizeof(hostKeys), STSAFE_A_NO_MAC);
}
return status_code;
}
/**
* \brief Initialize STSAFE-A100/A110 device
*
* \return STSAFE_A_OK on success.
* \return Other value on failure.
*/
int stsafe_interface_init(void)
{
int rc = 0;
uint8_t status_code;
const uint8_t echo_data[3] = {0x01, 0x02, 0x03};
StSafeA_EchoBuffer* echo_resp = NULL;
if (g_stsafe_handle != NULL) {
return 0; /* Already initialized */
}
/* Create handle */
status_code = StSafeA_CreateHandle(&g_stsafe_handle, STSAFE_I2C_ADDR);
if (status_code != STSAFE_A_OK) {
STSAFE_INTERFACE_PRINTF("StSafeA_CreateHandle error: %d\n",
status_code);
rc = -1;
}
/* Echo test to verify communication */
if (rc == 0) {
status_code = StSafeA_Echo(g_stsafe_handle, (uint8_t*)echo_data, 3,
&echo_resp, STSAFE_A_NO_MAC);
if (status_code != STSAFE_A_OK ||
XMEMCMP(echo_data, echo_resp->Data, 3) != 0) {
STSAFE_INTERFACE_PRINTF("StSafeA_Echo error: %d\n", status_code);
rc = -1;
}
XFREE(echo_resp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
}
/* Check/initialize host keys */
if (rc == 0) {
status_code = stsafe_check_host_keys(g_stsafe_handle);
if (status_code != STSAFE_A_OK) {
STSAFE_INTERFACE_PRINTF("stsafe_check_host_keys error: %d\n",
status_code);
rc = -1;
}
}
#ifdef USE_STSAFE_VERBOSE
if (rc == 0) {
WOLFSSL_MSG("STSAFE-A100/A110 initialized");
}
#endif
return rc;
}
/**
* \brief Generate ECC key pair on STSAFE-A100/A110
*
* \return STSAFE_A_OK on success.
* \return Other value on failure.
*/
static int stsafe_create_key(stsafe_slot_t* pSlot, stsafe_curve_id_t curve_id,
uint8_t* pPubKeyRaw)
{
int rc;
uint8_t status_code;
int key_sz = stsafe_get_key_size(curve_id);
stsafe_slot_t slot = STSAFE_KEY_SLOT_1;
StSafeA_CoordinateBuffer* pubX = NULL;
StSafeA_CoordinateBuffer* pubY = NULL;
uint8_t* pointRepId = NULL;
stsafe_set_host_keys(g_stsafe_handle);
status_code = StSafeA_GenerateKeyPair(g_stsafe_handle, slot, 0xFFFF, 1,
(StSafeA_KeyUsageAuthorizationFlags)(
STSAFE_A_COMMAND_RESPONSE_SIGNATURE |
STSAFE_A_MESSAGE_DIGEST_SIGNATURE |
STSAFE_A_KEY_ESTABLISHMENT),
curve_id, &pointRepId, &pubX, &pubY, STSAFE_A_HOST_C_MAC);
if (status_code == STSAFE_A_OK && pointRepId != NULL &&
*pointRepId == STSAFE_A_POINT_REPRESENTATION_ID &&
pubX != NULL && pubY != NULL) {
XMEMCPY(pPubKeyRaw, pubX->Data, pubX->Length);
XMEMCPY(pPubKeyRaw + key_sz, pubY->Data, pubY->Length);
rc = STSAFE_A_OK;
}
else {
rc = (int)(uint8_t)-1;
}
/* Free SDK-allocated buffers */
XFREE(pubX, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(pubY, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (rc == STSAFE_A_OK && pSlot != NULL) {
*pSlot = slot;
}
return rc;
}
/**
* \brief ECDSA sign using STSAFE-A100/A110
*
* \return STSAFE_A_OK on success.
* \return Other value on failure.
*/
static int stsafe_sign(stsafe_slot_t slot, stsafe_curve_id_t curve_id,
uint8_t* pHash, uint8_t* pSigRS)
{
int rc;
uint8_t status_code;
int key_sz = stsafe_get_key_size(curve_id);
StSafeA_SignatureBuffer* signature = NULL;
StSafeA_HashTypes hashType;
size_t r_length, s_length;
hashType = (curve_id == STSAFE_ECC_CURVE_P384 ||
curve_id == STSAFE_ECC_CURVE_BP384) ?
STSAFE_HASH_SHA384 : STSAFE_HASH_SHA256;
status_code = StSafeA_GenerateSignature(g_stsafe_handle, slot, pHash,
hashType, &signature, STSAFE_A_NO_MAC);
if (status_code == STSAFE_A_OK && signature != NULL) {
/* Parse signature - format is: len(2) || R || len(2) || S */
r_length = ((uint16_t)signature->Data[0] << 8) | signature->Data[1];
/* Bounds check: r_length must be valid and fit within signature buffer */
if (r_length > key_sz || r_length == 0 ||
(size_t)(2 + r_length + 2) > signature->Length) {
rc = ASN_PARSE_E;
}
else {
s_length = ((uint16_t)signature->Data[2 + r_length] << 8) |
signature->Data[3 + r_length];
/* Bounds check: s_length must be valid and fit within signature buffer */
if (s_length > key_sz || s_length == 0 ||
(size_t)(4 + r_length + s_length) > signature->Length) {
rc = ASN_PARSE_E;
}
else {
/* Copy R and S to output (zero-padded) */
XMEMSET(pSigRS, 0, key_sz * 2);
XMEMCPY(pSigRS + (key_sz - r_length), &signature->Data[2], r_length);
XMEMCPY(pSigRS + key_sz + (key_sz - s_length),
&signature->Data[4 + r_length], s_length);
rc = STSAFE_A_OK;
}
}
}
else {
rc = (int)status_code;
}
/* Free SDK-allocated buffer */
XFREE(signature, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return rc;
}
/**
* \brief ECDSA verify using STSAFE-A100/A110
*
* \return STSAFE_A_OK on success.
* \return Other value on failure.
*/
static int stsafe_verify(stsafe_curve_id_t curve_id, uint8_t* pHash,
uint8_t* pSigRS, uint8_t* pPubKeyX, uint8_t* pPubKeyY,
int32_t* pResult)
{
int rc = (int)(uint8_t)-1;
uint8_t status_code;
int key_sz = stsafe_get_key_size(curve_id);
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
StSafeA_CoordinateBuffer* X = NULL;
StSafeA_CoordinateBuffer* Y = NULL;
StSafeA_SignatureBuffer* R = NULL;
StSafeA_SignatureBuffer* S = NULL;
StSafeA_SignatureBuffer* Hash = NULL;
#else
/* Stack buffers: 2 bytes for Length + STSAFE_MAX_KEY_LEN for Data */
byte R_buf[2 + STSAFE_MAX_KEY_LEN];
byte S_buf[2 + STSAFE_MAX_KEY_LEN];
byte Hash_buf[2 + STSAFE_MAX_KEY_LEN];
byte X_buf[2 + STSAFE_MAX_KEY_LEN];
byte Y_buf[2 + STSAFE_MAX_KEY_LEN];
StSafeA_SignatureBuffer* R = (StSafeA_SignatureBuffer*)R_buf;
StSafeA_SignatureBuffer* S = (StSafeA_SignatureBuffer*)S_buf;
StSafeA_SignatureBuffer* Hash = (StSafeA_SignatureBuffer*)Hash_buf;
StSafeA_CoordinateBuffer* X = (StSafeA_CoordinateBuffer*)X_buf;
StSafeA_CoordinateBuffer* Y = (StSafeA_CoordinateBuffer*)Y_buf;
#endif
StSafeA_VerifySignatureBuffer* Verif = NULL;
*pResult = 0;
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
/* Allocate buffers */
R = (StSafeA_SignatureBuffer*)XMALLOC(key_sz + 2, NULL,
DYNAMIC_TYPE_TMP_BUFFER);
S = (StSafeA_SignatureBuffer*)XMALLOC(key_sz + 2, NULL,
DYNAMIC_TYPE_TMP_BUFFER);
Hash = (StSafeA_SignatureBuffer*)XMALLOC(key_sz + 2, NULL,
DYNAMIC_TYPE_TMP_BUFFER);
X = (StSafeA_CoordinateBuffer*)XMALLOC(key_sz + 2, NULL,
DYNAMIC_TYPE_TMP_BUFFER);
Y = (StSafeA_CoordinateBuffer*)XMALLOC(key_sz + 2, NULL,
DYNAMIC_TYPE_TMP_BUFFER);
if (X == NULL || Y == NULL || R == NULL || S == NULL || Hash == NULL) {
XFREE(R, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(S, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(Hash, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(X, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(Y, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return MEMORY_E;
}
#endif
R->Length = key_sz;
S->Length = key_sz;
Hash->Length = key_sz;
X->Length = key_sz;
Y->Length = key_sz;
XMEMCPY(R->Data, pSigRS, key_sz);
XMEMCPY(S->Data, pSigRS + key_sz, key_sz);
XMEMCPY(Hash->Data, pHash, key_sz);
XMEMCPY(X->Data, pPubKeyX, key_sz);
XMEMCPY(Y->Data, pPubKeyY, key_sz);
status_code = StSafeA_VerifyMessageSignature(g_stsafe_handle,
curve_id, X, Y, R, S, Hash, &Verif, STSAFE_A_NO_MAC);
if (status_code == STSAFE_A_OK && Verif != NULL) {
*pResult = Verif->SignatureValidity ? 1 : 0;
if (Verif->SignatureValidity) {
rc = STSAFE_A_OK;
}
}
#ifndef WOLFSSL_NO_MALLOC
/* Free SDK-allocated buffer */
XFREE(Verif, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
XFREE(R, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(S, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(Hash, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(X, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(Y, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return rc;
}
/**
* \brief ECDH shared secret using STSAFE-A100/A110
*
* \return STSAFE_A_OK on success.
* \return Other value on failure.
*/
static int stsafe_shared_secret(stsafe_slot_t slot, stsafe_curve_id_t curve_id,
uint8_t* pPubKeyX, uint8_t* pPubKeyY,
uint8_t* pSharedSecret,
int32_t* pSharedSecretLen)
{
int rc = (int)(uint8_t)-1;
uint8_t status_code;
int key_sz = stsafe_get_key_size(curve_id);
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
StSafeA_CoordinateBuffer* peerX = NULL;
StSafeA_CoordinateBuffer* peerY = NULL;
#else
/* Stack buffers: 2 bytes for Length + STSAFE_MAX_KEY_LEN for Data */
byte peerX_buf[2 + STSAFE_MAX_KEY_LEN];
byte peerY_buf[2 + STSAFE_MAX_KEY_LEN];
StSafeA_CoordinateBuffer* peerX = (StSafeA_CoordinateBuffer*)peerX_buf;
StSafeA_CoordinateBuffer* peerY = (StSafeA_CoordinateBuffer*)peerY_buf;
#endif
StSafeA_SharedSecretBuffer* sharedSecret = NULL;
stsafe_set_host_keys(g_stsafe_handle);
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
peerX = (StSafeA_CoordinateBuffer*)XMALLOC(key_sz + 2, NULL,
DYNAMIC_TYPE_TMP_BUFFER);
peerY = (StSafeA_CoordinateBuffer*)XMALLOC(key_sz + 2, NULL,
DYNAMIC_TYPE_TMP_BUFFER);
if (peerX == NULL || peerY == NULL) {
XFREE(peerX, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(peerY, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return MEMORY_E;
}
#endif
peerX->Length = key_sz;
peerY->Length = key_sz;
XMEMCPY(peerX->Data, pPubKeyX, key_sz);
XMEMCPY(peerY->Data, pPubKeyY, key_sz);
status_code = StSafeA_EstablishKey(g_stsafe_handle, slot,
peerX, peerY, &sharedSecret, STSAFE_A_HOST_C_MAC);
if (status_code == STSAFE_A_OK && sharedSecret != NULL) {
*pSharedSecretLen = sharedSecret->SharedSecret.Length;
XMEMCPY(pSharedSecret, sharedSecret->SharedSecret.Data,
sharedSecret->SharedSecret.Length);
rc = STSAFE_A_OK;
}
#ifndef WOLFSSL_NO_MALLOC
/* Free SDK-allocated buffer */
XFREE(sharedSecret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
XFREE(peerX, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(peerY, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return rc;
}
/**
* \brief Read device certificate from STSAFE-A100/A110
*
* \return STSAFE_A_OK on success.
* \return Other value on failure.
*/
static int stsafe_read_certificate(uint8_t** ppCert, uint32_t* pCertLen)
{
#ifdef WOLFSSL_NO_MALLOC
/* Certificate reading requires dynamic allocation */
(void)ppCert;
(void)pCertLen;
return NOT_COMPILED_IN;
#else
int rc = STSAFE_A_OK;
uint8_t status_code;
StSafeA_ReadBuffer* readBuf = NULL;
struct stsafe_a* stsafe_a = (struct stsafe_a*)g_stsafe_handle;
uint8_t step;
uint16_t i;
*pCertLen = 0;
/* Read first 4 bytes to determine certificate length */
status_code = StSafeA_Read(g_stsafe_handle, 0, 0, STSAFE_A_ALWAYS,
0, 0, 4, &readBuf, STSAFE_A_NO_MAC);
if (status_code == STSAFE_A_OK && readBuf != NULL && readBuf->Length == 4) {
/* Parse ASN.1 DER certificate header */
/* 0x30 = ASN_SEQUENCE | ASN_CONSTRUCTED (certificate is a SEQUENCE) */
if (readBuf->Data[0] == (ASN_SEQUENCE | ASN_CONSTRUCTED)) {
/* Parse ASN.1 length encoding */
switch (readBuf->Data[1]) {
case (ASN_LONG_LENGTH | 0x01): /* Length encoded in 1 byte */
*pCertLen = readBuf->Data[2] + 3;
break;
case (ASN_LONG_LENGTH | 0x02): /* Length encoded in 2 bytes */
*pCertLen = ((uint16_t)readBuf->Data[2] << 8) +
readBuf->Data[3] + 4;
break;
default:
/* Short form: length < 128, encoded directly */
if (readBuf->Data[1] < ASN_LONG_LENGTH) {
*pCertLen = readBuf->Data[1] + 2;
}
break;
}
/* Check if length parsing succeeded */
if (*pCertLen == 0) {
rc = ASN_PARSE_E;
}
}
else {
/* Invalid ASN.1 header - expected SEQUENCE tag */
rc = ASN_PARSE_E;
}
}
else {
rc = (int)status_code;
}
XFREE(readBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
readBuf = NULL;
if (rc == STSAFE_A_OK && *pCertLen > 0) {