Skip to content

Commit

Permalink
Add decrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
quexten committed Nov 1, 2024
1 parent f1cdc2e commit becd783
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
10 changes: 5 additions & 5 deletions crates/bitwarden-crypto/src/chacha20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@ pub fn encrypt_xchacha20_poly1305(
let nonce = XChaCha20Poly1305::generate_nonce(&mut OsRng);
// to dyn buf
let mut buffer = secret_data.to_vec();
let ciphertext = cipher.encrypt_in_place(&nonce, &authenticated_data, &mut buffer);
let ciphertext = cipher.encrypt_in_place(&nonce, &[], &mut buffer);
match ciphertext {
Ok(_) => Ok((nonce.into(), buffer)),
Err(_) => Err(CryptoError::InvalidKey),

Check warning on line 24 in crates/bitwarden-crypto/src/chacha20.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-crypto/src/chacha20.rs#L10-L24

Added lines #L10 - L24 were not covered by tests
}
}

Check warning on line 26 in crates/bitwarden-crypto/src/chacha20.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-crypto/src/chacha20.rs#L26

Added line #L26 was not covered by tests

pub fn decrypt_xchacha_poly1305(
pub fn decrypt_xchacha20_poly1305(
nonce: &[u8; 24],
authenticated_data: &[u8],
key: &GenericArray<u8, U32>,
data: Vec<u8>,
data: &[u8],
) -> Result<Vec<u8>, CryptoError> {
let cipher = XChaCha20Poly1305::new(&key);
let nonce = GenericArray::from_slice(nonce);
let mut buffer = data;
let plaintext = cipher.decrypt_in_place(&nonce, &authenticated_data, &mut buffer);
let mut buffer = data.to_vec();
let plaintext = cipher.decrypt_in_place(&nonce, &[], &mut buffer);
match plaintext {
Ok(_) => Ok(buffer),
Err(_) => Err(CryptoError::InvalidKey),

Check warning on line 40 in crates/bitwarden-crypto/src/chacha20.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-crypto/src/chacha20.rs#L28-L40

Added lines #L28 - L40 were not covered by tests
Expand Down
14 changes: 8 additions & 6 deletions crates/bitwarden-wasm-internal/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,23 @@ impl ClientCrypto {
Ok(self.0.crypto().initialize_org_crypto(req).await?)
}

pub fn decrypt_xchacha20_poly1305(data: &[u8], key: &[u8]) -> Result<Vec<u8>> {
let nonce = data[..24].try_into().unwrap();
let ciphertext = &data[24..];
let key_len32 = key[..32].try_into().unwrap();
let plaintext = chacha20::decrypt_xchacha20_poly1305(&nonce, &[], key_len32, ciphertext)?;
Ok(plaintext)
}

Check warning on line 41 in crates/bitwarden-wasm-internal/src/crypto.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm-internal/src/crypto.rs#L35-L41

Added lines #L35 - L41 were not covered by tests

pub fn encrypt_xchacha20_poly1305(
secret_data: &[u8],
authenticated_data: &[u8],
key: &[u8],
) -> Result<Vec<u8>> {
println!("encrypt_xchacha20_poly1305");
println!("secret_data {:?}", secret_data);
println!("authenticated_data {:?}", authenticated_data);
println!("key {:?}", key);
let key_len32 = key[..32].try_into().unwrap();
let (nonce, ciphertext) =
chacha20::encrypt_xchacha20_poly1305(secret_data, authenticated_data, key_len32)?;
println!("encrytped");
let result: Vec<u8> = nonce.into_iter().chain(ciphertext.into_iter()).collect();
println!("result {:?}", result);
Ok(result)
}

Check warning on line 53 in crates/bitwarden-wasm-internal/src/crypto.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm-internal/src/crypto.rs#L43-L53

Added lines #L43 - L53 were not covered by tests
}

0 comments on commit becd783

Please sign in to comment.