use hmac::{Hmac, Mac};
use rsa::RsaPrivateKey;
use rsa::traits::{PrivateKeyParts, PublicKeyParts};
use sha2::{Digest, Sha256};
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq};
use zeroize::Zeroizing;
const DOMAIN: &[u8] = b"openszigno/cms/implicit-rejection/v1";
const TAG_BYTES: usize = 32;
pub(super) fn unwrap_pkcs1v15(
private: &RsaPrivateKey,
ciphertext: &[u8],
key_bytes: usize,
) -> Zeroizing<Vec<u8>> {
let synthetic = synthetic_key(private, ciphertext, key_bytes);
let modulus_bytes = private.size();
if modulus_bytes < key_bytes + 11 {
return synthetic;
}
let block = private_operation(private, ciphertext, modulus_bytes);
let (usable, candidate) = unpad(&block, key_bytes);
select(&candidate, &synthetic, usable)
}
pub(super) fn unwrap_oaep(
private: &RsaPrivateKey,
padding: rsa::Oaep,
ciphertext: &[u8],
key_bytes: usize,
) -> Zeroizing<Vec<u8>> {
let synthetic = synthetic_key(private, ciphertext, key_bytes);
let mut rng = rand_core::OsRng;
let recovered = Zeroizing::new(
private
.decrypt_blinded(&mut rng, padding, ciphertext)
.unwrap_or_default(),
);
let usable = recovered.len().ct_eq(&key_bytes);
let mut candidate = Zeroizing::new(vec![0u8; key_bytes]);
let copied = recovered.len().min(key_bytes);
candidate[..copied].copy_from_slice(&recovered[..copied]);
select(&candidate, &synthetic, usable)
}
fn private_operation(
private: &RsaPrivateKey,
ciphertext: &[u8],
modulus_bytes: usize,
) -> Zeroizing<Vec<u8>> {
let mut block = Zeroizing::new(vec![0u8; modulus_bytes]);
if ciphertext.len() != modulus_bytes {
return block;
}
let mut rng = rand_core::OsRng;
let encoded = rsa::BigUint::from_bytes_be(ciphertext);
let Ok(plaintext) = rsa::hazmat::rsa_decrypt_and_check(private, Some(&mut rng), &encoded)
else {
return block;
};
let bytes = Zeroizing::new(plaintext.to_bytes_be());
let start = modulus_bytes.saturating_sub(bytes.len());
block[start..].copy_from_slice(&bytes[bytes.len() - (modulus_bytes - start)..]);
block
}
fn unpad(block: &[u8], key_bytes: usize) -> (Choice, Zeroizing<Vec<u8>>) {
let separator = block.len() - key_bytes - 1;
let mut usable = block[0].ct_eq(&0x00) & block[1].ct_eq(&0x02) & block[separator].ct_eq(&0x00);
for byte in &block[2..separator] {
usable &= !byte.ct_eq(&0x00);
}
(usable, Zeroizing::new(block[separator + 1..].to_vec()))
}
fn select(candidate: &[u8], fallback: &[u8], usable: Choice) -> Zeroizing<Vec<u8>> {
let mut chosen = Zeroizing::new(vec![0u8; candidate.len()]);
for (slot, (from_block, from_secret)) in
chosen.iter_mut().zip(candidate.iter().zip(fallback.iter()))
{
*slot = u8::conditional_select(from_secret, from_block, usable);
}
chosen
}
pub(super) fn synthetic_key(
private: &RsaPrivateKey,
ciphertext: &[u8],
key_bytes: usize,
) -> Zeroizing<Vec<u8>> {
let secret = derivation_secret(private);
let mut derived = Zeroizing::new(vec![0u8; key_bytes]);
for (counter, chunk) in derived.chunks_mut(TAG_BYTES).enumerate() {
let mut mac =
Hmac::<Sha256>::new_from_slice(secret.as_slice()).expect("HMAC takes any key length");
mac.update(DOMAIN);
mac.update(&(counter as u64).to_be_bytes());
mac.update(&(key_bytes as u64).to_be_bytes());
mac.update(ciphertext);
let mut tag = Zeroizing::new([0u8; TAG_BYTES]);
tag.copy_from_slice(&mac.finalize().into_bytes());
chunk.copy_from_slice(&tag[..chunk.len()]);
}
derived
}
fn derivation_secret(private: &RsaPrivateKey) -> Zeroizing<[u8; TAG_BYTES]> {
let mut hasher = Sha256::new();
hasher.update(DOMAIN);
hasher.update(Zeroizing::new(private.d().to_bytes_be()).as_slice());
for prime in private.primes() {
hasher.update(Zeroizing::new(prime.to_bytes_be()).as_slice());
}
Zeroizing::new(hasher.finalize().into())
}
#[cfg(test)]
mod tests {
use super::*;
fn padded(modulus_bytes: usize, key: &[u8]) -> Vec<u8> {
let mut block = vec![0u8; modulus_bytes];
block[1] = 0x02;
let separator = modulus_bytes - key.len() - 1;
for byte in &mut block[2..separator] {
*byte = 0xa5;
}
block[separator] = 0x00;
block[separator + 1..].copy_from_slice(key);
block
}
#[test]
fn a_well_formed_block_unpads_to_the_announced_key_length() {
let key: Vec<u8> = (0..32u8).collect();
let (usable, candidate) = unpad(&padded(256, &key), 32);
assert!(bool::from(usable), "valid type 2 padding must be accepted");
assert_eq!(candidate.as_slice(), key.as_slice());
}
#[test]
fn every_framing_fault_is_rejected_and_still_yields_a_candidate() {
let key: Vec<u8> = (0..16u8).collect();
for (case, offset, value) in [
("a first byte that is not 0x00", 0usize, 0x01u8),
("a second byte that is not 0x02", 1, 0x01),
("a zero inside the padding run", 100, 0x00),
("a padding run shorter than eight bytes", 2, 0x00),
("a separator that is not 0x00", 256 - 16 - 1, 0xff),
] {
let mut block = padded(256, &key);
block[offset] = value;
let (usable, candidate) = unpad(&block, 16);
assert!(!bool::from(usable), "{case} must be rejected");
assert_eq!(candidate.len(), 16, "{case} still produces a candidate");
}
}
#[test]
fn a_payload_of_the_wrong_length_shifts_the_separator_and_is_rejected() {
let key: Vec<u8> = (1..=32u8).collect();
let (usable, candidate) = unpad(&padded(256, &key), 16);
assert!(
!bool::from(usable),
"a key of the wrong length must not unpad"
);
assert_eq!(candidate.len(), 16);
}
fn keys() -> &'static [RsaPrivateKey; 2] {
use rand_core::SeedableRng as _;
static KEYS: std::sync::LazyLock<[RsaPrivateKey; 2]> = std::sync::LazyLock::new(|| {
let mut rng = rand_chacha::ChaCha20Rng::seed_from_u64(0x006F_7065_6E53_5A01);
[
RsaPrivateKey::new(&mut rng, 2048).expect("a synthetic key generates"),
RsaPrivateKey::new(&mut rng, 2048).expect("a synthetic key generates"),
]
});
&KEYS
}
#[test]
fn the_synthetic_key_is_stable_per_key_and_ciphertext_and_nothing_else() {
let [first, second] = keys();
let ciphertext = [0x11u8; 256];
let other = [0x12u8; 256];
let baseline = synthetic_key(first, &ciphertext, 32);
assert_eq!(baseline.len(), 32, "the length the caller asked for");
assert_eq!(
synthetic_key(first, &ciphertext, 32).as_slice(),
baseline.as_slice(),
"the same key and ciphertext must derive the same substitute"
);
assert_ne!(
synthetic_key(first, &other, 32).as_slice(),
baseline.as_slice(),
"a different ciphertext must derive a different substitute"
);
assert_ne!(
synthetic_key(second, &ciphertext, 32).as_slice(),
baseline.as_slice(),
"a different private key must derive a different substitute"
);
let short = synthetic_key(first, &ciphertext, 16);
assert_eq!(short.len(), 16);
assert_ne!(short.as_slice(), &baseline[..16]);
}
#[test]
fn an_unusable_ciphertext_still_yields_a_key_of_the_announced_length() {
let [private, _] = keys();
for (case, ciphertext) in [
("a ciphertext of the wrong length", vec![0x00; 8]),
("a ciphertext that is not a valid block", vec![0x01; 256]),
] {
for key_bytes in [16usize, 24, 32] {
let unwrapped = unwrap_pkcs1v15(private, &ciphertext, key_bytes);
assert_eq!(unwrapped.len(), key_bytes, "{case}");
assert_eq!(
unwrapped.as_slice(),
synthetic_key(private, &ciphertext, key_bytes).as_slice(),
"{case} must be answered with the synthetic key"
);
}
}
let unwrapped = unwrap_pkcs1v15(private, &[0x01; 256], 256);
assert_eq!(unwrapped.len(), 256);
}
#[test]
fn selection_takes_the_candidate_only_when_the_block_was_usable() {
let candidate = [1u8; 16];
let fallback = [2u8; 16];
assert_eq!(
select(&candidate, &fallback, Choice::from(1)).as_slice(),
&candidate
);
assert_eq!(
select(&candidate, &fallback, Choice::from(0)).as_slice(),
&fallback
);
}
}