Skip to content

Commit

Permalink
core: Make more types non-exhaustive
Browse files Browse the repository at this point in the history
This patch marks more types as non-exhaustive that could be extended in
the future and that don’t need to be matched exhaustively:
- KeySerialization
- SignatureSerialization
- consent::Error

The only exhaustive types that could be realistically be extended in
the future are now the request and reply structs.  But marking these as
non-exhaustive would make it very complex for the backends to implement
these syscalls.  We can try to find a solution for that when we think
about alternative syscall implementations, e. g. using a builder
pattern.
  • Loading branch information
robin-nitrokey committed Dec 19, 2024
1 parent f6c1ba5 commit f97475e
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- As a consequence the type `pipe::TrussedInterchange` becomes a const`pipe::TRUSSED_INTERCHANGE`
- Updated `littlefs2` to 0.4.0.
- Made `Request`, `Reply`, `Error`, `Context`, `CoreContext`, `Mechanism`,
`ui::Status` non-exhaustive.
`KeySerialization`, `SignatureSerialization`, `consent::Error`, `ui::Status` non-exhaustive.
- Made `postcard_deserialize`, `postcard_serialize` and
`postcard_serialize_bytes` private.
- Changed `&PathBuf` to `&Path` where possible.
Expand Down
1 change: 1 addition & 0 deletions core/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub use ui::UiClient;
// to be fair, this is a programmer error,
// and could also just panic
#[derive(Copy, Clone, Debug)]
#[non_exhaustive]
pub enum ClientError {
Full,
Pending,
Expand Down
5 changes: 4 additions & 1 deletion core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub mod consent {
}

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[non_exhaustive]
pub enum Error {
FailedToInterrupt,
Interrupted,
Expand Down Expand Up @@ -417,8 +418,8 @@ impl NotBefore {
///
/// This enum does not provide access to the trait features. It is only intended for backends to
/// use in constant assertions to ensure that the correct features are enabled.
#[non_exhaustive]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum Client {
AttestationClient,
CertificateClient,
Expand Down Expand Up @@ -611,6 +612,7 @@ impl Mechanism {
}

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[non_exhaustive]
pub enum KeySerialization {
// Asn1Der,
Cose,
Expand All @@ -627,6 +629,7 @@ pub enum KeySerialization {
}

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[non_exhaustive]
pub enum SignatureSerialization {
Asn1Der,
// Cose,
Expand Down
6 changes: 6 additions & 0 deletions src/mechanisms/p256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ impl Sign for super::P256 {
SignatureSerialization::Raw => {
Signature::from_slice(&signature.to_untagged_bytes()).unwrap()
}
_ => {
return Err(Error::InvalidSerializationFormat);
}
};

// return signature
Expand Down Expand Up @@ -304,6 +307,9 @@ impl Sign for super::P256Prehashed {
SignatureSerialization::Raw => {
Signature::from_slice(&signature.to_untagged_bytes()).unwrap()
}
_ => {
return Err(Error::InvalidSerializationFormat);
}
};

// return signature
Expand Down
6 changes: 6 additions & 0 deletions src/mechanisms/p384.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ impl Sign for P384 {
Signature::from_slice(der.as_bytes()).unwrap()
}
SignatureSerialization::Raw => Signature::from_slice(&signature.to_bytes()).unwrap(),
_ => {
return Err(Error::InvalidSerializationFormat);
}
};

// return signature
Expand All @@ -235,6 +238,9 @@ impl Sign for P384Prehashed {
Signature::from_slice(der.as_bytes()).unwrap()
}
SignatureSerialization::Raw => Signature::from_slice(&signature.to_bytes()).unwrap(),
_ => {
return Err(Error::InvalidSerializationFormat);
}
};

// return signature
Expand Down
6 changes: 6 additions & 0 deletions src/mechanisms/p521.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ impl Sign for P521 {
Signature::from_slice(der.as_bytes()).unwrap()
}
SignatureSerialization::Raw => Signature::from_slice(&signature.to_bytes()).unwrap(),
_ => {
return Err(Error::InvalidSerializationFormat);
}
};

// return signature
Expand All @@ -238,6 +241,9 @@ impl Sign for P521Prehashed {
Signature::from_slice(der.as_bytes()).unwrap()
}
SignatureSerialization::Raw => Signature::from_slice(&signature.to_bytes()).unwrap(),
_ => {
return Err(Error::InvalidSerializationFormat);
}
};

// return signature
Expand Down

0 comments on commit f97475e

Please sign in to comment.