Skip to content

Commit 33fa615

Browse files
committed
Fix
1 parent 03462bc commit 33fa615

2 files changed

Lines changed: 17 additions & 29 deletions

File tree

src/aead.rs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ mod recprot {
245245
mod recprot {
246246
use std::{
247247
fmt,
248-
os::raw::{c_char, c_int, c_uint, c_ulong},
248+
os::raw::{c_char, c_int, c_uint},
249249
ptr::{null, null_mut},
250250
};
251251

@@ -271,24 +271,8 @@ mod recprot {
271271
reason = "NONCE_LEN = 12 and TAG_LEN = 16 both fit in u32"
272272
)]
273273
const TAG_LEN_C: c_uint = TAG_LEN as c_uint;
274-
// On Windows c_ulong is u32; on other platforms it is u64 (same width as usize),
275-
// so cast_possible_truncation only fires on Windows.
276-
#[cfg_attr(
277-
target_os = "windows",
278-
expect(
279-
clippy::cast_possible_truncation,
280-
reason = "NONCE_LEN = 12 fits in u32"
281-
)
282-
)]
283-
const NONCE_LEN_UL: c_ulong = NONCE_LEN as c_ulong;
284-
#[cfg_attr(
285-
target_os = "windows",
286-
expect(
287-
clippy::cast_possible_truncation,
288-
reason = "TAG_LEN * 8 = 128 fits in u32"
289-
)
290-
)]
291-
const TAG_BITS_UL: c_ulong = (TAG_LEN * 8) as c_ulong;
274+
const NONCE_LEN_UL: u64 = NONCE_LEN as u64;
275+
const TAG_BITS_UL: u64 = (TAG_LEN * 8) as u64;
292276
#[expect(
293277
clippy::cast_possible_truncation,
294278
reason = "CK_GCM_MESSAGE_PARAMS is a small fixed struct"

src/freebl.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// self-test gate. This is intentional for neqo (non-FIPS) and saves ~7.6%
1414
// CPU on the PK11_AEADOp hot path (sftk_SessionFromHandle + mutex overhead).
1515

16-
use std::os::raw::{c_int, c_uchar, c_uint, c_ulong, c_void};
16+
use std::os::raw::{c_int, c_uchar, c_uint, c_void};
1717

1818
// NSS_AES_GCM = 4 (from blapit.h)
1919
pub const NSS_AES_GCM: c_int = 4;
@@ -35,23 +35,27 @@ pub type ChaCha20Poly1305Context = ChaCha20Poly1305ContextStr;
3535
// For CKG_NO_GENERATE (ivGenerator = 0), pIv/ulIvLen supply the full nonce.
3636
// For encrypt, pTag is the output tag buffer; for decrypt, pTag is the input
3737
// tag to verify. ulTagBits = TAG_LEN * 8 = 128.
38+
//
39+
// CK_ULONG maps to `unsigned long` in C, which is 8 bytes on LP64 (Linux/macOS)
40+
// and also on the Windows NSS build (whose freebl/gcm libraries are compiled
41+
// with an LP64 toolchain). Using u64 here ensures a uniform 48-byte layout
42+
// on all 64-bit targets and avoids a sizeof mismatch in the AES_AEAD paramLen
43+
// check on Windows.
3844
#[repr(C)]
3945
#[derive(Copy, Clone)]
4046
#[expect(non_snake_case, reason = "PKCS#11 naming conventions.")]
4147
pub struct CK_GCM_MESSAGE_PARAMS {
4248
pub pIv: *mut c_uchar,
43-
pub ulIvLen: c_ulong,
44-
pub ulIvFixedBits: c_ulong,
45-
pub ivGenerator: c_ulong, // CK_GENERATOR_FUNCTION; 0 = CKG_NO_GENERATE
49+
pub ulIvLen: u64,
50+
pub ulIvFixedBits: u64,
51+
pub ivGenerator: u64, // CK_GENERATOR_FUNCTION; 0 = CKG_NO_GENERATE
4652
pub pTag: *mut c_uchar,
47-
pub ulTagBits: c_ulong,
53+
pub ulTagBits: u64,
4854
}
4955

50-
// On LP64 (Linux/macOS) pointer == c_ulong == 8 bytes, so all 6 fields are
51-
// the same width. On Windows LLP64, c_ulong is 4 bytes but pointers are 8,
52-
// so the layout differs and the check would be wrong — skip it there.
53-
#[cfg(not(target_os = "windows"))]
54-
const _: () = assert!(size_of::<CK_GCM_MESSAGE_PARAMS>() == 6 * size_of::<c_ulong>());
56+
// All 6 fields are 8 bytes wide (two 8-byte pointers and four u64 values),
57+
// so the struct is 48 bytes on all 64-bit targets — no platform-specific padding.
58+
const _: () = assert!(size_of::<CK_GCM_MESSAGE_PARAMS>() == 6 * 8);
5559

5660
unsafe extern "C" {
5761
/// Allocate and initialise an AES context. Pass `NULL` for `iv` when

0 commit comments

Comments
 (0)