Skip to content

Commit 6ec59f6

Browse files
committed
Migrate to cipher v0.5.0-pre.7
1 parent 5e061ba commit 6ec59f6

34 files changed

+1144
-1168
lines changed

Cargo.lock

Lines changed: 9 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
[workspace]
22
resolver = "2"
3-
members = [
4-
"belt-ctr",
5-
"cbc",
6-
"cfb8",
7-
"cfb-mode",
8-
"ctr",
9-
"ige",
10-
"ofb",
11-
"pcbc",
12-
]
3+
members = ["belt-ctr", "cbc", "cfb8", "cfb-mode", "ctr", "ige", "ofb", "pcbc"]
4+
5+
[profile.dev]
6+
opt-level = 2
7+
8+
[patch.crates-io]
9+
cipher = { git = "https://github.com/RustCrypto/traits", branch = "block_backends" }
10+
11+
aes = { git = "https://github.com/RustCrypto/block-ciphers", branch = "cipher_new" }
12+
belt-block = { git = "https://github.com/RustCrypto/block-ciphers", branch = "cipher_new" }
13+
kuznyechik = { git = "https://github.com/RustCrypto/block-ciphers", branch = "cipher_new" }
14+
magma = { git = "https://github.com/RustCrypto/block-ciphers", branch = "cipher_new" }

belt-ctr/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ keywords = ["crypto", "block-mode", "stream-cipher", "ciphers", "belt"]
1313
categories = ["cryptography", "no-std"]
1414

1515
[dependencies]
16-
cipher = "=0.5.0-pre.6"
16+
cipher = "=0.5.0-pre.7"
1717
belt-block = "=0.2.0-pre.1"
1818

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

2424
[features]
2525
alloc = ["cipher/alloc"]

belt-ctr/src/backend.rs

Lines changed: 0 additions & 76 deletions
This file was deleted.

belt-ctr/src/lib.rs

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@ pub use cipher;
1212

1313
use belt_block::BeltBlock;
1414
use cipher::{
15-
array::Array, consts::U16, crypto_common::InnerUser, AlgorithmName, BlockCipherDecrypt,
16-
BlockCipherEncrypt, BlockSizeUser, InnerIvInit, Iv, IvSizeUser, IvState, StreamCipherCore,
17-
StreamCipherCoreWrapper, StreamCipherSeekCore, StreamClosure,
15+
array::Array, consts::U16, crypto_common::InnerUser, AlgorithmName, Block, BlockCipherDecrypt,
16+
BlockCipherEncBackend, BlockCipherEncClosure, BlockCipherEncrypt, BlockSizeUser, InOut,
17+
InnerIvInit, Iv, IvSizeUser, IvState, ParBlocks, ParBlocksSizeUser, StreamBackend,
18+
StreamCipherCore, StreamCipherCoreWrapper, StreamCipherSeekCore, StreamClosure,
1819
};
1920
use core::fmt;
2021

21-
mod backend;
22-
2322
/// Byte-level BelT CTR
2423
pub type BeltCtr<C = BeltBlock> = StreamCipherCoreWrapper<BeltCtrCore<C>>;
2524

@@ -43,8 +42,25 @@ where
4342
}
4443

4544
fn process_with_backend(&mut self, f: impl StreamClosure<BlockSize = Self::BlockSize>) {
45+
struct Closure<'a, C: StreamClosure<BlockSize = U16>> {
46+
s: &'a mut u128,
47+
f: C,
48+
}
49+
50+
impl<'a, C: StreamClosure<BlockSize = U16>> BlockSizeUser for Closure<'a, C> {
51+
type BlockSize = U16;
52+
}
53+
54+
impl<'a, C: StreamClosure<BlockSize = U16>> BlockCipherEncClosure for Closure<'a, C> {
55+
#[inline(always)]
56+
fn call<B: BlockCipherEncBackend<BlockSize = U16>>(self, cipher_backend: &B) {
57+
let Self { s, f } = self;
58+
f.call(&mut Backend { s, cipher_backend })
59+
}
60+
}
61+
4662
let Self { cipher, s, .. } = self;
47-
cipher.encrypt_with_backend(backend::Closure { s, f });
63+
cipher.encrypt_with_backend(Closure { s, f });
4864
}
4965
}
5066

@@ -129,3 +145,38 @@ impl<C: BlockCipherEncrypt + BlockSizeUser<BlockSize = U16>> fmt::Debug for Belt
129145
f.write_str("BeltCtrCore { ... }")
130146
}
131147
}
148+
149+
struct Backend<'a, B: BlockCipherEncBackend<BlockSize = U16>> {
150+
s: &'a mut u128,
151+
cipher_backend: &'a B,
152+
}
153+
154+
impl<'a, B: BlockCipherEncBackend<BlockSize = U16>> BlockSizeUser for Backend<'a, B> {
155+
type BlockSize = B::BlockSize;
156+
}
157+
158+
impl<'a, B: BlockCipherEncBackend<BlockSize = U16>> ParBlocksSizeUser for Backend<'a, B> {
159+
type ParBlocksSize = B::ParBlocksSize;
160+
}
161+
162+
impl<'a, B: BlockCipherEncBackend<BlockSize = U16>> StreamBackend for Backend<'a, B> {
163+
#[inline(always)]
164+
fn gen_ks_block(&mut self, block: &mut Block<Self>) {
165+
*self.s = self.s.wrapping_add(1);
166+
let tmp = self.s.to_le_bytes().into();
167+
self.cipher_backend.encrypt_block((&tmp, block).into());
168+
}
169+
170+
#[inline(always)]
171+
fn gen_par_ks_blocks(&mut self, blocks: &mut ParBlocks<Self>) {
172+
let mut tmp = ParBlocks::<Self>::default();
173+
let mut s = *self.s;
174+
for block in tmp.iter_mut() {
175+
s = s.wrapping_add(1);
176+
*block = s.to_le_bytes().into();
177+
}
178+
*self.s = s;
179+
let io_blocks = InOut::from((&tmp, blocks));
180+
self.cipher_backend.encrypt_par_blocks(io_blocks);
181+
}
182+
}

cbc/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ keywords = ["crypto", "block-mode", "ciphers"]
1313
categories = ["cryptography", "no-std"]
1414

1515
[dependencies]
16-
cipher = "=0.5.0-pre.6"
16+
cipher = "=0.5.0-pre.7"
1717

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

2323
[features]

0 commit comments

Comments
 (0)