Skip to content

Commit 131cbfa

Browse files
MGibson1quextenHinton
authored
expose cryptography primitives to wasm (#143)
## 📔 Objective This is a stop-gap to allow encryption changes to be developed only in the rust SDK and used from the javascript clients. ## ⏰ Reminders before review - Contributor guidelines followed - All formatters and local linters executed and passed - Written new unit and / or integration tests where applicable - Protected functional changes with optionality (feature flags) - Used internationalization (i18n) for all UI strings - CI builds passed - Communicated to DevOps any deployment requirements - Updated any necessary documentation (Confluence, contributing docs) or informed the documentation team ## 🦮 Reviewer guidelines <!-- Suggested interactions but feel free to use (or not) as you desire! --> - 👍 (`:+1:`) or similar for great changes - 📝 (`:memo:`) or ℹ️ (`:information_source:`) for notes or general info - ❓ (`:question:`) for questions - 🤔 (`:thinking:`) or 💭 (`:thought_balloon:`) for more open inquiry that's not quite a confirmed issue and could potentially benefit from discussion - 🎨 (`:art:`) for suggestions / improvements - ❌ (`:x:`) or ⚠️ (`:warning:`) for more significant problems or concerns needing attention - 🌱 (`:seedling:`) or ♻️ (`:recycle:`) for future improvements or indications of technical debt - ⛏ (`:pick:`) for minor or nitpick changes --------- Co-authored-by: Bernd Schoolmann <[email protected]> Co-authored-by: Oscar Hinton <[email protected]>
1 parent 782cbdf commit 131cbfa

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

crates/bitwarden-wasm-internal/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mod client;
22
mod crypto;
33
mod custom_types;
44
mod init;
5+
mod pure_crypto;
56
mod ssh;
67
mod vault;
78

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
use std::str::FromStr;
2+
3+
use bitwarden_crypto::{
4+
CryptoError, EncString, KeyDecryptable, KeyEncryptable, SymmetricCryptoKey,
5+
};
6+
use wasm_bindgen::prelude::*;
7+
8+
/// This module represents a stopgap solution to provide access to primitive crypto functions for JS
9+
/// clients. It is not intended to be used outside of the JS clients and this pattern should not be
10+
/// proliferated. It is necessary because we want to use SDK crypto prior to the SDK being fully
11+
/// responsible for state and keys.
12+
#[wasm_bindgen]
13+
pub struct PureCrypto {}
14+
15+
#[wasm_bindgen]
16+
impl PureCrypto {
17+
pub fn symmetric_decrypt(enc_string: String, key_b64: String) -> Result<String, CryptoError> {
18+
let enc_string = EncString::from_str(&enc_string)?;
19+
let key = SymmetricCryptoKey::try_from(key_b64)?;
20+
enc_string.decrypt_with_key(&key)
21+
}
22+
23+
pub fn symmetric_decrypt_to_bytes(
24+
enc_string: String,
25+
key_b64: String,
26+
) -> Result<Vec<u8>, CryptoError> {
27+
let enc_string = EncString::from_str(&enc_string)?;
28+
let key = SymmetricCryptoKey::try_from(key_b64)?;
29+
enc_string.decrypt_with_key(&key)
30+
}
31+
32+
pub fn symmetric_decrypt_array_buffer(
33+
enc_bytes: Vec<u8>,
34+
key_b64: String,
35+
) -> Result<Vec<u8>, CryptoError> {
36+
let enc_string = EncString::from_buffer(&enc_bytes)?;
37+
let key = SymmetricCryptoKey::try_from(key_b64)?;
38+
enc_string.decrypt_with_key(&key)
39+
}
40+
41+
pub fn symmetric_encrypt(plain: String, key_b64: String) -> Result<String, CryptoError> {
42+
let key = SymmetricCryptoKey::try_from(key_b64)?;
43+
44+
Ok(plain.encrypt_with_key(&key)?.to_string())
45+
}
46+
47+
pub fn symmetric_encrypt_to_array_buffer(
48+
plain: Vec<u8>,
49+
key_b64: String,
50+
) -> Result<Vec<u8>, CryptoError> {
51+
let key = SymmetricCryptoKey::try_from(key_b64)?;
52+
plain.encrypt_with_key(&key)?.to_buffer()
53+
}
54+
}
55+
56+
#[cfg(test)]
57+
mod tests {
58+
use std::str::FromStr;
59+
60+
use bitwarden_crypto::EncString;
61+
62+
use super::*;
63+
64+
const KEY_B64: &str =
65+
"UY4B5N4DA4UisCNClgZtRr6VLy9ZF5BXXC7cDZRqourKi4ghEMgISbCsubvgCkHf5DZctQjVot11/vVvN9NNHQ==";
66+
const ENCRYPTED: &str = "2.Dh7AFLXR+LXcxUaO5cRjpg==|uXyhubjAoNH8lTdy/zgJDQ==|cHEMboj0MYsU5yDRQ1rLCgxcjNbKRc1PWKuv8bpU5pM=";
67+
const DECRYPTED: &str = "test";
68+
const DECRYPTED_BYTES: &[u8] = b"test";
69+
const ENCRYPTED_BYTES: &[u8] = &[
70+
2, 209, 195, 115, 49, 205, 253, 128, 162, 169, 246, 175, 217, 144, 73, 108, 191, 27, 113,
71+
69, 55, 94, 142, 62, 129, 204, 173, 130, 37, 42, 97, 209, 25, 192, 64, 126, 112, 139, 248,
72+
2, 89, 112, 178, 83, 25, 77, 130, 187, 127, 85, 179, 211, 159, 186, 111, 44, 109, 211, 18,
73+
120, 104, 144, 4, 76, 3,
74+
];
75+
76+
#[test]
77+
fn test_symmetric_decrypt() {
78+
let enc_string = EncString::from_str(ENCRYPTED).unwrap();
79+
80+
let result = PureCrypto::symmetric_decrypt(enc_string.to_string(), KEY_B64.to_string());
81+
assert!(result.is_ok());
82+
assert_eq!(result.unwrap(), DECRYPTED);
83+
}
84+
85+
#[test]
86+
fn test_symmetric_encrypt() {
87+
let result = PureCrypto::symmetric_encrypt(DECRYPTED.to_string(), KEY_B64.to_string());
88+
assert!(result.is_ok());
89+
// Cannot test encrypted string content because IV is unique per encryption
90+
}
91+
92+
#[test]
93+
fn test_symmetric_round_trip() {
94+
let encrypted =
95+
PureCrypto::symmetric_encrypt(DECRYPTED.to_string(), KEY_B64.to_string()).unwrap();
96+
let decrypted =
97+
PureCrypto::symmetric_decrypt(encrypted.clone(), KEY_B64.to_string()).unwrap();
98+
assert_eq!(decrypted, DECRYPTED);
99+
}
100+
101+
#[test]
102+
fn test_symmetric_decrypt_array_buffer() {
103+
let result = PureCrypto::symmetric_decrypt_array_buffer(
104+
ENCRYPTED_BYTES.to_vec(),
105+
KEY_B64.to_string(),
106+
);
107+
assert!(result.is_ok());
108+
assert_eq!(result.unwrap(), DECRYPTED_BYTES);
109+
}
110+
111+
#[test]
112+
fn test_symmetric_encrypt_to_array_buffer() {
113+
let result = PureCrypto::symmetric_encrypt_to_array_buffer(
114+
DECRYPTED_BYTES.to_vec(),
115+
KEY_B64.to_string(),
116+
);
117+
assert!(result.is_ok());
118+
// Cannot test encrypted string content because IV is unique per encryption
119+
}
120+
121+
#[test]
122+
fn test_symmetric_buffer_round_trip() {
123+
let encrypted = PureCrypto::symmetric_encrypt_to_array_buffer(
124+
DECRYPTED_BYTES.to_vec(),
125+
KEY_B64.to_string(),
126+
)
127+
.unwrap();
128+
let decrypted =
129+
PureCrypto::symmetric_decrypt_array_buffer(encrypted.clone(), KEY_B64.to_string())
130+
.unwrap();
131+
assert_eq!(decrypted, DECRYPTED_BYTES);
132+
}
133+
}

0 commit comments

Comments
 (0)