@@ -29,6 +29,72 @@ SE05XClass::~SE05XClass()
29
29
30
30
}
31
31
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
+
32
98
int SE05XClass::begin ()
33
99
{
34
100
sss_status_t status;
@@ -153,6 +219,19 @@ int SE05XClass::generatePrivateKey(int keyId, byte pubKeyDer[], size_t pubKeyDer
153
219
return 1 ;
154
220
}
155
221
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
+
156
235
int SE05XClass::generatePublicKey (int keyId, byte pubKeyDer[], size_t pubKeyDerMaxLen, size_t * pubKeyDerlen)
157
236
{
158
237
sss_status_t status;
@@ -175,6 +254,19 @@ int SE05XClass::generatePublicKey(int keyId, byte pubKeyDer[], size_t pubKeyDerM
175
254
return 1 ;
176
255
}
177
256
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
+
178
270
int SE05XClass::importPublicKey (int keyId, const byte pubKeyDer[], size_t pubKeyDerLen)
179
271
{
180
272
sss_status_t status;
@@ -288,6 +380,19 @@ int SE05XClass::Sign(int keyId, const byte hash[], size_t hashLen, byte sig[], s
288
380
return 1 ;
289
381
}
290
382
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
+
291
396
int SE05XClass::Verify (int keyId, const byte hash[], size_t hashLen, byte sig[], size_t sigLen)
292
397
{
293
398
sss_status_t status;
@@ -317,6 +422,27 @@ int SE05XClass::Verify(int keyId, const byte hash[], size_t hashLen, byte sig[],
317
422
return 1 ;
318
423
}
319
424
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
+
320
446
int SE05XClass::readBinaryObject (int objectId, byte data[], size_t dataMaxLen, size_t * length)
321
447
{
322
448
sss_status_t status;
@@ -337,6 +463,12 @@ int SE05XClass::readBinaryObject(int objectId, byte data[], size_t dataMaxLen, s
337
463
return 1 ;
338
464
}
339
465
466
+ int SE05XClass::readSlot (int slot, byte data[], int length)
467
+ {
468
+ size_t binSizeBits;
469
+ return readBinaryObject (slot, data, length, &binSizeBits);
470
+ }
471
+
340
472
int SE05XClass::writeBinaryObject (int objectId, const byte data[], size_t length)
341
473
{
342
474
sss_status_t status;
@@ -355,6 +487,16 @@ int SE05XClass::writeBinaryObject(int objectId, const byte data[], size_t length
355
487
return 1 ;
356
488
}
357
489
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
+
358
500
int SE05XClass::existsBinaryObject (int objectId)
359
501
{
360
502
sss_object_t binObject;
0 commit comments