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
#![feature(i128_type)]
#![no_std]
#![forbid(unsafe_code)]
const ROUNDS: u64 = 32;
macro_rules! round {
($x:ident, $y:ident, $k:ident) => {
$x = $x.rotate_right(8);
$x = $x.wrapping_add($y);
$x ^= $k;
$y = $y.rotate_left(3);
$y ^= $x;
}
}
macro_rules! inv_round {
($x:ident, $y:ident, $k:ident) => {
$y ^= $x;
$y = $y.rotate_right(3);
$x ^= $k;
$x = $x.wrapping_sub($y);
$x = $x.rotate_left(8);
}
}
pub fn encrypt_block(m: u128, k: u128) -> u128 {
let mut m1 = (m >> 64) as u64;
let mut m2 = m as u64;
let mut k1 = (k >> 64) as u64;
let mut k2 = k as u64;
round!(m1, m2, k2);
for i in 0..ROUNDS - 1 {
round!(k1, k2, i);
round!(m1, m2, k2);
}
m2 as u128 | (m1 as u128) << 64
}
pub struct Key {
schedule: [u64; ROUNDS as usize],
}
impl Key {
pub fn new(k: u128) -> Key {
let mut k1 = (k >> 64) as u64;
let mut k2 = k as u64;
let mut ret = Key {
schedule: [0; ROUNDS as usize],
};
for i in 0..ROUNDS {
ret.schedule[i as usize] = k2;
round!(k1, k2, i);
}
ret
}
pub fn encrypt_block(&self, m: u128) -> u128 {
let mut m1 = (m >> 64) as u64;
let mut m2 = m as u64;
for &k in &self.schedule {
round!(m1, m2, k);
}
m2 as u128 | (m1 as u128) << 64
}
pub fn decrypt_block(&self, c: u128) -> u128 {
let mut c1 = (c >> 64) as u64;
let mut c2 = c as u64;
for &k in self.schedule.iter().rev() {
inv_round!(c1, c2, k);
}
c2 as u128 | (c1 as u128) << 64
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn encrypt_decrypt() {
for mut x in 0u128..90000 {
x = x.wrapping_mul(0x6eed0e9da4d94a4f6eed0e9da4d94a4f);
x ^= (x >> 6) >> (x >> 122);
x = x.wrapping_mul(0x6eed0e9da4d94a4f6eed0e9da4d94a4f);
let key = Key::new(!x);
assert_eq!(key.decrypt_block(key.encrypt_block(x)), x);
assert_eq!(key.encrypt_block(x), encrypt_block(x, !x));
}
}
#[test]
fn test_vectors() {
assert_eq!(
encrypt_block(
0x6c617669757165207469206564616d20,
0x0f0e0d0c0b0a09080706050403020100
),
0xa65d9851797832657860fedf5c570d18
);
}
}