Skip to content

Commit 7339b0c

Browse files
committed
Add ECC508 EC sign support
1 parent 9cc5833 commit 7339b0c

File tree

5 files changed

+85
-3
lines changed

5 files changed

+85
-3
lines changed

src/BearSSLClient.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,11 @@ int BearSSLClient::connectSSL(const char* host)
162162
unsigned char entropy[32];
163163

164164
if (ECC508.begin() && ECC508.random(entropy, sizeof(entropy))) {
165-
// ECC508 random success, add custom ECDSA vfry
165+
// ECC508 random success, add custom ECDSA vfry and EC sign
166166
br_ssl_engine_set_ecdsa(&_sc.eng, ecc508_vrfy_asn1);
167-
br_x509_minimal_set_ecdsa(&_xc, br_ssl_engine_get_ec(&_sc.eng), br_ssl_engine_get_ecdsa(&_sc.eng));
167+
br_x509_minimal_set_ecdsa(&_xc, br_ssl_engine_get_ec(&_sc.eng), br_ssl_engine_get_ecdsa(&_sc.eng));
168+
169+
// br_ssl_client_set_single_ec(&_sc, CHAIN, CHAIN_LEN, &EC, BR_KEYTYPE_KEYX | BR_KEYTYPE_SIGN, BR_KEYTYPE_EC, br_ec_get_default(), ecc508_sign_asn1);
168170
} else {
169171
// no ECC508 or random failed, fallback to pseudo random
170172
for (size_t i = 0; i < sizeof(entropy); i++) {

src/utility/ECC508.cpp

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,30 @@ int ECC508Class::ecdsaVerify(const byte message[], const byte signature[], const
6868
return 0;
6969
}
7070

71-
return verify(signature, pubkey);
71+
if (!verify(signature, pubkey)) {
72+
return 0;
73+
}
74+
75+
return 1;
76+
}
77+
78+
int ECC508Class::ecSign(int slot, const byte message[], byte signature[])
79+
{
80+
byte rand[32];
81+
82+
if (!random(rand, sizeof(rand))) {
83+
return 0;
84+
}
85+
86+
if (!challenge(message)) {
87+
return 0;
88+
}
89+
90+
if (!sign(slot, signature)) {
91+
return 0;
92+
}
93+
94+
return 1;
7295
}
7396

7497
int ECC508Class::wakeup()
@@ -197,6 +220,28 @@ int ECC508Class::verify(const byte signature[], const byte pubkey[])
197220
return 1;
198221
}
199222

223+
int ECC508Class::sign(int slot, byte signature[])
224+
{
225+
if (!wakeup()) {
226+
return 0;
227+
}
228+
229+
if (!sendCommand(0x41, 0x80, slot)) {
230+
return 0;
231+
}
232+
233+
delay(50);
234+
235+
if (!receiveResponse(signature, 64)) {
236+
return 0;
237+
}
238+
239+
delay(1);
240+
idle();
241+
242+
return 1;
243+
}
244+
200245
int ECC508Class::sendCommand(uint8_t opcode, uint8_t param1, uint16_t param2, const byte data[], size_t dataLength)
201246
{
202247
byte command[8 + dataLength]; // 1 for type, 1 for length, 1 for opcode, 1 for param1, 2 for param2, 2 for crc

src/utility/ECC508.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class ECC508Class
1616
int random(byte data[], size_t length);
1717

1818
int ecdsaVerify(const byte message[], const byte signature[], const byte pubkey[]);
19+
int ecSign(int slot, const byte message[], byte signature[]);
1920

2021
private:
2122
int wakeup();
@@ -25,6 +26,7 @@ class ECC508Class
2526
int version();
2627
int challenge(const byte message[]);
2728
int verify(const byte signature[], const byte pubkey[]);
29+
int sign(int slot, byte signature[]);
2830

2931
int sendCommand(uint8_t opcode, uint8_t param1, uint16_t param2, const byte data[] = NULL, size_t dataLength = 0);
3032
int receiveResponse(void* response, size_t length);

src/utility/ecc508_asn1.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

44
#include "bearssl/bearssl.h"
55

6+
size_t
7+
ecc508_sign_asn1(const br_ec_impl *impl,
8+
const br_hash_class *hf, const void *hash_value,
9+
const br_ec_private_key *sk, void *sig);
10+
611
uint32_t
712
ecc508_vrfy_asn1(const br_ec_impl *impl,
813
const void *hash, size_t hash_len,

src/utility/ecc508_sign_asn1.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "ecc508_asn1.h"
2+
3+
#include "ECC508.h"
4+
5+
#define BR_MAX_EC_SIZE 528
6+
#define ORDER_LEN ((BR_MAX_EC_SIZE + 7) >> 3)
7+
8+
size_t
9+
ecc508_sign_asn1(const br_ec_impl * /*impl*/,
10+
const br_hash_class * /*hf*/, const void *hash_value,
11+
const br_ec_private_key *sk, void *sig)
12+
{
13+
unsigned char rsig[(ORDER_LEN << 1) + 12];
14+
size_t sig_len;
15+
16+
if (sk->curve != BR_EC_secp256r1) {
17+
return 0;
18+
}
19+
20+
if (!ECC508.ecSign(0, (const uint8_t*)hash_value, (uint8_t*)rsig)) {
21+
return 0;
22+
}
23+
sig_len = 64;
24+
25+
sig_len = br_ecdsa_raw_to_asn1(rsig, sig_len);
26+
memcpy(sig, rsig, sig_len);
27+
return sig_len;
28+
}

0 commit comments

Comments
 (0)