Skip to content

Commit 3deddbe

Browse files
authoredNov 20, 2024··
Merge pull request #383 from pennam/sn-byte
SE050 add api to read serial number bytes
2 parents 936a7eb + 55f37fa commit 3deddbe

File tree

2 files changed

+71
-63
lines changed

2 files changed

+71
-63
lines changed
 

‎libraries/SE05X/src/SE05X.cpp

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
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+
111129
String 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()
357369
int 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
*/

‎libraries/SE05X/src/SE05X.h

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ extern "C" {
3333
}
3434
#endif
3535

36+
#define SE05X_SN_LENGTH 18
37+
3638
class SE05XClass
3739
{
3840
public:
@@ -42,6 +44,8 @@ class SE05XClass
4244
int begin();
4345
void end();
4446

47+
int serialNumber(byte sn[]);
48+
int serialNumber(byte sn[], size_t length);
4549
#if defined (ARDUINO)
4650
String serialNumber();
4751
#endif
@@ -65,7 +69,7 @@ class SE05XClass
6569
* @return 0 on Failure 1 on Success
6670
*/
6771
int generatePrivateKey(int keyID, byte keyBuf[], size_t keyBufMaxLen, size_t* keyLen);
68-
72+
6973
/** generatePublicKey
7074
*
7175
* Reads ECCurve_NIST_P256 public key from KeyID. Public key will be available
@@ -114,7 +118,7 @@ class SE05XClass
114118
* @param[in] inLen Input data length
115119
*
116120
* @return 0 on Failure 1 on Success
117-
*/
121+
*/
118122
int updateSHA256(const byte in[], size_t inLen);
119123

120124
/** endSHA256
@@ -125,9 +129,9 @@ class SE05XClass
125129
* @param[in,out] outLen Size of output data buffer, SHA256 length
126130
*
127131
* @return 0 on Failure 1 on Success
128-
*/
132+
*/
129133
int endSHA256(byte out[], size_t* outLen);
130-
134+
131135
/** SHA256
132136
*
133137
* One-shot SHA256
@@ -139,15 +143,15 @@ class SE05XClass
139143
* @param[out] outLen SHA256 length
140144
*
141145
* @return 0 on Failure 1 on Success
142-
*/
146+
*/
143147
int SHA256(const byte in[], size_t inLen, byte out[], size_t outMaxLen, size_t* outLen);
144148

145149
/** Sign
146150
*
147151
* Computes ECDSA signature using key stored in KeyID SE050 object.
148152
* Output buffer is filled with the signature in DER format:
149-
*
150-
* | 0x30 | payloadsize 1 byte | 0x02 | R length 1 byte | padding 0x00 (if length 0x21) | R values 32 bytes
153+
*
154+
* | 0x30 | payloadsize 1 byte | 0x02 | R length 1 byte | padding 0x00 (if length 0x21) | R values 32 bytes
151155
* | 0x02 | S length 1 byte | padding 0x00 (if length 0x21) | S values 32 bytes
152156
*
153157
* SHA256 -> private Key -> Signature
@@ -160,7 +164,7 @@ class SE05XClass
160164
* @param[out] sigLen signature length
161165
*
162166
* @return 0 on Failure 1 on Success
163-
*/
167+
*/
164168
int Sign(int keyID, const byte hash[], size_t hashLen, byte sig[], size_t maxSigLen, size_t* sigLen);
165169

166170
/** Verify
@@ -170,28 +174,28 @@ class SE05XClass
170174
* Input SHA256
171175
* ? Match ?
172176
* Signature -> public Key -> Original SHA256
173-
*
177+
*
174178
* @param[in] keyID SE050 object ID containing the key
175179
* @param[in] hash Input SHA256 used to compute the signature
176180
* @param[in] hashLen SHA256 length
177181
* @param[in] sig Input buffer containint the signature
178182
* @param[in] sigLen signature length
179183
*
180184
* @return 0 on Failure (Not match) 1 on Success (Match)
181-
*/
185+
*/
182186
int Verify(int keyID, const byte hash[], size_t hashLen, const byte sig[],size_t sigLen);
183187

184188
/** readBinaryObject
185189
*
186190
* Reads binary data from SE050 object.
187-
*
191+
*
188192
* @param[in] ObjectId SE050 object ID containing data
189193
* @param[out] data Output data buffer
190194
* @param[in] dataMaxLen Output data buffer size
191195
* @param[out] sig Binary object size
192196
*
193197
* @return 0 on Failure 1 on Success
194-
*/
198+
*/
195199
int readBinaryObject(int ObjectId, byte data[], size_t dataMaxLen, size_t* length);
196200

197201
/** AES_ECB_encrypt
@@ -265,43 +269,43 @@ class SE05XClass
265269
/** writeBinaryObject
266270
*
267271
* Writes binary data into SE050 object.
268-
*
272+
*
269273
* @param[in] ObjectId SE050 object ID
270274
* @param[in] data Input data buffer
271275
* @param[in] length Input data buffer size
272276
*
273277
* @return 0 on Failure 1 on Success
274-
*/
278+
*/
275279
int writeBinaryObject(int ObjectId, const byte data[], size_t length);
276280

277281
/** existsBinaryObject
278282
*
279283
* Checks if Object exist
280-
*
284+
*
281285
* @param[in] ObjectId SE050 object ID
282286
*
283287
* @return 0 on Failure (Not exist) 1 on Success (Exists)
284-
*/
288+
*/
285289
int existsBinaryObject(int objectId);
286290

287291
/** deleteBinaryObject
288292
*
289293
* Deletes SE050 object
290-
*
294+
*
291295
* @param[in] ObjectId SE050 object ID
292296
*
293297
* @return 0 on Failure 1 on Success
294-
*/
298+
*/
295299
int deleteBinaryObject(int objectId);
296300

297301
/** deleteBinaryObject
298302
*
299303
* Deletes all SE050 user objects
300-
*
304+
*
301305
* @param[in] ObjectId SE050 object ID
302306
*
303307
* @return 0 on Failure 1 on Success
304-
*/
308+
*/
305309
int deleteAllObjects();
306310

307311
/* ECCX08 legacy API*/
@@ -341,20 +345,20 @@ class SE05XClass
341345
* Input SHA256
342346
* ? Match ?
343347
* Signature -> public Key -> Original SHA256
344-
*
348+
*
345349
* @param[in] message Input SHA256 used to compute the signature 32 bytes
346350
* @param[in] sig Input buffer containint the signature R S values 64bytes
347351
* @param[in] pubkey Public key X Y values 64bytes
348352
*
349353
* @return 0 on Failure (Not match) 1 on Success (Match)
350-
*/
354+
*/
351355
int ecdsaVerify(const byte message[], const byte signature[], const byte pubkey[]);
352356

353357
/** ecSign
354358
*
355359
* Computes ECDSA signature using key stored in KeyID SE050 object.
356360
* Output buffer is filled with the signature R S values:
357-
*
361+
*
358362
* | R values 32 bytes | S values 32 bytes |
359363
*
360364
* SHA256 -> private Key -> Signature
@@ -364,36 +368,36 @@ class SE05XClass
364368
* @param[out] signature Output buffer containint the signature 64 bytes
365369
*
366370
* @return 0 on Failure 1 on Success
367-
*/
371+
*/
368372
int ecSign(int slot, const byte message[], byte signature[]);
369373

370374
/** readSlot
371375
*
372376
* Reads binary data from SE050 object.
373-
*
377+
*
374378
* @param[in] ObjecslottId SE050 object ID containing data
375379
* @param[out] data Output data buffer
376380
* @param[in] length Number of bytes to read
377381
*
378382
* @return 0 on Failure 1 on Success
379-
*/
383+
*/
380384
int readSlot(int slot, byte data[], int length);
381385

382386
/** writeSlot
383387
*
384388
* Writes binary data into SE050 object.
385-
*
389+
*
386390
* @param[in] ObjectId SE050 object ID
387391
* @param[in] data Input data buffer
388392
* @param[in] length Number of bytes to write
389393
*
390394
* @return 0 on Failure 1 on Success
391-
*/
395+
*/
392396
int writeSlot(int slot, const byte data[], int length);
393397

394398
inline int locked() { return 1; }
395399
inline int lock() { return 1; }
396-
inline int writeConfiguration(const byte data[]) { return 1; }
400+
inline int writeConfiguration(const byte data[]) { (void)data; return 1; }
397401
inline Se05xSession_t* getSession() { return &_se05x_session; }
398402

399403
private:

0 commit comments

Comments
 (0)
Please sign in to comment.