Skip to content

Commit

Permalink
Merge pull request #977 from pennam/se050-sn
Browse files Browse the repository at this point in the history
SE050: Add serialNumber(byte sn[])
  • Loading branch information
pennam authored Oct 24, 2024
2 parents 103d4ab + 8bc3ab2 commit aacd17c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
35 changes: 26 additions & 9 deletions libraries/SE05X/src/SE05X.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#define SE05X_EC_SIGNATURE_HEADER_LENGTH 6
#define SE05X_EC_SIGNATURE_DER_LENGTH SE05X_EC_SIGNATURE_HEADER_LENGTH + SE05X_EC_SIGNATURE_RAW_LENGTH
#define SE05X_SHA256_LENGTH 32
#define SE05X_SN_LENGTH 18
#define SE05X_DER_BUFFER_SIZE 256
#define SE05X_TEMP_OBJECT 9999

Expand Down Expand Up @@ -111,8 +110,6 @@ static void setECSignatureRsValuesInDER(const byte* rawSignature, byte* signatur

int SE05XClass::begin()
{
sss_status_t status;

memset(&_boot_ctx, 0, sizeof(ex_sss_boot_ctx_t));

se05x_ic_power_on();
Expand Down Expand Up @@ -158,17 +155,39 @@ int SE05XClass::readConfiguration(byte data[])
return 1;
}

int SE05XClass::serialNumber(byte sn[])
{
return serialNumber(sn, SE05X_SN_LENGTH);
}

int SE05XClass::serialNumber(byte sn[], size_t length)
{
size_t uidLen = SE05X_SN_LENGTH;
byte UID[SE05X_SN_LENGTH];

if(!sn) {
return 0;
}

sss_status_t status = sss_session_prop_get_au8(&_boot_ctx.session, kSSS_SessionProp_UID, UID, &uidLen);
if ((status != kStatus_SSS_Success)) {
SE05X_PRINT_ERROR("Error in Se05x_API_ReadObject \n");
return 0;
}
memcpy(sn, UID, length < SE05X_SN_LENGTH ? length : SE05X_SN_LENGTH);
return 1;
}

String SE05XClass::serialNumber()
{
String result = (char*)NULL;
byte UID[SE05X_SN_LENGTH];
size_t uidLen = 18;

sss_session_prop_get_au8(&_boot_ctx.session, kSSS_SessionProp_UID, UID, &uidLen);
serialNumber(UID, sizeof(UID));

result.reserve(uidLen*2);
result.reserve(SE05X_SN_LENGTH * 2);

for (int i = 0; i < uidLen; i++) {
for (size_t i = 0; i < SE05X_SN_LENGTH; i++) {
byte b = UID[i];

if (b < 16) {
Expand Down Expand Up @@ -591,8 +610,6 @@ int SE05XClass::deleteAllObjects(void)

int SE05XClass::getObjectHandle(int objectId, sss_object_t * object)
{
sss_status_t status;

if(kStatus_SSS_Success != sss_key_object_init(object, &_boot_ctx.ks)) {
SE05X_PRINT_ERROR("sss_key_object_init Failed");
return 0;
Expand Down
8 changes: 6 additions & 2 deletions libraries/SE05X/src/SE05X.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
#include "se05x_APDU.h"

#if defined SE05X_PRINT_ERROR_ENABLE
#define SE05X_PRINT_ERROR Serial.println
#define SE05X_PRINT_ERROR(x) Serial.println(x)
#else
#define SE05X_PRINT_ERROR
#define SE05X_PRINT_ERROR(x)
#endif

#define SE05X_SN_LENGTH 18

class SE05XClass
{
public:
Expand All @@ -41,6 +43,8 @@ class SE05XClass
int begin();
void end();

int serialNumber(byte sn[]);
int serialNumber(byte sn[], size_t length);
String serialNumber();

long random(long max);
Expand Down

0 comments on commit aacd17c

Please sign in to comment.