Skip to content

Commit

Permalink
supports mutable IV in GcmParams
Browse files Browse the repository at this point in the history
so that some PKCS11 implementation (like AWS CloudHSM) could write random IV into it

Signed-off-by: Konge <[email protected]>
  • Loading branch information
zkonge committed Sep 20, 2024
1 parent 024976f commit c458902
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
14 changes: 7 additions & 7 deletions cryptoki/src/mechanism/aead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion cryptoki/src/mechanism/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ impl TryFrom<CK_MECHANISM_TYPE> for MechanismType {
}
}

#[derive(Copy, Debug, Clone)]
#[derive(Debug)]
#[non_exhaustive]
/// Type defining a specific mechanism and its parameters
pub enum Mechanism<'a> {
Expand Down
8 changes: 4 additions & 4 deletions cryptoki/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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(())
Expand All @@ -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 = [
Expand All @@ -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(())
Expand Down

0 comments on commit c458902

Please sign in to comment.