Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: supports mutable IV in GcmParams, close #225 #226

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading