6969                                             SE05X_EC_SIGNATURE_RAW_LENGTH
7070
7171#define  SE05X_SHA256_LENGTH               32 
72- #define  SE05X_SN_LENGTH                   18 
7372
7473#define  SE05X_TEMP_OBJECT                 9999 
7574
@@ -108,22 +107,35 @@ void SE05XClass::end()
108107    Se05x_API_SessionClose (&_se05x_session);
109108}
110109
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+ 
111129String SE05XClass::serialNumber ()
112130{
113131    String result = (char *)NULL ;
114132    byte UID[SE05X_SN_LENGTH];
115-     size_t  uidLen = SE05X_SN_LENGTH;
116-     const  int  kSE05x_AppletResID_UNIQUE_ID  = 0x7FFF0206 ,
117133
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));
123135
124-     result.reserve (uidLen  * 2 );
136+     result.reserve (SE05X_SN_LENGTH  * 2 );
125137
126-     for  (size_t  i = 0 ; i < uidLen ; i++) {
138+     for  (size_t  i = 0 ; i < SE05X_SN_LENGTH ; i++) {
127139        byte b = UID[i];
128140
129141        if  (b < 16 ) {
@@ -168,11 +180,11 @@ int SE05XClass::random(byte data[], size_t length)
168180    smStatus_t status;
169181    uint16_t    offset = 0 ;
170182    uint16_t    left = length;
171-      
183+ 
172184    while  (left > 0 ) {
173185        uint16_t  chunk     = (left > SE05X_MAX_CHUNK_SIZE) ? SE05X_MAX_CHUNK_SIZE : left;
174186        size_t  max_buffer  = chunk;
175-          
187+ 
176188        status = Se05x_API_GetRandom (&_se05x_session, chunk, (data + offset), &max_buffer);
177189        if  (status != SM_OK) {
178190            SMLOG_E (" Error in Se05x_API_GetRandom \n " 
@@ -337,7 +349,7 @@ int SE05XClass::beginSHA256()
337349{
338350    smStatus_t      status;
339351    SE05x_CryptoModeSubType_t subtype;
340-      
352+ 
341353    subtype.digest  = kSE05x_DigestMode_SHA256 ;
342354
343355    status = Se05x_API_CreateCryptoObject (&_se05x_session, kSE05x_CryptoObject_DIGEST_SHA256 , kSE05x_CryptoContext_DIGEST , subtype);
@@ -357,7 +369,7 @@ int SE05XClass::beginSHA256()
357369int  SE05XClass::updateSHA256 (const  byte in[], size_t  inLen)
358370{
359371    smStatus_t      status;
360-      
372+ 
361373    status = Se05x_API_DigestUpdate (&_se05x_session, kSE05x_CryptoObject_DIGEST_SHA256 , in, inLen);
362374    if  (status != SM_OK) {
363375        SMLOG_E (" Error in Se05x_API_DigestUpdate \n " 
@@ -374,7 +386,7 @@ int SE05XClass::endSHA256(byte out[], size_t* outLen)
374386    if  (*outLen < SE05X_SHA256_LENGTH) {
375387        SMLOG_E (" Error in endSHA256 \n " 
376388        *outLen = 0 ;
377-         return  0 ;   
389+         return  0 ;
378390    }
379391
380392    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
530542    }
531543
532544    if  (!deleteBinaryObject (SE05X_TEMP_OBJECT)) {
533-         SMLOG_E (" ecdsaVerify failure deleting temporary object\n "       
545+         SMLOG_E (" ecdsaVerify failure deleting temporary object\n " 
534546        return  0 ;
535547    }
536548
@@ -574,7 +586,7 @@ int SE05XClass::readBinaryObject(int objectId, byte data[], size_t dataMaxLen, s
574586    while  (left > 0 ) {
575587        uint16_t  chunk     = (left > SE05X_MAX_CHUNK_SIZE) ? SE05X_MAX_CHUNK_SIZE : left;
576588        size_t  max_buffer  = chunk;
577-          
589+ 
578590        status = Se05x_API_ReadObject (&_se05x_session, objectId, offset, chunk, (data + offset), &max_buffer);
579591        if  (status != SM_OK) {
580592            SMLOG_E (" Error in Se05x_API_ReadObject \n " 
@@ -621,8 +633,6 @@ int SE05XClass::writeAESKey(int objectId, const byte data[], size_t length)
621633{
622634    smStatus_t      status;
623635    SE05x_Result_t  result;
624-     uint16_t         offset = 0 ;
625-     uint16_t         size;
626636
627637    status = Se05x_API_CheckObjectExists (&_se05x_session, objectId, &result);
628638    if  (status != SM_OK) {
@@ -635,9 +645,7 @@ int SE05XClass::writeAESKey(int objectId, const byte data[], size_t length)
635645        return  0 ;
636646    }
637647
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 );
641649
642650    if  (status != SM_OK) {
643651        SMLOG_E (" Error in Se05x_API_WriteSymmKey \n " 
@@ -650,9 +658,6 @@ int SE05XClass::writeHMACKey(int objectId, const byte data[], size_t length)
650658{
651659    smStatus_t      status;
652660    SE05x_Result_t  result;
653-     uint8_t          exists = 0 ;
654-     uint16_t         offset = 0 ;
655-     uint16_t         size;
656661
657662    status = Se05x_API_CheckObjectExists (&_se05x_session, objectId, &result);
658663    if  (status != SM_OK) {
@@ -662,7 +667,6 @@ int SE05XClass::writeHMACKey(int objectId, const byte data[], size_t length)
662667
663668    if  (result == kSE05x_Result_SUCCESS ) {
664669        SMLOG_E (" Object exists \n " 
665-         exists = 1 ;
666670    }
667671
668672    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
784788    if (*rawLen < SE05X_EC_KEY_RAW_LENGTH) {
785789        SMLOG_E (" Error in getECKeyXyValuesFromDER \n " 
786790        *rawLen = 0 ;
787-         return  0 ;     
791+         return  0 ;
788792    }
789-      
793+ 
790794    /*  XY values are stored in the last 64 bytes of DER buffer */ 
791795    *rawLen = SE05X_EC_KEY_RAW_LENGTH;
792796    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
799803    if (rawLen != SE05X_EC_KEY_RAW_LENGTH) {
800804        SMLOG_E (" Error in setECKeyXyVauesInDER invalid raw key\n " 
801805        *derLen = 0 ;
802-         return  0 ;     
806+         return  0 ;
803807    }
804808
805809    if (*derLen < SE05X_EC_KEY_DER_LENGTH) {
806810        SMLOG_E (" Error in setECKeyXyVauesInDER buffer too small\n " 
807811        *derLen = 0 ;
808-         return  0 ;     
812+         return  0 ;
809813    }
810-      
814+ 
811815    /*  Copy header byte from 0 to 25 */ 
812816    memcpy (&derKey[0 ], &ecc_der_header_nist256[0 ], SE05X_EC_KEY_DER_HEADER_LENGTH);
813817    /*  Add format byte */ 
@@ -827,13 +831,13 @@ int SE05XClass::getECSignatureRsValuesFromDER(byte* derSignature, size_t derLen,
827831    if  ((derLen < SE05X_EC_SIGNATURE_MIN_DER_LENGTH) || (derLen > SE05X_EC_SIGNATURE_MAX_DER_LENGTH)) {
828832        SMLOG_E (" Error in getECSignatureRsValuesFromDER invalid signature\n " 
829833        *rawLen = 0 ;
830-         return  0 ;   
834+         return  0 ;
831835    }
832836
833837    if  (*rawLen < SE05X_EC_SIGNATURE_RAW_LENGTH) {
834838        SMLOG_E (" Error in getECSignatureRsValuesFromDER buffer too small\n " 
835839        *rawLen = 0 ;
836-         return  0 ;   
840+         return  0 ;
837841    }
838842
839843    rLen = derSignature[3 ];
@@ -868,7 +872,7 @@ int SE05XClass::setECSignatureRsValuesInDER(const byte* rawSignature, size_t raw
868872{
869873    /* *
870874     * Always consider worst case with padding 
871-      *   
875+      * 
872876     * | 0x30 0x46 0x02 0x21 0x00 | R values 32 bytes | 0x02 0x21 0x00 | S values 32 bytes | 
873877     * 
874878     */  
0 commit comments