-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathctr32.rs
More file actions
172 lines (151 loc) · 4.39 KB
/
ctr32.rs
File metadata and controls
172 lines (151 loc) · 4.39 KB
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
//! 32-bit counter flavors.
use super::CtrFlavor;
use cipher::{
array::{Array, ArraySize},
typenum::{PartialDiv, PartialQuot, U4, Unsigned},
};
use core::fmt;
type ChunkSize = U4;
type Chunks<B> = PartialQuot<B, ChunkSize>;
const CS: usize = ChunkSize::USIZE;
#[derive(Clone)]
pub struct CtrNonce32<N: ArraySize> {
ctr: u32,
nonce: Array<u32, N>,
}
impl<N: ArraySize> fmt::Debug for CtrNonce32<N> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("CtrNonce32 { ... }")
}
}
impl<N: ArraySize> Drop for CtrNonce32<N> {
fn drop(&mut self) {
#[cfg(feature = "zeroize")]
{
use cipher::zeroize::Zeroize;
self.ctr.zeroize();
self.nonce.zeroize();
}
}
}
#[cfg(feature = "zeroize")]
impl<N: ArraySize> cipher::zeroize::ZeroizeOnDrop for CtrNonce32<N> {}
/// 32-bit big endian counter flavor.
#[derive(Clone, Copy, Debug)]
pub enum Ctr32BE {}
impl<B> CtrFlavor<B> for Ctr32BE
where
B: ArraySize + PartialDiv<ChunkSize>,
Chunks<B>: ArraySize,
{
type CtrNonce = CtrNonce32<Chunks<B>>;
type Backend = u32;
const NAME: &'static str = "32BE";
#[inline]
fn remaining(cn: &Self::CtrNonce) -> Option<usize> {
(u32::MAX - cn.ctr).try_into().ok()
}
#[inline(always)]
fn current_block(cn: &Self::CtrNonce) -> Array<u8, B> {
let mut block = Array::<u8, B>::default();
for i in 0..Chunks::<B>::USIZE {
let t = if i == Chunks::<B>::USIZE - 1 {
cn.ctr.wrapping_add(cn.nonce[i]).to_be_bytes()
} else {
cn.nonce[i].to_ne_bytes()
};
block[CS * i..][..CS].copy_from_slice(&t);
}
block
}
#[inline]
fn next_block(cn: &mut Self::CtrNonce) -> Array<u8, B> {
let block = Self::current_block(cn);
cn.ctr = cn.ctr.wrapping_add(1);
block
}
#[inline]
fn from_nonce(block: &Array<u8, B>) -> Self::CtrNonce {
let mut nonce = Array::<u32, Chunks<B>>::default();
for i in 0..Chunks::<B>::USIZE {
let chunk = block[CS * i..][..CS]
.try_into()
.expect("should be the correct size");
nonce[i] = if i == Chunks::<B>::USIZE - 1 {
u32::from_be_bytes(chunk)
} else {
u32::from_ne_bytes(chunk)
}
}
let ctr = 0;
Self::CtrNonce { ctr, nonce }
}
#[inline]
fn as_backend(cn: &Self::CtrNonce) -> Self::Backend {
cn.ctr
}
#[inline]
fn set_from_backend(cn: &mut Self::CtrNonce, v: Self::Backend) {
cn.ctr = v;
}
}
/// 32-bit little endian counter flavor.
#[derive(Clone, Copy, Debug)]
pub enum Ctr32LE {}
impl<B> CtrFlavor<B> for Ctr32LE
where
B: ArraySize + PartialDiv<ChunkSize>,
Chunks<B>: ArraySize,
{
type CtrNonce = CtrNonce32<Chunks<B>>;
type Backend = u32;
const NAME: &'static str = "32LE";
#[inline]
fn remaining(cn: &Self::CtrNonce) -> Option<usize> {
(u32::MAX - cn.ctr).try_into().ok()
}
#[inline(always)]
fn current_block(cn: &Self::CtrNonce) -> Array<u8, B> {
let mut block = Array::<u8, B>::default();
for i in 0..Chunks::<B>::USIZE {
let t = if i == 0 {
cn.ctr.wrapping_add(cn.nonce[i]).to_le_bytes()
} else {
cn.nonce[i].to_ne_bytes()
};
block[CS * i..][..CS].copy_from_slice(&t);
}
block
}
#[inline]
fn next_block(cn: &mut Self::CtrNonce) -> Array<u8, B> {
let block = Self::current_block(cn);
cn.ctr = cn.ctr.wrapping_add(1);
block
}
#[inline]
fn from_nonce(block: &Array<u8, B>) -> Self::CtrNonce {
let mut nonce = Array::<u32, Chunks<B>>::default();
for i in 0..Chunks::<B>::USIZE {
let chunk = block[CS * i..][..CS]
.try_into()
.expect("should be the correct size");
nonce[i] = if i == 0 {
u32::from_le_bytes(chunk)
} else {
u32::from_ne_bytes(chunk)
}
}
let ctr = 0;
Self::CtrNonce { ctr, nonce }
}
#[inline]
fn as_backend(cn: &Self::CtrNonce) -> Self::Backend {
cn.ctr
}
#[inline]
fn set_from_backend(cn: &mut Self::CtrNonce, v: Self::Backend) {
cn.ctr = v;
}
}