examples-features/ctap: Add the PrivateKey type
Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
diff --git a/Cargo.toml b/Cargo.toml
index 002ccaa..62b382c 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -26,6 +26,9 @@
# with the nightly toolchain used by libtock-rs.
serde = { version = "=1.0.114", default-features = false, features = ["derive"] }
ctap2-authenticator = { git = "https://gitlab.com/ctap2-authenticator/ctap2-authenticator.git" }
+p256 = { version = "0.5.0" , default-features = false, features = ["arithmetic", "ecdsa", "ecdsa-core"] }
+subtle = { version = "2.3.0", default-features = false, features = ["i128"] }
+generic-array = { version = "0.14.3" }
[[example]]
name = "alloc_error"
diff --git a/examples-features/ctap.rs b/examples-features/ctap.rs
index 63753ea..9715af9 100644
--- a/examples-features/ctap.rs
+++ b/examples-features/ctap.rs
@@ -15,11 +15,38 @@
use ctap2_authenticator::{
AuthenticatorPlatform, CredentialDescriptorList, CtapOptions, PublicKey, Signature,
};
+use generic_array::GenericArray;
use libtock::ctap::{CtapRecvBuffer, CtapSendBuffer};
use libtock::hmac::HmacDriverFactory;
use libtock::println;
use libtock::result::TockResult;
use libtock::syscalls;
+use p256::elliptic_curve::ff::PrimeField;
+use p256::Scalar;
+use subtle::{Choice, ConditionallySelectable};
+
+#[derive(Debug, Clone, PartialEq, Eq, Copy)]
+pub struct PrivateKey(Scalar);
+
+impl ConditionallySelectable for PrivateKey {
+ fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
+ Self(ConditionallySelectable::conditional_select(
+ &a.0, &b.0, choice,
+ ))
+ }
+}
+
+impl Default for PrivateKey {
+ fn default() -> Self {
+ Self(Scalar::default())
+ }
+}
+
+impl PrivateKey {
+ pub fn from_bytes(bytes: &[u8; 32]) -> Option<Self> {
+ Scalar::from_repr(GenericArray::clone_from_slice(bytes)).map(|s| Self(s))
+ }
+}
/// This is the provided implementation of `CtapHidPlatform` to be used in the `UsbContext`
pub(crate) struct UsbKeyHidPlatform {}