Skip to content

Commit

Permalink
Adding dp(), dq() and qi() methods to RSA, to get the CRT parameters …
Browse files Browse the repository at this point in the history
…back
  • Loading branch information
P-E-Meunier committed Dec 2, 2017
1 parent 490e8a4 commit fccb2ea
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
6 changes: 6 additions & 0 deletions openssl-sys/src/ossl110.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ extern "C" {
d: *mut *const ::BIGNUM,
);
pub fn RSA_get0_factors(r: *const ::RSA, p: *mut *const ::BIGNUM, q: *mut *const ::BIGNUM);
pub fn RSA_get0_crt_params(
r: *const ::RSA,
dmp1: *mut *const ::BIGNUM,
dmq1: *mut *const ::BIGNUM,
iqmp: *mut *const ::BIGNUM,
) -> c_int;
pub fn RSA_set0_key(
r: *mut ::RSA,
n: *mut ::BIGNUM,
Expand Down
43 changes: 43 additions & 0 deletions openssl/src/rsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,39 @@ impl RsaRef {
}
}
}

pub fn dp(&self) -> Option<&BigNumRef> {
unsafe {
let dp = compat::crt_params(self.as_ptr())[0];
if dp.is_null() {
None
} else {
Some(BigNumRef::from_ptr(dp as *mut _))
}
}
}

pub fn dq(&self) -> Option<&BigNumRef> {
unsafe {
let dq = compat::crt_params(self.as_ptr())[1];
if dq.is_null() {
None
} else {
Some(BigNumRef::from_ptr(dq as *mut _))
}
}
}

pub fn qi(&self) -> Option<&BigNumRef> {
unsafe {
let qi = compat::crt_params(self.as_ptr())[2];
if qi.is_null() {
None
} else {
Some(BigNumRef::from_ptr(qi as *mut _))
}
}
}
}

impl Rsa {
Expand Down Expand Up @@ -348,6 +381,12 @@ mod compat {
[p, q]
}

pub unsafe fn crt_params(r: *const RSA) -> [*const BIGNUM; 3] {
let (mut dp, mut dq, mut qi) = (ptr::null(), ptr::null(), ptr::null());
ffi::RSA_get0_crt_params(r, &mut dp, &mut dq, &mut qi);
[dp, dq, qi]
}

pub unsafe fn set_key(r: *mut RSA, n: *mut BIGNUM, e: *mut BIGNUM, d: *mut BIGNUM) -> c_int {
ffi::RSA_set0_key(r, n, e, d)
}
Expand Down Expand Up @@ -379,6 +418,10 @@ mod compat {
[(*r).p, (*r).q]
}

pub unsafe fn crt_params(r: *const RSA) -> [*const BIGNUM; 3] {
[(*r).dmp1, (*r).dmq1, (*r).iqmp]
}

pub unsafe fn set_key(r: *mut RSA, n: *mut BIGNUM, e: *mut BIGNUM, d: *mut BIGNUM) -> c_int {
(*r).n = n;
(*r).e = e;
Expand Down

0 comments on commit fccb2ea

Please sign in to comment.