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
#![warn(missing_docs)]
#![doc(html_root_url = "https://docs.rs/ipcrypt/0.1.0")]

//! `ipcrypt` was designed by Jean-Philippe Aumasson to encrypt IPv4
//! addresses with 16-byte keys, where the result is still an IPv4
//! address.
//!
//! Derived from the implementation at: <https://github.com/veorq/ipcrypt>
//!
//! As input and output this implementation takes various types
//! representing a sequence of 4 bytes.  `u32` is interpreted as big
//! endian (network order; for consistency with how IPv4 adresses are
//! represented as `u32`).
//!
//! This crate supports a `no-std` feature which removes support for
//! `Ipv4Addr` (because it's not available in `core`).
//!
//! # Example
//!
//! ```
//! use std::net::Ipv4Addr;
//! let addr = "127.0.0.1".parse::<Ipv4Addr>().unwrap();
//! println!("{}", ipcrypt::encrypt(addr, b"some 16-byte key"));
//! ```

#![cfg_attr(feature = "no-std", no_std)]
#![no_implicit_prelude]

#[cfg(not(feature = "no-std"))]
extern crate core;

#[cfg(not(feature = "no-std"))]
use std::net::Ipv4Addr;

use core::convert::{From, Into};
use core::ops::BitXorAssign;

/// Alias for the key type (16 bytes)
pub type Key = [u8; 16];

/// The inner state permutations are build on.  Input and Output types
/// are converted to an from this type.
///
/// You could provide custom `From` and `Into` implementations for local
/// types, and then use [`encrypt`] and [`decrypt`] directly on those
/// types.
///
/// [`encrypt`]: fn.encrypt.html
/// [`decrypt`]: fn.decrypt.html
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct State(u8, u8, u8, u8);

impl State {
	fn encrypt(mut self, key: &Key) -> Self {
		let KeyStates(a, b, c, d) = KeyStates::from(key);

		self ^= a;
		self = self.permute();
		self ^= b;
		self = self.permute();
		self ^= c;
		self = self.permute();
		self ^= d;

		self
	}

	fn decrypt(mut self, key: &Key) -> Self {
		let KeyStates(a, b, c, d) = KeyStates::from(key);

		self ^= d;
		self = self.permute_inverse();
		self ^= c;
		self = self.permute_inverse();
		self ^= b;
		self = self.permute_inverse();
		self ^= a;

		self
	}

	fn permute(self) -> Self {
		let State(mut a, mut b, mut c, mut d) = self;

		a = a.wrapping_add(b);
		c = c.wrapping_add(d);
		b = b.rotate_left(2);
		d = d.rotate_left(5);
		b ^= a;
		d ^= c;
		a = a.rotate_left(4);
		a = a.wrapping_add(d);
		c = c.wrapping_add(b);
		b = b.rotate_left(3);
		d = d.rotate_left(7);
		b ^= c;
		d ^= a;
		c = c.rotate_left(4);

		State(a, b, c, d)
	}

	fn permute_inverse(self) -> Self {
		let State(mut a, mut b, mut c, mut d) = self;

		c = c.rotate_left(4);
		b ^= c;
		d ^= a;
		b = b.rotate_left(5);
		d = d.rotate_left(1);
		a = a.wrapping_sub(d);
		c = c.wrapping_sub(b);
		a = a.rotate_left(4);
		b ^= a;
		d ^= c;
		b = b.rotate_left(6);
		d = d.rotate_left(3);
		a = a.wrapping_sub(b);
		c = c.wrapping_sub(d);

		State(a, b, c, d)
	}
}

impl From<[u8; 4]> for State {
	#[inline(always)]
	fn from(v: [u8; 4]) -> Self {
		State(v[0], v[1], v[2], v[3])
	}
}

impl Into<[u8; 4]> for State {
	#[inline(always)]
	fn into(self) -> [u8; 4] {
		let State(a, b, c, d) = self;
		[a, b, c, d]
	}
}

#[cfg(not(feature = "no-std"))]
impl From<Ipv4Addr> for State {
	#[inline(always)]
	fn from(ip: Ipv4Addr) -> Self {
		let o = ip.octets();
		State(o[0], o[1], o[2], o[3])
	}
}

#[cfg(not(feature = "no-std"))]
impl Into<Ipv4Addr> for State {
	#[inline(always)]
	fn into(self) -> Ipv4Addr {
		let octets: [u8; 4] = self.into();
		octets.into()
	}
}

impl From<u32> for State {
	#[inline(always)]
	fn from(v: u32) -> Self {
		let (a, b, c, d) =
			((v >> 24) as u8, (v >> 16) as u8, (v >> 8) as u8, v as u8);
		State(a, b, c, d)
	}
}

impl Into<u32> for State {
	#[inline(always)]
	fn into(self) -> u32 {
		let State(a, b, c, d) = self;
		((a as u32) << 24) | ((b as u32) << 16) | ((c as u32) << 8) | (d as u32)
	}
}

impl BitXorAssign for State {
	#[inline(always)]
	fn bitxor_assign(&mut self, rhs: State) {
		self.0 ^= rhs.0;
		self.1 ^= rhs.1;
		self.2 ^= rhs.2;
		self.3 ^= rhs.3;
	}
}

/// Represents 16-byte key (which is internally read as 4 `State`s).
#[derive(Clone, Copy, PartialEq, Eq)]
struct KeyStates(State, State, State, State);

impl<'a> From<&'a Key> for KeyStates {
	fn from(key: &'a Key) -> KeyStates {
		KeyStates(
			State(key[0], key[1], key[2], key[3]),
			State(key[4], key[5], key[6], key[7]),
			State(key[8], key[9], key[10], key[11]),
			State(key[12], key[13], key[14], key[15]),
		)
	}
}

/// Encrypt value with given key.
///
/// # Example
///
/// ```
/// use std::net::Ipv4Addr;
/// let addr = "127.0.0.1".parse::<Ipv4Addr>().unwrap();
/// println!("{}", ipcrypt::encrypt(addr, b"some 16-byte key"));
/// ```
pub fn encrypt<T>(v: T, key: &Key) -> T
where
	State: From<T> + Into<T>,
{
	State::from(v).encrypt(key).into()
}

/// Decrypt value with given key.
///
/// # Example
///
/// ```
/// use std::net::Ipv4Addr;
/// let addr = "114.62.227.59".parse::<Ipv4Addr>().unwrap();
/// println!("{}", ipcrypt::decrypt(addr, b"some 16-byte key"));
/// ```
pub fn decrypt<T>(v: T, key: &Key) -> T
where
	State: From<T> + Into<T>,
{
	State::from(v).decrypt(key).into()
}

#[cfg(test)]
#[cfg(not(feature = "no-std"))]
mod test {
	use {decrypt, encrypt, Key};
	use std::net::Ipv4Addr;

	fn check_addr(key: &Key, plain: Ipv4Addr, cipher: Ipv4Addr) {
		assert_eq!(encrypt(plain, key), cipher);

		assert_eq!(decrypt(cipher, key), plain);
	}

	fn check(key: &Key, plain: &str, cipher: &str) {
		let plain = plain.parse::<Ipv4Addr>().unwrap();
		let cipher = cipher.parse::<Ipv4Addr>().unwrap();
		check_addr(key, plain, cipher);
	}

	static KEY: &Key = b"some 16-byte key";

	#[test]
	fn test_a() {
		check(KEY, "127.0.0.1", "114.62.227.59");
	}

	#[test]
	fn test_b() {
		check(KEY, "8.8.8.8", "46.48.51.50");
	}

	#[test]
	fn test_c() {
		check(KEY, "1.2.3.4", "171.238.15.199");
	}
}

#[cfg(test)]
mod test_raw {
	use {decrypt, encrypt, Key};

	fn check(key: &Key, plain: [u8; 4], cipher: [u8; 4]) {
		assert_eq!(encrypt(plain, key), cipher);

		assert_eq!(decrypt(cipher, key), plain);
	}

	static KEY: &Key = b"some 16-byte key";

	#[test]
	fn test_a() {
		check(KEY, [127, 0, 0, 1], [114, 62, 227, 59]);
	}

	#[test]
	fn test_b() {
		check(KEY, [8, 8, 8, 8], [46, 48, 51, 50]);
	}

	#[test]
	fn test_c() {
		check(KEY, [1, 2, 3, 4], [171, 238, 15, 199]);
	}
}