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
use Error;
use serde::{Serialize, Deserialize, Deserializer, Serializer};
use serde::de::{Visitor, SeqAccess};
use serde::ser::SerializeTuple;
use serde;
use std::fmt;
use ring;
use untrusted;

/// Wrapper around `[u8; 64]`, mostly to implement serde traits.
pub struct SignatureBytes(pub [u8;64]);

/// A public and secret key.
pub enum KeyPair {
    Ed25519(ring::signature::Ed25519KeyPair)
}

impl KeyPair {

    /// Parse a `KeyPair` from a PKCS#8 binary buffer.
    pub fn from_pkcs8(input: &[u8]) -> Result<Self, Error> {
        Ok(KeyPair::Ed25519(
            ring::signature::Ed25519KeyPair::from_pkcs8(
                untrusted::Input::from(input)
            )?
        ))
    }

    /// Sign a message.
    pub fn sign(&self, msg: &[u8]) -> Signature {
        match *self {
            KeyPair::Ed25519(ref k) => {
                let mut pk = [0; 32];
                pk.clone_from_slice(k.public_key_bytes());
                let signature = k.sign(msg);
                let mut sig = [0; 64];
                sig.clone_from_slice(signature.as_ref());
                Signature::Ed25519 { publickey: pk, signature: SignatureBytes(sig) }
            }
        }
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum Signature {
    Ed25519 { publickey: [u8; 32], signature: SignatureBytes }
}

impl Signature {

    /// Public key corresponding to the secret key used to sign.
    pub fn public_key(&self) -> &[u8] {
        match *self {
            Signature::Ed25519 { ref publickey, .. } => publickey
        }
    }

    /// Verify the signature for the given message.
    pub fn verify<S:AsRef<[u8]>>(&self, msg: S) -> Result<(), Error> {
        match *self {
            Signature::Ed25519 { ref publickey, ref signature } => {
                debug!("verifying signature {:?} {:?}", publickey, signature);
                Ok(ring::signature::verify(
                    &ring::signature::EdDSAParameters,
                    untrusted::Input::from(publickey),
                    untrusted::Input::from(msg.as_ref()),
                    untrusted::Input::from(&signature.0),
                )?)
            }
        }
    }

    /// Length of that signature in bytes, once serialized to bincode.
    pub fn len(&self) -> usize {
        match *self {
            Signature::Ed25519 { .. } =>
            // u32 (signature variant id) + 32 bytes + 64 bytes
                100
        }
    }
}

impl AsRef<[u8]> for Signature {
    fn as_ref(&self) -> &[u8] {
        match *self {
            Signature::Ed25519 { ref signature, .. } => &signature.0
        }
    }
}

impl<'de> Deserialize<'de> for SignatureBytes {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where D: Deserializer<'de>
    {
        struct Vis;
        impl<'de> Visitor<'de> for Vis {
            type Value = SignatureBytes;
            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("64 bytes")
            }
            fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
                let mut result = [0; 64];
                for x in result.iter_mut() {
                    if let Some(y) = seq.next_element()? {
                        *x = y
                    } else {
                        return Err(serde::de::Error::invalid_length(64, &self))
                    }
                }
                Ok(SignatureBytes(result))
            }
        }
        deserializer.deserialize_tuple(64, Vis)
    }
}

impl Serialize for SignatureBytes {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where S: Serializer
    {
        let mut tup = serializer.serialize_tuple(64)?;
        for byte in self.0.iter() {
            tup.serialize_element(byte)?;
        }
        tup.end()
    }
}

impl fmt::Debug for SignatureBytes {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        write!(fmt, "{:?}", &self.0[..])
    }
}

impl Clone for SignatureBytes {
    fn clone(&self) -> Self {
        let mut result = SignatureBytes([0;64]);
        result.0.clone_from_slice(&self.0);
        result
    }
}