Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: rust

rust:
- 1.32.0
- stable
- beta
- nightly
Expand Down
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
[package]
name = "fpe"
version = "0.1.0"
authors = ["Jack Grigg <str4d@i2pmail.org>"]
version = "0.2.0"
authors = ["Jack Grigg <thestr4d@gmail.com>"]
license = "MIT/Apache-2.0"

edition = "2018"
Comment thread
str4d marked this conversation as resolved.
description = "Format-preserving encryption"
documentation = "https://docs.rs/fpe/"
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"
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 15 additions & 27 deletions src/ff1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -49,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,
}
}

Expand All @@ -63,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,
}
}
}
Expand Down Expand Up @@ -115,9 +110,7 @@ impl From<FlexibleNumeralString> for Vec<u16> {

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 {
Expand Down Expand Up @@ -148,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)
}
Expand Down Expand Up @@ -195,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 {
Expand Down Expand Up @@ -259,18 +250,15 @@ fn generate_s<CIPH: BlockCipher>(ciph: &CIPH, r: &[u8], d: usize) -> Vec<u8> {
let mut s = Vec::from(r);
s.reserve(d);
{
let mut j = BigUint::one();
let mut j = 0u128;
while s.len() < d {
let tmp = j.to_bytes_be();
assert!(tmp.len() <= 16);
let mut block = [0; 16];
block[16 - tmp.len()..].copy_from_slice(&tmp);
j += 1;
let mut block = j.to_be_bytes();
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);
Expand Down Expand Up @@ -479,7 +467,7 @@ impl<CIPH: BlockCipher> FF1<CIPH> {
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() {
Expand Down
6 changes: 0 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;