forked from OV2/RapidCRC-Unicode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MD4_asm.asm
259 lines (237 loc) · 11.7 KB
/
MD4_asm.asm
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
; #####################################################################################################################
;
; MD4_asm.asm
;
; Copyright (c) Shareaza Development Team, 2002-2007.
; This file is part of SHAREAZA (shareaza.sourceforge.net)
;
; Shareaza is free software; you can redistribute it
; and/or modify it under the terms of the GNU General Public License
; as published by the Free Software Foundation; either version 2 of
; the License, or (at your option) any later version.
;
; Shareaza is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with Shareaza; if not, write to the Free Software
; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
;
; #####################################################################################################################
;
; MD4_asm - Implementation of MD4 for x86 - use together with MD4.cpp and MD4.h
;
; #####################################################################################################################
.586p
.model flat, stdcall
option casemap:none ; case sensitive
option prologue:none ; we generate our own entry/exit code
option epilogue:none
; #####################################################################################################################
m_nCount0 equ 0
m_nCount1 equ 4
m_nState0 equ 8 ; offsets as found in MD4.h
m_nState1 equ 12
m_nState2 equ 16
m_nState3 equ 20
m_pBuffer equ 24
; Some magic numbers for Transform...
MD4_S11 equ 3
MD4_S12 equ 7
MD4_S13 equ 11
MD4_S14 equ 19
MD4_S21 equ 3
MD4_S22 equ 5
MD4_S23 equ 9
MD4_S24 equ 13
MD4_S31 equ 3
MD4_S32 equ 9
MD4_S33 equ 11
MD4_S34 equ 15
.data
MD4FF MACRO count:REQ,s:REQ
; a = (a+x[count]+((b&c)|(~b&d)))rol s
; a = (a+x[count]+(d^(b&(c^d))))rol s
mov reg_temp1, reg_c
xor reg_c, reg_d
add reg_a, [ebp+count*4]
and reg_c, reg_b
xor reg_c, reg_d
add reg_a, reg_c
rol reg_a, s
reg_t textequ reg_d
reg_d textequ reg_temp1
reg_temp1 textequ reg_c
reg_c textequ reg_b
reg_b textequ reg_a
reg_a textequ reg_t
ENDM
MD4GG MACRO count:REQ,s:REQ
; a = (a+x[count]+((b&c)|(b&d)|(c&d))+5A827999H) rol s
; a = (a+x[count]+((b&c)|(d&(b|c)))+5A827999H)rol s
mov reg_temp2, reg_b
mov reg_temp1, reg_b
add reg_a, [ebp+count*4]
or reg_b, reg_c
and reg_temp2, reg_c
and reg_b, reg_d
add reg_a, 5A827999H
or reg_b, reg_temp2
add reg_a, reg_b
rol reg_a, s
reg_t textequ reg_d
reg_d textequ reg_c
reg_c textequ reg_temp1
reg_temp1 textequ reg_b
reg_b textequ reg_a
reg_a textequ reg_t
ENDM
MD4HH MACRO count:REQ,s:REQ
; a = (a+x[count]+(b^c^d)+6ED9EBA1H)rol s
add reg_a, [ebp+count*4]
mov reg_temp1, reg_b
xor reg_b, reg_c
add reg_a, 6ED9EBA1H
xor reg_b, reg_d
add reg_a, reg_b
rol reg_a, s
reg_t textequ reg_d
reg_d textequ reg_c
reg_c textequ reg_temp1
reg_temp1 textequ reg_b
reg_b textequ reg_a
reg_a textequ reg_t
ENDM
.code
MD4_Transform_p5 PROC ; we expect ebp to point to the Data stream
; all other registers (eax,ebx,ecx,edx,esi,edi) will be destroyed
__this textequ <[esp+32+2*4]> ; 1*pusha+2*call
; set alias for registers
reg_a textequ <eax>
reg_b textequ <ebx>
reg_c textequ <ecx>
reg_d textequ <edx>
reg_temp1 textequ <esi>
reg_temp2 textequ <edi>
mov reg_temp1, __this
mov reg_a, [reg_temp1+m_nState0]
mov reg_b, [reg_temp1+m_nState1]
mov reg_c, [reg_temp1+m_nState2]
mov reg_d, [reg_temp1+m_nState3]
; round 1
MD4FF 0, MD4_S11
MD4FF 1, MD4_S12
MD4FF 2, MD4_S13
MD4FF 3, MD4_S14
MD4FF 4, MD4_S11
MD4FF 5, MD4_S12
MD4FF 6, MD4_S13
MD4FF 7, MD4_S14
MD4FF 8, MD4_S11
MD4FF 9, MD4_S12
MD4FF 10, MD4_S13
MD4FF 11, MD4_S14
MD4FF 12, MD4_S11
MD4FF 13, MD4_S12
MD4FF 14, MD4_S13
MD4FF 15, MD4_S14
; round 2
MD4GG 0, MD4_S21
MD4GG 4, MD4_S22
MD4GG 8, MD4_S23
MD4GG 12, MD4_S24
MD4GG 1, MD4_S21
MD4GG 5, MD4_S22
MD4GG 9, MD4_S23
MD4GG 13, MD4_S24
MD4GG 2, MD4_S21
MD4GG 6, MD4_S22
MD4GG 10, MD4_S23
MD4GG 14, MD4_S24
MD4GG 3, MD4_S21
MD4GG 7, MD4_S22
MD4GG 11, MD4_S23
MD4GG 15, MD4_S24
; round 3
MD4HH 0, MD4_S31
MD4HH 8, MD4_S32
MD4HH 4, MD4_S33
MD4HH 12, MD4_S34
MD4HH 2, MD4_S31
MD4HH 10, MD4_S32
MD4HH 6, MD4_S33
MD4HH 14, MD4_S34
MD4HH 1, MD4_S31
MD4HH 9, MD4_S32
MD4HH 5, MD4_S33
MD4HH 13, MD4_S34
MD4HH 3, MD4_S31
MD4HH 11, MD4_S32
MD4HH 7, MD4_S33
MD4HH 15, MD4_S34
mov reg_temp1, __this
add [reg_temp1+m_nState0], reg_a
add [reg_temp1+m_nState1], reg_b
add [reg_temp1+m_nState2], reg_c
add [reg_temp1+m_nState3], reg_d
ret
MD4_Transform_p5 ENDP
MD4_Add_p5 PROC PUBLIC, _this:DWORD, _Data:DWORD, _nLength:DWORD
pusha
__this textequ <[esp+36]> ; different offset due to pusha
__Data textequ <[esp+40]>
__nLength textequ <[esp+44]>
mov ecx, __nLength
and ecx, ecx
jz get_out
xor edx, edx
mov ebp, __Data
mov edi, __this
mov ebx, [edi+m_nCount0]
mov eax, ebx
add ebx, ecx
mov [edi+m_nCount0], ebx
adc [edi+m_nCount1], edx
and eax, 63
jnz partial_buffer
full_blocks: mov ecx, __nLength
and ecx, ecx
jz get_out
sub ecx, 64
jb end_of_stream
mov __nLength, ecx
call MD4_Transform_p5
add ebp, 64
jmp full_blocks
end_of_stream: mov edi, __this
mov esi, ebp
lea edi, [edi+m_pBuffer]
add ecx, 64
rep movsb
jmp get_out
partial_buffer: add ecx, eax ; eax = offset in buffer, ecx = _nLength
cmp ecx, 64
jb short_stream ; we can't fill the buffer
mov ecx, -64
add ecx, eax
add __nLength, ecx ; _nlength += (offset-64)
@@: mov bl, [ebp]
inc ebp
mov byte ptr [edi+m_pBuffer+64+ecx], bl
inc ecx
jnz @B ; offset = 64
mov __Data, ebp
lea ebp, [edi+m_pBuffer]
call MD4_Transform_p5
mov ebp, __Data
jmp full_blocks
short_stream: sub ecx, eax ; --> ecx=_nLength
mov esi, ebp
lea edi, [edi+m_pBuffer+eax]
rep movsb
get_out: popa
ret 12
MD4_Add_p5 ENDP
end