Ok Concord, I have had another look into this and worked out how to set up the hash data structure.
You need to call either EVP_sha1(), EVP_sha256(), or EVP_get_digestbyname( name );
The external function calls require many of the Jade Binaries to be declared of fixed size.
For that reason you need to code separate external calls for different hash sizes.
Here is a worked example for HMAC-SHA-1 and HMAC-SHA-256 using test vectors from RFCs 2202 and 4231.
It is just a quick test - not production code by any means...
External function definitions:
Code: Select all
evp_getDigestByName( pDigestName : String ) : Binary[72] is EVP_get_digestbyname in libeay32;
get_evp_sha1() : Binary[72] is EVP_sha1 in libeay32;
get_evp_sha256() : Binary[72] is EVP_sha256 in libeay32;
hmac_sha1( hash_func : Binary[72] input;
key : Binary;
key_len : Integer;
data : Binary;
data_len : Integer;
result : Binary[20] output;
res_len : Integer output ) is HMAC in libeay32;
hmac_sha256( hash_func : Binary[72] input;
key : Binary;
key_len : Integer;
data : Binary;
data_len : Integer;
result : Binary[32] output;
res_len : Integer output ) is HMAC in libeay32;
The JadeScript. You can either: (a) call evp_getDigestByName( "hash name" ), or (b) call get_evp_sha1() or get_evp_sha256(), as appropriate.
Code: Select all
test_libeay32_hmac();
constants
// test vector from RFC2202 for HMAC-SHA-1
KEY1 = #[0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b]; // 20 bytes
DATA1 = "Hi There".Binary;
RESULT1 = #[b6 17 31 86 55 05 72 64 e2 8b c0 b6 fb 37 8c 8e f1 46 be 00]; //20 bytes
// test vector from RFC4231 for HMAC-SHA-256
KEY2 = #[0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b]; // 20 bytes
DATA2 = "Hi There".Binary;
RESULT2 = #[b0 34 4c 61 d8 db 38 53 5c a8 af ce af 0b f1 2b
88 1d c2 00 c9 83 3d a7 26 e9 37 6c 2e 32 cf f7]; // 32 bytes
vars
evp_md : Binary[72];
result1 : Binary[20];
result2 : Binary[32];
resLen : Integer;
begin
// test 1 - HMAC-SHA-1
// evp_md := call evp_getDigestByName( "sha1" ); // either of these work
evp_md := call get_evp_sha1();
call hmac_sha1( evp_md, KEY1, KEY1.length, DATA1, DATA1.length, result1, resLen );
write "HMAC-SHA-1: " & resLen.String & " bytes" & result1.display;
write "result = " & (result1 = RESULT1).String & CrLf;
// test 2 - HMAC-SHA-256
// evp_md := call evp_getDigestByName( "sha256" ); // either of these work
evp_md := call get_evp_sha256();
call hmac_sha256( evp_md, KEY2, KEY2.length, DATA2, DATA2.length, result2, resLen );
write "HMAC-SHA-256: " & resLen.String & " bytes" & result2.display;
write "result = " & (result2 = RESULT2).String;
end;
p.s. I'm not 100% sure on the length of
evp_md being 70 bytes. I originally figured 64 bytes, but that still caused a crash. It works for me.
(Edited on 16/08/2012 to correct size of evp_md structure from 70 to 72 bytes)