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
use super::Block;
use crate::{endian::*, error};
use core::convert::TryInto;
use core::marker::PhantomData;
pub struct Nonce([u8; NONCE_LEN]);
impl Nonce {
#[inline]
pub fn try_assume_unique_for_key(value: &[u8]) -> Result<Self, error::Unspecified> {
let value: &[u8; NONCE_LEN] = value.try_into()?;
Ok(Self::assume_unique_for_key(*value))
}
#[inline]
pub fn assume_unique_for_key(value: [u8; NONCE_LEN]) -> Self {
Self(value)
}
}
impl AsRef<[u8; NONCE_LEN]> for Nonce {
fn as_ref(&self) -> &[u8; NONCE_LEN] {
&self.0
}
}
pub const NONCE_LEN: usize = 96 / 8;
#[repr(C)]
pub union Counter<U32: Layout<u32>>
where
u32: From<U32>,
{
block: Block,
u32s: [U32; 4],
encoding: PhantomData<U32>,
}
impl<U32: Layout<u32>> Counter<U32>
where
u32: From<U32>,
{
pub fn zero(nonce: Nonce) -> Self {
Self::new(nonce, 0)
}
pub fn one(nonce: Nonce) -> Self {
Self::new(nonce, 1)
}
#[cfg(test)]
pub fn from_test_vector(nonce: &[u8], initial_counter: u32) -> Self {
Self::new(
Nonce::try_assume_unique_for_key(nonce).unwrap(),
initial_counter,
)
}
fn new(Nonce(nonce): Nonce, initial_counter: u32) -> Self {
let mut r = Self {
block: Block::zero(),
};
let block = unsafe { &mut r.block };
block.as_mut()[U32::NONCE_BYTE_INDEX..][..NONCE_LEN].copy_from_slice(nonce.as_ref());
r.increment_by_less_safe(initial_counter);
r
}
#[inline]
pub fn increment(&mut self) -> Iv {
let block = unsafe { &self.block };
let r = Iv(block.clone());
self.increment_by_less_safe(1);
r
}
#[inline]
pub fn increment_by_less_safe(&mut self, increment_by: u32) {
let u32s = unsafe { &mut self.u32s };
let value = &mut u32s[U32::COUNTER_U32_INDEX];
*value = (u32::from(*value) + increment_by).into();
}
}
#[repr(C)]
pub struct Iv(Block);
impl<U32: Layout<u32>> From<Counter<U32>> for Iv
where
u32: From<U32>,
{
fn from(counter: Counter<U32>) -> Self {
Self(unsafe { counter.block })
}
}
impl Iv {
#[inline]
pub fn assume_unique_for_key(a: Block) -> Self {
Self(a)
}
#[inline]
pub fn into_block_less_safe(self) -> Block {
self.0
}
}
pub trait Layout<T>: Encoding<T>
where
T: From<Self>,
{
const COUNTER_U32_INDEX: usize;
const NONCE_BYTE_INDEX: usize;
}
impl<T> Layout<T> for BigEndian<T>
where
BigEndian<T>: Encoding<T>,
T: Copy + From<Self>,
{
const COUNTER_U32_INDEX: usize = 3;
const NONCE_BYTE_INDEX: usize = 0;
}
impl<T> Layout<T> for LittleEndian<T>
where
LittleEndian<T>: Encoding<T>,
T: Copy + From<Self>,
{
const COUNTER_U32_INDEX: usize = 0;
const NONCE_BYTE_INDEX: usize = 4;
}