Subversion Repositories Kolibri OS

Rev

Rev 3431 | Go to most recent revision | Show entire file | Regard 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
 
-
 
18
 
-
 
19
MD5_BLOCK_SIZE = 64
-
 
20
MD5_HASH_SIZE  = 16
-
 
21
MD5_ALIGN      = 4
-
 
22
MD5_ALIGN_MASK = MD5_ALIGN - 1
-
 
23
 
-
 
24
struct ctx_md5
-
 
25
        hash            rb MD5_HASH_SIZE
-
 
26
        block           rb MD5_BLOCK_SIZE
-
 
27
        index           rd 1
-
 
28
        msglen_0        rd 1
-
 
29
        msglen_1        rd 1
-
 
30
ends
-
 
31
 
17
 
32
 
18
macro crash.md5.f b, c, d
33
macro md5._.f b, c, d
19
{
34
{
20
	push	c
35
        push    c
21
	xor	c, d
36
        xor     c, d
22
	and	b, c
37
        and     b, c
23
	xor	b, d
38
        xor     b, d
24
	pop	c
39
        pop     c
Line 25... Line 40...
25
}
40
}
26
 
41
 
27
macro crash.md5.g b, c, d
42
macro md5._.g b, c, d
28
{
43
{
29
	push	c  d
44
        push    c  d
30
	and	b, d
45
        and     b, d
31
	not	d
46
        not     d
32
	and	c, d
47
        and     c, d
33
	or	b, c
48
        or      b, c
Line 34... Line 49...
34
	pop	d  c
49
        pop     d  c
35
}
50
}
36
 
51
 
37
macro crash.md5.h b, c, d
52
macro md5._.h b, c, d
38
{
53
{
Line 39... Line 54...
39
	xor	b, c
54
        xor     b, c
40
	xor	b, d
55
        xor     b, d
41
}
56
}
42
 
57
 
43
macro crash.md5.i b, c, d
58
macro md5._.i b, c, d
44
{
59
{
45
	push	d
60
        push    d
46
	not	d
61
        not     d
Line 47... Line 62...
47
	or	b, d
62
        or      b, d
48
	xor	b, c
63
        xor     b, c
49
	pop	d
64
        pop     d
50
}
65
}
51
 
66
 
52
macro crash.md5.round func, a, b, c, d, index, shift, ac
67
macro md5._.round func, a, b, c, d, index, shift, ac
Line 59... Line 74...
59
	pop	b
74
        pop     b
60
	add	a, b
75
        add     a, b
61
}
76
}
Line -... Line 77...
-
 
77
 
-
 
78
 
-
 
79
proc md5.init _ctx
-
 
80
        mov     ebx, [_ctx]
-
 
81
        lea     edi, [ebx + ctx_md5.hash]
-
 
82
        mov     esi, md5._.hash_init
-
 
83
        mov     ecx, MD5_HASH_SIZE/4
-
 
84
        rep     movsd
-
 
85
        xor     eax, eax
-
 
86
        mov     [ebx + ctx_md5.index], eax
-
 
87
        mov     [ebx + ctx_md5.msglen_0], eax
-
 
88
        mov     [ebx + ctx_md5.msglen_1], eax
-
 
89
        ret
-
 
90
endp
62
 
91
 
-
 
92
 
63
 
93
proc md5._.block _hash
64
proc crash.md5 _md5, _data
94
 
65
	mov	edi, [_md5]
95
        mov     edi, [_hash]
66
	mov	eax, [edi + 0x0]
96
        mov     eax, [edi + 0x0]
67
	mov	ebx, [edi + 0x4]
97
        mov     ebx, [edi + 0x4]
Line 68... Line 98...
68
	mov	ecx, [edi + 0x8]
98
        mov     ecx, [edi + 0x8]
69
	mov	edx, [edi + 0xc]
99
        mov     edx, [edi + 0xc]
70
 
100
 
71
	crash.md5.round		crash.md5.f, eax, ebx, ecx, edx,  0,  7, 0xd76aa478
101
        md5._.round     md5._.f, eax, ebx, ecx, edx,  0,  7, 0xd76aa478
72
	crash.md5.round		crash.md5.f, edx, eax, ebx, ecx,  1, 12, 0xe8c7b756
102
        md5._.round     md5._.f, edx, eax, ebx, ecx,  1, 12, 0xe8c7b756
73
	crash.md5.round		crash.md5.f, ecx, edx, eax, ebx,  2, 17, 0x242070db
103
        md5._.round     md5._.f, ecx, edx, eax, ebx,  2, 17, 0x242070db
74
	crash.md5.round		crash.md5.f, ebx, ecx, edx, eax,  3, 22, 0xc1bdceee
104
        md5._.round     md5._.f, ebx, ecx, edx, eax,  3, 22, 0xc1bdceee
75
	crash.md5.round		crash.md5.f, eax, ebx, ecx, edx,  4,  7, 0xf57c0faf
105
        md5._.round     md5._.f, eax, ebx, ecx, edx,  4,  7, 0xf57c0faf
76
	crash.md5.round		crash.md5.f, edx, eax, ebx, ecx,  5, 12, 0x4787c62a
106
        md5._.round     md5._.f, edx, eax, ebx, ecx,  5, 12, 0x4787c62a
77
	crash.md5.round		crash.md5.f, ecx, edx, eax, ebx,  6, 17, 0xa8304613
107
        md5._.round     md5._.f, ecx, edx, eax, ebx,  6, 17, 0xa8304613
78
	crash.md5.round		crash.md5.f, ebx, ecx, edx, eax,  7, 22, 0xfd469501
108
        md5._.round     md5._.f, ebx, ecx, edx, eax,  7, 22, 0xfd469501
79
	crash.md5.round		crash.md5.f, eax, ebx, ecx, edx,  8,  7, 0x698098d8
109
        md5._.round     md5._.f, eax, ebx, ecx, edx,  8,  7, 0x698098d8
80
	crash.md5.round		crash.md5.f, edx, eax, ebx, ecx,  9, 12, 0x8b44f7af
110
        md5._.round     md5._.f, edx, eax, ebx, ecx,  9, 12, 0x8b44f7af
81
	crash.md5.round		crash.md5.f, ecx, edx, eax, ebx, 10, 17, 0xffff5bb1
111
        md5._.round     md5._.f, ecx, edx, eax, ebx, 10, 17, 0xffff5bb1
82
	crash.md5.round		crash.md5.f, ebx, ecx, edx, eax, 11, 22, 0x895cd7be
112
        md5._.round     md5._.f, ebx, ecx, edx, eax, 11, 22, 0x895cd7be
83
	crash.md5.round		crash.md5.f, eax, ebx, ecx, edx, 12,  7, 0x6b901122
113
        md5._.round     md5._.f, eax, ebx, ecx, edx, 12,  7, 0x6b901122
84
	crash.md5.round		crash.md5.f, edx, eax, ebx, ecx, 13, 12, 0xfd987193
114
        md5._.round     md5._.f, edx, eax, ebx, ecx, 13, 12, 0xfd987193
85
	crash.md5.round		crash.md5.f, ecx, edx, eax, ebx, 14, 17, 0xa679438e
115
        md5._.round     md5._.f, ecx, edx, eax, ebx, 14, 17, 0xa679438e
86
	crash.md5.round		crash.md5.f, ebx, ecx, edx, eax, 15, 22, 0x49b40821
116
        md5._.round     md5._.f, ebx, ecx, edx, eax, 15, 22, 0x49b40821
87
 
117
 
88
	crash.md5.round		crash.md5.g, eax, ebx, ecx, edx,  1,  5, 0xf61e2562
118
        md5._.round     md5._.g, eax, ebx, ecx, edx,  1,  5, 0xf61e2562
89
	crash.md5.round		crash.md5.g, edx, eax, ebx, ecx,  6,  9, 0xc040b340
119
        md5._.round     md5._.g, edx, eax, ebx, ecx,  6,  9, 0xc040b340
90
	crash.md5.round		crash.md5.g, ecx, edx, eax, ebx, 11, 14, 0x265e5a51
120
        md5._.round     md5._.g, ecx, edx, eax, ebx, 11, 14, 0x265e5a51
91
	crash.md5.round		crash.md5.g, ebx, ecx, edx, eax,  0, 20, 0xe9b6c7aa
121
        md5._.round     md5._.g, ebx, ecx, edx, eax,  0, 20, 0xe9b6c7aa
92
	crash.md5.round		crash.md5.g, eax, ebx, ecx, edx,  5,  5, 0xd62f105d
122
        md5._.round     md5._.g, eax, ebx, ecx, edx,  5,  5, 0xd62f105d
93
	crash.md5.round		crash.md5.g, edx, eax, ebx, ecx, 10,  9, 0x02441453
123
        md5._.round     md5._.g, edx, eax, ebx, ecx, 10,  9, 0x02441453
94
	crash.md5.round		crash.md5.g, ecx, edx, eax, ebx, 15, 14, 0xd8a1e681
124
        md5._.round     md5._.g, ecx, edx, eax, ebx, 15, 14, 0xd8a1e681
95
	crash.md5.round		crash.md5.g, ebx, ecx, edx, eax,  4, 20, 0xe7d3fbc8
125
        md5._.round     md5._.g, ebx, ecx, edx, eax,  4, 20, 0xe7d3fbc8
96
	crash.md5.round		crash.md5.g, eax, ebx, ecx, edx,  9,  5, 0x21e1cde6
126
        md5._.round     md5._.g, eax, ebx, ecx, edx,  9,  5, 0x21e1cde6
97
	crash.md5.round		crash.md5.g, edx, eax, ebx, ecx, 14,  9, 0xc33707d6
127
        md5._.round     md5._.g, edx, eax, ebx, ecx, 14,  9, 0xc33707d6
98
	crash.md5.round		crash.md5.g, ecx, edx, eax, ebx,  3, 14, 0xf4d50d87
128
        md5._.round     md5._.g, ecx, edx, eax, ebx,  3, 14, 0xf4d50d87
99
	crash.md5.round		crash.md5.g, ebx, ecx, edx, eax,  8, 20, 0x455a14ed
129
        md5._.round     md5._.g, ebx, ecx, edx, eax,  8, 20, 0x455a14ed
100
	crash.md5.round		crash.md5.g, eax, ebx, ecx, edx, 13,  5, 0xa9e3e905
130
        md5._.round     md5._.g, eax, ebx, ecx, edx, 13,  5, 0xa9e3e905
101
	crash.md5.round		crash.md5.g, edx, eax, ebx, ecx,  2,  9, 0xfcefa3f8
131
        md5._.round     md5._.g, edx, eax, ebx, ecx,  2,  9, 0xfcefa3f8
102
	crash.md5.round		crash.md5.g, ecx, edx, eax, ebx,  7, 14, 0x676f02d9
132
        md5._.round     md5._.g, ecx, edx, eax, ebx,  7, 14, 0x676f02d9
103
	crash.md5.round		crash.md5.g, ebx, ecx, edx, eax, 12, 20, 0x8d2a4c8a
133
        md5._.round     md5._.g, ebx, ecx, edx, eax, 12, 20, 0x8d2a4c8a
104
 
134
 
105
	crash.md5.round		crash.md5.h, eax, ebx, ecx, edx,  5,  4, 0xfffa3942
135
        md5._.round     md5._.h, eax, ebx, ecx, edx,  5,  4, 0xfffa3942
106
	crash.md5.round		crash.md5.h, edx, eax, ebx, ecx,  8, 11, 0x8771f681
136
        md5._.round     md5._.h, edx, eax, ebx, ecx,  8, 11, 0x8771f681
107
	crash.md5.round		crash.md5.h, ecx, edx, eax, ebx, 11, 16, 0x6d9d6122
137
        md5._.round     md5._.h, ecx, edx, eax, ebx, 11, 16, 0x6d9d6122
108
	crash.md5.round		crash.md5.h, ebx, ecx, edx, eax, 14, 23, 0xfde5380c
138
        md5._.round     md5._.h, ebx, ecx, edx, eax, 14, 23, 0xfde5380c
109
	crash.md5.round		crash.md5.h, eax, ebx, ecx, edx,  1,  4, 0xa4beea44
139
        md5._.round     md5._.h, eax, ebx, ecx, edx,  1,  4, 0xa4beea44
110
	crash.md5.round		crash.md5.h, edx, eax, ebx, ecx,  4, 11, 0x4bdecfa9
140
        md5._.round     md5._.h, edx, eax, ebx, ecx,  4, 11, 0x4bdecfa9
111
	crash.md5.round		crash.md5.h, ecx, edx, eax, ebx,  7, 16, 0xf6bb4b60
141
        md5._.round     md5._.h, ecx, edx, eax, ebx,  7, 16, 0xf6bb4b60
112
	crash.md5.round		crash.md5.h, ebx, ecx, edx, eax, 10, 23, 0xbebfbc70
142
        md5._.round     md5._.h, ebx, ecx, edx, eax, 10, 23, 0xbebfbc70
113
	crash.md5.round		crash.md5.h, eax, ebx, ecx, edx, 13,  4, 0x289b7ec6
143
        md5._.round     md5._.h, eax, ebx, ecx, edx, 13,  4, 0x289b7ec6
114
	crash.md5.round		crash.md5.h, edx, eax, ebx, ecx,  0, 11, 0xeaa127fa
144
        md5._.round     md5._.h, edx, eax, ebx, ecx,  0, 11, 0xeaa127fa
115
	crash.md5.round		crash.md5.h, ecx, edx, eax, ebx,  3, 16, 0xd4ef3085
145
        md5._.round     md5._.h, ecx, edx, eax, ebx,  3, 16, 0xd4ef3085
116
	crash.md5.round		crash.md5.h, ebx, ecx, edx, eax,  6, 23, 0x04881d05
146
        md5._.round     md5._.h, ebx, ecx, edx, eax,  6, 23, 0x04881d05
117
	crash.md5.round		crash.md5.h, eax, ebx, ecx, edx,  9,  4, 0xd9d4d039
147
        md5._.round     md5._.h, eax, ebx, ecx, edx,  9,  4, 0xd9d4d039
118
	crash.md5.round		crash.md5.h, edx, eax, ebx, ecx, 12, 11, 0xe6db99e5
148
        md5._.round     md5._.h, edx, eax, ebx, ecx, 12, 11, 0xe6db99e5
119
	crash.md5.round		crash.md5.h, ecx, edx, eax, ebx, 15, 16, 0x1fa27cf8
149
        md5._.round     md5._.h, ecx, edx, eax, ebx, 15, 16, 0x1fa27cf8
120
	crash.md5.round		crash.md5.h, ebx, ecx, edx, eax,  2, 23, 0xc4ac5665
150
        md5._.round     md5._.h, ebx, ecx, edx, eax,  2, 23, 0xc4ac5665
121
 
151
 
122
	crash.md5.round		crash.md5.i, eax, ebx, ecx, edx,  0,  6, 0xf4292244
152
        md5._.round     md5._.i, eax, ebx, ecx, edx,  0,  6, 0xf4292244
123
	crash.md5.round		crash.md5.i, edx, eax, ebx, ecx,  7, 10, 0x432aff97
153
        md5._.round     md5._.i, edx, eax, ebx, ecx,  7, 10, 0x432aff97
124
	crash.md5.round		crash.md5.i, ecx, edx, eax, ebx, 14, 15, 0xab9423a7
154
        md5._.round     md5._.i, ecx, edx, eax, ebx, 14, 15, 0xab9423a7
125
	crash.md5.round		crash.md5.i, ebx, ecx, edx, eax,  5, 21, 0xfc93a039
155
        md5._.round     md5._.i, ebx, ecx, edx, eax,  5, 21, 0xfc93a039
126
	crash.md5.round		crash.md5.i, eax, ebx, ecx, edx, 12,  6, 0x655b59c3
156
        md5._.round     md5._.i, eax, ebx, ecx, edx, 12,  6, 0x655b59c3
127
	crash.md5.round		crash.md5.i, edx, eax, ebx, ecx,  3, 10, 0x8f0ccc92
157
        md5._.round     md5._.i, edx, eax, ebx, ecx,  3, 10, 0x8f0ccc92
128
	crash.md5.round		crash.md5.i, ecx, edx, eax, ebx, 10, 15, 0xffeff47d
158
        md5._.round     md5._.i, ecx, edx, eax, ebx, 10, 15, 0xffeff47d
129
	crash.md5.round		crash.md5.i, ebx, ecx, edx, eax,  1, 21, 0x85845dd1
159
        md5._.round     md5._.i, ebx, ecx, edx, eax,  1, 21, 0x85845dd1
130
	crash.md5.round		crash.md5.i, eax, ebx, ecx, edx,  8,  6, 0x6fa87e4f
160
        md5._.round     md5._.i, eax, ebx, ecx, edx,  8,  6, 0x6fa87e4f
131
	crash.md5.round		crash.md5.i, edx, eax, ebx, ecx, 15, 10, 0xfe2ce6e0
161
        md5._.round     md5._.i, edx, eax, ebx, ecx, 15, 10, 0xfe2ce6e0
132
	crash.md5.round		crash.md5.i, ecx, edx, eax, ebx,  6, 15, 0xa3014314
162
        md5._.round     md5._.i, ecx, edx, eax, ebx,  6, 15, 0xa3014314
133
	crash.md5.round		crash.md5.i, ebx, ecx, edx, eax, 13, 21, 0x4e0811a1
163
        md5._.round     md5._.i, ebx, ecx, edx, eax, 13, 21, 0x4e0811a1
134
	crash.md5.round		crash.md5.i, eax, ebx, ecx, edx,  4,  6, 0xf7537e82
164
        md5._.round     md5._.i, eax, ebx, ecx, edx,  4,  6, 0xf7537e82
Line 135... Line 165...
135
	crash.md5.round		crash.md5.i, edx, eax, ebx, ecx, 11, 10, 0xbd3af235
165
        md5._.round     md5._.i, edx, eax, ebx, ecx, 11, 10, 0xbd3af235
136
	crash.md5.round		crash.md5.i, ecx, edx, eax, ebx,  2, 15, 0x2ad7d2bb
166
        md5._.round     md5._.i, ecx, edx, eax, ebx,  2, 15, 0x2ad7d2bb
137
	crash.md5.round		crash.md5.i, ebx, ecx, edx, eax,  9, 21, 0xeb86d391
167
        md5._.round     md5._.i, ebx, ecx, edx, eax,  9, 21, 0xeb86d391
138
 
168
 
139
	mov	edi, [_md5]
169
        mov     edi, [_hash]
140
	add	[edi + 0x0], eax
-
 
Line 141... Line 170...
141
	add	[edi + 0x4], ebx
170
        add     [edi + 0x0], eax
142
	add	[edi + 0x8], ecx
171
        add     [edi + 0x4], ebx
Line -... Line 172...
-
 
172
        add     [edi + 0x8], ecx
-
 
173
        add     [edi + 0xc], edx
-
 
174
 
-
 
175
        ret
-
 
176
endp
-
 
177
 
-
 
178
 
-
 
179
proc md5.update _ctx, _msg, _size
-
 
180
        mov     ebx, [_ctx]
-
 
181
        mov     ecx, [_size]
-
 
182
        add     [ebx + ctx_md5.msglen_0], ecx
-
 
183
        adc     [ebx + ctx_md5.msglen_1], 0
-
 
184
 
-
 
185
  .next_block:
-
 
186
        mov     ebx, [_ctx]
-
 
187
        mov     esi, [_msg]
-
 
188
        mov     eax, [ebx + ctx_md5.index]
-
 
189
        and     eax, MD5_BLOCK_SIZE-1
-
 
190
        jnz     .copy_to_buf
-
 
191
        test    esi, MD5_ALIGN_MASK
-
 
192
        jnz     .copy_to_buf
-
 
193
  .no_copy:
-
 
194
        ; data is aligned, hash it in place without copying
-
 
195
        mov     ebx, [_ctx]
-
 
196
        cmp     [_size], MD5_BLOCK_SIZE
-
 
197
        jb      .copy_quit
-
 
198
        lea     eax, [ebx + ctx_md5.hash]
-
 
199
        stdcall md5._.block, eax
-
 
200
        sub     [_size], MD5_BLOCK_SIZE
-
 
201
        add     esi, MD5_BLOCK_SIZE
-
 
202
        jmp     .no_copy
-
 
203
 
-
 
204
  .copy_to_buf:
-
 
205
        lea     edi, [ebx + ctx_md5.block]
-
 
206
        add     edi, eax
-
 
207
        mov     ecx, MD5_BLOCK_SIZE
-
 
208
        sub     ecx, eax
-
 
209
        cmp     [_size], ecx
-
 
210
        jb      .copy_quit
-
 
211
        sub     [_size], ecx
-
 
212
        add     [_msg], ecx
-
 
213
        add     [ebx + ctx_md5.index], ecx
-
 
214
        rep     movsb
-
 
215
        lea     eax, [ebx + ctx_md5.hash]
-
 
216
        lea     esi, [ebx + ctx_md5.block]
-
 
217
        stdcall md5._.block, eax
-
 
218
        jmp     .next_block
-
 
219
 
-
 
220
  .copy_quit:
-
 
221
        mov     ebx, [_ctx]
-
 
222
        lea     edi, [ebx + ctx_md5.block]
-
 
223
        mov     eax, [ebx + ctx_md5.index]
-
 
224
        and     eax, MD5_BLOCK_SIZE-1
-
 
225
        add     edi, eax
-
 
226
        mov     ecx, [_size]
-
 
227
        add     [ebx + ctx_md5.index], ecx
-
 
228
        rep     movsb
-
 
229
  .quit:
-
 
230
 
-
 
231
        ret
-
 
232
endp
-
 
233
 
-
 
234
 
-
 
235
proc md5.final _ctx
-
 
236
        mov     ebx, [_ctx]
-
 
237
        lea     edi, [ebx + ctx_md5.block]
-
 
238
        mov     ecx, [ebx + ctx_md5.msglen_0]
-
 
239
        and     ecx, MD5_BLOCK_SIZE-1
-
 
240
        add     edi, ecx
-
 
241
        mov     byte[edi], 0x80
-
 
242
        inc     edi
-
 
243
        neg     ecx
-
 
244
        add     ecx, MD5_BLOCK_SIZE
-
 
245
        cmp     ecx, 8
-
 
246
        ja      .last
-
 
247
 
-
 
248
        dec     ecx
-
 
249
        xor     eax, eax
-
 
250
        rep     stosb
-
 
251
        lea     esi, [ebx + ctx_md5.block]
-
 
252
        lea     eax, [ebx + ctx_md5.hash]
-
 
253
        stdcall md5._.block, eax
-
 
254
        mov     ebx, [_ctx]
-
 
255
        lea     edi, [ebx + ctx_md5.block]
-
 
256
        mov     ecx, MD5_BLOCK_SIZE+1
-
 
257
  .last:
-
 
258
        dec     ecx
-
 
259
        sub     ecx, 8
-
 
260
        xor     eax, eax
-
 
261
        rep     stosb
-
 
262
        mov     eax, [ebx + ctx_md5.msglen_0]
-
 
263
        mov     edx, [ebx + ctx_md5.msglen_1]
-
 
264
        shld    edx, eax, 3
-
 
265
        shl     eax, 3
-
 
266
        mov     dword[edi], eax
-
 
267
        mov     dword[edi+4], edx
-
 
268
        lea     esi, [ebx + ctx_md5.block]
-
 
269
        lea     eax, [ebx + ctx_md5.hash]
-
 
270
        stdcall md5._.block, eax
-
 
271
 
-
 
272
        ret
-
 
273
endp