@@ -64,6 +64,7 @@ let cmac_key = ctx.keygen().unwrap();
64
64
//! let valid = ctx.verify(text, &signature).unwrap();
65
65
//! assert!(valid);
66
66
//! ```
67
+ use crate :: bn:: BigNumRef ;
67
68
#[ cfg( not( any( boringssl, awslc) ) ) ]
68
69
use crate :: cipher:: CipherRef ;
69
70
use crate :: error:: ErrorStack ;
@@ -73,6 +74,7 @@ use crate::pkey::{HasPrivate, HasPublic, Id, PKey, PKeyRef, Params, Private};
73
74
use crate :: rsa:: Padding ;
74
75
use crate :: sign:: RsaPssSaltlen ;
75
76
use crate :: { cvt, cvt_p} ;
77
+ use cfg_if:: cfg_if;
76
78
use foreign_types:: { ForeignType , ForeignTypeRef } ;
77
79
#[ cfg( not( any( boringssl, awslc) ) ) ]
78
80
use libc:: c_int;
@@ -544,6 +546,48 @@ impl<T> PkeyCtxRef<T> {
544
546
Ok ( ( ) )
545
547
}
546
548
549
+ /// Sets the RSA keygen bits.
550
+ ///
551
+ /// This is only useful for RSA keys.
552
+ #[ corresponds( EVP_PKEY_CTX_set_rsa_keygen_bits ) ]
553
+ #[ inline]
554
+ pub fn set_rsa_keygen_bits ( & mut self , bits : u32 ) -> Result < ( ) , ErrorStack > {
555
+ unsafe {
556
+ cvt ( ffi:: EVP_PKEY_CTX_set_rsa_keygen_bits (
557
+ self . as_ptr ( ) ,
558
+ bits as i32 ,
559
+ ) ) ?;
560
+ }
561
+
562
+ Ok ( ( ) )
563
+ }
564
+
565
+ /// Sets the RSA keygen public exponent.
566
+ ///
567
+ /// This is only useful for RSA keys.
568
+ #[ corresponds( EVP_PKEY_CTX_set1_rsa_keygen_pubexp ) ]
569
+ #[ inline]
570
+ pub fn set_rsa_keygen_pubexp ( & mut self , pubexp : & BigNumRef ) -> Result < ( ) , ErrorStack > {
571
+ unsafe {
572
+ cfg_if ! {
573
+ if #[ cfg( ossl300) ] {
574
+ cvt( ffi:: EVP_PKEY_CTX_set1_rsa_keygen_pubexp (
575
+ self . as_ptr( ) ,
576
+ pubexp. as_ptr( ) ,
577
+ ) ) ?;
578
+ } else {
579
+ cvt( ffi:: EVP_PKEY_CTX_set_rsa_keygen_pubexp (
580
+ self . as_ptr( ) ,
581
+ // Dupe the BN because the EVP_PKEY_CTX takes ownership of it and will free it.
582
+ cvt_p( ffi:: BN_dup ( pubexp. as_ptr( ) ) ) ?,
583
+ ) ) ?;
584
+ }
585
+ }
586
+ }
587
+
588
+ Ok ( ( ) )
589
+ }
590
+
547
591
/// Sets the RSA PSS salt length.
548
592
///
549
593
/// This is only useful for RSA keys.
@@ -874,6 +918,7 @@ impl<T> PkeyCtxRef<T> {
874
918
#[ cfg( test) ]
875
919
mod test {
876
920
use super :: * ;
921
+ use crate :: bn:: BigNum ;
877
922
#[ cfg( not( any( boringssl, awslc) ) ) ]
878
923
use crate :: cipher:: Cipher ;
879
924
use crate :: ec:: { EcGroup , EcKey } ;
@@ -1057,6 +1102,18 @@ mod test {
1057
1102
assert_eq ! ( params. size( ) , 72 ) ;
1058
1103
}
1059
1104
1105
+ #[ test]
1106
+ fn rsa_keygen ( ) {
1107
+ let pubexp = BigNum :: from_u32 ( 65537 ) . unwrap ( ) ;
1108
+ let mut ctx = PkeyCtx :: new_id ( Id :: RSA ) . unwrap ( ) ;
1109
+ ctx. keygen_init ( ) . unwrap ( ) ;
1110
+ ctx. set_rsa_keygen_pubexp ( & pubexp) . unwrap ( ) ;
1111
+ ctx. set_rsa_keygen_bits ( 2048 ) . unwrap ( ) ;
1112
+ let key = ctx. keygen ( ) . unwrap ( ) ;
1113
+
1114
+ assert_eq ! ( key. bits( ) , 2048 ) ;
1115
+ }
1116
+
1060
1117
#[ test]
1061
1118
#[ cfg( any( ossl110, boringssl, libressl360, awslc) ) ]
1062
1119
fn hkdf ( ) {
0 commit comments