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;
27pub(crate) mod noise;
28#[cfg(feature = "quic")]
29pub(crate) mod tls;
30pub(crate) mod keys_proto {
31    include!(concat!(env!("OUT_DIR"), "/keys_proto.rs"));
32}
33
34/// The public key of a node's identity keypair.
35#[derive(Clone, Debug, PartialEq, Eq)]
36pub enum PublicKey {
37    /// A public Ed25519 key.
38    Ed25519(ed25519::PublicKey),
39}
40
41impl PublicKey {
42    /// Verify a signature for a message using this public key, i.e. check
43    /// that the signature has been produced by the corresponding
44    /// private key (authenticity), and that the message has not been
45    /// tampered with (integrity).
46    #[must_use]
47    pub fn verify(&self, msg: &[u8], sig: &[u8]) -> bool {
48        use PublicKey::*;
49        match self {
50            Ed25519(pk) => pk.verify(msg, sig),
51        }
52    }
53
54    /// Encode the public key into a protobuf structure for storage or
55    /// exchange with other nodes.
56    pub fn to_protobuf_encoding(&self) -> Vec<u8> {
57        use prost::Message;
58
59        let public_key = keys_proto::PublicKey::from(self);
60
61        let mut buf = Vec::with_capacity(public_key.encoded_len());
62        public_key.encode(&mut buf).expect("Vec<u8> provides capacity as needed");
63        buf
64    }
65
66    /// Decode a public key from a protobuf structure, e.g. read from storage
67    /// or received from another node.
68    pub fn from_protobuf_encoding(bytes: &[u8]) -> Result<PublicKey, ParseError> {
69        use prost::Message;
70
71        let pubkey = keys_proto::PublicKey::decode(bytes)?;
72
73        pubkey.try_into()
74    }
75
76    /// Convert the `PublicKey` into the corresponding `PeerId`.
77    pub fn to_peer_id(&self) -> PeerId {
78        self.into()
79    }
80}
81
82impl From<&PublicKey> for keys_proto::PublicKey {
83    fn from(key: &PublicKey) -> Self {
84        match key {
85            PublicKey::Ed25519(key) => keys_proto::PublicKey {
86                r#type: keys_proto::KeyType::Ed25519 as i32,
87                data: key.to_bytes().to_vec(),
88            },
89        }
90    }
91}
92
93impl TryFrom<keys_proto::PublicKey> for PublicKey {
94    type Error = ParseError;
95
96    fn try_from(pubkey: keys_proto::PublicKey) -> Result<Self, Self::Error> {
97        let key_type = keys_proto::KeyType::try_from(pubkey.r#type)
98            .map_err(|_| ParseError::UnknownKeyType(pubkey.r#type))?;
99
100        if key_type == keys_proto::KeyType::Ed25519 {
101            Ok(ed25519::PublicKey::try_from_bytes(&pubkey.data).map(PublicKey::Ed25519)?)
102        } else {
103            Err(ParseError::UnknownKeyType(key_type as i32))
104        }
105    }
106}
107
108impl From<ed25519::PublicKey> for PublicKey {
109    fn from(public_key: ed25519::PublicKey) -> Self {
110        PublicKey::Ed25519(public_key)
111    }
112}