Subversion Repositories Kolibri OS

Rev

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

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