Subversion Repositories Kolibri OS

Rev

Rev 7698 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 7698 Rev 9216
Line 1... Line 1...
1
;    libcrash -- cryptographic hash functions
1
; libcrash -- cryptographic hash (and other) functions
2
;
2
;
3
;    Copyright (C) 2013,2016,2019 Ivan Baravy (dunkaist)
3
; Copyright (C) <2013,2016,2019,2021> Ivan Baravy
4
;
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.
5
; SPDX-License-Identifier: GPL-2.0-or-later
9
;
6
;
10
;    This program is distributed in the hope that it will be useful,
7
; This program is free software: you can redistribute it and/or modify it under
11
;    but WITHOUT ANY WARRANTY; without even the implied warranty of
8
; the terms of the GNU General Public License as published by the Free Software
12
;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9
; Foundation, either version 2 of the License, or (at your option) any later
13
;    GNU General Public License for more details.
10
; version.
14
;
11
;
15
;    You should have received a copy of the GNU General Public License
12
; This program is distributed in the hope that it will be useful, but WITHOUT
-
 
13
; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16
;    along with this program.  If not, see .
14
; FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17
 
-
 
18
 
15
;
19
SHA3_224_HASH_SIZE       = 28
-
 
20
SHA3_256_HASH_SIZE       = 32
-
 
21
SHA3_384_HASH_SIZE       = 48
16
; You should have received a copy of the GNU General Public License along with
22
SHA3_512_HASH_SIZE       = 64
17
; this program. If not, see .
Line 23... Line 18...
23
 
18
 
24
SHA3_224_BLOCK_SIZE      = 144
19
SHA3_224_BLOCK_SIZE = 144
25
SHA3_256_BLOCK_SIZE      = 136
20
SHA3_256_BLOCK_SIZE = 136
26
SHA3_384_BLOCK_SIZE      = 104
21
SHA3_384_BLOCK_SIZE = 104
27
SHA3_512_BLOCK_SIZE      = 72
22
SHA3_512_BLOCK_SIZE = 72
Line 28... Line 23...
28
SHA3MAX_BLOCK_SIZE      = SHA3_224_BLOCK_SIZE
23
SHA3_MAX_BLOCK_SIZE = SHA3_224_BLOCK_SIZE
29
 
24
 
30
SHA3_INIT_SIZE          = 200
25
SHA3_INIT_SIZE          = 200
Line 31... Line 26...
31
SHA3_ALIGN              = 16
26
SHA3_ALIGN              = 16
32
SHA3_ALIGN_MASK         = SHA3_ALIGN-1
27
SHA3_ALIGN_MASK         = SHA3_ALIGN-1
33
 
28
 
34
struct ctx_sha3
29
struct ctx_sha3
35
        hash            rb SHA3_INIT_SIZE
30
        hash            rb SHA3_INIT_SIZE
36
                        rb SHA3_ALIGN - (SHA3_INIT_SIZE mod SHA3_ALIGN)
31
                        rb SHA3_ALIGN - (SHA3_INIT_SIZE mod SHA3_ALIGN)
37
        block           rb SHA3MAX_BLOCK_SIZE
32
        block           rb SHA3_MAX_BLOCK_SIZE
38
                        rb SHA3_ALIGN - (SHA3MAX_BLOCK_SIZE mod SHA3_ALIGN)
33
                        rb SHA3_ALIGN - (SHA3_MAX_BLOCK_SIZE mod SHA3_ALIGN)
39
        index           rd 1
34
        index           rd 1
40
        block_size      rd 1
35
        block_size      rd 1
41
        rounds_cnt      rd 1
36
        rounds_cnt      rd 1
42
                        rd 1    ; align
37
                        rd 1    ; align
43
        ; tmp vars
38
        ; tmp vars
Line 44... Line -...
44
        C               rq 5
-
 
45
        D               rq 5
39
        C               rq 5
46
ends
-
 
Line 47... Line 40...
47
 
40
        D               rq 5
48
if defined sizeof.crash_ctx
41
ends
49
  assert sizeof.crash_ctx >= sizeof.ctx_sha3
42
 
50
end if
43
assert sizeof.ctx_sha3 <= LIBCRASH_CTX_LEN
Line 261... Line 254...
261
 
254
 
262
        ret
255
        ret
Line 263... Line 256...
263
endp
256
endp
264
 
257
 
265
 
258
 
266
proc sha3._.init _ctx
259
proc sha3._.init uses edi
267
        mov     [ebx + ctx_sha3.block_size], eax
260
        mov     [ebx + ctx_sha3.block_size], eax
268
        shr     eax, 3
261
        shr     eax, 3
Line 275... Line 268...
275
        mov     [ebx + ctx_sha3.index], eax
268
        mov     [ebx + ctx_sha3.index], eax
276
        ret
269
        ret
277
endp
270
endp
Line 278... Line 271...
278
 
271
 
279
 
272
 
280
proc sha3_224.init _ctx
273
proc sha3_224.init uses ebx, _ctx
281
        mov     ebx, [_ctx]
274
        mov     ebx, [_ctx]
282
        mov     eax, SHA3_224_BLOCK_SIZE
275
        mov     eax, SHA3_224_BLOCK_SIZE
283
        stdcall sha3._.init
276
        stdcall sha3._.init
Line 284... Line 277...
284
        ret
277
        ret
285
endp
278
endp
286
 
279
 
287
 
280
 
288
proc sha3_256.init _ctx
281
proc sha3_256.init uses ebx, _ctx
289
        mov     ebx, [_ctx]
282
        mov     ebx, [_ctx]
Line 290... Line 283...
290
        mov     eax, SHA3_256_BLOCK_SIZE
283
        mov     eax, SHA3_256_BLOCK_SIZE
291
        stdcall sha3._.init
284
        stdcall sha3._.init
292
        ret
285
        ret
293
endp
286
endp
294
 
287
 
295
 
288
 
Line 296... Line 289...
296
proc sha3_384.init _ctx
289
proc sha3_384.init uses ebx, _ctx
297
        mov     ebx, [_ctx]
290
        mov     ebx, [_ctx]
298
        mov     eax, SHA3_384_BLOCK_SIZE
291
        mov     eax, SHA3_384_BLOCK_SIZE
299
        stdcall sha3._.init
292
        stdcall sha3._.init
300
        ret
293
        ret
301
endp
294
endp
Line 328... Line 321...
328
 
321
 
329
sha3_224.update = sha3.update
322
sha3_224.update = sha3.update
330
sha3_256.update = sha3.update
323
sha3_256.update = sha3.update
331
sha3_384.update = sha3.update
324
sha3_384.update = sha3.update
332
sha3_512.update = sha3.update
325
sha3_512.update = sha3.update
333
proc sha3.update _ctx, _msg, _size
326
proc sha3.update uses ebx esi edi, _ctx, _msg, _size
334
  .next_block:
327
.next_block:
335
        mov     ebx, [_ctx]
328
        mov     ebx, [_ctx]
336
        mov     esi, [_msg]
329
        mov     esi, [_msg]
337
        mov     eax, [ebx + ctx_sha3.index]
330
        mov     eax, [ebx + ctx_sha3.index]
Line 386... Line 379...
386
  .quit:
379
.quit:
387
        ret
380
        ret
388
endp
381
endp
Line 389... Line 382...
389
 
382
 
390
 
383
 
391
sha3_224.final = sha3.final
384
sha3_224.finish = sha3.finish
392
sha3_256.final = sha3.final
385
sha3_256.finish = sha3.finish
393
sha3_384.final = sha3.final
386
sha3_384.finish = sha3.finish
394
sha3_512.final = sha3.final
-
 
395
proc sha3.final _ctx
387
sha3_512.finish = sha3.finish
396
        pushad
388
proc sha3.finish uses ebx esi edi, _ctx
397
        mov     ebx, [_ctx]
-
 
398
        mov     eax, [ebx + ctx_sha3.index]
389
        mov     ebx, [_ctx]
399
        xor     edx, edx
-
 
400
        mov     ecx, [ebx + ctx_sha3.block_size]
390
        mov     eax, [ebx + ctx_sha3.index]
401
        div     ecx
-
 
402
        sub     ecx, edx
-
 
403
        ja      @f
-
 
404
        add     ecx, [ebx + ctx_sha3.block_size]
-
 
405
    @@:
391
        mov     ecx, [ebx + ctx_sha3.block_size]
406
        add     [ebx + ctx_sha3.index], ecx
-
 
407
        mov     eax, [ebx + ctx_sha3.block_size]
392
        sub     ecx, eax
408
        cmp     [ebx + ctx_sha3.index], eax
-
 
409
        jb      @f
-
 
410
        sub     [ebx + ctx_sha3.index], eax
-
 
411
    @@:
393
        lea     edi, [ebx+ctx_sha3.block]
412
 
394
        add     edi, eax
413
        mov     byte[edi], 0x06
395
        mov     byte[edi], 0x06
414
        inc     edi
396
        inc     edi
415
        dec     ecx
397
        dec     ecx
Line 423... Line 405...
423
        stdcall sha3._.block, eax
405
        stdcall sha3._.block, eax
Line 424... Line 406...
424
 
406
 
425
        mov     ebx, [_ctx]
407
        mov     ebx, [_ctx]
426
        lea     eax, [ebx + ctx_sha3.hash]
408
        lea     eax, [ebx + ctx_sha3.hash]
427
        stdcall sha3._.postprocess, ebx, eax
-
 
428
 
-
 
429
        popad
409
        stdcall sha3._.postprocess, ebx, eax
430
        ret
410
        ret
Line 431... Line 411...
431
endp
411
endp
Line 438... Line 418...
438
 
418
 
439
 
419
 
440
proc sha3_224.oneshot _ctx, _data, _len
420
proc sha3_224.oneshot _ctx, _data, _len
441
	stdcall	sha3_224.init, [_ctx]
421
        stdcall sha3_224.init, [_ctx]
442
	stdcall	sha3.update, [_ctx], [_data], [_len]
422
        stdcall sha3.update, [_ctx], [_data], [_len]
443
	stdcall	sha3.final, [_ctx]
423
        stdcall sha3.finish, [_ctx]
Line 444... Line 424...
444
	ret
424
        ret
445
endp
425
endp
446
 
426
 
447
 
427
 
448
proc sha3_256.oneshot _ctx, _data, _len
428
proc sha3_256.oneshot _ctx, _data, _len
449
	stdcall	sha3_256.init, [_ctx]
429
        stdcall sha3_256.init, [_ctx]
Line 450... Line 430...
450
	stdcall	sha3.update, [_ctx], [_data], [_len]
430
        stdcall sha3.update, [_ctx], [_data], [_len]
451
	stdcall	sha3.final, [_ctx]
431
        stdcall sha3.finish, [_ctx]
452
	ret
432
        ret
453
endp
433
endp
454
 
434
 
455
 
435
 
Line 456... Line 436...
456
proc sha3_384.oneshot _ctx, _data, _len
436
proc sha3_384.oneshot _ctx, _data, _len
457
	stdcall	sha3_384.init, [_ctx]
437
        stdcall sha3_384.init, [_ctx]
458
	stdcall	sha3.update, [_ctx], [_data], [_len]
438
        stdcall sha3.update, [_ctx], [_data], [_len]
459
	stdcall	sha3.final, [_ctx]
439
        stdcall sha3.finish, [_ctx]
460
	ret
440
        ret
461
endp
441
endp
Line 462... Line 442...
462
 
442