cryptoki/mechanism/
rsa.rs1use super::{Mechanism, MechanismType};
6use crate::error::{Error, Result};
7use crate::types::Ulong;
8use cryptoki_sys::*;
9use log::error;
10use std::convert::{TryFrom, TryInto};
11use std::ffi::c_void;
12use std::marker::PhantomData;
13use std::ops::Deref;
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16#[repr(transparent)]
17pub struct PkcsMgfType {
20 val: CK_RSA_PKCS_MGF_TYPE,
21}
22
23impl PkcsMgfType {
24 pub const MGF1_SHA1: PkcsMgfType = PkcsMgfType { val: CKG_MGF1_SHA1 };
26 pub const MGF1_SHA224: PkcsMgfType = PkcsMgfType {
28 val: CKG_MGF1_SHA224,
29 };
30 pub const MGF1_SHA256: PkcsMgfType = PkcsMgfType {
32 val: CKG_MGF1_SHA256,
33 };
34 pub const MGF1_SHA384: PkcsMgfType = PkcsMgfType {
36 val: CKG_MGF1_SHA384,
37 };
38 pub const MGF1_SHA512: PkcsMgfType = PkcsMgfType {
40 val: CKG_MGF1_SHA512,
41 };
42}
43
44impl Deref for PkcsMgfType {
45 type Target = CK_RSA_PKCS_MGF_TYPE;
46
47 fn deref(&self) -> &Self::Target {
48 &self.val
49 }
50}
51
52impl From<PkcsMgfType> for CK_RSA_PKCS_MGF_TYPE {
53 fn from(mgf_type: PkcsMgfType) -> Self {
54 *mgf_type
55 }
56}
57
58impl TryFrom<CK_RSA_PKCS_MGF_TYPE> for PkcsMgfType {
59 type Error = Error;
60
61 fn try_from(mgf_type: CK_RSA_PKCS_MGF_TYPE) -> Result<Self> {
62 match mgf_type {
63 CKG_MGF1_SHA1 => Ok(PkcsMgfType::MGF1_SHA1),
64 CKG_MGF1_SHA224 => Ok(PkcsMgfType::MGF1_SHA224),
65 CKG_MGF1_SHA256 => Ok(PkcsMgfType::MGF1_SHA256),
66 CKG_MGF1_SHA384 => Ok(PkcsMgfType::MGF1_SHA384),
67 CKG_MGF1_SHA512 => Ok(PkcsMgfType::MGF1_SHA512),
68 other => {
69 error!(
70 "Mask Generation Function type {} is not one of the valid values.",
71 other
72 );
73 Err(Error::InvalidValue)
74 }
75 }
76 }
77}
78
79#[derive(Debug, Clone, Copy)]
80pub struct PkcsOaepSource<'a>(Option<&'a [u8]>);
83
84impl<'a> PkcsOaepSource<'a> {
85 pub fn empty() -> Self {
89 Self(None)
90 }
91
92 pub fn data_specified(source_data: &'a [u8]) -> Self {
94 Self(Some(source_data))
95 }
96
97 pub(crate) fn source_ptr(&self) -> *const c_void {
98 if let Some(source_data) = self.0 {
99 source_data.as_ptr() as _
100 } else {
101 std::ptr::null()
102 }
103 }
104
105 pub(crate) fn source_len(&self) -> Ulong {
106 self.0
107 .unwrap_or_default()
108 .len()
109 .try_into()
110 .expect("usize can not fit in CK_ULONG")
111 }
112
113 pub(crate) fn source_type(&self) -> CK_RSA_PKCS_OAEP_SOURCE_TYPE {
114 CKZ_DATA_SPECIFIED
115 }
116}
117
118#[derive(Copy, Debug, Clone)]
120#[repr(C)]
121pub struct PkcsPssParams {
122 pub hash_alg: MechanismType,
127 pub mgf: PkcsMgfType,
129 pub s_len: Ulong,
132}
133
134#[derive(Copy, Debug, Clone)]
136#[repr(C)]
137#[cfg_attr(windows, repr(packed))]
138pub struct PkcsOaepParams<'a> {
139 hash_alg: MechanismType,
142 mgf: PkcsMgfType,
144 source: CK_RSA_PKCS_OAEP_SOURCE_TYPE,
146 source_data: *const c_void,
148 source_data_len: Ulong,
150 _marker: PhantomData<&'a [u8]>,
152}
153
154impl<'a> PkcsOaepParams<'a> {
155 pub fn new(
164 hash_alg: MechanismType,
165 mgf: PkcsMgfType,
166 encoding_parameter: PkcsOaepSource<'a>,
167 ) -> Self {
168 PkcsOaepParams {
169 hash_alg,
170 mgf,
171 source: encoding_parameter.source_type(),
172 source_data: encoding_parameter.source_ptr(),
173 source_data_len: encoding_parameter.source_len(),
174 _marker: PhantomData,
175 }
176 }
177
178 pub fn hash_alg(&self) -> MechanismType {
180 self.hash_alg
181 }
182}
183
184impl<'a> From<PkcsOaepParams<'a>> for Mechanism<'a> {
185 fn from(pkcs_oaep_params: PkcsOaepParams<'a>) -> Self {
186 Mechanism::RsaPkcsOaep(pkcs_oaep_params)
187 }
188}