-
Notifications
You must be signed in to change notification settings - Fork 2
/
aes.go
304 lines (247 loc) · 8.28 KB
/
aes.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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package cryptopals
/*
## Cryptopals Solutions by Mohit Muthanna Cheppudira 2020.
Implementation of AES encryption modes: ECB, CBC, CTR, along with some detection
and cracking code for cryptopals.
*/
import (
"bytes"
"crypto/aes"
"encoding/binary"
"fmt"
"log"
"math/rand"
)
// Encrypt plainText with key using Electronic Code Block (ECB) mode.
func encryptAESECB(plainText []byte, key []byte, blockSize int) ([]byte, error) {
cipherText := make([]byte, len(plainText))
cipher, err := aes.NewCipher(key)
if err != nil {
return nil, fmt.Errorf("could not initialize AES: %w", err)
}
if len(plainText)%blockSize > 0 {
log.Printf("WARN: plainText (%d) is not a multiple of blockSize (%d)", len(plainText), blockSize)
}
for i := 0; i < (len(plainText) / blockSize); i++ {
start := i * blockSize
end := (i + 1) * blockSize
cipher.Encrypt(cipherText[start:end], plainText[start:end])
}
return cipherText, nil
}
// Decrypt cipherText with key using Electronic Code Block (ECB) mode.
func decryptAESECB(cipherText []byte, key []byte, blockSize int) ([]byte, error) {
plainText := make([]byte, len(cipherText))
cipher, err := aes.NewCipher(key)
if err != nil {
return nil, fmt.Errorf("could not initialize AES: %w", err)
}
for i := 0; i < (len(plainText) / blockSize); i++ {
start := i * blockSize
end := (i + 1) * blockSize
cipher.Decrypt(plainText[start:end], cipherText[start:end])
}
return plainText, nil
}
// Encrypt plainText with key using Cipher Block Chaining (CBC) mode.
func encryptAESCBC(plainText []byte, key []byte, iv []byte) ([]byte, error) {
blockSize := 16
if len(key) < blockSize {
return nil, fmt.Errorf("key size must be %d", blockSize)
}
if len(iv) < blockSize {
return nil, fmt.Errorf("iv size must be %d", blockSize)
}
cipherText := make([]byte, len(plainText))
cipher, err := aes.NewCipher(key)
if err != nil {
return nil, fmt.Errorf("could not initialize AES: %w", err)
}
buffer := make([]byte, blockSize)
lastCipherText := make([]byte, blockSize)
copy(lastCipherText, iv)
for i := 0; i < (len(plainText) / blockSize); i++ {
start := i * blockSize
end := (i + 1) * blockSize
for j := 0; j < blockSize; j++ {
buffer[j] = lastCipherText[j] ^ plainText[start:end][j]
}
cipher.Encrypt(lastCipherText, buffer)
copy(cipherText[start:end], lastCipherText)
}
return cipherText, nil
}
// Decrypt cipherText with key using Cipher Block Chaining (CBC) mode.
func decryptAESCBC(cipherText []byte, key []byte, iv []byte) ([]byte, error) {
blockSize := 16
if len(key) < blockSize {
return nil, fmt.Errorf("key size must be %d", blockSize)
}
if len(iv) < blockSize {
return nil, fmt.Errorf("iv size must be %d", blockSize)
}
cipher, err := aes.NewCipher(key)
if err != nil {
return nil, fmt.Errorf("could not initialize AES: %w", err)
}
plainText := make([]byte, len(cipherText))
buffer := make([]byte, blockSize)
plainTextBuffer := make([]byte, blockSize)
lastCipherText := make([]byte, blockSize)
copy(lastCipherText, iv)
for i := 0; i < (len(plainText) / blockSize); i++ {
start := i * blockSize
end := (i + 1) * blockSize
cipher.Decrypt(buffer, cipherText[start:end])
for j := 0; j < blockSize; j++ {
plainTextBuffer[j] = lastCipherText[j] ^ buffer[j]
}
copy(plainText[start:end], plainTextBuffer)
copy(lastCipherText, cipherText[start:end])
}
return plainText, nil
}
// Encrypt plainText with key using Counter (CTR) mode.
func encryptAESCTR(plainText []byte, key []byte, nonce uint64) ([]byte, error) {
blockSize := 16
blockCount := uint64(0)
length := len(plainText)
cipher, err := aes.NewCipher(key)
if err != nil {
return nil, fmt.Errorf("could not initialize AES: %w", err)
}
// CTR mode does not need padding, but we add it anyway to simplify
// the loop below. The extra padding length is sliced off of the cipherText
// before returning.
plainText, err = padPKCS7ToBlockSize(plainText, blockSize)
if err != nil {
return nil, fmt.Errorf("couldn't pad plainText: %w", err)
}
cipherText := make([]byte, len(plainText))
ctr := make([]byte, 16)
keyStream := make([]byte, 16)
for i := 0; i < len(plainText); i += blockSize {
binary.LittleEndian.PutUint64(ctr[:8], nonce)
binary.LittleEndian.PutUint64(ctr[8:], blockCount)
cipher.Encrypt(keyStream, ctr)
for j := 0; j < blockSize; j++ {
cipherText[i+j] = plainText[i+j] ^ keyStream[j]
}
blockCount++
}
// Silce padding off of cipherText before returning
return cipherText[:length], nil
}
// Decrypt cipherText with key using Counter (CTR) mode.
func decryptAESCTR(cipherText []byte, key []byte, nonce uint64) ([]byte, error) {
// Turns out that decryption is simply the opposite of encryption.
return encryptAESCTR(cipherText, key, nonce)
}
// Encrypts plainText under an unknown key, using ECB 50% of the time and CBC (with a
// random IV) 50% of the time (randomly.)
func encryptAESRandom(plainText []byte) ([]byte, error) {
key := make([]byte, 16)
_, err := rand.Read(key)
if err != nil {
return nil, fmt.Errorf("Can't generate random key: %w", err)
}
iv := make([]byte, 16)
_, err = rand.Read(iv)
if err != nil {
return nil, fmt.Errorf("Can't generate random IV: %w", err)
}
beforeData := make([]byte, rand.Intn(5)+5)
_, err = rand.Read(beforeData)
if err != nil {
return nil, fmt.Errorf("Can't generate random prefix data: %w", err)
}
afterData := make([]byte, rand.Intn(5)+5)
_, err = rand.Read(key)
if err != nil {
return nil, fmt.Errorf("Can't generate random suffix data: %w", err)
}
newPlainText := append(append(beforeData, plainText...), afterData...)
toss := rand.Intn(2)
var cipherText []byte
if toss == 0 {
fmt.Println("ECB")
// Mode ECB
cipherText, err = encryptAESECB(newPlainText, key, 16)
if err != nil {
return nil, fmt.Errorf("Could not perform ECB encryption: %w", err)
}
} else {
fmt.Println("CBC")
// Mode CBC
cipherText, err = encryptAESCBC(newPlainText, key, iv)
if err != nil {
return nil, fmt.Errorf("Could not perform CBC encryption: %w", err)
}
}
return cipherText, nil
}
type encryptor func([]byte) ([]byte, error)
// This function determines if "func f encryptor" below is an ECB encryptor, and
// returns the ECB block size, if true. Expects that encryptor f uses a stable key
// and pads input.
func detectAESECB(f encryptor) (bool, int, error) {
plainText := []byte("0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE")
cipherText, err := f(plainText)
if err != nil {
return false, 0, fmt.Errorf("could not encrypt: %w", err)
}
length := len(cipherText)
newPlainText := []byte(plainText)
for blockSize := 1; blockSize <= 64; blockSize++ {
newPlainText = append([]byte("A"), newPlainText...)
newCipherText, err := f(newPlainText)
if err != nil {
return false, 0, fmt.Errorf("could not encrypt: %w", err)
}
if bytes.Equal(cipherText[length-blockSize:], newCipherText[len(newCipherText)-blockSize:]) {
return true, blockSize, nil
}
}
return false, 0, nil
}
// Perform byte-at-a-time cracking on ECB function "encrypt" up to
// maxLen bytes. maxLen must be a multiple of the block size.
func crackAESECB(encrypt encryptor, maxLen int) ([]byte, error) {
// Crack ECB byte-at-a-time
crackedSecret := []byte{}
// Allocate enough room to crack up to maxLen bytes
prefix := make([]byte, maxLen)
// Crack secret one byte at a time and stop when no more plainText
// can be recovered.
for match := true; match; {
match = false
if len(crackedSecret) > maxLen {
return crackedSecret, nil
}
// Prefix should be just one byte less than the length
prefixLen := maxLen - (len(crackedSecret) % maxLen) - 1
prefix = prefix[:prefixLen]
// Encrypt data prefixed by 1-fewer byte than needed
cipherPrefix, err := encrypt(prefix)
if err != nil {
return nil, fmt.Errorf("could not encrypt prefix: %w", err)
}
// Append what we've cracked so far
prefix = append(prefix, crackedSecret...)
// Lengthen prefix to make it maxLen bytes
prefix = append(prefix, '\x00')
for testByte := byte(0); testByte < 255; testByte++ {
prefix[maxLen-1] = testByte
cipherText, err := encrypt(prefix)
if err != nil {
return nil, fmt.Errorf("could not encrypt prefix: %w", err)
}
if bytes.Equal(cipherPrefix[:maxLen], cipherText[:maxLen]) {
crackedSecret = append(crackedSecret, testByte)
match = true
break
}
}
}
return crackedSecret, nil
}