|
| 1 | +#![cfg(all(any(blake2b, blake2s), feature = "digest"))] |
| 2 | + |
| 3 | +use digest::{Digest, FixedOutputReset}; |
| 4 | +use digest::block_api::BlockSizeUser; |
| 5 | + |
| 6 | +mod common; |
| 7 | + |
| 8 | +fn check_digest<D: Digest + BlockSizeUser + FixedOutputReset + Default>( |
| 9 | + input: &[u8], |
| 10 | + expected: &[u8], |
| 11 | + expected_block_size: usize, |
| 12 | +) { |
| 13 | + assert_eq!(<D as Digest>::output_size(), expected.len()); |
| 14 | + assert_eq!(<D as BlockSizeUser>::block_size(), expected_block_size); |
| 15 | + |
| 16 | + /* One-shot digest via associated function. */ |
| 17 | + let out = D::digest(input); |
| 18 | + assert_eq!(out.as_slice(), expected); |
| 19 | + |
| 20 | + /* Streaming via Digest::update and finalize. */ |
| 21 | + let mut hasher = D::new(); |
| 22 | + Digest::update(&mut hasher, input); |
| 23 | + let out = hasher.finalize(); |
| 24 | + assert_eq!(out.as_slice(), expected); |
| 25 | + |
| 26 | + /* Split update via Default + Update + FixedOutputReset::finalize_reset. */ |
| 27 | + let mut hasher = D::default(); |
| 28 | + if input.len() >= 2 { |
| 29 | + let mid = input.len() / 2; |
| 30 | + Digest::update(&mut hasher, &input[..mid]); |
| 31 | + Digest::update(&mut hasher, &input[mid..]); |
| 32 | + } else { |
| 33 | + Digest::update(&mut hasher, input); |
| 34 | + } |
| 35 | + let out = hasher.finalize_reset(); |
| 36 | + assert_eq!(out.as_slice(), expected); |
| 37 | + |
| 38 | + /* After reset, the same hasher should produce the same result. */ |
| 39 | + Digest::update(&mut hasher, input); |
| 40 | + let out = hasher.finalize(); |
| 41 | + assert_eq!(out.as_slice(), expected); |
| 42 | +} |
| 43 | + |
| 44 | +#[test] |
| 45 | +#[cfg(blake2b)] |
| 46 | +fn test_digest_blake2b_512() { |
| 47 | + use wolfssl_wolfcrypt::blake2_digest::Blake2b512; |
| 48 | + common::setup(); |
| 49 | + check_digest::<Blake2b512>( |
| 50 | + b"abc", |
| 51 | + b"\xBA\x80\xA5\x3F\x98\x1C\x4D\x0D\x6A\x27\x97\xB6\x9F\x12\xF6\xE9\x4C\x21\x2F\x14\x68\x5A\xC4\xB7\x4B\x12\xBB\x6F\xDB\xFF\xA2\xD1\x7D\x87\xC5\x39\x2A\xAB\x79\x2D\xC2\x52\xD5\xDE\x45\x33\xCC\x95\x18\xD3\x8A\xA8\xDB\xF1\x92\x5A\xB9\x23\x86\xED\xD4\x00\x99\x23", |
| 52 | + 128, |
| 53 | + ); |
| 54 | +} |
| 55 | + |
| 56 | +#[test] |
| 57 | +#[cfg(blake2b)] |
| 58 | +fn test_digest_blake2b_384() { |
| 59 | + use wolfssl_wolfcrypt::blake2::BLAKE2b; |
| 60 | + use wolfssl_wolfcrypt::blake2_digest::Blake2b384; |
| 61 | + common::setup(); |
| 62 | + |
| 63 | + let mut reference = BLAKE2b::new(48).expect("Error with new()"); |
| 64 | + reference.update(b"abc").expect("Error with update()"); |
| 65 | + let mut expected = [0u8; 48]; |
| 66 | + reference.finalize(&mut expected).expect("Error with finalize()"); |
| 67 | + |
| 68 | + check_digest::<Blake2b384>(b"abc", &expected, 128); |
| 69 | +} |
| 70 | + |
| 71 | +#[test] |
| 72 | +#[cfg(blake2b)] |
| 73 | +fn test_digest_blake2b_256() { |
| 74 | + use wolfssl_wolfcrypt::blake2::BLAKE2b; |
| 75 | + use wolfssl_wolfcrypt::blake2_digest::Blake2b256; |
| 76 | + common::setup(); |
| 77 | + |
| 78 | + let mut reference = BLAKE2b::new(32).expect("Error with new()"); |
| 79 | + reference.update(b"abc").expect("Error with update()"); |
| 80 | + let mut expected = [0u8; 32]; |
| 81 | + reference.finalize(&mut expected).expect("Error with finalize()"); |
| 82 | + |
| 83 | + check_digest::<Blake2b256>(b"abc", &expected, 128); |
| 84 | +} |
| 85 | + |
| 86 | +#[test] |
| 87 | +#[cfg(blake2s)] |
| 88 | +fn test_digest_blake2s_256() { |
| 89 | + use wolfssl_wolfcrypt::blake2_digest::Blake2s256; |
| 90 | + common::setup(); |
| 91 | + check_digest::<Blake2s256>( |
| 92 | + b"abc", |
| 93 | + b"\x50\x8C\x5E\x8C\x32\x7C\x14\xE2\xE1\xA7\x2B\xA3\x4E\xEB\x45\x2F\x37\x45\x8B\x20\x9E\xD6\x3A\x29\x4D\x99\x9B\x4C\x86\x67\x59\x82", |
| 94 | + 64, |
| 95 | + ); |
| 96 | +} |
| 97 | + |
| 98 | +#[test] |
| 99 | +#[cfg(blake2s)] |
| 100 | +fn test_digest_blake2s_192() { |
| 101 | + use wolfssl_wolfcrypt::blake2::BLAKE2s; |
| 102 | + use wolfssl_wolfcrypt::blake2_digest::Blake2s192; |
| 103 | + common::setup(); |
| 104 | + |
| 105 | + let mut reference = BLAKE2s::new(24).expect("Error with new()"); |
| 106 | + reference.update(b"abc").expect("Error with update()"); |
| 107 | + let mut expected = [0u8; 24]; |
| 108 | + reference.finalize(&mut expected).expect("Error with finalize()"); |
| 109 | + |
| 110 | + check_digest::<Blake2s192>(b"abc", &expected, 64); |
| 111 | +} |
| 112 | + |
| 113 | +#[test] |
| 114 | +#[cfg(blake2s)] |
| 115 | +fn test_digest_blake2s_128() { |
| 116 | + use wolfssl_wolfcrypt::blake2::BLAKE2s; |
| 117 | + use wolfssl_wolfcrypt::blake2_digest::Blake2s128; |
| 118 | + common::setup(); |
| 119 | + |
| 120 | + let mut reference = BLAKE2s::new(16).expect("Error with new()"); |
| 121 | + reference.update(b"abc").expect("Error with update()"); |
| 122 | + let mut expected = [0u8; 16]; |
| 123 | + reference.finalize(&mut expected).expect("Error with finalize()"); |
| 124 | + |
| 125 | + check_digest::<Blake2s128>(b"abc", &expected, 64); |
| 126 | +} |
0 commit comments