Skip to content
Open
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
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
matrix:
target:
- wasm32-wasi
- wasm32-wasip1

steps:
- uses: actions/checkout@v3
Expand All @@ -27,7 +27,7 @@ jobs:

steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@1.60.0
- uses: dtolnay/rust-toolchain@1.85.0
id: toolchain
- run: rustup override set ${{steps.toolchain.outputs.name}}
- name: Run tests
Expand All @@ -38,7 +38,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@1.60.0
- uses: dtolnay/rust-toolchain@1.85.0
id: toolchain
- run: rustup override set ${{steps.toolchain.outputs.name}}
# Build benchmarks to prevent bitrot
Expand All @@ -51,7 +51,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@1.60.0
- uses: dtolnay/rust-toolchain@1.85.0
id: toolchain
- run: rustup override set ${{steps.toolchain.outputs.name}}
- run: rustup component add clippy
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this library adheres to Rust's notion of
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- MSRV is now 1.85.0.
- Bumped dependencies to `cipher 0.5.1`, `cbc 0.2`.
- `aes 0.9` is now the minimum compatible crate version.

## [0.6.1] - 2023-04-13
### Fixed
Expand Down
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.6.1"
authors = ["Jack Grigg <thestr4d@gmail.com>"]
license = "MIT/Apache-2.0"
edition = "2021"
rust-version = "1.56"
rust-version = "1.85"
description = "Format-preserving encryption"
documentation = "https://docs.rs/fpe/"
homepage = "https://github.com/str4d/fpe"
Expand All @@ -13,16 +13,16 @@ keywords = ["ff1"]
categories = ["cryptography", "no-std"]

[dependencies]
cbc = { version = "0.1", default-features = false }
cipher = "0.4"
cbc = { version = "0.2", default-features = false }
cipher = "0.5.1"
libm = "0.2"

num-bigint = { version = "0.4", optional = true, default-features = false }
num-integer = { version = "0.1", optional = true, default-features = false }
num-traits = { version = "0.2", optional = true, default-features = false }

[dev-dependencies]
aes = "0.8"
aes = "0.9"

# Tests
proptest = "1.1"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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.56 or greater.
This crate requires Rust version 1.85 or greater.

## License

Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "1.56.0"
channel = "1.85.0"
components = ["clippy", "rustfmt"]
42 changes: 26 additions & 16 deletions src/ff1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
use core::cmp;

use cipher::{
generic_array::GenericArray, Block, BlockCipher, BlockEncrypt, BlockEncryptMut, InnerIvInit,
KeyInit,
common::IvState, Array, Block, BlockCipherEncrypt, BlockModeEncrypt, InnerIvInit, KeyInit,
};

#[cfg(test)]
Expand Down Expand Up @@ -64,7 +63,7 @@ impl Radix {
let log_radix = 31 - radix.leading_zeros();
Radix::PowerTwo {
radix,
min_len: cmp::max((MIN_RADIX_2_NS_LEN + log_radix - 1) / log_radix, MIN_NS_LEN),
min_len: cmp::max(MIN_RADIX_2_NS_LEN.div_ceil(log_radix), MIN_NS_LEN),
log_radix: u8::try_from(log_radix).unwrap(),
}
} else {
Expand Down Expand Up @@ -156,20 +155,29 @@ pub trait NumeralString: Sized {
fn concat(a: Self::Ops, b: Self::Ops) -> Self;
}

#[derive(Clone)]
struct Prf<CIPH: BlockCipher + BlockEncrypt> {
struct Prf<CIPH: BlockCipherEncrypt> {
state: cbc::Encryptor<CIPH>,
// Contains the output when offset = 0, and partial input otherwise
buf: [Block<CIPH>; 1],
offset: usize,
}

impl<CIPH: BlockCipher + BlockEncrypt + Clone> Prf<CIPH> {
impl<CIPH: BlockCipherEncrypt + Clone> Prf<CIPH> {
fn fork(&self, ciph: &CIPH) -> Self {
Self {
state: cbc::Encryptor::inner_iv_init(ciph.clone(), &self.state.iv_state()),
buf: self.buf.clone(),
offset: self.offset,
}
}
}

impl<CIPH: BlockCipherEncrypt + Clone> Prf<CIPH> {
fn new(ciph: &CIPH) -> Self {
let ciph = ciph.clone();
Prf {
state: cbc::Encryptor::inner_iv_init(ciph, GenericArray::from_slice(&[0; 16])),
buf: [Block::<CIPH>::default()],
state: cbc::Encryptor::inner_iv_init(ciph, &Array::from_fn(|_| 0)),
buf: [Block::<CIPH>::from_fn(|_| 0)],
offset: 0,
}
}
Expand All @@ -182,7 +190,7 @@ impl<CIPH: BlockCipher + BlockEncrypt + Clone> Prf<CIPH> {
data = &data[to_read..];

if self.offset == self.buf[0].len() {
self.state.encrypt_blocks_mut(&mut self.buf);
self.state.encrypt_blocks(&mut self.buf);
self.offset = 0;
}
}
Expand All @@ -197,7 +205,7 @@ impl<CIPH: BlockCipher + BlockEncrypt + Clone> Prf<CIPH> {
}
}

fn generate_s<'a, CIPH: BlockEncrypt>(
fn generate_s<'a, CIPH: BlockCipherEncrypt>(
ciph: &'a CIPH,
r: &'a Block<CIPH>,
d: usize,
Expand All @@ -216,23 +224,25 @@ fn generate_s<'a, CIPH: BlockEncrypt>(
}

/// A struct for performing FF1 encryption and decryption operations.
pub struct FF1<CIPH: BlockCipher> {
pub struct FF1<CIPH> {
ciph: CIPH,
radix: Radix,
}

impl<CIPH: BlockCipher + KeyInit> FF1<CIPH> {
impl<CIPH: KeyInit> FF1<CIPH> {
/// Creates a new FF1 object for the given key and radix.
///
/// Returns an error if the given radix is not in [2..2^16].
///
/// Panics if the key size does not match that expected by the cipher.
pub fn new(key: &[u8], radix: u32) -> Result<Self, InvalidRadix> {
let ciph = CIPH::new(GenericArray::from_slice(key));
let ciph = CIPH::new(key.try_into().expect("Invalid key length"));
let radix = Radix::from_u32(radix)?;
Ok(FF1 { ciph, radix })
}
}

impl<CIPH: BlockCipher + BlockEncrypt + Clone> FF1<CIPH> {
impl<CIPH: BlockCipherEncrypt + Clone> FF1<CIPH> {
/// Encrypts the given numeral string.
///
/// Returns an error if the numeral string is not in the required radix.
Expand Down Expand Up @@ -277,7 +287,7 @@ impl<CIPH: BlockCipher + BlockEncrypt + Clone> FF1<CIPH> {
prf.update(&[0]);
}
for i in 0..10 {
let mut prf = prf.clone();
let mut prf = prf.fork(&self.ciph);
prf.update(&[i]);
prf.update(x_b.to_be_bytes(self.radix.to_u32(), b).as_ref());
let r = prf.output();
Expand Down Expand Up @@ -348,7 +358,7 @@ impl<CIPH: BlockCipher + BlockEncrypt + Clone> FF1<CIPH> {
}
for i in 0..10 {
let i = 9 - i;
let mut prf = prf.clone();
let mut prf = prf.fork(&self.ciph);
prf.update(&[i]);
prf.update(x_a.to_be_bytes(self.radix.to_u32(), b).as_ref());
let r = prf.output();
Expand Down
6 changes: 3 additions & 3 deletions src/ff1/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl NumeralString for FlexibleNumeralString {
type Ops = Self;

fn is_valid(&self, radix: u32) -> bool {
self.0.iter().all(|n| (u32::from(*n) < radix))
self.0.iter().all(|n| u32::from(*n) < radix)
}

fn numeral_count(&self) -> usize {
Expand Down Expand Up @@ -171,7 +171,7 @@ impl BinaryNumeralString {
BinaryNumeralString(s.to_vec())
}

/// Returns a Vec<u8>, with each byte written from the BinaryNumeralString
/// Returns a Vec, with each byte written from the BinaryNumeralString
/// in little-endian bit order.
pub fn to_bytes_le(&self) -> Vec<u8> {
self.0.to_vec()
Expand Down Expand Up @@ -297,7 +297,7 @@ impl NumeralString for BinaryNumeralString {
// Simple case: no shifting necessary, just reversing and joining.
b.data
.into_iter()
.chain(a.data.into_iter())
.chain(a.data)
.map(|b| b.reverse_bits())
.rev()
.collect()
Expand Down
2 changes: 1 addition & 1 deletion src/ff1/proptests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ proptest! {
key in prop::array::uniform32(prop::num::u8::ANY),
(radix, _, _) in valid_radix(),
) {
assert!(matches!(FF1::<Aes256>::new(&key, radix), Ok(_)));
assert!(FF1::<Aes256>::new(&key, radix).is_ok());
}

#[test]
Expand Down
Loading