Skip to content

Instantly share code, notes, and snippets.

@sangfansh
Created August 10, 2018 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sangfansh/9859ce170fce4fa63deba999d6d13326 to your computer and use it in GitHub Desktop.
Save sangfansh/9859ce170fce4fa63deba999d6d13326 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <iostream>
#include "Enclave_u.h"
#include "sgx_urts.h"
#include "sgx_utils/sgx_utils.h"
/* Global EID shared by multiple threads */
sgx_enclave_id_t global_eid = 0;
// OCall implementations
void ocall_print(const char* str) {
printf("%s\n", str);
}
int main(int argc, char const *argv[]) {
if (initialize_enclave(&global_eid, "enclave.token", "enclave.signed.so") < 0) {
std::cout << "Fail to initialize enclave." << std::endl;
return 1;
}
int ptr;
sgx_status_t status = generate_random_number(global_eid, &ptr);
std::cout << status << std::endl;
if (status != SGX_SUCCESS) {
std::cout << "noob" << std::endl;
}
printf("Random number: %d\n", ptr);
// Seal the random number
size_t sealed_size = sizeof(sgx_sealed_data_t) + sizeof(ptr);
uint8_t* sealed_data = (uint8_t*)malloc(sealed_size);
sgx_status_t ecall_status;
status = seal(global_eid, &ecall_status,
(uint8_t*)&ptr, sizeof(ptr),
(sgx_sealed_data_t*)sealed_data, sealed_size);
if (!is_ecall_successful(status, "Sealing failed :(", ecall_status)) {
return 1;
}
int unsealed;
status = unseal(global_eid, &ecall_status,
(sgx_sealed_data_t*)sealed_data, sealed_size,
(uint8_t*)&unsealed, sizeof(unsealed));
if (!is_ecall_successful(status, "Unsealing failed :(", ecall_status)) {
return 1;
}
std::cout << "Seal round trip success! Receive back " << unsealed << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment