69
69
SE05X_EC_SIGNATURE_RAW_LENGTH
70
70
71
71
#define SE05X_SHA256_LENGTH 32
72
- #define SE05X_SN_LENGTH 18
73
72
74
73
#define SE05X_TEMP_OBJECT 9999
75
74
@@ -108,22 +107,35 @@ void SE05XClass::end()
108
107
Se05x_API_SessionClose (&_se05x_session);
109
108
}
110
109
110
+ int SE05XClass::serialNumber (byte sn[])
111
+ {
112
+ return serialNumber (sn, SE05X_SN_LENGTH);
113
+ }
114
+
115
+ int SE05XClass::serialNumber (byte sn[], size_t length)
116
+ {
117
+ size_t uidLen = length;
118
+ const int kSE05x_AppletResID_UNIQUE_ID = 0x7FFF0206 ;
119
+ smStatus_t status;
120
+
121
+ status = Se05x_API_ReadObject (&_se05x_session, kSE05x_AppletResID_UNIQUE_ID , 0 , length, sn, &uidLen);
122
+ if (status != SM_OK || length != uidLen) {
123
+ SMLOG_E (" Error in Se05x_API_ReadObject \n " );
124
+ return 0 ;
125
+ }
126
+ return 1 ;
127
+ }
128
+
111
129
String SE05XClass::serialNumber ()
112
130
{
113
131
String result = (char *)NULL ;
114
132
byte UID[SE05X_SN_LENGTH];
115
- size_t uidLen = SE05X_SN_LENGTH;
116
- const int kSE05x_AppletResID_UNIQUE_ID = 0x7FFF0206 ,
117
133
118
- status = Se05x_API_ReadObject (&_se05x_session, kSE05x_AppletResID_UNIQUE_ID , 0 , uidLen, UID, &uidLen);
119
- if (status != SM_OK) {
120
- SMLOG_E (" Error in Se05x_API_ReadObject \n " );
121
- return " " ;
122
- }
134
+ serialNumber (UID, sizeof (UID));
123
135
124
- result.reserve (uidLen * 2 );
136
+ result.reserve (SE05X_SN_LENGTH * 2 );
125
137
126
- for (size_t i = 0 ; i < uidLen ; i++) {
138
+ for (size_t i = 0 ; i < SE05X_SN_LENGTH ; i++) {
127
139
byte b = UID[i];
128
140
129
141
if (b < 16 ) {
@@ -168,11 +180,11 @@ int SE05XClass::random(byte data[], size_t length)
168
180
smStatus_t status;
169
181
uint16_t offset = 0 ;
170
182
uint16_t left = length;
171
-
183
+
172
184
while (left > 0 ) {
173
185
uint16_t chunk = (left > SE05X_MAX_CHUNK_SIZE) ? SE05X_MAX_CHUNK_SIZE : left;
174
186
size_t max_buffer = chunk;
175
-
187
+
176
188
status = Se05x_API_GetRandom (&_se05x_session, chunk, (data + offset), &max_buffer);
177
189
if (status != SM_OK) {
178
190
SMLOG_E (" Error in Se05x_API_GetRandom \n " );
@@ -337,7 +349,7 @@ int SE05XClass::beginSHA256()
337
349
{
338
350
smStatus_t status;
339
351
SE05x_CryptoModeSubType_t subtype;
340
-
352
+
341
353
subtype.digest = kSE05x_DigestMode_SHA256 ;
342
354
343
355
status = Se05x_API_CreateCryptoObject (&_se05x_session, kSE05x_CryptoObject_DIGEST_SHA256 , kSE05x_CryptoContext_DIGEST , subtype);
@@ -357,7 +369,7 @@ int SE05XClass::beginSHA256()
357
369
int SE05XClass::updateSHA256 (const byte in[], size_t inLen)
358
370
{
359
371
smStatus_t status;
360
-
372
+
361
373
status = Se05x_API_DigestUpdate (&_se05x_session, kSE05x_CryptoObject_DIGEST_SHA256 , in, inLen);
362
374
if (status != SM_OK) {
363
375
SMLOG_E (" Error in Se05x_API_DigestUpdate \n " );
@@ -374,7 +386,7 @@ int SE05XClass::endSHA256(byte out[], size_t* outLen)
374
386
if (*outLen < SE05X_SHA256_LENGTH) {
375
387
SMLOG_E (" Error in endSHA256 \n " );
376
388
*outLen = 0 ;
377
- return 0 ;
389
+ return 0 ;
378
390
}
379
391
380
392
status = Se05x_API_DigestFinal (&_se05x_session, kSE05x_CryptoObject_DIGEST_SHA256 , NULL , 0 , out, outLen);
@@ -530,7 +542,7 @@ int SE05XClass::ecdsaVerify(const byte message[], const byte signature[], const
530
542
}
531
543
532
544
if (!deleteBinaryObject (SE05X_TEMP_OBJECT)) {
533
- SMLOG_E (" ecdsaVerify failure deleting temporary object\n " );
545
+ SMLOG_E (" ecdsaVerify failure deleting temporary object\n " );
534
546
return 0 ;
535
547
}
536
548
@@ -574,7 +586,7 @@ int SE05XClass::readBinaryObject(int objectId, byte data[], size_t dataMaxLen, s
574
586
while (left > 0 ) {
575
587
uint16_t chunk = (left > SE05X_MAX_CHUNK_SIZE) ? SE05X_MAX_CHUNK_SIZE : left;
576
588
size_t max_buffer = chunk;
577
-
589
+
578
590
status = Se05x_API_ReadObject (&_se05x_session, objectId, offset, chunk, (data + offset), &max_buffer);
579
591
if (status != SM_OK) {
580
592
SMLOG_E (" Error in Se05x_API_ReadObject \n " );
@@ -621,8 +633,6 @@ int SE05XClass::writeAESKey(int objectId, const byte data[], size_t length)
621
633
{
622
634
smStatus_t status;
623
635
SE05x_Result_t result;
624
- uint16_t offset = 0 ;
625
- uint16_t size;
626
636
627
637
status = Se05x_API_CheckObjectExists (&_se05x_session, objectId, &result);
628
638
if (status != SM_OK) {
@@ -635,9 +645,7 @@ int SE05XClass::writeAESKey(int objectId, const byte data[], size_t length)
635
645
return 0 ;
636
646
}
637
647
638
- uint16_t left = length;
639
-
640
- status = Se05x_API_WriteSymmKey (&_se05x_session, NULL , 3 , objectId, NULL , data, length, kSE05x_INS_NA , kSE05x_SymmKeyType_AES );
648
+ status = Se05x_API_WriteSymmKey (&_se05x_session, NULL , 3 , objectId, SE05x_KeyID_KEK_NONE, data, length, kSE05x_INS_NA , kSE05x_SymmKeyType_AES );
641
649
642
650
if (status != SM_OK) {
643
651
SMLOG_E (" Error in Se05x_API_WriteSymmKey \n " );
@@ -650,9 +658,6 @@ int SE05XClass::writeHMACKey(int objectId, const byte data[], size_t length)
650
658
{
651
659
smStatus_t status;
652
660
SE05x_Result_t result;
653
- uint8_t exists = 0 ;
654
- uint16_t offset = 0 ;
655
- uint16_t size;
656
661
657
662
status = Se05x_API_CheckObjectExists (&_se05x_session, objectId, &result);
658
663
if (status != SM_OK) {
@@ -662,7 +667,6 @@ int SE05XClass::writeHMACKey(int objectId, const byte data[], size_t length)
662
667
663
668
if (result == kSE05x_Result_SUCCESS ) {
664
669
SMLOG_E (" Object exists \n " );
665
- exists = 1 ;
666
670
}
667
671
668
672
status = Se05x_API_WriteSymmKey (&_se05x_session, NULL , 0 , objectId, SE05x_KeyID_KEK_NONE, data, length, kSE05x_INS_NA , kSE05x_SymmKeyType_HMAC );
@@ -784,9 +788,9 @@ int SE05XClass::getECKeyXyValuesFromDER(byte* derKey, size_t derLen, byte* rawKe
784
788
if (*rawLen < SE05X_EC_KEY_RAW_LENGTH) {
785
789
SMLOG_E (" Error in getECKeyXyValuesFromDER \n " );
786
790
*rawLen = 0 ;
787
- return 0 ;
791
+ return 0 ;
788
792
}
789
-
793
+
790
794
/* XY values are stored in the last 64 bytes of DER buffer */
791
795
*rawLen = SE05X_EC_KEY_RAW_LENGTH;
792
796
memcpy (rawKey, &derKey[derLen - SE05X_EC_KEY_RAW_LENGTH], SE05X_EC_KEY_RAW_LENGTH);
@@ -799,15 +803,15 @@ int SE05XClass::setECKeyXyVauesInDER(const byte* rawKey, size_t rawLen, byte* de
799
803
if (rawLen != SE05X_EC_KEY_RAW_LENGTH) {
800
804
SMLOG_E (" Error in setECKeyXyVauesInDER invalid raw key\n " );
801
805
*derLen = 0 ;
802
- return 0 ;
806
+ return 0 ;
803
807
}
804
808
805
809
if (*derLen < SE05X_EC_KEY_DER_LENGTH) {
806
810
SMLOG_E (" Error in setECKeyXyVauesInDER buffer too small\n " );
807
811
*derLen = 0 ;
808
- return 0 ;
812
+ return 0 ;
809
813
}
810
-
814
+
811
815
/* Copy header byte from 0 to 25 */
812
816
memcpy (&derKey[0 ], &ecc_der_header_nist256[0 ], SE05X_EC_KEY_DER_HEADER_LENGTH);
813
817
/* Add format byte */
@@ -827,13 +831,13 @@ int SE05XClass::getECSignatureRsValuesFromDER(byte* derSignature, size_t derLen,
827
831
if ((derLen < SE05X_EC_SIGNATURE_MIN_DER_LENGTH) || (derLen > SE05X_EC_SIGNATURE_MAX_DER_LENGTH)) {
828
832
SMLOG_E (" Error in getECSignatureRsValuesFromDER invalid signature\n " );
829
833
*rawLen = 0 ;
830
- return 0 ;
834
+ return 0 ;
831
835
}
832
836
833
837
if (*rawLen < SE05X_EC_SIGNATURE_RAW_LENGTH) {
834
838
SMLOG_E (" Error in getECSignatureRsValuesFromDER buffer too small\n " );
835
839
*rawLen = 0 ;
836
- return 0 ;
840
+ return 0 ;
837
841
}
838
842
839
843
rLen = derSignature[3 ];
@@ -868,7 +872,7 @@ int SE05XClass::setECSignatureRsValuesInDER(const byte* rawSignature, size_t raw
868
872
{
869
873
/* *
870
874
* Always consider worst case with padding
871
- *
875
+ *
872
876
* | 0x30 0x46 0x02 0x21 0x00 | R values 32 bytes | 0x02 0x21 0x00 | S values 32 bytes |
873
877
*
874
878
*/
0 commit comments