Subversion Repositories Kolibri OS

Rev

Rev 3431 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3431 dunkaist 1
;    libcrash -- cryptographic hash functions
2
;
6461 dunkaist 3
;    Copyright (C) 2012-2013,2016 Ivan Baravy (dunkaist)
3431 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
 
6461 dunkaist 18
 
19
SHA384512_BLOCK_SIZE = 128
20
SHA384512_INIT_SIZE  = 64
21
 
22
SHA384_HASH_SIZE     = 48
23
SHA512_HASH_SIZE     = 64
24
 
25
SHA384512_ALIGN      = 16
26
SHA384512_ALIGN_MASK = SHA384512_ALIGN - 1
27
 
28
struct ctx_sha384512
29
        hash            rb SHA384512_INIT_SIZE
30
        block           rb SHA384512_BLOCK_SIZE
31
        index           rd 1
32
        msglen_0        rd 1
33
        msglen_1        rd 1
34
        msglen_2        rd 1
35
        msglen_3        rd 1
36
                        rd 3    ; align
37
        ; tmp vars
38
        w               rq 80
39
        A               rq 1
40
        B               rq 1
41
        C               rq 1
42
        D               rq 1
43
        E               rq 1
44
        F               rq 1
45
        G               rq 1
46
        H               rq 1
47
        temp            rq 1
48
ends
49
 
50
 
51
macro sha384512._.chn x, y, z
3115 dunkaist 52
{
6461 dunkaist 53
        movq    mm0, [y]
54
        pxor    mm0, [z]
55
        pand    mm0, [x]
56
        pxor    mm0, [z]
3115 dunkaist 57
}
58
 
6461 dunkaist 59
macro sha384512._.maj x, y, z
3115 dunkaist 60
{
6461 dunkaist 61
        movq    mm0, [x]
62
        pxor    mm0, [y]
63
        pand    mm0, [z]
64
        movq    mm2, [x]
65
        pand    mm2, [y]
66
        pxor    mm0, mm2
3115 dunkaist 67
}
68
 
6461 dunkaist 69
macro sha384512._.Sigma0 x
3115 dunkaist 70
{
6461 dunkaist 71
        movq    mm0, x
72
        movq    mm2, mm0
73
        movq    mm7, mm2
74
        psrlq   mm2, 28
75
        psllq   mm7, 36
76
        por     mm2, mm7
77
        movq    mm7, mm0
78
        psrlq   mm0, 34
79
        psllq   mm7, 30
80
        por     mm0, mm7
81
        pxor    mm0, mm2
82
        movq    mm2, x
83
        movq    mm7, mm2
84
        psrlq   mm2, 39
85
        psllq   mm7, 25
86
        por     mm2, mm7
87
        pxor    mm0, mm2
3115 dunkaist 88
}
89
 
6461 dunkaist 90
macro sha384512._.Sigma1 x
3115 dunkaist 91
{
6461 dunkaist 92
        movq    mm0, x
93
        movq    mm2, mm0
94
        movq    mm7, mm2
95
        psrlq   mm2, 14
96
        psllq   mm7, 50
97
        por     mm2, mm7
98
        movq    mm7, mm0
99
        psrlq   mm0, 18
100
        psllq   mm7, 46
101
        por     mm0, mm7
102
        pxor    mm0, mm2
103
        movq    mm2, x
104
        movq    mm7, mm2
105
        psrlq   mm2, 41
106
        psllq   mm7, 23
107
        por     mm2, mm7
108
        pxor    mm0, mm2
3115 dunkaist 109
}
110
 
6461 dunkaist 111
macro sha384512._.sigma0 x
3115 dunkaist 112
{
6461 dunkaist 113
        movq    mm0, x
114
        movq    mm2, mm0
115
        movq    mm7, mm2
116
        psrlq   mm2, 1
117
        psllq   mm7, 63
118
        por     mm2, mm7
119
        movq    mm7, mm0
120
        psrlq   mm0, 8
121
        psllq   mm7, 56
122
        por     mm0, mm7
123
        pxor    mm0, mm2
124
        movq    mm2, x
125
        psrlq   mm2, 7
126
        pxor    mm0, mm2
3115 dunkaist 127
}
128
 
6461 dunkaist 129
macro sha384512._.sigma1 x
3115 dunkaist 130
{
6461 dunkaist 131
        movq    mm0, x
132
        movq    mm2, mm0
133
        movq    mm7, mm2
134
        psrlq   mm2, 19
135
        psllq   mm7, 45
136
        por     mm2, mm7
137
        movq    mm7, mm0
138
        psrlq   mm0, 61
139
        psllq   mm7, 3
140
        por     mm0, mm7
141
        pxor    mm0, mm2
142
        movq    mm2, x
143
        psrlq   mm2, 6
144
        pxor    mm0, mm2
3115 dunkaist 145
}
146
 
6461 dunkaist 147
macro sha384512._.recalculate_w n
3115 dunkaist 148
{
6461 dunkaist 149
        movq    mm3, [w + ((n-2) and 15)*8]
150
        sha384512._.sigma1  mm3
151
        paddq   mm0, [w + ((n-7) and 15)*8]
152
        movq    mm6, mm0
153
        movq    mm3, [w + ((n-15) and 15)*8]
154
        sha384512._.sigma0  mm3
155
        movq    mm2, mm6
156
        paddq   mm0, mm2
157
        movq    mm7, [w + (n)*8]
158
        paddq   mm7, mm0
159
        movq    [w + (n)*8], mm7
3115 dunkaist 160
}
161
 
6461 dunkaist 162
macro sha384512._.round a, b, c, d, e, f, g, h, k
3115 dunkaist 163
{
6461 dunkaist 164
        movq    mm1, [h]
165
        movq    mm3, [e]
166
        sha384512._.Sigma1  mm3
167
        paddq   mm1, mm0
168
        sha384512._.chn     e, f, g
169
        paddq   mm1, mm0
170
        paddq   mm1, [k]
171
        paddq   mm1, mm5
172
        movq    mm7, [d]
173
        paddq   mm7, mm1
174
        movq    [d], mm7
175
        movq    mm3, [a]
176
        sha384512._.Sigma0  mm3
177
        paddq   mm1, mm0
178
        sha384512._.maj     a, b, c
179
        paddq   mm0, mm1
180
        movq    [h], mm0
3115 dunkaist 181
}
182
 
183
 
6461 dunkaist 184
macro sha384512._.round_1_16 a, b, c, d, e, f, g, h, n
3115 dunkaist 185
{
186
 
6461 dunkaist 187
        movq    mm0, [esi + (n)*8]
188
        movq    [temp], mm0
189
        mov     eax, dword[temp]
190
        bswap   eax
191
        push    eax
192
        mov     eax, dword[temp + 4]
193
        bswap   eax
194
        mov     dword[temp], eax
195
        pop     eax
196
        mov     dword[temp + 4], eax
197
        movq    mm0, [temp]
198
        movq    [w + (n)*8], mm0
199
        movq    mm5, mm0
200
        sha384512._.round a, b, c, d, e, f, g, h, (sha384512._.table + (n)*8)
3115 dunkaist 201
}
202
 
6461 dunkaist 203
macro sha384512._.round_17_64 a, b, c, d, e, f, g, h, n, rep_num
3115 dunkaist 204
{
6461 dunkaist 205
        sha384512._.recalculate_w n
206
        movq    mm5, [w + (n)*8]
207
        sha384512._.round a, b, c, d, e, f, g, h, (sha384512._.table + (n+16*rep_num)*8)
3115 dunkaist 208
}
209
 
210
 
6461 dunkaist 211
proc sha384.init _ctx
212
        mov     ebx, [_ctx]
213
        lea     edi, [ebx + ctx_sha384512.hash]
214
        mov     esi, sha384._.hash_init
215
        mov     ecx, SHA384512_INIT_SIZE/4
216
        rep     movsd
217
        xor     eax, eax
218
        mov     [ebx + ctx_sha384512.index], eax
219
        mov     [ebx + ctx_sha384512.msglen_0], eax
220
        mov     [ebx + ctx_sha384512.msglen_1], eax
221
        mov     [ebx + ctx_sha384512.msglen_2], eax
222
        mov     [ebx + ctx_sha384512.msglen_3], eax
223
        ret
224
endp
3115 dunkaist 225
 
226
 
6461 dunkaist 227
proc sha512.init _ctx
228
        mov     ebx, [_ctx]
229
        lea     edi, [ebx + ctx_sha384512.hash]
230
        mov     esi, sha512._.hash_init
231
        mov     ecx, SHA384512_INIT_SIZE/4
232
        rep     movsd
233
        xor     eax, eax
234
        mov     [ebx + ctx_sha384512.index], eax
235
        mov     [ebx + ctx_sha384512.msglen_0], eax
236
        mov     [ebx + ctx_sha384512.msglen_1], eax
237
        mov     [ebx + ctx_sha384512.msglen_2], eax
238
        mov     [ebx + ctx_sha384512.msglen_3], eax
239
        ret
240
endp
3115 dunkaist 241
 
6461 dunkaist 242
 
243
proc sha384512._.block _hash
244
;locals
245
;        w       rq 80
246
;        A       rq 1
247
;        B       rq 1
248
;        C       rq 1
249
;        D       rq 1
250
;        E       rq 1
251
;        F       rq 1
252
;        G       rq 1
253
;        H       rq 1
254
;        temp    rq 1
255
;endl
256
w equ ebx + ctx_sha384512.w
257
A equ ebx + ctx_sha384512.A
258
B equ ebx + ctx_sha384512.B
259
C equ ebx + ctx_sha384512.C
260
D equ ebx + ctx_sha384512.D
261
E equ ebx + ctx_sha384512.E
262
F equ ebx + ctx_sha384512.F
263
G equ ebx + ctx_sha384512.G
264
H equ ebx + ctx_sha384512.H
265
temp equ ebx + ctx_sha384512.temp
266
 
267
        mov     edi, [_hash]
268
        movq    mm0, [edi + 0x00]
269
        movq    [A], mm0
270
        movq    mm0, [edi + 0x08]
271
        movq    [B], mm0
272
        movq    mm0, [edi + 0x10]
273
        movq    [C], mm0
274
        movq    mm0, [edi + 0x18]
275
        movq    [D], mm0
276
        movq    mm0, [edi + 0x20]
277
        movq    [E], mm0
278
        movq    mm0, [edi + 0x28]
279
        movq    [F], mm0
280
        movq    mm0, [edi + 0x30]
281
        movq    [G], mm0
282
        movq    mm0, [edi + 0x38]
283
        movq    [H], mm0
284
 
285
 
286
        sha384512._.round_1_16  A, B, C, D, E, F, G, H,  0
287
        sha384512._.round_1_16  H, A, B, C, D, E, F, G,  1
288
        sha384512._.round_1_16  G, H, A, B, C, D, E, F,  2
289
        sha384512._.round_1_16  F, G, H, A, B, C, D, E,  3
290
        sha384512._.round_1_16  E, F, G, H, A, B, C, D,  4
291
        sha384512._.round_1_16  D, E, F, G, H, A, B, C,  5
292
        sha384512._.round_1_16  C, D, E, F, G, H, A, B,  6
293
        sha384512._.round_1_16  B, C, D, E, F, G, H, A,  7
294
        sha384512._.round_1_16  A, B, C, D, E, F, G, H,  8
295
        sha384512._.round_1_16  H, A, B, C, D, E, F, G,  9
296
        sha384512._.round_1_16  G, H, A, B, C, D, E, F, 10
297
        sha384512._.round_1_16  F, G, H, A, B, C, D, E, 11
298
        sha384512._.round_1_16  E, F, G, H, A, B, C, D, 12
299
        sha384512._.round_1_16  D, E, F, G, H, A, B, C, 13
300
        sha384512._.round_1_16  C, D, E, F, G, H, A, B, 14
301
        sha384512._.round_1_16  B, C, D, E, F, G, H, A, 15
302
 
3115 dunkaist 303
repeat 4
6461 dunkaist 304
        sha384512._.round_17_64 A, B, C, D, E, F, G, H,  0, %
305
        sha384512._.round_17_64 H, A, B, C, D, E, F, G,  1, %
306
        sha384512._.round_17_64 G, H, A, B, C, D, E, F,  2, %
307
        sha384512._.round_17_64 F, G, H, A, B, C, D, E,  3, %
308
        sha384512._.round_17_64 E, F, G, H, A, B, C, D,  4, %
309
        sha384512._.round_17_64 D, E, F, G, H, A, B, C,  5, %
310
        sha384512._.round_17_64 C, D, E, F, G, H, A, B,  6, %
311
        sha384512._.round_17_64 B, C, D, E, F, G, H, A,  7, %
312
        sha384512._.round_17_64 A, B, C, D, E, F, G, H,  8, %
313
        sha384512._.round_17_64 H, A, B, C, D, E, F, G,  9, %
314
        sha384512._.round_17_64 G, H, A, B, C, D, E, F, 10, %
315
        sha384512._.round_17_64 F, G, H, A, B, C, D, E, 11, %
316
        sha384512._.round_17_64 E, F, G, H, A, B, C, D, 12, %
317
        sha384512._.round_17_64 D, E, F, G, H, A, B, C, 13, %
318
        sha384512._.round_17_64 C, D, E, F, G, H, A, B, 14, %
319
        sha384512._.round_17_64 B, C, D, E, F, G, H, A, 15, %
3115 dunkaist 320
end repeat
321
 
322
 
6461 dunkaist 323
        mov     edi, [_hash]
324
        movq    mm0, [A]
325
        paddq   mm0, [edi + 0x00]
326
        movq    [edi + 0x00], mm0
327
        movq    mm0, [B]
328
        paddq   mm0, [edi + 0x08]
329
        movq    [edi + 0x08], mm0
330
        movq    mm0, [C]
331
        paddq   mm0, [edi + 0x10]
332
        movq    [edi + 0x10], mm0
333
        movq    mm0, [D]
334
        paddq   mm0, [edi + 0x18]
335
        movq    [edi + 0x18], mm0
336
        movq    mm0, [E]
337
        paddq   mm0, [edi + 0x20]
338
        movq    [edi + 0x20], mm0
339
        movq    mm0, [F]
340
        paddq   mm0, [edi + 0x28]
341
        movq    [edi + 0x28], mm0
342
        movq    mm0, [G]
343
        paddq   mm0, [edi + 0x30]
344
        movq    [edi + 0x30], mm0
345
        movq    mm0, [H]
346
        paddq   mm0, [edi + 0x38]
347
        movq    [edi + 0x38], mm0
3115 dunkaist 348
 
6461 dunkaist 349
        ret
350
restore w,A,B,C,D,E,F,G,H,temp
3115 dunkaist 351
endp
352
 
6461 dunkaist 353
 
354
proc sha384512.update _ctx, _msg, _size
355
        mov     ebx, [_ctx]
356
        mov     ecx, [_size]
357
        add     [ebx + ctx_sha384512.msglen_0], ecx
358
        adc     [ebx + ctx_sha384512.msglen_1], 0
359
        adc     [ebx + ctx_sha384512.msglen_2], 0
360
        adc     [ebx + ctx_sha384512.msglen_3], 0
361
 
362
  .next_block:
363
        mov     ebx, [_ctx]
364
        mov     esi, [_msg]
365
        mov     eax, [ebx + ctx_sha384512.index]
366
        and     eax, SHA384512_BLOCK_SIZE-1
367
        jnz     .copy_to_buf
368
        test    esi, SHA384512_ALIGN_MASK
369
        jnz     .copy_to_buf
370
  .no_copy:
371
        ; data is aligned, hash it in place without copying
372
        mov     ebx, [_ctx]
373
        cmp     [_size], SHA384512_BLOCK_SIZE
374
        jb      .copy_quit
375
        lea     eax, [ebx + ctx_sha384512.hash]
376
        stdcall sha384512._.block, eax
377
        sub     [_size], SHA384512_BLOCK_SIZE
378
        add     esi, SHA384512_BLOCK_SIZE           ; FIXME
379
        jmp     .no_copy
380
 
381
  .copy_to_buf:
382
        lea     edi, [ebx + ctx_sha384512.block]
383
        add     edi, eax
384
        mov     ecx, SHA384512_BLOCK_SIZE
385
        sub     ecx, eax
386
        cmp     [_size], ecx
387
        jb      .copy_quit
388
        sub     [_size], ecx
389
        add     [_msg], ecx
390
        add     [ebx + ctx_sha384512.index], ecx
391
        rep     movsb
392
        lea     eax, [ebx + ctx_sha384512.hash]
393
        lea     esi, [ebx + ctx_sha384512.block]
394
        stdcall sha384512._.block, eax
395
        jmp     .next_block
396
 
397
  .copy_quit:
398
        mov     ebx, [_ctx]
399
        lea     edi, [ebx + ctx_sha384512.block]
400
        mov     eax, [ebx + ctx_sha384512.index]
401
        and     eax, SHA384512_BLOCK_SIZE-1
402
        add     edi, eax
403
        mov     ecx, [_size]
404
        add     [ebx + ctx_sha384512.index], ecx
405
        rep     movsb
406
  .quit:
407
 
408
        ret
409
endp
410
 
411
 
412
proc sha384512.final _ctx
413
        mov     ebx, [_ctx]
414
        lea     edi, [ebx + ctx_sha384512.block]
415
        mov     ecx, [ebx + ctx_sha384512.msglen_0]
416
        and     ecx, SHA384512_BLOCK_SIZE-1
417
        add     edi, ecx
418
        mov     byte[edi], 0x80
419
        inc     edi
420
        neg     ecx
421
        add     ecx, SHA384512_BLOCK_SIZE
422
        cmp     ecx, 16
423
        ja      .last
424
 
425
        dec     ecx
426
        xor     eax, eax
427
        rep     stosb
428
        lea     esi, [ebx + ctx_sha384512.block]
429
        lea     eax, [ebx + ctx_sha384512.hash]
430
        stdcall sha384512._.block, eax
431
        mov     ebx, [_ctx]
432
        lea     edi, [ebx + ctx_sha384512.block]
433
        mov     ecx, SHA384512_BLOCK_SIZE+1
434
  .last:
435
        dec     ecx
436
        sub     ecx, 16
437
        xor     eax, eax
438
        rep     stosb
439
        mov     eax, [ebx + ctx_sha384512.msglen_1]
440
        shld    [ebx + ctx_sha384512.msglen_0], eax, 3
441
        mov     eax, [ebx + ctx_sha384512.msglen_2]
442
        shld    [ebx + ctx_sha384512.msglen_1], eax, 3
443
        mov     eax, [ebx + ctx_sha384512.msglen_3]
444
        shld    [ebx + ctx_sha384512.msglen_2], eax, 3
445
        shl     eax, 3
446
        bswap   eax
447
        mov     dword[edi + 0], eax
448
        mov     eax, [ebx + ctx_sha384512.msglen_2]
449
        bswap   eax
450
        mov     dword[edi + 4], eax
451
        mov     eax, [ebx + ctx_sha384512.msglen_1]
452
        bswap   eax
453
        mov     dword[edi + 8], eax
454
        mov     eax, [ebx + ctx_sha384512.msglen_0]
455
        bswap   eax
456
        mov     dword[edi + 12], eax
457
        mov     ebx, [_ctx]
458
        lea     esi, [ebx + ctx_sha384512.block]
459
        lea     eax, [ebx + ctx_sha384512.hash]
460
        stdcall sha384512._.block, eax
461
 
462
        mov     ebx, [_ctx]
463
        lea     eax, [ebx + ctx_sha384512.hash]
464
        stdcall sha384512._.postprocess, ebx, eax
465
 
466
        ret
467
endp
468
 
469
 
470
proc sha384512._.postprocess _ctx, _hash
471
        mov     ecx, 8
472
        mov     esi, [_hash]
473
        mov     edi, esi
474
    @@:
475
        lodsd
476
        mov     ebx, eax
477
        lodsd
478
        bswap   eax
479
        bswap   ebx
480
        stosd
481
        mov     eax, ebx
482
        stosd
483
        dec     ecx     ; FIXME: what should I fix here?
484
        jnz     @b
485
        emms
486
        ret
487
endp
488
 
489
 
490
align SHA384512_ALIGN
491
 
492
sha384._.hash_init      dq 0xcbbb9d5dc1059ed8, 0x629a292a367cd507,\
493
                           0x9159015a3070dd17, 0x152fecd8f70e5939,\
494
                           0x67332667ffc00b31, 0x8eb44a8768581511,\
495
                           0xdb0c2e0d64f98fa7, 0x47b5481dbefa4fa4
496
 
497
sha512._.hash_init      dq 0x6a09e667f3bcc908, 0xbb67ae8584caa73b,\
498
                           0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1,\
499
                           0x510e527fade682d1, 0x9b05688c2b3e6c1f,\
500
                           0x1f83d9abfb41bd6b, 0x5be0cd19137e2179
501
 
502
sha384512._.table       dq 0x428a2f98d728ae22, 0x7137449123ef65cd,\
503
                           0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc,\
504
                           0x3956c25bf348b538, 0x59f111f1b605d019,\
505
                           0x923f82a4af194f9b, 0xab1c5ed5da6d8118,\
506
                           0xd807aa98a3030242, 0x12835b0145706fbe,\
507
                           0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2,\
508
                           0x72be5d74f27b896f, 0x80deb1fe3b1696b1,\
509
                           0x9bdc06a725c71235, 0xc19bf174cf692694,\
510
                           0xe49b69c19ef14ad2, 0xefbe4786384f25e3,\
511
                           0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65,\
512
                           0x2de92c6f592b0275, 0x4a7484aa6ea6e483,\
513
                           0x5cb0a9dcbd41fbd4, 0x76f988da831153b5,\
514
                           0x983e5152ee66dfab, 0xa831c66d2db43210,\
515
                           0xb00327c898fb213f, 0xbf597fc7beef0ee4,\
516
                           0xc6e00bf33da88fc2, 0xd5a79147930aa725,\
517
                           0x06ca6351e003826f, 0x142929670a0e6e70,\
518
                           0x27b70a8546d22ffc, 0x2e1b21385c26c926,\
519
                           0x4d2c6dfc5ac42aed, 0x53380d139d95b3df,\
520
                           0x650a73548baf63de, 0x766a0abb3c77b2a8,\
521
                           0x81c2c92e47edaee6, 0x92722c851482353b,\
522
                           0xa2bfe8a14cf10364, 0xa81a664bbc423001,\
523
                           0xc24b8b70d0f89791, 0xc76c51a30654be30,\
524
                           0xd192e819d6ef5218, 0xd69906245565a910,\
525
                           0xf40e35855771202a, 0x106aa07032bbd1b8,\
526
                           0x19a4c116b8d2d0c8, 0x1e376c085141ab53,\
527
                           0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8,\
528
                           0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb,\
529
                           0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3,\
530
                           0x748f82ee5defb2fc, 0x78a5636f43172f60,\
531
                           0x84c87814a1f0ab72, 0x8cc702081a6439ec,\
532
                           0x90befffa23631e28, 0xa4506cebde82bde9,\
533
                           0xbef9a3f7b2c67915, 0xc67178f2e372532b,\
534
                           0xca273eceea26619c, 0xd186b8c721c0c207,\
535
                           0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178,\
536
                           0x06f067aa72176fba, 0x0a637dc5a2c898a6,\
537
                           0x113f9804bef90dae, 0x1b710b35131c471b,\
538
                           0x28db77f523047d84, 0x32caab7b40c72493,\
539
                           0x3c9ebe0a15c9bebc, 0x431d67c49c100d4c,\
540
                           0x4cc5d4becb3e42b6, 0x597f299cfc657e2a,\
541
                           0x5fcb6fab3ad6faec, 0x6c44198c4a475817
542