Skip to content

Commit e107ed3

Browse files
aes256cbc: Use no padding instead of zero padding
Defaulting to zero padding can be problematic. Using no padding leaves it up to the application to handle the padding correctly according to the use case. Fixes: #209
1 parent 024e0ec commit e107ed3

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6666
- Removed the `unsafe` keyword for the `Platform` trait.
6767
- Replaced the mechanism RPC traits in `service` with a single `MechanismImpl` trait.
6868
- Made the `mechanisms` module private. Mechanism implementation can still be accessed via the `Mechanism` enum.
69+
- Changed the `Aes256Cbc` mechanism to use no padding instead of zero padding.
6970

7071
### Fixed
7172

src/mechanisms/aes256cbc.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ impl MechanismImpl for super::Aes256Cbc {
1515
request: &request::Encrypt,
1616
) -> Result<reply::Encrypt, Error> {
1717
use aes::Aes256;
18-
use cbc::cipher::{block_padding::ZeroPadding, BlockEncryptMut, KeyIvInit};
18+
use cbc::cipher::{block_padding::NoPadding, BlockEncryptMut, KeyIvInit};
1919

2020
type Aes256CbcEnc = cbc::Encryptor<Aes256>;
21-
// TODO: perhaps use NoPadding and have client pad, to emphasize spec-conformance?
2221

2322
let key_id = request.key;
2423
let key = keystore.load_key(key::Secrecy::Secret, None, &key_id)?;
@@ -51,11 +50,9 @@ impl MechanismImpl for super::Aes256Cbc {
5150
// hprintln!(" aes256cbc encrypting l = {}B: {:?}", l, &buffer).ok();
5251

5352
// Encrypt message in-place.
54-
// &buffer[..pos] is used as a message and &buffer[pos..] as a reserved space for padding.
55-
// The padding space should be big enough for padding, otherwise method will return Err(BlockModeError).
5653
let ciphertext = cipher
57-
.encrypt_padded_mut::<ZeroPadding>(&mut buffer, l)
58-
.unwrap();
54+
.encrypt_padded_mut::<NoPadding>(&mut buffer, l)
55+
.map_err(|_| Error::MechanismParamInvalid)?;
5956

6057
let ciphertext = Message::from_slice(ciphertext).unwrap();
6158
Ok(reply::Encrypt {
@@ -104,9 +101,8 @@ impl MechanismImpl for super::Aes256Cbc {
104101
request: &request::Decrypt,
105102
) -> Result<reply::Decrypt, Error> {
106103
use aes::Aes256;
107-
use cbc::cipher::{block_padding::ZeroPadding, BlockDecryptMut, KeyIvInit};
104+
use cbc::cipher::{block_padding::NoPadding, BlockDecryptMut, KeyIvInit};
108105

109-
// TODO: perhaps use NoPadding and have client pad, to emphasize spec-conformance?
110106
type Aes256CbcDec = cbc::Decryptor<Aes256>;
111107

112108
let key_id = request.key;
@@ -140,13 +136,12 @@ impl MechanismImpl for super::Aes256Cbc {
140136
// let l = buffer.len();
141137

142138
// Decrypt message in-place.
143-
// Returns an error if buffer length is not multiple of block size and
144-
// if after decoding message has malformed padding.
139+
// Returns an error if buffer length is not multiple of block size
145140
// hprintln!("encrypted: {:?}", &buffer).ok();
146141
// hprintln!("symmetric key: {:?}", &symmetric_key).ok();
147142
let plaintext = cipher
148-
.decrypt_padded_mut::<ZeroPadding>(&mut buffer)
149-
.unwrap();
143+
.decrypt_padded_mut::<NoPadding>(&mut buffer)
144+
.map_err(|_| Error::MechanismParamInvalid)?;
150145
// hprintln!("decrypted: {:?}", &plaintext).ok();
151146
let plaintext = Message::from_slice(plaintext).unwrap();
152147

0 commit comments

Comments
 (0)