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
MD5_HASH_SIZE  = 16
20
MD5_BLOCK_SIZE = 64
21
 
22
MD5_ALIGN      = 4
23
MD5_ALIGN_MASK = MD5_ALIGN - 1
24
 
25
struct ctx_md5
26
        hash            rb MD5_HASH_SIZE
27
        block           rb MD5_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_md5
35
end if
36
 
37
macro md5._.f b, c, d
38
{
39
        push    c
40
        xor     c, d
41
        and     b, c
42
        xor     b, d
43
        pop     c
44
}
45
 
46
macro md5._.g b, c, d
47
{
48
        push    c  d
49
        and     b, d
50
        not     d
51
        and     c, d
52
        or      b, c
53
        pop     d  c
54
}
55
 
56
macro md5._.h b, c, d
57
{
58
        xor     b, c
59
        xor     b, d
60
}
61
 
62
macro md5._.i b, c, d
63
{
64
        push    d
65
        not     d
66
        or      b, d
67
        xor     b, c
68
        pop     d
69
}
70
 
71
macro md5._.round func, a, b, c, d, index, shift, ac
72
{
73
        push    b
74
        func    b, c, d
75
        lea     a, [a + b + ac]
76
        add     a, [esi + index*4]
77
        rol     a, shift
78
        pop     b
79
        add     a, b
80
}
81
 
82
 
83
proc md5.init _ctx
84
        mov     ebx, [_ctx]
85
        lea     edi, [ebx + ctx_md5.hash]
86
        mov     esi, md5._.hash_init
87
        mov     ecx, MD5_HASH_SIZE/4
88
        rep     movsd
89
        xor     eax, eax
90
        mov     [ebx + ctx_md5.index], eax
91
        mov     [ebx + ctx_md5.msglen_0], eax
92
        mov     [ebx + ctx_md5.msglen_1], eax
93
        ret
94
endp
95
 
96
 
97
proc md5._.block _hash
98
 
99
        mov     edi, [_hash]
100
        mov     eax, [edi + 0x0]
101
        mov     ebx, [edi + 0x4]
102
        mov     ecx, [edi + 0x8]
103
        mov     edx, [edi + 0xc]
104
 
105
        md5._.round     md5._.f, eax, ebx, ecx, edx,  0,  7, 0xd76aa478
106
        md5._.round     md5._.f, edx, eax, ebx, ecx,  1, 12, 0xe8c7b756
107
        md5._.round     md5._.f, ecx, edx, eax, ebx,  2, 17, 0x242070db
108
        md5._.round     md5._.f, ebx, ecx, edx, eax,  3, 22, 0xc1bdceee
109
        md5._.round     md5._.f, eax, ebx, ecx, edx,  4,  7, 0xf57c0faf
110
        md5._.round     md5._.f, edx, eax, ebx, ecx,  5, 12, 0x4787c62a
111
        md5._.round     md5._.f, ecx, edx, eax, ebx,  6, 17, 0xa8304613
112
        md5._.round     md5._.f, ebx, ecx, edx, eax,  7, 22, 0xfd469501
113
        md5._.round     md5._.f, eax, ebx, ecx, edx,  8,  7, 0x698098d8
114
        md5._.round     md5._.f, edx, eax, ebx, ecx,  9, 12, 0x8b44f7af
115
        md5._.round     md5._.f, ecx, edx, eax, ebx, 10, 17, 0xffff5bb1
116
        md5._.round     md5._.f, ebx, ecx, edx, eax, 11, 22, 0x895cd7be
117
        md5._.round     md5._.f, eax, ebx, ecx, edx, 12,  7, 0x6b901122
118
        md5._.round     md5._.f, edx, eax, ebx, ecx, 13, 12, 0xfd987193
119
        md5._.round     md5._.f, ecx, edx, eax, ebx, 14, 17, 0xa679438e
120
        md5._.round     md5._.f, ebx, ecx, edx, eax, 15, 22, 0x49b40821
121
 
122
        md5._.round     md5._.g, eax, ebx, ecx, edx,  1,  5, 0xf61e2562
123
        md5._.round     md5._.g, edx, eax, ebx, ecx,  6,  9, 0xc040b340
124
        md5._.round     md5._.g, ecx, edx, eax, ebx, 11, 14, 0x265e5a51
125
        md5._.round     md5._.g, ebx, ecx, edx, eax,  0, 20, 0xe9b6c7aa
126
        md5._.round     md5._.g, eax, ebx, ecx, edx,  5,  5, 0xd62f105d
127
        md5._.round     md5._.g, edx, eax, ebx, ecx, 10,  9, 0x02441453
128
        md5._.round     md5._.g, ecx, edx, eax, ebx, 15, 14, 0xd8a1e681
129
        md5._.round     md5._.g, ebx, ecx, edx, eax,  4, 20, 0xe7d3fbc8
130
        md5._.round     md5._.g, eax, ebx, ecx, edx,  9,  5, 0x21e1cde6
131
        md5._.round     md5._.g, edx, eax, ebx, ecx, 14,  9, 0xc33707d6
132
        md5._.round     md5._.g, ecx, edx, eax, ebx,  3, 14, 0xf4d50d87
133
        md5._.round     md5._.g, ebx, ecx, edx, eax,  8, 20, 0x455a14ed
134
        md5._.round     md5._.g, eax, ebx, ecx, edx, 13,  5, 0xa9e3e905
135
        md5._.round     md5._.g, edx, eax, ebx, ecx,  2,  9, 0xfcefa3f8
136
        md5._.round     md5._.g, ecx, edx, eax, ebx,  7, 14, 0x676f02d9
137
        md5._.round     md5._.g, ebx, ecx, edx, eax, 12, 20, 0x8d2a4c8a
138
 
139
        md5._.round     md5._.h, eax, ebx, ecx, edx,  5,  4, 0xfffa3942
140
        md5._.round     md5._.h, edx, eax, ebx, ecx,  8, 11, 0x8771f681
141
        md5._.round     md5._.h, ecx, edx, eax, ebx, 11, 16, 0x6d9d6122
142
        md5._.round     md5._.h, ebx, ecx, edx, eax, 14, 23, 0xfde5380c
143
        md5._.round     md5._.h, eax, ebx, ecx, edx,  1,  4, 0xa4beea44
144
        md5._.round     md5._.h, edx, eax, ebx, ecx,  4, 11, 0x4bdecfa9
145
        md5._.round     md5._.h, ecx, edx, eax, ebx,  7, 16, 0xf6bb4b60
146
        md5._.round     md5._.h, ebx, ecx, edx, eax, 10, 23, 0xbebfbc70
147
        md5._.round     md5._.h, eax, ebx, ecx, edx, 13,  4, 0x289b7ec6
148
        md5._.round     md5._.h, edx, eax, ebx, ecx,  0, 11, 0xeaa127fa
149
        md5._.round     md5._.h, ecx, edx, eax, ebx,  3, 16, 0xd4ef3085
150
        md5._.round     md5._.h, ebx, ecx, edx, eax,  6, 23, 0x04881d05
151
        md5._.round     md5._.h, eax, ebx, ecx, edx,  9,  4, 0xd9d4d039
152
        md5._.round     md5._.h, edx, eax, ebx, ecx, 12, 11, 0xe6db99e5
153
        md5._.round     md5._.h, ecx, edx, eax, ebx, 15, 16, 0x1fa27cf8
154
        md5._.round     md5._.h, ebx, ecx, edx, eax,  2, 23, 0xc4ac5665
155
 
156
        md5._.round     md5._.i, eax, ebx, ecx, edx,  0,  6, 0xf4292244
157
        md5._.round     md5._.i, edx, eax, ebx, ecx,  7, 10, 0x432aff97
158
        md5._.round     md5._.i, ecx, edx, eax, ebx, 14, 15, 0xab9423a7
159
        md5._.round     md5._.i, ebx, ecx, edx, eax,  5, 21, 0xfc93a039
160
        md5._.round     md5._.i, eax, ebx, ecx, edx, 12,  6, 0x655b59c3
161
        md5._.round     md5._.i, edx, eax, ebx, ecx,  3, 10, 0x8f0ccc92
162
        md5._.round     md5._.i, ecx, edx, eax, ebx, 10, 15, 0xffeff47d
163
        md5._.round     md5._.i, ebx, ecx, edx, eax,  1, 21, 0x85845dd1
164
        md5._.round     md5._.i, eax, ebx, ecx, edx,  8,  6, 0x6fa87e4f
165
        md5._.round     md5._.i, edx, eax, ebx, ecx, 15, 10, 0xfe2ce6e0
166
        md5._.round     md5._.i, ecx, edx, eax, ebx,  6, 15, 0xa3014314
167
        md5._.round     md5._.i, ebx, ecx, edx, eax, 13, 21, 0x4e0811a1
168
        md5._.round     md5._.i, eax, ebx, ecx, edx,  4,  6, 0xf7537e82
169
        md5._.round     md5._.i, edx, eax, ebx, ecx, 11, 10, 0xbd3af235
170
        md5._.round     md5._.i, ecx, edx, eax, ebx,  2, 15, 0x2ad7d2bb
171
        md5._.round     md5._.i, ebx, ecx, edx, eax,  9, 21, 0xeb86d391
172
 
173
        mov     edi, [_hash]
174
        add     [edi + 0x0], eax
175
        add     [edi + 0x4], ebx
176
        add     [edi + 0x8], ecx
177
        add     [edi + 0xc], edx
178
 
179
        ret
180
endp
181
 
182
 
183
proc md5.update _ctx, _msg, _size
184
        mov     ebx, [_ctx]
185
        mov     ecx, [_size]
186
        add     [ebx + ctx_md5.msglen_0], ecx
187
        adc     [ebx + ctx_md5.msglen_1], 0
188
 
189
  .next_block:
190
        mov     ebx, [_ctx]
191
        mov     esi, [_msg]
192
        mov     eax, [ebx + ctx_md5.index]
193
        and     eax, MD5_BLOCK_SIZE-1
194
        jnz     .copy_to_buf
195
        test    esi, MD5_ALIGN_MASK
196
        jnz     .copy_to_buf
197
  .no_copy:
198
        ; data is aligned, hash it in place without copying
199
        mov     ebx, [_ctx]
200
        cmp     [_size], MD5_BLOCK_SIZE
201
        jb      .copy_quit
202
        lea     eax, [ebx + ctx_md5.hash]
203
        stdcall md5._.block, eax
204
        sub     [_size], MD5_BLOCK_SIZE
205
        add     esi, MD5_BLOCK_SIZE
206
        jmp     .no_copy
207
 
208
  .copy_to_buf:
209
        lea     edi, [ebx + ctx_md5.block]
210
        add     edi, eax
211
        mov     ecx, MD5_BLOCK_SIZE
212
        sub     ecx, eax
213
        cmp     [_size], ecx
214
        jb      .copy_quit
215
        sub     [_size], ecx
216
        add     [_msg], ecx
217
        add     [ebx + ctx_md5.index], ecx
218
        rep     movsb
219
        lea     eax, [ebx + ctx_md5.hash]
220
        lea     esi, [ebx + ctx_md5.block]
221
        stdcall md5._.block, eax
222
        jmp     .next_block
223
 
224
  .copy_quit:
225
        mov     ebx, [_ctx]
226
        lea     edi, [ebx + ctx_md5.block]
227
        mov     eax, [ebx + ctx_md5.index]
228
        and     eax, MD5_BLOCK_SIZE-1
229
        add     edi, eax
230
        mov     ecx, [_size]
231
        add     [ebx + ctx_md5.index], ecx
232
        rep     movsb
233
  .quit:
234
 
235
        ret
236
endp
237
 
238
 
239
proc md5.final _ctx
240
        mov     ebx, [_ctx]
241
        lea     edi, [ebx + ctx_md5.block]
242
        mov     ecx, [ebx + ctx_md5.msglen_0]
243
        and     ecx, MD5_BLOCK_SIZE-1
244
        add     edi, ecx
245
        mov     byte[edi], 0x80
246
        inc     edi
247
        neg     ecx
248
        add     ecx, MD5_BLOCK_SIZE
249
        cmp     ecx, 8
250
        ja      .last
251
 
252
        dec     ecx
253
        xor     eax, eax
254
        rep     stosb
255
        lea     esi, [ebx + ctx_md5.block]
256
        lea     eax, [ebx + ctx_md5.hash]
257
        stdcall md5._.block, eax
258
        mov     ebx, [_ctx]
259
        lea     edi, [ebx + ctx_md5.block]
260
        mov     ecx, MD5_BLOCK_SIZE+1
261
  .last:
262
        dec     ecx
263
        sub     ecx, 8
264
        xor     eax, eax
265
        rep     stosb
266
        mov     eax, [ebx + ctx_md5.msglen_0]
267
        mov     edx, [ebx + ctx_md5.msglen_1]
268
        shld    edx, eax, 3
269
        shl     eax, 3
270
        mov     dword[edi], eax
271
        mov     dword[edi+4], edx
272
        lea     esi, [ebx + ctx_md5.block]
273
        lea     eax, [ebx + ctx_md5.hash]
274
        stdcall md5._.block, eax
275
 
276
        ret
277
endp
278
 
279
 
280
proc md5.oneshot _ctx, _data, _len
281
	stdcall	md5.init, [_ctx]
282
	stdcall	md5.update, [_ctx], [_data], [_len]
283
	stdcall	md5.final, [_ctx]
284
	ret
285
endp
286
 
287
 
288
iglobal
289
align MD5_ALIGN
290
md5._.hash_init dd 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0
291
endg