Skip to content

Commit 3c0a7ce

Browse files
Improved comments and expect() error messages
Signed-off-by: Jacob Prud'homme <[email protected]>
1 parent c22eec5 commit 3c0a7ce

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

cryptoki/src/mechanism/kbkdf.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ impl KbkdfDkmLengthFormat {
8585
},
8686
bLittleEndian: (endianness == Endianness::Little).into(),
8787
ulWidthInBits: width_in_bits.try_into().expect(
88-
"bit width of KBKDF derived key material length value does not fit in CK_ULONG",
88+
"bit width of KBKDF DKM length value does not fit in CK_ULONG",
8989
),
9090
})
9191
}
@@ -111,7 +111,8 @@ pub enum PrfDataParamType<'a> {
111111
/// * [`PrfDataParamType::IterationVariable`] is required for the KDF in all modes.
112112
/// * In counter-mode, [`PrfDataParamType::IterationVariable`] must contain [`KbkdfCounterFormat`].
113113
/// In feedback- and double pipeline-mode, it must contain [`None`].
114-
/// * [`PrfDataParamType::Counter`] must not be present in counter-mode.
114+
/// * [`PrfDataParamType::Counter`] must not be present in counter-mode, and can be present at most
115+
/// once in feedback- and double-pipeline modes.
115116
/// * [`PrfDataParamType::DkmLength`] can be present at most once, in any mode.
116117
/// * [`PrfDataParamType::ByteArray`] can be present any amount of times, in any mode.
117118
#[derive(Debug, Clone, Copy)]
@@ -162,7 +163,7 @@ impl<'a> PrfDataParam<'a> {
162163
ulValueLen: data
163164
.len()
164165
.try_into()
165-
.expect("length of data parameter does not fit in CK_ULONG"),
166+
.expect("length of PRF data parameter does not fit in CK_ULONG"),
166167
},
167168
},
168169
_marker: PhantomData,
@@ -259,7 +260,7 @@ impl<'a> KbkdfParams<'a> {
259260
ulNumberOfDataParams: prf_data_params
260261
.len()
261262
.try_into()
262-
.expect("number of data parameters does not fit in CK_ULONG"),
263+
.expect("number of PRF data parameters does not fit in CK_ULONG"),
263264
pDataParams: prf_data_params.as_ptr() as cryptoki_sys::CK_PRF_DATA_PARAM_PTR,
264265
ulAdditionalDerivedKeys: additional_derived_keys.as_ref().map_or(0, |keys| {
265266
keys.len()
@@ -331,7 +332,7 @@ impl<'a> KbkdfFeedbackParams<'a> {
331332
ulNumberOfDataParams: prf_data_params
332333
.len()
333334
.try_into()
334-
.expect("number of data parameters does not fit in CK_ULONG"),
335+
.expect("number of PRF data parameters does not fit in CK_ULONG"),
335336
pDataParams: prf_data_params.as_ptr() as cryptoki_sys::CK_PRF_DATA_PARAM_PTR,
336337
ulIVLen: iv.map_or(0, |iv| {
337338
iv.len()

cryptoki/src/mechanism/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,9 +1194,11 @@ impl From<&Mechanism<'_>> for CK_MECHANISM {
11941194
fn make_mechanism<T>(mechanism: CK_MECHANISM_TYPE, param: &T) -> CK_MECHANISM {
11951195
CK_MECHANISM {
11961196
mechanism,
1197-
// SAFETY: Although the type signature says *mut, none of the
1198-
// mechanisms we support involve mutating the parameter, so
1199-
// this cast is OK.
1197+
// SAFETY: Parameters that expect to have some part of themselves
1198+
// mutated (such as additional_derived_keys in Kbkdf{*}Params) should
1199+
// indicate this to the end user by marking the relevant constructor
1200+
// parameters as mut. Otherwise, we should generally not expect the
1201+
// backend to mutate the parameters, so this cast is fine.
12001202
pParameter: param as *const T as *mut c_void,
12011203
ulParameterLen: size_of::<T>()
12021204
.try_into()

0 commit comments

Comments
 (0)