Skip to content

Commit 03f1155

Browse files
committed
Remove the aliasing on the attribute level
Signed-off-by: Jakub Jelen <[email protected]>
1 parent 00ed0b7 commit 03f1155

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
@@ -148,10 +148,6 @@ pub enum AttributeType {
148148
Seed,
149149
/// Algorithm-specific parameter set
150150
ParameterSet,
151-
/// ML-KEM parameter set
152-
MlKemParameterSet,
153-
/// ML-DSA parameter set
154-
MlDsaParameterSet,
155151
}
156152

157153
impl AttributeType {
@@ -323,8 +319,6 @@ impl From<AttributeType> for CK_ATTRIBUTE_TYPE {
323319
AttributeType::KeyType => CKA_KEY_TYPE,
324320
AttributeType::Label => CKA_LABEL,
325321
AttributeType::Local => CKA_LOCAL,
326-
AttributeType::MlDsaParameterSet => CKA_PARAMETER_SET,
327-
AttributeType::MlKemParameterSet => CKA_PARAMETER_SET,
328322
AttributeType::Modifiable => CKA_MODIFIABLE,
329323
AttributeType::Modulus => CKA_MODULUS,
330324
AttributeType::ModulusBits => CKA_MODULUS_BITS,
@@ -508,10 +502,6 @@ pub enum Attribute {
508502
Label(Vec<u8>),
509503
/// Indicates if the key was generated locally or copied from a locally created object
510504
Local(bool),
511-
/// ML-DSA parameter set
512-
MlDsaParameterSet(MlDsaParameterSetType),
513-
/// ML-KEM parameter set
514-
MlKemParameterSet(MlKemParameterSetType),
515505
/// Determines if the object can be modified
516506
Modifiable(bool),
517507
/// Modulus value of a key
@@ -524,8 +514,8 @@ pub enum Attribute {
524514
ObjectId(Vec<u8>),
525515
/// DER encoding of the attribute certificate's subject field
526516
Owner(Vec<u8>),
527-
/// Algorithm specific parameter set
528-
ParameterSet(Vec<u8>),
517+
/// Algorithm specific parameter set, now used for ML-DSA and ML-KEM algorithms
518+
ParameterSet(ParameterSetType),
529519
/// Prime number value of a key
530520
Prime(Vec<u8>),
531521
/// The prime `p` of an RSA private key
@@ -616,8 +606,6 @@ impl Attribute {
616606
Attribute::KeyType(_) => AttributeType::KeyType,
617607
Attribute::Label(_) => AttributeType::Label,
618608
Attribute::Local(_) => AttributeType::Local,
619-
Attribute::MlDsaParameterSet(_) => AttributeType::MlDsaParameterSet,
620-
Attribute::MlKemParameterSet(_) => AttributeType::MlKemParameterSet,
621609
Attribute::Modifiable(_) => AttributeType::Modifiable,
622610
Attribute::Modulus(_) => AttributeType::Modulus,
623611
Attribute::ModulusBits(_) => AttributeType::ModulusBits,
@@ -705,7 +693,7 @@ impl Attribute {
705693
Attribute::ModulusBits(_) => size_of::<CK_ULONG>(),
706694
Attribute::ObjectId(bytes) => bytes.len(),
707695
Attribute::Owner(bytes) => bytes.len(),
708-
Attribute::ParameterSet(bytes) => bytes.len(),
696+
Attribute::ParameterSet(_) => size_of::<CK_ULONG>(),
709697
Attribute::Prime(bytes) => bytes.len(),
710698
Attribute::Prime1(bytes) => bytes.len(),
711699
Attribute::Prime2(bytes) => bytes.len(),
@@ -719,8 +707,6 @@ impl Attribute {
719707
Attribute::Value(bytes) => bytes.len(),
720708
Attribute::ValueLen(_) => size_of::<CK_ULONG>(),
721709
Attribute::EndDate(_) | Attribute::StartDate(_) => size_of::<CK_DATE>(),
722-
Attribute::MlKemParameterSet(_) => size_of::<CK_ML_KEM_PARAMETER_SET_TYPE>(),
723-
Attribute::MlDsaParameterSet(_) => size_of::<CK_ML_DSA_PARAMETER_SET_TYPE>(),
724710

725711
Attribute::AllowedMechanisms(mechanisms) => {
726712
size_of::<CK_MECHANISM_TYPE>() * mechanisms.len()
@@ -788,7 +774,6 @@ impl Attribute {
788774
| Attribute::Issuer(bytes)
789775
| Attribute::Label(bytes)
790776
| Attribute::ObjectId(bytes)
791-
| Attribute::ParameterSet(bytes)
792777
| Attribute::Prime(bytes)
793778
| Attribute::Prime1(bytes)
794779
| Attribute::Prime2(bytes)
@@ -806,14 +791,13 @@ impl Attribute {
806791
| Attribute::VendorDefined((_, bytes))
807792
| Attribute::Id(bytes) => bytes.as_ptr() as *mut c_void,
808793
// Unique types
794+
Attribute::ParameterSet(val) => val as *const _ as *mut c_void,
809795
Attribute::CertificateType(certificate_type) => {
810796
certificate_type as *const _ as *mut c_void
811797
}
812798
Attribute::Class(object_class) => object_class as *const _ as *mut c_void,
813799
Attribute::KeyGenMechanism(mech) => mech as *const _ as *mut c_void,
814800
Attribute::KeyType(key_type) => key_type as *const _ as *mut c_void,
815-
Attribute::MlKemParameterSet(p) => p as *const _ as *mut c_void,
816-
Attribute::MlDsaParameterSet(p) => p as *const _ as *mut c_void,
817801
Attribute::AllowedMechanisms(mechanisms) => mechanisms.as_ptr() as *mut c_void,
818802
Attribute::EndDate(date) | Attribute::StartDate(date) => {
819803
date as *const _ as *mut c_void
@@ -921,7 +905,6 @@ impl TryFrom<CK_ATTRIBUTE> for Attribute {
921905
}
922906
AttributeType::Issuer => Ok(Attribute::Issuer(val.to_vec())),
923907
AttributeType::Label => Ok(Attribute::Label(val.to_vec())),
924-
AttributeType::ParameterSet => Ok(Attribute::ParameterSet(val.to_vec())),
925908
AttributeType::Prime => Ok(Attribute::Prime(val.to_vec())),
926909
AttributeType::Prime1 => Ok(Attribute::Prime1(val.to_vec())),
927910
AttributeType::Prime2 => Ok(Attribute::Prime2(val.to_vec())),
@@ -939,6 +922,9 @@ impl TryFrom<CK_ATTRIBUTE> for Attribute {
939922
AttributeType::Value => Ok(Attribute::Value(val.to_vec())),
940923
AttributeType::Id => Ok(Attribute::Id(val.to_vec())),
941924
// Unique types
925+
AttributeType::ParameterSet => Ok(Attribute::ParameterSet(ParameterSetType {
926+
val: CK_ULONG::from_ne_bytes(val.try_into()?).into(),
927+
})),
942928
AttributeType::CertificateType => Ok(Attribute::CertificateType(
943929
CK_CERTIFICATE_TYPE::from_ne_bytes(val.try_into()?).try_into()?,
944930
)),
@@ -951,12 +937,6 @@ impl TryFrom<CK_ATTRIBUTE> for Attribute {
951937
AttributeType::KeyType => Ok(Attribute::KeyType(
952938
CK_KEY_TYPE::from_ne_bytes(val.try_into()?).try_into()?,
953939
)),
954-
AttributeType::MlKemParameterSet => Ok(Attribute::MlKemParameterSet(
955-
CK_ML_KEM_PARAMETER_SET_TYPE::from_ne_bytes(val.try_into()?).try_into()?,
956-
)),
957-
AttributeType::MlDsaParameterSet => Ok(Attribute::MlDsaParameterSet(
958-
CK_ML_DSA_PARAMETER_SET_TYPE::from_ne_bytes(val.try_into()?).try_into()?,
959-
)),
960940
AttributeType::AllowedMechanisms => {
961941
let val = unsafe {
962942
std::slice::from_raw_parts(
@@ -1059,6 +1039,63 @@ impl std::fmt::UpperHex for ObjectHandle {
10591039
}
10601040
}
10611041

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

1166+
impl From<ParameterSetType> for MlKemParameterSetType {
1167+
fn from(val: ParameterSetType) -> Self {
1168+
MlKemParameterSetType {
1169+
val: CK_ULONG::from(*val),
1170+
}
1171+
}
1172+
}
1173+
11291174
#[derive(Copy, Debug, Clone, PartialEq, Eq)]
11301175
#[repr(transparent)]
11311176
/// Identifier of the ML-DSA parameter set
@@ -1187,6 +1232,14 @@ impl TryFrom<CK_ML_DSA_PARAMETER_SET_TYPE> for MlDsaParameterSetType {
11871232
}
11881233
}
11891234

1235+
impl From<ParameterSetType> for MlDsaParameterSetType {
1236+
fn from(val: ParameterSetType) -> Self {
1237+
MlDsaParameterSetType {
1238+
val: CK_ULONG::from(*val),
1239+
}
1240+
}
1241+
}
1242+
11901243
#[derive(Copy, Debug, Clone, PartialEq, Eq)]
11911244
#[repr(transparent)]
11921245
/// 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)