-
Notifications
You must be signed in to change notification settings - Fork 0
/
3_aesfuncs_test.go
244 lines (209 loc) · 5.58 KB
/
3_aesfuncs_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
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
package matasano_test
// TESTAES
import (
"crypto/aes"
"encoding/base64"
"encoding/hex"
"fmt"
"testing"
"bytes"
"github.com/nthapaliya/matasano"
"github.com/nthapaliya/matasano/utils"
)
const UNKNOWN = "Um9sbGluJyBpbiBteSA1LjA" +
"KV2l0aCBteSByYWctdG9wIG" +
"Rvd24gc28gbXkgaGFpciBjY" +
"W4gYmxvdwpUaGUgZ2lybGll" +
"cyBvbiBzdGFuZGJ5IHdhdml" +
"uZyBqdXN0IHRvIHNheSBoaQ" +
"pEaWQgeW91IHN0b3A/IE5vL" +
"CBJIGp1c3QgZHJvdmUgYnkK"
// Challenge 7
func TestAESDecryptECB(t *testing.T) {
filename := "txt/aesData.txt"
s, err := utils.ReadAll(filename, "")
if err != nil {
t.Error(err)
}
buff, err := base64.StdEncoding.DecodeString(s)
if err != nil {
t.Error(err)
}
err = matasano.AESDecryptECB(buff, []byte("YELLOW SUBMARINE"))
if err != nil || buff == nil {
t.Error(err)
}
logpass(t, 6)
}
// Challenge 8
func TestAESDetectECB(t *testing.T) {
filename := "txt/detectAes.txt"
strings, err := utils.ReadLines(filename)
if err != nil {
t.Error(err)
}
detected := false
for _, s := range strings {
buff, _ := hex.DecodeString(s) // ignore error for now
if matasano.AESDetectECB(buff) {
detected = true
}
}
if !detected {
t.Errorf("Did not detect ecb string")
}
logpass(t, 7)
}
func TestAESEncryptECB(t *testing.T) {
filename := "txt/aesData.txt"
s, err := utils.ReadAll(filename, "")
if err != nil {
t.Error(err)
}
buff, err := base64.StdEncoding.DecodeString(s)
if err != nil {
t.Error(err)
}
originalbuff := make([]byte, len(buff))
copy(originalbuff, buff)
err = matasano.AESDecryptECB(buff, []byte("YELLOW SUBMARINE"))
if err != nil || buff == nil {
t.Error(err)
}
err = matasano.AESEncryptECB(buff, []byte("YELLOW SUBMARINE"))
if err != nil || buff == nil {
t.Error(err)
}
if bytes.Compare(originalbuff, buff) != 0 {
fmt.Printf("original buff \n%s\ngot\n%s\n", string(originalbuff), string(buff))
t.Errorf("decryption did not work")
}
}
// Challenge 10
func TestAESDecryptCBC(t *testing.T) {
s, err := utils.ReadAll("txt/10.txt", "")
if err != nil {
t.Error(err)
}
buff, err := base64.StdEncoding.DecodeString(s)
if err != nil {
t.Error(err)
}
key := []byte("YELLOW SUBMARINE")
iv := []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
if err := matasano.AESDecryptCBC(buff, key, iv); err != nil {
t.Error(err)
}
logpass(t, 9)
}
func TestAESEncryptCBC(t *testing.T) {
s, err := utils.ReadAll("txt/10.txt", "")
if err != nil {
t.Error(err)
}
buff, err := base64.StdEncoding.DecodeString(s)
if err != nil {
t.Error(err)
}
originalbuff := make([]byte, len(buff))
copy(originalbuff, buff)
key := []byte("YELLOW SUBMARINE")
iv := []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
if err := matasano.AESDecryptCBC(buff, key, iv); err != nil {
t.Error(err)
}
if err := matasano.AESEncryptCBC(buff, key, iv); err != nil {
t.Error(err)
}
if bytes.Compare(buff, originalbuff) != 0 {
t.Errorf("decoding CBC did not work")
}
}
func _TestGenRandomKey(t *testing.T) {
out := [][]byte{
[]byte{7, 56, 216, 204, 46, 140, 253, 81, 248, 93, 25, 198, 65, 60, 123, 44},
[]byte{92, 50, 159, 83, 147, 170, 26, 120, 29, 113, 179, 20, 59, 87, 99, 122},
[]byte{137, 17, 161, 118, 24, 190, 35, 126, 76, 134, 68, 26, 3, 13, 191, 208},
[]byte{105, 110, 32, 183, 95, 199, 113, 21, 46, 109, 185, 66, 216, 156, 133, 230},
[]byte{199, 10, 226, 31, 6, 31, 76, 122, 26, 88, 240, 154, 243, 78, 167, 247},
[]byte{27, 84, 128, 83, 199, 219, 23, 250, 50, 110, 97, 178, 53, 10, 240, 8},
}
// NOTE: The above values only work for rnd = rnd.New(rand.NewSource(100.0))
// ie the seed has to be 100.0 otherwise it won't work.
// Consider making this function non-exported
for i := 0; i < 6; i++ {
b := matasano.GenRandBuffer(16)
if bytes.Compare(b, out[i]) != 0 {
t.Errorf("genrandkey produces wrong output")
}
}
}
func TestOracle(t *testing.T) {
s := "this is a stupid string that I just typed out"
buff := []byte(s)
out, err := matasano.Oracle(buff)
if err != nil || out == nil {
t.Errorf("oracle did not work")
}
}
// Challenge 11
func TestDetection(t *testing.T) {
s := "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
buff := []byte(s)
for i := 0; i < 100; i++ {
out, err := matasano.Oracle(buff)
if err != nil || out == nil {
t.Errorf("oracle did not work")
}
if matasano.AESDetectECB(out) {
logpass(t, 10)
return
}
}
// if they get here, it's a problem
t.Fail()
}
func TestECBOracle(t *testing.T) {
unknown, err := base64.StdEncoding.DecodeString(UNKNOWN)
if err != nil {
t.Error(err)
}
in := []byte("AAAAAAAAAAAAAAAAAAAAAAAAA")
if out, err := matasano.ECBOracle(in, unknown); err != nil {
t.Error(err)
} else if len(out)%aes.BlockSize != 0 {
t.Errorf("something went wrong with oracle")
}
}
// Challenge 12
func TestAESBreakECBEncryption(t *testing.T) {
unknown, err := base64.StdEncoding.DecodeString(UNKNOWN)
if err != nil {
t.Error(err)
}
out := matasano.AESBreakECBEncryption(unknown)
if s := string(out); bytes.Compare(out, unknown) != 0 {
t.Errorf("breaking ECB failed, got %s", s)
}
logpass(t, 11)
}
func TestProfileOracle(t *testing.T) {
_, err := matasano.ProfileOracle("[email protected]")
if err != nil {
t.Error(err)
}
}
func TestEncryptingProfiles(t *testing.T) {
email := "[email protected]"
key := matasano.GenRandBuffer(aes.BlockSize)
p := matasano.ProfileFor(email) // string
profile := []byte(p)
buff := append([]byte{}, profile...) // copy profile to new slice buff
matasano.AESEncryptECB(buff, key)
matasano.AESDecryptECB(buff, key)
if bytes.Compare(buff, profile) != 0 {
t.Errorf("error in decrypting profile")
}
}
func TestBreakProfileEncryption(t *testing.T) {
}