1
0
mirror of https://github.com/bitwarden/server synced 2025-12-17 00:33:23 +00:00

Generate valid keys using rust

This commit is contained in:
Hinton
2025-07-31 10:20:53 +02:00
parent 072f9f2278
commit 75f11f68ac
7 changed files with 108 additions and 40 deletions

View File

@@ -1,3 +1,4 @@
#![allow(clippy::missing_safety_doc)]
use std::{
ffi::{c_char, CStr, CString},
num::NonZeroU32,
@@ -10,6 +11,39 @@ pub extern "C" fn my_add(x: i32, y: i32) -> i32 {
x + y
}
#[no_mangle]
pub unsafe extern "C" fn generate_user_keys(
email: *const c_char,
password: *const c_char,
) -> *const c_char {
// TODO: We might want to make KDF configurable in the future.
let kdf = Kdf::PBKDF2 {
iterations: NonZeroU32::new(600_000).unwrap(),
};
let email = CStr::from_ptr(email).to_str().unwrap();
let password = CStr::from_ptr(password).to_str().unwrap();
let master_key = MasterKey::derive(password, email, &kdf).unwrap();
let master_password_hash = master_key
.derive_master_key_hash(password.as_bytes(), HashPurpose::ServerAuthorization)
.unwrap();
let (user_key, encrypted_user_key) = master_key.make_user_key().unwrap();
let keys = user_key.make_key_pair().unwrap();
let json = serde_json::json!({
"masterPasswordHash": master_password_hash,
"encryptedUserKey": encrypted_user_key.to_string(),
"publicKey": keys.public.to_string(),
"privateKey": keys.private.to_string(),
})
.to_string();
let result = CString::new(json).unwrap();
result.into_raw()
}
/// # Safety
///
/// The `email` and `password` pointers must be valid null-terminated C strings.