20
20
#include " SE05X.h"
21
21
22
22
SE05XClass::SE05XClass ()
23
+ : _cipher_type {kSSS_CipherType_EC_NIST_P }
24
+ , _algorithm_type {kAlgorithm_SSS_ECDSA_SHA256 }
25
+ , _key_size_bits {256 }
23
26
{
24
27
25
28
}
@@ -126,6 +129,24 @@ void SE05XClass::end()
126
129
se05x_ic_power_off ();
127
130
}
128
131
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
+
129
150
String SE05XClass::serialNumber ()
130
151
{
131
152
String result = (char *)NULL ;
@@ -195,15 +216,13 @@ int SE05XClass::generatePrivateKey(int keyId, byte pubKeyDer[], size_t pubKeyDer
195
216
{
196
217
sss_status_t status;
197
218
sss_object_t keyObject;
198
- size_t keySizeBits;
199
219
size_t derSzBits;
200
220
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 )) {
202
222
return 0 ;
203
223
}
204
224
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 );
207
226
208
227
if (status == kStatus_SSS_Success ) {
209
228
derSzBits = pubKeyDerMaxLen * 8 ;
@@ -224,6 +243,10 @@ int SE05XClass::generatePrivateKey(int slot, byte publicKey[])
224
243
byte publicKeyDer[256 ];
225
244
size_t publicKeyDerLen;
226
245
246
+ if ((_cipher_type != kSSS_CipherType_EC_NIST_P ) || (_algorithm_type != kAlgorithm_SSS_ECDSA_SHA256 )) {
247
+ return 0 ;
248
+ }
249
+
227
250
if (!generatePrivateKey (slot, publicKeyDer, sizeof (publicKeyDer), &publicKeyDerLen)) {
228
251
return 0 ;
229
252
}
@@ -238,7 +261,7 @@ int SE05XClass::generatePublicKey(int keyId, byte pubKeyDer[], size_t pubKeyDerM
238
261
sss_object_t keyObject;
239
262
size_t derSzBits;
240
263
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 )) {
242
265
return 0 ;
243
266
}
244
267
@@ -259,6 +282,10 @@ int SE05XClass::generatePublicKey(int slot, byte publicKey[])
259
282
byte publicKeyDer[256 ];
260
283
size_t publicKeyDerLen;
261
284
285
+ if ((_cipher_type != kSSS_CipherType_EC_NIST_P ) || (_algorithm_type != kAlgorithm_SSS_ECDSA_SHA256 )) {
286
+ return 0 ;
287
+ }
288
+
262
289
if (!generatePublicKey (slot, publicKeyDer, sizeof (publicKeyDer), &publicKeyDerLen)) {
263
290
return 0 ;
264
291
}
@@ -271,14 +298,12 @@ int SE05XClass::importPublicKey(int keyId, const byte pubKeyDer[], size_t pubKey
271
298
{
272
299
sss_status_t status;
273
300
sss_object_t keyObject;
274
- size_t keySizeBits;
275
301
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 )) {
277
303
return 0 ;
278
304
}
279
305
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 );
282
307
283
308
if (status != kStatus_SSS_Success ) {
284
309
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
356
381
sss_object_t keyObject;
357
382
sss_asymmetric_t ctx_asymm;
358
383
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 )) {
360
385
return 0 ;
361
386
}
362
387
363
388
status = sss_asymmetric_context_init (&ctx_asymm,
364
389
&_boot_ctx.session ,
365
390
&keyObject,
366
- kAlgorithm_SSS_ECDSA_SHA256 ,
391
+ _algorithm_type ,
367
392
kMode_SSS_Sign );
368
393
369
394
if (status != kStatus_SSS_Success ) {
@@ -384,6 +409,11 @@ int SE05XClass::ecSign(int slot, const byte message[], byte signature[])
384
409
{
385
410
byte signatureDer[256 ];
386
411
size_t signatureDerLen;
412
+
413
+ if ((_cipher_type != kSSS_CipherType_EC_NIST_P ) || (_algorithm_type != kAlgorithm_SSS_ECDSA_SHA256 )) {
414
+ return 0 ;
415
+ }
416
+
387
417
if (!Sign (slot, message, 32 , signatureDer, sizeof (signatureDer), &signatureDerLen)) {
388
418
return 0 ;
389
419
}
@@ -399,14 +429,14 @@ int SE05XClass::Verify(int keyId, const byte hash[], size_t hashLen, const byte
399
429
sss_object_t keyObject;
400
430
sss_asymmetric_t ctx_asymm;
401
431
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 )) {
403
433
return 0 ;
404
434
}
405
435
406
436
status = sss_asymmetric_context_init (&ctx_asymm,
407
437
&_boot_ctx.session ,
408
438
&keyObject,
409
- kAlgorithm_SSS_ECDSA_SHA256 ,
439
+ _algorithm_type ,
410
440
kMode_SSS_Verify );
411
441
412
442
if (status != kStatus_SSS_Success ) {
@@ -428,6 +458,10 @@ int SE05XClass::ecdsaVerify(const byte message[], const byte signature[], const
428
458
byte signatureDER[70 ];
429
459
int result;
430
460
461
+ if ((_cipher_type != kSSS_CipherType_EC_NIST_P ) || (_algorithm_type != kAlgorithm_SSS_ECDSA_SHA256 )) {
462
+ return 0 ;
463
+ }
464
+
431
465
setECKeyXyVauesInDER (pubkey, pubKeyDER);
432
466
if (!importPublicKey (0xA5A5 , pubKeyDER, sizeof (pubKeyDER))) {
433
467
return 0 ;
0 commit comments