forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto.rs
More file actions
179 lines (138 loc) · 5.81 KB
/
crypto.rs
File metadata and controls
179 lines (138 loc) · 5.81 KB
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
pub mod keys {
pub mod seeds {
use self::detail::DEFAULT_SEED;
use crate::ephemeral_instance_keys::{Seed, RANDOM_SEED};
pub trait SeedKeeper {
type Seed: Sized + Default + AsMut<[u8]>;
fn get_seed() -> &'static Self::Seed;
}
pub struct InstanceSeed;
pub struct DefaultSeed;
impl SeedKeeper for InstanceSeed {
type Seed = Seed;
fn get_seed() -> &'static Self::Seed {
&RANDOM_SEED
}
}
impl SeedKeeper for DefaultSeed {
type Seed = Seed;
#[allow(clippy::needless_borrow)]
fn get_seed() -> &'static Self::Seed {
&DEFAULT_SEED
}
}
#[cfg(test)]
mod tests {
use super::detail::ZEROED_TEST_SEED;
use super::{DefaultSeed, InstanceSeed, SeedKeeper};
use crate::ephemeral_instance_keys::Seed;
pub struct ZeroedTestSeed;
impl SeedKeeper for ZeroedTestSeed {
type Seed = Seed;
#[allow(clippy::needless_borrow)]
fn get_seed() -> &'static Self::Seed {
&ZEROED_TEST_SEED
}
}
#[test]
fn the_default_seed_and_the_zeroed_seed_should_be_the_same_when_testing() {
assert_eq!(DefaultSeed::get_seed(), ZeroedTestSeed::get_seed())
}
#[test]
fn the_default_seed_and_the_instance_seed_should_be_different_when_testing() {
assert_ne!(DefaultSeed::get_seed(), InstanceSeed::get_seed())
}
}
mod detail {
use crate::ephemeral_instance_keys::Seed;
#[allow(dead_code)]
pub const ZEROED_TEST_SEED: &Seed = &[0u8; 32];
#[cfg(test)]
pub use ZEROED_TEST_SEED as DEFAULT_SEED;
#[cfg(not(test))]
pub use crate::ephemeral_instance_keys::RANDOM_SEED as DEFAULT_SEED;
#[cfg(test)]
mod tests {
use std::convert::TryInto;
use crate::ephemeral_instance_keys::RANDOM_SEED;
use crate::protocol::crypto::keys::seeds::detail::ZEROED_TEST_SEED;
use crate::protocol::crypto::keys::seeds::DEFAULT_SEED;
#[test]
fn it_should_have_a_zero_test_seed() {
assert_eq!(*ZEROED_TEST_SEED, [0u8; 32])
}
#[test]
fn it_should_default_to_zeroed_seed_when_testing() {
assert_eq!(*DEFAULT_SEED, *ZEROED_TEST_SEED)
}
#[test]
fn it_should_have_a_large_random_seed() {
assert!(u128::from_ne_bytes((*RANDOM_SEED)[..16].try_into().unwrap()) > u64::MAX as u128);
assert!(u128::from_ne_bytes((*RANDOM_SEED)[16..].try_into().unwrap()) > u64::MAX as u128);
}
}
}
}
pub mod block_ciphers {
use cipher::generic_array::GenericArray;
use cipher::BlockSizeUser;
pub(super) use crate::block_ciphers::ephemeral_instance::BLOCK_CIPHER_BLOWFISH as INSTANCE_BLOCK_CIPHER;
#[allow(unused_imports)]
pub(super) use crate::block_ciphers::testing::TEST_BLOCK_CIPHER_BLOWFISH as TEST_BLOCK_CIPHER;
use crate::block_ciphers::Cipher;
pub trait BlockCipherKeeper {
type BlockCipher: cipher::BlockCipher;
fn get_block_cipher() -> &'static Self::BlockCipher;
}
pub type CipherArray = GenericArray<u8, <Cipher as BlockSizeUser>::BlockSize>;
pub struct DefaultBlockCipher;
pub struct InstanceBlockCipher;
impl BlockCipherKeeper for DefaultBlockCipher {
type BlockCipher = Cipher;
fn get_block_cipher() -> &'static Self::BlockCipher {
&self::detail::DEFAULT_BLOCK_CIPHER
}
}
impl BlockCipherKeeper for InstanceBlockCipher {
type BlockCipher = Cipher;
fn get_block_cipher() -> &'static Self::BlockCipher {
&INSTANCE_BLOCK_CIPHER
}
}
#[cfg(test)]
mod tests {
use cipher::BlockEncrypt;
use super::{BlockCipherKeeper, CipherArray, DefaultBlockCipher, InstanceBlockCipher, TEST_BLOCK_CIPHER};
use crate::block_ciphers::Cipher;
pub struct TestBlockCipher;
impl BlockCipherKeeper for TestBlockCipher {
type BlockCipher = Cipher;
fn get_block_cipher() -> &'static Self::BlockCipher {
&TEST_BLOCK_CIPHER
}
}
#[test]
fn when_testing_the_default_and_test_block_ciphers_should_be_the_same() {
let mut array = CipherArray::from([0u8; 8]);
let mut array2 = CipherArray::from([0u8; 8]);
DefaultBlockCipher::get_block_cipher().encrypt_block(&mut array);
TestBlockCipher::get_block_cipher().encrypt_block(&mut array2);
assert_eq!(array, array2)
}
#[test]
fn when_testing_the_default_and_instance_block_ciphers_should_be_the_different() {
let mut array = CipherArray::from([0u8; 8]);
let mut array2 = CipherArray::from([0u8; 8]);
DefaultBlockCipher::get_block_cipher().encrypt_block(&mut array);
InstanceBlockCipher::get_block_cipher().encrypt_block(&mut array2);
assert_ne!(array, array2)
}
}
mod detail {
#[cfg(not(test))]
pub(super) use super::INSTANCE_BLOCK_CIPHER as DEFAULT_BLOCK_CIPHER;
#[cfg(test)]
pub(super) use super::TEST_BLOCK_CIPHER as DEFAULT_BLOCK_CIPHER;
}
}
}