Anyone implemented a sha1 hash function?

Forums for specific tips, techniques and example code
advaro
Posts: 19
Joined: Mon Oct 10, 2016 5:59 pm

Re: Anyone implemented a sha1 hash function?

Postby advaro » Thu Jun 20, 2019 4:27 pm

Hi - just have the need at the moment to incorporate a HMAC-SHA-1 and have managed to get this working based on the examples in this post.
Many thanks.

But I can't get it to work on a 64 bit client. It works perfect for a 32 bit client, but gives assertion errors and kills Jade when run from a 64 bit client.
I have attached the error message that pops up. It mentions size of the key, so being a total novice with external functions, I assumed it would be as simple as replacing the Integer with Integer64 but no such luck.
The get_evp_sha1 call works, it's the hmac_sha1 one that causes the error.

Any help or pointers to getting this working would be hugely appreciated.
Thanks
Attachments
error message.png
error message.png (9.04 KiB) Viewed 7211 times

murray
Posts: 144
Joined: Fri Aug 14, 2009 6:58 pm
Location: New Plymouth, New Zealand

Re: Anyone implemented a sha1 hash function?

Postby murray » Thu Jun 20, 2019 8:16 pm

Hi @advaro,

To upgrade the 32-bit SHA1 library call to 64-bit simply requires changing the Integer parameter in the library call to Integer64. I think it is for the length, but I am at home and cannot access my code. The reason for this is that, for a 64-bit assembly, the libeay32 source is recompiled with 64-bit Integers as the default. (Note that the DLL is still called libeay32.dll).
I had to make this change in our system when we upgraded to 64-bit Jade and it is quite simple.

Have a look at the SHA2 thread here: https://forums.jadeworld.com/viewtopic.php?f=9&t=2194. It may have enough information for you. If not, post back here and I will get you some more information.
Murray (N.Z.)

advaro
Posts: 19
Joined: Mon Oct 10, 2016 5:59 pm

Re: Anyone implemented a sha1 hash function?

Postby advaro » Fri Jun 21, 2019 10:32 am

Thanks Murray - I thought changing to Integer64 was all I needed to do but that still gives the assertion screen shot in my last post.

Have tried all combinations of Integer and Integer64 without any luck.
My function is:
hmac_sha1( hash_func : Binary[72] input;
key : Binary;
key_len : Integer64;
data : Binary;
data_len : Integer64;
result : Binary[20] output;
res_len : Integer64 output ) is "HMAC" in libeay32;

And my JadeScript to test is:

test_libeay32_hmac();

vars
evp_md : Binary[72];
result : Binary[20];
resLen : Integer64;
key, string : String;
begin
// test 1 - HMAC-SHA-1
string := 'test string';
key := 'key';

evp_md := call get_evp_sha1();
call hmac_sha1( evp_md, key.Binary, key.length.Integer64, string.Binary, string.length.Integer64, result, resLen);
write result.base64Encode;
end;

Thanks
Stephen

murray
Posts: 144
Joined: Fri Aug 14, 2009 6:58 pm
Location: New Plymouth, New Zealand

Re: Anyone implemented a sha1 hash function?

Postby murray » Fri Jun 21, 2019 8:40 pm

Hi Stephen,

Sorry, I've been busy. You will see from this thread that I just used the SHA1 call and implemented the HMAC myself. There was a need for us to do it that way. There may be additional steps, such as initialising a keyblock, when you call the HMAC_SHA1. I know that is the case with the 3DES encryption calls.

Before I can look into this in any detail, you can download the source code for libeay32 from www.openssl.org. This will give you the definitions of the calls, no need to bother with the internal implementations. Take note of any comments therein.

Cheers.
Have a good weekend.
Murray (N.Z.)

murray
Posts: 144
Joined: Fri Aug 14, 2009 6:58 pm
Location: New Plymouth, New Zealand

Re: Anyone implemented a sha1 hash function?

Postby murray » Thu Jun 27, 2019 9:13 am

Hi Stephen,

Did you check and adjust the size of your evp_md variable to allow for 64 bit? Usually a structure will have some ints in it which need to be increased from 4 bytes to 8 bytes. I haven't dug down into the source files yet, but it would be important to find out what the size of the EVP_MD definition is.

Murray.
Murray (N.Z.)

murray
Posts: 144
Joined: Fri Aug 14, 2009 6:58 pm
Location: New Plymouth, New Zealand

Re: Anyone implemented a sha1 hash function?

Postby murray » Thu Jun 27, 2019 1:39 pm

Hi Stephen / @advaro,

After a quick look at the openssl source it looks to me like EVP_MD is defined as 'struct ev_md_st'.
By my count, there are 13 ints and 1 long in the struct. For 32-bit this would require 60 bytes. For 64-bit this would require 112 bytes.

You have defined it as 72 bytes which, if correct, means that I am out by a bit. However this illustrates the need to allow for a bigger allocation of bytes for the structure in 64-bit mode. Also, allowance has to be made for word alignment and padding which could increase overall size.

regards,
Murray.
Murray (N.Z.)


Return to “Tips and Techniques”

Who is online

Users browsing this forum: No registered users and 2 guests

cron