intel / isa-l_crypto

Other
271 stars 80 forks source link

MD5 digest byte order is different from SHA1 and SHA256 #121

Closed gh-andre closed 4 months ago

gh-andre commented 1 year ago

When I compute SHA1 and SHA256 via *_mb.h functions in v2.24.0, hashes are generated as uint32_t arrays, so bytes need to be rearranged to yield a normal byte-ordered digest. However, MD5 is computed as a normal byte-ordered digest right away, which creates challenges for a generic wrapper for multi-buffer hashing functionality because there is no way to tell which hash type produces which byte order.

For example, hashing ABC yields these bytes sequences on an x64 Intel processor.

SHA1: 3c01bdbb26f358bab27f267924aa2c9a03fcfdb8

bytes: bb bd 01 3c ba 58 f3 26 79 26 7f b2 9a 2c aa 24 b8 fd fc 03
uint32_t[]: 3c01bdbb 26f358ba b27f2679 24aa2c9a 03fcfdb8

SHA256: b5d4045c3f466fa91fe2cc6abe79232a1a57cdf104f7a26e716e0a1e2789df78

bytes: 5c 04 d4 b5 a9 6f 46 3f 6a cc e2 1f 2a 23 79 be f1 cd 57 1a 6e a2 f7 04 1e 0a 6e 71 78 df 89 27
uint32_t[]: b5d4045c 3f466fa9 1fe2cc6a be79232a 1a57cdf1 04f7a26e 716e0a1e 2789df78

MD5: 902fbdd2b1df0c4f70b4a5d23525e932

bytes: 90 2f bd d2 b1 df 0c 4f 70 b4 a5 d2 35 25 e9 32
uint32_t[]: d2bd2f90 4f0cdfb1 d2a5b470 32e92535

The byte order conversion is easy enough and documenting it for different types of hashes and processor architectures could be the solution for this issue, but having either consistent order or some indicator returned to indicate byte order would be more self-contained.