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
#![cfg_attr(feature = "nightly", feature(external_doc))]
#![cfg_attr(feature = "nightly", doc(include = "../README.md"))]

use std::convert::TryFrom;

use curve25519_dalek::{
    constants,
    ristretto::{CompressedRistretto, RistrettoPoint},
    scalar::Scalar,
};
use rand_core::{CryptoRng, RngCore};
use serde::{Deserialize, Serialize};

#[cfg(test)]
use proptest::{arbitrary::Arbitrary, array, collection, prelude::*};

/// A Diffie-Hellman secret key used to derive a shared secret when
/// combined with a public key, that only exists for a short time.
#[cfg_attr(test, derive(Debug))]
pub struct EphemeralSecret(pub(crate) Scalar);

impl From<[u8; 32]> for EphemeralSecret {
    fn from(bytes: [u8; 32]) -> EphemeralSecret {
        match Scalar::from_canonical_bytes(bytes) {
            Some(scalar) => Self(scalar),
            None => Self(Scalar::from_bytes_mod_order(bytes)),
        }
    }
}

impl From<[u8; 64]> for EphemeralSecret {
    fn from(bytes: [u8; 64]) -> EphemeralSecret {
        Self(Scalar::from_bytes_mod_order_wide(&bytes))
    }
}

impl EphemeralSecret {
    /// Generate a `EphemeralSecret` using a new scalar mod the group
    /// order.
    pub fn new<T>(mut rng: T) -> EphemeralSecret
    where
        T: RngCore + CryptoRng,
    {
        Self(Scalar::random(&mut rng))
    }

    /// Do Diffie-Hellman key agreement between self's secret
    /// and a peer's public key, resulting in a `SharedSecret`.
    pub fn diffie_hellman(&self, peer_public: &PublicKey) -> SharedSecret {
        SharedSecret(self.0 * peer_public.0)
    }
}

#[cfg(test)]
impl Arbitrary for EphemeralSecret {
    type Parameters = ();

    fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
        array::uniform32(any::<u8>()).prop_map_into().boxed()
    }

    type Strategy = BoxedStrategy<Self>;
}

/// The public key derived from an ephemeral or static secret key.
#[derive(Clone, Copy, Debug, Eq, Deserialize, PartialEq, Serialize)]
pub struct PublicKey(pub(crate) RistrettoPoint);

impl<'a> From<&'a EphemeralSecret> for PublicKey {
    fn from(secret: &'a EphemeralSecret) -> PublicKey {
        Self(&secret.0 * &constants::RISTRETTO_BASEPOINT_TABLE)
    }
}

impl From<PublicKey> for [u8; 32] {
    /// Copy the bytes of the internal `RistrettoPoint` as the
    /// canonical compressed wire format. Two `RistrettoPoint`s (and
    /// thus two `PublicKey`s) are equal iff their encodings are
    /// equal.
    fn from(public_key: PublicKey) -> [u8; 32] {
        public_key.0.compress().to_bytes()
    }
}

impl<'a> From<&'a StaticSecret> for PublicKey {
    fn from(secret: &'a StaticSecret) -> PublicKey {
        Self(&secret.0 * &constants::RISTRETTO_BASEPOINT_TABLE)
    }
}

impl TryFrom<[u8; 32]> for PublicKey {
    type Error = &'static str;

    /// Attempts to decompress an internal `RistrettoPoint` from the
    /// input bytes, which should be the canonical compressed encoding
    /// of a `RistrettoPoint`.
    fn try_from(bytes: [u8; 32]) -> Result<PublicKey, Self::Error> {
        match CompressedRistretto::from_slice(&bytes).decompress() {
            Some(ristretto_point) => Ok(Self(ristretto_point)),
            None => Err("Ristretto point decompression failed"),
        }
    }
}

#[cfg(test)]
impl Arbitrary for PublicKey {
    type Parameters = ();

    fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
        array::uniform32(any::<u8>())
            .prop_filter_map("Decompressible Ristretto point", |b| {
                PublicKey::try_from(b).ok()
            })
            .boxed()
    }

    type Strategy = BoxedStrategy<Self>;
}

/// A Diffie-Hellman shared secret derived from an `EphemeralSecret`
/// or `StaticSecret` and the other party's `PublicKey`.
pub struct SharedSecret(pub(crate) RistrettoPoint);

impl From<SharedSecret> for [u8; 32] {
    /// Copy the bytes of the internal `RistrettoPoint` as the
    /// canonical compressed wire format. Two `RistrettoPoint`s (and
    /// thus two `PublicKey`s) are equal iff their encodings are
    /// equal.
    fn from(shared_secret: SharedSecret) -> [u8; 32] {
        shared_secret.0.compress().to_bytes()
    }
}

/// A Diffie-Hellman secret key used to derive a shared secret when
/// combined with a public key, that can be stored and loaded.
#[derive(Clone, Copy, Eq, PartialEq, Deserialize, Serialize)]
#[cfg_attr(test, derive(Debug))]
pub struct StaticSecret(pub(crate) Scalar);

impl From<StaticSecret> for [u8; 32] {
    fn from(static_secret: StaticSecret) -> [u8; 32] {
        static_secret.0.to_bytes()
    }
}

impl From<[u8; 32]> for StaticSecret {
    fn from(bytes: [u8; 32]) -> StaticSecret {
        match Scalar::from_canonical_bytes(bytes) {
            Some(scalar) => Self(scalar),
            None => Self(Scalar::from_bytes_mod_order(bytes)),
        }
    }
}

impl From<[u8; 64]> for StaticSecret {
    fn from(bytes: [u8; 64]) -> StaticSecret {
        Self(Scalar::from_bytes_mod_order_wide(&bytes))
    }
}

impl StaticSecret {
    /// Generate a `StaticSecret` using a new scalar mod the group
    /// order.
    pub fn new<T>(mut rng: T) -> StaticSecret
    where
        T: RngCore + CryptoRng,
    {
        Self(Scalar::random(&mut rng))
    }

    /// Do Diffie-Hellman key agreement between self's secret
    /// and a peer's public key, resulting in a `SharedSecret`.
    pub fn diffie_hellman(&self, peer_public: &PublicKey) -> SharedSecret {
        SharedSecret(self.0 * peer_public.0)
    }
}

#[cfg(test)]
impl Arbitrary for StaticSecret {
    type Parameters = ();

    fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
        array::uniform32(any::<u8>())
            .prop_filter("Valid scalar mod l", |b| {
                Scalar::from_bytes_mod_order(*b).is_canonical()
            })
            .prop_map(|bytes| return Self::from(bytes))
            .boxed()
    }

    type Strategy = BoxedStrategy<Self>;
}

#[cfg(test)]
mod tests {

    use bincode;
    use rand_core::OsRng;

    use super::*;

    #[test]
    fn random_dh() {
        let alice_secret = EphemeralSecret::new(&mut OsRng);
        let alice_public = PublicKey::from(&alice_secret);

        let bob_secret = StaticSecret::new(&mut OsRng);
        let bob_public = PublicKey::from(&bob_secret);

        let alice_shared_secret = alice_secret.diffie_hellman(&bob_public);
        let bob_shared_secret = bob_secret.diffie_hellman(&alice_public);

        assert_eq!(
            <[u8; 32]>::from(alice_shared_secret),
            <[u8; 32]>::from(bob_shared_secret)
        );
    }

    proptest! {

        #[test]
        fn random_dh_wide(alice_bytes in collection::vec(any::<u8>(), 64),
                          bob_bytes in collection::vec(any::<u8>(), 64)) {
            let mut a = [0u8; 64];
            a.copy_from_slice(alice_bytes.as_slice());

            let alice_secret = EphemeralSecret::from(a);
            let alice_public = PublicKey::from(&alice_secret);

            let mut b = [0u8; 64];
            b.copy_from_slice(bob_bytes.as_slice());

            let bob_secret = StaticSecret::from(b);
            let bob_public = PublicKey::from(&bob_secret);

            let alice_shared_secret = alice_secret.diffie_hellman(&bob_public);
            let bob_shared_secret = bob_secret.diffie_hellman(&alice_public);

            assert_eq!(
                <[u8; 32]>::from(alice_shared_secret),
                <[u8; 32]>::from(bob_shared_secret)
            );
        }

        #[test]
        fn ephemeral_dh(
            alice_secret in any::<EphemeralSecret>(),
            bob_secret in any::<EphemeralSecret>()
        ) {
            let alice_public = PublicKey::from(&alice_secret);
            let bob_public = PublicKey::from(&bob_secret);

            let alice_shared_secret = alice_secret.diffie_hellman(&bob_public);
            let bob_shared_secret = bob_secret.diffie_hellman(&alice_public);

            prop_assert_eq!(
                <[u8; 32]>::from(alice_shared_secret),
                <[u8; 32]>::from(bob_shared_secret)
            );
        }

        #[test]
        fn static_dh(
            alice_secret in any::<StaticSecret>(),
            bob_secret in any::<StaticSecret>()
        ) {
            let alice_public = PublicKey::from(&alice_secret);
            let bob_public = PublicKey::from(&bob_secret);

            let alice_shared_secret = alice_secret.diffie_hellman(&bob_public);
            let bob_shared_secret = bob_secret.diffie_hellman(&alice_public);

            prop_assert_eq!(
                <[u8; 32]>::from(alice_shared_secret),
                <[u8; 32]>::from(bob_shared_secret)
            );
        }

        #[test]
        fn serde_pubkey(alice_secret in any::<EphemeralSecret>()) {
            let alice_public = PublicKey::from(&alice_secret);

            let serialized = bincode::serialize(&alice_public).unwrap();

            prop_assert_eq!(
                alice_public, bincode::deserialize(&serialized[..]).unwrap()
            );
        }

        #[test]
        fn serde_static_key(alice_secret in any::<StaticSecret>()) {
            let serialized = bincode::serialize(&alice_secret).unwrap();

            prop_assert_eq!(
                alice_secret, bincode::deserialize(&serialized[..]).unwrap()
            );
        }

        #[test]
        fn from_into_pubkey_bytes(pubkey in any::<PublicKey>()) {
            let bytes: [u8; 32] = pubkey.into();

            prop_assert_eq!(
                Ok(pubkey), PublicKey::try_from(bytes)
            );
        }

        #[test]
        fn from_into_static_secret_bytes(static_secret in any::<StaticSecret>()) {
            let bytes: [u8; 32] = static_secret.into();

            prop_assert_eq!(
                static_secret, StaticSecret::from(bytes)
            );
        }

        #[test]
        fn scalar_mul_different_paths(
            secret in any::<EphemeralSecret>(),
        ) {
            let other_public = PublicKey(constants::RISTRETTO_BASEPOINT_POINT * secret.0);

            prop_assert_eq!(
                other_public,
                PublicKey::from(&secret)
            );
        }

    }
}