SWI-Prolog / packages-ssl

The SWI-Prolog SSL interface
5 stars 14 forks source link

Wrong hmac values when the key contains "null" bytes #170

Closed jinwoo closed 8 months ago

jinwoo commented 8 months ago

For example, crypto_data_hash gives the same hash value for keys [1,0,1] and [1]. I notice that strlen is used to calculate the key length: https://github.com/SWI-Prolog/packages-ssl/blob/acb9607626e594db394e461f061a01546244dc93/crypto4pl.c#L396 and https://github.com/SWI-Prolog/packages-ssl/blob/acb9607626e594db394e461f061a01546244dc93/crypto4pl.c#L407. It'd work when the key is a string but wouldn't work when the key is a binary data.

?- Key = [1], crypto_data_hash(`foo`, Hash, [algorithm(sha256),hmac(Key)]).
Key = [1],
Hash = '63a592e348d812a8027985ecfc6e0df958e9177437e59a182037ada35a4636e8'.

?- Key = [1,0,1], crypto_data_hash(`foo`, Hash, [algorithm(sha256),hmac(Key)]).
Key = [1, 0, 1],
Hash = '63a592e348d812a8027985ecfc6e0df958e9177437e59a182037ada35a4636e8'.
JanWielemaker commented 8 months ago

This issue has been mentioned on SWI-Prolog. There might be relevant details there:

https://swi-prolog.discourse.group/t/wrong-hmac-values-from-crypto-data-hash/7256/1