|
| 1 | +// Copyright 2025 Contributors to the Parsec project. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +//! Miscellaneous mechanisms: |
| 4 | +//! - Simple key derivation mechanisms |
| 5 | +//! See: <https://docs.oasis-open.org/pkcs11/pkcs11-spec/v3.1/os/pkcs11-spec-v3.1-os.html#_Toc111203697> |
| 6 | +
|
| 7 | +use std::marker::PhantomData; |
| 8 | + |
| 9 | +use cryptoki_sys::*; |
| 10 | + |
| 11 | +/// A parameter used as input for one of the simple key derivation mechanisms |
| 12 | +/// that takes a bytestring as input (CKM_CONCATENATE_BASE_AND_DATA, |
| 13 | +/// CKM_CONCATENATE_DATA_AND_BASE, CKM_XOR_BASE_AND_DATA). |
| 14 | +#[derive(Debug, Clone, Copy)] |
| 15 | +#[repr(transparent)] |
| 16 | +pub struct KeyDerivationStringData<'a> { |
| 17 | + inner: CK_KEY_DERIVATION_STRING_DATA, |
| 18 | + /// Marker type to ensure we don't outlive the data |
| 19 | + _marker: PhantomData<&'a [u8]>, |
| 20 | +} |
| 21 | + |
| 22 | +impl<'a> KeyDerivationStringData<'a> { |
| 23 | + /// Construct parameter for simple key derivation mechanisms that take a |
| 24 | + /// bytestring as one of their inputs. |
| 25 | + /// |
| 26 | + /// # Arguments |
| 27 | + /// |
| 28 | + /// * `data` - The bytestring to use as input to the key derivation method. |
| 29 | + pub fn new(data: &'a [u8]) -> Self { |
| 30 | + Self { |
| 31 | + inner: CK_KEY_DERIVATION_STRING_DATA { |
| 32 | + pData: data.as_ptr() as *mut _, |
| 33 | + ulLen: data |
| 34 | + .len() |
| 35 | + .try_into() |
| 36 | + .expect("length of data does not fit in CK_ULONG"), |
| 37 | + }, |
| 38 | + _marker: PhantomData, |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +/// A parameter indicating the index of the base key from which to extract the |
| 44 | +/// derived key. |
| 45 | +#[derive(Debug, Clone, Copy)] |
| 46 | +#[repr(transparent)] |
| 47 | +pub struct ExtractParams(CK_EXTRACT_PARAMS); |
| 48 | + |
| 49 | +impl ExtractParams { |
| 50 | + /// Construct parameter from index to extract the derived key from the base |
| 51 | + /// key. |
| 52 | + /// |
| 53 | + /// # Arguments |
| 54 | + /// |
| 55 | + /// * `index` - The index from which to extract the derived key from the base key. |
| 56 | + pub fn new(index: usize) -> Self { |
| 57 | + Self( |
| 58 | + index |
| 59 | + .try_into() |
| 60 | + .expect("given usize value does not fit into CK_ULONG"), |
| 61 | + ) |
| 62 | + } |
| 63 | +} |
0 commit comments