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
|
// Copyright © 2021 rsiddharth <s@ricketyspace.net>
// SPDX-License-Identifier: ISC
package challenge
import (
"fmt"
"net/http"
"net/url"
"time"
"ricketyspace.net/cryptopals/lib"
)
func C32(serve bool) {
// In-Secure Byte Compare.
inSecureCompare := func(a, b []byte) bool {
for i := 0; i < len(a); i++ {
if len(b) <= i {
return false
}
if a[i] != b[i] {
return false
}
lib.Sleep(500000) // Sleep 0.5ms
}
return true
}
// HMAC HTTP Server.
httpServer := func() {
// Secret for HMAC
sec := lib.StrToBytes("Rae Street")
// Signature checkker.
sigCheck := func(f, s string) bool {
// Check signature.
h := lib.HmacSha1(sec, lib.StrToBytes(f))
if inSecureCompare(h, lib.HexStrToBytes(s)) {
return true
}
return false
}
// HTTP Handler.
http.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
// Read query.
var f, s []string
var ok bool
q := r.URL.Query()
if f, ok = q["file"]; !ok {
w.WriteHeader(400)
fmt.Fprintf(w, "file not specified\n")
return
}
if len(f) > 1 {
w.WriteHeader(400)
fmt.Fprintf(w, "Cannot specify more than one file\n")
return
}
if s, ok = q["signature"]; !ok {
w.WriteHeader(400)
fmt.Fprintf(w, "signature not specified\n")
return
}
if len(s) > 1 {
w.WriteHeader(400)
fmt.Fprintf(w,
"Cannot specify more than one signature\n")
return
}
if !sigCheck(f[0], s[0]) {
w.WriteHeader(500) // Signature check failed.
}
})
// Start HTTP Server
http.ListenAndServe(":9000", nil)
}
// Makes HTTP POST request to the server.
makeRequest := func(f, s string) int {
// Make HTTP request.
q := fmt.Sprintf("file=%s&signature=%s", f, s)
u, err := url.Parse("http://localhost:9000/test?" + q)
if err != nil {
fmt.Printf("url parse failed: %v\n", err)
return 0
}
res, err := http.Post(u.String(), "text/plain", nil)
if err != nil {
fmt.Printf("http post failed: %v\n", err)
return res.StatusCode
}
return res.StatusCode
}
// Crack signature for file "foo".
crack := func() {
stc := time.Now() // Start time of crack
f := "foo" // File
s := make([]byte, 20) // Signature
i := 0
g := byte(0) // Guess byte.
tl := int64(50 + 1000) // Time limit.
tries := 0
for {
st := time.Now()
sc := makeRequest(f, lib.BytesToHexStr(s))
et := time.Now().Sub(st) // Elapsed time.
if sc == 200 {
fmt.Printf("Signature Cracked: %x"+
" [tot. elapsed time: %v]\n",
s, time.Now().Sub(stc))
break
}
if et.Microseconds() >= tl {
if tries < 5 { // Try guess at least 5 times.
tries += 1
continue
}
fmt.Printf("Signature Cracked Status: %x"+
" [tot. elapsed time: %v]\n",
s, time.Now().Sub(stc))
tl = et.Microseconds() + 50 + 200
g = 0
i += 1
tries = 0
} else {
g += 1 // Change guess.
s[i] = g // Set guess.
tries = 0
}
}
}
if serve {
httpServer()
} else {
crack()
}
}
// Output:
//
// # Server:
// $ ./cryptopals -c 32 -s
// # Client:
// $ ./cryptopals -c 32
// Signature Cracked Status: 4400000000000000000000000000000000000000 [tot. elapsed time: 126.869763ms]
// Signature Cracked Status: 44ee000000000000000000000000000000000000 [tot. elapsed time: 5.146844063s]
// Signature Cracked Status: 44ee570000000000000000000000000000000000 [tot. elapsed time: 9.026893144s]
// Signature Cracked Status: 44ee574500000000000000000000000000000000 [tot. elapsed time: 13.646937167s]
// Signature Cracked Status: 44ee5745d3000000000000000000000000000000 [tot. elapsed time: 31.367079778s]
// Signature Cracked Status: 44ee5745d3240000000000000000000000000000 [tot. elapsed time: 35.807108716s]
// Signature Cracked Status: 44ee5745d324fd00000000000000000000000000 [tot. elapsed time: 1m8.247369106s]
// Signature Cracked Status: 44ee5745d324fda7000000000000000000000000 [tot. elapsed time: 1m32.867563308s]
// Signature Cracked Status: 44ee5745d324fda71e0000000000000000000000 [tot. elapsed time: 1m38.747613348s]
// Signature Cracked Status: 44ee5745d324fda71ec000000000000000000000 [tot. elapsed time: 2m15.067906592s]
// Signature Cracked Status: 44ee5745d324fda71ec0cb000000000000000000 [tot. elapsed time: 2m57.588228069s]
// Signature Cracked Status: 44ee5745d324fda71ec0cbdd0000000000000000 [tot. elapsed time: 3m49.198625184s]
// Signature Cracked Status: 44ee5745d324fda71ec0cbdd7e00000000000000 [tot. elapsed time: 4m20.99889498s]
// Signature Cracked Status: 44ee5745d324fda71ec0cbdd7e0c000000000000 [tot. elapsed time: 4m25.798911645s]
// Signature Cracked Status: 44ee5745d324fda71ec0cbdd7e0c620000000000 [tot. elapsed time: 4m55.039159294s]
// Signature Cracked Status: 44ee5745d324fda71ec0cbdd7e0c621c00000000 [tot. elapsed time: 5m5.359239694s]
// Signature Cracked Status: 44ee5745d324fda71ec0cbdd7e0c621c70000000 [tot. elapsed time: 5m43.219684819s]
// Signature Cracked Status: 44ee5745d324fda71ec0cbdd7e0c621c70ba0000 [tot. elapsed time: 6m49.610083935s]
// Signature Cracked Status: 44ee5745d324fda71ec0cbdd7e0c621c70bae700 [tot. elapsed time: 8m15.280753257s]
// Signature Cracked: 44ee5745d324fda71ec0cbdd7e0c621c70bae785 [tot. elapsed time: 9m6.221153561s]
|