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