1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//  Copyright (C) 2018  The Duniter Project Developers.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

//! Provide wrappers around public keys, private keys and signatures.
//!
//! - Keys can be converted to/from Base58 string format.
//! - Signatures can be converted to/from Base64 string format.
//!
//! # Usage
//!
//! ```
//! use dup_crypto::keys::{Signature, PublicKey, PrivateKey, KeyPair};
//! use dup_crypto::keys::ed25519::KeyPairFromSaltedPasswordGenerator;
//!
//! let generator = KeyPairFromSaltedPasswordGenerator::with_default_parameters();
//!
//! let keypair = generator.generate(
//!     b"password",
//!     b"salt"
//! );
//!
//! let message = "Hello, world!";
//!
//! let signature = keypair.sign(&message.as_bytes());
//!
//! assert!(keypair.pubkey.verify(&message.as_bytes(), &signature));
//! ```
//!
//! # Format
//!
//! - Base58 use the following alphabet :
//! `123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz`
//! - Base64 use the following alphabet :
//! `ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/`
//! with `=` as padding character.

use crate::bases::BaseConvertionError;
use base58::ToBase58;
use bincode;
use std::fmt::Debug;
use std::fmt::Display;
use std::fmt::Error;
use std::fmt::Formatter;
use std::hash::Hash;

pub mod bin_signable;
pub mod ed25519;
pub mod text_signable;

/// Cryptographic keys algorithms list
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub enum KeysAlgo {
    /// Ed25519 algorithm
    Ed25519 = 0,
    /// Schnorr algorithm
    Schnorr = 1,
}

/// Get the cryptographic algorithm.
pub trait GetKeysAlgo: Clone + Debug + PartialEq + Eq {
    /// Get the cryptographic algorithm.
    fn algo(&self) -> KeysAlgo;
}

/// Errors enumeration for signature verification.
#[derive(Debug)]
pub enum SigError {
    /// Signature and pubkey are not the same algo
    NotSameAlgo(),
    /// Invalid signature
    InvalidSig(),
    /// Absence of signature
    NotSig(),
    /// Deserialization error
    DeserError(bincode::Error),
}

impl From<bincode::Error> for SigError {
    fn from(e: bincode::Error) -> Self {
        SigError::DeserError(e)
    }
}

/// SignError
#[derive(Debug, Copy, Clone)]
pub enum SignError {
    /// WrongAlgo
    WrongAlgo(),
    /// WrongPrivkey
    WrongPrivkey(),
    /// AlreadySign
    AlreadySign(),
}

/// Define the operations that can be performed on a cryptographic signature.
///
/// A signature can be converted from/to Base64 format.
/// When converted back and forth the value should be the same.
///
/// A signature can be made with a [`PrivateKey`]
/// and a message, and verified with the associated [`PublicKey`].
///
/// [`PrivateKey`]: trait.PrivateKey.html
/// [`PublicKey`]: trait.PublicKey.html
pub trait Signature: Clone + Display + Debug + PartialEq + Eq + Hash {
    /// Create a `Signature` from a Base64 string.
    ///
    /// The Base64 string should contains only valid Base64 characters
    /// and have a correct length (64 bytes when converted). If it's not the case,
    /// a [`BaseConvertionError`] is returned with the corresponding variant.
    ///
    /// [`BaseConvertionError`]: enum.BaseConvertionError.html
    fn from_base64(base64_string: &str) -> Result<Self, BaseConvertionError>;

    /// Convert Signature into butes vector
    fn to_bytes_vector(&self) -> Vec<u8>;

    /// Encode the signature into Base64 string format.
    fn to_base64(&self) -> String;
}

/// Store a cryptographic signature.
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub enum Sig {
    /// Store a ed25519 Signature
    Ed25519(ed25519::Signature),
    /// Store a Schnorr Signature
    Schnorr(),
}

impl Sig {
    /// Get Sig size in bytes
    pub fn size_in_bytes(&self) -> usize {
        match *self {
            Sig::Ed25519(_) => *ed25519::SIG_SIZE_IN_BYTES + 2,
            Sig::Schnorr() => panic!("Schnorr algo not yet supported !"),
        }
    }
}

impl Display for Sig {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        write!(f, "{}", self.to_base64())
    }
}

impl GetKeysAlgo for Sig {
    fn algo(&self) -> KeysAlgo {
        match *self {
            Sig::Ed25519(_) => KeysAlgo::Ed25519,
            Sig::Schnorr() => KeysAlgo::Schnorr,
        }
    }
}

impl Signature for Sig {
    fn from_base64(_base64_string: &str) -> Result<Self, BaseConvertionError> {
        unimplemented!()
    }
    fn to_bytes_vector(&self) -> Vec<u8> {
        match *self {
            Sig::Ed25519(ed25519_sig) => ed25519_sig.to_bytes_vector(),
            Sig::Schnorr() => panic!("Schnorr algo not yet supported !"),
        }
    }
    fn to_base64(&self) -> String {
        match *self {
            Sig::Ed25519(ed25519_sig) => ed25519_sig.to_base64(),
            Sig::Schnorr() => panic!("Schnorr algo not yet supported !"),
        }
    }
}

/// Define the operations that can be performed on a cryptographic public key.
///
/// A `PublicKey` can be converted from/to Base64 format.
/// When converted back and forth the value should be the same.
///
/// A `PublicKey` is used to verify the signature of a message
/// with the associated [`PrivateKey`].
///
/// [`PrivateKey`]: trait.PrivateKey.html
pub trait PublicKey: Clone + Display + Debug + PartialEq + Eq + Hash + ToBase58 {
    /// Signature type of associated cryptosystem.
    type Signature: Signature;

    /// Create a PublicKey from a Base58 string.
    ///
    /// The Base58 string should contains only valid Base58 characters
    /// and have a correct length. If it's not the case,
    /// a [`BaseConvertionError`] is returned with the corresponding variant.
    ///
    /// [`BaseConvertionError`]: enum.BaseConvertionError.html
    fn from_base58(base58_string: &str) -> Result<Self, BaseConvertionError>;

    /// Convert into bytes vector
    fn to_bytes_vector(&self) -> Vec<u8>;

    /// Verify a signature with this public key.
    fn verify(&self, message: &[u8], signature: &Self::Signature) -> bool;
}

/// Store a cryptographic public key.
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub enum PubKey {
    /// Store a ed25519 public key.
    Ed25519(ed25519::PublicKey),
    /// Store a Schnorr public key.
    Schnorr(),
}

impl PubKey {
    /// Compute PubKey size in bytes
    pub fn size_in_bytes(&self) -> usize {
        match *self {
            PubKey::Ed25519(_) => ed25519::PUBKEY_SIZE_IN_BYTES + 3,
            PubKey::Schnorr() => panic!("Schnorr algo not yet supported !"),
        }
    }
}

impl Default for PubKey {
    fn default() -> Self {
        PubKey::Schnorr()
    }
}

impl GetKeysAlgo for PubKey {
    fn algo(&self) -> KeysAlgo {
        match *self {
            PubKey::Ed25519(_) => KeysAlgo::Ed25519,
            PubKey::Schnorr() => KeysAlgo::Schnorr,
        }
    }
}

impl ToBase58 for PubKey {
    fn to_base58(&self) -> String {
        match *self {
            PubKey::Ed25519(ed25519_pub) => ed25519_pub.to_base58(),
            PubKey::Schnorr() => panic!("Schnorr algo not yet supported !"),
        }
    }
}

impl Display for PubKey {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        write!(f, "{}", self.to_base58())
    }
}

impl PublicKey for PubKey {
    type Signature = Sig;

    fn from_base58(_base58_string: &str) -> Result<Self, BaseConvertionError> {
        unimplemented!()
    }
    fn to_bytes_vector(&self) -> Vec<u8> {
        match *self {
            PubKey::Ed25519(ed25519_pubkey) => ed25519_pubkey.to_bytes_vector(),
            PubKey::Schnorr() => panic!("Schnorr algo not yet supported !"),
        }
    }
    fn verify(&self, message: &[u8], signature: &Self::Signature) -> bool {
        match *self {
            PubKey::Ed25519(ed25519_pubkey) => {
                if let Sig::Ed25519(ed25519_sig) = signature {
                    ed25519_pubkey.verify(message, ed25519_sig)
                } else {
                    panic!("Try to verify a signature with public key of a different algorithm !\nSignature={:?}\nPublickey={:?}", signature, self)
                }
            }
            PubKey::Schnorr() => panic!("Schnorr algo not yet supported !"),
        }
    }
}

/// Define the operations that can be performed on a cryptographic private key.
///
/// A `PrivateKey` can be converted from/to Base58 format.
/// When converted back and forth the value should be the same.
///
/// A `PrivateKey` is used to sign a message. The corresponding [`PublicKey`]
/// will then be used to verify that signature.
///
/// [`PublicKey`]: trait.PublicKey.html
pub trait PrivateKey: Clone + Display + Debug + PartialEq + Eq + ToBase58 {
    /// Signature type of associated cryptosystem.
    type Signature: Signature;

    /// Create a PrivateKey from a Base58 string.
    ///
    /// The Base58 string should contains only valid Base58 characters
    /// and have a correct length. If it's not the case, a [`BaseConvertionError`]
    /// is returned with the corresponding variant.
    ///
    /// [`BaseConvertionError`]: enum.BaseConvertionError.html
    fn from_base58(base58_string: &str) -> Result<Self, BaseConvertionError>;

    /// Sign a message with this private key.
    fn sign(&self, message: &[u8]) -> Self::Signature;
}

/// Store a cryptographic private key.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PrivKey {
    /// Store a ed25519 private key.
    Ed25519(ed25519::PrivateKey),
    /// Store a Schnorr private key.
    Schnorr(),
}

impl GetKeysAlgo for PrivKey {
    fn algo(&self) -> KeysAlgo {
        match *self {
            PrivKey::Ed25519(_) => KeysAlgo::Ed25519,
            PrivKey::Schnorr() => KeysAlgo::Schnorr,
        }
    }
}

impl ToBase58 for PrivKey {
    fn to_base58(&self) -> String {
        match *self {
            PrivKey::Ed25519(ed25519_privkey) => ed25519_privkey.to_base58(),
            PrivKey::Schnorr() => panic!("Schnorr algo not yet supported !"),
        }
    }
}

impl Display for PrivKey {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        write!(f, "{}", self.to_base58())
    }
}

impl PrivateKey for PrivKey {
    type Signature = Sig;
    fn from_base58(_base58_string: &str) -> Result<Self, BaseConvertionError> {
        unimplemented!()
    }
    fn sign(&self, message: &[u8]) -> Self::Signature {
        match *self {
            PrivKey::Ed25519(ed25519_privkey) => Sig::Ed25519(ed25519_privkey.sign(message)),
            PrivKey::Schnorr() => panic!("Schnorr algo not yet supported !"),
        }
    }
}

/// Define the operations that can be performed on a cryptographic key pair.
pub trait KeyPair: Clone + Display + Debug + PartialEq + Eq {
    /// Signature type of associated cryptosystem.
    type Signature: Signature;
    /// PublicKey type of associated cryptosystem.
    type PublicKey: PublicKey;
    /// PrivateKey type of associated cryptosystem.
    type PrivateKey: PrivateKey;

    /// Get `PublicKey`
    fn public_key(&self) -> Self::PublicKey;

    /// Get `PrivateKey`
    fn private_key(&self) -> Self::PrivateKey;

    /// Sign a message with private key.
    fn sign(&self, message: &[u8]) -> Self::Signature;

    /// Verify a signature with public key.
    fn verify(&self, message: &[u8], signature: &Self::Signature) -> bool;
}

/// Store a cryptographic key pair.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum KeyPairEnum {
    /// Store a ed25519 key pair.
    Ed25519(ed25519::KeyPair),
    /// Store a Schnorr key pair.
    Schnorr(),
}

impl GetKeysAlgo for KeyPairEnum {
    fn algo(&self) -> KeysAlgo {
        match *self {
            KeyPairEnum::Ed25519(_) => KeysAlgo::Ed25519,
            KeyPairEnum::Schnorr() => KeysAlgo::Schnorr,
        }
    }
}

impl Display for KeyPairEnum {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        match *self {
            KeyPairEnum::Ed25519(ed25519_keypair) => {
                write!(f, "({}, hidden)", ed25519_keypair.pubkey.to_base58())
            }
            KeyPairEnum::Schnorr() => panic!("Schnorr algo not yet supported !"),
        }
    }
}

impl KeyPair for KeyPairEnum {
    type Signature = Sig;
    type PublicKey = PubKey;
    type PrivateKey = PrivKey;

    fn public_key(&self) -> Self::PublicKey {
        match *self {
            KeyPairEnum::Ed25519(ed25519_keypair) => PubKey::Ed25519(ed25519_keypair.public_key()),
            KeyPairEnum::Schnorr() => panic!("Schnorr algo not yet supported !"),
        }
    }
    fn private_key(&self) -> Self::PrivateKey {
        match *self {
            KeyPairEnum::Ed25519(ed25519_keypair) => {
                PrivKey::Ed25519(ed25519_keypair.private_key())
            }
            KeyPairEnum::Schnorr() => panic!("Schnorr algo not yet supported !"),
        }
    }
    fn verify(&self, message: &[u8], signature: &Sig) -> bool {
        match *self {
            KeyPairEnum::Ed25519(ed25519_keypair) => {
                if let Sig::Ed25519(ed25519_sig) = signature {
                    ed25519_keypair.verify(message, ed25519_sig)
                } else {
                    panic!("Try to verify a signature with key pair of a different algorithm !\nSignature={:?}\nKeyPair={:?}", signature, self)
                }
            }
            KeyPairEnum::Schnorr() => panic!("Schnorr algo not yet supported !"),
        }
    }
    fn sign(&self, message: &[u8]) -> Sig {
        match *self {
            KeyPairEnum::Ed25519(ed25519_keypair) => Sig::Ed25519(ed25519_keypair.sign(message)),
            KeyPairEnum::Schnorr() => panic!("Schnorr algo not yet supported !"),
        }
    }
}