-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge52.go
188 lines (170 loc) · 4.14 KB
/
challenge52.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
package set7
import (
"bytes"
"crypto/rand"
"fmt"
"sort"
"github.com/alokmenghrajani/go-cryptopals/utils"
"github.com/alokmenghrajani/go-cryptopals/utils/aes"
)
func Challenge52() {
utils.PrintTitle(7, 52)
fmt.Println("H1 is a 16-bit hash")
fmt.Println("demonstration of 32-way collision with ~1280 calls to H.")
msgs, cost := findMultiCollisions(5)
sort.Slice(msgs, func(i, j int) bool {
return bytes.Compare(msgs[i], msgs[j]) <= 0
})
for i := 0; i < len(msgs); i++ {
h1 := MD1(msgs[i])
fmt.Printf("%2d %s %s\n", i, utils.ByteSliceToHex(msgs[i]), utils.ByteSliceToHex(h1))
}
fmt.Printf("calls to H1: %d\n", cost)
fmt.Println()
fmt.Println("H2 is a 24-bit hash")
fmt.Println("demonstration of breaking H1(...) || H2(...)")
left, right, cost1, cost2 := findPairCollision()
h1 := MD1(left)
h2 := MD2(left)
fmt.Printf("%s %s %s\n", utils.ByteSliceToHex(left), utils.ByteSliceToHex(h1), utils.ByteSliceToHex(h2))
h1 = MD1(right)
h2 = MD2(right)
fmt.Printf("%s %s %s\n", utils.ByteSliceToHex(right), utils.ByteSliceToHex(h1), utils.ByteSliceToHex(h2))
fmt.Printf("calls to H1: %d\n", cost1)
fmt.Printf("calls to H2: %d\n", cost2)
}
func C1(buf []byte, key []byte) []byte {
aesKey := []byte{}
for i := 0; i < 8; i++ {
aesKey = append(aesKey, key...)
}
aesCipher := aes.NewAes(aesKey)
r := make([]byte, aes.BlockSize)
aesCipher.Encrypt(r, buf)
return r[0:2]
}
func MD1(msg []byte) []byte {
msg = utils.Pad(msg, aes.BlockSize)
h := []byte{0x35, 0xca}
for i := 0; i < len(msg); i += aes.BlockSize {
h = C1(msg[i:i+aes.BlockSize], h)
}
return h
}
func findCollision(state []byte) ([]byte, []byte, []byte, int) {
found := map[[2]byte][]byte{}
h := [2]byte{}
cost := 0
for {
msg := make([]byte, aes.BlockSize)
_, err := rand.Read(msg)
utils.PanicOnErr(err)
h1 := C1(msg, state)
cost++
copy(h[:], h1)
if v, ok := found[h]; ok {
if !bytes.Equal(v, msg) {
return v, msg, h1, cost
} else {
// It's more likely we have a logic bug than a collision between two 128-bit messages
panic("something is wrong")
}
}
found[h] = msg
}
}
type pair struct {
left []byte
right []byte
}
type multiCollisions struct {
pairs []pair
state []byte
totalCost int
}
func (mc *multiCollisions) increase() {
left, right, hash, cost := findCollision(mc.state)
mc.pairs = append(mc.pairs, pair{left: left, right: right})
mc.totalCost += cost
mc.state = hash
}
func (mc *multiCollisions) combine() [][]byte {
msgs := [][]byte{{}}
for i := 0; i < len(mc.pairs); i++ {
// double msgs
n := len(msgs)
for j := 0; j < n; j++ {
t := make([]byte, len(msgs[j]))
copy(t, msgs[j])
msgs = append(msgs, t)
}
// append left
for j := 0; j < n; j++ {
msgs[j] = append(msgs[j], mc.pairs[i].left...)
}
// append right
for j := n; j < 2*n; j++ {
msgs[j] = append(msgs[j], mc.pairs[i].right...)
}
}
return msgs
}
func findMultiCollisions(n int) ([][]byte, int) {
// find n pairs
mc := &multiCollisions{
pairs: []pair{},
state: []byte{0x35, 0xca},
totalCost: 0,
}
for i := 0; i < n; i++ {
mc.increase()
}
return mc.combine(), mc.totalCost
}
func C2(buf []byte, key []byte) []byte {
aesKey := []byte{}
aesKey = append(aesKey, key...)
aesKey = utils.Pad(aesKey, aes.BlockSize)
aesCipher := aes.NewAes(aesKey)
r := make([]byte, aes.BlockSize)
aesCipher.Encrypt(r, buf)
return r[0:3]
}
func MD2(msg []byte) []byte {
msg = utils.Pad(msg, aes.BlockSize)
h := []byte{0x35, 0xca, 0xe7}
for i := 0; i < len(msg); i += aes.BlockSize {
h = C2(msg[i:i+aes.BlockSize], h)
}
return h
}
func findPairCollision() ([]byte, []byte, int, int) {
mc := &multiCollisions{
pairs: []pair{},
state: []byte{0x35, 0xca},
totalCost: 0,
}
for {
mc.increase()
msgs := mc.combine()
left, right, ok, cost2 := checkCollisions(msgs)
if ok {
return left, right, mc.totalCost, cost2
}
}
}
func checkCollisions(msgs [][]byte) ([]byte, []byte, bool, int) {
m := map[[3]byte][]byte{}
cost := 0
for i := 0; i < len(msgs); i++ {
h := MD2(msgs[i])
h2 := [3]byte{}
copy(h2[:], h)
cost++
if v, ok := m[h2]; ok {
return v, msgs[i], true, cost
}
m[h2] = msgs[i]
}
return nil, nil, false, 0
}