Skip to content

Commit a7d86ac

Browse files
committed
add sl solution
1 parent e412c4c commit a7d86ac

File tree

32 files changed

+6466
-2
lines changed

32 files changed

+6466
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
*.lock
88
*.iml
99
*.tar.gz
10+
*.h

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
members = [
44
"crypto",
55
"solution/verifiable_confidential_ledger",
6+
"solution/selective_disclosure",
67
"common/utils",
78
"common/macros",
89
"ffi/ffi_common",
@@ -11,5 +12,7 @@ members = [
1112
"ffi/ffi_java/ffi_java_vcl",
1213
"ffi/ffi_c/ffi_c_crypto",
1314
"ffi/ffi_java/ffi_java_crypto",
15+
"ffi/ffi_java/ffi_java_selective_disclosure",
16+
"ffi/ffi_c/ffi_c_selective_disclosure",
1417
"protos",
1518
]

common/utils/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ base64 = "0.10.1"
1111
failure = "0.1"
1212
hex = "0.3.0"
1313
wedpr_macros = { path = "../macros/"}
14+

common/utils/src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ pub enum WedprError {
1212
FormatError,
1313
#[fail(display = "Data cannot be decoded")]
1414
DecodeError,
15+
#[fail(display = "Indy Crypto error.")]
16+
IndyCryptoError,
1517
}

crypto/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ sha3 = "0.8"
1818
wedpr_macros = { path = "../common/macros/"}
1919
wedpr_protos = { path = "../protos/" }
2020
wedpr_utils = { path = "../common/utils" }
21+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[package]
2+
name = "ffi_c_selective_disclosure"
3+
version = "0.1.0"
4+
authors = ["HaoXuan40404 <[email protected]>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[lib]
10+
name = "ffi_c_selective_disclosure"
11+
crate-type = ["cdylib", "staticlib"]
12+
13+
[dependencies]
14+
libc = "0.2.60"
15+
wedpr_crypto = { path = "../../../crypto/" }
16+
selective_disclosure = { path = "../../../solution/selective_disclosure" }
17+
wedpr_ffi_c_common = { path = "../ffi_c_common/" }
18+
wedpr_ffi_common = { path = "../../ffi_common/" }
19+
wedpr_ffi_macros = { path = "../../../ffi/ffi_macros/" }
20+
wedpr_macros = { path = "../../../common/macros/" }
21+
wedpr_utils = { path = "../../../common/utils" }
22+
wedpr_protos = { path = "../../../protos/" }
23+
protobuf = "2.10.1"
24+
25+
# This is required to generate C/C++ header files.
26+
[build-dependencies]
27+
cbindgen = "0.9.0"
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0.
2+
3+
//! Library of macros and functions for FFI of selective_disclosure solution,
4+
//! targeting C/C++ compatible architectures (including iOS).
5+
6+
// C/C++ FFI: C-style interfaces will be generated.
7+
8+
use wedpr_protos::generated::selective_disclosure::{
9+
AttributeTemplate, CredentialSignatureRequest, CredentialTemplate,
10+
SelectiveDisclosureResult, TemplateSecretKey,
11+
};
12+
13+
use wedpr_crypto::utils::{bytes_to_string, string_to_bytes};
14+
15+
use libc::c_char;
16+
use protobuf::{self, Message};
17+
use std::{ffi::CString, panic, ptr};
18+
use wedpr_ffi_common::utils::c_char_pointer_to_string;
19+
20+
#[no_mangle]
21+
/// C interface for 'wedpr_make_credential_template'.
22+
pub extern "C" fn wedpr_make_credential_template(
23+
attribute_template_cstring: *mut c_char,
24+
) -> *mut c_char {
25+
let result = panic::catch_unwind(|| {
26+
let attribute_template_pb = c_safe_c_char_pointer_to_proto!(
27+
attribute_template_cstring,
28+
AttributeTemplate
29+
);
30+
let (credential_template, template_secret_key) =
31+
match selective_disclosure::issuer::make_credential_template(
32+
&attribute_template_pb,
33+
) {
34+
Ok(v) => v,
35+
Err(_) => return ptr::null_mut(),
36+
};
37+
38+
let mut sl_result = SelectiveDisclosureResult::new();
39+
sl_result.set_credential_template(credential_template);
40+
sl_result.set_template_secret_key(template_secret_key);
41+
c_safe_proto_to_c_char_pointer!(sl_result)
42+
});
43+
c_safe_return!(result)
44+
}
45+
46+
#[no_mangle]
47+
/// C interface for 'wedpr_sign_credential'.
48+
pub extern "C" fn wedpr_sign_credential(
49+
credential_template_cstring: *mut c_char,
50+
template_secret_key_cstring: *mut c_char,
51+
credential_request_cstring: *mut c_char,
52+
user_id_cstring: *mut c_char,
53+
nonce_cstring: *mut c_char,
54+
) -> *mut c_char
55+
{
56+
let result = panic::catch_unwind(|| {
57+
let credential_template_pb = c_safe_c_char_pointer_to_proto!(
58+
credential_template_cstring,
59+
CredentialTemplate
60+
);
61+
let template_secret_key_pb = c_safe_c_char_pointer_to_proto!(
62+
template_secret_key_cstring,
63+
TemplateSecretKey
64+
);
65+
let credential_request_pb = c_safe_c_char_pointer_to_proto!(
66+
credential_request_cstring,
67+
CredentialSignatureRequest
68+
);
69+
let user_id = c_safe_c_char_pointer_to_string!(user_id_cstring);
70+
let nonce = c_safe_c_char_pointer_to_string!(nonce_cstring);
71+
let (credential_signature, cred_issuance_nonce_str) =
72+
match selective_disclosure::issuer::sign_credential(
73+
&credential_template_pb,
74+
&template_secret_key_pb,
75+
&credential_request_pb,
76+
&user_id,
77+
&nonce,
78+
) {
79+
Ok(v) => v,
80+
Err(_) => return ptr::null_mut(),
81+
};
82+
83+
let mut sl_result = SelectiveDisclosureResult::new();
84+
sl_result.set_credential_signature(credential_signature);
85+
sl_result.set_nonce(cred_issuance_nonce_str);
86+
c_safe_proto_to_c_char_pointer!(sl_result)
87+
});
88+
c_safe_return!(result)
89+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0.
2+
3+
//! Library of macros and functions for FFI of selective_disclosure solution,
4+
//! targeting C/C++ compatible architectures (including iOS).
5+
6+
// C/C++ FFI: C-style interfaces will be generated.
7+
8+
pub mod issuer;
9+
pub mod user;
10+
pub mod verifier;
11+
12+
#[macro_use]
13+
extern crate wedpr_ffi_macros;
14+
#[macro_use]
15+
extern crate wedpr_macros;
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
// Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0.
2+
3+
//! Library of macros and functions for FFI of selective_disclosure solution,
4+
//! targeting C/C++ compatible architectures (including iOS).
5+
6+
// C/C++ FFI: C-style interfaces will be generated.
7+
8+
use wedpr_protos::generated::selective_disclosure::{
9+
CredentialInfo, CredentialSignature, CredentialTemplate,
10+
SelectiveDisclosureResult, VerificationRule,
11+
};
12+
13+
use wedpr_crypto::utils::{bytes_to_string, string_to_bytes};
14+
15+
use libc::c_char;
16+
use protobuf::{self, Message};
17+
use std::{ffi::CString, panic, ptr};
18+
use wedpr_ffi_common::utils::c_char_pointer_to_string;
19+
20+
#[no_mangle]
21+
/// C interface for 'wedpr_sign_credential'.
22+
pub extern "C" fn wedpr_make_credential(
23+
credential_info_cstring: *mut c_char,
24+
credential_template_cstring: *mut c_char,
25+
) -> *mut c_char
26+
{
27+
let result = panic::catch_unwind(|| {
28+
let credential_info_pb = c_safe_c_char_pointer_to_proto!(
29+
credential_info_cstring,
30+
CredentialInfo
31+
);
32+
let credential_template_pb = c_safe_c_char_pointer_to_proto!(
33+
credential_template_cstring,
34+
CredentialTemplate
35+
);
36+
37+
let (
38+
credential_signature_request,
39+
master_secret_str,
40+
credential_secrets_blinding_factors_str,
41+
nonce_credential_str,
42+
) = match selective_disclosure::user::make_credential(
43+
&credential_info_pb,
44+
&credential_template_pb,
45+
) {
46+
Ok(v) => v,
47+
Err(_) => return ptr::null_mut(),
48+
};
49+
let mut sl_result = SelectiveDisclosureResult::new();
50+
sl_result
51+
.set_credential_signature_request(credential_signature_request);
52+
sl_result.set_master_secret(master_secret_str);
53+
sl_result.set_credential_secrets_blinding_factors(
54+
credential_secrets_blinding_factors_str,
55+
);
56+
sl_result.set_nonce_credential(nonce_credential_str);
57+
c_safe_proto_to_c_char_pointer!(sl_result)
58+
});
59+
c_safe_return!(result)
60+
}
61+
62+
#[no_mangle]
63+
/// C interface for 'wedpr_blind_credential_signature'.
64+
pub extern "C" fn wedpr_blind_credential_signature(
65+
credential_signature_cstring: *mut c_char,
66+
credential_info_cstring: *mut c_char,
67+
credential_template_cstring: *mut c_char,
68+
master_secret_cstring: *mut c_char,
69+
credential_secrets_blinding_factors_cstring: *mut c_char,
70+
nonce_credential_cstring: *mut c_char,
71+
) -> *mut c_char
72+
{
73+
let result = panic::catch_unwind(|| {
74+
let credential_signature_pb = c_safe_c_char_pointer_to_proto!(
75+
credential_signature_cstring,
76+
CredentialSignature
77+
);
78+
let credential_info_pb = c_safe_c_char_pointer_to_proto!(
79+
credential_info_cstring,
80+
CredentialInfo
81+
);
82+
let credential_template_pb = c_safe_c_char_pointer_to_proto!(
83+
credential_template_cstring,
84+
CredentialTemplate
85+
);
86+
let master_secret =
87+
c_safe_c_char_pointer_to_string!(master_secret_cstring);
88+
let credential_secrets_blinding_factors = c_safe_c_char_pointer_to_string!(
89+
credential_secrets_blinding_factors_cstring
90+
);
91+
let nonce_credential =
92+
c_safe_c_char_pointer_to_string!(nonce_credential_cstring);
93+
94+
let new_credential_signature =
95+
match selective_disclosure::user::blind_credential_signature(
96+
&credential_signature_pb,
97+
&credential_info_pb,
98+
&credential_template_pb,
99+
&master_secret,
100+
&credential_secrets_blinding_factors,
101+
&nonce_credential,
102+
) {
103+
Ok(v) => v,
104+
Err(_) => return ptr::null_mut(),
105+
};
106+
107+
let mut sl_result = SelectiveDisclosureResult::new();
108+
sl_result.set_credential_signature(new_credential_signature);
109+
c_safe_proto_to_c_char_pointer!(sl_result)
110+
});
111+
c_safe_return!(result)
112+
}
113+
114+
#[no_mangle]
115+
/// C interface for 'wedpr_prove_credential_info'.
116+
pub extern "C" fn wedpr_prove_credential_info(
117+
verification_predicate_rule_cstring: *mut c_char,
118+
credential_signature_cstring: *mut c_char,
119+
credential_info_cstring: *mut c_char,
120+
credential_template_cstring: *mut c_char,
121+
master_secret_cstring: *mut c_char,
122+
) -> *mut c_char
123+
{
124+
let result = panic::catch_unwind(|| {
125+
let credential_signature_pb = c_safe_c_char_pointer_to_proto!(
126+
credential_signature_cstring,
127+
CredentialSignature
128+
);
129+
let credential_info_pb = c_safe_c_char_pointer_to_proto!(
130+
credential_info_cstring,
131+
CredentialInfo
132+
);
133+
let credential_template_pb = c_safe_c_char_pointer_to_proto!(
134+
credential_template_cstring,
135+
CredentialTemplate
136+
);
137+
let verification_predicate_rule_pb = c_safe_c_char_pointer_to_proto!(
138+
verification_predicate_rule_cstring,
139+
VerificationRule
140+
);
141+
let master_secret =
142+
c_safe_c_char_pointer_to_string!(master_secret_cstring);
143+
144+
let request =
145+
match selective_disclosure::user::prove_selected_credential_info(
146+
&verification_predicate_rule_pb,
147+
&credential_signature_pb,
148+
&credential_info_pb,
149+
&credential_template_pb,
150+
&master_secret,
151+
) {
152+
Ok(v) => v,
153+
Err(_) => return ptr::null_mut(),
154+
};
155+
156+
let mut sl_result = SelectiveDisclosureResult::new();
157+
sl_result.set_verification_request(request);
158+
c_safe_proto_to_c_char_pointer!(sl_result)
159+
});
160+
c_safe_return!(result)
161+
}

0 commit comments

Comments
 (0)