Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement SerializableState for hashes #385

Closed
wants to merge 15 commits into from

Conversation

rpiasetskyi
Copy link

@rpiasetskyi rpiasetskyi commented Aug 10, 2022

The internal state of different hashes can be used for serialization.

sha2/src/lib.rs Outdated
pub type Sha512_224 = CoreWrapper<Sha512_224Core>;
/// SHA-512/224 core serialized state.
pub type Sha512_224CoreSerializedState =
GenericArray<u8, <Sha512_224Core as HashCoreSerializableState>::SerializedStateSize>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to introduce a whole bunch of type aliases for serialized states.

use hex_literal::hex;
use sha2::{Digest, Sha224, Sha256, Sha384, Sha512, Sha512_224, Sha512_256};

macro_rules! test_serializable_state {
($name:ident, $hasher:ty) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add tests which check stability of the serialized state, i.e. that every version of this crate results in the same serialized state for a given hasher state.

Copy link
Author

@rpiasetskyi rpiasetskyi Sep 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test macro is moved to traits, added expected_serialized_state. RustCrypto/traits@793f9f2.

let mut h = <$hasher>::new();

// First part of data.
feed_rand_16mib(&mut h);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

16 MiB is not a great value for testing serialization, we should use an odd number of bytes to check that buffer gets serialized properly. Also it's a bit too big in my opinion, we can simply use small fixed strings with length equal to block size plus several bytes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test macro is moved to traits, input data is changed to BlockSize + 1.
RustCrypto/traits@793f9f2.

@rpiasetskyi rpiasetskyi changed the title sha2: Implement Sha256CoreState and Sha512CoreState Implement SerializableState for hashes Sep 4, 2022
@rpiasetskyi
Copy link
Author

Could you please suggest the best solution for tests if the expected values are very long?

digest::hash_serialization_test!(md5_serialization, Md5, 
    hex!("e5ca2295db93d6dd07ab990fcad2219e00000000000000010113000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"));
digest::hash_serialization_test!(
    md5_serialization,
    Md5,
    [
        0xe5, 0xca, 0x22, 0x95, 0xdb, 0x93, 0xd6, 0xdd, 0x07, 0xab, 0x99, 0x0f, 0xca, 0xd2, 0x21,
        0x9e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x13, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    ]
);

@newpavlov
Copy link
Member

You can split it into several lines:

let bytes = hex!(
    "0001020304050607"
    "08090a0b0c0d0e0f"
);
// equivalent to:
let bytes = hex!("000102030405060708090a0b0c0d0e0f");

It requires hex-literal v0.3.3.

@rpiasetskyi
Copy link
Author

Is it okay if I bump hex-literal? This change is moving MSRV of crates to 1.45.
If yes, what is the preferable width of the line?

@newpavlov
Copy link
Member

Ah, I forgot about that. In this case simply use new lines:

let bytes = hex!("
    0001020304050607
    08090a0b0c0d0e0f
");

Unfortunately, rustfmt does not like it too much, so you may need to add #[rustfmt::ignore] or use an alternative formatting choice.

md2/src/lib.rs Outdated Show resolved Hide resolved
13131313131313131313131313131313
01130000000000000000000000000000
00000000000000000000000000000000
00
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm... This redundant byte for eager hashes is certainly an eyesore. It could be worth to add serialize/deserialize methods to the block-buffer crate.

tarcieri pushed a commit that referenced this pull request Mar 19, 2024
This is a rebase of #385 on master.

RustCrypto/traits#1369 introduced a
`SerializableState` trait to serialize the hasher. This implements it
for each of the hashers we have.

Co-authored-by: Ruslan Piasetskyi <ruslan.piasetskyi@esrlabs.com>
@tarcieri
Copy link
Member

Merged as #574

@tarcieri tarcieri closed this Mar 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants