Skip to content

Commit

Permalink
Move all cryptography modules into one parent module.
Browse files Browse the repository at this point in the history
  • Loading branch information
gibbz00 committed Dec 11, 2023
1 parent ddac336 commit 99947e9
Show file tree
Hide file tree
Showing 15 changed files with 39 additions and 40 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ impl Integration for AgeIntegration {
type PublicKey = age::x25519::Recipient;
type PrivateKey = age::x25519::Identity;

fn parse_public_key(public_key_str: &str) -> RopsResult<Self::PublicKey> {
fn parse_public_key(public_key_str: &str) -> IntegrationResult<Self::PublicKey> {
public_key_str
.parse()
.map_err(|err: &str| RopsError::PublicKeyParsing(err.to_string()))
.map_err(|err: &str| IntegrationError::PublicKeyParsing(err.to_string()))
}

fn parse_private_key(private_key_str: &str) -> RopsResult<Self::PrivateKey> {
fn parse_private_key(private_key_str: &str) -> IntegrationResult<Self::PrivateKey> {
private_key_str
.parse()
.map_err(|err: &str| RopsError::PrivateKeyParsing(err.to_string()))
.map_err(|err: &str| IntegrationError::PrivateKeyParsing(err.to_string()))
}

fn encrypt_data_key(public_key: &Self::PublicKey, data_key: &DataKey) -> RopsResult<String> {
fn encrypt_data_key(public_key: &Self::PublicKey, data_key: &DataKey) -> IntegrationResult<String> {
let unarmored_buffer = {
// IMPROVEMENT: avoid vec box allocation
let encryptor =
Expand All @@ -51,7 +51,7 @@ impl Integration for AgeIntegration {
Ok(String::from_utf8(armored_buffer)?)
}

fn decrypt_data_key(private_key: &Self::PrivateKey, encrypted_data_key: &str) -> RopsResult<DataKey> {
fn decrypt_data_key(private_key: &Self::PrivateKey, encrypted_data_key: &str) -> IntegrationResult<DataKey> {
let mut unarmored_encrypted_buffer = Vec::with_capacity(Self::APPROX_MAX_ARMORED_DATA_KEY_LENGTH);

ArmoredReader::new(encrypted_data_key.as_bytes()).read_to_end(&mut unarmored_encrypted_buffer)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ pub trait Integration {
type PublicKey: Display;
type PrivateKey;

fn parse_public_key(public_key_str: &str) -> RopsResult<Self::PublicKey>;
fn parse_public_key(public_key_str: &str) -> IntegrationResult<Self::PublicKey>;

fn parse_private_key(private_key_str: &str) -> RopsResult<Self::PrivateKey>;
fn parse_private_key(private_key_str: &str) -> IntegrationResult<Self::PrivateKey>;

fn encrypt_data_key(public_key: &Self::PublicKey, data_key: &DataKey) -> RopsResult<String>;
fn encrypt_data_key(public_key: &Self::PublicKey, data_key: &DataKey) -> IntegrationResult<String>;

fn decrypt_data_key(private_key: &Self::PrivateKey, encrypted_data_key: &str) -> RopsResult<DataKey>;
fn decrypt_data_key(private_key: &Self::PrivateKey, encrypted_data_key: &str) -> IntegrationResult<DataKey>;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use thiserror::Error;

pub type RopsResult<T> = Result<T, RopsError>;
pub type IntegrationResult<T> = Result<T, IntegrationError>;

#[derive(Debug, Error)]
pub enum RopsError {
pub enum IntegrationError {
#[error("{0} integration - encryption error: {1}")]
Encryption(&'static str, String),
#[error("{0} integration - decryption error: {1}")]
Expand All @@ -19,15 +19,15 @@ pub enum RopsError {
}

#[cfg(feature = "age")]
impl From<age::EncryptError> for RopsError {
impl From<age::EncryptError> for IntegrationError {
fn from(encrypt_error: age::EncryptError) -> Self {
use crate::*;
Self::Encryption(AgeIntegration::NAME, encrypt_error.to_string())
}
}

#[cfg(feature = "age")]
impl From<age::DecryptError> for RopsError {
impl From<age::DecryptError> for IntegrationError {
fn from(decrypt_error: age::DecryptError) -> Self {
use crate::*;
Self::Decryption(AgeIntegration::NAME, decrypt_error.to_string())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
mod core;
pub use core::Integration;

mod error;
pub use error::{IntegrationError, IntegrationResult};

#[cfg(feature = "age")]
mod age;
#[cfg(feature = "age")]
Expand Down
File renamed without changes.
20 changes: 20 additions & 0 deletions crates/lib/src/cryptography/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
mod authorization_tag;
pub use authorization_tag::AuthorizationTag;

mod rng_key;
pub use rng_key::RngKey;

mod data_key;
pub use data_key::DataKey;

mod initial_value;
pub use initial_value::InitialValue;

mod encrypted_data;
pub use encrypted_data::EncryptedData;

mod cipher;
pub use cipher::*;

mod integration;
pub use integration::*;
File renamed without changes.
28 changes: 2 additions & 26 deletions crates/lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,9 @@
// Cryptography modules
mod authorization_tag;
pub use authorization_tag::AuthorizationTag;
mod cryptography;
pub use cryptography::*;

mod rng_key;
pub use rng_key::RngKey;

mod data_key;
pub use data_key::DataKey;

mod initial_value;
pub use initial_value::InitialValue;

mod encrypted_data;
pub use encrypted_data::EncryptedData;

mod cipher;
pub use cipher::*;

// Rops file modules
mod rops_file;
pub use rops_file::*;

// Misc
mod error_handling;
pub use error_handling::{RopsError, RopsResult};

mod integration;
pub use integration::*;

mod base64utils;
pub use base64utils::*;

Expand Down

0 comments on commit 99947e9

Please sign in to comment.