Subversion Repositories Kolibri OS

Rev

Rev 7698 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
9216 dunkaist 1
; libcrash -- cryptographic hash (and other) functions
7698 dunkaist 2
;
9216 dunkaist 3
; Copyright (C) <2012-2013,2016,2019,2021> Ivan Baravy
7698 dunkaist 4
;
9216 dunkaist 5
; SPDX-License-Identifier: GPL-2.0-or-later
7698 dunkaist 6
;
9216 dunkaist 7
; This program is free software: you can redistribute it and/or modify it under
8
; the terms of the GNU General Public License as published by the Free Software
9
; Foundation, either version 2 of the License, or (at your option) any later
10
; version.
7698 dunkaist 11
;
9216 dunkaist 12
; This program is distributed in the hope that it will be useful, but WITHOUT
13
; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14
; FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15
;
16
; You should have received a copy of the GNU General Public License along with
17
; this program. If not, see .
7698 dunkaist 18
 
19
MD5_BLOCK_SIZE = 64
20
 
21
MD5_ALIGN      = 4
22
MD5_ALIGN_MASK = MD5_ALIGN - 1
23
 
24
struct ctx_md5
9216 dunkaist 25
        hash            rb MD5_LEN
7698 dunkaist 26
        block           rb MD5_BLOCK_SIZE
27
        index           rd 1
28
        msglen_0        rd 1
29
        msglen_1        rd 1
30
ends
31
 
9216 dunkaist 32
assert sizeof.ctx_md5 <= LIBCRASH_CTX_LEN
7698 dunkaist 33
 
34
macro md5._.f b, c, d
35
{
36
        push    c
37
        xor     c, d
38
        and     b, c
39
        xor     b, d
40
        pop     c
41
}
42
 
43
macro md5._.g b, c, d
44
{
45
        push    c  d
46
        and     b, d
47
        not     d
48
        and     c, d
49
        or      b, c
50
        pop     d  c
51
}
52
 
53
macro md5._.h b, c, d
54
{
55
        xor     b, c
56
        xor     b, d
57
}
58
 
59
macro md5._.i b, c, d
60
{
61
        push    d
62
        not     d
63
        or      b, d
64
        xor     b, c
65
        pop     d
66
}
67
 
68
macro md5._.round func, a, b, c, d, index, shift, ac
69
{
70
        push    b
71
        func    b, c, d
72
        lea     a, [a + b + ac]
73
        add     a, [esi + index*4]
74
        rol     a, shift
75
        pop     b
76
        add     a, b
77
}
78
 
79
 
9216 dunkaist 80
proc md5.init uses ebx esi edi, _ctx
7698 dunkaist 81
        mov     ebx, [_ctx]
82
        lea     edi, [ebx + ctx_md5.hash]
83
        mov     esi, md5._.hash_init
9216 dunkaist 84
        mov     ecx, MD5_LEN/4
85
        rep movsd
7698 dunkaist 86
        xor     eax, eax
87
        mov     [ebx + ctx_md5.index], eax
88
        mov     [ebx + ctx_md5.msglen_0], eax
89
        mov     [ebx + ctx_md5.msglen_1], eax
90
        ret
91
endp
92
 
93
 
94
proc md5._.block _hash
95
 
96
        mov     edi, [_hash]
97
        mov     eax, [edi + 0x0]
98
        mov     ebx, [edi + 0x4]
99
        mov     ecx, [edi + 0x8]
100
        mov     edx, [edi + 0xc]
101
 
102
        md5._.round     md5._.f, eax, ebx, ecx, edx,  0,  7, 0xd76aa478
103
        md5._.round     md5._.f, edx, eax, ebx, ecx,  1, 12, 0xe8c7b756
104
        md5._.round     md5._.f, ecx, edx, eax, ebx,  2, 17, 0x242070db
105
        md5._.round     md5._.f, ebx, ecx, edx, eax,  3, 22, 0xc1bdceee
106
        md5._.round     md5._.f, eax, ebx, ecx, edx,  4,  7, 0xf57c0faf
107
        md5._.round     md5._.f, edx, eax, ebx, ecx,  5, 12, 0x4787c62a
108
        md5._.round     md5._.f, ecx, edx, eax, ebx,  6, 17, 0xa8304613
109
        md5._.round     md5._.f, ebx, ecx, edx, eax,  7, 22, 0xfd469501
110
        md5._.round     md5._.f, eax, ebx, ecx, edx,  8,  7, 0x698098d8
111
        md5._.round     md5._.f, edx, eax, ebx, ecx,  9, 12, 0x8b44f7af
112
        md5._.round     md5._.f, ecx, edx, eax, ebx, 10, 17, 0xffff5bb1
113
        md5._.round     md5._.f, ebx, ecx, edx, eax, 11, 22, 0x895cd7be
114
        md5._.round     md5._.f, eax, ebx, ecx, edx, 12,  7, 0x6b901122
115
        md5._.round     md5._.f, edx, eax, ebx, ecx, 13, 12, 0xfd987193
116
        md5._.round     md5._.f, ecx, edx, eax, ebx, 14, 17, 0xa679438e
117
        md5._.round     md5._.f, ebx, ecx, edx, eax, 15, 22, 0x49b40821
118
 
119
        md5._.round     md5._.g, eax, ebx, ecx, edx,  1,  5, 0xf61e2562
120
        md5._.round     md5._.g, edx, eax, ebx, ecx,  6,  9, 0xc040b340
121
        md5._.round     md5._.g, ecx, edx, eax, ebx, 11, 14, 0x265e5a51
122
        md5._.round     md5._.g, ebx, ecx, edx, eax,  0, 20, 0xe9b6c7aa
123
        md5._.round     md5._.g, eax, ebx, ecx, edx,  5,  5, 0xd62f105d
124
        md5._.round     md5._.g, edx, eax, ebx, ecx, 10,  9, 0x02441453
125
        md5._.round     md5._.g, ecx, edx, eax, ebx, 15, 14, 0xd8a1e681
126
        md5._.round     md5._.g, ebx, ecx, edx, eax,  4, 20, 0xe7d3fbc8
127
        md5._.round     md5._.g, eax, ebx, ecx, edx,  9,  5, 0x21e1cde6
128
        md5._.round     md5._.g, edx, eax, ebx, ecx, 14,  9, 0xc33707d6
129
        md5._.round     md5._.g, ecx, edx, eax, ebx,  3, 14, 0xf4d50d87
130
        md5._.round     md5._.g, ebx, ecx, edx, eax,  8, 20, 0x455a14ed
131
        md5._.round     md5._.g, eax, ebx, ecx, edx, 13,  5, 0xa9e3e905
132
        md5._.round     md5._.g, edx, eax, ebx, ecx,  2,  9, 0xfcefa3f8
133
        md5._.round     md5._.g, ecx, edx, eax, ebx,  7, 14, 0x676f02d9
134
        md5._.round     md5._.g, ebx, ecx, edx, eax, 12, 20, 0x8d2a4c8a
135
 
136
        md5._.round     md5._.h, eax, ebx, ecx, edx,  5,  4, 0xfffa3942
137
        md5._.round     md5._.h, edx, eax, ebx, ecx,  8, 11, 0x8771f681
138
        md5._.round     md5._.h, ecx, edx, eax, ebx, 11, 16, 0x6d9d6122
139
        md5._.round     md5._.h, ebx, ecx, edx, eax, 14, 23, 0xfde5380c
140
        md5._.round     md5._.h, eax, ebx, ecx, edx,  1,  4, 0xa4beea44
141
        md5._.round     md5._.h, edx, eax, ebx, ecx,  4, 11, 0x4bdecfa9
142
        md5._.round     md5._.h, ecx, edx, eax, ebx,  7, 16, 0xf6bb4b60
143
        md5._.round     md5._.h, ebx, ecx, edx, eax, 10, 23, 0xbebfbc70
144
        md5._.round     md5._.h, eax, ebx, ecx, edx, 13,  4, 0x289b7ec6
145
        md5._.round     md5._.h, edx, eax, ebx, ecx,  0, 11, 0xeaa127fa
146
        md5._.round     md5._.h, ecx, edx, eax, ebx,  3, 16, 0xd4ef3085
147
        md5._.round     md5._.h, ebx, ecx, edx, eax,  6, 23, 0x04881d05
148
        md5._.round     md5._.h, eax, ebx, ecx, edx,  9,  4, 0xd9d4d039
149
        md5._.round     md5._.h, edx, eax, ebx, ecx, 12, 11, 0xe6db99e5
150
        md5._.round     md5._.h, ecx, edx, eax, ebx, 15, 16, 0x1fa27cf8
151
        md5._.round     md5._.h, ebx, ecx, edx, eax,  2, 23, 0xc4ac5665
152
 
153
        md5._.round     md5._.i, eax, ebx, ecx, edx,  0,  6, 0xf4292244
154
        md5._.round     md5._.i, edx, eax, ebx, ecx,  7, 10, 0x432aff97
155
        md5._.round     md5._.i, ecx, edx, eax, ebx, 14, 15, 0xab9423a7
156
        md5._.round     md5._.i, ebx, ecx, edx, eax,  5, 21, 0xfc93a039
157
        md5._.round     md5._.i, eax, ebx, ecx, edx, 12,  6, 0x655b59c3
158
        md5._.round     md5._.i, edx, eax, ebx, ecx,  3, 10, 0x8f0ccc92
159
        md5._.round     md5._.i, ecx, edx, eax, ebx, 10, 15, 0xffeff47d
160
        md5._.round     md5._.i, ebx, ecx, edx, eax,  1, 21, 0x85845dd1
161
        md5._.round     md5._.i, eax, ebx, ecx, edx,  8,  6, 0x6fa87e4f
162
        md5._.round     md5._.i, edx, eax, ebx, ecx, 15, 10, 0xfe2ce6e0
163
        md5._.round     md5._.i, ecx, edx, eax, ebx,  6, 15, 0xa3014314
164
        md5._.round     md5._.i, ebx, ecx, edx, eax, 13, 21, 0x4e0811a1
165
        md5._.round     md5._.i, eax, ebx, ecx, edx,  4,  6, 0xf7537e82
166
        md5._.round     md5._.i, edx, eax, ebx, ecx, 11, 10, 0xbd3af235
167
        md5._.round     md5._.i, ecx, edx, eax, ebx,  2, 15, 0x2ad7d2bb
168
        md5._.round     md5._.i, ebx, ecx, edx, eax,  9, 21, 0xeb86d391
169
 
170
        mov     edi, [_hash]
171
        add     [edi + 0x0], eax
172
        add     [edi + 0x4], ebx
173
        add     [edi + 0x8], ecx
174
        add     [edi + 0xc], edx
175
 
176
        ret
177
endp
178
 
179
 
9216 dunkaist 180
proc md5.update uses ebx esi edi, _ctx, _msg, _size
7698 dunkaist 181
        mov     ebx, [_ctx]
182
        mov     ecx, [_size]
183
        add     [ebx + ctx_md5.msglen_0], ecx
184
        adc     [ebx + ctx_md5.msglen_1], 0
185
 
9216 dunkaist 186
.next_block:
7698 dunkaist 187
        mov     ebx, [_ctx]
188
        mov     esi, [_msg]
189
        mov     eax, [ebx + ctx_md5.index]
190
        and     eax, MD5_BLOCK_SIZE-1
191
        jnz     .copy_to_buf
192
        test    esi, MD5_ALIGN_MASK
193
        jnz     .copy_to_buf
9216 dunkaist 194
.no_copy:
7698 dunkaist 195
        ; data is aligned, hash it in place without copying
196
        mov     ebx, [_ctx]
197
        cmp     [_size], MD5_BLOCK_SIZE
198
        jb      .copy_quit
199
        lea     eax, [ebx + ctx_md5.hash]
200
        stdcall md5._.block, eax
201
        sub     [_size], MD5_BLOCK_SIZE
202
        add     esi, MD5_BLOCK_SIZE
203
        jmp     .no_copy
204
 
9216 dunkaist 205
.copy_to_buf:
7698 dunkaist 206
        lea     edi, [ebx + ctx_md5.block]
207
        add     edi, eax
208
        mov     ecx, MD5_BLOCK_SIZE
209
        sub     ecx, eax
210
        cmp     [_size], ecx
211
        jb      .copy_quit
212
        sub     [_size], ecx
213
        add     [_msg], ecx
214
        add     [ebx + ctx_md5.index], ecx
9216 dunkaist 215
        rep movsb
7698 dunkaist 216
        lea     eax, [ebx + ctx_md5.hash]
217
        lea     esi, [ebx + ctx_md5.block]
218
        stdcall md5._.block, eax
219
        jmp     .next_block
220
 
9216 dunkaist 221
.copy_quit:
7698 dunkaist 222
        mov     ebx, [_ctx]
223
        lea     edi, [ebx + ctx_md5.block]
224
        mov     eax, [ebx + ctx_md5.index]
225
        and     eax, MD5_BLOCK_SIZE-1
226
        add     edi, eax
227
        mov     ecx, [_size]
228
        add     [ebx + ctx_md5.index], ecx
9216 dunkaist 229
        rep movsb
230
.quit:
7698 dunkaist 231
 
232
        ret
233
endp
234
 
235
 
9216 dunkaist 236
proc md5.finish uses ebx esi edi, _ctx
7698 dunkaist 237
        mov     ebx, [_ctx]
238
        lea     edi, [ebx + ctx_md5.block]
239
        mov     ecx, [ebx + ctx_md5.msglen_0]
240
        and     ecx, MD5_BLOCK_SIZE-1
241
        add     edi, ecx
242
        mov     byte[edi], 0x80
243
        inc     edi
244
        neg     ecx
245
        add     ecx, MD5_BLOCK_SIZE
246
        cmp     ecx, 8
247
        ja      .last
248
 
249
        dec     ecx
250
        xor     eax, eax
9216 dunkaist 251
        rep stosb
7698 dunkaist 252
        lea     esi, [ebx + ctx_md5.block]
253
        lea     eax, [ebx + ctx_md5.hash]
254
        stdcall md5._.block, eax
255
        mov     ebx, [_ctx]
256
        lea     edi, [ebx + ctx_md5.block]
257
        mov     ecx, MD5_BLOCK_SIZE+1
9216 dunkaist 258
.last:
7698 dunkaist 259
        dec     ecx
260
        sub     ecx, 8
261
        xor     eax, eax
9216 dunkaist 262
        rep stosb
7698 dunkaist 263
        mov     eax, [ebx + ctx_md5.msglen_0]
264
        mov     edx, [ebx + ctx_md5.msglen_1]
265
        shld    edx, eax, 3
266
        shl     eax, 3
267
        mov     dword[edi], eax
268
        mov     dword[edi+4], edx
269
        lea     esi, [ebx + ctx_md5.block]
270
        lea     eax, [ebx + ctx_md5.hash]
271
        stdcall md5._.block, eax
272
 
273
        ret
274
endp
275
 
276
 
277
proc md5.oneshot _ctx, _data, _len
9216 dunkaist 278
        stdcall md5.init, [_ctx]
279
        stdcall md5.update, [_ctx], [_data], [_len]
280
        stdcall md5.finish, [_ctx]
281
        ret
7698 dunkaist 282
endp
283
 
284
 
285
iglobal
286
align MD5_ALIGN
287
md5._.hash_init dd 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0
288
endg