@@ -10,7 +10,7 @@ use rsa::Pkcs1v15Encrypt;
1010use rust_util:: { debugging, failure, iff, information, opt_result, simple_error, success, util_size, XResult } ;
1111use rust_util:: util_time:: UnixEpochTime ;
1212
13- use crate :: { crypto_cryptor, crypto_simple, util, util_enc_file, util_env, util_p256 , util_p384 , util_x25519 } ;
13+ use crate :: { crypto_cryptor, crypto_simple, util, util_enc_file, util_env} ;
1414use crate :: compress:: GzStreamEncoder ;
1515use crate :: config:: { TinyEncryptConfig , TinyEncryptConfigEnvelop } ;
1616use crate :: consts:: {
@@ -24,6 +24,7 @@ use crate::spec::{
2424 EncEncryptedMeta , EncMetadata ,
2525 TinyEncryptEnvelop , TinyEncryptEnvelopType , TinyEncryptMeta ,
2626} ;
27+ use crate :: util_ecdh:: { ecdh_p256, ecdh_p384, ecdh_x25519} ;
2728use crate :: util_progress:: Progress ;
2829use crate :: wrap_key:: { WrapKey , WrapKeyHeader } ;
2930
@@ -265,14 +266,14 @@ fn encrypt_envelops(cryptor: Cryptor, key: &[u8], envelops: &[&TinyEncryptConfig
265266 let mut encrypted_envelops = vec ! [ ] ;
266267 for envelop in envelops {
267268 match envelop. r#type {
268- TinyEncryptEnvelopType :: PgpRsa => {
269- encrypted_envelops. push ( encrypt_envelop_pgp ( key, envelop) ?) ;
269+ TinyEncryptEnvelopType :: PgpRsa | TinyEncryptEnvelopType :: PivRsa => {
270+ encrypted_envelops. push ( encrypt_envelop_rsa ( key, envelop) ?) ;
270271 }
271272 TinyEncryptEnvelopType :: PgpX25519 | TinyEncryptEnvelopType :: StaticX25519 => {
272273 encrypted_envelops. push ( encrypt_envelop_ecdh_x25519 ( cryptor, key, envelop) ?) ;
273274 }
274275 TinyEncryptEnvelopType :: PivP256 | TinyEncryptEnvelopType :: KeyP256 => {
275- encrypted_envelops. push ( encrypt_envelop_ecdh ( cryptor, key, envelop) ?) ;
276+ encrypted_envelops. push ( encrypt_envelop_ecdh_p256 ( cryptor, key, envelop) ?) ;
276277 }
277278 TinyEncryptEnvelopType :: PivP384 => {
278279 encrypted_envelops. push ( encrypt_envelop_ecdh_p384 ( cryptor, key, envelop) ?) ;
@@ -283,9 +284,9 @@ fn encrypt_envelops(cryptor: Cryptor, key: &[u8], envelops: &[&TinyEncryptConfig
283284 Ok ( encrypted_envelops)
284285}
285286
286- fn encrypt_envelop_ecdh ( cryptor : Cryptor , key : & [ u8 ] , envelop : & TinyEncryptConfigEnvelop ) -> XResult < TinyEncryptEnvelop > {
287+ fn encrypt_envelop_ecdh_p256 ( cryptor : Cryptor , key : & [ u8 ] , envelop : & TinyEncryptConfigEnvelop ) -> XResult < TinyEncryptEnvelop > {
287288 let public_key_point_hex = & envelop. public_part ;
288- let ( shared_secret, ephemeral_spki) = util_p256 :: compute_p256_shared_secret ( public_key_point_hex) ?;
289+ let ( shared_secret, ephemeral_spki) = ecdh_p256 :: compute_p256_shared_secret ( public_key_point_hex) ?;
289290 let enc_type = match cryptor {
290291 Cryptor :: Aes256Gcm => ENC_AES256_GCM_P256 ,
291292 Cryptor :: ChaCha20Poly1305 => ENC_CHACHA20_POLY1305_P256 ,
@@ -295,7 +296,7 @@ fn encrypt_envelop_ecdh(cryptor: Cryptor, key: &[u8], envelop: &TinyEncryptConfi
295296
296297fn encrypt_envelop_ecdh_p384 ( cryptor : Cryptor , key : & [ u8 ] , envelop : & TinyEncryptConfigEnvelop ) -> XResult < TinyEncryptEnvelop > {
297298 let public_key_point_hex = & envelop. public_part ;
298- let ( shared_secret, ephemeral_spki) = util_p384 :: compute_p384_shared_secret ( public_key_point_hex) ?;
299+ let ( shared_secret, ephemeral_spki) = ecdh_p384 :: compute_p384_shared_secret ( public_key_point_hex) ?;
299300 let enc_type = match cryptor {
300301 Cryptor :: Aes256Gcm => ENC_AES256_GCM_P384 ,
301302 Cryptor :: ChaCha20Poly1305 => ENC_CHACHA20_POLY1305_P384 ,
@@ -305,7 +306,7 @@ fn encrypt_envelop_ecdh_p384(cryptor: Cryptor, key: &[u8], envelop: &TinyEncrypt
305306
306307fn encrypt_envelop_ecdh_x25519 ( cryptor : Cryptor , key : & [ u8 ] , envelop : & TinyEncryptConfigEnvelop ) -> XResult < TinyEncryptEnvelop > {
307308 let public_key_point_hex = & envelop. public_part ;
308- let ( shared_secret, ephemeral_spki) = util_x25519 :: compute_x25519_shared_secret ( public_key_point_hex) ?;
309+ let ( shared_secret, ephemeral_spki) = ecdh_x25519 :: compute_x25519_shared_secret ( public_key_point_hex) ?;
309310 let enc_type = match cryptor {
310311 Cryptor :: Aes256Gcm => ENC_AES256_GCM_X25519 ,
311312 Cryptor :: ChaCha20Poly1305 => ENC_CHACHA20_POLY1305_X25519 ,
@@ -341,10 +342,10 @@ fn encrypt_envelop_shared_secret(cryptor: Cryptor,
341342 } )
342343}
343344
344- fn encrypt_envelop_pgp ( key : & [ u8 ] , envelop : & TinyEncryptConfigEnvelop ) -> XResult < TinyEncryptEnvelop > {
345- let pgp_public_key = opt_result ! ( crypto_rsa:: parse_spki( & envelop. public_part) , "Parse PGP public key failed: {}" ) ;
345+ fn encrypt_envelop_rsa ( key : & [ u8 ] , envelop : & TinyEncryptConfigEnvelop ) -> XResult < TinyEncryptEnvelop > {
346+ let pgp_public_key = opt_result ! ( crypto_rsa:: parse_spki( & envelop. public_part) , "Parse RSA public key failed: {}" ) ;
346347 let mut rng = rand:: thread_rng ( ) ;
347- let encrypted_key = opt_result ! ( pgp_public_key. encrypt( & mut rng, Pkcs1v15Encrypt , key) , "PGP public key encrypt failed: {}" ) ;
348+ let encrypted_key = opt_result ! ( pgp_public_key. encrypt( & mut rng, Pkcs1v15Encrypt , key) , "RSA public key encrypt failed: {}" ) ;
348349 Ok ( TinyEncryptEnvelop {
349350 r#type : envelop. r#type ,
350351 kid : envelop. kid . clone ( ) ,
0 commit comments