Skip to content

Commit d59145d

Browse files
committed
implement StreamCipher::new_var via BlockCipher::new_varkey
1 parent 5f7d49f commit d59145d

File tree

8 files changed

+54
-8
lines changed

8 files changed

+54
-8
lines changed

cfb-mode/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cfb-mode"
3-
version = "0.3.1"
3+
version = "0.3.2"
44
authors = ["RustCrypto Developers"]
55
license = "MIT OR Apache-2.0"
66
description = "Generic Cipher Feedback (CFB) mode implementation."

cfb-mode/src/lib.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
pub extern crate stream_cipher;
5252
extern crate block_cipher_trait;
5353

54-
use stream_cipher::{StreamCipher, NewStreamCipher};
54+
use stream_cipher::{StreamCipher, NewStreamCipher, InvalidKeyNonceLength};
5555
use block_cipher_trait::BlockCipher;
5656
use block_cipher_trait::generic_array::GenericArray;
5757
use block_cipher_trait::generic_array::typenum::Unsigned;
@@ -78,6 +78,17 @@ impl<C: BlockCipher> NewStreamCipher for Cfb<C> {
7878
cipher.encrypt_block(&mut iv);
7979
Self { cipher, iv, pos: 0 }
8080
}
81+
82+
fn new_var(key: &[u8], iv: &[u8] ) -> Result<Self, InvalidKeyNonceLength> {
83+
if Self::NonceSize::to_usize() != iv.len() {
84+
Err(InvalidKeyNonceLength)
85+
} else {
86+
let cipher = C::new_varkey(key).map_err(|_| InvalidKeyNonceLength)?;
87+
let mut iv = GenericArray::clone_from_slice(iv);
88+
cipher.encrypt_block(&mut iv);
89+
Ok(Self { cipher, iv, pos: 0 })
90+
}
91+
}
8192
}
8293

8394
impl<C: BlockCipher> StreamCipher for Cfb<C> {

cfb8/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cfb8"
3-
version = "0.3.1"
3+
version = "0.3.2"
44
authors = ["RustCrypto Developers"]
55
license = "MIT OR Apache-2.0"
66
description = "Generic 8-bit Cipher Feedback (CFB8) mode implementation."

cfb8/src/lib.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@
5151
extern crate block_cipher_trait;
5252
pub extern crate stream_cipher;
5353

54-
use stream_cipher::{NewStreamCipher, StreamCipher};
54+
use stream_cipher::{NewStreamCipher, StreamCipher, InvalidKeyNonceLength};
5555
use block_cipher_trait::BlockCipher;
5656
use block_cipher_trait::generic_array::GenericArray;
57+
use block_cipher_trait::generic_array::typenum::Unsigned;
5758

5859
/// CFB self-synchronizing stream cipher instance.
5960
pub struct Cfb8<C: BlockCipher> {
@@ -71,6 +72,16 @@ impl<C: BlockCipher> NewStreamCipher for Cfb8<C> {
7172
) -> Self {
7273
Self { cipher: C::new(key), iv: iv.clone() }
7374
}
75+
76+
fn new_var(key: &[u8], iv: &[u8] ) -> Result<Self, InvalidKeyNonceLength> {
77+
if Self::NonceSize::to_usize() != iv.len() {
78+
Err(InvalidKeyNonceLength)
79+
} else {
80+
let iv = GenericArray::clone_from_slice(iv);
81+
let cipher = C::new_varkey(key).map_err(|_| InvalidKeyNonceLength)?;
82+
Ok(Self { cipher, iv })
83+
}
84+
}
7485
}
7586

7687
impl<C: BlockCipher> StreamCipher for Cfb8<C> {

ctr/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ctr"
3-
version = "0.3.1"
3+
version = "0.3.2"
44
authors = ["RustCrypto Developers"]
55
license = "MIT OR Apache-2.0"
66
description = "CTR block mode of operation"

ctr/src/lib.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ pub extern crate stream_cipher;
4242
extern crate block_cipher_trait;
4343

4444
use stream_cipher::{
45-
SyncStreamCipher, SyncStreamCipherSeek, NewStreamCipher, LoopError
45+
SyncStreamCipher, SyncStreamCipherSeek, NewStreamCipher,
46+
LoopError, InvalidKeyNonceLength
4647
};
4748

4849
use block_cipher_trait::generic_array::{ArrayLength, GenericArray};
@@ -128,6 +129,16 @@ impl<C> NewStreamCipher for Ctr128<C>
128129
let cipher = C::new(key);
129130
Self::from_cipher(cipher, nonce)
130131
}
132+
133+
fn new_var(key: &[u8], nonce: &[u8] ) -> Result<Self, InvalidKeyNonceLength> {
134+
let nonce = if Self::NonceSize::to_usize() != nonce.len() {
135+
Err(InvalidKeyNonceLength)?
136+
} else {
137+
GenericArray::from_slice(nonce)
138+
};
139+
let cipher = C::new_varkey(key).map_err(|_| InvalidKeyNonceLength)?;
140+
Ok(Self::from_cipher(cipher, nonce))
141+
}
131142
}
132143

133144
impl<C> SyncStreamCipher for Ctr128<C>

ofb/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ofb"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
authors = ["RustCrypto Developers"]
55
license = "MIT OR Apache-2.0"
66
description = "Generic Output Feedback (OFB) mode implementation."

ofb/src/lib.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@
5353
pub extern crate stream_cipher;
5454
extern crate block_cipher_trait;
5555

56-
use stream_cipher::{SyncStreamCipher, NewStreamCipher, LoopError};
56+
use stream_cipher::{
57+
SyncStreamCipher, NewStreamCipher, LoopError, InvalidKeyNonceLength,
58+
};
5759
use block_cipher_trait::BlockCipher;
5860
use block_cipher_trait::generic_array::GenericArray;
5961
use block_cipher_trait::generic_array::typenum::Unsigned;
@@ -77,6 +79,17 @@ impl<C: BlockCipher> NewStreamCipher for Ofb<C> {
7779
cipher.encrypt_block(&mut block);
7880
Self { cipher, block, pos: 0 }
7981
}
82+
83+
fn new_var(key: &[u8], iv: &[u8] ) -> Result<Self, InvalidKeyNonceLength> {
84+
if Self::NonceSize::to_usize() != iv.len() {
85+
Err(InvalidKeyNonceLength)
86+
} else {
87+
let cipher = C::new_varkey(key).map_err(|_| InvalidKeyNonceLength)?;
88+
let mut block = GenericArray::clone_from_slice(iv);
89+
cipher.encrypt_block(&mut block);
90+
Ok(Self { cipher, block, pos: 0 })
91+
}
92+
}
8093
}
8194

8295
impl<C: BlockCipher> SyncStreamCipher for Ofb<C> {

0 commit comments

Comments
 (0)