Skip to content

Commit 06b2f4f

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

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

cryptoki/src/mechanism/kbkdf.rs

+6-5
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

+5-3
Original file line numberDiff line numberDiff line change
@@ -1183,9 +1183,11 @@ impl From<&Mechanism<'_>> for CK_MECHANISM {
11831183
fn make_mechanism<T>(mechanism: CK_MECHANISM_TYPE, param: &T) -> CK_MECHANISM {
11841184
CK_MECHANISM {
11851185
mechanism,
1186-
// SAFETY: Although the type signature says *mut, none of the
1187-
// mechanisms we support involve mutating the parameter, so
1188-
// this cast is OK.
1186+
// SAFETY: Parameters that expect to have some part of themselves
1187+
// mutated (such as additional_derived_keys in Kbkdf{*}Params) should
1188+
// indicate this to the end user by marking the relevant constructor
1189+
// parameters as mut. Otherwise, we should generally not expect the
1190+
// backend to mutate the parameters, so this cast is fine.
11891191
pParameter: param as *const T as *mut c_void,
11901192
ulParameterLen: size_of::<T>()
11911193
.try_into()

0 commit comments

Comments
 (0)