safe_crypto 0.8.0

A convenience library providing abstractions for cryptographic functions.
Documentation
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
// Copyright 2018 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under the MIT license <LICENSE-MIT
// https://opensource.org/licenses/MIT> or the Modified BSD license <LICENSE-BSD
// https://opensource.org/licenses/BSD-3-Clause>, at your option. This file may not be copied,
// modified, or distributed except according to those terms. Please review the Licences for the
// specific language governing permissions and limitations relating to use of the SAFE Network
// Software.

//! Mock cryptographic primitives.
//!
//! These primitives are designed to be very fast (several times faster than the real ones), but
//! they are NOT secure. They are supposed to be used for testing only.

/// Mock hash functions.
pub(crate) mod hashing_impl {
    use super::hash64;

    /// Fast mock version of the Keccak SHA-3 hash function.
    pub(crate) fn sha3_256(data: &[u8]) -> [u8; 32] {
        let mut hash = [0; 32];
        hash64(data)
            .iter()
            .cycle()
            .take(32)
            .enumerate()
            .for_each(|(i, b)| hash[i] = *b);
        hash
    }
}

/// Mock version of the `scrypt` crate.
pub(crate) mod derive_impl {
    use crate::Error;
    pub(crate) use scrypt::ScryptParams;

    pub(crate) fn scrypt(
        password: &[u8],
        salt: &[u8],
        _params: &ScryptParams,
        output: &mut [u8],
    ) -> Result<(), Error> {
        // Use params with lowest complexity setting.
        let params = ScryptParams::new(1, 8, 1).map_err(|_| Error::DeriveKey)?;
        scrypt::scrypt(password, salt, &params, output).map_err(|_| Error::DeriveKey)
    }
}

/// Mock version of a subset of the `rust_sodium` crate.
pub(crate) mod crypto_impl {
    use rand::{Rng, SeedableRng, XorShiftRng};
    use std::cell::RefCell;

    thread_local! {
        static RNG: RefCell<XorShiftRng> = RefCell::new(XorShiftRng::new_unseeded());
    }

    /// Initialise mock `rust_sodium`.
    pub(crate) fn init() -> Result<(), ()> {
        Ok(())
    }

    /// Initialise mock `rust_sodium` with the given random number generator. This can be used to
    /// guarantee reproducible test results.
    pub(crate) fn init_with_rng<T: Rng>(other: &mut T) -> Result<(), i32> {
        RNG.with(|rng| rng.borrow_mut().reseed(other.gen()));
        Ok(())
    }

    /// Mock cryptographic functions.
    pub(crate) mod crypto {
        /// Mock signing.
        pub(crate) mod sign {
            use super::super::with_rng;
            use crate::mock_crypto::hash512;
            use rand::Rng;
            use serde::de::{SeqAccess, Visitor};
            use serde::ser::SerializeTuple;
            use serde::{self, Deserialize, Deserializer, Serialize, Serializer};
            use std::marker::PhantomData;
            use std::ops::{Index, RangeFull};
            use std::{cmp, fmt, hash};

            /// Number of bytes in a `PublicKey`.
            pub(crate) const PUBLICKEYBYTES: usize = 32;
            /// Number of bytes in a `SecretKey`.
            pub(crate) const SECRETKEYBYTES: usize = 64;
            /// Number of bytes in a `Signature`.
            pub(crate) const SIGNATUREBYTES: usize = 64;
            /// Number of bytes in a `Seed`.
            pub(crate) const SEEDBYTES: usize = 32;

            /// Mock signing public key.
            #[derive(
                Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize,
            )]
            pub(crate) struct PublicKey(pub(crate) [u8; PUBLICKEYBYTES]);

            impl Index<RangeFull> for PublicKey {
                type Output = [u8];
                fn index(&self, index: RangeFull) -> &[u8] {
                    self.0.index(index)
                }
            }

            /// Mock signing secret key.
            #[derive(Clone)]
            pub(crate) struct SecretKey(pub(crate) [u8; SECRETKEYBYTES]);

            /// Mock signature.
            #[derive(Clone, Copy)]
            pub(crate) struct Signature(pub(crate) [u8; SIGNATUREBYTES]);

            impl hash::Hash for Signature {
                fn hash<H: hash::Hasher>(&self, state: &mut H) {
                    hash::Hash::hash(&self.0[..], state)
                }
            }

            impl Ord for Signature {
                #[inline]
                fn cmp(&self, other: &Signature) -> cmp::Ordering {
                    Ord::cmp(&&self.0[..], &&other.0[..])
                }
            }

            impl PartialOrd for Signature {
                #[inline]
                fn partial_cmp(&self, other: &Signature) -> Option<cmp::Ordering> {
                    PartialOrd::partial_cmp(&&self.0[..], &&other.0[..])
                }

                #[inline]
                fn lt(&self, other: &Signature) -> bool {
                    PartialOrd::lt(&&self.0[..], &&other.0[..])
                }

                #[inline]
                fn le(&self, other: &Signature) -> bool {
                    PartialOrd::le(&&self.0[..], &&other.0[..])
                }

                #[inline]
                fn ge(&self, other: &Signature) -> bool {
                    PartialOrd::ge(&&self.0[..], &&other.0[..])
                }

                #[inline]
                fn gt(&self, other: &Signature) -> bool {
                    PartialOrd::gt(&&self.0[..], &&other.0[..])
                }
            }

            struct ArrayVisitor<A> {
                marker: PhantomData<A>,
            }

            impl<A> ArrayVisitor<A> {
                fn new() -> Self {
                    ArrayVisitor {
                        marker: PhantomData,
                    }
                }
            }

            macro_rules! impl_common_key_traits {
                ($type:tt, $inner_size:ident) => {
                    impl fmt::Debug for $type {
                        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                            fmt::Debug::fmt(&&self.0[..], f)
                        }
                    }

                    impl PartialEq for $type {
                        #[inline]
                        fn eq(&self, other: &$type) -> bool {
                            self.0[..] == other.0[..]
                        }
                    }
                    impl Eq for $type {}

                    impl Serialize for $type {
                        #[inline]
                        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
                        where
                            S: Serializer,
                        {
                            let mut seq = serializer.serialize_tuple($inner_size)?;
                            for e in self.0.iter() {
                                seq.serialize_element(e)?;
                            }
                            seq.end()
                        }
                    }

                    impl<'de> Visitor<'de> for ArrayVisitor<$type> {
                        type Value = $type;

                        fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                            formatter.write_str(&format!("an array of length {}", $inner_size))
                        }

                        #[inline]
                        fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
                        where
                            A: SeqAccess<'de>,
                        {
                            let mut key = [0u8; $inner_size];
                            for elem in key.iter_mut() {
                                *elem = match seq.next_element()? {
                                    Some(val) => val,
                                    None => {
                                        return Err(serde::de::Error::invalid_length(
                                            $inner_size,
                                            &self,
                                        ));
                                    }
                                };
                            }

                            Ok($type(key))
                        }
                    }

                    impl<'de> Deserialize<'de> for $type {
                        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
                        where
                            D: Deserializer<'de>,
                        {
                            deserializer
                                .deserialize_tuple($inner_size, ArrayVisitor::<$type>::new())
                        }
                    }
                };
            }

            impl_common_key_traits!(SecretKey, SECRETKEYBYTES);
            impl_common_key_traits!(Signature, SIGNATUREBYTES);

            impl AsRef<[u8]> for Signature {
                fn as_ref(&self) -> &[u8] {
                    &self.0
                }
            }

            /// Generate mock public and corresponding secret key.
            pub(crate) fn gen_keypair() -> (PublicKey, SecretKey) {
                with_rng(|rng| {
                    let pub_key: [u8; 32] = rng.gen();
                    let mut sec_key = [0u8; 64];
                    sec_key[0..32].clone_from_slice(&pub_key); // simply get 64 byte array
                    (PublicKey(pub_key), SecretKey(sec_key))
                })
            }

            /// Mock seed.
            #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)]
            pub(crate) struct Seed(pub(crate) [u8; SEEDBYTES]);

            /// Generate mock public and corresponding secret key using a seed.
            pub(crate) fn keypair_from_seed(seed: &Seed) -> (PublicKey, SecretKey) {
                let pub_key = seed.0;
                let mut sec_key = [0u8; 64];
                sec_key[0..32].clone_from_slice(&seed.0); // simply get 64 byte array
                (PublicKey(pub_key), SecretKey(sec_key))
            }

            /// Sign a message using the mock secret key.
            pub(crate) fn sign_detached(m: &[u8], sk: &SecretKey) -> Signature {
                let mut temp = m.to_vec();
                temp.extend_from_slice(&sk.0);
                Signature(hash512(&temp))
            }

            /// Verify the mock signature against the message and the signer's mock public key.
            pub(crate) fn verify_detached(signature: &Signature, m: &[u8], pk: &PublicKey) -> bool {
                let mut temp = m.to_vec();
                temp.extend(&pk.0);
                temp.extend(&[0; 32]);
                *signature == Signature(hash512(&temp))
            }
        }

        /// Mock encryption.
        pub(crate) mod box_ {
            use super::super::with_rng;
            use rand::Rng;
            use std::ops::{Index, RangeFull};

            /// Number of bytes in a `PublicKey`.
            pub(crate) const PUBLICKEYBYTES: usize = 32;
            /// Number of bytes in a `SecretKey`.
            pub(crate) const SECRETKEYBYTES: usize = 32;
            /// Number of bytes in a `Nonce`.
            pub(crate) const NONCEBYTES: usize = 24;
            /// Number of bytes in a `SharedSecretKey`.
            pub(crate) const PRECOMPUTEDKEYBYTES: usize = 32;

            /// Mock public key for asymmetric encryption/decryption.
            #[derive(
                Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize,
            )]
            pub(crate) struct PublicKey(pub(crate) [u8; PUBLICKEYBYTES]);

            impl Index<RangeFull> for PublicKey {
                type Output = [u8];
                fn index(&self, index: RangeFull) -> &[u8] {
                    self.0.index(index)
                }
            }

            /// Mock secret key for asymmetric encryption/decryption.
            #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
            pub(crate) struct SecretKey(pub(crate) [u8; SECRETKEYBYTES]);

            /// Mock nonce for asymmetric encryption/decryption.
            #[derive(Serialize, Deserialize)]
            pub(crate) struct Nonce(pub(crate) [u8; NONCEBYTES]);

            #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
            pub(crate) struct PrecomputedKey(pub(crate) [u8; SECRETKEYBYTES]);

            /// Generate mock public and corresponding secret key.
            pub(crate) fn gen_keypair() -> (PublicKey, SecretKey) {
                with_rng(|rng| {
                    let value = rng.gen();
                    (PublicKey(value), SecretKey(value))
                })
            }

            /// Generate mock nonce.
            pub(crate) fn gen_nonce() -> Nonce {
                with_rng(|rng| Nonce(rng.gen()))
            }

            /// Generate mock shared key
            pub(crate) fn precompute(pk: &PublicKey, sk: &SecretKey) -> PrecomputedKey {
                let mut shared_secret: [u8; SECRETKEYBYTES] = [0; SECRETKEYBYTES];
                for (i, shared) in shared_secret.iter_mut().enumerate().take(pk.0.len()) {
                    *shared = pk.0[i] ^ sk.0[i];
                }
                PrecomputedKey(shared_secret)
            }

            /// Perform mock encryption of the given message using the shared key and nonce.
            pub(crate) fn seal_precomputed(
                m: &[u8],
                nonce: &Nonce,
                sk: &PrecomputedKey,
            ) -> Vec<u8> {
                let mut result = Vec::with_capacity(m.len() + nonce.0.len() + sk.0.len());
                result.extend(&nonce.0);
                result.extend(&sk.0);
                result.extend(m);
                result
            }

            /// Perform mock decryption of the given ciphertext using their secret key, our public
            /// key and nonce.
            pub(crate) fn open_precomputed(
                c: &[u8],
                nonce: &Nonce,
                sk: &PrecomputedKey,
            ) -> Result<Vec<u8>, ()> {
                let n = nonce.0.len();
                let s = sk.0.len();

                if c[0..n] != nonce.0 {
                    return Err(());
                }

                if c[n..n + s] != sk.0 {
                    return Err(());
                }

                Ok(c[n + s..].to_vec())
            }
        }

        pub(crate) mod sealedbox {
            use crate::box_::{PublicKey, SecretKey};

            /// Perform mock anonymous encryption.
            pub(crate) fn seal(m: &[u8], pk: &PublicKey) -> Vec<u8> {
                let mut result = Vec::with_capacity(m.len() + pk.0.len());
                result.extend(&pk.0);
                result.extend(m);
                result
            }

            /// Perform mock anonymous decryption.
            pub(crate) fn open(c: &[u8], pk: &PublicKey, sk: &SecretKey) -> Result<Vec<u8>, ()> {
                let p = pk.0.len();

                if c[0..p] != pk.0 {
                    return Err(());
                }

                if pk.0 != sk.0 {
                    return Err(());
                }

                Ok(c[p..].to_vec())
            }
        }

        /// Mock symmetric encryption.
        pub(crate) mod secretbox {
            use super::super::with_rng;
            use rand::Rng;

            /// Number of bytes in a `Key`.
            pub(crate) const KEYBYTES: usize = 32;
            /// Number of bytes in a `Nonce`.
            pub(crate) const NONCEBYTES: usize = 24;

            /// Mock secret key for symmetric encryption/decryption.
            #[derive(
                Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize,
            )]
            pub(crate) struct Key(pub(crate) [u8; KEYBYTES]);

            /// Mock nonce for symmetric encryption/decryption.
            #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
            pub(crate) struct Nonce(pub(crate) [u8; NONCEBYTES]);

            /// Generate mock public and corresponding secret key.
            pub(crate) fn gen_key() -> Key {
                with_rng(|rng| Key(rng.gen()))
            }

            /// Generate mock nonce.
            pub(crate) fn gen_nonce() -> Nonce {
                with_rng(|rng| Nonce(rng.gen()))
            }

            /// Perform mock symmetric encryption.
            pub(crate) fn seal(m: &[u8], nonce: &Nonce, key: &Key) -> Vec<u8> {
                let mut result = Vec::with_capacity(m.len() + nonce.0.len() + key.0.len());
                result.extend(&key.0);
                result.extend(&nonce.0);
                result.extend(m);
                result
            }

            /// Perform mock symmetric decryption.
            pub(crate) fn open(c: &[u8], nonce: &Nonce, key: &Key) -> Result<Vec<u8>, ()> {
                let p = key.0.len();
                let n = nonce.0.len();

                if c[0..p] != key.0 {
                    return Err(());
                }

                if c[p..p + n] != nonce.0 {
                    return Err(());
                }

                Ok(c[p + n..].to_vec())
            }
        }
    }

    fn with_rng<F, R>(f: F) -> R
    where
        F: FnOnce(&mut XorShiftRng) -> R,
    {
        RNG.with(|rng| f(&mut *rng.borrow_mut()))
    }
}

fn hash64(data: &[u8]) -> [u8; 8] {
    use std::collections::hash_map::DefaultHasher;
    use std::hash::Hasher;

    let mut hasher = DefaultHasher::new();
    hasher.write(data);

    let hash = hasher.finish();
    [
        (hash >> 56) as u8,
        (hash >> 48) as u8,
        (hash >> 40) as u8,
        (hash >> 32) as u8,
        (hash >> 24) as u8,
        (hash >> 16) as u8,
        (hash >> 8) as u8,
        (hash) as u8,
    ]
}

fn hash512(data: &[u8]) -> [u8; 64] {
    let mut hash = [0u8; 64];
    hash[56..].clone_from_slice(&hash64(data));
    hash
}

#[cfg(test)]
mod tests {
    use super::crypto_impl::crypto::{
        box_,
        sign::{self, Seed, SEEDBYTES},
    };
    use rand::{self, Rng};

    #[test]
    fn keypair_generation() {
        // Sign keypairs.

        let (sign_pk0, sign_sk0) = sign::gen_keypair();
        let (sign_pk1, sign_sk1) = sign::gen_keypair();
        assert_ne!(sign_pk0, sign_pk1);
        assert_ne!(sign_sk0, sign_sk1);

        // Sign keypairs from a seed.

        let seed0 = Seed([0; SEEDBYTES]);
        let (sign_pk0, sign_sk0) = sign::keypair_from_seed(&seed0);
        let (sign_pk1, sign_sk1) = sign::keypair_from_seed(&seed0);
        assert_eq!(sign_pk0, sign_pk1);
        assert_eq!(sign_sk0, sign_sk1);

        let seed1 = Seed([1; SEEDBYTES]);
        let (sign_pk2, sign_sk2) = sign::keypair_from_seed(&seed1);
        assert_ne!(sign_pk0, sign_pk2);
        assert_ne!(sign_sk0, sign_sk2);

        // Encrypt keypairs.

        let (box_pk0, box_sk0) = box_::gen_keypair();
        let (box_pk1, box_sk1) = box_::gen_keypair();
        assert_ne!(box_pk0, box_pk1);
        assert_ne!(box_sk0, box_sk1);
    }

    #[test]
    fn sign_and_verify() {
        let (pk0, sk0) = sign::gen_keypair();
        let message: Vec<_> = rand::thread_rng().gen_iter().take(10).collect();

        let signature = sign::sign_detached(&message, &sk0);
        assert!(sign::verify_detached(&signature, &message, &pk0));

        let (pk1, _) = sign::gen_keypair();
        assert!(!sign::verify_detached(&signature, &message, &pk1));
    }
}