Skip to content

Commit 8d39a09

Browse files
authored
Add NonceSize to FromBlockCipher (#209)
1 parent 79c0843 commit 8d39a09

File tree

6 files changed

+39
-11
lines changed

6 files changed

+39
-11
lines changed

Cargo.lock

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

cryptography/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## 0.2.0 (2020-07-03)
9+
### Changed
10+
- Bump `stream-cipher` to v0.5 ([#209])
11+
12+
[#209]: https://github.com/RustCrypto/traits/pull/209
13+
814
## 0.1.4 (2020-06-21)
915
### Added
1016
- rustdoc improvements ([#205])

cryptography/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cryptography"
3-
version = "0.1.4"
3+
version = "0.2.0"
44
authors = ["The RustCrypto Project Developers"]
55
license = "Apache-2.0 OR MIT"
66
description = "Facade crate for the RustCrypto project's traits"
@@ -17,7 +17,7 @@ block-cipher = { version = "0.7", optional = true, path = "../block-cipher" }
1717
digest = { version = "0.9", optional = true, path = "../digest" }
1818
mac = { version = "0.8", package = "crypto-mac", optional = true, path = "../crypto-mac" }
1919
signature = { version = "1.1.0", optional = true, default-features = false, path = "../signature" }
20-
stream-cipher = { version = "0.4", optional = true, path = "../stream-cipher" }
20+
stream-cipher = { version = "0.5", optional = true, path = "../stream-cipher" }
2121
universal-hash = { version = "0.4", optional = true, path = "../universal-hash" }
2222

2323
[package.metadata.docs.rs]

stream-cipher/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## 0.5.0 (2020-07-03)
9+
### Changed
10+
- Add `NonceSize` associated type to the `FromBlockCipher` trait ([#209])
11+
12+
[#209]: https://github.com/RustCrypto/traits/pull/209
13+
814
## 0.4.1 (2020-06-10)
915
### Added
1016
- `Key` and `Nonce` type aliases ([#188])

stream-cipher/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "stream-cipher"
33
description = "Stream cipher traits"
4-
version = "0.4.1"
4+
version = "0.5.0"
55
authors = ["RustCrypto Developers"]
66
license = "MIT OR Apache-2.0"
77
readme = "README.md"
@@ -21,6 +21,7 @@ optional = true
2121
path = "../block-cipher"
2222

2323
[features]
24+
default = ["block-cipher"]
2425
std = []
2526
dev = ["blobby"]
2627

stream-cipher/src/lib.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,14 @@ impl<C: SyncStreamCipher> StreamCipher for C {
131131
pub trait FromBlockCipher {
132132
/// Block cipher
133133
type BlockCipher: BlockCipher + NewBlockCipher;
134+
/// Nonce size in bytes
135+
type NonceSize: ArrayLength<u8>;
134136

135137
/// Instantiate a stream cipher from a block cipher
136138
// TODO(tarcieri): add associated type for NonceSize?
137139
fn from_block_cipher(
138140
cipher: Self::BlockCipher,
139-
nonce: &block_cipher::Block<Self::BlockCipher>,
141+
nonce: &GenericArray<u8, Self::NonceSize>,
140142
) -> Self;
141143
}
142144

@@ -146,12 +148,25 @@ where
146148
C: FromBlockCipher,
147149
{
148150
type KeySize = <<Self as FromBlockCipher>::BlockCipher as NewBlockCipher>::KeySize;
149-
type NonceSize = <<Self as FromBlockCipher>::BlockCipher as BlockCipher>::BlockSize;
151+
type NonceSize = <Self as FromBlockCipher>::NonceSize;
150152

151153
fn new(key: &Key<Self>, nonce: &Nonce<Self>) -> C {
152154
C::from_block_cipher(
153155
<<Self as FromBlockCipher>::BlockCipher as NewBlockCipher>::new(key),
154156
nonce,
155157
)
156158
}
159+
160+
fn new_var(key: &[u8], nonce: &[u8]) -> Result<Self, InvalidKeyNonceLength> {
161+
if nonce.len() != Self::NonceSize::USIZE {
162+
Err(InvalidKeyNonceLength)
163+
} else {
164+
C::BlockCipher::new_varkey(key)
165+
.map_err(|_| InvalidKeyNonceLength)
166+
.map(|cipher| {
167+
let nonce = GenericArray::from_slice(nonce);
168+
Self::from_block_cipher(cipher, nonce)
169+
})
170+
}
171+
}
157172
}

0 commit comments

Comments
 (0)