Skip to content

Commit

Permalink
feat: add support for conditional compilation of protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
dufkan committed Oct 16, 2023
1 parent d60cb96 commit 6a6fbf7
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
14 changes: 11 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ edition = "2018"
crate-type = ["cdylib", "lib"]

[dependencies]
mpecdsa = { git = "https://github.com/jirigav/mpecdsa.git" }
mpecdsa = { git = "https://github.com/jirigav/mpecdsa.git", optional = true }
openssl = "0.10.55"
prost = "0.11"
serde = "1.0"
serde_json = "1.0"
typetag = "0.2.1"
elastic-elgamal = { version = "0.3.0", features = ["serde"] }
elastic-elgamal = { version = "0.3.0", features = ["serde"], optional = true }
rand = "0.8.5"
curve25519-dalek = { version = "4", default-features = false, features = ["alloc"] }
frost-secp256k1 = { git = "https://github.com/dufkan/frost.git", branch = "serialize-state", features = ["serde"] }
frost-secp256k1 = { git = "https://github.com/dufkan/frost.git", branch = "serialize-state", features = ["serde"], optional = true }
aes-gcm = "0.10.2"

[build-dependencies]
Expand All @@ -28,3 +28,11 @@ prost-build = "0.11"
[dev-dependencies]
p256 = { version="0.13.2", features = ["ecdsa"] }
sha2 = "0.10.7"

[features]
default = ["gg18", "frost", "elgamal", "bindings"]
protocol = []
bindings = []
gg18 = ["protocol", "dep:mpecdsa"]
frost = ["protocol", "dep:frost-secp256k1"]
elgamal = ["protocol", "dep:elastic-elgamal"]
2 changes: 2 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use std::env;
fn main() {
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();

// TODO remove and configure cbindgen to respect build features (if possible)
#[cfg(feature = "bindings")]
cbindgen::Builder::new()
.with_crate(crate_dir)
.with_language(cbindgen::Language::C)
Expand Down
27 changes: 26 additions & 1 deletion src/c_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ use std::ffi::CString;
use std::os::raw::c_char;

use crate::auth;
use crate::protocol::{self, elgamal, frost, gg18, KeygenProtocol, ThresholdProtocol};
#[cfg(feature = "elgamal")]
use crate::protocol::elgamal;
#[cfg(feature = "frost")]
use crate::protocol::frost;
#[cfg(feature = "gg18")]
use crate::protocol::gg18;
#[cfg(feature = "protocol")]
use crate::protocol::{self, KeygenProtocol, ThresholdProtocol};

#[repr(C)]
pub enum ProtocolId {
Expand Down Expand Up @@ -75,28 +82,37 @@ impl ProtocolResult {
}
}

#[cfg(feature = "protocol")]
#[no_mangle]
#[allow(unused_variables)]
pub unsafe extern "C" fn protocol_result_free(res: ProtocolResult) {}

#[cfg(feature = "protocol")]
#[no_mangle]
pub unsafe extern "C" fn protocol_keygen(proto_id: ProtocolId) -> ProtocolResult {
let ctx: Box<dyn protocol::Protocol> = match proto_id {
#[cfg(feature = "gg18")]
ProtocolId::Gg18 => Box::new(gg18::KeygenContext::new()),
#[cfg(feature = "elgamal")]
ProtocolId::Elgamal => Box::new(elgamal::KeygenContext::new()),
#[cfg(feature = "frost")]
ProtocolId::Frost => Box::new(frost::KeygenContext::new()),
#[cfg(not(all(feature = "gg18", feature = "elgamal", feature = "frost")))]
_ => panic!("Protocol not supported"),
};
let ctx_ser = serde_json::to_vec(&ctx).unwrap();
ProtocolResult::new(ctx_ser, vec![])
}

#[cfg(feature = "protocol")]
fn advance(ctx1_ser: &[u8], data_in: &[u8]) -> protocol::Result<(Vec<u8>, Vec<u8>)> {
let mut ctx1: Box<dyn protocol::Protocol> = serde_json::from_slice(ctx1_ser).unwrap();
let data_out = ctx1.advance(data_in)?;
let ctx2_ser = serde_json::to_vec(&ctx1).unwrap();
Ok((ctx2_ser, data_out))
}

#[cfg(feature = "protocol")]
#[no_mangle]
pub unsafe extern "C" fn protocol_advance(
ctx_ptr: *const u8,
Expand All @@ -117,12 +133,14 @@ pub unsafe extern "C" fn protocol_advance(
}
}

#[cfg(feature = "protocol")]
fn finish(ctx_ser: &[u8]) -> protocol::Result<(Vec<u8>, Vec<u8>)> {
let ctx: Box<dyn protocol::Protocol> = serde_json::from_slice(ctx_ser).unwrap();
let data_out = ctx.finish()?;
Ok((vec![], data_out))
}

#[cfg(feature = "protocol")]
#[no_mangle]
pub unsafe extern "C" fn protocol_finish(
ctx_ptr: *const u8,
Expand All @@ -140,6 +158,7 @@ pub unsafe extern "C" fn protocol_finish(
}
}

#[cfg(feature = "protocol")]
#[no_mangle]
pub unsafe extern "C" fn protocol_init(
proto_id: ProtocolId,
Expand All @@ -149,9 +168,14 @@ pub unsafe extern "C" fn protocol_init(
let group_ser = unsafe { slice::from_raw_parts(group_ptr, group_len) };

let ctx: Box<dyn protocol::Protocol> = match proto_id {
#[cfg(feature = "gg18")]
ProtocolId::Gg18 => Box::new(gg18::SignContext::new(group_ser)),
#[cfg(feature = "elgamal")]
ProtocolId::Elgamal => Box::new(elgamal::DecryptContext::new(group_ser)),
#[cfg(feature = "frost")]
ProtocolId::Frost => Box::new(frost::SignContext::new(group_ser)),
#[cfg(not(all(feature = "gg18", feature = "elgamal", feature = "frost")))]
_ => panic!("Protocol not supported"),
};
let ctx_ser = serde_json::to_vec(&ctx).unwrap();

Expand Down Expand Up @@ -209,6 +233,7 @@ pub unsafe extern "C" fn auth_cert_key_to_pkcs12(
}
}

#[cfg(feature = "elgamal")]
#[no_mangle]
pub unsafe extern "C" fn encrypt(
msg_ptr: *const u8,
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
mod auth;
pub mod c_api;
#[cfg(feature = "protocol")]
pub mod protocol;

pub mod proto {
pub use prost::Message;
include!(concat!(env!("OUT_DIR"), "/meesign.rs"));
}
3 changes: 3 additions & 0 deletions src/protocol/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#[cfg(feature = "elgamal")]
pub mod elgamal;
#[cfg(feature = "frost")]
pub mod frost;
#[cfg(feature = "gg18")]
pub mod gg18;

pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
Expand Down

0 comments on commit 6a6fbf7

Please sign in to comment.