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
//! Implementation of I2P's ElGamal public-key encryption scheme over the
//! 2048-bit MODP DH group.
//!
//! This implementation is not constant-time (yet).

#[macro_use]
extern crate lazy_static;

extern crate num_bigint;
extern crate num_traits;
extern crate rand;
extern crate sha2;

#[cfg(test)]
extern crate data_encoding;

use std::fmt;

mod constants;
mod elgamal;
mod math;

pub use elgamal::{Decryptor, Encryptor, KeyPairGenerator};

/// The public component of an ElGamal encryption keypair. Represents only the
/// exponent, not the primes (which are constants).
pub struct PublicKey(pub [u8; 256]);

impl PublicKey {
    fn from_bytes(buf: &[u8; 256]) -> Self {
        let mut x = [0u8; 256];
        x.copy_from_slice(buf);
        PublicKey(x)
    }
}

impl Clone for PublicKey {
    fn clone(&self) -> Self {
        PublicKey::from_bytes(&self.0)
    }
}

impl fmt::Debug for PublicKey {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        self.0[..].fmt(formatter)
    }
}

impl PartialEq for PublicKey {
    fn eq(&self, other: &Self) -> bool {
        self.0
            .iter()
            .zip(other.0.iter())
            .fold(true, |acc, (a, b)| acc && (a == b))
    }
}

/// The private component of an ElGamal encryption keypair.
pub struct PrivateKey(pub [u8; 256]);

impl PrivateKey {
    fn from_bytes(buf: &[u8; 256]) -> Self {
        let mut x = [0u8; 256];
        x.copy_from_slice(buf);
        PrivateKey(x)
    }
}

impl Clone for PrivateKey {
    fn clone(&self) -> Self {
        PrivateKey::from_bytes(&self.0)
    }
}

impl fmt::Debug for PrivateKey {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        self.0[..].fmt(formatter)
    }
}