-
Notifications
You must be signed in to change notification settings - Fork 2
/
challenge_keys_test.go
executable file
·160 lines (130 loc) · 4.64 KB
/
challenge_keys_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package libwallet
import (
"reflect"
"testing"
"github.com/btcsuite/btcutil/base58"
)
func TestNewChallengePrivateKey(t *testing.T) {
type args struct {
input []byte
salt []byte
}
tests := []struct {
name string
args args
want *ChallengePrivateKey
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewChallengePrivateKey(tt.args.input, tt.args.salt); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewChallengePrivateKey() = %v, want %v", got, tt.want)
}
})
}
}
func TestChallengeKeySignSha(t *testing.T) {
input := randomBytes(32)
salt := randomBytes(32)
challengePrivKey := NewChallengePrivateKey(input, salt)
payload := []byte("foobar")
_, err := challengePrivKey.SignSha(payload)
if err != nil {
t.Fatal(err)
}
// TODO(federicobond): assert that signature verifies
}
func TestChallengeKeyCrypto(t *testing.T) {
const birthday = 376
network := Regtest()
salt := randomBytes(8)
privKey, _ := NewHDPrivateKey(randomBytes(32), network)
challengePrivKey := NewChallengePrivateKey([]byte("a very good password"), salt)
encryptedKey, err := challengePrivKey.PubKey().EncryptKey(privKey, salt, birthday)
if err != nil {
t.Fatal(err)
}
decryptedKey, err := challengePrivKey.DecryptRawKey(encryptedKey, network)
if err != nil {
t.Fatal(err)
}
if privKey.String() != decryptedKey.Key.String() {
t.Fatalf("keys dont match: orig %v vs decrypted %v", privKey.String(), decryptedKey.Key.String())
}
if birthday != decryptedKey.Birthday {
t.Fatalf("birthdays dont match: expected %v got %v", birthday, decryptedKey.Birthday)
}
}
func TestChallengeKeyCryptoV2(t *testing.T) {
const (
encodedKey = "tprv8ZgxMBicQKsPcxg1GFGZgL5zALjPwijrYNUqTi2s9JsVqDLzbpX55U9JH2PKAQKExtpdTyboZmV2ytaqr9pAHuxE1hX8k9bQgZAjq25E6P7"
encryptedKey = "4LbSKwcepbbx4dPetoxvTWszb6mLyJHFhumzmdPRVprbn8XZBvFa6Ffarm6R3WGKutFzdxxJgQDdSHuYdjhDp1EZfSNbj12gXMND1AgmNijSxEua3LwVURU3nzWsvV5b1AsWEjJca24CaFY6T3C"
password = "a very good password"
saltLength = 8
birthday = 376
)
extractSalt := func(rawKey string) []byte {
bytes := base58.Decode(rawKey)
return bytes[len(bytes)-saltLength:]
}
challengeKey := NewChallengePrivateKey([]byte(password), extractSalt(encryptedKey))
decryptedKey, err := challengeKey.DecryptRawKey(encryptedKey, Regtest())
if err != nil {
t.Fatal(err)
}
if decryptedKey.Birthday != birthday {
t.Fatalf("decrypted birthday %v differs from expected %v", decryptedKey.Birthday, birthday)
}
if decryptedKey.Key.String() != encodedKey {
t.Fatalf("key doesnt match\ngot %v\nexpected %v\n", decryptedKey.Key.String(), encodedKey)
}
_, err = challengeKey.PubKey().EncryptKey(decryptedKey.Key, extractSalt(encryptedKey), birthday)
if err != nil {
t.Fatal(err)
}
}
func TestDecodeKeyWithOrWithoutSalt(t *testing.T) {
const (
// The same encoded key, with one version missing the salt field:
saltedKey = "4LbSKwcepbbx4dPetoxvTWszb6mLyJHFhumzmdPRVprbn8XZBvFa6Ffarm6R3WGKutFzdxxJgQDdSHuYdjhDp1EZfSNbj12gXMND1AgmNijSxEua3LwVURU3nzWsvV5b1AsWEjJca24CaFY6T3C"
unsaltedKey = "5XEEts6mc9WV34krDWsqmpLcPCw2JkK8qJu3gFdZpP8ngkERuQEsaDvYrGkhXUpM6jQRtimTYm4XnBPujpo3MsdYBedsNVxvT3WC6uCCFuzNUZCoydVY39yJXbxva7naDxH5iTra"
)
expected := &EncryptedPrivateKeyInfo{
Version: 2,
Birthday: 376,
CipherText: "f6af1ecd17052a81b75902c1712567cf1c650329875feb7e24af3e27235f384054ea549025e99dc2659f95bb6447cf861aa2ec0407ea74baf5a9d6a885ae184b",
EphPublicKey: "020a8d322dda8ff685d80b16681d4e87c109664cdc246a9d3625adfe0de203e71e",
Salt: "e3305526d0cd675f",
}
// Verify the salted version:
actual, err := DecodeEncryptedPrivateKey(saltedKey)
if err != nil {
t.Fatal(err)
}
assertDecodedKeysEqual(t, actual, expected)
// Verify the unsalted version:
actual, err = DecodeEncryptedPrivateKey(unsaltedKey)
if err != nil {
t.Fatal(err)
}
expected.Salt = "0000000000000000" // unsalted key should decode with zeroed field
assertDecodedKeysEqual(t, actual, expected)
}
func assertDecodedKeysEqual(t *testing.T, actual, expected *EncryptedPrivateKeyInfo) {
if actual.Version != expected.Version {
t.Fatalf("version %v expected %v", actual.Version, expected.Version)
}
if actual.Birthday != expected.Birthday {
t.Fatalf("birthday %v, expected %v", actual.Birthday, expected.Birthday)
}
if actual.CipherText != expected.CipherText {
t.Fatalf("cipherText %x expected %x", actual.CipherText, expected.CipherText)
}
if actual.EphPublicKey != expected.EphPublicKey {
t.Fatalf("ephPublicKey %x expected %x", actual.EphPublicKey, expected.EphPublicKey)
}
if actual.Salt != expected.Salt {
t.Fatalf("salt %x expected %x", actual.Salt, expected.Salt)
}
}