Skip to content

Commit aa8622b

Browse files
authored
Change purecrypto interface to work on uint8arrays (#201)
## 🎟️ Tracking - No jira ticket ## 📔 Objective Moves the `purecrypto` interface to work directly on uint8arrays instead of converting to/from b64, which saves us some conversion overhead, from base64, but once TS drops the base64 representation also into base64. ## ⏰ 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
1 parent f5ab03b commit aa8622b

File tree

1 file changed

+33
-42
lines changed

1 file changed

+33
-42
lines changed

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

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,37 @@ pub struct PureCrypto {}
1414

1515
#[wasm_bindgen]
1616
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)
17+
pub fn symmetric_decrypt(enc_string: String, key: Vec<u8>) -> Result<String, CryptoError> {
18+
EncString::from_str(&enc_string)?.decrypt_with_key(&SymmetricCryptoKey::try_from(key)?)
2119
}
2220

2321
pub fn symmetric_decrypt_to_bytes(
2422
enc_string: String,
25-
key_b64: String,
23+
key: Vec<u8>,
2624
) -> 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)
25+
EncString::from_str(&enc_string)?.decrypt_with_key(&SymmetricCryptoKey::try_from(key)?)
3026
}
3127

3228
pub fn symmetric_decrypt_array_buffer(
3329
enc_bytes: Vec<u8>,
34-
key_b64: String,
30+
key: Vec<u8>,
3531
) -> 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)
32+
EncString::from_buffer(&enc_bytes)?.decrypt_with_key(&SymmetricCryptoKey::try_from(key)?)
3933
}
4034

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())
35+
pub fn symmetric_encrypt(plain: String, key: Vec<u8>) -> Result<String, CryptoError> {
36+
plain
37+
.encrypt_with_key(&SymmetricCryptoKey::try_from(key)?)
38+
.map(|enc| enc.to_string())
4539
}
4640

4741
pub fn symmetric_encrypt_to_array_buffer(
4842
plain: Vec<u8>,
49-
key_b64: String,
43+
key: Vec<u8>,
5044
) -> Result<Vec<u8>, CryptoError> {
51-
let key = SymmetricCryptoKey::try_from(key_b64)?;
52-
plain.encrypt_with_key(&key)?.to_buffer()
45+
plain
46+
.encrypt_with_key(&SymmetricCryptoKey::try_from(key)?)?
47+
.to_buffer()
5348
}
5449
}
5550

@@ -61,8 +56,13 @@ mod tests {
6156

6257
use super::*;
6358

64-
const KEY_B64: &str =
65-
"UY4B5N4DA4UisCNClgZtRr6VLy9ZF5BXXC7cDZRqourKi4ghEMgISbCsubvgCkHf5DZctQjVot11/vVvN9NNHQ==";
59+
const KEY: &[u8] = &[
60+
81, 142, 1, 228, 222, 3, 3, 133, 34, 176, 35, 66, 150, 6, 109, 70, 190, 149, 47, 47, 89,
61+
23, 144, 87, 92, 46, 220, 13, 148, 106, 162, 234, 202, 139, 136, 33, 16, 200, 8, 73, 176,
62+
172, 185, 187, 224, 10, 65, 223, 228, 54, 92, 181, 8, 213, 162, 221, 117, 254, 245, 111,
63+
55, 211, 77, 29,
64+
];
65+
6666
const ENCRYPTED: &str = "2.Dh7AFLXR+LXcxUaO5cRjpg==|uXyhubjAoNH8lTdy/zgJDQ==|cHEMboj0MYsU5yDRQ1rLCgxcjNbKRc1PWKuv8bpU5pM=";
6767
const DECRYPTED: &str = "test";
6868
const DECRYPTED_BYTES: &[u8] = b"test";
@@ -77,57 +77,48 @@ mod tests {
7777
fn test_symmetric_decrypt() {
7878
let enc_string = EncString::from_str(ENCRYPTED).unwrap();
7979

80-
let result = PureCrypto::symmetric_decrypt(enc_string.to_string(), KEY_B64.to_string());
80+
let result = PureCrypto::symmetric_decrypt(enc_string.to_string(), KEY.to_vec());
8181
assert!(result.is_ok());
8282
assert_eq!(result.unwrap(), DECRYPTED);
8383
}
8484

8585
#[test]
8686
fn test_symmetric_encrypt() {
87-
let result = PureCrypto::symmetric_encrypt(DECRYPTED.to_string(), KEY_B64.to_string());
87+
let result = PureCrypto::symmetric_encrypt(DECRYPTED.to_string(), KEY.to_vec());
8888
assert!(result.is_ok());
8989
// Cannot test encrypted string content because IV is unique per encryption
9090
}
9191

9292
#[test]
9393
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();
94+
let encrypted = PureCrypto::symmetric_encrypt(DECRYPTED.to_string(), KEY.to_vec()).unwrap();
95+
let decrypted = PureCrypto::symmetric_decrypt(encrypted.clone(), KEY.to_vec()).unwrap();
9896
assert_eq!(decrypted, DECRYPTED);
9997
}
10098

10199
#[test]
102100
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-
);
101+
let result =
102+
PureCrypto::symmetric_decrypt_array_buffer(ENCRYPTED_BYTES.to_vec(), KEY.to_vec());
107103
assert!(result.is_ok());
108104
assert_eq!(result.unwrap(), DECRYPTED_BYTES);
109105
}
110106

111107
#[test]
112108
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-
);
109+
let result =
110+
PureCrypto::symmetric_encrypt_to_array_buffer(DECRYPTED_BYTES.to_vec(), KEY.to_vec());
117111
assert!(result.is_ok());
118112
// Cannot test encrypted string content because IV is unique per encryption
119113
}
120114

121115
#[test]
122116
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())
117+
let encrypted =
118+
PureCrypto::symmetric_encrypt_to_array_buffer(DECRYPTED_BYTES.to_vec(), KEY.to_vec())
130119
.unwrap();
120+
let decrypted =
121+
PureCrypto::symmetric_decrypt_array_buffer(encrypted.clone(), KEY.to_vec()).unwrap();
131122
assert_eq!(decrypted, DECRYPTED_BYTES);
132123
}
133124
}

0 commit comments

Comments
 (0)