ssh_key/public/
ed25519.rs

1//! Ed25519 public keys.
2//!
3//! Edwards Digital Signature Algorithm (EdDSA) over Curve25519.
4
5use crate::{Error, Result};
6use core::fmt;
7use encoding::{CheckedSum, Decode, Encode, Reader, Writer};
8
9/// Ed25519 public key.
10// TODO(tarcieri): use `ed25519::PublicKey`? (doesn't exist yet)
11#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
12pub struct Ed25519PublicKey(pub [u8; Self::BYTE_SIZE]);
13
14impl Ed25519PublicKey {
15    /// Size of an Ed25519 public key in bytes.
16    pub const BYTE_SIZE: usize = 32;
17}
18
19impl AsRef<[u8; Self::BYTE_SIZE]> for Ed25519PublicKey {
20    fn as_ref(&self) -> &[u8; Self::BYTE_SIZE] {
21        &self.0
22    }
23}
24
25impl Decode for Ed25519PublicKey {
26    type Error = Error;
27
28    fn decode(reader: &mut impl Reader) -> Result<Self> {
29        let mut bytes = [0u8; Self::BYTE_SIZE];
30        reader.read_prefixed(|reader| reader.read(&mut bytes))?;
31        Ok(Self(bytes))
32    }
33}
34
35impl Encode for Ed25519PublicKey {
36    fn encoded_len(&self) -> encoding::Result<usize> {
37        [4, Self::BYTE_SIZE].checked_sum()
38    }
39
40    fn encode(&self, writer: &mut impl Writer) -> encoding::Result<()> {
41        self.0.encode(writer)?;
42        Ok(())
43    }
44}
45
46impl TryFrom<&[u8]> for Ed25519PublicKey {
47    type Error = Error;
48
49    fn try_from(bytes: &[u8]) -> Result<Self> {
50        Ok(Self(bytes.try_into()?))
51    }
52}
53
54impl fmt::Display for Ed25519PublicKey {
55    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56        write!(f, "{self:X}")
57    }
58}
59
60impl fmt::LowerHex for Ed25519PublicKey {
61    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62        for byte in self.as_ref() {
63            write!(f, "{byte:02x}")?;
64        }
65        Ok(())
66    }
67}
68
69impl fmt::UpperHex for Ed25519PublicKey {
70    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71        for byte in self.as_ref() {
72            write!(f, "{byte:02X}")?;
73        }
74        Ok(())
75    }
76}
77
78#[cfg(feature = "ed25519")]
79impl TryFrom<Ed25519PublicKey> for ed25519_dalek::VerifyingKey {
80    type Error = Error;
81
82    fn try_from(key: Ed25519PublicKey) -> Result<ed25519_dalek::VerifyingKey> {
83        ed25519_dalek::VerifyingKey::try_from(&key)
84    }
85}
86
87#[cfg(feature = "ed25519")]
88impl TryFrom<&Ed25519PublicKey> for ed25519_dalek::VerifyingKey {
89    type Error = Error;
90
91    fn try_from(key: &Ed25519PublicKey) -> Result<ed25519_dalek::VerifyingKey> {
92        ed25519_dalek::VerifyingKey::from_bytes(key.as_ref()).map_err(|_| Error::Crypto)
93    }
94}
95
96#[cfg(feature = "ed25519")]
97impl From<ed25519_dalek::VerifyingKey> for Ed25519PublicKey {
98    fn from(key: ed25519_dalek::VerifyingKey) -> Ed25519PublicKey {
99        Ed25519PublicKey::from(&key)
100    }
101}
102
103#[cfg(feature = "ed25519")]
104impl From<&ed25519_dalek::VerifyingKey> for Ed25519PublicKey {
105    fn from(key: &ed25519_dalek::VerifyingKey) -> Ed25519PublicKey {
106        Ed25519PublicKey(key.to_bytes())
107    }
108}