From 9150027535d37ab196d52ceb9d0afd810109acfa Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Sun, 28 Oct 2018 16:22:17 +1300 Subject: [PATCH 1/8] Upgrade to aes 0.3 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index b729965..0dcdfb4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ homepage = "https://github.com/str4d/fpe" repository = "https://github.com/str4d/fpe" [dependencies] -aes = "0.2" +aes = "0.3" byteorder = "1" num-bigint = "0.2" num-integer = "0.1" From ee25d2294d64ea6067d791c3f2aa89b9d0eec6c2 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Sun, 28 Oct 2018 16:24:11 +1300 Subject: [PATCH 2/8] cargo fmt --- src/ff1.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ff1.rs b/src/ff1.rs index 89c9b18..b4967ae 100644 --- a/src/ff1.rs +++ b/src/ff1.rs @@ -6,7 +6,8 @@ use byteorder::{BigEndian, WriteBytesExt}; use num_bigint::{BigInt, BigUint, Sign}; use num_integer::Integer; use num_traits::{ - identities::{One, Zero}, ToPrimitive, + identities::{One, Zero}, + ToPrimitive, }; #[derive(Debug, PartialEq)] @@ -479,7 +480,7 @@ impl FF1 { mod tests { use aes::{Aes128, Aes192, Aes256}; - use super::{BinaryNumeralString, FF1, FlexibleNumeralString, NumeralString, Radix}; + use super::{BinaryNumeralString, FlexibleNumeralString, NumeralString, Radix, FF1}; #[test] fn ns_is_valid() { From 757a5e1eb16f10f69e7520fb1de97ccf846262de Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Sun, 28 Oct 2018 16:30:31 +1300 Subject: [PATCH 3/8] Address clippy lints --- src/ff1.rs | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/src/ff1.rs b/src/ff1.rs index b4967ae..3b141e6 100644 --- a/src/ff1.rs +++ b/src/ff1.rs @@ -50,12 +50,9 @@ impl Radix { /// Calculates b = ceil(ceil(v * log2(radix)) / 8). fn calculate_b(&self, v: usize) -> usize { - match self { - &Radix::Any(r) => (v as f64 * (r as f64).log2() / 8f64).ceil() as usize, - &Radix::PowerTwo { - radix: _, - log_radix, - } => ((v * log_radix as usize) + 7) / 8, + match *self { + Radix::Any(r) => (v as f64 * f64::from(r).log2() / 8f64).ceil() as usize, + Radix::PowerTwo { log_radix, .. } => ((v * log_radix as usize) + 7) / 8, } } @@ -64,12 +61,9 @@ impl Radix { } fn to_u32(&self) -> u32 { - match self { - &Radix::Any(r) => r, - &Radix::PowerTwo { - radix, - log_radix: _, - } => radix, + match *self { + Radix::Any(r) => r, + Radix::PowerTwo { radix, .. } => radix, } } } @@ -116,9 +110,7 @@ impl From for Vec { impl NumeralString for FlexibleNumeralString { fn is_valid(&self, radix: u32) -> bool { - self.0 - .iter() - .fold(true, |acc, n| acc && ((*n as u32) < radix)) + self.0.iter().all(|n| (u32::from(*n) < radix)) } fn len(&self) -> usize { @@ -149,7 +141,7 @@ impl NumeralString for FlexibleNumeralString { let mut res = vec![0; m]; for i in 0..m { res[m - 1 - i] = (&x % radix).to_u16().unwrap(); - x = x / radix; + x /= radix; } FlexibleNumeralString(res) } @@ -196,9 +188,7 @@ impl BinaryNumeralString { impl NumeralString for BinaryNumeralString { fn is_valid(&self, radix: u32) -> bool { - self.0 - .iter() - .fold(true, |acc, n| acc && ((*n as u32) < radix)) + self.0.iter().all(|n| (u32::from(*n) < radix)) } fn len(&self) -> usize { From 4022b74a96bd4a301531cec6f3172be7c86f11c2 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Sun, 28 Oct 2018 17:20:40 +1300 Subject: [PATCH 4/8] Rewrite generate_s() to not use BigUint Closes #3. --- src/ff1.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/ff1.rs b/src/ff1.rs index 3b141e6..07e36ab 100644 --- a/src/ff1.rs +++ b/src/ff1.rs @@ -250,18 +250,25 @@ fn generate_s(ciph: &CIPH, r: &[u8], d: usize) -> Vec { let mut s = Vec::from(r); s.reserve(d); { - let mut j = BigUint::one(); + let mut j = [0u8; 16]; while s.len() < d { - let tmp = j.to_bytes_be(); - assert!(tmp.len() <= 16); + // Increment j + for k in 0..16 { + let offset = 16 - k - 1; + j[offset] = j[offset].wrapping_add(1); + if j[offset] != 0 { + assert!(k < 15); + break; + } + } + let mut block = [0; 16]; - block[16 - tmp.len()..].copy_from_slice(&tmp); + block.copy_from_slice(&j); for k in 0..16 { block[k] ^= r[k]; } ciph.encrypt_block(&mut GenericArray::from_mut_slice(&mut block)); s.extend_from_slice(&block[..]); - j += BigUint::one(); } } s.truncate(d); From 761651fe2a590b2d19c4b34eafeabf3d61cf920a Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Fri, 8 Mar 2019 00:15:23 +0000 Subject: [PATCH 5/8] Rust 2018 edition --- Cargo.toml | 2 +- src/lib.rs | 6 ------ 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0dcdfb4..58876dc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "fpe" version = "0.1.0" authors = ["Jack Grigg "] license = "MIT/Apache-2.0" - +edition = "2018" description = "Format-preserving encryption" documentation = "https://docs.rs/fpe/" homepage = "https://github.com/str4d/fpe" diff --git a/src/lib.rs b/src/lib.rs index 09ccd3b..24f0569 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,10 +23,4 @@ #![deny(missing_docs)] // refuse to compile if documentation is missing -extern crate aes; -extern crate byteorder; -extern crate num_bigint; -extern crate num_integer; -extern crate num_traits; - pub mod ff1; From 76922eae286cea1c718a510bf27c1521d3037bf2 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Fri, 8 Mar 2019 00:16:57 +0000 Subject: [PATCH 6/8] Update email --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 58876dc..c01946b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "fpe" version = "0.1.0" -authors = ["Jack Grigg "] +authors = ["Jack Grigg "] license = "MIT/Apache-2.0" edition = "2018" description = "Format-preserving encryption" From a0bd29a4a897a86f1fa1bd6fa592a3715e97dfb1 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Fri, 8 Mar 2019 00:17:10 +0000 Subject: [PATCH 7/8] Bump version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index c01946b..7b496cc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "fpe" -version = "0.1.0" +version = "0.2.0" authors = ["Jack Grigg "] license = "MIT/Apache-2.0" edition = "2018" From 90cde19583f3dd9aee215c965c3ae01f63169d78 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Mon, 22 Jul 2019 22:44:02 +0100 Subject: [PATCH 8/8] Rewrite generate_s() to use u128 Requires a minimum Rust version of 1.32 for u128::to_be_bytes() Co-authored-by: Eirik Ogilvie-Wigley --- .travis.yml | 1 + README.md | 2 ++ src/ff1.rs | 16 +++------------- 3 files changed, 6 insertions(+), 13 deletions(-) diff --git a/.travis.yml b/.travis.yml index 343b801..b024d89 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,7 @@ language: rust rust: + - 1.32.0 - stable - beta - nightly diff --git a/README.md b/README.md index 3b09208..5f2d6b9 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,8 @@ algorithms. The following algorithms are implemented: - FF1 (specified in [NIST Special Publication 800-38G](http://dx.doi.org/10.6028/NIST.SP.800-38G)). +This crate requires Rust version 1.32 or greater. + ## License Licensed under either of diff --git a/src/ff1.rs b/src/ff1.rs index 07e36ab..bc7b294 100644 --- a/src/ff1.rs +++ b/src/ff1.rs @@ -250,20 +250,10 @@ fn generate_s(ciph: &CIPH, r: &[u8], d: usize) -> Vec { let mut s = Vec::from(r); s.reserve(d); { - let mut j = [0u8; 16]; + let mut j = 0u128; while s.len() < d { - // Increment j - for k in 0..16 { - let offset = 16 - k - 1; - j[offset] = j[offset].wrapping_add(1); - if j[offset] != 0 { - assert!(k < 15); - break; - } - } - - let mut block = [0; 16]; - block.copy_from_slice(&j); + j += 1; + let mut block = j.to_be_bytes(); for k in 0..16 { block[k] ^= r[k]; }