Skip to content

Commit c458902

Browse files
committed
supports mutable IV in GcmParams
so that some PKCS11 implementation (like AWS CloudHSM) could write random IV into it Signed-off-by: Konge <[email protected]>
1 parent 024976f commit c458902

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

cryptoki/src/mechanism/aead.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ use std::marker::PhantomData;
99
use std::slice;
1010

1111
/// Parameters for AES-GCM.
12-
#[derive(Debug, Clone, Copy)]
12+
#[derive(Debug)]
1313
#[repr(transparent)]
1414
pub struct GcmParams<'a> {
1515
inner: CK_GCM_PARAMS,
16-
_marker: PhantomData<&'a [u8]>,
16+
_marker: PhantomData<&'a mut [u8]>,
1717
}
1818

1919
impl<'a> GcmParams<'a> {
@@ -36,7 +36,7 @@ impl<'a> GcmParams<'a> {
3636
///
3737
/// This function panics if the length of `iv` or `aad` does not
3838
/// fit into an [Ulong].
39-
pub fn new(iv: &'a [u8], aad: &'a [u8], tag_bits: Ulong) -> Self {
39+
pub fn new(iv: &'a mut [u8], aad: &'a [u8], tag_bits: Ulong) -> Self {
4040
// The ulIvBits parameter seems to be missing from the 2.40 spec,
4141
// although it is included in the header file. In [1], OASIS clarified
4242
// that the header file is normative. In 3.0, they added the parameter
@@ -55,7 +55,7 @@ impl<'a> GcmParams<'a> {
5555
// [1]: https://www.oasis-open.org/committees/document.php?document_id=58032&wg_abbrev=pkcs11
5656
GcmParams {
5757
inner: CK_GCM_PARAMS {
58-
pIv: iv.as_ptr() as *mut _,
58+
pIv: iv.as_mut_ptr(),
5959
ulIvLen: iv
6060
.len()
6161
.try_into()
@@ -73,9 +73,9 @@ impl<'a> GcmParams<'a> {
7373
}
7474

7575
/// The initialization vector.
76-
pub fn iv(&self) -> &'a [u8] {
77-
// SAFETY: In the constructor, the IV always comes from a &'a [u8]
78-
unsafe { slice::from_raw_parts(self.inner.pIv, self.inner.ulIvLen as _) }
76+
pub fn iv(&mut self) -> &mut [u8] {
77+
// SAFETY: In the constructor, the IV always comes from a &'a mut [u8]
78+
unsafe { slice::from_raw_parts_mut(self.inner.pIv, self.inner.ulIvLen as _) }
7979
}
8080

8181
/// The additional authenticated data.

cryptoki/src/mechanism/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ impl TryFrom<CK_MECHANISM_TYPE> for MechanismType {
720720
}
721721
}
722722

723-
#[derive(Copy, Debug, Clone)]
723+
#[derive(Debug)]
724724
#[non_exhaustive]
725725
/// Type defining a specific mechanism and its parameters
726726
pub enum Mechanism<'a> {

cryptoki/tests/basic.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1172,7 +1172,7 @@ fn sha256_digest() -> TestResult {
11721172
fn aes_gcm_no_aad() -> TestResult {
11731173
// Encrypt two blocks of zeros with AES-128-GCM
11741174
let key = vec![0; 16];
1175-
let iv = [0; 12];
1175+
let mut iv = [0; 12];
11761176
let aad = [];
11771177
let plain = [0; 32];
11781178
let expected_cipher_and_tag = [
@@ -1192,7 +1192,7 @@ fn aes_gcm_no_aad() -> TestResult {
11921192
Attribute::Encrypt(true),
11931193
];
11941194
let key_handle = session.create_object(&template)?;
1195-
let mechanism = Mechanism::AesGcm(GcmParams::new(&iv, &aad, 96.into()));
1195+
let mechanism = Mechanism::AesGcm(GcmParams::new(&mut iv, &aad, 96.into()));
11961196
let cipher_and_tag = session.encrypt(&mechanism, key_handle, &plain)?;
11971197
assert_eq!(expected_cipher_and_tag[..], cipher_and_tag[..]);
11981198
Ok(())
@@ -1204,7 +1204,7 @@ fn aes_gcm_with_aad() -> TestResult {
12041204
// Encrypt a block of zeros with AES-128-GCM.
12051205
// Use another block of zeros for AAD.
12061206
let key = vec![0; 16];
1207-
let iv = [0; 12];
1207+
let mut iv = [0; 12];
12081208
let aad = [0; 16];
12091209
let plain = [0; 16];
12101210
let expected_cipher_and_tag = [
@@ -1223,7 +1223,7 @@ fn aes_gcm_with_aad() -> TestResult {
12231223
Attribute::Encrypt(true),
12241224
];
12251225
let key_handle = session.create_object(&template)?;
1226-
let mechanism = Mechanism::AesGcm(GcmParams::new(&iv, &aad, 96.into()));
1226+
let mechanism = Mechanism::AesGcm(GcmParams::new(&mut iv, &aad, 96.into()));
12271227
let cipher_and_tag = session.encrypt(&mechanism, key_handle, &plain)?;
12281228
assert_eq!(expected_cipher_and_tag[..], cipher_and_tag[..]);
12291229
Ok(())

0 commit comments

Comments
 (0)