Skip to content
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
31 changes: 20 additions & 11 deletions benches/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use base64ct::{Base64, Encoding};
use num_bigint::BigUint;
use num_traits::{FromPrimitive, Num};
use rand_chacha::{rand_core::SeedableRng, ChaCha8Rng};
use rsa::{PaddingScheme, RsaPrivateKey};
use rsa::signature::RandomizedSigner;
use rsa::traits::Decryptor;
use rsa::RsaPrivateKey;
use sha2::{Digest, Sha256};
use test::Bencher;

Expand All @@ -28,30 +30,37 @@ fn get_key() -> RsaPrivateKey {
#[bench]
fn bench_rsa_2048_pkcsv1_decrypt(b: &mut Bencher) {
let priv_key = get_key();
let decryption_key = rsa::pkcs1v15::DecryptingKey::new(priv_key);
let x = Base64::decode_vec(DECRYPT_VAL).unwrap();

b.iter(|| {
let res = priv_key
.decrypt(PaddingScheme::new_pkcs1v15_encrypt(), &x)
.unwrap();
let res = decryption_key.decrypt(&x).unwrap();
test::black_box(res);
});
}

#[bench]
fn bench_rsa_2048_pkcsv1_sign_blinded(b: &mut Bencher) {
let priv_key = get_key();
let signing_key = rsa::pkcs1v15::SigningKey::<Sha256>::new_with_prefix(priv_key);
let digest = Sha256::digest(b"testing").to_vec();
let mut rng = ChaCha8Rng::from_seed([42; 32]);

b.iter(|| {
let res = priv_key
.sign_blinded(
&mut rng,
PaddingScheme::new_pkcs1v15_sign::<Sha256>(),
&digest,
)
.unwrap();
let res = signing_key.sign_with_rng(&mut rng, &digest);
test::black_box(res);
});
}

#[bench]
fn bench_rsa_2048_pss_sign_blinded(b: &mut Bencher) {
let priv_key = get_key();
let signing_key = rsa::pss::SigningKey::<Sha256>::new(priv_key);
let digest = Sha256::digest(b"testing").to_vec();
let mut rng = ChaCha8Rng::from_seed([42; 32]);

b.iter(|| {
let res = signing_key.sign_with_rng(&mut rng, &digest);
test::black_box(res);
});
}
33 changes: 1 addition & 32 deletions src/algorithms.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Useful algorithms related to RSA.

use digest::{Digest, DynDigest, FixedOutputReset};
use digest::{Digest, FixedOutputReset};
use num_bigint::traits::ModInverse;
use num_bigint::{BigUint, RandPrime};
#[allow(unused_imports)]
Expand Down Expand Up @@ -135,37 +135,6 @@ pub fn generate_multi_prime_key_with_exp<R: CryptoRngCore + ?Sized>(
RsaPrivateKey::from_components(n_final, exp.clone(), d_final, primes)
}

/// Mask generation function.
///
/// Panics if out is larger than 2**32. This is in accordance with RFC 8017 - PKCS #1 B.2.1
pub fn mgf1_xor(out: &mut [u8], digest: &mut dyn DynDigest, seed: &[u8]) {
let mut counter = [0u8; 4];
let mut i = 0;

const MAX_LEN: u64 = core::u32::MAX as u64 + 1;
assert!(out.len() as u64 <= MAX_LEN);

while i < out.len() {
let mut digest_input = vec![0u8; seed.len() + 4];
digest_input[0..seed.len()].copy_from_slice(seed);
digest_input[seed.len()..].copy_from_slice(&counter);

digest.update(digest_input.as_slice());
let digest_output = &*digest.finalize_reset();
let mut j = 0;
loop {
if j >= digest_output.len() || i >= out.len() {
break;
}

out[i] ^= digest_output[j];
j += 1;
i += 1;
}
inc_counter(&mut counter);
}
}

/// Mask generation function.
///
/// Panics if out is larger than 2**32. This is in accordance with RFC 8017 - PKCS #1 B.2.1
Expand Down
Loading