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
#[cfg(feature = "serialization")]
use serde::{Deserialize, Serialize};
use evercrypt_sys::evercrypt_bindings::*;
#[derive(Copy, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))]
pub enum Mode {
    Sha1 = Spec_Hash_Definitions_SHA1 as isize,
    
    
    Sha256 = Spec_Hash_Definitions_SHA2_256 as isize,
    Sha384 = Spec_Hash_Definitions_SHA2_384 as isize,
    Sha512 = Spec_Hash_Definitions_SHA2_512 as isize,
}
#[deprecated(
    since = "0.0.10",
    note = "Please use tag_size instead. This alias will be removed with the first stable 0.1 release."
)]
pub fn get_tag_size(mode: Mode) -> usize {
    tag_size(mode)
}
pub const fn tag_size(mode: Mode) -> usize {
    match mode {
        Mode::Sha1 => 20,
        Mode::Sha256 => 32,
        Mode::Sha384 => 48,
        Mode::Sha512 => 64,
    }
}
pub fn hmac(mode: Mode, key: &[u8], data: &[u8], tag_length: Option<usize>) -> Vec<u8> {
    let native_tag_length = tag_size(mode);
    let tag_length = match tag_length {
        Some(v) => v,
        None => native_tag_length,
    };
    let mut dst = vec![0u8; native_tag_length];
    unsafe {
        EverCrypt_HMAC_compute(
            mode as u8,
            dst.as_mut_ptr(),
            key.as_ptr() as _,
            key.len() as u32,
            data.as_ptr() as _,
            data.len() as u32,
        );
    }
    dst.truncate(tag_length);
    dst
}