SHA and HMAC functions ported to Hollywood

Show off your project created with Hollywood
User avatar
Allanon
Posts: 732
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

SHA and HMAC functions ported to Hollywood

Post by Allanon »

Hello all!
I've been melting my brain for some time trying to bring hashing functions to Hollywood and I finally succeeded, but only after endless debugging sessions :)
You can download the library from Github, here:
https://github.com/Allanon71/lib_sha

The port for Hollywood I brought is based from the source code of Egor-Skriptunoff, his original project is here:
https://github.com/Egor-Skriptunoff/pure_lua_SHA

Here is the list of the implemented (and tested) functions, all of them are stored inside the sha table:

Code: Select all

sha (the main storage)
 .md5 (supported natively by Hollywood but ported as an exercise)
 .sha1
 .sha224
 .sha256
 .sha512_224
 .sha512_256
 .sha384
 .sha512
 .sha3_224
 .sha3_256
 .sha3_384
 .sha3_512
 .shake128
 .shake256
 .hmac
 .hex2bin
 .base642bin
 .bin2base64
Let me know if there are problems, the porting wasn't trivial.

See the post on my Patreon page: https://www.patreon.com/posts/58242374

Happy coding! :)
matty47
Posts: 18
Joined: Fri May 21, 2021 10:28 am

Re: SHA and HMAC functions ported to Hollywood

Post by matty47 »

Thank you for the hard work!
User avatar
airsoftsoftwair
Posts: 5425
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: SHA and HMAC functions ported to Hollywood

Post by airsoftsoftwair »

Great job, thanks for sharing!
User avatar
lazi
Posts: 625
Joined: Thu Feb 24, 2011 11:08 pm

Re: SHA and HMAC functions ported to Hollywood

Post by lazi »

Ohohoo! That is great!
Thank you Allanon for sharing it with the public. It could serve the next step for one my projects.
I'll tell you about that if it happens.

Thanks again!
User avatar
lazi
Posts: 625
Joined: Thu Feb 24, 2011 11:08 pm

Re: SHA and HMAC functions ported to Hollywood

Post by lazi »

I have just tried it and the tests are failing.
The file I have downloaded:
lib_sha.hws 96450 bytes
ed9a71153bcd38304aa2ef881e0caf8c lib_sha.hws md5sum

Run after uncomment last line:

Code: Select all

+---------------------------------+
| TESTING...                      |
+---------------------------------+
Tool:Hollywood/Projects/Szamlazz.hu/lib_sha.hws:1763: Assertion failed!
What is wrong here? (OS4)
User avatar
lazi
Posts: 625
Joined: Thu Feb 24, 2011 11:08 pm

Re: SHA and HMAC functions ported to Hollywood

Post by lazi »

Can it be little endian only?
If so, could you point me parts that could be affected?

Edit:
It is obviously an endian problem :-)
User avatar
Allanon
Posts: 732
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

Re: SHA and HMAC functions ported to Hollywood

Post by Allanon »

@lazi
Unfortunately I don't have time to have a look at it right now but if IIRC the original implementation should not depend on endianess... the problem could depend on some functions I used to mimic the original Lua functions (some of them are really hacked ones :) ), have a look at the beginning of the module where all custom funcs are defined, let me know if you find the issue! :)
User avatar
Allanon
Posts: 732
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

Re: SHA and HMAC functions ported to Hollywood

Post by Allanon »

Hello all :)
As you can see in the "bug report" subforum I've found a bug with bitwise operators that was returning a full 32bit value if the number is bigger than 32bit. This is happening only on my Raspberry Pi 4 and not on Windows where the bit greater than 32 bit are simply ignored.
I've imopplemented a software workaround to deal with this problem that reduces mathematically numbers bigger than 32bit to 32bit so that bitwise operations can be executed without issues.
I've tested the code on my Pi4 and it is working fine even if a little slower.

Check it on my GitHub : https://github.com/Allanon71/lib_sha
User avatar
lazi
Posts: 625
Joined: Thu Feb 24, 2011 11:08 pm

Re: SHA and HMAC functions ported to Hollywood

Post by lazi »

Dear @Allanon!

I think you have solved the OS4 related problem. Thank you!
The new version seems to work as intended.
Here is the output of the test_hash_lib():

+---------------------------------+
| TESTING... |
+---------------------------------+
=== sha256 ===
Some tests strings...
Chunk-by-chunk loading...
One liner test...
Empty string...
Two parallel computations...
zeros...
aa...a...
Negative byte values...
aa...a... chunk-by-chunk mode
aa...a... as whole string
80 lines of 80 zeroes each) in chunk-by-chunk mode
=== sha512 ===
Some test strings...
1-140 a letters
Negative byte values...
=== MD5 ===
Performing tests...
=== SHA1 ===
=== SHA3 ===
Performing tests...
Testing shake256...
Testing shake128...
Testing shake-stream...
Testing shake-stream 2...
=== HMAC ===
Testing hmac...
Testing hmac chunks...
Testing hmac larger blocks...
=== Test Base64 ===
Encoding...
Decoding...
Elapsed: 40065ms
*** ALL TESTS PASSED ***
User avatar
lazi
Posts: 625
Joined: Thu Feb 24, 2011 11:08 pm

Re: SHA and HMAC functions ported to Hollywood

Post by lazi »

Speedwise my Peg2/G4@1.00 GHz does the test in 33872ms without linehook.
Don't understand correctly your remarks on speed at the end of the script, but seems there is better times on a Pi4.
Not the speed was my target to choose this system :-)
I will check it on the X5000 later...
Post Reply