Merge pull request #821 from sfackler/rsa-accessors

Rename and document RSA accessors
This commit is contained in:
Steven Fackler 2018-01-06 18:32:12 -08:00 committed by GitHub
commit fe0f33e32b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 50 additions and 10 deletions

View File

@ -124,6 +124,11 @@ where
} }
} }
/// Returns a reference to the private exponent of the key.
///
/// This corresponds to [`RSA_get0_key`].
///
/// [`RSA_get0_key`]: https://www.openssl.org/docs/man1.1.0/crypto/RSA_get0_key.html
pub fn d(&self) -> &BigNumRef { pub fn d(&self) -> &BigNumRef {
unsafe { unsafe {
let d = compat::key(self.as_ptr())[2]; let d = compat::key(self.as_ptr())[2];
@ -131,6 +136,11 @@ where
} }
} }
/// Returns a reference to the first factor of the exponent of the key.
///
/// This corresponds to [`RSA_get0_factors`].
///
/// [`RSA_get0_factors`]: https://www.openssl.org/docs/man1.1.0/crypto/RSA_get0_key.html
pub fn p(&self) -> Option<&BigNumRef> { pub fn p(&self) -> Option<&BigNumRef> {
unsafe { unsafe {
let p = compat::factors(self.as_ptr())[0]; let p = compat::factors(self.as_ptr())[0];
@ -142,6 +152,11 @@ where
} }
} }
/// Returns a reference to the second factor of the exponent of the key.
///
/// This corresponds to [`RSA_get0_factors`].
///
/// [`RSA_get0_factors`]: https://www.openssl.org/docs/man1.1.0/crypto/RSA_get0_key.html
pub fn q(&self) -> Option<&BigNumRef> { pub fn q(&self) -> Option<&BigNumRef> {
unsafe { unsafe {
let q = compat::factors(self.as_ptr())[1]; let q = compat::factors(self.as_ptr())[1];
@ -153,7 +168,12 @@ where
} }
} }
pub fn dp(&self) -> Option<&BigNumRef> { /// Returns a reference to the first exponent used for CRT calculations.
///
/// This corresponds to [`RSA_get0_crt_params`].
///
/// [`RSA_get0_crt_params`]: https://www.openssl.org/docs/man1.1.0/crypto/RSA_get0_key.html
pub fn dmp1(&self) -> Option<&BigNumRef> {
unsafe { unsafe {
let dp = compat::crt_params(self.as_ptr())[0]; let dp = compat::crt_params(self.as_ptr())[0];
if dp.is_null() { if dp.is_null() {
@ -164,7 +184,12 @@ where
} }
} }
pub fn dq(&self) -> Option<&BigNumRef> { /// Returns a reference to the second exponent used for CRT calculations.
///
/// This corresponds to [`RSA_get0_crt_params`].
///
/// [`RSA_get0_crt_params`]: https://www.openssl.org/docs/man1.1.0/crypto/RSA_get0_key.html
pub fn dmq1(&self) -> Option<&BigNumRef> {
unsafe { unsafe {
let dq = compat::crt_params(self.as_ptr())[1]; let dq = compat::crt_params(self.as_ptr())[1];
if dq.is_null() { if dq.is_null() {
@ -175,7 +200,12 @@ where
} }
} }
pub fn qi(&self) -> Option<&BigNumRef> { /// Returns a reference to the coefficient used for CRT calculations.
///
/// This corresponds to [`RSA_get0_crt_params`].
///
/// [`RSA_get0_crt_params`]: https://www.openssl.org/docs/man1.1.0/crypto/RSA_get0_key.html
pub fn iqmp(&self) -> Option<&BigNumRef> {
unsafe { unsafe {
let qi = compat::crt_params(self.as_ptr())[2]; let qi = compat::crt_params(self.as_ptr())[2];
if qi.is_null() { if qi.is_null() {
@ -291,6 +321,11 @@ where
} }
} }
/// Returns a reference to the modulus of the key.
///
/// This corresponds to [`RSA_get0_key`].
///
/// [`RSA_get0_key`]: https://www.openssl.org/docs/man1.1.0/crypto/RSA_get0_key.html
pub fn n(&self) -> &BigNumRef { pub fn n(&self) -> &BigNumRef {
unsafe { unsafe {
let n = compat::key(self.as_ptr())[0]; let n = compat::key(self.as_ptr())[0];
@ -298,6 +333,11 @@ where
} }
} }
/// Returns a reference to the public exponent of the key.
///
/// This corresponds to [`RSA_get0_key`].
///
/// [`RSA_get0_key`]: https://www.openssl.org/docs/man1.1.0/crypto/RSA_get0_key.html
pub fn e(&self) -> &BigNumRef { pub fn e(&self) -> &BigNumRef {
unsafe { unsafe {
let e = compat::key(self.as_ptr())[1]; let e = compat::key(self.as_ptr())[1];
@ -364,9 +404,9 @@ impl Rsa<Private> {
d: BigNum, d: BigNum,
p: BigNum, p: BigNum,
q: BigNum, q: BigNum,
dp: BigNum, dmp1: BigNum,
dq: BigNum, dmq1: BigNum,
qi: BigNum, iqmp: BigNum,
) -> Result<Rsa<Private>, ErrorStack> { ) -> Result<Rsa<Private>, ErrorStack> {
unsafe { unsafe {
let rsa = Rsa::from_ptr(cvt_p(ffi::RSA_new())?); let rsa = Rsa::from_ptr(cvt_p(ffi::RSA_new())?);
@ -376,11 +416,11 @@ impl Rsa<Private> {
mem::forget((p, q)); mem::forget((p, q));
cvt(compat::set_crt_params( cvt(compat::set_crt_params(
rsa.0, rsa.0,
dp.as_ptr(), dmp1.as_ptr(),
dq.as_ptr(), dmq1.as_ptr(),
qi.as_ptr(), iqmp.as_ptr(),
))?; ))?;
mem::forget((dp, dq, qi)); mem::forget((dmp1, dmq1, iqmp));
Ok(rsa) Ok(rsa)
} }
} }