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
#![feature(i128_type, const_fn)]
#![no_std]
#![forbid(unsafe_code)]
extern crate speck;
extern crate byteorder;
use speck::Key;
use byteorder::ByteOrder;
pub const BLOCK_SIZE: usize = core::mem::size_of::<u128>();
pub fn encrypt<Endian: ByteOrder>(input: &[u8], mut output: &mut [u8], key: &[u8], iv: &[u8]) {
assert_eq!(input.len() % BLOCK_SIZE, 0);
assert_eq!(output.len(), input.len());
assert_eq!(key.len(), BLOCK_SIZE);
assert_eq!(iv.len(), BLOCK_SIZE);
let key_schedule = Key::new(Endian::read_u128(key));
let mut last_block_ciphertext = 0;
for (block_index, (block_input, mut block_output)) in input
.chunks(BLOCK_SIZE)
.zip(output.chunks_mut(BLOCK_SIZE))
.enumerate()
{
let block_plaintext = Endian::read_u128(block_input);
let block_xor = block_plaintext ^ if block_index == 0 {
Endian::read_u128(iv)
} else {
last_block_ciphertext
};
let block_ciphertext = key_schedule.encrypt_block(block_xor);
last_block_ciphertext = block_ciphertext;
Endian::write_u128(block_output, block_ciphertext);
}
}
pub fn decrypt<Endian: ByteOrder>(input: &[u8], mut output: &mut [u8], key: &[u8], iv: &[u8]) {
assert_eq!(input.len() % BLOCK_SIZE, 0);
assert_eq!(output.len(), input.len());
assert_eq!(key.len(), BLOCK_SIZE);
assert_eq!(iv.len(), BLOCK_SIZE);
let key_schedule = Key::new(Endian::read_u128(key));
let mut last_block_ciphertext = 0;
for (block_index, (block_input, mut block_output)) in input
.chunks(BLOCK_SIZE)
.zip(output.chunks_mut(BLOCK_SIZE))
.enumerate()
{
let block_ciphertext = Endian::read_u128(block_input);
let block_xor = key_schedule.decrypt_block(block_ciphertext);
let block_plaintext = block_xor ^ if block_index == 0 {
Endian::read_u128(iv)
} else {
last_block_ciphertext
};
last_block_ciphertext = block_ciphertext;
Endian::write_u128(block_output, block_plaintext);
}
}