pub mod ed25519;
pub use ed25519::Ed25519;
pub trait DsignAlgorithm: Clone + Send + Sync + 'static {
type SigningKey;
type VerificationKey;
type Signature;
const ALGORITHM_NAME: &'static str;
const SIGNING_KEY_SIZE: usize;
const VERIFICATION_KEY_SIZE: usize;
const SIGNATURE_SIZE: usize;
fn derive_verification_key(signing_key: &Self::SigningKey) -> Self::VerificationKey;
fn sign(signing_key: &Self::SigningKey, message: &[u8]) -> Self::Signature;
fn verify(
verification_key: &Self::VerificationKey,
message: &[u8],
signature: &Self::Signature,
) -> crate::common::Result<()>;
fn gen_key(seed: &[u8]) -> Self::SigningKey;
fn raw_serialize_verification_key(key: &Self::VerificationKey) -> &[u8];
fn raw_deserialize_verification_key(bytes: &[u8]) -> Option<Self::VerificationKey>;
fn raw_serialize_signature(sig: &Self::Signature) -> &[u8];
fn raw_deserialize_signature(bytes: &[u8]) -> Option<Self::Signature>;
fn hash_verification_key<H: crate::hash::HashAlgorithm>(
key: &Self::VerificationKey,
) -> alloc::vec::Vec<u8> {
let raw = Self::raw_serialize_verification_key(key);
H::hash(raw)
}
}
#[derive(Clone, PartialEq, Eq)]
pub struct SignedDsign<D: DsignAlgorithm> {
signature: D::Signature,
}
impl<D: DsignAlgorithm> core::fmt::Debug for SignedDsign<D>
where
D::Signature: core::fmt::Debug,
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("SignedDsign")
.field("signature", &self.signature)
.finish()
}
}
impl<D: DsignAlgorithm> SignedDsign<D> {
pub fn sign(signing_key: &D::SigningKey, message: &[u8]) -> Self {
let signature = D::sign(signing_key, message);
Self { signature }
}
pub fn verify(
&self,
verification_key: &D::VerificationKey,
message: &[u8],
) -> crate::common::Result<()> {
D::verify(verification_key, message, &self.signature)
}
pub fn get_signature(&self) -> &D::Signature {
&self.signature
}
pub fn from_signature(signature: D::Signature) -> Self {
Self { signature }
}
}
pub type DsignSigningKey = ed25519::Ed25519SigningKey;
pub type DsignVerificationKey = ed25519::Ed25519VerificationKey;
pub type DsignSignature = ed25519::Ed25519Signature;
#[derive(Clone, PartialEq, Eq)]
pub struct DsignKeyPair {
pub signing_key: DsignSigningKey,
pub verification_key: DsignVerificationKey,
}
impl core::fmt::Debug for DsignKeyPair {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("DsignKeyPair")
.field("signing_key", &"<redacted>")
.field("verification_key", &self.verification_key)
.finish()
}
}
impl DsignKeyPair {
pub fn generate(seed: &[u8; 32]) -> Self {
let signing_key = Ed25519::gen_key(seed);
let verification_key = Ed25519::derive_verification_key(&signing_key);
Self {
signing_key,
verification_key,
}
}
pub fn from_keys(signing_key: DsignSigningKey, verification_key: DsignVerificationKey) -> Self {
Self {
signing_key,
verification_key,
}
}
}
#[cfg(feature = "cbor")]
mod cbor_impl {
use super::*;
use crate::cbor::{
decode_bytes, encode_bytes, encoded_size_bytes, CborError, FromCbor, ToCbor,
};
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
impl ToCbor for SignedDsign<Ed25519> {
#[cfg(feature = "alloc")]
fn to_cbor(&self) -> Vec<u8> {
encode_bytes(self.signature.as_bytes())
}
fn encoded_size(&self) -> usize {
encoded_size_bytes(Ed25519::SIGNATURE_SIZE)
}
}
impl FromCbor for SignedDsign<Ed25519> {
fn from_cbor(bytes: &[u8]) -> Result<Self, CborError> {
let decoded = decode_bytes(bytes)?;
let sig =
ed25519::Ed25519Signature::from_bytes(&decoded).ok_or(CborError::InvalidLength)?;
Ok(SignedDsign::from_signature(sig))
}
}
impl ToCbor for ed25519::Ed25519VerificationKey {
#[cfg(feature = "alloc")]
fn to_cbor(&self) -> Vec<u8> {
encode_bytes(self.as_bytes())
}
fn encoded_size(&self) -> usize {
encoded_size_bytes(Ed25519::VERIFICATION_KEY_SIZE)
}
}
impl FromCbor for ed25519::Ed25519VerificationKey {
fn from_cbor(bytes: &[u8]) -> Result<Self, CborError> {
let decoded = decode_bytes(bytes)?;
Self::from_bytes(&decoded).ok_or(CborError::InvalidLength)
}
}
impl ToCbor for ed25519::Ed25519Signature {
#[cfg(feature = "alloc")]
fn to_cbor(&self) -> Vec<u8> {
encode_bytes(self.as_bytes())
}
fn encoded_size(&self) -> usize {
encoded_size_bytes(64)
}
}
impl FromCbor for ed25519::Ed25519Signature {
fn from_cbor(bytes: &[u8]) -> Result<Self, CborError> {
let decoded = decode_bytes(bytes)?;
Self::from_bytes(&decoded).ok_or(CborError::InvalidLength)
}
}
}