Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 10 additions & 24 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,7 @@ dependencies = [
"signature",
"symcrypt",
"thiserror 2.0.16",
"widestring",
"windows 0.62.0",
"windows-result 0.4.0",
"x509-cert",
Expand Down Expand Up @@ -1798,6 +1799,7 @@ dependencies = [
"tempfile",
"thiserror 2.0.16",
"vm_resource",
"widestring",
"windows-sys 0.61.0",
"zerocopy",
]
Expand Down Expand Up @@ -2266,7 +2268,6 @@ dependencies = [
"vmcore",
"vmm_core",
"watchdog_core",
"wchar",
"zerocopy",
]

Expand Down Expand Up @@ -3480,7 +3481,6 @@ dependencies = [
"ucs2 0.0.0",
"uefi_nvram_storage",
"vmcore",
"wchar",
"zerocopy",
]

Expand Down Expand Up @@ -3752,6 +3752,7 @@ dependencies = [
"static_assertions",
"tempfile",
"virt",
"widestring",
"zerocopy",
]

Expand Down Expand Up @@ -3780,6 +3781,7 @@ dependencies = [
"vmbus_channel",
"vmbus_ring",
"vmcore",
"widestring",
"zerocopy",
]

Expand Down Expand Up @@ -4645,6 +4647,7 @@ name = "make_imc_hive"
version = "0.0.0"
dependencies = [
"anyhow",
"widestring",
"windows-sys 0.61.0",
]

Expand Down Expand Up @@ -5322,6 +5325,7 @@ dependencies = [
"vmbus_core",
"vmbus_ring",
"vmcore",
"widestring",
"zerocopy",
]

Expand Down Expand Up @@ -8804,6 +8808,7 @@ dependencies = [
"test_with_tracing",
"tracing",
"tracing-subscriber",
"widestring",
"windows-sys 0.61.0",
]

Expand Down Expand Up @@ -9388,7 +9393,6 @@ dependencies = [
"ucs2 0.0.0",
"uefi_specs",
"vmcore",
"wchar",
"zerocopy",
]

Expand All @@ -9402,7 +9406,7 @@ dependencies = [
"open_enum",
"static_assertions",
"ucs2 0.0.0",
"wchar",
"widestring",
"zerocopy",
]

Expand Down Expand Up @@ -10055,6 +10059,7 @@ dependencies = [
"test_with_tracing",
"thiserror 2.0.16",
"tracing",
"widestring",
"windows 0.62.0",
"zerocopy",
]
Expand Down Expand Up @@ -11903,26 +11908,6 @@ dependencies = [
"vmcore",
]

[[package]]
name = "wchar"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8be48fe4c433c0d4aa71bb8759c5f7b1da6dacb1b99998566ebe16503f6a59c"
dependencies = [
"wchar-impl",
]

[[package]]
name = "wchar-impl"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "075c93156fed21f9dab57af5e81604d0fdb67432c919a8c1f78bb979f06a3d25"
dependencies = [
"proc-macro2",
"quote",
"syn 1.0.109",
]

[[package]]
name = "web-time"
version = "1.1.0"
Expand Down Expand Up @@ -11963,6 +11948,7 @@ dependencies = [
"criterion",
"guid",
"pal",
"widestring",
"windows-sys 0.61.0",
]

Expand Down
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,6 @@ serde_json = { version = "1.0", default-features = false }
serde_yaml = "0.9"
toml_edit = "0.23"
urlencoding = "2.1.3"
wchar = "0.11"
widestring = "1.0"
# We add the derive feature here since the vast majority of our crates use it.
zerocopy = { version = "0.8.25", features = ["derive"]}
Expand Down
4 changes: 1 addition & 3 deletions openhcl/underhill_attestation/src/jwt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,7 @@ mod tests {
// UTF-16 string
let data = "UTF-16 encoded"
.encode_utf16()
.collect::<Vec<u16>>()
.iter()
.flat_map(|character| character.to_ne_bytes())
.flat_map(u16::to_ne_bytes)
.collect::<Vec<u8>>();
let result = string_from_utf8_preserve_invalid_bytes(data.as_slice());
assert_eq!(result, "U\0T\0F\0-\x001\x006\0 \0e\0n\0c\0o\0d\0e\0d\0");
Expand Down
1 change: 1 addition & 0 deletions petri/make_imc_hive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ rust-version.workspace = true
windows-sys = { workspace = true, features = ["Wdk_System_OfflineRegistry", "Win32_Foundation", "Win32_Security", "Win32_System_Registry"] }

anyhow.workspace = true
widestring.workspace = true

[lints]
workspace = true
42 changes: 22 additions & 20 deletions petri/make_imc_hive/src/windows/offreg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
// UNSAFETY: needed for the FFI bindings.
#![expect(unsafe_code)]

use std::ffi::OsStr;
use std::ops::Deref;
use std::os::windows::ffi::OsStrExt;
use std::path::Path;
use std::ptr::null;
use std::ptr::null_mut;
use widestring::U16CString;
use widestring::U16String;
use windows_sys::Wdk::System::OfflineRegistry::ORCloseHive;
use windows_sys::Wdk::System::OfflineRegistry::ORCloseKey;
use windows_sys::Wdk::System::OfflineRegistry::ORCreateHive;
Expand All @@ -36,11 +38,7 @@ impl Hive {
}

pub fn save(&self, path: &Path) -> std::io::Result<()> {
let path16 = path
.as_os_str()
.encode_wide()
.chain([0])
.collect::<Vec<_>>();
let path16 = wide(path)?;

// SAFETY: calling as documented with owned key and null-terminated
// path.
Expand Down Expand Up @@ -104,7 +102,7 @@ pub struct Key(ORHKEY);
impl Key {
pub fn create_key(&self, name: &str) -> anyhow::Result<OwnedKey> {
let mut new_key = null_mut();
let name16 = name.encode_utf16().chain([0]).collect::<Vec<_>>();
let name16 = wide(name)?;
// SAFETY: calling as documented with owned key and null-terminated
// path.
unsafe {
Expand All @@ -122,7 +120,7 @@ impl Key {
}

pub fn set_dword(&self, name: &str, dword: u32) -> std::io::Result<()> {
let name16 = name.encode_utf16().chain([0]).collect::<Vec<_>>();
let name16 = wide(name)?;
// SAFETY: calling as documented with owned key and null-terminated
// name.
unsafe {
Expand All @@ -138,8 +136,8 @@ impl Key {
}

pub fn set_sz(&self, name: &str, value: &str) -> std::io::Result<()> {
let name16 = name.encode_utf16().chain([0]).collect::<Vec<_>>();
let value16 = value.encode_utf16().chain([0]).collect::<Vec<_>>();
let name16 = wide(name)?;
let value16 = wide(value)?;
// SAFETY: calling as documented with owned key and null-terminated
// name and value.
unsafe {
Expand All @@ -148,15 +146,15 @@ impl Key {
name16.as_ptr(),
REG_SZ,
value16.as_ptr().cast(),
value16.len() as u32 * 2,
value16.as_slice_with_nul().len() as u32 * 2,
))?;
}
Ok(())
}

pub fn set_expand_sz(&self, name: &str, value: &str) -> std::io::Result<()> {
let name16 = name.encode_utf16().chain([0]).collect::<Vec<_>>();
let value16 = value.encode_utf16().chain([0]).collect::<Vec<_>>();
let name16 = wide(name)?;
let value16 = wide(value)?;
// SAFETY: calling as documented with owned key and null-terminated
// name and value.
unsafe {
Expand All @@ -165,7 +163,7 @@ impl Key {
name16.as_ptr(),
REG_EXPAND_SZ,
value16.as_ptr().cast(),
value16.len() as u32 * 2,
value16.as_slice_with_nul().len() as u32 * 2,
))?;
}
Ok(())
Expand All @@ -176,12 +174,12 @@ impl Key {
name: &str,
value: impl IntoIterator<Item = &'a str>,
) -> std::io::Result<()> {
let name16 = name.encode_utf16().chain([0]).collect::<Vec<_>>();
let value16 = value
.into_iter()
.flat_map(|s| s.encode_utf16().chain([0]))
.chain([0])
.collect::<Vec<_>>();
let name16 = wide(name)?;
let mut value16 = U16String::new();
for s in value {
value16.push_slice(wide(s)?.as_slice_with_nul());
}
value16.push_char('\0');
// SAFETY: calling as documented with owned key and null-terminated
// name and value.
unsafe {
Expand All @@ -197,6 +195,10 @@ impl Key {
}
}

fn wide(s: impl AsRef<OsStr>) -> std::io::Result<U16CString> {
U16CString::from_os_str(s).map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidInput, e))
}

fn chk(err: u32) -> std::io::Result<()> {
if err != 0 {
return Err(std::io::Error::from_raw_os_error(err as i32));
Expand Down
1 change: 1 addition & 0 deletions support/crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ jiff.workspace = true
thiserror.workspace = true

[target.'cfg(windows)'.dependencies]
widestring.workspace = true
windows = { workspace = true, features = [
"Win32_Foundation",
"Win32_Security_Cryptography",
Expand Down
6 changes: 3 additions & 3 deletions support/crypto/src/aes_256_gcm/win.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use super::*;
use crate::win::*;
use std::sync::LazyLock;
use widestring::U16CStr;
use windows::Win32::Security::Cryptography::BCRYPT_ALG_HANDLE;
use windows::Win32::Security::Cryptography::BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO;
use windows::Win32::Security::Cryptography::BCRYPT_CHAIN_MODE_GCM;
Expand All @@ -29,12 +30,11 @@ static AES_256_GCM: LazyLock<Result<AlgHandle, Aes256GcmError>> = LazyLock::new(
.map(|()| AlgHandle(handle))
.map_err(|e| err(e, "opening the AES algorithm provider"))?;

let mut chaining_mode = BCRYPT_CHAIN_MODE_GCM.as_wide().to_vec();
chaining_mode.push(0);
let chaining_mode = U16CStr::from_ptr_str(BCRYPT_CHAIN_MODE_GCM.as_ptr());
windows::Win32::Security::Cryptography::BCryptSetProperty(
handle.0.into(),
windows::Win32::Security::Cryptography::BCRYPT_CHAINING_MODE,
chaining_mode.as_bytes(),
chaining_mode.as_slice_with_nul().as_bytes(),
0,
)
.ok()
Expand Down
6 changes: 3 additions & 3 deletions support/crypto/src/aes_kwp/win.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use super::AesKeyWrapErrorInner;
use super::kwp;
use crate::win::*;
use std::sync::LazyLock;
use widestring::U16CStr;
use windows::Win32::Foundation::NTE_BAD_DATA;
use windows::Win32::Security::Cryptography::BCRYPT_ALG_HANDLE;
use windows::Win32::Security::Cryptography::BCRYPT_CHAIN_MODE_ECB;
Expand All @@ -35,12 +36,11 @@ static AES_ECB: LazyLock<Result<AlgHandle, AesKeyWrapError>> = LazyLock::new(||
.map(|()| AlgHandle(handle))
.map_err(|e| err(e, "opening the AES algorithm provider"))?;

let mut chaining_mode = BCRYPT_CHAIN_MODE_ECB.as_wide().to_vec();
chaining_mode.push(0);
let chaining_mode = U16CStr::from_ptr_str(BCRYPT_CHAIN_MODE_ECB.as_ptr());
windows::Win32::Security::Cryptography::BCryptSetProperty(
handle.0.into(),
windows::Win32::Security::Cryptography::BCRYPT_CHAINING_MODE,
chaining_mode.as_bytes(),
chaining_mode.as_slice_with_nul().as_bytes(),
0,
)
.ok()
Expand Down
Loading
Loading