ZenGo-X / curv

Rust language general purpose elliptic curve cryptography.
MIT License
264 stars 111 forks source link

Fix a bug in HmacExt::verify_bigint #151

Closed elichai closed 1 year ago

elichai commented 2 years ago

Every once in a while I get tests failing like this a few weeks ago:

---- cryptographic_primitives::hashing::ext::test::create_hmac_test_blake2b stdout ----
thread 'cryptographic_primitives::hashing::ext::test::create_hmac_test_blake2b' panicked at 'assertion failed: Hmac::<H>::new_bigint(&key).chain_bigint(&BigInt::from(10)).verify_bigint(&result1).is_ok()', src/cryptographic_primitives/hashing/ext.rs:306:9

---- cryptographic_primitives::hashing::ext::test::create_hmac_test_sha512 stdout ----
thread 'cryptographic_primitives::hashing::ext::test::create_hmac_test_sha512' panicked at 'assertion failed: Hmac::<H>::new_bigint(&key).chain_bigint(&BigInt::from(10)).verify_bigint(&result1).is_ok()', src/cryptographic_primitives/hashing/ext.rs:306:9

failures:
    cryptographic_primitives::hashing::ext::test::create_hmac_test_blake2b
    cryptographic_primitives::hashing::ext::test::create_hmac_test_sha512

test result: FAILED. 475 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out; finished in 4.16s

and this today:

---- cryptographic_primitives::hashing::ext::test::create_hmac_test_sha3_512 stdout ----
thread 'cryptographic_primitives::hashing::ext::test::create_hmac_test_sha3_512' panicked at 'assertion failed: Hmac::<H>::new_bigint(&key).chain_bigint(&BigInt::from(10)).verify_bigint(&result1).is_ok()', src/cryptographic_primitives/hashing/ext.rs:306:9

The reason was that we converted BigInt to a vector and then compared that means that if the least significant byte was 0 it returned 32 bytes instead of 33 (or 63 bytes instead of 64).

We fix it by copying into an array of the right size.