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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//! # ChaCha20 Poly1305 AEAD
//!
//! This module provides safe Rust APIs for the HACL C functions.
//! The caller MUST ensure that the required hardware features are available
//! before calling the functions.

use hacl_sys::{Hacl_AEAD_Chacha20Poly1305_decrypt, Hacl_AEAD_Chacha20Poly1305_encrypt};

pub type Chacha20Key = [u8; 32];
pub type Iv = [u8; 12];
pub type Tag = [u8; 16];

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Error {
    InvalidCiphertext,
}

/// Portable 32-bit in-place encrypt.
///
/// There are no special hardware requirements to call this function.
#[must_use]
pub fn encrypt(key: &Chacha20Key, msg_ctxt: &mut [u8], iv: Iv, aad: &[u8]) -> Tag {
    let mut tag = Tag::default();
    unsafe {
        Hacl_AEAD_Chacha20Poly1305_encrypt(
            msg_ctxt.as_mut_ptr(),
            tag.as_mut_ptr(),
            msg_ctxt.as_ptr() as _,
            msg_ctxt.len() as u32,
            aad.as_ptr() as _,
            aad.len() as u32,
            key.as_ptr() as _,
            iv.as_ptr() as _,
        );
    }
    tag
}

/// Portable 32-bit in-place decrypt.
///
/// There are no special hardware requirements to call this function.
#[must_use]
pub fn decrypt(
    key: &Chacha20Key,
    ctxt_msg: &mut [u8],
    iv: Iv,
    aad: &[u8],
    tag: &Tag,
) -> Result<(), Error> {
    if unsafe {
        Hacl_AEAD_Chacha20Poly1305_decrypt(
            ctxt_msg.as_mut_ptr(),
            ctxt_msg.as_ptr() as _,
            ctxt_msg.len() as u32,
            aad.as_ptr() as _,
            aad.len() as u32,
            key.as_ptr() as _,
            iv.as_ptr() as _,
            tag.as_ptr() as _,
        ) == 0
    } {
        Ok(())
    } else {
        Err(Error::InvalidCiphertext)
    }
}

#[cfg(simd128)]
pub mod simd128 {
    use super::*;
    use hacl_sys::{
        Hacl_AEAD_Chacha20Poly1305_Simd128_decrypt, Hacl_AEAD_Chacha20Poly1305_Simd128_encrypt,
    };

    /// 128-bit SIMD encrypt.
    ///
    /// This function requires
    /// * x86_64: AVX, SSE2, SSE3, SSE4.1
    /// * ARM: Arm64, NEON
    /// * s390x: z14
    #[must_use]
    pub fn encrypt(key: &Chacha20Key, msg_ctxt: &mut [u8], iv: Iv, aad: &[u8]) -> Tag {
        let mut tag = Tag::default();
        unsafe {
            Hacl_AEAD_Chacha20Poly1305_Simd128_encrypt(
                msg_ctxt.as_mut_ptr(),
                tag.as_mut_ptr(),
                msg_ctxt.as_ptr() as _,
                msg_ctxt.len() as u32,
                aad.as_ptr() as _,
                aad.len() as u32,
                key.as_ptr() as _,
                iv.as_ptr() as _,
            );
        }
        tag
    }

    /// 128-bit SIMD decrypt.
    ///
    /// This function requires
    /// * x86_64: AVX, SSE2, SSE3, SSE4.1
    /// * ARM: Arm64, NEON
    /// * s390x: z14
    #[must_use]
    pub fn decrypt(
        key: &Chacha20Key,
        ctxt_msg: &mut [u8],
        iv: Iv,
        aad: &[u8],
        tag: &Tag,
    ) -> Result<(), Error> {
        if unsafe {
            Hacl_AEAD_Chacha20Poly1305_Simd128_decrypt(
                ctxt_msg.as_mut_ptr(),
                ctxt_msg.as_ptr() as _,
                ctxt_msg.len() as u32,
                aad.as_ptr() as _,
                aad.len() as u32,
                key.as_ptr() as _,
                iv.as_ptr() as _,
                tag.as_ptr() as _,
            ) == 0
        } {
            Ok(())
        } else {
            Err(Error::InvalidCiphertext)
        }
    }
}

#[cfg(simd256)]
pub mod simd256 {
    use super::*;
    use hacl_sys::{
        Hacl_AEAD_Chacha20Poly1305_Simd256_decrypt, Hacl_AEAD_Chacha20Poly1305_Simd256_encrypt,
    };

    /// 256-bit SIMD encrypt.
    ///
    /// This function requires
    /// * x86_64: AVX, AVX2
    #[must_use]
    pub fn encrypt(key: &Chacha20Key, msg_ctxt: &mut [u8], iv: Iv, aad: &[u8]) -> Tag {
        let mut tag = Tag::default();
        unsafe {
            Hacl_AEAD_Chacha20Poly1305_Simd256_encrypt(
                msg_ctxt.as_mut_ptr(),
                tag.as_mut_ptr(),
                msg_ctxt.as_ptr() as _,
                msg_ctxt.len() as u32,
                aad.as_ptr() as _,
                aad.len() as u32,
                key.as_ptr() as _,
                iv.as_ptr() as _,
            );
        }
        tag
    }

    /// 256-bit SIMD decrypt.
    ///
    /// This function requires
    /// * x86_64: AVX, AVX2
    #[must_use]
    pub fn decrypt(
        key: &Chacha20Key,
        ctxt_msg: &mut [u8],
        iv: Iv,
        aad: &[u8],
        tag: &Tag,
    ) -> Result<(), Error> {
        if unsafe {
            Hacl_AEAD_Chacha20Poly1305_Simd256_decrypt(
                ctxt_msg.as_mut_ptr(),
                ctxt_msg.as_ptr() as _,
                ctxt_msg.len() as u32,
                aad.as_ptr() as _,
                aad.len() as u32,
                key.as_ptr() as _,
                iv.as_ptr() as _,
                tag.as_ptr() as _,
            ) == 0
        } {
            Ok(())
        } else {
            Err(Error::InvalidCiphertext)
        }
    }
}