diff --git a/cryptoki/src/mechanism/aead.rs b/cryptoki/src/mechanism/aead.rs index 5c482e89..c3dbf08c 100644 --- a/cryptoki/src/mechanism/aead.rs +++ b/cryptoki/src/mechanism/aead.rs @@ -9,11 +9,11 @@ use std::marker::PhantomData; use std::slice; /// Parameters for AES-GCM. -#[derive(Debug, Clone, Copy)] +#[derive(Debug)] #[repr(transparent)] pub struct GcmParams<'a> { inner: CK_GCM_PARAMS, - _marker: PhantomData<&'a [u8]>, + _marker: PhantomData<&'a mut [u8]>, } impl<'a> GcmParams<'a> { @@ -36,7 +36,7 @@ impl<'a> GcmParams<'a> { /// /// This function panics if the length of `iv` or `aad` does not /// fit into an [Ulong]. - pub fn new(iv: &'a [u8], aad: &'a [u8], tag_bits: Ulong) -> Self { + pub fn new(iv: &'a mut [u8], aad: &'a [u8], tag_bits: Ulong) -> Self { // The ulIvBits parameter seems to be missing from the 2.40 spec, // although it is included in the header file. In [1], OASIS clarified // that the header file is normative. In 3.0, they added the parameter @@ -55,7 +55,7 @@ impl<'a> GcmParams<'a> { // [1]: https://www.oasis-open.org/committees/document.php?document_id=58032&wg_abbrev=pkcs11 GcmParams { inner: CK_GCM_PARAMS { - pIv: iv.as_ptr() as *mut _, + pIv: iv.as_mut_ptr(), ulIvLen: iv .len() .try_into() @@ -73,9 +73,9 @@ impl<'a> GcmParams<'a> { } /// The initialization vector. - pub fn iv(&self) -> &'a [u8] { - // SAFETY: In the constructor, the IV always comes from a &'a [u8] - unsafe { slice::from_raw_parts(self.inner.pIv, self.inner.ulIvLen as _) } + pub fn iv(&mut self) -> &mut [u8] { + // SAFETY: In the constructor, the IV always comes from a &'a mut [u8] + unsafe { slice::from_raw_parts_mut(self.inner.pIv, self.inner.ulIvLen as _) } } /// The additional authenticated data. diff --git a/cryptoki/src/mechanism/mod.rs b/cryptoki/src/mechanism/mod.rs index c18f2b56..cd3a7956 100644 --- a/cryptoki/src/mechanism/mod.rs +++ b/cryptoki/src/mechanism/mod.rs @@ -720,7 +720,7 @@ impl TryFrom for MechanismType { } } -#[derive(Copy, Debug, Clone)] +#[derive(Debug)] #[non_exhaustive] /// Type defining a specific mechanism and its parameters pub enum Mechanism<'a> { diff --git a/cryptoki/tests/basic.rs b/cryptoki/tests/basic.rs index 8368a547..90b833dc 100644 --- a/cryptoki/tests/basic.rs +++ b/cryptoki/tests/basic.rs @@ -1172,7 +1172,7 @@ fn sha256_digest() -> TestResult { fn aes_gcm_no_aad() -> TestResult { // Encrypt two blocks of zeros with AES-128-GCM let key = vec![0; 16]; - let iv = [0; 12]; + let mut iv = [0; 12]; let aad = []; let plain = [0; 32]; let expected_cipher_and_tag = [ @@ -1192,7 +1192,7 @@ fn aes_gcm_no_aad() -> TestResult { Attribute::Encrypt(true), ]; let key_handle = session.create_object(&template)?; - let mechanism = Mechanism::AesGcm(GcmParams::new(&iv, &aad, 96.into())); + let mechanism = Mechanism::AesGcm(GcmParams::new(&mut iv, &aad, 96.into())); let cipher_and_tag = session.encrypt(&mechanism, key_handle, &plain)?; assert_eq!(expected_cipher_and_tag[..], cipher_and_tag[..]); Ok(()) @@ -1204,7 +1204,7 @@ fn aes_gcm_with_aad() -> TestResult { // Encrypt a block of zeros with AES-128-GCM. // Use another block of zeros for AAD. let key = vec![0; 16]; - let iv = [0; 12]; + let mut iv = [0; 12]; let aad = [0; 16]; let plain = [0; 16]; let expected_cipher_and_tag = [ @@ -1223,7 +1223,7 @@ fn aes_gcm_with_aad() -> TestResult { Attribute::Encrypt(true), ]; let key_handle = session.create_object(&template)?; - let mechanism = Mechanism::AesGcm(GcmParams::new(&iv, &aad, 96.into())); + let mechanism = Mechanism::AesGcm(GcmParams::new(&mut iv, &aad, 96.into())); let cipher_and_tag = session.encrypt(&mechanism, key_handle, &plain)?; assert_eq!(expected_cipher_and_tag[..], cipher_and_tag[..]); Ok(())