Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
tuta-sudipg committed Oct 9, 2024
1 parent 0b86edf commit 51411ba
Show file tree
Hide file tree
Showing 35 changed files with 919 additions and 1,654 deletions.
4 changes: 4 additions & 0 deletions tuta-sdk/rust/sdk/src/crypto/aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ macro_rules! aes_key {

impl $name {
/// Generate an AES key.
#[must_use]
pub fn generate(randomizer_facade: &RandomizerFacade) -> Self {
let key: [u8; $size] = randomizer_facade.generate_random_array();
Self(key)
}

/// Get the key represented as bytes.
#[must_use]
pub fn as_bytes(&self) -> &[u8; $size] {
&self.0
}
Expand Down Expand Up @@ -154,6 +156,7 @@ impl Clone for Iv {

impl Iv {
/// Generate an initialisation vector.
#[must_use]
pub fn generate(randomizer_facade: &RandomizerFacade) -> Self {
Self(randomizer_facade.generate_random_array())
}
Expand All @@ -166,6 +169,7 @@ impl Iv {
Self::from_bytes(slice).ok()
}

#[must_use]
pub fn get_inner(&self) -> &[u8; IV_BYTE_SIZE] {
&self.0
}
Expand Down
1 change: 1 addition & 0 deletions tuta-sdk/rust/sdk/src/crypto/argon2_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const ARGON2ID_KEY_LENGTH: usize = 32;
/// `pass` The passphrase to use for key generation as utf8 string.
/// `salt` 16 bytes of random data
/// returns resolved with the key
#[must_use]
pub fn generate_key_from_passphrase(password: &str, salt: [u8; 16]) -> Aes256Key {
let params = Params::new(
ARGON2ID_MEMORY_IN_KIB,
Expand Down
12 changes: 7 additions & 5 deletions tuta-sdk/rust/sdk/src/crypto/crypto_facade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub struct ResolvedSessionKey {

#[cfg_attr(test, mockall::automock)]
impl CryptoFacade {
#[must_use]
pub fn new(
key_loader_facade: Option<Arc<KeyLoaderFacade>>,
instance_mapper: Arc<InstanceMapper>,
Expand Down Expand Up @@ -371,6 +372,12 @@ impl SessionKeyResolutionErrorSubtype for PQError {}

impl SessionKeyResolutionErrorSubtype for RSAEncryptionError {}

#[must_use]
pub fn create_auth_verifier(user_passphrase_key: Aes256Key) -> String {
let sha_user_passphrase = crate::crypto::sha::sha256(user_passphrase_key.as_bytes());
BASE64_URL_SAFE_NO_PAD.encode(sha_user_passphrase)
}

// FIXME: check for returned owner_enc_session_key
#[cfg(test)]
mod test {
Expand Down Expand Up @@ -650,8 +657,3 @@ mod test {
}
}
}

pub fn create_auth_verifier(user_passphrase_key: Aes256Key) -> String {
let sha_user_passphrase = crate::crypto::sha::sha256(user_passphrase_key.as_bytes());
BASE64_URL_SAFE_NO_PAD.encode(sha_user_passphrase)
}
1 change: 1 addition & 0 deletions tuta-sdk/rust/sdk/src/crypto/hkdf.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// Derives a key of a defined length from `salt`, `input_key_material` and `info`.
#[must_use]
pub fn hkdf(salt: &[u8], input_key_material: &[u8], info: &[u8], length_bytes: usize) -> Vec<u8> {
let generator = hkdf::Hkdf::<sha2::Sha256>::new(Some(salt), input_key_material);
let mut output_buffer = vec![0u8; length_bytes];
Expand Down
2 changes: 2 additions & 0 deletions tuta-sdk/rust/sdk/src/crypto/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ impl GenericAesKey {
}

/// Encrypts `key_to_encrypt` with this key.
#[must_use]
pub fn encrypt_key(&self, key_to_encrypt: &GenericAesKey, iv: Iv) -> Vec<u8> {
match self {
Self::Aes128(key) => {
Expand Down Expand Up @@ -111,6 +112,7 @@ impl GenericAesKey {
}
}

#[must_use]
pub fn as_bytes(&self) -> &[u8] {
match self {
Self::Aes128(n) => n.as_bytes(),
Expand Down
2 changes: 2 additions & 0 deletions tuta-sdk/rust/sdk/src/crypto/randomizer_facade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ impl RandomizerFacade {
}

/// Generate a random array of a given size.
#[must_use]
pub fn generate_random_array<const S: usize>(&self) -> [u8; S] {
let mut output = [0u8; S];
self.fill_slice(&mut output);
Expand Down Expand Up @@ -65,6 +66,7 @@ pub mod test_util {
use super::*;

/// Used for internal testing using OsRng.
#[must_use]
pub fn make_thread_rng_facade() -> RandomizerFacade {
RandomizerFacade::from_core(rand::rngs::OsRng {})
}
Expand Down
1 change: 1 addition & 0 deletions tuta-sdk/rust/sdk/src/crypto/sha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use sha2::Digest;

/// Generates a SHA256 hash of `data`
#[must_use]
pub fn sha256(data: &[u8]) -> Vec<u8> {
let mut hasher = sha2::Sha256::new();
hasher.update(data);
Expand Down
1 change: 1 addition & 0 deletions tuta-sdk/rust/sdk/src/crypto/tuta_crypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ pub struct PQKeyPairs {

impl PQKeyPairs {
/// Generate a keypair with the given random number generator.
#[must_use]
pub fn generate(randomizer_facade: &RandomizerFacade) -> Self {
let ecc_keys = EccKeyPair::generate(randomizer_facade);
let kyber_keys = KyberKeyPair::generate();
Expand Down
3 changes: 3 additions & 0 deletions tuta-sdk/rust/sdk/src/custom_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@ pub const CUSTOM_ID_STRUCT_NAME: &str = "CustomId";
pub struct CustomId(pub String);

impl CustomId {
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}

/// Create a CustomId from an arbitrary (unencoded) string
#[must_use]
pub fn from_custom_string(custom_string: &str) -> Self {
Self(base64::prelude::BASE64_URL_SAFE_NO_PAD.encode(custom_string))
}

/// Generates and returns a random `CustomId`
#[cfg(test)]
#[must_use]
pub fn test_random() -> Self {
use crate::util::test_utils::generate_random_string;
Self(generate_random_string::<9>())
Expand Down
Loading

0 comments on commit 51411ba

Please sign in to comment.