Subversion Repositories Kolibri OS

Rev

Rev 3431 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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