Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

COSE serialization #1

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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