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
macro sha224256._.chn x, y, z
20
{
21
        mov     eax, [y]
22
        xor     eax, [z]
23
        and     eax, [x]
24
        xor     eax, [z]
25
}
26
 
27
macro sha224256._.maj x, y, z
28
{
29
        mov     eax, [x]
30
        xor     eax, [y]
31
        and     eax, [z]
32
        mov     ecx, [x]
33
        and     ecx, [y]
34
        xor     eax, ecx
35
}
36
 
37
macro sha224256._.Sigma0 x
38
{
39
        mov     eax, x
40
        mov     ecx, eax
41
        ror     ecx, 2
42
        ror     eax, 13
43
        xor     eax, ecx
44
        mov     ecx, x
45
        ror     ecx, 22
46
        xor     eax, ecx
47
}
48
 
49
macro sha224256._.Sigma1 x
50
{
51
        mov     eax, x
52
        mov     ecx, eax
53
        ror     ecx, 6
54
        ror     eax, 11
55
        xor     eax, ecx
56
        mov     ecx, x
57
        ror     ecx, 25
58
        xor     eax, ecx
59
}
60
 
61
macro sha224256._.sigma0 x
62
{
63
        mov     eax, x
64
        mov     ecx, eax
65
        ror     ecx, 7
66
        ror     eax, 18
67
        xor     eax, ecx
68
        mov     ecx, x
69
        shr     ecx, 3
70
        xor     eax, ecx
71
}
72
 
73
macro sha224256._.sigma1 x
74
{
75
        mov     eax, x
76
        mov     ecx, eax
77
        ror     ecx, 17
78
        ror     eax, 19
79
        xor     eax, ecx
80
        mov     ecx, x
81
        shr     ecx, 10
82
        xor     eax, ecx
83
}
84
 
85
macro sha224256._.recalculate_w n
86
{
87
        mov     edx, [w + ((n-2) and 15)*4]
88
        sha224256._.sigma1  edx
89
        add     eax, [w + ((n-7) and 15)*4]
90
        push    eax
91
        mov     edx, [w + ((n-15) and 15)*4]
92
        sha224256._.sigma0  edx
93
        pop     ecx
94
        add     eax, ecx
95
        add     [w + (n)*4], eax
96
}
97
 
98
macro sha224256._.round a, b, c, d, e, f, g, h, k
99
{
100
        mov     ebx, [h]
101
        mov     edx, [e]
102
        sha224256._.Sigma1  edx
103
 
104
        add     ebx, eax
105
        sha224256._.chn     e, f, g
106
 
107
        add     ebx, eax
108
        add     ebx, [k]
109
        add     ebx, edi
110
 
111
        add     [d], ebx
112
 
113
        mov     edx, [a]
114
        sha224256._.Sigma0  edx
115
        add     ebx, eax
116
        sha224256._.maj     a, b, c
117
        add     eax, ebx
118
        mov     [h], eax
119
}
120
 
121
 
122
macro sha224256._.round_1_16 a, b, c, d, e, f, g, h, n
123
{
124
 
125
        mov     eax, [esi + (n)*4]
126
        bswap   eax
127
 
128
        mov     dword[w + (n)*4], eax
129
        mov     edi, eax
130
        sha224256._.round a, b, c, d, e, f, g, h, (sha256_table + (n)*4)
131
}
132
 
133
macro sha224256._.round_17_64 a, b, c, d, e, f, g, h, n, rep_num
134
{
135
        sha224256._.recalculate_w n
136
        mov     edi, [w + (n)*4]
137
        sha224256._.round a, b, c, d, e, f, g, h, (sha256_table + (n+16*rep_num)*4)
138
}
139
 
140
 
141
proc sha224.init _ctx
142
        mov     ebx, [_ctx]
143
        lea     edi, [ebx + ctx_sha224256.hash]
144
        mov     esi, sha224._.hash_init
145
        mov     ecx, SHA224256_INIT_SIZE/4
146
        rep     movsd
147
        xor     eax, eax
148
        mov     [ebx + ctx_sha224256.index], eax
149
        mov     [ebx + ctx_sha224256.msglen_0], eax
150
        mov     [ebx + ctx_sha224256.msglen_1], eax
151
        ret
152
endp
153
 
154
 
155
proc sha256.init _ctx
156
        mov     ebx, [_ctx]
157
        lea     edi, [ebx + ctx_sha224256.hash]
158
        mov     esi, sha256._.hash_init
159
        mov     ecx, SHA224256_INIT_SIZE/4
160
        rep     movsd
161
        xor     eax, eax
162
        mov     [ebx + ctx_sha224256.index], eax
163
        mov     [ebx + ctx_sha224256.msglen_0], eax
164
        mov     [ebx + ctx_sha224256.msglen_1], eax
165
        ret
166
endp
167
 
168
 
169
proc sha224256._.block _hash
170
locals
171
        w       rd 64
172
        A       rd 1
173
        B       rd 1
174
        C       rd 1
175
        D       rd 1
176
        E       rd 1
177
        F       rd 1
178
        G       rd 1
179
        H       rd 1
180
endl
181
        mov     edi, [_hash]
182
        mov     eax, [edi + 0x00]
183
        mov     [A], eax
184
        mov     eax, [edi + 0x04]
185
        mov     [B], eax
186
        mov     eax, [edi + 0x08]
187
        mov     [C], eax
188
        mov     eax, [edi + 0x0c]
189
        mov     [D], eax
190
        mov     eax, [edi + 0x10]
191
        mov     [E], eax
192
        mov     eax, [edi + 0x14]
193
        mov     [F], eax
194
        mov     eax, [edi + 0x18]
195
        mov     [G], eax
196
        mov     eax, [edi + 0x1c]
197
        mov     [H], eax
198
 
199
        sha224256._.round_1_16  A, B, C, D, E, F, G, H,  0
200
        sha224256._.round_1_16  H, A, B, C, D, E, F, G,  1
201
        sha224256._.round_1_16  G, H, A, B, C, D, E, F,  2
202
        sha224256._.round_1_16  F, G, H, A, B, C, D, E,  3
203
        sha224256._.round_1_16  E, F, G, H, A, B, C, D,  4
204
        sha224256._.round_1_16  D, E, F, G, H, A, B, C,  5
205
        sha224256._.round_1_16  C, D, E, F, G, H, A, B,  6
206
        sha224256._.round_1_16  B, C, D, E, F, G, H, A,  7
207
        sha224256._.round_1_16  A, B, C, D, E, F, G, H,  8
208
        sha224256._.round_1_16  H, A, B, C, D, E, F, G,  9
209
        sha224256._.round_1_16  G, H, A, B, C, D, E, F, 10
210
        sha224256._.round_1_16  F, G, H, A, B, C, D, E, 11
211
        sha224256._.round_1_16  E, F, G, H, A, B, C, D, 12
212
        sha224256._.round_1_16  D, E, F, G, H, A, B, C, 13
213
        sha224256._.round_1_16  C, D, E, F, G, H, A, B, 14
214
        sha224256._.round_1_16  B, C, D, E, F, G, H, A, 15
215
 
216
repeat 3
217
        sha224256._.round_17_64 A, B, C, D, E, F, G, H,  0, %
218
        sha224256._.round_17_64 H, A, B, C, D, E, F, G,  1, %
219
        sha224256._.round_17_64 G, H, A, B, C, D, E, F,  2, %
220
        sha224256._.round_17_64 F, G, H, A, B, C, D, E,  3, %
221
        sha224256._.round_17_64 E, F, G, H, A, B, C, D,  4, %
222
        sha224256._.round_17_64 D, E, F, G, H, A, B, C,  5, %
223
        sha224256._.round_17_64 C, D, E, F, G, H, A, B,  6, %
224
        sha224256._.round_17_64 B, C, D, E, F, G, H, A,  7, %
225
        sha224256._.round_17_64 A, B, C, D, E, F, G, H,  8, %
226
        sha224256._.round_17_64 H, A, B, C, D, E, F, G,  9, %
227
        sha224256._.round_17_64 G, H, A, B, C, D, E, F, 10, %
228
        sha224256._.round_17_64 F, G, H, A, B, C, D, E, 11, %
229
        sha224256._.round_17_64 E, F, G, H, A, B, C, D, 12, %
230
        sha224256._.round_17_64 D, E, F, G, H, A, B, C, 13, %
231
        sha224256._.round_17_64 C, D, E, F, G, H, A, B, 14, %
232
        sha224256._.round_17_64 B, C, D, E, F, G, H, A, 15, %
233
end repeat
234
 
235
        mov     edi, [_hash]
236
        mov     eax, [A]
237
        add     [edi + 0x00], eax
238
        mov     eax, [B]
239
        add     [edi + 0x04], eax
240
        mov     eax, [C]
241
        add     [edi + 0x08], eax
242
        mov     eax, [D]
243
        add     [edi + 0x0c], eax
244
        mov     eax, [E]
245
        add     [edi + 0x10], eax
246
        mov     eax, [F]
247
        add     [edi + 0x14], eax
248
        mov     eax, [G]
249
        add     [edi + 0x18], eax
250
        mov     eax, [H]
251
        add     [edi + 0x1c], eax
252
 
253
        ret
254
endp
255
 
256
sha256.update = sha224.update
257
proc sha224.update _ctx, _msg, _size
258
        mov     ebx, [_ctx]
259
        mov     ecx, [_size]
260
        add     [ebx + ctx_sha224256.msglen_0], ecx
261
        adc     [ebx + ctx_sha224256.msglen_1], 0
262
 
263
  .next_block:
264
        mov     ebx, [_ctx]
265
        mov     esi, [_msg]
266
        mov     eax, [ebx + ctx_sha224256.index]
267
        and     eax, SHA224256_BLOCK_SIZE-1
268
        jnz     .copy_to_buf
269
        test    esi, SHA224256_ALIGN_MASK
270
        jnz     .copy_to_buf
271
  .no_copy:
272
        ; data is aligned, hash it in place without copying
273
        mov     ebx, [_ctx]
274
        cmp     [_size], SHA224256_BLOCK_SIZE
275
        jb      .copy_quit
276
        lea     eax, [ebx + ctx_sha224256.hash]
277
        stdcall sha224256._.block, eax
278
        sub     [_size], SHA224256_BLOCK_SIZE
279
        add     esi, SHA224256_BLOCK_SIZE           ; FIXME
280
        jmp     .no_copy
281
 
282
  .copy_to_buf:
283
        lea     edi, [ebx + ctx_sha224256.block]
284
        add     edi, eax
285
        mov     ecx, SHA224256_BLOCK_SIZE
286
        sub     ecx, eax
287
        cmp     [_size], ecx
288
        jb      .copy_quit
289
        sub     [_size], ecx
290
        add     [_msg], ecx
291
        add     [ebx + ctx_sha224256.index], ecx
292
        rep     movsb
293
        lea     eax, [ebx + ctx_sha224256.hash]
294
        lea     esi, [ebx + ctx_sha224256.block]
295
        stdcall sha224256._.block, eax
296
        jmp     .next_block
297
 
298
  .copy_quit:
299
        mov     ebx, [_ctx]
300
        lea     edi, [ebx + ctx_sha224256.block]
301
        mov     eax, [ebx + ctx_sha224256.index]
302
        and     eax, SHA224256_BLOCK_SIZE-1
303
        add     edi, eax
304
        mov     ecx, [_size]
305
        add     [ebx + ctx_sha224256.index], ecx
306
        rep     movsb
307
  .quit:
308
 
309
        ret
310
endp
311
 
312
 
313
sha256.final = sha224.final
314
proc sha224.final _ctx
315
        mov     ebx, [_ctx]
316
        lea     edi, [ebx + ctx_sha224256.block]
317
        mov     ecx, [ebx + ctx_sha224256.msglen_0]
318
        and     ecx, SHA224256_BLOCK_SIZE-1
319
        add     edi, ecx
320
        mov     byte[edi], 0x80
321
        inc     edi
322
        neg     ecx
323
        add     ecx, SHA224256_BLOCK_SIZE
324
        cmp     ecx, 8
325
        ja      .last
326
 
327
        dec     ecx
328
        xor     eax, eax
329
        rep     stosb
330
        lea     esi, [ebx + ctx_sha224256.block]
331
        lea     eax, [ebx + ctx_sha224256.hash]
332
        stdcall sha224256._.block, eax
333
        mov     ebx, [_ctx]
334
        lea     edi, [ebx + ctx_sha224256.block]
335
        mov     ecx, SHA224256_BLOCK_SIZE+1
336
  .last:
337
        dec     ecx
338
        sub     ecx, 8
339
        xor     eax, eax
340
        rep     stosb
341
        mov     eax, [ebx + ctx_sha224256.msglen_0]
342
        mov     edx, [ebx + ctx_sha224256.msglen_1]
343
        shld    edx, eax, 3
344
        shl     eax, 3
345
        bswap   eax
346
        bswap   edx
347
        mov     dword[edi], edx
348
        mov     dword[edi+4], eax
349
        lea     esi, [ebx + ctx_sha224256.block]
350
        lea     eax, [ebx + ctx_sha224256.hash]
351
        stdcall sha224256._.block, eax
352
 
353
        mov     ebx, [_ctx]
354
        lea     eax, [ebx + ctx_sha224256.hash]
355
        stdcall sha224256._.postprocess, ebx, eax
356
 
357
        ret
358
endp
359
 
360
 
361
proc sha224256._.postprocess _ctx, _hash
362
        mov     ecx, 8
363
        mov     esi, [_hash]
364
        mov     edi, esi
365
    @@:
366
        lodsd
367
        bswap   eax
368
        stosd
369
        dec     ecx
370
        jnz     @b
371
        ret
372
endp
373
 
374
 
375
align SHA224256_ALIGN
376
 
377
sha224._.hash_init      dd 0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939,\
378
                           0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4
379
 
380
sha256._.hash_init      dd 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,\
381
                           0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
382
 
383
sha256_table            dd 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,\
384
                           0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,\
385
                           0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,\
386
                           0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,\
387
                           0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,\
388
                           0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,\
389
                           0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,\
390
                           0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,\
391
                           0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,\
392
                           0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,\
393
                           0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,\
394
                           0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,\
395
                           0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,\
396
                           0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,\
397
                           0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,\
398
                           0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
399