Skip to content

Commit

Permalink
add ios && android interface
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoXuan40404 authored and qyan-dev committed Oct 27, 2020
1 parent 65640ba commit f0668b6
Show file tree
Hide file tree
Showing 44 changed files with 2,112 additions and 136 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ members = [
"common/macros",
"ffi/ffi_common",
"ffi/ffi_macros",
"ffi/ffi_vcl",
"ffi/ffi_c/ffi_c_vcl",
"ffi/ffi_java/ffi_java_vcl",
"ffi/ffi_c/ffi_c_crypto",
"ffi/ffi_java/ffi_java_crypto",
"protos",
]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ cargo run
在本项目的根目录(即`WeDPR-Lab-Core`目录)中,运行如下命令。

```bash
cargo doc
cargo doc --no-deps
```

以上命令将根据代码中的注释,在`target/doc`子目录中,生成的SDK接口文档。
Expand Down
2 changes: 1 addition & 1 deletion common/macros/Cargo.toml
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"

Expand Down
5 changes: 4 additions & 1 deletion common/utils/Cargo.toml
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/"}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

extern crate base64;

use crate::coder::Coder;
use wedpr_utils::error::WedprError;
use crate::{coder::Coder, error::WedprError};

#[derive(Default, Debug, Clone)]
pub struct WedprBase64 {}
Expand Down
41 changes: 41 additions & 0 deletions common/utils/src/coder/basic.rs
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);
}
}
3 changes: 2 additions & 1 deletion crypto/src/coder/mod.rs → common/utils/src/coder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
//! Data encoding and decoding functions.

pub mod base_x;
pub mod basic;

use wedpr_utils::error::WedprError;
use crate::error::WedprError;

/// Trait of a replaceable coder algorithm.
pub trait Coder {
Expand Down
3 changes: 3 additions & 0 deletions common/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@

#[macro_use]
extern crate failure;
#[macro_use]
extern crate wedpr_macros;

pub mod coder;
pub mod error;
6 changes: 3 additions & 3 deletions crypto/Cargo.toml
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/" }
Expand Down
14 changes: 12 additions & 2 deletions crypto/src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ use curve25519_dalek::{
};
extern crate secp256k1;
use crate::{
coder::base_x::WedprBase64, hash::keccak256::WedprKeccak256,
ecies::secp256k1::WedprSecp256k1Ecies, hash::keccak256::WedprKeccak256,
signature::secp256k1::WedprSecp256k1Recover,
};
use secp256k1::{All, Secp256k1, VerifyOnly};
use sha3::Sha3_512;
use wedpr_utils::coder::base_x::WedprBase64;

lazy_static! {
/// A base point used by various crypto algorithms.
Expand All @@ -31,6 +32,7 @@ lazy_static! {

/// Shared coder algorithm reference for quick implementation replacement.
/// Other code should use this reference, and not directly use a specific implementation.
// You may change WedprBase64 to other coder implementation such as WedprHex.
pub static ref CODER: WedprBase64 = WedprBase64::default();

/// Shared signature algorithm reference for quick implementation replacement.
Expand All @@ -41,16 +43,24 @@ lazy_static! {
/// Shared hash algorithm reference for quick implementation replacement.
/// Other code should use this reference, and not directly use a specific implementation.
pub static ref HASH: WedprKeccak256 = WedprKeccak256::default();

/// Shared ECIES algorithm reference for quick implementation replacement.
/// Other code should use this reference, and not directly use a specific implementation.
pub static ref ECIES: WedprSecp256k1Ecies = WedprSecp256k1Ecies::default();
}

/// Serialized data size of a point.
pub const RISTRETTO_POINT_SIZE_IN_BYTES: usize = 32;

/// Constants only used by tests.
pub mod tests {
// secp256k1 test key pair
// Test key pair for secp256k1 algorithms.
pub const SECP256K1_TEST_SECRET_KEY: &str =
"EMGwfgpqDQVUsVO7jxUniSNYxTPjGcbbf6eikaAIKog=";
pub const SECP256K1_TEST_PUBLIC_KEY: &str =
"ApBrmk0PHxPp4ElvnhlmwEiAklVdDbX+MicqML591eC2";

// Test string for a base64 encoded message.
pub const BASE64_ENCODED_TEST_MESSAGE: &str =
"g6sLGLyLvnkOSvBcQdKNSPx8m4XmAogueNMmE6V0Ico=";
}
27 changes: 27 additions & 0 deletions crypto/src/ecies/mod.rs
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>;
}
77 changes: 77 additions & 0 deletions crypto/src/ecies/secp256k1.rs
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);
}
}
23 changes: 14 additions & 9 deletions crypto/src/hash/keccak256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,29 @@ pub struct WedprKeccak256 {}

/// Implements FISCO-BCOS-compatible Keccak256 as a Hash instance.
impl Hash for WedprKeccak256 {
fn hash(&self, msg: &str) -> Vec<u8> {
let mut mes = Keccak256::default();
mes.input(msg);
mes.result().to_vec()
fn hash<T: ?Sized + AsRef<[u8]>>(&self, input: &T) -> Vec<u8> {
let mut hash_algorithm = Keccak256::default();
hash_algorithm.input(input);
hash_algorithm.result().to_vec()
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::utils::bytes_to_string;
use crate::{
constant::tests::BASE64_ENCODED_TEST_MESSAGE,
utils::{bytes_to_string, string_to_bytes},
};

#[test]
fn test_keccak256() {
let keccak256 = WedprKeccak256::default();
let msg = "WeDPR";
let computed_hash = bytes_to_string(&keccak256.hash(msg));
let expected_hash = "g6sLGLyLvnkOSvBcQdKNSPx8m4XmAogueNMmE6V0Ico=";
assert_eq!(expected_hash, computed_hash);
let msg = BASE64_ENCODED_TEST_MESSAGE;
let expected_hash = "5S04Vv6HBCWG6xNARqwPb294Hz/3BlaFVwKvAJByd9Q=";
assert_eq!(
expected_hash,
bytes_to_string(&keccak256.hash(&string_to_bytes(&msg).unwrap()))
);
}
}
5 changes: 3 additions & 2 deletions crypto/src/hash/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod keccak256;

/// Trait of a replaceable hash algorithm.
pub trait Hash {
/// Generates a fixed length hash bytes vector from any string.
fn hash(&self, msg: &str) -> Vec<u8>;
/// Generates a fixed length hash bytes vector from a bytes array of any
/// length.
fn hash<T: ?Sized + AsRef<[u8]>>(&self, input: &T) -> Vec<u8>;
}
2 changes: 1 addition & 1 deletion crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ extern crate wedpr_macros;
#[macro_use]
extern crate lazy_static;

pub mod coder;
pub mod constant;
pub mod ecies;
pub mod hash;
pub mod signature;
pub mod utils;
Expand Down
21 changes: 13 additions & 8 deletions crypto/src/signature/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,21 @@ pub mod secp256k1;
/// Trait of a replaceable signature algorithm.
pub trait Signature {
/// Signs a message hash with the private key.
fn sign(
fn sign<T: ?Sized + AsRef<[u8]>>(
&self,
private_key: &str,
msg_hash_str: &str,
) -> Result<String, WedprError>;
private_key: &T,
msg_hash: &T,
) -> Result<Vec<u8>, WedprError>;

/// Verifies a message hash with the public key.
fn verify(
fn verify<T: ?Sized + AsRef<[u8]>>(
&self,
public_key: &str,
msg_hash_str: &str,
signature: &str,
public_key: &T,
msg_hash: &T,
signature: &T,
) -> bool;

/// Generates a new key pair for signature algorithm.
// TODO: Replace output list with a struct or protobuf.
fn generate_keypair(&self) -> (Vec<u8>, Vec<u8>);
}
Loading

0 comments on commit f0668b6

Please sign in to comment.