Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ name = "fpe"
version = "0.1.0"
authors = ["Jack Grigg <str4d@i2pmail.org>"]
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
50 changes: 24 additions & 26 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,25 @@ 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 = [0u8; 16];
Comment thread
str4d marked this conversation as resolved.
Outdated
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);
Expand Down Expand Up @@ -479,7 +477,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;