litep2p/crypto/
mod.rs

1// Copyright 2023 Protocol Labs.
2// Copyright 2023 litep2p developers
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22//! Crypto-related code.
23
24use crate::{error::ParseError, peer_id::*};
25
26pub mod ed25519;
27#[cfg(feature = "rsa")]
28pub mod rsa;
29
30pub(crate) mod noise;
31#[cfg(feature = "quic")]
32pub(crate) mod tls;
33pub(crate) mod keys_proto {
34    include!(concat!(env!("OUT_DIR"), "/keys_proto.rs"));
35}
36
37/// The public key of a node's identity keypair.
38#[derive(Clone, Debug, PartialEq, Eq)]
39pub enum PublicKey {
40    /// A public Ed25519 key.
41    Ed25519(ed25519::PublicKey),
42}
43
44impl PublicKey {
45    /// Encode the public key into a protobuf structure for storage or
46    /// exchange with other nodes.
47    pub fn to_protobuf_encoding(&self) -> Vec<u8> {
48        use prost::Message;
49
50        let public_key = keys_proto::PublicKey::from(self);
51
52        let mut buf = Vec::with_capacity(public_key.encoded_len());
53        public_key.encode(&mut buf).expect("Vec<u8> provides capacity as needed");
54        buf
55    }
56
57    /// Convert the `PublicKey` into the corresponding `PeerId`.
58    pub fn to_peer_id(&self) -> PeerId {
59        self.into()
60    }
61}
62
63impl From<&PublicKey> for keys_proto::PublicKey {
64    fn from(key: &PublicKey) -> Self {
65        match key {
66            PublicKey::Ed25519(key) => keys_proto::PublicKey {
67                r#type: keys_proto::KeyType::Ed25519 as i32,
68                data: key.to_bytes().to_vec(),
69            },
70        }
71    }
72}
73
74impl TryFrom<keys_proto::PublicKey> for PublicKey {
75    type Error = ParseError;
76
77    fn try_from(pubkey: keys_proto::PublicKey) -> Result<Self, Self::Error> {
78        let key_type = keys_proto::KeyType::try_from(pubkey.r#type)
79            .map_err(|_| ParseError::UnknownKeyType(pubkey.r#type))?;
80
81        if key_type == keys_proto::KeyType::Ed25519 {
82            Ok(ed25519::PublicKey::try_from_bytes(&pubkey.data).map(PublicKey::Ed25519)?)
83        } else {
84            Err(ParseError::UnknownKeyType(key_type as i32))
85        }
86    }
87}
88
89impl From<ed25519::PublicKey> for PublicKey {
90    fn from(public_key: ed25519::PublicKey) -> Self {
91        PublicKey::Ed25519(public_key)
92    }
93}
94
95/// The public key of a remote node's identity keypair. Supports RSA keys additionally to ed25519.
96#[derive(Clone, Debug, PartialEq, Eq)]
97pub enum RemotePublicKey {
98    /// A public Ed25519 key.
99    Ed25519(ed25519::PublicKey),
100    /// A public RSA key.
101    #[cfg(feature = "rsa")]
102    Rsa(rsa::PublicKey),
103}
104
105impl RemotePublicKey {
106    /// Verify a signature for a message using this public key, i.e. check
107    /// that the signature has been produced by the corresponding
108    /// private key (authenticity), and that the message has not been
109    /// tampered with (integrity).
110    #[must_use]
111    pub fn verify(&self, msg: &[u8], sig: &[u8]) -> bool {
112        use RemotePublicKey::*;
113        match self {
114            Ed25519(pk) => pk.verify(msg, sig),
115            #[cfg(feature = "rsa")]
116            Rsa(pk) => pk.verify(msg, sig),
117        }
118    }
119
120    /// Decode a public key from a protobuf structure, e.g. read from storage
121    /// or received from another node.
122    pub fn from_protobuf_encoding(bytes: &[u8]) -> Result<RemotePublicKey, ParseError> {
123        use prost::Message;
124
125        let pubkey = keys_proto::PublicKey::decode(bytes)?;
126
127        pubkey.try_into()
128    }
129}
130
131impl TryFrom<keys_proto::PublicKey> for RemotePublicKey {
132    type Error = ParseError;
133
134    fn try_from(pubkey: keys_proto::PublicKey) -> Result<Self, Self::Error> {
135        let key_type = keys_proto::KeyType::try_from(pubkey.r#type)
136            .map_err(|_| ParseError::UnknownKeyType(pubkey.r#type))?;
137
138        match key_type {
139            keys_proto::KeyType::Ed25519 =>
140                ed25519::PublicKey::try_from_bytes(&pubkey.data).map(RemotePublicKey::Ed25519),
141            #[cfg(feature = "rsa")]
142            keys_proto::KeyType::Rsa =>
143                rsa::PublicKey::try_decode_x509(&pubkey.data).map(RemotePublicKey::Rsa),
144            _ => Err(ParseError::UnknownKeyType(key_type as i32)),
145        }
146    }
147}