Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use associated data as IV for Aes256Cbc #36

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
by default).
- Change store implementations to use littlefs2’s `DynFilesystem` trait instead
of being generic over the storage implementation.
- Use associated data as IV if not empty for `Aes256Cbc`.

### Fixed

Expand Down
24 changes: 20 additions & 4 deletions src/mechanisms/aes256cbc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,16 @@ impl Encrypt for super::Aes256Cbc {
.try_into()
.map_err(|_| Error::InternalError)?;

let zero_iv = [0u8; 16];
let cipher = Aes256CbcEnc::new_from_slices(&symmetric_key, &zero_iv).unwrap();
let iv = if request.associated_data.is_empty() {
[0u8; 16]
} else {
request
.associated_data
.as_slice()
.try_into()
.map_err(|_| Error::MechanismParamInvalid)?
};
let cipher = Aes256CbcEnc::new_from_slices(&symmetric_key, &iv).unwrap();

// buffer must have enough space for message+padding
let mut buffer = request.message.clone();
Expand Down Expand Up @@ -117,8 +125,16 @@ impl Decrypt for super::Aes256Cbc {
.try_into()
.map_err(|_| Error::InternalError)?;

let zero_iv = [0u8; 16];
let cipher = Aes256CbcDec::new_from_slices(&symmetric_key, &zero_iv).unwrap();
let iv = if request.associated_data.is_empty() {
[0u8; 16]
} else {
request
.associated_data
.as_slice()
.try_into()
.map_err(|_| Error::MechanismParamInvalid)?
};
let cipher = Aes256CbcDec::new_from_slices(&symmetric_key, &iv).unwrap();

// buffer must have enough space for message+padding
let mut buffer = request.message.clone();
Expand Down
Loading