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

oaep: fixup Deserialize implementation #545

Merged
merged 1 commit into from
Jun 27, 2024
Merged
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
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,
})
}
}
Loading