-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added CKM_GENERIC_SECRET_KEY_GEN; Added prettier `MechanismInfo` fmt. Signed-off-by: nikita.kalinichenko <[email protected]>
- Loading branch information
Showing
3 changed files
with
113 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright 2021 Contributors to the Parsec project. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
//! Mechanisms of key derivation by data encryption | ||
//! See: https://docs.oasis-open.org/pkcs11/pkcs11-spec/v3.1/os/pkcs11-spec-v3.1-os.html#_Toc111203514 | ||
|
||
use std::{convert::TryInto, marker::PhantomData, slice}; | ||
|
||
/// AES CBC derivation parameters. | ||
/// | ||
/// The mechanisms will function by performing the encryption over the data provided using the base | ||
/// key. The resulting cipher text shall be used to create the key value of the resulting key. | ||
/// | ||
/// This structure wraps a `CK_AES_CBC_ENCRYPT_DATA_PARAMS` structure. | ||
#[derive(Debug, Clone, Copy)] | ||
#[repr(transparent)] | ||
pub struct AesCbcDeriveParams<'a> { | ||
inner: cryptoki_sys::CK_AES_CBC_ENCRYPT_DATA_PARAMS, | ||
|
||
/// Marker type to ensure we don't outlive the data | ||
_marker: PhantomData<&'a [u8]>, | ||
} | ||
|
||
impl<'a> AesCbcDeriveParams<'a> { | ||
/// Construct parameters for key derivation via encryption (EKDF). | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `iv` - The initialization vector | ||
/// | ||
/// * `data` - Data that will be encryption with the base key to obtain | ||
/// the new key from the resulted cypher. | ||
pub fn new(iv: [u8; 16], data: &'a [u8]) -> Self { | ||
Self { | ||
inner: cryptoki_sys::CK_AES_CBC_ENCRYPT_DATA_PARAMS { | ||
iv, | ||
pData: data.as_ptr() as *mut _, | ||
length: data | ||
.len() | ||
.try_into() | ||
.expect("data length does not fit in CK_ULONG"), | ||
}, | ||
_marker: PhantomData, | ||
} | ||
} | ||
|
||
/// The initialization vector. | ||
pub fn iv(&self) -> &'a [u8] { | ||
unsafe { slice::from_raw_parts(self.inner.iv.as_ptr(), self.inner.iv.len()) } | ||
} | ||
|
||
/// The data. | ||
pub fn data(&self) -> &'a [u8] { | ||
unsafe { slice::from_raw_parts(self.inner.pData, self.inner.length as _) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters