Skip to content

Commit 5607757

Browse files
committed
Remove the aliasing on the attribute level
Signed-off-by: Jakub Jelen <jjelen@redhat.com>
1 parent 5888f9e commit 5607757

File tree

3 files changed

+85
-32
lines changed

3 files changed

+85
-32
lines changed

cryptoki/src/object.rs

Lines changed: 80 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,6 @@ pub enum AttributeType {
150150
Seed,
151151
/// Algorithm-specific parameter set
152152
ParameterSet,
153-
/// ML-KEM parameter set
154-
MlKemParameterSet,
155-
/// ML-DSA parameter set
156-
MlDsaParameterSet,
157153
}
158154

159155
impl AttributeType {
@@ -325,8 +321,6 @@ impl From<AttributeType> for CK_ATTRIBUTE_TYPE {
325321
AttributeType::KeyType => CKA_KEY_TYPE,
326322
AttributeType::Label => CKA_LABEL,
327323
AttributeType::Local => CKA_LOCAL,
328-
AttributeType::MlDsaParameterSet => CKA_PARAMETER_SET,
329-
AttributeType::MlKemParameterSet => CKA_PARAMETER_SET,
330324
AttributeType::Modifiable => CKA_MODIFIABLE,
331325
AttributeType::Modulus => CKA_MODULUS,
332326
AttributeType::ModulusBits => CKA_MODULUS_BITS,
@@ -510,10 +504,6 @@ pub enum Attribute {
510504
Label(Vec<u8>),
511505
/// Indicates if the key was generated locally or copied from a locally created object
512506
Local(bool),
513-
/// ML-DSA parameter set
514-
MlDsaParameterSet(MlDsaParameterSetType),
515-
/// ML-KEM parameter set
516-
MlKemParameterSet(MlKemParameterSetType),
517507
/// Determines if the object can be modified
518508
Modifiable(bool),
519509
/// Modulus value of a key
@@ -526,8 +516,8 @@ pub enum Attribute {
526516
ObjectId(Vec<u8>),
527517
/// DER encoding of the attribute certificate's subject field
528518
Owner(Vec<u8>),
529-
/// Algorithm specific parameter set
530-
ParameterSet(Vec<u8>),
519+
/// Algorithm specific parameter set, now used for ML-DSA and ML-KEM algorithms
520+
ParameterSet(ParameterSetType),
531521
/// Prime number value of a key
532522
Prime(Vec<u8>),
533523
/// The prime `p` of an RSA private key
@@ -618,8 +608,6 @@ impl Attribute {
618608
Attribute::KeyType(_) => AttributeType::KeyType,
619609
Attribute::Label(_) => AttributeType::Label,
620610
Attribute::Local(_) => AttributeType::Local,
621-
Attribute::MlDsaParameterSet(_) => AttributeType::MlDsaParameterSet,
622-
Attribute::MlKemParameterSet(_) => AttributeType::MlKemParameterSet,
623611
Attribute::Modifiable(_) => AttributeType::Modifiable,
624612
Attribute::Modulus(_) => AttributeType::Modulus,
625613
Attribute::ModulusBits(_) => AttributeType::ModulusBits,
@@ -707,7 +695,7 @@ impl Attribute {
707695
Attribute::ModulusBits(_) => size_of::<CK_ULONG>(),
708696
Attribute::ObjectId(bytes) => bytes.len(),
709697
Attribute::Owner(bytes) => bytes.len(),
710-
Attribute::ParameterSet(bytes) => bytes.len(),
698+
Attribute::ParameterSet(_) => size_of::<CK_ULONG>(),
711699
Attribute::Prime(bytes) => bytes.len(),
712700
Attribute::Prime1(bytes) => bytes.len(),
713701
Attribute::Prime2(bytes) => bytes.len(),
@@ -721,8 +709,6 @@ impl Attribute {
721709
Attribute::Value(bytes) => bytes.len(),
722710
Attribute::ValueLen(_) => size_of::<CK_ULONG>(),
723711
Attribute::EndDate(_) | Attribute::StartDate(_) => size_of::<CK_DATE>(),
724-
Attribute::MlKemParameterSet(_) => size_of::<CK_ML_KEM_PARAMETER_SET_TYPE>(),
725-
Attribute::MlDsaParameterSet(_) => size_of::<CK_ML_DSA_PARAMETER_SET_TYPE>(),
726712

727713
Attribute::AllowedMechanisms(mechanisms) => {
728714
size_of::<CK_MECHANISM_TYPE>() * mechanisms.len()
@@ -790,7 +776,6 @@ impl Attribute {
790776
| Attribute::Issuer(bytes)
791777
| Attribute::Label(bytes)
792778
| Attribute::ObjectId(bytes)
793-
| Attribute::ParameterSet(bytes)
794779
| Attribute::Prime(bytes)
795780
| Attribute::Prime1(bytes)
796781
| Attribute::Prime2(bytes)
@@ -808,14 +793,13 @@ impl Attribute {
808793
| Attribute::VendorDefined((_, bytes))
809794
| Attribute::Id(bytes) => bytes.as_ptr() as *mut c_void,
810795
// Unique types
796+
Attribute::ParameterSet(val) => val as *const _ as *mut c_void,
811797
Attribute::CertificateType(certificate_type) => {
812798
certificate_type as *const _ as *mut c_void
813799
}
814800
Attribute::Class(object_class) => object_class as *const _ as *mut c_void,
815801
Attribute::KeyGenMechanism(mech) => mech as *const _ as *mut c_void,
816802
Attribute::KeyType(key_type) => key_type as *const _ as *mut c_void,
817-
Attribute::MlKemParameterSet(p) => p as *const _ as *mut c_void,
818-
Attribute::MlDsaParameterSet(p) => p as *const _ as *mut c_void,
819803
Attribute::AllowedMechanisms(mechanisms) => mechanisms.as_ptr() as *mut c_void,
820804
Attribute::EndDate(date) | Attribute::StartDate(date) => {
821805
date as *const _ as *mut c_void
@@ -923,7 +907,6 @@ impl TryFrom<CK_ATTRIBUTE> for Attribute {
923907
}
924908
AttributeType::Issuer => Ok(Attribute::Issuer(val.to_vec())),
925909
AttributeType::Label => Ok(Attribute::Label(val.to_vec())),
926-
AttributeType::ParameterSet => Ok(Attribute::ParameterSet(val.to_vec())),
927910
AttributeType::Prime => Ok(Attribute::Prime(val.to_vec())),
928911
AttributeType::Prime1 => Ok(Attribute::Prime1(val.to_vec())),
929912
AttributeType::Prime2 => Ok(Attribute::Prime2(val.to_vec())),
@@ -941,6 +924,9 @@ impl TryFrom<CK_ATTRIBUTE> for Attribute {
941924
AttributeType::Value => Ok(Attribute::Value(val.to_vec())),
942925
AttributeType::Id => Ok(Attribute::Id(val.to_vec())),
943926
// Unique types
927+
AttributeType::ParameterSet => Ok(Attribute::ParameterSet(ParameterSetType {
928+
val: CK_ULONG::from_ne_bytes(val.try_into()?).into(),
929+
})),
944930
AttributeType::CertificateType => Ok(Attribute::CertificateType(
945931
CK_CERTIFICATE_TYPE::from_ne_bytes(val.try_into()?).try_into()?,
946932
)),
@@ -953,12 +939,6 @@ impl TryFrom<CK_ATTRIBUTE> for Attribute {
953939
AttributeType::KeyType => Ok(Attribute::KeyType(
954940
CK_KEY_TYPE::from_ne_bytes(val.try_into()?).try_into()?,
955941
)),
956-
AttributeType::MlKemParameterSet => Ok(Attribute::MlKemParameterSet(
957-
CK_ML_KEM_PARAMETER_SET_TYPE::from_ne_bytes(val.try_into()?).try_into()?,
958-
)),
959-
AttributeType::MlDsaParameterSet => Ok(Attribute::MlDsaParameterSet(
960-
CK_ML_DSA_PARAMETER_SET_TYPE::from_ne_bytes(val.try_into()?).try_into()?,
961-
)),
962942
AttributeType::AllowedMechanisms => {
963943
let val = unsafe {
964944
std::slice::from_raw_parts(
@@ -1061,6 +1041,63 @@ impl std::fmt::UpperHex for ObjectHandle {
10611041
}
10621042
}
10631043

1044+
#[derive(Copy, Debug, Clone, PartialEq, Eq)]
1045+
#[repr(transparent)]
1046+
/// Generic parameter set
1047+
pub struct ParameterSetType {
1048+
val: Ulong,
1049+
}
1050+
1051+
impl ParameterSetType {
1052+
pub(crate) fn stringify(val: Ulong) -> String {
1053+
format!("unknown ({:08x})", *val)
1054+
}
1055+
}
1056+
1057+
impl std::fmt::Display for ParameterSetType {
1058+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
1059+
write!(f, "{}", ParameterSetType::stringify(self.val))
1060+
}
1061+
}
1062+
1063+
impl Deref for ParameterSetType {
1064+
type Target = Ulong;
1065+
1066+
fn deref(&self) -> &Self::Target {
1067+
&self.val
1068+
}
1069+
}
1070+
1071+
impl From<ParameterSetType> for Ulong {
1072+
fn from(val: ParameterSetType) -> Self {
1073+
*val
1074+
}
1075+
}
1076+
1077+
impl TryFrom<Ulong> for ParameterSetType {
1078+
type Error = Error;
1079+
1080+
fn try_from(val: Ulong) -> Result<Self> {
1081+
Ok(ParameterSetType { val })
1082+
}
1083+
}
1084+
1085+
impl From<MlKemParameterSetType> for ParameterSetType {
1086+
fn from(val: MlKemParameterSetType) -> Self {
1087+
ParameterSetType {
1088+
val: Ulong::new(*val),
1089+
}
1090+
}
1091+
}
1092+
1093+
impl From<MlDsaParameterSetType> for ParameterSetType {
1094+
fn from(val: MlDsaParameterSetType) -> Self {
1095+
ParameterSetType {
1096+
val: Ulong::new(*val),
1097+
}
1098+
}
1099+
}
1100+
10641101
#[derive(Copy, Debug, Clone, PartialEq, Eq)]
10651102
#[repr(transparent)]
10661103
/// Identifier of the ML-KEM parameter set
@@ -1128,6 +1165,14 @@ impl TryFrom<CK_ML_KEM_PARAMETER_SET_TYPE> for MlKemParameterSetType {
11281165
}
11291166
}
11301167

1168+
impl From<ParameterSetType> for MlKemParameterSetType {
1169+
fn from(val: ParameterSetType) -> Self {
1170+
MlKemParameterSetType {
1171+
val: CK_ULONG::from(*val),
1172+
}
1173+
}
1174+
}
1175+
11311176
#[derive(Copy, Debug, Clone, PartialEq, Eq)]
11321177
#[repr(transparent)]
11331178
/// Identifier of the ML-DSA parameter set
@@ -1189,6 +1234,14 @@ impl TryFrom<CK_ML_DSA_PARAMETER_SET_TYPE> for MlDsaParameterSetType {
11891234
}
11901235
}
11911236

1237+
impl From<ParameterSetType> for MlDsaParameterSetType {
1238+
fn from(val: ParameterSetType) -> Self {
1239+
MlDsaParameterSetType {
1240+
val: CK_ULONG::from(*val),
1241+
}
1242+
}
1243+
}
1244+
11921245
#[derive(Copy, Debug, Clone, PartialEq, Eq)]
11931246
#[repr(transparent)]
11941247
/// Identifier of the class of an object

cryptoki/tests/ml_dsa.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn ml_dsa() -> TestResult {
3636
// pub key template
3737
let pub_key_template = vec![
3838
Attribute::Token(true),
39-
Attribute::MlDsaParameterSet(MlDsaParameterSetType::ML_DSA_65),
39+
Attribute::ParameterSet(MlDsaParameterSetType::ML_DSA_65.into()),
4040
Attribute::Verify(true),
4141
];
4242

@@ -129,7 +129,7 @@ fn ml_dsa_multipart() -> TestResult {
129129
// pub key template
130130
let pub_key_template = vec![
131131
Attribute::Token(true),
132-
Attribute::MlDsaParameterSet(MlDsaParameterSetType::ML_DSA_87),
132+
Attribute::ParameterSet(MlDsaParameterSetType::ML_DSA_87.into()),
133133
Attribute::Verify(true),
134134
];
135135

@@ -199,7 +199,7 @@ fn ml_dsa_hash() -> TestResult {
199199
// pub key template
200200
let pub_key_template = vec![
201201
Attribute::Token(true),
202-
Attribute::MlDsaParameterSet(MlDsaParameterSetType::ML_DSA_44),
202+
Attribute::ParameterSet(MlDsaParameterSetType::ML_DSA_44.into()),
203203
Attribute::Verify(true),
204204
];
205205

@@ -294,7 +294,7 @@ fn ml_dsa_hashes() -> TestResult {
294294
// pub key template
295295
let pub_key_template = vec![
296296
Attribute::Token(true),
297-
Attribute::MlDsaParameterSet(MlDsaParameterSetType::ML_DSA_65),
297+
Attribute::ParameterSet(MlDsaParameterSetType::ML_DSA_65.into()),
298298
Attribute::Verify(true),
299299
];
300300

cryptoki/tests/ml_kem.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fn ml_kem() -> TestResult {
3434
// pub key template
3535
let pub_key_template = vec![
3636
Attribute::Token(true),
37-
Attribute::MlKemParameterSet(MlKemParameterSetType::ML_KEM_768),
37+
Attribute::ParameterSet(MlKemParameterSetType::ML_KEM_768.into()),
3838
Attribute::Encapsulate(true),
3939
];
4040

0 commit comments

Comments
 (0)