Intel® Software Guard Extensions (Intel® SGX)
Discussion board focused on hardware-based isolation and memory encryption to provide extended code protection in solutions.

SGX error unsealing data in Java but working in C++

João_R_
Novice
637 Views

I'm using in Ubuntu intel sgx to seal and unseal data.

I have both implementation in C and Java.

The example is simple:

  • Seal the data
  • Restart the program
  • Unseal the data

This works fine in C++ but when I'm using Java the program gives segmentation fault.

I guess that is related with the Makefile options that I need to setup the library to use in Java, that are the following:

App_SO_Name := libSGXEnclave.so

$(App_SO_Name): App/Enclave_u.o $(App_Cpp_Objects)
	@echo "LINK =>  $@"
	@$(CXX) $^ $(App_Link_Flags) -shared -o $@

The remaining part of the Makefile and other things related with the code can be found in https://github.com/digawp/hello-enclave/

I'm available to give further information regarding this issue.

0 Kudos
2 Replies
Shivananda_H_Intel
637 Views

Enclave code has to be written in c/c++. 

Below link has some example code link  to call those c/c++ interfaces from other native interface capabilities.

https://software.intel.com/en-us/forums/intel-software-guard-extensions-intel-sgx/topic/605347.

If the link for samples  doesn't work, you can directly check here: https://software.intel.com/en-us/sgx/code-samples

Regards

Shivananda

0 Kudos
João_R_
Novice
637 Views

The following example describes the process of unsealing in both examples. Yes I'm using Jni. I comment the place where the program crashes.

Java / JNI example

JNIEXPORT jstring JNICALL Java_Joao_unseal(
  JNIEnv *env, jobject obj, jstring javaString) {
if (initialize_enclave(&global_eid, "enclave.token", "enclave.signed.so") < 0) {
std::cout << "Fail to initialize enclave." << std::endl;
       return NULL;
}
 sgx_status_t status;
    sgx_status_t ecall_status;

  const char * teste;

size_t sealed_size = sizeof(sgx_sealed_data_t) + sizeof(teste);
    uint8_t* sealed_data = (uint8_t*) malloc(sealed_size);

FILE *fp;
  fp = fopen("simple.zod", "a+");
  int numRead = fread(sealed_data,sealed_size, 1, fp );
  if (numRead == 0) {
        return NULL;
        }
fclose(fp);
   
    status = unseal(global_eid, &ecall_status,
            (sgx_sealed_data_t*)sealed_data, sealed_size,
            (uint8_t*)&teste,  sizeof(teste));

    if (!is_ecall_successful(status, "Unsealing failed :(", ecall_status)) {
        return NULL;
    }

    std::cout << "UnSeal round trip success! Receive back final -> " << teste << "end"<< std::endl;  // the error is at this point
    return (env)->NewStringUTF("teste");
}

The error is in the print function:
std::cout << "UnSeal round trip success! Receive back final -> " << teste << "end"<< std::endl
C++ code that I use is this:

int  unseal(){
if (initialize_enclave(&global_eid, "enclave.token", "enclave.signed.so") < 0) {
std::cout << "Fail to initialize enclave." << std::endl;
       return NULL;
}
 sgx_status_t status;
    sgx_status_t ecall_status;

  const char * teste;

size_t sealed_size = sizeof(sgx_sealed_data_t) + sizeof(teste);
    uint8_t* sealed_data = (uint8_t*) malloc(sealed_size);

FILE *fp;
  fp = fopen("simple.zod", "a+");
  int numRead = fread(sealed_data,sealed_size, 1, fp );
  if (numRead == 0) {
        return NULL;
        }
fclose(fp);

    status = unseal(global_eid, &ecall_status,
            (sgx_sealed_data_t*)sealed_data, sealed_size,
            (uint8_t*)&teste,  sizeof(teste));


    if (!is_ecall_successful(status, "Unsealing failed :(", ecall_status)) {
        return NULL;
    }
 
    std::cout << "Seal round trip success! Receive back final -> " << teste << "end"<< std::endl;


return 1;
}


Note that I can unseal if I do not stop the program. The problem is when I start the program again.
I can give information regarding the seal process or make the project fully available.

0 Kudos
Reply