deoxys 0.2.1

Pure Rust implementation of the Deoxys Authenticated Encryption with Associated Data (AEAD) cipher, including the Deoxys-II variant which was selected by the CAESAR competition as the first choice for in-depth security
Documentation
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
#![no_std]
#![doc = include_str!("../README.md")]
#![doc(
    html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg",
    html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg"
)]

//! # Usage
//!
#![cfg_attr(feature = "getrandom", doc = "```")]
#![cfg_attr(not(feature = "getrandom"), doc = "```ignore")]
//! # fn main() -> Result<(), Box<dyn core::error::Error>> {
//! // NOTE: requires the `getrandom` feature is enabled
//!
//! use deoxys::{
//!     aead::{Aead, AeadCore, Generate, Key, KeyInit},
//!     DeoxysII256, // Can be `DeoxysI128`, `DeoxysI256`, `DeoxysII128` of `DeoxysII256`
//!     Nonce
//! };
//!
//! let key = Key::<DeoxysII256>::generate();
//! let cipher = DeoxysII256::new(&key);
//!
//! let nonce = Nonce::generate(); // MUST be unique per message
//! let ciphertext = cipher.encrypt(&nonce, b"plaintext message".as_ref())?;
//!
//! let plaintext = cipher.decrypt(&nonce, ciphertext.as_ref())?;
//! assert_eq!(&plaintext, b"plaintext message");
//! # Ok(())
//! # }
//! ```
//!
//! ## Usage with AAD
//! Deoxys can authenticate additional data that is not encrypted alongside with the ciphertext.
//!
#![cfg_attr(feature = "getrandom", doc = "```")]
#![cfg_attr(not(feature = "getrandom"), doc = "```ignore")]
//! # fn main() -> Result<(), Box<dyn core::error::Error>> {
//! // NOTE: requires the `getrandom` feature is enabled
//!
//! use deoxys::{
//!     aead::{Aead, AeadCore, Generate, Key, KeyInit, Payload},
//!     DeoxysII256, // Can be `DeoxysI128`, `DeoxysI256`, `DeoxysII128` of `DeoxysII256`
//!     Nonce
//! };
//!
//! let key = Key::<DeoxysII256>::generate();
//! let cipher = DeoxysII256::new(&key);
//!
//! let nonce = Nonce::generate(); // MUST be unique per message
//!
//! let payload = Payload {
//!    msg: &b"this will be encrypted".as_ref(),
//!    aad: &b"this will NOT be encrypted, but will be authenticated".as_ref(),
//! };
//!
//! let ciphertext = cipher.encrypt(&nonce, payload)?;
//!
//! let payload = Payload {
//!    msg: &ciphertext,
//!    aad: &b"this will NOT be encrypted, but will be authenticated".as_ref(),
//! };
//!
//! let plaintext = cipher.decrypt(&nonce, payload)?;
//! assert_eq!(&plaintext, b"this will be encrypted");
//! # Ok(())
//! # }
//! ```
//!
//! ## In-place Usage (eliminates `alloc` requirement)
//!
//! This crate has an optional `alloc` feature which can be disabled in e.g.
//! microcontroller environments that don't have a heap.
//!
//! The [`AeadInOut::encrypt_in_place`] and [`AeadInOut::decrypt_in_place`]
//! methods accept any type that impls the [`aead::Buffer`] trait which
//! contains the plaintext for encryption or ciphertext for decryption.
//!
//! Enabling the `arrayvec` feature of this crate will provide an impl of
//! [`aead::Buffer`] for `arrayvec::ArrayVec` (re-exported from the [`aead`] crate as
//! [`aead::arrayvec::ArrayVec`]), and enabling the `bytes` feature of this crate will
//! provide an impl of [`aead::Buffer`] for `bytes::BytesMut` (re-exported from the
//! [`aead`] crate as [`aead::bytes::BytesMut`]).
//!
//! It can then be passed as the `buffer` parameter to the in-place encrypt
//! and decrypt methods:
//!
#![cfg_attr(all(feature = "getrandom", feature = "arrayvec"), doc = "```")]
#![cfg_attr(
    not(all(feature = "getrandom", feature = "arrayvec")),
    doc = "```ignore"
)]
//! # fn main() -> Result<(), Box<dyn core::error::Error>> {
//! // NOTE: requires the `arrayvec` and `getrandom` features are enabled
//!
//! use deoxys::{
//!     aead::{AeadCore, AeadInOut, Generate, Key, KeyInit, arrayvec::ArrayVec},
//!     DeoxysII256, // Can be `DeoxysI128`, `DeoxysI256`, `DeoxysII128` of `DeoxysII256`
//!     Nonce
//! };
//!
//! let key = Key::<DeoxysII256>::generate();
//! let cipher = DeoxysII256::new(&key);
//!
//! let nonce = Nonce::generate(); // MUST be unique per message
//!
//! let mut buffer: ArrayVec<u8, 128> = ArrayVec::new(); // Buffer needs 16-bytes overhead for tag
//! buffer.try_extend_from_slice(b"plaintext message").unwrap();
//!
//! // Encrypt `buffer` in-place, replacing the plaintext contents with ciphertext
//! cipher.encrypt_in_place(&nonce, b"", &mut buffer)?;
//!
//! // `buffer` now contains the message ciphertext
//! assert_ne!(buffer.as_ref(), b"plaintext message");
//!
//! // Decrypt `buffer` in-place, replacing its ciphertext context with the original plaintext
//! cipher.decrypt_in_place(&nonce, b"", &mut buffer)?;
//! assert_eq!(buffer.as_ref(), b"plaintext message");
//! # Ok(())
//! # }
//! ```

/// Deoxys-BC implementations.
mod deoxys_bc;

/// Operation modes for Deoxys.
mod modes;

pub use aead::{self, AeadCore, AeadInOut, Error, Key, KeyInit, KeySizeUser, consts};

use aead::{
    TagPosition,
    array::{Array, ArraySize},
    consts::U16,
    inout::{InOut, InOutBuf},
};
use core::{fmt, marker::PhantomData};

/// Deoxys-I with 128-bit keys
pub type DeoxysI128 = Deoxys<modes::DeoxysI<deoxys_bc::DeoxysBc256>, deoxys_bc::DeoxysBc256>;

/// Deoxys-I with 256-bit keys
pub type DeoxysI256 = Deoxys<modes::DeoxysI<deoxys_bc::DeoxysBc384>, deoxys_bc::DeoxysBc384>;

/// Deoxys-II with 128-bit keys
#[allow(clippy::upper_case_acronyms)]
pub type DeoxysII128 = Deoxys<modes::DeoxysII<deoxys_bc::DeoxysBc256>, deoxys_bc::DeoxysBc256>;

/// Deoxys-II with 256-bit keys
#[allow(clippy::upper_case_acronyms)]
pub type DeoxysII256 = Deoxys<modes::DeoxysII<deoxys_bc::DeoxysBc384>, deoxys_bc::DeoxysBc384>;

/// Deoxys nonces
pub type Nonce<NonceSize> = Array<u8, NonceSize>;

/// Deoxys tags
pub type Tag = Array<u8, U16>;

type Block = Array<u8, U16>;
type Tweak = Array<u8, U16>;
type DeoxysKey = Array<u8, U16>;

/// Deoxys encryption modes.
/// This type contains the public API for a Deoxys mode, like Deoxys-I and Deoxys-II.
pub trait DeoxysMode<B>: modes::DeoxysModeInternal<B>
where
    B: DeoxysBcType,
{
    /// The size of the required nonce
    type NonceSize: ArraySize;

    /// Encrypts the data in place with the specified parameters.
    ///
    /// Returns the tag.
    fn encrypt_inout(
        nonce: &Array<u8, Self::NonceSize>,
        associated_data: &[u8],
        buffer: InOutBuf<'_, '_, u8>,
        subkeys: &Array<DeoxysKey, B::SubkeysSize>,
    ) -> Tag;

    /// Decrypts the data in place with the specified parameters.
    ///
    /// # Errors
    /// Returns an error if the tag verification fails.
    fn decrypt_inout(
        nonce: &Array<u8, Self::NonceSize>,
        associated_data: &[u8],
        buffer: InOutBuf<'_, '_, u8>,
        tag: &Tag,
        subkeys: &Array<DeoxysKey, B::SubkeysSize>,
    ) -> Result<(), Error>;
}

/// Deoxys-BC trait.
/// This type contains the public API for Deoxys-BC implementations, which varies depending on the size of the key.
pub trait DeoxysBcType: deoxys_bc::DeoxysBcInternal {
    /// The size of the required tweakey.
    type KeySize: ArraySize;

    /// Precompute the subkeys
    fn precompute_subkeys(key: &Array<u8, Self::KeySize>) -> Array<DeoxysKey, Self::SubkeysSize>;

    /// Encrypts a block of data in place.
    fn encrypt_inout(
        mut block: InOut<'_, '_, Block>,
        tweak: &Tweak,
        subkeys: &Array<DeoxysKey, Self::SubkeysSize>,
    ) {
        let keys = Self::key_schedule(tweak, subkeys);

        block.xor_in2out(&keys[0]);

        for k in &keys[1..] {
            aes::hazmat::cipher_round(block.get_out(), k);
        }
    }

    /// Decrypts a block of data in place.
    fn decrypt_inout(
        mut block: InOut<'_, '_, Block>,
        tweak: &Tweak,
        subkeys: &Array<DeoxysKey, Self::SubkeysSize>,
    ) {
        let mut keys = Self::key_schedule(tweak, subkeys);

        let r = keys.len();

        block.xor_in2out(&keys[r - 1]);

        aes::hazmat::inv_mix_columns(block.get_out());

        for k in keys[..r - 1].iter_mut().rev() {
            aes::hazmat::inv_mix_columns(k);
            aes::hazmat::equiv_inv_cipher_round(block.get_out(), k);
        }

        aes::hazmat::mix_columns(block.get_out());
    }
}

/// Generic Deoxys implementation.
///
/// This type is generic to support multiple Deoxys modes(namely Deoxys-I and Deoxys-II) and key size.
pub struct Deoxys<M, B>
where
    M: DeoxysMode<B>,
    B: DeoxysBcType,
{
    subkeys: Array<DeoxysKey, B::SubkeysSize>,
    mode: PhantomData<M>,
}

impl<M, B> KeySizeUser for Deoxys<M, B>
where
    M: DeoxysMode<B>,
    B: DeoxysBcType,
{
    type KeySize = B::KeySize;
}

impl<M, B> KeyInit for Deoxys<M, B>
where
    M: DeoxysMode<B>,
    B: DeoxysBcType,
{
    fn new(key: &Key<Self>) -> Self {
        Self {
            subkeys: B::precompute_subkeys(key),
            mode: PhantomData,
        }
    }
}

impl<M, B> AeadCore for Deoxys<M, B>
where
    M: DeoxysMode<B>,
    B: DeoxysBcType,
{
    type NonceSize = M::NonceSize;
    type TagSize = U16;
    const TAG_POSITION: TagPosition = TagPosition::Postfix;
}

impl<M, B> AeadInOut for Deoxys<M, B>
where
    M: DeoxysMode<B>,
    B: DeoxysBcType,
{
    fn encrypt_inout_detached(
        &self,
        nonce: &Nonce<M::NonceSize>,
        associated_data: &[u8],
        buffer: InOutBuf<'_, '_, u8>,
    ) -> Result<Tag, Error> {
        Ok(Tag::from(M::encrypt_inout(
            nonce,
            associated_data,
            buffer,
            &self.subkeys,
        )))
    }

    fn decrypt_inout_detached(
        &self,
        nonce: &Nonce<M::NonceSize>,
        associated_data: &[u8],
        buffer: InOutBuf<'_, '_, u8>,
        tag: &Tag,
    ) -> Result<(), Error> {
        M::decrypt_inout(nonce, associated_data, buffer, tag, &self.subkeys)
    }
}

impl<M, B> Drop for Deoxys<M, B>
where
    M: DeoxysMode<B>,
    B: DeoxysBcType,
{
    fn drop(&mut self) {
        #[cfg(feature = "zeroize")]
        {
            use zeroize::Zeroize;
            for s in self.subkeys.iter_mut() {
                s.zeroize();
            }
        }
    }
}

impl<M, B> fmt::Debug for Deoxys<M, B>
where
    M: DeoxysMode<B>,
    B: DeoxysBcType,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Deoxys").finish_non_exhaustive()
    }
}

#[cfg(feature = "zeroize")]
impl<M, B> zeroize::ZeroizeOnDrop for Deoxys<M, B>
where
    M: DeoxysMode<B>,
    B: DeoxysBcType,
{
}

#[cfg(test)]
mod tests {
    //! this module is here to test the inout behavior which is not currently exposed.
    //! it will be once we port over to the API made in RustCrypto/traits#1793.
    //!
    //! This is to drop once https://github.com/RustCrypto/traits/pull/1797 is made available.
    //!
    //! It duplicates test vectors from `tests/deoxys_i_128.rs` and provides a mock buffer backing
    //! for InOut.

    use hex_literal::hex;

    use super::*;

    struct MockBuffer {
        in_buf: [u8; 33],
        out_buf: [u8; 33],
    }

    impl From<&[u8]> for MockBuffer {
        fn from(buf: &[u8]) -> Self {
            let mut in_buf = [0u8; 33];
            in_buf.copy_from_slice(buf);
            Self {
                in_buf,
                out_buf: [0u8; 33],
            }
        }
    }

    impl MockBuffer {
        /// Get an [`InOutBuf`] from a [`MockBuffer`]
        pub fn as_in_out_buf(&mut self) -> InOutBuf<'_, '_, u8> {
            InOutBuf::new(self.in_buf.as_slice(), self.out_buf.as_mut_slice())
                .expect("Invariant violation")
        }
    }

    impl AsRef<[u8]> for MockBuffer {
        fn as_ref(&self) -> &[u8] {
            &self.out_buf
        }
    }

    #[test]
    fn test_deoxys_i_128_5() {
        let plaintext = hex!("5a4c652cb880808707230679224b11799b5883431292973215e9bd03cf3bc32fe4");
        let mut buffer = MockBuffer::from(&plaintext[..]);

        let aad = [];

        let key = hex!("101112131415161718191a1b1c1d1e1f");
        let key = Array(key);

        let nonce = hex!("202122232425262728292a2b2c2d2e2f");
        let nonce = Array::try_from(&nonce[..8]).unwrap();

        let ciphertext_expected =
            hex!("cded5a43d3c76e942277c2a1517530ad66037897c985305ede345903ed7585a626");

        let tag_expected: [u8; 16] = hex!("cbf5faa6b8398c47f4278d2019161776");

        type M = modes::DeoxysI<deoxys_bc::DeoxysBc256>;
        let cipher = DeoxysI128::new(&key);
        let tag: Tag = M::encrypt_inout(&nonce, &aad, buffer.as_in_out_buf(), &cipher.subkeys);

        let ciphertext = buffer.as_ref();
        assert_eq!(ciphertext, ciphertext_expected);
        assert_eq!(tag, tag_expected);

        let mut buffer = MockBuffer::from(buffer.as_ref());
        M::decrypt_inout(&nonce, &aad, buffer.as_in_out_buf(), &tag, &cipher.subkeys)
            .expect("decryption failed");

        assert_eq!(&plaintext[..], buffer.as_ref());
    }

    #[test]
    fn test_deoxys_ii_128_5() {
        let plaintext = hex!("06ac1756eccece62bd743fa80c299f7baa3872b556130f52265919494bdc136db3");
        let mut buffer = MockBuffer::from(&plaintext[..]);

        let aad = [];

        let key = hex!("101112131415161718191a1b1c1d1e1f");
        let key = Array(key);

        let nonce = hex!("202122232425262728292a2b2c2d2e2f");
        let nonce = Array::try_from(&nonce[..15]).unwrap();

        let ciphertext_expected =
            hex!("82bf241958b324ed053555d23315d3cc20935527fc970ff34a9f521a95e302136d");

        let tag_expected: [u8; 16] = hex!("0eadc8612d5208c491e93005195e9769");

        type M = modes::DeoxysII<deoxys_bc::DeoxysBc256>;
        let cipher = DeoxysII128::new(&key);
        let tag: Tag = M::encrypt_inout(&nonce, &aad, buffer.as_in_out_buf(), &cipher.subkeys);

        let ciphertext = buffer.as_ref();
        assert_eq!(ciphertext, ciphertext_expected);
        assert_eq!(tag, tag_expected);

        let mut buffer = MockBuffer::from(buffer.as_ref());
        M::decrypt_inout(&nonce, &aad, buffer.as_in_out_buf(), &tag, &cipher.subkeys)
            .expect("decryption failed");

        assert_eq!(&plaintext[..], buffer.as_ref());
    }
}