Rename RSA*
to Rsa*
.
This commit is contained in:
parent
b0f8ca2532
commit
6609177e63
12
src/rsa.rs
12
src/rsa.rs
@ -24,10 +24,8 @@ use untrusted;
|
||||
mod padding;
|
||||
|
||||
// `RSA_PKCS1_SHA1` is intentionally not exposed.
|
||||
pub use self::padding::RSAEncoding;
|
||||
|
||||
pub use self::padding::{
|
||||
RSA_PKCS1_SHA256, RSA_PKCS1_SHA384, RSA_PKCS1_SHA512, RSA_PSS_SHA256, RSA_PSS_SHA384,
|
||||
Encoding, RSA_PKCS1_SHA256, RSA_PKCS1_SHA384, RSA_PKCS1_SHA512, RSA_PSS_SHA256, RSA_PSS_SHA384,
|
||||
RSA_PSS_SHA512,
|
||||
};
|
||||
|
||||
@ -38,13 +36,13 @@ const PUBLIC_KEY_PUBLIC_MODULUS_MAX_LEN: usize = bigint::MODULUS_MAX_LIMBS * lim
|
||||
const PRIVATE_KEY_PUBLIC_MODULUS_MAX_BITS: bits::BitLength = bits::BitLength::from_usize_bits(4096);
|
||||
|
||||
/// Parameters for RSA verification.
|
||||
pub struct RSAParameters {
|
||||
padding_alg: &'static padding::RSAVerification,
|
||||
pub struct Parameters {
|
||||
padding_alg: &'static padding::Verification,
|
||||
min_bits: bits::BitLength,
|
||||
id: RSAParametersID,
|
||||
id: ParametersId,
|
||||
}
|
||||
|
||||
enum RSAParametersID {
|
||||
enum ParametersId {
|
||||
RSA_PKCS1_2048_8192_SHA1,
|
||||
RSA_PKCS1_2048_8192_SHA256,
|
||||
RSA_PKCS1_2048_8192_SHA384,
|
||||
|
@ -20,7 +20,7 @@ use untrusted;
|
||||
use crate::rand;
|
||||
|
||||
/// Common features of both RSA padding encoding and RSA padding verification.
|
||||
pub trait RSAPadding: 'static + Sync + crate::sealed::Sealed {
|
||||
pub trait Padding: 'static + Sync + crate::sealed::Sealed {
|
||||
// The digest algorithm used for digesting the message (and maybe for
|
||||
// other things).
|
||||
fn digest_alg(&self) -> &'static digest::Algorithm;
|
||||
@ -30,7 +30,7 @@ pub trait RSAPadding: 'static + Sync + crate::sealed::Sealed {
|
||||
///
|
||||
/// [RFC 3447 Section 8]: https://tools.ietf.org/html/rfc3447#section-8
|
||||
#[cfg(feature = "use_heap")]
|
||||
pub trait RSAEncoding: RSAPadding {
|
||||
pub trait Encoding: Padding {
|
||||
#[doc(hidden)]
|
||||
fn encode(
|
||||
&self, m_hash: &digest::Digest, m_out: &mut [u8], mod_bits: bits::BitLength,
|
||||
@ -42,7 +42,7 @@ pub trait RSAEncoding: RSAPadding {
|
||||
/// [RFC 3447 Section 8].
|
||||
///
|
||||
/// [RFC 3447 Section 8]: https://tools.ietf.org/html/rfc3447#section-8
|
||||
pub trait RSAVerification: RSAPadding {
|
||||
pub trait Verification: Padding {
|
||||
fn verify(
|
||||
&self, m_hash: &digest::Digest, m: &mut untrusted::Reader, mod_bits: bits::BitLength,
|
||||
) -> Result<(), error::Unspecified>;
|
||||
@ -61,12 +61,12 @@ pub struct PKCS1 {
|
||||
|
||||
impl crate::sealed::Sealed for PKCS1 {}
|
||||
|
||||
impl RSAPadding for PKCS1 {
|
||||
impl Padding for PKCS1 {
|
||||
fn digest_alg(&self) -> &'static digest::Algorithm { self.digest_alg }
|
||||
}
|
||||
|
||||
#[cfg(feature = "use_heap")]
|
||||
impl RSAEncoding for PKCS1 {
|
||||
impl Encoding for PKCS1 {
|
||||
fn encode(
|
||||
&self, m_hash: &digest::Digest, m_out: &mut [u8], _mod_bits: bits::BitLength,
|
||||
_rng: &rand::SecureRandom,
|
||||
@ -76,7 +76,7 @@ impl RSAEncoding for PKCS1 {
|
||||
}
|
||||
}
|
||||
|
||||
impl RSAVerification for PKCS1 {
|
||||
impl Verification for PKCS1 {
|
||||
fn verify(
|
||||
&self, m_hash: &digest::Digest, m: &mut untrusted::Reader, mod_bits: bits::BitLength,
|
||||
) -> Result<(), error::Unspecified> {
|
||||
@ -210,11 +210,11 @@ impl crate::sealed::Sealed for PSS {}
|
||||
// In practice, this is constrained by the maximum digest length.
|
||||
const MAX_SALT_LEN: usize = digest::MAX_OUTPUT_LEN;
|
||||
|
||||
impl RSAPadding for PSS {
|
||||
impl Padding for PSS {
|
||||
fn digest_alg(&self) -> &'static digest::Algorithm { self.digest_alg }
|
||||
}
|
||||
|
||||
impl RSAEncoding for PSS {
|
||||
impl Encoding for PSS {
|
||||
// Implement padding procedure per EMSA-PSS,
|
||||
// https://tools.ietf.org/html/rfc3447#section-9.1.
|
||||
fn encode(
|
||||
@ -283,7 +283,7 @@ impl RSAEncoding for PSS {
|
||||
}
|
||||
}
|
||||
|
||||
impl RSAVerification for PSS {
|
||||
impl Verification for PSS {
|
||||
// RSASSA-PSS-VERIFY from https://tools.ietf.org/html/rfc3447#section-8.1.2
|
||||
// where steps 1, 2(a), and 2(b) have been done for us.
|
||||
fn verify(
|
||||
|
@ -14,7 +14,7 @@
|
||||
|
||||
use super::{
|
||||
bigint::{self, Prime},
|
||||
verification, N,
|
||||
verification, Encoding, N,
|
||||
};
|
||||
/// RSA PKCS#1 1.5 signatures.
|
||||
use crate::{
|
||||
@ -470,8 +470,8 @@ impl KeyPair {
|
||||
/// x86-64, this is done pretty well, but not perfectly. On other
|
||||
/// platforms, it is done less perfectly.
|
||||
pub fn sign(
|
||||
&self, padding_alg: &'static crate::signature::RSAEncoding, rng: &rand::SecureRandom,
|
||||
msg: &[u8], signature: &mut [u8],
|
||||
&self, padding_alg: &'static Encoding, rng: &rand::SecureRandom, msg: &[u8],
|
||||
signature: &mut [u8],
|
||||
) -> Result<(), error::Unspecified> {
|
||||
let mod_bits = self.public_key.n_bits;
|
||||
if signature.len() != mod_bits.as_usize_bytes_rounded_up() {
|
||||
@ -558,7 +558,7 @@ mod tests {
|
||||
const PRIVATE_KEY_DER: &'static [u8] =
|
||||
include_bytes!("signature_rsa_example_private_key.der");
|
||||
let key_bytes_der = untrusted::Input::from(PRIVATE_KEY_DER);
|
||||
let key_pair = signature::RSAKeyPair::from_der(key_bytes_der).unwrap();
|
||||
let key_pair = signature::RsaKeyPair::from_der(key_bytes_der).unwrap();
|
||||
|
||||
// The output buffer is one byte too short.
|
||||
let mut signature = vec![0; key_pair.public_modulus_len() - 1];
|
||||
|
@ -12,7 +12,7 @@
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
use super::{bigint, parse_public_key, RSAParameters, N, PUBLIC_KEY_PUBLIC_MODULUS_MAX_LEN};
|
||||
use super::{bigint, parse_public_key, Parameters, N, PUBLIC_KEY_PUBLIC_MODULUS_MAX_LEN};
|
||||
use crate::{bits, cpu, digest, error, sealed, signature};
|
||||
/// RSA PKCS#1 1.5 signatures.
|
||||
use core;
|
||||
@ -82,7 +82,7 @@ impl Key {
|
||||
pub fn modulus_len(&self) -> usize { self.n_bits.as_usize_bytes_rounded_up() }
|
||||
}
|
||||
|
||||
impl signature::VerificationAlgorithm for RSAParameters {
|
||||
impl signature::VerificationAlgorithm for Parameters {
|
||||
fn verify(
|
||||
&self, public_key: untrusted::Input, msg: untrusted::Input, signature: untrusted::Input,
|
||||
) -> Result<(), error::Unspecified> {
|
||||
@ -91,11 +91,11 @@ impl signature::VerificationAlgorithm for RSAParameters {
|
||||
}
|
||||
}
|
||||
|
||||
impl sealed::Sealed for RSAParameters {}
|
||||
impl sealed::Sealed for Parameters {}
|
||||
|
||||
impl core::fmt::Debug for RSAParameters {
|
||||
impl core::fmt::Debug for Parameters {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
|
||||
use super::RSAParametersID::*;
|
||||
use super::ParametersId::*;
|
||||
// XXX: This doesn't include the padding algorithm nor the size range.
|
||||
write!(
|
||||
f,
|
||||
@ -120,10 +120,10 @@ macro_rules! rsa_params {
|
||||
#[doc=$doc_str]
|
||||
///
|
||||
/// Only available in `use_heap` mode.
|
||||
pub static $VERIFY_ALGORITHM: RSAParameters = RSAParameters {
|
||||
pub static $VERIFY_ALGORITHM: Parameters = Parameters {
|
||||
padding_alg: $PADDING_ALGORITHM,
|
||||
min_bits: bits::BitLength::from_usize_bits($min_bits),
|
||||
id: super::RSAParametersID::$VERIFY_ALGORITHM,
|
||||
id: super::ParametersId::$VERIFY_ALGORITHM,
|
||||
};
|
||||
};
|
||||
}
|
||||
@ -213,14 +213,14 @@ rsa_params!(
|
||||
//
|
||||
// There are a small number of tests that test `verify_rsa` directly, but the
|
||||
// test coverage for this function mostly depends on the test coverage for the
|
||||
// `signature::VerificationAlgorithm` implementation for `RSAParameters`. If we
|
||||
// `signature::VerificationAlgorithm` implementation for `RsaParameters`. If we
|
||||
// change that, test coverage for `verify_rsa()` will need to be reconsidered.
|
||||
// (The NIST test vectors were originally in a form that was optimized for
|
||||
// testing `verify_rsa` directly, but the testing work for RSA PKCS#1
|
||||
// verification was done during the implementation of
|
||||
// `signature::VerificationAlgorithm`, before `verify_rsa` was factored out).
|
||||
pub fn verify_rsa(
|
||||
params: &RSAParameters, (n, e): (untrusted::Input, untrusted::Input), msg: untrusted::Input,
|
||||
params: &Parameters, (n, e): (untrusted::Input, untrusted::Input), msg: untrusted::Input,
|
||||
signature: untrusted::Input,
|
||||
) -> Result<(), error::Unspecified> {
|
||||
cpu::cache_detected_features();
|
||||
@ -228,7 +228,7 @@ pub fn verify_rsa(
|
||||
}
|
||||
|
||||
pub(crate) fn verify_rsa_(
|
||||
params: &RSAParameters, (n, e): (untrusted::Input, untrusted::Input), msg: untrusted::Input,
|
||||
params: &Parameters, (n, e): (untrusted::Input, untrusted::Input), msg: untrusted::Input,
|
||||
signature: untrusted::Input,
|
||||
) -> Result<(), error::Unspecified> {
|
||||
let max_bits = bits::BitLength::from_usize_bytes(PUBLIC_KEY_PUBLIC_MODULUS_MAX_LEN)?;
|
||||
|
@ -200,11 +200,11 @@
|
||||
//! fn sign_and_verify_rsa(private_key_path: &std::path::Path,
|
||||
//! public_key_path: &std::path::Path)
|
||||
//! -> Result<(), MyError> {
|
||||
//! // Create an `RSAKeyPair` from the DER-encoded bytes. This example uses
|
||||
//! // Create an `RsaKeyPair` from the DER-encoded bytes. This example uses
|
||||
//! // a 2048-bit key, but larger keys are also supported.
|
||||
//! let private_key_der = read_file(private_key_path)?;
|
||||
//! let private_key_der = untrusted::Input::from(&private_key_der);
|
||||
//! let key_pair = signature::RSAKeyPair::from_der(private_key_der)
|
||||
//! let key_pair = signature::RsaKeyPair::from_der(private_key_der)
|
||||
//! .map_err(|_| MyError::BadPrivateKey)?;
|
||||
//!
|
||||
//! // Sign the message "hello, world", using PKCS#1 v1.5 padding and the
|
||||
@ -290,7 +290,7 @@ pub use crate::ec::{
|
||||
|
||||
#[cfg(feature = "use_heap")]
|
||||
pub use crate::rsa::{
|
||||
signing::KeyPair as RSAKeyPair,
|
||||
signing::KeyPair as RsaKeyPair,
|
||||
|
||||
verification::{
|
||||
RSA_PKCS1_2048_8192_SHA1, RSA_PKCS1_2048_8192_SHA256, RSA_PKCS1_2048_8192_SHA384,
|
||||
@ -298,8 +298,8 @@ pub use crate::rsa::{
|
||||
RSA_PSS_2048_8192_SHA384, RSA_PSS_2048_8192_SHA512,
|
||||
},
|
||||
|
||||
RSAEncoding,
|
||||
RSAParameters,
|
||||
Encoding as RsaEncoding,
|
||||
Parameters as RsaParameters,
|
||||
|
||||
// `RSA_PKCS1_SHA1` is intentionally not exposed. At a minimum, we'd need
|
||||
// to create test vectors for signing with it, which we don't currently
|
||||
|
@ -47,7 +47,7 @@ fn rsa_from_pkcs8_test() {
|
||||
|
||||
let error = test_case.consume_optional_string("Error");
|
||||
|
||||
match (signature::RSAKeyPair::from_pkcs8(input), error) {
|
||||
match (signature::RsaKeyPair::from_pkcs8(input), error) {
|
||||
(Ok(_), None) => (),
|
||||
(Err(e), None) => panic!("Failed with error \"{}\", but expected to succeed", e),
|
||||
(Ok(_), Some(e)) => panic!("Succeeded, but expected error \"{}\"", e),
|
||||
@ -79,7 +79,7 @@ fn test_signature_rsa_pkcs1_sign() {
|
||||
let result = test_case.consume_string("Result");
|
||||
|
||||
let private_key = untrusted::Input::from(&private_key);
|
||||
let key_pair = signature::RSAKeyPair::from_der(private_key);
|
||||
let key_pair = signature::RsaKeyPair::from_der(private_key);
|
||||
if result == "Fail-Invalid-Key" {
|
||||
assert!(key_pair.is_err());
|
||||
return Ok(());
|
||||
@ -115,7 +115,7 @@ fn test_signature_rsa_pss_sign() {
|
||||
let result = test_case.consume_string("Result");
|
||||
let private_key = test_case.consume_bytes("Key");
|
||||
let private_key = untrusted::Input::from(&private_key);
|
||||
let key_pair = signature::RSAKeyPair::from_der(private_key);
|
||||
let key_pair = signature::RsaKeyPair::from_der(private_key);
|
||||
if key_pair.is_err() && result == "Fail-Invalid-Key" {
|
||||
return Ok(());
|
||||
}
|
||||
@ -136,9 +136,9 @@ fn test_signature_rsa_pss_sign() {
|
||||
#[cfg(feature = "use_heap")]
|
||||
#[test]
|
||||
fn test_rsa_key_pair_traits() {
|
||||
test::compile_time_assert_send::<signature::RSAKeyPair>();
|
||||
test::compile_time_assert_sync::<signature::RSAKeyPair>();
|
||||
test::compile_time_assert_debug::<signature::RSAKeyPair>();
|
||||
test::compile_time_assert_send::<signature::RsaKeyPair>();
|
||||
test::compile_time_assert_sync::<signature::RsaKeyPair>();
|
||||
test::compile_time_assert_debug::<signature::RsaKeyPair>();
|
||||
}
|
||||
|
||||
#[cfg(feature = "use_heap")]
|
||||
|
Loading…
x
Reference in New Issue
Block a user