Subversion Repositories Kolibri OS

Rev

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

Rev 3115 Rev 3431
Line -... Line 1...
-
 
1
;    libcrash -- cryptographic hash functions
-
 
2
;
-
 
3
;    Copyright (C) 2012-2013 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
 
1
format MS COFF
18
format MS COFF
Line 2... Line 19...
2
 
19
 
Line 3... Line 20...
3
public @EXPORT as 'EXPORTS'
20
public @EXPORT as 'EXPORTS'
Line 9... Line 26...
9
;include '../../../../debug.inc'
26
;include '../../../../debug.inc'
Line 10... Line 27...
10
 
27
 
11
purge section,mov,add,sub
28
purge section,mov,add,sub
Line -... Line 29...
-
 
29
section '.flat' code readable align 16
12
section '.flat' code readable align 16
30
 
13
 
-
 
14
include 'libcrash.inc'
31
 
15
include 'crc32.asm'
32
include 'libcrash.inc'
16
include 'md4.asm'
33
include 'md4.asm'
17
include 'md5.asm'
34
include 'md5.asm'
18
include 'sha1.asm'
35
include 'sha1.asm'
Line 19... Line 36...
19
include 'sha224_256.asm'
36
include 'sha224_256.asm'
20
include 'sha384_512.asm'
-
 
21
 
-
 
22
 
-
 
23
proc lib_init
-
 
24
	; generate crc32 table
-
 
25
	bts	[crash._.init_flags], 0
-
 
26
	jc	.crc32_table_done
-
 
27
	
-
 
28
	mov	edi, crash._.crc32_table
-
 
29
	xor	ecx, ecx
-
 
30
	mov	edx, 0xedb88320
-
 
31
  .1:
-
 
32
	mov	ebx, 8
-
 
33
	mov	eax, ecx
-
 
34
  .2:
-
 
35
	shr	eax, 1
-
 
36
	jnc	@f
-
 
37
	xor	eax, edx
-
 
38
    @@:
-
 
39
	dec	ebx
-
 
40
	jnz	.2
-
 
41
	stosd
-
 
42
	inc	cl
37
include 'sha384_512.asm'
43
	jnz	.1
38
 
Line 44... Line 39...
44
 
39
 
45
  .crc32_table_done:
40
proc lib_init
46
	ret
41
	ret
-
 
42
endp
-
 
43
 
47
endp
44
 
48
 
45
proc crash.hash  _hid, _hash, _data, _len, _callback, _msglen
49
 
46
locals
50
proc crash.hash  _type, _hash, _data, _len, _callback, _msglen
47
	hash_func	rd	1
-
 
48
	final		rd	1
-
 
49
	hi		rd	1
51
locals
50
endl
52
	hash_func	rd	1
51
	mov	eax, [_hid]
53
endl
52
	imul	eax, sizeof.crash_item
54
	mov	eax, [_type]
53
	lea	eax, [crash._.table + eax]
55
	mov	ecx, eax
54
	mov	[hi], eax
56
	and	eax, 0x000000ff
55
 
57
	sub	eax, 1
56
	mov	eax, [hi]
58
	mov	edx, [crash._.table + eax*8]
57
	mov	edx, [eax + crash_item.function]
Line -... Line 58...
-
 
58
	mov	esi, [eax + crash_item.init_val]
-
 
59
	mov	edi, [_hash]
-
 
60
	mov	[hash_func], edx
-
 
61
	mov	ecx, [hi]
-
 
62
	mov	ecx, [ecx + crash_item.len_in]
-
 
63
	rep	movsd
-
 
64
 
-
 
65
	mov	[final], 0
-
 
66
  .first:
-
 
67
	mov	eax, [_msglen]
-
 
68
	mov	ecx, [_len]
-
 
69
	add	[eax], ecx
-
 
70
	mov	esi, [_data]
-
 
71
	test	ecx, ecx
-
 
72
	jz	.callback
-
 
73
  .begin:
59
	mov	esi, [crash._.table + eax*8 + 4]
74
	mov	eax, [hi]
-
 
75
	mov	eax, [eax + crash_item.len_blk]
-
 
76
	sub	[_len], eax
-
 
77
	jnc	@f
-
 
78
	add	[_len], eax
-
 
79
	jmp	.endofblock
-
 
80
    @@:
-
 
81
	stdcall	[hash_func], [_hash], [_data]
-
 
82
	jmp	.begin
-
 
83
  .endofblock:
-
 
84
	cmp	[final], 1
-
 
85
	je	.quit
-
 
86
 
Line -... Line 87...
-
 
87
  .callback:
-
 
88
	call	[_callback]
-
 
89
	test	eax, eax
-
 
90
	jz	@f
-
 
91
	mov	[_len], eax
-
 
92
	jmp	.first
-
 
93
    @@:
-
 
94
 
-
 
95
	mov	edi, [_data]
-
 
96
	mov	ecx, [_len]
-
 
97
	rep	movsb
-
 
98
	mov	eax, [_msglen]
-
 
99
	mov	eax, [eax]
-
 
100
	mov	edx, [hi]
-
 
101
	mov	edx, [edx + crash_item.len_blk]
-
 
102
	sub	edx, 1
-
 
103
	and	eax, edx
-
 
104
	mov	edx, [hi]
-
 
105
	mov	ecx, [edx + crash_item.len_blk]
-
 
106
	sub	ecx, [edx + crash_item.len_size]
-
 
107
	sub	ecx, eax
-
 
108
	ja	@f
-
 
109
	add	ecx, [edx + crash_item.len_blk]
-
 
110
    @@:
-
 
111
	add	[_len], ecx
-
 
112
	mov	eax, [hi]
-
 
113
	mov	byte[edi], 0x80
-
 
114
	add	edi, 1
-
 
115
	sub	ecx, 1
-
 
116
	mov	al, 0
-
 
117
	rep	stosb
-
 
118
	push	ecx
-
 
119
	xor	eax, eax
-
 
120
	mov	ecx, [hi]
-
 
121
	mov	ecx, [ecx + crash_item.len_size]
-
 
122
	sub	ecx, 8			; FIXME for > 2^64 input length
-
 
123
	shr	ecx, 2
-
 
124
	rep	stosd
-
 
125
	pop	ecx
-
 
126
	mov	eax, [_msglen]
-
 
127
	mov	eax, [eax]
-
 
128
	mov	edx, 8
-
 
129
	mul	edx
-
 
130
	mov	ecx, [hi]
-
 
131
	cmp	[ecx + crash_item.endianness], LIBCRASH_ENDIAN_BIG
-
 
132
	jne	@f
-
 
133
	bswap	eax
-
 
134
	bswap	edx
-
 
135
	xchg	eax, edx
-
 
136
    @@:
-
 
137
	mov	dword[edi], eax
-
 
138
	mov	dword[edi + 4], edx
60
	mov	edi, [_hash]
139
	mov	ecx, [hi]
61
	mov	[hash_func], edx
140
	mov	eax, [ecx + crash_item.len_size]
Line 62... Line 141...
62
	and	ecx, 0x0000ff00
141
	add	[_len], eax
-
 
142
	mov	[final], 1
-
 
143
	jmp	.first
-
 
144
  .quit:
-
 
145
	mov	eax, [hi]
-
 
146
	stdcall [eax + crash_item.postproc], [eax + crash_item.len_out], [_hash]
-
 
147
	ret
-
 
148
endp
-
 
149
 
-
 
150
 
-
 
151
proc crash._.md4_md5_postprocess _len_out, _hash
-
 
152
	ret
-
 
153
endp
-
 
154
 
-
 
155
 
-
 
156
proc crash._.sha1_224_256_postprocess _len_out, _hash
-
 
157
	mov	ecx, [_len_out]
-
 
158
	mov	esi, [_hash]
-
 
159
	mov	edi, esi
-
 
160
    @@:
-
 
161
	lodsd
-
 
162
	bswap	eax
-
 
163
	stosd
-
 
164
	dec	ecx
-
 
165
	jnz	@b
-
 
166
	ret
-
 
167
endp
-
 
168
 
-
 
169
 
-
 
170
proc crash._.sha384_512_postprocess _len_out, _hash
63
	shr	ecx, 8
171
	mov	ecx, [_len_out]
-
 
172
	mov	esi, [_hash]
-
 
173
	mov	edi, esi
-
 
174
    @@:
-
 
175
	lodsd
-
 
176
	mov	ebx, eax
-
 
177
	lodsd
-
 
178
	bswap	eax
-
 
179
	bswap	ebx
-
 
180
	stosd
-
 
181
	mov	eax, ebx
-
 
182
	stosd
-
 
183
	dec	ecx
64
	rep	movsd
184
	jnz	@b
65
 
185
	emms
66
	stdcall	[hash_func], [_hash], [_data], [_len], [_callback], [_msglen]
186
	ret
67
 
187
endp
68
	ret
188
 
69
endp
189
 
70
 
-
 
71
 
190
proc crash.bin2hex _bin, _hex, _hid	; FIXME _hid param?
72
proc crash.bin2hex _bin, _hex, _type
191
	mov	eax, [_hid]
73
	xor	eax, eax
192
	imul	eax, sizeof.crash_item
74
	mov	ebx, crash._.bin2hex_table
-
 
75
	mov	esi, [_bin]
193
	mov	ecx, [crash._.table + eax + crash_item.len_out]
76
	mov	edi, [_hex]
194
	mov	ebx, crash._.bin2hex_table
77
	mov	ecx, [_type]
195
	mov	esi, [_bin]
78
	and	ecx, 0x00ff0000
196
	mov	edi, [_hex]
79
	shr	ecx, 16 - 2
197
	shl	ecx, 2
80
  .byte:
198
  .next_byte:
-
 
199
	xor	eax, eax
81
	lodsb
200
	lodsb
82
	mov	ah, al
201
	shl	eax, 4
83
	shr	ah, 4
202
	shr	al, 4
Line 84... Line 203...
84
	and	al, 0x0f
203
	xlatb
85
	xlatb
-
 
-
 
204
	xchg	al, ah
86
	xchg	al, ah
205
	xlatb
87
	xlatb
206
	stosw
88
	stosw
207
	dec	ecx
89
	dec	ecx
208
	jnz	.next_byte
90
	jnz	.byte
209
	xor	al, al
91
 
210
	stosb
92
	ret
211
	ret
93
endp
212
endp
Line 94... Line 213...
94
 
213
 
-
 
214
 
95
 
215
crash._.bin2hex_table	db	'0123456789abcdef'
96
crash._.bin2hex_table	db	'0123456789abcdef'
216
 
97
crash._.table:		;	hash function,	address of initial hash value
217
; see crash_item struct for details
-
 
218
crash._.table		dd	crash.md4,	crash._.md4_init,    crash._.md4_md5_postprocess,       4,  4,  64,  8, 0
98
			dd	crash.crc32,	crash._.crc_init
219
			dd	crash.md5,	crash._.md5_init,    crash._.md4_md5_postprocess,       4,  4,  64,  8, 0
-
 
220
			dd	crash.sha1,	crash._.sha1_init,   crash._.sha1_224_256_postprocess,  5,  5,  64,  8, 1
99
			dd	crash.md4,	crash._.md4_init
221
			dd	crash.sha256,	crash._.sha224_init, crash._.sha1_224_256_postprocess,  8,  7,  64,  8, 1
-
 
222
			dd	crash.sha256,	crash._.sha256_init, crash._.sha1_224_256_postprocess,  8,  8,  64,  8, 1
100
			dd	crash.md5,	crash._.md5_init
223
			dd	crash.sha512,	crash._.sha384_init, crash._.sha384_512_postprocess, 16, 12, 128, 16, 1
101
			dd	crash.sha1,	crash._.sha1_init
224
			dd	crash.sha512,	crash._.sha512_init, crash._.sha384_512_postprocess, 16, 16, 128, 16, 1
-
 
225
 
102
			dd	crash.sha256,	crash._.sha224_init
226
crash._.crc_init	dd	0xffffffff
103
			dd	crash.sha256,	crash._.sha256_init
227
 
Line 104... Line -...
104
			dd	crash.sha512,	crash._.sha384_init
-
 
105
			dd	crash.sha512,	crash._.sha512_init
228
crash._.md4_init:
106
 
229
crash._.md5_init:
107
crash._.crc_init	dd	0xffffffff
230
crash._.sha1_init	dd	0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0
108
crash._.md4_init:
231
 
109
crash._.md5_init:
232
crash._.sha224_init	dd	0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4
Line 144... Line 267...
144
				0xca273eceea26619c, 0xd186b8c721c0c207, 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178,\
267
				0xca273eceea26619c, 0xd186b8c721c0c207, 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178,\
145
				0x06f067aa72176fba, 0x0a637dc5a2c898a6, 0x113f9804bef90dae, 0x1b710b35131c471b,\
268
				0x06f067aa72176fba, 0x0a637dc5a2c898a6, 0x113f9804bef90dae, 0x1b710b35131c471b,\
146
				0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc, 0x431d67c49c100d4c,\
269
				0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc, 0x431d67c49c100d4c,\
147
				0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817
270
				0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817
Line -... Line 271...
-
 
271
 
148
 
272
 
149
align 4
273
align 4
Line 150... Line 274...
150
@EXPORT:
274
@EXPORT:
151
 
275