Skip to content

Commit 1daf63f

Browse files
authored
Merge pull request #226 from zkonge/main
WIP: supports mutable IV in GcmParams, close #225
2 parents fa868f1 + c458902 commit 1daf63f

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
@@ -772,7 +772,7 @@ impl TryFrom<CK_MECHANISM_TYPE> for MechanismType {
772772
}
773773
}
774774

775-
#[derive(Copy, Debug, Clone)]
775+
#[derive(Debug)]
776776
#[non_exhaustive]
777777
/// Type defining a specific mechanism and its parameters
778778
pub enum Mechanism<'a> {

cryptoki/tests/basic.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1275,7 +1275,7 @@ fn sha256_digest() -> TestResult {
12751275
fn aes_gcm_no_aad() -> TestResult {
12761276
// Encrypt two blocks of zeros with AES-128-GCM
12771277
let key = vec![0; 16];
1278-
let iv = [0; 12];
1278+
let mut iv = [0; 12];
12791279
let aad = [];
12801280
let plain = [0; 32];
12811281
let expected_cipher_and_tag = [
@@ -1295,7 +1295,7 @@ fn aes_gcm_no_aad() -> TestResult {
12951295
Attribute::Encrypt(true),
12961296
];
12971297
let key_handle = session.create_object(&template)?;
1298-
let mechanism = Mechanism::AesGcm(GcmParams::new(&iv, &aad, 96.into()));
1298+
let mechanism = Mechanism::AesGcm(GcmParams::new(&mut iv, &aad, 96.into()));
12991299
let cipher_and_tag = session.encrypt(&mechanism, key_handle, &plain)?;
13001300
assert_eq!(expected_cipher_and_tag[..], cipher_and_tag[..]);
13011301
Ok(())
@@ -1307,7 +1307,7 @@ fn aes_gcm_with_aad() -> TestResult {
13071307
// Encrypt a block of zeros with AES-128-GCM.
13081308
// Use another block of zeros for AAD.
13091309
let key = vec![0; 16];
1310-
let iv = [0; 12];
1310+
let mut iv = [0; 12];
13111311
let aad = [0; 16];
13121312
let plain = [0; 16];
13131313
let expected_cipher_and_tag = [
@@ -1326,7 +1326,7 @@ fn aes_gcm_with_aad() -> TestResult {
13261326
Attribute::Encrypt(true),
13271327
];
13281328
let key_handle = session.create_object(&template)?;
1329-
let mechanism = Mechanism::AesGcm(GcmParams::new(&iv, &aad, 96.into()));
1329+
let mechanism = Mechanism::AesGcm(GcmParams::new(&mut iv, &aad, 96.into()));
13301330
let cipher_and_tag = session.encrypt(&mechanism, key_handle, &plain)?;
13311331
assert_eq!(expected_cipher_and_tag[..], cipher_and_tag[..]);
13321332
Ok(())

0 commit comments

Comments
 (0)