-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
crypto: clean up crypto and cypher clode
- Loading branch information
Showing
6 changed files
with
192 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
pub mod keys { | ||
|
||
pub mod seeds { | ||
use crate::static_keys::static_seeds::{SeedSize, INSTANCE_SEED}; | ||
|
||
pub trait Seed { | ||
type Seed: Sized + Default + AsMut<[u8]>; | ||
fn seed() -> &'static Self::Seed; | ||
} | ||
|
||
pub struct DefaultSeed; | ||
pub struct InstanceSeed; | ||
|
||
impl Seed for DefaultSeed { | ||
type Seed = SeedSize; | ||
fn seed() -> &'static Self::Seed { | ||
&*self::detail::DEFAULT_SEED | ||
} | ||
} | ||
|
||
impl Seed for InstanceSeed { | ||
type Seed = SeedSize; | ||
fn seed() -> &'static Self::Seed { | ||
&*INSTANCE_SEED | ||
} | ||
} | ||
|
||
pub fn initialize_default_seed() { | ||
lazy_static::initialize(&self::detail::DEFAULT_SEED); | ||
} | ||
|
||
mod detail { | ||
|
||
pub use crate::static_keys::static_seeds::INSTANCE_SEED; | ||
|
||
#[cfg(not(test))] | ||
pub use INSTANCE_SEED as DEFAULT_SEED; | ||
|
||
#[cfg(test)] | ||
pub use crate::static_keys::static_seeds::ZEROED_TEST_SEED as DEFAULT_SEED; | ||
} | ||
} | ||
|
||
pub mod block_ciphers { | ||
use cipher::{BlockSizeUser, generic_array::GenericArray}; | ||
|
||
use crate::static_keys::block_ciphers::Cypher; | ||
|
||
use super::seeds::initialize_default_seed; | ||
|
||
pub trait BlockCipher { | ||
type BlockCipher: cipher::BlockCipher; | ||
fn block_cipher() -> &'static Self::BlockCipher; | ||
} | ||
|
||
pub type CypherArray = GenericArray<u8, <Cypher as BlockSizeUser>::BlockSize>; | ||
|
||
pub struct DefaultBlockCipher; | ||
pub struct InstanceBlockCipher; | ||
|
||
impl BlockCipher for DefaultBlockCipher { | ||
type BlockCipher = Cypher; | ||
fn block_cipher() -> &'static Self::BlockCipher { | ||
&*self::detail::DEFAULT_BLOCK_CIPHER | ||
} | ||
} | ||
|
||
impl BlockCipher for InstanceBlockCipher { | ||
type BlockCipher = Cypher; | ||
fn block_cipher() -> &'static Self::BlockCipher { | ||
&*self::detail::INSTANCE_CIPHER | ||
} | ||
} | ||
|
||
pub fn initialize_default_block_cipher() { | ||
initialize_default_seed(); | ||
lazy_static::initialize(&self::detail::DEFAULT_BLOCK_CIPHER); | ||
} | ||
|
||
mod detail { | ||
|
||
pub use crate::static_keys::block_ciphers::INSTANCE_BLOCK_CIPHER_BLOWFISH as INSTANCE_CIPHER; | ||
|
||
#[cfg(not(test))] | ||
pub use INSTANCE_CIPHER as DEFAULT_BLOCK_CIPHER; | ||
|
||
#[cfg(test)] | ||
pub use crate::static_keys::block_ciphers::TEST_BLOCK_CIPHER_BLOWFISH as DEFAULT_BLOCK_CIPHER; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.