Camel case Rsa

This commit is contained in:
Steven Fackler 2016-10-22 10:21:16 -07:00
parent b619c4e885
commit 3c50c74444
4 changed files with 36 additions and 36 deletions

View File

@ -6,7 +6,7 @@ use ffi;
use {cvt, cvt_p}; use {cvt, cvt_p};
use bio::{MemBio, MemBioSlice}; use bio::{MemBio, MemBioSlice};
use dsa::Dsa; use dsa::Dsa;
use rsa::RSA; use rsa::Rsa;
use error::ErrorStack; use error::ErrorStack;
use util::{CallbackState, invoke_passwd_cb}; use util::{CallbackState, invoke_passwd_cb};
@ -18,7 +18,7 @@ unsafe impl Sync for PKey {}
/// Represents a public key, optionally with a private key attached. /// Represents a public key, optionally with a private key attached.
impl PKey { impl PKey {
/// Create a new `PKey` containing an RSA key. /// Create a new `PKey` containing an RSA key.
pub fn from_rsa(rsa: RSA) -> Result<PKey, ErrorStack> { pub fn from_rsa(rsa: Rsa) -> Result<PKey, ErrorStack> {
unsafe { unsafe {
let evp = try!(cvt_p(ffi::EVP_PKEY_new())); let evp = try!(cvt_p(ffi::EVP_PKEY_new()));
let pkey = PKey(evp); let pkey = PKey(evp);
@ -102,7 +102,7 @@ impl PKey {
} }
/// assign RSA key to this pkey /// assign RSA key to this pkey
pub fn set_rsa(&mut self, rsa: &RSA) -> Result<(), ErrorStack> { pub fn set_rsa(&mut self, rsa: &Rsa) -> Result<(), ErrorStack> {
unsafe { unsafe {
// this needs to be a reference as the set1_RSA ups the reference count // this needs to be a reference as the set1_RSA ups the reference count
let rsa_ptr = rsa.as_ptr(); let rsa_ptr = rsa.as_ptr();
@ -112,11 +112,11 @@ impl PKey {
} }
/// Get a reference to the interal RSA key for direct access to the key components /// Get a reference to the interal RSA key for direct access to the key components
pub fn rsa(&self) -> Result<RSA, ErrorStack> { pub fn rsa(&self) -> Result<Rsa, ErrorStack> {
unsafe { unsafe {
let rsa = try!(cvt_p(ffi::EVP_PKEY_get1_RSA(self.0))); let rsa = try!(cvt_p(ffi::EVP_PKEY_get1_RSA(self.0)));
// this is safe as the ffi increments a reference counter to the internal key // this is safe as the ffi increments a reference counter to the internal key
Ok(RSA::from_ptr(rsa)) Ok(Rsa::from_ptr(rsa))
} }
} }

View File

@ -28,9 +28,9 @@ impl Padding {
} }
} }
pub struct RSA(*mut ffi::RSA); pub struct Rsa(*mut ffi::RSA);
impl Drop for RSA { impl Drop for Rsa {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
ffi::RSA_free(self.0); ffi::RSA_free(self.0);
@ -38,12 +38,12 @@ impl Drop for RSA {
} }
} }
impl RSA { impl Rsa {
/// only useful for associating the key material directly with the key, it's safer to use /// only useful for associating the key material directly with the key, it's safer to use
/// the supplied load and save methods for DER formatted keys. /// the supplied load and save methods for DER formatted keys.
pub fn from_public_components(n: BigNum, e: BigNum) -> Result<RSA, ErrorStack> { pub fn from_public_components(n: BigNum, e: BigNum) -> Result<Rsa, ErrorStack> {
unsafe { unsafe {
let rsa = RSA(try!(cvt_p(ffi::RSA_new()))); let rsa = Rsa(try!(cvt_p(ffi::RSA_new())));
try!(cvt(compat::set_key(rsa.0, try!(cvt(compat::set_key(rsa.0,
n.as_ptr(), n.as_ptr(),
e.as_ptr(), e.as_ptr(),
@ -61,9 +61,9 @@ impl RSA {
dp: BigNum, dp: BigNum,
dq: BigNum, dq: BigNum,
qi: BigNum) qi: BigNum)
-> Result<RSA, ErrorStack> { -> Result<Rsa, ErrorStack> {
unsafe { unsafe {
let rsa = RSA(try!(cvt_p(ffi::RSA_new()))); let rsa = Rsa(try!(cvt_p(ffi::RSA_new())));
try!(cvt(compat::set_key(rsa.0, n.as_ptr(), e.as_ptr(), d.as_ptr()))); try!(cvt(compat::set_key(rsa.0, n.as_ptr(), e.as_ptr(), d.as_ptr())));
mem::forget((n, e, d)); mem::forget((n, e, d));
try!(cvt(compat::set_factors(rsa.0, p.as_ptr(), q.as_ptr()))); try!(cvt(compat::set_factors(rsa.0, p.as_ptr(), q.as_ptr())));
@ -75,16 +75,16 @@ impl RSA {
} }
} }
pub unsafe fn from_ptr(rsa: *mut ffi::RSA) -> RSA { pub unsafe fn from_ptr(rsa: *mut ffi::RSA) -> Rsa {
RSA(rsa) Rsa(rsa)
} }
/// Generates a public/private key pair with the specified size. /// Generates a public/private key pair with the specified size.
/// ///
/// The public exponent will be 65537. /// The public exponent will be 65537.
pub fn generate(bits: u32) -> Result<RSA, ErrorStack> { pub fn generate(bits: u32) -> Result<Rsa, ErrorStack> {
unsafe { unsafe {
let rsa = RSA(try!(cvt_p(ffi::RSA_new()))); let rsa = Rsa(try!(cvt_p(ffi::RSA_new())));
let e = try!(BigNum::from_u32(ffi::RSA_F4 as u32)); let e = try!(BigNum::from_u32(ffi::RSA_F4 as u32));
try!(cvt(ffi::RSA_generate_key_ex(rsa.0, bits as c_int, e.as_ptr(), ptr::null_mut()))); try!(cvt(ffi::RSA_generate_key_ex(rsa.0, bits as c_int, e.as_ptr(), ptr::null_mut())));
Ok(rsa) Ok(rsa)
@ -92,19 +92,19 @@ impl RSA {
} }
/// Reads an RSA private key from PEM formatted data. /// Reads an RSA private key from PEM formatted data.
pub fn private_key_from_pem(buf: &[u8]) -> Result<RSA, ErrorStack> { pub fn private_key_from_pem(buf: &[u8]) -> Result<Rsa, ErrorStack> {
let mem_bio = try!(MemBioSlice::new(buf)); let mem_bio = try!(MemBioSlice::new(buf));
unsafe { unsafe {
let rsa = try!(cvt_p(ffi::PEM_read_bio_RSAPrivateKey(mem_bio.as_ptr(), let rsa = try!(cvt_p(ffi::PEM_read_bio_RSAPrivateKey(mem_bio.as_ptr(),
ptr::null_mut(), ptr::null_mut(),
None, None,
ptr::null_mut()))); ptr::null_mut())));
Ok(RSA(rsa)) Ok(Rsa(rsa))
} }
} }
/// Reads an RSA private key from PEM formatted data and supplies a password callback. /// Reads an RSA private key from PEM formatted data and supplies a password callback.
pub fn private_key_from_pem_cb<F>(buf: &[u8], pass_cb: F) -> Result<RSA, ErrorStack> pub fn private_key_from_pem_cb<F>(buf: &[u8], pass_cb: F) -> Result<Rsa, ErrorStack>
where F: FnOnce(&mut [c_char]) -> usize where F: FnOnce(&mut [c_char]) -> usize
{ {
let mut cb = CallbackState::new(pass_cb); let mut cb = CallbackState::new(pass_cb);
@ -116,19 +116,19 @@ impl RSA {
ptr::null_mut(), ptr::null_mut(),
Some(invoke_passwd_cb::<F>), Some(invoke_passwd_cb::<F>),
cb_ptr))); cb_ptr)));
Ok(RSA(rsa)) Ok(Rsa(rsa))
} }
} }
/// Reads an RSA public key from PEM formatted data. /// Reads an RSA public key from PEM formatted data.
pub fn public_key_from_pem(buf: &[u8]) -> Result<RSA, ErrorStack> { pub fn public_key_from_pem(buf: &[u8]) -> Result<Rsa, ErrorStack> {
let mem_bio = try!(MemBioSlice::new(buf)); let mem_bio = try!(MemBioSlice::new(buf));
unsafe { unsafe {
let rsa = try!(cvt_p(ffi::PEM_read_bio_RSA_PUBKEY(mem_bio.as_ptr(), let rsa = try!(cvt_p(ffi::PEM_read_bio_RSA_PUBKEY(mem_bio.as_ptr(),
ptr::null_mut(), ptr::null_mut(),
None, None,
ptr::null_mut()))); ptr::null_mut())));
Ok(RSA(rsa)) Ok(Rsa(rsa))
} }
} }
@ -323,7 +323,7 @@ impl RSA {
} }
} }
impl fmt::Debug for RSA { impl fmt::Debug for Rsa {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "RSA") write!(f, "RSA")
} }
@ -422,7 +422,7 @@ mod test {
pub fn test_password() { pub fn test_password() {
let mut password_queried = false; let mut password_queried = false;
let key = include_bytes!("../test/rsa-encrypted.pem"); let key = include_bytes!("../test/rsa-encrypted.pem");
RSA::private_key_from_pem_cb(key, |password| { Rsa::private_key_from_pem_cb(key, |password| {
password_queried = true; password_queried = true;
password[0] = b'm' as c_char; password[0] = b'm' as c_char;
password[1] = b'y' as c_char; password[1] = b'y' as c_char;
@ -439,7 +439,7 @@ mod test {
#[test] #[test]
pub fn test_public_encrypt_private_decrypt_with_padding() { pub fn test_public_encrypt_private_decrypt_with_padding() {
let key = include_bytes!("../test/rsa.pem.pub"); let key = include_bytes!("../test/rsa.pem.pub");
let public_key = RSA::public_key_from_pem(key).unwrap(); let public_key = Rsa::public_key_from_pem(key).unwrap();
let mut result = vec![0; public_key.size()]; let mut result = vec![0; public_key.size()];
let original_data = b"This is test"; let original_data = b"This is test";
@ -447,7 +447,7 @@ mod test {
assert_eq!(len, 256); assert_eq!(len, 256);
let pkey = include_bytes!("../test/rsa.pem"); let pkey = include_bytes!("../test/rsa.pem");
let private_key = RSA::private_key_from_pem(pkey).unwrap(); let private_key = Rsa::private_key_from_pem(pkey).unwrap();
let mut dec_result = vec![0; private_key.size()]; let mut dec_result = vec![0; private_key.size()];
let len = private_key.private_decrypt(&result, &mut dec_result, Padding::pkcs1()).unwrap(); let len = private_key.private_decrypt(&result, &mut dec_result, Padding::pkcs1()).unwrap();
@ -456,9 +456,9 @@ mod test {
#[test] #[test]
fn test_private_encrypt() { fn test_private_encrypt() {
let k0 = super::RSA::generate(512).unwrap(); let k0 = super::Rsa::generate(512).unwrap();
let k0pkey = k0.public_key_to_pem().unwrap(); let k0pkey = k0.public_key_to_pem().unwrap();
let k1 = super::RSA::public_key_from_pem(&k0pkey).unwrap(); let k1 = super::Rsa::public_key_from_pem(&k0pkey).unwrap();
let msg = vec![0xdeu8, 0xadu8, 0xd0u8, 0x0du8]; let msg = vec![0xdeu8, 0xadu8, 0xd0u8, 0x0du8];
@ -471,9 +471,9 @@ mod test {
#[test] #[test]
fn test_public_encrypt() { fn test_public_encrypt() {
let k0 = super::RSA::generate(512).unwrap(); let k0 = super::Rsa::generate(512).unwrap();
let k0pkey = k0.private_key_to_pem().unwrap(); let k0pkey = k0.private_key_to_pem().unwrap();
let k1 = super::RSA::private_key_from_pem(&k0pkey).unwrap(); let k1 = super::Rsa::private_key_from_pem(&k0pkey).unwrap();
let msg = vec![0xdeu8, 0xadu8, 0xd0u8, 0x0du8]; let msg = vec![0xdeu8, 0xadu8, 0xd0u8, 0x0du8];

View File

@ -210,7 +210,7 @@ mod test {
use hash::MessageDigest; use hash::MessageDigest;
use sign::{Signer, Verifier}; use sign::{Signer, Verifier};
use rsa::RSA; use rsa::Rsa;
use dsa::Dsa; use dsa::Dsa;
use pkey::PKey; use pkey::PKey;
@ -241,7 +241,7 @@ mod test {
#[test] #[test]
fn rsa_sign() { fn rsa_sign() {
let key = include_bytes!("../test/rsa.pem"); let key = include_bytes!("../test/rsa.pem");
let private_key = RSA::private_key_from_pem(key).unwrap(); let private_key = Rsa::private_key_from_pem(key).unwrap();
let pkey = PKey::from_rsa(private_key).unwrap(); let pkey = PKey::from_rsa(private_key).unwrap();
let mut signer = Signer::new(MessageDigest::sha256(), &pkey).unwrap(); let mut signer = Signer::new(MessageDigest::sha256(), &pkey).unwrap();
@ -254,7 +254,7 @@ mod test {
#[test] #[test]
fn rsa_verify_ok() { fn rsa_verify_ok() {
let key = include_bytes!("../test/rsa.pem"); let key = include_bytes!("../test/rsa.pem");
let private_key = RSA::private_key_from_pem(key).unwrap(); let private_key = Rsa::private_key_from_pem(key).unwrap();
let pkey = PKey::from_rsa(private_key).unwrap(); let pkey = PKey::from_rsa(private_key).unwrap();
let mut verifier = Verifier::new(MessageDigest::sha256(), &pkey).unwrap(); let mut verifier = Verifier::new(MessageDigest::sha256(), &pkey).unwrap();
@ -265,7 +265,7 @@ mod test {
#[test] #[test]
fn rsa_verify_invalid() { fn rsa_verify_invalid() {
let key = include_bytes!("../test/rsa.pem"); let key = include_bytes!("../test/rsa.pem");
let private_key = RSA::private_key_from_pem(key).unwrap(); let private_key = Rsa::private_key_from_pem(key).unwrap();
let pkey = PKey::from_rsa(private_key).unwrap(); let pkey = PKey::from_rsa(private_key).unwrap();
let mut verifier = Verifier::new(MessageDigest::sha256(), &pkey).unwrap(); let mut verifier = Verifier::new(MessageDigest::sha256(), &pkey).unwrap();

View File

@ -2,7 +2,7 @@ use serialize::hex::FromHex;
use hash::MessageDigest; use hash::MessageDigest;
use pkey::PKey; use pkey::PKey;
use rsa::RSA; use rsa::Rsa;
use x509::{X509, X509Generator}; use x509::{X509, X509Generator};
use x509::extension::Extension::{KeyUsage, ExtKeyUsage, SubjectAltName, OtherNid, OtherStr}; use x509::extension::Extension::{KeyUsage, ExtKeyUsage, SubjectAltName, OtherNid, OtherStr};
use x509::extension::AltNameOption as SAN; use x509::extension::AltNameOption as SAN;
@ -25,7 +25,7 @@ fn get_generator() -> X509Generator {
} }
fn pkey() -> PKey { fn pkey() -> PKey {
let rsa = RSA::generate(2048).unwrap(); let rsa = Rsa::generate(2048).unwrap();
PKey::from_rsa(rsa).unwrap() PKey::from_rsa(rsa).unwrap()
} }