port crypto.go

This commit is contained in:
c0repwn3r 2023-03-29 13:13:50 -04:00
parent 201374fba4
commit eaf4cee4ee
Signed by: core
GPG Key ID: FDBF740DADDCEECF
4 changed files with 46 additions and 2 deletions

1
Cargo.lock generated
View File

@ -626,6 +626,7 @@ dependencies = [
"base64 0.21.0", "base64 0.21.0",
"base64-serde", "base64-serde",
"log", "log",
"rand",
"reqwest", "reqwest",
"serde", "serde",
"serde_json", "serde_json",

View File

@ -20,3 +20,4 @@ url = "2.3.1"
base64 = "0.21.0" base64 = "0.21.0"
serde_json = "1.0.95" serde_json = "1.0.95"
trifid-pki = { version = "0.1.6", path = "../trifid-pki" } trifid-pki = { version = "0.1.6", path = "../trifid-pki" }
rand = "0.8.5"

41
dnapi-rs/src/crypto.rs Normal file
View File

@ -0,0 +1,41 @@
//! Functions for generating keys and nonces for use in API calls
use rand::Rng;
use rand::rngs::OsRng;
use trifid_pki::cert::{serialize_x25519_private, serialize_x25519_public};
use trifid_pki::ed25519_dalek::{SigningKey, VerifyingKey};
use trifid_pki::x25519_dalek::{PublicKey, StaticSecret};
/// Generate a new random set of Nebula (Diffie-Hellman) and Ed25519 (API calls) keys for use in your client
pub fn new_keys() -> (Vec<u8>, Vec<u8>, VerifyingKey, SigningKey) {
let (dh_pub, dh_priv) = new_nebula_keypair();
let (ed_pub, ed_priv) = new_ed25519_keypair();
(dh_pub, dh_priv, ed_pub, ed_priv)
}
/// Generate a new PEM-encoded Nebula keypair
pub fn new_nebula_keypair() -> (Vec<u8>, Vec<u8>) {
let (pub_key, priv_key) = new_x25519_keypair();
let pub_key_encoded = serialize_x25519_public(&pub_key);
let priv_key_encoded = serialize_x25519_private(&priv_key);
(pub_key_encoded, priv_key_encoded)
}
/// Generate a new 32-byte X25519 keypair
pub fn new_x25519_keypair() -> ([u8; 32], [u8; 32]) {
let priv_key = StaticSecret::new(&mut OsRng);
let pub_key = PublicKey::from(&priv_key);
(pub_key.to_bytes(), priv_key.to_bytes())
}
/// Generate a new random Ed25519 signing keypair for signing API calls
pub fn new_ed25519_keypair() -> (VerifyingKey, SigningKey) {
let secret = SigningKey::generate(&mut OsRng);
let public = secret.verifying_key();
(public, secret)
}
/// Generates a 16-byte random nonce for use in API calls
pub fn nonce() -> [u8; 16] {
rand::thread_rng().gen()
}

View File

@ -18,3 +18,4 @@
pub mod message; pub mod message;
pub mod client; pub mod client;
pub mod credentials; pub mod credentials;
pub mod crypto;