|
1 | 1 | //! This module contains logic related to cryptographic keys.
|
2 |
| -pub mod seeds { |
3 |
| - //! This module contains logic related to cryptographic seeds. |
4 |
| - //! |
5 |
| - //! Specifically, it contains the logic for storing the seed and providing |
6 |
| - //! it to other modules. |
7 |
| - //! |
8 |
| - //! A **seed** is a pseudo-random number that is used as a secret key for |
9 |
| - //! cryptographic operations. |
10 |
| - use self::detail::CURRENT_SEED; |
11 |
| - use crate::shared::crypto::ephemeral_instance_keys::{Seed, RANDOM_SEED}; |
12 |
| - |
13 |
| - /// This trait is for structures that can keep and provide a seed. |
14 |
| - pub trait Keeper { |
15 |
| - type Seed: Sized + Default + AsMut<[u8]>; |
16 |
| - |
17 |
| - /// It returns a reference to the seed that is keeping. |
18 |
| - fn get_seed() -> &'static Self::Seed; |
| 2 | +//! |
| 3 | +//! Specifically, it contains the logic for storing the seed and providing |
| 4 | +//! it to other modules. |
| 5 | +//! |
| 6 | +//! It also provides the logic for the cipher for encryption and decryption. |
| 7 | +
|
| 8 | +use self::detail_cipher::CURRENT_CIPHER; |
| 9 | +use self::detail_seed::CURRENT_SEED; |
| 10 | +pub use crate::shared::crypto::ephemeral_instance_keys::CipherArrayBlowfish; |
| 11 | +use crate::shared::crypto::ephemeral_instance_keys::{CipherBlowfish, Seed, RANDOM_CIPHER_BLOWFISH, RANDOM_SEED}; |
| 12 | + |
| 13 | +/// This trait is for structures that can keep and provide a seed. |
| 14 | +pub trait Keeper { |
| 15 | + type Seed: Sized + Default + AsMut<[u8]>; |
| 16 | + type Cipher: cipher::BlockCipher; |
| 17 | + |
| 18 | + /// It returns a reference to the seed that is keeping. |
| 19 | + fn get_seed() -> &'static Self::Seed; |
| 20 | + fn get_cipher_blowfish() -> &'static Self::Cipher; |
| 21 | +} |
| 22 | + |
| 23 | +/// The keeper for the instance. When the application is running |
| 24 | +/// in production, this will be the seed keeper that is used. |
| 25 | +pub struct Instance; |
| 26 | + |
| 27 | +/// The keeper for the current execution. It's a facade at compilation |
| 28 | +/// time that will either be the instance seed keeper (with a randomly |
| 29 | +/// generated key for production) or the zeroed seed keeper. |
| 30 | +pub struct Current; |
| 31 | + |
| 32 | +impl Keeper for Instance { |
| 33 | + type Seed = Seed; |
| 34 | + type Cipher = CipherBlowfish; |
| 35 | + |
| 36 | + fn get_seed() -> &'static Self::Seed { |
| 37 | + &RANDOM_SEED |
19 | 38 | }
|
20 | 39 |
|
21 |
| - /// The seed keeper for the instance. When the application is running |
22 |
| - /// in production, this will be the seed keeper that is used. |
23 |
| - pub struct Instance; |
| 40 | + fn get_cipher_blowfish() -> &'static Self::Cipher { |
| 41 | + &RANDOM_CIPHER_BLOWFISH |
| 42 | + } |
| 43 | +} |
24 | 44 |
|
25 |
| - /// The seed keeper for the current execution. It's a facade at compilation |
26 |
| - /// time that will either be the instance seed keeper (with a randomly |
27 |
| - /// generated key for production) or the zeroed seed keeper. |
28 |
| - pub struct Current; |
| 45 | +impl Keeper for Current { |
| 46 | + type Seed = Seed; |
| 47 | + type Cipher = CipherBlowfish; |
29 | 48 |
|
30 |
| - impl Keeper for Instance { |
31 |
| - type Seed = Seed; |
| 49 | + #[allow(clippy::needless_borrow)] |
| 50 | + fn get_seed() -> &'static Self::Seed { |
| 51 | + &CURRENT_SEED |
| 52 | + } |
32 | 53 |
|
33 |
| - fn get_seed() -> &'static Self::Seed { |
34 |
| - &RANDOM_SEED |
35 |
| - } |
| 54 | + fn get_cipher_blowfish() -> &'static Self::Cipher { |
| 55 | + &CURRENT_CIPHER |
36 | 56 | }
|
| 57 | +} |
| 58 | + |
| 59 | +#[cfg(test)] |
| 60 | +mod tests { |
| 61 | + |
| 62 | + use super::detail_seed::ZEROED_TEST_SEED; |
| 63 | + use super::{Current, Instance, Keeper}; |
| 64 | + use crate::shared::crypto::ephemeral_instance_keys::{CipherBlowfish, Seed, ZEROED_TEST_CIPHER_BLOWFISH}; |
37 | 65 |
|
38 |
| - impl Keeper for Current { |
| 66 | + pub struct ZeroedTest; |
| 67 | + |
| 68 | + impl Keeper for ZeroedTest { |
39 | 69 | type Seed = Seed;
|
| 70 | + type Cipher = CipherBlowfish; |
40 | 71 |
|
41 | 72 | #[allow(clippy::needless_borrow)]
|
42 | 73 | fn get_seed() -> &'static Self::Seed {
|
43 |
| - &CURRENT_SEED |
| 74 | + &ZEROED_TEST_SEED |
| 75 | + } |
| 76 | + |
| 77 | + fn get_cipher_blowfish() -> &'static Self::Cipher { |
| 78 | + &ZEROED_TEST_CIPHER_BLOWFISH |
44 | 79 | }
|
45 | 80 | }
|
46 | 81 |
|
| 82 | + #[test] |
| 83 | + fn the_default_seed_and_the_zeroed_seed_should_be_the_same_when_testing() { |
| 84 | + assert_eq!(Current::get_seed(), ZeroedTest::get_seed()); |
| 85 | + } |
| 86 | + |
| 87 | + #[test] |
| 88 | + fn the_default_seed_and_the_instance_seed_should_be_different_when_testing() { |
| 89 | + assert_ne!(Current::get_seed(), Instance::get_seed()); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +mod detail_seed { |
| 94 | + use crate::shared::crypto::ephemeral_instance_keys::Seed; |
| 95 | + |
| 96 | + #[allow(dead_code)] |
| 97 | + pub const ZEROED_TEST_SEED: Seed = [0u8; 32]; |
| 98 | + |
47 | 99 | #[cfg(test)]
|
48 |
| - mod tests { |
49 |
| - use super::detail::ZEROED_TEST_SEED; |
50 |
| - use super::{Current, Instance, Keeper}; |
51 |
| - use crate::shared::crypto::ephemeral_instance_keys::Seed; |
| 100 | + pub use ZEROED_TEST_SEED as CURRENT_SEED; |
52 | 101 |
|
53 |
| - pub struct ZeroedTestSeed; |
| 102 | + #[cfg(not(test))] |
| 103 | + pub use crate::shared::crypto::ephemeral_instance_keys::RANDOM_SEED as CURRENT_SEED; |
54 | 104 |
|
55 |
| - impl Keeper for ZeroedTestSeed { |
56 |
| - type Seed = Seed; |
| 105 | + #[cfg(test)] |
| 106 | + mod tests { |
| 107 | + use crate::shared::crypto::ephemeral_instance_keys::RANDOM_SEED; |
| 108 | + use crate::shared::crypto::keys::detail_seed::ZEROED_TEST_SEED; |
| 109 | + use crate::shared::crypto::keys::CURRENT_SEED; |
57 | 110 |
|
58 |
| - #[allow(clippy::needless_borrow)] |
59 |
| - fn get_seed() -> &'static Self::Seed { |
60 |
| - &ZEROED_TEST_SEED |
61 |
| - } |
| 111 | + #[test] |
| 112 | + fn it_should_have_a_zero_test_seed() { |
| 113 | + assert_eq!(ZEROED_TEST_SEED, [0u8; 32]); |
62 | 114 | }
|
63 | 115 |
|
64 | 116 | #[test]
|
65 |
| - fn the_default_seed_and_the_zeroed_seed_should_be_the_same_when_testing() { |
66 |
| - assert_eq!(Current::get_seed(), ZeroedTestSeed::get_seed()); |
| 117 | + fn it_should_default_to_zeroed_seed_when_testing() { |
| 118 | + assert_eq!(CURRENT_SEED, ZEROED_TEST_SEED); |
67 | 119 | }
|
68 | 120 |
|
69 | 121 | #[test]
|
70 |
| - fn the_default_seed_and_the_instance_seed_should_be_different_when_testing() { |
71 |
| - assert_ne!(Current::get_seed(), Instance::get_seed()); |
| 122 | + fn it_should_have_a_large_random_seed() { |
| 123 | + assert!(u128::from_ne_bytes((*RANDOM_SEED)[..16].try_into().unwrap()) > u128::from(u64::MAX)); |
| 124 | + assert!(u128::from_ne_bytes((*RANDOM_SEED)[16..].try_into().unwrap()) > u128::from(u64::MAX)); |
72 | 125 | }
|
73 | 126 | }
|
| 127 | +} |
74 | 128 |
|
75 |
| - mod detail { |
76 |
| - use crate::shared::crypto::ephemeral_instance_keys::Seed; |
77 |
| - |
78 |
| - #[allow(dead_code)] |
79 |
| - pub const ZEROED_TEST_SEED: &Seed = &[0u8; 32]; |
80 |
| - |
81 |
| - #[cfg(test)] |
82 |
| - pub use ZEROED_TEST_SEED as CURRENT_SEED; |
| 129 | +mod detail_cipher { |
| 130 | + #[allow(unused_imports)] |
| 131 | + #[cfg(not(test))] |
| 132 | + pub use crate::shared::crypto::ephemeral_instance_keys::RANDOM_CIPHER_BLOWFISH as CURRENT_CIPHER; |
| 133 | + #[cfg(test)] |
| 134 | + pub use crate::shared::crypto::ephemeral_instance_keys::ZEROED_TEST_CIPHER_BLOWFISH as CURRENT_CIPHER; |
83 | 135 |
|
84 |
| - #[cfg(not(test))] |
85 |
| - pub use crate::shared::crypto::ephemeral_instance_keys::RANDOM_SEED as CURRENT_SEED; |
| 136 | + #[cfg(test)] |
| 137 | + mod tests { |
| 138 | + use cipher::BlockEncrypt; |
86 | 139 |
|
87 |
| - #[cfg(test)] |
88 |
| - mod tests { |
89 |
| - use crate::shared::crypto::ephemeral_instance_keys::RANDOM_SEED; |
90 |
| - use crate::shared::crypto::keys::seeds::detail::ZEROED_TEST_SEED; |
91 |
| - use crate::shared::crypto::keys::seeds::CURRENT_SEED; |
| 140 | + use crate::shared::crypto::ephemeral_instance_keys::{CipherArrayBlowfish, ZEROED_TEST_CIPHER_BLOWFISH}; |
| 141 | + use crate::shared::crypto::keys::detail_cipher::CURRENT_CIPHER; |
92 | 142 |
|
93 |
| - #[test] |
94 |
| - fn it_should_have_a_zero_test_seed() { |
95 |
| - assert_eq!(*ZEROED_TEST_SEED, [0u8; 32]); |
96 |
| - } |
| 143 | + #[test] |
| 144 | + fn it_should_default_to_zeroed_seed_when_testing() { |
| 145 | + let mut data: cipher::generic_array::GenericArray<u8, _> = CipherArrayBlowfish::from([0u8; 8]); |
| 146 | + let mut data_2 = CipherArrayBlowfish::from([0u8; 8]); |
97 | 147 |
|
98 |
| - #[test] |
99 |
| - fn it_should_default_to_zeroed_seed_when_testing() { |
100 |
| - assert_eq!(*CURRENT_SEED, *ZEROED_TEST_SEED); |
101 |
| - } |
| 148 | + CURRENT_CIPHER.encrypt_block(&mut data); |
| 149 | + ZEROED_TEST_CIPHER_BLOWFISH.encrypt_block(&mut data_2); |
102 | 150 |
|
103 |
| - #[test] |
104 |
| - fn it_should_have_a_large_random_seed() { |
105 |
| - assert!(u128::from_ne_bytes((*RANDOM_SEED)[..16].try_into().unwrap()) > u128::from(u64::MAX)); |
106 |
| - assert!(u128::from_ne_bytes((*RANDOM_SEED)[16..].try_into().unwrap()) > u128::from(u64::MAX)); |
107 |
| - } |
| 151 | + assert_eq!(data, data_2); |
108 | 152 | }
|
109 | 153 | }
|
110 | 154 | }
|
0 commit comments