Skip to content
This repository was archived by the owner on Jul 11, 2025. It is now read-only.

COSE serialization #1

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/dilithium.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ use pqcrypto::sign::dilithium3;
use pqcrypto::sign::dilithium5;

mod oids {
#[cfg(feature = "dilithium2")]
pub const DILITHIUM2: pkcs8::ObjectIdentifier =
pkcs8::ObjectIdentifier::new_unwrap("1.3.6.1.4.1.2.267.7.4.4");
#[cfg(feature = "dilithium3")]
pub const DILITHIUM3: pkcs8::ObjectIdentifier =
pkcs8::ObjectIdentifier::new_unwrap("1.3.6.1.4.1.2.267.7.6.5");
#[cfg(feature = "dilithium5")]
pub const DILITHIUM5: pkcs8::ObjectIdentifier =
pkcs8::ObjectIdentifier::new_unwrap("1.3.6.1.4.1.2.267.7.8.7");
}
Expand Down Expand Up @@ -198,7 +201,30 @@ fn serialize_key(
.material;

let serialized_key: Bytes<MAX_SERIALIZED_KEY_LENGTH> = match request.format {
KeySerialization::Raw | KeySerialization::Cose => {
KeySerialization::Cose => {
let pub_key_der = pkcs8::SubjectPublicKeyInfoRef::from_der(&pub_key_der).map_err(|_| Error::InvalidSerializationFormat)?;
let pub_key_bytes = pub_key_der.subject_public_key.raw_bytes();
match pub_key_der.algorithm.oid {
#[cfg(feature = "dilithium2")]
oids::DILITHIUM2 => {
let cose_pk = cosey::Dilithium2PublicKey { pk: Bytes::from_slice(pub_key_bytes).unwrap() };
trussed::cbor_serialize_bytes(&cose_pk).map_err(|_| Error::CborError)?
}
#[cfg(feature = "dilithium3")]
oids::DILITHIUM3 => {
let cose_pk = cosey::Dilithium3PublicKey { pk: Bytes::from_slice(pub_key_bytes).unwrap() };
trussed::cbor_serialize_bytes(&cose_pk).map_err(|_| Error::CborError)?
}
#[cfg(feature = "dilithium5")]
oids::DILITHIUM5 => {
let cose_pk = cosey::Dilithium5PublicKey { pk: Bytes::from_slice(pub_key_bytes).unwrap() };
trussed::cbor_serialize_bytes(&cose_pk).map_err(|_| Error::CborError)?
}
_ => return Err(Error::WrongKeyKind)
}
}

KeySerialization::Raw => {
let mut data = SerializedKey::new();
data.extend_from_slice(&pub_key_der[..])
.map_err(|_| Error::InternalError)?;
Expand All @@ -220,8 +246,11 @@ fn deserialize_pkcs_key(

// TODO: check key lengths for each of these
match pub_key.algorithm.oid {
#[cfg(feature = "dilithium2")]
oids::DILITHIUM2 => {}
#[cfg(feature = "dilithium3")]
oids::DILITHIUM3 => {}
#[cfg(feature = "dilithium5")]
oids::DILITHIUM5 => {}
_ => return Err(Error::InvalidSerializationFormat),
}
Expand Down