|
@@ -52,41 +52,41 @@
|
|
|
uint8_t apache_iv[12] = {0,0,0,0, 0,0,0,0, 0,0,0,0};
|
|
|
uint8_t client_iv[12] = {0,0,0,0, 0,0,0,0, 0,0,0,0};
|
|
|
uint8_t verifier_iv[12] = {0,0,0,0, 0,0,0,0, 0,0,0,0};
|
|
|
-extern uint8_t apache_key[16];
|
|
|
-extern uint8_t verifier_key[16];
|
|
|
+extern uint8_t apache_key[16];
|
|
|
+extern uint8_t verifier_key[16];
|
|
|
|
|
|
|
|
|
-//uint32_t client_iv=0;
|
|
|
+//uint32_t client_iv=0;
|
|
|
|
|
|
// internal-internal
|
|
|
uint32_t create_ec_key_pair(sgx_ec256_public_t* pub_key, sgx_ec256_private_t* priv_key);
|
|
|
void serialize_key_pair_to_string( sgx_ec256_public_t* pub_key, sgx_ec256_private_t* signing_priv_key, uint8_t* private_public_key_string);
|
|
|
-void deserialize_string_to_key_pair(uint8_t* private_public_key_string, sgx_ec256_public_t* pub_key, sgx_ec256_private_t* priv_key);
|
|
|
-uint32_t create_mitigator_header_value(__attribute__((unused)) uint8_t* signature_data, __attribute__((unused)) uint8_t* signature, __attribute__((unused)) uint8_t* private_key, __attribute__((unused)) sgx_ec256_signature_t* sig2);
|
|
|
+void deserialize_string_to_key_pair(uint8_t* private_public_key_string, sgx_ec256_public_t* pub_key, sgx_ec256_private_t* priv_key);
|
|
|
+uint32_t create_mitigator_header_value(__attribute__((unused)) uint8_t* signature_data, __attribute__((unused)) uint8_t* signature, __attribute__((unused)) uint8_t* private_key, __attribute__((unused)) sgx_ec256_signature_t* sig2);
|
|
|
uint32_t aes_gcm_internal_call(uint8_t* ip_ciphertext, uint32_t ip_ciphertext_len, uint8_t* ip_key, uint8_t* ip_iv, uint8_t* tag, uint8_t* op_plaintext, uint32_t enc);
|
|
|
void memcpy_equivalent_copy(uint8_t* dest, uint8_t* src, uint32_t length);
|
|
|
|
|
|
uint32_t verify_mitigator_header_value(uint8_t* signature_data, uint8_t* signature, sgx_ec256_public_t* pub_key);
|
|
|
|
|
|
-uint32_t calculate_sealed_data_size( uint32_t input_size) ;
|
|
|
-uint32_t create_and_seal_ecdsa_signing_key_pair(__attribute__((unused)) sgx_ec256_public_t* pub_key, __attribute__((unused)) uint32_t* sealed_data_length, __attribute__((unused)) uint8_t* sealed_data);
|
|
|
+uint32_t calculate_sealed_data_size( uint32_t input_size) ;
|
|
|
+uint32_t create_and_seal_ecdsa_signing_key_pair(__attribute__((unused)) sgx_ec256_public_t* pub_key, __attribute__((unused)) uint32_t* sealed_data_length, __attribute__((unused)) uint8_t* sealed_data);
|
|
|
uint32_t unseal_and_restore_sealed_signing_key_pair(__attribute__((unused)) sgx_ec256_public_t* pub_key, uint8_t* sealed_data, size_t* sgx_sealed_data_length);
|
|
|
|
|
|
uint32_t decrypt_verifiers_message_set_apache_mrsigner(uint8_t* ciphertext, uint8_t* tag);
|
|
|
uint32_t create_and_encrypt_mitigator_header_value(uint8_t* plaintext_sign_data_and_sign, uint8_t* encrypted_sign_data_and_sign, uint8_t* tag, uint8_t* signing_private_key, __attribute__((unused)) sgx_ec256_signature_t* sig2);
|
|
|
-static void reverse_byte_array(uint8_t *array, size_t size);
|
|
|
+//static void reverse_byte_array(uint8_t *array, size_t size);
|
|
|
|
|
|
|
|
|
-uint32_t one_la_done=0;
|
|
|
+uint32_t one_la_done=0;
|
|
|
static sgx_ec256_public_t short_term_pub_key;
|
|
|
-static sgx_ec256_private_t short_term_priv_key;
|
|
|
-unsigned char short_term_private_key_arr[32];
|
|
|
+static sgx_ec256_private_t short_term_priv_key;
|
|
|
+unsigned char short_term_private_key_arr[32];
|
|
|
unsigned char short_term_public_key_arr[64];
|
|
|
|
|
|
-//sgx_ec256_signature_t generated_signature; // TODO: remove
|
|
|
-sgx_measurement_t apache_mr_signer; // TODO: remove
|
|
|
-sgx_measurement_t verifier_mr_enclave; // TODO: remove
|
|
|
-static sgx_ec256_private_t signing_priv_key;
|
|
|
+//sgx_ec256_signature_t generated_signature; // TODO: remove
|
|
|
+sgx_measurement_t apache_mr_signer; // TODO: remove
|
|
|
+sgx_measurement_t verifier_mr_enclave; // TODO: remove
|
|
|
+static sgx_ec256_private_t signing_priv_key;
|
|
|
|
|
|
extern "C" uint32_t verify_peer_enclave_trust(__attribute__((unused)) sgx_dh_session_enclave_identity_t* peer_enclave_identity)
|
|
|
{
|
|
@@ -100,47 +100,47 @@ extern "C" uint32_t verify_peer_enclave_trust(__attribute__((unused)) sgx_dh_se
|
|
|
// return 0x55;
|
|
|
//sgx_measurement_t local_mr_enclave;
|
|
|
verifier_mr_enclave = peer_enclave_identity->mr_enclave;
|
|
|
- memset(&(apache_mr_signer.m),0x0,SGX_HASH_SIZE); // "initialization"
|
|
|
+ memset(&(apache_mr_signer.m),0x0,SGX_HASH_SIZE); // "initialization"
|
|
|
one_la_done=1;
|
|
|
|
|
|
}
|
|
|
else // apache enclave
|
|
|
{
|
|
|
sgx_measurement_t actual_mr_signer = peer_enclave_identity->mr_signer;
|
|
|
- // verifier's mrsigner
|
|
|
- // uint8_t expected_mr_signer[32] ={0xdf, 0xd7, 0x3b, 0x93, 0xea, 0x39, 0x02, 0x02, 0x3c, 0xd0, 0x52, 0x1a, 0xbd, 0x00, 0xaf, 0xb9, 0xa6, 0x54, 0x57, 0x3e, 0xe5, 0xef, 0x36, 0xf4, 0x8c, 0xc2, 0x4d, 0x92, 0x70, 0xae, 0xd4, 0x7c};
|
|
|
+ // verifier's mrsigner
|
|
|
+ // uint8_t expected_mr_signer[32] ={0xdf, 0xd7, 0x3b, 0x93, 0xea, 0x39, 0x02, 0x02, 0x3c, 0xd0, 0x52, 0x1a, 0xbd, 0x00, 0xaf, 0xb9, 0xa6, 0x54, 0x57, 0x3e, 0xe5, 0xef, 0x36, 0xf4, 0x8c, 0xc2, 0x4d, 0x92, 0x70, 0xae, 0xd4, 0x7c};
|
|
|
int count;
|
|
|
for(count=0; count<SGX_HASH_SIZE; count++)
|
|
|
{
|
|
|
if( actual_mr_signer.m[count] != apache_mr_signer.m[count] )
|
|
|
- return ENCLAVE_TRUST_ERROR;
|
|
|
+ return ENCLAVE_TRUST_ERROR;
|
|
|
}
|
|
|
}
|
|
|
return SGX_SUCCESS;
|
|
|
}
|
|
|
|
|
|
-// increments last 4 bytes (in big-endian order)
|
|
|
+// increments last 4 bytes (in big-endian order)
|
|
|
uint32_t aes_gcm_increment_iv_internal_call(uint8_t* iv)
|
|
|
{
|
|
|
uint32_t counter;
|
|
|
for(counter=11;counter>7;counter--)
|
|
|
{
|
|
|
- if(iv[counter] == 0xff)
|
|
|
+ if(iv[counter] == 0xff)
|
|
|
{
|
|
|
if(counter - 1 == 7)
|
|
|
- return 0xff;
|
|
|
+ return 0xff;
|
|
|
iv[counter-1] = 0x01;
|
|
|
- iv[counter] = 0x0;
|
|
|
+ iv[counter] = 0x0;
|
|
|
}
|
|
|
else
|
|
|
- iv[counter] += 1;
|
|
|
+ iv[counter] += 1;
|
|
|
}
|
|
|
- return 0;
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
-// TODO: change global_session_info to two different dh_sessions
|
|
|
-// This needs to be called after the first local attestation is successful - otherwise, the internal apache_mr_signer.m will not be set properly for the comparison of the mrsigner for the 2nd LA in verify_peer_enclave_trust.
|
|
|
-// (I.e. if it is not called then DoS
|
|
|
+// TODO: change global_session_info to two different dh_sessions
|
|
|
+// This needs to be called after the first local attestation is successful - otherwise, the internal apache_mr_signer.m will not be set properly for the comparison of the mrsigner for the 2nd LA in verify_peer_enclave_trust.
|
|
|
+// (I.e. if it is not called then DoS
|
|
|
uint32_t decrypt_verifiers_message_set_apache_mrsigner(uint8_t* ciphertext, uint8_t* tag)
|
|
|
{
|
|
|
uint32_t internal_ret_status= aes_gcm_internal_call(ciphertext, 32, verifier_key, verifier_iv , tag, (uint8_t*) &(apache_mr_signer.m), 0);
|
|
@@ -152,18 +152,18 @@ uint32_t create_and_encrypt_mitigator_header_value(uint8_t* plaintext_sign_data_
|
|
|
|
|
|
{
|
|
|
uint32_t count;
|
|
|
- uint8_t sign_data_and_sign[160];
|
|
|
+ uint8_t sign_data_and_sign[160];
|
|
|
uint32_t ret_status=create_mitigator_header_value(sign_data_and_sign, sign_data_and_sign+96, signing_private_key, sig2);
|
|
|
if(ret_status != SGX_SUCCESS)
|
|
|
return 0xFFFFFFDD;
|
|
|
// TODO: Remove - just for troubleshooting
|
|
|
for(count=0; count<160; count++)
|
|
|
- *(plaintext_sign_data_and_sign+count)=sign_data_and_sign[count];
|
|
|
+ *(plaintext_sign_data_and_sign+count)=sign_data_and_sign[count];
|
|
|
|
|
|
- ret_status = aes_gcm_internal_call(sign_data_and_sign, 160, apache_key, apache_iv, tag, encrypted_sign_data_and_sign, 1);
|
|
|
-// ret_status = encrypt_internal(sign_data_and_sign, 160, tag, encrypted_sign_data_and_sign);
|
|
|
- aes_gcm_increment_iv_internal_call(apache_iv);
|
|
|
- return ret_status;
|
|
|
+ ret_status = aes_gcm_internal_call(sign_data_and_sign, 160, apache_key, apache_iv, tag, encrypted_sign_data_and_sign, 1);
|
|
|
+// ret_status = encrypt_internal(sign_data_and_sign, 160, tag, encrypted_sign_data_and_sign);
|
|
|
+ aes_gcm_increment_iv_internal_call(apache_iv);
|
|
|
+ return ret_status;
|
|
|
}
|
|
|
|
|
|
|
|
@@ -207,7 +207,7 @@ void serialize_key_pair_to_string(sgx_ec256_public_t* pub_key, sgx_ec256_private
|
|
|
{
|
|
|
for(counter=2*SGX_ECP256_KEY_SIZE;counter<3*SGX_ECP256_KEY_SIZE; counter++)
|
|
|
*(private_public_key_string+counter)=signing_priv_key->r[counter - 2*SGX_ECP256_KEY_SIZE];
|
|
|
- }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -215,10 +215,10 @@ void serialize_key_pair_to_string(sgx_ec256_public_t* pub_key, sgx_ec256_private
|
|
|
// todo: set to private
|
|
|
void deserialize_string_to_key_pair(uint8_t* private_public_key_string, sgx_ec256_public_t* pub_key, sgx_ec256_private_t* signing_priv_key)
|
|
|
{
|
|
|
- if(private_public_key_string != NULL) // nowhere to deserialize from
|
|
|
+ if(private_public_key_string != NULL) // nowhere to deserialize from
|
|
|
{
|
|
|
uint32_t counter;
|
|
|
- if(signing_priv_key != NULL)
|
|
|
+ if(signing_priv_key != NULL)
|
|
|
{
|
|
|
|
|
|
for(counter=2*SGX_ECP256_KEY_SIZE;counter<3*SGX_ECP256_KEY_SIZE; counter++)
|
|
@@ -245,24 +245,24 @@ uint32_t create_and_seal_ecdsa_signing_key_pair(__attribute__((unused)) sgx_ec
|
|
|
if(ret_status!=SGX_SUCCESS)
|
|
|
return ret_status;
|
|
|
for(counter=0;counter<SGX_ECP256_KEY_SIZE; counter++)
|
|
|
- signing_priv_key.r[counter]=private_key.r[counter];
|
|
|
+ signing_priv_key.r[counter]=private_key.r[counter];
|
|
|
// generating the entire string as there is no SGX function to generate the public key from the private one.
|
|
|
uint8_t* private_public_key_string = (uint8_t*) malloc(3*SGX_ECP256_KEY_SIZE);
|
|
|
uint8_t* sealed_data2 = (uint8_t*) malloc(*sealed_data_length);
|
|
|
// serializing keypair to string
|
|
|
serialize_key_pair_to_string(pub_key, &private_key, private_public_key_string);
|
|
|
- uint8_t* private_key_string = (uint8_t*) malloc(SGX_ECP256_KEY_SIZE);
|
|
|
+ uint8_t* private_key_string = (uint8_t*) malloc(SGX_ECP256_KEY_SIZE);
|
|
|
for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
|
|
|
*(private_key_string+counter)=private_key.r[counter];
|
|
|
// return *sealed_data_length;
|
|
|
- ret_status = sgx_seal_data(0, NULL, 3*SGX_ECP256_KEY_SIZE, private_public_key_string, *sealed_data_length, (sgx_sealed_data_t*) sealed_data2);
|
|
|
+ ret_status = sgx_seal_data(0, NULL, 3*SGX_ECP256_KEY_SIZE, private_public_key_string, *sealed_data_length, (sgx_sealed_data_t*) sealed_data2);
|
|
|
for(counter=0;counter<*sealed_data_length;counter++)
|
|
|
- *(sealed_data+counter)=*(sealed_data2+counter);
|
|
|
+ *(sealed_data+counter)=*(sealed_data2+counter);
|
|
|
free(sealed_data2);
|
|
|
- free(private_key_string); //free(private_key);
|
|
|
+ free(private_key_string); //free(private_key);
|
|
|
free(private_public_key_string);
|
|
|
|
|
|
- return ret_status; // SGX_SUCCESS;
|
|
|
+ return ret_status; // SGX_SUCCESS;
|
|
|
}
|
|
|
|
|
|
uint32_t unseal_and_restore_sealed_signing_key_pair(__attribute__((unused)) sgx_ec256_public_t* pub_key, uint8_t* sealed_data, size_t* sgx_sealed_data_length)
|
|
@@ -272,10 +272,10 @@ uint32_t unseal_and_restore_sealed_signing_key_pair(__attribute__((unused)) sgx_
|
|
|
if(expected_plaintext_msg_length == 0xffffffff)
|
|
|
return 0xFFFFFFFF;
|
|
|
|
|
|
- uint8_t* sealed_data2 = (uint8_t*) malloc(*sgx_sealed_data_length);
|
|
|
+ uint8_t* sealed_data2 = (uint8_t*) malloc(*sgx_sealed_data_length);
|
|
|
for(counter=0;counter<*sgx_sealed_data_length;counter++)
|
|
|
{
|
|
|
- *(sealed_data2+counter)=*(sealed_data+counter);
|
|
|
+ *(sealed_data2+counter)=*(sealed_data+counter);
|
|
|
}
|
|
|
|
|
|
temp_plaintext = (uint8_t*)malloc( expected_plaintext_msg_length );
|
|
@@ -286,74 +286,74 @@ uint32_t unseal_and_restore_sealed_signing_key_pair(__attribute__((unused)) sgx_
|
|
|
return ret_status;
|
|
|
}
|
|
|
deserialize_string_to_key_pair(temp_plaintext, pub_key, &signing_priv_key);
|
|
|
- free(temp_plaintext); free(sealed_data2);
|
|
|
+ free(temp_plaintext); free(sealed_data2);
|
|
|
return SGX_SUCCESS;
|
|
|
}
|
|
|
|
|
|
uint32_t create_mitigator_header_value(__attribute__((unused)) uint8_t* signature_data, __attribute__((unused)) uint8_t* signature, __attribute__((unused)) uint8_t* private_key, __attribute__((unused)) sgx_ec256_signature_t* sig2)
|
|
|
{
|
|
|
- // Otherwise: DoS or possible bypass (fake verifier does LA but real verifier mrenclave is given out by decryptor) - signature with junk verifier mrenclave or whatever is in the memory.
|
|
|
+ // Otherwise: DoS or possible bypass (fake verifier does LA but real verifier mrenclave is given out by decryptor) - signature with junk verifier mrenclave or whatever is in the memory.
|
|
|
if(one_la_done < 1)
|
|
|
return 0xde; // This needs to be called at any point after the first local attestation is done - else, a junk verifier mrenclave will be included in the signature
|
|
|
|
|
|
// create key pair
|
|
|
- uint32_t ret_status = ec_key_gen(short_term_public_key_arr, short_term_public_key_arr + 32, short_term_private_key_arr); //create_ec_key_pair(&short_term_pub_key, &short_term_priv_key);
|
|
|
+ uint32_t ret_status = ecdh_key_gen(short_term_public_key_arr, short_term_public_key_arr + 32, short_term_private_key_arr); //create_ec_key_pair(&short_term_pub_key, &short_term_priv_key);
|
|
|
uint32_t counter;
|
|
|
uint32_t ret_status2;
|
|
|
if(ret_status!=0)
|
|
|
- return ret_status;
|
|
|
+ return ret_status;
|
|
|
for(counter=0;counter<32;counter++)
|
|
|
{
|
|
|
*(signature_data + counter) = short_term_public_key_arr[counter]; // public key -> x component
|
|
|
*(signature_data + counter + 32) = short_term_public_key_arr[counter + 32]; // public key -> y component
|
|
|
- *(signature_data + counter + 64) = 0x55; // verifier mr_enclave // TODO: fix this.
|
|
|
+ *(signature_data + counter + 64) = 0x55; // verifier mr_enclave // TODO: fix this.
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// retrieve long-term private key from global variable - apparently, need to create a local copy or it crashes
|
|
|
sgx_ec256_private_t long_term_priv_key;
|
|
|
- for(counter=0; counter<SGX_ECP256_KEY_SIZE; counter++)
|
|
|
- long_term_priv_key.r[counter] = signing_priv_key.r[counter];
|
|
|
- // sign public key with long-term private key
|
|
|
- sgx_ec256_signature_t local_signature; sgx_ecc_state_handle_t ecc_handle;
|
|
|
+ for(counter=0; counter<SGX_ECP256_KEY_SIZE; counter++)
|
|
|
+ long_term_priv_key.r[counter] = signing_priv_key.r[counter];
|
|
|
+ // sign public key with long-term private key
|
|
|
+ sgx_ec256_signature_t local_signature; sgx_ecc_state_handle_t ecc_handle;
|
|
|
|
|
|
- // TODO: For testing/checking purposes only.
|
|
|
+ // TODO: For testing/checking purposes only.
|
|
|
for(counter=0;counter<32;counter++)
|
|
|
- *(private_key+counter)=short_term_private_key_arr[counter]; //short_term_priv_key.r[counter];
|
|
|
+ *(private_key+counter)=short_term_private_key_arr[counter]; //short_term_priv_key.r[counter];
|
|
|
|
|
|
//// opening context for signature
|
|
|
- ret_status = sgx_ecc256_open_context(&ecc_handle);
|
|
|
- if(ret_status != SGX_SUCCESS)
|
|
|
- return ret_status;
|
|
|
+ ret_status = sgx_ecc256_open_context(&ecc_handle);
|
|
|
+ if(ret_status != SGX_SUCCESS)
|
|
|
+ return ret_status;
|
|
|
ret_status = sgx_ecdsa_sign(signature_data, 96, &long_term_priv_key, &local_signature, ecc_handle);
|
|
|
- ret_status2 = sgx_ecc256_close_context(ecc_handle);
|
|
|
-// free(public_key_string);
|
|
|
- if(ret_status == SGX_SUCCESS)
|
|
|
+ ret_status2 = sgx_ecc256_close_context(ecc_handle);
|
|
|
+// free(public_key_string);
|
|
|
+ if(ret_status == SGX_SUCCESS)
|
|
|
{ // this only works for Little-endian architectures - need to do byte-wise swapping of the bytes obtained on RHS
|
|
|
- uint8_t *current_sig_byte = (uint8_t*)(&(local_signature.x));
|
|
|
- uint32_t ecdsa_sig_count;
|
|
|
+ uint8_t *current_sig_byte = (uint8_t*)(&(local_signature.x));
|
|
|
+ uint32_t ecdsa_sig_count;
|
|
|
for(ecdsa_sig_count=0;ecdsa_sig_count<32;ecdsa_sig_count++)
|
|
|
- signature[31-ecdsa_sig_count]=*(current_sig_byte+ecdsa_sig_count);
|
|
|
- current_sig_byte = (uint8_t*)(&(local_signature.y));
|
|
|
+ signature[31-ecdsa_sig_count]=*(current_sig_byte+ecdsa_sig_count);
|
|
|
+ current_sig_byte = (uint8_t*)(&(local_signature.y));
|
|
|
for(ecdsa_sig_count=0;ecdsa_sig_count<32;ecdsa_sig_count++)
|
|
|
- signature[63-ecdsa_sig_count]=*(current_sig_byte+ecdsa_sig_count);
|
|
|
+ signature[63-ecdsa_sig_count]=*(current_sig_byte+ecdsa_sig_count);
|
|
|
for(ecdsa_sig_count=0;ecdsa_sig_count<8;ecdsa_sig_count++)
|
|
|
sig2->x[ecdsa_sig_count]=local_signature.x[ecdsa_sig_count];
|
|
|
for(ecdsa_sig_count=0;ecdsa_sig_count<8;ecdsa_sig_count++)
|
|
|
sig2->y[ecdsa_sig_count]=local_signature.y[ecdsa_sig_count];
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
if(ret_status != SGX_SUCCESS || ret_status2 != SGX_SUCCESS)
|
|
|
return 0xFFFFFFFF;
|
|
|
- return 0;
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
uint32_t verify_mitigator_header_value(uint8_t* signature_data, uint8_t* signature, sgx_ec256_public_t* pub_key)
|
|
|
{
|
|
|
- sgx_ec256_public_t local_pub_key; uint32_t counter; uint32_t ret_status; uint32_t ret_status2;
|
|
|
+ sgx_ec256_public_t local_pub_key; uint32_t counter; uint32_t ret_status; uint32_t ret_status2;
|
|
|
for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
|
|
|
{
|
|
|
- local_pub_key.gx[counter] = pub_key->gx[counter];
|
|
|
- local_pub_key.gy[counter] = pub_key->gy[counter];
|
|
|
+ local_pub_key.gx[counter] = pub_key->gx[counter];
|
|
|
+ local_pub_key.gy[counter] = pub_key->gy[counter];
|
|
|
}
|
|
|
sgx_ec256_signature_t local_signature; sgx_ecc_state_handle_t ecc_handle;
|
|
|
uint8_t *current_sig_byte = (uint8_t*)(&(local_signature.x));
|
|
@@ -374,54 +374,54 @@ uint32_t verify_mitigator_header_value(uint8_t* signature_data, uint8_t* signatu
|
|
|
if(ret_status != SGX_SUCCESS || ret_status2 != SGX_SUCCESS)
|
|
|
return 0xFFFFFFFF;
|
|
|
if(verification_result != SGX_EC_VALID)
|
|
|
- return 0xee;
|
|
|
+ return 0xee;
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
uint32_t derive_shared_secret_for_client(uint8_t* pub_key, uint8_t* shared_key)
|
|
|
{
|
|
|
- return 0;
|
|
|
+ return 0;
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
-uint32_t calculate_sealed_data_size( uint32_t input_size)
|
|
|
+uint32_t calculate_sealed_data_size( uint32_t input_size)
|
|
|
{
|
|
|
-// *op_size=sgx_calc_sealed_data_size(0, input_size);
|
|
|
+// *op_size=sgx_calc_sealed_data_size(0, input_size);
|
|
|
return sgx_calc_sealed_data_size(0, input_size);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
-// ip_key will always be within the enclave.
|
|
|
+// ip_key will always be within the enclave.
|
|
|
// enc = 1 for encryption and 0 for decryption, like openssl api
|
|
|
-uint32_t aes_gcm_internal_call(uint8_t* ip_ciphertext, uint32_t ip_ciphertext_len, uint8_t* ip_key, uint8_t* ip_iv, uint8_t* tag, uint8_t* op_plaintext, uint32_t enc)
|
|
|
+uint32_t aes_gcm_internal_call(uint8_t* ip_ciphertext, uint32_t ip_ciphertext_len, uint8_t* ip_key, uint8_t* ip_iv, uint8_t* tag, uint8_t* op_plaintext, uint32_t enc)
|
|
|
{
|
|
|
uint32_t counter;
|
|
|
if(ip_ciphertext == NULL)
|
|
|
- return 0x33;
|
|
|
+ return 0x33;
|
|
|
if(tag == NULL)
|
|
|
return 0x34;
|
|
|
- if(op_plaintext == NULL)
|
|
|
- return 0x36;
|
|
|
- if(ip_key == NULL)
|
|
|
+ if(op_plaintext == NULL)
|
|
|
+ return 0x36;
|
|
|
+ if(ip_key == NULL)
|
|
|
return 0x35;
|
|
|
if(ip_iv == NULL)
|
|
|
- return 0x37;
|
|
|
+ return 0x37;
|
|
|
|
|
|
- uint8_t* ip_ciphertext_in_enclave = (uint8_t*) malloc(ip_ciphertext_len);
|
|
|
- memcpy_equivalent_copy(ip_ciphertext_in_enclave, ip_ciphertext, ip_ciphertext_len);
|
|
|
+ uint8_t* ip_ciphertext_in_enclave = (uint8_t*) malloc(ip_ciphertext_len);
|
|
|
+ memcpy_equivalent_copy(ip_ciphertext_in_enclave, ip_ciphertext, ip_ciphertext_len);
|
|
|
|
|
|
uint8_t tag_in_enclave [16];
|
|
|
if(!enc)
|
|
|
- memcpy_equivalent_copy(tag_in_enclave, tag, 16);
|
|
|
+ memcpy_equivalent_copy(tag_in_enclave, tag, 16);
|
|
|
|
|
|
uint8_t* op_plaintext_in_enclave = (uint8_t*) malloc(ip_ciphertext_len);
|
|
|
uint32_t internal_ret_status;
|
|
|
if(enc)
|
|
|
internal_ret_status = sgx_rijndael128GCM_encrypt((sgx_key_128bit_t*) ip_key, ip_ciphertext_in_enclave, ip_ciphertext_len, op_plaintext_in_enclave, ip_iv, 0xc, NULL, 0, (sgx_aes_gcm_128bit_tag_t*)tag_in_enclave);
|
|
|
- else
|
|
|
- internal_ret_status = sgx_rijndael128GCM_decrypt((sgx_key_128bit_t*) ip_key, ip_ciphertext_in_enclave, ip_ciphertext_len, op_plaintext_in_enclave, ip_iv, 0xc, NULL, 0, (sgx_aes_gcm_128bit_tag_t*)tag_in_enclave);
|
|
|
+ else
|
|
|
+ internal_ret_status = sgx_rijndael128GCM_decrypt((sgx_key_128bit_t*) ip_key, ip_ciphertext_in_enclave, ip_ciphertext_len, op_plaintext_in_enclave, ip_iv, 0xc, NULL, 0, (sgx_aes_gcm_128bit_tag_t*)tag_in_enclave);
|
|
|
|
|
|
if(internal_ret_status == 0)
|
|
|
{
|
|
@@ -430,61 +430,72 @@ uint32_t aes_gcm_internal_call(uint8_t* ip_ciphertext, uint32_t ip_ciphertext_le
|
|
|
memcpy_equivalent_copy(tag, tag_in_enclave, 16);
|
|
|
}
|
|
|
|
|
|
- free(ip_ciphertext_in_enclave); free(op_plaintext_in_enclave);
|
|
|
-
|
|
|
- return internal_ret_status;
|
|
|
+ free(ip_ciphertext_in_enclave); free(op_plaintext_in_enclave);
|
|
|
+
|
|
|
+ return internal_ret_status;
|
|
|
}
|
|
|
|
|
|
void memcpy_equivalent_copy(uint8_t* dest, uint8_t* src, uint32_t length)
|
|
|
{
|
|
|
- uint32_t counter;
|
|
|
+ uint32_t counter;
|
|
|
for(counter=0; counter<length; counter++)
|
|
|
- *(dest + counter) = *(src + counter);
|
|
|
+ *(dest + counter) = *(src + counter);
|
|
|
}
|
|
|
|
|
|
-uint32_t decrypt_client_data(__attribute__((unused)) unsigned char* ip_client_pub_key, uint32_t ciphertext_length, unsigned char* ip_user_data, unsigned char* op_client_data_to_apache, uint8_t* clen)
|
|
|
+uint32_t decrypt_client_data(unsigned char* ip_client_pub_key, unsigned char* ip_base64_encrypted_client_data, uint32_t base64_encrypted_client_data_length, unsigned char* op_client_data_to_apache, uint8_t* clen)
|
|
|
{
|
|
|
- int counter;
|
|
|
- for(counter=0;counter<ciphertext_length;counter++)
|
|
|
- op_client_data_to_apache[counter]=ip_user_data[counter];
|
|
|
-
|
|
|
- // TODO: Shared key is only needed for testing/verification manually.
|
|
|
- unsigned char shared_key[32];
|
|
|
- unsigned char derived_key[32];
|
|
|
- unsigned long check_ret = compute_shared_ECDHE_key(ip_client_pub_key, ip_client_pub_key + 32, short_term_private_key_arr, shared_key, derived_key);
|
|
|
+ unsigned int counter; unsigned long check_ret; uint32_t ret; int ret2;
|
|
|
+ unsigned char derived_key[32];
|
|
|
+ unsigned char* ip_encrypted_client_data;
|
|
|
+ int ip_encrypted_client_data_length;
|
|
|
+ unsigned char* plaintext_client_data;
|
|
|
+ unsigned char client_iv[12]={0,0,0,0, 0,0,0,0, 0,0,0,0};
|
|
|
+ int op_encrypted_client_data_length; // As the internal enclave aes gcm function wont work with external pointers
|
|
|
+
|
|
|
+ for(counter=0;counter<base64_encrypted_client_data_length;counter++)
|
|
|
+ op_client_data_to_apache[counter]=ip_base64_encrypted_client_data[counter];
|
|
|
+ *clen = (uint8_t) base64_encrypted_client_data_length;
|
|
|
+
|
|
|
+ check_ret = compute_ecdh_shared_key(ip_client_pub_key, ip_client_pub_key + 32, short_term_private_key_arr, derived_key);
|
|
|
if(check_ret != 0)
|
|
|
- return check_ret;
|
|
|
+ return check_ret;
|
|
|
|
|
|
- // TODO: For debugging only.
|
|
|
- for(counter=0;counter<32;counter++)
|
|
|
+ // TODO: For debugging only.
|
|
|
+// for(counter=0;counter<32;counter++)
|
|
|
+// op_client_data_to_apache[counter + base64_encrypted_client_data_length + 32] = derived_key[counter];
|
|
|
+
|
|
|
+ ip_encrypted_client_data = (unsigned char*) malloc(base64_encrypted_client_data_length*3/4); // ciphertext length will be a multiple of 4; this is the maximum length.
|
|
|
+ ip_encrypted_client_data_length = base64_decoding_wrapper(ip_base64_encrypted_client_data, ip_encrypted_client_data, base64_encrypted_client_data_length);
|
|
|
+ if(ip_encrypted_client_data_length == -1)
|
|
|
{
|
|
|
- op_client_data_to_apache[counter+ciphertext_length] = shared_key[counter];
|
|
|
- op_client_data_to_apache[counter+ciphertext_length + 32] = derived_key[counter];
|
|
|
+ free(ip_encrypted_client_data);
|
|
|
+ return 0x33; //ret;
|
|
|
}
|
|
|
|
|
|
- unsigned char miti[4] = {'M', 'i', 't', 'i'};
|
|
|
- unsigned char ciphertext1[16];
|
|
|
- unsigned char tag[16];
|
|
|
- uint8_t client_iv2[12] = {0,0,0,0, 0,0,0,0, 0,0,0,0};
|
|
|
+ for(counter=0; counter<ip_encrypted_client_data_length; counter++)
|
|
|
+ op_client_data_to_apache[counter] = ip_encrypted_client_data[counter];
|
|
|
+ *clen = (uint8_t) ip_encrypted_client_data_length;
|
|
|
|
|
|
- int clen1;
|
|
|
- int result = aes_gcm(1, derived_key, client_iv2, miti , 4, ciphertext1, &clen1, tag);
|
|
|
- *clen = (uint8_t) clen1;
|
|
|
- for(counter=0;counter<clen1;counter++)
|
|
|
+ plaintext_client_data = (unsigned char*) malloc(ip_encrypted_client_data_length);
|
|
|
+ check_ret = aes_gcm(0, derived_key, client_iv, ip_encrypted_client_data, ip_encrypted_client_data_length - 16, plaintext_client_data, &op_encrypted_client_data_length, ip_encrypted_client_data + (ip_encrypted_client_data_length - 16));
|
|
|
+ if(check_ret != 0)
|
|
|
{
|
|
|
- op_client_data_to_apache[counter+ciphertext_length+64] = ciphertext1[counter];
|
|
|
+ free(ip_encrypted_client_data);
|
|
|
+ free(plaintext_client_data);
|
|
|
+ return check_ret;
|
|
|
}
|
|
|
- for(counter=0;counter<16;counter++)
|
|
|
- {
|
|
|
- op_client_data_to_apache[counter+ciphertext_length+64+clen1] = tag[counter];
|
|
|
- }
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+ // TODO: Encryption here - with aes128bit gcm to apache.
|
|
|
+ for(counter=0; counter<op_encrypted_client_data_length; counter++)
|
|
|
+ op_client_data_to_apache[counter] = plaintext_client_data[counter];
|
|
|
+ *clen = (uint8_t) op_encrypted_client_data_length;
|
|
|
+ free(ip_encrypted_client_data);
|
|
|
+ free(plaintext_client_data);
|
|
|
|
|
|
return 0;//result;
|
|
|
}
|
|
|
|
|
|
+/*
|
|
|
static void reverse_byte_array(uint8_t *array, size_t size)
|
|
|
{
|
|
|
size_t i = 0;
|
|
@@ -495,4 +506,4 @@ static void reverse_byte_array(uint8_t *array, size_t size)
|
|
|
array[size - i - 1] = temp;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+*/
|