Skip to content

Commit

Permalink
fix(*): Fixes incorrect tests and removes use of hazmat
Browse files Browse the repository at this point in the history
Signed-off-by: Taylor Thomas <taylor@cosmonic.com>
  • Loading branch information
thomastaylor312 committed Aug 15, 2023
1 parent d9ecb88 commit 8a8d1eb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
17 changes: 13 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "nkeys"
version = "0.3.1"
version = "0.3.2"
authors = ["wasmCloud Team"]
edition = "2021"
description = "Rust implementation of the NATS nkeys library"
Expand All @@ -13,7 +13,14 @@ keywords = ["crypto", "nats", "ed25519", "cryptography"]
categories = ["cryptography", "authentication"]

[features]
cli = ["quicli", "structopt", "term-table", "exitfailure", "env_logger", "serde_json"]
cli = [
"quicli",
"structopt",
"term-table",
"exitfailure",
"env_logger",
"serde_json",
]

[[bin]]
name = "nk"
Expand All @@ -22,7 +29,9 @@ required-features = ["cli"]
[dependencies]
signatory = "0.27"
ed25519 = { version = "2.0.0", default-features = false }
ed25519-dalek = { version = "2.0.0", default-features = false, features = ["digest", "hazmat"] }
ed25519-dalek = { version = "2.0.0", default-features = false, features = [
"digest",
] }
rand = "0.8"
byteorder = "1.3.4"
data-encoding = "2.3.0"
Expand All @@ -32,7 +41,7 @@ log = "0.4.11"
quicli = { version = "0.4", optional = true }
structopt = { version = "0.3.17", optional = true }
term-table = { version = "1.3.0", optional = true }
exitfailure = { version = "0.5.1", optional =true }
exitfailure = { version = "0.5.1", optional = true }
env_logger = { version = "0.9", optional = true }
serde_json = { version = "1.0", optional = true }

Expand Down
2 changes: 1 addition & 1 deletion src/bin/nk/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fn generate(kt: &KeyPairType, output_type: &Output) {
"seed": kp.seed().unwrap(),
});

println!("{}", output.to_string());
println!("{}", output);
}
}
}
25 changes: 11 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
use std::fmt::{self, Debug};

use crc::{extract_crc, push_crc, valid_checksum};
use ed25519_dalek::{hazmat::ExpandedSecretKey, SecretKey, Signature, Verifier, VerifyingKey};
use ed25519_dalek::{SecretKey, Signer, SigningKey, Verifier, VerifyingKey};
use rand::prelude::*;

const ENCODED_SEED_LENGTH: usize = 58;
Expand Down Expand Up @@ -81,6 +81,7 @@ type Result<T> = std::result::Result<T, crate::error::Error>;
pub struct KeyPair {
kp_type: KeyPairType,
sk: Option<SecretKey>, //rawkey_kind: RawKeyKind,
signing_key: Option<SigningKey>,
pk: VerifyingKey,
}

Expand Down Expand Up @@ -161,10 +162,11 @@ impl KeyPair {
/// NOTE: These bytes should be generated from a cryptographically secure random source.
pub fn new_from_raw(kp_type: KeyPairType, random_bytes: [u8; 32]) -> Result<KeyPair> {
let s = random_bytes;
let pk = pk_from_seed(&s)?;
let signing_key = SigningKey::from_bytes(&s);
Ok(KeyPair {
kp_type,
pk,
pk: signing_key.verifying_key(),
signing_key: Some(signing_key),
sk: Some(s),
})
}
Expand Down Expand Up @@ -244,11 +246,8 @@ impl KeyPair {

/// Attempts to sign the given input with the key pair's seed
pub fn sign(&self, input: &[u8]) -> Result<Vec<u8>> {
if let Some(ref seed) = self.sk {
let expanded: ExpandedSecretKey = seed.into();
let sig: Signature = ed25519_dalek::hazmat::raw_sign::<ed25519_dalek::Sha512>(
&expanded, input, &self.pk,
);
if let Some(ref seed) = self.signing_key {
let sig = seed.sign(input);
Ok(sig.to_bytes().to_vec())
} else {
Err(err!(SignatureError, "Cannot sign without a seed key"))
Expand Down Expand Up @@ -308,6 +307,7 @@ impl KeyPair {
kp_type: KeyPairType::from(prefix),
pk,
sk: None,
signing_key: None,
}),
Err(_) => Err(err!(VerifyError, "Could not read public key")),
}
Expand Down Expand Up @@ -338,12 +338,13 @@ impl KeyPair {
let mut seed = [0u8; 32];
seed.copy_from_slice(&raw[2..]);

let pk = pk_from_seed(&seed)?;
let signing_key = SigningKey::from_bytes(&seed);

Ok(KeyPair {
kp_type,
pk,
pk: signing_key.verifying_key(),
sk: Some(seed),
signing_key: Some(signing_key),
})
}
}
Expand All @@ -354,10 +355,6 @@ impl KeyPair {
}
}

fn pk_from_seed(seed: &SecretKey) -> Result<VerifyingKey> {
VerifyingKey::from_bytes(seed).map_err(|err| err.into())
}

fn decode_raw(raw: &[u8]) -> Result<Vec<u8>> {
let mut b32_decoded = data_encoding::BASE32_NOPAD.decode(raw)?;

Expand Down

0 comments on commit 8a8d1eb

Please sign in to comment.