duniter_crypto/keys/mod.rs
1// Copyright (C) 2018 The Duniter Project Developers.
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU Affero General Public License as
5// published by the Free Software Foundation, either version 3 of the
6// License, or (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU Affero General Public License for more details.
12//
13// You should have received a copy of the GNU Affero General Public License
14// along with this program. If not, see <https://www.gnu.org/licenses/>.
15
16//! Provide wrappers around public keys, private keys and signatures.
17//!
18//! - Keys can be converted to/from Base58 string format.
19//! - Signatures can be converted to/from Base64 string format.
20//!
21//! # Usage
22//!
23//! ```
24//! use duniter_crypto::keys::{Signature, PublicKey, PrivateKey, KeyPair};
25//! use duniter_crypto::keys::ed25519::KeyPairFromSaltedPasswordGenerator;
26//!
27//! let generator = KeyPairFromSaltedPasswordGenerator::with_default_parameters();
28//!
29//! let keypair = generator.generate(
30//! b"password",
31//! b"salt"
32//! );
33//!
34//! let message = "Hello, world!";
35//!
36//! let signature = keypair.sign(&message.as_bytes());
37//!
38//! assert!(keypair.pubkey.verify(&message.as_bytes(), &signature));
39//! ```
40//!
41//! # Format
42//!
43//! - Base58 use the following alphabet :
44//! `123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz`
45//! - Base64 use the following alphabet :
46//! `ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/`
47//! with `=` as padding character.
48
49use std::fmt::Display;
50use std::fmt::Debug;
51use std::hash::Hash;
52
53use base58::ToBase58;
54
55pub mod ed25519;
56
57/// Errors enumeration for Base58/64 strings convertion.
58#[derive(Clone, Copy, Debug, PartialEq, Eq)]
59pub enum BaseConvertionError {
60 /// Data have invalid key length (found, expected).
61 InvalidKeyLendth(usize, usize),
62 /// Base58 have an invalid character.
63 InvalidCharacter(char, usize),
64 /// Base58 have invalid lendth
65 InvalidBaseConverterLength(),
66}
67
68/// Store a cryptographic signature.
69///
70/// A signature can be converted from/to Base64 format.
71/// When converted back and forth the value should be the same.
72///
73/// A signature can be made with a [`PrivateKey`]
74/// and a message, and verified with the associated [`PublicKey`].
75///
76/// [`PrivateKey`]: trait.PrivateKey.html
77/// [`PublicKey`]: trait.PublicKey.html
78pub trait Signature: Clone + Display + Debug + PartialEq + Eq + Hash {
79 /// Create a `Signature` from a Base64 string.
80 ///
81 /// The Base64 string should contains only valid Base64 characters
82 /// and have a correct length (64 bytes when converted). If it's not the case,
83 /// a [`BaseConvertionError`] is returned with the corresponding variant.
84 ///
85 /// [`BaseConvertionError`]: enum.BaseConvertionError.html
86 fn from_base64(base64_string: &str) -> Result<Self, BaseConvertionError>;
87
88 /// Encode the signature into Base64 string format.
89 fn to_base64(&self) -> String;
90}
91
92/// Store a cryptographic public key.
93///
94/// A `PublicKey` can be converted from/to Base64 format.
95/// When converted back and forth the value should be the same.
96///
97/// A `PublicKey` is used to verify the signature of a message
98/// with the associated [`PrivateKey`].
99///
100/// [`PrivateKey`]: trait.PrivateKey.html
101pub trait PublicKey: Clone + Display + Debug + PartialEq + Eq + Hash + ToBase58 {
102 /// Signature type of associated cryptosystem.
103 type Signature: Signature;
104
105 /// Create a PublicKey from a Base58 string.
106 ///
107 /// The Base58 string should contains only valid Base58 characters
108 /// and have a correct length. If it's not the case,
109 /// a [`BaseConvertionError`] is returned with the corresponding variant.
110 ///
111 /// [`BaseConvertionError`]: enum.BaseConvertionError.html
112 fn from_base58(base58_string: &str) -> Result<Self, BaseConvertionError>;
113
114 /// Verify a signature with this public key.
115 fn verify(&self, message: &[u8], signature: &Self::Signature) -> bool;
116}
117
118/// Store a cryptographic private key.
119///
120/// A `PrivateKey` can be converted from/to Base58 format.
121/// When converted back and forth the value should be the same.
122///
123/// A `PrivateKey` is used to sign a message. The corresponding [`PublicKey`]
124/// will then be used to verify that signature.
125///
126/// [`PublicKey`]: trait.PublicKey.html
127pub trait PrivateKey: Clone + Display + Debug + PartialEq + Eq + ToBase58 {
128 /// Signature type of associated cryptosystem.
129 type Signature: Signature;
130
131 /// Create a PrivateKey from a Base58 string.
132 ///
133 /// The Base58 string should contains only valid Base58 characters
134 /// and have a correct length. If it's not the case, a [`BaseConvertionError`]
135 /// is returned with the corresponding variant.
136 ///
137 /// [`BaseConvertionError`]: enum.BaseConvertionError.html
138 fn from_base58(base58_string: &str) -> Result<Self, BaseConvertionError>;
139
140 /// Sign a message with this private key.
141 fn sign(&self, message: &[u8]) -> Self::Signature;
142}
143
144/// Store a cryptographic key pair (`PublicKey` + `PrivateKey`)
145pub trait KeyPair: Clone + Display + Debug + PartialEq + Eq {
146 /// Signature type of associated cryptosystem.
147 type Signature: Signature;
148 /// PublicKey type of associated cryptosystem.
149 type PublicKey: PublicKey;
150 /// PrivateKey type of associated cryptosystem.
151 type PrivateKey: PrivateKey;
152
153 /// Get `PublicKey`
154 fn public_key(&self) -> Self::PublicKey;
155
156 /// Get `PrivateKey`
157 fn private_key(&self) -> Self::PrivateKey;
158
159 /// Sign a message with private key.
160 fn sign(&self, message: &[u8]) -> Self::Signature;
161
162 /// Verify a signature with public key.
163 fn verify(&self, message: &[u8], signature: &Self::Signature) -> bool;
164}