@@ -11,7 +11,7 @@ pub use scheme::{decrypt, encrypt, iota, keygen, setup, tau};
1111use scheme:: {
1212 iota:: Iota ,
1313 tau:: Tau ,
14- types:: { Ciphertext , MPK , MSK } ,
14+ types:: { Ciphertext , MPK , MSK , USK } ,
1515} ;
1616use serde:: { Deserialize , Serialize } ;
1717use std:: {
@@ -141,3 +141,45 @@ pub unsafe extern "C" fn encrypt_abe4(
141141 let output_json = serde_json:: to_string ( & output) . unwrap ( ) ;
142142 CString :: new ( output_json) . unwrap ( ) . into_raw ( )
143143}
144+
145+ #[ allow( clippy:: missing_safety_doc) ]
146+ #[ unsafe( no_mangle) ]
147+ pub unsafe extern "C" fn decrypt_abe4 (
148+ usk_b64 : * const c_char ,
149+ gid : * const c_char ,
150+ policy_str : * const c_char ,
151+ ct_b64 : * const c_char ,
152+ ) -> * mut c_char {
153+ let usk_b64_cstr = unsafe { CStr :: from_ptr ( usk_b64) } ;
154+ let gid_cstr = unsafe { CStr :: from_ptr ( gid) } ;
155+ let policy_cstr = unsafe { CStr :: from_ptr ( policy_str) } ;
156+ let ct_b64_cstr = unsafe { CStr :: from_ptr ( ct_b64) } ;
157+
158+ let usk_bytes = general_purpose:: STANDARD
159+ . decode ( usk_b64_cstr. to_str ( ) . unwrap ( ) )
160+ . unwrap ( ) ;
161+ let usk: USK = USK :: deserialize_compressed ( & usk_bytes[ ..] ) . unwrap ( ) ;
162+
163+ let policy = Policy :: parse ( policy_cstr. to_str ( ) . unwrap ( ) ) . unwrap ( ) ;
164+ let tau = Tau :: new ( & policy) ;
165+
166+ let user_attrs = usk. get_user_attributes ( ) ;
167+ let iota = Iota :: new ( & user_attrs) ;
168+
169+ let ct_bytes = general_purpose:: STANDARD
170+ . decode ( ct_b64_cstr. to_str ( ) . unwrap ( ) )
171+ . unwrap ( ) ;
172+ let ct: Ciphertext = Ciphertext :: deserialize_compressed ( & ct_bytes[ ..] ) . unwrap ( ) ;
173+
174+ let result = decrypt ( & usk, gid_cstr. to_str ( ) . unwrap ( ) , & iota, & tau, & policy, & ct) ;
175+
176+ match result {
177+ Some ( gt) => {
178+ let mut gt_bytes = Vec :: new ( ) ;
179+ gt. serialize_compressed ( & mut gt_bytes) . unwrap ( ) ;
180+ let gt_b64 = general_purpose:: STANDARD . encode ( & gt_bytes) ;
181+ CString :: new ( gt_b64) . unwrap ( ) . into_raw ( )
182+ }
183+ None => std:: ptr:: null_mut ( ) ,
184+ }
185+ }
0 commit comments