1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use crate::blake2b;
use crate::constants::{
    CRYPTO_GENERICHASH_BLAKE2B_BYTES_MAX, CRYPTO_GENERICHASH_BLAKE2B_BYTES_MIN,
    CRYPTO_GENERICHASH_BLAKE2B_KEYBYTES_MAX, CRYPTO_GENERICHASH_BLAKE2B_KEYBYTES_MIN,
    CRYPTO_GENERICHASH_BLAKE2B_PERSONALBYTES, CRYPTO_GENERICHASH_BLAKE2B_SALTBYTES,
};
use crate::error::Error;

#[inline]
pub(crate) fn crypto_generichash_blake2b_validate_key(key: Option<&[u8]>) -> Result<(), Error> {
    match key {
        Some(key) => {
            if key.len() < CRYPTO_GENERICHASH_BLAKE2B_KEYBYTES_MIN
                || key.len() > CRYPTO_GENERICHASH_BLAKE2B_KEYBYTES_MAX
            {
                return Err(dryoc_error!(format!(
                    "key length is {}, should be at least {} and less than {} bytes",
                    key.len(),
                    CRYPTO_GENERICHASH_BLAKE2B_KEYBYTES_MIN,
                    CRYPTO_GENERICHASH_BLAKE2B_KEYBYTES_MAX
                )));
            }
            Ok(())
        }
        None => Ok(()),
    }
}

#[inline]
pub(crate) fn crypto_generichash_blake2b_validate_outlen(outlen: usize) -> Result<(), Error> {
    if !(CRYPTO_GENERICHASH_BLAKE2B_BYTES_MIN..=CRYPTO_GENERICHASH_BLAKE2B_BYTES_MAX)
        .contains(&outlen)
    {
        return Err(dryoc_error!(format!(
            "output length is {}, expected at least {} and less than {} bytes",
            outlen, CRYPTO_GENERICHASH_BLAKE2B_BYTES_MIN, CRYPTO_GENERICHASH_BLAKE2B_BYTES_MAX
        )));
    }
    Ok(())
}

#[inline]
pub(crate) fn crypto_generichash_blake2b(
    output: &mut [u8],
    input: &[u8],
    key: Option<&[u8]>,
) -> Result<(), Error> {
    crypto_generichash_blake2b_validate_outlen(output.len())?;
    crypto_generichash_blake2b_validate_key(key)?;

    blake2b::hash(output, input, key)
}

#[inline]
pub(crate) fn crypto_generichash_blake2b_init(
    key: Option<&[u8]>,
    outlen: usize,
    salt: Option<&[u8; CRYPTO_GENERICHASH_BLAKE2B_SALTBYTES]>,
    personal: Option<&[u8; CRYPTO_GENERICHASH_BLAKE2B_PERSONALBYTES]>,
) -> Result<blake2b::State, Error> {
    crypto_generichash_blake2b_validate_outlen(outlen)?;
    crypto_generichash_blake2b_validate_key(key)?;

    blake2b::State::init(outlen as u8, key, salt, personal)
}

#[inline]
pub(crate) fn crypto_generichash_blake2b_update(state: &mut blake2b::State, input: &[u8]) {
    state.update(input)
}

#[inline]
pub(crate) fn crypto_generichash_blake2b_final(
    state: blake2b::State,
    output: &mut [u8],
) -> Result<(), Error> {
    state.finalize(output)
}