Skip to content

Commit

Permalink
Merge pull request #11 from unbekanntes-pferd/features/0.7.0
Browse files Browse the repository at this point in the history
features/0.7.0
  • Loading branch information
unbekanntes-pferd committed Apr 23, 2024
2 parents 35ef90e + 46e9537 commit f403ea9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 28 deletions.
51 changes: 28 additions & 23 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,10 @@ impl DracoonRSACrypto for DracoonCrypto {
/// DracoonCrypto::create_plain_user_keypair(UserKeyPairVersion::RSA4096).unwrap();
/// let enc_4096_keypair = DracoonCrypto::encrypt_private_key(secret, plain_4096_keypair).unwrap();
///
/// let plain_keypair = DracoonCrypto::decrypt_private_key(secret, enc_4096_keypair).unwrap();
/// let plain_keypair = DracoonCrypto::decrypt_keypair(secret, enc_4096_keypair).unwrap();
/// ```
///
fn decrypt_private_key(
fn decrypt_keypair(
secret: &str,
keypair: UserKeyPairContainer,
) -> Result<PlainUserKeyPairContainer, DracoonCryptoError> {
Expand Down Expand Up @@ -234,10 +234,10 @@ impl DracoonRSACrypto for DracoonCrypto {
/// let plain_4096_keypair =
/// DracoonCrypto::create_plain_user_keypair(UserKeyPairVersion::RSA4096).unwrap();
/// let enc_4096_keypair = DracoonCrypto::encrypt_private_key(secret, plain_4096_keypair).unwrap();
/// let plain_private_key = DracoonCrypto::decrypt_private_key_only(secret, &enc_4096_keypair.private_key_container).unwrap();
/// let plain_private_key = DracoonCrypto::decrypt_private_key(secret, &enc_4096_keypair.private_key_container).unwrap();
/// ```
///
fn decrypt_private_key_only(
fn decrypt_private_key(
secret: &str,
private_key: &PrivateKeyContainer,
) -> Result<PrivateKeyContainer, DracoonCryptoError> {
Expand All @@ -252,9 +252,11 @@ impl DracoonRSACrypto for DracoonCrypto {
.flat_map(|buf| std::str::from_utf8(buf))
.collect::<String>();

Ok(PrivateKeyContainer::new(private_key_pem, private_key.version.clone()))

}
Ok(PrivateKeyContainer::new(
private_key_pem,
private_key.version.clone(),
))
}
/// Encrypts a file key used for file encryption using either the public key or a plain keypair
/// container.
/// # Example
Expand Down Expand Up @@ -333,15 +335,15 @@ impl DracoonRSACrypto for DracoonCrypto {
/// let plain_file_key = PlainFileKey::try_new_for_encryption().unwrap();
/// let enc_file_key = DracoonCrypto::encrypt_file_key(plain_file_key, public_key_container).unwrap();

/// let plain_file_key = DracoonCrypto::decrypt_file_key(enc_file_key, &plain_4096_keypair).unwrap();
/// let plain_file_key = DracoonCrypto::decrypt_file_key(enc_file_key, plain_4096_keypair).unwrap();
///
/// ```
///
fn decrypt_file_key(
file_key: FileKey,
keypair: &PlainUserKeyPairContainer,
private_key: impl PrivateKey,
) -> Result<PlainFileKey, DracoonCryptoError> {
let private_key_pem = keypair.private_key_container.private_key.as_bytes();
let private_key_pem = private_key.get_private_key().private_key.as_bytes();
let rsa = Rsa::private_key_from_pem(private_key_pem)
.map_err(|_| DracoonCryptoError::RsaImportFailed)?;

Expand All @@ -354,7 +356,7 @@ impl DracoonRSACrypto for DracoonCrypto {
let mut key_ctx = PkeyCtx::new(&pkey)?;
let mgf1_md = Md::sha256();

let md = match keypair.get_public_key().version {
let md = match private_key.get_private_key().version {
UserKeyPairVersion::RSA2048 => Md::sha1(),
UserKeyPairVersion::RSA4096 => Md::sha256(),
};
Expand Down Expand Up @@ -425,7 +427,10 @@ impl Decrypt for DracoonCrypto {
/// let plain_message = DracoonCrypto::decrypt(&enc_message, plain_file_key);
///
/// ```
fn decrypt(data: &impl AsRef<[u8]>, plain_file_key: PlainFileKey) -> Result<Vec<u8>, DracoonCryptoError> {
fn decrypt(
data: &impl AsRef<[u8]>,
plain_file_key: PlainFileKey,
) -> Result<Vec<u8>, DracoonCryptoError> {
let cipher = Cipher::aes_256_gcm();

let key = base64::decode_block(&plain_file_key.key).map_err(|e| {
Expand All @@ -445,10 +450,11 @@ impl Decrypt for DracoonCrypto {

let aad = b"";

let res = decrypt_aead(cipher, &key, Some(&iv), aad, data.as_ref(), &tag).map_err(|_| {
error!("Cannot decrypt data (bad data?).");
DracoonCryptoError::BadData
})?;
let res =
decrypt_aead(cipher, &key, Some(&iv), aad, data.as_ref(), &tag).map_err(|_| {
error!("Cannot decrypt data (bad data?).");
DracoonCryptoError::BadData
})?;

Ok(res)
}
Expand Down Expand Up @@ -502,7 +508,7 @@ impl<'b> ChunkedEncryption<'b, OpenSslCrypter> for Crypter<'b, OpenSslCrypter> {
count: 0,
plain_file_key,
mode: Mode::Decrypt,
state: std::marker::PhantomData::<Open>
state: std::marker::PhantomData::<Open>,
})
}

Expand Down Expand Up @@ -540,7 +546,7 @@ impl<'b> ChunkedEncryption<'b, OpenSslCrypter> for Crypter<'b, OpenSslCrypter> {
count: 0,
plain_file_key,
mode: Mode::Encrypt,
state: std::marker::PhantomData::<Open>
state: std::marker::PhantomData::<Open>,
})
}

Expand Down Expand Up @@ -658,7 +664,7 @@ mod tests {
);

let plain_4096_keypair =
DracoonCrypto::decrypt_private_key(secret, enc_4096_keypair).expect("Should not fail");
DracoonCrypto::decrypt_keypair(secret, enc_4096_keypair).expect("Should not fail");

assert_eq!(
plain_4096_keypair.private_key_container.private_key,
Expand All @@ -684,7 +690,7 @@ mod tests {
);

let plain_2048_keypair =
DracoonCrypto::decrypt_private_key(secret, enc_2048_keypair).expect("Should not fail");
DracoonCrypto::decrypt_keypair(secret, enc_2048_keypair).expect("Should not fail");

assert_eq!(
plain_2048_keypair.private_key_container.private_key,
Expand Down Expand Up @@ -721,7 +727,7 @@ mod tests {
assert_ne!(key.clone(), enc_file_key.key);
assert_eq!("123456", enc_file_key.iv);

let plain_file_key = DracoonCrypto::decrypt_file_key(enc_file_key, &plain_4096_keypair)
let plain_file_key = DracoonCrypto::decrypt_file_key(enc_file_key, plain_4096_keypair)
.expect("Should not fail");

assert_eq!(key, plain_file_key.key);
Expand Down Expand Up @@ -756,7 +762,7 @@ mod tests {
assert_ne!(key.clone(), enc_file_key.key);
assert_eq!("123456", enc_file_key.iv);

let plain_file_key = DracoonCrypto::decrypt_file_key(enc_file_key, &plain_2048_keypair)
let plain_file_key = DracoonCrypto::decrypt_file_key(enc_file_key, plain_2048_keypair)
.expect("Should not fail");

assert_eq!(key, plain_file_key.key);
Expand Down Expand Up @@ -816,7 +822,6 @@ mod tests {

let mut encrypter = DracoonCrypto::encrypter(&mut buf).expect("Should not fail");


let mut count: usize = 0;
let mut chunks = message.chunks(8);

Expand Down
29 changes: 24 additions & 5 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,12 @@ pub trait PublicKey {
fn get_public_key(&self) -> &PublicKeyContainer;
}

/// Trait to get only the private key container of either a private key or
/// a user keypair container
pub trait PrivateKey {
fn get_private_key(&self) -> &PrivateKeyContainer;
}

/// Returns only the public key container as reference of a plain
/// user keypair container
impl PublicKey for PlainUserKeyPairContainer {
Expand All @@ -297,6 +303,20 @@ impl PublicKey for PublicKeyContainer {
}
}

/// Returns the private key of a user keypair container as reference
impl PrivateKey for PlainUserKeyPairContainer {
fn get_private_key(&self) -> &PrivateKeyContainer {
&self.private_key_container
}
}

/// Returns the private key of a private key container as reference
impl PrivateKey for PrivateKeyContainer {
fn get_private_key(&self) -> &PrivateKeyContainer {
self
}
}

/// Trait representing all functions required for asymmetric encryption
/// - generate a new (plain) keypair
/// - encrypt / decrypt the private key of a keypair
Expand All @@ -306,18 +326,17 @@ pub trait DracoonRSACrypto {
version: UserKeyPairVersion,
) -> Result<PlainUserKeyPairContainer, DracoonCryptoError>;


fn encrypt_private_key(
secret: &str,
plain_keypair: PlainUserKeyPairContainer,
) -> Result<UserKeyPairContainer, DracoonCryptoError>;

fn decrypt_private_key(
fn decrypt_keypair(
secret: &str,
keypair: UserKeyPairContainer,
) -> Result<PlainUserKeyPairContainer, DracoonCryptoError>;

fn decrypt_private_key_only(
fn decrypt_private_key(
secret: &str,
plain_private_key: &PrivateKeyContainer,
) -> Result<PrivateKeyContainer, DracoonCryptoError>;
Expand All @@ -329,7 +348,7 @@ pub trait DracoonRSACrypto {

fn decrypt_file_key(
file_key: FileKey,
keypair: &PlainUserKeyPairContainer,
keypair: impl PrivateKey,
) -> Result<PlainFileKey, DracoonCryptoError>;
}

Expand Down Expand Up @@ -366,7 +385,7 @@ pub struct Crypter<'b, C, State = Open> {
pub count: usize,
pub plain_file_key: PlainFileKey,
pub mode: Mode,
pub state: std::marker::PhantomData<State>
pub state: std::marker::PhantomData<State>,
}

/// Represents methods to return an enrypter over a generic internal C
Expand Down

0 comments on commit f403ea9

Please sign in to comment.