Export separate init, update and finalize hash functions.

git-svn-id: svn://kolibrios.org@6465 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
hidnplayr 2016-07-31 00:16:12 +00:00
parent ee14b53b33
commit 523ccd603b
10 changed files with 2706 additions and 2657 deletions

View File

@ -1,154 +1,153 @@
use32 use32
org 0x0 org 0x0
db 'MENUET01' db 'MENUET01'
dd 0x01,start,i_end,e_end,e_end,0,this_file_name dd 0x01,start,i_end,e_end,e_end,0,this_file_name
include '../../../../proc32.inc' include '../../../../proc32.inc'
include '../../../../macros.inc' include '../../../../macros.inc'
include '../../../../dll.inc' include '../../../../dll.inc'
;include '../../../../debug.inc' ;include '../../../../debug.inc'
include 'libcrash.inc' include 'libcrash.inc'
BUFFER_SIZE = 4096 BUFFER_SIZE = 4096
start: start:
pushfd pushfd
pop eax pop eax
or eax, 1 SHL 18 ; Alignment Check flag or eax, 1 SHL 18 ; Alignment Check flag
push eax push eax
popfd popfd
mcall 68, 11 mcall 68, 11
stdcall dll.Load, @IMPORT stdcall dll.Load, @IMPORT
or eax, eax or eax, eax
jnz quit jnz quit
still: still:
mcall 10 mcall 10
dec eax dec eax
jz redraw jz redraw
dec eax dec eax
jz key jz key
button: button:
mcall 17 mcall 17
shr eax, 8 shr eax, 8
cmp eax, 1 cmp eax, 1
je quit je quit
redraw: redraw:
mcall 12, 1 mcall 12, 1
mcall 0, <0,900>, <0,160>, 0x34000000, 0x80000000, window_title mcall 0, <0,900>, <0,160>, 0x34000000, 0x80000000, window_title
mcall 4, < 0, 0>, 0xc0ffffff, message, , 0 mcall 4, < 0, 0>, 0xc0ffffff, message, , 0
mov [hid], 0 mov [hid], 0
mov [text_pos_y], 10 mov [text_pos_y], 10
mov [hash_name], hash_strings mov [hash_name], hash_strings
.next_hash: .next_hash:
mov dword[f70_buf + 4], 0 mov dword[f70_buf + 4], 0
mov dword[msglen], 0 mov dword[msglen], 0
mov edi, hex mov edi, hex
xor eax, eax xor eax, eax
mov ecx, 1024/4 mov ecx, 1024/4
rep stosd rep stosd
invoke crash.hash, [hid], data_buffer, update_data_buffer, ctx invoke crash.hash, [hid], data_buffer, update_data_buffer, ctx
invoke crash.bin2hex, ctx, hex, [hid] invoke crash.bin2hex, ctx, hex, [hid]
mov ebx, [text_pos_y] mov ebx, [text_pos_y]
mcall 4, , 0xc0ffffff, [hash_name], , 0 mcall 4, , 0xc0ffffff, [hash_name], , 0
mov ebx, [text_pos_y] mov ebx, [text_pos_y]
add ebx, 66 SHL 16 add ebx, 66 SHL 16
mcall 4, , 0x40ffffff, hex, 128, 0 mcall 4, , 0x40ffffff, hex, 128, 0
add [text_pos_y], 10 add [text_pos_y], 10
add [hash_name], 12 ; lenght of hash_strings item add [hash_name], 12 ; lenght of hash_strings item
inc [hid] inc [hid]
cmp [hid], LIBCRASH_SHA3_512 cmp [hid], LIBCRASH_SHA3_512
jng .next_hash jng .next_hash
mcall 12, 2 mcall 12, 2
jmp still jmp still
key: key:
mcall 2 mcall 2
jmp still jmp still
quit: quit:
mcall -1 mcall -1
proc update_data_buffer _left proc update_data_buffer _left
mov eax, data_buffer mov eax, data_buffer
add eax, [_left] add eax, [_left]
mov dword[f70_buf + 16], eax mov dword[f70_buf + 16], eax
mov eax, BUFFER_SIZE mov eax, BUFFER_SIZE
sub eax, [_left] sub eax, [_left]
mov dword[f70_buf + 12], eax mov dword[f70_buf + 12], eax
mcall 70, f70_buf mcall 70, f70_buf
mov eax, ebx mov eax, ebx
cmp eax, -1 cmp eax, -1
jne @f jne @f
inc eax inc eax
@@: @@:
add dword[f70_buf + 4], eax add dword[f70_buf + 4], eax
ret ret
endp endp
szZ window_title ,'libcrash example' szZ window_title , 'libcrash example'
szZ message , 'hash sums of this file'
szZ message , 'hash sums of this file'
hash_strings:
hash_strings: db 'crc32 : ',0
db 'crc32 : ',0 db 'md4 : ',0
db 'md4 : ',0 db 'md5 : ',0
db 'md5 : ',0 db 'sha1 : ',0
db 'sha1 : ',0 db 'sha224 : ',0
db 'sha224 : ',0 db 'sha256 : ',0
db 'sha256 : ',0 db 'sha384 : ',0
db 'sha384 : ',0 db 'sha512 : ',0
db 'sha512 : ',0 db 'sha3-224 : ',0
db 'sha3-224 : ',0 db 'sha3-256 : ',0
db 'sha3-256 : ',0 db 'sha3-384 : ',0
db 'sha3-384 : ',0 db 'sha3-512 : ',0
db 'sha3-512 : ',0
f70_buf:
f70_buf: funcnum dd 0
funcnum dd 0 src dd 0
src dd 0 res1 dd 0
res1 dd 0 count dd BUFFER_SIZE
count dd BUFFER_SIZE dst dd data_buffer
dst dd data_buffer res2 db 0
res2 db 0 fname dd this_file_name
fname dd this_file_name
align 4
align 4 @IMPORT:
@IMPORT:
library \
library \ libcrash, 'libcrash.obj'
libcrash, 'libcrash.obj'
import libcrash , \
import libcrash , \ libcrash.init , 'lib_init' , \
libcrash.init , 'lib_init' , \ crash.hash , 'crash_hash' , \
crash.hash , 'crash_hash' , \ crash.bin2hex , 'crash_bin2hex'
crash.bin2hex , 'crash_bin2hex'
i_end:
i_end: hash_name rd 1
hash_name rd 1 text_pos_y rd 1
text_pos_y rd 1 hash rd 50
hash rd 50 hid rd 1 ; hash id
hid rd 1 ; hash id msglen rd 1
msglen rd 1 hex rb 1024
hex rb 1024 data_buffer rb BUFFER_SIZE
data_buffer rb BUFFER_SIZE this_file_name rb 4096
this_file_name rb 4096 rb 0x800 ;stack
rb 0x800 ;stack align 16 ; should be enough
align 16 ; should be enough ctx rb 0x1000
ctx rb 0x1000 e_end:
e_end:

View File

@ -1,121 +1,112 @@
; libcrash -- cryptographic hash functions ; libcrash -- cryptographic hash functions
; ;
; Copyright (C) 2012-2013,2016 Ivan Baravy (dunkaist) ; Copyright (C) 2012-2013,2016 Ivan Baravy (dunkaist)
; ;
; This program is free software: you can redistribute it and/or modify ; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by ; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or ; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version. ; (at your option) any later version.
; ;
; This program is distributed in the hope that it will be useful, ; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of ; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details. ; GNU General Public License for more details.
; ;
; You should have received a copy of the GNU General Public License ; You should have received a copy of the GNU General Public License
; along with this program. If not, see <http://www.gnu.org/licenses/>. ; along with this program. If not, see <http://www.gnu.org/licenses/>.
CRC32_HASH_SIZE = 4 proc crc32.init _ctx
CRC32_ALIGN = 4 mov ebx, [_ctx]
CRC32_ALIGN_MASK = CRC32_ALIGN - 1 lea edi, [ebx + ctx_crc32.hash]
mov esi, crc32._.hash_init
struct ctx_crc32 mov ecx, 1
hash rd 1 rep movsd
ends ret
endp
proc crc32.init _ctx
mov ebx, [_ctx] proc crc32.update _ctx, _msg, _size
lea edi, [ebx + ctx_crc32.hash] mov ebx, [_ctx]
mov esi, crc32._.hash_init mov esi, [_msg]
mov ecx, 1 lea edi, [ebx + ctx_crc32.hash]
rep movsd mov eax, [edi]
ret mov ecx, [_size]
endp jecxz .quit
@@:
proc crc32.update _ctx, _msg, _size movzx edx, al
mov ebx, [_ctx] xor dl, byte[esi]
mov esi, [_msg] add esi, 1
lea edi, [ebx + ctx_crc32.hash] shr eax, 8
mov eax, [edi] xor eax, [crc32._.table + edx*4]
mov ecx, [_size] dec ecx
jecxz .quit jnz @b
@@: stosd
movzx edx, al .quit:
xor dl, byte[esi] ret
add esi, 1 endp
shr eax, 8
xor eax, [crc32._.table + edx*4]
dec ecx proc crc32.final _ctx
jnz @b mov ebx, [_ctx]
lea esi, [ebx + ctx_crc32.hash]
stosd mov edi, esi
.quit: lodsd
ret xor eax, 0xffffffff
endp bswap eax
stosd
ret
proc crc32.final _ctx endp
mov ebx, [_ctx]
lea esi, [ebx + ctx_crc32.hash]
mov edi, esi align CRC32_ALIGN
lodsd
xor eax, 0xffffffff crc32._.hash_init dd 0xffffffff
bswap eax
stosd crc32._.table dd \
ret 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,\
endp 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,\
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,\
0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,\
align CRC32_ALIGN 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,\
0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,\
crc32._.hash_init dd 0xffffffff 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,\
0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,\
crc32._.table dd \ 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,\
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,\ 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,\
0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,\ 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,\
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,\ 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,\
0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,\ 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,\
0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,\ 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,\
0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,\ 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,\
0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,\ 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,\
0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,\ 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,\
0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,\ 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,\
0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,\ 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,\
0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,\ 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,\
0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,\ 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,\
0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,\ 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,\
0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,\ 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,\
0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,\ 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,\
0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,\ 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,\
0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,\ 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,\
0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,\ 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,\
0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,\ 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,\
0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,\ 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,\
0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,\ 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,\
0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,\ 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,\
0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,\ 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,\
0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,\ 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,\
0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,\ 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,\
0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,\ 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,\
0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,\ 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,\
0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,\ 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,\
0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,\ 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,\
0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,\ 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,\
0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,\ 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,\
0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,\ 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,\
0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,\ 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,\
0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,\ 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,\
0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,\
0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,\
0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,\
0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,\
0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,\
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,\
0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,\
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d

View File

@ -1,129 +1,164 @@
; libcrash -- cryptographic hash functions ; libcrash -- cryptographic hash functions
; ;
; Copyright (C) 2012-2014,2016 Ivan Baravy (dunkaist) ; Copyright (C) 2012-2014,2016 Ivan Baravy (dunkaist)
; ;
; This program is free software: you can redistribute it and/or modify ; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by ; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or ; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version. ; (at your option) any later version.
; ;
; This program is distributed in the hope that it will be useful, ; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of ; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details. ; GNU General Public License for more details.
; ;
; You should have received a copy of the GNU General Public License ; You should have received a copy of the GNU General Public License
; along with this program. If not, see <http://www.gnu.org/licenses/>. ; along with this program. If not, see <http://www.gnu.org/licenses/>.
format MS COFF format MS COFF
public @EXPORT as 'EXPORTS' public @EXPORT as 'EXPORTS'
include '../../../../struct.inc' include '../../../../struct.inc'
include '../../../../proc32.inc' include '../../../../proc32.inc'
include '../../../../macros.inc' include '../../../../macros.inc'
include '../../../../config.inc' include '../../../../config.inc'
;include '../../../../debug.inc' ;include '../../../../debug.inc'
purge section,mov,add,sub purge section,mov,add,sub
section '.flat' code readable align 16 section '.flat' code readable align 16
include 'libcrash.inc' include 'libcrash.inc'
include 'crc32.asm' include 'crc32.asm'
include 'md4.asm' include 'md4.asm'
include 'md5.asm' include 'md5.asm'
include 'sha1.asm' include 'sha1.asm'
include 'sha224_256.asm' include 'sha224_256.asm'
include 'sha384_512.asm' include 'sha384_512.asm'
include 'sha3.asm' include 'sha3.asm'
proc lib_init proc lib_init
ret ret
endp endp
proc crash.hash _hid, _data, _callback, _ctx proc crash.hash _hid, _data, _callback, _ctx
locals locals
size dd ? size dd ?
endl endl
mov [size], 0 mov [size], 0
mov eax, [_hid] mov eax, [_hid]
imul eax, sizeof.crash_item imul eax, sizeof.crash_item
lea edx, [crash._.table + eax] lea edx, [crash._.table + eax]
mov ebx, [_ctx] mov ebx, [_ctx]
stdcall [edx + crash_item.init], [_ctx] stdcall [edx + crash_item.init], [_ctx]
.hash: .hash:
mov esi, [_data] mov esi, [_data]
push edx push edx
stdcall [edx + crash_item.update], [_ctx], [_data], [size] stdcall [edx + crash_item.update], [_ctx], [_data], [size]
mov [size], 0 mov [size], 0
pop edx pop edx
mov eax, [_callback] mov eax, [_callback]
test eax, eax test eax, eax
jz .quit jz .quit
push edx push edx
stdcall [_callback], [size] stdcall [_callback], [size]
pop edx pop edx
mov [size], eax mov [size], eax
test eax, eax test eax, eax
jnz .hash jnz .hash
stdcall [edx + crash_item.final], [_ctx] stdcall [edx + crash_item.final], [_ctx]
.quit: .quit:
ret ret
endp endp
proc crash.bin2hex _bin, _hex, _hid proc crash.bin2hex _bin, _hex, _hid
mov eax, [_hid] mov eax, [_hid]
imul eax, sizeof.crash_item imul eax, sizeof.crash_item
mov ecx, [crash._.table + eax + crash_item.len_out] mov ecx, [crash._.table + eax + crash_item.len_out]
mov ebx, crash._.bin2hex_table mov ebx, crash._.bin2hex_table
mov esi, [_bin] mov esi, [_bin]
mov edi, [_hex] mov edi, [_hex]
.next_byte: .next_byte:
xor eax, eax xor eax, eax
lodsb lodsb
shl eax, 4 shl eax, 4
shr al, 4 shr al, 4
xlatb xlatb
xchg al, ah xchg al, ah
xlatb xlatb
stosw stosw
dec ecx dec ecx
jnz .next_byte jnz .next_byte
xor al, al xor al, al
stosb stosb
ret ret
endp endp
section '.data' data readable align 16 section '.data' data readable align 16
crash._.bin2hex_table db '0123456789abcdef' crash._.bin2hex_table db '0123456789abcdef'
crash._.table dd \ crash._.table dd \
crc32.init, crc32.update, crc32.final, CRC32_HASH_SIZE, \ crc32.init, crc32.update, crc32.final, CRC32_HASH_SIZE, \
md4.init, md4.update, md4.final, MD4_HASH_SIZE, \ md4.init, md4.update, md4.final, MD4_HASH_SIZE, \
md5.init, md5.update, md5.final, MD5_HASH_SIZE, \ md5.init, md5.update, md5.final, MD5_HASH_SIZE, \
sha1.init, sha1.update, sha1.final, SHA1_HASH_SIZE, \ sha1.init, sha1.update, sha1.final, SHA1_HASH_SIZE, \
sha224.init, sha224256.update, sha224256.final, SHA224_HASH_SIZE, \ sha224.init, sha224.update, sha224.final, SHA224_HASH_SIZE, \
sha256.init, sha224256.update, sha224256.final, SHA256_HASH_SIZE, \ sha256.init, sha256.update, sha256.final, SHA256_HASH_SIZE, \
sha384.init, sha384512.update, sha384512.final, SHA384_HASH_SIZE, \ sha384.init, sha384.update, sha384.final, SHA384_HASH_SIZE, \
sha512.init, sha384512.update, sha384512.final, SHA512_HASH_SIZE, \ sha512.init, sha512.update, sha512.final, SHA512_HASH_SIZE, \
sha3224.init, sha3.update, sha3.final, SHA3224_HASH_SIZE,\ sha3224.init, sha3224.update, sha3224.final, SHA3224_HASH_SIZE,\
sha3256.init, sha3.update, sha3.final, SHA3256_HASH_SIZE,\ sha3256.init, sha3256.update, sha3256.final, SHA3256_HASH_SIZE,\
sha3384.init, sha3.update, sha3.final, SHA3384_HASH_SIZE,\ sha3384.init, sha3384.update, sha3384.final, SHA3384_HASH_SIZE,\
sha3512.init, sha3.update, sha3.final, SHA3512_HASH_SIZE sha3512.init, sha3512.update, sha3512.final, SHA3512_HASH_SIZE
align 4 align 4
@EXPORT: @EXPORT:
export \ export \
lib_init , 'lib_init' , \ lib_init, 'lib_init' , \
crash.hash , 'crash_hash' , \ crash.hash, 'crash_hash' , \
crash.bin2hex , 'crash_bin2hex' crash.bin2hex, 'crash_bin2hex' , \
crc32.init, 'crc32_init' , \
crc32.update, 'crc32_update' , \
crc32.final, 'crc32_final' , \
md4.init, 'md4_init' , \
md4.update, 'md4_update' , \
md4.final, 'md4_final' , \
md5.init, 'md5_init' , \
md5.update, 'md5_update' , \
md5.final, 'md5_final' , \
sha1.init, 'sha1_init' , \
sha1.update, 'sha1_update' , \
sha1.final, 'sha1_final' , \
sha224.init, 'sha224_init' , \
sha224.update, 'sha224_update' , \
sha224.final, 'sha224_final' , \
sha256.init, 'sha256_init' , \
sha256.update, 'sha256_update' , \
sha256.final, 'sha256_final' , \
sha384.init, 'sha384_init' , \
sha384.update, 'sha384_update' , \
sha384.final, 'sha384_final' , \
sha512.init, 'sha512_init' , \
sha512.update, 'sha512_update' , \
sha512.final, 'sha512_final' , \
sha3224.init, 'sha3_224_init' , \
sha3224.update, 'sha3_224_update' , \
sha3224.final, 'sha3_224_final' , \
sha3256.init, 'sha3_256_init' , \
sha3256.update, 'sha3_256_update' , \
sha3256.final, 'sha3_256_final' , \
sha3384.init, 'sha3_384_init' , \
sha3384.update, 'sha3_384_update' , \
sha3384.final, 'sha3_384_final' , \
sha3512.init, 'sha3_512_init' , \
sha3512.update, 'sha3_512_update' , \
sha3512.final, 'sha3_512_final'

View File

@ -1,40 +1,175 @@
; libcrash -- cryptographic hash functions ; libcrash -- cryptographic hash functions
; ;
; Copyright (C) 2012-2014,2016 Ivan Baravy (dunkaist) ; Copyright (C) 2012-2014,2016 Ivan Baravy (dunkaist)
; ;
; This program is free software: you can redistribute it and/or modify ; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by ; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or ; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version. ; (at your option) any later version.
; ;
; This program is distributed in the hope that it will be useful, ; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of ; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details. ; GNU General Public License for more details.
; ;
; You should have received a copy of the GNU General Public License ; You should have received a copy of the GNU General Public License
; along with this program. If not, see <http://www.gnu.org/licenses/>. ; along with this program. If not, see <http://www.gnu.org/licenses/>.
LIBCRASH_CRC32 = 0 LIBCRASH_CRC32 = 0
LIBCRASH_MD4 = 1 LIBCRASH_MD4 = 1
LIBCRASH_MD5 = 2 LIBCRASH_MD5 = 2
LIBCRASH_SHA1 = 3 LIBCRASH_SHA1 = 3
LIBCRASH_SHA224 = 4 LIBCRASH_SHA224 = 4
LIBCRASH_SHA256 = 5 LIBCRASH_SHA256 = 5
LIBCRASH_SHA384 = 6 LIBCRASH_SHA384 = 6
LIBCRASH_SHA512 = 7 LIBCRASH_SHA512 = 7
LIBCRASH_SHA3_224 = 8 LIBCRASH_SHA3_224 = 8
LIBCRASH_SHA3_256 = 9 LIBCRASH_SHA3_256 = 9
LIBCRASH_SHA3_384 = 10 LIBCRASH_SHA3_384 = 10
LIBCRASH_SHA3_512 = 11 LIBCRASH_SHA3_512 = 11
LIBCRASH_LAST = 11 LIBCRASH_LAST = 11
struct crash_item struct crash_item
init dd ? init dd ?
update dd ? update dd ?
final dd ? final dd ?
len_out dd ? len_out dd ?
ends ends
; CRC32
CRC32_HASH_SIZE = 4
CRC32_ALIGN = 4
CRC32_ALIGN_MASK = CRC32_ALIGN - 1
struct ctx_crc32
hash rd 1
ends
; MD4
MD4_BLOCK_SIZE = 64
MD4_HASH_SIZE = 16
MD4_ALIGN = 4
MD4_ALIGN_MASK = MD4_ALIGN - 1
struct ctx_md4
hash rb MD4_HASH_SIZE
block rb MD4_BLOCK_SIZE
index rd 1
msglen_0 rd 1
msglen_1 rd 1
ends
; MD5
MD5_BLOCK_SIZE = 64
MD5_HASH_SIZE = 16
MD5_ALIGN = 4
MD5_ALIGN_MASK = MD5_ALIGN - 1
struct ctx_md5
hash rb MD5_HASH_SIZE
block rb MD5_BLOCK_SIZE
index rd 1
msglen_0 rd 1
msglen_1 rd 1
ends
; SHA1
SHA1_BLOCK_SIZE = 64
SHA1_HASH_SIZE = 20
SHA1_ALIGN = 4
SHA1_ALIGN_MASK = SHA1_ALIGN - 1
struct ctx_sha1
hash rb SHA1_HASH_SIZE
block rb SHA1_BLOCK_SIZE
index rd 1
msglen_0 rd 1
msglen_1 rd 1
ends
; SHA2
SHA224256_BLOCK_SIZE = 64
SHA224256_INIT_SIZE = 32
SHA224_HASH_SIZE = 28
SHA256_HASH_SIZE = 32
SHA224256_ALIGN = 4
SHA224256_ALIGN_MASK = SHA224256_ALIGN - 1
struct ctx_sha224256
hash rb SHA224256_INIT_SIZE
block rb SHA224256_BLOCK_SIZE
index rd 1
msglen_0 rd 1
msglen_1 rd 1
ends
SHA384512_BLOCK_SIZE = 128
SHA384512_INIT_SIZE = 64
SHA384_HASH_SIZE = 48
SHA512_HASH_SIZE = 64
SHA384512_ALIGN = 16
SHA384512_ALIGN_MASK = SHA384512_ALIGN - 1
struct ctx_sha384512
hash rb SHA384512_INIT_SIZE
block rb SHA384512_BLOCK_SIZE
index rd 1
msglen_0 rd 1
msglen_1 rd 1
msglen_2 rd 1
msglen_3 rd 1
rd 3 ; align
; tmp vars
w rq 80
A rq 1
B rq 1
C rq 1
D rq 1
E rq 1
F rq 1
G rq 1
H rq 1
temp rq 1
ends
; SHA3
SHA3224_BLOCK_SIZE = 144
SHA3256_BLOCK_SIZE = 136
SHA3384_BLOCK_SIZE = 104
SHA3512_BLOCK_SIZE = 72
SHA3MAX_BLOCK_SIZE = SHA3224_BLOCK_SIZE
SHA3_INIT_SIZE = 200
SHA3224_HASH_SIZE = 28
SHA3256_HASH_SIZE = 32
SHA3384_HASH_SIZE = 48
SHA3512_HASH_SIZE = 64
SHA3_ALIGN = 16
SHA3_ALIGN_MASK = SHA3_ALIGN-1
struct ctx_sha3
hash rb SHA3_INIT_SIZE
rb SHA3_ALIGN - (SHA3_INIT_SIZE mod SHA3_ALIGN)
block rb SHA3MAX_BLOCK_SIZE
rb SHA3_ALIGN - (SHA3MAX_BLOCK_SIZE mod SHA3_ALIGN)
index rd 1
block_size rd 1
rounds_cnt rd 1
rd 1 ; align
; tmp vars
C rq 5
D rq 5
ends

View File

@ -1,251 +1,237 @@
; libcrash -- cryptographic hash functions ; libcrash -- cryptographic hash functions
; ;
; Copyright (C) 2012-2013,2016 Ivan Baravy (dunkaist) ; Copyright (C) 2012-2013,2016 Ivan Baravy (dunkaist)
; ;
; This program is free software: you can redistribute it and/or modify ; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by ; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or ; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version. ; (at your option) any later version.
; ;
; This program is distributed in the hope that it will be useful, ; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of ; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details. ; GNU General Public License for more details.
; ;
; You should have received a copy of the GNU General Public License ; You should have received a copy of the GNU General Public License
; along with this program. If not, see <http://www.gnu.org/licenses/>. ; along with this program. If not, see <http://www.gnu.org/licenses/>.
MD4_BLOCK_SIZE = 64 macro md4._.f b, c, d
MD4_HASH_SIZE = 16 {
MD4_ALIGN = 4 mov eax, c
MD4_ALIGN_MASK = MD4_ALIGN - 1 xor eax, d
and eax, b
struct ctx_md4 xor eax, d
hash rb MD4_HASH_SIZE }
block rb MD4_BLOCK_SIZE
index rd 1 macro md4._.g b, c, d
msglen_0 rd 1 {
msglen_1 rd 1 push c d
ends mov eax, b
and eax, c
and c, d
macro md4._.f b, c, d and d, b
{ or eax, c
mov eax, c or eax, d
xor eax, d pop d c
and eax, b }
xor eax, d
} macro md4._.h b, c, d
{
macro md4._.g b, c, d mov eax, b
{ xor eax, c
push c d xor eax, d
mov eax, b }
and eax, c
and c, d macro md4._.round func, a, b, c, d, index, shift, ac
and d, b {
or eax, c func b, c, d
or eax, d add eax, [esi + index*4]
pop d c lea a, [a + eax + ac]
} rol a, shift
}
macro md4._.h b, c, d
{
mov eax, b proc md4.init _ctx
xor eax, c mov ebx, [_ctx]
xor eax, d lea edi, [ebx + ctx_md4.hash]
} mov esi, md4._.hash_init
mov ecx, MD4_HASH_SIZE/4
macro md4._.round func, a, b, c, d, index, shift, ac rep movsd
{ xor eax, eax
func b, c, d mov [ebx + ctx_md4.index], eax
add eax, [esi + index*4] mov [ebx + ctx_md4.msglen_0], eax
lea a, [a + eax + ac] mov [ebx + ctx_md4.msglen_1], eax
rol a, shift ret
} endp
proc md4.init _ctx proc md4._.block _hash
mov ebx, [_ctx]
lea edi, [ebx + ctx_md4.hash] mov eax, [_hash]
mov esi, md4._.hash_init mov edi, [eax + 0x0]
mov ecx, MD4_HASH_SIZE/4 mov ebx, [eax + 0x4]
rep movsd mov ecx, [eax + 0x8]
xor eax, eax mov edx, [eax + 0xc]
mov [ebx + ctx_md4.index], eax
mov [ebx + ctx_md4.msglen_0], eax md4._.round md4._.f, edi, ebx, ecx, edx, 0, 3, 0x00000000
mov [ebx + ctx_md4.msglen_1], eax md4._.round md4._.f, edx, edi, ebx, ecx, 1, 7, 0x00000000
ret md4._.round md4._.f, ecx, edx, edi, ebx, 2, 11, 0x00000000
endp md4._.round md4._.f, ebx, ecx, edx, edi, 3, 19, 0x00000000
md4._.round md4._.f, edi, ebx, ecx, edx, 4, 3, 0x00000000
md4._.round md4._.f, edx, edi, ebx, ecx, 5, 7, 0x00000000
proc md4._.block _hash md4._.round md4._.f, ecx, edx, edi, ebx, 6, 11, 0x00000000
md4._.round md4._.f, ebx, ecx, edx, edi, 7, 19, 0x00000000
mov eax, [_hash] md4._.round md4._.f, edi, ebx, ecx, edx, 8, 3, 0x00000000
mov edi, [eax + 0x0] md4._.round md4._.f, edx, edi, ebx, ecx, 9, 7, 0x00000000
mov ebx, [eax + 0x4] md4._.round md4._.f, ecx, edx, edi, ebx, 10, 11, 0x00000000
mov ecx, [eax + 0x8] md4._.round md4._.f, ebx, ecx, edx, edi, 11, 19, 0x00000000
mov edx, [eax + 0xc] md4._.round md4._.f, edi, ebx, ecx, edx, 12, 3, 0x00000000
md4._.round md4._.f, edx, edi, ebx, ecx, 13, 7, 0x00000000
md4._.round md4._.f, edi, ebx, ecx, edx, 0, 3, 0x00000000 md4._.round md4._.f, ecx, edx, edi, ebx, 14, 11, 0x00000000
md4._.round md4._.f, edx, edi, ebx, ecx, 1, 7, 0x00000000 md4._.round md4._.f, ebx, ecx, edx, edi, 15, 19, 0x00000000
md4._.round md4._.f, ecx, edx, edi, ebx, 2, 11, 0x00000000
md4._.round md4._.f, ebx, ecx, edx, edi, 3, 19, 0x00000000 md4._.round md4._.g, edi, ebx, ecx, edx, 0, 3, 0x5a827999
md4._.round md4._.f, edi, ebx, ecx, edx, 4, 3, 0x00000000 md4._.round md4._.g, edx, edi, ebx, ecx, 4, 5, 0x5a827999
md4._.round md4._.f, edx, edi, ebx, ecx, 5, 7, 0x00000000 md4._.round md4._.g, ecx, edx, edi, ebx, 8, 9, 0x5a827999
md4._.round md4._.f, ecx, edx, edi, ebx, 6, 11, 0x00000000 md4._.round md4._.g, ebx, ecx, edx, edi, 12, 13, 0x5a827999
md4._.round md4._.f, ebx, ecx, edx, edi, 7, 19, 0x00000000 md4._.round md4._.g, edi, ebx, ecx, edx, 1, 3, 0x5a827999
md4._.round md4._.f, edi, ebx, ecx, edx, 8, 3, 0x00000000 md4._.round md4._.g, edx, edi, ebx, ecx, 5, 5, 0x5a827999
md4._.round md4._.f, edx, edi, ebx, ecx, 9, 7, 0x00000000 md4._.round md4._.g, ecx, edx, edi, ebx, 9, 9, 0x5a827999
md4._.round md4._.f, ecx, edx, edi, ebx, 10, 11, 0x00000000 md4._.round md4._.g, ebx, ecx, edx, edi, 13, 13, 0x5a827999
md4._.round md4._.f, ebx, ecx, edx, edi, 11, 19, 0x00000000 md4._.round md4._.g, edi, ebx, ecx, edx, 2, 3, 0x5a827999
md4._.round md4._.f, edi, ebx, ecx, edx, 12, 3, 0x00000000 md4._.round md4._.g, edx, edi, ebx, ecx, 6, 5, 0x5a827999
md4._.round md4._.f, edx, edi, ebx, ecx, 13, 7, 0x00000000 md4._.round md4._.g, ecx, edx, edi, ebx, 10, 9, 0x5a827999
md4._.round md4._.f, ecx, edx, edi, ebx, 14, 11, 0x00000000 md4._.round md4._.g, ebx, ecx, edx, edi, 14, 13, 0x5a827999
md4._.round md4._.f, ebx, ecx, edx, edi, 15, 19, 0x00000000 md4._.round md4._.g, edi, ebx, ecx, edx, 3, 3, 0x5a827999
md4._.round md4._.g, edx, edi, ebx, ecx, 7, 5, 0x5a827999
md4._.round md4._.g, edi, ebx, ecx, edx, 0, 3, 0x5a827999 md4._.round md4._.g, ecx, edx, edi, ebx, 11, 9, 0x5a827999
md4._.round md4._.g, edx, edi, ebx, ecx, 4, 5, 0x5a827999 md4._.round md4._.g, ebx, ecx, edx, edi, 15, 13, 0x5a827999
md4._.round md4._.g, ecx, edx, edi, ebx, 8, 9, 0x5a827999
md4._.round md4._.g, ebx, ecx, edx, edi, 12, 13, 0x5a827999 md4._.round md4._.h, edi, ebx, ecx, edx, 0, 3, 0x6ed9eba1
md4._.round md4._.g, edi, ebx, ecx, edx, 1, 3, 0x5a827999 md4._.round md4._.h, edx, edi, ebx, ecx, 8, 9, 0x6ed9eba1
md4._.round md4._.g, edx, edi, ebx, ecx, 5, 5, 0x5a827999 md4._.round md4._.h, ecx, edx, edi, ebx, 4, 11, 0x6ed9eba1
md4._.round md4._.g, ecx, edx, edi, ebx, 9, 9, 0x5a827999 md4._.round md4._.h, ebx, ecx, edx, edi, 12, 15, 0x6ed9eba1
md4._.round md4._.g, ebx, ecx, edx, edi, 13, 13, 0x5a827999 md4._.round md4._.h, edi, ebx, ecx, edx, 2, 3, 0x6ed9eba1
md4._.round md4._.g, edi, ebx, ecx, edx, 2, 3, 0x5a827999 md4._.round md4._.h, edx, edi, ebx, ecx, 10, 9, 0x6ed9eba1
md4._.round md4._.g, edx, edi, ebx, ecx, 6, 5, 0x5a827999 md4._.round md4._.h, ecx, edx, edi, ebx, 6, 11, 0x6ed9eba1
md4._.round md4._.g, ecx, edx, edi, ebx, 10, 9, 0x5a827999 md4._.round md4._.h, ebx, ecx, edx, edi, 14, 15, 0x6ed9eba1
md4._.round md4._.g, ebx, ecx, edx, edi, 14, 13, 0x5a827999 md4._.round md4._.h, edi, ebx, ecx, edx, 1, 3, 0x6ed9eba1
md4._.round md4._.g, edi, ebx, ecx, edx, 3, 3, 0x5a827999 md4._.round md4._.h, edx, edi, ebx, ecx, 9, 9, 0x6ed9eba1
md4._.round md4._.g, edx, edi, ebx, ecx, 7, 5, 0x5a827999 md4._.round md4._.h, ecx, edx, edi, ebx, 5, 11, 0x6ed9eba1
md4._.round md4._.g, ecx, edx, edi, ebx, 11, 9, 0x5a827999 md4._.round md4._.h, ebx, ecx, edx, edi, 13, 15, 0x6ed9eba1
md4._.round md4._.g, ebx, ecx, edx, edi, 15, 13, 0x5a827999 md4._.round md4._.h, edi, ebx, ecx, edx, 3, 3, 0x6ed9eba1
md4._.round md4._.h, edx, edi, ebx, ecx, 11, 9, 0x6ed9eba1
md4._.round md4._.h, edi, ebx, ecx, edx, 0, 3, 0x6ed9eba1 md4._.round md4._.h, ecx, edx, edi, ebx, 7, 11, 0x6ed9eba1
md4._.round md4._.h, edx, edi, ebx, ecx, 8, 9, 0x6ed9eba1 md4._.round md4._.h, ebx, ecx, edx, edi, 15, 15, 0x6ed9eba1
md4._.round md4._.h, ecx, edx, edi, ebx, 4, 11, 0x6ed9eba1
md4._.round md4._.h, ebx, ecx, edx, edi, 12, 15, 0x6ed9eba1 mov eax, [_hash]
md4._.round md4._.h, edi, ebx, ecx, edx, 2, 3, 0x6ed9eba1 add [eax + 0x0], edi
md4._.round md4._.h, edx, edi, ebx, ecx, 10, 9, 0x6ed9eba1 add [eax + 0x4], ebx
md4._.round md4._.h, ecx, edx, edi, ebx, 6, 11, 0x6ed9eba1 add [eax + 0x8], ecx
md4._.round md4._.h, ebx, ecx, edx, edi, 14, 15, 0x6ed9eba1 add [eax + 0xc], edx
md4._.round md4._.h, edi, ebx, ecx, edx, 1, 3, 0x6ed9eba1
md4._.round md4._.h, edx, edi, ebx, ecx, 9, 9, 0x6ed9eba1 ret
md4._.round md4._.h, ecx, edx, edi, ebx, 5, 11, 0x6ed9eba1 endp
md4._.round md4._.h, ebx, ecx, edx, edi, 13, 15, 0x6ed9eba1
md4._.round md4._.h, edi, ebx, ecx, edx, 3, 3, 0x6ed9eba1
md4._.round md4._.h, edx, edi, ebx, ecx, 11, 9, 0x6ed9eba1 proc md4.update _ctx, _msg, _size
md4._.round md4._.h, ecx, edx, edi, ebx, 7, 11, 0x6ed9eba1 mov ebx, [_ctx]
md4._.round md4._.h, ebx, ecx, edx, edi, 15, 15, 0x6ed9eba1 mov ecx, [_size]
add [ebx + ctx_md4.msglen_0], ecx
mov eax, [_hash] adc [ebx + ctx_md4.msglen_1], 0
add [eax + 0x0], edi
add [eax + 0x4], ebx .next_block:
add [eax + 0x8], ecx mov ebx, [_ctx]
add [eax + 0xc], edx mov esi, [_msg]
mov eax, [ebx + ctx_md4.index]
ret and eax, MD4_BLOCK_SIZE-1
endp jnz .copy_to_buf
test esi, MD4_ALIGN_MASK
jnz .copy_to_buf
proc md4.update _ctx, _msg, _size .no_copy:
mov ebx, [_ctx] ; data is aligned, hash it in place without copying
mov ecx, [_size] mov ebx, [_ctx]
add [ebx + ctx_md4.msglen_0], ecx cmp [_size], MD4_BLOCK_SIZE
adc [ebx + ctx_md4.msglen_1], 0 jb .copy_quit
lea eax, [ebx + ctx_md4.hash]
.next_block: stdcall md4._.block, eax
mov ebx, [_ctx] sub [_size], MD4_BLOCK_SIZE
mov esi, [_msg] add esi, MD4_BLOCK_SIZE
mov eax, [ebx + ctx_md4.index] jmp .no_copy
and eax, MD4_BLOCK_SIZE-1
jnz .copy_to_buf .copy_to_buf:
test esi, MD4_ALIGN_MASK lea edi, [ebx + ctx_md4.block]
jnz .copy_to_buf add edi, eax
.no_copy: mov ecx, MD4_BLOCK_SIZE
; data is aligned, hash it in place without copying sub ecx, eax
mov ebx, [_ctx] cmp [_size], ecx
cmp [_size], MD4_BLOCK_SIZE jb .copy_quit
jb .copy_quit sub [_size], ecx
lea eax, [ebx + ctx_md4.hash] add [_msg], ecx
stdcall md4._.block, eax add [ebx + ctx_md4.index], ecx
sub [_size], MD4_BLOCK_SIZE rep movsb
add esi, MD4_BLOCK_SIZE lea eax, [ebx + ctx_md4.hash]
jmp .no_copy lea esi, [ebx + ctx_md4.block]
stdcall md4._.block, eax
.copy_to_buf: jmp .next_block
lea edi, [ebx + ctx_md4.block]
add edi, eax .copy_quit:
mov ecx, MD4_BLOCK_SIZE mov ebx, [_ctx]
sub ecx, eax lea edi, [ebx + ctx_md4.block]
cmp [_size], ecx mov eax, [ebx + ctx_md4.index]
jb .copy_quit and eax, MD4_BLOCK_SIZE-1
sub [_size], ecx add edi, eax
add [_msg], ecx mov ecx, [_size]
add [ebx + ctx_md4.index], ecx add [ebx + ctx_md4.index], ecx
rep movsb rep movsb
lea eax, [ebx + ctx_md4.hash] .quit:
lea esi, [ebx + ctx_md4.block] ret
stdcall md4._.block, eax endp
jmp .next_block
.copy_quit: proc md4.final _ctx
mov ebx, [_ctx] mov ebx, [_ctx]
lea edi, [ebx + ctx_md4.block] lea edi, [ebx + ctx_md4.block]
mov eax, [ebx + ctx_md4.index] mov ecx, [ebx + ctx_md4.msglen_0]
and eax, MD4_BLOCK_SIZE-1 and ecx, MD4_BLOCK_SIZE-1
add edi, eax add edi, ecx
mov ecx, [_size] mov byte[edi], 0x80
add [ebx + ctx_md4.index], ecx inc edi
rep movsb neg ecx
.quit: add ecx, MD4_BLOCK_SIZE
ret cmp ecx, 8
endp ja .last
dec ecx
proc md4.final _ctx xor eax, eax
mov ebx, [_ctx] rep stosb
lea edi, [ebx + ctx_md4.block] lea esi, [ebx + ctx_md4.block]
mov ecx, [ebx + ctx_md4.msglen_0] lea eax, [ebx + ctx_md4.hash]
and ecx, MD4_BLOCK_SIZE-1 stdcall md4._.block, eax
add edi, ecx mov ebx, [_ctx]
mov byte[edi], 0x80 lea edi, [ebx + ctx_md4.block]
inc edi mov ecx, MD4_BLOCK_SIZE+1
neg ecx .last:
add ecx, MD4_BLOCK_SIZE dec ecx
cmp ecx, 8 sub ecx, 8
ja .last xor eax, eax
rep stosb
dec ecx mov eax, [ebx + ctx_md4.msglen_0]
xor eax, eax mov edx, [ebx + ctx_md4.msglen_1]
rep stosb shld edx, eax, 3
lea esi, [ebx + ctx_md4.block] shl eax, 3
lea eax, [ebx + ctx_md4.hash] mov dword[edi], eax
stdcall md4._.block, eax mov dword[edi+4], edx
mov ebx, [_ctx] lea esi, [ebx + ctx_md4.block]
lea edi, [ebx + ctx_md4.block] lea eax, [ebx + ctx_md4.hash]
mov ecx, MD4_BLOCK_SIZE+1 stdcall md4._.block, eax
.last:
dec ecx ret
sub ecx, 8 endp
xor eax, eax
rep stosb
mov eax, [ebx + ctx_md4.msglen_0] align MD4_ALIGN
mov edx, [ebx + ctx_md4.msglen_1]
shld edx, eax, 3 md4._.hash_init dd 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0
shl eax, 3
mov dword[edi], eax
mov dword[edi+4], edx
lea esi, [ebx + ctx_md4.block]
lea eax, [ebx + ctx_md4.hash]
stdcall md4._.block, eax
ret
endp
align MD4_ALIGN
md4._.hash_init dd 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0

View File

@ -1,279 +1,265 @@
; libcrash -- cryptographic hash functions ; libcrash -- cryptographic hash functions
; ;
; Copyright (C) 2012-2013,2016 Ivan Baravy (dunkaist) ; Copyright (C) 2012-2013,2016 Ivan Baravy (dunkaist)
; ;
; This program is free software: you can redistribute it and/or modify ; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by ; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or ; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version. ; (at your option) any later version.
; ;
; This program is distributed in the hope that it will be useful, ; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of ; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details. ; GNU General Public License for more details.
; ;
; You should have received a copy of the GNU General Public License ; You should have received a copy of the GNU General Public License
; along with this program. If not, see <http://www.gnu.org/licenses/>. ; along with this program. If not, see <http://www.gnu.org/licenses/>.
MD5_BLOCK_SIZE = 64 macro md5._.f b, c, d
MD5_HASH_SIZE = 16 {
MD5_ALIGN = 4 push c
MD5_ALIGN_MASK = MD5_ALIGN - 1 xor c, d
and b, c
struct ctx_md5 xor b, d
hash rb MD5_HASH_SIZE pop c
block rb MD5_BLOCK_SIZE }
index rd 1
msglen_0 rd 1 macro md5._.g b, c, d
msglen_1 rd 1 {
ends push c d
and b, d
not d
macro md5._.f b, c, d and c, d
{ or b, c
push c pop d c
xor c, d }
and b, c
xor b, d macro md5._.h b, c, d
pop c {
} xor b, c
xor b, d
macro md5._.g b, c, d }
{
push c d macro md5._.i b, c, d
and b, d {
not d push d
and c, d not d
or b, c or b, d
pop d c xor b, c
} pop d
}
macro md5._.h b, c, d
{ macro md5._.round func, a, b, c, d, index, shift, ac
xor b, c {
xor b, d push b
} func b, c, d
lea a, [a + b + ac]
macro md5._.i b, c, d add a, [esi + index*4]
{ rol a, shift
push d pop b
not d add a, b
or b, d }
xor b, c
pop d
} proc md5.init _ctx
mov ebx, [_ctx]
macro md5._.round func, a, b, c, d, index, shift, ac lea edi, [ebx + ctx_md5.hash]
{ mov esi, md5._.hash_init
push b mov ecx, MD5_HASH_SIZE/4
func b, c, d rep movsd
lea a, [a + b + ac] xor eax, eax
add a, [esi + index*4] mov [ebx + ctx_md5.index], eax
rol a, shift mov [ebx + ctx_md5.msglen_0], eax
pop b mov [ebx + ctx_md5.msglen_1], eax
add a, b ret
} endp
proc md5.init _ctx proc md5._.block _hash
mov ebx, [_ctx]
lea edi, [ebx + ctx_md5.hash] mov edi, [_hash]
mov esi, md5._.hash_init mov eax, [edi + 0x0]
mov ecx, MD5_HASH_SIZE/4 mov ebx, [edi + 0x4]
rep movsd mov ecx, [edi + 0x8]
xor eax, eax mov edx, [edi + 0xc]
mov [ebx + ctx_md5.index], eax
mov [ebx + ctx_md5.msglen_0], eax md5._.round md5._.f, eax, ebx, ecx, edx, 0, 7, 0xd76aa478
mov [ebx + ctx_md5.msglen_1], eax md5._.round md5._.f, edx, eax, ebx, ecx, 1, 12, 0xe8c7b756
ret md5._.round md5._.f, ecx, edx, eax, ebx, 2, 17, 0x242070db
endp md5._.round md5._.f, ebx, ecx, edx, eax, 3, 22, 0xc1bdceee
md5._.round md5._.f, eax, ebx, ecx, edx, 4, 7, 0xf57c0faf
md5._.round md5._.f, edx, eax, ebx, ecx, 5, 12, 0x4787c62a
proc md5._.block _hash md5._.round md5._.f, ecx, edx, eax, ebx, 6, 17, 0xa8304613
md5._.round md5._.f, ebx, ecx, edx, eax, 7, 22, 0xfd469501
mov edi, [_hash] md5._.round md5._.f, eax, ebx, ecx, edx, 8, 7, 0x698098d8
mov eax, [edi + 0x0] md5._.round md5._.f, edx, eax, ebx, ecx, 9, 12, 0x8b44f7af
mov ebx, [edi + 0x4] md5._.round md5._.f, ecx, edx, eax, ebx, 10, 17, 0xffff5bb1
mov ecx, [edi + 0x8] md5._.round md5._.f, ebx, ecx, edx, eax, 11, 22, 0x895cd7be
mov edx, [edi + 0xc] md5._.round md5._.f, eax, ebx, ecx, edx, 12, 7, 0x6b901122
md5._.round md5._.f, edx, eax, ebx, ecx, 13, 12, 0xfd987193
md5._.round md5._.f, eax, ebx, ecx, edx, 0, 7, 0xd76aa478 md5._.round md5._.f, ecx, edx, eax, ebx, 14, 17, 0xa679438e
md5._.round md5._.f, edx, eax, ebx, ecx, 1, 12, 0xe8c7b756 md5._.round md5._.f, ebx, ecx, edx, eax, 15, 22, 0x49b40821
md5._.round md5._.f, ecx, edx, eax, ebx, 2, 17, 0x242070db
md5._.round md5._.f, ebx, ecx, edx, eax, 3, 22, 0xc1bdceee md5._.round md5._.g, eax, ebx, ecx, edx, 1, 5, 0xf61e2562
md5._.round md5._.f, eax, ebx, ecx, edx, 4, 7, 0xf57c0faf md5._.round md5._.g, edx, eax, ebx, ecx, 6, 9, 0xc040b340
md5._.round md5._.f, edx, eax, ebx, ecx, 5, 12, 0x4787c62a md5._.round md5._.g, ecx, edx, eax, ebx, 11, 14, 0x265e5a51
md5._.round md5._.f, ecx, edx, eax, ebx, 6, 17, 0xa8304613 md5._.round md5._.g, ebx, ecx, edx, eax, 0, 20, 0xe9b6c7aa
md5._.round md5._.f, ebx, ecx, edx, eax, 7, 22, 0xfd469501 md5._.round md5._.g, eax, ebx, ecx, edx, 5, 5, 0xd62f105d
md5._.round md5._.f, eax, ebx, ecx, edx, 8, 7, 0x698098d8 md5._.round md5._.g, edx, eax, ebx, ecx, 10, 9, 0x02441453
md5._.round md5._.f, edx, eax, ebx, ecx, 9, 12, 0x8b44f7af md5._.round md5._.g, ecx, edx, eax, ebx, 15, 14, 0xd8a1e681
md5._.round md5._.f, ecx, edx, eax, ebx, 10, 17, 0xffff5bb1 md5._.round md5._.g, ebx, ecx, edx, eax, 4, 20, 0xe7d3fbc8
md5._.round md5._.f, ebx, ecx, edx, eax, 11, 22, 0x895cd7be md5._.round md5._.g, eax, ebx, ecx, edx, 9, 5, 0x21e1cde6
md5._.round md5._.f, eax, ebx, ecx, edx, 12, 7, 0x6b901122 md5._.round md5._.g, edx, eax, ebx, ecx, 14, 9, 0xc33707d6
md5._.round md5._.f, edx, eax, ebx, ecx, 13, 12, 0xfd987193 md5._.round md5._.g, ecx, edx, eax, ebx, 3, 14, 0xf4d50d87
md5._.round md5._.f, ecx, edx, eax, ebx, 14, 17, 0xa679438e md5._.round md5._.g, ebx, ecx, edx, eax, 8, 20, 0x455a14ed
md5._.round md5._.f, ebx, ecx, edx, eax, 15, 22, 0x49b40821 md5._.round md5._.g, eax, ebx, ecx, edx, 13, 5, 0xa9e3e905
md5._.round md5._.g, edx, eax, ebx, ecx, 2, 9, 0xfcefa3f8
md5._.round md5._.g, eax, ebx, ecx, edx, 1, 5, 0xf61e2562 md5._.round md5._.g, ecx, edx, eax, ebx, 7, 14, 0x676f02d9
md5._.round md5._.g, edx, eax, ebx, ecx, 6, 9, 0xc040b340 md5._.round md5._.g, ebx, ecx, edx, eax, 12, 20, 0x8d2a4c8a
md5._.round md5._.g, ecx, edx, eax, ebx, 11, 14, 0x265e5a51
md5._.round md5._.g, ebx, ecx, edx, eax, 0, 20, 0xe9b6c7aa md5._.round md5._.h, eax, ebx, ecx, edx, 5, 4, 0xfffa3942
md5._.round md5._.g, eax, ebx, ecx, edx, 5, 5, 0xd62f105d md5._.round md5._.h, edx, eax, ebx, ecx, 8, 11, 0x8771f681
md5._.round md5._.g, edx, eax, ebx, ecx, 10, 9, 0x02441453 md5._.round md5._.h, ecx, edx, eax, ebx, 11, 16, 0x6d9d6122
md5._.round md5._.g, ecx, edx, eax, ebx, 15, 14, 0xd8a1e681 md5._.round md5._.h, ebx, ecx, edx, eax, 14, 23, 0xfde5380c
md5._.round md5._.g, ebx, ecx, edx, eax, 4, 20, 0xe7d3fbc8 md5._.round md5._.h, eax, ebx, ecx, edx, 1, 4, 0xa4beea44
md5._.round md5._.g, eax, ebx, ecx, edx, 9, 5, 0x21e1cde6 md5._.round md5._.h, edx, eax, ebx, ecx, 4, 11, 0x4bdecfa9
md5._.round md5._.g, edx, eax, ebx, ecx, 14, 9, 0xc33707d6 md5._.round md5._.h, ecx, edx, eax, ebx, 7, 16, 0xf6bb4b60
md5._.round md5._.g, ecx, edx, eax, ebx, 3, 14, 0xf4d50d87 md5._.round md5._.h, ebx, ecx, edx, eax, 10, 23, 0xbebfbc70
md5._.round md5._.g, ebx, ecx, edx, eax, 8, 20, 0x455a14ed md5._.round md5._.h, eax, ebx, ecx, edx, 13, 4, 0x289b7ec6
md5._.round md5._.g, eax, ebx, ecx, edx, 13, 5, 0xa9e3e905 md5._.round md5._.h, edx, eax, ebx, ecx, 0, 11, 0xeaa127fa
md5._.round md5._.g, edx, eax, ebx, ecx, 2, 9, 0xfcefa3f8 md5._.round md5._.h, ecx, edx, eax, ebx, 3, 16, 0xd4ef3085
md5._.round md5._.g, ecx, edx, eax, ebx, 7, 14, 0x676f02d9 md5._.round md5._.h, ebx, ecx, edx, eax, 6, 23, 0x04881d05
md5._.round md5._.g, ebx, ecx, edx, eax, 12, 20, 0x8d2a4c8a md5._.round md5._.h, eax, ebx, ecx, edx, 9, 4, 0xd9d4d039
md5._.round md5._.h, edx, eax, ebx, ecx, 12, 11, 0xe6db99e5
md5._.round md5._.h, eax, ebx, ecx, edx, 5, 4, 0xfffa3942 md5._.round md5._.h, ecx, edx, eax, ebx, 15, 16, 0x1fa27cf8
md5._.round md5._.h, edx, eax, ebx, ecx, 8, 11, 0x8771f681 md5._.round md5._.h, ebx, ecx, edx, eax, 2, 23, 0xc4ac5665
md5._.round md5._.h, ecx, edx, eax, ebx, 11, 16, 0x6d9d6122
md5._.round md5._.h, ebx, ecx, edx, eax, 14, 23, 0xfde5380c md5._.round md5._.i, eax, ebx, ecx, edx, 0, 6, 0xf4292244
md5._.round md5._.h, eax, ebx, ecx, edx, 1, 4, 0xa4beea44 md5._.round md5._.i, edx, eax, ebx, ecx, 7, 10, 0x432aff97
md5._.round md5._.h, edx, eax, ebx, ecx, 4, 11, 0x4bdecfa9 md5._.round md5._.i, ecx, edx, eax, ebx, 14, 15, 0xab9423a7
md5._.round md5._.h, ecx, edx, eax, ebx, 7, 16, 0xf6bb4b60 md5._.round md5._.i, ebx, ecx, edx, eax, 5, 21, 0xfc93a039
md5._.round md5._.h, ebx, ecx, edx, eax, 10, 23, 0xbebfbc70 md5._.round md5._.i, eax, ebx, ecx, edx, 12, 6, 0x655b59c3
md5._.round md5._.h, eax, ebx, ecx, edx, 13, 4, 0x289b7ec6 md5._.round md5._.i, edx, eax, ebx, ecx, 3, 10, 0x8f0ccc92
md5._.round md5._.h, edx, eax, ebx, ecx, 0, 11, 0xeaa127fa md5._.round md5._.i, ecx, edx, eax, ebx, 10, 15, 0xffeff47d
md5._.round md5._.h, ecx, edx, eax, ebx, 3, 16, 0xd4ef3085 md5._.round md5._.i, ebx, ecx, edx, eax, 1, 21, 0x85845dd1
md5._.round md5._.h, ebx, ecx, edx, eax, 6, 23, 0x04881d05 md5._.round md5._.i, eax, ebx, ecx, edx, 8, 6, 0x6fa87e4f
md5._.round md5._.h, eax, ebx, ecx, edx, 9, 4, 0xd9d4d039 md5._.round md5._.i, edx, eax, ebx, ecx, 15, 10, 0xfe2ce6e0
md5._.round md5._.h, edx, eax, ebx, ecx, 12, 11, 0xe6db99e5 md5._.round md5._.i, ecx, edx, eax, ebx, 6, 15, 0xa3014314
md5._.round md5._.h, ecx, edx, eax, ebx, 15, 16, 0x1fa27cf8 md5._.round md5._.i, ebx, ecx, edx, eax, 13, 21, 0x4e0811a1
md5._.round md5._.h, ebx, ecx, edx, eax, 2, 23, 0xc4ac5665 md5._.round md5._.i, eax, ebx, ecx, edx, 4, 6, 0xf7537e82
md5._.round md5._.i, edx, eax, ebx, ecx, 11, 10, 0xbd3af235
md5._.round md5._.i, eax, ebx, ecx, edx, 0, 6, 0xf4292244 md5._.round md5._.i, ecx, edx, eax, ebx, 2, 15, 0x2ad7d2bb
md5._.round md5._.i, edx, eax, ebx, ecx, 7, 10, 0x432aff97 md5._.round md5._.i, ebx, ecx, edx, eax, 9, 21, 0xeb86d391
md5._.round md5._.i, ecx, edx, eax, ebx, 14, 15, 0xab9423a7
md5._.round md5._.i, ebx, ecx, edx, eax, 5, 21, 0xfc93a039 mov edi, [_hash]
md5._.round md5._.i, eax, ebx, ecx, edx, 12, 6, 0x655b59c3 add [edi + 0x0], eax
md5._.round md5._.i, edx, eax, ebx, ecx, 3, 10, 0x8f0ccc92 add [edi + 0x4], ebx
md5._.round md5._.i, ecx, edx, eax, ebx, 10, 15, 0xffeff47d add [edi + 0x8], ecx
md5._.round md5._.i, ebx, ecx, edx, eax, 1, 21, 0x85845dd1 add [edi + 0xc], edx
md5._.round md5._.i, eax, ebx, ecx, edx, 8, 6, 0x6fa87e4f
md5._.round md5._.i, edx, eax, ebx, ecx, 15, 10, 0xfe2ce6e0 ret
md5._.round md5._.i, ecx, edx, eax, ebx, 6, 15, 0xa3014314 endp
md5._.round md5._.i, ebx, ecx, edx, eax, 13, 21, 0x4e0811a1
md5._.round md5._.i, eax, ebx, ecx, edx, 4, 6, 0xf7537e82
md5._.round md5._.i, edx, eax, ebx, ecx, 11, 10, 0xbd3af235 proc md5.update _ctx, _msg, _size
md5._.round md5._.i, ecx, edx, eax, ebx, 2, 15, 0x2ad7d2bb mov ebx, [_ctx]
md5._.round md5._.i, ebx, ecx, edx, eax, 9, 21, 0xeb86d391 mov ecx, [_size]
add [ebx + ctx_md5.msglen_0], ecx
mov edi, [_hash] adc [ebx + ctx_md5.msglen_1], 0
add [edi + 0x0], eax
add [edi + 0x4], ebx .next_block:
add [edi + 0x8], ecx mov ebx, [_ctx]
add [edi + 0xc], edx mov esi, [_msg]
mov eax, [ebx + ctx_md5.index]
ret and eax, MD5_BLOCK_SIZE-1
endp jnz .copy_to_buf
test esi, MD5_ALIGN_MASK
jnz .copy_to_buf
proc md5.update _ctx, _msg, _size .no_copy:
mov ebx, [_ctx] ; data is aligned, hash it in place without copying
mov ecx, [_size] mov ebx, [_ctx]
add [ebx + ctx_md5.msglen_0], ecx cmp [_size], MD5_BLOCK_SIZE
adc [ebx + ctx_md5.msglen_1], 0 jb .copy_quit
lea eax, [ebx + ctx_md5.hash]
.next_block: stdcall md5._.block, eax
mov ebx, [_ctx] sub [_size], MD5_BLOCK_SIZE
mov esi, [_msg] add esi, MD5_BLOCK_SIZE
mov eax, [ebx + ctx_md5.index] jmp .no_copy
and eax, MD5_BLOCK_SIZE-1
jnz .copy_to_buf .copy_to_buf:
test esi, MD5_ALIGN_MASK lea edi, [ebx + ctx_md5.block]
jnz .copy_to_buf add edi, eax
.no_copy: mov ecx, MD5_BLOCK_SIZE
; data is aligned, hash it in place without copying sub ecx, eax
mov ebx, [_ctx] cmp [_size], ecx
cmp [_size], MD5_BLOCK_SIZE jb .copy_quit
jb .copy_quit sub [_size], ecx
lea eax, [ebx + ctx_md5.hash] add [_msg], ecx
stdcall md5._.block, eax add [ebx + ctx_md5.index], ecx
sub [_size], MD5_BLOCK_SIZE rep movsb
add esi, MD5_BLOCK_SIZE lea eax, [ebx + ctx_md5.hash]
jmp .no_copy lea esi, [ebx + ctx_md5.block]
stdcall md5._.block, eax
.copy_to_buf: jmp .next_block
lea edi, [ebx + ctx_md5.block]
add edi, eax .copy_quit:
mov ecx, MD5_BLOCK_SIZE mov ebx, [_ctx]
sub ecx, eax lea edi, [ebx + ctx_md5.block]
cmp [_size], ecx mov eax, [ebx + ctx_md5.index]
jb .copy_quit and eax, MD5_BLOCK_SIZE-1
sub [_size], ecx add edi, eax
add [_msg], ecx mov ecx, [_size]
add [ebx + ctx_md5.index], ecx add [ebx + ctx_md5.index], ecx
rep movsb rep movsb
lea eax, [ebx + ctx_md5.hash] .quit:
lea esi, [ebx + ctx_md5.block]
stdcall md5._.block, eax ret
jmp .next_block endp
.copy_quit:
mov ebx, [_ctx] proc md5.final _ctx
lea edi, [ebx + ctx_md5.block] mov ebx, [_ctx]
mov eax, [ebx + ctx_md5.index] lea edi, [ebx + ctx_md5.block]
and eax, MD5_BLOCK_SIZE-1 mov ecx, [ebx + ctx_md5.msglen_0]
add edi, eax and ecx, MD5_BLOCK_SIZE-1
mov ecx, [_size] add edi, ecx
add [ebx + ctx_md5.index], ecx mov byte[edi], 0x80
rep movsb inc edi
.quit: neg ecx
add ecx, MD5_BLOCK_SIZE
ret cmp ecx, 8
endp ja .last
dec ecx
proc md5.final _ctx xor eax, eax
mov ebx, [_ctx] rep stosb
lea edi, [ebx + ctx_md5.block] lea esi, [ebx + ctx_md5.block]
mov ecx, [ebx + ctx_md5.msglen_0] lea eax, [ebx + ctx_md5.hash]
and ecx, MD5_BLOCK_SIZE-1 stdcall md5._.block, eax
add edi, ecx mov ebx, [_ctx]
mov byte[edi], 0x80 lea edi, [ebx + ctx_md5.block]
inc edi mov ecx, MD5_BLOCK_SIZE+1
neg ecx .last:
add ecx, MD5_BLOCK_SIZE dec ecx
cmp ecx, 8 sub ecx, 8
ja .last xor eax, eax
rep stosb
dec ecx mov eax, [ebx + ctx_md5.msglen_0]
xor eax, eax mov edx, [ebx + ctx_md5.msglen_1]
rep stosb shld edx, eax, 3
lea esi, [ebx + ctx_md5.block] shl eax, 3
lea eax, [ebx + ctx_md5.hash] mov dword[edi], eax
stdcall md5._.block, eax mov dword[edi+4], edx
mov ebx, [_ctx] lea esi, [ebx + ctx_md5.block]
lea edi, [ebx + ctx_md5.block] lea eax, [ebx + ctx_md5.hash]
mov ecx, MD5_BLOCK_SIZE+1 stdcall md5._.block, eax
.last:
dec ecx ret
sub ecx, 8 endp
xor eax, eax
rep stosb
mov eax, [ebx + ctx_md5.msglen_0] align MD5_ALIGN
mov edx, [ebx + ctx_md5.msglen_1]
shld edx, eax, 3 md5._.hash_init dd 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0
shl eax, 3
mov dword[edi], eax
mov dword[edi+4], edx
lea esi, [ebx + ctx_md5.block]
lea eax, [ebx + ctx_md5.hash]
stdcall md5._.block, eax
ret
endp
align MD5_ALIGN
md5._.hash_init dd 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0

View File

@ -1,287 +1,273 @@
; libcrash -- cryptographic hash functions ; libcrash -- cryptographic hash functions
; ;
; Copyright (C) 2012-2013,2016 Ivan Baravy (dunkaist) ; Copyright (C) 2012-2013,2016 Ivan Baravy (dunkaist)
; ;
; This program is free software: you can redistribute it and/or modify ; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by ; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or ; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version. ; (at your option) any later version.
; ;
; This program is distributed in the hope that it will be useful, ; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of ; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details. ; GNU General Public License for more details.
; ;
; You should have received a copy of the GNU General Public License ; You should have received a copy of the GNU General Public License
; along with this program. If not, see <http://www.gnu.org/licenses/>. ; along with this program. If not, see <http://www.gnu.org/licenses/>.
SHA1_BLOCK_SIZE = 64 proc sha1._.f
SHA1_HASH_SIZE = 20 push ebx ecx edx
SHA1_ALIGN = 4 xor ecx, edx
SHA1_ALIGN_MASK = SHA1_ALIGN - 1 and ebx, ecx
xor ebx, edx
struct ctx_sha1 mov esi, ebx
hash rb SHA1_HASH_SIZE pop edx ecx ebx
block rb SHA1_BLOCK_SIZE ret
index rd 1 endp
msglen_0 rd 1
msglen_1 rd 1 proc sha1._.g
ends push ebx ecx edx
xor ebx, ecx
xor ebx, edx
proc sha1._.f mov esi, ebx
push ebx ecx edx pop edx ecx ebx
xor ecx, edx ret
and ebx, ecx endp
xor ebx, edx
mov esi, ebx proc sha1._.h
pop edx ecx ebx push ebx ecx edx
ret mov esi, ebx
endp and ebx, ecx
and ecx, edx
proc sha1._.g and esi, edx
push ebx ecx edx or ebx, ecx
xor ebx, ecx or esi, ebx
xor ebx, edx pop edx ecx ebx
mov esi, ebx ret
pop edx ecx ebx endp
ret
endp macro sha1._.round f, k, c
{
proc sha1._.h mov esi, eax
push ebx ecx edx rol esi, 5
mov esi, ebx mov [temp], esi
and ebx, ecx call f
and ecx, edx
and esi, edx add esi, edi
or ebx, ecx add [temp], esi
or esi, ebx mov esi, [w + (c)*4]
pop edx ecx ebx add esi, k
ret add [temp], esi
endp
mov edi, edx
macro sha1._.round f, k, c mov edx, ecx
{ mov ecx, ebx
mov esi, eax rol ecx, 30
rol esi, 5 mov ebx, eax
mov [temp], esi mov eax, [temp]
call f }
add esi, edi
add [temp], esi proc sha1.init _ctx
mov esi, [w + (c)*4] mov ebx, [_ctx]
add esi, k lea edi, [ebx + ctx_sha1.hash]
add [temp], esi mov esi, sha1._.hash_init
mov ecx, SHA1_HASH_SIZE/4
mov edi, edx rep movsd
mov edx, ecx xor eax, eax
mov ecx, ebx mov [ebx + ctx_sha1.index], eax
rol ecx, 30 mov [ebx + ctx_sha1.msglen_0], eax
mov ebx, eax mov [ebx + ctx_sha1.msglen_1], eax
mov eax, [temp] ret
} endp
proc sha1.init _ctx proc sha1._.block _hash
mov ebx, [_ctx] locals
lea edi, [ebx + ctx_sha1.hash] temp rd 1
mov esi, sha1._.hash_init w rd 80
mov ecx, SHA1_HASH_SIZE/4 endl
rep movsd lea edi, [w]
xor eax, eax xor ecx, ecx
mov [ebx + ctx_sha1.index], eax @@:
mov [ebx + ctx_sha1.msglen_0], eax mov eax, [esi]
mov [ebx + ctx_sha1.msglen_1], eax add esi, 4
ret bswap eax
endp mov [edi], eax
add edi, 4
add ecx, 1
proc sha1._.block _hash cmp ecx, 16
locals jne @b
temp rd 1 @@:
w rd 80 mov eax, [w + (ecx - 3)*4]
endl xor eax, [w + (ecx - 8)*4]
lea edi, [w] xor eax, [w + (ecx - 14)*4]
xor ecx, ecx xor eax, [w + (ecx - 16)*4]
@@: rol eax, 1
mov eax, [esi] mov [w + ecx*4], eax
add esi, 4 add ecx, 1
bswap eax cmp ecx, 80
mov [edi], eax jne @b
add edi, 4
add ecx, 1 mov edi, [_hash]
cmp ecx, 16 mov eax, [edi + 0x00]
jne @b mov ebx, [edi + 0x04]
@@: mov ecx, [edi + 0x08]
mov eax, [w + (ecx - 3)*4] mov edx, [edi + 0x0c]
xor eax, [w + (ecx - 8)*4] mov edi, [edi + 0x10]
xor eax, [w + (ecx - 14)*4]
xor eax, [w + (ecx - 16)*4] push esi
rol eax, 1
mov [w + ecx*4], eax repeat 20
add ecx, 1 sha1._.round sha1._.f, 0x5a827999, %-1
cmp ecx, 80 end repeat
jne @b
repeat 20
mov edi, [_hash] sha1._.round sha1._.g, 0x6ed9eba1, %-1+20
mov eax, [edi + 0x00] end repeat
mov ebx, [edi + 0x04]
mov ecx, [edi + 0x08] repeat 20
mov edx, [edi + 0x0c] sha1._.round sha1._.h, 0x8f1bbcdc, %-1+40
mov edi, [edi + 0x10] end repeat
push esi repeat 20
sha1._.round sha1._.g, 0xca62c1d6, %-1+60
repeat 20 end repeat
sha1._.round sha1._.f, 0x5a827999, %-1
end repeat pop esi
repeat 20 mov [temp], edi
sha1._.round sha1._.g, 0x6ed9eba1, %-1+20 mov edi, [_hash]
end repeat add [edi + 0x00], eax
add [edi + 0x04], ebx
repeat 20 add [edi + 0x08], ecx
sha1._.round sha1._.h, 0x8f1bbcdc, %-1+40 add [edi + 0x0c], edx
end repeat mov eax, [temp]
add [edi + 0x10], eax
repeat 20
sha1._.round sha1._.g, 0xca62c1d6, %-1+60 ret
end repeat endp
pop esi
proc sha1.update _ctx, _msg, _size
mov [temp], edi mov ebx, [_ctx]
mov edi, [_hash] mov ecx, [_size]
add [edi + 0x00], eax add [ebx + ctx_sha1.msglen_0], ecx
add [edi + 0x04], ebx adc [ebx + ctx_sha1.msglen_1], 0
add [edi + 0x08], ecx
add [edi + 0x0c], edx .next_block:
mov eax, [temp] mov ebx, [_ctx]
add [edi + 0x10], eax mov esi, [_msg]
mov eax, [ebx + ctx_sha1.index]
ret and eax, SHA1_BLOCK_SIZE-1
endp jnz .copy_to_buf
test esi, SHA1_ALIGN_MASK
jnz .copy_to_buf
proc sha1.update _ctx, _msg, _size .no_copy:
mov ebx, [_ctx] ; data is aligned, hash it in place without copying
mov ecx, [_size] mov ebx, [_ctx]
add [ebx + ctx_sha1.msglen_0], ecx cmp [_size], SHA1_BLOCK_SIZE
adc [ebx + ctx_sha1.msglen_1], 0 jb .copy_quit
lea eax, [ebx + ctx_sha1.hash]
.next_block: stdcall sha1._.block, eax
mov ebx, [_ctx] sub [_size], SHA1_BLOCK_SIZE
mov esi, [_msg] ; add esi, SHA1_BLOCK_SIZE ; FIXME
mov eax, [ebx + ctx_sha1.index] jmp .no_copy
and eax, SHA1_BLOCK_SIZE-1
jnz .copy_to_buf .copy_to_buf:
test esi, SHA1_ALIGN_MASK lea edi, [ebx + ctx_sha1.block]
jnz .copy_to_buf add edi, eax
.no_copy: mov ecx, SHA1_BLOCK_SIZE
; data is aligned, hash it in place without copying sub ecx, eax
mov ebx, [_ctx] cmp [_size], ecx
cmp [_size], SHA1_BLOCK_SIZE jb .copy_quit
jb .copy_quit sub [_size], ecx
lea eax, [ebx + ctx_sha1.hash] add [_msg], ecx
stdcall sha1._.block, eax add [ebx + ctx_sha1.index], ecx
sub [_size], SHA1_BLOCK_SIZE rep movsb
; add esi, SHA1_BLOCK_SIZE ; FIXME lea eax, [ebx + ctx_sha1.hash]
jmp .no_copy lea esi, [ebx + ctx_sha1.block]
stdcall sha1._.block, eax
.copy_to_buf: jmp .next_block
lea edi, [ebx + ctx_sha1.block]
add edi, eax .copy_quit:
mov ecx, SHA1_BLOCK_SIZE mov ebx, [_ctx]
sub ecx, eax lea edi, [ebx + ctx_sha1.block]
cmp [_size], ecx mov eax, [ebx + ctx_sha1.index]
jb .copy_quit and eax, SHA1_BLOCK_SIZE-1
sub [_size], ecx add edi, eax
add [_msg], ecx mov ecx, [_size]
add [ebx + ctx_sha1.index], ecx add [ebx + ctx_sha1.index], ecx
rep movsb rep movsb
lea eax, [ebx + ctx_sha1.hash] .quit:
lea esi, [ebx + ctx_sha1.block]
stdcall sha1._.block, eax ret
jmp .next_block endp
.copy_quit:
mov ebx, [_ctx] proc sha1.final _ctx
lea edi, [ebx + ctx_sha1.block] mov ebx, [_ctx]
mov eax, [ebx + ctx_sha1.index] lea edi, [ebx + ctx_sha1.block]
and eax, SHA1_BLOCK_SIZE-1 mov ecx, [ebx + ctx_sha1.msglen_0]
add edi, eax and ecx, SHA1_BLOCK_SIZE-1
mov ecx, [_size] add edi, ecx
add [ebx + ctx_sha1.index], ecx mov byte[edi], 0x80
rep movsb inc edi
.quit: neg ecx
add ecx, SHA1_BLOCK_SIZE
ret cmp ecx, 8
endp ja .last
dec ecx
proc sha1.final _ctx xor eax, eax
mov ebx, [_ctx] rep stosb
lea edi, [ebx + ctx_sha1.block] lea esi, [ebx + ctx_sha1.block]
mov ecx, [ebx + ctx_sha1.msglen_0] lea eax, [ebx + ctx_sha1.hash]
and ecx, SHA1_BLOCK_SIZE-1 stdcall sha1._.block, eax
add edi, ecx mov ebx, [_ctx]
mov byte[edi], 0x80 lea edi, [ebx + ctx_sha1.block]
inc edi mov ecx, SHA1_BLOCK_SIZE+1
neg ecx .last:
add ecx, SHA1_BLOCK_SIZE dec ecx
cmp ecx, 8 sub ecx, 8
ja .last xor eax, eax
rep stosb
dec ecx mov eax, [ebx + ctx_sha1.msglen_0]
xor eax, eax mov edx, [ebx + ctx_sha1.msglen_1]
rep stosb shld edx, eax, 3
lea esi, [ebx + ctx_sha1.block] shl eax, 3
lea eax, [ebx + ctx_sha1.hash] bswap eax
stdcall sha1._.block, eax bswap edx
mov ebx, [_ctx] mov dword[edi], edx
lea edi, [ebx + ctx_sha1.block] mov dword[edi+4], eax
mov ecx, SHA1_BLOCK_SIZE+1 lea esi, [ebx + ctx_sha1.block]
.last: lea eax, [ebx + ctx_sha1.hash]
dec ecx stdcall sha1._.block, eax
sub ecx, 8
xor eax, eax mov ebx, [_ctx]
rep stosb lea eax, [ebx + ctx_sha1.hash]
mov eax, [ebx + ctx_sha1.msglen_0] stdcall sha1._.postprocess, ebx, eax
mov edx, [ebx + ctx_sha1.msglen_1]
shld edx, eax, 3 ret
shl eax, 3 endp
bswap eax
bswap edx
mov dword[edi], edx proc sha1._.postprocess _ctx, _hash
mov dword[edi+4], eax mov ecx, 5
lea esi, [ebx + ctx_sha1.block] mov esi, [_hash]
lea eax, [ebx + ctx_sha1.hash] mov edi, esi
stdcall sha1._.block, eax @@:
lodsd
mov ebx, [_ctx] bswap eax
lea eax, [ebx + ctx_sha1.hash] stosd
stdcall sha1._.postprocess, ebx, eax dec ecx
jnz @b
ret ret
endp endp
proc sha1._.postprocess _ctx, _hash align SHA1_ALIGN
mov ecx, 5
mov esi, [_hash] sha1._.hash_init dd 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0
mov edi, esi
@@:
lodsd
bswap eax
stosd
dec ecx
jnz @b
ret
endp
align SHA1_ALIGN
sha1._.hash_init dd 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0

View File

@ -1,414 +1,399 @@
; libcrash -- cryptographic hash functions ; libcrash -- cryptographic hash functions
; ;
; Copyright (C) 2012-2013,2016 Ivan Baravy (dunkaist) ; Copyright (C) 2012-2013,2016 Ivan Baravy (dunkaist)
; ;
; This program is free software: you can redistribute it and/or modify ; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by ; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or ; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version. ; (at your option) any later version.
; ;
; This program is distributed in the hope that it will be useful, ; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of ; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details. ; GNU General Public License for more details.
; ;
; You should have received a copy of the GNU General Public License ; You should have received a copy of the GNU General Public License
; along with this program. If not, see <http://www.gnu.org/licenses/>. ; along with this program. If not, see <http://www.gnu.org/licenses/>.
SHA224256_BLOCK_SIZE = 64 macro sha224256._.chn x, y, z
SHA224256_INIT_SIZE = 32 {
SHA224_HASH_SIZE = 28 mov eax, [y]
SHA256_HASH_SIZE = 32 xor eax, [z]
SHA224256_ALIGN = 4 and eax, [x]
SHA224256_ALIGN_MASK = SHA224256_ALIGN - 1 xor eax, [z]
}
struct ctx_sha224256
hash rb SHA224256_INIT_SIZE macro sha224256._.maj x, y, z
block rb SHA224256_BLOCK_SIZE {
index rd 1 mov eax, [x]
msglen_0 rd 1 xor eax, [y]
msglen_1 rd 1 and eax, [z]
ends mov ecx, [x]
and ecx, [y]
xor eax, ecx
macro sha224256._.chn x, y, z }
{
mov eax, [y] macro sha224256._.Sigma0 x
xor eax, [z] {
and eax, [x] mov eax, x
xor eax, [z] mov ecx, eax
} ror ecx, 2
ror eax, 13
macro sha224256._.maj x, y, z xor eax, ecx
{ mov ecx, x
mov eax, [x] ror ecx, 22
xor eax, [y] xor eax, ecx
and eax, [z] }
mov ecx, [x]
and ecx, [y] macro sha224256._.Sigma1 x
xor eax, ecx {
} mov eax, x
mov ecx, eax
macro sha224256._.Sigma0 x ror ecx, 6
{ ror eax, 11
mov eax, x xor eax, ecx
mov ecx, eax mov ecx, x
ror ecx, 2 ror ecx, 25
ror eax, 13 xor eax, ecx
xor eax, ecx }
mov ecx, x
ror ecx, 22 macro sha224256._.sigma0 x
xor eax, ecx {
} mov eax, x
mov ecx, eax
macro sha224256._.Sigma1 x ror ecx, 7
{ ror eax, 18
mov eax, x xor eax, ecx
mov ecx, eax mov ecx, x
ror ecx, 6 shr ecx, 3
ror eax, 11 xor eax, ecx
xor eax, ecx }
mov ecx, x
ror ecx, 25 macro sha224256._.sigma1 x
xor eax, ecx {
} mov eax, x
mov ecx, eax
macro sha224256._.sigma0 x ror ecx, 17
{ ror eax, 19
mov eax, x xor eax, ecx
mov ecx, eax mov ecx, x
ror ecx, 7 shr ecx, 10
ror eax, 18 xor eax, ecx
xor eax, ecx }
mov ecx, x
shr ecx, 3 macro sha224256._.recalculate_w n
xor eax, ecx {
} mov edx, [w + ((n-2) and 15)*4]
sha224256._.sigma1 edx
macro sha224256._.sigma1 x add eax, [w + ((n-7) and 15)*4]
{ push eax
mov eax, x mov edx, [w + ((n-15) and 15)*4]
mov ecx, eax sha224256._.sigma0 edx
ror ecx, 17 pop ecx
ror eax, 19 add eax, ecx
xor eax, ecx add [w + (n)*4], eax
mov ecx, x }
shr ecx, 10
xor eax, ecx macro sha224256._.round a, b, c, d, e, f, g, h, k
} {
mov ebx, [h]
macro sha224256._.recalculate_w n mov edx, [e]
{ sha224256._.Sigma1 edx
mov edx, [w + ((n-2) and 15)*4]
sha224256._.sigma1 edx add ebx, eax
add eax, [w + ((n-7) and 15)*4] sha224256._.chn e, f, g
push eax
mov edx, [w + ((n-15) and 15)*4] add ebx, eax
sha224256._.sigma0 edx add ebx, [k]
pop ecx add ebx, edi
add eax, ecx
add [w + (n)*4], eax add [d], ebx
}
mov edx, [a]
macro sha224256._.round a, b, c, d, e, f, g, h, k sha224256._.Sigma0 edx
{ add ebx, eax
mov ebx, [h] sha224256._.maj a, b, c
mov edx, [e] add eax, ebx
sha224256._.Sigma1 edx mov [h], eax
}
add ebx, eax
sha224256._.chn e, f, g
macro sha224256._.round_1_16 a, b, c, d, e, f, g, h, n
add ebx, eax {
add ebx, [k]
add ebx, edi mov eax, [esi + (n)*4]
bswap eax
add [d], ebx
mov dword[w + (n)*4], eax
mov edx, [a] mov edi, eax
sha224256._.Sigma0 edx sha224256._.round a, b, c, d, e, f, g, h, (sha256_table + (n)*4)
add ebx, eax }
sha224256._.maj a, b, c
add eax, ebx macro sha224256._.round_17_64 a, b, c, d, e, f, g, h, n, rep_num
mov [h], eax {
} sha224256._.recalculate_w n
mov edi, [w + (n)*4]
sha224256._.round a, b, c, d, e, f, g, h, (sha256_table + (n+16*rep_num)*4)
macro sha224256._.round_1_16 a, b, c, d, e, f, g, h, n }
{
mov eax, [esi + (n)*4] proc sha224.init _ctx
bswap eax mov ebx, [_ctx]
lea edi, [ebx + ctx_sha224256.hash]
mov dword[w + (n)*4], eax mov esi, sha224._.hash_init
mov edi, eax mov ecx, SHA224256_INIT_SIZE/4
sha224256._.round a, b, c, d, e, f, g, h, (sha256_table + (n)*4) rep movsd
} xor eax, eax
mov [ebx + ctx_sha224256.index], eax
macro sha224256._.round_17_64 a, b, c, d, e, f, g, h, n, rep_num mov [ebx + ctx_sha224256.msglen_0], eax
{ mov [ebx + ctx_sha224256.msglen_1], eax
sha224256._.recalculate_w n ret
mov edi, [w + (n)*4] endp
sha224256._.round a, b, c, d, e, f, g, h, (sha256_table + (n+16*rep_num)*4)
}
proc sha256.init _ctx
mov ebx, [_ctx]
proc sha224.init _ctx lea edi, [ebx + ctx_sha224256.hash]
mov ebx, [_ctx] mov esi, sha256._.hash_init
lea edi, [ebx + ctx_sha224256.hash] mov ecx, SHA224256_INIT_SIZE/4
mov esi, sha224._.hash_init rep movsd
mov ecx, SHA224256_INIT_SIZE/4 xor eax, eax
rep movsd mov [ebx + ctx_sha224256.index], eax
xor eax, eax mov [ebx + ctx_sha224256.msglen_0], eax
mov [ebx + ctx_sha224256.index], eax mov [ebx + ctx_sha224256.msglen_1], eax
mov [ebx + ctx_sha224256.msglen_0], eax ret
mov [ebx + ctx_sha224256.msglen_1], eax endp
ret
endp
proc sha224256._.block _hash
locals
proc sha256.init _ctx w rd 64
mov ebx, [_ctx] A rd 1
lea edi, [ebx + ctx_sha224256.hash] B rd 1
mov esi, sha256._.hash_init C rd 1
mov ecx, SHA224256_INIT_SIZE/4 D rd 1
rep movsd E rd 1
xor eax, eax F rd 1
mov [ebx + ctx_sha224256.index], eax G rd 1
mov [ebx + ctx_sha224256.msglen_0], eax H rd 1
mov [ebx + ctx_sha224256.msglen_1], eax endl
ret mov edi, [_hash]
endp mov eax, [edi + 0x00]
mov [A], eax
mov eax, [edi + 0x04]
proc sha224256._.block _hash mov [B], eax
locals mov eax, [edi + 0x08]
w rd 64 mov [C], eax
A rd 1 mov eax, [edi + 0x0c]
B rd 1 mov [D], eax
C rd 1 mov eax, [edi + 0x10]
D rd 1 mov [E], eax
E rd 1 mov eax, [edi + 0x14]
F rd 1 mov [F], eax
G rd 1 mov eax, [edi + 0x18]
H rd 1 mov [G], eax
endl mov eax, [edi + 0x1c]
mov edi, [_hash] mov [H], eax
mov eax, [edi + 0x00]
mov [A], eax sha224256._.round_1_16 A, B, C, D, E, F, G, H, 0
mov eax, [edi + 0x04] sha224256._.round_1_16 H, A, B, C, D, E, F, G, 1
mov [B], eax sha224256._.round_1_16 G, H, A, B, C, D, E, F, 2
mov eax, [edi + 0x08] sha224256._.round_1_16 F, G, H, A, B, C, D, E, 3
mov [C], eax sha224256._.round_1_16 E, F, G, H, A, B, C, D, 4
mov eax, [edi + 0x0c] sha224256._.round_1_16 D, E, F, G, H, A, B, C, 5
mov [D], eax sha224256._.round_1_16 C, D, E, F, G, H, A, B, 6
mov eax, [edi + 0x10] sha224256._.round_1_16 B, C, D, E, F, G, H, A, 7
mov [E], eax sha224256._.round_1_16 A, B, C, D, E, F, G, H, 8
mov eax, [edi + 0x14] sha224256._.round_1_16 H, A, B, C, D, E, F, G, 9
mov [F], eax sha224256._.round_1_16 G, H, A, B, C, D, E, F, 10
mov eax, [edi + 0x18] sha224256._.round_1_16 F, G, H, A, B, C, D, E, 11
mov [G], eax sha224256._.round_1_16 E, F, G, H, A, B, C, D, 12
mov eax, [edi + 0x1c] sha224256._.round_1_16 D, E, F, G, H, A, B, C, 13
mov [H], eax sha224256._.round_1_16 C, D, E, F, G, H, A, B, 14
sha224256._.round_1_16 B, C, D, E, F, G, H, A, 15
sha224256._.round_1_16 A, B, C, D, E, F, G, H, 0
sha224256._.round_1_16 H, A, B, C, D, E, F, G, 1 repeat 3
sha224256._.round_1_16 G, H, A, B, C, D, E, F, 2 sha224256._.round_17_64 A, B, C, D, E, F, G, H, 0, %
sha224256._.round_1_16 F, G, H, A, B, C, D, E, 3 sha224256._.round_17_64 H, A, B, C, D, E, F, G, 1, %
sha224256._.round_1_16 E, F, G, H, A, B, C, D, 4 sha224256._.round_17_64 G, H, A, B, C, D, E, F, 2, %
sha224256._.round_1_16 D, E, F, G, H, A, B, C, 5 sha224256._.round_17_64 F, G, H, A, B, C, D, E, 3, %
sha224256._.round_1_16 C, D, E, F, G, H, A, B, 6 sha224256._.round_17_64 E, F, G, H, A, B, C, D, 4, %
sha224256._.round_1_16 B, C, D, E, F, G, H, A, 7 sha224256._.round_17_64 D, E, F, G, H, A, B, C, 5, %
sha224256._.round_1_16 A, B, C, D, E, F, G, H, 8 sha224256._.round_17_64 C, D, E, F, G, H, A, B, 6, %
sha224256._.round_1_16 H, A, B, C, D, E, F, G, 9 sha224256._.round_17_64 B, C, D, E, F, G, H, A, 7, %
sha224256._.round_1_16 G, H, A, B, C, D, E, F, 10 sha224256._.round_17_64 A, B, C, D, E, F, G, H, 8, %
sha224256._.round_1_16 F, G, H, A, B, C, D, E, 11 sha224256._.round_17_64 H, A, B, C, D, E, F, G, 9, %
sha224256._.round_1_16 E, F, G, H, A, B, C, D, 12 sha224256._.round_17_64 G, H, A, B, C, D, E, F, 10, %
sha224256._.round_1_16 D, E, F, G, H, A, B, C, 13 sha224256._.round_17_64 F, G, H, A, B, C, D, E, 11, %
sha224256._.round_1_16 C, D, E, F, G, H, A, B, 14 sha224256._.round_17_64 E, F, G, H, A, B, C, D, 12, %
sha224256._.round_1_16 B, C, D, E, F, G, H, A, 15 sha224256._.round_17_64 D, E, F, G, H, A, B, C, 13, %
sha224256._.round_17_64 C, D, E, F, G, H, A, B, 14, %
repeat 3 sha224256._.round_17_64 B, C, D, E, F, G, H, A, 15, %
sha224256._.round_17_64 A, B, C, D, E, F, G, H, 0, % end repeat
sha224256._.round_17_64 H, A, B, C, D, E, F, G, 1, %
sha224256._.round_17_64 G, H, A, B, C, D, E, F, 2, % mov edi, [_hash]
sha224256._.round_17_64 F, G, H, A, B, C, D, E, 3, % mov eax, [A]
sha224256._.round_17_64 E, F, G, H, A, B, C, D, 4, % add [edi + 0x00], eax
sha224256._.round_17_64 D, E, F, G, H, A, B, C, 5, % mov eax, [B]
sha224256._.round_17_64 C, D, E, F, G, H, A, B, 6, % add [edi + 0x04], eax
sha224256._.round_17_64 B, C, D, E, F, G, H, A, 7, % mov eax, [C]
sha224256._.round_17_64 A, B, C, D, E, F, G, H, 8, % add [edi + 0x08], eax
sha224256._.round_17_64 H, A, B, C, D, E, F, G, 9, % mov eax, [D]
sha224256._.round_17_64 G, H, A, B, C, D, E, F, 10, % add [edi + 0x0c], eax
sha224256._.round_17_64 F, G, H, A, B, C, D, E, 11, % mov eax, [E]
sha224256._.round_17_64 E, F, G, H, A, B, C, D, 12, % add [edi + 0x10], eax
sha224256._.round_17_64 D, E, F, G, H, A, B, C, 13, % mov eax, [F]
sha224256._.round_17_64 C, D, E, F, G, H, A, B, 14, % add [edi + 0x14], eax
sha224256._.round_17_64 B, C, D, E, F, G, H, A, 15, % mov eax, [G]
end repeat add [edi + 0x18], eax
mov eax, [H]
mov edi, [_hash] add [edi + 0x1c], eax
mov eax, [A]
add [edi + 0x00], eax ret
mov eax, [B] endp
add [edi + 0x04], eax
mov eax, [C] sha256.update = sha224.update
add [edi + 0x08], eax proc sha224.update _ctx, _msg, _size
mov eax, [D] mov ebx, [_ctx]
add [edi + 0x0c], eax mov ecx, [_size]
mov eax, [E] add [ebx + ctx_sha224256.msglen_0], ecx
add [edi + 0x10], eax adc [ebx + ctx_sha224256.msglen_1], 0
mov eax, [F]
add [edi + 0x14], eax .next_block:
mov eax, [G] mov ebx, [_ctx]
add [edi + 0x18], eax mov esi, [_msg]
mov eax, [H] mov eax, [ebx + ctx_sha224256.index]
add [edi + 0x1c], eax and eax, SHA224256_BLOCK_SIZE-1
jnz .copy_to_buf
ret test esi, SHA224256_ALIGN_MASK
endp jnz .copy_to_buf
.no_copy:
; data is aligned, hash it in place without copying
proc sha224256.update _ctx, _msg, _size mov ebx, [_ctx]
mov ebx, [_ctx] cmp [_size], SHA224256_BLOCK_SIZE
mov ecx, [_size] jb .copy_quit
add [ebx + ctx_sha224256.msglen_0], ecx lea eax, [ebx + ctx_sha224256.hash]
adc [ebx + ctx_sha224256.msglen_1], 0 stdcall sha224256._.block, eax
sub [_size], SHA224256_BLOCK_SIZE
.next_block: add esi, SHA224256_BLOCK_SIZE ; FIXME
mov ebx, [_ctx] jmp .no_copy
mov esi, [_msg]
mov eax, [ebx + ctx_sha224256.index] .copy_to_buf:
and eax, SHA224256_BLOCK_SIZE-1 lea edi, [ebx + ctx_sha224256.block]
jnz .copy_to_buf add edi, eax
test esi, SHA224256_ALIGN_MASK mov ecx, SHA224256_BLOCK_SIZE
jnz .copy_to_buf sub ecx, eax
.no_copy: cmp [_size], ecx
; data is aligned, hash it in place without copying jb .copy_quit
mov ebx, [_ctx] sub [_size], ecx
cmp [_size], SHA224256_BLOCK_SIZE add [_msg], ecx
jb .copy_quit add [ebx + ctx_sha224256.index], ecx
lea eax, [ebx + ctx_sha224256.hash] rep movsb
stdcall sha224256._.block, eax lea eax, [ebx + ctx_sha224256.hash]
sub [_size], SHA224256_BLOCK_SIZE lea esi, [ebx + ctx_sha224256.block]
add esi, SHA224256_BLOCK_SIZE ; FIXME stdcall sha224256._.block, eax
jmp .no_copy jmp .next_block
.copy_to_buf: .copy_quit:
lea edi, [ebx + ctx_sha224256.block] mov ebx, [_ctx]
add edi, eax lea edi, [ebx + ctx_sha224256.block]
mov ecx, SHA224256_BLOCK_SIZE mov eax, [ebx + ctx_sha224256.index]
sub ecx, eax and eax, SHA224256_BLOCK_SIZE-1
cmp [_size], ecx add edi, eax
jb .copy_quit mov ecx, [_size]
sub [_size], ecx add [ebx + ctx_sha224256.index], ecx
add [_msg], ecx rep movsb
add [ebx + ctx_sha224256.index], ecx .quit:
rep movsb
lea eax, [ebx + ctx_sha224256.hash] ret
lea esi, [ebx + ctx_sha224256.block] endp
stdcall sha224256._.block, eax
jmp .next_block
sha256.final = sha224.final
.copy_quit: proc sha224.final _ctx
mov ebx, [_ctx] mov ebx, [_ctx]
lea edi, [ebx + ctx_sha224256.block] lea edi, [ebx + ctx_sha224256.block]
mov eax, [ebx + ctx_sha224256.index] mov ecx, [ebx + ctx_sha224256.msglen_0]
and eax, SHA224256_BLOCK_SIZE-1 and ecx, SHA224256_BLOCK_SIZE-1
add edi, eax add edi, ecx
mov ecx, [_size] mov byte[edi], 0x80
add [ebx + ctx_sha224256.index], ecx inc edi
rep movsb neg ecx
.quit: add ecx, SHA224256_BLOCK_SIZE
cmp ecx, 8
ret ja .last
endp
dec ecx
xor eax, eax
proc sha224256.final _ctx rep stosb
mov ebx, [_ctx] lea esi, [ebx + ctx_sha224256.block]
lea edi, [ebx + ctx_sha224256.block] lea eax, [ebx + ctx_sha224256.hash]
mov ecx, [ebx + ctx_sha224256.msglen_0] stdcall sha224256._.block, eax
and ecx, SHA224256_BLOCK_SIZE-1 mov ebx, [_ctx]
add edi, ecx lea edi, [ebx + ctx_sha224256.block]
mov byte[edi], 0x80 mov ecx, SHA224256_BLOCK_SIZE+1
inc edi .last:
neg ecx dec ecx
add ecx, SHA224256_BLOCK_SIZE sub ecx, 8
cmp ecx, 8 xor eax, eax
ja .last rep stosb
mov eax, [ebx + ctx_sha224256.msglen_0]
dec ecx mov edx, [ebx + ctx_sha224256.msglen_1]
xor eax, eax shld edx, eax, 3
rep stosb shl eax, 3
lea esi, [ebx + ctx_sha224256.block] bswap eax
lea eax, [ebx + ctx_sha224256.hash] bswap edx
stdcall sha224256._.block, eax mov dword[edi], edx
mov ebx, [_ctx] mov dword[edi+4], eax
lea edi, [ebx + ctx_sha224256.block] lea esi, [ebx + ctx_sha224256.block]
mov ecx, SHA224256_BLOCK_SIZE+1 lea eax, [ebx + ctx_sha224256.hash]
.last: stdcall sha224256._.block, eax
dec ecx
sub ecx, 8 mov ebx, [_ctx]
xor eax, eax lea eax, [ebx + ctx_sha224256.hash]
rep stosb stdcall sha224256._.postprocess, ebx, eax
mov eax, [ebx + ctx_sha224256.msglen_0]
mov edx, [ebx + ctx_sha224256.msglen_1] ret
shld edx, eax, 3 endp
shl eax, 3
bswap eax
bswap edx proc sha224256._.postprocess _ctx, _hash
mov dword[edi], edx mov ecx, 8
mov dword[edi+4], eax mov esi, [_hash]
lea esi, [ebx + ctx_sha224256.block] mov edi, esi
lea eax, [ebx + ctx_sha224256.hash] @@:
stdcall sha224256._.block, eax lodsd
bswap eax
mov ebx, [_ctx] stosd
lea eax, [ebx + ctx_sha224256.hash] dec ecx
stdcall sha224256._.postprocess, ebx, eax jnz @b
ret
ret endp
endp
align SHA224256_ALIGN
proc sha224256._.postprocess _ctx, _hash
mov ecx, 8 sha224._.hash_init dd 0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939,\
mov esi, [_hash] 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4
mov edi, esi
@@: sha256._.hash_init dd 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,\
lodsd 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
bswap eax
stosd sha256_table dd 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,\
dec ecx 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,\
jnz @b 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,\
ret 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,\
endp 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,\
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,\
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,\
align SHA224256_ALIGN 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,\
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,\
sha224._.hash_init dd 0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939,\ 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,\
0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,\
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,\
sha256._.hash_init dd 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,\ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,\
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,\
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,\
sha256_table dd 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,\ 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,\
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,\
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,\
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,\
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,\
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,\
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,\
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,\
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,\
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,\
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,\
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,\
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,\
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,\
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2

View File

@ -1,440 +1,417 @@
; libcrash -- cryptographic hash functions ; libcrash -- cryptographic hash functions
; ;
; Copyright (C) 2013,2016 Ivan Baravy (dunkaist) ; Copyright (C) 2013,2016 Ivan Baravy (dunkaist)
; ;
; This program is free software: you can redistribute it and/or modify ; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by ; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or ; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version. ; (at your option) any later version.
; ;
; This program is distributed in the hope that it will be useful, ; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of ; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details. ; GNU General Public License for more details.
; ;
; You should have received a copy of the GNU General Public License ; You should have received a copy of the GNU General Public License
; along with this program. If not, see <http://www.gnu.org/licenses/>. ; along with this program. If not, see <http://www.gnu.org/licenses/>.
SHA3224_BLOCK_SIZE = 144 macro sha3._.rol_xor nd, ncl, ncr
SHA3256_BLOCK_SIZE = 136 {
SHA3384_BLOCK_SIZE = 104 movq mm0, [C + 8*(ncl)]
SHA3512_BLOCK_SIZE = 72 movq mm1, mm0
SHA3MAX_BLOCK_SIZE = SHA3224_BLOCK_SIZE psllq mm0, 1
psrlq mm1, 63
SHA3_INIT_SIZE = 200 por mm0, mm1
pxor mm0, [C + 8*(ncr)]
SHA3224_HASH_SIZE = 28 movq [D + 8*(nd)], mm0
SHA3256_HASH_SIZE = 32 }
SHA3384_HASH_SIZE = 48
SHA3512_HASH_SIZE = 64 proc sha3._.theta
;locals
SHA3_ALIGN = 16 ; C rq 5
SHA3_ALIGN_MASK = SHA3_ALIGN-1 ; D rq 5
;endl
struct ctx_sha3 C equ ebx + ctx_sha3.C
hash rb SHA3_INIT_SIZE D equ ebx + ctx_sha3.D
rb SHA3_ALIGN - (SHA3_INIT_SIZE mod SHA3_ALIGN)
block rb SHA3MAX_BLOCK_SIZE repeat 5
rb SHA3_ALIGN - (SHA3MAX_BLOCK_SIZE mod SHA3_ALIGN) movq mm0, [edi + 8*(%-1 + 0)]
index rd 1 pxor mm0, [edi + 8*(%-1 + 5)]
block_size rd 1 pxor mm0, [edi + 8*(%-1 + 10)]
rounds_cnt rd 1 pxor mm0, [edi + 8*(%-1 + 15)]
rd 1 ; align pxor mm0, [edi + 8*(%-1 + 20)]
; tmp vars movq [C + 8*(%-1)], mm0
C rq 5 end repeat
D rq 5
ends sha3._.rol_xor 0, 1, 4
sha3._.rol_xor 1, 2, 0
sha3._.rol_xor 2, 3, 1
macro sha3._.rol_xor nd, ncl, ncr sha3._.rol_xor 3, 4, 2
{ sha3._.rol_xor 4, 0, 3
movq mm0, [C + 8*(ncl)]
movq mm1, mm0 repeat 5
psllq mm0, 1 movq mm1, [D + 8*(%-1)]
psrlq mm1, 63 movq mm0, mm1
por mm0, mm1 pxor mm0, [edi + 8*(%-1 + 0)]
pxor mm0, [C + 8*(ncr)] movq [edi + 8*(%-1 + 0)], mm0
movq [D + 8*(nd)], mm0 movq mm0, mm1
} pxor mm0, [edi + 8*(%-1 + 5)]
movq [edi + 8*(%-1 + 5)], mm0
proc sha3._.theta movq mm0, mm1
;locals pxor mm0, [edi + 8*(%-1 + 10)]
; C rq 5 movq [edi + 8*(%-1 + 10)], mm0
; D rq 5 movq mm0, mm1
;endl pxor mm0, [edi + 8*(%-1 + 15)]
C equ ebx + ctx_sha3.C movq [edi + 8*(%-1 + 15)], mm0
D equ ebx + ctx_sha3.D movq mm0, mm1
pxor mm0, [edi + 8*(%-1 + 20)]
repeat 5 movq [edi + 8*(%-1 + 20)], mm0
movq mm0, [edi + 8*(%-1 + 0)] end repeat
pxor mm0, [edi + 8*(%-1 + 5)]
pxor mm0, [edi + 8*(%-1 + 10)] restore C,D
pxor mm0, [edi + 8*(%-1 + 15)] ret
pxor mm0, [edi + 8*(%-1 + 20)] endp
movq [C + 8*(%-1)], mm0
end repeat
proc sha3._.pi
sha3._.rol_xor 0, 1, 4 movq mm1, [edi + 8*1]
sha3._.rol_xor 1, 2, 0 movq mm0, [edi + 8*6]
sha3._.rol_xor 2, 3, 1 movq [edi + 8*1], mm0
sha3._.rol_xor 3, 4, 2 movq mm0, [edi + 8*9]
sha3._.rol_xor 4, 0, 3 movq [edi + 8*6], mm0
movq mm0, [edi + 8*22]
repeat 5 movq [edi + 8*9], mm0
movq mm1, [D + 8*(%-1)] movq mm0, [edi + 8*14]
movq mm0, mm1 movq [edi + 8*22], mm0
pxor mm0, [edi + 8*(%-1 + 0)] movq mm0, [edi + 8*20]
movq [edi + 8*(%-1 + 0)], mm0 movq [edi + 8*14], mm0
movq mm0, mm1 movq mm0, [edi + 8*2]
pxor mm0, [edi + 8*(%-1 + 5)] movq [edi + 8*20], mm0
movq [edi + 8*(%-1 + 5)], mm0 movq mm0, [edi + 8*12]
movq mm0, mm1 movq [edi + 8*2], mm0
pxor mm0, [edi + 8*(%-1 + 10)] movq mm0, [edi + 8*13]
movq [edi + 8*(%-1 + 10)], mm0 movq [edi + 8*12], mm0
movq mm0, mm1 movq mm0, [edi + 8*19]
pxor mm0, [edi + 8*(%-1 + 15)] movq [edi + 8*13], mm0
movq [edi + 8*(%-1 + 15)], mm0 movq mm0, [edi + 8*23]
movq mm0, mm1 movq [edi + 8*19], mm0
pxor mm0, [edi + 8*(%-1 + 20)] movq mm0, [edi + 8*15]
movq [edi + 8*(%-1 + 20)], mm0 movq [edi + 8*23], mm0
end repeat movq mm0, [edi + 8*4]
movq [edi + 8*15], mm0
restore C,D movq mm0, [edi + 8*24]
ret movq [edi + 8*4], mm0
endp movq mm0, [edi + 8*21]
movq [edi + 8*24], mm0
movq mm0, [edi + 8*8]
proc sha3._.pi movq [edi + 8*21], mm0
movq mm1, [edi + 8*1] movq mm0, [edi + 8*16]
movq mm0, [edi + 8*6] movq [edi + 8*8], mm0
movq [edi + 8*1], mm0 movq mm0, [edi + 8*5]
movq mm0, [edi + 8*9] movq [edi + 8*16], mm0
movq [edi + 8*6], mm0 movq mm0, [edi + 8*3]
movq mm0, [edi + 8*22] movq [edi + 8*5], mm0
movq [edi + 8*9], mm0 movq mm0, [edi + 8*18]
movq mm0, [edi + 8*14] movq [edi + 8*3], mm0
movq [edi + 8*22], mm0 movq mm0, [edi + 8*17]
movq mm0, [edi + 8*20] movq [edi + 8*18], mm0
movq [edi + 8*14], mm0 movq mm0, [edi + 8*11]
movq mm0, [edi + 8*2] movq [edi + 8*17], mm0
movq [edi + 8*20], mm0 movq mm0, [edi + 8*7]
movq mm0, [edi + 8*12] movq [edi + 8*11], mm0
movq [edi + 8*2], mm0 movq mm0, [edi + 8*10]
movq mm0, [edi + 8*13] movq [edi + 8*7], mm0
movq [edi + 8*12], mm0 movq [edi + 8*10], mm1
movq mm0, [edi + 8*19]
movq [edi + 8*13], mm0 ret
movq mm0, [edi + 8*23] endp
movq [edi + 8*19], mm0
movq mm0, [edi + 8*15]
movq [edi + 8*23], mm0 proc sha3._.chi
movq mm0, [edi + 8*4]
movq [edi + 8*15], mm0 mov eax, 0xffffffff
movq mm0, [edi + 8*24] movd mm0, eax
movq [edi + 8*4], mm0 movq mm2, mm0
movq mm0, [edi + 8*21] punpckldq mm2, mm0
movq [edi + 8*24], mm0
movq mm0, [edi + 8*8] repeat 5
movq [edi + 8*21], mm0 movq mm6, [edi + 8*(0 + 5*(%-1))]
movq mm0, [edi + 8*16] movq mm7, [edi + 8*(1 + 5*(%-1))]
movq [edi + 8*8], mm0
movq mm0, [edi + 8*5] movq mm0, [edi + 8*(0 + 5*(%-1))]
movq [edi + 8*16], mm0 movq mm1, mm7
movq mm0, [edi + 8*3] pandn mm1, mm2
movq [edi + 8*5], mm0 pand mm1, [edi + 8*(2 + 5*(%-1))]
movq mm0, [edi + 8*18] pxor mm0, mm1
movq [edi + 8*3], mm0 movq [edi + 8*(0 + 5*(%-1))], mm0
movq mm0, [edi + 8*17]
movq [edi + 8*18], mm0 movq mm0, [edi + 8*(1 + 5*(%-1))]
movq mm0, [edi + 8*11] movq mm1, [edi + 8*(2 + 5*(%-1))]
movq [edi + 8*17], mm0 pandn mm1, mm2
movq mm0, [edi + 8*7] pand mm1, [edi + 8*(3 + 5*(%-1))]
movq [edi + 8*11], mm0 pxor mm0, mm1
movq mm0, [edi + 8*10] movq [edi + 8*(1 + 5*(%-1))], mm0
movq [edi + 8*7], mm0
movq [edi + 8*10], mm1 movq mm0, [edi + 8*(2 + 5*(%-1))]
movq mm1, [edi + 8*(3 + 5*(%-1))]
ret pandn mm1, mm2
endp pand mm1, [edi + 8*(4 + 5*(%-1))]
pxor mm0, mm1
movq [edi + 8*(2 + 5*(%-1))], mm0
proc sha3._.chi
movq mm0, [edi + 8*(3 + 5*(%-1))]
mov eax, 0xffffffff movq mm1, [edi + 8*(4 + 5*(%-1))]
movd mm0, eax pandn mm1, mm2
movq mm2, mm0 pand mm1, mm6
punpckldq mm2, mm0 pxor mm0, mm1
movq [edi + 8*(3 + 5*(%-1))], mm0
repeat 5
movq mm6, [edi + 8*(0 + 5*(%-1))] movq mm0, [edi + 8*(4 + 5*(%-1))]
movq mm7, [edi + 8*(1 + 5*(%-1))] movq mm1, mm6
pandn mm1, mm2
movq mm0, [edi + 8*(0 + 5*(%-1))] pand mm1, mm7
movq mm1, mm7 pxor mm0, mm1
pandn mm1, mm2 movq [edi + 8*(4 + 5*(%-1))], mm0
pand mm1, [edi + 8*(2 + 5*(%-1))] end repeat
pxor mm0, mm1 ret
movq [edi + 8*(0 + 5*(%-1))], mm0 endp
movq mm0, [edi + 8*(1 + 5*(%-1))]
movq mm1, [edi + 8*(2 + 5*(%-1))] macro sha3._.rol_mov n, c
pandn mm1, mm2 {
pand mm1, [edi + 8*(3 + 5*(%-1))] movq mm0, [edi + 8*(n)]
pxor mm0, mm1 movq mm1, mm0
movq [edi + 8*(1 + 5*(%-1))], mm0 psllq mm0, (c)
psrlq mm1, (64-(c))
movq mm0, [edi + 8*(2 + 5*(%-1))] por mm0, mm1
movq mm1, [edi + 8*(3 + 5*(%-1))] movq [edi + 8*(n)], mm0
pandn mm1, mm2 }
pand mm1, [edi + 8*(4 + 5*(%-1))]
pxor mm0, mm1 proc sha3._.permutation
movq [edi + 8*(2 + 5*(%-1))], mm0
repeat 24
movq mm0, [edi + 8*(3 + 5*(%-1))] stdcall sha3._.theta
movq mm1, [edi + 8*(4 + 5*(%-1))]
pandn mm1, mm2 sha3._.rol_mov 1, 1
pand mm1, mm6 sha3._.rol_mov 2, 62
pxor mm0, mm1 sha3._.rol_mov 3, 28
movq [edi + 8*(3 + 5*(%-1))], mm0 sha3._.rol_mov 4, 27
sha3._.rol_mov 5, 36
movq mm0, [edi + 8*(4 + 5*(%-1))] sha3._.rol_mov 6, 44
movq mm1, mm6 sha3._.rol_mov 7, 6
pandn mm1, mm2 sha3._.rol_mov 8, 55
pand mm1, mm7 sha3._.rol_mov 9, 20
pxor mm0, mm1 sha3._.rol_mov 10, 3
movq [edi + 8*(4 + 5*(%-1))], mm0 sha3._.rol_mov 11, 10
end repeat sha3._.rol_mov 12, 43
ret sha3._.rol_mov 13, 25
endp sha3._.rol_mov 14, 39
sha3._.rol_mov 15, 41
sha3._.rol_mov 16, 45
macro sha3._.rol_mov n, c sha3._.rol_mov 17, 15
{ sha3._.rol_mov 18, 21
movq mm0, [edi + 8*(n)] sha3._.rol_mov 19, 8
movq mm1, mm0 sha3._.rol_mov 20, 18
psllq mm0, (c) sha3._.rol_mov 21, 2
psrlq mm1, (64-(c)) sha3._.rol_mov 22, 61
por mm0, mm1 sha3._.rol_mov 23, 56
movq [edi + 8*(n)], mm0 sha3._.rol_mov 24, 14
}
stdcall sha3._.pi
proc sha3._.permutation stdcall sha3._.chi
repeat 24 movq mm0, [edi + 8*(0)]
stdcall sha3._.theta pxor mm0, [sha3._.round + 8*(%-1)]
movq [edi + 8*(0)], mm0
sha3._.rol_mov 1, 1 end repeat
sha3._.rol_mov 2, 62
sha3._.rol_mov 3, 28 ret
sha3._.rol_mov 4, 27 endp
sha3._.rol_mov 5, 36
sha3._.rol_mov 6, 44
sha3._.rol_mov 7, 6 proc sha3._.init _ctx
sha3._.rol_mov 8, 55 mov [ebx + ctx_sha3.block_size], eax
sha3._.rol_mov 9, 20 shr eax, 3
sha3._.rol_mov 10, 3 dec eax
sha3._.rol_mov 11, 10 mov [ebx + ctx_sha3.rounds_cnt], eax
sha3._.rol_mov 12, 43 xor eax, eax
sha3._.rol_mov 13, 25 lea edi, [ebx + ctx_sha3.hash]
sha3._.rol_mov 14, 39 mov ecx, SHA3_INIT_SIZE/4
sha3._.rol_mov 15, 41 rep stosd
sha3._.rol_mov 16, 45 mov [ebx + ctx_sha3.index], eax
sha3._.rol_mov 17, 15 ret
sha3._.rol_mov 18, 21 endp
sha3._.rol_mov 19, 8
sha3._.rol_mov 20, 18
sha3._.rol_mov 21, 2 proc sha3224.init _ctx
sha3._.rol_mov 22, 61 mov ebx, [_ctx]
sha3._.rol_mov 23, 56 mov eax, SHA3224_BLOCK_SIZE
sha3._.rol_mov 24, 14 stdcall sha3._.init
ret
stdcall sha3._.pi endp
stdcall sha3._.chi
movq mm0, [edi + 8*(0)] proc sha3256.init _ctx
pxor mm0, [sha3._.round + 8*(%-1)] mov ebx, [_ctx]
movq [edi + 8*(0)], mm0 mov eax, SHA3256_BLOCK_SIZE
end repeat stdcall sha3._.init
ret
ret endp
endp
proc sha3384.init _ctx
proc sha3._.init _ctx mov ebx, [_ctx]
mov [ebx + ctx_sha3.block_size], eax mov eax, SHA3384_BLOCK_SIZE
shr eax, 3 stdcall sha3._.init
dec eax ret
mov [ebx + ctx_sha3.rounds_cnt], eax endp
xor eax, eax
lea edi, [ebx + ctx_sha3.hash]
mov ecx, SHA3_INIT_SIZE/4 proc sha3512.init _ctx
rep stosd mov ebx, [_ctx]
mov [ebx + ctx_sha3.index], eax mov eax, SHA3512_BLOCK_SIZE
ret stdcall sha3._.init
endp ret
endp
proc sha3224.init _ctx
mov ebx, [_ctx] proc sha3._.block _hash
mov eax, SHA3224_BLOCK_SIZE mov ecx, [ebx + ctx_sha3.rounds_cnt]
stdcall sha3._.init mov edi, [_hash]
ret
endp @@:
movq mm0, [esi + 8*ecx]
pxor mm0, [edi + 8*ecx]
proc sha3256.init _ctx movq [edi + 8*ecx], mm0
mov ebx, [_ctx] dec ecx
mov eax, SHA3256_BLOCK_SIZE jns @b
stdcall sha3._.init
ret stdcall sha3._.permutation
endp
ret
endp
proc sha3384.init _ctx
mov ebx, [_ctx]
mov eax, SHA3384_BLOCK_SIZE sha3224.update = sha3.update
stdcall sha3._.init sha3256.update = sha3.update
ret sha3384.update = sha3.update
endp sha3512.update = sha3.update
proc sha3.update _ctx, _msg, _size
.next_block:
proc sha3512.init _ctx mov ebx, [_ctx]
mov ebx, [_ctx] mov esi, [_msg]
mov eax, SHA3512_BLOCK_SIZE mov eax, [ebx + ctx_sha3.index]
stdcall sha3._.init test eax, eax
ret jnz .copy_to_buf
endp test esi, SHA3_ALIGN_MASK
jnz .copy_to_buf
.no_copy:
proc sha3._.block _hash ; data is aligned, hash it in place without copying
mov ecx, [ebx + ctx_sha3.rounds_cnt] mov ebx, [_ctx]
mov edi, [_hash] mov eax, [ebx + ctx_sha3.block_size]
cmp [_size], eax
@@: jb .copy_quit
movq mm0, [esi + 8*ecx] lea eax, [ebx + ctx_sha3.hash]
pxor mm0, [edi + 8*ecx] push ebx esi
movq [edi + 8*ecx], mm0 stdcall sha3._.block, eax
dec ecx pop esi ebx
jns @b mov eax, [ebx + ctx_sha3.block_size]
sub [_size], eax
stdcall sha3._.permutation add esi, [ebx + ctx_sha3.block_size]
jmp .no_copy
ret
endp .copy_to_buf:
lea edi, [ebx + ctx_sha3.block]
add edi, eax
proc sha3.update _ctx, _msg, _size mov ecx, [ebx + ctx_sha3.block_size]
.next_block: sub ecx, eax
mov ebx, [_ctx] cmp [_size], ecx
mov esi, [_msg] jb .copy_quit
mov eax, [ebx + ctx_sha3.index] sub [_size], ecx
test eax, eax add [_msg], ecx
jnz .copy_to_buf add [ebx + ctx_sha3.index], ecx
test esi, SHA3_ALIGN_MASK mov eax, [ebx + ctx_sha3.block_size]
jnz .copy_to_buf cmp [ebx + ctx_sha3.index], eax
.no_copy: jb @f
; data is aligned, hash it in place without copying sub [ebx + ctx_sha3.index], eax
mov ebx, [_ctx] @@:
mov eax, [ebx + ctx_sha3.block_size] rep movsb
cmp [_size], eax lea eax, [ebx + ctx_sha3.hash]
jb .copy_quit lea esi, [ebx + ctx_sha3.block]
lea eax, [ebx + ctx_sha3.hash] stdcall sha3._.block, eax
push ebx esi jmp .next_block
stdcall sha3._.block, eax
pop esi ebx .copy_quit:
mov eax, [ebx + ctx_sha3.block_size] mov ebx, [_ctx]
sub [_size], eax lea edi, [ebx + ctx_sha3.block]
add esi, [ebx + ctx_sha3.block_size] mov eax, [ebx + ctx_sha3.index]
jmp .no_copy add edi, eax
mov ecx, [_size]
.copy_to_buf: add [ebx + ctx_sha3.index], ecx
lea edi, [ebx + ctx_sha3.block] rep movsb
add edi, eax .quit:
mov ecx, [ebx + ctx_sha3.block_size] ret
sub ecx, eax endp
cmp [_size], ecx
jb .copy_quit
sub [_size], ecx sha3224.final = sha3.final
add [_msg], ecx sha3256.final = sha3.final
add [ebx + ctx_sha3.index], ecx sha3384.final = sha3.final
mov eax, [ebx + ctx_sha3.block_size] sha3512.final = sha3.final
cmp [ebx + ctx_sha3.index], eax proc sha3.final _ctx
jb @f pushad
sub [ebx + ctx_sha3.index], eax mov ebx, [_ctx]
@@: mov eax, [ebx + ctx_sha3.index]
rep movsb xor edx, edx
lea eax, [ebx + ctx_sha3.hash] mov ecx, [ebx + ctx_sha3.block_size]
lea esi, [ebx + ctx_sha3.block] div ecx
stdcall sha3._.block, eax sub ecx, edx
jmp .next_block ja @f
add ecx, [ebx + ctx_sha3.block_size]
.copy_quit: @@:
mov ebx, [_ctx] add [ebx + ctx_sha3.index], ecx
lea edi, [ebx + ctx_sha3.block] mov eax, [ebx + ctx_sha3.block_size]
mov eax, [ebx + ctx_sha3.index] cmp [ebx + ctx_sha3.index], eax
add edi, eax jb @f
mov ecx, [_size] sub [ebx + ctx_sha3.index], eax
add [ebx + ctx_sha3.index], ecx @@:
rep movsb
.quit: mov byte[edi], 0x06
ret inc edi
endp dec ecx
xor eax, eax
rep stosb
proc sha3.final _ctx or byte[edi - 1], 0x80
pushad
mov ebx, [_ctx] mov ebx, [_ctx]
mov eax, [ebx + ctx_sha3.index] lea esi, [ebx + ctx_sha3.block]
xor edx, edx lea eax, [ebx + ctx_sha3.hash]
mov ecx, [ebx + ctx_sha3.block_size] stdcall sha3._.block, eax
div ecx
sub ecx, edx mov ebx, [_ctx]
ja @f lea eax, [ebx + ctx_sha3.hash]
add ecx, [ebx + ctx_sha3.block_size] stdcall sha3._.postprocess, ebx, eax
@@:
add [ebx + ctx_sha3.index], ecx popad
mov eax, [ebx + ctx_sha3.block_size] ret
cmp [ebx + ctx_sha3.index], eax endp
jb @f
sub [ebx + ctx_sha3.index], eax
@@: proc sha3._.postprocess _ctx, _hash
emms
mov byte[edi], 0x06 ret
inc edi endp
dec ecx
xor eax, eax
rep stosb align SHA3_ALIGN
or byte[edi - 1], 0x80
sha3._.round dq 0x0000000000000001, 0x0000000000008082, 0x800000000000808A,\
mov ebx, [_ctx] 0x8000000080008000, 0x000000000000808B, 0x0000000080000001,\
lea esi, [ebx + ctx_sha3.block] 0x8000000080008081, 0x8000000000008009, 0x000000000000008A,\
lea eax, [ebx + ctx_sha3.hash] 0x0000000000000088, 0x0000000080008009, 0x000000008000000A,\
stdcall sha3._.block, eax 0x000000008000808B, 0x800000000000008B, 0x8000000000008089,\
0x8000000000008003, 0x8000000000008002, 0x8000000000000080,\
mov ebx, [_ctx] 0x000000000000800A, 0x800000008000000A, 0x8000000080008081,\
lea eax, [ebx + ctx_sha3.hash] 0x8000000000008080, 0x0000000080000001, 0x8000000080008008
stdcall sha3._.postprocess, ebx, eax
popad
ret
endp
proc sha3._.postprocess _ctx, _hash
emms
ret
endp
align SHA3_ALIGN
sha3._.round dq 0x0000000000000001, 0x0000000000008082, 0x800000000000808A,\
0x8000000080008000, 0x000000000000808B, 0x0000000080000001,\
0x8000000080008081, 0x8000000000008009, 0x000000000000008A,\
0x0000000000000088, 0x0000000080008009, 0x000000008000000A,\
0x000000008000808B, 0x800000000000008B, 0x8000000000008089,\
0x8000000000008003, 0x8000000000008002, 0x8000000000000080,\
0x000000000000800A, 0x800000008000000A, 0x8000000080008081,\
0x8000000000008080, 0x0000000080000001, 0x8000000080008008

File diff suppressed because it is too large Load Diff