crypto: Add ECDH trait and `MemorySigner` impl
Signed-off-by: Alexis Sellier <alexis@radicle.xyz>
This commit is contained in:
parent
66d76d7ab2
commit
96d1ce5da6
|
|
@ -26,6 +26,9 @@ pub struct Verified;
|
|||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
pub struct Unverified;
|
||||
|
||||
/// Output of a Diffie-Hellman key exchange.
|
||||
pub type SharedSecret = [u8; 32];
|
||||
|
||||
/// Error returned if signing fails, eg. due to an HSM or KMS.
|
||||
#[derive(Debug, Error)]
|
||||
#[error(transparent)]
|
||||
|
|
@ -69,6 +72,13 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
/// A signer that can perform Elliptic-curve Diffie–Hellman.
|
||||
pub trait Ecdh: Signer {
|
||||
/// Perform an ECDH key exchange. Takes the counter-party's public key,
|
||||
/// and returns a computed shared secret.
|
||||
fn ecdh(&self, other: &PublicKey) -> Result<SharedSecret, Error>;
|
||||
}
|
||||
|
||||
/// Cryptographic signature.
|
||||
#[derive(PartialEq, Eq, Hash, Copy, Clone, Serialize, Deserialize)]
|
||||
#[serde(into = "String", try_from = "String")]
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ use std::{fs, io};
|
|||
use thiserror::Error;
|
||||
use zeroize::Zeroizing;
|
||||
|
||||
use crate::{keypair, PublicKey, SecretKey, Signature, Signer, SignerError};
|
||||
use crate::{keypair, Ecdh, PublicKey, SecretKey, SharedSecret, Signature, Signer, SignerError};
|
||||
|
||||
/// A secret key passphrase.
|
||||
pub type Passphrase = Zeroizing<String>;
|
||||
|
|
@ -139,6 +139,16 @@ impl Signer for MemorySigner {
|
|||
}
|
||||
}
|
||||
|
||||
impl Ecdh for MemorySigner {
|
||||
fn ecdh(&self, other: &PublicKey) -> Result<SharedSecret, ed25519_compact::Error> {
|
||||
let pk = ed25519_compact::x25519::PublicKey::from_ed25519(other)?;
|
||||
let sk = ed25519_compact::x25519::SecretKey::from_ed25519(&self.secret)?;
|
||||
let ss = pk.dh(&sk)?;
|
||||
|
||||
Ok(*ss)
|
||||
}
|
||||
}
|
||||
|
||||
impl MemorySigner {
|
||||
/// Load this signer from a keystore, given a secret key passphrase.
|
||||
pub fn load(keystore: &Keystore, passphrase: Passphrase) -> Result<Self, MemorySignerError> {
|
||||
|
|
|
|||
Loading…
Reference in New Issue