The MASM Forum

General => The Laboratory => Topic started by: guga on April 30, 2020, 01:49:44 PM

Title: SHA-3 Hash with Keccak
Post by: guga on April 30, 2020, 01:49:44 PM
Hi Guys

here is a SHA-3 hashing algorithm with or without Keccak encryption. The algorithm is consistent with the SHA-3 standard plus the keccak routine to enforce security.

The algorithm produces 224, 256, 384 and 512 bits of data to be used as a signature of whatever file or text that is needed.

I built one biased  to these ones:

http://gauss.ececs.uc.edu/Courses/c6053/lectures/Hashing/sha3
https://github.com/magurosan/sha3-odzhan

The usage of the algorithm consists on a single function named "Sha3Start" to create the hashing of the inputted data.

Sha3Start

                The function creates a Secure Hash Algorithm of level "3" (SHA-3) used to create a digital signature of texts, files etc. Currently this version is biased on the creation of strings, but can easily be ported to create the hash of any data as well.

Parameters

         InputString - The inputted null terminated string to compute the SHA-3 hash of it.

         SHA3Type - The type of desired hashing bits to be created. It can be one of the following equates.
                     Equate Name         Equate Value       Description
                     TYPE_SHA3_224             0                    Create a 224 Bit hash
                     TYPE_SHA3_256             1                    Create a 256 Bit hash
                     TYPE_SHA3_384             2                    Create a 384 Bit hash
                     TYPE_SHA3_512             3                    Create a 512 Bit hash

          KeccakFlag - A integer used as a flag to enable or disable the usage of additional keccak encryption. It can be one of the following equates.  (Of course, the equate can also be replaced simply as a TRUE/FALSE ones)
                     Equate Name                Equate Value       Description
                     SHA3_FLAGS_NONE             0                      Don´t use Keccak
                     SHA3_FLAGS_KECCAK          1                     Use Keccak

          Return Values:
                  On exiting, the function will return in eax a chunk of 200 Bytes where the hash was stored. Of course, you can also represent it a form of 25 Qwords (or any other array of bytes, words, dwords etc) as well when displaying the returned value.

         Remarks - Since the returned value is a buffer limited to 200 bytes, it means (in theory) that we can produce a maximum hash of 6400 bits (32*200) instead only the limit of 512 i used here at this function.
The only thing that do matters is that the amount of bits to be calculated must be a multiple of 2. So, 228, 384. 512, 100 and a maximum of 6400 bits. But for other types i´ll need to adapt the code on newer versions.



Example of usage:

szTestString  db "guga",0

invoke Sha3Start, ADDR szTestString, TYPE_SHA3_256, SHA3_FLAGS_NONE


To properly convert the data to a hexadecimal string format, all you have to do is create a function to convert byte to hexa char and count the amount of bytes used to export the string.
Ex: If input is 224 Bits, then the hash is using 7 dwords (28 bytes longs)
If you are using 256 bits  then the hash is using 8 dwords (32 bytes longs)
and so on.

To make a bit easier to follow i included a function called DisplaySHA3 to export the hash on a messagebox.

Attached files in Rosasm syntax and masm syntax.

RosAsm version the source code is embedded to it (as usual)
Masm version. Included the executable and also the asm source to be assembled. (Needs ml.exe version bigger then 6  to assemble, i presume)

if someone can help, please i´m trying to optimize the algorithm a bit more and also seeing if it will really needs to use a function called "Sha3_Update_Endian_Independent" wicch, apparently it will never be used in windows when dealing with string hashing  ?

And also, can someone please benchmark the algorithm ?


Note: You can also use https://md5calc.com/hash/sha3-512 to check if the results are correct

Also, i´ll later clean up the code and insert more comments about it to be more understandable.