-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto_test.go
140 lines (129 loc) · 10.4 KB
/
crypto_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package gopace
import (
"bytes"
"testing"
)
// canTest is the CAN of the card where APDU traces were captured
var canTest []byte = []byte("050746")
func TestDecryptNonce(t *testing.T) {
encNonce := []byte{0x4d, 0x82, 0x55, 0x19, 0xe5, 0x61, 0xfc, 0xa5, 0xad, 0xcd, 0xb2, 0x72, 0xd9, 0x19, 0x5b, 0x22, 0x9d, 0xa8, 0x3f, 0x21, 0x0f, 0x04, 0xab, 0xe7, 0x31, 0x0c, 0xda, 0xf8, 0x4b, 0xbb, 0x24, 0x5e}
decNonce, err := DecryptNonce(canTest, encNonce)
if err != nil {
t.Fatalf("Decrypting nonce failed: %v", err)
}
expected := []byte{0xb3, 0x07, 0xb4, 0x76, 0xaf, 0xf7, 0x91, 0x28, 0x77, 0x8b, 0xc4, 0x00, 0x93, 0x9d, 0x95, 0x07, 0xd0, 0x34, 0x40, 0x88, 0x93, 0x7e, 0xe4, 0x7d, 0xcf, 0xdc, 0x86, 0xd9, 0xe5, 0xd0, 0xda, 0xbb}
if !bytes.Equal(decNonce, expected) {
t.Errorf("Expected: %x, got: %x", expected, decNonce)
}
}
func TestTerminalKeyMap(t *testing.T) {
sk := []byte{0x1f, 0x5e, 0x08, 0xde, 0xce, 0x8c, 0xd7, 0x45, 0xdf, 0xf7, 0x68, 0x63, 0x57, 0xe9, 0x92, 0x36, 0x68, 0x51, 0xe1, 0x23, 0xed, 0x80, 0xd8, 0x92, 0x24, 0x62, 0x0c, 0xe6, 0xe4, 0x51, 0xf4, 0xfb}
// rnd := bytes.NewBuffer(skbuf)
x, y, err := TerminalKeyMap(sk)
if err != nil {
t.Fatalf("Terminal key mapping failed: %v", err)
}
expectedX := []byte{0x8b, 0x50, 0x75, 0x6d, 0xe0, 0x11, 0xb9, 0xc6, 0xc7, 0xf5, 0x3e, 0x31, 0xae, 0x36, 0x08, 0x21, 0x01, 0x68, 0x7f, 0x8d, 0xed, 0xef, 0x7c, 0x19, 0x3f, 0xef, 0x36, 0x54, 0x52, 0xfb, 0x92, 0x2b}
expectedY := []byte{0x82, 0xcb, 0x18, 0xdc, 0x26, 0xcf, 0xd3, 0x3e, 0xa1, 0x74, 0x34, 0xc2, 0xc1, 0x7b, 0x7e, 0xec, 0x4d, 0x06, 0xbf, 0xdc, 0xf5, 0x6c, 0xd2, 0x4d, 0xdb, 0xf6, 0xa6, 0x6e, 0x47, 0xf9, 0x4b, 0xdd}
if !bytes.Equal(x, expectedX) {
t.Errorf("Expected X: %x, got: %x", expectedX, x)
}
if !bytes.Equal(y, expectedY) {
t.Errorf("Expected Y: %x, got: %x", expectedY, y)
}
}
func TestSharedPoint(t *testing.T) {
sk := []byte{0x1f, 0x5e, 0x08, 0xde, 0xce, 0x8c, 0xd7, 0x45, 0xdf, 0xf7, 0x68, 0x63, 0x57, 0xe9, 0x92, 0x36, 0x68, 0x51, 0xe1, 0x23, 0xed, 0x80, 0xd8, 0x92, 0x24, 0x62, 0x0c, 0xe6, 0xe4, 0x51, 0xf4, 0xfb}
x := []byte{0x3b, 0xe5, 0xfb, 0xb9, 0x70, 0xf3, 0x30, 0x08, 0x57, 0x5f, 0xd3, 0x5c, 0xc4, 0xa2, 0xd4, 0xc1, 0x05, 0xbc, 0x08, 0x7e, 0x7f, 0xca, 0xe1, 0x29, 0x21, 0x28, 0xe6, 0xdc, 0xdc, 0xbe, 0xa2, 0xd4}
y := []byte{0x1d, 0x96, 0xb2, 0x79, 0x8e, 0x4e, 0x5f, 0x12, 0x6a, 0x1c, 0x61, 0x31, 0xcf, 0x09, 0x2a, 0x75, 0xdf, 0x0a, 0x16, 0xa7, 0xe0, 0xdc, 0x58, 0x46, 0xe5, 0xfd, 0x2a, 0x95, 0xe6, 0xf0, 0x28, 0xc7}
expectedX := []byte{0x0f, 0xdf, 0x01, 0xba, 0xce, 0x05, 0xd8, 0xee, 0xea, 0x14, 0x61, 0xa2, 0x0c, 0xeb, 0xc5, 0x84, 0xf4, 0xbe, 0x25, 0x3d, 0x89, 0x1e, 0xd1, 0x39, 0x52, 0xf7, 0xa4, 0x26, 0x19, 0xdc, 0xa9, 0x22}
expectedY := []byte{0x63, 0xcc, 0xa5, 0x40, 0xbd, 0x95, 0xe2, 0x20, 0x50, 0x50, 0x72, 0x54, 0x47, 0xea, 0xaf, 0x4b, 0x2f, 0xad, 0xbe, 0x64, 0x32, 0x73, 0xc1, 0x3a, 0x81, 0xe7, 0x68, 0xce, 0xd8, 0x66, 0x9b, 0x71}
sx, sy := SharedPoint(x, y, sk)
if !bytes.Equal(sx, expectedX) {
t.Errorf("Expected X: %x, got: %x", expectedX, sx)
}
if !bytes.Equal(sy, expectedY) {
t.Errorf("Expected Y: %x, got: %x", expectedY, sy)
}
}
func TestMappedPoint(t *testing.T) {
decNonce := []byte{0xb3, 0x07, 0xb4, 0x76, 0xaf, 0xf7, 0x91, 0x28, 0x77, 0x8b, 0xc4, 0x00, 0x93, 0x9d, 0x95, 0x07, 0xd0, 0x34, 0x40, 0x88, 0x93, 0x7e, 0xe4, 0x7d, 0xcf, 0xdc, 0x86, 0xd9, 0xe5, 0xd0, 0xda, 0xbb}
sx := []byte{0x0f, 0xdf, 0x01, 0xba, 0xce, 0x05, 0xd8, 0xee, 0xea, 0x14, 0x61, 0xa2, 0x0c, 0xeb, 0xc5, 0x84, 0xf4, 0xbe, 0x25, 0x3d, 0x89, 0x1e, 0xd1, 0x39, 0x52, 0xf7, 0xa4, 0x26, 0x19, 0xdc, 0xa9, 0x22}
sy := []byte{0x63, 0xcc, 0xa5, 0x40, 0xbd, 0x95, 0xe2, 0x20, 0x50, 0x50, 0x72, 0x54, 0x47, 0xea, 0xaf, 0x4b, 0x2f, 0xad, 0xbe, 0x64, 0x32, 0x73, 0xc1, 0x3a, 0x81, 0xe7, 0x68, 0xce, 0xd8, 0x66, 0x9b, 0x71}
mx, my := MappedBasePoint(decNonce, sx, sy)
expectedX := []byte{0x6b, 0x55, 0x81, 0x8e, 0x89, 0xbb, 0x96, 0xfd, 0xa3, 0x2f, 0xaa, 0x31, 0x02, 0x12, 0xe9, 0x2b, 0x07, 0x53, 0xa6, 0x03, 0x1b, 0x79, 0x76, 0x97, 0xe9, 0x7c, 0xa0, 0x75, 0x48, 0xde, 0xb0, 0x86}
expectedY := []byte{0x9d, 0x71, 0xe8, 0xa3, 0x63, 0x4c, 0x7e, 0xdc, 0xcc, 0x13, 0x4a, 0xc9, 0x27, 0x25, 0x67, 0x7f, 0x07, 0x38, 0xca, 0x88, 0x68, 0x8f, 0xb3, 0x75, 0x3d, 0x98, 0xd2, 0x19, 0x6d, 0xa0, 0x52, 0xf0}
if !bytes.Equal(mx, expectedX) {
t.Errorf("Expected X: %x, got: %x", expectedX, mx)
}
if !bytes.Equal(my, expectedY) {
t.Errorf("Expected Y: %x, got: %x", expectedY, my)
}
}
func TestMappedPublicKey(t *testing.T) {
sk2 := []byte{0x27, 0xf9, 0xa7, 0xa2, 0xa4, 0xc8, 0x17, 0xa5, 0x95, 0xa3, 0x21, 0x91, 0x06, 0xce, 0xdd, 0xb1, 0x72, 0xb5, 0xd4, 0x7f, 0xa6, 0x90, 0x32, 0x00, 0x88, 0xff, 0x85, 0x57, 0x73, 0x97, 0x6e, 0xdb}
mx := []byte{0x6b, 0x55, 0x81, 0x8e, 0x89, 0xbb, 0x96, 0xfd, 0xa3, 0x2f, 0xaa, 0x31, 0x02, 0x12, 0xe9, 0x2b, 0x07, 0x53, 0xa6, 0x03, 0x1b, 0x79, 0x76, 0x97, 0xe9, 0x7c, 0xa0, 0x75, 0x48, 0xde, 0xb0, 0x86}
my := []byte{0x9d, 0x71, 0xe8, 0xa3, 0x63, 0x4c, 0x7e, 0xdc, 0xcc, 0x13, 0x4a, 0xc9, 0x27, 0x25, 0x67, 0x7f, 0x07, 0x38, 0xca, 0x88, 0x68, 0x8f, 0xb3, 0x75, 0x3d, 0x98, 0xd2, 0x19, 0x6d, 0xa0, 0x52, 0xf0}
msx, msy := MappedPublicKey(sk2, mx, my)
expectedX := []byte{0x0d, 0x79, 0x20, 0x45, 0x9a, 0xbc, 0x54, 0xda, 0x30, 0xa5, 0x04, 0x67, 0xf1, 0x81, 0x96, 0xa5, 0xad, 0x88, 0x27, 0xce, 0x11, 0x13, 0x95, 0x2d, 0x8c, 0x2f, 0x95, 0x94, 0xf6, 0xad, 0x9d, 0x0a}
expectedY := []byte{0x4c, 0xc1, 0x3f, 0x20, 0x1e, 0x1a, 0x8b, 0x2a, 0x16, 0x4b, 0x4b, 0xa0, 0x8d, 0x7f, 0x04, 0xf8, 0x7c, 0x76, 0xe0, 0x2c, 0x5a, 0x75, 0x8a, 0xdb, 0x72, 0x63, 0x8a, 0xdb, 0x47, 0xac, 0xd7, 0x2c}
if !bytes.Equal(msx, expectedX) {
t.Errorf("Expected X: %x, got: %x", expectedX, msx)
}
if !bytes.Equal(msy, expectedY) {
t.Errorf("Expected Y: %x, got: %x", expectedY, msy)
}
}
func TestMappedECDH(t *testing.T) {
sk2 := []byte{0x27, 0xf9, 0xa7, 0xa2, 0xa4, 0xc8, 0x17, 0xa5, 0x95, 0xa3, 0x21, 0x91, 0x06, 0xce, 0xdd, 0xb1, 0x72, 0xb5, 0xd4, 0x7f, 0xa6, 0x90, 0x32, 0x00, 0x88, 0xff, 0x85, 0x57, 0x73, 0x97, 0x6e, 0xdb}
msx := []byte{0x58, 0x24, 0x55, 0xf1, 0x49, 0xc2, 0xc7, 0x8f, 0xc1, 0x8c, 0x32, 0x70, 0xde, 0x86, 0xd9, 0x74, 0x7d, 0x44, 0x13, 0x3f, 0xf4, 0x43, 0x90, 0xb7, 0x9f, 0x44, 0x9d, 0xa2, 0xa0, 0x11, 0xb4, 0xed}
msy := []byte{0x36, 0x55, 0x8f, 0x11, 0xc6, 0xbc, 0x37, 0x2d, 0x5c, 0x40, 0xd1, 0xd3, 0xc0, 0xee, 0x48, 0x51, 0x79, 0xbc, 0x43, 0xa7, 0xdf, 0x5e, 0xd8, 0x02, 0xa1, 0x26, 0x86, 0xcb, 0x9b, 0xdd, 0x65, 0x6d}
smsx, _ := MappedECDH(sk2, msx, msy)
expectedX := []byte{0xe4, 0xd6, 0xff, 0xa1, 0x65, 0xa5, 0xc8, 0xf6, 0x9b, 0x8a, 0x55, 0x4a, 0xe8, 0xb4, 0x56, 0x77, 0x60, 0x23, 0x62, 0x6d, 0x63, 0xd7, 0xf5, 0x85, 0x1d, 0x0c, 0x79, 0x65, 0xac, 0x81, 0x01, 0x13}
if !bytes.Equal(smsx, expectedX) {
t.Errorf("Expected X: %x, got: %x", expectedX, msx)
}
}
func TestSessionKeys(t *testing.T) {
smsx := []byte{0xe4, 0xd6, 0xff, 0xa1, 0x65, 0xa5, 0xc8, 0xf6, 0x9b, 0x8a, 0x55, 0x4a, 0xe8, 0xb4, 0x56, 0x77, 0x60, 0x23, 0x62, 0x6d, 0x63, 0xd7, 0xf5, 0x85, 0x1d, 0x0c, 0x79, 0x65, 0xac, 0x81, 0x01, 0x13}
kenc, kmac := SessionKeys(smsx)
expectedKenc := []byte{0x1b, 0xfd, 0xd3, 0xd7, 0xf3, 0xa4, 0x5b, 0xab, 0x26, 0x60, 0xe8, 0x5d, 0xd1, 0x12, 0xd8, 0x9f, 0x98, 0x9b, 0x67, 0xdf, 0x1b, 0xc0, 0x03, 0xa0, 0x67, 0x22, 0xde, 0xa3, 0xe5, 0x5a, 0x2b, 0x79}
expectedKmac := []byte{0xa3, 0x71, 0xb2, 0x1c, 0x3f, 0x41, 0x9b, 0xb4, 0x82, 0x79, 0x37, 0x64, 0x64, 0xdb, 0x19, 0x6c, 0x53, 0xda, 0x86, 0x30, 0x39, 0x92, 0x74, 0xfe, 0x76, 0x9d, 0xd3, 0x63, 0xac, 0xe2, 0xf7, 0x82}
if !bytes.Equal(kenc, expectedKenc) {
t.Errorf("Expected X: %x, got: %x", expectedKenc, kenc)
}
if !bytes.Equal(kmac, expectedKmac) {
t.Errorf("Expected X: %x, got: %x", expectedKmac, kmac)
}
}
func TestComputeAuthenticationToken(t *testing.T) {
deroid := []byte{0x04, 0x00, 0x7f, 0x00, 0x07, 0x02, 0x02, 0x04, 0x02, 0x04}
x := []byte{0x58, 0x24, 0x55, 0xf1, 0x49, 0xc2, 0xc7, 0x8f, 0xc1, 0x8c, 0x32, 0x70, 0xde, 0x86, 0xd9, 0x74, 0x7d, 0x44, 0x13, 0x3f, 0xf4, 0x43, 0x90, 0xb7, 0x9f, 0x44, 0x9d, 0xa2, 0xa0, 0x11, 0xb4, 0xed}
y := []byte{0x36, 0x55, 0x8f, 0x11, 0xc6, 0xbc, 0x37, 0x2d, 0x5c, 0x40, 0xd1, 0xd3, 0xc0, 0xee, 0x48, 0x51, 0x79, 0xbc, 0x43, 0xa7, 0xdf, 0x5e, 0xd8, 0x02, 0xa1, 0x26, 0x86, 0xcb, 0x9b, 0xdd, 0x65, 0x6d}
tauth := ComputeAuthenticationToken(deroid, x, y)
expected := []byte{0x7f, 0x49, 0x4f, 0x06, 0x0a, 0x04, 0x00, 0x7f, 0x00, 0x07, 0x02, 0x02, 0x04, 0x02, 0x04, 0x86, 0x41, 0x04, 0x58, 0x24, 0x55, 0xf1, 0x49, 0xc2, 0xc7, 0x8f, 0xc1, 0x8c, 0x32, 0x70, 0xde, 0x86, 0xd9, 0x74, 0x7d, 0x44, 0x13, 0x3f, 0xf4, 0x43, 0x90, 0xb7, 0x9f, 0x44, 0x9d, 0xa2, 0xa0, 0x11, 0xb4, 0xed, 0x36, 0x55, 0x8f, 0x11, 0xc6, 0xbc, 0x37, 0x2d, 0x5c, 0x40, 0xd1, 0xd3, 0xc0, 0xee, 0x48, 0x51, 0x79, 0xbc, 0x43, 0xa7, 0xdf, 0x5e, 0xd8, 0x02, 0xa1, 0x26, 0x86, 0xcb, 0x9b, 0xdd, 0x65, 0x6d}
if !bytes.Equal(tauth, expected) {
t.Errorf("Expected %x got %x", expected, tauth)
}
}
func TestCMAC(t *testing.T) {
Kmac := []byte{0xa3, 0x71, 0xb2, 0x1c, 0x3f, 0x41, 0x9b, 0xb4, 0x82, 0x79, 0x37, 0x64, 0x64, 0xdb, 0x19, 0x6c, 0x53, 0xda, 0x86, 0x30, 0x39, 0x92, 0x74, 0xfe, 0x76, 0x9d, 0xd3, 0x63, 0xac, 0xe2, 0xf7, 0x82}
tauth := []byte{0x7f, 0x49, 0x4f, 0x06, 0x0a, 0x04, 0x00, 0x7f, 0x00, 0x07, 0x02, 0x02, 0x04, 0x02, 0x04, 0x86, 0x41, 0x04, 0x58, 0x24, 0x55, 0xf1, 0x49, 0xc2, 0xc7, 0x8f, 0xc1, 0x8c, 0x32, 0x70, 0xde, 0x86, 0xd9, 0x74, 0x7d, 0x44, 0x13, 0x3f, 0xf4, 0x43, 0x90, 0xb7, 0x9f, 0x44, 0x9d, 0xa2, 0xa0, 0x11, 0xb4, 0xed, 0x36, 0x55, 0x8f, 0x11, 0xc6, 0xbc, 0x37, 0x2d, 0x5c, 0x40, 0xd1, 0xd3, 0xc0, 0xee, 0x48, 0x51, 0x79, 0xbc, 0x43, 0xa7, 0xdf, 0x5e, 0xd8, 0x02, 0xa1, 0x26, 0x86, 0xcb, 0x9b, 0xdd, 0x65, 0x6d}
cauth := []byte{0x7f, 0x49, 0x4f, 0x06, 0x0a, 0x04, 0x00, 0x7f, 0x00, 0x07, 0x02, 0x02, 0x04, 0x02, 0x04, 0x86, 0x41, 0x04, 0x0d, 0x79, 0x20, 0x45, 0x9a, 0xbc, 0x54, 0xda, 0x30, 0xa5, 0x04, 0x67, 0xf1, 0x81, 0x96, 0xa5, 0xad, 0x88, 0x27, 0xce, 0x11, 0x13, 0x95, 0x2d, 0x8c, 0x2f, 0x95, 0x94, 0xf6, 0xad, 0x9d, 0x0a, 0x4c, 0xc1, 0x3f, 0x20, 0x1e, 0x1a, 0x8b, 0x2a, 0x16, 0x4b, 0x4b, 0xa0, 0x8d, 0x7f, 0x04, 0xf8, 0x7c, 0x76, 0xe0, 0x2c, 0x5a, 0x75, 0x8a, 0xdb, 0x72, 0x63, 0x8a, 0xdb, 0x47, 0xac, 0xd7, 0x2c}
tmac, err := CMAC(Kmac, tauth)
if err != nil {
t.Fatalf("Computation failed: %v", err)
}
cmac, err := CMAC(Kmac, cauth)
if err != nil {
t.Fatalf("Computation failed: %v", err)
}
expectedtmac := []byte{0xee, 0xbc, 0x2b, 0x60, 0xd4, 0xa4, 0x90, 0xd3}
expectedcmac := []byte{0xf7, 0xe5, 0x33, 0x6b, 0xdf, 0x05, 0xb9, 0x77}
if !bytes.Equal(tmac, expectedtmac) {
t.Errorf("Expected %x got %x", expectedtmac, tmac)
}
if !bytes.Equal(cmac, expectedcmac) {
t.Errorf("Expected %x got %x", expectedcmac, cmac)
}
}