Skip to content

Commit 5863e50

Browse files
pennamgiulcioffi
authored andcommitted
SE05X: Add possibility to configure cipher type and algorithm for Sign Verify and Key generation
1 parent 95d5111 commit 5863e50

File tree

2 files changed

+52
-15
lines changed

2 files changed

+52
-15
lines changed

libraries/SE05X/src/SE05X.cpp

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
#include "SE05X.h"
2121

2222
SE05XClass::SE05XClass()
23+
: _cipher_type {kSSS_CipherType_EC_NIST_P}
24+
, _algorithm_type {kAlgorithm_SSS_ECDSA_SHA256}
25+
, _key_size_bits {256}
2326
{
2427

2528
}
@@ -126,6 +129,24 @@ void SE05XClass::end()
126129
se05x_ic_power_off();
127130
}
128131

132+
int SE05XClass::writeConfiguration(const byte data[])
133+
{
134+
_cipher_type = (sss_cipher_type_t)data[0];
135+
_algorithm_type = (sss_algorithm_t)(data[1] << 8 | data[2]);
136+
_key_size_bits = (size_t)(data[3] << 8 | data[4]);
137+
return 1;
138+
}
139+
140+
int SE05XClass::readConfiguration(byte data[])
141+
{
142+
data[0] = (byte)_cipher_type;
143+
data[1] = (byte)_algorithm_type >> 8;
144+
data[2] = (byte)_algorithm_type;
145+
data[3] = (byte)_key_size_bits >> 8;
146+
data[4] = (byte)_key_size_bits;
147+
return 1;
148+
}
149+
129150
String SE05XClass::serialNumber()
130151
{
131152
String result = (char*)NULL;
@@ -195,15 +216,13 @@ int SE05XClass::generatePrivateKey(int keyId, byte pubKeyDer[], size_t pubKeyDer
195216
{
196217
sss_status_t status;
197218
sss_object_t keyObject;
198-
size_t keySizeBits;
199219
size_t derSzBits;
200220

201-
if(!initObject(keyId, &keyObject, kSSS_KeyPart_Pair, kKeyObject_Mode_Persistent, kSSS_CipherType_EC_NIST_P)) {
221+
if(!initObject(keyId, &keyObject, kSSS_KeyPart_Pair, kKeyObject_Mode_Persistent, _cipher_type)) {
202222
return 0;
203223
}
204224

205-
keySizeBits = 256;
206-
status = sss_key_store_generate_key(&_boot_ctx.ks, &keyObject, keySizeBits, NULL);
225+
status = sss_key_store_generate_key(&_boot_ctx.ks, &keyObject, _key_size_bits, NULL);
207226

208227
if (status == kStatus_SSS_Success) {
209228
derSzBits = pubKeyDerMaxLen * 8;
@@ -224,6 +243,10 @@ int SE05XClass::generatePrivateKey(int slot, byte publicKey[])
224243
byte publicKeyDer[256];
225244
size_t publicKeyDerLen;
226245

246+
if ((_cipher_type != kSSS_CipherType_EC_NIST_P) || (_algorithm_type != kAlgorithm_SSS_ECDSA_SHA256)) {
247+
return 0;
248+
}
249+
227250
if (!generatePrivateKey(slot, publicKeyDer, sizeof(publicKeyDer), &publicKeyDerLen)) {
228251
return 0;
229252
}
@@ -238,7 +261,7 @@ int SE05XClass::generatePublicKey(int keyId, byte pubKeyDer[], size_t pubKeyDerM
238261
sss_object_t keyObject;
239262
size_t derSzBits;
240263

241-
if(!initObject(keyId, &keyObject, kSSS_KeyPart_Pair, kKeyObject_Mode_Persistent, kSSS_CipherType_EC_NIST_P)) {
264+
if(!initObject(keyId, &keyObject, kSSS_KeyPart_Pair, kKeyObject_Mode_Persistent, _cipher_type)) {
242265
return 0;
243266
}
244267

@@ -259,6 +282,10 @@ int SE05XClass::generatePublicKey(int slot, byte publicKey[])
259282
byte publicKeyDer[256];
260283
size_t publicKeyDerLen;
261284

285+
if ((_cipher_type != kSSS_CipherType_EC_NIST_P) || (_algorithm_type != kAlgorithm_SSS_ECDSA_SHA256)) {
286+
return 0;
287+
}
288+
262289
if (!generatePublicKey(slot, publicKeyDer, sizeof(publicKeyDer), &publicKeyDerLen)) {
263290
return 0;
264291
}
@@ -271,14 +298,12 @@ int SE05XClass::importPublicKey(int keyId, const byte pubKeyDer[], size_t pubKey
271298
{
272299
sss_status_t status;
273300
sss_object_t keyObject;
274-
size_t keySizeBits;
275301

276-
if(!initObject(keyId, &keyObject, kSSS_KeyPart_Public, kKeyObject_Mode_Persistent, kSSS_CipherType_EC_NIST_P)) {
302+
if(!initObject(keyId, &keyObject, kSSS_KeyPart_Public, kKeyObject_Mode_Persistent, _cipher_type)) {
277303
return 0;
278304
}
279305

280-
keySizeBits = 256;
281-
status = sss_key_store_set_key(&_boot_ctx.ks, &keyObject, pubKeyDer, pubKeyDerLen, keySizeBits, NULL, 0);
306+
status = sss_key_store_set_key(&_boot_ctx.ks, &keyObject, pubKeyDer, pubKeyDerLen, _key_size_bits, NULL, 0);
282307

283308
if(status != kStatus_SSS_Success ) {
284309
LOG_E("sss_key_store_set_key Failed");
@@ -356,14 +381,14 @@ int SE05XClass::Sign(int keyId, const byte hash[], size_t hashLen, byte sig[], s
356381
sss_object_t keyObject;
357382
sss_asymmetric_t ctx_asymm;
358383

359-
if(!initObject(keyId, &keyObject, kSSS_KeyPart_Private, kKeyObject_Mode_Persistent, kSSS_CipherType_EC_NIST_P)) {
384+
if(!initObject(keyId, &keyObject, kSSS_KeyPart_Private, kKeyObject_Mode_Persistent, _cipher_type)) {
360385
return 0;
361386
}
362387

363388
status = sss_asymmetric_context_init(&ctx_asymm,
364389
&_boot_ctx.session,
365390
&keyObject,
366-
kAlgorithm_SSS_ECDSA_SHA256,
391+
_algorithm_type,
367392
kMode_SSS_Sign);
368393

369394
if(status != kStatus_SSS_Success) {
@@ -384,6 +409,11 @@ int SE05XClass::ecSign(int slot, const byte message[], byte signature[])
384409
{
385410
byte signatureDer[256];
386411
size_t signatureDerLen;
412+
413+
if ((_cipher_type != kSSS_CipherType_EC_NIST_P) || (_algorithm_type != kAlgorithm_SSS_ECDSA_SHA256)) {
414+
return 0;
415+
}
416+
387417
if (!Sign(slot, message, 32, signatureDer, sizeof(signatureDer), &signatureDerLen)) {
388418
return 0;
389419
}
@@ -399,14 +429,14 @@ int SE05XClass::Verify(int keyId, const byte hash[], size_t hashLen, const byte
399429
sss_object_t keyObject;
400430
sss_asymmetric_t ctx_asymm;
401431

402-
if(!initObject(keyId, &keyObject, kSSS_KeyPart_Public, kKeyObject_Mode_Persistent, kSSS_CipherType_EC_NIST_P)) {
432+
if(!initObject(keyId, &keyObject, kSSS_KeyPart_Public, kKeyObject_Mode_Persistent, _cipher_type)) {
403433
return 0;
404434
}
405435

406436
status = sss_asymmetric_context_init(&ctx_asymm,
407437
&_boot_ctx.session,
408438
&keyObject,
409-
kAlgorithm_SSS_ECDSA_SHA256,
439+
_algorithm_type,
410440
kMode_SSS_Verify);
411441

412442
if(status != kStatus_SSS_Success) {
@@ -428,6 +458,10 @@ int SE05XClass::ecdsaVerify(const byte message[], const byte signature[], const
428458
byte signatureDER[70];
429459
int result;
430460

461+
if ((_cipher_type != kSSS_CipherType_EC_NIST_P) || (_algorithm_type != kAlgorithm_SSS_ECDSA_SHA256)) {
462+
return 0;
463+
}
464+
431465
setECKeyXyVauesInDER(pubkey, pubKeyDER);
432466
if (!importPublicKey(0xA5A5, pubKeyDER, sizeof(pubKeyDER))) {
433467
return 0;

libraries/SE05X/src/SE05X.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ class SE05XClass
7676
int readSlot(int slot, byte data[], int length);
7777
int writeSlot(int slot, const byte data[], int length);
7878
inline int locked() { return 1; }
79-
inline int writeConfiguration(const byte data[]) { return 1; }
80-
inline int readConfiguration(byte data[]) { return 1; }
79+
inline int writeConfiguration(const byte data[]);
80+
inline int readConfiguration(byte data[]);
8181
inline int lock() { return 1; }
8282

8383
private:
@@ -86,6 +86,9 @@ class SE05XClass
8686
private:
8787
ex_sss_boot_ctx_t _boot_ctx;
8888
sss_digest_t _digest_ctx;
89+
sss_cipher_type_t _cipher_type;
90+
sss_algorithm_t _algorithm_type;
91+
size_t _key_size_bits;
8992
};
9093

9194
extern SE05XClass SE05X;

0 commit comments

Comments
 (0)