-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaes_test.go
196 lines (176 loc) · 5.36 KB
/
aes_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
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
package encryption
import (
"math/big"
"testing"
"time"
"github.com/stretchr/testify/require"
)
const (
TestDenom = "factory/sei1239081236470/testToken"
TestKey = "examplekey12345678901234567890ab"
)
func TestGetAESKey(t *testing.T) {
tests := []struct {
name string
privateKey []byte
denom string
expectEqual bool
anotherKey []byte
anotherDenom string
}{
{
name: "Deterministic Key Generation",
privateKey: generateTestKey(),
expectEqual: true,
},
{
name: "Different PrivateKey Generates Different Key",
privateKey: generateTestKey(),
anotherKey: generateTestKey(),
expectEqual: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
aesPK, err := GetAESKey(tt.privateKey)
require.Nil(t, err, "Should not have error here")
if tt.anotherKey != nil {
aesPKDiff, err := GetAESKey(tt.anotherKey)
require.Nil(t, err)
require.NotEqual(t, aesPK, aesPKDiff, "PK should be different for different private keys")
} else {
aesPKAgain, err := GetAESKey(tt.privateKey)
require.Nil(t, err, "Should not have error here")
if tt.expectEqual {
require.Equal(t, aesPK, aesPKAgain, "PK should be deterministically generated")
} else {
require.NotEqual(t, aesPK, aesPKAgain, "PK should be different for different denoms")
}
}
})
}
}
func TestGetAESKey_InvalidInput(t *testing.T) {
// Nil private key
_, err := GetAESKey([]byte{})
require.Error(t, err, "Should return error for nil private key")
}
func TestAESEncryptionDecryption(t *testing.T) {
tests := []struct {
name string
key []byte
anotherKey []byte
value *big.Int
expectError bool
decryptWithKey []byte
encryptAgain bool
}{
{
name: "Successful Encryption and Decryption",
key: []byte(TestKey), // 32 bytes for AES-256
value: big.NewInt(3023),
expectError: false,
},
{
name: "Encryption Yields Different Ciphertext If Encrypted Again",
key: []byte(TestKey),
value: big.NewInt(3023),
encryptAgain: true,
expectError: false,
},
{
name: "Different Key Produces Different Ciphertext",
key: []byte(TestKey),
anotherKey: []byte("randomkey12345678901234567890abc"), // 32 bytes for AES-256
value: big.NewInt(3023),
expectError: false,
},
{
name: "Decryption with Wrong Key",
key: []byte(TestKey),
value: big.NewInt(3023),
expectError: true,
decryptWithKey: []byte("wrongkey12345678901234567890ab"),
},
{
name: "Edge Case: Zero Value",
key: []byte(TestKey),
value: big.NewInt(0),
expectError: false,
},
{
name: "Maximum Uint64",
key: []byte(TestKey),
value: new(big.Int).Exp(big.NewInt(2), big.NewInt(256), nil).Sub(new(big.Int).Exp(big.NewInt(2), big.NewInt(256), nil), big.NewInt(1)), // 2^256 - 1
expectError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
encrypted, err := EncryptAESGCM(tt.value, tt.key)
if tt.expectError && err != nil {
// Expected error during encryption
require.Error(t, err)
return
}
require.Nil(t, err, "Should have no error encrypting")
if tt.anotherKey != nil {
encryptedAgain, err := EncryptAESGCM(tt.value, tt.anotherKey)
require.Nil(t, err, "Should have no error encrypting with another key")
require.NotEqual(t, encrypted, encryptedAgain, "Ciphertext should differ on encryption with different key")
}
if tt.decryptWithKey != nil {
_, err := DecryptAESGCM(encrypted, tt.decryptWithKey)
require.Error(t, err, "Should have an error decrypting with wrong key")
return
}
decrypted, err := DecryptAESGCM(encrypted, tt.key)
if tt.expectError {
require.Empty(t, decrypted)
require.Error(t, err, "Should have an error decrypting with wrong key")
} else {
require.Nil(t, err, "Should have no error decrypting")
require.Equal(t, tt.value, decrypted)
}
// Additional checks for encryption consistency
if tt.encryptAgain {
encryptedAgain, err := EncryptAESGCM(tt.value, tt.key)
require.Nil(t, err, "Should have no error encrypting again")
require.NotEqual(t, encrypted, encryptedAgain, "Ciphertext should differ on re-encryption")
}
})
}
}
func TestEncryptAESGCM_InvalidKeyLength(t *testing.T) {
invalidKeys := [][]byte{
{}, // Empty key
[]byte("shortkey"), // Too short
[]byte("thiskeyiswaytoolongforaesgcm"), // Too long
}
value := big.NewInt(1234)
for _, key := range invalidKeys {
t.Run("InvalidKeyLength", func(t *testing.T) {
_, err := EncryptAESGCM(value, key)
require.Error(t, err, "Should return error for invalid key length")
})
}
}
func TestDecryptAESGCM_InvalidCiphertext(t *testing.T) {
key := []byte(TestKey)
invalidCiphertexts := [][]byte{
{}, // Empty ciphertext
[]byte("invalidciphertext"),
}
for _, ct := range invalidCiphertexts {
t.Run("InvalidCiphertext", func(t *testing.T) {
decrypted, err := DecryptAESGCM(string(ct), key)
require.Empty(t, decrypted)
require.Error(t, err, "Should return error for invalid ciphertext")
})
}
}
// Helper function to generate a test private key
func generateTestKey() []byte {
randomString := time.Now()
return []byte(randomString.String())
}