// // Created by miti on 21/07/19. // #include "protobufSgxTransformsHelper.h" namespace protobufSgxTransformsHelper { namespace { int fit_32_into_uint16_t(google::protobuf::uint32 temp32, uint16_t* temp16) { if(temp32 > UINT16_MAX) return -1; *temp16 = (uint16_t)temp32; return 0; } } int fit_32_into_uint8_t(google::protobuf::uint32 temp32, uint8_t* temp8) { if(temp32 > UINT8_MAX) return -1; *temp8 = (uint8_t)temp32; return 0; } void encode_ec256_public_key_to_protobuf(protobuf_sgx_ec256_public_t* protobuf_g_a , sgx_ec256_public_t* g_a) { int counter; google::protobuf::uint32 temp32; for(counter=0;countergx[counter]; protobuf_g_a->add_gx(temp32); temp32 = g_a->gy[counter]; protobuf_g_a->add_gy(temp32); } } int decode_ec256_public_key_from_protobuf(protobuf_sgx_ec256_public_t* protobuf_g_a , sgx_ec256_public_t* g_a) { printf("\n ec256 pub key\n"); int counter; google::protobuf::uint32 temp32; for(counter=0;countergx(counter); if(fit_32_into_uint8_t(temp32, &(g_a->gx[counter]))!=0) return -1; printf("%02x ",g_a->gx[counter]); temp32 = protobuf_g_a->gy(counter); if(fit_32_into_uint8_t(temp32, &(g_a->gy[counter]))!=0) return -1; printf("%02x ",g_a->gy[counter]); } return 0; } void encode_attributes_to_protobuf(protobuf_sgx_attributes_t* protobuf_attributes, sgx_attributes_t* attributes) { protobuf_attributes->set_flags(attributes->flags); // 64 bit protobuf_attributes->set_xfrm(attributes->xfrm); // 64 bit } int decode_attributes_from_protobuf(protobuf_sgx_attributes_t* protobuf_attributes, sgx_attributes_t* attributes) { attributes->flags = protobuf_attributes->flags(); printf("\n flags %" PRIu64 " \n", attributes->flags); attributes->xfrm = protobuf_attributes->xfrm(); printf("\n xfrm %" PRIu64 " \n", attributes->xfrm); return 0; } void encode_report_to_protobuf(protobuf_sgx_report_t* protobuf_report, sgx_report_t* report) { printf("message 3 ------------------------------------------------------------ \n"); fflush(stdout); int counter; google::protobuf::uint32 temp32; printf("Key ID\n"); for(counter=0;counterkey_id.id[counter]; printf("%02x ", temp32); protobuf_report->add_key_id(temp32); } printf("MAC\n"); for(counter=0;countermac[counter]; printf("%02x ", temp32); protobuf_report->add_mac(temp32); } protobuf_report->mutable_body()->set_misc_select(report->body.misc_select); // 32 bit protobuf_report->mutable_body()->set_isv_svn(report->body.isv_svn); // 16 bit protobuf_report->mutable_body()->set_isv_prod_id(report->body.isv_prod_id); // 16 bit encode_attributes_to_protobuf(protobuf_report->mutable_body()->mutable_attributes(), &(report->body.attributes)); for(counter=0;counterbody.cpu_svn.svn[counter]; protobuf_report->mutable_body()->add_cpu_svn(temp32); } for(counter=0;counterbody.reserved1[counter]; // TODO: Could be optimized out - if these are determined to be 0s. protobuf_report->mutable_body()->add_reserved1(temp32); } for(counter=0;counterbody.reserved2[counter]; // TODO: Could be optimized out - if these are determined to be 0s. protobuf_report->mutable_body()->add_reserved2(temp32); } for(counter=0;counterbody.reserved3[counter]; // TODO: Could be optimized out - if these are determined to be 0s. protobuf_report->mutable_body()->add_reserved3(temp32); } for(counter=0;counterbody.reserved4[counter]; // TODO: Could be optimized out - if these are determined to be 0s. protobuf_report->mutable_body()->add_reserved4(temp32); } printf("\n enclave measurement\n"); for(counter=0;counterbody.mr_enclave.m[counter]; printf("%02x ", temp32); protobuf_report->mutable_body()->add_mr_enclave(temp32); } printf("\n signer measurement\n"); for(counter=0;counterbody.mr_signer.m[counter]; printf("%02x ", temp32); protobuf_report->mutable_body()->add_mr_signer(temp32); } printf("\n report data\n"); for(counter=0;counterbody.report_data.d[counter]; printf("%02x ", temp32); protobuf_report->mutable_body()->add_report_data(temp32); } } int decode_report_from_protobuf(protobuf_sgx_report_t* protobuf_report, sgx_report_t* report) { int counter; google::protobuf::uint32 temp32; printf("\n decoding report body:\n keyid\n"); for(counter=0;counterkey_id(counter); if(fit_32_into_uint8_t(temp32, &(report->key_id.id[counter]))!=0) return -1; printf("%02x ", report->key_id.id[counter]); } printf("\nreport mac\n"); for(counter=0;countermac(counter); if(fit_32_into_uint8_t(temp32, &(report->mac[counter]))!=0) return -1; printf("%02x ", report->mac[counter]); } report->body.misc_select=protobuf_report->mutable_body()->misc_select(); // 32 bit temp32=protobuf_report->mutable_body()->isv_svn(); if(fit_32_into_uint16_t(temp32, &(report->body.isv_svn))!=0) return -1; printf("\nmisc select %02x \n", report->body.misc_select); temp32=protobuf_report->mutable_body()->isv_prod_id(); if(fit_32_into_uint16_t(temp32, &(report->body.isv_prod_id))!=0) return -1; printf("\nprod id %02x \n", report->body.isv_prod_id); decode_attributes_from_protobuf(protobuf_report->mutable_body()->mutable_attributes(), &(report->body.attributes)); printf("\n cpu svn\n"); for(counter=0;countermutable_body()->cpu_svn(counter); if(fit_32_into_uint8_t(temp32, &(report->body.cpu_svn.svn[counter]))!=0) return -1; printf("%02x ", report->body.cpu_svn.svn[counter]); } /* printf("\n reserved1 \n"); for(counter=0;countermutable_body()->reserved1(counter); if(fit_32_into_uint8_t(temp32, &(report->body.reserved1[counter]))!=0) return -1; printf("%02x ", report->body.reserved1[counter]); } printf("\n reserved2 \n"); for(counter=0;countermutable_body()->reserved2(counter); if(fit_32_into_uint8_t(temp32, &(report->body.reserved2[counter]))!=0) return -1; printf("%02x ", report->body.reserved2[counter]); } printf("\n reserved3 \n"); for(counter=0;countermutable_body()->reserved3(counter); if(fit_32_into_uint8_t(temp32, &(report->body.reserved3[counter]))!=0) return -1; printf("%02x ", report->body.reserved3[counter]); } printf("\n reserved4 \n"); for(counter=0;countermutable_body()->reserved4(counter); if(fit_32_into_uint8_t(temp32, &(report->body.reserved4[counter]))!=0) return -1; printf("%02x ", report->body.reserved4[counter]); } */ printf("\n mrenclave \n"); for(counter=0;countermutable_body()->mr_enclave(counter); if(fit_32_into_uint8_t(temp32, &(report->body.mr_enclave.m[counter]))!=0) return -1; printf("%02x ", report->body.mr_enclave.m[counter]); } printf("\n mrsigner \n"); for(counter=0;countermutable_body()->mr_signer(counter); if(fit_32_into_uint8_t(temp32, &(report->body.mr_signer.m[counter]))!=0) return -1; printf("%02x ", report->body.mr_signer.m[counter]); } printf("\n report data\n"); for(counter=0;countermutable_body()->report_data(counter); if(fit_32_into_uint8_t(temp32, &(report->body.report_data.d[counter]))!=0) return -1; printf("%02x ", report->body.report_data.d[counter]); } return 0; } }