@@ -43,6 +43,7 @@ use security_framework_sys::key::{
4343 SecKeyCopyAttributes , SecKeyCopyExternalRepresentation ,
4444 SecKeyCreateSignature , SecKeyCreateRandomKey ,
4545 SecKeyCopyPublicKey ,
46+ SecKeyCreateDecryptedData , SecKeyCreateEncryptedData ,
4647} ;
4748#[ cfg( any( feature = "OSX_10_12" , target_os = "ios" , target_os = "tvos" , target_os = "watchos" , target_os = "visionos" ) ) ]
4849use security_framework_sys:: item:: kSecAttrApplicationLabel;
@@ -195,6 +196,40 @@ impl SecKey {
195196 Some ( unsafe { SecKey :: wrap_under_create_rule ( pub_seckey) } )
196197 }
197198
199+ #[ cfg( any( feature = "OSX_10_12" , target_os = "ios" , target_os = "tvos" , target_os = "watchos" , target_os = "visionos" ) ) ]
200+ /// Encrypts a block of data using a public key and specified algorithm
201+ pub fn encrypt_data ( & self , algorithm : Algorithm , input : & [ u8 ] ) -> Result < Vec < u8 > , CFError > {
202+ let mut error: CFErrorRef = std:: ptr:: null_mut ( ) ;
203+
204+ let output = unsafe {
205+ SecKeyCreateEncryptedData ( self . as_concrete_TypeRef ( ) , algorithm. into ( ) , CFData :: from_buffer ( input) . as_concrete_TypeRef ( ) , & mut error)
206+ } ;
207+
208+ if !error. is_null ( ) {
209+ Err ( unsafe { CFError :: wrap_under_create_rule ( error) } )
210+ } else {
211+ let output = unsafe { CFData :: wrap_under_create_rule ( output) } ;
212+ Ok ( output. to_vec ( ) )
213+ }
214+ }
215+
216+ #[ cfg( any( feature = "OSX_10_12" , target_os = "ios" , target_os = "tvos" , target_os = "watchos" , target_os = "visionos" ) ) ]
217+ /// Decrypts a block of data using a private key and specified algorithm
218+ pub fn decrypt_data ( & self , algorithm : Algorithm , input : & [ u8 ] ) -> Result < Vec < u8 > , CFError > {
219+ let mut error: CFErrorRef = std:: ptr:: null_mut ( ) ;
220+
221+ let output = unsafe {
222+ SecKeyCreateDecryptedData ( self . as_concrete_TypeRef ( ) , algorithm. into ( ) , CFData :: from_buffer ( input) . as_concrete_TypeRef ( ) , & mut error)
223+ } ;
224+
225+ if !error. is_null ( ) {
226+ Err ( unsafe { CFError :: wrap_under_create_rule ( error) } )
227+ } else {
228+ let output = unsafe { CFData :: wrap_under_create_rule ( output) } ;
229+ Ok ( output. to_vec ( ) )
230+ }
231+ }
232+
198233 #[ cfg( any( feature = "OSX_10_12" , target_os = "ios" , target_os = "tvos" , target_os = "watchos" , target_os = "visionos" ) ) ]
199234 /// Creates the cryptographic signature for a block of data using a private
200235 /// key and specified algorithm.
0 commit comments