Skip to content

Commit

Permalink
Add arbitrary::Arbitrary implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
robin-nitrokey committed Jun 25, 2024
1 parent 8a48754 commit ec37876
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:
- name: Check library
run: |
cargo check
cargo check --features arbitrary
cargo check --features get-info-full
cargo check --features large-blobs
cargo check --all-features
Expand Down
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ description = "no_std friendly types for FIDO CTAP"
repository = "https://github.com/trussed-dev/ctap-types"

[dependencies]
arbitrary = { version = "1.3.2", features = ["derive"], optional = true }
bitflags = "1.3"
cbor-smol = "0.4"
cosey = "0.3.1"
Expand All @@ -25,6 +26,10 @@ hex-literal = "0.4.1"
serde_test = "1.0.176"

[features]
std = []

# implements arbitrary::Arbitrary for requests
arbitrary = ["dep:arbitrary", "std"]
# enables all fields for ctap2::get_info
get-info-full = []
# enables support for implementing the large-blobs extension, see src/sizes.rs
Expand Down
31 changes: 31 additions & 0 deletions src/arbitrary.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use arbitrary::{Arbitrary, Unstructured, Result};

use crate::ctap1;

// cannot be derived because of missing impl for &[T; N]
impl<'a> Arbitrary<'a> for ctap1::authenticate::Request<'a> {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
let control_byte = Arbitrary::arbitrary(u)?;
let challenge = u.bytes(32)?.try_into().unwrap();
let app_id = u.bytes(32)?.try_into().unwrap();
let key_handle = Arbitrary::arbitrary(u)?;
Ok(Self {
control_byte,
challenge,
app_id,
key_handle,
})
}
}

// cannot be derived because of missing impl for &[T; N]
impl<'a> Arbitrary<'a> for ctap1::register::Request<'a> {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
let challenge = u.bytes(32)?.try_into().unwrap();
let app_id = u.bytes(32)?.try_into().unwrap();
Ok(Self {
challenge,
app_id,
})
}
}
2 changes: 2 additions & 0 deletions src/ctap1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ pub mod register {

#[repr(u8)]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum ControlByte {
// Conor:
// I think U2F check-only maps to FIDO2 MakeCredential with the credID in the excludeList,
Expand Down Expand Up @@ -109,6 +110,7 @@ pub type RegisterResponse = register::Response;
pub type AuthenticateResponse = authenticate::Response;

#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[allow(clippy::large_enum_variant)]
/// Enum of all CTAP1 requests.
pub enum Request<'a> {
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg_attr(not(test), no_std)]
#![cfg_attr(all(not(test), not(feature = "std")), no_std)]
// #![no_std]

//! `ctap-types` maps the various types involved in the FIDO CTAP protocol
Expand All @@ -25,6 +25,8 @@ pub use heapless_bytes;
pub use heapless_bytes::Bytes;
pub use serde_bytes::ByteArray;

#[cfg(feature = "arbitrary")]
mod arbitrary;
pub mod authenticator;
pub mod ctap1;
pub mod ctap2;
Expand Down

0 comments on commit ec37876

Please sign in to comment.