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
SHA1_HASH_SIZE  = 20
20
SHA1_BLOCK_SIZE = 64
21
 
22
SHA1_ALIGN      = 4
23
SHA1_ALIGN_MASK = SHA1_ALIGN - 1
24
 
25
struct ctx_sha1
26
        hash            rb SHA1_HASH_SIZE
27
        block           rb SHA1_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_sha1
35
end if
36
 
37
proc sha1._.f
38
        push    ebx ecx edx
39
        xor     ecx, edx
40
        and     ebx, ecx
41
        xor     ebx, edx
42
        mov     esi, ebx
43
        pop     edx ecx ebx
44
        ret
45
endp
46
 
47
proc sha1._.g
48
        push    ebx ecx edx
49
        xor     ebx, ecx
50
        xor     ebx, edx
51
        mov     esi, ebx
52
        pop     edx ecx ebx
53
        ret
54
endp
55
 
56
proc sha1._.h
57
        push    ebx ecx edx
58
        mov     esi, ebx
59
        and     ebx, ecx
60
        and     ecx, edx
61
        and     esi, edx
62
        or      ebx, ecx
63
        or      esi, ebx
64
        pop     edx ecx ebx
65
        ret
66
endp
67
 
68
macro sha1._.round f, k, c
69
{
70
        mov     esi, eax
71
        rol     esi, 5
72
        mov     [temp], esi
73
        call    f
74
 
75
        add     esi, edi
76
        add     [temp], esi
77
        mov     esi, [w + (c)*4]
78
        add     esi, k
79
        add     [temp], esi
80
 
81
        mov     edi, edx
82
        mov     edx, ecx
83
        mov     ecx, ebx
84
        rol     ecx, 30
85
        mov     ebx, eax
86
        mov     eax, [temp]
87
}
88
 
89
 
90
proc sha1.init _ctx
91
        mov     ebx, [_ctx]
92
        lea     edi, [ebx + ctx_sha1.hash]
93
        mov     esi, sha1._.hash_init
94
        mov     ecx, SHA1_HASH_SIZE/4
95
        rep     movsd
96
        xor     eax, eax
97
        mov     [ebx + ctx_sha1.index], eax
98
        mov     [ebx + ctx_sha1.msglen_0], eax
99
        mov     [ebx + ctx_sha1.msglen_1], eax
100
        ret
101
endp
102
 
103
 
104
proc sha1._.block _hash
105
locals
106
        temp     rd 1
107
        w        rd 80
108
endl
109
        lea     edi, [w]
110
        xor     ecx, ecx
111
    @@:
112
        mov     eax, [esi]
113
        add     esi, 4
114
        bswap   eax
115
        mov     [edi], eax
116
        add     edi, 4
117
        add     ecx, 1
118
        cmp     ecx, 16
119
        jne     @b
120
    @@:
121
        mov     eax, [w + (ecx -  3)*4]
122
        xor     eax, [w + (ecx -  8)*4]
123
        xor     eax, [w + (ecx - 14)*4]
124
        xor     eax, [w + (ecx - 16)*4]
125
        rol     eax, 1
126
        mov     [w + ecx*4], eax
127
        add     ecx, 1
128
        cmp     ecx, 80
129
        jne     @b
130
 
131
        mov     edi, [_hash]
132
        mov     eax, [edi + 0x00]
133
        mov     ebx, [edi + 0x04]
134
        mov     ecx, [edi + 0x08]
135
        mov     edx, [edi + 0x0c]
136
        mov     edi, [edi + 0x10]
137
 
138
        push    esi
139
 
140
repeat 20
141
        sha1._.round    sha1._.f, 0x5a827999, %-1
142
end repeat
143
 
144
repeat 20
145
        sha1._.round    sha1._.g, 0x6ed9eba1, %-1+20
146
end repeat
147
 
148
repeat 20
149
        sha1._.round    sha1._.h, 0x8f1bbcdc, %-1+40
150
end repeat
151
 
152
repeat 20
153
        sha1._.round    sha1._.g, 0xca62c1d6, %-1+60
154
end repeat
155
 
156
        pop     esi
157
 
158
        mov     [temp], edi
159
        mov     edi, [_hash]
160
        add     [edi + 0x00], eax
161
        add     [edi + 0x04], ebx
162
        add     [edi + 0x08], ecx
163
        add     [edi + 0x0c], edx
164
        mov     eax, [temp]
165
        add     [edi + 0x10], eax
166
 
167
        ret
168
endp
169
 
170
 
171
proc sha1.update _ctx, _msg, _size
172
        mov     ebx, [_ctx]
173
        mov     ecx, [_size]
174
        add     [ebx + ctx_sha1.msglen_0], ecx
175
        adc     [ebx + ctx_sha1.msglen_1], 0
176
 
177
  .next_block:
178
        mov     ebx, [_ctx]
179
        mov     esi, [_msg]
180
        mov     eax, [ebx + ctx_sha1.index]
181
        and     eax, SHA1_BLOCK_SIZE-1
182
        jnz     .copy_to_buf
183
        test    esi, SHA1_ALIGN_MASK
184
        jnz     .copy_to_buf
185
  .no_copy:
186
        ; data is aligned, hash it in place without copying
187
        mov     ebx, [_ctx]
188
        cmp     [_size], SHA1_BLOCK_SIZE
189
        jb      .copy_quit
190
        lea     eax, [ebx + ctx_sha1.hash]
191
        stdcall sha1._.block, eax
192
        sub     [_size], SHA1_BLOCK_SIZE
193
;        add     esi, SHA1_BLOCK_SIZE           ; FIXME
194
        jmp     .no_copy
195
 
196
  .copy_to_buf:
197
        lea     edi, [ebx + ctx_sha1.block]
198
        add     edi, eax
199
        mov     ecx, SHA1_BLOCK_SIZE
200
        sub     ecx, eax
201
        cmp     [_size], ecx
202
        jb      .copy_quit
203
        sub     [_size], ecx
204
        add     [_msg], ecx
205
        add     [ebx + ctx_sha1.index], ecx
206
        rep     movsb
207
        lea     eax, [ebx + ctx_sha1.hash]
208
        lea     esi, [ebx + ctx_sha1.block]
209
        stdcall sha1._.block, eax
210
        jmp     .next_block
211
 
212
  .copy_quit:
213
        mov     ebx, [_ctx]
214
        lea     edi, [ebx + ctx_sha1.block]
215
        mov     eax, [ebx + ctx_sha1.index]
216
        and     eax, SHA1_BLOCK_SIZE-1
217
        add     edi, eax
218
        mov     ecx, [_size]
219
        add     [ebx + ctx_sha1.index], ecx
220
        rep     movsb
221
  .quit:
222
 
223
        ret
224
endp
225
 
226
 
227
proc sha1.final _ctx
228
        mov     ebx, [_ctx]
229
        lea     edi, [ebx + ctx_sha1.block]
230
        mov     ecx, [ebx + ctx_sha1.msglen_0]
231
        and     ecx, SHA1_BLOCK_SIZE-1
232
        add     edi, ecx
233
        mov     byte[edi], 0x80
234
        inc     edi
235
        neg     ecx
236
        add     ecx, SHA1_BLOCK_SIZE
237
        cmp     ecx, 8
238
        ja      .last
239
 
240
        dec     ecx
241
        xor     eax, eax
242
        rep     stosb
243
        lea     esi, [ebx + ctx_sha1.block]
244
        lea     eax, [ebx + ctx_sha1.hash]
245
        stdcall sha1._.block, eax
246
        mov     ebx, [_ctx]
247
        lea     edi, [ebx + ctx_sha1.block]
248
        mov     ecx, SHA1_BLOCK_SIZE+1
249
  .last:
250
        dec     ecx
251
        sub     ecx, 8
252
        xor     eax, eax
253
        rep     stosb
254
        mov     eax, [ebx + ctx_sha1.msglen_0]
255
        mov     edx, [ebx + ctx_sha1.msglen_1]
256
        shld    edx, eax, 3
257
        shl     eax, 3
258
        bswap   eax
259
        bswap   edx
260
        mov     dword[edi], edx
261
        mov     dword[edi+4], eax
262
        lea     esi, [ebx + ctx_sha1.block]
263
        lea     eax, [ebx + ctx_sha1.hash]
264
        stdcall sha1._.block, eax
265
 
266
        mov     ebx, [_ctx]
267
        lea     eax, [ebx + ctx_sha1.hash]
268
        stdcall sha1._.postprocess, ebx, eax
269
 
270
        ret
271
endp
272
 
273
 
274
proc sha1._.postprocess _ctx, _hash
275
        mov     ecx, 5
276
        mov     esi, [_hash]
277
        mov     edi, esi
278
    @@:
279
        lodsd
280
        bswap   eax
281
        stosd
282
        dec     ecx
283
        jnz     @b
284
        ret
285
endp
286
 
287
 
288
proc sha1.oneshot _ctx, _data, _len
289
	stdcall	sha1.init, [_ctx]
290
	stdcall	sha1.update, [_ctx], [_data], [_len]
291
	stdcall	sha1.final, [_ctx]
292
	ret
293
endp
294
 
295
 
296
iglobal
297
align SHA1_ALIGN
298
sha1._.hash_init dd 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0
299
endg