@@ -25,8 +25,9 @@ use crate::compress::GzStreamDecoder;
2525use crate :: config:: TinyEncryptConfig ;
2626use crate :: consts:: {
2727 DATE_TIME_FORMAT ,
28- ENC_AES256_GCM_P256 , ENC_AES256_GCM_P384 , ENC_AES256_GCM_X25519 ,
29- ENC_CHACHA20_POLY1305_P256 , ENC_CHACHA20_POLY1305_P384 , ENC_CHACHA20_POLY1305_X25519 ,
28+ ENC_AES256_GCM_KYBER1204 , ENC_AES256_GCM_P256 , ENC_AES256_GCM_P384 ,
29+ ENC_AES256_GCM_X25519 , ENC_CHACHA20_POLY1305_KYBER1204 , ENC_CHACHA20_POLY1305_P256 ,
30+ ENC_CHACHA20_POLY1305_P384 , ENC_CHACHA20_POLY1305_X25519 ,
3031 SALT_COMMENT , TINY_ENC_CONFIG_FILE , TINY_ENC_FILE_EXT ,
3132} ;
3233use crate :: crypto_cryptor:: { Cryptor , KeyNonce } ;
@@ -451,6 +452,8 @@ pub fn try_decrypt_key(config: &Option<TinyEncryptConfig>,
451452 #[ cfg( feature = "secure-enclave" ) ]
452453 TinyEncryptEnvelopType :: KeyP256 => try_decrypt_se_key_ecdh ( config, envelop) ,
453454 TinyEncryptEnvelopType :: PivRsa => try_decrypt_piv_key_rsa ( config, envelop, pin, slot) ,
455+ #[ cfg( feature = "macos" ) ]
456+ TinyEncryptEnvelopType :: StaticKyber1024 => try_decrypt_key_ecdh_static_kyber1204 ( config, envelop) ,
454457 unknown_type => simple_error ! ( "Unknown or unsupported type: {}" , unknown_type. get_name( ) ) ,
455458 }
456459}
@@ -619,7 +622,42 @@ fn try_decrypt_key_ecdh_static_x25519(config: &Option<TinyEncryptConfig>, envelo
619622 } ;
620623
621624 let shared_secret = opt_result ! (
622- util_keychainstatic:: decrypt_data( & keychain_key, & e_pub_key_bytes) , "Decrypt static x25519 failed: {}" ) ;
625+ util_keychainstatic:: decrypt_x25519_data( & keychain_key, & e_pub_key_bytes) , "Decrypt static x25519 failed: {}" ) ;
626+
627+ let key = util:: simple_kdf ( shared_secret. as_slice ( ) ) ;
628+ let key_nonce = KeyNonce { k : & key, n : & wrap_key. nonce } ;
629+ let decrypted_key = crypto_simple:: decrypt (
630+ cryptor, & key_nonce, & wrap_key. encrypted_data ) ?;
631+ util:: zeroize ( key) ;
632+ util:: zeroize ( shared_secret) ;
633+ Ok ( decrypted_key)
634+ }
635+
636+ #[ cfg( feature = "macos" ) ]
637+ fn try_decrypt_key_ecdh_static_kyber1204 ( config : & Option < TinyEncryptConfig > , envelop : & TinyEncryptEnvelop ) -> XResult < Vec < u8 > > {
638+ let wrap_key = WrapKey :: parse ( & envelop. encrypted_key ) ?;
639+ let cryptor = match wrap_key. header . enc . as_str ( ) {
640+ ENC_AES256_GCM_KYBER1204 => Cryptor :: Aes256Gcm ,
641+ ENC_CHACHA20_POLY1305_KYBER1204 => Cryptor :: ChaCha20Poly1305 ,
642+ _ => return simple_error ! ( "Unsupported header enc: {}" , & wrap_key. header. enc) ,
643+ } ;
644+ let e_pub_key_bytes = wrap_key. header . get_e_pub_key_bytes ( ) ?;
645+ let config = opt_value_result ! ( config, "Tiny encrypt config is not found" ) ;
646+ let config_envelop = opt_value_result ! (
647+ config. find_by_kid( & envelop. kid) , "Cannot find config for: {}" , & envelop. kid) ;
648+ let config_envelop_args = opt_value_result ! ( & config_envelop. args, "No arguments found for: {}" , & envelop. kid) ;
649+ if config_envelop_args. len ( ) != 1 && config_envelop_args. len ( ) != 3 {
650+ return simple_error ! ( "Not enough arguments for: {}" , & envelop. kid) ;
651+ }
652+
653+ let keychain_key = if config_envelop_args. len ( ) == 1 {
654+ KeychainKey :: parse ( & config_envelop_args[ 0 ] ) ?
655+ } else {
656+ KeychainKey :: from ( & config_envelop_args[ 0 ] , & config_envelop_args[ 1 ] , & config_envelop_args[ 2 ] )
657+ } ;
658+
659+ let shared_secret = opt_result ! (
660+ util_keychainstatic:: decrypt_kyber1204_data( & keychain_key, & e_pub_key_bytes) , "Decrypt static kyber1204 failed: {}" ) ;
623661
624662 let key = util:: simple_kdf ( shared_secret. as_slice ( ) ) ;
625663 let key_nonce = KeyNonce { k : & key, n : & wrap_key. nonce } ;
0 commit comments