From b51d135d5c24590d8ed4bc483f50348957f19269 Mon Sep 17 00:00:00 2001 From: Ivan Baravy Date: Mon, 21 Oct 2019 22:33:41 +0000 Subject: [PATCH] libcrash: Sync with upstream. * Add oneshot wrappers. * Move tables to data section. * Add/rename macros and fix their use in ssh. git-svn-id: svn://kolibrios.org@7698 a494cfbc-eb01-0410-851d-a64ba20cac60 --- .../libraries/libcrash/trunk/Tupfile.lua | 6 +- .../libraries/libcrash/trunk/crc32.asm | 245 ++-- .../libraries/libcrash/trunk/libcrash.asm | 74 +- .../libraries/libcrash/trunk/libcrash.inc | 256 ++-- .../develop/libraries/libcrash/trunk/md4.asm | 500 ++++---- .../develop/libraries/libcrash/trunk/md5.asm | 556 +++++---- .../develop/libraries/libcrash/trunk/sha1.asm | 572 ++++----- .../libraries/libcrash/trunk/sha224_256.asm | 840 +++++++------ .../develop/libraries/libcrash/trunk/sha3.asm | 899 +++++++------- .../libraries/libcrash/trunk/sha384_512.asm | 1077 +++++++++-------- programs/network/ssh/dh_gex.inc | 12 +- programs/network/ssh/hmac_md5.inc | 4 +- programs/network/ssh/hmac_sha1.inc | 4 +- programs/network/ssh/hmac_sha256.inc | 30 +- programs/network/ssh/ssh.asm | 4 +- 15 files changed, 2631 insertions(+), 2448 deletions(-) diff --git a/programs/develop/libraries/libcrash/trunk/Tupfile.lua b/programs/develop/libraries/libcrash/trunk/Tupfile.lua index 7f1895b022..1983d62fc5 100644 --- a/programs/develop/libraries/libcrash/trunk/Tupfile.lua +++ b/programs/develop/libraries/libcrash/trunk/Tupfile.lua @@ -1,3 +1,5 @@ if tup.getconfig("NO_FASM") ~= "" then return end -tup.rule("libcrash.asm", "fasm %f %o " .. tup.getconfig("KPACK_CMD"), "libcrash.obj") -tup.rule("crashtest.asm", "fasm %f %o " .. tup.getconfig("KPACK_CMD"), "crashtest") +HELPERDIR = (tup.getconfig("HELPERDIR") == "") and "../../../.." or tup.getconfig("HELPERDIR") +tup.include(HELPERDIR .. "/use_fasm.lua") +tup.rule("libcrash.asm", FASM .. " %f %o " .. tup.getconfig("KPACK_CMD"), "libcrash.obj") +tup.rule("crashtest.asm", FASM .. " %f %o " .. tup.getconfig("KPACK_CMD"), "crashtest") diff --git a/programs/develop/libraries/libcrash/trunk/crc32.asm b/programs/develop/libraries/libcrash/trunk/crc32.asm index 1d6d3e4c2f..f44811642a 100644 --- a/programs/develop/libraries/libcrash/trunk/crc32.asm +++ b/programs/develop/libraries/libcrash/trunk/crc32.asm @@ -1,112 +1,133 @@ -; libcrash -- cryptographic hash functions -; -; Copyright (C) 2012-2013,2016 Ivan Baravy (dunkaist) -; -; 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 -; the Free Software Foundation, either version 3 of the License, or -; (at your option) any later version. -; -; This program is distributed in the hope that it will be useful, -; but WITHOUT ANY WARRANTY; without even the implied warranty of -; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -; GNU General Public License for more details. -; -; You should have received a copy of the GNU General Public License -; along with this program. If not, see . - - -proc crc32.init _ctx - mov ebx, [_ctx] - lea edi, [ebx + ctx_crc32.hash] - mov esi, crc32._.hash_init - mov ecx, 1 - rep movsd - ret -endp - - -proc crc32.update _ctx, _msg, _size - mov ebx, [_ctx] - mov esi, [_msg] - lea edi, [ebx + ctx_crc32.hash] - mov eax, [edi] - mov ecx, [_size] - jecxz .quit - - @@: - movzx edx, al - xor dl, byte[esi] - add esi, 1 - shr eax, 8 - xor eax, [crc32._.table + edx*4] - dec ecx - jnz @b - - stosd - .quit: - ret -endp - - -proc crc32.final _ctx - mov ebx, [_ctx] - lea esi, [ebx + ctx_crc32.hash] - mov edi, esi - lodsd - xor eax, 0xffffffff - bswap eax - stosd - ret -endp - - -align CRC32_ALIGN - -crc32._.hash_init dd 0xffffffff - -crc32._.table dd \ - 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,\ - 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,\ - 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,\ - 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,\ - 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,\ - 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,\ - 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,\ - 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,\ - 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,\ - 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,\ - 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,\ - 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,\ - 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,\ - 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,\ - 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,\ - 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,\ - 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,\ - 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,\ - 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,\ - 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,\ - 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,\ - 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,\ - 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,\ - 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,\ - 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,\ - 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,\ - 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,\ - 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,\ - 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,\ - 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,\ - 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,\ - 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,\ - 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,\ - 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,\ - 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 - +; libcrash -- cryptographic hash functions +; +; Copyright (C) 2012-2013,2016,2019 Ivan Baravy (dunkaist) +; +; 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 +; the Free Software Foundation, either version 3 of the License, or +; (at your option) any later version. +; +; This program is distributed in the hope that it will be useful, +; but WITHOUT ANY WARRANTY; without even the implied warranty of +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +; GNU General Public License for more details. +; +; You should have received a copy of the GNU General Public License +; along with this program. If not, see . + + +CRC32_HASH_SIZE = 4 + +CRC32_ALIGN = 4 +CRC32_ALIGN_MASK = CRC32_ALIGN - 1 + +struct ctx_crc32 + hash rd 1 +ends + +if defined sizeof.crash_ctx + assert sizeof.crash_ctx >= sizeof.ctx_crc32 +end if + +proc crc32.init _ctx + mov ebx, [_ctx] + lea edi, [ebx + ctx_crc32.hash] + mov esi, crc32._.hash_init + mov ecx, 1 + rep movsd + ret +endp + + +proc crc32.update _ctx, _msg, _size + mov ebx, [_ctx] + mov esi, [_msg] + lea edi, [ebx + ctx_crc32.hash] + mov eax, [edi] + mov ecx, [_size] + jecxz .quit + + @@: + movzx edx, al + xor dl, byte[esi] + add esi, 1 + shr eax, 8 + xor eax, [crc32._.table + edx*4] + dec ecx + jnz @b + + stosd + .quit: + ret +endp + + +proc crc32.final _ctx + mov ebx, [_ctx] + lea esi, [ebx + ctx_crc32.hash] + mov edi, esi + lodsd + xor eax, 0xffffffff + bswap eax + stosd + ret +endp + + +proc crc32.oneshot _ctx, _data, _len + stdcall crc32.init, [_ctx] + stdcall crc32.update, [_ctx], [_data], [_len] + stdcall crc32.final, [_ctx] + ret +endp + + +iglobal +align CRC32_ALIGN +crc32._.hash_init dd 0xffffffff + +crc32._.table dd \ + 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,\ + 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,\ + 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,\ + 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,\ + 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,\ + 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,\ + 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,\ + 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,\ + 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,\ + 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,\ + 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,\ + 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,\ + 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,\ + 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,\ + 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,\ + 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,\ + 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,\ + 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,\ + 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,\ + 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,\ + 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,\ + 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,\ + 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,\ + 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,\ + 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,\ + 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,\ + 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,\ + 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,\ + 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,\ + 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,\ + 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,\ + 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,\ + 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,\ + 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,\ + 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 +endg diff --git a/programs/develop/libraries/libcrash/trunk/libcrash.asm b/programs/develop/libraries/libcrash/trunk/libcrash.asm index 6dcae6866a..76b35cb40e 100644 --- a/programs/develop/libraries/libcrash/trunk/libcrash.asm +++ b/programs/develop/libraries/libcrash/trunk/libcrash.asm @@ -19,11 +19,11 @@ format MS COFF public @EXPORT as 'EXPORTS' -include '../../../../struct.inc' -include '../../../../proc32.inc' -include '../../../../macros.inc' -include '../../../../config.inc' -;include '../../../../debug.inc' +include 'struct.inc' +include 'proc32.inc' +include 'macros.inc' +include 'config.inc' +include 'kglobals.inc' purge section,mov,add,sub section '.flat' code readable align 16 @@ -103,21 +103,23 @@ endp section '.data' data readable align 16 -crash._.bin2hex_table db '0123456789abcdef' +crash._.bin2hex_table db '0123456789abcdef' crash._.table dd \ - crc32.init, crc32.update, crc32.final, CRC32_HASH_SIZE, \ - md4.init, md4.update, md4.final, MD4_HASH_SIZE, \ - md5.init, md5.update, md5.final, MD5_HASH_SIZE, \ - sha1.init, sha1.update, sha1.final, SHA1_HASH_SIZE, \ - sha224.init, sha224.update, sha224.final, SHA224_HASH_SIZE, \ - sha256.init, sha256.update, sha256.final, SHA256_HASH_SIZE, \ - sha384.init, sha384.update, sha384.final, SHA384_HASH_SIZE, \ - sha512.init, sha512.update, sha512.final, SHA512_HASH_SIZE, \ - sha3224.init, sha3224.update, sha3224.final, SHA3224_HASH_SIZE,\ - sha3256.init, sha3256.update, sha3256.final, SHA3256_HASH_SIZE,\ - sha3384.init, sha3384.update, sha3384.final, SHA3384_HASH_SIZE,\ - sha3512.init, sha3512.update, sha3512.final, SHA3512_HASH_SIZE + crc32.init, crc32.update, crc32.final, crc32.oneshot, CRC32_HASH_SIZE, \ + md4.init, md4.update, md4.final, md4.oneshot, MD4_HASH_SIZE, \ + md5.init, md5.update, md5.final, md5.oneshot, MD5_HASH_SIZE, \ + sha1.init, sha1.update, sha1.final, sha1.oneshot, SHA1_HASH_SIZE, \ + sha224.init, sha224.update, sha224.final, sha224.oneshot, SHA224_HASH_SIZE, \ + sha256.init, sha256.update, sha256.final, sha256.oneshot, SHA256_HASH_SIZE, \ + sha384.init, sha384.update, sha384.final, sha384.oneshot, SHA384_HASH_SIZE, \ + sha512.init, sha512.update, sha512.final, sha512.oneshot, SHA512_HASH_SIZE, \ + sha3_224.init, sha3_224.update, sha3_224.final, sha3_224.oneshot, SHA3_224_HASH_SIZE,\ + sha3_256.init, sha3_256.update, sha3_256.final, sha3_256.oneshot, SHA3_256_HASH_SIZE,\ + sha3_384.init, sha3_384.update, sha3_384.final, sha3_384.oneshot, SHA3_384_HASH_SIZE,\ + sha3_512.init, sha3_512.update, sha3_512.final, sha3_512.oneshot, SHA3_512_HASH_SIZE + +IncludeIGlobals align 4 @EXPORT: @@ -129,36 +131,48 @@ export \ crc32.init, 'crc32_init' , \ crc32.update, 'crc32_update' , \ crc32.final, 'crc32_final' , \ + crc32.oneshot, 'crc32_oneshot' , \ md4.init, 'md4_init' , \ md4.update, 'md4_update' , \ md4.final, 'md4_final' , \ + md4.oneshot, 'md4_oneshot' , \ md5.init, 'md5_init' , \ md5.update, 'md5_update' , \ md5.final, 'md5_final' , \ + md5.oneshot, 'md5_oneshot' , \ sha1.init, 'sha1_init' , \ sha1.update, 'sha1_update' , \ sha1.final, 'sha1_final' , \ + sha1.oneshot, 'sha1_oneshot' , \ sha224.init, 'sha224_init' , \ sha224.update, 'sha224_update' , \ sha224.final, 'sha224_final' , \ + sha224.oneshot, 'sha224_oneshot' , \ sha256.init, 'sha256_init' , \ sha256.update, 'sha256_update' , \ sha256.final, 'sha256_final' , \ + sha256.oneshot, 'sha256_oneshot' , \ sha384.init, 'sha384_init' , \ sha384.update, 'sha384_update' , \ sha384.final, 'sha384_final' , \ + sha384.oneshot, 'sha384_oneshot' , \ 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' + sha512.oneshot, 'sha512_oneshot' , \ + sha3_224.init, 'sha3_224_init' , \ + sha3_224.update, 'sha3_224_update' , \ + sha3_224.final, 'sha3_224_final' , \ + sha3_224.oneshot, 'sha3_224_oneshot' , \ + sha3_256.init, 'sha3_256_init' , \ + sha3_256.update, 'sha3_256_update' , \ + sha3_256.final, 'sha3_256_final' , \ + sha3_256.oneshot, 'sha3_256_oneshot' , \ + sha3_384.init, 'sha3_384_init' , \ + sha3_384.update, 'sha3_384_update' , \ + sha3_384.final, 'sha3_384_final' , \ + sha3_384.oneshot, 'sha3_384_oneshot' , \ + sha3_512.init, 'sha3_512_init' , \ + sha3_512.update, 'sha3_512_update' , \ + sha3_512.final, 'sha3_512_final' , \ + sha3_512.oneshot, 'sha3_512_oneshot' diff --git a/programs/develop/libraries/libcrash/trunk/libcrash.inc b/programs/develop/libraries/libcrash/trunk/libcrash.inc index 6930df00be..67a8c682cd 100644 --- a/programs/develop/libraries/libcrash/trunk/libcrash.inc +++ b/programs/develop/libraries/libcrash/trunk/libcrash.inc @@ -1,175 +1,81 @@ -; libcrash -- cryptographic hash functions -; -; Copyright (C) 2012-2014,2016 Ivan Baravy (dunkaist) -; -; 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 -; the Free Software Foundation, either version 3 of the License, or -; (at your option) any later version. -; -; This program is distributed in the hope that it will be useful, -; but WITHOUT ANY WARRANTY; without even the implied warranty of -; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -; GNU General Public License for more details. -; -; You should have received a copy of the GNU General Public License -; along with this program. If not, see . - - -LIBCRASH_CRC32 = 0 -LIBCRASH_MD4 = 1 -LIBCRASH_MD5 = 2 -LIBCRASH_SHA1 = 3 -LIBCRASH_SHA224 = 4 -LIBCRASH_SHA256 = 5 -LIBCRASH_SHA384 = 6 -LIBCRASH_SHA512 = 7 -LIBCRASH_SHA3_224 = 8 -LIBCRASH_SHA3_256 = 9 -LIBCRASH_SHA3_384 = 10 -LIBCRASH_SHA3_512 = 11 -LIBCRASH_LAST = 11 - - -struct crash_item - init dd ? - update dd ? - final dd ? - len_out dd ? -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 - +; libcrash -- cryptographic hash functions +; +; Copyright (C) 2012-2014,2016,2019 Ivan Baravy (dunkaist) +; +; 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 +; the Free Software Foundation, either version 3 of the License, or +; (at your option) any later version. +; +; This program is distributed in the hope that it will be useful, +; but WITHOUT ANY WARRANTY; without even the implied warranty of +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +; GNU General Public License for more details. +; +; You should have received a copy of the GNU General Public License +; along with this program. If not, see . + + +LIBCRASH_CRC32 = 0 +LIBCRASH_MD4 = 1 +LIBCRASH_MD5 = 2 +LIBCRASH_SHA1 = 3 +LIBCRASH_SHA224 = 4 +LIBCRASH_SHA256 = 5 +LIBCRASH_SHA384 = 6 +LIBCRASH_SHA512 = 7 +LIBCRASH_SHA3_224 = 8 +LIBCRASH_SHA3_256 = 9 +LIBCRASH_SHA3_384 = 10 +LIBCRASH_SHA3_512 = 11 +LIBCRASH_LAST = 11 + + +struct crash_item + init dd ? + update dd ? + final dd ? + oneshot dd ? + len_out dd ? +ends + +struct crash_ctx + hash rb 1024 ; context starts with hash data +ends + + +CRC32_HASH_SIZE = 4 +CRC32_BLOCK_SIZE = 1 + +MD4_HASH_SIZE = 16 +MD4_BLOCK_SIZE = 64 + +MD5_HASH_SIZE = 16 +MD5_BLOCK_SIZE = 64 + +SHA1_HASH_SIZE = 20 +SHA1_BLOCK_SIZE = 64 + +SHA224_HASH_SIZE = 28 +SHA224_BLOCK_SIZE = 64 + +SHA256_HASH_SIZE = 32 +SHA256_BLOCK_SIZE = 64 + +SHA384_HASH_SIZE = 48 +SHA384_BLOCK_SIZE = 128 + +SHA512_HASH_SIZE = 64 +SHA512_BLOCK_SIZE = 128 + +SHA3_224_HASH_SIZE = 28 +SHA3_224_BLOCK_SIZE = 144 + +SHA3_256_HASH_SIZE = 32 +SHA3_256_BLOCK_SIZE = 136 + +SHA3_384_HASH_SIZE = 48 +SHA3_384_BLOCK_SIZE = 104 + +SHA3_512_HASH_SIZE = 64 +SHA3_512_BLOCK_SIZE = 72 diff --git a/programs/develop/libraries/libcrash/trunk/md4.asm b/programs/develop/libraries/libcrash/trunk/md4.asm index f3cdf0e93f..07f57e614a 100644 --- a/programs/develop/libraries/libcrash/trunk/md4.asm +++ b/programs/develop/libraries/libcrash/trunk/md4.asm @@ -1,237 +1,263 @@ -; libcrash -- cryptographic hash functions -; -; Copyright (C) 2012-2013,2016 Ivan Baravy (dunkaist) -; -; 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 -; the Free Software Foundation, either version 3 of the License, or -; (at your option) any later version. -; -; This program is distributed in the hope that it will be useful, -; but WITHOUT ANY WARRANTY; without even the implied warranty of -; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -; GNU General Public License for more details. -; -; You should have received a copy of the GNU General Public License -; along with this program. If not, see . - - -macro md4._.f b, c, d -{ - mov eax, c - xor eax, d - and eax, b - xor eax, d -} - -macro md4._.g b, c, d -{ - push c d - mov eax, b - and eax, c - and c, d - and d, b - or eax, c - or eax, d - pop d c -} - -macro md4._.h b, c, d -{ - mov eax, b - xor eax, c - xor eax, d -} - -macro md4._.round func, a, b, c, d, index, shift, ac -{ - func b, c, d - add eax, [esi + index*4] - lea a, [a + eax + ac] - rol a, shift -} - - -proc md4.init _ctx - mov ebx, [_ctx] - lea edi, [ebx + ctx_md4.hash] - mov esi, md4._.hash_init - mov ecx, MD4_HASH_SIZE/4 - rep movsd - xor eax, eax - mov [ebx + ctx_md4.index], eax - mov [ebx + ctx_md4.msglen_0], eax - mov [ebx + ctx_md4.msglen_1], eax - ret -endp - - -proc md4._.block _hash - - mov eax, [_hash] - mov edi, [eax + 0x0] - mov ebx, [eax + 0x4] - mov ecx, [eax + 0x8] - mov edx, [eax + 0xc] - - md4._.round md4._.f, edi, ebx, ecx, edx, 0, 3, 0x00000000 - md4._.round md4._.f, edx, edi, ebx, ecx, 1, 7, 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._.f, edi, ebx, ecx, edx, 4, 3, 0x00000000 - md4._.round md4._.f, edx, edi, ebx, ecx, 5, 7, 0x00000000 - md4._.round md4._.f, ecx, edx, edi, ebx, 6, 11, 0x00000000 - md4._.round md4._.f, ebx, ecx, edx, edi, 7, 19, 0x00000000 - md4._.round md4._.f, edi, ebx, ecx, edx, 8, 3, 0x00000000 - md4._.round md4._.f, edx, edi, ebx, ecx, 9, 7, 0x00000000 - md4._.round md4._.f, ecx, edx, edi, ebx, 10, 11, 0x00000000 - md4._.round md4._.f, ebx, ecx, edx, edi, 11, 19, 0x00000000 - 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, ecx, edx, edi, ebx, 14, 11, 0x00000000 - md4._.round md4._.f, ebx, ecx, edx, edi, 15, 19, 0x00000000 - - md4._.round md4._.g, edi, ebx, ecx, edx, 0, 3, 0x5a827999 - md4._.round md4._.g, edx, edi, ebx, ecx, 4, 5, 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._.g, edi, ebx, ecx, edx, 1, 3, 0x5a827999 - md4._.round md4._.g, edx, edi, ebx, ecx, 5, 5, 0x5a827999 - md4._.round md4._.g, ecx, edx, edi, ebx, 9, 9, 0x5a827999 - md4._.round md4._.g, ebx, ecx, edx, edi, 13, 13, 0x5a827999 - md4._.round md4._.g, edi, ebx, ecx, edx, 2, 3, 0x5a827999 - md4._.round md4._.g, edx, edi, ebx, ecx, 6, 5, 0x5a827999 - md4._.round md4._.g, ecx, edx, edi, ebx, 10, 9, 0x5a827999 - md4._.round md4._.g, ebx, ecx, edx, edi, 14, 13, 0x5a827999 - 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, ecx, edx, edi, ebx, 11, 9, 0x5a827999 - md4._.round md4._.g, ebx, ecx, edx, edi, 15, 13, 0x5a827999 - - md4._.round md4._.h, edi, ebx, ecx, edx, 0, 3, 0x6ed9eba1 - md4._.round md4._.h, edx, edi, ebx, ecx, 8, 9, 0x6ed9eba1 - md4._.round md4._.h, ecx, edx, edi, ebx, 4, 11, 0x6ed9eba1 - md4._.round md4._.h, ebx, ecx, edx, edi, 12, 15, 0x6ed9eba1 - md4._.round md4._.h, edi, ebx, ecx, edx, 2, 3, 0x6ed9eba1 - md4._.round md4._.h, edx, edi, ebx, ecx, 10, 9, 0x6ed9eba1 - md4._.round md4._.h, ecx, edx, edi, ebx, 6, 11, 0x6ed9eba1 - md4._.round md4._.h, ebx, ecx, edx, edi, 14, 15, 0x6ed9eba1 - md4._.round md4._.h, edi, ebx, ecx, edx, 1, 3, 0x6ed9eba1 - md4._.round md4._.h, edx, edi, ebx, ecx, 9, 9, 0x6ed9eba1 - md4._.round md4._.h, ecx, edx, edi, ebx, 5, 11, 0x6ed9eba1 - 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 - md4._.round md4._.h, ecx, edx, edi, ebx, 7, 11, 0x6ed9eba1 - md4._.round md4._.h, ebx, ecx, edx, edi, 15, 15, 0x6ed9eba1 - - mov eax, [_hash] - add [eax + 0x0], edi - add [eax + 0x4], ebx - add [eax + 0x8], ecx - add [eax + 0xc], edx - - ret -endp - - -proc md4.update _ctx, _msg, _size - mov ebx, [_ctx] - mov ecx, [_size] - add [ebx + ctx_md4.msglen_0], ecx - adc [ebx + ctx_md4.msglen_1], 0 - - .next_block: - mov ebx, [_ctx] - mov esi, [_msg] - mov eax, [ebx + ctx_md4.index] - and eax, MD4_BLOCK_SIZE-1 - jnz .copy_to_buf - test esi, MD4_ALIGN_MASK - jnz .copy_to_buf - .no_copy: - ; data is aligned, hash it in place without copying - mov ebx, [_ctx] - cmp [_size], MD4_BLOCK_SIZE - jb .copy_quit - lea eax, [ebx + ctx_md4.hash] - stdcall md4._.block, eax - sub [_size], MD4_BLOCK_SIZE - add esi, MD4_BLOCK_SIZE - jmp .no_copy - - .copy_to_buf: - lea edi, [ebx + ctx_md4.block] - add edi, eax - mov ecx, MD4_BLOCK_SIZE - sub ecx, eax - cmp [_size], ecx - jb .copy_quit - sub [_size], ecx - add [_msg], ecx - add [ebx + ctx_md4.index], ecx - rep movsb - lea eax, [ebx + ctx_md4.hash] - lea esi, [ebx + ctx_md4.block] - stdcall md4._.block, eax - jmp .next_block - - .copy_quit: - mov ebx, [_ctx] - lea edi, [ebx + ctx_md4.block] - mov eax, [ebx + ctx_md4.index] - and eax, MD4_BLOCK_SIZE-1 - add edi, eax - mov ecx, [_size] - add [ebx + ctx_md4.index], ecx - rep movsb - .quit: - ret -endp - - -proc md4.final _ctx - mov ebx, [_ctx] - lea edi, [ebx + ctx_md4.block] - mov ecx, [ebx + ctx_md4.msglen_0] - and ecx, MD4_BLOCK_SIZE-1 - add edi, ecx - mov byte[edi], 0x80 - inc edi - neg ecx - add ecx, MD4_BLOCK_SIZE - cmp ecx, 8 - ja .last - - dec ecx - xor eax, eax - rep stosb - lea esi, [ebx + ctx_md4.block] - lea eax, [ebx + ctx_md4.hash] - stdcall md4._.block, eax - mov ebx, [_ctx] - lea edi, [ebx + ctx_md4.block] - mov ecx, MD4_BLOCK_SIZE+1 - .last: - dec ecx - sub ecx, 8 - xor eax, eax - rep stosb - mov eax, [ebx + ctx_md4.msglen_0] - mov edx, [ebx + ctx_md4.msglen_1] - shld edx, eax, 3 - 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 - +; libcrash -- cryptographic hash functions +; +; Copyright (C) 2012-2013,2016,2019 Ivan Baravy (dunkaist) +; +; 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 +; the Free Software Foundation, either version 3 of the License, or +; (at your option) any later version. +; +; This program is distributed in the hope that it will be useful, +; but WITHOUT ANY WARRANTY; without even the implied warranty of +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +; GNU General Public License for more details. +; +; You should have received a copy of the GNU General Public License +; along with this program. If not, see . + + +MD4_HASH_SIZE = 16 +MD4_BLOCK_SIZE = 64 + +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 + +if defined sizeof.crash_ctx + assert sizeof.crash_ctx >= sizeof.ctx_md4 +end if + +macro md4._.f b, c, d +{ + mov eax, c + xor eax, d + and eax, b + xor eax, d +} + +macro md4._.g b, c, d +{ + push c d + mov eax, b + and eax, c + and c, d + and d, b + or eax, c + or eax, d + pop d c +} + +macro md4._.h b, c, d +{ + mov eax, b + xor eax, c + xor eax, d +} + +macro md4._.round func, a, b, c, d, index, shift, ac +{ + func b, c, d + add eax, [esi + index*4] + lea a, [a + eax + ac] + rol a, shift +} + + +proc md4.init _ctx + mov ebx, [_ctx] + lea edi, [ebx + ctx_md4.hash] + mov esi, md4._.hash_init + mov ecx, MD4_HASH_SIZE/4 + rep movsd + xor eax, eax + mov [ebx + ctx_md4.index], eax + mov [ebx + ctx_md4.msglen_0], eax + mov [ebx + ctx_md4.msglen_1], eax + ret +endp + + +proc md4._.block _hash + + mov eax, [_hash] + mov edi, [eax + 0x0] + mov ebx, [eax + 0x4] + mov ecx, [eax + 0x8] + mov edx, [eax + 0xc] + + md4._.round md4._.f, edi, ebx, ecx, edx, 0, 3, 0x00000000 + md4._.round md4._.f, edx, edi, ebx, ecx, 1, 7, 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._.f, edi, ebx, ecx, edx, 4, 3, 0x00000000 + md4._.round md4._.f, edx, edi, ebx, ecx, 5, 7, 0x00000000 + md4._.round md4._.f, ecx, edx, edi, ebx, 6, 11, 0x00000000 + md4._.round md4._.f, ebx, ecx, edx, edi, 7, 19, 0x00000000 + md4._.round md4._.f, edi, ebx, ecx, edx, 8, 3, 0x00000000 + md4._.round md4._.f, edx, edi, ebx, ecx, 9, 7, 0x00000000 + md4._.round md4._.f, ecx, edx, edi, ebx, 10, 11, 0x00000000 + md4._.round md4._.f, ebx, ecx, edx, edi, 11, 19, 0x00000000 + 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, ecx, edx, edi, ebx, 14, 11, 0x00000000 + md4._.round md4._.f, ebx, ecx, edx, edi, 15, 19, 0x00000000 + + md4._.round md4._.g, edi, ebx, ecx, edx, 0, 3, 0x5a827999 + md4._.round md4._.g, edx, edi, ebx, ecx, 4, 5, 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._.g, edi, ebx, ecx, edx, 1, 3, 0x5a827999 + md4._.round md4._.g, edx, edi, ebx, ecx, 5, 5, 0x5a827999 + md4._.round md4._.g, ecx, edx, edi, ebx, 9, 9, 0x5a827999 + md4._.round md4._.g, ebx, ecx, edx, edi, 13, 13, 0x5a827999 + md4._.round md4._.g, edi, ebx, ecx, edx, 2, 3, 0x5a827999 + md4._.round md4._.g, edx, edi, ebx, ecx, 6, 5, 0x5a827999 + md4._.round md4._.g, ecx, edx, edi, ebx, 10, 9, 0x5a827999 + md4._.round md4._.g, ebx, ecx, edx, edi, 14, 13, 0x5a827999 + 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, ecx, edx, edi, ebx, 11, 9, 0x5a827999 + md4._.round md4._.g, ebx, ecx, edx, edi, 15, 13, 0x5a827999 + + md4._.round md4._.h, edi, ebx, ecx, edx, 0, 3, 0x6ed9eba1 + md4._.round md4._.h, edx, edi, ebx, ecx, 8, 9, 0x6ed9eba1 + md4._.round md4._.h, ecx, edx, edi, ebx, 4, 11, 0x6ed9eba1 + md4._.round md4._.h, ebx, ecx, edx, edi, 12, 15, 0x6ed9eba1 + md4._.round md4._.h, edi, ebx, ecx, edx, 2, 3, 0x6ed9eba1 + md4._.round md4._.h, edx, edi, ebx, ecx, 10, 9, 0x6ed9eba1 + md4._.round md4._.h, ecx, edx, edi, ebx, 6, 11, 0x6ed9eba1 + md4._.round md4._.h, ebx, ecx, edx, edi, 14, 15, 0x6ed9eba1 + md4._.round md4._.h, edi, ebx, ecx, edx, 1, 3, 0x6ed9eba1 + md4._.round md4._.h, edx, edi, ebx, ecx, 9, 9, 0x6ed9eba1 + md4._.round md4._.h, ecx, edx, edi, ebx, 5, 11, 0x6ed9eba1 + 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 + md4._.round md4._.h, ecx, edx, edi, ebx, 7, 11, 0x6ed9eba1 + md4._.round md4._.h, ebx, ecx, edx, edi, 15, 15, 0x6ed9eba1 + + mov eax, [_hash] + add [eax + 0x0], edi + add [eax + 0x4], ebx + add [eax + 0x8], ecx + add [eax + 0xc], edx + + ret +endp + + +proc md4.update _ctx, _msg, _size + mov ebx, [_ctx] + mov ecx, [_size] + add [ebx + ctx_md4.msglen_0], ecx + adc [ebx + ctx_md4.msglen_1], 0 + + .next_block: + mov ebx, [_ctx] + mov esi, [_msg] + mov eax, [ebx + ctx_md4.index] + and eax, MD4_BLOCK_SIZE-1 + jnz .copy_to_buf + test esi, MD4_ALIGN_MASK + jnz .copy_to_buf + .no_copy: + ; data is aligned, hash it in place without copying + mov ebx, [_ctx] + cmp [_size], MD4_BLOCK_SIZE + jb .copy_quit + lea eax, [ebx + ctx_md4.hash] + stdcall md4._.block, eax + sub [_size], MD4_BLOCK_SIZE + add esi, MD4_BLOCK_SIZE + jmp .no_copy + + .copy_to_buf: + lea edi, [ebx + ctx_md4.block] + add edi, eax + mov ecx, MD4_BLOCK_SIZE + sub ecx, eax + cmp [_size], ecx + jb .copy_quit + sub [_size], ecx + add [_msg], ecx + add [ebx + ctx_md4.index], ecx + rep movsb + lea eax, [ebx + ctx_md4.hash] + lea esi, [ebx + ctx_md4.block] + stdcall md4._.block, eax + jmp .next_block + + .copy_quit: + mov ebx, [_ctx] + lea edi, [ebx + ctx_md4.block] + mov eax, [ebx + ctx_md4.index] + and eax, MD4_BLOCK_SIZE-1 + add edi, eax + mov ecx, [_size] + add [ebx + ctx_md4.index], ecx + rep movsb + .quit: + ret +endp + + +proc md4.final _ctx + mov ebx, [_ctx] + lea edi, [ebx + ctx_md4.block] + mov ecx, [ebx + ctx_md4.msglen_0] + and ecx, MD4_BLOCK_SIZE-1 + add edi, ecx + mov byte[edi], 0x80 + inc edi + neg ecx + add ecx, MD4_BLOCK_SIZE + cmp ecx, 8 + ja .last + + dec ecx + xor eax, eax + rep stosb + lea esi, [ebx + ctx_md4.block] + lea eax, [ebx + ctx_md4.hash] + stdcall md4._.block, eax + mov ebx, [_ctx] + lea edi, [ebx + ctx_md4.block] + mov ecx, MD4_BLOCK_SIZE+1 + .last: + dec ecx + sub ecx, 8 + xor eax, eax + rep stosb + mov eax, [ebx + ctx_md4.msglen_0] + mov edx, [ebx + ctx_md4.msglen_1] + shld edx, eax, 3 + 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 + + +proc md4.oneshot _ctx, _data, _len + stdcall md4.init, [_ctx] + stdcall md4.update, [_ctx], [_data], [_len] + stdcall md4.final, [_ctx] + ret +endp + + +iglobal +align MD4_ALIGN +md4._.hash_init dd 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0 +endg diff --git a/programs/develop/libraries/libcrash/trunk/md5.asm b/programs/develop/libraries/libcrash/trunk/md5.asm index 3dc3f822ff..7925995c59 100644 --- a/programs/develop/libraries/libcrash/trunk/md5.asm +++ b/programs/develop/libraries/libcrash/trunk/md5.asm @@ -1,265 +1,291 @@ -; libcrash -- cryptographic hash functions -; -; Copyright (C) 2012-2013,2016 Ivan Baravy (dunkaist) -; -; 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 -; the Free Software Foundation, either version 3 of the License, or -; (at your option) any later version. -; -; This program is distributed in the hope that it will be useful, -; but WITHOUT ANY WARRANTY; without even the implied warranty of -; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -; GNU General Public License for more details. -; -; You should have received a copy of the GNU General Public License -; along with this program. If not, see . - - -macro md5._.f b, c, d -{ - push c - xor c, d - and b, c - xor b, d - pop c -} - -macro md5._.g b, c, d -{ - push c d - and b, d - not d - and c, d - or b, c - pop d c -} - -macro md5._.h b, c, d -{ - xor b, c - xor b, d -} - -macro md5._.i b, c, d -{ - push d - not d - or b, d - xor b, c - pop d -} - -macro md5._.round func, a, b, c, d, index, shift, ac -{ - push b - func b, c, d - lea a, [a + b + ac] - add a, [esi + index*4] - rol a, shift - pop b - add a, b -} - - -proc md5.init _ctx - mov ebx, [_ctx] - lea edi, [ebx + ctx_md5.hash] - mov esi, md5._.hash_init - mov ecx, MD5_HASH_SIZE/4 - rep movsd - xor eax, eax - mov [ebx + ctx_md5.index], eax - mov [ebx + ctx_md5.msglen_0], eax - mov [ebx + ctx_md5.msglen_1], eax - ret -endp - - -proc md5._.block _hash - - mov edi, [_hash] - mov eax, [edi + 0x0] - mov ebx, [edi + 0x4] - mov ecx, [edi + 0x8] - mov edx, [edi + 0xc] - - md5._.round md5._.f, eax, ebx, ecx, edx, 0, 7, 0xd76aa478 - md5._.round md5._.f, edx, eax, ebx, ecx, 1, 12, 0xe8c7b756 - 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._.f, eax, ebx, ecx, edx, 4, 7, 0xf57c0faf - md5._.round md5._.f, edx, eax, ebx, ecx, 5, 12, 0x4787c62a - md5._.round md5._.f, ecx, edx, eax, ebx, 6, 17, 0xa8304613 - md5._.round md5._.f, ebx, ecx, edx, eax, 7, 22, 0xfd469501 - md5._.round md5._.f, eax, ebx, ecx, edx, 8, 7, 0x698098d8 - md5._.round md5._.f, edx, eax, ebx, ecx, 9, 12, 0x8b44f7af - md5._.round md5._.f, ecx, edx, eax, ebx, 10, 17, 0xffff5bb1 - md5._.round md5._.f, ebx, ecx, edx, eax, 11, 22, 0x895cd7be - 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, ecx, edx, eax, ebx, 14, 17, 0xa679438e - md5._.round md5._.f, ebx, ecx, edx, eax, 15, 22, 0x49b40821 - - md5._.round md5._.g, eax, ebx, ecx, edx, 1, 5, 0xf61e2562 - md5._.round md5._.g, edx, eax, ebx, ecx, 6, 9, 0xc040b340 - 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._.g, eax, ebx, ecx, edx, 5, 5, 0xd62f105d - md5._.round md5._.g, edx, eax, ebx, ecx, 10, 9, 0x02441453 - md5._.round md5._.g, ecx, edx, eax, ebx, 15, 14, 0xd8a1e681 - md5._.round md5._.g, ebx, ecx, edx, eax, 4, 20, 0xe7d3fbc8 - md5._.round md5._.g, eax, ebx, ecx, edx, 9, 5, 0x21e1cde6 - md5._.round md5._.g, edx, eax, ebx, ecx, 14, 9, 0xc33707d6 - md5._.round md5._.g, ecx, edx, eax, ebx, 3, 14, 0xf4d50d87 - md5._.round md5._.g, ebx, ecx, edx, eax, 8, 20, 0x455a14ed - 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, ecx, edx, eax, ebx, 7, 14, 0x676f02d9 - md5._.round md5._.g, ebx, ecx, edx, eax, 12, 20, 0x8d2a4c8a - - md5._.round md5._.h, eax, ebx, ecx, edx, 5, 4, 0xfffa3942 - md5._.round md5._.h, edx, eax, ebx, ecx, 8, 11, 0x8771f681 - 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._.h, eax, ebx, ecx, edx, 1, 4, 0xa4beea44 - md5._.round md5._.h, edx, eax, ebx, ecx, 4, 11, 0x4bdecfa9 - md5._.round md5._.h, ecx, edx, eax, ebx, 7, 16, 0xf6bb4b60 - md5._.round md5._.h, ebx, ecx, edx, eax, 10, 23, 0xbebfbc70 - md5._.round md5._.h, eax, ebx, ecx, edx, 13, 4, 0x289b7ec6 - md5._.round md5._.h, edx, eax, ebx, ecx, 0, 11, 0xeaa127fa - md5._.round md5._.h, ecx, edx, eax, ebx, 3, 16, 0xd4ef3085 - md5._.round md5._.h, ebx, ecx, edx, eax, 6, 23, 0x04881d05 - 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, ecx, edx, eax, ebx, 15, 16, 0x1fa27cf8 - md5._.round md5._.h, ebx, ecx, edx, eax, 2, 23, 0xc4ac5665 - - md5._.round md5._.i, eax, ebx, ecx, edx, 0, 6, 0xf4292244 - md5._.round md5._.i, edx, eax, ebx, ecx, 7, 10, 0x432aff97 - md5._.round md5._.i, ecx, edx, eax, ebx, 14, 15, 0xab9423a7 - md5._.round md5._.i, ebx, ecx, edx, eax, 5, 21, 0xfc93a039 - md5._.round md5._.i, eax, ebx, ecx, edx, 12, 6, 0x655b59c3 - md5._.round md5._.i, edx, eax, ebx, ecx, 3, 10, 0x8f0ccc92 - md5._.round md5._.i, ecx, edx, eax, ebx, 10, 15, 0xffeff47d - md5._.round md5._.i, ebx, ecx, edx, eax, 1, 21, 0x85845dd1 - md5._.round md5._.i, eax, ebx, ecx, edx, 8, 6, 0x6fa87e4f - md5._.round md5._.i, edx, eax, ebx, ecx, 15, 10, 0xfe2ce6e0 - md5._.round md5._.i, ecx, edx, eax, ebx, 6, 15, 0xa3014314 - 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 - md5._.round md5._.i, ecx, edx, eax, ebx, 2, 15, 0x2ad7d2bb - md5._.round md5._.i, ebx, ecx, edx, eax, 9, 21, 0xeb86d391 - - mov edi, [_hash] - add [edi + 0x0], eax - add [edi + 0x4], ebx - add [edi + 0x8], ecx - add [edi + 0xc], edx - - ret -endp - - -proc md5.update _ctx, _msg, _size - mov ebx, [_ctx] - mov ecx, [_size] - add [ebx + ctx_md5.msglen_0], ecx - adc [ebx + ctx_md5.msglen_1], 0 - - .next_block: - mov ebx, [_ctx] - mov esi, [_msg] - mov eax, [ebx + ctx_md5.index] - and eax, MD5_BLOCK_SIZE-1 - jnz .copy_to_buf - test esi, MD5_ALIGN_MASK - jnz .copy_to_buf - .no_copy: - ; data is aligned, hash it in place without copying - mov ebx, [_ctx] - cmp [_size], MD5_BLOCK_SIZE - jb .copy_quit - lea eax, [ebx + ctx_md5.hash] - stdcall md5._.block, eax - sub [_size], MD5_BLOCK_SIZE - add esi, MD5_BLOCK_SIZE - jmp .no_copy - - .copy_to_buf: - lea edi, [ebx + ctx_md5.block] - add edi, eax - mov ecx, MD5_BLOCK_SIZE - sub ecx, eax - cmp [_size], ecx - jb .copy_quit - sub [_size], ecx - add [_msg], ecx - add [ebx + ctx_md5.index], ecx - rep movsb - lea eax, [ebx + ctx_md5.hash] - lea esi, [ebx + ctx_md5.block] - stdcall md5._.block, eax - jmp .next_block - - .copy_quit: - mov ebx, [_ctx] - lea edi, [ebx + ctx_md5.block] - mov eax, [ebx + ctx_md5.index] - and eax, MD5_BLOCK_SIZE-1 - add edi, eax - mov ecx, [_size] - add [ebx + ctx_md5.index], ecx - rep movsb - .quit: - - ret -endp - - -proc md5.final _ctx - mov ebx, [_ctx] - lea edi, [ebx + ctx_md5.block] - mov ecx, [ebx + ctx_md5.msglen_0] - and ecx, MD5_BLOCK_SIZE-1 - add edi, ecx - mov byte[edi], 0x80 - inc edi - neg ecx - add ecx, MD5_BLOCK_SIZE - cmp ecx, 8 - ja .last - - dec ecx - xor eax, eax - rep stosb - lea esi, [ebx + ctx_md5.block] - lea eax, [ebx + ctx_md5.hash] - stdcall md5._.block, eax - mov ebx, [_ctx] - lea edi, [ebx + ctx_md5.block] - mov ecx, MD5_BLOCK_SIZE+1 - .last: - dec ecx - sub ecx, 8 - xor eax, eax - rep stosb - mov eax, [ebx + ctx_md5.msglen_0] - mov edx, [ebx + ctx_md5.msglen_1] - shld edx, eax, 3 - 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 - +; libcrash -- cryptographic hash functions +; +; Copyright (C) 2012-2013,2016,2019 Ivan Baravy (dunkaist) +; +; 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 +; the Free Software Foundation, either version 3 of the License, or +; (at your option) any later version. +; +; This program is distributed in the hope that it will be useful, +; but WITHOUT ANY WARRANTY; without even the implied warranty of +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +; GNU General Public License for more details. +; +; You should have received a copy of the GNU General Public License +; along with this program. If not, see . + + +MD5_HASH_SIZE = 16 +MD5_BLOCK_SIZE = 64 + +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 + +if defined sizeof.crash_ctx + assert sizeof.crash_ctx >= sizeof.ctx_md5 +end if + +macro md5._.f b, c, d +{ + push c + xor c, d + and b, c + xor b, d + pop c +} + +macro md5._.g b, c, d +{ + push c d + and b, d + not d + and c, d + or b, c + pop d c +} + +macro md5._.h b, c, d +{ + xor b, c + xor b, d +} + +macro md5._.i b, c, d +{ + push d + not d + or b, d + xor b, c + pop d +} + +macro md5._.round func, a, b, c, d, index, shift, ac +{ + push b + func b, c, d + lea a, [a + b + ac] + add a, [esi + index*4] + rol a, shift + pop b + add a, b +} + + +proc md5.init _ctx + mov ebx, [_ctx] + lea edi, [ebx + ctx_md5.hash] + mov esi, md5._.hash_init + mov ecx, MD5_HASH_SIZE/4 + rep movsd + xor eax, eax + mov [ebx + ctx_md5.index], eax + mov [ebx + ctx_md5.msglen_0], eax + mov [ebx + ctx_md5.msglen_1], eax + ret +endp + + +proc md5._.block _hash + + mov edi, [_hash] + mov eax, [edi + 0x0] + mov ebx, [edi + 0x4] + mov ecx, [edi + 0x8] + mov edx, [edi + 0xc] + + md5._.round md5._.f, eax, ebx, ecx, edx, 0, 7, 0xd76aa478 + md5._.round md5._.f, edx, eax, ebx, ecx, 1, 12, 0xe8c7b756 + 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._.f, eax, ebx, ecx, edx, 4, 7, 0xf57c0faf + md5._.round md5._.f, edx, eax, ebx, ecx, 5, 12, 0x4787c62a + md5._.round md5._.f, ecx, edx, eax, ebx, 6, 17, 0xa8304613 + md5._.round md5._.f, ebx, ecx, edx, eax, 7, 22, 0xfd469501 + md5._.round md5._.f, eax, ebx, ecx, edx, 8, 7, 0x698098d8 + md5._.round md5._.f, edx, eax, ebx, ecx, 9, 12, 0x8b44f7af + md5._.round md5._.f, ecx, edx, eax, ebx, 10, 17, 0xffff5bb1 + md5._.round md5._.f, ebx, ecx, edx, eax, 11, 22, 0x895cd7be + 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, ecx, edx, eax, ebx, 14, 17, 0xa679438e + md5._.round md5._.f, ebx, ecx, edx, eax, 15, 22, 0x49b40821 + + md5._.round md5._.g, eax, ebx, ecx, edx, 1, 5, 0xf61e2562 + md5._.round md5._.g, edx, eax, ebx, ecx, 6, 9, 0xc040b340 + 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._.g, eax, ebx, ecx, edx, 5, 5, 0xd62f105d + md5._.round md5._.g, edx, eax, ebx, ecx, 10, 9, 0x02441453 + md5._.round md5._.g, ecx, edx, eax, ebx, 15, 14, 0xd8a1e681 + md5._.round md5._.g, ebx, ecx, edx, eax, 4, 20, 0xe7d3fbc8 + md5._.round md5._.g, eax, ebx, ecx, edx, 9, 5, 0x21e1cde6 + md5._.round md5._.g, edx, eax, ebx, ecx, 14, 9, 0xc33707d6 + md5._.round md5._.g, ecx, edx, eax, ebx, 3, 14, 0xf4d50d87 + md5._.round md5._.g, ebx, ecx, edx, eax, 8, 20, 0x455a14ed + 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, ecx, edx, eax, ebx, 7, 14, 0x676f02d9 + md5._.round md5._.g, ebx, ecx, edx, eax, 12, 20, 0x8d2a4c8a + + md5._.round md5._.h, eax, ebx, ecx, edx, 5, 4, 0xfffa3942 + md5._.round md5._.h, edx, eax, ebx, ecx, 8, 11, 0x8771f681 + 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._.h, eax, ebx, ecx, edx, 1, 4, 0xa4beea44 + md5._.round md5._.h, edx, eax, ebx, ecx, 4, 11, 0x4bdecfa9 + md5._.round md5._.h, ecx, edx, eax, ebx, 7, 16, 0xf6bb4b60 + md5._.round md5._.h, ebx, ecx, edx, eax, 10, 23, 0xbebfbc70 + md5._.round md5._.h, eax, ebx, ecx, edx, 13, 4, 0x289b7ec6 + md5._.round md5._.h, edx, eax, ebx, ecx, 0, 11, 0xeaa127fa + md5._.round md5._.h, ecx, edx, eax, ebx, 3, 16, 0xd4ef3085 + md5._.round md5._.h, ebx, ecx, edx, eax, 6, 23, 0x04881d05 + 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, ecx, edx, eax, ebx, 15, 16, 0x1fa27cf8 + md5._.round md5._.h, ebx, ecx, edx, eax, 2, 23, 0xc4ac5665 + + md5._.round md5._.i, eax, ebx, ecx, edx, 0, 6, 0xf4292244 + md5._.round md5._.i, edx, eax, ebx, ecx, 7, 10, 0x432aff97 + md5._.round md5._.i, ecx, edx, eax, ebx, 14, 15, 0xab9423a7 + md5._.round md5._.i, ebx, ecx, edx, eax, 5, 21, 0xfc93a039 + md5._.round md5._.i, eax, ebx, ecx, edx, 12, 6, 0x655b59c3 + md5._.round md5._.i, edx, eax, ebx, ecx, 3, 10, 0x8f0ccc92 + md5._.round md5._.i, ecx, edx, eax, ebx, 10, 15, 0xffeff47d + md5._.round md5._.i, ebx, ecx, edx, eax, 1, 21, 0x85845dd1 + md5._.round md5._.i, eax, ebx, ecx, edx, 8, 6, 0x6fa87e4f + md5._.round md5._.i, edx, eax, ebx, ecx, 15, 10, 0xfe2ce6e0 + md5._.round md5._.i, ecx, edx, eax, ebx, 6, 15, 0xa3014314 + 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 + md5._.round md5._.i, ecx, edx, eax, ebx, 2, 15, 0x2ad7d2bb + md5._.round md5._.i, ebx, ecx, edx, eax, 9, 21, 0xeb86d391 + + mov edi, [_hash] + add [edi + 0x0], eax + add [edi + 0x4], ebx + add [edi + 0x8], ecx + add [edi + 0xc], edx + + ret +endp + + +proc md5.update _ctx, _msg, _size + mov ebx, [_ctx] + mov ecx, [_size] + add [ebx + ctx_md5.msglen_0], ecx + adc [ebx + ctx_md5.msglen_1], 0 + + .next_block: + mov ebx, [_ctx] + mov esi, [_msg] + mov eax, [ebx + ctx_md5.index] + and eax, MD5_BLOCK_SIZE-1 + jnz .copy_to_buf + test esi, MD5_ALIGN_MASK + jnz .copy_to_buf + .no_copy: + ; data is aligned, hash it in place without copying + mov ebx, [_ctx] + cmp [_size], MD5_BLOCK_SIZE + jb .copy_quit + lea eax, [ebx + ctx_md5.hash] + stdcall md5._.block, eax + sub [_size], MD5_BLOCK_SIZE + add esi, MD5_BLOCK_SIZE + jmp .no_copy + + .copy_to_buf: + lea edi, [ebx + ctx_md5.block] + add edi, eax + mov ecx, MD5_BLOCK_SIZE + sub ecx, eax + cmp [_size], ecx + jb .copy_quit + sub [_size], ecx + add [_msg], ecx + add [ebx + ctx_md5.index], ecx + rep movsb + lea eax, [ebx + ctx_md5.hash] + lea esi, [ebx + ctx_md5.block] + stdcall md5._.block, eax + jmp .next_block + + .copy_quit: + mov ebx, [_ctx] + lea edi, [ebx + ctx_md5.block] + mov eax, [ebx + ctx_md5.index] + and eax, MD5_BLOCK_SIZE-1 + add edi, eax + mov ecx, [_size] + add [ebx + ctx_md5.index], ecx + rep movsb + .quit: + + ret +endp + + +proc md5.final _ctx + mov ebx, [_ctx] + lea edi, [ebx + ctx_md5.block] + mov ecx, [ebx + ctx_md5.msglen_0] + and ecx, MD5_BLOCK_SIZE-1 + add edi, ecx + mov byte[edi], 0x80 + inc edi + neg ecx + add ecx, MD5_BLOCK_SIZE + cmp ecx, 8 + ja .last + + dec ecx + xor eax, eax + rep stosb + lea esi, [ebx + ctx_md5.block] + lea eax, [ebx + ctx_md5.hash] + stdcall md5._.block, eax + mov ebx, [_ctx] + lea edi, [ebx + ctx_md5.block] + mov ecx, MD5_BLOCK_SIZE+1 + .last: + dec ecx + sub ecx, 8 + xor eax, eax + rep stosb + mov eax, [ebx + ctx_md5.msglen_0] + mov edx, [ebx + ctx_md5.msglen_1] + shld edx, eax, 3 + 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 + + +proc md5.oneshot _ctx, _data, _len + stdcall md5.init, [_ctx] + stdcall md5.update, [_ctx], [_data], [_len] + stdcall md5.final, [_ctx] + ret +endp + + +iglobal +align MD5_ALIGN +md5._.hash_init dd 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0 +endg diff --git a/programs/develop/libraries/libcrash/trunk/sha1.asm b/programs/develop/libraries/libcrash/trunk/sha1.asm index 93d4414cad..4031929d13 100644 --- a/programs/develop/libraries/libcrash/trunk/sha1.asm +++ b/programs/develop/libraries/libcrash/trunk/sha1.asm @@ -1,273 +1,299 @@ -; libcrash -- cryptographic hash functions -; -; Copyright (C) 2012-2013,2016 Ivan Baravy (dunkaist) -; -; 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 -; the Free Software Foundation, either version 3 of the License, or -; (at your option) any later version. -; -; This program is distributed in the hope that it will be useful, -; but WITHOUT ANY WARRANTY; without even the implied warranty of -; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -; GNU General Public License for more details. -; -; You should have received a copy of the GNU General Public License -; along with this program. If not, see . - - -proc sha1._.f - push ebx ecx edx - xor ecx, edx - and ebx, ecx - xor ebx, edx - mov esi, ebx - pop edx ecx ebx - ret -endp - -proc sha1._.g - push ebx ecx edx - xor ebx, ecx - xor ebx, edx - mov esi, ebx - pop edx ecx ebx - ret -endp - -proc sha1._.h - push ebx ecx edx - mov esi, ebx - and ebx, ecx - and ecx, edx - and esi, edx - or ebx, ecx - or esi, ebx - pop edx ecx ebx - ret -endp - -macro sha1._.round f, k, c -{ - mov esi, eax - rol esi, 5 - mov [temp], esi - call f - - add esi, edi - add [temp], esi - mov esi, [w + (c)*4] - add esi, k - add [temp], esi - - mov edi, edx - mov edx, ecx - mov ecx, ebx - rol ecx, 30 - mov ebx, eax - mov eax, [temp] -} - - -proc sha1.init _ctx - mov ebx, [_ctx] - lea edi, [ebx + ctx_sha1.hash] - mov esi, sha1._.hash_init - mov ecx, SHA1_HASH_SIZE/4 - rep movsd - xor eax, eax - mov [ebx + ctx_sha1.index], eax - mov [ebx + ctx_sha1.msglen_0], eax - mov [ebx + ctx_sha1.msglen_1], eax - ret -endp - - -proc sha1._.block _hash -locals - temp rd 1 - w rd 80 -endl - lea edi, [w] - xor ecx, ecx - @@: - mov eax, [esi] - add esi, 4 - bswap eax - mov [edi], eax - add edi, 4 - add ecx, 1 - cmp ecx, 16 - jne @b - @@: - mov eax, [w + (ecx - 3)*4] - xor eax, [w + (ecx - 8)*4] - xor eax, [w + (ecx - 14)*4] - xor eax, [w + (ecx - 16)*4] - rol eax, 1 - mov [w + ecx*4], eax - add ecx, 1 - cmp ecx, 80 - jne @b - - mov edi, [_hash] - mov eax, [edi + 0x00] - mov ebx, [edi + 0x04] - mov ecx, [edi + 0x08] - mov edx, [edi + 0x0c] - mov edi, [edi + 0x10] - - push esi - -repeat 20 - sha1._.round sha1._.f, 0x5a827999, %-1 -end repeat - -repeat 20 - sha1._.round sha1._.g, 0x6ed9eba1, %-1+20 -end repeat - -repeat 20 - sha1._.round sha1._.h, 0x8f1bbcdc, %-1+40 -end repeat - -repeat 20 - sha1._.round sha1._.g, 0xca62c1d6, %-1+60 -end repeat - - pop esi - - mov [temp], edi - mov edi, [_hash] - add [edi + 0x00], eax - add [edi + 0x04], ebx - add [edi + 0x08], ecx - add [edi + 0x0c], edx - mov eax, [temp] - add [edi + 0x10], eax - - ret -endp - - -proc sha1.update _ctx, _msg, _size - mov ebx, [_ctx] - mov ecx, [_size] - add [ebx + ctx_sha1.msglen_0], ecx - adc [ebx + ctx_sha1.msglen_1], 0 - - .next_block: - mov ebx, [_ctx] - mov esi, [_msg] - mov eax, [ebx + ctx_sha1.index] - and eax, SHA1_BLOCK_SIZE-1 - jnz .copy_to_buf - test esi, SHA1_ALIGN_MASK - jnz .copy_to_buf - .no_copy: - ; data is aligned, hash it in place without copying - mov ebx, [_ctx] - cmp [_size], SHA1_BLOCK_SIZE - jb .copy_quit - lea eax, [ebx + ctx_sha1.hash] - stdcall sha1._.block, eax - sub [_size], SHA1_BLOCK_SIZE -; add esi, SHA1_BLOCK_SIZE ; FIXME - jmp .no_copy - - .copy_to_buf: - lea edi, [ebx + ctx_sha1.block] - add edi, eax - mov ecx, SHA1_BLOCK_SIZE - sub ecx, eax - cmp [_size], ecx - jb .copy_quit - sub [_size], ecx - add [_msg], ecx - add [ebx + ctx_sha1.index], ecx - rep movsb - lea eax, [ebx + ctx_sha1.hash] - lea esi, [ebx + ctx_sha1.block] - stdcall sha1._.block, eax - jmp .next_block - - .copy_quit: - mov ebx, [_ctx] - lea edi, [ebx + ctx_sha1.block] - mov eax, [ebx + ctx_sha1.index] - and eax, SHA1_BLOCK_SIZE-1 - add edi, eax - mov ecx, [_size] - add [ebx + ctx_sha1.index], ecx - rep movsb - .quit: - - ret -endp - - -proc sha1.final _ctx - mov ebx, [_ctx] - lea edi, [ebx + ctx_sha1.block] - mov ecx, [ebx + ctx_sha1.msglen_0] - and ecx, SHA1_BLOCK_SIZE-1 - add edi, ecx - mov byte[edi], 0x80 - inc edi - neg ecx - add ecx, SHA1_BLOCK_SIZE - cmp ecx, 8 - ja .last - - dec ecx - xor eax, eax - rep stosb - lea esi, [ebx + ctx_sha1.block] - lea eax, [ebx + ctx_sha1.hash] - stdcall sha1._.block, eax - mov ebx, [_ctx] - lea edi, [ebx + ctx_sha1.block] - mov ecx, SHA1_BLOCK_SIZE+1 - .last: - dec ecx - sub ecx, 8 - xor eax, eax - rep stosb - mov eax, [ebx + ctx_sha1.msglen_0] - mov edx, [ebx + ctx_sha1.msglen_1] - shld edx, eax, 3 - shl eax, 3 - bswap eax - bswap edx - mov dword[edi], edx - mov dword[edi+4], eax - lea esi, [ebx + ctx_sha1.block] - lea eax, [ebx + ctx_sha1.hash] - stdcall sha1._.block, eax - - mov ebx, [_ctx] - lea eax, [ebx + ctx_sha1.hash] - stdcall sha1._.postprocess, ebx, eax - - ret -endp - - -proc sha1._.postprocess _ctx, _hash - mov ecx, 5 - mov esi, [_hash] - 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 - +; libcrash -- cryptographic hash functions +; +; Copyright (C) 2012-2013,2016,2019 Ivan Baravy (dunkaist) +; +; 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 +; the Free Software Foundation, either version 3 of the License, or +; (at your option) any later version. +; +; This program is distributed in the hope that it will be useful, +; but WITHOUT ANY WARRANTY; without even the implied warranty of +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +; GNU General Public License for more details. +; +; You should have received a copy of the GNU General Public License +; along with this program. If not, see . + + +SHA1_HASH_SIZE = 20 +SHA1_BLOCK_SIZE = 64 + +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 + +if defined sizeof.crash_ctx + assert sizeof.crash_ctx >= sizeof.ctx_sha1 +end if + +proc sha1._.f + push ebx ecx edx + xor ecx, edx + and ebx, ecx + xor ebx, edx + mov esi, ebx + pop edx ecx ebx + ret +endp + +proc sha1._.g + push ebx ecx edx + xor ebx, ecx + xor ebx, edx + mov esi, ebx + pop edx ecx ebx + ret +endp + +proc sha1._.h + push ebx ecx edx + mov esi, ebx + and ebx, ecx + and ecx, edx + and esi, edx + or ebx, ecx + or esi, ebx + pop edx ecx ebx + ret +endp + +macro sha1._.round f, k, c +{ + mov esi, eax + rol esi, 5 + mov [temp], esi + call f + + add esi, edi + add [temp], esi + mov esi, [w + (c)*4] + add esi, k + add [temp], esi + + mov edi, edx + mov edx, ecx + mov ecx, ebx + rol ecx, 30 + mov ebx, eax + mov eax, [temp] +} + + +proc sha1.init _ctx + mov ebx, [_ctx] + lea edi, [ebx + ctx_sha1.hash] + mov esi, sha1._.hash_init + mov ecx, SHA1_HASH_SIZE/4 + rep movsd + xor eax, eax + mov [ebx + ctx_sha1.index], eax + mov [ebx + ctx_sha1.msglen_0], eax + mov [ebx + ctx_sha1.msglen_1], eax + ret +endp + + +proc sha1._.block _hash +locals + temp rd 1 + w rd 80 +endl + lea edi, [w] + xor ecx, ecx + @@: + mov eax, [esi] + add esi, 4 + bswap eax + mov [edi], eax + add edi, 4 + add ecx, 1 + cmp ecx, 16 + jne @b + @@: + mov eax, [w + (ecx - 3)*4] + xor eax, [w + (ecx - 8)*4] + xor eax, [w + (ecx - 14)*4] + xor eax, [w + (ecx - 16)*4] + rol eax, 1 + mov [w + ecx*4], eax + add ecx, 1 + cmp ecx, 80 + jne @b + + mov edi, [_hash] + mov eax, [edi + 0x00] + mov ebx, [edi + 0x04] + mov ecx, [edi + 0x08] + mov edx, [edi + 0x0c] + mov edi, [edi + 0x10] + + push esi + +repeat 20 + sha1._.round sha1._.f, 0x5a827999, %-1 +end repeat + +repeat 20 + sha1._.round sha1._.g, 0x6ed9eba1, %-1+20 +end repeat + +repeat 20 + sha1._.round sha1._.h, 0x8f1bbcdc, %-1+40 +end repeat + +repeat 20 + sha1._.round sha1._.g, 0xca62c1d6, %-1+60 +end repeat + + pop esi + + mov [temp], edi + mov edi, [_hash] + add [edi + 0x00], eax + add [edi + 0x04], ebx + add [edi + 0x08], ecx + add [edi + 0x0c], edx + mov eax, [temp] + add [edi + 0x10], eax + + ret +endp + + +proc sha1.update _ctx, _msg, _size + mov ebx, [_ctx] + mov ecx, [_size] + add [ebx + ctx_sha1.msglen_0], ecx + adc [ebx + ctx_sha1.msglen_1], 0 + + .next_block: + mov ebx, [_ctx] + mov esi, [_msg] + mov eax, [ebx + ctx_sha1.index] + and eax, SHA1_BLOCK_SIZE-1 + jnz .copy_to_buf + test esi, SHA1_ALIGN_MASK + jnz .copy_to_buf + .no_copy: + ; data is aligned, hash it in place without copying + mov ebx, [_ctx] + cmp [_size], SHA1_BLOCK_SIZE + jb .copy_quit + lea eax, [ebx + ctx_sha1.hash] + stdcall sha1._.block, eax + sub [_size], SHA1_BLOCK_SIZE +; add esi, SHA1_BLOCK_SIZE ; FIXME + jmp .no_copy + + .copy_to_buf: + lea edi, [ebx + ctx_sha1.block] + add edi, eax + mov ecx, SHA1_BLOCK_SIZE + sub ecx, eax + cmp [_size], ecx + jb .copy_quit + sub [_size], ecx + add [_msg], ecx + add [ebx + ctx_sha1.index], ecx + rep movsb + lea eax, [ebx + ctx_sha1.hash] + lea esi, [ebx + ctx_sha1.block] + stdcall sha1._.block, eax + jmp .next_block + + .copy_quit: + mov ebx, [_ctx] + lea edi, [ebx + ctx_sha1.block] + mov eax, [ebx + ctx_sha1.index] + and eax, SHA1_BLOCK_SIZE-1 + add edi, eax + mov ecx, [_size] + add [ebx + ctx_sha1.index], ecx + rep movsb + .quit: + + ret +endp + + +proc sha1.final _ctx + mov ebx, [_ctx] + lea edi, [ebx + ctx_sha1.block] + mov ecx, [ebx + ctx_sha1.msglen_0] + and ecx, SHA1_BLOCK_SIZE-1 + add edi, ecx + mov byte[edi], 0x80 + inc edi + neg ecx + add ecx, SHA1_BLOCK_SIZE + cmp ecx, 8 + ja .last + + dec ecx + xor eax, eax + rep stosb + lea esi, [ebx + ctx_sha1.block] + lea eax, [ebx + ctx_sha1.hash] + stdcall sha1._.block, eax + mov ebx, [_ctx] + lea edi, [ebx + ctx_sha1.block] + mov ecx, SHA1_BLOCK_SIZE+1 + .last: + dec ecx + sub ecx, 8 + xor eax, eax + rep stosb + mov eax, [ebx + ctx_sha1.msglen_0] + mov edx, [ebx + ctx_sha1.msglen_1] + shld edx, eax, 3 + shl eax, 3 + bswap eax + bswap edx + mov dword[edi], edx + mov dword[edi+4], eax + lea esi, [ebx + ctx_sha1.block] + lea eax, [ebx + ctx_sha1.hash] + stdcall sha1._.block, eax + + mov ebx, [_ctx] + lea eax, [ebx + ctx_sha1.hash] + stdcall sha1._.postprocess, ebx, eax + + ret +endp + + +proc sha1._.postprocess _ctx, _hash + mov ecx, 5 + mov esi, [_hash] + mov edi, esi + @@: + lodsd + bswap eax + stosd + dec ecx + jnz @b + ret +endp + + +proc sha1.oneshot _ctx, _data, _len + stdcall sha1.init, [_ctx] + stdcall sha1.update, [_ctx], [_data], [_len] + stdcall sha1.final, [_ctx] + ret +endp + + +iglobal +align SHA1_ALIGN +sha1._.hash_init dd 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0 +endg diff --git a/programs/develop/libraries/libcrash/trunk/sha224_256.asm b/programs/develop/libraries/libcrash/trunk/sha224_256.asm index 2a9e04f5e3..d68eec268a 100644 --- a/programs/develop/libraries/libcrash/trunk/sha224_256.asm +++ b/programs/develop/libraries/libcrash/trunk/sha224_256.asm @@ -1,399 +1,441 @@ -; libcrash -- cryptographic hash functions -; -; Copyright (C) 2012-2013,2016 Ivan Baravy (dunkaist) -; -; 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 -; the Free Software Foundation, either version 3 of the License, or -; (at your option) any later version. -; -; This program is distributed in the hope that it will be useful, -; but WITHOUT ANY WARRANTY; without even the implied warranty of -; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -; GNU General Public License for more details. -; -; You should have received a copy of the GNU General Public License -; along with this program. If not, see . - - -macro sha224256._.chn x, y, z -{ - mov eax, [y] - xor eax, [z] - and eax, [x] - xor eax, [z] -} - -macro sha224256._.maj x, y, z -{ - mov eax, [x] - xor eax, [y] - and eax, [z] - mov ecx, [x] - and ecx, [y] - xor eax, ecx -} - -macro sha224256._.Sigma0 x -{ - mov eax, x - mov ecx, eax - ror ecx, 2 - ror eax, 13 - xor eax, ecx - mov ecx, x - ror ecx, 22 - xor eax, ecx -} - -macro sha224256._.Sigma1 x -{ - mov eax, x - mov ecx, eax - ror ecx, 6 - ror eax, 11 - xor eax, ecx - mov ecx, x - ror ecx, 25 - xor eax, ecx -} - -macro sha224256._.sigma0 x -{ - mov eax, x - mov ecx, eax - ror ecx, 7 - ror eax, 18 - xor eax, ecx - mov ecx, x - shr ecx, 3 - xor eax, ecx -} - -macro sha224256._.sigma1 x -{ - mov eax, x - mov ecx, eax - ror ecx, 17 - ror eax, 19 - xor eax, ecx - mov ecx, x - shr ecx, 10 - xor eax, ecx -} - -macro sha224256._.recalculate_w n -{ - mov edx, [w + ((n-2) and 15)*4] - sha224256._.sigma1 edx - add eax, [w + ((n-7) and 15)*4] - push eax - mov edx, [w + ((n-15) and 15)*4] - sha224256._.sigma0 edx - pop ecx - add eax, ecx - add [w + (n)*4], eax -} - -macro sha224256._.round a, b, c, d, e, f, g, h, k -{ - mov ebx, [h] - mov edx, [e] - sha224256._.Sigma1 edx - - add ebx, eax - sha224256._.chn e, f, g - - add ebx, eax - add ebx, [k] - add ebx, edi - - add [d], ebx - - mov edx, [a] - sha224256._.Sigma0 edx - add ebx, eax - sha224256._.maj a, b, c - add eax, ebx - mov [h], eax -} - - -macro sha224256._.round_1_16 a, b, c, d, e, f, g, h, n -{ - - mov eax, [esi + (n)*4] - bswap eax - - mov dword[w + (n)*4], eax - mov edi, eax - sha224256._.round a, b, c, d, e, f, g, h, (sha256_table + (n)*4) -} - -macro sha224256._.round_17_64 a, b, c, d, e, f, g, h, n, rep_num -{ - 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) -} - - -proc sha224.init _ctx - mov ebx, [_ctx] - lea edi, [ebx + ctx_sha224256.hash] - mov esi, sha224._.hash_init - mov ecx, SHA224256_INIT_SIZE/4 - rep movsd - xor eax, eax - mov [ebx + ctx_sha224256.index], eax - mov [ebx + ctx_sha224256.msglen_0], eax - mov [ebx + ctx_sha224256.msglen_1], eax - ret -endp - - -proc sha256.init _ctx - mov ebx, [_ctx] - lea edi, [ebx + ctx_sha224256.hash] - mov esi, sha256._.hash_init - mov ecx, SHA224256_INIT_SIZE/4 - rep movsd - xor eax, eax - mov [ebx + ctx_sha224256.index], eax - mov [ebx + ctx_sha224256.msglen_0], eax - mov [ebx + ctx_sha224256.msglen_1], eax - ret -endp - - -proc sha224256._.block _hash -locals - w rd 64 - A rd 1 - B rd 1 - C rd 1 - D rd 1 - E rd 1 - F rd 1 - G rd 1 - H rd 1 -endl - mov edi, [_hash] - mov eax, [edi + 0x00] - mov [A], eax - mov eax, [edi + 0x04] - mov [B], eax - mov eax, [edi + 0x08] - mov [C], eax - mov eax, [edi + 0x0c] - mov [D], eax - mov eax, [edi + 0x10] - mov [E], eax - mov eax, [edi + 0x14] - mov [F], eax - mov eax, [edi + 0x18] - mov [G], eax - mov eax, [edi + 0x1c] - mov [H], eax - - 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 - sha224256._.round_1_16 G, H, A, B, C, D, E, F, 2 - sha224256._.round_1_16 F, G, H, A, B, C, D, E, 3 - sha224256._.round_1_16 E, F, G, H, A, B, C, D, 4 - sha224256._.round_1_16 D, E, F, G, H, A, B, C, 5 - sha224256._.round_1_16 C, D, E, F, G, H, A, B, 6 - sha224256._.round_1_16 B, C, D, E, F, G, H, A, 7 - sha224256._.round_1_16 A, B, C, D, E, F, G, H, 8 - sha224256._.round_1_16 H, A, B, C, D, E, F, G, 9 - sha224256._.round_1_16 G, H, A, B, C, D, E, F, 10 - sha224256._.round_1_16 F, G, H, A, B, C, D, E, 11 - sha224256._.round_1_16 E, F, G, H, A, B, C, D, 12 - sha224256._.round_1_16 D, E, F, G, H, A, B, C, 13 - 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 - -repeat 3 - sha224256._.round_17_64 A, B, C, D, E, F, G, H, 0, % - 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, % - sha224256._.round_17_64 F, G, H, A, B, C, D, E, 3, % - sha224256._.round_17_64 E, F, G, H, A, B, C, D, 4, % - sha224256._.round_17_64 D, E, F, G, H, A, B, C, 5, % - sha224256._.round_17_64 C, D, E, F, G, H, A, B, 6, % - sha224256._.round_17_64 B, C, D, E, F, G, H, A, 7, % - sha224256._.round_17_64 A, B, C, D, E, F, G, H, 8, % - sha224256._.round_17_64 H, A, B, C, D, E, F, G, 9, % - sha224256._.round_17_64 G, H, A, B, C, D, E, F, 10, % - sha224256._.round_17_64 F, G, H, A, B, C, D, E, 11, % - sha224256._.round_17_64 E, F, G, H, A, B, C, D, 12, % - 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, % - sha224256._.round_17_64 B, C, D, E, F, G, H, A, 15, % -end repeat - - mov edi, [_hash] - mov eax, [A] - add [edi + 0x00], eax - mov eax, [B] - add [edi + 0x04], eax - mov eax, [C] - add [edi + 0x08], eax - mov eax, [D] - add [edi + 0x0c], eax - mov eax, [E] - add [edi + 0x10], eax - mov eax, [F] - add [edi + 0x14], eax - mov eax, [G] - add [edi + 0x18], eax - mov eax, [H] - add [edi + 0x1c], eax - - ret -endp - -sha256.update = sha224.update -proc sha224.update _ctx, _msg, _size - mov ebx, [_ctx] - mov ecx, [_size] - add [ebx + ctx_sha224256.msglen_0], ecx - adc [ebx + ctx_sha224256.msglen_1], 0 - - .next_block: - mov ebx, [_ctx] - mov esi, [_msg] - mov eax, [ebx + ctx_sha224256.index] - and eax, SHA224256_BLOCK_SIZE-1 - jnz .copy_to_buf - test esi, SHA224256_ALIGN_MASK - jnz .copy_to_buf - .no_copy: - ; data is aligned, hash it in place without copying - mov ebx, [_ctx] - cmp [_size], SHA224256_BLOCK_SIZE - jb .copy_quit - lea eax, [ebx + ctx_sha224256.hash] - stdcall sha224256._.block, eax - sub [_size], SHA224256_BLOCK_SIZE - add esi, SHA224256_BLOCK_SIZE ; FIXME - jmp .no_copy - - .copy_to_buf: - lea edi, [ebx + ctx_sha224256.block] - add edi, eax - mov ecx, SHA224256_BLOCK_SIZE - sub ecx, eax - cmp [_size], ecx - jb .copy_quit - sub [_size], ecx - add [_msg], ecx - add [ebx + ctx_sha224256.index], ecx - rep movsb - lea eax, [ebx + ctx_sha224256.hash] - lea esi, [ebx + ctx_sha224256.block] - stdcall sha224256._.block, eax - jmp .next_block - - .copy_quit: - mov ebx, [_ctx] - lea edi, [ebx + ctx_sha224256.block] - mov eax, [ebx + ctx_sha224256.index] - and eax, SHA224256_BLOCK_SIZE-1 - add edi, eax - mov ecx, [_size] - add [ebx + ctx_sha224256.index], ecx - rep movsb - .quit: - - ret -endp - - -sha256.final = sha224.final -proc sha224.final _ctx - mov ebx, [_ctx] - lea edi, [ebx + ctx_sha224256.block] - mov ecx, [ebx + ctx_sha224256.msglen_0] - and ecx, SHA224256_BLOCK_SIZE-1 - add edi, ecx - mov byte[edi], 0x80 - inc edi - neg ecx - add ecx, SHA224256_BLOCK_SIZE - cmp ecx, 8 - ja .last - - dec ecx - xor eax, eax - rep stosb - lea esi, [ebx + ctx_sha224256.block] - lea eax, [ebx + ctx_sha224256.hash] - stdcall sha224256._.block, eax - mov ebx, [_ctx] - lea edi, [ebx + ctx_sha224256.block] - mov ecx, SHA224256_BLOCK_SIZE+1 - .last: - dec ecx - sub ecx, 8 - xor eax, eax - rep stosb - mov eax, [ebx + ctx_sha224256.msglen_0] - mov edx, [ebx + ctx_sha224256.msglen_1] - shld edx, eax, 3 - shl eax, 3 - bswap eax - bswap edx - mov dword[edi], edx - mov dword[edi+4], eax - lea esi, [ebx + ctx_sha224256.block] - lea eax, [ebx + ctx_sha224256.hash] - stdcall sha224256._.block, eax - - mov ebx, [_ctx] - lea eax, [ebx + ctx_sha224256.hash] - stdcall sha224256._.postprocess, ebx, eax - - ret -endp - - -proc sha224256._.postprocess _ctx, _hash - mov ecx, 8 - mov esi, [_hash] - mov edi, esi - @@: - lodsd - bswap eax - stosd - dec ecx - jnz @b - ret -endp - - -align SHA224256_ALIGN - -sha224._.hash_init dd 0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939,\ - 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4 - -sha256._.hash_init dd 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,\ - 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 - -sha256_table dd 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,\ - 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 - +; libcrash -- cryptographic hash functions +; +; Copyright (C) 2012-2013,2016,2019 Ivan Baravy (dunkaist) +; +; 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 +; the Free Software Foundation, either version 3 of the License, or +; (at your option) any later version. +; +; This program is distributed in the hope that it will be useful, +; but WITHOUT ANY WARRANTY; without even the implied warranty of +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +; GNU General Public License for more details. +; +; You should have received a copy of the GNU General Public License +; along with this program. If not, see . + + +SHA224256_BLOCK_SIZE = 64 +SHA224_BLOCK_SIZE = SHA224256_BLOCK_SIZE +SHA256_BLOCK_SIZE = SHA224256_BLOCK_SIZE +SHA224_HASH_SIZE = 28 +SHA256_HASH_SIZE = 32 + +SHA224256_INIT_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 + +if defined sizeof.crash_ctx + assert sizeof.crash_ctx >= sizeof.ctx_sha224256 +end if + + +macro sha224256._.chn x, y, z +{ + mov eax, [y] + xor eax, [z] + and eax, [x] + xor eax, [z] +} + +macro sha224256._.maj x, y, z +{ + mov eax, [x] + xor eax, [y] + and eax, [z] + mov ecx, [x] + and ecx, [y] + xor eax, ecx +} + +macro sha224256._.Sigma0 x +{ + mov eax, x + mov ecx, eax + ror ecx, 2 + ror eax, 13 + xor eax, ecx + mov ecx, x + ror ecx, 22 + xor eax, ecx +} + +macro sha224256._.Sigma1 x +{ + mov eax, x + mov ecx, eax + ror ecx, 6 + ror eax, 11 + xor eax, ecx + mov ecx, x + ror ecx, 25 + xor eax, ecx +} + +macro sha224256._.sigma0 x +{ + mov eax, x + mov ecx, eax + ror ecx, 7 + ror eax, 18 + xor eax, ecx + mov ecx, x + shr ecx, 3 + xor eax, ecx +} + +macro sha224256._.sigma1 x +{ + mov eax, x + mov ecx, eax + ror ecx, 17 + ror eax, 19 + xor eax, ecx + mov ecx, x + shr ecx, 10 + xor eax, ecx +} + +macro sha224256._.recalculate_w n +{ + mov edx, [w + ((n-2) and 15)*4] + sha224256._.sigma1 edx + add eax, [w + ((n-7) and 15)*4] + push eax + mov edx, [w + ((n-15) and 15)*4] + sha224256._.sigma0 edx + pop ecx + add eax, ecx + add [w + (n)*4], eax +} + +macro sha224256._.round a, b, c, d, e, f, g, h, k +{ + mov ebx, [h] + mov edx, [e] + sha224256._.Sigma1 edx + + add ebx, eax + sha224256._.chn e, f, g + + add ebx, eax + add ebx, [k] + add ebx, edi + + add [d], ebx + + mov edx, [a] + sha224256._.Sigma0 edx + add ebx, eax + sha224256._.maj a, b, c + add eax, ebx + mov [h], eax +} + + +macro sha224256._.round_1_16 a, b, c, d, e, f, g, h, n +{ + + mov eax, [esi + (n)*4] + bswap eax + + mov dword[w + (n)*4], eax + mov edi, eax + sha224256._.round a, b, c, d, e, f, g, h, (sha256_table + (n)*4) +} + +macro sha224256._.round_17_64 a, b, c, d, e, f, g, h, n, rep_num +{ + 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) +} + + +proc sha224.init _ctx + mov ebx, [_ctx] + lea edi, [ebx + ctx_sha224256.hash] + mov esi, sha224._.hash_init + mov ecx, SHA224256_INIT_SIZE/4 + rep movsd + xor eax, eax + mov [ebx + ctx_sha224256.index], eax + mov [ebx + ctx_sha224256.msglen_0], eax + mov [ebx + ctx_sha224256.msglen_1], eax + ret +endp + + +proc sha256.init _ctx + mov ebx, [_ctx] + lea edi, [ebx + ctx_sha224256.hash] + mov esi, sha256._.hash_init + mov ecx, SHA224256_INIT_SIZE/4 + rep movsd + xor eax, eax + mov [ebx + ctx_sha224256.index], eax + mov [ebx + ctx_sha224256.msglen_0], eax + mov [ebx + ctx_sha224256.msglen_1], eax + ret +endp + + +proc sha224256._.block _hash +locals + w rd 64 + A rd 1 + B rd 1 + C rd 1 + D rd 1 + E rd 1 + F rd 1 + G rd 1 + H rd 1 +endl + mov edi, [_hash] + mov eax, [edi + 0x00] + mov [A], eax + mov eax, [edi + 0x04] + mov [B], eax + mov eax, [edi + 0x08] + mov [C], eax + mov eax, [edi + 0x0c] + mov [D], eax + mov eax, [edi + 0x10] + mov [E], eax + mov eax, [edi + 0x14] + mov [F], eax + mov eax, [edi + 0x18] + mov [G], eax + mov eax, [edi + 0x1c] + mov [H], eax + + 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 + sha224256._.round_1_16 G, H, A, B, C, D, E, F, 2 + sha224256._.round_1_16 F, G, H, A, B, C, D, E, 3 + sha224256._.round_1_16 E, F, G, H, A, B, C, D, 4 + sha224256._.round_1_16 D, E, F, G, H, A, B, C, 5 + sha224256._.round_1_16 C, D, E, F, G, H, A, B, 6 + sha224256._.round_1_16 B, C, D, E, F, G, H, A, 7 + sha224256._.round_1_16 A, B, C, D, E, F, G, H, 8 + sha224256._.round_1_16 H, A, B, C, D, E, F, G, 9 + sha224256._.round_1_16 G, H, A, B, C, D, E, F, 10 + sha224256._.round_1_16 F, G, H, A, B, C, D, E, 11 + sha224256._.round_1_16 E, F, G, H, A, B, C, D, 12 + sha224256._.round_1_16 D, E, F, G, H, A, B, C, 13 + 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 + +repeat 3 + sha224256._.round_17_64 A, B, C, D, E, F, G, H, 0, % + 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, % + sha224256._.round_17_64 F, G, H, A, B, C, D, E, 3, % + sha224256._.round_17_64 E, F, G, H, A, B, C, D, 4, % + sha224256._.round_17_64 D, E, F, G, H, A, B, C, 5, % + sha224256._.round_17_64 C, D, E, F, G, H, A, B, 6, % + sha224256._.round_17_64 B, C, D, E, F, G, H, A, 7, % + sha224256._.round_17_64 A, B, C, D, E, F, G, H, 8, % + sha224256._.round_17_64 H, A, B, C, D, E, F, G, 9, % + sha224256._.round_17_64 G, H, A, B, C, D, E, F, 10, % + sha224256._.round_17_64 F, G, H, A, B, C, D, E, 11, % + sha224256._.round_17_64 E, F, G, H, A, B, C, D, 12, % + 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, % + sha224256._.round_17_64 B, C, D, E, F, G, H, A, 15, % +end repeat + + mov edi, [_hash] + mov eax, [A] + add [edi + 0x00], eax + mov eax, [B] + add [edi + 0x04], eax + mov eax, [C] + add [edi + 0x08], eax + mov eax, [D] + add [edi + 0x0c], eax + mov eax, [E] + add [edi + 0x10], eax + mov eax, [F] + add [edi + 0x14], eax + mov eax, [G] + add [edi + 0x18], eax + mov eax, [H] + add [edi + 0x1c], eax + + ret +endp + + +sha224.update = sha224256.update +sha256.update = sha224256.update +proc sha224256.update _ctx, _msg, _size + mov ebx, [_ctx] + mov ecx, [_size] + add [ebx + ctx_sha224256.msglen_0], ecx + adc [ebx + ctx_sha224256.msglen_1], 0 + + .next_block: + mov ebx, [_ctx] + mov esi, [_msg] + mov eax, [ebx + ctx_sha224256.index] + and eax, SHA224256_BLOCK_SIZE-1 + jnz .copy_to_buf + test esi, SHA224256_ALIGN_MASK + jnz .copy_to_buf + .no_copy: + ; data is aligned, hash it in place without copying + mov ebx, [_ctx] + cmp [_size], SHA224256_BLOCK_SIZE + jb .copy_quit + lea eax, [ebx + ctx_sha224256.hash] + stdcall sha224256._.block, eax + sub [_size], SHA224256_BLOCK_SIZE + add esi, SHA224256_BLOCK_SIZE ; FIXME + jmp .no_copy + + .copy_to_buf: + lea edi, [ebx + ctx_sha224256.block] + add edi, eax + mov ecx, SHA224256_BLOCK_SIZE + sub ecx, eax + cmp [_size], ecx + jb .copy_quit + sub [_size], ecx + add [_msg], ecx + add [ebx + ctx_sha224256.index], ecx + rep movsb + lea eax, [ebx + ctx_sha224256.hash] + lea esi, [ebx + ctx_sha224256.block] + stdcall sha224256._.block, eax + jmp .next_block + + .copy_quit: + mov ebx, [_ctx] + lea edi, [ebx + ctx_sha224256.block] + mov eax, [ebx + ctx_sha224256.index] + and eax, SHA224256_BLOCK_SIZE-1 + add edi, eax + mov ecx, [_size] + add [ebx + ctx_sha224256.index], ecx + rep movsb + .quit: + + ret +endp + + +sha224.final = sha224256.final +sha256.final = sha224256.final +proc sha224256.final _ctx + mov ebx, [_ctx] + lea edi, [ebx + ctx_sha224256.block] + mov ecx, [ebx + ctx_sha224256.msglen_0] + and ecx, SHA224256_BLOCK_SIZE-1 + add edi, ecx + mov byte[edi], 0x80 + inc edi + neg ecx + add ecx, SHA224256_BLOCK_SIZE + cmp ecx, 8 + ja .last + + dec ecx + xor eax, eax + rep stosb + lea esi, [ebx + ctx_sha224256.block] + lea eax, [ebx + ctx_sha224256.hash] + stdcall sha224256._.block, eax + mov ebx, [_ctx] + lea edi, [ebx + ctx_sha224256.block] + mov ecx, SHA224256_BLOCK_SIZE+1 + .last: + dec ecx + sub ecx, 8 + xor eax, eax + rep stosb + mov eax, [ebx + ctx_sha224256.msglen_0] + mov edx, [ebx + ctx_sha224256.msglen_1] + shld edx, eax, 3 + shl eax, 3 + bswap eax + bswap edx + mov dword[edi], edx + mov dword[edi+4], eax + lea esi, [ebx + ctx_sha224256.block] + lea eax, [ebx + ctx_sha224256.hash] + stdcall sha224256._.block, eax + + mov ebx, [_ctx] + lea eax, [ebx + ctx_sha224256.hash] + stdcall sha224256._.postprocess, ebx, eax + + ret +endp + + +proc sha224256._.postprocess _ctx, _hash + mov ecx, 8 + mov esi, [_hash] + mov edi, esi + @@: + lodsd + bswap eax + stosd + dec ecx + jnz @b + ret +endp + + +proc sha224.oneshot _ctx, _data, _len + stdcall sha224.init, [_ctx] + stdcall sha224.update, [_ctx], [_data], [_len] + stdcall sha224.final, [_ctx] + ret +endp + + +proc sha256.oneshot _ctx, _data, _len + stdcall sha256.init, [_ctx] + stdcall sha256.update, [_ctx], [_data], [_len] + stdcall sha256.final, [_ctx] + ret +endp + + +iglobal +align SHA224256_ALIGN +sha224._.hash_init dd 0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939,\ + 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4 + +sha256._.hash_init dd 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,\ + 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 + +sha256_table dd 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,\ + 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 +endg diff --git a/programs/develop/libraries/libcrash/trunk/sha3.asm b/programs/develop/libraries/libcrash/trunk/sha3.asm index 4197a80666..6c538450f2 100644 --- a/programs/develop/libraries/libcrash/trunk/sha3.asm +++ b/programs/develop/libraries/libcrash/trunk/sha3.asm @@ -1,417 +1,482 @@ -; libcrash -- cryptographic hash functions -; -; Copyright (C) 2013,2016 Ivan Baravy (dunkaist) -; -; 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 -; the Free Software Foundation, either version 3 of the License, or -; (at your option) any later version. -; -; This program is distributed in the hope that it will be useful, -; but WITHOUT ANY WARRANTY; without even the implied warranty of -; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -; GNU General Public License for more details. -; -; You should have received a copy of the GNU General Public License -; along with this program. If not, see . - - -macro sha3._.rol_xor nd, ncl, ncr -{ - movq mm0, [C + 8*(ncl)] - movq mm1, mm0 - psllq mm0, 1 - psrlq mm1, 63 - por mm0, mm1 - pxor mm0, [C + 8*(ncr)] - movq [D + 8*(nd)], mm0 -} - -proc sha3._.theta -;locals -; C rq 5 -; D rq 5 -;endl -C equ ebx + ctx_sha3.C -D equ ebx + ctx_sha3.D - -repeat 5 - movq mm0, [edi + 8*(%-1 + 0)] - pxor mm0, [edi + 8*(%-1 + 5)] - pxor mm0, [edi + 8*(%-1 + 10)] - pxor mm0, [edi + 8*(%-1 + 15)] - pxor mm0, [edi + 8*(%-1 + 20)] - movq [C + 8*(%-1)], mm0 -end repeat - - sha3._.rol_xor 0, 1, 4 - sha3._.rol_xor 1, 2, 0 - sha3._.rol_xor 2, 3, 1 - sha3._.rol_xor 3, 4, 2 - sha3._.rol_xor 4, 0, 3 - -repeat 5 - movq mm1, [D + 8*(%-1)] - movq mm0, mm1 - pxor mm0, [edi + 8*(%-1 + 0)] - movq [edi + 8*(%-1 + 0)], mm0 - movq mm0, mm1 - pxor mm0, [edi + 8*(%-1 + 5)] - movq [edi + 8*(%-1 + 5)], mm0 - movq mm0, mm1 - pxor mm0, [edi + 8*(%-1 + 10)] - movq [edi + 8*(%-1 + 10)], mm0 - movq mm0, mm1 - pxor mm0, [edi + 8*(%-1 + 15)] - movq [edi + 8*(%-1 + 15)], mm0 - movq mm0, mm1 - pxor mm0, [edi + 8*(%-1 + 20)] - movq [edi + 8*(%-1 + 20)], mm0 -end repeat - -restore C,D - ret -endp - - -proc sha3._.pi - movq mm1, [edi + 8*1] - movq mm0, [edi + 8*6] - movq [edi + 8*1], mm0 - movq mm0, [edi + 8*9] - movq [edi + 8*6], mm0 - movq mm0, [edi + 8*22] - movq [edi + 8*9], mm0 - movq mm0, [edi + 8*14] - movq [edi + 8*22], mm0 - movq mm0, [edi + 8*20] - movq [edi + 8*14], mm0 - movq mm0, [edi + 8*2] - movq [edi + 8*20], mm0 - movq mm0, [edi + 8*12] - movq [edi + 8*2], mm0 - movq mm0, [edi + 8*13] - movq [edi + 8*12], mm0 - movq mm0, [edi + 8*19] - movq [edi + 8*13], mm0 - movq mm0, [edi + 8*23] - movq [edi + 8*19], mm0 - movq mm0, [edi + 8*15] - movq [edi + 8*23], mm0 - movq mm0, [edi + 8*4] - movq [edi + 8*15], mm0 - movq mm0, [edi + 8*24] - movq [edi + 8*4], mm0 - movq mm0, [edi + 8*21] - movq [edi + 8*24], mm0 - movq mm0, [edi + 8*8] - movq [edi + 8*21], mm0 - movq mm0, [edi + 8*16] - movq [edi + 8*8], mm0 - movq mm0, [edi + 8*5] - movq [edi + 8*16], mm0 - movq mm0, [edi + 8*3] - movq [edi + 8*5], mm0 - movq mm0, [edi + 8*18] - movq [edi + 8*3], mm0 - movq mm0, [edi + 8*17] - movq [edi + 8*18], mm0 - movq mm0, [edi + 8*11] - movq [edi + 8*17], mm0 - movq mm0, [edi + 8*7] - movq [edi + 8*11], mm0 - movq mm0, [edi + 8*10] - movq [edi + 8*7], mm0 - movq [edi + 8*10], mm1 - - ret -endp - - -proc sha3._.chi - - mov eax, 0xffffffff - movd mm0, eax - movq mm2, mm0 - punpckldq mm2, mm0 - -repeat 5 - movq mm6, [edi + 8*(0 + 5*(%-1))] - movq mm7, [edi + 8*(1 + 5*(%-1))] - - movq mm0, [edi + 8*(0 + 5*(%-1))] - movq mm1, mm7 - pandn mm1, mm2 - pand mm1, [edi + 8*(2 + 5*(%-1))] - pxor mm0, mm1 - movq [edi + 8*(0 + 5*(%-1))], mm0 - - movq mm0, [edi + 8*(1 + 5*(%-1))] - movq mm1, [edi + 8*(2 + 5*(%-1))] - pandn mm1, mm2 - pand mm1, [edi + 8*(3 + 5*(%-1))] - pxor mm0, mm1 - movq [edi + 8*(1 + 5*(%-1))], mm0 - - movq mm0, [edi + 8*(2 + 5*(%-1))] - movq mm1, [edi + 8*(3 + 5*(%-1))] - pandn mm1, mm2 - pand mm1, [edi + 8*(4 + 5*(%-1))] - pxor mm0, mm1 - movq [edi + 8*(2 + 5*(%-1))], mm0 - - movq mm0, [edi + 8*(3 + 5*(%-1))] - movq mm1, [edi + 8*(4 + 5*(%-1))] - pandn mm1, mm2 - pand mm1, mm6 - pxor mm0, mm1 - movq [edi + 8*(3 + 5*(%-1))], mm0 - - movq mm0, [edi + 8*(4 + 5*(%-1))] - movq mm1, mm6 - pandn mm1, mm2 - pand mm1, mm7 - pxor mm0, mm1 - movq [edi + 8*(4 + 5*(%-1))], mm0 -end repeat - ret -endp - - -macro sha3._.rol_mov n, c -{ - movq mm0, [edi + 8*(n)] - movq mm1, mm0 - psllq mm0, (c) - psrlq mm1, (64-(c)) - por mm0, mm1 - movq [edi + 8*(n)], mm0 -} - -proc sha3._.permutation - -repeat 24 - stdcall sha3._.theta - - sha3._.rol_mov 1, 1 - sha3._.rol_mov 2, 62 - sha3._.rol_mov 3, 28 - sha3._.rol_mov 4, 27 - sha3._.rol_mov 5, 36 - sha3._.rol_mov 6, 44 - sha3._.rol_mov 7, 6 - sha3._.rol_mov 8, 55 - sha3._.rol_mov 9, 20 - sha3._.rol_mov 10, 3 - sha3._.rol_mov 11, 10 - sha3._.rol_mov 12, 43 - sha3._.rol_mov 13, 25 - sha3._.rol_mov 14, 39 - sha3._.rol_mov 15, 41 - sha3._.rol_mov 16, 45 - sha3._.rol_mov 17, 15 - sha3._.rol_mov 18, 21 - sha3._.rol_mov 19, 8 - sha3._.rol_mov 20, 18 - sha3._.rol_mov 21, 2 - sha3._.rol_mov 22, 61 - sha3._.rol_mov 23, 56 - sha3._.rol_mov 24, 14 - - stdcall sha3._.pi - stdcall sha3._.chi - - movq mm0, [edi + 8*(0)] - pxor mm0, [sha3._.round + 8*(%-1)] - movq [edi + 8*(0)], mm0 -end repeat - - ret -endp - - -proc sha3._.init _ctx - mov [ebx + ctx_sha3.block_size], eax - shr eax, 3 - dec eax - mov [ebx + ctx_sha3.rounds_cnt], eax - xor eax, eax - lea edi, [ebx + ctx_sha3.hash] - mov ecx, SHA3_INIT_SIZE/4 - rep stosd - mov [ebx + ctx_sha3.index], eax - ret -endp - - -proc sha3224.init _ctx - mov ebx, [_ctx] - mov eax, SHA3224_BLOCK_SIZE - stdcall sha3._.init - ret -endp - - -proc sha3256.init _ctx - mov ebx, [_ctx] - mov eax, SHA3256_BLOCK_SIZE - stdcall sha3._.init - ret -endp - - -proc sha3384.init _ctx - mov ebx, [_ctx] - mov eax, SHA3384_BLOCK_SIZE - stdcall sha3._.init - ret -endp - - -proc sha3512.init _ctx - mov ebx, [_ctx] - mov eax, SHA3512_BLOCK_SIZE - stdcall sha3._.init - ret -endp - - -proc sha3._.block _hash - mov ecx, [ebx + ctx_sha3.rounds_cnt] - mov edi, [_hash] - - @@: - movq mm0, [esi + 8*ecx] - pxor mm0, [edi + 8*ecx] - movq [edi + 8*ecx], mm0 - dec ecx - jns @b - - stdcall sha3._.permutation - - ret -endp - - -sha3224.update = sha3.update -sha3256.update = sha3.update -sha3384.update = sha3.update -sha3512.update = sha3.update -proc sha3.update _ctx, _msg, _size - .next_block: - mov ebx, [_ctx] - mov esi, [_msg] - mov eax, [ebx + ctx_sha3.index] - test eax, eax - jnz .copy_to_buf - test esi, SHA3_ALIGN_MASK - jnz .copy_to_buf - .no_copy: - ; data is aligned, hash it in place without copying - mov ebx, [_ctx] - mov eax, [ebx + ctx_sha3.block_size] - cmp [_size], eax - jb .copy_quit - lea eax, [ebx + ctx_sha3.hash] - push ebx esi - stdcall sha3._.block, eax - pop esi ebx - mov eax, [ebx + ctx_sha3.block_size] - sub [_size], eax - add esi, [ebx + ctx_sha3.block_size] - jmp .no_copy - - .copy_to_buf: - lea edi, [ebx + ctx_sha3.block] - add edi, eax - mov ecx, [ebx + ctx_sha3.block_size] - sub ecx, eax - cmp [_size], ecx - jb .copy_quit - sub [_size], ecx - add [_msg], ecx - add [ebx + ctx_sha3.index], ecx - mov eax, [ebx + ctx_sha3.block_size] - cmp [ebx + ctx_sha3.index], eax - jb @f - sub [ebx + ctx_sha3.index], eax - @@: - rep movsb - lea eax, [ebx + ctx_sha3.hash] - lea esi, [ebx + ctx_sha3.block] - stdcall sha3._.block, eax - jmp .next_block - - .copy_quit: - mov ebx, [_ctx] - lea edi, [ebx + ctx_sha3.block] - mov eax, [ebx + ctx_sha3.index] - add edi, eax - mov ecx, [_size] - add [ebx + ctx_sha3.index], ecx - rep movsb - .quit: - ret -endp - - -sha3224.final = sha3.final -sha3256.final = sha3.final -sha3384.final = sha3.final -sha3512.final = sha3.final -proc sha3.final _ctx - pushad - mov ebx, [_ctx] - mov eax, [ebx + ctx_sha3.index] - xor edx, edx - mov ecx, [ebx + ctx_sha3.block_size] - div ecx - sub ecx, edx - ja @f - add ecx, [ebx + ctx_sha3.block_size] - @@: - add [ebx + ctx_sha3.index], ecx - mov eax, [ebx + ctx_sha3.block_size] - cmp [ebx + ctx_sha3.index], eax - jb @f - sub [ebx + ctx_sha3.index], eax - @@: - - mov byte[edi], 0x06 - inc edi - dec ecx - xor eax, eax - rep stosb - or byte[edi - 1], 0x80 - - mov ebx, [_ctx] - lea esi, [ebx + ctx_sha3.block] - lea eax, [ebx + ctx_sha3.hash] - stdcall sha3._.block, eax - - mov ebx, [_ctx] - lea eax, [ebx + ctx_sha3.hash] - 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 - +; libcrash -- cryptographic hash functions +; +; Copyright (C) 2013,2016,2019 Ivan Baravy (dunkaist) +; +; 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 +; the Free Software Foundation, either version 3 of the License, or +; (at your option) any later version. +; +; This program is distributed in the hope that it will be useful, +; but WITHOUT ANY WARRANTY; without even the implied warranty of +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +; GNU General Public License for more details. +; +; You should have received a copy of the GNU General Public License +; along with this program. If not, see . + + +SHA3_224_HASH_SIZE = 28 +SHA3_256_HASH_SIZE = 32 +SHA3_384_HASH_SIZE = 48 +SHA3_512_HASH_SIZE = 64 + +SHA3_224_BLOCK_SIZE = 144 +SHA3_256_BLOCK_SIZE = 136 +SHA3_384_BLOCK_SIZE = 104 +SHA3_512_BLOCK_SIZE = 72 +SHA3MAX_BLOCK_SIZE = SHA3_224_BLOCK_SIZE + +SHA3_INIT_SIZE = 200 +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 + +if defined sizeof.crash_ctx + assert sizeof.crash_ctx >= sizeof.ctx_sha3 +end if + +macro sha3._.rol_xor nd, ncl, ncr +{ + movq mm0, [C + 8*(ncl)] + movq mm1, mm0 + psllq mm0, 1 + psrlq mm1, 63 + por mm0, mm1 + pxor mm0, [C + 8*(ncr)] + movq [D + 8*(nd)], mm0 +} + +proc sha3._.theta +;locals +; C rq 5 +; D rq 5 +;endl +C equ ebx + ctx_sha3.C +D equ ebx + ctx_sha3.D + +repeat 5 + movq mm0, [edi + 8*(%-1 + 0)] + pxor mm0, [edi + 8*(%-1 + 5)] + pxor mm0, [edi + 8*(%-1 + 10)] + pxor mm0, [edi + 8*(%-1 + 15)] + pxor mm0, [edi + 8*(%-1 + 20)] + movq [C + 8*(%-1)], mm0 +end repeat + + sha3._.rol_xor 0, 1, 4 + sha3._.rol_xor 1, 2, 0 + sha3._.rol_xor 2, 3, 1 + sha3._.rol_xor 3, 4, 2 + sha3._.rol_xor 4, 0, 3 + +repeat 5 + movq mm1, [D + 8*(%-1)] + movq mm0, mm1 + pxor mm0, [edi + 8*(%-1 + 0)] + movq [edi + 8*(%-1 + 0)], mm0 + movq mm0, mm1 + pxor mm0, [edi + 8*(%-1 + 5)] + movq [edi + 8*(%-1 + 5)], mm0 + movq mm0, mm1 + pxor mm0, [edi + 8*(%-1 + 10)] + movq [edi + 8*(%-1 + 10)], mm0 + movq mm0, mm1 + pxor mm0, [edi + 8*(%-1 + 15)] + movq [edi + 8*(%-1 + 15)], mm0 + movq mm0, mm1 + pxor mm0, [edi + 8*(%-1 + 20)] + movq [edi + 8*(%-1 + 20)], mm0 +end repeat + +restore C,D + ret +endp + + +proc sha3._.pi + movq mm1, [edi + 8*1] + movq mm0, [edi + 8*6] + movq [edi + 8*1], mm0 + movq mm0, [edi + 8*9] + movq [edi + 8*6], mm0 + movq mm0, [edi + 8*22] + movq [edi + 8*9], mm0 + movq mm0, [edi + 8*14] + movq [edi + 8*22], mm0 + movq mm0, [edi + 8*20] + movq [edi + 8*14], mm0 + movq mm0, [edi + 8*2] + movq [edi + 8*20], mm0 + movq mm0, [edi + 8*12] + movq [edi + 8*2], mm0 + movq mm0, [edi + 8*13] + movq [edi + 8*12], mm0 + movq mm0, [edi + 8*19] + movq [edi + 8*13], mm0 + movq mm0, [edi + 8*23] + movq [edi + 8*19], mm0 + movq mm0, [edi + 8*15] + movq [edi + 8*23], mm0 + movq mm0, [edi + 8*4] + movq [edi + 8*15], mm0 + movq mm0, [edi + 8*24] + movq [edi + 8*4], mm0 + movq mm0, [edi + 8*21] + movq [edi + 8*24], mm0 + movq mm0, [edi + 8*8] + movq [edi + 8*21], mm0 + movq mm0, [edi + 8*16] + movq [edi + 8*8], mm0 + movq mm0, [edi + 8*5] + movq [edi + 8*16], mm0 + movq mm0, [edi + 8*3] + movq [edi + 8*5], mm0 + movq mm0, [edi + 8*18] + movq [edi + 8*3], mm0 + movq mm0, [edi + 8*17] + movq [edi + 8*18], mm0 + movq mm0, [edi + 8*11] + movq [edi + 8*17], mm0 + movq mm0, [edi + 8*7] + movq [edi + 8*11], mm0 + movq mm0, [edi + 8*10] + movq [edi + 8*7], mm0 + movq [edi + 8*10], mm1 + + ret +endp + + +proc sha3._.chi + + mov eax, 0xffffffff + movd mm0, eax + movq mm2, mm0 + punpckldq mm2, mm0 + +repeat 5 + movq mm6, [edi + 8*(0 + 5*(%-1))] + movq mm7, [edi + 8*(1 + 5*(%-1))] + + movq mm0, [edi + 8*(0 + 5*(%-1))] + movq mm1, mm7 + pandn mm1, mm2 + pand mm1, [edi + 8*(2 + 5*(%-1))] + pxor mm0, mm1 + movq [edi + 8*(0 + 5*(%-1))], mm0 + + movq mm0, [edi + 8*(1 + 5*(%-1))] + movq mm1, [edi + 8*(2 + 5*(%-1))] + pandn mm1, mm2 + pand mm1, [edi + 8*(3 + 5*(%-1))] + pxor mm0, mm1 + movq [edi + 8*(1 + 5*(%-1))], mm0 + + movq mm0, [edi + 8*(2 + 5*(%-1))] + movq mm1, [edi + 8*(3 + 5*(%-1))] + pandn mm1, mm2 + pand mm1, [edi + 8*(4 + 5*(%-1))] + pxor mm0, mm1 + movq [edi + 8*(2 + 5*(%-1))], mm0 + + movq mm0, [edi + 8*(3 + 5*(%-1))] + movq mm1, [edi + 8*(4 + 5*(%-1))] + pandn mm1, mm2 + pand mm1, mm6 + pxor mm0, mm1 + movq [edi + 8*(3 + 5*(%-1))], mm0 + + movq mm0, [edi + 8*(4 + 5*(%-1))] + movq mm1, mm6 + pandn mm1, mm2 + pand mm1, mm7 + pxor mm0, mm1 + movq [edi + 8*(4 + 5*(%-1))], mm0 +end repeat + ret +endp + + +macro sha3._.rol_mov n, c +{ + movq mm0, [edi + 8*(n)] + movq mm1, mm0 + psllq mm0, (c) + psrlq mm1, (64-(c)) + por mm0, mm1 + movq [edi + 8*(n)], mm0 +} + +proc sha3._.permutation + +repeat 24 + stdcall sha3._.theta + + sha3._.rol_mov 1, 1 + sha3._.rol_mov 2, 62 + sha3._.rol_mov 3, 28 + sha3._.rol_mov 4, 27 + sha3._.rol_mov 5, 36 + sha3._.rol_mov 6, 44 + sha3._.rol_mov 7, 6 + sha3._.rol_mov 8, 55 + sha3._.rol_mov 9, 20 + sha3._.rol_mov 10, 3 + sha3._.rol_mov 11, 10 + sha3._.rol_mov 12, 43 + sha3._.rol_mov 13, 25 + sha3._.rol_mov 14, 39 + sha3._.rol_mov 15, 41 + sha3._.rol_mov 16, 45 + sha3._.rol_mov 17, 15 + sha3._.rol_mov 18, 21 + sha3._.rol_mov 19, 8 + sha3._.rol_mov 20, 18 + sha3._.rol_mov 21, 2 + sha3._.rol_mov 22, 61 + sha3._.rol_mov 23, 56 + sha3._.rol_mov 24, 14 + + stdcall sha3._.pi + stdcall sha3._.chi + + movq mm0, [edi + 8*(0)] + pxor mm0, [sha3._.round + 8*(%-1)] + movq [edi + 8*(0)], mm0 +end repeat + + ret +endp + + +proc sha3._.init _ctx + mov [ebx + ctx_sha3.block_size], eax + shr eax, 3 + dec eax + mov [ebx + ctx_sha3.rounds_cnt], eax + xor eax, eax + lea edi, [ebx + ctx_sha3.hash] + mov ecx, SHA3_INIT_SIZE/4 + rep stosd + mov [ebx + ctx_sha3.index], eax + ret +endp + + +proc sha3_224.init _ctx + mov ebx, [_ctx] + mov eax, SHA3_224_BLOCK_SIZE + stdcall sha3._.init + ret +endp + + +proc sha3_256.init _ctx + mov ebx, [_ctx] + mov eax, SHA3_256_BLOCK_SIZE + stdcall sha3._.init + ret +endp + + +proc sha3_384.init _ctx + mov ebx, [_ctx] + mov eax, SHA3_384_BLOCK_SIZE + stdcall sha3._.init + ret +endp + + +proc sha3_512.init _ctx + mov ebx, [_ctx] + mov eax, SHA3_512_BLOCK_SIZE + stdcall sha3._.init + ret +endp + + +proc sha3._.block _hash + mov ecx, [ebx + ctx_sha3.rounds_cnt] + mov edi, [_hash] + + @@: + movq mm0, [esi + 8*ecx] + pxor mm0, [edi + 8*ecx] + movq [edi + 8*ecx], mm0 + dec ecx + jns @b + + stdcall sha3._.permutation + + ret +endp + + +sha3_224.update = sha3.update +sha3_256.update = sha3.update +sha3_384.update = sha3.update +sha3_512.update = sha3.update +proc sha3.update _ctx, _msg, _size + .next_block: + mov ebx, [_ctx] + mov esi, [_msg] + mov eax, [ebx + ctx_sha3.index] + test eax, eax + jnz .copy_to_buf + test esi, SHA3_ALIGN_MASK + jnz .copy_to_buf + .no_copy: + ; data is aligned, hash it in place without copying + mov ebx, [_ctx] + mov eax, [ebx + ctx_sha3.block_size] + cmp [_size], eax + jb .copy_quit + lea eax, [ebx + ctx_sha3.hash] + push ebx esi + stdcall sha3._.block, eax + pop esi ebx + mov eax, [ebx + ctx_sha3.block_size] + sub [_size], eax + add esi, [ebx + ctx_sha3.block_size] + jmp .no_copy + + .copy_to_buf: + lea edi, [ebx + ctx_sha3.block] + add edi, eax + mov ecx, [ebx + ctx_sha3.block_size] + sub ecx, eax + cmp [_size], ecx + jb .copy_quit + sub [_size], ecx + add [_msg], ecx + add [ebx + ctx_sha3.index], ecx + mov eax, [ebx + ctx_sha3.block_size] + cmp [ebx + ctx_sha3.index], eax + jb @f + sub [ebx + ctx_sha3.index], eax + @@: + rep movsb + lea eax, [ebx + ctx_sha3.hash] + lea esi, [ebx + ctx_sha3.block] + stdcall sha3._.block, eax + jmp .next_block + + .copy_quit: + mov ebx, [_ctx] + lea edi, [ebx + ctx_sha3.block] + mov eax, [ebx + ctx_sha3.index] + add edi, eax + mov ecx, [_size] + add [ebx + ctx_sha3.index], ecx + rep movsb + .quit: + ret +endp + + +sha3_224.final = sha3.final +sha3_256.final = sha3.final +sha3_384.final = sha3.final +sha3_512.final = sha3.final +proc sha3.final _ctx + pushad + mov ebx, [_ctx] + mov eax, [ebx + ctx_sha3.index] + xor edx, edx + mov ecx, [ebx + ctx_sha3.block_size] + div ecx + sub ecx, edx + ja @f + add ecx, [ebx + ctx_sha3.block_size] + @@: + add [ebx + ctx_sha3.index], ecx + mov eax, [ebx + ctx_sha3.block_size] + cmp [ebx + ctx_sha3.index], eax + jb @f + sub [ebx + ctx_sha3.index], eax + @@: + + mov byte[edi], 0x06 + inc edi + dec ecx + xor eax, eax + rep stosb + or byte[edi - 1], 0x80 + + mov ebx, [_ctx] + lea esi, [ebx + ctx_sha3.block] + lea eax, [ebx + ctx_sha3.hash] + stdcall sha3._.block, eax + + mov ebx, [_ctx] + lea eax, [ebx + ctx_sha3.hash] + stdcall sha3._.postprocess, ebx, eax + + popad + ret +endp + + +proc sha3._.postprocess _ctx, _hash + emms + ret +endp + + +proc sha3_224.oneshot _ctx, _data, _len + stdcall sha3_224.init, [_ctx] + stdcall sha3.update, [_ctx], [_data], [_len] + stdcall sha3.final, [_ctx] + ret +endp + + +proc sha3_256.oneshot _ctx, _data, _len + stdcall sha3_256.init, [_ctx] + stdcall sha3.update, [_ctx], [_data], [_len] + stdcall sha3.final, [_ctx] + ret +endp + + +proc sha3_384.oneshot _ctx, _data, _len + stdcall sha3_384.init, [_ctx] + stdcall sha3.update, [_ctx], [_data], [_len] + stdcall sha3.final, [_ctx] + ret +endp + + +proc sha3_512.oneshot _ctx, _data, _len + stdcall sha3_512.init, [_ctx] + stdcall sha3.update, [_ctx], [_data], [_len] + stdcall sha3.final, [_ctx] + ret +endp + + +iglobal +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 +endg diff --git a/programs/develop/libraries/libcrash/trunk/sha384_512.asm b/programs/develop/libraries/libcrash/trunk/sha384_512.asm index df57fe5903..abe3a982e6 100644 --- a/programs/develop/libraries/libcrash/trunk/sha384_512.asm +++ b/programs/develop/libraries/libcrash/trunk/sha384_512.asm @@ -1,511 +1,566 @@ -; libcrash -- cryptographic hash functions -; -; Copyright (C) 2012-2013,2016 Ivan Baravy (dunkaist) -; -; 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 -; the Free Software Foundation, either version 3 of the License, or -; (at your option) any later version. -; -; This program is distributed in the hope that it will be useful, -; but WITHOUT ANY WARRANTY; without even the implied warranty of -; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -; GNU General Public License for more details. -; -; You should have received a copy of the GNU General Public License -; along with this program. If not, see . - - -macro sha384512._.chn x, y, z -{ - movq mm0, [y] - pxor mm0, [z] - pand mm0, [x] - pxor mm0, [z] -} - -macro sha384512._.maj x, y, z -{ - movq mm0, [x] - pxor mm0, [y] - pand mm0, [z] - movq mm2, [x] - pand mm2, [y] - pxor mm0, mm2 -} - -macro sha384512._.Sigma0 x -{ - movq mm0, x - movq mm2, mm0 - movq mm7, mm2 - psrlq mm2, 28 - psllq mm7, 36 - por mm2, mm7 - movq mm7, mm0 - psrlq mm0, 34 - psllq mm7, 30 - por mm0, mm7 - pxor mm0, mm2 - movq mm2, x - movq mm7, mm2 - psrlq mm2, 39 - psllq mm7, 25 - por mm2, mm7 - pxor mm0, mm2 -} - -macro sha384512._.Sigma1 x -{ - movq mm0, x - movq mm2, mm0 - movq mm7, mm2 - psrlq mm2, 14 - psllq mm7, 50 - por mm2, mm7 - movq mm7, mm0 - psrlq mm0, 18 - psllq mm7, 46 - por mm0, mm7 - pxor mm0, mm2 - movq mm2, x - movq mm7, mm2 - psrlq mm2, 41 - psllq mm7, 23 - por mm2, mm7 - pxor mm0, mm2 -} - -macro sha384512._.sigma0 x -{ - movq mm0, x - movq mm2, mm0 - movq mm7, mm2 - psrlq mm2, 1 - psllq mm7, 63 - por mm2, mm7 - movq mm7, mm0 - psrlq mm0, 8 - psllq mm7, 56 - por mm0, mm7 - pxor mm0, mm2 - movq mm2, x - psrlq mm2, 7 - pxor mm0, mm2 -} - -macro sha384512._.sigma1 x -{ - movq mm0, x - movq mm2, mm0 - movq mm7, mm2 - psrlq mm2, 19 - psllq mm7, 45 - por mm2, mm7 - movq mm7, mm0 - psrlq mm0, 61 - psllq mm7, 3 - por mm0, mm7 - pxor mm0, mm2 - movq mm2, x - psrlq mm2, 6 - pxor mm0, mm2 -} - -macro sha384512._.recalculate_w n -{ - movq mm3, [w + ((n-2) and 15)*8] - sha384512._.sigma1 mm3 - paddq mm0, [w + ((n-7) and 15)*8] - movq mm6, mm0 - movq mm3, [w + ((n-15) and 15)*8] - sha384512._.sigma0 mm3 - movq mm2, mm6 - paddq mm0, mm2 - movq mm7, [w + (n)*8] - paddq mm7, mm0 - movq [w + (n)*8], mm7 -} - -macro sha384512._.round a, b, c, d, e, f, g, h, k -{ - movq mm1, [h] - movq mm3, [e] - sha384512._.Sigma1 mm3 - paddq mm1, mm0 - sha384512._.chn e, f, g - paddq mm1, mm0 - paddq mm1, [k] - paddq mm1, mm5 - movq mm7, [d] - paddq mm7, mm1 - movq [d], mm7 - movq mm3, [a] - sha384512._.Sigma0 mm3 - paddq mm1, mm0 - sha384512._.maj a, b, c - paddq mm0, mm1 - movq [h], mm0 -} - - -macro sha384512._.round_1_16 a, b, c, d, e, f, g, h, n -{ - - movq mm0, [esi + (n)*8] - movq [temp], mm0 - mov eax, dword[temp] - bswap eax - push eax - mov eax, dword[temp + 4] - bswap eax - mov dword[temp], eax - pop eax - mov dword[temp + 4], eax - movq mm0, [temp] - movq [w + (n)*8], mm0 - movq mm5, mm0 - sha384512._.round a, b, c, d, e, f, g, h, (sha384512._.table + (n)*8) -} - -macro sha384512._.round_17_64 a, b, c, d, e, f, g, h, n, rep_num -{ - sha384512._.recalculate_w n - movq mm5, [w + (n)*8] - sha384512._.round a, b, c, d, e, f, g, h, (sha384512._.table + (n+16*rep_num)*8) -} - - -proc sha384.init _ctx - mov ebx, [_ctx] - lea edi, [ebx + ctx_sha384512.hash] - mov esi, sha384._.hash_init - mov ecx, SHA384512_INIT_SIZE/4 - rep movsd - xor eax, eax - mov [ebx + ctx_sha384512.index], eax - mov [ebx + ctx_sha384512.msglen_0], eax - mov [ebx + ctx_sha384512.msglen_1], eax - mov [ebx + ctx_sha384512.msglen_2], eax - mov [ebx + ctx_sha384512.msglen_3], eax - ret -endp - - -proc sha512.init _ctx - mov ebx, [_ctx] - lea edi, [ebx + ctx_sha384512.hash] - mov esi, sha512._.hash_init - mov ecx, SHA384512_INIT_SIZE/4 - rep movsd - xor eax, eax - mov [ebx + ctx_sha384512.index], eax - mov [ebx + ctx_sha384512.msglen_0], eax - mov [ebx + ctx_sha384512.msglen_1], eax - mov [ebx + ctx_sha384512.msglen_2], eax - mov [ebx + ctx_sha384512.msglen_3], eax - ret -endp - - -proc sha384512._.block _hash -;locals -; 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 -;endl -w equ ebx + ctx_sha384512.w -A equ ebx + ctx_sha384512.A -B equ ebx + ctx_sha384512.B -C equ ebx + ctx_sha384512.C -D equ ebx + ctx_sha384512.D -E equ ebx + ctx_sha384512.E -F equ ebx + ctx_sha384512.F -G equ ebx + ctx_sha384512.G -H equ ebx + ctx_sha384512.H -temp equ ebx + ctx_sha384512.temp - - mov edi, [_hash] - movq mm0, [edi + 0x00] - movq [A], mm0 - movq mm0, [edi + 0x08] - movq [B], mm0 - movq mm0, [edi + 0x10] - movq [C], mm0 - movq mm0, [edi + 0x18] - movq [D], mm0 - movq mm0, [edi + 0x20] - movq [E], mm0 - movq mm0, [edi + 0x28] - movq [F], mm0 - movq mm0, [edi + 0x30] - movq [G], mm0 - movq mm0, [edi + 0x38] - movq [H], mm0 - - - sha384512._.round_1_16 A, B, C, D, E, F, G, H, 0 - sha384512._.round_1_16 H, A, B, C, D, E, F, G, 1 - sha384512._.round_1_16 G, H, A, B, C, D, E, F, 2 - sha384512._.round_1_16 F, G, H, A, B, C, D, E, 3 - sha384512._.round_1_16 E, F, G, H, A, B, C, D, 4 - sha384512._.round_1_16 D, E, F, G, H, A, B, C, 5 - sha384512._.round_1_16 C, D, E, F, G, H, A, B, 6 - sha384512._.round_1_16 B, C, D, E, F, G, H, A, 7 - sha384512._.round_1_16 A, B, C, D, E, F, G, H, 8 - sha384512._.round_1_16 H, A, B, C, D, E, F, G, 9 - sha384512._.round_1_16 G, H, A, B, C, D, E, F, 10 - sha384512._.round_1_16 F, G, H, A, B, C, D, E, 11 - sha384512._.round_1_16 E, F, G, H, A, B, C, D, 12 - sha384512._.round_1_16 D, E, F, G, H, A, B, C, 13 - sha384512._.round_1_16 C, D, E, F, G, H, A, B, 14 - sha384512._.round_1_16 B, C, D, E, F, G, H, A, 15 - -repeat 4 - sha384512._.round_17_64 A, B, C, D, E, F, G, H, 0, % - sha384512._.round_17_64 H, A, B, C, D, E, F, G, 1, % - sha384512._.round_17_64 G, H, A, B, C, D, E, F, 2, % - sha384512._.round_17_64 F, G, H, A, B, C, D, E, 3, % - sha384512._.round_17_64 E, F, G, H, A, B, C, D, 4, % - sha384512._.round_17_64 D, E, F, G, H, A, B, C, 5, % - sha384512._.round_17_64 C, D, E, F, G, H, A, B, 6, % - sha384512._.round_17_64 B, C, D, E, F, G, H, A, 7, % - sha384512._.round_17_64 A, B, C, D, E, F, G, H, 8, % - sha384512._.round_17_64 H, A, B, C, D, E, F, G, 9, % - sha384512._.round_17_64 G, H, A, B, C, D, E, F, 10, % - sha384512._.round_17_64 F, G, H, A, B, C, D, E, 11, % - sha384512._.round_17_64 E, F, G, H, A, B, C, D, 12, % - sha384512._.round_17_64 D, E, F, G, H, A, B, C, 13, % - sha384512._.round_17_64 C, D, E, F, G, H, A, B, 14, % - sha384512._.round_17_64 B, C, D, E, F, G, H, A, 15, % -end repeat - - - mov edi, [_hash] - movq mm0, [A] - paddq mm0, [edi + 0x00] - movq [edi + 0x00], mm0 - movq mm0, [B] - paddq mm0, [edi + 0x08] - movq [edi + 0x08], mm0 - movq mm0, [C] - paddq mm0, [edi + 0x10] - movq [edi + 0x10], mm0 - movq mm0, [D] - paddq mm0, [edi + 0x18] - movq [edi + 0x18], mm0 - movq mm0, [E] - paddq mm0, [edi + 0x20] - movq [edi + 0x20], mm0 - movq mm0, [F] - paddq mm0, [edi + 0x28] - movq [edi + 0x28], mm0 - movq mm0, [G] - paddq mm0, [edi + 0x30] - movq [edi + 0x30], mm0 - movq mm0, [H] - paddq mm0, [edi + 0x38] - movq [edi + 0x38], mm0 - - ret -restore w,A,B,C,D,E,F,G,H,temp -endp - -sha512.update = sha384.update -proc sha384.update _ctx, _msg, _size - mov ebx, [_ctx] - mov ecx, [_size] - add [ebx + ctx_sha384512.msglen_0], ecx - adc [ebx + ctx_sha384512.msglen_1], 0 - adc [ebx + ctx_sha384512.msglen_2], 0 - adc [ebx + ctx_sha384512.msglen_3], 0 - - .next_block: - mov ebx, [_ctx] - mov esi, [_msg] - mov eax, [ebx + ctx_sha384512.index] - and eax, SHA384512_BLOCK_SIZE-1 - jnz .copy_to_buf - test esi, SHA384512_ALIGN_MASK - jnz .copy_to_buf - .no_copy: - ; data is aligned, hash it in place without copying - mov ebx, [_ctx] - cmp [_size], SHA384512_BLOCK_SIZE - jb .copy_quit - lea eax, [ebx + ctx_sha384512.hash] - stdcall sha384512._.block, eax - sub [_size], SHA384512_BLOCK_SIZE - add esi, SHA384512_BLOCK_SIZE ; FIXME - jmp .no_copy - - .copy_to_buf: - lea edi, [ebx + ctx_sha384512.block] - add edi, eax - mov ecx, SHA384512_BLOCK_SIZE - sub ecx, eax - cmp [_size], ecx - jb .copy_quit - sub [_size], ecx - add [_msg], ecx - add [ebx + ctx_sha384512.index], ecx - rep movsb - lea eax, [ebx + ctx_sha384512.hash] - lea esi, [ebx + ctx_sha384512.block] - stdcall sha384512._.block, eax - jmp .next_block - - .copy_quit: - mov ebx, [_ctx] - lea edi, [ebx + ctx_sha384512.block] - mov eax, [ebx + ctx_sha384512.index] - and eax, SHA384512_BLOCK_SIZE-1 - add edi, eax - mov ecx, [_size] - add [ebx + ctx_sha384512.index], ecx - rep movsb - .quit: - - ret -endp - - -sha512.final = sha384.final -proc sha384.final _ctx - mov ebx, [_ctx] - lea edi, [ebx + ctx_sha384512.block] - mov ecx, [ebx + ctx_sha384512.msglen_0] - and ecx, SHA384512_BLOCK_SIZE-1 - add edi, ecx - mov byte[edi], 0x80 - inc edi - neg ecx - add ecx, SHA384512_BLOCK_SIZE - cmp ecx, 16 - ja .last - - dec ecx - xor eax, eax - rep stosb - lea esi, [ebx + ctx_sha384512.block] - lea eax, [ebx + ctx_sha384512.hash] - stdcall sha384512._.block, eax - mov ebx, [_ctx] - lea edi, [ebx + ctx_sha384512.block] - mov ecx, SHA384512_BLOCK_SIZE+1 - .last: - dec ecx - sub ecx, 16 - xor eax, eax - rep stosb - mov eax, [ebx + ctx_sha384512.msglen_1] - shld [ebx + ctx_sha384512.msglen_0], eax, 3 - mov eax, [ebx + ctx_sha384512.msglen_2] - shld [ebx + ctx_sha384512.msglen_1], eax, 3 - mov eax, [ebx + ctx_sha384512.msglen_3] - shld [ebx + ctx_sha384512.msglen_2], eax, 3 - shl eax, 3 - bswap eax - mov dword[edi + 0], eax - mov eax, [ebx + ctx_sha384512.msglen_2] - bswap eax - mov dword[edi + 4], eax - mov eax, [ebx + ctx_sha384512.msglen_1] - bswap eax - mov dword[edi + 8], eax - mov eax, [ebx + ctx_sha384512.msglen_0] - bswap eax - mov dword[edi + 12], eax - mov ebx, [_ctx] - lea esi, [ebx + ctx_sha384512.block] - lea eax, [ebx + ctx_sha384512.hash] - stdcall sha384512._.block, eax - - mov ebx, [_ctx] - lea eax, [ebx + ctx_sha384512.hash] - stdcall sha384512._.postprocess, ebx, eax - - ret -endp - - -proc sha384512._.postprocess _ctx, _hash - mov ecx, 8 - mov esi, [_hash] - mov edi, esi - @@: - lodsd - mov ebx, eax - lodsd - bswap eax - bswap ebx - stosd - mov eax, ebx - stosd - dec ecx ; FIXME: what should I fix here? - jnz @b - emms - ret -endp - - -align SHA384512_ALIGN - -sha384._.hash_init dq 0xcbbb9d5dc1059ed8, 0x629a292a367cd507,\ - 0x9159015a3070dd17, 0x152fecd8f70e5939,\ - 0x67332667ffc00b31, 0x8eb44a8768581511,\ - 0xdb0c2e0d64f98fa7, 0x47b5481dbefa4fa4 - -sha512._.hash_init dq 0x6a09e667f3bcc908, 0xbb67ae8584caa73b,\ - 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1,\ - 0x510e527fade682d1, 0x9b05688c2b3e6c1f,\ - 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179 - -sha384512._.table dq 0x428a2f98d728ae22, 0x7137449123ef65cd,\ - 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc,\ - 0x3956c25bf348b538, 0x59f111f1b605d019,\ - 0x923f82a4af194f9b, 0xab1c5ed5da6d8118,\ - 0xd807aa98a3030242, 0x12835b0145706fbe,\ - 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2,\ - 0x72be5d74f27b896f, 0x80deb1fe3b1696b1,\ - 0x9bdc06a725c71235, 0xc19bf174cf692694,\ - 0xe49b69c19ef14ad2, 0xefbe4786384f25e3,\ - 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65,\ - 0x2de92c6f592b0275, 0x4a7484aa6ea6e483,\ - 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5,\ - 0x983e5152ee66dfab, 0xa831c66d2db43210,\ - 0xb00327c898fb213f, 0xbf597fc7beef0ee4,\ - 0xc6e00bf33da88fc2, 0xd5a79147930aa725,\ - 0x06ca6351e003826f, 0x142929670a0e6e70,\ - 0x27b70a8546d22ffc, 0x2e1b21385c26c926,\ - 0x4d2c6dfc5ac42aed, 0x53380d139d95b3df,\ - 0x650a73548baf63de, 0x766a0abb3c77b2a8,\ - 0x81c2c92e47edaee6, 0x92722c851482353b,\ - 0xa2bfe8a14cf10364, 0xa81a664bbc423001,\ - 0xc24b8b70d0f89791, 0xc76c51a30654be30,\ - 0xd192e819d6ef5218, 0xd69906245565a910,\ - 0xf40e35855771202a, 0x106aa07032bbd1b8,\ - 0x19a4c116b8d2d0c8, 0x1e376c085141ab53,\ - 0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8,\ - 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb,\ - 0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3,\ - 0x748f82ee5defb2fc, 0x78a5636f43172f60,\ - 0x84c87814a1f0ab72, 0x8cc702081a6439ec,\ - 0x90befffa23631e28, 0xa4506cebde82bde9,\ - 0xbef9a3f7b2c67915, 0xc67178f2e372532b,\ - 0xca273eceea26619c, 0xd186b8c721c0c207,\ - 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178,\ - 0x06f067aa72176fba, 0x0a637dc5a2c898a6,\ - 0x113f9804bef90dae, 0x1b710b35131c471b,\ - 0x28db77f523047d84, 0x32caab7b40c72493,\ - 0x3c9ebe0a15c9bebc, 0x431d67c49c100d4c,\ - 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a,\ - 0x5fcb6fab3ad6faec, 0x6c44198c4a475817 - +; libcrash -- cryptographic hash functions +; +; Copyright (C) 2012-2013,2016,2019 Ivan Baravy (dunkaist) +; +; 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 +; the Free Software Foundation, either version 3 of the License, or +; (at your option) any later version. +; +; This program is distributed in the hope that it will be useful, +; but WITHOUT ANY WARRANTY; without even the implied warranty of +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +; GNU General Public License for more details. +; +; You should have received a copy of the GNU General Public License +; along with this program. If not, see . + + +SHA384512_BLOCK_SIZE = 128 +SHA384_BLOCK_SIZE = SHA384512_BLOCK_SIZE +SHA512_BLOCK_SIZE = SHA384512_BLOCK_SIZE +SHA384_HASH_SIZE = 48 +SHA512_HASH_SIZE = 64 + +SHA384512_INIT_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 + +if defined sizeof.crash_ctx + assert sizeof.crash_ctx >= sizeof.ctx_sha384512 +end if + +macro sha384512._.chn x, y, z +{ + movq mm0, [y] + pxor mm0, [z] + pand mm0, [x] + pxor mm0, [z] +} + +macro sha384512._.maj x, y, z +{ + movq mm0, [x] + pxor mm0, [y] + pand mm0, [z] + movq mm2, [x] + pand mm2, [y] + pxor mm0, mm2 +} + +macro sha384512._.Sigma0 x +{ + movq mm0, x + movq mm2, mm0 + movq mm7, mm2 + psrlq mm2, 28 + psllq mm7, 36 + por mm2, mm7 + movq mm7, mm0 + psrlq mm0, 34 + psllq mm7, 30 + por mm0, mm7 + pxor mm0, mm2 + movq mm2, x + movq mm7, mm2 + psrlq mm2, 39 + psllq mm7, 25 + por mm2, mm7 + pxor mm0, mm2 +} + +macro sha384512._.Sigma1 x +{ + movq mm0, x + movq mm2, mm0 + movq mm7, mm2 + psrlq mm2, 14 + psllq mm7, 50 + por mm2, mm7 + movq mm7, mm0 + psrlq mm0, 18 + psllq mm7, 46 + por mm0, mm7 + pxor mm0, mm2 + movq mm2, x + movq mm7, mm2 + psrlq mm2, 41 + psllq mm7, 23 + por mm2, mm7 + pxor mm0, mm2 +} + +macro sha384512._.sigma0 x +{ + movq mm0, x + movq mm2, mm0 + movq mm7, mm2 + psrlq mm2, 1 + psllq mm7, 63 + por mm2, mm7 + movq mm7, mm0 + psrlq mm0, 8 + psllq mm7, 56 + por mm0, mm7 + pxor mm0, mm2 + movq mm2, x + psrlq mm2, 7 + pxor mm0, mm2 +} + +macro sha384512._.sigma1 x +{ + movq mm0, x + movq mm2, mm0 + movq mm7, mm2 + psrlq mm2, 19 + psllq mm7, 45 + por mm2, mm7 + movq mm7, mm0 + psrlq mm0, 61 + psllq mm7, 3 + por mm0, mm7 + pxor mm0, mm2 + movq mm2, x + psrlq mm2, 6 + pxor mm0, mm2 +} + +macro sha384512._.recalculate_w n +{ + movq mm3, [w + ((n-2) and 15)*8] + sha384512._.sigma1 mm3 + paddq mm0, [w + ((n-7) and 15)*8] + movq mm6, mm0 + movq mm3, [w + ((n-15) and 15)*8] + sha384512._.sigma0 mm3 + movq mm2, mm6 + paddq mm0, mm2 + movq mm7, [w + (n)*8] + paddq mm7, mm0 + movq [w + (n)*8], mm7 +} + +macro sha384512._.round a, b, c, d, e, f, g, h, k +{ + movq mm1, [h] + movq mm3, [e] + sha384512._.Sigma1 mm3 + paddq mm1, mm0 + sha384512._.chn e, f, g + paddq mm1, mm0 + paddq mm1, [k] + paddq mm1, mm5 + movq mm7, [d] + paddq mm7, mm1 + movq [d], mm7 + movq mm3, [a] + sha384512._.Sigma0 mm3 + paddq mm1, mm0 + sha384512._.maj a, b, c + paddq mm0, mm1 + movq [h], mm0 +} + + +macro sha384512._.round_1_16 a, b, c, d, e, f, g, h, n +{ + + movq mm0, [esi + (n)*8] + movq [temp], mm0 + mov eax, dword[temp] + bswap eax + push eax + mov eax, dword[temp + 4] + bswap eax + mov dword[temp], eax + pop eax + mov dword[temp + 4], eax + movq mm0, [temp] + movq [w + (n)*8], mm0 + movq mm5, mm0 + sha384512._.round a, b, c, d, e, f, g, h, (sha384512._.table + (n)*8) +} + +macro sha384512._.round_17_64 a, b, c, d, e, f, g, h, n, rep_num +{ + sha384512._.recalculate_w n + movq mm5, [w + (n)*8] + sha384512._.round a, b, c, d, e, f, g, h, (sha384512._.table + (n+16*rep_num)*8) +} + + +proc sha384.init _ctx + mov ebx, [_ctx] + lea edi, [ebx + ctx_sha384512.hash] + mov esi, sha384._.hash_init + mov ecx, SHA384512_INIT_SIZE/4 + rep movsd + xor eax, eax + mov [ebx + ctx_sha384512.index], eax + mov [ebx + ctx_sha384512.msglen_0], eax + mov [ebx + ctx_sha384512.msglen_1], eax + mov [ebx + ctx_sha384512.msglen_2], eax + mov [ebx + ctx_sha384512.msglen_3], eax + ret +endp + + +proc sha512.init _ctx + mov ebx, [_ctx] + lea edi, [ebx + ctx_sha384512.hash] + mov esi, sha512._.hash_init + mov ecx, SHA384512_INIT_SIZE/4 + rep movsd + xor eax, eax + mov [ebx + ctx_sha384512.index], eax + mov [ebx + ctx_sha384512.msglen_0], eax + mov [ebx + ctx_sha384512.msglen_1], eax + mov [ebx + ctx_sha384512.msglen_2], eax + mov [ebx + ctx_sha384512.msglen_3], eax + ret +endp + + +proc sha384512._.block _hash +;locals +; 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 +;endl +w equ ebx + ctx_sha384512.w +A equ ebx + ctx_sha384512.A +B equ ebx + ctx_sha384512.B +C equ ebx + ctx_sha384512.C +D equ ebx + ctx_sha384512.D +E equ ebx + ctx_sha384512.E +F equ ebx + ctx_sha384512.F +G equ ebx + ctx_sha384512.G +H equ ebx + ctx_sha384512.H +temp equ ebx + ctx_sha384512.temp + + mov edi, [_hash] + movq mm0, [edi + 0x00] + movq [A], mm0 + movq mm0, [edi + 0x08] + movq [B], mm0 + movq mm0, [edi + 0x10] + movq [C], mm0 + movq mm0, [edi + 0x18] + movq [D], mm0 + movq mm0, [edi + 0x20] + movq [E], mm0 + movq mm0, [edi + 0x28] + movq [F], mm0 + movq mm0, [edi + 0x30] + movq [G], mm0 + movq mm0, [edi + 0x38] + movq [H], mm0 + + + sha384512._.round_1_16 A, B, C, D, E, F, G, H, 0 + sha384512._.round_1_16 H, A, B, C, D, E, F, G, 1 + sha384512._.round_1_16 G, H, A, B, C, D, E, F, 2 + sha384512._.round_1_16 F, G, H, A, B, C, D, E, 3 + sha384512._.round_1_16 E, F, G, H, A, B, C, D, 4 + sha384512._.round_1_16 D, E, F, G, H, A, B, C, 5 + sha384512._.round_1_16 C, D, E, F, G, H, A, B, 6 + sha384512._.round_1_16 B, C, D, E, F, G, H, A, 7 + sha384512._.round_1_16 A, B, C, D, E, F, G, H, 8 + sha384512._.round_1_16 H, A, B, C, D, E, F, G, 9 + sha384512._.round_1_16 G, H, A, B, C, D, E, F, 10 + sha384512._.round_1_16 F, G, H, A, B, C, D, E, 11 + sha384512._.round_1_16 E, F, G, H, A, B, C, D, 12 + sha384512._.round_1_16 D, E, F, G, H, A, B, C, 13 + sha384512._.round_1_16 C, D, E, F, G, H, A, B, 14 + sha384512._.round_1_16 B, C, D, E, F, G, H, A, 15 + +repeat 4 + sha384512._.round_17_64 A, B, C, D, E, F, G, H, 0, % + sha384512._.round_17_64 H, A, B, C, D, E, F, G, 1, % + sha384512._.round_17_64 G, H, A, B, C, D, E, F, 2, % + sha384512._.round_17_64 F, G, H, A, B, C, D, E, 3, % + sha384512._.round_17_64 E, F, G, H, A, B, C, D, 4, % + sha384512._.round_17_64 D, E, F, G, H, A, B, C, 5, % + sha384512._.round_17_64 C, D, E, F, G, H, A, B, 6, % + sha384512._.round_17_64 B, C, D, E, F, G, H, A, 7, % + sha384512._.round_17_64 A, B, C, D, E, F, G, H, 8, % + sha384512._.round_17_64 H, A, B, C, D, E, F, G, 9, % + sha384512._.round_17_64 G, H, A, B, C, D, E, F, 10, % + sha384512._.round_17_64 F, G, H, A, B, C, D, E, 11, % + sha384512._.round_17_64 E, F, G, H, A, B, C, D, 12, % + sha384512._.round_17_64 D, E, F, G, H, A, B, C, 13, % + sha384512._.round_17_64 C, D, E, F, G, H, A, B, 14, % + sha384512._.round_17_64 B, C, D, E, F, G, H, A, 15, % +end repeat + + + mov edi, [_hash] + movq mm0, [A] + paddq mm0, [edi + 0x00] + movq [edi + 0x00], mm0 + movq mm0, [B] + paddq mm0, [edi + 0x08] + movq [edi + 0x08], mm0 + movq mm0, [C] + paddq mm0, [edi + 0x10] + movq [edi + 0x10], mm0 + movq mm0, [D] + paddq mm0, [edi + 0x18] + movq [edi + 0x18], mm0 + movq mm0, [E] + paddq mm0, [edi + 0x20] + movq [edi + 0x20], mm0 + movq mm0, [F] + paddq mm0, [edi + 0x28] + movq [edi + 0x28], mm0 + movq mm0, [G] + paddq mm0, [edi + 0x30] + movq [edi + 0x30], mm0 + movq mm0, [H] + paddq mm0, [edi + 0x38] + movq [edi + 0x38], mm0 + + ret +restore w,A,B,C,D,E,F,G,H,temp +endp + + +sha384.update = sha384512.update +sha512.update = sha384512.update +proc sha384512.update _ctx, _msg, _size + mov ebx, [_ctx] + mov ecx, [_size] + add [ebx + ctx_sha384512.msglen_0], ecx + adc [ebx + ctx_sha384512.msglen_1], 0 + adc [ebx + ctx_sha384512.msglen_2], 0 + adc [ebx + ctx_sha384512.msglen_3], 0 + + .next_block: + mov ebx, [_ctx] + mov esi, [_msg] + mov eax, [ebx + ctx_sha384512.index] + and eax, SHA384512_BLOCK_SIZE-1 + jnz .copy_to_buf + test esi, SHA384512_ALIGN_MASK + jnz .copy_to_buf + .no_copy: + ; data is aligned, hash it in place without copying + mov ebx, [_ctx] + cmp [_size], SHA384512_BLOCK_SIZE + jb .copy_quit + lea eax, [ebx + ctx_sha384512.hash] + stdcall sha384512._.block, eax + sub [_size], SHA384512_BLOCK_SIZE + add esi, SHA384512_BLOCK_SIZE ; FIXME + jmp .no_copy + + .copy_to_buf: + lea edi, [ebx + ctx_sha384512.block] + add edi, eax + mov ecx, SHA384512_BLOCK_SIZE + sub ecx, eax + cmp [_size], ecx + jb .copy_quit + sub [_size], ecx + add [_msg], ecx + add [ebx + ctx_sha384512.index], ecx + rep movsb + lea eax, [ebx + ctx_sha384512.hash] + lea esi, [ebx + ctx_sha384512.block] + stdcall sha384512._.block, eax + jmp .next_block + + .copy_quit: + mov ebx, [_ctx] + lea edi, [ebx + ctx_sha384512.block] + mov eax, [ebx + ctx_sha384512.index] + and eax, SHA384512_BLOCK_SIZE-1 + add edi, eax + mov ecx, [_size] + add [ebx + ctx_sha384512.index], ecx + rep movsb + .quit: + + ret +endp + + +sha384.final = sha384512.final +sha512.final = sha384512.final +proc sha384512.final _ctx + mov ebx, [_ctx] + lea edi, [ebx + ctx_sha384512.block] + mov ecx, [ebx + ctx_sha384512.msglen_0] + and ecx, SHA384512_BLOCK_SIZE-1 + add edi, ecx + mov byte[edi], 0x80 + inc edi + neg ecx + add ecx, SHA384512_BLOCK_SIZE + cmp ecx, 16 + ja .last + + dec ecx + xor eax, eax + rep stosb + lea esi, [ebx + ctx_sha384512.block] + lea eax, [ebx + ctx_sha384512.hash] + stdcall sha384512._.block, eax + mov ebx, [_ctx] + lea edi, [ebx + ctx_sha384512.block] + mov ecx, SHA384512_BLOCK_SIZE+1 + .last: + dec ecx + sub ecx, 16 + xor eax, eax + rep stosb + mov eax, [ebx + ctx_sha384512.msglen_1] + shld [ebx + ctx_sha384512.msglen_0], eax, 3 + mov eax, [ebx + ctx_sha384512.msglen_2] + shld [ebx + ctx_sha384512.msglen_1], eax, 3 + mov eax, [ebx + ctx_sha384512.msglen_3] + shld [ebx + ctx_sha384512.msglen_2], eax, 3 + shl eax, 3 + bswap eax + mov dword[edi + 0], eax + mov eax, [ebx + ctx_sha384512.msglen_2] + bswap eax + mov dword[edi + 4], eax + mov eax, [ebx + ctx_sha384512.msglen_1] + bswap eax + mov dword[edi + 8], eax + mov eax, [ebx + ctx_sha384512.msglen_0] + bswap eax + mov dword[edi + 12], eax + mov ebx, [_ctx] + lea esi, [ebx + ctx_sha384512.block] + lea eax, [ebx + ctx_sha384512.hash] + stdcall sha384512._.block, eax + + mov ebx, [_ctx] + lea eax, [ebx + ctx_sha384512.hash] + stdcall sha384512._.postprocess, ebx, eax + + ret +endp + + +proc sha384512._.postprocess _ctx, _hash + mov ecx, 8 + mov esi, [_hash] + mov edi, esi + @@: + lodsd + mov ebx, eax + lodsd + bswap eax + bswap ebx + stosd + mov eax, ebx + stosd + dec ecx ; FIXME: what should I fix here? + jnz @b + emms + ret +endp + + +proc sha384.oneshot _ctx, _data, _len + stdcall sha384.init, [_ctx] + stdcall sha384.update, [_ctx], [_data], [_len] + stdcall sha384.final, [_ctx] + ret +endp + + +proc sha512.oneshot _ctx, _data, _len + stdcall sha512.init, [_ctx] + stdcall sha512.update, [_ctx], [_data], [_len] + stdcall sha512.final, [_ctx] + ret +endp + + +iglobal +align SHA384512_ALIGN +sha384._.hash_init dq 0xcbbb9d5dc1059ed8, 0x629a292a367cd507,\ + 0x9159015a3070dd17, 0x152fecd8f70e5939,\ + 0x67332667ffc00b31, 0x8eb44a8768581511,\ + 0xdb0c2e0d64f98fa7, 0x47b5481dbefa4fa4 + +sha512._.hash_init dq 0x6a09e667f3bcc908, 0xbb67ae8584caa73b,\ + 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1,\ + 0x510e527fade682d1, 0x9b05688c2b3e6c1f,\ + 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179 + +sha384512._.table dq 0x428a2f98d728ae22, 0x7137449123ef65cd,\ + 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc,\ + 0x3956c25bf348b538, 0x59f111f1b605d019,\ + 0x923f82a4af194f9b, 0xab1c5ed5da6d8118,\ + 0xd807aa98a3030242, 0x12835b0145706fbe,\ + 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2,\ + 0x72be5d74f27b896f, 0x80deb1fe3b1696b1,\ + 0x9bdc06a725c71235, 0xc19bf174cf692694,\ + 0xe49b69c19ef14ad2, 0xefbe4786384f25e3,\ + 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65,\ + 0x2de92c6f592b0275, 0x4a7484aa6ea6e483,\ + 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5,\ + 0x983e5152ee66dfab, 0xa831c66d2db43210,\ + 0xb00327c898fb213f, 0xbf597fc7beef0ee4,\ + 0xc6e00bf33da88fc2, 0xd5a79147930aa725,\ + 0x06ca6351e003826f, 0x142929670a0e6e70,\ + 0x27b70a8546d22ffc, 0x2e1b21385c26c926,\ + 0x4d2c6dfc5ac42aed, 0x53380d139d95b3df,\ + 0x650a73548baf63de, 0x766a0abb3c77b2a8,\ + 0x81c2c92e47edaee6, 0x92722c851482353b,\ + 0xa2bfe8a14cf10364, 0xa81a664bbc423001,\ + 0xc24b8b70d0f89791, 0xc76c51a30654be30,\ + 0xd192e819d6ef5218, 0xd69906245565a910,\ + 0xf40e35855771202a, 0x106aa07032bbd1b8,\ + 0x19a4c116b8d2d0c8, 0x1e376c085141ab53,\ + 0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8,\ + 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb,\ + 0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3,\ + 0x748f82ee5defb2fc, 0x78a5636f43172f60,\ + 0x84c87814a1f0ab72, 0x8cc702081a6439ec,\ + 0x90befffa23631e28, 0xa4506cebde82bde9,\ + 0xbef9a3f7b2c67915, 0xc67178f2e372532b,\ + 0xca273eceea26619c, 0xd186b8c721c0c207,\ + 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178,\ + 0x06f067aa72176fba, 0x0a637dc5a2c898a6,\ + 0x113f9804bef90dae, 0x1b710b35131c471b,\ + 0x28db77f523047d84, 0x32caab7b40c72493,\ + 0x3c9ebe0a15c9bebc, 0x431d67c49c100d4c,\ + 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a,\ + 0x5fcb6fab3ad6faec, 0x6c44198c4a475817 +endg diff --git a/programs/network/ssh/dh_gex.inc b/programs/network/ssh/dh_gex.inc index 88691a781c..320db6f0d3 100644 --- a/programs/network/ssh/dh_gex.inc +++ b/programs/network/ssh/dh_gex.inc @@ -231,7 +231,7 @@ end if mov esi, con.k_h_ctx mov edi, con.temp_ctx - mov ecx, sizeof.ctx_sha224256/4 + mov ecx, sizeof.crash_ctx rep movsd mov [con.session_id_prefix], 'A' invoke sha256_update, con.temp_ctx, con.session_id_prefix, 32+1 @@ -249,7 +249,7 @@ end if mov esi, con.k_h_ctx mov edi, con.temp_ctx - mov ecx, sizeof.ctx_sha224256/4 + mov ecx, sizeof.crash_ctx/4 rep movsd inc [con.session_id_prefix] invoke sha256_update, con.temp_ctx, con.session_id_prefix, 32+1 @@ -267,7 +267,7 @@ end if mov esi, con.k_h_ctx mov edi, con.temp_ctx - mov ecx, sizeof.ctx_sha224256/4 + mov ecx, sizeof.crash_ctx rep movsd inc [con.session_id_prefix] invoke sha256_update, con.temp_ctx, con.session_id_prefix, 32+1 @@ -285,7 +285,7 @@ end if mov esi, con.k_h_ctx mov edi, con.temp_ctx - mov ecx, sizeof.ctx_sha224256/4 + mov ecx, sizeof.crash_ctx/4 rep movsd inc [con.session_id_prefix] invoke sha256_update, con.temp_ctx, con.session_id_prefix, 32+1 @@ -303,7 +303,7 @@ end if mov esi, con.k_h_ctx mov edi, con.temp_ctx - mov ecx, sizeof.ctx_sha224256/4 + mov ecx, sizeof.crash_ctx/4 rep movsd inc [con.session_id_prefix] invoke sha256_update, con.temp_ctx, con.session_id_prefix, 32+1 @@ -321,7 +321,7 @@ end if mov esi, con.k_h_ctx mov edi, con.temp_ctx - mov ecx, sizeof.ctx_sha224256/4 + mov ecx, sizeof.crash_ctx/4 rep movsd inc [con.session_id_prefix] invoke sha256_update, con.temp_ctx, con.session_id_prefix, 32+1 diff --git a/programs/network/ssh/hmac_md5.inc b/programs/network/ssh/hmac_md5.inc index 472b43ca3b..8d7746928a 100644 --- a/programs/network/ssh/hmac_md5.inc +++ b/programs/network/ssh/hmac_md5.inc @@ -22,8 +22,8 @@ struct hmac_md5_context hash rb MD5_HASH_SIZE - ipad_ctx ctx_md5 - opad_ctx ctx_md5 + ipad_ctx crash_ctx + opad_ctx crash_ctx ends ; We will precompute partial hashes of K XOR ipad and K XOR opad, diff --git a/programs/network/ssh/hmac_sha1.inc b/programs/network/ssh/hmac_sha1.inc index 34c3830e2c..9e16eb28ec 100644 --- a/programs/network/ssh/hmac_sha1.inc +++ b/programs/network/ssh/hmac_sha1.inc @@ -22,8 +22,8 @@ struct hmac_sha1_context hash rb SHA1_HASH_SIZE - ipad_ctx ctx_sha1 - opad_ctx ctx_sha1 + ipad_ctx crash_ctx + opad_ctx crash_ctx ends ; We will precompute partial hashes of K XOR ipad and K XOR opad, diff --git a/programs/network/ssh/hmac_sha256.inc b/programs/network/ssh/hmac_sha256.inc index 61c2d0ffb4..4405a0115e 100644 --- a/programs/network/ssh/hmac_sha256.inc +++ b/programs/network/ssh/hmac_sha256.inc @@ -22,8 +22,8 @@ struct hmac_sha256_context hash rb SHA256_HASH_SIZE - ipad_ctx ctx_sha224256 - opad_ctx ctx_sha224256 + ipad_ctx crash_ctx + opad_ctx crash_ctx ends ; We will precompute partial hashes of K XOR ipad and K XOR opad, @@ -32,21 +32,21 @@ ends proc hmac_sha256_setkey ctx, key, key_length locals - k_temp rb SHA224256_BLOCK_SIZE + k_temp rb SHA256_BLOCK_SIZE endl pusha ; input esi = key, ecx=key_length mov ecx, [key_length] - cmp ecx, SHA224256_BLOCK_SIZE + cmp ecx, SHA256_BLOCK_SIZE ja .hash_it ; Key is smaller then or equal to blocksize, ; copy key to ipad mov esi, [key] lea edi, [k_temp] rep movsb - mov ecx, SHA224256_BLOCK_SIZE + mov ecx, SHA256_BLOCK_SIZE sub ecx, [key_length] jz .finish ; append zeros to the key @@ -64,13 +64,13 @@ endl mov ecx, SHA256_HASH_SIZE/4 rep movsd xor eax, eax - mov ecx, (SHA224256_BLOCK_SIZE-SHA256_HASH_SIZE)/4 + mov ecx, (SHA256_BLOCK_SIZE-SHA256_HASH_SIZE)/4 rep stosd .finish: ; xor ipad buffer with 0x36363... lea esi, [k_temp] - mov ecx, SHA224256_BLOCK_SIZE/4 + mov ecx, SHA256_BLOCK_SIZE/4 @@: xor dword[esi], 0x36363636 ; ipad constant add esi, 4 @@ -84,15 +84,15 @@ endl lea esi, [k_temp] DEBUGF 1, "HASH: " - stdcall dump_hex, esi, SHA224256_BLOCK_SIZE/4 + stdcall dump_hex, esi, SHA256_BLOCK_SIZE/4 mov ebx, [ctx] lea edi, [ebx+hmac_sha256_context.ipad_ctx] - invoke sha256_update, edi, esi, SHA224256_BLOCK_SIZE + invoke sha256_update, edi, esi, SHA256_BLOCK_SIZE ; xor opad buffer with 0x5c5c5... lea esi, [k_temp] - mov ecx, SHA224256_BLOCK_SIZE/4 + mov ecx, SHA256_BLOCK_SIZE/4 @@: xor dword[esi], 0x36363636 xor 0x5c5c5c5c ; opad constant add esi, 4 @@ -106,11 +106,11 @@ endl lea esi, [k_temp] DEBUGF 1, "HASH: " - stdcall dump_hex, esi, SHA224256_BLOCK_SIZE/4 + stdcall dump_hex, esi, SHA256_BLOCK_SIZE/4 mov ebx, [ctx] lea edi, [ebx+hmac_sha256_context.opad_ctx] - invoke sha256_update, edi, esi, SHA224256_BLOCK_SIZE + invoke sha256_update, edi, esi, SHA256_BLOCK_SIZE popa ret @@ -124,8 +124,8 @@ endp proc hmac_sha256 ctx, _data, _length locals - inner_ctx ctx_sha224256 - outer_ctx ctx_sha224256 + inner_ctx crash_ctx + outer_ctx crash_ctx endl pusha @@ -138,7 +138,7 @@ endl mov esi, [ctx] lea esi, [esi+hmac_sha256_context.ipad_ctx] lea edi, [inner_ctx] -repeat (sizeof.ctx_sha224256)/4*2 +repeat (sizeof.crash_ctx)/4*2 movsd end repeat diff --git a/programs/network/ssh/ssh.asm b/programs/network/ssh/ssh.asm index 5c1449f335..dd9947908d 100644 --- a/programs/network/ssh/ssh.asm +++ b/programs/network/ssh/ssh.asm @@ -165,8 +165,8 @@ struct ssh_connection dh_signature dd ? rb MAX_BITS/8 - temp_ctx ctx_sha224256 - k_h_ctx ctx_sha224256 + temp_ctx crash_ctx + k_h_ctx crash_ctx mpint_tmp dd ? rb MAX_BITS/8