use std::cell::RefCell;
use std::fmt;
use ed25519_dalek::Signer;
use nibkeep_base::cbor::{self, FromCbor, ToCbor, Value};
use rand_core::CryptoRng;
use zeroize::Zeroizing;
use crate::CryptoError;
use crate::format::{self, Hex};
#[derive(Clone)]
pub struct SigningKey(ed25519_dalek::SigningKey);
impl SigningKey {
#[must_use]
pub fn generate(rng: &mut impl CryptoRng) -> Self {
let mut bytes = Zeroizing::new([0; 32]);
rng.fill_bytes(&mut bytes[..]);
Self::from_bytes(&bytes)
}
#[must_use]
pub fn from_bytes(bytes: &[u8; 32]) -> Self {
Self(ed25519_dalek::SigningKey::from_bytes(bytes))
}
#[must_use]
pub fn expose_bytes(&self) -> &[u8; 32] {
self.0.as_bytes()
}
#[must_use]
pub fn verifying_key(&self) -> VerifyingKey {
VerifyingKey(self.0.verifying_key())
}
#[must_use]
pub fn sign(&self, context: &'static str, message: &[u8]) -> Signature {
Signature(self.0.sign(&signed_bytes(context, message)).to_bytes())
}
}
impl fmt::Debug for SigningKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SigningKey")
.field("verifying_key", &self.verifying_key())
.finish_non_exhaustive()
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct VerifyingKey(ed25519_dalek::VerifyingKey);
impl VerifyingKey {
#[must_use]
pub fn as_bytes(&self) -> &[u8; 32] {
self.0.as_bytes()
}
#[must_use]
pub fn from_bytes(bytes: &[u8; 32]) -> Option<Self> {
Self::decode(bytes)
}
#[must_use]
pub fn to_text(&self) -> String {
<base64ct::Base64UrlUnpadded as base64ct::Encoding>::encode_string(self.as_bytes())
}
#[must_use]
pub fn from_text(text: &str) -> Option<Self> {
let bytes: [u8; 32] = <base64ct::Base64UrlUnpadded as base64ct::Encoding>::decode_vec(text)
.ok()?
.try_into()
.ok()?;
Self::from_bytes(&bytes)
}
pub fn verify(
&self,
context: &'static str,
message: &[u8],
signature: &Signature,
) -> Result<(), CryptoError> {
let signature = ed25519_dalek::Signature::from_bytes(&signature.0);
self.0
.verify_strict(&signed_bytes(context, message), &signature)
.map_err(|_| CryptoError::InvalidSignature)
}
}
impl fmt::Debug for VerifyingKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "VerifyingKey({})", Hex(self.as_bytes()))
}
}
impl ToCbor for VerifyingKey {
fn to_cbor(&self) -> Value {
format::write_bytes(&[self.as_bytes()])
}
}
impl FromCbor for VerifyingKey {
fn from_cbor(value: &Value) -> Result<Self, cbor::Error> {
const EXPECTED: &str = "a verifying key: [1, Ed25519 public key]";
let error = || cbor::Error::Expected(EXPECTED);
let [bytes] = format::read_bytes(value, EXPECTED)?;
let bytes: &[u8; 32] = bytes.try_into().map_err(|_| error())?;
VerifyingKey::decode(bytes).ok_or_else(error)
}
}
const DECODED_KEYS: usize = 16;
thread_local! {
static DECODED: RefCell<Vec<([u8; 32], VerifyingKey)>> =
const { RefCell::new(Vec::new()) };
}
impl VerifyingKey {
fn decode(bytes: &[u8; 32]) -> Option<Self> {
let remembered = DECODED
.try_with(|decoded| {
let mut decoded = decoded.borrow_mut();
let position = decoded.iter().position(|(known, _)| known == bytes)?;
let entry = decoded.remove(position);
decoded.push(entry);
Some(entry.1)
})
.ok()
.flatten();
if remembered.is_some() {
return remembered;
}
let key = Self::decode_in_full(bytes)?;
let _ = DECODED.try_with(|decoded| {
let mut decoded = decoded.borrow_mut();
if decoded.len() == DECODED_KEYS {
decoded.remove(0);
}
decoded.push((*bytes, key));
});
Some(key)
}
fn decode_in_full(bytes: &[u8; 32]) -> Option<Self> {
let key = ed25519_dalek::VerifyingKey::from_bytes(bytes).ok()?;
if key.is_weak() || key.to_edwards().compress().as_bytes() != bytes {
return None;
}
Some(Self(key))
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct Signature([u8; 64]);
impl Signature {
#[must_use]
pub const fn as_bytes(&self) -> &[u8; 64] {
&self.0
}
}
impl fmt::Debug for Signature {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Signature({})", Hex(&self.0))
}
}
impl ToCbor for Signature {
fn to_cbor(&self) -> Value {
format::write_bytes(&[&self.0])
}
}
impl FromCbor for Signature {
fn from_cbor(value: &Value) -> Result<Self, cbor::Error> {
const EXPECTED: &str = "a signature: [1, Ed25519 signature]";
let [bytes] = format::read_bytes(value, EXPECTED)?;
bytes
.try_into()
.map(Self)
.map_err(|_| cbor::Error::Expected(EXPECTED))
}
}
fn signed_bytes(context: &str, message: &[u8]) -> Vec<u8> {
cbor::encode(&Value::Array(vec![
Value::Text(context.to_owned()),
Value::Bytes(message.to_vec()),
]))
}