-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prepare for configuration in the future (#102)
- Loading branch information
Showing
10 changed files
with
190 additions
and
57 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,41 @@ | ||
{ | ||
// Version of the setting file. Always 0.2 | ||
"version": "0.2", | ||
// language - current active spelling language | ||
"language": "en", | ||
// words - list of words to be always considered correct | ||
"words": [ | ||
"bindgen", | ||
"Codacy", | ||
"consts", | ||
"Ctarget", | ||
"ecies", | ||
"eciespy", | ||
"eciesrs", | ||
"helloworld", | ||
"HKDF", | ||
"keypair", | ||
"libsecp", | ||
"reqwest", | ||
"RUSTFLAGS", | ||
"sandybridge", | ||
"secp", | ||
"secp256k1", | ||
"ssse", | ||
"symm", | ||
"typenum" | ||
], | ||
// flagWords - list of words to be always considered incorrect | ||
// This is useful for offensive words and common spelling errors. | ||
// For example "hte" should be "the" | ||
"flagWords": ["hte"], | ||
"ignorePaths": [ | ||
".git", | ||
".github", | ||
".gitignore", | ||
".cspell.jsonc", | ||
"LICENSE", | ||
"package.json", | ||
"yarn.lock" | ||
] | ||
} |
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 |
---|---|---|
@@ -1,13 +1,6 @@ | ||
{ | ||
"spellright.language": ["en"], | ||
"spellright.documentTypes": ["markdown", "latex", "plaintext", "rust", "toml"], | ||
"rust-analyzer.cargo.features": ["pure"], | ||
"rust-analyzer.cargo.noDefaultFeatures": true, | ||
"rust-analyzer.procMacro.enable": true, | ||
"spellright.ignoreFiles": [ | ||
"~/.cargo/", | ||
"~/.rustup/", | ||
"**/.gitignore", | ||
"**/.spellignore" | ||
] | ||
"rust-analyzer.linkedProjects": ["./Cargo.toml"] | ||
} |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use std::sync::Mutex; | ||
|
||
use once_cell::sync::Lazy; | ||
|
||
use crate::consts::{COMPRESSED_PUBLIC_KEY_SIZE, UNCOMPRESSED_PUBLIC_KEY_SIZE}; | ||
|
||
pub enum SymmetricAlgorithm { | ||
Aes256Gcm, | ||
} | ||
|
||
pub struct Config { | ||
pub is_ephemeral_key_compressed: bool, | ||
pub is_hkdf_key_compressed: bool, | ||
pub symmetric_algorithm: SymmetricAlgorithm, | ||
} | ||
|
||
impl Config { | ||
pub fn default() -> Self { | ||
Config { | ||
is_ephemeral_key_compressed: false, | ||
is_hkdf_key_compressed: false, | ||
symmetric_algorithm: SymmetricAlgorithm::Aes256Gcm, | ||
} | ||
} | ||
} | ||
|
||
/// Global config | ||
pub static ECIES_CONFIG: Lazy<Mutex<Config>> = Lazy::new(|| { | ||
let config: Config = Config::default(); | ||
Mutex::new(config) | ||
}); | ||
|
||
pub fn update_config(config: Config) { | ||
*ECIES_CONFIG.lock().unwrap() = config; | ||
} | ||
|
||
pub fn reset_config() { | ||
update_config(Config::default()) | ||
} | ||
|
||
pub fn is_ephemeral_key_compressed() -> bool { | ||
ECIES_CONFIG.lock().unwrap().is_ephemeral_key_compressed | ||
} | ||
|
||
pub fn get_ephemeral_key_size() -> usize { | ||
if is_ephemeral_key_compressed() { | ||
COMPRESSED_PUBLIC_KEY_SIZE | ||
} else { | ||
UNCOMPRESSED_PUBLIC_KEY_SIZE | ||
} | ||
} | ||
|
||
pub fn is_hkdf_key_compressed() -> bool { | ||
ECIES_CONFIG.lock().unwrap().is_hkdf_key_compressed | ||
} |
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
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,49 @@ | ||
use ecies::{ | ||
config::{reset_config, update_config, Config, SymmetricAlgorithm}, | ||
decrypt, encrypt, | ||
utils::{decapsulate, encapsulate, generate_keypair}, | ||
PublicKey, SecretKey, | ||
}; | ||
|
||
use hex::decode; | ||
|
||
const MSG: &[u8] = "helloworld".as_bytes(); | ||
|
||
#[test] | ||
fn can_change_behavior_with_config() { | ||
let mut two = [0u8; 32]; | ||
let mut three = [0u8; 32]; | ||
two[31] = 2u8; | ||
three[31] = 3u8; | ||
|
||
let sk2 = SecretKey::parse_slice(&two).unwrap(); | ||
let pk2 = PublicKey::from_secret_key(&sk2); | ||
let sk3 = SecretKey::parse_slice(&three).unwrap(); | ||
let pk3 = PublicKey::from_secret_key(&sk3); | ||
|
||
update_config(Config { | ||
is_ephemeral_key_compressed: false, | ||
is_hkdf_key_compressed: true, | ||
symmetric_algorithm: SymmetricAlgorithm::Aes256Gcm, | ||
}); | ||
|
||
assert_eq!(encapsulate(&sk2, &pk3), decapsulate(&pk2, &sk3)); | ||
|
||
assert_eq!( | ||
encapsulate(&sk2, &pk3).map(|v| v.to_vec()).unwrap(), | ||
decode("b192b226edb3f02da11ef9c6ce4afe1c7e40be304e05ae3b988f4834b1cb6c69").unwrap() | ||
); | ||
|
||
update_config(Config { | ||
is_ephemeral_key_compressed: true, | ||
is_hkdf_key_compressed: true, | ||
symmetric_algorithm: SymmetricAlgorithm::Aes256Gcm, | ||
}); | ||
|
||
let (sk, pk) = generate_keypair(); | ||
let (sk, pk) = (&sk.serialize(), &pk.serialize_compressed()); | ||
|
||
assert_eq!(MSG, decrypt(sk, &encrypt(pk, MSG).unwrap()).unwrap().as_slice()); | ||
|
||
reset_config(); | ||
} |