Skip to content

Commit

Permalink
Migrate to cipher v0.5.0-pre.7
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Aug 12, 2024
1 parent 5e061ba commit 6ec59f6
Show file tree
Hide file tree
Showing 34 changed files with 1,144 additions and 1,168 deletions.
23 changes: 9 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 12 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
[workspace]
resolver = "2"
members = [
"belt-ctr",
"cbc",
"cfb8",
"cfb-mode",
"ctr",
"ige",
"ofb",
"pcbc",
]
members = ["belt-ctr", "cbc", "cfb8", "cfb-mode", "ctr", "ige", "ofb", "pcbc"]

[profile.dev]
opt-level = 2

[patch.crates-io]
cipher = { git = "https://github.com/RustCrypto/traits", branch = "block_backends" }

aes = { git = "https://github.com/RustCrypto/block-ciphers", branch = "cipher_new" }
belt-block = { git = "https://github.com/RustCrypto/block-ciphers", branch = "cipher_new" }
kuznyechik = { git = "https://github.com/RustCrypto/block-ciphers", branch = "cipher_new" }
magma = { git = "https://github.com/RustCrypto/block-ciphers", branch = "cipher_new" }
4 changes: 2 additions & 2 deletions belt-ctr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ keywords = ["crypto", "block-mode", "stream-cipher", "ciphers", "belt"]
categories = ["cryptography", "no-std"]

[dependencies]
cipher = "=0.5.0-pre.6"
cipher = "=0.5.0-pre.7"
belt-block = "=0.2.0-pre.1"

[dev-dependencies]
hex-literal = "0.4"
belt-block = "=0.2.0-pre.1"
cipher = { version = "=0.5.0-pre.6", features = ["dev"] }
cipher = { version = "=0.5.0-pre.7", features = ["dev"] }

[features]
alloc = ["cipher/alloc"]
Expand Down
76 changes: 0 additions & 76 deletions belt-ctr/src/backend.rs

This file was deleted.

63 changes: 57 additions & 6 deletions belt-ctr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ pub use cipher;

use belt_block::BeltBlock;
use cipher::{
array::Array, consts::U16, crypto_common::InnerUser, AlgorithmName, BlockCipherDecrypt,
BlockCipherEncrypt, BlockSizeUser, InnerIvInit, Iv, IvSizeUser, IvState, StreamCipherCore,
StreamCipherCoreWrapper, StreamCipherSeekCore, StreamClosure,
array::Array, consts::U16, crypto_common::InnerUser, AlgorithmName, Block, BlockCipherDecrypt,
BlockCipherEncBackend, BlockCipherEncClosure, BlockCipherEncrypt, BlockSizeUser, InOut,
InnerIvInit, Iv, IvSizeUser, IvState, ParBlocks, ParBlocksSizeUser, StreamBackend,
StreamCipherCore, StreamCipherCoreWrapper, StreamCipherSeekCore, StreamClosure,
};
use core::fmt;

mod backend;

/// Byte-level BelT CTR
pub type BeltCtr<C = BeltBlock> = StreamCipherCoreWrapper<BeltCtrCore<C>>;

Expand All @@ -43,8 +42,25 @@ where
}

fn process_with_backend(&mut self, f: impl StreamClosure<BlockSize = Self::BlockSize>) {
struct Closure<'a, C: StreamClosure<BlockSize = U16>> {
s: &'a mut u128,
f: C,
}

impl<'a, C: StreamClosure<BlockSize = U16>> BlockSizeUser for Closure<'a, C> {
type BlockSize = U16;
}

impl<'a, C: StreamClosure<BlockSize = U16>> BlockCipherEncClosure for Closure<'a, C> {
#[inline(always)]
fn call<B: BlockCipherEncBackend<BlockSize = U16>>(self, cipher_backend: &B) {
let Self { s, f } = self;
f.call(&mut Backend { s, cipher_backend })
}
}

let Self { cipher, s, .. } = self;
cipher.encrypt_with_backend(backend::Closure { s, f });
cipher.encrypt_with_backend(Closure { s, f });
}
}

Expand Down Expand Up @@ -129,3 +145,38 @@ impl<C: BlockCipherEncrypt + BlockSizeUser<BlockSize = U16>> fmt::Debug for Belt
f.write_str("BeltCtrCore { ... }")
}
}

struct Backend<'a, B: BlockCipherEncBackend<BlockSize = U16>> {
s: &'a mut u128,
cipher_backend: &'a B,
}

impl<'a, B: BlockCipherEncBackend<BlockSize = U16>> BlockSizeUser for Backend<'a, B> {
type BlockSize = B::BlockSize;
}

impl<'a, B: BlockCipherEncBackend<BlockSize = U16>> ParBlocksSizeUser for Backend<'a, B> {
type ParBlocksSize = B::ParBlocksSize;
}

impl<'a, B: BlockCipherEncBackend<BlockSize = U16>> StreamBackend for Backend<'a, B> {
#[inline(always)]
fn gen_ks_block(&mut self, block: &mut Block<Self>) {
*self.s = self.s.wrapping_add(1);
let tmp = self.s.to_le_bytes().into();
self.cipher_backend.encrypt_block((&tmp, block).into());
}

#[inline(always)]
fn gen_par_ks_blocks(&mut self, blocks: &mut ParBlocks<Self>) {
let mut tmp = ParBlocks::<Self>::default();
let mut s = *self.s;
for block in tmp.iter_mut() {
s = s.wrapping_add(1);
*block = s.to_le_bytes().into();
}
*self.s = s;
let io_blocks = InOut::from((&tmp, blocks));
self.cipher_backend.encrypt_par_blocks(io_blocks);
}
}
4 changes: 2 additions & 2 deletions cbc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ keywords = ["crypto", "block-mode", "ciphers"]
categories = ["cryptography", "no-std"]

[dependencies]
cipher = "=0.5.0-pre.6"
cipher = "=0.5.0-pre.7"

[dev-dependencies]
aes = "=0.9.0-pre.1"
cipher = { version = "=0.5.0-pre.6", features = ["dev"] }
cipher = { version = "=0.5.0-pre.7", features = ["dev"] }
hex-literal = "0.4"

[features]
Expand Down
Loading

0 comments on commit 6ec59f6

Please sign in to comment.