-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcipher_test.go
More file actions
285 lines (261 loc) · 9.03 KB
/
Copy pathcipher_test.go
File metadata and controls
285 lines (261 loc) · 9.03 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright (c) 2026 Satinderjit Singh
// SPDX-License-Identifier: MIT
package slip39
import (
"bytes"
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"testing"
"golang.org/x/crypto/pbkdf2"
)
// TestFeistelRoundTrip verifies encrypt then decrypt recovers the original
// for various secret sizes and parameters.
func TestFeistelRoundTrip(t *testing.T) {
testCases := []struct {
name string
secret string // hex
pass string
iterExp int
id int
extendable bool
}{
{"128-bit", "bb54aac4b89dc868ba37d9cc21b2cece", "TREZOR", 0, 7945, false},
{"256-bit", "bb54aac4b89dc868ba37d9cc21b2cecebb54aac4b89dc868ba37d9cc21b2cece", "TREZOR", 0, 7945, false},
{"128-bit ext", "bb54aac4b89dc868ba37d9cc21b2cece", "TREZOR", 0, 7945, true},
{"128-bit e=1", "bb54aac4b89dc868ba37d9cc21b2cece", "TREZOR", 1, 7945, false},
{"128-bit empty pass", "bb54aac4b89dc868ba37d9cc21b2cece", "", 0, 7945, false},
{"128-bit id=0", "bb54aac4b89dc868ba37d9cc21b2cece", "TREZOR", 0, 0, false},
{"all zeros 128", "00000000000000000000000000000000", "TREZOR", 0, 7945, false},
{"all FF 128", "ffffffffffffffffffffffffffffffff", "TREZOR", 0, 7945, false},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
secret, err := hex.DecodeString(tc.secret)
if err != nil {
t.Fatal(err)
}
enc := encrypt(secret, []byte(tc.pass), tc.iterExp, tc.id, tc.extendable)
dec := decrypt(enc, []byte(tc.pass), tc.iterExp, tc.id, tc.extendable)
if !bytes.Equal(dec, secret) {
t.Fatalf("round-trip failed:\n got: %x\n want: %x", dec, secret)
}
ZeroBytes(enc)
ZeroBytes(dec)
})
}
}
// TestFeistelPreservesLength verifies len(encrypt(s)) == len(s).
func TestFeistelPreservesLength(t *testing.T) {
for _, n := range []int{16, 18, 20, 24, 32, 48, 64} {
secret := make([]byte, n)
for i := range secret {
secret[i] = byte(i)
}
enc := encrypt(secret, []byte("TREZOR"), 0, 7945, false)
if len(enc) != n {
t.Fatalf("encrypt(%d bytes) returned %d bytes", n, len(enc))
}
ZeroBytes(enc)
}
}
// TestFeistelPythonVectors verifies against Python-generated intermediate vectors.
// Generated from trezor/python-shamir-mnemonic.
func TestFeistelPythonVectors(t *testing.T) {
vectors := []struct {
name string
plaintext string
passphrase string
iterExp int
id int
extendable bool
ciphertext string
}{
{
"V1: 128-bit e=0 id=7945 ext=false",
"bb54aac4b89dc868ba37d9cc21b2cece",
"TREZOR", 0, 7945, false,
"11bc609d21747c49ba78c0701293e417",
},
{
"V2: 256-bit e=0 id=7945 ext=false",
"bb54aac4b89dc868ba37d9cc21b2cecebb54aac4b89dc868ba37d9cc21b2cece",
"TREZOR", 0, 7945, false,
"bb55f4da1ca8a768605a379439c08827e02c5c8b3c99c4a1dbd2a3a1f30e0880",
},
{
"V3: 128-bit e=0 id=7945 ext=true",
"bb54aac4b89dc868ba37d9cc21b2cece",
"TREZOR", 0, 7945, true,
"4ed5aafca6faf2cbf90519a4d6f41191",
},
{
"V4: 128-bit e=1 id=7945 ext=false",
"bb54aac4b89dc868ba37d9cc21b2cece",
"TREZOR", 1, 7945, false,
"0399f6fbffcaab71479d89c53fb416a3",
},
{
"V5: 128-bit empty passphrase",
"bb54aac4b89dc868ba37d9cc21b2cece",
"", 0, 7945, false,
"d0815038ea5f892f45ca1591a669e8f9",
},
{
"V6: 128-bit id=0",
"bb54aac4b89dc868ba37d9cc21b2cece",
"TREZOR", 0, 0, false,
"2c7ce65b815b774dee4aad05877a016c",
},
}
for _, v := range vectors {
t.Run(v.name, func(t *testing.T) {
plaintext, _ := hex.DecodeString(v.plaintext)
expectedCT, _ := hex.DecodeString(v.ciphertext)
ct := encrypt(plaintext, []byte(v.passphrase), v.iterExp, v.id, v.extendable)
if !bytes.Equal(ct, expectedCT) {
t.Fatalf("encrypt mismatch:\n got: %x\n want: %x", ct, expectedCT)
}
// Verify round-trip.
pt := decrypt(ct, []byte(v.passphrase), v.iterExp, v.id, v.extendable)
if !bytes.Equal(pt, plaintext) {
t.Fatalf("decrypt mismatch:\n got: %x\n want: %x", pt, plaintext)
}
})
}
}
// TestGetSaltAntiTamper verifies getSalt output against Python-generated values.
func TestGetSaltAntiTamper(t *testing.T) {
tests := []struct {
id int
extendable bool
expected string // hex
}{
{7945, false, "7368616d69721f09"},
{7945, true, ""},
{0, false, "7368616d69720000"},
}
for _, tc := range tests {
salt := getSalt(tc.id, tc.extendable)
got := hex.EncodeToString(salt)
if got != tc.expected {
t.Fatalf("getSalt(%d, %v): got %s, want %s", tc.id, tc.extendable, got, tc.expected)
}
}
}
// TestPBKDF2SHA256KnownVectors verifies x/crypto/pbkdf2 with HMAC-SHA256
// against Python hashlib.pbkdf2_hmac output. Pins our dependency behavior.
func TestPBKDF2SHA256KnownVectors(t *testing.T) {
vectors := []struct {
password string
salt string
iterations int
dkLen int
expected string
}{
{"password", "salt", 1, 32, "120fb6cffcf8b32c43e7225256c4f837a86548c92ccc35480805987cb70be17b"},
{"password", "salt", 4096, 32, "c5e478d59288c841aa530db6845c4c8d962893a001ce4e11a4963873aa98134a"},
}
for _, v := range vectors {
dk := pbkdf2.Key([]byte(v.password), []byte(v.salt), v.iterations, v.dkLen, sha256.New)
expected, _ := hex.DecodeString(v.expected)
if !bytes.Equal(dk, expected) {
t.Fatalf("PBKDF2-SHA256(%q, %q, %d, %d):\n got: %x\n want: %x",
v.password, v.salt, v.iterations, v.dkLen, dk, expected)
}
}
}
// TestHMACArgumentOrderAntiTamper verifies the HMAC argument order in createDigest.
// key=randomPart, msg=sharedSecret. Swapping them produces a different digest.
func TestHMACArgumentOrderAntiTamper(t *testing.T) {
randomPart := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
secret := []byte{0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00}
// Correct order: key=randomPart, msg=secret.
digest := createDigest(randomPart, secret)
// Swapped order: key=secret, msg=randomPart.
swappedDigest := createDigest(secret, randomPart)
if bytes.Equal(digest, swappedDigest) {
t.Fatal("HMAC argument order doesn't matter for these inputs - test is invalid")
}
// Verify against independent HMAC computation using stdlib.
// This proves createDigest uses HMAC-SHA256(key=randomPart, msg=secret)[:4]
// and not the swapped argument order.
h := hmac.New(sha256.New, randomPart) // key = randomPart (explicit)
h.Write(secret) // msg = secret (explicit)
independentFull := h.Sum(nil)
if !bytes.Equal(digest, independentFull[:digestLengthBytes]) {
t.Fatalf("createDigest does not match independent HMAC computation:\n got: %x\n want: %x", digest, independentFull[:digestLengthBytes])
}
}
// TestFeistelCiphertextNotPlaintext verifies encrypt produces different output.
func TestFeistelCiphertextNotPlaintext(t *testing.T) {
secret := make([]byte, 16)
for i := range secret {
secret[i] = byte(i + 1)
}
ct := encrypt(secret, []byte("TREZOR"), 0, 7945, false)
if bytes.Equal(ct, secret) {
t.Fatal("ciphertext equals plaintext")
}
}
// TestFeistelWrongPassphraseDifferentResult verifies that different passphrases
// produce different ciphertexts (plausible deniability by design).
func TestFeistelWrongPassphraseDifferentResult(t *testing.T) {
secret, _ := hex.DecodeString("bb54aac4b89dc868ba37d9cc21b2cece")
ct1 := encrypt(secret, []byte("TREZOR"), 0, 7945, false)
ct2 := encrypt(secret, []byte("wrong"), 0, 7945, false)
if bytes.Equal(ct1, ct2) {
t.Fatal("different passphrases produced same ciphertext")
}
}
// TestFeistelNilPassphrase verifies nil passphrase behaves identically
// to empty passphrase (nil normalization).
func TestFeistelNilPassphrase(t *testing.T) {
secret, _ := hex.DecodeString("bb54aac4b89dc868ba37d9cc21b2cece")
ctNil := encrypt(secret, nil, 0, 7945, false)
ctEmpty := encrypt(secret, []byte{}, 0, 7945, false)
if !bytes.Equal(ctNil, ctEmpty) {
t.Fatalf("nil vs empty passphrase differ:\n nil: %x\n empty: %x", ctNil, ctEmpty)
}
// Verify decrypt also works with nil.
dec := decrypt(ctNil, nil, 0, 7945, false)
if !bytes.Equal(dec, secret) {
t.Fatalf("decrypt with nil passphrase failed:\n got: %x\n want: %x", dec, secret)
}
}
// BenchmarkFeistelEncrypt128 establishes the performance baseline for
// Feistel encryption of a 128-bit secret. PBKDF2 dominates runtime.
func BenchmarkFeistelEncrypt128(b *testing.B) {
secret, _ := hex.DecodeString("bb54aac4b89dc868ba37d9cc21b2cece")
pass := []byte("TREZOR")
b.ResetTimer()
for i := 0; i < b.N; i++ {
result := encrypt(secret, pass, 0, 7945, false)
ZeroBytes(result)
}
}
// BenchmarkFeistelEncrypt256 benchmarks 256-bit Feistel encryption.
func BenchmarkFeistelEncrypt256(b *testing.B) {
secret := make([]byte, 32)
for i := range secret {
secret[i] = byte(i)
}
pass := []byte("TREZOR")
b.ResetTimer()
for i := 0; i < b.N; i++ {
result := encrypt(secret, pass, 0, 7945, false)
ZeroBytes(result)
}
}
// BenchmarkShamirSplit3of5 benchmarks Shamir split for 3-of-5 with 128-bit secret.
func BenchmarkShamirSplit3of5(b *testing.B) {
secret, _ := hex.DecodeString("bb54aac4b89dc868ba37d9cc21b2cece")
b.ResetTimer()
for i := 0; i < b.N; i++ {
shares, _ := splitSecret(3, 5, secret, rand.Reader)
for _, s := range shares {
ZeroBytes(s.data)
}
}
}