This is an automated email from the ASF dual-hosted git repository. mssun pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-teaclave.git
commit 2a5a7c176574f6b4821a447c49df201f65fa6135 Author: Mengyuan-L <[email protected]> AuthorDate: Thu Feb 18 21:17:02 2021 +0000 Update cmac format from string to bytes array. --- examples/c/builtin_echo.c | 9 +++++- examples/c/builtin_ordered_set_intersect.c | 37 ++++++++++++++++++---- examples/python/builtin_gbdt_train.py | 5 ++- examples/python/builtin_ordered_set_intersect.py | 16 ++++++---- examples/python/builtin_password_check.py | 13 +++++--- .../python/builtin_private_join_and_compute.py | 23 ++++++++------ examples/python/builtin_rsa_sign.py | 5 ++- examples/python/mesapy_logistic_reg.py | 16 ++++++---- sdk/python/teaclave.py | 4 +-- services/proto/src/proto/teaclave_common.proto | 2 +- .../src/proto/teaclave_frontend_service.proto | 6 ++-- services/proto/src/teaclave_frontend_service.rs | 12 +++---- types/src/crypto.rs | 4 +++ types/src/task.rs | 12 +++---- 14 files changed, 108 insertions(+), 56 deletions(-) diff --git a/examples/c/builtin_echo.c b/examples/c/builtin_echo.c index e8d2e35..37be293 100644 --- a/examples/c/builtin_echo.c +++ b/examples/c/builtin_echo.c @@ -181,11 +181,18 @@ int main() { printf("[+] Task result in string: %s\n", task_result); -bail: ret = teaclave_close_frontend_service(frontend_client); if (ret != 0) { fprintf(stderr, "[-] Failed to close the frontend service client.\n"); } return ret; + +bail: + ret = teaclave_close_frontend_service(frontend_client); + if (ret != 0) { + fprintf(stderr, "[-] Failed to close the frontend service client.\n"); + } + + exit(-1); } diff --git a/examples/c/builtin_ordered_set_intersect.c b/examples/c/builtin_ordered_set_intersect.c index 285f000..582b78a 100644 --- a/examples/c/builtin_ordered_set_intersect.c +++ b/examples/c/builtin_ordered_set_intersect.c @@ -39,7 +39,7 @@ typedef struct UserData { char *password; char *input_url; char *output_url; - char *input_cmac; + char input_cmac[16]; char key[16]; } UserData; @@ -48,7 +48,7 @@ struct UserData user0_data = { .password = "password", .input_url = "http://localhost:6789/fixtures/functions/ordered_set_intersect/psi0.txt.enc", .output_url = "http://localhost:6789/fixtures/functions/ordered_set_intersect/output_psi0.enc", - .input_cmac = "e08adeb021e876ffe82234445e632121", + .input_cmac = {0xe0, 0x8a, 0xde, 0xb0, 0x21, 0xe8, 0x76, 0xff, 0xe8, 0x22, 0x34, 0x44, 0x5e, 0x63, 0x21, 0x21}, .key = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}; struct UserData user1_data = { @@ -56,7 +56,7 @@ struct UserData user1_data = { .password = "password", .input_url = "http://localhost:6789/fixtures/functions/ordered_set_intersect/psi1.txt.enc", .output_url = "http://localhost:6789/fixtures/functions/ordered_set_intersect/output_psi1.enc", - .input_cmac = "538dafbf7802d962bb01e2389b4e943a", + .input_cmac = {0x53, 0x8d, 0xaf, 0xbf, 0x78, 0x02, 0xd9, 0x62, 0xbb, 0x01, 0xe2, 0x38, 0x9b, 0x4e, 0x94, 0x3a}, .key = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}; const char *register_function_request_serialized = QUOTE( @@ -98,7 +98,7 @@ const char *register_input_serialized= QUOTE( { "request": "register_input_file", "url": "%s", - "cmac": "%s", + "cmac": %s, "crypto_info": { "schema": "teaclave-file-128", "key": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], @@ -236,6 +236,17 @@ int set_task(struct FrontendClient *frontend_client, char *serialized_response, return ret; } +char *format_cmac_to_string(UserData user_data){ + static char cmac[128] = {0}; + int n = 0; + n += sprintf (&cmac[n], "[%hhu", user_data.input_cmac[0]); + for (int i = 1; i < 16; i++) { + n += sprintf (&cmac[n], ",%hhu", user_data.input_cmac[i]); + } + sprintf (&cmac[n], "]"); + return cmac; +} + int main() { int ret = 0; @@ -280,7 +291,7 @@ int main() /* User0 register input data. */ printf("[+] %s register input data\n", user0_data.user_id); snprintf(user0_serialized_input_request, BUFFER_SIZE, register_input_serialized, user0_data.input_url, - user0_data.input_cmac); + format_cmac_to_string(user0_data)); memset(serialized_response, 0, BUFFER_SIZE); serialized_response_len = BUFFER_SIZE; ret = teaclave_register_input_file_serialized(client0, user0_serialized_input_request, serialized_response, @@ -320,7 +331,7 @@ int main() /* User1 register input data. */ printf("[+] %s register input data\n", user1_data.user_id); snprintf(user1_serialized_input_request, BUFFER_SIZE, register_input_serialized, user1_data.input_url, - user1_data.input_cmac); + format_cmac_to_string(user1_data)); memset(serialized_response, 0, BUFFER_SIZE); serialized_response_len = BUFFER_SIZE; ret = teaclave_register_input_file_serialized(client1, user1_serialized_input_request, serialized_response, @@ -409,7 +420,6 @@ int main() } printf("[+] %s task result in string: %s\n", user1_data.user_id, user1_task_result); -bail: printf("close client - 0\n"); ret = teaclave_close_frontend_service(client0); if (ret != 0) { @@ -421,4 +431,17 @@ bail: fprintf(stderr, "[-] Failed to close the frontend service client.\n"); } return ret; + +bail: + printf("close client - 0\n"); + ret = teaclave_close_frontend_service(client0); + if (ret != 0) { + fprintf(stderr, "[-] Failed to close the frontend service client.\n"); + } + printf("close client - 1\n"); + ret = teaclave_close_frontend_service(client1); + if (ret != 0) { + fprintf(stderr, "[-] Failed to close the frontend service client.\n"); + } + exit(-1); } diff --git a/examples/python/builtin_gbdt_train.py b/examples/python/builtin_gbdt_train.py index 8db51ed..f3910aa 100644 --- a/examples/python/builtin_gbdt_train.py +++ b/examples/python/builtin_gbdt_train.py @@ -66,7 +66,10 @@ class BuiltinGbdtExample: print("[+] registering input file") url = "http://localhost:6789/fixtures/functions/gbdt_training/train.enc" - cmac = "881adca6b0524472da0a9d0bb02b9af9" + cmac = [ + 0x88, 0x1a, 0xdc, 0xa6, 0xb0, 0x52, 0x44, 0x72, 0xda, 0x0a, 0x9d, + 0x0b, 0xb0, 0x2b, 0x9a, 0xf9 + ] schema = "teaclave-file-128" key = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] iv = [] diff --git a/examples/python/builtin_ordered_set_intersect.py b/examples/python/builtin_ordered_set_intersect.py index 9cd9e9d..c32b9af 100644 --- a/examples/python/builtin_ordered_set_intersect.py +++ b/examples/python/builtin_ordered_set_intersect.py @@ -36,7 +36,7 @@ class UserData: password, input_url="", output_url="", - input_cmac="", + input_cmac=[], key=[]): self.user_id = user_id self.password = password @@ -51,15 +51,17 @@ OUTPUT_FILE_URL_PREFIX = "http://localhost:6789/fixtures/functions/ordered_set_i USER_DATA_0 = UserData("user0", "password", INPUT_FILE_URL_PREFIX + "psi0.txt.enc", - OUTPUT_FILE_URL_PREFIX + "output_psi0.enc", - "e08adeb021e876ffe82234445e632121", - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) + OUTPUT_FILE_URL_PREFIX + "output_psi0.enc", [ + 0xe0, 0x8a, 0xde, 0xb0, 0x21, 0xe8, 0x76, 0xff, + 0xe8, 0x22, 0x34, 0x44, 0x5e, 0x63, 0x21, 0x21 + ], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) USER_DATA_1 = UserData("user1", "password", INPUT_FILE_URL_PREFIX + "psi1.txt.enc", - OUTPUT_FILE_URL_PREFIX + "output_psi1.enc", - "538dafbf7802d962bb01e2389b4e943a", - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) + OUTPUT_FILE_URL_PREFIX + "output_psi1.enc", [ + 0x53, 0x8d, 0xaf, 0xbf, 0x78, 0x02, 0xd9, 0x62, + 0xbb, 0x01, 0xe2, 0x38, 0x9b, 0x4e, 0x94, 0x3a + ], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) class DataList: diff --git a/examples/python/builtin_password_check.py b/examples/python/builtin_password_check.py index fb87d42..4da8635 100644 --- a/examples/python/builtin_password_check.py +++ b/examples/python/builtin_password_check.py @@ -36,7 +36,7 @@ class UserData: password, input_url="", encryption_algorithm="teaclave-file-128", - input_cmac="", + input_cmac=[], iv=[], key=[]): self.user_id = user_id @@ -56,15 +56,20 @@ USER_DATA_0 = UserData( "password", "data:text/plain;base64,c+mpvRfZ0fboR0j3rTgOGDBiubSzlCt9", # base64 of encrypted string "password" "aes-gcm-128", - "e84748f7ad380e183062b9b4b3942b7d", + [ + 0xe8, 0x47, 0x48, 0xf7, 0xad, 0x38, 0x0e, 0x18, 0x30, 0x62, 0xb9, 0xb4, + 0xb3, 0x94, 0x2b, 0x7d + ], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) # Data provider USER_DATA_1 = UserData("user1", "password", INPUT_FILE_URL_PREFIX + "exposed_passwords.txt.enc", - "teaclave-file-128", "42b16c29edeb9ee0e4d219f3b5395946", - [], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) + "teaclave-file-128", [ + 0x42, 0xb1, 0x6c, 0x29, 0xed, 0xeb, 0x9e, 0xe0, + 0xe4, 0xd2, 0x19, 0xf3, 0xb5, 0x39, 0x59, 0x46 + ], [], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) class DataList: diff --git a/examples/python/builtin_private_join_and_compute.py b/examples/python/builtin_private_join_and_compute.py index 57d31f7..0699be3 100644 --- a/examples/python/builtin_private_join_and_compute.py +++ b/examples/python/builtin_private_join_and_compute.py @@ -36,7 +36,7 @@ class UserData: password, input_url="", output_url="", - input_cmac="", + input_cmac=[], key=[]): self.user_id = user_id self.password = password @@ -51,21 +51,24 @@ OUTPUT_FILE_URL_PREFIX = "http://localhost:6789/fixtures/functions/private_join_ USER_DATA_0 = UserData("user0", "password", INPUT_FILE_URL_PREFIX + "bank_a.enc", - OUTPUT_FILE_URL_PREFIX + "user0_output.enc", - "7884a62894e7be50b9795ba22ce5ee7f", - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) + OUTPUT_FILE_URL_PREFIX + "user0_output.enc", [ + 0x78, 0x84, 0xa6, 0x28, 0x94, 0xe7, 0xbe, 0x50, + 0xb9, 0x79, 0x5b, 0xa2, 0x2c, 0xe5, 0xee, 0x7f + ], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) USER_DATA_1 = UserData("user1", "password", INPUT_FILE_URL_PREFIX + "bank_b.enc", - OUTPUT_FILE_URL_PREFIX + "user1_output.enc", - "75b8e931887bd57564d93df31c282bb9", - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) + OUTPUT_FILE_URL_PREFIX + "user1_output.enc", [ + 0x75, 0xb8, 0xe9, 0x31, 0x88, 0x7b, 0xd5, 0x75, + 0x64, 0xd9, 0x3d, 0xf3, 0x1c, 0x28, 0x2b, 0xb9 + ], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) USER_DATA_2 = UserData("user2", "password", INPUT_FILE_URL_PREFIX + "bank_c.enc", - OUTPUT_FILE_URL_PREFIX + "user2_output.enc", - "35acf29139485067d1ae6212c0577b43", - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) + OUTPUT_FILE_URL_PREFIX + "user2_output.enc", [ + 0x35, 0xac, 0xf2, 0x91, 0x39, 0x48, 0x50, 0x67, + 0xd1, 0xae, 0x62, 0x12, 0xc0, 0x57, 0x7b, 0x43 + ], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) USER_DATA_3 = UserData("user3", "password") diff --git a/examples/python/builtin_rsa_sign.py b/examples/python/builtin_rsa_sign.py index 1761550..1443f3d 100644 --- a/examples/python/builtin_rsa_sign.py +++ b/examples/python/builtin_rsa_sign.py @@ -56,7 +56,10 @@ def register_input_file(client): --print-cmac """ url = "http://localhost:6789/fixtures/functions/rsa_sign/rsakey.enc" - cmac = "4de3bb77327c82923640835c6e5ada66" + cmac = [ + 0x4d, 0xe3, 0xbb, 0x77, 0x32, 0x7c, 0x82, 0x92, 0x36, 0x40, 0x83, 0x5c, + 0x6e, 0x5a, 0xda, 0x66 + ] schema = "teaclave-file-128" key = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3] iv = [] diff --git a/examples/python/mesapy_logistic_reg.py b/examples/python/mesapy_logistic_reg.py index 6a1fcce..1e4423c 100644 --- a/examples/python/mesapy_logistic_reg.py +++ b/examples/python/mesapy_logistic_reg.py @@ -47,7 +47,7 @@ class UserClient: class InputData: def __init__(self, input_url="", - input_cmac="", + input_cmac=[], key=[], label="input_data_0", schema="teaclave-file-128", @@ -205,9 +205,10 @@ USER = UserClient("user0", "password") fo_test = "http://localhost:6789/fixtures/functions/py_logistic_reg/testa.out" train_inputs = [ - InputData(train, - "057baff3598121d05cec6c5e9ce2aa39", - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + InputData(train, [ + 0x05, 0x7b, 0xaf, 0xf3, 0x59, 0x81, 0x21, 0xd0, 0x5c, 0xec, 0x6c, 0x5e, + 0x9c, 0xe2, 0xaa, 0x39 + ], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], label="input_train") ] train_outputs = [ @@ -252,9 +253,10 @@ def main(): output_scaler_cmac = tc.get_output_cmac_by_tag(train_task_id, "output_scaler") predict_inputs = [ - InputData(predict, - "ae168afac768fd53e5d90d8e4b594d27", - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + InputData(predict, [ + 0xae, 0x16, 0x8a, 0xfa, 0xc7, 0x68, 0xfd, 0x53, 0xe5, 0xd9, 0x0d, + 0x8e, 0x4b, 0x59, 0x4d, 0x27 + ], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], label="input_predict"), InputData(params, output_params_cmac, diff --git a/sdk/python/teaclave.py b/sdk/python/teaclave.py index d439b53..9d0d105 100644 --- a/sdk/python/teaclave.py +++ b/sdk/python/teaclave.py @@ -274,7 +274,7 @@ class RegisterFunctionRequest: class RegisterInputFileRequest: - def __init__(self, metadata: Metadata, url: str, cmac: str, + def __init__(self, metadata: Metadata, url: str, cmac: List[int], crypto_info: CryptoInfo): self.request = "register_input_file" self.metadata = metadata @@ -374,7 +374,7 @@ class FrontendClient: return response["content"]["function_id"] def register_input_file(self, url: str, schema: str, key: List[int], - iv: List[int], cmac: str): + iv: List[int], cmac: List[int]): request = RegisterInputFileRequest(self.metadata, url, cmac, CryptoInfo(schema, key, iv)) _write_message(self.channel, request) diff --git a/services/proto/src/proto/teaclave_common.proto b/services/proto/src/proto/teaclave_common.proto index 155deeb..31aa6c1 100644 --- a/services/proto/src/proto/teaclave_common.proto +++ b/services/proto/src/proto/teaclave_common.proto @@ -14,7 +14,7 @@ message FileCryptoInfo { message TaskOutputs { bytes return_value = 1; - map<string, string> tags_map = 2; + map<string, bytes> tags_map = 2; } message TaskFailure { diff --git a/services/proto/src/proto/teaclave_frontend_service.proto b/services/proto/src/proto/teaclave_frontend_service.proto index 0f7e313..b5114cc 100644 --- a/services/proto/src/proto/teaclave_frontend_service.proto +++ b/services/proto/src/proto/teaclave_frontend_service.proto @@ -6,7 +6,7 @@ import "teaclave_common.proto"; message RegisterInputFileRequest { string url = 1; - string cmac = 2; + bytes cmac = 2; teaclave_common_proto.FileCryptoInfo crypto_info = 3; } @@ -63,7 +63,7 @@ message GetOutputFileRequest { message GetOutputFileResponse { repeated string owner = 1; - string cmac = 2; + bytes cmac = 2; } message GetInputFileRequest { @@ -72,7 +72,7 @@ message GetInputFileRequest { message GetInputFileResponse { repeated string owner = 1; - string cmac = 2; + bytes cmac = 2; } message FunctionInput { diff --git a/services/proto/src/teaclave_frontend_service.rs b/services/proto/src/teaclave_frontend_service.rs index 311e397..e929bdf 100644 --- a/services/proto/src/teaclave_frontend_service.rs +++ b/services/proto/src/teaclave_frontend_service.rs @@ -545,7 +545,7 @@ impl std::convert::TryFrom<proto::RegisterInputFileRequest> for RegisterInputFil fn try_from(proto: proto::RegisterInputFileRequest) -> Result<Self> { let url = Url::parse(&proto.url)?; - let cmac = FileAuthTag::from_hex(proto.cmac)?; + let cmac = FileAuthTag::from_bytes(&proto.cmac)?; let crypto_info = proto .crypto_info .ok_or_else(|| anyhow!("missing crypto_info"))? @@ -562,7 +562,7 @@ impl From<RegisterInputFileRequest> for proto::RegisterInputFileRequest { fn from(request: RegisterInputFileRequest) -> Self { Self { url: request.url.into_string(), - cmac: request.cmac.to_hex(), + cmac: request.cmac.to_bytes(), crypto_info: Some(request.crypto_info.into()), } } @@ -805,7 +805,7 @@ impl std::convert::TryFrom<proto::GetInputFileResponse> for GetInputFileResponse fn try_from(proto: proto::GetInputFileResponse) -> Result<Self> { Ok(Self { owner: OwnerList::new(proto.owner), - cmac: FileAuthTag::from_hex(proto.cmac)?, + cmac: FileAuthTag::from_bytes(&proto.cmac)?, }) } } @@ -814,7 +814,7 @@ impl From<GetInputFileResponse> for proto::GetInputFileResponse { fn from(request: GetInputFileResponse) -> Self { Self { owner: request.owner.into(), - cmac: request.cmac.to_hex(), + cmac: request.cmac.to_bytes(), } } } @@ -846,7 +846,7 @@ impl std::convert::TryFrom<proto::GetOutputFileResponse> for GetOutputFileRespon if proto.cmac.is_empty() { None } else { - Some(FileAuthTag::from_hex(&proto.cmac)?) + Some(FileAuthTag::from_bytes(&proto.cmac)?) } }; @@ -861,7 +861,7 @@ impl From<GetOutputFileResponse> for proto::GetOutputFileResponse { fn from(request: GetOutputFileResponse) -> Self { Self { owner: request.owner.into(), - cmac: request.cmac.map_or_else(String::new, |cmac| cmac.to_hex()), + cmac: request.cmac.map_or_else(Vec::new, |cmac| cmac.to_bytes()), } } } diff --git a/types/src/crypto.rs b/types/src/crypto.rs index 8c03586..48d7409 100644 --- a/types/src/crypto.rs +++ b/types/src/crypto.rs @@ -53,6 +53,10 @@ impl FileAuthTag { hex::encode(&self.tag) } + pub fn to_bytes(&self) -> Vec<u8> { + self.tag.to_vec() + } + #[cfg(test_mode)] pub fn mock() -> Self { Self { diff --git a/types/src/task.rs b/types/src/task.rs index 99a6a77..95c26d8 100644 --- a/types/src/task.rs +++ b/types/src/task.rs @@ -165,12 +165,12 @@ impl OutputsTags { } } -impl std::convert::TryFrom<HashMap<String, String>> for OutputsTags { +impl std::convert::TryFrom<HashMap<String, Vec<u8>>> for OutputsTags { type Error = anyhow::Error; - fn try_from(input: HashMap<String, String>) -> Result<Self> { + fn try_from(input: HashMap<String, Vec<u8>>) -> Result<Self> { let mut ret = HashMap::with_capacity(input.len()); for (k, v) in input.iter() { - let tag = FileAuthTag::from_hex(v)?; + let tag = FileAuthTag::from_bytes(&v)?; ret.insert(k.to_string(), tag); } Ok(OutputsTags::new(ret)) @@ -178,11 +178,11 @@ impl std::convert::TryFrom<HashMap<String, String>> for OutputsTags { } impl<S: std::default::Default + std::hash::BuildHasher> std::convert::From<OutputsTags> - for HashMap<String, String, S> + for HashMap<String, Vec<u8>, S> { - fn from(tags: OutputsTags) -> HashMap<String, String, S> { + fn from(tags: OutputsTags) -> HashMap<String, Vec<u8>, S> { tags.iter() - .map(|(k, v)| (k.to_string(), v.to_hex())) + .map(|(k, v)| (k.to_string(), v.to_bytes())) .collect() } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
