Skip to content

Commit

Permalink
oaep: fixup Deserialize implementation
Browse files Browse the repository at this point in the history
label_data was always empty because it was consumed by data.

This commits re-splits the payload according to the selected mgf hash.
  • Loading branch information
baloo committed Jun 27, 2024
1 parent 011764b commit 29b5e1e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ serde = { version = "1", features = ["serde_derive"] }
rand_core = { version = "0.6", features = ["std"] }
rsa = "0.9.6"
signature = { version = "2", features = ["derive"] }
sha1 = { version = "0.10", default-features = false }
sha2 = { version = "0.10", features = ["oid"] }
spki = { version = "0.7.3", default-features = false }
subtle = "2"
Expand Down
52 changes: 50 additions & 2 deletions src/rsa/oaep/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ use crate::{
response::Response,
rsa,
};
use serde::{Deserialize, Serialize};
use serde::{de::Deserializer, Deserialize, Serialize};
use sha1::Sha1;
use sha2::{
digest::{typenum::Unsigned, OutputSizeUser},
Sha256, Sha384, Sha512,
};

/// Request parameters for `command::decrypt_rsa_oaep`
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Debug)]
pub(crate) struct DecryptOaepCommand {
/// ID of the decryption key
pub key_id: object::Id,
Expand Down Expand Up @@ -41,3 +46,46 @@ impl From<DecryptOaepResponse> for rsa::oaep::DecryptedData {
response.0
}
}

impl<'de> Deserialize<'de> for DecryptOaepCommand {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
struct DecryptOaepCommand {
/// ID of the decryption key
key_id: object::Id,

/// Hash algorithm to use for MGF1
mgf1_hash_alg: rsa::mgf::Algorithm,

/// Data to be decrypted
data: Vec<u8>,
}

let mut value = DecryptOaepCommand::deserialize(deserializer)?;

let label_hash = match value.mgf1_hash_alg {
rsa::mgf::Algorithm::Sha1 => value
.data
.split_off(value.data.len() - <Sha1 as OutputSizeUser>::OutputSize::USIZE),
rsa::mgf::Algorithm::Sha256 => value
.data
.split_off(value.data.len() - <Sha256 as OutputSizeUser>::OutputSize::USIZE),
rsa::mgf::Algorithm::Sha384 => value
.data
.split_off(value.data.len() - <Sha384 as OutputSizeUser>::OutputSize::USIZE),
rsa::mgf::Algorithm::Sha512 => value
.data
.split_off(value.data.len() - <Sha512 as OutputSizeUser>::OutputSize::USIZE),
};

Ok(Self {
key_id: value.key_id,
mgf1_hash_alg: value.mgf1_hash_alg,
data: value.data,
label_hash,
})
}
}

0 comments on commit 29b5e1e

Please sign in to comment.