-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_2_test.go
78 lines (65 loc) · 1.74 KB
/
set_2_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
package cryptopals
import (
"bufio"
"bytes"
"io/ioutil"
"os"
"path"
"testing"
)
func TestChallenge9(t *testing.T) {
padded := PKCS7Pad([]byte("YELLOW SUBMARINE"), 20)
if bytes.Compare(padded, []byte("YELLOW SUBMARINE\x04\x04\x04\x04")) != 0 {
t.Fatal("padded text does not match solution")
}
}
func TestChallenge10(t *testing.T) {
inputFile, err := os.Open(path.Join(resourcesPath, "input_10.txt"))
if err != nil {
t.Fatal(err)
}
contents := ""
scanner := bufio.NewScanner(inputFile)
for scanner.Scan() {
contents += scanner.Text()
}
ciphertext := MustBase64Decode(contents)
plaintext := AESCBCDecrypt(ciphertext, []byte("YELLOW SUBMARINE"), bytes.Repeat([]byte{byte(0)}, 16))
plaintext = PKCS7Unpad(plaintext)
output, err := ioutil.ReadFile(path.Join(resourcesPath, "output_10.txt"))
if err != nil {
t.Fatal(err)
}
if bytes.Compare(plaintext, output) != 0 {
t.Fatal("decrypted plaintext does not match solution")
}
}
func TestChallenge11(t *testing.T) {
key := RandomBytes(16)
data := RandomBytes(128)
iv := make([]byte, 16)
var cbc bool
for i := 0; i < 20; i++ {
head := bytes.Repeat([]byte{byte(0)}, RandomInt(5, 10))
tail := bytes.Repeat([]byte{byte(0)}, RandomInt(5, 10))
plaintext := append(head, data...)
plaintext = append(plaintext, tail...)
ciphertext := make([]byte, len(plaintext))
if RandomBool() {
cbc = true
ciphertext = AESCBCEncrypt(plaintext, key, iv)
} else {
cbc = false
ciphertext = AESECBEncrypt(plaintext, key)
}
if bytes.Compare(ciphertext[32:48], MustAESEncryptBlock(plaintext[32:48], key)) == 0 {
if cbc {
t.Fatal("ciphertext incorrectly identified as AES ECB")
}
} else {
if !cbc {
t.Fatal("ciphertext incorrectly identified as AES CBC")
}
}
}
}