Subversion Repositories Kolibri OS

Rev

Rev 6465 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
7698 dunkaist 1
;    libcrash -- cryptographic hash functions
2
;
3
;    Copyright (C) 2012-2013,2016,2019 Ivan Baravy (dunkaist)
4
;
5
;    This program is free software: you can redistribute it and/or modify
6
;    it under the terms of the GNU General Public License as published by
7
;    the Free Software Foundation, either version 3 of the License, or
8
;    (at your option) any later version.
9
;
10
;    This program is distributed in the hope that it will be useful,
11
;    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
;    GNU General Public License for more details.
14
;
15
;    You should have received a copy of the GNU General Public License
16
;    along with this program.  If not, see .
17
 
18
 
19
MD4_HASH_SIZE  = 16
20
MD4_BLOCK_SIZE = 64
21
 
22
MD4_ALIGN      = 4
23
MD4_ALIGN_MASK = MD4_ALIGN - 1
24
 
25
struct ctx_md4
26
        hash            rb MD4_HASH_SIZE
27
        block           rb MD4_BLOCK_SIZE
28
        index           rd 1
29
        msglen_0        rd 1
30
        msglen_1        rd 1
31
ends
32
 
33
if defined sizeof.crash_ctx
34
  assert sizeof.crash_ctx >= sizeof.ctx_md4
35
end if
36
 
37
macro md4._.f b, c, d
38
{
39
        mov     eax, c
40
        xor     eax, d
41
        and     eax, b
42
        xor     eax, d
43
}
44
 
45
macro md4._.g b, c, d
46
{
47
        push    c d
48
        mov     eax, b
49
        and     eax, c
50
        and     c, d
51
        and     d, b
52
        or      eax, c
53
        or      eax, d
54
        pop     d c
55
}
56
 
57
macro md4._.h b, c, d
58
{
59
        mov     eax, b
60
        xor     eax, c
61
        xor     eax, d
62
}
63
 
64
macro md4._.round func, a, b, c, d, index, shift, ac
65
{
66
        func    b, c, d
67
        add     eax, [esi + index*4]
68
        lea     a, [a + eax + ac]
69
        rol     a, shift
70
}
71
 
72
 
73
proc md4.init _ctx
74
        mov     ebx, [_ctx]
75
        lea     edi, [ebx + ctx_md4.hash]
76
        mov     esi, md4._.hash_init
77
        mov     ecx, MD4_HASH_SIZE/4
78
        rep     movsd
79
        xor     eax, eax
80
        mov     [ebx + ctx_md4.index], eax
81
        mov     [ebx + ctx_md4.msglen_0], eax
82
        mov     [ebx + ctx_md4.msglen_1], eax
83
        ret
84
endp
85
 
86
 
87
proc md4._.block _hash
88
 
89
        mov     eax, [_hash]
90
        mov     edi, [eax + 0x0]
91
        mov     ebx, [eax + 0x4]
92
        mov     ecx, [eax + 0x8]
93
        mov     edx, [eax + 0xc]
94
 
95
        md4._.round     md4._.f, edi, ebx, ecx, edx,  0,  3, 0x00000000
96
        md4._.round     md4._.f, edx, edi, ebx, ecx,  1,  7, 0x00000000
97
        md4._.round     md4._.f, ecx, edx, edi, ebx,  2, 11, 0x00000000
98
        md4._.round     md4._.f, ebx, ecx, edx, edi,  3, 19, 0x00000000
99
        md4._.round     md4._.f, edi, ebx, ecx, edx,  4,  3, 0x00000000
100
        md4._.round     md4._.f, edx, edi, ebx, ecx,  5,  7, 0x00000000
101
        md4._.round     md4._.f, ecx, edx, edi, ebx,  6, 11, 0x00000000
102
        md4._.round     md4._.f, ebx, ecx, edx, edi,  7, 19, 0x00000000
103
        md4._.round     md4._.f, edi, ebx, ecx, edx,  8,  3, 0x00000000
104
        md4._.round     md4._.f, edx, edi, ebx, ecx,  9,  7, 0x00000000
105
        md4._.round     md4._.f, ecx, edx, edi, ebx, 10, 11, 0x00000000
106
        md4._.round     md4._.f, ebx, ecx, edx, edi, 11, 19, 0x00000000
107
        md4._.round     md4._.f, edi, ebx, ecx, edx, 12,  3, 0x00000000
108
        md4._.round     md4._.f, edx, edi, ebx, ecx, 13,  7, 0x00000000
109
        md4._.round     md4._.f, ecx, edx, edi, ebx, 14, 11, 0x00000000
110
        md4._.round     md4._.f, ebx, ecx, edx, edi, 15, 19, 0x00000000
111
 
112
        md4._.round     md4._.g, edi, ebx, ecx, edx,  0,  3, 0x5a827999
113
        md4._.round     md4._.g, edx, edi, ebx, ecx,  4,  5, 0x5a827999
114
        md4._.round     md4._.g, ecx, edx, edi, ebx,  8,  9, 0x5a827999
115
        md4._.round     md4._.g, ebx, ecx, edx, edi, 12, 13, 0x5a827999
116
        md4._.round     md4._.g, edi, ebx, ecx, edx,  1,  3, 0x5a827999
117
        md4._.round     md4._.g, edx, edi, ebx, ecx,  5,  5, 0x5a827999
118
        md4._.round     md4._.g, ecx, edx, edi, ebx,  9,  9, 0x5a827999
119
        md4._.round     md4._.g, ebx, ecx, edx, edi, 13, 13, 0x5a827999
120
        md4._.round     md4._.g, edi, ebx, ecx, edx,  2,  3, 0x5a827999
121
        md4._.round     md4._.g, edx, edi, ebx, ecx,  6,  5, 0x5a827999
122
        md4._.round     md4._.g, ecx, edx, edi, ebx, 10,  9, 0x5a827999
123
        md4._.round     md4._.g, ebx, ecx, edx, edi, 14, 13, 0x5a827999
124
        md4._.round     md4._.g, edi, ebx, ecx, edx,  3,  3, 0x5a827999
125
        md4._.round     md4._.g, edx, edi, ebx, ecx,  7,  5, 0x5a827999
126
        md4._.round     md4._.g, ecx, edx, edi, ebx, 11,  9, 0x5a827999
127
        md4._.round     md4._.g, ebx, ecx, edx, edi, 15, 13, 0x5a827999
128
 
129
        md4._.round     md4._.h, edi, ebx, ecx, edx,  0,  3, 0x6ed9eba1
130
        md4._.round     md4._.h, edx, edi, ebx, ecx,  8,  9, 0x6ed9eba1
131
        md4._.round     md4._.h, ecx, edx, edi, ebx,  4, 11, 0x6ed9eba1
132
        md4._.round     md4._.h, ebx, ecx, edx, edi, 12, 15, 0x6ed9eba1
133
        md4._.round     md4._.h, edi, ebx, ecx, edx,  2,  3, 0x6ed9eba1
134
        md4._.round     md4._.h, edx, edi, ebx, ecx, 10,  9, 0x6ed9eba1
135
        md4._.round     md4._.h, ecx, edx, edi, ebx,  6, 11, 0x6ed9eba1
136
        md4._.round     md4._.h, ebx, ecx, edx, edi, 14, 15, 0x6ed9eba1
137
        md4._.round     md4._.h, edi, ebx, ecx, edx,  1,  3, 0x6ed9eba1
138
        md4._.round     md4._.h, edx, edi, ebx, ecx,  9,  9, 0x6ed9eba1
139
        md4._.round     md4._.h, ecx, edx, edi, ebx,  5, 11, 0x6ed9eba1
140
        md4._.round     md4._.h, ebx, ecx, edx, edi, 13, 15, 0x6ed9eba1
141
        md4._.round     md4._.h, edi, ebx, ecx, edx,  3,  3, 0x6ed9eba1
142
        md4._.round     md4._.h, edx, edi, ebx, ecx, 11,  9, 0x6ed9eba1
143
        md4._.round     md4._.h, ecx, edx, edi, ebx,  7, 11, 0x6ed9eba1
144
        md4._.round     md4._.h, ebx, ecx, edx, edi, 15, 15, 0x6ed9eba1
145
 
146
        mov     eax, [_hash]
147
        add     [eax + 0x0], edi
148
        add     [eax + 0x4], ebx
149
        add     [eax + 0x8], ecx
150
        add     [eax + 0xc], edx
151
 
152
        ret
153
endp
154
 
155
 
156
proc md4.update _ctx, _msg, _size
157
        mov     ebx, [_ctx]
158
        mov     ecx, [_size]
159
        add     [ebx + ctx_md4.msglen_0], ecx
160
        adc     [ebx + ctx_md4.msglen_1], 0
161
 
162
  .next_block:
163
        mov     ebx, [_ctx]
164
        mov     esi, [_msg]
165
        mov     eax, [ebx + ctx_md4.index]
166
        and     eax, MD4_BLOCK_SIZE-1
167
        jnz     .copy_to_buf
168
        test    esi, MD4_ALIGN_MASK
169
        jnz     .copy_to_buf
170
  .no_copy:
171
        ; data is aligned, hash it in place without copying
172
        mov     ebx, [_ctx]
173
        cmp     [_size], MD4_BLOCK_SIZE
174
        jb      .copy_quit
175
        lea     eax, [ebx + ctx_md4.hash]
176
        stdcall md4._.block, eax
177
        sub     [_size], MD4_BLOCK_SIZE
178
        add     esi, MD4_BLOCK_SIZE
179
        jmp     .no_copy
180
 
181
  .copy_to_buf:
182
        lea     edi, [ebx + ctx_md4.block]
183
        add     edi, eax
184
        mov     ecx, MD4_BLOCK_SIZE
185
        sub     ecx, eax
186
        cmp     [_size], ecx
187
        jb      .copy_quit
188
        sub     [_size], ecx
189
        add     [_msg], ecx
190
        add     [ebx + ctx_md4.index], ecx
191
        rep     movsb
192
        lea     eax, [ebx + ctx_md4.hash]
193
        lea     esi, [ebx + ctx_md4.block]
194
        stdcall md4._.block, eax
195
        jmp     .next_block
196
 
197
  .copy_quit:
198
        mov     ebx, [_ctx]
199
        lea     edi, [ebx + ctx_md4.block]
200
        mov     eax, [ebx + ctx_md4.index]
201
        and     eax, MD4_BLOCK_SIZE-1
202
        add     edi, eax
203
        mov     ecx, [_size]
204
        add     [ebx + ctx_md4.index], ecx
205
        rep     movsb
206
  .quit:
207
        ret
208
endp
209
 
210
 
211
proc md4.final _ctx
212
        mov     ebx, [_ctx]
213
        lea     edi, [ebx + ctx_md4.block]
214
        mov     ecx, [ebx + ctx_md4.msglen_0]
215
        and     ecx, MD4_BLOCK_SIZE-1
216
        add     edi, ecx
217
        mov     byte[edi], 0x80
218
        inc     edi
219
        neg     ecx
220
        add     ecx, MD4_BLOCK_SIZE
221
        cmp     ecx, 8
222
        ja      .last
223
 
224
        dec     ecx
225
        xor     eax, eax
226
        rep     stosb
227
        lea     esi, [ebx + ctx_md4.block]
228
        lea     eax, [ebx + ctx_md4.hash]
229
        stdcall md4._.block, eax
230
        mov     ebx, [_ctx]
231
        lea     edi, [ebx + ctx_md4.block]
232
        mov     ecx, MD4_BLOCK_SIZE+1
233
  .last:
234
        dec     ecx
235
        sub     ecx, 8
236
        xor     eax, eax
237
        rep     stosb
238
        mov     eax, [ebx + ctx_md4.msglen_0]
239
        mov     edx, [ebx + ctx_md4.msglen_1]
240
        shld    edx, eax, 3
241
        shl     eax, 3
242
        mov     dword[edi], eax
243
        mov     dword[edi+4], edx
244
        lea     esi, [ebx + ctx_md4.block]
245
        lea     eax, [ebx + ctx_md4.hash]
246
        stdcall md4._.block, eax
247
 
248
        ret
249
endp
250
 
251
 
252
proc md4.oneshot _ctx, _data, _len
253
	stdcall	md4.init, [_ctx]
254
	stdcall	md4.update, [_ctx], [_data], [_len]
255
	stdcall	md4.final, [_ctx]
256
	ret
257
endp
258
 
259
 
260
iglobal
261
align MD4_ALIGN
262
md4._.hash_init dd 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0
263
endg