diff --git a/src/encryption.rs b/src/encryption.rs index 4e60f78..6159bc8 100644 --- a/src/encryption.rs +++ b/src/encryption.rs @@ -26,18 +26,11 @@ pub fn encrypt( data: impl AsRef<[u8]>, data_context: &[impl AsRef<[u8]>], ) -> Result { - // Derive the key let ikm = ikml.get_latest_ikm()?; let key = derive_key(ikm, key_context); - - // Generate the AAD let aad = generate_aad(key_context, data_context); - - // Encrypt let encryption_function = ikm.scheme.get_encryption(); let encrypted_data = encryption_function(&key, data.as_ref(), &aad)?; - - // Encode Ok(storage::encode(ikm.id, &encrypted_data)) } @@ -77,17 +70,10 @@ pub fn decrypt( stored_data: &str, data_context: &[impl AsRef<[u8]>], ) -> Result> { - // Retreive the IKM and the encrypted data let (ikm_id, encrypted_data) = storage::decode(stored_data)?; let ikm = ikml.get_ikm_by_id(ikm_id)?; - - // Derive the key let key = derive_key(ikm, key_context); - - // Generate the AAD let aad = generate_aad(key_context, data_context); - - // Decrypt let decryption_function = ikm.scheme.get_decryption(); decryption_function(&key, &encrypted_data, &aad) } @@ -97,17 +83,12 @@ pub(crate) fn xchacha20poly1305_decrypt( encrypted_data: &EncryptedData, aad: &str, ) -> Result> { - // Adapt the key and the nonce let key = Key::from_slice(key); let nonce = XNonce::from_slice(&encrypted_data.nonce); - - // Prepare the payload let payload = Payload { msg: &encrypted_data.ciphertext, aad: aad.as_bytes(), }; - - // Decrypt the payload let cipher = XChaCha20Poly1305::new(key); Ok(cipher.decrypt(nonce, payload)?) }