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
#![no_std]
use core::{mem, ptr};
pub fn write_u64_be(dst: &mut [u8], mut input: u64) {
assert!(dst.len() == 8);
input = input.to_be();
unsafe {
let tmp = &input as *const _ as *const u8;
ptr::copy_nonoverlapping(tmp, dst.get_unchecked_mut(0), 8);
}
}
pub fn write_u64_le(dst: &mut [u8], mut input: u64) {
assert!(dst.len() == 8);
input = input.to_le();
unsafe {
let tmp = &input as *const _ as *const u8;
ptr::copy_nonoverlapping(tmp, dst.get_unchecked_mut(0), 8);
}
}
pub fn write_u64v_be(dst: &mut [u8], input: &[u64]) {
assert!(dst.len() == 8 * input.len());
unsafe {
let mut x: *mut u8 = dst.get_unchecked_mut(0);
let mut y: *const u64 = input.get_unchecked(0);
for _ in 0..input.len() {
let tmp = (*y).to_be();
ptr::copy_nonoverlapping(&tmp as *const _ as *const u8, x, 8);
x = x.offset(8);
y = y.offset(1);
}
}
}
pub fn write_u64v_le(dst: &mut [u8], input: &[u64]) {
assert!(dst.len() == 8 * input.len());
unsafe {
let mut x: *mut u8 = dst.get_unchecked_mut(0);
let mut y: *const u64 = input.get_unchecked(0);
for _ in 0..input.len() {
let tmp = (*y).to_le();
ptr::copy_nonoverlapping(&tmp as *const _ as *const u8, x, 8);
x = x.offset(8);
y = y.offset(1);
}
}
}
pub fn write_u32_be(dst: &mut [u8], mut input: u32) {
assert!(dst.len() == 4);
input = input.to_be();
unsafe {
let tmp = &input as *const _ as *const u8;
ptr::copy_nonoverlapping(tmp, dst.get_unchecked_mut(0), 4);
}
}
pub fn write_u32_le(dst: &mut [u8], mut input: u32) {
assert!(dst.len() == 4);
input = input.to_le();
unsafe {
let tmp = &input as *const _ as *const u8;
ptr::copy_nonoverlapping(tmp, dst.get_unchecked_mut(0), 4);
}
}
pub fn write_u32v_le(dst: &mut [u8], input: &[u32]) {
assert!(dst.len() == 4 * input.len());
unsafe {
let mut x: *mut u8 = dst.get_unchecked_mut(0);
let mut y: *const u32 = input.get_unchecked(0);
for _ in 0..input.len() {
let tmp = (*y).to_le();
ptr::copy_nonoverlapping(&tmp as *const _ as *const u8, x, 4);
x = x.offset(4);
y = y.offset(1);
}
}
}
pub fn write_u32v_be(dst: &mut [u8], input: &[u32]) {
assert!(dst.len() == 4 * input.len());
unsafe {
let mut x: *mut u8 = dst.get_unchecked_mut(0);
let mut y: *const u32 = input.get_unchecked(0);
for _ in 0..input.len() {
let tmp = (*y).to_be();
ptr::copy_nonoverlapping(&tmp as *const _ as *const u8, x, 4);
x = x.offset(4);
y = y.offset(1);
}
}
}
pub fn read_u64v_be(dst: &mut [u64], input: &[u8]) {
assert!(dst.len() * 8 == input.len());
unsafe {
let mut x: *mut u64 = dst.get_unchecked_mut(0);
let mut y: *const u8 = input.get_unchecked(0);
for _ in 0..dst.len() {
let mut tmp: u64 = mem::uninitialized();
ptr::copy_nonoverlapping(y, &mut tmp as *mut _ as *mut u8, 8);
*x = u64::from_be(tmp);
x = x.offset(1);
y = y.offset(8);
}
}
}
pub fn read_u64v_le(dst: &mut [u64], input: &[u8]) {
assert!(dst.len() * 8 == input.len());
unsafe {
let mut x: *mut u64 = dst.get_unchecked_mut(0);
let mut y: *const u8 = input.get_unchecked(0);
for _ in 0..dst.len() {
let mut tmp: u64 = mem::uninitialized();
ptr::copy_nonoverlapping(y, &mut tmp as *mut _ as *mut u8, 8);
*x = u64::from_le(tmp);
x = x.offset(1);
y = y.offset(8);
}
}
}
pub fn read_u32v_be(dst: &mut [u32], input: &[u8]) {
assert!(dst.len() * 4 == input.len());
unsafe {
let mut x: *mut u32 = dst.get_unchecked_mut(0);
let mut y: *const u8 = input.get_unchecked(0);
for _ in 0..dst.len() {
let mut tmp: u32 = mem::uninitialized();
ptr::copy_nonoverlapping(y, &mut tmp as *mut _ as *mut u8, 4);
*x = u32::from_be(tmp);
x = x.offset(1);
y = y.offset(4);
}
}
}
pub fn read_u32v_le(dst: &mut [u32], input: &[u8]) {
assert!(dst.len() * 4 == input.len());
unsafe {
let mut x: *mut u32 = dst.get_unchecked_mut(0);
let mut y: *const u8 = input.get_unchecked(0);
for _ in 0..dst.len() {
let mut tmp: u32 = mem::uninitialized();
ptr::copy_nonoverlapping(y, &mut tmp as *mut _ as *mut u8, 4);
*x = u32::from_le(tmp);
x = x.offset(1);
y = y.offset(4);
}
}
}
pub fn read_u32_le(input: &[u8]) -> u32 {
assert!(input.len() == 4);
unsafe {
let mut tmp: u32 = mem::uninitialized();
ptr::copy_nonoverlapping(input.get_unchecked(0),
&mut tmp as *mut _ as *mut u8,
4);
u32::from_le(tmp)
}
}
pub fn read_u32_be(input: &[u8]) -> u32 {
assert!(input.len() == 4);
unsafe {
let mut tmp: u32 = mem::uninitialized();
ptr::copy_nonoverlapping(input.get_unchecked(0),
&mut tmp as *mut _ as *mut u8,
4);
u32::from_be(tmp)
}
}
pub fn xor_keystream(dst: &mut [u8], plaintext: &[u8], keystream: &[u8]) {
assert!(dst.len() == plaintext.len());
assert!(plaintext.len() <= keystream.len());
let p = plaintext.as_ptr();
let k = keystream.as_ptr();
let d = dst.as_mut_ptr();
for i in 0isize..plaintext.len() as isize {
unsafe { *d.offset(i) = *p.offset(i) ^ *k.offset(i) };
}
}
#[inline]
pub fn copy_memory(src: &[u8], dst: &mut [u8]) {
assert!(dst.len() >= src.len());
unsafe {
let srcp = src.as_ptr();
let dstp = dst.as_mut_ptr();
ptr::copy_nonoverlapping(srcp, dstp, src.len());
}
}
#[inline]
pub fn zero(dst: &mut [u8]) {
unsafe {
ptr::write_bytes(dst.as_mut_ptr(), 0, dst.len());
}
}
fn to_bits(x: u64) -> (u64, u64) { (x >> 61, x << 3) }
pub fn add_bytes_to_bits(bits: u64, bytes: u64) -> u64 {
let (new_high_bits, new_low_bits) = to_bits(bytes);
if new_high_bits > 0 {
panic!("Numeric overflow occured.")
}
bits.checked_add(new_low_bits).expect("Numeric overflow occured.")
}
pub fn add_bytes_to_bits_tuple(bits: (u64, u64), bytes: u64) -> (u64, u64) {
let (new_high_bits, new_low_bits) = to_bits(bytes);
let (hi, low) = bits;
match low.checked_add(new_low_bits) {
Some(x) => {
if new_high_bits == 0 {
(hi, x)
} else {
match hi.checked_add(new_high_bits) {
Some(y) => (y, x),
None => panic!("Numeric overflow occured."),
}
}
},
None => {
let z = match new_high_bits.checked_add(1) {
Some(w) => w,
None => panic!("Numeric overflow occured."),
};
match hi.checked_add(z) {
Some(y) => (y, low.wrapping_add(new_low_bits)),
None => panic!("Numeric overflow occured."),
}
},
}
}
#[cfg(test)]
pub mod tests;