-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmd5_reuse.expc
253 lines (197 loc) · 7.14 KB
/
md5_reuse.expc
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
haskell {
import Data.List as L
start :: Hash
start = (0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476)
zero_hash :: Hash
zero_hash = (0, 0, 0, 0)
start_hash :: Hash
start_hash = (1732584193,4023233417,2562383102,271733878)
-- a message of four zeroes
zero_msg :: Vec 16 UInt
zero_msg = (0:>0:>0:>0:>128:>0:>0:>0:>0:>0:>0:>0:>0:>0:>0:>4:>Nil)
hash_a (a,_,_,_) = a
hash_b (_,b,_,_) = b
hash_c (_,_,c,_) = c
hash_d (_,_,_,d) = d
data MD5State = Loading (Unsigned 2) | Hashing (Unsigned 6) deriving (Show, Generic, NFDataX)
start_state = Loading 0
add_hash :: Hash -> Hash -> Hash
add_hash (a,b,c,d) (a',b',c',d') = (a+a', b+b', c+c', d+d')
data_ctr_state = LoadingData 0
data DataCtrState = Ready | LoadingData (Unsigned 2) deriving (Show, Generic, NFDataX)
-- mapM_ print $ L.take 110 $ L.zip [0..] $ simulate @System system $ [Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Just 1,Nothing,Nothing,Just 2,Just 3,Just 4, Nothing, Nothing, Nothing, Nothing, Nothing, Just 1, Just 2, Just 3, Just 4, Nothing, Just 1, Nothing, Just 2, Nothing, Just 3, Nothing, Just 4, Nothing, Just 1, Just 2, Just 3, Just 4] L.++ L.repeat Nothing
type UInt = Unsigned 32
type Hash = (UInt, UInt, UInt, UInt)
}
component const_store() {
input stall : Bool
state i = start_state : MD5State
output k : UInt
output s : Unsigned 5
output out_i : Unsigned 6
output is_hashing : Bool
output ready : Bool
ready = case i of
(Loading _) -> False
(Hashing n) -> n == 63
k = k_store !! index
s = s_store !! index
-- We assume that loading will actually happen in those four cycles, and will happily start
-- hashing even if that didn't happen.
i' = if stall then i else case i of
Loading 3 -> Hashing 0
Loading n -> Loading (n + 1)
Hashing 63 -> Loading 0
Hashing n -> Hashing (n + 1)
out_i = index
load_i = case i of
Loading step -> step
Hashing _ -> 0
is_hashing = case i of
Loading _ -> False
Hashing _ -> True && (not stall) -- Ja dit kan korter
index = case i of
Loading _ -> 0
Hashing step -> step
k_store = 3614090360:>3905402710:>606105819:>3250441966:>4118548399:>1200080426:>2821735955:>4249261313:>1770035416:>2336552879:>4294925233:>2304563134:>1804603682:>4254626195:>2792965006:>1236535329:>4129170786:>3225465664:>643717713:>3921069994:>3593408605:>38016083:>3634488961:>3889429448:>568446438:>3275163606:>4107603335:>1163531501:>2850285829:>4243563512:>1735328473:>2368359562:>4294588738:>2272392833:>1839030562:>4259657740:>2763975236:>1272893353:>4139469664:>3200236656:>681279174:>3936430074:>3572445317:>76029189:>3654602809:>3873151461:>530742520:>3299628645:>4096336452:>1126891415:>2878612391:>4237533241:>1700485571:>2399980690:>4293915773:>2240044497:>1873313359:>4264355552:>2734768916:>1309151649:>4149444226:>3174756917:>718787259:>3951481745:>Nil
s_store = 7:>12:>17:>22:>7:>12:>17:>22:>7:>12:>17:>22:>7:>12:>17:>22:>5:>9:>14:>20:>5:>9:>14:>20:>5:>9:>14:>20:>5:>9:>14:>20:>4:>11:>16:>23:>4:>11:>16:>23:>4:>11:>16:>23:>4:>11:>16:>23:>6:>10:>15:>21:>6:>10:>15:>21:>6:>10:>15:>21:>6:>10:>15:>21:>Nil
}
component message_store() {
input g : Unsigned 4
input data_in : Maybe UInt
state data_ctr = data_ctr_state : DataCtrState
state message = zero_msg : Vec 16 UInt
output m : UInt
output stall : Bool
(message') = case data_in of
(Just d) -> case data_ctr of
(LoadingData ctr_index) -> (replace ctr_index d message)
_ -> message
Nothing -> (message)
data_ctr' = case data_in of
(Just d) -> case data_ctr of
LoadingData 3 -> Ready
LoadingData n -> LoadingData (n + 1)
Nothing -> data_ctr
stall = case data_ctr of
Ready -> False
_ -> True
m = message !! g
}
component fabcd_update() {
input f : UInt
input mg : UInt
input s : Unsigned 5
input k : UInt
input in_A : UInt
input in_B : UInt
input in_C : UInt
input in_D : UInt
output out_A : UInt
output out_B : UInt
output out_C : UInt
output out_D : UInt
f' = f + in_A + k + mg
out_A = in_D
out_D = in_C
out_C = in_B
out_B = in_B + rotateL f' (fromIntegral s) -- Kan dit wel, variabele rotate?
}
component calculator() {
input a : UInt
input b : UInt
input c : UInt
input d : UInt
input i : Unsigned 6
output out_F : UInt
output out_g : Unsigned 4
stage = i `shiftR` 4
out_F = case stage of
0 -> (b .&. c) .|. ((complement b) .&. d)
1 -> (d .&. b) .|. ((complement d) .&. c)
2 -> b `xor` c `xor` d
3 -> c `xor` (b .|. (complement d))
out_g = case stage of
0 -> resize i
1 -> resize (5 * i + 1)
2 -> resize (3 * i + 5)
3 -> resize (7 * i)
}
component abcd_store() {
input enable : Bool
input in_A : UInt
input in_B : UInt
input in_C : UInt
input in_D : UInt
state s_A = 1732584193 : UInt
state s_B = 4023233417 : UInt
state s_C = 2562383102 : UInt
state s_D = 271733878 : UInt
output out_A : UInt
output out_B : UInt
output out_C : UInt
output out_D : UInt
(s_A', s_B', s_C', s_D') = if enable
then (in_A, in_B, in_C, in_D)
else (s_A, s_B, s_C, s_D)
out_A = s_A
out_B = s_B
out_C = s_C
out_D = s_D
}
component hash_output() {
input ready : Bool
input a : UInt
input b : UInt
input c : UInt
input d : UInt
state saved_hash = start_hash : Hash
state output_ctr = 0 : Unsigned 2
output hash_word : Maybe UInt
saved_hash' = if ready
then ((a,b,c,d)) `add_hash` saved_hash
else saved_hash
should_emit = ready || output_ctr /= 0
output_ctr' = if should_emit
then output_ctr + 1
else output_ctr
hash_word = if should_emit
then case output_ctr of
0 -> Just (hash_a saved_hash')
1 -> Just (hash_b saved_hash')
2 -> Just (hash_c saved_hash')
3 -> Just (hash_d saved_hash')
else Nothing
}
component load_balance_4() {
input msg_words : Maybe UInt
state word_ctr = 0 : Unsigned 4
output word_out_1 : Maybe UInt
output word_out_2 : Maybe UInt
output word_out_3 : Maybe UInt
output word_out_4 : Maybe UInt
word_out_1 = if turn == 0 then msg_words else Nothing
word_out_2 = if turn == 1 then msg_words else Nothing
word_out_3 = if turn == 2 then msg_words else Nothing
word_out_4 = if turn == 3 then msg_words else Nothing
word_ctr' = case msg_words of
(Just _) -> word_ctr + 1
Nothing -> word_ctr
turn = word_ctr `shiftR` 2
}
component cat_4_maybes() {
input hash_1 : Maybe UInt
input hash_2 : Maybe UInt
input hash_3 : Maybe UInt
input hash_4 : Maybe UInt
output hash : Maybe UInt
hash = case hash_1 of
(Just hash) -> Just hash
Nothing -> case hash_2 of
(Just hash) -> Just hash
Nothing -> case hash_3 of
(Just hash) -> Just hash
Nothing -> case hash_4 of
(Just hash) -> Just hash
Nothing -> Nothing
}