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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
use core::ptr::{addr_of_mut};
use wolf_crypto_sys::{Aes as AesLL, wc_AesCtrEncrypt, wc_AesSetKey, wc_AesFree};
use crate::aes::{Key, init_aes, AesM};
use crate::ptr::{ConstPtr};
use core::ffi::c_int;
use crate::buf::Iv;
use crate::opaque_res::Res;
use core::mem::MaybeUninit;

#[must_use]
#[cfg_attr(not(debug_assertions), inline(always))]
pub(crate) unsafe fn init_aes_ctr(
    aes: *mut AesLL, key: ConstPtr<Key>, iv: ConstPtr<Iv>, mode: AesM
) -> c_int {
    wc_AesSetKey(
        aes,
        key.as_slice().as_ptr(),
        key.capacity() as u32,
        iv.slice().as_ptr(),
        mode.mode() as c_int
    )
}

#[inline]
pub(crate) unsafe fn create_aes_ctr(
    key: ConstPtr<Key>, iv: ConstPtr<Iv>, mode: AesM
) -> (MaybeUninit<AesLL>, Res) {
    let (mut aes, mut res) = init_aes(MaybeUninit::<AesLL>::uninit());

    // NOTE: It looks as though the `wc_AesSetKey` can handle `wc_AesInit` failing from this
    // example:
    // https://www.wolfssl.com/documentation/manuals/wolfssl/group__AES.html#function-wc_aessetkey
    // This requires testing to back up this assumption.

    res.ensure_0(init_aes_ctr(aes.as_mut_ptr(), key, iv, mode));

    (aes, res)
}

#[inline]
#[must_use]
const fn size_predicate(len: usize) -> bool {
    len <= (u32::MAX as usize)
}

#[inline]
#[must_use]
const fn larger(left: usize, right: usize) -> usize {
    if left < right {
        right
    } else {
        left
    }
}

#[inline]
#[must_use]
const fn predicate(input_len: usize, output_len: usize) -> bool {
    let larger = larger(input_len, output_len);
    input_len <= output_len && size_predicate(larger)
}

macro_rules! impl_aes_api {
    (
        $(#[$ll_meta:meta])*
        unsafe => $ll_ident:ident,
        $(#[$sized_meta:meta])*
        sized => $sized_ident:ident,
        $(#[$try_meta:meta])*
        try => $try_ident:ident,
        $(#[$panics_meta:meta])*
        panics => $panics_ident:ident $(,)?
    ) => {
        $(#[$sized_meta])*
        #[inline]
        pub fn $sized_ident<const S: usize>(
            &mut self, input: &[u8; S], output: &mut [u8; S]
        ) -> Res {
            if !size_predicate(S) {
                return Res::ERR
            }
            unsafe {
                // SAFETY: Output size is guaranteed from type system to be at least the size
                // (or in this case equivalent) of the input.
                self.$ll_ident(input.as_slice(), output.as_mut_slice())
            }
        }

        $(#[$try_meta])*
        #[inline]
        pub fn $try_ident(&mut self, input: &[u8], output: &mut [u8]) -> Res {
            if !predicate(input.len(), output.len()) {
                return Res::ERR
            }

            unsafe {
                // SAFETY: `Self::predicate` ensures output is at least the size of input and
                // that the size does not overflow on u32 cast.
                self.$ll_ident(input, output)
            }
        }

        panic_api! {
        $(#[$panics_meta])*
        pub fn $panics_ident(&mut self, input: &[u8], output: &mut [u8]) {
            if self.$try_ident(input, output).is_err() {
                panic!("Failed to apply keystream");
            }
        }
        }

        $(#[$ll_meta])*
        pub unsafe fn $ll_ident(&mut self, input: &[u8], output: &mut [u8]) -> Res {
            let mut res = Res::new();

            res.ensure_0(wc_AesCtrEncrypt(
                addr_of_mut!(self.inner),
                output.as_mut_ptr(),
                input.as_ptr(),
                input.len() as u32
            ));

            res
        }
    };
}

macro_rules! impl_aes_type {
    (
        $(#[$struct_meta:meta])*
        struct $s_ident:ident,
        direction $dir:ident,
        api {
            $(#[$new_meta:meta])*
            $new_vis:vis new,
            $(#[$ll_meta:meta])*
            unsafe => $ll_ident:ident,
            $(#[$sized_meta:meta])*
            sized => $sized_ident:ident,
            $(#[$try_meta:meta])*
            try => $try_ident:ident,
            $(#[$panics_meta:meta])*
            panics => $panics_ident:ident $(,)?
        }
    ) => {
        $(#[$struct_meta])*
        #[repr(transparent)]
        pub struct $s_ident {
            inner: AesLL,
        }

        impl $s_ident {
            $(#[$new_meta])*
            $new_vis fn new(key: &Key, iv: &Iv) -> Result<Self, $crate::error::Unspecified> {
                let key_ptr = ConstPtr::new(::core::ptr::from_ref(key));
                let nonce_ptr = ConstPtr::new(::core::ptr::from_ref(iv));

                unsafe {
                    let (aes_ll, res) = create_aes_ctr(key_ptr, nonce_ptr, AesM::$dir);
                    res.unit_err_with(|| Self { inner: aes_ll.assume_init() })
                }
            }

            impl_aes_api! {
                $(#[$ll_meta])*
                unsafe => $ll_ident,
                $(#[$sized_meta])*
                sized => $sized_ident,
                $(#[$try_meta])*
                try => $try_ident,
                $(#[$panics_meta])*
                panics => $panics_ident
            }
        }

        impl Drop for $s_ident {
            #[inline]
            fn drop(&mut self) {
                unsafe {
                    // SAFETY:
                    //
                    // We are in the drop implementation, so we are never going to be using the
                    // `Aes` type again. Since we are configured to not malloc, this simply zeroes
                    // the secrets that were copied on `wc_AesSetKey` invocation. I wish there
                    // was a way to avoid the copying as I do not like secrets living in memory
                    // more than once, but I understand the decision to do this for ensuring safety.
                    wc_AesFree(addr_of_mut!(self.inner));
                }
            }
        }
    };
}

impl_aes_type! {
    /// Represents an AES-CTR (Counter Mode) instance.
    struct AesCtr,
    direction ENCRYPT,
    api {
        /// Create a new AES CTR instance.
        ///
        /// # Arguments
        ///
        /// * `key`   - The key material to use (which determines the number of rounds).
        /// * `iv` - The initialization vector (nonce).
        ///
        /// # Returns
        ///
        /// A new AES instance in CTR mode.
        ///
        /// # Note
        ///
        /// This copies the key and nonce in the underlying C code and is out of scope of this Rust
        /// API. At the end of the `AesCtr`'s lifetime these will be zeroed. It may be desirable
        /// to immediately zero the key and nonce passed to this function by reference post
        /// invocation.
        pub new,

        /// Apply the underlying keystream to the output buffer.
        ///
        /// This method performs no runtime safety checks.
        ///
        /// # Safety
        ///
        /// - The `output` buffer must be at least the size of the `input`.
        /// - The size of both buffers must be representable by an `unsigned int` (u32).
        ///
        /// # Arguments
        ///
        /// * `input` - The input to apply the keystream to.
        /// * `output` - The output buffer to store the result of applying the keystream.
        ///
        /// # Errors
        ///
        /// If the application of the keystream failed.
        ///
        /// # Example
        ///
        /// ```
        /// use wolf_crypto::{buf::Iv, aes::{Key, AesCtr}};
        ///
        /// // securely generate a random key and initialization vector ...
        /// # let mut key = Key::Aes256([1u8; 32]);
        /// # let iv = Iv::new([2u8; 16]);
        ///
        /// let mut input = [1u8; 32];
        /// let mut output = [0u8; 32];
        ///
        /// # unsafe {
        /// assert!(AesCtr::new(&key, &iv)
        ///     .unwrap()
        ///     .apply_keystream_unchecked(input.as_slice(), output.as_mut_slice())
        ///     .is_ok());
        /// # }
        ///
        /// assert_ne!(&output, &input);
        ///
        /// // and decrypt
        ///
        /// let mut original = [0u8; 32];
        /// # unsafe {
        /// assert!(AesCtr::new(&key, &iv)
        ///     .unwrap()
        ///     .apply_keystream_unchecked(output.as_slice(), original.as_mut_slice())
        ///     .is_ok());
        /// # }
        ///
        /// assert_eq!(&original, &input);
        ///
        /// key.zero();
        /// ```
        unsafe => apply_keystream_unchecked,

        /// Apply the underlying keystream to the output buffer, with the size of both the input and
        /// output buffers described at compile time to avoid most runtime checks.
        ///
        /// # Arguments
        ///
        /// * `input` - The input to apply the keystream to.
        /// * `output` - The output buffer to store the result of applying the keystream.
        ///
        /// # Errors
        ///
        /// - If the application of the keystream failed.
        /// - (Unlikely) If the size of the input and output buffer is greater than what can be
        ///   represented by an `unsigned int` (u32).
        ///
        /// # Example
        ///
        /// ```
        /// use wolf_crypto::{buf::Iv, aes::{Key, AesCtr}};
        /// // securely generate a random key and initialization vector ...
        /// # let mut key = Key::Aes256([1u8; 32]);
        /// # let iv = Iv::new([2u8; 16]);
        ///
        /// let mut input = [1u8; 32];
        /// let mut output = [0u8; 32];
        ///
        /// assert!(AesCtr::new(&key, &iv)
        ///     .unwrap()
        ///     .apply_keystream_sized(&input, &mut output)
        ///     .is_ok());
        ///
        /// assert_ne!(&input, &output);
        /// assert_ne!(output, [0u8; 32]);
        ///
        /// // and decrypt
        ///
        /// let mut plain = [0u8; 32];
        /// assert!(AesCtr::new(&key, &iv)
        ///     .unwrap()
        ///     .apply_keystream_sized(&output, &mut plain)
        ///     .is_ok());
        ///
        /// assert_eq!(&plain, &input);
        /// key.zero();
        /// ```
        sized => apply_keystream_sized,

        /// Try to apply the underlying keystream to the output buffer.
        ///
        /// # Arguments
        ///
        /// * `input` - The input to apply the keystream to.
        /// * `output` - The output buffer to store the result of applying the keystream.
        ///
        /// # Errors
        ///
        /// - If the application of the keystream failed.
        /// - If the `input` buffer is larger than the `output` buffer.
        /// - (Unlikely) If the size of the `input` or `output` buffer is greater than what can be
        ///   represented by an `unsigned int` (u32).
        ///
        /// # Example
        ///
        /// ```
        /// use wolf_crypto::{buf::Iv, aes::{Key, AesCtr}};
        /// // securely generate a random key and initialization vector ...
        /// # let mut key = Key::Aes256([1u8; 32]);
        /// # let iv = Iv::new([2u8; 16]);
        ///
        /// let mut input = [1u8; 32];
        /// let mut output = [0u8; 32];
        ///
        /// assert!(AesCtr::new(&key, &iv)
        ///     .unwrap()
        ///     .try_apply_keystream(input.as_slice(), output.as_mut_slice())
        ///     .is_ok());
        ///
        /// assert_ne!(&input, &output);
        /// assert_ne!(output, [0u8; 32]);
        ///
        /// // and decrypt
        ///
        /// let mut plain = [0u8; 32];
        /// assert!(AesCtr::new(&key, &iv)
        ///     .unwrap()
        ///     .try_apply_keystream(output.as_slice(), plain.as_mut_slice())
        ///     .is_ok());
        ///
        /// assert_eq!(&plain, &input);
        /// key.zero();
        /// ```
        try => try_apply_keystream,

        /// Apply the underlying keystream to the output buffer using the encryption key.
        ///
        /// # Arguments
        ///
        /// * `input` - The input to apply the keystream to.
        /// * `output` - The output buffer to store the result of applying the keystream.
        ///
        /// # Panics
        ///
        /// - If the application of the keystream failed.
        /// - If the `input` buffer is larger than the `output` buffer.
        /// - (Unlikely) If the size of the `input` or `output` buffer is greater than what can be
        ///   represented by an `unsigned int` (u32).
        ///
        /// # Example
        ///
        /// ```
        /// use wolf_crypto::{buf::Iv, aes::{Key, AesCtr}};
        /// // securely generate a random key and initialization vector ...
        /// # let mut key = Key::Aes256([1u8; 32]);
        /// # let iv = Iv::new([2u8; 16]);
        ///
        /// let mut input = [1u8; 32];
        /// let mut output = [0u8; 32];
        ///
        /// AesCtr::new(&key, &iv)
        ///     .unwrap()
        ///     .apply_keystream(input.as_slice(), output.as_mut_slice());
        ///
        /// assert_ne!(&input, &output);
        /// assert_ne!(output, [0u8; 32]);
        ///
        /// // and decrypt
        ///
        /// let mut plain = [0u8; 32];
        /// AesCtr::new(&key, &iv)
        ///     .unwrap()
        ///     .apply_keystream(output.as_slice(), plain.as_mut_slice());
        ///
        /// assert_eq!(&plain, &input);
        /// key.zero();
        /// ```
        panics => apply_keystream
    }
}

// SAFETY:
// All methods which mutate the underlying AES instance require a mutable reference,
// the only way to obtain a mutable reference across thread boundaries is via synchronization or
// unsafe in Rust (which then would be the user's responsibility).
unsafe impl Send for AesCtr {}

// SAFETY:
// There is no providing of interior mutability in the `AesCtr`, all methods which mutate the
// underlying AES instance require a mutable reference, thus making this safe to mark `Sync`.
unsafe impl Sync for AesCtr {}

#[cfg(test)]
mod tests {
    use ctr::Ctr128BE;
    use aes::Aes256;
    use ctr::cipher::{KeyIvInit, StreamCipher};
    use super::*;

    #[test]
    fn apply_smoke() {
        let key = Key::Aes256([7; 32]);
        let nonce = [0u8; 16].into();

        let mut ctr = AesCtr::new(&key, &nonce).unwrap();

        let input = [0u8; 12];
        let mut output = [0u8; 12];

        assert!(ctr.apply_keystream_sized(&input, &mut output).is_ok());

        let mut output2 = [0u8; 12];
        assert!(ctr.apply_keystream_sized(&input, &mut output2).is_ok());

        assert_ne!(output, output2);
    }

    #[test]
    fn against_ctr_rust_crypto_smoke() {
        let key = Key::Aes256([7; 32]);
        let nonce = [3; 16].into();
        let mut ctr = AesCtr::new(&key, &nonce).unwrap();

        let mut rc_ctr = Ctr128BE::<Aes256>::new_from_slices(
            key.as_slice(), nonce.slice()
        ).unwrap();

        let input = [0u8; 12];
        let mut out = [0u8; 12];
        let mut out_rc = [0u8; 12];

        rc_ctr.apply_keystream_b2b(input.as_slice(), out_rc.as_mut_slice()).unwrap();
        ctr.apply_keystream(input.as_slice(), out.as_mut_slice());

        assert_eq!(out, out_rc);
    }

    #[test]
    fn self_bijective_smoke() {
        let key = Key::Aes256([7; 32]);
        let nonce = [1u8; 16].into();

        let mut ctr = AesCtr::new(&key, &nonce).unwrap();

        let input = [1u8; 12];
        let mut output = [0u8; 12];

        assert!(ctr.apply_keystream_sized(&input, &mut output).is_ok());
        assert_ne!(output, input);

        let mut decrypt_ctr = AesCtr::new(&key, &nonce).unwrap();

        let mut plain = [0u8; 12];
        assert!(decrypt_ctr.apply_keystream_sized(&output, &mut plain).is_ok());

        assert_eq!(plain, input);
    }

    #[test]
    fn precondition_ensured() {
        let input = [0u8; 12];
        let mut output = [0u8; 11];

        let key = Key::Aes256([7; 32]);
        let nonce = [1u8; 16].into();

        let res = AesCtr::new(&key, &nonce).unwrap()
            .try_apply_keystream(input.as_slice(), output.as_mut_slice());

        assert!(res.is_err());
    }
}

#[cfg(all(test, not(miri)))]
mod property_tests {
    use aes::{Aes256, Aes192, Aes128};
    use ctr::cipher::{KeyIvInit, StreamCipher};
    use ctr::Ctr128BE;
    use proptest::prelude::*;
    use crate::aes::test_utils::*;
    use super::*;

    macro_rules! with_rust_crypto_ctr {
        ($key:expr, $nonce:expr, |$ctr:ident| $do:expr) => {
            match $key {
                Key::Aes256(buf) => {
                    let mut $ctr = Ctr128BE::<Aes256>::new_from_slices(
                        buf.as_slice(), $nonce.slice()
                    ).unwrap();

                    $do
                },
                Key::Aes128(buf) => {
                    let mut $ctr = Ctr128BE::<Aes128>::new_from_slices(
                        buf.as_slice(), $nonce.slice()
                    ).unwrap();

                    $do
                },
                Key::Aes192(buf) => {
                    let mut $ctr = Ctr128BE::<Aes192>::new_from_slices(
                        buf.as_slice(), $nonce.slice()
                    ).unwrap();

                    $do
                }
            }
        };
    }

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(10000))]

        #[test]
        fn self_bijective(
            input in any::<BoundList<1028>>(),
            key in any::<Key>(),
            nonce in any::<Iv>()
        ) {
            let mut output = input.create_self();

            let res = AesCtr::new(&key, &nonce)
                .unwrap()
                .try_apply_keystream(input.as_slice(), output.as_mut_slice());

            prop_assert!(res.is_ok());

            if input.len() >= 2 {
                prop_assert_ne!(&output, &input);
            }

            let mut plain = input.create_self();
            let res = AesCtr::new(&key, &nonce)
                .unwrap()
                .try_apply_keystream(output.as_slice(), plain.as_mut_slice());

            prop_assert!(res.is_ok());

            prop_assert_eq!(plain.as_slice(), input.as_slice());
        }

        #[test]
        fn from_ctr_crate_to_wolf(
            input in any::<BoundList<1028>>(),
            key in any::<Key>(),
            nonce in any::<Iv>()
        ) {
            let mut ctr = AesCtr::new(&key, &nonce).unwrap();
            let mut c_in = input;

            with_rust_crypto_ctr!(key, nonce, |o_ctr| {
                o_ctr.apply_keystream(c_in.as_mut_slice());
            });

            let mut plain = input.create_self();
            ctr.apply_keystream(c_in.as_slice(), plain.as_mut_slice());

            prop_assert_eq!(plain.as_slice(), input.as_slice());
        }

        #[test]
        fn from_wolf_to_ctr_crate(
            input in any::<BoundList<1028>>(),
            key in any::<Key>(),
            nonce in any::<Iv>()
        ) {
            let mut ctr = AesCtr::new(&key, &nonce).unwrap();
            let mut cipher = input.create_self();

            ctr.apply_keystream(input.as_slice(), cipher.as_mut_slice());

            if input.len() >= 2 {
                prop_assert_ne!(&input, &cipher);
            }

            with_rust_crypto_ctr!(key, nonce, |o_ctr| {
                o_ctr.apply_keystream(cipher.as_mut_slice());
            });

            prop_assert_eq!(cipher.as_slice(), input.as_slice());
        }
    }

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(1000))]

        #[test]
        fn wolf_and_ctr_crate_eq_many_invocations(
            mut input in any::<BoundList<512>>(),
            key in any::<Key>(),
            nonce in any::<Iv>(),
        ) {
            let mut ctr = AesCtr::new(&key, &nonce).unwrap();

            with_rust_crypto_ctr!(key, nonce, |o_ctr| {
                for _ in 0..256 {
                    let mut wolf_out = input.create_self();
                    ctr.apply_keystream(input.as_slice(), wolf_out.as_mut_slice());

                    o_ctr.apply_keystream(input.as_mut_slice());

                    prop_assert_eq!(wolf_out.as_slice(), input.as_slice());
                }
            });
        }
    }
}