-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
65640ba
commit f0668b6
Showing
44 changed files
with
2,112 additions
and
136 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
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "wedpr_macros" | ||
version = "1.0.0" | ||
version = "1.1.0" | ||
authors = ["WeDPR <[email protected]>"] | ||
edition = "2018" | ||
|
||
|
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,10 +1,13 @@ | ||
[package] | ||
name = "wedpr_utils" | ||
version = "1.0.0" | ||
version = "1.1.0" | ||
authors = ["WeDPR <[email protected]>"] | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
base64 = "0.10.1" | ||
failure = "0.1" | ||
hex = "0.3.0" | ||
wedpr_macros = { path = "../macros/"} |
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,41 @@ | ||
// Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0. | ||
|
||
//! Basic encoding and decoding functions. | ||
|
||
extern crate hex; | ||
|
||
use crate::{coder::Coder, error::WedprError}; | ||
|
||
#[derive(Default, Debug, Clone)] | ||
pub struct WedprHex {} | ||
|
||
/// Implements Hex as a Coder instance. | ||
impl Coder for WedprHex { | ||
fn encode<T: ?Sized + AsRef<[u8]>>(&self, input: &T) -> String { | ||
hex::encode(input) | ||
} | ||
|
||
fn decode(&self, input: &str) -> Result<Vec<u8>, WedprError> { | ||
match hex::decode(input) { | ||
Ok(v) => return Ok(v), | ||
Err(_) => { | ||
wedpr_println!("hex decoding failed, input was: {}", input); | ||
return Err(WedprError::DecodeError); | ||
}, | ||
}; | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_hex() { | ||
let hex = WedprHex::default(); | ||
let str = "5c74d17c6a"; | ||
let bytes = hex.decode(&str).unwrap(); | ||
let recovered_str = hex.encode(&bytes); | ||
assert_eq!(str, recovered_str); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -4,5 +4,8 @@ | |
|
||
#[macro_use] | ||
extern crate failure; | ||
#[macro_use] | ||
extern crate wedpr_macros; | ||
|
||
pub mod coder; | ||
pub mod error; |
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,19 +1,19 @@ | ||
[package] | ||
name = "wedpr_crypto" | ||
version = "1.0.0" | ||
version = "1.1.0" | ||
authors = ["WeDPR <[email protected]>"] | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
base64 = "0.10.1" | ||
bulletproofs = "1.0.4" | ||
curve25519-dalek = { version = "1", features = ["serde"] } | ||
ecies = "0.1.4" | ||
lazy_static = "0.2" | ||
merlin = "1" | ||
rand = "0.6" | ||
secp256k1 = { version = "0.17.2", features = ["recovery"] } | ||
secp256k1 = { version = "0.19.0", features = ["recovery", "rand"] } | ||
sha3 = "0.8" | ||
wedpr_macros = { path = "../common/macros/"} | ||
wedpr_protos = { path = "../protos/" } | ||
|
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,27 @@ | ||
// Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0. | ||
|
||
//! ECIES (Elliptic Curve Integrated Encryption Scheme) functions. | ||
//! ECIES is a public-key authenticated encryption scheme, which allows | ||
//! using a public key to encrypt a message of any length and provide integrity | ||
//! check. | ||
|
||
use wedpr_utils::error::WedprError; | ||
|
||
pub mod secp256k1; | ||
|
||
/// Trait of a replaceable ECIES algorithm. | ||
pub trait Ecies { | ||
/// Encrypts a message by ECIES with a public key. | ||
fn encrypt<T: ?Sized + AsRef<[u8]>>( | ||
&self, | ||
public_key: &T, | ||
message: &T, | ||
) -> Result<Vec<u8>, WedprError>; | ||
|
||
/// Decrypts a ciphertext by ECIES with a private key. | ||
fn decrypt<T: ?Sized + AsRef<[u8]>>( | ||
&self, | ||
private_key: &T, | ||
ciphertext: &T, | ||
) -> Result<Vec<u8>, WedprError>; | ||
} |
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,77 @@ | ||
// Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0. | ||
|
||
//! ECIES functions on Secp256k1 curve. | ||
|
||
extern crate ecies; | ||
|
||
use wedpr_utils::error::WedprError; | ||
|
||
use crate::ecies::Ecies; | ||
|
||
#[derive(Default, Debug, Clone)] | ||
pub struct WedprSecp256k1Ecies {} | ||
|
||
/// Implements a ECIES instance on Secp256k1 curve. | ||
impl Ecies for WedprSecp256k1Ecies { | ||
fn encrypt<T: ?Sized + AsRef<[u8]>>( | ||
&self, | ||
public_key: &T, | ||
message: &T, | ||
) -> Result<Vec<u8>, WedprError> | ||
{ | ||
match ecies::encrypt(public_key.as_ref(), message.as_ref()) { | ||
Ok(v) => Ok(v.to_vec()), | ||
Err(_) => { | ||
wedpr_println!("secp256k1 ECIES encrypt failed"); | ||
return Err(WedprError::FormatError); | ||
}, | ||
} | ||
} | ||
|
||
fn decrypt<T: ?Sized + AsRef<[u8]>>( | ||
&self, | ||
private_key: &T, | ||
ciphertext: &T, | ||
) -> Result<Vec<u8>, WedprError> | ||
{ | ||
match ecies::decrypt(private_key.as_ref(), ciphertext.as_ref()) { | ||
Ok(v) => Ok(v.to_vec()), | ||
Err(_) => { | ||
wedpr_println!("secp256k1 ECIES decrypt failed"); | ||
return Err(WedprError::FormatError); | ||
}, | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::{ | ||
constant::tests::{ | ||
BASE64_ENCODED_TEST_MESSAGE, SECP256K1_TEST_PUBLIC_KEY, | ||
SECP256K1_TEST_SECRET_KEY, | ||
}, | ||
utils::{bytes_to_string, string_to_bytes}, | ||
}; | ||
|
||
#[test] | ||
fn test_secp256k1_ecies() { | ||
let secp256k1_ecies = WedprSecp256k1Ecies::default(); | ||
|
||
let encoded_msg = BASE64_ENCODED_TEST_MESSAGE; | ||
let ciphertext = secp256k1_ecies | ||
.encrypt( | ||
&string_to_bytes(SECP256K1_TEST_PUBLIC_KEY).unwrap(), | ||
&string_to_bytes(encoded_msg).unwrap(), | ||
) | ||
.unwrap(); | ||
let decrypted_msg = secp256k1_ecies | ||
.decrypt( | ||
&string_to_bytes(SECP256K1_TEST_SECRET_KEY).unwrap(), | ||
&ciphertext, | ||
) | ||
.unwrap(); | ||
assert_eq!(bytes_to_string(&decrypted_msg), encoded_msg); | ||
} | ||
} |
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
Oops, something went wrong.