uint32_t load_enclaves() { uint32_t enclave_temp_no; int ret, launch_token_updated; sgx_launch_token_t launch_token; enclave_temp_no = 0; ret = sgx_create_enclave(ENCLAVE1_PATH, SGX_DEBUG_FLAG, &launch_token, &launch_token_updated, &e1_enclave_id, NULL); if (ret != SGX_SUCCESS) { return ret; } enclave_temp_no++; g_enclave_id_map.insert(std::pair<sgx_enclave_id_t, uint32_t>(e1_enclave_id, enclave_temp_no)); ret = sgx_create_enclave(ENCLAVE2_PATH, SGX_DEBUG_FLAG, &launch_token, &launch_token_updated, &e2_enclave_id, NULL); if (ret != SGX_SUCCESS) { return ret; } enclave_temp_no++; g_enclave_id_map.insert(std::pair<sgx_enclave_id_t, uint32_t>(e2_enclave_id, enclave_temp_no)); return SGX_SUCCESS; }
/* Very simple for now: debug mode, no launch token supported */ sgx_status_t launch_enclave(const LPCWSTR enclave_dll_file, /*out*/ sgx_enclave_id_t* eid) { sgx_launch_token_t token = {0}; int updated = 0; // Launch the enclave // Token is not stored for now (would speed up subsequent launches) return check(sgx_create_enclave(enclave_dll_file, SGX_DEBUG_FLAG, &token, &updated, eid, NULL), "Failed to launch enclave"); }
void init_Enclave(){ wchar_t* myUString=L"CryptoEnclave.signed.dll"; sgx_status_t ret = SGX_SUCCESS; sgx_launch_token_t token = {0}; int updated = 0; ret = sgx_create_enclave(myUString, SGX_DEBUG_FLAG, &token, &updated, &global_eid, NULL); if (ret != SGX_SUCCESS) { printf("App: error %#x, failed to create enclave.\n", ret); } else printf("enclave creation was successful.\n", ret); }
/* Initialize the enclave: * Call sgx_create_enclave to initialize an enclave instance */ int initialize_enclave(void) { sgx_status_t ret = SGX_ERROR_UNEXPECTED; /* Call sgx_create_enclave to initialize an enclave instance */ /* Debug Support: set 2nd parameter to 1 */ ret = sgx_create_enclave(ENCLAVE_FILENAME, SGX_DEBUG_FLAG, NULL, NULL, &global_eid, NULL); if (ret != SGX_SUCCESS) { print_error_message(ret); return -1; } return 0; }
/* Initialize the enclave: * Step 1: try to retrieve the launch token saved by last transaction * Step 2: call sgx_create_enclave to initialize an enclave instance * Step 3: save the launch token if it is updated */ int initialize_enclave(void) { char token_path[MAX_PATH] = {'\0'}; sgx_launch_token_t token = {0}; sgx_status_t ret = SGX_ERROR_UNEXPECTED; int updated = 0; /* Step 1: try to retrieve the launch token saved by last transaction * if there is no token, then create a new one. */ /* try to get the token saved in $HOME */ const char *home_dir = getpwuid(getuid())->pw_dir; if (home_dir != NULL && (strlen(home_dir)+strlen("/")+sizeof(TOKEN_FILENAME)+1) <= MAX_PATH) { /* compose the token path */ strncpy(token_path, home_dir, strlen(home_dir)); strncat(token_path, "/", strlen("/")); strncat(token_path, TOKEN_FILENAME, sizeof(TOKEN_FILENAME)+1); } else { /* if token path is too long or $HOME is NULL */ strncpy(token_path, TOKEN_FILENAME, sizeof(TOKEN_FILENAME)); } FILE *fp = fopen(token_path, "rb"); if (fp == NULL && (fp = fopen(token_path, "wb")) == NULL) { printf("Warning: Failed to create/open the launch token file \"%s\".\n", token_path); } if (fp != NULL) { /* read the token from saved file */ size_t read_num = fread(token, 1, sizeof(sgx_launch_token_t), fp); if (read_num != 0 && read_num != sizeof(sgx_launch_token_t)) { /* if token is invalid, clear the buffer */ memset(&token, 0x0, sizeof(sgx_launch_token_t)); printf("Warning: Invalid launch token read from \"%s\".\n", token_path); } } /* Step 2: call sgx_create_enclave to initialize an enclave instance */ /* Debug Support: set 2nd parameter to 1 */ ret = sgx_create_enclave(ENCLAVE_FILENAME, SGX_DEBUG_FLAG, &token, &updated, &global_eid, NULL); if (ret != SGX_SUCCESS) { print_error_message(ret); if (fp != NULL) fclose(fp); return -1; } printf("[+] global_eid: %ld\n", global_eid); /* Step 3: save the launch token if it is updated */ if (updated == FALSE || fp == NULL) { /* if the token is not updated, or file handler is invalid, do not perform saving */ if (fp != NULL) fclose(fp); return 0; } /* reopen the file with write capablity */ fp = freopen(token_path, "wb", fp); if (fp == NULL) return 0; size_t write_num = fwrite(token, 1, sizeof(sgx_launch_token_t), fp); if (write_num != sizeof(sgx_launch_token_t)) printf("Warning: Failed to save launch token to \"%s\".\n", token_path); fclose(fp); return 0; }
/* Initialize the enclave: * Call sgx_create_enclave to initialize an enclave instance */ sgx_status_t initialize_enclave ( const char *file_name, sgx_enclave_id_t* eid ) { sgx_status_t ret = SGX_ERROR_UNEXPECTED; size_t read_num = 0; /* Call sgx_create_enclave to initialize an enclave instance */ /* Debug Support: set 2nd parameter to 1 */ #ifdef SGX_USE_PCL bool open_seal_enclave = true; uint8_t* sealed_blob = NULL; FILE *fsealp = fopen(SEALED_KEY_FILE_NAME, "rb"); size_t sealed_blob_size = 0; if(NULL != fsealp) { // Read file size: fseek(fsealp, 0L, SEEK_END); sealed_blob_size = ftell(fsealp); fseek(fsealp, 0L, SEEK_SET); // Read file into buffer: sealed_blob = new uint8_t[sealed_blob_size]; read_num = fread(sealed_blob, 1, sealed_blob_size, fsealp); if ( read_num != sealed_blob_size ) { printf ( "Warning: Failed to read sealed blob.\n" ); } else { open_seal_enclave = false; } fclose(fsealp); } if (true == open_seal_enclave) { printf ("Open Seal Enclave: %s\n", SEAL_FILENAME ); sgx_enclave_id_t seal_eid = 0; ret = sgx_create_enclave( SEAL_FILENAME, SGX_DEBUG_FLAG, NULL, NULL, &seal_eid, NULL); if (SGX_SUCCESS != ret) { print_error_message(ret); return ret; } ret = ecall_get_sealed_blob_size(seal_eid, &sealed_blob_size); if (ret != SGX_SUCCESS || UINT32_MAX == sealed_blob_size) { printf("ecall_get_sealed_blob_size: ret = %d, sealed_blob_size = %ld\n", ret, sealed_blob_size); sgx_destroy_enclave(seal_eid); return ret; } //printf("ecall_get_sealed_blob_size: ret = %d, sealed_blob_size = %ld\n", ret, sealed_blob_size); sealed_blob = new uint8_t[sealed_blob_size]; sgx_status_t gret = SGX_ERROR_UNEXPECTED; ret = ecall_generate_sealed_blob(seal_eid, &gret, sealed_blob, sealed_blob_size); if ((SGX_SUCCESS != ret) || (SGX_SUCCESS != gret)) { printf("ecall_generate_sealed_blob: ret = %d, gret = 0x%x\n", ret, gret); sgx_destroy_enclave(seal_eid); delete sealed_blob; return ret; } sgx_destroy_enclave(seal_eid); fsealp = fopen(SEALED_KEY_FILE_NAME, "wb"); if(NULL != fsealp) { fwrite(sealed_blob, 1, sealed_blob_size, fsealp); fclose(fsealp); } } // Load the PCL protected Enclave: ret = sgx_create_encrypted_enclave(file_name, SGX_DEBUG_FLAG, NULL, NULL, eid, NULL, sealed_blob); delete sealed_blob; #else // SGX_USE_PCL ret = sgx_create_enclave(file_name, SGX_DEBUG_FLAG, NULL, NULL, eid, NULL); #endif // SGX_USE_PCL if (ret != SGX_SUCCESS) { print_error_message(ret); return ret; } return SGX_SUCCESS; }