diff --git a/ssh-key/src/public/sk.rs b/ssh-key/src/public/sk.rs index 03ff599..53dab24 100644 --- a/ssh-key/src/public/sk.rs +++ b/ssh-key/src/public/sk.rs @@ -30,6 +30,15 @@ pub struct SkEcdsaSha2NistP256 { #[cfg(feature = "ecdsa")] impl SkEcdsaSha2NistP256 { + /// Construct new instance of SkEcdsaSha2NistP256. + #[cfg(feature = "alloc")] + pub fn new(ec_point: EcdsaNistP256PublicKey, application: impl Into) -> Self { + SkEcdsaSha2NistP256 { + ec_point: ec_point, + application: application, + } + } + /// Get the elliptic curve point for this Security Key. pub fn ec_point(&self) -> &EcdsaNistP256PublicKey { &self.ec_point diff --git a/ssh-key/tests/public_key.rs b/ssh-key/tests/public_key.rs index ff0d528..0c54d62 100644 --- a/ssh-key/tests/public_key.rs +++ b/ssh-key/tests/public_key.rs @@ -1,7 +1,9 @@ //! SSH public key tests. use hex_literal::hex; +use sec1::consts::U32; use ssh_key::{Algorithm, PublicKey}; +use ssh_key::public::SkEcdsaSha2NistP256; use std::collections::HashSet; #[cfg(feature = "ecdsa")] @@ -296,6 +298,32 @@ fn decode_sk_ecdsa_p256_openssh() { ); } +#[cfg(feature = "ecdsa")] +#[test] +fn new_sk_ecdsa_p256() { + const EXAMPLE_EC_POINT: [u8; 65] = [ + 0x04, 0x81, 0x0b, 0x40, 0x9d, 0x83, 0x82, 0xf6, + 0x97, 0xd7, 0x24, 0x25, 0x28, 0x5a, 0x24, 0x7d, + 0x63, 0x36, 0xb2, 0xeb, 0x9a, 0x08, 0x52, 0x36, + 0xaa, 0x9d, 0x1e, 0x26, 0x87, 0x47, 0xca, 0x0e, + 0x8e, 0xe2, 0x27, 0xf1, 0x73, 0x75, 0xe9, 0x44, + 0xa7, 0x75, 0x39, 0x2f, 0x1d, 0x35, 0x84, 0x2d, + 0x13, 0xf6, 0x23, 0x75, 0x74, 0xab, 0x03, 0xe0, + 0x0e, 0x9c, 0xc1, 0x79, 0x9e, 0xcd, 0x8d, 0x93, + 0x1e, + ]; + + let ec_point = sec1::EncodedPoint::::from_bytes(&EXAMPLE_EC_POINT).unwrap(); + let sk_key = SkEcdsaSha2NistP256::new(ec_point, "ssh:".to_string()); + let key = PublicKey::from_openssh(OPENSSH_SK_ECDSA_P256_EXAMPLE).unwrap(); + + let ecdsa_key = key.key_data().sk_ecdsa_p256().unwrap(); + assert_eq!( + &sk_key, + ecdsa_key + ); +} + #[test] fn decode_sk_ed25519_openssh() { let key = PublicKey::from_openssh(OPENSSH_SK_ED25519_EXAMPLE).unwrap();