Merge pull request #110 from RustCrypto/fix-type-names
refactor: rename `RSA*` to `Rsa`
This commit is contained in:
commit
cd257a74aa
@ -14,13 +14,13 @@ A portable RSA implementation in pure Rust.
|
||||
## Example
|
||||
|
||||
```rust
|
||||
use rsa::{PublicKey, RSAPrivateKey, PaddingScheme};
|
||||
use rsa::{PublicKey, RsaPrivateKey, PaddingScheme};
|
||||
use rand::rngs::OsRng;
|
||||
|
||||
let mut rng = OsRng;
|
||||
let bits = 2048;
|
||||
let priv_key = RSAPrivateKey::new(&mut rng, bits).expect("failed to generate a key");
|
||||
let pub_key = RSAPublicKey::from(&priv_key);
|
||||
let priv_key = RsaPrivateKey::new(&mut rng, bits).expect("failed to generate a key");
|
||||
let pub_key = RsaPublicKey::from(&priv_key);
|
||||
|
||||
// Encrypt
|
||||
let data = b"hello world";
|
||||
@ -32,7 +32,7 @@ let dec_data = priv_key.decrypt(PaddingScheme::new_pkcs1v15(), &enc_data).expect
|
||||
assert_eq!(&data[..], &dec_data[..]);
|
||||
```
|
||||
|
||||
> **Note:** If you encounter unusually slow key generation time while using `RSAPrivateKey::new` you can try to compile in release mode or add the following to your `Cargo.toml`. Key generation is much faster when building with higher optimization levels, but this will increase the compile time a bit.
|
||||
> **Note:** If you encounter unusually slow key generation time while using `RsaPrivateKey::new` you can try to compile in release mode or add the following to your `Cargo.toml`. Key generation is much faster when building with higher optimization levels, but this will increase the compile time a bit.
|
||||
> ```toml
|
||||
> [profile.debug]
|
||||
> opt-level = 3
|
||||
|
@ -6,15 +6,15 @@ use base64;
|
||||
use num_bigint::BigUint;
|
||||
use num_traits::{FromPrimitive, Num};
|
||||
use rand::{rngs::StdRng, SeedableRng};
|
||||
use rsa::{Hash, PaddingScheme, RSAPrivateKey};
|
||||
use rsa::{Hash, PaddingScheme, RsaPrivateKey};
|
||||
use sha2::{Digest, Sha256};
|
||||
use test::Bencher;
|
||||
|
||||
const DECRYPT_VAL: &'static str =
|
||||
"XW4qfrpQDarEMBfPyIYE9UvuOFkbBi0tiGYbIOJPLMNe/LWuPD0BQ7ceqlOlPPcKLinYz0DlnqW3It/V7ae59zw9afA3YIWdq0Ut2BnYL+aJixnqaP+PjsQNcHg6axCF11iNQ4jpXrZDiQcI+q9EEzZDTMsiMxtjfgBQUd8LHT87YoQXDWaFPCVpliACMc8aUk442kH1tc4jEuXwjEjFErvAM/J7VizCdU/dnKrlq2mBDzvZ6hxY9TYHFB/zY6DZPJAgEMUxYWCR9xPJ7X256DV1Kt0Ht33DWoFcgh/pPLM1q9pK0HVxCdclXfZOeCqlrLgZ5Gxv5DM4BtV7Z4m85w==";
|
||||
|
||||
fn get_key() -> RSAPrivateKey {
|
||||
RSAPrivateKey::from_components(
|
||||
fn get_key() -> RsaPrivateKey {
|
||||
RsaPrivateKey::from_components(
|
||||
BigUint::from_str_radix("14314132931241006650998084889274020608918049032671858325988396851334124245188214251956198731333464217832226406088020736932173064754214329009979944037640912127943488972644697423190955557435910767690712778463524983667852819010259499695177313115447116110358524558307947613422897787329221478860907963827160223559690523660574329011927531289655711860504630573766609239332569210831325633840174683944553667352219670930408593321661375473885147973879086994006440025257225431977751512374815915392249179976902953721486040787792801849818254465486633791826766873076617116727073077821584676715609985777563958286637185868165868520557", 10).unwrap(),
|
||||
BigUint::from_u32(3).unwrap(),
|
||||
BigUint::from_str_radix("9542755287494004433998723259516013739278699355114572217325597900889416163458809501304132487555642811888150937392013824621448709836142886006653296025093941418628992648429798282127303704957273845127141852309016655778568546006839666463451542076964744073572349705538631742281931858219480985907271975884773482372966847639853897890615456605598071088189838676728836833012254065983259638538107719766738032720239892094196108713378822882383694456030043492571063441943847195939549773271694647657549658603365629458610273821292232646334717612674519997533901052790334279661754176490593041941863932308687197618671528035670452762731", 10).unwrap(),
|
||||
|
@ -8,7 +8,7 @@ use num_traits::{FromPrimitive, One, Zero};
|
||||
use rand::Rng;
|
||||
|
||||
use crate::errors::{Error, Result};
|
||||
use crate::key::RSAPrivateKey;
|
||||
use crate::key::RsaPrivateKey;
|
||||
|
||||
/// Default exponent for RSA keys.
|
||||
const EXP: u64 = 65537;
|
||||
@ -32,7 +32,7 @@ pub fn generate_multi_prime_key<R: Rng>(
|
||||
rng: &mut R,
|
||||
nprimes: usize,
|
||||
bit_size: usize,
|
||||
) -> Result<RSAPrivateKey> {
|
||||
) -> Result<RsaPrivateKey> {
|
||||
let exp = BigUint::from_u64(EXP).expect("invalid static exponent");
|
||||
generate_multi_prime_key_with_exp(rng, nprimes, bit_size, &exp)
|
||||
}
|
||||
@ -53,7 +53,7 @@ pub fn generate_multi_prime_key_with_exp<R: Rng>(
|
||||
nprimes: usize,
|
||||
bit_size: usize,
|
||||
exp: &BigUint,
|
||||
) -> Result<RSAPrivateKey> {
|
||||
) -> Result<RsaPrivateKey> {
|
||||
if nprimes < 2 {
|
||||
return Err(Error::NprimesTooSmall);
|
||||
}
|
||||
@ -131,7 +131,7 @@ pub fn generate_multi_prime_key_with_exp<R: Rng>(
|
||||
}
|
||||
}
|
||||
|
||||
Ok(RSAPrivateKey::from_components(
|
||||
Ok(RsaPrivateKey::from_components(
|
||||
n_final,
|
||||
exp.clone(),
|
||||
d_final,
|
||||
|
@ -1,6 +1,6 @@
|
||||
//! PKCS#1 encoding support
|
||||
|
||||
use crate::{key::PublicKeyParts, BigUint, RSAPrivateKey, RSAPublicKey};
|
||||
use crate::{key::PublicKeyParts, BigUint, RsaPrivateKey, RsaPublicKey};
|
||||
use num_bigint::ModInverse;
|
||||
use pkcs1::{
|
||||
FromRsaPrivateKey, FromRsaPublicKey, RsaPrivateKeyDocument, RsaPublicKeyDocument,
|
||||
@ -8,7 +8,7 @@ use pkcs1::{
|
||||
};
|
||||
use zeroize::Zeroizing;
|
||||
|
||||
impl FromRsaPrivateKey for RSAPrivateKey {
|
||||
impl FromRsaPrivateKey for RsaPrivateKey {
|
||||
fn from_pkcs1_private_key(pkcs1_key: pkcs1::RsaPrivateKey<'_>) -> pkcs1::Result<Self> {
|
||||
let n = BigUint::from_bytes_be(pkcs1_key.modulus.as_bytes());
|
||||
let e = BigUint::from_bytes_be(pkcs1_key.public_exponent.as_bytes());
|
||||
@ -16,19 +16,19 @@ impl FromRsaPrivateKey for RSAPrivateKey {
|
||||
let prime1 = BigUint::from_bytes_be(pkcs1_key.prime1.as_bytes());
|
||||
let prime2 = BigUint::from_bytes_be(pkcs1_key.prime2.as_bytes());
|
||||
let primes = vec![prime1, prime2];
|
||||
Ok(RSAPrivateKey::from_components(n, e, d, primes))
|
||||
Ok(RsaPrivateKey::from_components(n, e, d, primes))
|
||||
}
|
||||
}
|
||||
|
||||
impl FromRsaPublicKey for RSAPublicKey {
|
||||
impl FromRsaPublicKey for RsaPublicKey {
|
||||
fn from_pkcs1_public_key(pkcs1_key: pkcs1::RsaPublicKey<'_>) -> pkcs1::Result<Self> {
|
||||
let n = BigUint::from_bytes_be(pkcs1_key.modulus.as_bytes());
|
||||
let e = BigUint::from_bytes_be(pkcs1_key.public_exponent.as_bytes());
|
||||
RSAPublicKey::new(n, e).map_err(|_| pkcs1::Error::Crypto)
|
||||
RsaPublicKey::new(n, e).map_err(|_| pkcs1::Error::Crypto)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToRsaPrivateKey for RSAPrivateKey {
|
||||
impl ToRsaPrivateKey for RsaPrivateKey {
|
||||
fn to_pkcs1_der(&self) -> pkcs1::Result<RsaPrivateKeyDocument> {
|
||||
// Check if the key is multi prime
|
||||
if self.primes.len() > 2 {
|
||||
@ -65,7 +65,7 @@ impl ToRsaPrivateKey for RSAPrivateKey {
|
||||
}
|
||||
}
|
||||
|
||||
impl ToRsaPublicKey for RSAPublicKey {
|
||||
impl ToRsaPublicKey for RsaPublicKey {
|
||||
fn to_pkcs1_der(&self) -> pkcs1::Result<RsaPublicKeyDocument> {
|
||||
let modulus = self.n().to_bytes_be();
|
||||
let public_exponent = self.e().to_bytes_be();
|
||||
|
@ -7,7 +7,7 @@ use rand::Rng;
|
||||
use zeroize::Zeroize;
|
||||
|
||||
use crate::errors::{Error, Result};
|
||||
use crate::key::{PublicKeyParts, RSAPrivateKey};
|
||||
use crate::key::{PublicKeyParts, RsaPrivateKey};
|
||||
|
||||
/// Raw RSA encryption of m with the public key. No padding is performed.
|
||||
#[inline]
|
||||
@ -20,7 +20,7 @@ pub fn encrypt<K: PublicKeyParts>(key: &K, m: &BigUint) -> BigUint {
|
||||
#[inline]
|
||||
pub fn decrypt<R: Rng>(
|
||||
mut rng: Option<&mut R>,
|
||||
priv_key: &RSAPrivateKey,
|
||||
priv_key: &RsaPrivateKey,
|
||||
c: &BigUint,
|
||||
) -> Result<BigUint> {
|
||||
if c >= priv_key.n() {
|
||||
@ -110,7 +110,7 @@ pub fn decrypt<R: Rng>(
|
||||
#[inline]
|
||||
pub fn decrypt_and_check<R: Rng>(
|
||||
rng: Option<&mut R>,
|
||||
priv_key: &RSAPrivateKey,
|
||||
priv_key: &RsaPrivateKey,
|
||||
c: &BigUint,
|
||||
) -> Result<BigUint> {
|
||||
let m = decrypt(rng, priv_key, c)?;
|
||||
|
116
src/key.rs
116
src/key.rs
@ -45,7 +45,7 @@ pub trait PrivateKey: DecryptionPrimitive + PublicKeyParts {}
|
||||
derive(Serialize, Deserialize),
|
||||
serde(crate = "serde_crate")
|
||||
)]
|
||||
pub struct RSAPublicKey {
|
||||
pub struct RsaPublicKey {
|
||||
n: BigUint,
|
||||
e: BigUint,
|
||||
}
|
||||
@ -57,9 +57,9 @@ pub struct RSAPublicKey {
|
||||
derive(Serialize, Deserialize),
|
||||
serde(crate = "serde_crate")
|
||||
)]
|
||||
pub struct RSAPrivateKey {
|
||||
pub struct RsaPrivateKey {
|
||||
/// Public components of the private key.
|
||||
pubkey_components: RSAPublicKey,
|
||||
pubkey_components: RsaPublicKey,
|
||||
/// Private exponent
|
||||
pub(crate) d: BigUint,
|
||||
/// Prime factors of N, contains >= 2 elements.
|
||||
@ -69,18 +69,18 @@ pub struct RSAPrivateKey {
|
||||
pub(crate) precomputed: Option<PrecomputedValues>,
|
||||
}
|
||||
|
||||
impl PartialEq for RSAPrivateKey {
|
||||
impl PartialEq for RsaPrivateKey {
|
||||
#[inline]
|
||||
fn eq(&self, other: &RSAPrivateKey) -> bool {
|
||||
fn eq(&self, other: &RsaPrivateKey) -> bool {
|
||||
self.pubkey_components == other.pubkey_components
|
||||
&& self.d == other.d
|
||||
&& self.primes == other.primes
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for RSAPrivateKey {}
|
||||
impl Eq for RsaPrivateKey {}
|
||||
|
||||
impl Zeroize for RSAPrivateKey {
|
||||
impl Zeroize for RsaPrivateKey {
|
||||
fn zeroize(&mut self) {
|
||||
self.d.zeroize();
|
||||
for prime in self.primes.iter_mut() {
|
||||
@ -93,15 +93,15 @@ impl Zeroize for RSAPrivateKey {
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for RSAPrivateKey {
|
||||
impl Drop for RsaPrivateKey {
|
||||
fn drop(&mut self) {
|
||||
self.zeroize();
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for RSAPrivateKey {
|
||||
type Target = RSAPublicKey;
|
||||
fn deref(&self) -> &RSAPublicKey {
|
||||
impl Deref for RsaPrivateKey {
|
||||
type Target = RsaPublicKey;
|
||||
fn deref(&self) -> &RsaPublicKey {
|
||||
&self.pubkey_components
|
||||
}
|
||||
}
|
||||
@ -151,18 +151,18 @@ pub(crate) struct CRTValue {
|
||||
pub(crate) r: BigInt,
|
||||
}
|
||||
|
||||
impl From<RSAPrivateKey> for RSAPublicKey {
|
||||
fn from(private_key: RSAPrivateKey) -> Self {
|
||||
impl From<RsaPrivateKey> for RsaPublicKey {
|
||||
fn from(private_key: RsaPrivateKey) -> Self {
|
||||
(&private_key).into()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&RSAPrivateKey> for RSAPublicKey {
|
||||
fn from(private_key: &RSAPrivateKey) -> Self {
|
||||
impl From<&RsaPrivateKey> for RsaPublicKey {
|
||||
fn from(private_key: &RsaPrivateKey) -> Self {
|
||||
let n = private_key.n.clone();
|
||||
let e = private_key.e.clone();
|
||||
|
||||
RSAPublicKey { n, e }
|
||||
RsaPublicKey { n, e }
|
||||
}
|
||||
}
|
||||
|
||||
@ -178,7 +178,7 @@ pub trait PublicKey: EncryptionPrimitive + PublicKeyParts {
|
||||
fn verify(&self, padding: PaddingScheme, hashed: &[u8], sig: &[u8]) -> Result<()>;
|
||||
}
|
||||
|
||||
impl PublicKeyParts for RSAPublicKey {
|
||||
impl PublicKeyParts for RsaPublicKey {
|
||||
fn n(&self) -> &BigUint {
|
||||
&self.n
|
||||
}
|
||||
@ -188,7 +188,7 @@ impl PublicKeyParts for RSAPublicKey {
|
||||
}
|
||||
}
|
||||
|
||||
impl PublicKey for RSAPublicKey {
|
||||
impl PublicKey for RsaPublicKey {
|
||||
fn encrypt<R: Rng>(&self, rng: &mut R, padding: PaddingScheme, msg: &[u8]) -> Result<Vec<u8>> {
|
||||
match padding {
|
||||
PaddingScheme::PKCS1v15Encrypt => pkcs1v15::encrypt(rng, self, msg),
|
||||
@ -212,17 +212,17 @@ impl PublicKey for RSAPublicKey {
|
||||
}
|
||||
}
|
||||
|
||||
impl RSAPublicKey {
|
||||
impl RsaPublicKey {
|
||||
/// Create a new key from its components.
|
||||
pub fn new(n: BigUint, e: BigUint) -> Result<Self> {
|
||||
let k = RSAPublicKey { n, e };
|
||||
let k = RsaPublicKey { n, e };
|
||||
check_public(&k)?;
|
||||
|
||||
Ok(k)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> PublicKeyParts for &'a RSAPublicKey {
|
||||
impl<'a> PublicKeyParts for &'a RsaPublicKey {
|
||||
/// Returns the modulus of the key.
|
||||
fn n(&self) -> &BigUint {
|
||||
&self.n
|
||||
@ -234,7 +234,7 @@ impl<'a> PublicKeyParts for &'a RSAPublicKey {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> PublicKey for &'a RSAPublicKey {
|
||||
impl<'a> PublicKey for &'a RsaPublicKey {
|
||||
fn encrypt<R: Rng>(&self, rng: &mut R, padding: PaddingScheme, msg: &[u8]) -> Result<Vec<u8>> {
|
||||
(*self).encrypt(rng, padding, msg)
|
||||
}
|
||||
@ -244,7 +244,7 @@ impl<'a> PublicKey for &'a RSAPublicKey {
|
||||
}
|
||||
}
|
||||
|
||||
impl PublicKeyParts for RSAPrivateKey {
|
||||
impl PublicKeyParts for RsaPrivateKey {
|
||||
fn n(&self) -> &BigUint {
|
||||
&self.n
|
||||
}
|
||||
@ -254,9 +254,9 @@ impl PublicKeyParts for RSAPrivateKey {
|
||||
}
|
||||
}
|
||||
|
||||
impl PrivateKey for RSAPrivateKey {}
|
||||
impl PrivateKey for RsaPrivateKey {}
|
||||
|
||||
impl<'a> PublicKeyParts for &'a RSAPrivateKey {
|
||||
impl<'a> PublicKeyParts for &'a RsaPrivateKey {
|
||||
fn n(&self) -> &BigUint {
|
||||
&self.n
|
||||
}
|
||||
@ -266,23 +266,23 @@ impl<'a> PublicKeyParts for &'a RSAPrivateKey {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> PrivateKey for &'a RSAPrivateKey {}
|
||||
impl<'a> PrivateKey for &'a RsaPrivateKey {}
|
||||
|
||||
impl RSAPrivateKey {
|
||||
/// Generate a new RSA key pair of the given bit size using the passed in `rng`.
|
||||
pub fn new<R: Rng>(rng: &mut R, bit_size: usize) -> Result<RSAPrivateKey> {
|
||||
impl RsaPrivateKey {
|
||||
/// Generate a new Rsa key pair of the given bit size using the passed in `rng`.
|
||||
pub fn new<R: Rng>(rng: &mut R, bit_size: usize) -> Result<RsaPrivateKey> {
|
||||
generate_multi_prime_key(rng, 2, bit_size)
|
||||
}
|
||||
|
||||
/// Generate a new RSA key pair of the given bit size and the public exponent
|
||||
/// using the passed in `rng`.
|
||||
///
|
||||
/// Unless you have specific needs, you should use `RSAPrivateKey::new` instead.
|
||||
/// Unless you have specific needs, you should use `RsaPrivateKey::new` instead.
|
||||
pub fn new_with_exp<R: Rng>(
|
||||
rng: &mut R,
|
||||
bit_size: usize,
|
||||
exp: &BigUint,
|
||||
) -> Result<RSAPrivateKey> {
|
||||
) -> Result<RsaPrivateKey> {
|
||||
generate_multi_prime_key_with_exp(rng, 2, bit_size, exp)
|
||||
}
|
||||
|
||||
@ -292,9 +292,9 @@ impl RSAPrivateKey {
|
||||
e: BigUint,
|
||||
d: BigUint,
|
||||
primes: Vec<BigUint>,
|
||||
) -> RSAPrivateKey {
|
||||
let mut k = RSAPrivateKey {
|
||||
pubkey_components: RSAPublicKey { n, e },
|
||||
) -> RsaPrivateKey {
|
||||
let mut k = RsaPrivateKey {
|
||||
pubkey_components: RsaPublicKey { n, e },
|
||||
d,
|
||||
primes,
|
||||
precomputed: None,
|
||||
@ -308,11 +308,11 @@ impl RSAPrivateKey {
|
||||
|
||||
/// Get the public key from the private key, cloning `n` and `e`.
|
||||
///
|
||||
/// Generally this is not needed since `RSAPrivateKey` implements the `PublicKey` trait,
|
||||
/// Generally this is not needed since `RsaPrivateKey` implements the `PublicKey` trait,
|
||||
/// but it can occationally be useful to discard the private information entirely.
|
||||
pub fn to_public_key(&self) -> RSAPublicKey {
|
||||
pub fn to_public_key(&self) -> RsaPublicKey {
|
||||
// Safe to unwrap since n and e are already verified.
|
||||
RSAPublicKey::new(self.n().clone(), self.e().clone()).unwrap()
|
||||
RsaPublicKey::new(self.n().clone(), self.e().clone()).unwrap()
|
||||
}
|
||||
|
||||
/// Performs some calculations to speed up private key operations.
|
||||
@ -539,8 +539,8 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_from_into() {
|
||||
let private_key = RSAPrivateKey {
|
||||
pubkey_components: RSAPublicKey {
|
||||
let private_key = RsaPrivateKey {
|
||||
pubkey_components: RsaPublicKey {
|
||||
n: BigUint::from_u64(100).unwrap(),
|
||||
e: BigUint::from_u64(200).unwrap(),
|
||||
},
|
||||
@ -548,13 +548,13 @@ mod tests {
|
||||
primes: vec![],
|
||||
precomputed: None,
|
||||
};
|
||||
let public_key: RSAPublicKey = private_key.into();
|
||||
let public_key: RsaPublicKey = private_key.into();
|
||||
|
||||
assert_eq!(public_key.n().to_u64(), Some(100));
|
||||
assert_eq!(public_key.e().to_u64(), Some(200));
|
||||
}
|
||||
|
||||
fn test_key_basics(private_key: &RSAPrivateKey) {
|
||||
fn test_key_basics(private_key: &RsaPrivateKey) {
|
||||
private_key.validate().expect("invalid private key");
|
||||
|
||||
assert!(
|
||||
@ -562,7 +562,7 @@ mod tests {
|
||||
"private exponent too large"
|
||||
);
|
||||
|
||||
let pub_key: RSAPublicKey = private_key.clone().into();
|
||||
let pub_key: RsaPublicKey = private_key.clone().into();
|
||||
let m = BigUint::from_u64(42).expect("invalid 42");
|
||||
let c = internals::encrypt(&pub_key, &m);
|
||||
let m2 = internals::decrypt::<StdRng>(None, &private_key, &c)
|
||||
@ -588,7 +588,7 @@ mod tests {
|
||||
|
||||
for _ in 0..10 {
|
||||
let private_key = if $multi == 2 {
|
||||
RSAPrivateKey::new(&mut rng, $size).expect("failed to generate key")
|
||||
RsaPrivateKey::new(&mut rng, $size).expect("failed to generate key")
|
||||
} else {
|
||||
generate_multi_prime_key(&mut rng, $multi, $size).unwrap()
|
||||
};
|
||||
@ -619,7 +619,7 @@ mod tests {
|
||||
.unwrap();
|
||||
let mut rng = StdRng::seed_from_u64(seed.as_secs());
|
||||
for i in 0..32 {
|
||||
let _ = RSAPrivateKey::new(&mut rng, i).is_err();
|
||||
let _ = RsaPrivateKey::new(&mut rng, i).is_err();
|
||||
let _ = generate_multi_prime_key(&mut rng, 3, i);
|
||||
let _ = generate_multi_prime_key(&mut rng, 4, i);
|
||||
let _ = generate_multi_prime_key(&mut rng, 5, i);
|
||||
@ -628,7 +628,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_negative_decryption_value() {
|
||||
let private_key = RSAPrivateKey::from_components(
|
||||
let private_key = RsaPrivateKey::from_components(
|
||||
BigUint::from_bytes_le(&vec![
|
||||
99, 192, 208, 179, 0, 220, 7, 29, 49, 151, 75, 107, 75, 73, 200, 180,
|
||||
]),
|
||||
@ -655,16 +655,16 @@ mod tests {
|
||||
use serde_test::{assert_tokens, Token};
|
||||
|
||||
let mut rng = XorShiftRng::from_seed([1; 16]);
|
||||
let priv_key = RSAPrivateKey::new(&mut rng, 64).expect("failed to generate key");
|
||||
let priv_key = RsaPrivateKey::new(&mut rng, 64).expect("failed to generate key");
|
||||
|
||||
let priv_tokens = [
|
||||
Token::Struct {
|
||||
name: "RSAPrivateKey",
|
||||
name: "RsaPrivateKey",
|
||||
len: 3,
|
||||
},
|
||||
Token::Str("pubkey_components"),
|
||||
Token::Struct {
|
||||
name: "RSAPublicKey",
|
||||
name: "RsaPublicKey",
|
||||
len: 2,
|
||||
},
|
||||
Token::Str("n"),
|
||||
@ -697,7 +697,7 @@ mod tests {
|
||||
|
||||
let priv_tokens = [
|
||||
Token::Struct {
|
||||
name: "RSAPublicKey",
|
||||
name: "RsaPublicKey",
|
||||
len: 2,
|
||||
},
|
||||
Token::Str("n"),
|
||||
@ -711,7 +711,7 @@ mod tests {
|
||||
Token::SeqEnd,
|
||||
Token::StructEnd,
|
||||
];
|
||||
assert_tokens(&RSAPublicKey::from(priv_key), &priv_tokens);
|
||||
assert_tokens(&RsaPublicKey::from(priv_key), &priv_tokens);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -727,7 +727,7 @@ mod tests {
|
||||
base64::decode("CUWC+hRWOT421kwRllgVjy6FYv6jQUcgDNHeAiYZnf5HjS9iK2ki7v8G5dL/0f+Yf+NhE/4q8w4m8go51hACrVpP1p8GJDjiT09+RsOzITsHwl+ceEKoe56ZW6iDHBLlrNw5/MtcYhKpjNU9KJ2udm5J/c9iislcjgckrZG2IB8ADgXHMEByZ5DgaMl4AKZ1Gx8/q6KftTvmOT5rNTMLi76VN5KWQcDWK/DqXiOiZHM7Nr4dX4me3XeRgABJyNR8Fqxj3N1+HrYLe/zs7LOaK0++F9Ul3tLelhrhsvLxei3oCZkF9A/foD3on3luYA+1cRcxWpSY3h2J4/22+yo4+Q==").unwrap(),
|
||||
];
|
||||
|
||||
RSAPrivateKey::from_components(
|
||||
RsaPrivateKey::from_components(
|
||||
BigUint::from_bytes_be(&n),
|
||||
BigUint::from_bytes_be(&e),
|
||||
BigUint::from_bytes_be(&d),
|
||||
@ -735,7 +735,7 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
fn get_private_key() -> RSAPrivateKey {
|
||||
fn get_private_key() -> RsaPrivateKey {
|
||||
// -----BEGIN RSA PRIVATE KEY-----
|
||||
// MIIEpAIBAAKCAQEA05e4TZikwmE47RtpWoEG6tkdVTvwYEG2LT/cUKBB4iK49FKW
|
||||
// icG4LF5xVU9d1p+i9LYVjPDb61eBGg/DJ+HyjnT+dNO8Fmweq9wbi1e5NMqL5bAL
|
||||
@ -764,7 +764,7 @@ mod tests {
|
||||
// BoB0er/UmDm4Ly/97EO9A0PKMOE5YbMq9s3t3RlWcsdrU7dvw+p2+A==
|
||||
// -----END RSA PRIVATE KEY-----
|
||||
|
||||
RSAPrivateKey::from_components(
|
||||
RsaPrivateKey::from_components(
|
||||
BigUint::parse_bytes(b"00d397b84d98a4c26138ed1b695a8106ead91d553bf06041b62d3fdc50a041e222b8f4529689c1b82c5e71554f5dd69fa2f4b6158cf0dbeb57811a0fc327e1f28e74fe74d3bc166c1eabdc1b8b57b934ca8be5b00b4f29975bcc99acaf415b59bb28a6782bb41a2c3c2976b3c18dbadef62f00c6bb226640095096c0cc60d22fe7ef987d75c6a81b10d96bf292028af110dc7cc1bbc43d22adab379a0cd5d8078cc780ff5cd6209dea34c922cf784f7717e428d75b5aec8ff30e5f0141510766e2e0ab8d473c84e8710b2b98227c3db095337ad3452f19e2b9bfbccdd8148abf6776fa552775e6e75956e45229ae5a9c46949bab1e622f0e48f56524a84ed3483b", 16).unwrap(),
|
||||
BigUint::from_u64(65537).unwrap(),
|
||||
BigUint::parse_bytes(b"00c4e70c689162c94c660828191b52b4d8392115df486a9adbe831e458d73958320dc1b755456e93701e9702d76fb0b92f90e01d1fe248153281fe79aa9763a92fae69d8d7ecd144de29fa135bd14f9573e349e45031e3b76982f583003826c552e89a397c1a06bd2163488630d92e8c2bb643d7abef700da95d685c941489a46f54b5316f62b5d2c3a7f1bbd134cb37353a44683fdc9d95d36458de22f6c44057fe74a0a436c4308f73f4da42f35c47ac16a7138d483afc91e41dc3a1127382e0c0f5119b0221b4fc639d6b9c38177a6de9b526ebd88c38d7982c07f98a0efd877d508aae275b946915c02e2e1106d175d74ec6777f5e80d12c053d9c7be1e341", 16).unwrap(),
|
||||
@ -797,7 +797,7 @@ mod tests {
|
||||
do_test_oaep_with_different_hashes::<Sha3_512, Sha1>(&priv_key);
|
||||
}
|
||||
|
||||
fn do_test_encrypt_decrypt_oaep<D: 'static + Digest + DynDigest>(prk: &RSAPrivateKey) {
|
||||
fn do_test_encrypt_decrypt_oaep<D: 'static + Digest + DynDigest>(prk: &RsaPrivateKey) {
|
||||
let seed = SystemTime::now()
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.unwrap();
|
||||
@ -823,7 +823,7 @@ mod tests {
|
||||
None
|
||||
};
|
||||
|
||||
let pub_key: RSAPublicKey = prk.into();
|
||||
let pub_key: RsaPublicKey = prk.into();
|
||||
|
||||
let ciphertext = if let Some(ref label) = label {
|
||||
let padding = PaddingScheme::new_oaep_with_label::<D, _>(label);
|
||||
@ -856,7 +856,7 @@ mod tests {
|
||||
D: 'static + Digest + DynDigest,
|
||||
U: 'static + Digest + DynDigest,
|
||||
>(
|
||||
prk: &RSAPrivateKey,
|
||||
prk: &RsaPrivateKey,
|
||||
) {
|
||||
let seed = SystemTime::now()
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
@ -883,7 +883,7 @@ mod tests {
|
||||
None
|
||||
};
|
||||
|
||||
let pub_key: RSAPublicKey = prk.into();
|
||||
let pub_key: RsaPublicKey = prk.into();
|
||||
|
||||
let ciphertext = if let Some(ref label) = label {
|
||||
let padding = PaddingScheme::new_oaep_with_mgf_hash_with_label::<D, U, _>(label);
|
||||
@ -918,7 +918,7 @@ mod tests {
|
||||
.unwrap();
|
||||
let mut rng = StdRng::seed_from_u64(seed.as_secs());
|
||||
let priv_key = get_private_key();
|
||||
let pub_key: RSAPublicKey = (&priv_key).into();
|
||||
let pub_key: RsaPublicKey = (&priv_key).into();
|
||||
let ciphertext = pub_key
|
||||
.encrypt(
|
||||
&mut rng,
|
||||
|
30
src/lib.rs
30
src/lib.rs
@ -5,7 +5,7 @@
|
||||
//!
|
||||
//! Using PKCS1v15.
|
||||
//! ```
|
||||
//! use rsa::{PublicKey, RSAPrivateKey, RSAPublicKey, PaddingScheme};
|
||||
//! use rsa::{PublicKey, RsaPrivateKey, RsaPublicKey, PaddingScheme};
|
||||
//! # /*
|
||||
//! use rand::rngs::OsRng;
|
||||
//! let mut rng = OsRng;
|
||||
@ -13,8 +13,8 @@
|
||||
//! # use rand::{SeedableRng, rngs::StdRng};
|
||||
//! # let mut rng = rand::rngs::StdRng::seed_from_u64(0);
|
||||
//! let bits = 2048;
|
||||
//! let private_key = RSAPrivateKey::new(&mut rng, bits).expect("failed to generate a key");
|
||||
//! let public_key = RSAPublicKey::from(&private_key);
|
||||
//! let private_key = RsaPrivateKey::new(&mut rng, bits).expect("failed to generate a key");
|
||||
//! let public_key = RsaPublicKey::from(&private_key);
|
||||
//!
|
||||
//! // Encrypt
|
||||
//! let data = b"hello world";
|
||||
@ -30,7 +30,7 @@
|
||||
//!
|
||||
//! Using OAEP.
|
||||
//! ```
|
||||
//! use rsa::{PublicKey, RSAPrivateKey, RSAPublicKey, PaddingScheme};
|
||||
//! use rsa::{PublicKey, RsaPrivateKey, RsaPublicKey, PaddingScheme};
|
||||
//! # /*
|
||||
//! use rand::rngs::OsRng;
|
||||
//! let mut rng = OsRng;
|
||||
@ -39,8 +39,8 @@
|
||||
//! # let mut rng = rand::rngs::StdRng::seed_from_u64(0);
|
||||
//!
|
||||
//! let bits = 2048;
|
||||
//! let private_key = RSAPrivateKey::new(&mut rng, bits).expect("failed to generate a key");
|
||||
//! let public_key = RSAPublicKey::from(&private_key);
|
||||
//! let private_key = RsaPrivateKey::new(&mut rng, bits).expect("failed to generate a key");
|
||||
//! let public_key = RsaPublicKey::from(&private_key);
|
||||
//!
|
||||
//! // Encrypt
|
||||
//! let data = b"hello world";
|
||||
@ -68,8 +68,8 @@
|
||||
//!
|
||||
//! Most modern applications use the newer PKCS#8 format instead (see below).
|
||||
//!
|
||||
//! The following traits can be used to decode/encode [`RSAPrivateKey`] and
|
||||
//! [`RSAPublicKey`] as PKCS#1. Note that [`pkcs1`] is re-exported from the
|
||||
//! The following traits can be used to decode/encode [`RsaPrivateKey`] and
|
||||
//! [`RsaPublicKey`] as PKCS#1. Note that [`pkcs1`] is re-exported from the
|
||||
//! toplevel of the `rsa` crate:
|
||||
//!
|
||||
//! - [`pkcs1::FromRsaPrivateKey`]: decode RSA private keys from PKCS#1
|
||||
@ -83,7 +83,7 @@
|
||||
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
//! # #[cfg(feature = "pem")]
|
||||
//! # {
|
||||
//! use rsa::{RSAPublicKey, pkcs1::FromRsaPublicKey};
|
||||
//! use rsa::{RsaPublicKey, pkcs1::FromRsaPublicKey};
|
||||
//!
|
||||
//! let pem = "-----BEGIN RSA PUBLIC KEY-----
|
||||
//! MIIBCgKCAQEAtsQsUV8QpqrygsY+2+JCQ6Fw8/omM71IM2N/R8pPbzbgOl0p78MZ
|
||||
@ -94,7 +94,7 @@
|
||||
//! cFjqJbE/Xilcvqxt6DirjFCvYeKYl1uHLwIDAQAB
|
||||
//! -----END RSA PUBLIC KEY-----";
|
||||
//!
|
||||
//! let public_key = RSAPublicKey::from_pkcs1_pem(pem)?;
|
||||
//! let public_key = RsaPublicKey::from_pkcs1_pem(pem)?;
|
||||
//! # }
|
||||
//! # Ok(())
|
||||
//! # }
|
||||
@ -112,8 +112,8 @@
|
||||
//! -----BEGIN PRIVATE KEY-----
|
||||
//! ```
|
||||
//!
|
||||
//! The following traits can be used to decode/encode [`RSAPrivateKey`] and
|
||||
//! [`RSAPublicKey`] as PKCS#8. Note that [`pkcs8`] is re-exported from the
|
||||
//! The following traits can be used to decode/encode [`RsaPrivateKey`] and
|
||||
//! [`RsaPublicKey`] as PKCS#8. Note that [`pkcs8`] is re-exported from the
|
||||
//! toplevel of the `rsa` crate:
|
||||
//!
|
||||
//! - [`pkcs8::FromPrivateKey`]: decode private keys from PKCS#8
|
||||
@ -127,7 +127,7 @@
|
||||
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
//! # #[cfg(feature = "pem")]
|
||||
//! # {
|
||||
//! use rsa::{RSAPublicKey, pkcs8::FromPublicKey};
|
||||
//! use rsa::{RsaPublicKey, pkcs8::FromPublicKey};
|
||||
//!
|
||||
//! let pem = "-----BEGIN PUBLIC KEY-----
|
||||
//! MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtsQsUV8QpqrygsY+2+JC
|
||||
@ -139,7 +139,7 @@
|
||||
//! LwIDAQAB
|
||||
//! -----END PUBLIC KEY-----";
|
||||
//!
|
||||
//! let public_key = RSAPublicKey::from_public_key_pem(pem)?;
|
||||
//! let public_key = RsaPublicKey::from_public_key_pem(pem)?;
|
||||
//! # }
|
||||
//! # Ok(())
|
||||
//! # }
|
||||
@ -204,7 +204,7 @@ pub use pkcs8;
|
||||
#[cfg(feature = "alloc")]
|
||||
pub use self::hash::Hash;
|
||||
#[cfg(feature = "alloc")]
|
||||
pub use self::key::{PublicKey, PublicKeyParts, RSAPrivateKey, RSAPublicKey};
|
||||
pub use self::key::{PublicKey, PublicKeyParts, RsaPrivateKey, RsaPublicKey};
|
||||
#[cfg(feature = "alloc")]
|
||||
pub use self::padding::PaddingScheme;
|
||||
|
||||
|
@ -72,12 +72,12 @@ impl PaddingScheme {
|
||||
/// use sha1::Sha1;
|
||||
/// use sha2::Sha256;
|
||||
/// use rand::rngs::OsRng;
|
||||
/// use rsa::{BigUint, RSAPublicKey, PaddingScheme, PublicKey};
|
||||
/// use rsa::{BigUint, RsaPublicKey, PaddingScheme, PublicKey};
|
||||
///
|
||||
/// let n = base64::decode("ALHgDoZmBQIx+jTmgeeHW6KsPOrj11f6CvWsiRleJlQpW77AwSZhd21ZDmlTKfaIHBSUxRUsuYNh7E2SHx8rkFVCQA2/gXkZ5GK2IUbzSTio9qXA25MWHvVxjMfKSL8ZAxZyKbrG94FLLszFAFOaiLLY8ECs7g+dXOriYtBwLUJK+lppbd+El+8ZA/zH0bk7vbqph5pIoiWggxwdq3mEz4LnrUln7r6dagSQzYErKewY8GADVpXcq5mfHC1xF2DFBub7bFjMVM5fHq7RK+pG5xjNDiYITbhLYrbVv3X0z75OvN0dY49ITWjM7xyvMWJXVJS7sJlgmCCL6RwWgP8PhcE=").unwrap();
|
||||
/// let e = base64::decode("AQAB").unwrap();
|
||||
///
|
||||
/// let key = RSAPublicKey::new(BigUint::from_bytes_be(&n), BigUint::from_bytes_be(&e)).unwrap();
|
||||
/// let key = RsaPublicKey::new(BigUint::from_bytes_be(&n), BigUint::from_bytes_be(&e)).unwrap();
|
||||
/// let padding = PaddingScheme::new_oaep_with_mgf_hash::<Sha256, Sha1>();
|
||||
/// let encrypted_data = key.encrypt(&mut OsRng, padding, b"secret").unwrap();
|
||||
/// ```
|
||||
@ -99,12 +99,12 @@ impl PaddingScheme {
|
||||
/// use sha1::Sha1;
|
||||
/// use sha2::Sha256;
|
||||
/// use rand::rngs::OsRng;
|
||||
/// use rsa::{BigUint, RSAPublicKey, PaddingScheme, PublicKey};
|
||||
/// use rsa::{BigUint, RsaPublicKey, PaddingScheme, PublicKey};
|
||||
|
||||
/// let n = base64::decode("ALHgDoZmBQIx+jTmgeeHW6KsPOrj11f6CvWsiRleJlQpW77AwSZhd21ZDmlTKfaIHBSUxRUsuYNh7E2SHx8rkFVCQA2/gXkZ5GK2IUbzSTio9qXA25MWHvVxjMfKSL8ZAxZyKbrG94FLLszFAFOaiLLY8ECs7g+dXOriYtBwLUJK+lppbd+El+8ZA/zH0bk7vbqph5pIoiWggxwdq3mEz4LnrUln7r6dagSQzYErKewY8GADVpXcq5mfHC1xF2DFBub7bFjMVM5fHq7RK+pG5xjNDiYITbhLYrbVv3X0z75OvN0dY49ITWjM7xyvMWJXVJS7sJlgmCCL6RwWgP8PhcE=").unwrap();
|
||||
/// let e = base64::decode("AQAB").unwrap();
|
||||
///
|
||||
/// let key = RSAPublicKey::new(BigUint::from_bytes_be(&n), BigUint::from_bytes_be(&e)).unwrap();
|
||||
/// let key = RsaPublicKey::new(BigUint::from_bytes_be(&n), BigUint::from_bytes_be(&e)).unwrap();
|
||||
/// let padding = PaddingScheme::new_oaep::<Sha256>();
|
||||
/// let encrypted_data = key.encrypt(&mut OsRng, padding, b"secret").unwrap();
|
||||
/// ```
|
||||
|
@ -221,7 +221,7 @@ mod tests {
|
||||
use sha1::{Digest, Sha1};
|
||||
use std::time::SystemTime;
|
||||
|
||||
use crate::{Hash, PaddingScheme, PublicKey, PublicKeyParts, RSAPrivateKey, RSAPublicKey};
|
||||
use crate::{Hash, PaddingScheme, PublicKey, PublicKeyParts, RsaPrivateKey, RsaPublicKey};
|
||||
|
||||
#[test]
|
||||
fn test_non_zero_bytes() {
|
||||
@ -238,7 +238,7 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
fn get_private_key() -> RSAPrivateKey {
|
||||
fn get_private_key() -> RsaPrivateKey {
|
||||
// In order to generate new test vectors you'll need the PEM form of this key:
|
||||
// -----BEGIN RSA PRIVATE KEY-----
|
||||
// MIIBOgIBAAJBALKZD0nEffqM1ACuak0bijtqE2QrI/KLADv7l3kK3ppMyCuLKoF0
|
||||
@ -250,7 +250,7 @@ mod tests {
|
||||
// tAboUGBxTDq3ZroNism3DaMIbKPyYrAqhKov1h5V
|
||||
// -----END RSA PRIVATE KEY-----
|
||||
|
||||
RSAPrivateKey::from_components(
|
||||
RsaPrivateKey::from_components(
|
||||
BigUint::from_str_radix("9353930466774385905609975137998169297361893554149986716853295022578535724979677252958524466350471210367835187480748268864277464700638583474144061408845077", 10).unwrap(),
|
||||
BigUint::from_u64(65537).unwrap(),
|
||||
BigUint::from_str_radix("7266398431328116344057699379749222532279343923819063639497049039389899328538543087657733766554155839834519529439851673014800261285757759040931985506583861", 10).unwrap(),
|
||||
@ -305,7 +305,7 @@ mod tests {
|
||||
input = input[0..k - 11].to_vec();
|
||||
}
|
||||
|
||||
let pub_key: RSAPublicKey = priv_key.clone().into();
|
||||
let pub_key: RsaPublicKey = priv_key.clone().into();
|
||||
let ciphertext = encrypt(&mut rng, &pub_key, &input).unwrap();
|
||||
assert_ne!(input, ciphertext);
|
||||
let blind: bool = rng.gen();
|
||||
@ -355,7 +355,7 @@ mod tests {
|
||||
let tests = [[
|
||||
"Test.\n", "a4f3fa6ea93bcdd0c57be020c1193ecbfd6f200a3d95c409769b029578fa0e336ad9a347600e40d3ae823b8c7e6bad88cc07c1d54c3a1523cbbb6d58efc362ae"
|
||||
]];
|
||||
let pub_key: RSAPublicKey = priv_key.into();
|
||||
let pub_key: RsaPublicKey = priv_key.into();
|
||||
|
||||
for test in &tests {
|
||||
let digest = Sha1::digest(test[0].as_bytes()).to_vec();
|
||||
@ -382,7 +382,7 @@ mod tests {
|
||||
.unwrap();
|
||||
assert_eq!(expected_sig, sig);
|
||||
|
||||
let pub_key: RSAPublicKey = priv_key.into();
|
||||
let pub_key: RsaPublicKey = priv_key.into();
|
||||
pub_key
|
||||
.verify(PaddingScheme::new_pkcs1v15_sign(None), msg, &sig)
|
||||
.expect("failed to verify");
|
||||
|
@ -236,7 +236,7 @@ fn emsa_pss_verify(
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::{PaddingScheme, PublicKey, RSAPrivateKey, RSAPublicKey};
|
||||
use crate::{PaddingScheme, PublicKey, RsaPrivateKey, RsaPublicKey};
|
||||
|
||||
use num_bigint::BigUint;
|
||||
use num_traits::{FromPrimitive, Num};
|
||||
@ -244,7 +244,7 @@ mod test {
|
||||
use sha1::{Digest, Sha1};
|
||||
use std::time::SystemTime;
|
||||
|
||||
fn get_private_key() -> RSAPrivateKey {
|
||||
fn get_private_key() -> RsaPrivateKey {
|
||||
// In order to generate new test vectors you'll need the PEM form of this key:
|
||||
// -----BEGIN RSA PRIVATE KEY-----
|
||||
// MIIBOgIBAAJBALKZD0nEffqM1ACuak0bijtqE2QrI/KLADv7l3kK3ppMyCuLKoF0
|
||||
@ -256,7 +256,7 @@ mod test {
|
||||
// tAboUGBxTDq3ZroNism3DaMIbKPyYrAqhKov1h5V
|
||||
// -----END RSA PRIVATE KEY-----
|
||||
|
||||
RSAPrivateKey::from_components(
|
||||
RsaPrivateKey::from_components(
|
||||
BigUint::from_str_radix("9353930466774385905609975137998169297361893554149986716853295022578535724979677252958524466350471210367835187480748268864277464700638583474144061408845077", 10).unwrap(),
|
||||
BigUint::from_u64(65537).unwrap(),
|
||||
BigUint::from_str_radix("7266398431328116344057699379749222532279343923819063639497049039389899328538543087657733766554155839834519529439851673014800261285757759040931985506583861", 10).unwrap(),
|
||||
@ -274,7 +274,7 @@ mod test {
|
||||
let tests = [[
|
||||
"test\n", "6f86f26b14372b2279f79fb6807c49889835c204f71e38249b4c5601462da8ae30f26ffdd9c13f1c75eee172bebe7b7c89f2f1526c722833b9737d6c172a962f"
|
||||
]];
|
||||
let pub_key: RSAPublicKey = priv_key.into();
|
||||
let pub_key: RsaPublicKey = priv_key.into();
|
||||
|
||||
for test in &tests {
|
||||
let digest = Sha1::digest(test[0].as_bytes()).to_vec();
|
||||
|
10
src/raw.rs
10
src/raw.rs
@ -5,7 +5,7 @@ use zeroize::Zeroize;
|
||||
|
||||
use crate::errors::{Error, Result};
|
||||
use crate::internals;
|
||||
use crate::key::{RSAPrivateKey, RSAPublicKey};
|
||||
use crate::key::{RsaPrivateKey, RsaPublicKey};
|
||||
|
||||
pub trait EncryptionPrimitive {
|
||||
/// Do NOT use directly! Only for implementors.
|
||||
@ -22,7 +22,7 @@ pub trait DecryptionPrimitive {
|
||||
) -> Result<Vec<u8>>;
|
||||
}
|
||||
|
||||
impl EncryptionPrimitive for RSAPublicKey {
|
||||
impl EncryptionPrimitive for RsaPublicKey {
|
||||
fn raw_encryption_primitive(&self, plaintext: &[u8], pad_size: usize) -> Result<Vec<u8>> {
|
||||
let mut m = BigUint::from_bytes_be(plaintext);
|
||||
let mut c = internals::encrypt(self, &m);
|
||||
@ -42,13 +42,13 @@ impl EncryptionPrimitive for RSAPublicKey {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> EncryptionPrimitive for &'a RSAPublicKey {
|
||||
impl<'a> EncryptionPrimitive for &'a RsaPublicKey {
|
||||
fn raw_encryption_primitive(&self, plaintext: &[u8], pad_size: usize) -> Result<Vec<u8>> {
|
||||
(*self).raw_encryption_primitive(plaintext, pad_size)
|
||||
}
|
||||
}
|
||||
|
||||
impl DecryptionPrimitive for RSAPrivateKey {
|
||||
impl DecryptionPrimitive for RsaPrivateKey {
|
||||
fn raw_decryption_primitive<R: Rng>(
|
||||
&self,
|
||||
rng: Option<&mut R>,
|
||||
@ -69,7 +69,7 @@ impl DecryptionPrimitive for RSAPrivateKey {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> DecryptionPrimitive for &'a RSAPrivateKey {
|
||||
impl<'a> DecryptionPrimitive for &'a RsaPrivateKey {
|
||||
fn raw_decryption_primitive<R: Rng>(
|
||||
&self,
|
||||
rng: Option<&mut R>,
|
||||
|
@ -5,7 +5,7 @@
|
||||
use hex_literal::hex;
|
||||
use rsa::{
|
||||
pkcs1::{FromRsaPrivateKey, FromRsaPublicKey, ToRsaPrivateKey, ToRsaPublicKey},
|
||||
PublicKeyParts, RSAPrivateKey, RSAPublicKey,
|
||||
PublicKeyParts, RsaPrivateKey, RsaPublicKey,
|
||||
};
|
||||
|
||||
/// RSA-2048 PKCS#1 private key encoded as ASN.1 DER.
|
||||
@ -44,7 +44,7 @@ const RSA_4096_PUB_PEM: &str = include_str!("examples/pkcs1/rsa4096-pub.pem");
|
||||
|
||||
#[test]
|
||||
fn decode_rsa2048_priv_der() {
|
||||
let key = RSAPrivateKey::from_pkcs1_der(RSA_2048_PRIV_DER).unwrap();
|
||||
let key = RsaPrivateKey::from_pkcs1_der(RSA_2048_PRIV_DER).unwrap();
|
||||
|
||||
// Extracted using:
|
||||
// $ openssl asn1parse -in tests/examples/pkcs1/rsa2048-priv.pem
|
||||
@ -57,7 +57,7 @@ fn decode_rsa2048_priv_der() {
|
||||
|
||||
#[test]
|
||||
fn decode_rsa4096_priv_der() {
|
||||
let key = RSAPrivateKey::from_pkcs1_der(RSA_4096_PRIV_DER).unwrap();
|
||||
let key = RsaPrivateKey::from_pkcs1_der(RSA_4096_PRIV_DER).unwrap();
|
||||
|
||||
// Extracted using:
|
||||
// $ openssl asn1parse -in tests/examples/pkcs1/rsa4096-priv.pem
|
||||
@ -70,7 +70,7 @@ fn decode_rsa4096_priv_der() {
|
||||
|
||||
#[test]
|
||||
fn decode_rsa2048_pub_der() {
|
||||
let key = RSAPublicKey::from_pkcs1_der(RSA_2048_PUB_DER).unwrap();
|
||||
let key = RsaPublicKey::from_pkcs1_der(RSA_2048_PUB_DER).unwrap();
|
||||
|
||||
// Extracted using:
|
||||
// $ openssl asn1parse -in tests/examples/pkcs1/rsa2048-pub.pem
|
||||
@ -80,7 +80,7 @@ fn decode_rsa2048_pub_der() {
|
||||
|
||||
#[test]
|
||||
fn decode_rsa4096_pub_der() {
|
||||
let key = RSAPublicKey::from_pkcs1_der(RSA_4096_PUB_DER).unwrap();
|
||||
let key = RsaPublicKey::from_pkcs1_der(RSA_4096_PUB_DER).unwrap();
|
||||
|
||||
// Extracted using:
|
||||
// $ openssl asn1parse -in tests/examples/pkcs1/rsa4096-pub.pem
|
||||
@ -90,28 +90,28 @@ fn decode_rsa4096_pub_der() {
|
||||
|
||||
#[test]
|
||||
fn encode_rsa2048_priv_der() {
|
||||
let key = RSAPrivateKey::from_pkcs1_der(RSA_2048_PRIV_DER).unwrap();
|
||||
let key = RsaPrivateKey::from_pkcs1_der(RSA_2048_PRIV_DER).unwrap();
|
||||
let der = key.to_pkcs1_der().unwrap();
|
||||
assert_eq!(der.as_ref(), RSA_2048_PRIV_DER)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encode_rsa4096_priv_der() {
|
||||
let key = RSAPrivateKey::from_pkcs1_der(RSA_4096_PRIV_DER).unwrap();
|
||||
let key = RsaPrivateKey::from_pkcs1_der(RSA_4096_PRIV_DER).unwrap();
|
||||
let der = key.to_pkcs1_der().unwrap();
|
||||
assert_eq!(der.as_ref(), RSA_4096_PRIV_DER)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encode_rsa2048_pub_der() {
|
||||
let key = RSAPublicKey::from_pkcs1_der(RSA_2048_PUB_DER).unwrap();
|
||||
let key = RsaPublicKey::from_pkcs1_der(RSA_2048_PUB_DER).unwrap();
|
||||
let der = key.to_pkcs1_der().unwrap();
|
||||
assert_eq!(der.as_ref(), RSA_2048_PUB_DER)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encode_rsa4096_pub_der() {
|
||||
let key = RSAPublicKey::from_pkcs1_der(RSA_4096_PUB_DER).unwrap();
|
||||
let key = RsaPublicKey::from_pkcs1_der(RSA_4096_PUB_DER).unwrap();
|
||||
let der = key.to_pkcs1_der().unwrap();
|
||||
assert_eq!(der.as_ref(), RSA_4096_PUB_DER)
|
||||
}
|
||||
@ -119,7 +119,7 @@ fn encode_rsa4096_pub_der() {
|
||||
#[test]
|
||||
#[cfg(feature = "pem")]
|
||||
fn decode_rsa2048_priv_pem() {
|
||||
let key = RSAPrivateKey::from_pkcs1_pem(RSA_2048_PRIV_PEM).unwrap();
|
||||
let key = RsaPrivateKey::from_pkcs1_pem(RSA_2048_PRIV_PEM).unwrap();
|
||||
|
||||
// Extracted using:
|
||||
// $ openssl asn1parse -in tests/examples/pkcs1/rsa2048-priv.pem
|
||||
@ -133,7 +133,7 @@ fn decode_rsa2048_priv_pem() {
|
||||
#[test]
|
||||
#[cfg(feature = "pem")]
|
||||
fn decode_rsa4096_priv_pem() {
|
||||
let key = RSAPrivateKey::from_pkcs1_pem(RSA_4096_PRIV_PEM).unwrap();
|
||||
let key = RsaPrivateKey::from_pkcs1_pem(RSA_4096_PRIV_PEM).unwrap();
|
||||
|
||||
// Extracted using:
|
||||
// $ openssl asn1parse -in tests/examples/pkcs1/rsa4096-priv.pem
|
||||
@ -147,7 +147,7 @@ fn decode_rsa4096_priv_pem() {
|
||||
#[test]
|
||||
#[cfg(feature = "pem")]
|
||||
fn decode_rsa2048_pub_pem() {
|
||||
let key = RSAPublicKey::from_pkcs1_pem(RSA_2048_PUB_PEM).unwrap();
|
||||
let key = RsaPublicKey::from_pkcs1_pem(RSA_2048_PUB_PEM).unwrap();
|
||||
|
||||
// Extracted using:
|
||||
// $ openssl asn1parse -in tests/examples/pkcs1/rsa2048-pub.pem
|
||||
@ -158,7 +158,7 @@ fn decode_rsa2048_pub_pem() {
|
||||
#[test]
|
||||
#[cfg(feature = "pem")]
|
||||
fn decode_rsa4096_pub_pem() {
|
||||
let key = RSAPublicKey::from_pkcs1_pem(RSA_4096_PUB_PEM).unwrap();
|
||||
let key = RsaPublicKey::from_pkcs1_pem(RSA_4096_PUB_PEM).unwrap();
|
||||
|
||||
// Extracted using:
|
||||
// $ openssl asn1parse -in tests/examples/pkcs1/rsa4096-pub.pem
|
||||
@ -169,7 +169,7 @@ fn decode_rsa4096_pub_pem() {
|
||||
#[test]
|
||||
#[cfg(feature = "pem")]
|
||||
fn encode_rsa2048_priv_pem() {
|
||||
let key = RSAPrivateKey::from_pkcs1_pem(RSA_2048_PRIV_PEM).unwrap();
|
||||
let key = RsaPrivateKey::from_pkcs1_pem(RSA_2048_PRIV_PEM).unwrap();
|
||||
let pem = key.to_pkcs1_pem().unwrap();
|
||||
assert_eq!(&*pem, RSA_2048_PRIV_PEM)
|
||||
}
|
||||
@ -177,7 +177,7 @@ fn encode_rsa2048_priv_pem() {
|
||||
#[test]
|
||||
#[cfg(feature = "pem")]
|
||||
fn encode_rsa4096_priv_pem() {
|
||||
let key = RSAPrivateKey::from_pkcs1_pem(RSA_4096_PRIV_PEM).unwrap();
|
||||
let key = RsaPrivateKey::from_pkcs1_pem(RSA_4096_PRIV_PEM).unwrap();
|
||||
let pem = key.to_pkcs1_pem().unwrap();
|
||||
assert_eq!(&*pem, RSA_4096_PRIV_PEM)
|
||||
}
|
||||
@ -185,7 +185,7 @@ fn encode_rsa4096_priv_pem() {
|
||||
#[test]
|
||||
#[cfg(feature = "pem")]
|
||||
fn encode_rsa2048_pub_pem() {
|
||||
let key = RSAPublicKey::from_pkcs1_pem(RSA_2048_PUB_PEM).unwrap();
|
||||
let key = RsaPublicKey::from_pkcs1_pem(RSA_2048_PUB_PEM).unwrap();
|
||||
let pem = key.to_pkcs1_pem().unwrap();
|
||||
assert_eq!(&*pem, RSA_2048_PUB_PEM)
|
||||
}
|
||||
@ -193,7 +193,7 @@ fn encode_rsa2048_pub_pem() {
|
||||
#[test]
|
||||
#[cfg(feature = "pem")]
|
||||
fn encode_rsa4096_pub_pem() {
|
||||
let key = RSAPublicKey::from_pkcs1_pem(RSA_4096_PUB_PEM).unwrap();
|
||||
let key = RsaPublicKey::from_pkcs1_pem(RSA_4096_PUB_PEM).unwrap();
|
||||
let pem = key.to_pkcs1_pem().unwrap();
|
||||
assert_eq!(&*pem, RSA_4096_PUB_PEM)
|
||||
}
|
||||
|
@ -19,12 +19,12 @@ const RSA_2048_PUB_PEM: &str = include_str!("examples/pkcs8/rsa2048-pub.pem");
|
||||
use hex_literal::hex;
|
||||
use rsa::{
|
||||
pkcs8::{FromPrivateKey, FromPublicKey, ToPrivateKey, ToPublicKey},
|
||||
PublicKeyParts, RSAPrivateKey, RSAPublicKey,
|
||||
PublicKeyParts, RsaPrivateKey, RsaPublicKey,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn decode_rsa2048_priv_der() {
|
||||
let key = RSAPrivateKey::from_pkcs8_der(RSA_2048_PRIV_DER).unwrap();
|
||||
let key = RsaPrivateKey::from_pkcs8_der(RSA_2048_PRIV_DER).unwrap();
|
||||
|
||||
// Note: matches PKCS#1 test vectors
|
||||
assert_eq!(&key.n().to_bytes_be(), &hex!("B6C42C515F10A6AAF282C63EDBE24243A170F3FA2633BD4833637F47CA4F6F36E03A5D29EFC3191AC80F390D874B39E30F414FCEC1FCA0ED81E547EDC2CD382C76F61C9018973DB9FA537972A7C701F6B77E0982DFC15FC01927EE5E7CD94B4F599FF07013A7C8281BDF22DCBC9AD7CABB7C4311C982F58EDB7213AD4558B332266D743AED8192D1884CADB8B14739A8DADA66DC970806D9C7AC450CB13D0D7C575FB198534FC61BC41BC0F0574E0E0130C7BBBFBDFDC9F6A6E2E3E2AFF1CBEAC89BA57884528D55CFB08327A1E8C89F4E003CF2888E933241D9D695BCBBACDC90B44E3E095FA37058EA25B13F5E295CBEAC6DE838AB8C50AF61E298975B872F"));
|
||||
@ -36,7 +36,7 @@ fn decode_rsa2048_priv_der() {
|
||||
|
||||
#[test]
|
||||
fn decode_rsa2048_pub_der() {
|
||||
let key = RSAPublicKey::from_public_key_der(RSA_2048_PUB_DER).unwrap();
|
||||
let key = RsaPublicKey::from_public_key_der(RSA_2048_PUB_DER).unwrap();
|
||||
|
||||
// Note: matches PKCS#1 test vectors
|
||||
assert_eq!(&key.n().to_bytes_be(), &hex!("B6C42C515F10A6AAF282C63EDBE24243A170F3FA2633BD4833637F47CA4F6F36E03A5D29EFC3191AC80F390D874B39E30F414FCEC1FCA0ED81E547EDC2CD382C76F61C9018973DB9FA537972A7C701F6B77E0982DFC15FC01927EE5E7CD94B4F599FF07013A7C8281BDF22DCBC9AD7CABB7C4311C982F58EDB7213AD4558B332266D743AED8192D1884CADB8B14739A8DADA66DC970806D9C7AC450CB13D0D7C575FB198534FC61BC41BC0F0574E0E0130C7BBBFBDFDC9F6A6E2E3E2AFF1CBEAC89BA57884528D55CFB08327A1E8C89F4E003CF2888E933241D9D695BCBBACDC90B44E3E095FA37058EA25B13F5E295CBEAC6DE838AB8C50AF61E298975B872F"));
|
||||
@ -45,14 +45,14 @@ fn decode_rsa2048_pub_der() {
|
||||
|
||||
#[test]
|
||||
fn encode_rsa2048_priv_der() {
|
||||
let key = RSAPrivateKey::from_pkcs8_der(RSA_2048_PRIV_DER).unwrap();
|
||||
let key = RsaPrivateKey::from_pkcs8_der(RSA_2048_PRIV_DER).unwrap();
|
||||
let der = key.to_pkcs8_der().unwrap();
|
||||
assert_eq!(der.as_ref(), RSA_2048_PRIV_DER)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encode_rsa2048_pub_der() {
|
||||
let key = RSAPublicKey::from_public_key_der(RSA_2048_PUB_DER).unwrap();
|
||||
let key = RsaPublicKey::from_public_key_der(RSA_2048_PUB_DER).unwrap();
|
||||
let der = key.to_public_key_der().unwrap();
|
||||
assert_eq!(der.as_ref(), RSA_2048_PUB_DER)
|
||||
}
|
||||
@ -60,7 +60,7 @@ fn encode_rsa2048_pub_der() {
|
||||
#[test]
|
||||
#[cfg(feature = "pem")]
|
||||
fn decode_rsa2048_priv_pem() {
|
||||
let key = RSAPrivateKey::from_pkcs8_pem(RSA_2048_PRIV_PEM).unwrap();
|
||||
let key = RsaPrivateKey::from_pkcs8_pem(RSA_2048_PRIV_PEM).unwrap();
|
||||
|
||||
// Note: matches PKCS#1 test vectors
|
||||
assert_eq!(&key.n().to_bytes_be(), &hex!("B6C42C515F10A6AAF282C63EDBE24243A170F3FA2633BD4833637F47CA4F6F36E03A5D29EFC3191AC80F390D874B39E30F414FCEC1FCA0ED81E547EDC2CD382C76F61C9018973DB9FA537972A7C701F6B77E0982DFC15FC01927EE5E7CD94B4F599FF07013A7C8281BDF22DCBC9AD7CABB7C4311C982F58EDB7213AD4558B332266D743AED8192D1884CADB8B14739A8DADA66DC970806D9C7AC450CB13D0D7C575FB198534FC61BC41BC0F0574E0E0130C7BBBFBDFDC9F6A6E2E3E2AFF1CBEAC89BA57884528D55CFB08327A1E8C89F4E003CF2888E933241D9D695BCBBACDC90B44E3E095FA37058EA25B13F5E295CBEAC6DE838AB8C50AF61E298975B872F"));
|
||||
@ -73,7 +73,7 @@ fn decode_rsa2048_priv_pem() {
|
||||
#[test]
|
||||
#[cfg(feature = "pem")]
|
||||
fn decode_rsa2048_pub_pem() {
|
||||
let key = RSAPublicKey::from_public_key_pem(RSA_2048_PUB_PEM).unwrap();
|
||||
let key = RsaPublicKey::from_public_key_pem(RSA_2048_PUB_PEM).unwrap();
|
||||
|
||||
// Note: matches PKCS#1 test vectors
|
||||
assert_eq!(&key.n().to_bytes_be(), &hex!("B6C42C515F10A6AAF282C63EDBE24243A170F3FA2633BD4833637F47CA4F6F36E03A5D29EFC3191AC80F390D874B39E30F414FCEC1FCA0ED81E547EDC2CD382C76F61C9018973DB9FA537972A7C701F6B77E0982DFC15FC01927EE5E7CD94B4F599FF07013A7C8281BDF22DCBC9AD7CABB7C4311C982F58EDB7213AD4558B332266D743AED8192D1884CADB8B14739A8DADA66DC970806D9C7AC450CB13D0D7C575FB198534FC61BC41BC0F0574E0E0130C7BBBFBDFDC9F6A6E2E3E2AFF1CBEAC89BA57884528D55CFB08327A1E8C89F4E003CF2888E933241D9D695BCBBACDC90B44E3E095FA37058EA25B13F5E295CBEAC6DE838AB8C50AF61E298975B872F"));
|
||||
@ -83,7 +83,7 @@ fn decode_rsa2048_pub_pem() {
|
||||
#[test]
|
||||
#[cfg(feature = "pem")]
|
||||
fn encode_rsa2048_priv_pem() {
|
||||
let key = RSAPrivateKey::from_pkcs8_pem(RSA_2048_PRIV_PEM).unwrap();
|
||||
let key = RsaPrivateKey::from_pkcs8_pem(RSA_2048_PRIV_PEM).unwrap();
|
||||
let pem = key.to_pkcs8_pem().unwrap();
|
||||
assert_eq!(&*pem, RSA_2048_PRIV_PEM)
|
||||
}
|
||||
@ -91,7 +91,7 @@ fn encode_rsa2048_priv_pem() {
|
||||
#[test]
|
||||
#[cfg(feature = "pem")]
|
||||
fn encode_rsa2048_pub_pem() {
|
||||
let key = RSAPublicKey::from_public_key_pem(RSA_2048_PUB_PEM).unwrap();
|
||||
let key = RsaPublicKey::from_public_key_pem(RSA_2048_PUB_PEM).unwrap();
|
||||
let pem = key.to_public_key_pem().unwrap();
|
||||
assert_eq!(&*pem, RSA_2048_PUB_PEM)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user