|
| 1 | +// Copyright 2024 Contributors to the Parsec project. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +//! Mechanisms of hash-based key derive function (HKDF) |
| 4 | +//! See: <https://docs.oasis-open.org/pkcs11/pkcs11-curr/v3.0/os/pkcs11-curr-v3.0-os.html#_Toc30061597> |
| 5 | +
|
| 6 | +use std::{convert::TryInto, marker::PhantomData, ptr::null_mut, slice}; |
| 7 | + |
| 8 | +use cryptoki_sys::{CKF_HKDF_SALT_DATA, CKF_HKDF_SALT_KEY, CKF_HKDF_SALT_NULL}; |
| 9 | + |
| 10 | +use crate::object::ObjectHandle; |
| 11 | + |
| 12 | +use super::MechanismType; |
| 13 | + |
| 14 | +/// The salt for the extract stage. |
| 15 | +#[derive(Debug, Clone, Copy)] |
| 16 | +pub enum HkdfSalt<'a> { |
| 17 | + /// CKF_HKDF_SALT_NULL no salt is supplied. |
| 18 | + Null, |
| 19 | + /// CKF_HKDF_SALT_DATA salt is supplied as a data in pSalt with length ulSaltLen. |
| 20 | + Data(&'a [u8]), |
| 21 | + /// CKF_HKDF_SALT_KEY salt is supplied as a key in hSaltKey |
| 22 | + Key(ObjectHandle), |
| 23 | +} |
| 24 | + |
| 25 | +/// HKDF parameters. |
| 26 | +/// |
| 27 | +/// This structure wraps a `CK_HKDF_PARAMS` structure. |
| 28 | +#[derive(Debug, Clone, Copy)] |
| 29 | +#[repr(transparent)] |
| 30 | +pub struct HkdfParams<'a> { |
| 31 | + inner: cryptoki_sys::CK_HKDF_PARAMS, |
| 32 | + /// Marker type to ensure we don't outlive the data |
| 33 | + _marker: PhantomData<&'a [u8]>, |
| 34 | +} |
| 35 | + |
| 36 | +impl<'a> HkdfParams<'a> { |
| 37 | + /// Construct parameters for hash-based key derive function (HKDF). |
| 38 | + /// |
| 39 | + /// # Arguments |
| 40 | + /// |
| 41 | + /// * `prf_hash_mechanism` - The base hash used for the HMAC in the underlying HKDF operation |
| 42 | + /// |
| 43 | + /// * `salt` - The salt for the extract stage, skip extract if `None`. |
| 44 | + /// |
| 45 | + /// * `info` - The info string for the expand stage, skip expand if `None`. |
| 46 | + pub fn new( |
| 47 | + prf_hash_mechanism: MechanismType, |
| 48 | + salt: Option<HkdfSalt>, |
| 49 | + info: Option<&'a [u8]>, |
| 50 | + ) -> Self { |
| 51 | + Self { |
| 52 | + inner: cryptoki_sys::CK_HKDF_PARAMS { |
| 53 | + bExtract: salt.is_some() as u8, |
| 54 | + bExpand: info.is_some() as u8, |
| 55 | + prfHashMechanism: *prf_hash_mechanism, |
| 56 | + ulSaltType: match salt { |
| 57 | + None | Some(HkdfSalt::Null) => CKF_HKDF_SALT_NULL, |
| 58 | + Some(HkdfSalt::Data(_)) => CKF_HKDF_SALT_DATA, |
| 59 | + Some(HkdfSalt::Key(_)) => CKF_HKDF_SALT_KEY, |
| 60 | + }, |
| 61 | + pSalt: if let Some(HkdfSalt::Data(data)) = salt { |
| 62 | + data.as_ptr() as *mut _ |
| 63 | + } else { |
| 64 | + null_mut() |
| 65 | + }, |
| 66 | + ulSaltLen: if let Some(HkdfSalt::Data(data)) = salt { |
| 67 | + data.len() |
| 68 | + .try_into() |
| 69 | + .expect("salt length does not fit in CK_ULONG") |
| 70 | + } else { |
| 71 | + 0 |
| 72 | + }, |
| 73 | + hSaltKey: if let Some(HkdfSalt::Key(key)) = salt { |
| 74 | + key.handle() |
| 75 | + } else { |
| 76 | + 0 |
| 77 | + }, |
| 78 | + pInfo: if let Some(info) = info { |
| 79 | + info.as_ptr() as *mut _ |
| 80 | + } else { |
| 81 | + null_mut() |
| 82 | + }, |
| 83 | + ulInfoLen: if let Some(info) = info { |
| 84 | + info.len() |
| 85 | + .try_into() |
| 86 | + .expect("salt length does not fit in CK_ULONG") |
| 87 | + } else { |
| 88 | + 0 |
| 89 | + }, |
| 90 | + }, |
| 91 | + _marker: PhantomData, |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /// Whether to execute the extract portion of HKDF. |
| 96 | + pub fn extract(&self) -> bool { |
| 97 | + self.inner.bExtract != 0 |
| 98 | + } |
| 99 | + |
| 100 | + /// Whether to execute the expand portion of HKDF. |
| 101 | + pub fn expand(&self) -> bool { |
| 102 | + self.inner.bExpand != 0 |
| 103 | + } |
| 104 | + |
| 105 | + /// The salt for the extract stage. |
| 106 | + pub fn salt(&self) -> HkdfSalt<'a> { |
| 107 | + match self.inner.ulSaltType { |
| 108 | + CKF_HKDF_SALT_NULL => HkdfSalt::Null, |
| 109 | + CKF_HKDF_SALT_DATA => HkdfSalt::Data(unsafe { |
| 110 | + slice::from_raw_parts(self.inner.pSalt, self.inner.ulSaltLen as _) |
| 111 | + }), |
| 112 | + CKF_HKDF_SALT_KEY => HkdfSalt::Key(ObjectHandle::new(self.inner.hSaltKey)), |
| 113 | + _ => unreachable!(), |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /// The info string for the expand stage. |
| 118 | + pub fn info(&self) -> &'a [u8] { |
| 119 | + unsafe { slice::from_raw_parts(self.inner.pInfo, self.inner.ulInfoLen as _) } |
| 120 | + } |
| 121 | +} |
0 commit comments