Skip to content

Commit

Permalink
ssh-key: introduce SkEcdsaSha2NistP256::new() constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
jviki committed Mar 6, 2024
1 parent 8abfc94 commit 11a037c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
9 changes: 9 additions & 0 deletions ssh-key/src/public/sk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) -> 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
Expand Down
28 changes: 28 additions & 0 deletions ssh-key/tests/public_key.rs
Original file line number Diff line number Diff line change
@@ -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")]
Expand Down Expand Up @@ -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::<U32>::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();
Expand Down

0 comments on commit 11a037c

Please sign in to comment.