From 7fe3fabf245a1e65ad8441dc8c00f1b3ab28d70d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Sat, 10 Mar 2018 00:26:20 +0100 Subject: [PATCH] Switches to new type wrapper for RsaPssSaltlen --- openssl/src/pkey.rs | 3 +-- openssl/src/sign.rs | 35 +++++++++++++++++------------------ 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/openssl/src/pkey.rs b/openssl/src/pkey.rs index 76ecef7c..506c6db7 100644 --- a/openssl/src/pkey.rs +++ b/openssl/src/pkey.rs @@ -178,8 +178,7 @@ impl PKeyRef { } } - /// Returns `Some(Oid)` as the type of this key or `None`, if the Oid is supported by this - /// library. + /// Returns the Oid that represents the type of this key. /// /// This corresponds to [`EVP_PKEY_id`]. /// diff --git a/openssl/src/sign.rs b/openssl/src/sign.rs index 488e7291..d4c36cad 100644 --- a/openssl/src/sign.rs +++ b/openssl/src/sign.rs @@ -80,26 +80,25 @@ use ffi::{EVP_MD_CTX_free, EVP_MD_CTX_new}; use ffi::{EVP_MD_CTX_create as EVP_MD_CTX_new, EVP_MD_CTX_destroy as EVP_MD_CTX_free}; /// Salt lengths that must be used with `set_rsa_pss_saltlen`. -pub enum RsaPssSaltlen { - /// The salt length is set to the digest length. - /// Corresponds to the special value `-1`. - DigestLength, - /// The salt length is set to the maximum permissible value. - /// Corresponds to the special value `-2`. - MaximumLength, - /// Sets the salt length to the given value. - Custom(u32) -} +pub struct RsaPssSaltlen(c_int); impl RsaPssSaltlen { - /// Returns the integer representation of `Oid`. + /// Returns the integer representation of `RsaPssSaltlen`. fn as_raw(&self) -> c_int { - match *self { - RsaPssSaltlen::DigestLength => -1, - RsaPssSaltlen::MaximumLength => -2, - RsaPssSaltlen::Custom(val) => val as c_int, - } + self.0 } + + /// Sets the salt length to the given value. + pub fn custom(val: c_int) -> RsaPssSaltlen { + RsaPssSaltlen(val) + } + + /// The salt length is set to the digest length. + /// Corresponds to the special value `-1`. + pub const DIGEST_LENGTH: RsaPssSaltlen = RsaPssSaltlen(-1); + /// The salt length is set to the maximum permissible value. + /// Corresponds to the special value `-2`. + pub const MAXIMUM_LENGTH: RsaPssSaltlen = RsaPssSaltlen(-2); } /// A type which computes cryptographic signatures of data. @@ -657,14 +656,14 @@ mod test { let mut signer = Signer::new(MessageDigest::sha256(), &pkey).unwrap(); signer.set_rsa_padding(Padding::PKCS1_PSS).unwrap(); assert_eq!(signer.rsa_padding().unwrap(), Padding::PKCS1_PSS); - signer.set_rsa_pss_saltlen(RsaPssSaltlen::DigestLength).unwrap(); + signer.set_rsa_pss_saltlen(RsaPssSaltlen::DIGEST_LENGTH).unwrap(); signer.set_rsa_mgf1_md(MessageDigest::sha256()).unwrap(); signer.update(&Vec::from_hex(INPUT).unwrap()).unwrap(); let signature = signer.sign_to_vec().unwrap(); let mut verifier = Verifier::new(MessageDigest::sha256(), &pkey).unwrap(); verifier.set_rsa_padding(Padding::PKCS1_PSS).unwrap(); - verifier.set_rsa_pss_saltlen(RsaPssSaltlen::DigestLength).unwrap(); + verifier.set_rsa_pss_saltlen(RsaPssSaltlen::DIGEST_LENGTH).unwrap(); verifier.set_rsa_mgf1_md(MessageDigest::sha256()).unwrap(); verifier.update(&Vec::from_hex(INPUT).unwrap()).unwrap(); assert!(verifier.verify(&signature).unwrap());