wolfssl-wolfcrypt 2.0.0

Rust wrapper for wolfssl C library cryptographic functionality
#[cfg(any(blake2b, blake2s))]
use wolfssl_wolfcrypt::blake2::*;
#[cfg(any(blake2b, blake2s))]
use wolfssl_wolfcrypt::sys;

#[test]
#[cfg(blake2b)]
fn test_blake2b() {
    let expected_hashes: [&[u8]; 3] = [
        &[
            0x78, 0x6A, 0x02, 0xF7, 0x42, 0x01, 0x59, 0x03,
            0xC6, 0xC6, 0xFD, 0x85, 0x25, 0x52, 0xD2, 0x72,
            0x91, 0x2F, 0x47, 0x40, 0xE1, 0x58, 0x47, 0x61,
            0x8A, 0x86, 0xE2, 0x17, 0xF7, 0x1F, 0x54, 0x19,
            0xD2, 0x5E, 0x10, 0x31, 0xAF, 0xEE, 0x58, 0x53,
            0x13, 0x89, 0x64, 0x44, 0x93, 0x4E, 0xB0, 0x4B,
            0x90, 0x3A, 0x68, 0x5B, 0x14, 0x48, 0xB7, 0x55,
            0xD5, 0x6F, 0x70, 0x1A, 0xFE, 0x9B, 0xE2, 0xCE
        ],
        &[
            0x2F, 0xA3, 0xF6, 0x86, 0xDF, 0x87, 0x69, 0x95,
            0x16, 0x7E, 0x7C, 0x2E, 0x5D, 0x74, 0xC4, 0xC7,
            0xB6, 0xE4, 0x8F, 0x80, 0x68, 0xFE, 0x0E, 0x44,
            0x20, 0x83, 0x44, 0xD4, 0x80, 0xF7, 0x90, 0x4C,
            0x36, 0x96, 0x3E, 0x44, 0x11, 0x5F, 0xE3, 0xEB,
            0x2A, 0x3A, 0xC8, 0x69, 0x4C, 0x28, 0xBC, 0xB4,
            0xF5, 0xA0, 0xF3, 0x27, 0x6F, 0x2E, 0x79, 0x48,
            0x7D, 0x82, 0x19, 0x05, 0x7A, 0x50, 0x6E, 0x4B
        ],
        &[
            0x1C, 0x08, 0x79, 0x8D, 0xC6, 0x41, 0xAB, 0xA9,
            0xDE, 0xE4, 0x35, 0xE2, 0x25, 0x19, 0xA4, 0x72,
            0x9A, 0x09, 0xB2, 0xBF, 0xE0, 0xFF, 0x00, 0xEF,
            0x2D, 0xCD, 0x8E, 0xD6, 0xF8, 0xA0, 0x7D, 0x15,
            0xEA, 0xF4, 0xAE, 0xE5, 0x2B, 0xBF, 0x18, 0xAB,
            0x56, 0x08, 0xA6, 0x19, 0x0F, 0x70, 0xB9, 0x04,
            0x86, 0xC8, 0xA7, 0xD4, 0x87, 0x37, 0x10, 0xB1,
            0x11, 0x5D, 0x3D, 0xEB, 0xBB, 0x43, 0x27, 0xB5
        ],
    ];

    for (i, expected_hash) in expected_hashes.iter().enumerate() {
        let mut blake2b = BLAKE2b::new(expected_hash.len()).expect("Error with new()");
        let mut input = vec![0u8; i];
        for idx in 0..input.len() {
            input[idx] = idx as u8;
        }
        blake2b.update(&input).expect("Error with update()");
        let mut hash = [0u8; 64];
        blake2b.finalize(&mut hash).expect("error with finalize()");
        assert_eq!(hash, *expected_hash);
    }
}

#[test]
#[cfg(blake2b)]
fn test_blake2b_finalize_empty_buffer() {
    let mut blake2b = BLAKE2b::new(64).expect("Error with new()");
    let mut hash: [u8; 0] = [];
    let rc = blake2b.finalize(&mut hash).expect_err("finalize() should fail");
    assert_eq!(rc, sys::wolfCrypt_ErrorCodes_BUFFER_E);
}

#[test]
#[cfg(blake2b_hmac)]
fn test_blake2b_hmac() {
    let key1 = [0x41u8, 0x42, 0x43, 0x44];
    let message1 = [0x48u8, 0x65, 0x6c, 0x6c, 0x6f];
    let expected1 = [
        0x46u8, 0x76, 0xbb, 0x0e, 0xf8, 0xa1, 0x56, 0x33,
        0xde, 0xdc, 0x44, 0xe3, 0x2b, 0xf3, 0xee, 0x5b,
        0x5f, 0x7f, 0x04, 0x00, 0x2c, 0xaa, 0xd4, 0x93,
        0xc6, 0xa6, 0xb4, 0xf3, 0x14, 0x8d, 0x6d, 0x9c,
        0x6a, 0x12, 0x02, 0x85, 0x66, 0xed, 0x9b, 0x5d,
        0x8d, 0x0e, 0x3d, 0xf4, 0x78, 0xee, 0x5a, 0xf6,
        0x2f, 0x97, 0xa5, 0x77, 0x88, 0x8c, 0xc4, 0x66,
        0x46, 0xb1, 0xba, 0x51, 0x29, 0x19, 0xd7, 0xaa,
    ];
    let key2 = [
        0x30u8, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42,
        0x43, 0x44, 0x45, 0x46, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
        0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x30, 0x31, 0x32, 0x33,
        0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46,
        0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42,
        0x43, 0x44, 0x45, 0x46, 0x30, 0x31, 0x32, 0x33
    ];
    let message2 = [
        0x61u8, 0x62, 0x63, 0x64, 0x62, 0x63, 0x64, 0x65, 0x63, 0x64, 0x65, 0x66,
        0x64, 0x65, 0x66, 0x67, 0x65, 0x66, 0x67, 0x68, 0x66, 0x67, 0x68, 0x69,
        0x67, 0x68, 0x69, 0x6a, 0x68, 0x69, 0x6a, 0x6b, 0x69, 0x6a, 0x6b, 0x6c,
        0x6a, 0x6b, 0x6c, 0x6d, 0x6b, 0x6c, 0x6d, 0x6e, 0x6c, 0x6d, 0x6e, 0x6f,
        0x6d, 0x6e, 0x6f, 0x70, 0x6e, 0x6f, 0x70, 0x71
    ];
    let expected2 = [
        0x2au8, 0xda, 0xf6, 0x94, 0x79, 0xce, 0xe2, 0xd2,
        0x5d, 0x89, 0x8b, 0xd7, 0x0d, 0xbc, 0x11, 0x1f,
        0x98, 0x99, 0xe0, 0x17, 0x7c, 0x5b, 0x8f, 0x94,
        0xf5, 0x95, 0xbc, 0x1b, 0xb1, 0x95, 0xe8, 0x60,
        0xbb, 0x29, 0xa4, 0xd9, 0x27, 0x2e, 0x00, 0xea,
        0xba, 0xc3, 0x3e, 0xe6, 0x9c, 0xc7, 0xd7, 0x8d,
        0x69, 0xc7, 0xb4, 0xf7, 0x31, 0x4a, 0xb1, 0xf0,
        0x3c, 0xed, 0x06, 0x49, 0x6f, 0x46, 0x99, 0xea,
    ];

    let mut out1 = [0u8; 64];
    BLAKE2bHmac::hmac(&message1, &key1, &mut out1).expect("Error with hmac()");
    assert_eq!(out1, expected1);

    let mut out2 = [0u8; 64];
    BLAKE2bHmac::hmac(&message2, &key2, &mut out2).expect("Error with hmac()");
    assert_eq!(out2, expected2);

    let mut hmac_blake2b = BLAKE2bHmac::new(&key1).expect("Error with new()");
    hmac_blake2b.update(&message1[0..4]).expect("Error with update()");
    hmac_blake2b.update(&message1[4..]).expect("Error with update()");
    let mut out1 = [0u8; 64];
    hmac_blake2b.finalize(&key1, &mut out1).expect("Error with finalize()");
    assert_eq!(out1, expected1);

    let mut hmac_blake2b = BLAKE2bHmac::new(&key2).expect("Error with new()");
    hmac_blake2b.update(&message2[0..48]).expect("Error with update()");
    hmac_blake2b.update(&message2[48..]).expect("Error with update()");
    let mut out2 = [0u8; 64];
    hmac_blake2b.finalize(&key2, &mut out2).expect("Error with finalize()");
    assert_eq!(out2, expected2);
}

#[test]
#[cfg(blake2s)]
fn test_blake2s() {
    let expected_hashes: [&[u8]; 3] = [
        &[
            0x69, 0x21, 0x7a, 0x30, 0x79, 0x90, 0x80, 0x94,
            0xe1, 0x11, 0x21, 0xd0, 0x42, 0x35, 0x4a, 0x7c,
            0x1f, 0x55, 0xb6, 0x48, 0x2c, 0xa1, 0xa5, 0x1e,
            0x1b, 0x25, 0x0d, 0xfd, 0x1e, 0xd0, 0xee, 0xf9,
        ],
        &[
            0xe3, 0x4d, 0x74, 0xdb, 0xaf, 0x4f, 0xf4, 0xc6,
            0xab, 0xd8, 0x71, 0xcc, 0x22, 0x04, 0x51, 0xd2,
            0xea, 0x26, 0x48, 0x84, 0x6c, 0x77, 0x57, 0xfb,
            0xaa, 0xc8, 0x2f, 0xe5, 0x1a, 0xd6, 0x4b, 0xea,
        ],
        &[
            0xdd, 0xad, 0x9a, 0xb1, 0x5d, 0xac, 0x45, 0x49,
            0xba, 0x42, 0xf4, 0x9d, 0x26, 0x24, 0x96, 0xbe,
            0xf6, 0xc0, 0xba, 0xe1, 0xdd, 0x34, 0x2a, 0x88,
            0x08, 0xf8, 0xea, 0x26, 0x7c, 0x6e, 0x21, 0x0c,
        ],
    ];

    for (i, expected_hash) in expected_hashes.iter().enumerate() {
        let mut blake2s = BLAKE2s::new(expected_hash.len()).expect("Error with new()");
        let mut input = vec![0u8; i];
        for idx in 0..input.len() {
            input[idx] = idx as u8;
        }
        blake2s.update(&input).expect("Error with update()");
        let mut hash = [0u8; 32];
        blake2s.finalize(&mut hash).expect("error with finalize()");
        assert_eq!(hash, *expected_hash);
    }
}

#[test]
#[cfg(blake2s)]
fn test_blake2s_finalize_empty_buffer() {
    let mut blake2s = BLAKE2s::new(32).expect("Error with new()");
    let mut hash: [u8; 0] = [];
    let rc = blake2s.finalize(&mut hash).expect_err("finalize() should fail");
    assert_eq!(rc, sys::wolfCrypt_ErrorCodes_BUFFER_E);
}

#[test]
#[cfg(blake2s_hmac)]
fn test_blake2s_hmac() {
    let key1 = [0x41u8, 0x42, 0x43, 0x44];
    let message1 = [0x48u8, 0x65, 0x6c, 0x6c, 0x6f];
    let expected1 = [
        0x96u8, 0xca, 0x1d, 0xaa, 0x9a, 0x33, 0x97, 0x3d,
        0xc5, 0x95, 0x3e, 0xce, 0x49, 0x93, 0x75, 0xc1,
        0x2a, 0x7c, 0x8f, 0x5b, 0xf0, 0x28, 0xef, 0xc3,
        0xfb, 0xc5, 0x97, 0xcd, 0xcc, 0x74, 0x44, 0x68,
    ];
    let key2 = [
        0x30u8, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42,
        0x43, 0x44, 0x45, 0x46, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
        0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x30, 0x31, 0x32, 0x33,
    ];
    let message2 = [
        0x61u8, 0x62, 0x63, 0x64, 0x62, 0x63, 0x64, 0x65, 0x63, 0x64, 0x65, 0x66,
        0x64, 0x65, 0x66, 0x67, 0x65, 0x66, 0x67, 0x68, 0x66, 0x67, 0x68, 0x69,
        0x67, 0x68, 0x69, 0x6a, 0x68, 0x69, 0x6a, 0x6b, 0x69, 0x6a, 0x6b, 0x6c,
        0x6a, 0x6b, 0x6c, 0x6d, 0x6b, 0x6c, 0x6d, 0x6e, 0x6c, 0x6d, 0x6e, 0x6f,
        0x6d, 0x6e, 0x6f, 0x70, 0x6e, 0x6f, 0x70, 0x71
    ];
    let expected2 = [
        0xc4u8, 0x63, 0xdb, 0x28, 0x97, 0x60, 0x6a, 0xa7,
        0x1e, 0xe6, 0xcf, 0x93, 0x85, 0x3c, 0x90, 0x71,
        0xea, 0x76, 0x7f, 0x6a, 0xa7, 0x20, 0x80, 0x35,
        0xe1, 0x68, 0x95, 0xfe, 0x65, 0x65, 0x43, 0x76,
    ];

    let mut out1 = [0u8; 32];
    BLAKE2sHmac::hmac(&message1, &key1, &mut out1).expect("Error with hmac()");
    assert_eq!(out1, expected1);

    let mut out2 = [0u8; 32];
    BLAKE2sHmac::hmac(&message2, &key2, &mut out2).expect("Error with hmac()");
    assert_eq!(out2, expected2);

    let mut hmac_blake2s = BLAKE2sHmac::new(&key1).expect("Error with new()");
    hmac_blake2s.update(&message1[0..4]).expect("Error with update()");
    hmac_blake2s.update(&message1[4..]).expect("Error with update()");
    let mut out1 = [0u8; 32];
    hmac_blake2s.finalize(&key1, &mut out1).expect("Error with finalize()");
    assert_eq!(out1, expected1);

    let mut hmac_blake2s = BLAKE2sHmac::new(&key2).expect("Error with new()");
    hmac_blake2s.update(&message2[0..48]).expect("Error with update()");
    hmac_blake2s.update(&message2[48..]).expect("Error with update()");
    let mut out2 = [0u8; 32];
    hmac_blake2s.finalize(&key2, &mut out2).expect("Error with finalize()");
    assert_eq!(out2, expected2);
}