Skip to content

Commit 173b86e

Browse files
crisdosaygoclaude
andcommitted
Fix Windows build: use *mut c_void for BCrypt handles
windows-sys 0.59 changed BCRYPT_ALG_HANDLE from usize to *mut c_void. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 5ec86c3 commit 173b86e

1 file changed

Lines changed: 18 additions & 14 deletions

File tree

crates/crypto/src/windows.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
// API, which is the OS-provided FIPS-validated crypto surface on Windows.
55

66
use crate::{KEY_LEN, PBKDF2_ITERATIONS};
7+
use std::ffi::c_void;
8+
9+
type AlgHandle = *mut c_void;
10+
type KeyHandle = *mut c_void;
711

812
pub fn random_bytes(len: usize) -> Result<Vec<u8>, String> {
913
use windows_sys::Win32::Security::Cryptography::BCryptGenRandom;
1014
let mut buf = vec![0u8; len];
11-
let status = unsafe { BCryptGenRandom(0, buf.as_mut_ptr(), len as u32, 0x00000002) };
15+
let status = unsafe { BCryptGenRandom(std::ptr::null_mut(), buf.as_mut_ptr(), len as u32, 0x00000002) };
1216
if status != 0 {
1317
return Err(format!("BCryptGenRandom failed: NTSTATUS {status:#x}"));
1418
}
@@ -20,10 +24,10 @@ pub fn derive_key(passphrase: &[u8], salt: &[u8]) -> Result<[u8; KEY_LEN], Strin
2024
// For simplicity and correctness, we use BCryptDeriveKeyPBKDF2
2125
use windows_sys::Win32::Security::Cryptography::*;
2226

23-
let mut alg_handle: usize = 0;
27+
let mut alg_handle: AlgHandle = std::ptr::null_mut();
2428
let status = unsafe {
2529
BCryptOpenAlgorithmProvider(
26-
&mut alg_handle as *mut _,
30+
&mut alg_handle,
2731
// BCRYPT_SHA256_ALGORITHM = "SHA256\0" as wide string
2832
[b'S' as u16, b'H' as u16, b'A' as u16, b'2' as u16, b'5' as u16, b'6' as u16, 0].as_ptr(),
2933
std::ptr::null(),
@@ -145,16 +149,16 @@ const AES_ALGORITHM: &[u16] = &[b'A' as u16, b'E' as u16, b'S' as u16, 0];
145149
// FFI declarations for BCrypt functions not in windows-sys or needed with specific signatures
146150
unsafe extern "system" {
147151
fn BCryptSetProperty(
148-
h_object: usize,
152+
h_object: AlgHandle,
149153
psz_property: *const u16,
150154
pb_input: *const u8,
151155
cb_input: u32,
152156
dw_flags: u32,
153157
) -> i32;
154158

155159
fn BCryptGenerateSymmetricKey(
156-
h_algorithm: usize,
157-
ph_key: *mut usize,
160+
h_algorithm: AlgHandle,
161+
ph_key: *mut KeyHandle,
158162
pb_key_object: *mut u8,
159163
cb_key_object: u32,
160164
pb_secret: *const u8,
@@ -163,7 +167,7 @@ unsafe extern "system" {
163167
) -> i32;
164168

165169
fn BCryptEncrypt(
166-
h_key: usize,
170+
h_key: KeyHandle,
167171
pb_input: *const u8,
168172
cb_input: u32,
169173
p_padding_info: *mut AuthCipherModeInfo,
@@ -176,7 +180,7 @@ unsafe extern "system" {
176180
) -> i32;
177181

178182
fn BCryptDecrypt(
179-
h_key: usize,
183+
h_key: KeyHandle,
180184
pb_input: *const u8,
181185
cb_input: u32,
182186
p_padding_info: *mut AuthCipherModeInfo,
@@ -188,19 +192,19 @@ unsafe extern "system" {
188192
dw_flags: u32,
189193
) -> i32;
190194

191-
fn BCryptDestroyKey(h_key: usize) -> i32;
195+
fn BCryptDestroyKey(h_key: KeyHandle) -> i32;
192196
}
193197

194198
/// Opens an AES algorithm handle configured for GCM chaining mode.
195-
fn open_aes_gcm_handle() -> Result<usize, String> {
199+
fn open_aes_gcm_handle() -> Result<AlgHandle, String> {
196200
use windows_sys::Win32::Security::Cryptography::{
197201
BCryptCloseAlgorithmProvider, BCryptOpenAlgorithmProvider,
198202
};
199203

200-
let mut alg_handle: usize = 0;
204+
let mut alg_handle: AlgHandle = std::ptr::null_mut();
201205
let status = unsafe {
202206
BCryptOpenAlgorithmProvider(
203-
&mut alg_handle as *mut _,
207+
&mut alg_handle,
204208
AES_ALGORITHM.as_ptr(),
205209
std::ptr::null(),
206210
0,
@@ -236,7 +240,7 @@ pub fn aes_gcm_encrypt(key: &[u8; KEY_LEN], nonce: &[u8], plaintext: &[u8]) -> R
236240
let alg_handle = open_aes_gcm_handle()?;
237241

238242
// Generate symmetric key handle
239-
let mut key_handle: usize = 0;
243+
let mut key_handle: KeyHandle = std::ptr::null_mut();
240244
let status = unsafe {
241245
BCryptGenerateSymmetricKey(
242246
alg_handle,
@@ -307,7 +311,7 @@ pub fn aes_gcm_decrypt(key: &[u8; KEY_LEN], nonce: &[u8], ciphertext_and_tag: &[
307311
let alg_handle = open_aes_gcm_handle()?;
308312

309313
// Generate symmetric key handle
310-
let mut key_handle: usize = 0;
314+
let mut key_handle: KeyHandle = std::ptr::null_mut();
311315
let status = unsafe {
312316
BCryptGenerateSymmetricKey(
313317
alg_handle,

0 commit comments

Comments
 (0)