feat: aes_gcm_decrypt
This commit is contained in:
@@ -4,7 +4,6 @@ use std::io::{Read, Write};
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use aes_gcm_stream::Aes256GcmStreamDecryptor;
|
|
||||||
use openpgp_card::crypto_data::Cryptogram;
|
use openpgp_card::crypto_data::Cryptogram;
|
||||||
use openpgp_card::OpenPgp;
|
use openpgp_card::OpenPgp;
|
||||||
use rust_util::{debugging, failure, opt_result, simple_error, success, util_term, XResult};
|
use rust_util::{debugging, failure, opt_result, simple_error, success, util_term, XResult};
|
||||||
@@ -15,6 +14,7 @@ use yubikey::YubiKey;
|
|||||||
|
|
||||||
use crate::{file, util};
|
use crate::{file, util};
|
||||||
use crate::card::get_card;
|
use crate::card::get_card;
|
||||||
|
use crate::crypto::aes_gcm_decrypt;
|
||||||
use crate::spec::{TinyEncryptEnvelop, TinyEncryptEnvelopType, TinyEncryptMeta};
|
use crate::spec::{TinyEncryptEnvelop, TinyEncryptEnvelopType, TinyEncryptMeta};
|
||||||
use crate::util::{decode_base64, decode_base64_url_no_pad, ENC_AES256_GCM_P256, simple_kdf, TINY_ENC_FILE_EXT};
|
use crate::util::{decode_base64, decode_base64_url_no_pad, ENC_AES256_GCM_P256, simple_kdf, TINY_ENC_FILE_EXT};
|
||||||
use crate::wrap_key::WrapKey;
|
use crate::wrap_key::WrapKey;
|
||||||
@@ -104,13 +104,8 @@ fn try_decrypt_key_ecdh(envelop: &TinyEncryptEnvelop, pin: &Option<String>, slot
|
|||||||
slot_id,
|
slot_id,
|
||||||
), "Decrypt piv failed: {}");
|
), "Decrypt piv failed: {}");
|
||||||
let key = simple_kdf(decrypted_shared_secret.as_slice());
|
let key = simple_kdf(decrypted_shared_secret.as_slice());
|
||||||
let key: [u8; 32] = opt_result!(key.as_slice().try_into(), "Invalid envelop: {}");
|
let decrypted_key = aes_gcm_decrypt(&key, &wrap_key.nonce, &wrap_key.encrypted_data)?;
|
||||||
let mut aes256_gcm = Aes256GcmStreamDecryptor::new(key, &wrap_key.nonce);
|
Ok(decrypted_key)
|
||||||
let mut b1 = aes256_gcm.update(&wrap_key.encrypted_data);
|
|
||||||
let b2 = opt_result!(aes256_gcm.finalize(), "Invalid envelop: {}");
|
|
||||||
b1.extend_from_slice(&b2);
|
|
||||||
|
|
||||||
Ok(b1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn try_decrypt_key_pgp(envelop: &TinyEncryptEnvelop, pin: &Option<String>) -> XResult<Vec<u8>> {
|
fn try_decrypt_key_pgp(envelop: &TinyEncryptEnvelop, pin: &Option<String>) -> XResult<Vec<u8>> {
|
||||||
|
|||||||
@@ -1,3 +1,14 @@
|
|||||||
|
use aes_gcm_stream::Aes256GcmStreamDecryptor;
|
||||||
|
use rust_util::{opt_result, XResult};
|
||||||
|
|
||||||
|
pub fn aes_gcm_decrypt(key: &[u8], nonce: &[u8], message: &[u8]) -> XResult<Vec<u8>> {
|
||||||
|
let key: [u8; 32] = opt_result!(key.try_into(), "Invalid envelop: {}");
|
||||||
|
let mut aes256_gcm = Aes256GcmStreamDecryptor::new(key, nonce);
|
||||||
|
let mut b1 = aes256_gcm.update(message);
|
||||||
|
let b2 = opt_result!(aes256_gcm.finalize(), "Invalid envelop: {}");
|
||||||
|
b1.extend_from_slice(&b2);
|
||||||
|
Ok(b1)
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_aes_gcm_01() {
|
fn test_aes_gcm_01() {
|
||||||
|
|||||||
Reference in New Issue
Block a user