Skip to content

Commit ac25230

Browse files
pennamgiulcioffi
authored andcommitted
SE050: Add functions to act like ECCX08
1 parent 40f4961 commit ac25230

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed

β€Žlibraries/SE05X/src/SE05X.cpp

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,72 @@ SE05XClass::~SE05XClass()
2929

3030
}
3131

32+
static void getECKeyXyValuesFromDER(byte* derKey, size_t derLen, byte* rawKey)
33+
{
34+
memcpy(rawKey, &derKey[derLen-64], 64);
35+
}
36+
37+
static void setECKeyXyVauesInDER(const byte* rawKey, byte* derKey)
38+
{
39+
static const byte ecc_der_header_nist256[27] =
40+
{
41+
0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86,
42+
0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a,
43+
0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03,
44+
0x42, 0x00, 0x04
45+
};
46+
47+
memcpy(&derKey[0], &ecc_der_header_nist256[0], 27);
48+
memcpy(&derKey[27], &rawKey[0], 64);
49+
}
50+
51+
static void getECSignatureRsValuesFromDER(byte* derSignature, size_t derLen, byte* rawSignature)
52+
{
53+
byte rLen;
54+
byte sLen;
55+
56+
rLen = derSignature[3];
57+
sLen = derSignature[3 + rLen + 2];
58+
59+
byte * out = rawSignature;
60+
61+
if(rLen == 32)
62+
{
63+
memcpy(out, &derSignature[4], 32);
64+
}
65+
else if ((rLen == 33) && (derSignature[4] == 0))
66+
{
67+
memcpy(out, &derSignature[5], 32);
68+
}
69+
70+
out += 32;
71+
72+
if(sLen == 32)
73+
{
74+
memcpy(out, &derSignature[3 + rLen + 3], 32);
75+
}
76+
else if ((sLen == 33) && (derSignature[3 + rLen + 3] == 0))
77+
{
78+
memcpy(out, &derSignature[3 + rLen + 4], 32);
79+
}
80+
}
81+
82+
static void setECSignatureRsValuesInDER(const byte* rawSignature, byte* signature)
83+
{
84+
byte rLen = 32;
85+
byte sLen = 32;
86+
byte rawSignatureLen = 64;
87+
88+
signature[0] = 0x30;
89+
signature[1] = (uint8_t)(rawSignatureLen + 4);
90+
signature[2] = 0x02;
91+
signature[3] = (uint8_t)rLen;
92+
memcpy(&signature[4], &rawSignature[0], rLen);
93+
signature[3 + rLen + 1] = 0x02;
94+
signature[3 + rLen + 2] = (uint8_t)sLen;
95+
memcpy(&signature[3 + rLen + 3], &rawSignature[rLen], sLen);
96+
}
97+
3298
int SE05XClass::begin()
3399
{
34100
sss_status_t status;
@@ -153,6 +219,19 @@ int SE05XClass::generatePrivateKey(int keyId, byte pubKeyDer[], size_t pubKeyDer
153219
return 1;
154220
}
155221

222+
int SE05XClass::generatePrivateKey(int slot, byte publicKey[])
223+
{
224+
byte publicKeyDer[256];
225+
size_t publicKeyDerLen;
226+
227+
if (!generatePrivateKey(slot, publicKeyDer, sizeof(publicKeyDer), &publicKeyDerLen)) {
228+
return 0;
229+
}
230+
231+
getECKeyXyValuesFromDER(publicKeyDer, publicKeyDerLen, publicKey);
232+
return 1;
233+
}
234+
156235
int SE05XClass::generatePublicKey(int keyId, byte pubKeyDer[], size_t pubKeyDerMaxLen, size_t * pubKeyDerlen)
157236
{
158237
sss_status_t status;
@@ -175,6 +254,19 @@ int SE05XClass::generatePublicKey(int keyId, byte pubKeyDer[], size_t pubKeyDerM
175254
return 1;
176255
}
177256

257+
int SE05XClass::generatePublicKey(int slot, byte publicKey[])
258+
{
259+
byte publicKeyDer[256];
260+
size_t publicKeyDerLen;
261+
262+
if (!generatePublicKey(slot, publicKeyDer, sizeof(publicKeyDer), &publicKeyDerLen)) {
263+
return 0;
264+
}
265+
266+
getECKeyXyValuesFromDER(publicKeyDer, publicKeyDerLen, publicKey);
267+
return 1;
268+
}
269+
178270
int SE05XClass::importPublicKey(int keyId, const byte pubKeyDer[], size_t pubKeyDerLen)
179271
{
180272
sss_status_t status;
@@ -288,6 +380,19 @@ int SE05XClass::Sign(int keyId, const byte hash[], size_t hashLen, byte sig[], s
288380
return 1;
289381
}
290382

383+
int SE05XClass::ecSign(int slot, const byte message[], byte signature[])
384+
{
385+
byte signatureDer[256];
386+
size_t signatureDerLen;
387+
if (!Sign(slot, message, 32, signatureDer, sizeof(signatureDer), &signatureDerLen)) {
388+
return 0;
389+
}
390+
391+
/* Get r s values from DER buffer */
392+
getECSignatureRsValuesFromDER(signatureDer, signatureDerLen, signature);
393+
return 1;
394+
}
395+
291396
int SE05XClass::Verify(int keyId, const byte hash[], size_t hashLen, byte sig[], size_t sigLen)
292397
{
293398
sss_status_t status;
@@ -317,6 +422,27 @@ int SE05XClass::Verify(int keyId, const byte hash[], size_t hashLen, byte sig[],
317422
return 1;
318423
}
319424

425+
int SE05XClass::ecdsaVerify(const byte message[], const byte signature[], const byte pubkey[])
426+
{
427+
byte pubKeyDER[91];
428+
byte signatureDER[70];
429+
int result;
430+
431+
setECKeyXyVauesInDER(pubkey, pubKeyDER);
432+
if (!importPublicKey(0xA5A5, pubKeyDER, sizeof(pubKeyDER))) {
433+
return 0;
434+
}
435+
436+
setECSignatureRsValuesInDER(signature, signatureDER);
437+
438+
result = Verify(0xA5A5, message, 32, signatureDER, 70);
439+
440+
if (!deleteBinaryObject(0xA5A5)) {
441+
return 0;
442+
}
443+
return result;
444+
}
445+
320446
int SE05XClass::readBinaryObject(int objectId, byte data[], size_t dataMaxLen, size_t * length)
321447
{
322448
sss_status_t status;
@@ -337,6 +463,12 @@ int SE05XClass::readBinaryObject(int objectId, byte data[], size_t dataMaxLen, s
337463
return 1;
338464
}
339465

466+
int SE05XClass::readSlot(int slot, byte data[], int length)
467+
{
468+
size_t binSizeBits;
469+
return readBinaryObject(slot, data, length, &binSizeBits);
470+
}
471+
340472
int SE05XClass::writeBinaryObject(int objectId, const byte data[], size_t length)
341473
{
342474
sss_status_t status;
@@ -355,6 +487,16 @@ int SE05XClass::writeBinaryObject(int objectId, const byte data[], size_t length
355487
return 1;
356488
}
357489

490+
int SE05XClass::writeSlot(int slot, const byte data[], int length)
491+
{
492+
if (existsBinaryObject(slot)) {
493+
if (!deleteBinaryObject(slot)) {
494+
return 0;
495+
}
496+
}
497+
return writeBinaryObject(slot, data, length);
498+
}
499+
358500
int SE05XClass::existsBinaryObject(int objectId)
359501
{
360502
sss_object_t binObject;

β€Žlibraries/SE05X/src/SE05X.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ class SE05XClass
6969

7070
ex_sss_boot_ctx_t* getDeviceCtx(void);
7171

72+
int generatePrivateKey(int slot, byte publicKey[]);
73+
int generatePublicKey(int slot, byte publicKey[]);
74+
int ecdsaVerify(const byte message[], const byte signature[], const byte pubkey[]);
75+
int ecSign(int slot, const byte message[], byte signature[]);
76+
int readSlot(int slot, byte data[], int length);
77+
int writeSlot(int slot, const byte data[], int length);
78+
inline int locked() { return 1; }
79+
inline int writeConfiguration(const byte data[]) { return 1; }
80+
inline int readConfiguration(byte data[]) { return 1; }
81+
inline int lock() { return 1; }
82+
7283
private:
7384
int initObject(size_t objectId, sss_object_t * object, sss_key_part_t objectPart, sss_key_object_mode_t objectMode, sss_cipher_type_t objectChiper);
7485

0 commit comments

Comments
 (0)