use crate::pem;
use base64::Engine;
use base64::engine::general_purpose::STANDARD as BASE64;
use der::asn1::OctetString;
use der::{Decode, Encode};
use ed25519_dalek::ed25519::signature::rand_core::OsRng;
use ed25519_dalek::pkcs8::{DecodePrivateKey, DecodePublicKey, EncodePublicKey};
use ed25519_dalek::{SignatureError, Signer, Verifier};
use pkcs8::PrivateKeyInfo;
use serde::{Deserialize, Deserializer, Serialize, Serializer, de};
use sha2::Digest;
use spki::der::AnyRef;
use spki::der::asn1::BitStringRef;
use spki::{AlgorithmIdentifier, ObjectIdentifier, SubjectPublicKeyInfo};
use std::error::Error;
pub const OID: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.3.101.112");
pub const SECRET_KEY_SIZE: usize = 32;
pub const PUBLIC_KEY_SIZE: usize = 32;
pub const SIGNATURE_SIZE: usize = 64;
pub const FINGERPRINT_SIZE: usize = 32;
#[derive(Clone)]
pub struct SecretKey {
inner: ed25519_dalek::SigningKey,
}
impl SecretKey {
pub fn generate() -> SecretKey {
let mut rng = OsRng;
let key = ed25519_dalek::SigningKey::generate(&mut rng);
Self { inner: key }
}
pub fn from_bytes(bin: &[u8; SECRET_KEY_SIZE]) -> Self {
let key = ed25519_dalek::SecretKey::from(*bin);
let sig = ed25519_dalek::SigningKey::from(&key);
Self { inner: sig }
}
pub fn from_der(der: &[u8]) -> Result<Self, Box<dyn Error>> {
let info = PrivateKeyInfo::try_from(der)?;
if info.encoded_len()?.try_into() != Ok(der.len()) {
return Err("trailing data in private key".into());
}
if info.public_key.is_some() {
return Err("embedded public key not supported".into());
}
let inner = ed25519_dalek::SigningKey::from_pkcs8_der(der)?;
Ok(Self { inner })
}
pub fn from_pem(pem_str: &str) -> Result<Self, Box<dyn Error>> {
let (kind, data) = pem::decode(pem_str.as_bytes())?;
if kind != "PRIVATE KEY" {
return Err(format!("invalid PEM tag {}", kind).into());
}
Self::from_der(&data)
}
pub fn to_bytes(&self) -> [u8; SECRET_KEY_SIZE] {
self.inner.to_bytes()
}
pub fn to_der(&self) -> Vec<u8> {
let seed = OctetString::new(self.inner.to_bytes()).unwrap();
let inner = seed.to_der().unwrap();
let alg = pkcs8::AlgorithmIdentifierRef {
oid: OID,
parameters: None::<AnyRef>,
};
let info = PrivateKeyInfo {
algorithm: alg,
private_key: &inner,
public_key: None,
};
info.to_der().unwrap()
}
pub fn to_pem(&self) -> String {
pem::encode("PRIVATE KEY", &self.to_der())
}
pub fn public_key(&self) -> PublicKey {
PublicKey {
inner: self.inner.verifying_key(),
}
}
pub fn fingerprint(&self) -> Fingerprint {
self.public_key().fingerprint()
}
pub fn sign(&self, message: &[u8]) -> Signature {
Signature(self.inner.sign(message).to_bytes())
}
}
#[derive(Debug, Clone)]
pub struct PublicKey {
inner: ed25519_dalek::VerifyingKey,
}
impl PublicKey {
pub fn from_bytes(bin: &[u8; PUBLIC_KEY_SIZE]) -> Result<Self, Box<dyn Error>> {
let inner = ed25519_dalek::VerifyingKey::from_bytes(bin)?;
Ok(Self { inner })
}
pub fn from_der(der: &[u8]) -> Result<Self, Box<dyn Error>> {
let info: SubjectPublicKeyInfo<AlgorithmIdentifier<AnyRef>, BitStringRef> =
SubjectPublicKeyInfo::from_der(der)?;
if info.encoded_len()?.try_into() != Ok(der.len()) {
return Err("trailing data in public key".into());
}
let inner = ed25519_dalek::VerifyingKey::from_public_key_der(der)?;
Ok(Self { inner })
}
pub fn from_pem(pem_str: &str) -> Result<Self, Box<dyn Error>> {
let (kind, data) = pem::decode(pem_str.as_bytes())?;
if kind != "PUBLIC KEY" {
return Err(format!("invalid PEM tag {}", kind).into());
}
Self::from_der(&data)
}
pub fn to_bytes(&self) -> [u8; PUBLIC_KEY_SIZE] {
self.inner.to_bytes()
}
pub fn to_der(&self) -> Vec<u8> {
self.inner.to_public_key_der().unwrap().as_bytes().to_vec()
}
pub fn to_pem(&self) -> String {
pem::encode("PUBLIC KEY", &self.to_der())
}
pub fn fingerprint(&self) -> Fingerprint {
let mut hasher = sha2::Sha256::new();
hasher.update(self.to_bytes());
Fingerprint(hasher.finalize().into())
}
pub fn verify(&self, message: &[u8], signature: &Signature) -> Result<(), SignatureError> {
let sig = ed25519_dalek::Signature::from_bytes(&signature.to_bytes());
self.inner.verify(message, &sig)
}
}
impl Serialize for PublicKey {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(&BASE64.encode(self.to_bytes()))
}
}
impl<'de> Deserialize<'de> for PublicKey {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let s = String::deserialize(deserializer)?;
let bytes = BASE64.decode(&s).map_err(de::Error::custom)?;
let arr: [u8; PUBLIC_KEY_SIZE] = bytes
.try_into()
.map_err(|_| de::Error::custom("invalid public key length"))?;
PublicKey::from_bytes(&arr).map_err(de::Error::custom)
}
}
#[cfg(feature = "cbor")]
impl crate::cbor::Encode for PublicKey {
fn encode_cbor(&self) -> Vec<u8> {
self.to_bytes().encode_cbor()
}
}
#[cfg(feature = "cbor")]
impl crate::cbor::Decode for PublicKey {
fn decode_cbor(data: &[u8]) -> Result<Self, crate::cbor::Error> {
let bytes = <[u8; PUBLIC_KEY_SIZE]>::decode_cbor(data)?;
Self::from_bytes(&bytes).map_err(|e| crate::cbor::Error::DecodeFailed(e.to_string()))
}
fn decode_cbor_notrail(
decoder: &mut crate::cbor::Decoder<'_>,
) -> Result<Self, crate::cbor::Error> {
let bytes = decoder.decode_bytes_fixed::<PUBLIC_KEY_SIZE>()?;
Self::from_bytes(&bytes).map_err(|e| crate::cbor::Error::DecodeFailed(e.to_string()))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Signature([u8; SIGNATURE_SIZE]);
impl Signature {
pub fn from_bytes(bytes: &[u8; SIGNATURE_SIZE]) -> Self {
Self(*bytes)
}
pub fn to_bytes(&self) -> [u8; SIGNATURE_SIZE] {
self.0
}
}
impl Serialize for Signature {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(&BASE64.encode(self.to_bytes()))
}
}
impl<'de> Deserialize<'de> for Signature {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let s = String::deserialize(deserializer)?;
let bytes = BASE64.decode(&s).map_err(de::Error::custom)?;
let arr: [u8; SIGNATURE_SIZE] = bytes
.try_into()
.map_err(|_| de::Error::custom("invalid signature length"))?;
Ok(Signature::from_bytes(&arr))
}
}
#[cfg(feature = "cbor")]
impl crate::cbor::Encode for Signature {
fn encode_cbor(&self) -> Vec<u8> {
self.to_bytes().encode_cbor()
}
}
#[cfg(feature = "cbor")]
impl crate::cbor::Decode for Signature {
fn decode_cbor(data: &[u8]) -> Result<Self, crate::cbor::Error> {
let bytes = <[u8; SIGNATURE_SIZE]>::decode_cbor(data)?;
Ok(Self::from_bytes(&bytes))
}
fn decode_cbor_notrail(
decoder: &mut crate::cbor::Decoder<'_>,
) -> Result<Self, crate::cbor::Error> {
let bytes = decoder.decode_bytes_fixed::<SIGNATURE_SIZE>()?;
Ok(Self::from_bytes(&bytes))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Fingerprint([u8; FINGERPRINT_SIZE]);
impl Fingerprint {
pub fn from_bytes(bytes: &[u8; FINGERPRINT_SIZE]) -> Self {
Self(*bytes)
}
pub fn to_bytes(&self) -> [u8; FINGERPRINT_SIZE] {
self.0
}
}
impl Serialize for Fingerprint {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(&BASE64.encode(self.to_bytes()))
}
}
impl<'de> Deserialize<'de> for Fingerprint {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let s = String::deserialize(deserializer)?;
let bytes = BASE64.decode(&s).map_err(de::Error::custom)?;
let arr: [u8; FINGERPRINT_SIZE] = bytes
.try_into()
.map_err(|_| de::Error::custom("invalid fingerprint length"))?;
Ok(Fingerprint::from_bytes(&arr))
}
}
#[cfg(feature = "cbor")]
impl crate::cbor::Encode for Fingerprint {
fn encode_cbor(&self) -> Vec<u8> {
self.to_bytes().encode_cbor()
}
}
#[cfg(feature = "cbor")]
impl crate::cbor::Decode for Fingerprint {
fn decode_cbor(data: &[u8]) -> Result<Self, crate::cbor::Error> {
let bytes = <[u8; FINGERPRINT_SIZE]>::decode_cbor(data)?;
Ok(Self::from_bytes(&bytes))
}
fn decode_cbor_notrail(
decoder: &mut crate::cbor::Decoder<'_>,
) -> Result<Self, crate::cbor::Error> {
let bytes = decoder.decode_bytes_fixed::<FINGERPRINT_SIZE>()?;
Ok(Self::from_bytes(&bytes))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sign_verify() {
let secret = SecretKey::generate();
let public = secret.public_key();
struct TestCase<'a> {
message: &'a [u8],
}
let tests = [TestCase {
message: b"message to authenticate",
}];
for tt in &tests {
let signature = secret.sign(tt.message);
public
.verify(tt.message, &signature)
.unwrap_or_else(|e| panic!("failed to verify message: {}", e));
}
}
}