Subversion Repositories Kolibri OS

Rev

Rev 3431 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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