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
//! 128-bit counter falvors.
use super::CtrFlavor;
use cipher::generic_array::{
    typenum::{operator_aliases::PartialQuot, type_operators::PartialDiv, Unsigned, U16},
    ArrayLength, GenericArray,
};
use core::convert::TryInto;

type ChunkSize = U16;
type Chunks<B> = PartialQuot<B, ChunkSize>;
const CS: usize = ChunkSize::USIZE;

/// 128-bit big endian counter flavor.
#[derive(Default, Clone)]
#[repr(transparent)]
pub struct Ctr128BE(u128);

impl<B> CtrFlavor<B> for Ctr128BE
where
    Self: Default + Clone,
    B: ArrayLength<u8> + PartialDiv<ChunkSize>,
    Chunks<B>: ArrayLength<u128>,
{
    type Nonce = GenericArray<u128, Chunks<B>>;
    type Backend = u128;

    #[inline]
    fn generate_block(&self, nonce: &Self::Nonce) -> GenericArray<u8, B> {
        let mut block = GenericArray::<u8, B>::default();
        for i in 0..Chunks::<B>::USIZE {
            let t = if i == Chunks::<B>::USIZE - 1 {
                self.0.wrapping_add(nonce[i]).to_be_bytes()
            } else {
                nonce[i].to_ne_bytes()
            };
            block[CS * i..][..CS].copy_from_slice(&t);
        }
        block
    }

    #[inline]
    fn load(block: &GenericArray<u8, B>) -> Self::Nonce {
        let mut res = Self::Nonce::default();
        for i in 0..Chunks::<B>::USIZE {
            let chunk = block[CS * i..][..CS].try_into().unwrap();
            res[i] = if i == Chunks::<B>::USIZE - 1 {
                u128::from_be_bytes(chunk)
            } else {
                u128::from_ne_bytes(chunk)
            }
        }
        res
    }

    #[inline]
    fn checked_add(&self, rhs: usize) -> Option<Self> {
        rhs.try_into()
            .ok()
            .and_then(|rhs| self.0.checked_add(rhs))
            .map(Self)
    }

    #[inline]
    fn increment(&mut self) {
        self.0 = self.0.wrapping_add(1);
    }

    #[inline]
    fn to_backend(&self) -> Self::Backend {
        self.0
    }

    #[inline]
    fn from_backend(v: Self::Backend) -> Self {
        Self(v)
    }
}

/// 128-bit little endian counter flavor.
#[derive(Default, Clone)]
#[repr(transparent)]
pub struct Ctr128LE(u128);

impl<B> CtrFlavor<B> for Ctr128LE
where
    Self: Default + Clone,
    B: ArrayLength<u8> + PartialDiv<ChunkSize>,
    Chunks<B>: ArrayLength<u128>,
{
    type Nonce = GenericArray<u128, Chunks<B>>;
    type Backend = u128;

    #[inline]
    fn generate_block(&self, nonce: &Self::Nonce) -> GenericArray<u8, B> {
        let mut block = GenericArray::<u8, B>::default();
        for i in 0..Chunks::<B>::USIZE {
            let t = if i == 0 {
                self.0.wrapping_add(nonce[i]).to_le_bytes()
            } else {
                nonce[i].to_ne_bytes()
            };
            block[CS * i..][..CS].copy_from_slice(&t);
        }
        block
    }

    #[inline]
    fn load(block: &GenericArray<u8, B>) -> Self::Nonce {
        let mut res = Self::Nonce::default();
        for i in 0..Chunks::<B>::USIZE {
            let chunk = block[CS * i..][..CS].try_into().unwrap();
            res[i] = if i == 0 {
                u128::from_le_bytes(chunk)
            } else {
                u128::from_ne_bytes(chunk)
            }
        }
        res
    }

    #[inline]
    fn checked_add(&self, rhs: usize) -> Option<Self> {
        rhs.try_into()
            .ok()
            .and_then(|rhs| self.0.checked_add(rhs))
            .map(Self)
    }

    #[inline]
    fn increment(&mut self) {
        self.0 = self.0.wrapping_add(1);
    }

    #[inline]
    fn to_backend(&self) -> Self::Backend {
        self.0
    }

    #[inline]
    fn from_backend(v: Self::Backend) -> Self {
        Self(v)
    }
}