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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
//! Authenticated Encryption with Associated Data (AEAD)
//!
//! This module implements AES-GCM 128 and 256, and Chacha20Poly1305.
//!
//! # Usage
//! This module provides two APIs
//!
//! ## Aead with key state
//! ```rust
//! use hacl::aead::{Aead, Algorithm, Error};
//!
//! let key = [0x5b, 0x96, 0x04, 0xfe, 0x14, 0xea, 0xdb, 0xa9, 0x31, 0xb0, 0xcc,
//!            0xf3, 0x48, 0x43, 0xda, 0xb9, 0x5b, 0x96, 0x04, 0xfe, 0x14, 0xea,
//!            0xdb, 0xa9, 0x31, 0xb0, 0xcc, 0xf3, 0x48, 0x43, 0xda, 0xb9];
//! let cipher = match Aead::new(Algorithm::Chacha20Poly1305, &key) {
//!    Ok(c) => c,
//!    Err(e) => panic!("Error instantiating AEAD.\n{:?}", e),
//! };
//!
//! let iv = [0x02, 0x83, 0x18, 0xab, 0xc1, 0x82, 0x40, 0x29, 0x13, 0x81, 0x41, 0xa2];
//! let msg = [0x00, 0x1d, 0x0c, 0x23, 0x12, 0x87, 0xc1, 0x18, 0x27, 0x84, 0x55, 0x4c, 0xa3, 0xa2, 0x19, 0x08];
//! let aad = [];
//!
//! let (ciphertext, tag) = match cipher.encrypt(&msg, &iv, &aad) {
//!     Ok(r) => r,
//!     Err(e) => panic!("Error encrypting.\n{:?}", e),
//! };
//!
//! let msg_ = match cipher.decrypt(&ciphertext, &tag, &iv, &aad) {
//!     Ok(r) => r,
//!     Err(e) => panic!("Error decrypting.\n{:?}", e),
//! };
//!
//! assert_eq!(&msg[..], &msg_[..]);
//! ```
//!
//! ## Single-shot API
//! ```rust
//! use hacl::aead::{self, Algorithm};
//!
//! let key = [0x5b, 0x96, 0x04, 0xfe, 0x14, 0xea, 0xdb, 0xa9, 0x31, 0xb0, 0xcc,
//!            0xf3, 0x48, 0x43, 0xda, 0xb9, 0x5b, 0x96, 0x04, 0xfe, 0x14, 0xea,
//!            0xdb, 0xa9, 0x31, 0xb0, 0xcc, 0xf3, 0x48, 0x43, 0xda, 0xb9];
//! let iv = [0x02, 0x83, 0x18, 0xab, 0xc1, 0x82, 0x40, 0x29, 0x13, 0x81, 0x41, 0xa2];
//! let msg = [0x00, 0x1d, 0x0c, 0x23, 0x12, 0x87, 0xc1, 0x18, 0x27, 0x84, 0x55, 0x4c, 0xa3, 0xa2, 0x19, 0x08];
//! let aad = [];
//!
//! let (ciphertext, tag) = match aead::encrypt(Algorithm::Chacha20Poly1305, &key, &msg, &iv, &aad) {
//!    Ok(r) => r,
//!    Err(e) => panic!("Error encrypting.\n{:?}", e),
//! };
//!
//! let msg_ = match aead::decrypt(Algorithm::Chacha20Poly1305, &key, &ciphertext, &tag, &iv, &aad) {
//!     Ok(r) => r,
//!     Err(e) => panic!("Error decrypting.\n{:?}", e),
//! };
//!
//! assert_eq!(&msg[..], &msg_[..]);
//! ```
//!

use std::convert::TryInto;

#[cfg(feature = "serialization")]
use serde::{Deserialize, Serialize};

use hacl_sys::*;

/// The AEAD Algorithm Identifier.
#[derive(Clone, Copy, PartialEq, Debug)]
#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))]
#[repr(u32)]
// ANCHOR: aead_algorithm
pub enum Algorithm {
    /// AES GCM 128
    Aes128Gcm = Spec_Agile_AEAD_AES128_GCM,

    /// AES GCM 256
    Aes256Gcm = Spec_Agile_AEAD_AES256_GCM,

    /// ChaCha20 Poly1305
    Chacha20Poly1305 = Spec_Agile_AEAD_CHACHA20_POLY1305,
}
// ANCHOR_END: aead_algorithm

impl From<u8> for Algorithm {
    fn from(v: u8) -> Algorithm {
        match v {
            0 => Algorithm::Aes128Gcm,
            1 => Algorithm::Aes256Gcm,
            2 => Algorithm::Chacha20Poly1305,
            _ => panic!("Unknown AEAD mode {}", v),
        }
    }
}

impl From<Algorithm> for Spec_Agile_AEAD_alg {
    fn from(v: Algorithm) -> Spec_Agile_AEAD_alg {
        match v {
            Algorithm::Aes128Gcm => Spec_Agile_AEAD_AES128_GCM as Spec_Agile_AEAD_alg,
            Algorithm::Aes256Gcm => Spec_Agile_AEAD_AES256_GCM as Spec_Agile_AEAD_alg,
            Algorithm::Chacha20Poly1305 => Spec_Agile_AEAD_CHACHA20_POLY1305 as Spec_Agile_AEAD_alg,
        }
    }
}

impl Algorithm {
    /// Get the key size of the `Algorithm` in bytes.
    #[inline]
    pub const fn key_size(self) -> usize {
        match self {
            Algorithm::Aes128Gcm => 16,
            Algorithm::Aes256Gcm => 32,
            Algorithm::Chacha20Poly1305 => 32,
        }
    }

    /// Get the tag size of the `Algorithm` in bytes.
    #[inline]
    pub const fn tag_size(self) -> usize {
        match self {
            Algorithm::Aes128Gcm => 16,
            Algorithm::Aes256Gcm => 16,
            Algorithm::Chacha20Poly1305 => 16,
        }
    }

    /// Get the nonce size of the `Algorithm` in bytes.
    #[inline]
    pub const fn nonce_size(self) -> usize {
        match self {
            Algorithm::Aes128Gcm => 12,
            Algorithm::Aes256Gcm => 12,
            Algorithm::Chacha20Poly1305 => 12,
        }
    }
}

/// AEAD Errors
#[derive(Debug, PartialEq)]
pub enum Error {
    InvalidInit = 0,
    InvalidAlgorithm = 1,
    InvalidCiphertext = 2,
    InvalidNonce = 3,
    UnsupportedConfig = 4,
    Encrypting = 5,
    Decrypting = 6,
    InvalidKeySize = 7,
    InvalidTagSize = 8,
}

/// The Aead struct allows to re-use a key without having to initialize it
/// every time.
pub struct Aead {
    alg: Algorithm,
    c_state: Option<*mut EverCrypt_AEAD_state_s>,
}

/// Ciphertexts are byte vectors.
pub type Ciphertext = Vec<u8>;

pub type Aes128Key = [u8; Algorithm::key_size(Algorithm::Aes128Gcm)];
pub type Aes256Key = [u8; Algorithm::key_size(Algorithm::Aes256Gcm)];
pub type Chacha20Key = [u8; Algorithm::key_size(Algorithm::Chacha20Poly1305)];
pub type Tag = [u8; 16];

/// Associated data are byte arrays.
pub type Aad = [u8];

/// Check hardware support for HACL* AES implementation.
///
/// # Safety
/// This function checks CPU flags, which can not be done in safe rust.
/// It does not interact with memory and should therefore always be safe to use.
pub unsafe fn hacl_aes_available() -> bool {
    EverCrypt_AutoConfig2_has_pclmulqdq()
        && EverCrypt_AutoConfig2_has_avx()
        && EverCrypt_AutoConfig2_has_sse()
        && EverCrypt_AutoConfig2_has_movbe()
        && EverCrypt_AutoConfig2_has_aesni()
}

impl Aead {
    fn set_key_(&mut self, k: &[u8]) -> Result<(), Error> {
        let state = unsafe {
            let mut state_ptr: *mut EverCrypt_AEAD_state_s = std::ptr::null_mut();
            let e = EverCrypt_AEAD_create_in(self.alg.into(), &mut state_ptr, k.as_ptr() as _);
            if e != 0 {
                return Err(Error::InvalidInit);
            }
            state_ptr
        };
        self.c_state = Some(state);
        Ok(())
    }

    /// Create a new Aead cipher with the given Algorithm `alg` and key `k`.
    /// If the algorithm is not supported or the state generation fails, this
    /// function returns an `Error`.
    ///
    /// To get an Aead instance without setting a key immediately see `init`.
    pub fn new(alg: Algorithm, k: &[u8]) -> Result<Self, Error> {
        // Check key lengths. Evercrypt is not doing this.
        if k.len() != alg.key_size() {
            return Err(Error::InvalidKeySize);
        }

        unsafe {
            // Make sure this happened.
            EverCrypt_AutoConfig2_init();
        }
        let mut out = Self::init(alg)?;
        out.set_key_(k)?;
        Ok(out)
    }

    /// Initialize a new Aead object without a key.
    /// Use `set_key` to do so later.
    pub fn init(mode: Algorithm) -> Result<Self, Error> {
        if unsafe {
            // Make sure this happened.
            EverCrypt_AutoConfig2_init();

            // Make sure the algorithm is supported
            (mode == Algorithm::Aes128Gcm || mode == Algorithm::Aes256Gcm) && !hacl_aes_available()
        } {
            return Err(Error::UnsupportedConfig);
        }
        Ok(Self {
            alg: mode,
            c_state: None,
        })
    }

    /// Set the key for this instance.
    /// This consumes the Aead and returns a new instance with the key.
    pub fn set_key(self, k: &[u8]) -> Result<Self, Error> {
        Self::new(self.alg, k)
    }

    /// Generate a new random key for this instance.
    /// This consumes the Aead and returns a new instance with the key.
    pub fn set_random_key(&mut self) -> Result<(), Error> {
        self.set_key_(&self.key_gen())
    }

    /// Generate a random key.
    pub fn key_gen(&self) -> Vec<u8> {
        key_gen(self.alg)
    }

    /// Generate a nonce.
    pub fn nonce_gen(&self) -> Vec<u8> {
        nonce_gen(self.alg)
    }

    /// Get the nonce size of this Aead in bytes.
    pub const fn nonce_size(&self) -> usize {
        self.alg.nonce_size()
    }

    /// Get the key size of this Aead in bytes.
    pub const fn key_size(&self) -> usize {
        self.alg.key_size()
    }

    /// Get the tag size of this Aead in bytes.
    pub const fn tag_size(&self) -> usize {
        self.alg.tag_size()
    }

    /// Encrypt with the algorithm and key of this Aead.
    /// Returns `(ctxt, tag)` or an `Error`.
    pub fn encrypt(
        &self,
        msg: &[u8],
        iv: &[u8],
        aad: &Aad,
    ) -> Result<(Ciphertext, Vec<u8>), Error> {
        if iv.len() != self.nonce_size() {
            return Err(Error::InvalidNonce);
        }

        let mut ctxt = vec![0u8; msg.len()];
        let mut tag = vec![0u8; self.tag_size()];
        unsafe {
            EverCrypt_AEAD_encrypt(
                self.c_state.unwrap(),
                iv.as_ptr() as _,
                self.nonce_size().try_into().unwrap(),
                aad.as_ptr() as _,
                aad.len() as u32,
                msg.as_ptr() as _,
                msg.len() as u32,
                ctxt.as_mut_ptr(),
                tag.as_mut_ptr(),
            );
        }
        Ok((ctxt, tag))
    }

    /// Encrypt with the algorithm and key of this Aead.
    /// Returns `(ctxt || tag)` or an `Error`.
    /// This is more efficient if the tag needs to be appended to the cipher text.
    // ANCHOR: aead_encrypt_combined
    pub fn encrypt_combined(&self, msg: &[u8], iv: &[u8], aad: &Aad) -> Result<Ciphertext, Error> {
        // ANCHOR_END: aead_encrypt_combined
        if iv.len() != self.nonce_size() {
            return Err(Error::InvalidNonce);
        }

        // combined cipher text and tag
        let mut ctxt = vec![0u8; msg.len() + self.tag_size()];
        unsafe {
            EverCrypt_AEAD_encrypt(
                self.c_state.unwrap(),
                iv.as_ptr() as _,
                self.nonce_size().try_into().unwrap(),
                aad.as_ptr() as _,
                aad.len() as u32,
                msg.as_ptr() as _,
                msg.len() as u32,
                ctxt.as_mut_ptr(),
                ctxt.as_mut_ptr().offset(msg.len().try_into().unwrap()),
            );
        }
        Ok(ctxt)
    }

    /// Encrypt with the algorithm and key of this Aead.
    /// Returns the cipher text in the `payload` and a `tag` or an `Error`.
    // ANCHOR: aead_encrypt_in_place
    pub fn encrypt_in_place(
        &self,
        payload: &mut [u8],
        iv: &[u8],
        aad: &Aad,
    ) -> Result<Vec<u8>, Error> {
        // ANCHOR_END: aead_encrypt_in_place
        if iv.len() != self.nonce_size() {
            return Err(Error::InvalidNonce);
        }

        // The tag
        let mut tag = vec![0u8; self.tag_size()];
        unsafe {
            EverCrypt_AEAD_encrypt(
                self.c_state.unwrap(),
                iv.as_ptr() as _,
                self.nonce_size().try_into().unwrap(),
                aad.as_ptr() as _,
                aad.len() as u32,
                payload.as_ptr() as _,
                payload.len() as u32,
                payload.as_ptr() as _,
                tag.as_mut_ptr(),
            );
        }
        Ok(tag)
    }

    #[inline]
    fn _decrypt_checks(&self, tag: &[u8], iv: &[u8]) -> Result<(), Error> {
        if iv.len() != 12 {
            return Err(Error::InvalidNonce);
        }
        if tag.len() != self.tag_size() {
            return Err(Error::InvalidTagSize);
        }
        Ok(())
    }

    #[inline]
    fn _decrypt(&self, ctxt: &[u8], tag: &[u8], iv: &[u8], aad: &Aad) -> Result<Vec<u8>, Error> {
        self._decrypt_checks(tag, iv)?;

        let mut msg = vec![0u8; ctxt.len()];
        let r = unsafe {
            EverCrypt_AEAD_decrypt(
                self.c_state.unwrap(),
                iv.as_ptr() as _,
                self.nonce_size().try_into().unwrap(),
                aad.as_ptr() as _,
                aad.len() as u32,
                ctxt.as_ptr() as _,
                ctxt.len() as u32,
                tag.as_ptr() as _,
                msg.as_mut_ptr(),
            )
        };
        if r as u32 != EverCrypt_Error_Success {
            Err(Error::InvalidCiphertext)
        } else {
            Ok(msg)
        }
    }

    /// Decrypt with the algorithm and key of this Aead.
    /// Returns `msg` or an `Error`.
    pub fn decrypt(&self, ctxt: &[u8], tag: &[u8], iv: &[u8], aad: &Aad) -> Result<Vec<u8>, Error> {
        self._decrypt(ctxt, tag, iv, aad)
    }

    /// Decrypt with the algorithm and key of this Aead.
    /// Returns `msg` or an `Error`.
    /// This takes the combined ctxt || tag as input and might be more efficient
    /// than `decrypt`.
    // ANCHOR: aead_decrypt_combined
    pub fn decrypt_combined(&self, ctxt: &[u8], iv: &[u8], aad: &Aad) -> Result<Vec<u8>, Error> {
        // ANCHOR_END: aead_decrypt_combined
        if ctxt.len() < self.tag_size() {
            return Err(Error::InvalidTagSize);
        }
        let msg_len = ctxt.len() - self.tag_size();
        let tag = &ctxt[msg_len..];
        let ctxt = &ctxt[..msg_len];
        self._decrypt(ctxt, tag, iv, aad)
    }

    /// Decrypt with the algorithm and key of this Aead.
    ///
    /// Returns an `Error` if decryption failed. The decrypted `payload` is written
    /// into `payload`.
    // ANCHOR: aead_decrypt_in_place
    pub fn decrypt_in_place(
        &self,
        payload: &mut [u8],
        tag: &[u8],
        iv: &[u8],
        aad: &Aad,
    ) -> Result<(), Error> {
        // ANCHOR_END: aead_decrypt_in_place
        self._decrypt_checks(tag, iv)?;

        let r = unsafe {
            EverCrypt_AEAD_decrypt(
                self.c_state.unwrap(),
                iv.as_ptr() as _,
                self.nonce_size().try_into().unwrap(),
                aad.as_ptr() as _,
                aad.len() as u32,
                payload.as_ptr() as _,
                payload.len() as u32,
                tag.as_ptr() as _,
                payload.as_mut_ptr(),
            )
        };
        if r as u32 != EverCrypt_Error_Success {
            Err(Error::InvalidCiphertext)
        } else {
            Ok(())
        }
    }
}

impl Drop for Aead {
    fn drop(&mut self) {
        if let Some(c_state) = self.c_state {
            unsafe { EverCrypt_AEAD_free(c_state) }
        }
    }
}

// Single-shot APIs

/// Single-shot API for AEAD encryption.
pub fn encrypt(
    alg: Algorithm,
    k: &[u8],
    msg: &[u8],
    iv: &[u8],
    aad: &Aad,
) -> Result<(Ciphertext, Vec<u8>), Error> {
    let cipher = Aead::new(alg, k)?;
    cipher.encrypt(msg, iv, aad)
}

/// Single-shot API for combined AEAD encryption.
pub fn encrypt_combined(
    alg: Algorithm,
    k: &[u8],
    msg: &[u8],
    iv: &[u8],
    aad: &Aad,
) -> Result<Ciphertext, Error> {
    let cipher = Aead::new(alg, k)?;
    cipher.encrypt_combined(msg, iv, aad)
}

/// Single-shot API for in place AEAD encryption.
pub fn encrypt_in_place(
    alg: Algorithm,
    k: &[u8],
    payload: &mut [u8],
    iv: &[u8],
    aad: &Aad,
) -> Result<Vec<u8>, Error> {
    let cipher = Aead::new(alg, k)?;
    cipher.encrypt_in_place(payload, iv, aad)
}

/// Single-shot API for AEAD decryption.
pub fn decrypt(
    alg: Algorithm,
    k: &[u8],
    ctxt: &[u8],
    tag: &[u8],
    iv: &[u8],
    aad: &Aad,
) -> Result<Vec<u8>, Error> {
    let cipher = Aead::new(alg, k)?;
    cipher.decrypt(ctxt, tag, iv, aad)
}

/// Single-shot API for combined AEAD decryption.
pub fn decrypt_combined(
    alg: Algorithm,
    k: &[u8],
    ctxt: &[u8],
    iv: &[u8],
    aad: &Aad,
) -> Result<Vec<u8>, Error> {
    let cipher = Aead::new(alg, k)?;
    cipher.decrypt_combined(ctxt, iv, aad)
}

/// Single-shot API for AEAD decryption in place.
pub fn decrypt_in_place(
    alg: Algorithm,
    k: &[u8],
    payload: &mut [u8],
    tag: &[u8],
    iv: &[u8],
    aad: &Aad,
) -> Result<(), Error> {
    let cipher = Aead::new(alg, k)?;
    cipher.decrypt_in_place(payload, tag, iv, aad)
}

/// Generate a random key.
pub fn key_gen(alg: Algorithm) -> Vec<u8> {
    crate::rand_util::random_vec(alg.key_size())
}

/// Generate a nonce.
pub fn nonce_gen(alg: Algorithm) -> Vec<u8> {
    crate::rand_util::random_vec(alg.nonce_size())
}

// /// Generate a random key.
// pub fn key_gen<const L: usize>() -> [u8; L] {
//     crate::rand_util::random_array()
// }

// /// Generate a nonce.
// pub fn nonce_gen<const L: usize>() -> [u8; L] {
//     crate::rand_util::random_array()
// }