@@ -18,7 +18,7 @@ use crate::data::py::simple::{
1818 PyAttribute , PyEncryptedAttribute , PyEncryptedPseudonym , PyPseudonym ,
1919} ;
2020#[ cfg( feature = "offline" ) ]
21- use crate :: keys:: py:: types:: { PyAttributeGlobalPublicKey , PyPseudonymGlobalPublicKey } ;
21+ use crate :: keys:: py:: types:: { PyAttributeGlobalPublicKey , PyPseudonymGlobalPublicKey , PyGlobalPublicKeys } ;
2222#[ cfg( all( feature = "offline" , feature = "insecure" ) ) ]
2323use crate :: keys:: py:: types:: { PyAttributeGlobalSecretKey , PyPseudonymGlobalSecretKey } ;
2424use crate :: keys:: py:: PySessionKeys ;
@@ -30,10 +30,7 @@ use crate::keys::py::{
3030use crate :: keys:: { AttributeGlobalPublicKey , PseudonymGlobalPublicKey } ;
3131#[ cfg( all( feature = "offline" , feature = "insecure" ) ) ]
3232use crate :: keys:: { AttributeGlobalSecretKey , PseudonymGlobalSecretKey } ;
33- use crate :: keys:: {
34- AttributeSessionPublicKey , AttributeSessionSecretKey , PseudonymSessionPublicKey ,
35- PseudonymSessionSecretKey , SessionKeys ,
36- } ;
33+ use crate :: keys:: { AttributeSessionPublicKey , AttributeSessionSecretKey , GlobalPublicKeys , PseudonymSessionPublicKey , PseudonymSessionSecretKey , SessionKeys } ;
3734use pyo3:: exceptions:: PyTypeError ;
3835use pyo3:: prelude:: * ;
3936use pyo3:: types:: PyAny ;
@@ -367,6 +364,19 @@ pub fn py_encrypt_global(message: &Bound<PyAny>, public_key: &Bound<PyAny>) -> P
367364 }
368365 }
369366
367+ // Try PEPJSONValue with SessionKeys
368+ #[ cfg( feature = "json" ) ]
369+ if let Ok ( json) = message. extract :: < PyPEPJSONValue > ( ) {
370+ if let Ok ( pk) = public_key. extract :: < PyGlobalPublicKeys > ( ) {
371+ let keys = GlobalPublicKeys {
372+ pseudonym : PseudonymGlobalPublicKey ( * pk. pseudonym . 0 ) ,
373+ attribute : AttributeGlobalPublicKey ( * pk. attribute . 0 )
374+ } ;
375+ let result = encrypt_global ( & json. 0 , & keys, & mut rng) ;
376+ return Ok ( Py :: new ( py, PyEncryptedPEPJSONValue ( result) ) ?. into_any ( ) ) ;
377+ }
378+ }
379+
370380 Err ( PyTypeError :: new_err (
371381 "encrypt_global() requires (unencrypted_type, matching_global_public_key)" ,
372382 ) )
@@ -430,6 +440,18 @@ pub fn py_decrypt_global(
430440 }
431441 }
432442
443+ // Try EncryptedPEPJSONValue with SessionKeys
444+ #[ cfg( feature = "json" ) ]
445+ if let Ok ( ej) = encrypted. extract :: < PyEncryptedPEPJSONValue > ( ) {
446+ if let Ok ( sk) = secret_key. extract :: < PySessionKeys > ( ) {
447+ let keys: SessionKeys = sk. clone ( ) . into ( ) ;
448+ if let Some ( result) = decrypt_global ( & ej. 0 , & keys) {
449+ return Ok ( Py :: new ( py, PyPEPJSONValue ( result) ) ?. into_any ( ) ) ;
450+ }
451+ return Err ( pyo3:: exceptions:: PyValueError :: new_err ( "Decryption failed" ) ) ;
452+ }
453+ }
454+
433455 Err ( PyTypeError :: new_err (
434456 "decrypt_global() requires (encrypted_type, matching_global_secret_key)" ,
435457 ) )
@@ -484,6 +506,16 @@ pub fn py_decrypt_global(
484506 }
485507 }
486508
509+ // Try EncryptedPEPJSONValue with SessionKeys
510+ #[ cfg( feature = "json" ) ]
511+ if let Ok ( ej) = encrypted. extract :: < PyEncryptedPEPJSONValue > ( ) {
512+ if let Ok ( sk) = secret_key. extract :: < PySessionKeys > ( ) {
513+ let keys: SessionKeys = sk. clone ( ) . into ( ) ;
514+ let result = decrypt_global ( & ej. 0 , & keys) ;
515+ return Ok ( Py :: new ( py, PyPEPJSONValue ( result) ) ?. into_any ( ) ) ;
516+ }
517+ }
518+
487519 Err ( PyTypeError :: new_err (
488520 "decrypt_global() requires (encrypted_type, matching_global_secret_key)" ,
489521 ) )
@@ -628,6 +660,32 @@ pub fn py_encrypt_batch(
628660 }
629661 }
630662
663+ // Try PEPJSONValue + SessionKeys
664+ #[ cfg( feature = "json" ) ]
665+ if let Ok ( sk) = key. extract :: < PySessionKeys > ( ) {
666+ if messages[ 0 ] . extract :: < PyPEPJSONValue > ( ) . is_ok ( ) {
667+ let rust_msgs: Vec < _ > = messages
668+ . iter ( )
669+ . map ( |m| {
670+ m. extract :: < PyPEPJSONValue > ( )
671+ . expect ( "type already validated" )
672+ . 0
673+ } )
674+ . collect ( ) ;
675+ let keys: SessionKeys = sk. clone ( ) . into ( ) ;
676+ let encrypted = encrypt_batch ( & rust_msgs, & keys, & mut rng)
677+ . map_err ( |e| pyo3:: exceptions:: PyValueError :: new_err ( format ! ( "{}" , e) ) ) ?;
678+ return Ok ( encrypted
679+ . into_iter ( )
680+ . map ( |e| {
681+ Py :: new ( py, PyEncryptedPEPJSONValue ( e) )
682+ . expect ( "PyO3 allocation failed" )
683+ . into_any ( )
684+ } )
685+ . collect ( ) ) ;
686+ }
687+ }
688+
631689 Err ( PyTypeError :: new_err (
632690 "encrypt_batch() requires list of (Pseudonym|Attribute|LongPseudonym|LongAttribute) and matching key" ,
633691 ) )
@@ -748,6 +806,32 @@ pub fn py_decrypt_batch(
748806 }
749807 }
750808
809+ // Try EncryptedPEPJSONValue + SessionKeys
810+ #[ cfg( feature = "json" ) ]
811+ if let Ok ( sk) = key. extract :: < PySessionKeys > ( ) {
812+ if encrypted[ 0 ] . extract :: < PyEncryptedPEPJSONValue > ( ) . is_ok ( ) {
813+ let rust_encs: Vec < _ > = encrypted
814+ . iter ( )
815+ . map ( |e| {
816+ e. extract :: < PyEncryptedPEPJSONValue > ( )
817+ . expect ( "type already validated" )
818+ . 0
819+ } )
820+ . collect ( ) ;
821+ let keys: SessionKeys = sk. clone ( ) . into ( ) ;
822+ let decrypted = decrypt_batch ( & rust_encs, & keys)
823+ . map_err ( |e| pyo3:: exceptions:: PyValueError :: new_err ( format ! ( "{}" , e) ) ) ?;
824+ return Ok ( decrypted
825+ . into_iter ( )
826+ . map ( |d| {
827+ Py :: new ( py, PyPEPJSONValue ( d) )
828+ . expect ( "PyO3 allocation failed" )
829+ . into_any ( )
830+ } )
831+ . collect ( ) ) ;
832+ }
833+ }
834+
751835 Err ( PyTypeError :: new_err (
752836 "decrypt_batch() requires list of encrypted types and matching key" ,
753837 ) )
@@ -868,6 +952,32 @@ pub fn py_decrypt_batch(
868952 }
869953 }
870954
955+ // Try EncryptedPEPJSONValue + SessionKeys
956+ #[ cfg( feature = "json" ) ]
957+ if let Ok ( sk) = key. extract :: < PySessionKeys > ( ) {
958+ if encrypted[ 0 ] . extract :: < PyEncryptedPEPJSONValue > ( ) . is_ok ( ) {
959+ let rust_encs: Vec < _ > = encrypted
960+ . iter ( )
961+ . map ( |e| {
962+ e. extract :: < PyEncryptedPEPJSONValue > ( )
963+ . expect ( "type already validated" )
964+ . 0
965+ } )
966+ . collect ( ) ;
967+ let keys: SessionKeys = sk. clone ( ) . into ( ) ;
968+ let decrypted = decrypt_batch ( & rust_encs, & keys)
969+ . map_err ( |e| pyo3:: exceptions:: PyValueError :: new_err ( format ! ( "{}" , e) ) ) ?;
970+ return Ok ( decrypted
971+ . into_iter ( )
972+ . map ( |d| {
973+ Py :: new ( py, PyPEPJSONValue ( d) )
974+ . expect ( "PyO3 allocation failed" )
975+ . into_any ( )
976+ } )
977+ . collect ( ) ) ;
978+ }
979+ }
980+
871981 Err ( PyTypeError :: new_err (
872982 "decrypt_batch() requires list of encrypted types and matching key" ,
873983 ) )
0 commit comments