Subversion Repositories Kolibri OS

Rev

Rev 6461 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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