ironcrypt 0.1.1

Library-first crypto toolkit: Argon2 password hashing, AES-256-GCM / XChaCha20 streaming, RSA/ECC hybrid encryption, and optional CLI/daemon.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
use crate::{
    algorithms::{AsymmetricAlgorithm, SymmetricAlgorithm},
    audit::{AuditEvent, Operation, Outcome},
    config::{DataType, IronCryptConfig},
    ecc_utils,
    encrypt::{EncryptedData, RecipientInfo},
    generate_rsa_keys,
    handle_error::IronCryptError,
    keys::{PrivateKey, PublicKey},
    load_any_private_key, load_any_public_key, save_keys_to_files,
    secrets::SecretStore,
};
#[cfg(feature = "vault")]
use crate::secrets::vault::VaultStore;
#[cfg(feature = "aws")]
use crate::secrets::aws::AwsStore;
#[cfg(feature = "azure")]
use crate::secrets::azure::AzureStore;
#[cfg(feature = "hsm")]
use crate::secrets::hsm::HsmSecretStore;
use aes_gcm::aead::{Aead, KeyInit};
use aes_gcm::{Aes256Gcm, Nonce};
use argon2::password_hash::{PasswordHasher, SaltString};
use argon2::{Algorithm, Argon2, Params, Version};
use base64::engine::general_purpose::STANDARD as base64_standard;
use base64::Engine;
use chacha20poly1305::{XChaCha20Poly1305, XNonce};
use p256::pkcs8::spki::{DecodePublicKey, EncodePublicKey};
use p256::pkcs8::LineEnding;
use rand::rngs::OsRng;
use rand::RngCore;
use rsa::Oaep;
use sha2::{Digest, Sha256};
use std::fs;
use std::path::Path;
use zeroize::Zeroize;
use crate::rsa_utils;

// Helper function to ensure keys exist, creating them if they don't.
fn ensure_keys_exist(
    key_directory: &str,
    key_version: &str,
    config: &IronCryptConfig,
) -> Result<(), IronCryptError> {
    let public_key_path = format!("{}/public_key_{}.pem", key_directory, key_version);
    if Path::new(&public_key_path).exists() {
        return Ok(());
    }

    if !Path::new(key_directory).exists() {
        fs::create_dir_all(key_directory)?;
    }

    let private_key_path = format!("{}/private_key_{}.pem", key_directory, key_version);
    let passphrase = config
        .data_type_config
        .as_ref()
        .and_then(|d| d.get(&DataType::Generic).and_then(|km| km.passphrase.clone()));

    let mut event = AuditEvent::new(Operation::GenerateKey);
    event.key_version = Some(key_version.to_string());

    let generation_result = match config.asymmetric_algorithm {
        AsymmetricAlgorithm::Rsa => {
            event.key_type = Some("RSA".to_string());
            event.key_size = Some(config.rsa_key_size as usize);
            let (priv_key, pub_key) = generate_rsa_keys(config.rsa_key_size)?;
            save_keys_to_files(
                &priv_key,
                &pub_key,
                &private_key_path,
                &public_key_path,
                passphrase.as_deref(),
            )
        }
        AsymmetricAlgorithm::Ecc => {
            event.key_type = Some("ECC".to_string());
            event.key_size = Some(256); // P-256
            let (priv_key, pub_key) = ecc_utils::generate_ecc_keys()?;
            ecc_utils::save_keys_to_files(
                &priv_key,
                &pub_key,
                &private_key_path,
                &public_key_path,
                passphrase.as_deref(),
            )
        }
    };

    if let Err(e) = &generation_result {
        event.outcome = Outcome::Failure;
        event.error_message = Some(e.to_string());
    } else {
        event.outcome = Outcome::Success;
    }
    event.log();

    generation_result
}

/// The main entry point for cryptographic operations with IronCrypt.
pub struct IronCrypt {
    pub config: IronCryptConfig,
    secret_store: Option<Box<dyn SecretStore + Send + Sync>>,
    data_type: DataType,
    key_directory: String,
    key_version: String,
    public_key: PublicKey,
}

impl IronCrypt {
    pub fn sign_audit_log(&self) -> Result<(), IronCryptError> {
        let audit_config = self.config.audit.as_ref().ok_or_else(|| {
            IronCryptError::ConfigurationError("Audit configuration is not set.".to_string())
        })?;

        let signing_key_path = audit_config.signing_key_path.as_ref().ok_or_else(|| {
            IronCryptError::ConfigurationError(
                "Audit log signing key path is not configured.".to_string(),
            )
        })?;

        let log_content = fs::read_to_string(&audit_config.log_path)?;

        // For signing the audit log, we don't assume a standard passphrase.
        // The key should ideally be protected by other means (e.g., file permissions).
        let private_key = load_any_private_key(signing_key_path, None)?;

        let rsa_private_key = match private_key {
            PrivateKey::Rsa(key) => key,
            PrivateKey::Ecc(_) => {
                return Err(IronCryptError::UnsupportedOperation(
                    "Log signing is only supported with RSA keys.".to_string(),
                ))
            }
        };

        let mut hasher = Sha256::new();
        hasher.update(log_content.as_bytes());
        let hash = hasher.finalize();

        let signature = rsa_utils::sign_hash(&rsa_private_key, &hash)?;

        let signature_path = format!("{}.sig", audit_config.log_path);
        fs::write(signature_path, base64_standard.encode(signature))?;

        Ok(())
    }

    pub async fn new(
        mut config: IronCryptConfig,
        data_type: DataType,
    ) -> Result<Self, IronCryptError> {
        // Apply the selected standard's parameters, if not custom.
        if let Some(params) = config.standard.get_params() {
            config.symmetric_algorithm = params.symmetric_algorithm;
            config.asymmetric_algorithm = params.asymmetric_algorithm;
            config.rsa_key_size = params.rsa_key_size;
        }

        let secret_store = if let Some(secrets_config) = &config.secrets {
            match secrets_config.provider.as_str() {
                #[cfg(feature = "vault")]
                "vault" => {
                    let vault_config = secrets_config.vault.as_ref().ok_or_else(|| {
                        IronCryptError::ConfigurationError(
                            "Vault provider selected but no vault config provided".to_string(),
                        )
                    })?;
                    let store = VaultStore::new(vault_config, &vault_config.mount)?;
                    Some(Box::new(store) as Box<dyn SecretStore + Send + Sync>)
                }
                #[cfg(feature = "aws")]
                "aws" => {
                    let aws_config = secrets_config.aws.as_ref().ok_or_else(|| {
                        IronCryptError::ConfigurationError(
                            "AWS provider selected but no AWS config provided".to_string(),
                        )
                    })?;
                    let store = AwsStore::new(aws_config).await?;
                    Some(Box::new(store) as Box<dyn SecretStore + Send + Sync>)
                }
                #[cfg(feature = "azure")]
                "azure" => {
                    let azure_config = secrets_config.azure.as_ref().ok_or_else(|| {
                        IronCryptError::ConfigurationError(
                            "Azure provider selected but no Azure config provided".to_string(),
                        )
                    })?;
                    let store = AzureStore::new(azure_config).await?;
                    Some(Box::new(store) as Box<dyn SecretStore + Send + Sync>)
                }
                #[cfg(feature = "gcp")]
                "google" => {
                    let google_config = secrets_config.google.as_ref().ok_or_else(|| {
                        IronCryptError::ConfigurationError(
                            "Google provider selected but no Google config provided".to_string(),
                        )
                    })?;
                    let store = crate::secrets::google::GoogleStore::new(google_config).await?;
                    Some(Box::new(store) as Box<dyn SecretStore + Send + Sync>)
                }
                #[cfg(feature = "hsm")]
                "hsm" => {
                    let hsm_config = secrets_config.hsm.as_ref().ok_or_else(|| {
                        IronCryptError::ConfigurationError(
                            "HSM provider selected but no HSM config provided".to_string(),
                        )
                    })?;
                    let store = HsmSecretStore::new(hsm_config.clone());
                    Some(Box::new(store) as Box<dyn SecretStore + Send + Sync>)
                }
                other => {
                    return Err(IronCryptError::ConfigurationError(format!(
                        "Unsupported secrets provider: {}",
                        other
                    )))
                }
            }
        } else {
            None
        };

        let (key_directory, key_version) = if let Some(dt_cfg) = &config.data_type_config {
            if let Some(km) = dt_cfg.get(&data_type) {
                (km.key_directory.clone(), km.key_version.clone())
            } else {
                ("keys".to_string(), "v1".to_string())
            }
        } else {
            ("keys".to_string(), "v1".to_string())
        };

        ensure_keys_exist(&key_directory, &key_version, &config)?;
        let public_key_path = format!("{}/public_key_{}.pem", key_directory, key_version);
        let public_key = load_any_public_key(&public_key_path)?;

        Ok(Self {
            config,
            secret_store,
            data_type,
            key_directory,
            key_version,
            public_key,
        })
    }

    #[doc(hidden)]
    pub fn with_store(
        config: IronCryptConfig,
        data_type: DataType,
        secret_store: Box<dyn SecretStore + Send + Sync>,
        key_directory: String,
        key_version: String,
    ) -> Result<Self, IronCryptError> {
        ensure_keys_exist(&key_directory, &key_version, &config)?;
        let public_key_path = format!("{}/public_key_{}.pem", key_directory, key_version);
        let public_key = load_any_public_key(&public_key_path)?;

        Ok(Self {
            config,
            secret_store: Some(secret_store),
            data_type,
            key_directory,
            key_version,
            public_key,
        })
    }

    pub fn encrypt_password(&self, password: &str) -> Result<String, IronCryptError> {
        let argon_cfg = crate::Argon2Config {
            memory_cost: self.config.argon2_memory_cost,
            time_cost: self.config.argon2_time_cost,
            parallelism: self.config.argon2_parallelism,
        };
        crate::password::encrypt(password, &self.public_key, &self.key_version, &argon_cfg)
    }

    pub fn verify_password(
        &self,
        encrypted_json: &str,
        user_input_password: &str,
    ) -> Result<bool, IronCryptError> {
        let private_key_path = format!("{}/private_key_{}.pem", self.key_directory, self.key_version);
        let passphrase = self.get_passphrase()?;
        let private_key = load_any_private_key(&private_key_path, passphrase.as_deref())?;
        crate::password::verify(encrypted_json, user_input_password, &private_key)
    }

    pub async fn store_secret(&self, key: &str, value: &str) -> Result<(), IronCryptError> {
        if let Some(store) = &self.secret_store {
            store.set_secret(key, value).await.map_err(IronCryptError::from)
        } else {
            Err(IronCryptError::ConfigurationError(
                "No secret store configured".to_string(),
            ))
        }
    }

    pub async fn retrieve_secret(&self, key: &str) -> Result<String, IronCryptError> {
        if let Some(store) = &self.secret_store {
            store.get_secret(key).await.map_err(IronCryptError::from)
        } else {
            Err(IronCryptError::ConfigurationError(
                "No secret store configured".to_string(),
            ))
        }
    }

    pub fn encrypt_binary_data(
        &self,
        data: &[u8],
        password: &str,
    ) -> Result<String, IronCryptError> {
        let mut event = AuditEvent::new(Operation::Write);
        event.key_version = Some(self.key_version.to_string());
        event.symmetric_algorithm = Some(self.config.symmetric_algorithm.to_string());

        let result: Result<String, IronCryptError> = (|| {
            let mut pwd_string = password.to_string();
            self.config.password_criteria.validate(&pwd_string)?;

            let password_hash = if !password.is_empty() {
                let argon_cfg = &self.config;
                let params = Params::new(
                    argon_cfg.argon2_memory_cost,
                    argon_cfg.argon2_time_cost,
                    argon_cfg.argon2_parallelism,
                    None,
                )?;
                let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, params);
                let salt = SaltString::generate(&mut OsRng);
                let hash_str = argon2
                    .hash_password(pwd_string.as_bytes(), &salt)?
                    .to_string();
                // Will be sealed after the content nonce is generated.
                Some(hash_str)
            } else {
                None
            };
            pwd_string.zeroize();

            let mut symmetric_key = [0u8; 32];
            OsRng.fill_bytes(&mut symmetric_key);

            let sym_algo = self.config.symmetric_algorithm;
            let nonce_len = match sym_algo {
                SymmetricAlgorithm::Aes256Gcm => 12,
                SymmetricAlgorithm::ChaCha20Poly1305 => 24,
            };
            let mut nonce_bytes = vec![0u8; nonce_len];
            OsRng.fill_bytes(&mut nonce_bytes);

            let sealed_password_hash = match &password_hash {
                Some(hash_str) => Some(crate::encrypt::seal_password_hash(
                    &symmetric_key,
                    &nonce_bytes,
                    hash_str,
                )?),
                None => None,
            };

            let ciphertext = match sym_algo {
                SymmetricAlgorithm::Aes256Gcm => {
                    let cipher = Aes256Gcm::new_from_slice(&symmetric_key)?;
                    cipher.encrypt(Nonce::from_slice(&nonce_bytes), data)?
                }
                SymmetricAlgorithm::ChaCha20Poly1305 => {
                    let cipher = XChaCha20Poly1305::new_from_slice(&symmetric_key)?;
                    cipher.encrypt(XNonce::from_slice(&nonce_bytes), data)?
                }
            };

            let recipient_info = match &self.public_key {
                PublicKey::Rsa(rsa_pub_key) => {
                    let padding = Oaep::new::<Sha256>();
                    let encrypted_symmetric_key =
                        rsa_pub_key.encrypt(&mut OsRng, padding, &symmetric_key)?;
                    RecipientInfo::Rsa {
                        key_version: self.key_version.clone(),
                        encrypted_symmetric_key: base64_standard.encode(&encrypted_symmetric_key),
                    }
                }
                PublicKey::Ecc(ecc_pub_key) => {
                    let kek = ecc_utils::ecies_key_encap(ecc_pub_key, &symmetric_key)?;
                    let ephemeral_public_key_pem = kek
                        .ephemeral_pk
                        .to_public_key_pem(LineEnding::LF)
                        .map_err(|e| IronCryptError::KeySavingError(e.to_string()))?;

                    RecipientInfo::Ecc {
                        key_version: self.key_version.clone(),
                        ephemeral_public_key: base64_standard.encode(ephemeral_public_key_pem),
                        encrypted_symmetric_key: base64_standard.encode(kek.encapsulated_key),
                    }
                }
            };

            let enc_data = EncryptedData {
                symmetric_algorithm: sym_algo,
                recipient_info,
                nonce: base64_standard.encode(&nonce_bytes),
                ciphertext: base64_standard.encode(&ciphertext),
                password_hash: sealed_password_hash,
            };

            symmetric_key.zeroize();
            Ok(serde_json::to_string(&enc_data)?)
        })();

        if let Err(e) = &result {
            event.outcome = Outcome::Failure;
            event.error_message = Some(e.to_string());
        } else {
            event.outcome = Outcome::Success;
        }
        event.log();

        result
    }

    pub fn decrypt_binary_data(
        &self,
        encrypted_json: &str,
        password: &str,
    ) -> Result<Vec<u8>, IronCryptError> {
        let mut event = AuditEvent::new(Operation::Read);

        let result: Result<Vec<u8>, IronCryptError> = (|| {
            let ed: EncryptedData = serde_json::from_str(encrypted_json)?;

            let key_version = match &ed.recipient_info {
                RecipientInfo::Rsa { key_version, .. } => key_version.clone(),
                RecipientInfo::Ecc { key_version, .. } => key_version.clone(),
            };
            event.key_version = Some(key_version.to_string());
            event.symmetric_algorithm = Some(ed.symmetric_algorithm.to_string());

            let private_key_path = format!("{}/private_key_{}.pem", self.key_directory, key_version);
            let passphrase = self.get_passphrase()?;
            let private_key = load_any_private_key(&private_key_path, passphrase.as_deref())?;

            let mut symmetric_key = match (&private_key, &ed.recipient_info) {
                (
                    PrivateKey::Rsa(rsa_priv_key),
                    RecipientInfo::Rsa {
                        encrypted_symmetric_key,
                        ..
                    },
                ) => {
                    let key_bytes = base64_standard.decode(encrypted_symmetric_key)?;
                    rsa_priv_key.decrypt(Oaep::new::<Sha256>(), &key_bytes)?
                }
                (
                    PrivateKey::Ecc(ecc_priv_key),
                    RecipientInfo::Ecc {
                        ephemeral_public_key,
                        encrypted_symmetric_key,
                        ..
                    },
                ) => {
                    let eph_pub_key_pem = base64_standard.decode(ephemeral_public_key)?;
                    let eph_pub_key = p256::PublicKey::from_public_key_pem(
                        &String::from_utf8(eph_pub_key_pem)?,
                    )?;
                    let encapsulated_key = base64_standard.decode(encrypted_symmetric_key)?;

                    ecc_utils::ecies_key_decap(ecc_priv_key, &eph_pub_key, &encapsulated_key)?
                }
                _ => {
                    return Err(IronCryptError::DecryptionError(
                        "Mismatched private key and recipient info type".into(),
                    ))
                }
            };

            let ciphertext = base64_standard.decode(&ed.ciphertext)?;
            let nonce_bytes = base64_standard.decode(&ed.nonce)?;

            // Verify optional password before decrypting the payload to plaintext.
            let password_ok = if let Some(hash_field) = ed.password_hash.as_ref() {
                crate::encrypt::verify_sealed_or_legacy_password_hash(
                    &symmetric_key,
                    &nonce_bytes,
                    hash_field,
                    password,
                )
            } else {
                true
            };

            if !password_ok {
                symmetric_key.zeroize();
                return Err(IronCryptError::DecryptionError(
                    "Invalid password or ciphertext".to_string(),
                ));
            }

            let plaintext_result = match ed.symmetric_algorithm {
                SymmetricAlgorithm::Aes256Gcm => {
                    let cipher = Aes256Gcm::new_from_slice(&symmetric_key)?;
                    cipher.decrypt(Nonce::from_slice(&nonce_bytes), ciphertext.as_ref())
                }
                SymmetricAlgorithm::ChaCha20Poly1305 => {
                    let cipher = XChaCha20Poly1305::new_from_slice(&symmetric_key)?;
                    cipher.decrypt(XNonce::from_slice(&nonce_bytes), ciphertext.as_ref())
                }
            };

            symmetric_key.zeroize();

            plaintext_result.map_err(|_| {
                IronCryptError::DecryptionError("Invalid password or ciphertext".to_string())
            })
        })();

        if let Err(e) = &result {
            event.outcome = Outcome::Failure;
            event.error_message = Some(e.to_string());
        } else {
            event.outcome = Outcome::Success;
        }
        event.log();

        result
    }

    pub fn re_encrypt_data(
        &self,
        encrypted_json: &str,
        new_public_key: &PublicKey,
        new_key_version: &str,
    ) -> Result<String, IronCryptError> {
        let mut event = AuditEvent::new(Operation::Rekey);
        event.key_version = Some(new_key_version.to_string());

        let result: Result<String, IronCryptError> = (|| {
            let mut ed: EncryptedData = serde_json::from_str(encrypted_json)?;

            let old_key_version = match &ed.recipient_info {
                RecipientInfo::Rsa { key_version, .. } => key_version.clone(),
                RecipientInfo::Ecc { key_version, .. } => key_version.clone(),
            };

            let private_key_path =
                format!("{}/private_key_{}.pem", self.key_directory, old_key_version);
            let passphrase = self.get_passphrase()?;
            let old_private_key = load_any_private_key(&private_key_path, passphrase.as_deref())?;

            let mut symmetric_key = match (&old_private_key, &ed.recipient_info) {
                (
                    PrivateKey::Rsa(rsa_priv_key),
                    RecipientInfo::Rsa {
                        encrypted_symmetric_key,
                        ..
                    },
                ) => {
                    let key_bytes = base64_standard.decode(encrypted_symmetric_key)?;
                    rsa_priv_key.decrypt(Oaep::new::<Sha256>(), &key_bytes)?
                }
                (
                    PrivateKey::Ecc(ecc_priv_key),
                    RecipientInfo::Ecc {
                        ephemeral_public_key,
                        encrypted_symmetric_key,
                        ..
                    },
                ) => {
                    let eph_pub_key_pem = base64_standard.decode(ephemeral_public_key)?;
                    let eph_pub_key = p256::PublicKey::from_public_key_pem(
                        &String::from_utf8(eph_pub_key_pem)?,
                    )?;
                    let encapsulated_key = base64_standard.decode(encrypted_symmetric_key)?;
                    ecc_utils::ecies_key_decap(ecc_priv_key, &eph_pub_key, &encapsulated_key)?
                }
                _ => {
                    return Err(IronCryptError::DecryptionError(
                        "Mismatched private key and recipient info type".into(),
                    ))
                }
            };

            let new_recipient_info = match new_public_key {
                PublicKey::Rsa(rsa_pub_key) => {
                    let padding = Oaep::new::<Sha256>();
                    let encrypted_symmetric_key =
                        rsa_pub_key.encrypt(&mut OsRng, padding, &symmetric_key)?;
                    RecipientInfo::Rsa {
                        key_version: new_key_version.to_string(),
                        encrypted_symmetric_key: base64_standard.encode(&encrypted_symmetric_key),
                    }
                }
                PublicKey::Ecc(ecc_pub_key) => {
                    let kek = ecc_utils::ecies_key_encap(ecc_pub_key, &symmetric_key)?;
                    let ephemeral_public_key_pem = kek
                        .ephemeral_pk
                        .to_public_key_pem(LineEnding::LF)
                        .map_err(|e| IronCryptError::KeySavingError(e.to_string()))?;

                    RecipientInfo::Ecc {
                        key_version: new_key_version.to_string(),
                        ephemeral_public_key: base64_standard.encode(ephemeral_public_key_pem),
                        encrypted_symmetric_key: base64_standard.encode(kek.encapsulated_key),
                    }
                }
            };

            symmetric_key.zeroize();

            ed.recipient_info = new_recipient_info;

            Ok(serde_json::to_string(&ed)?)
        })();

        if let Err(e) = &result {
            event.outcome = Outcome::Failure;
            event.error_message = Some(e.to_string());
        } else {
            event.outcome = Outcome::Success;
        }
        event.log();

        result
    }

    pub fn public_key(&self) -> &PublicKey {
        &self.public_key
    }

    pub fn key_version(&self) -> &str {
        &self.key_version
    }

    fn get_passphrase(&self) -> Result<Option<String>, IronCryptError> {
        if let Some(dt_cfg) = &self.config.data_type_config {
            if let Some(km) = dt_cfg.get(&self.data_type) {
                return Ok(km.passphrase.clone());
            }
        }
        Ok(None)
    }

    pub fn sign(&self, data_to_sign: &[u8]) -> Result<String, IronCryptError> {
        let mut event = AuditEvent::new(Operation::Sign);
        event.key_version = Some(self.key_version.to_string());

        let result: Result<String, IronCryptError> = (|| {
            let private_key_path =
                format!("{}/private_key_{}.pem", self.key_directory, self.key_version);
            let passphrase = self.get_passphrase()?;
            let private_key = load_any_private_key(&private_key_path, passphrase.as_deref())?;

            let mut hasher = Sha256::new();
            hasher.update(data_to_sign);
            let hash = hasher.finalize();

            let (signature, algo) = match private_key {
                PrivateKey::Rsa(key) => (
                    rsa_utils::sign_hash_pss(&key, &hash)?,
                    "rsa-pss-sha256",
                ),
                PrivateKey::Ecc(key) => (
                    ecc_utils::sign_hash_ecc(&key, &hash)?,
                    "ecdsa-p256-sha256",
                ),
            };
            event.signature_algorithm = Some(algo.to_string());

            Ok(base64_standard.encode(signature))
        })();

        if let Err(e) = &result {
            event.outcome = Outcome::Failure;
            event.error_message = Some(e.to_string());
        } else {
            event.outcome = Outcome::Success;
        }
        event.log();

        result
    }

    pub fn verify(
        &self,
        data_to_verify: &[u8],
        signature: &str,
    ) -> Result<bool, IronCryptError> {
        let mut event = AuditEvent::new(Operation::Verify);
        event.key_version = Some(self.key_version.to_string());
        event.signature_algorithm = Some(match &self.public_key {
            PublicKey::Rsa(_) => "rsa-pss-sha256".to_string(),
            PublicKey::Ecc(_) => "ecdsa-p256-sha256".to_string(),
        });

        let verification_result = (|| {
            let signature_bytes = base64_standard.decode(signature)?;

            let mut hasher = Sha256::new();
            hasher.update(data_to_verify);
            let hash = hasher.finalize();

            match &self.public_key {
                PublicKey::Rsa(key) => {
                    // Accept PSS (current) and PKCS#1 v1.5 (legacy).
                    rsa_utils::verify_signature(key, &hash, &signature_bytes)
                }
                PublicKey::Ecc(key) => {
                    ecc_utils::verify_signature_ecc(key, &hash, &signature_bytes)
                }
            }
        })();

        match verification_result {
            Ok(_) => {
                event.outcome = Outcome::Success;
                event.log();
                Ok(true)
            }
            Err(IronCryptError::SignatureError(_))
            | Err(IronCryptError::SignatureVerificationFailed(_)) => {
                event.outcome = Outcome::Failure;
                event.error_message = Some("Signature verification failed.".to_string());
                event.log();
                Ok(false)
            }
            Err(e) => {
                event.outcome = Outcome::Failure;
                event.error_message = Some(e.to_string());
                event.log();
                Err(e)
            }
        }
    }
}