Skip to content

Commit 5a4ec08

Browse files
committed
fix card identification
1 parent 0d3f4e8 commit 5a4ec08

File tree

3 files changed

+45
-18
lines changed

3 files changed

+45
-18
lines changed

types/certificate.go

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"crypto/sha256"
55
"errors"
66

7-
"github.com/ethereum/go-ethereum/crypto"
87
"github.com/status-im/keycard-go/apdu"
98
)
109

@@ -23,7 +22,7 @@ func ParseCertificate(data []byte) (*Certificate, error) {
2322
}
2423

2524
identPub := data[0:33]
26-
sigData := data[33:97]
25+
sigData := data[33:98]
2726
msg := sha256.Sum256(identPub)
2827

2928
sig, err := ParseRecoverableSignature(msg[:], sigData)
@@ -58,25 +57,12 @@ func VerifyIdentity(challenge []byte, tlvData []byte) ([]byte, error) {
5857
return nil, err
5958
}
6059

61-
sig := append(r, s...)
60+
// TODO: investigate why verify signature fails but recovery works
61+
_, err = calculateV(challenge, cert.identPub, r, s)
6262

63-
if !crypto.VerifySignature(cert.identPub, challenge, sig) {
63+
if err != nil {
6464
return nil, errors.New("invalid signature")
6565
}
6666

6767
return compressPublicKey(cert.signature.pubKey), nil
6868
}
69-
70-
func compressPublicKey(pubKey []byte) []byte {
71-
if len(pubKey) == 33 {
72-
return pubKey
73-
}
74-
75-
if (pubKey[63] & 1) == 1 {
76-
pubKey[0] = 3
77-
} else {
78-
pubKey[0] = 2
79-
}
80-
81-
return pubKey[0:33]
82-
}

types/certificate_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package types
2+
3+
import (
4+
"encoding/hex"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func hexMustDecode(str string) []byte {
11+
out, _ := hex.DecodeString(str)
12+
return out
13+
}
14+
15+
func TestVerifyIdentity(t *testing.T) {
16+
challenge := hexMustDecode("63acd6e02a8b5783551ff2836a9cbdf237c115c3ff018b943f044e6a69b19fe7")
17+
response := hexMustDecode("a081ab8a620365c18485fe7018e11cb992011426803aa8e843c63aab9657aed7d3ee4b85a62a11188ada267db3312a84e1be27c01c736a89da7a1fe4f7e90ce297e74f00008e2bfdb06058374abfc1c026386d16ead7bbc19bc0645d2e7acf7b953169bbc1ac0130450220364c5ca937b7ca42861978f086d206cc569ef0bb2ea4c7de08929c2fcca7434d022100c87699ce4f977e6a7a4800343db9b6842b91ca873e56dfe3327d19a2d01af14e")
18+
expectedKey := hexMustDecode("02fc929321aa94fea085b166994aa66590116252cf0235a03accaa2c8ab4595de5")
19+
20+
pubkey, err := VerifyIdentity(challenge, response)
21+
assert.NoError(t, err)
22+
assert.Equal(t, expectedKey, pubkey)
23+
}

types/signature.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,28 @@ func calculateV(message, pubKey, r, s []byte) (v byte, err error) {
126126
return v, err
127127
}
128128

129+
if len(pubKey) == 33 {
130+
rec = compressPublicKey(rec)
131+
}
132+
129133
if bytes.Equal(pubKey, rec) {
130134
return v, nil
131135
}
132136
}
133137

134138
return v, err
135139
}
140+
141+
func compressPublicKey(pubKey []byte) []byte {
142+
if len(pubKey) == 33 {
143+
return pubKey
144+
}
145+
146+
if (pubKey[63] & 1) == 1 {
147+
pubKey[0] = 3
148+
} else {
149+
pubKey[0] = 2
150+
}
151+
152+
return pubKey[0:33]
153+
}

0 commit comments

Comments
 (0)