Added gmp

parent f3fe85cf
...@@ -3,65 +3,54 @@ ...@@ -3,65 +3,54 @@
// //
#define GMP_WITH_SGX #define GMP_WITH_SGX
#include "libff/algebra/curves/alt_bn128/alt_bn128_pp.hpp"
#include "libff/algebra/curves/alt_bn128/alt_bn128_init.hpp"
#include "BLSUtils.h" #include "BLSUtils.h"
#include "libff/algebra/curves/alt_bn128/alt_bn128_init.hpp"
#include "libff/algebra/curves/alt_bn128/alt_bn128_pp.hpp"
std::string *stringFromKey(libff::alt_bn128_Fr *_key) {
std::string* stringFromKey(libff::alt_bn128_Fr* _key) {
mpz_t t; mpz_t t;
mpz_init(t); mpz_init(t);
_key->as_bigint().to_mpz(t); _key->as_bigint().to_mpz(t);
char arr[mpz_sizeinbase (t, 10) + 2]; char arr[mpz_sizeinbase(t, 10) + 2];
char * tmp = mpz_get_str(arr, 10, t); char *tmp = mpz_get_str(arr, 10, t);
mpz_clear(t); mpz_clear(t);
return new std::string(tmp); return new std::string(tmp);
} }
libff::alt_bn128_Fr *keyFromString(std::string &_keyString) {
libff::alt_bn128_Fr* keyFromString(std::string& _keyString) {
return new libff::alt_bn128_Fr(_keyString.c_str()); return new libff::alt_bn128_Fr(_keyString.c_str());
} }
bool check_key(const char *_keyString) {
void import_key(const char* _keyString, char* encryptedKey, uint64_t bufLen) {
libff::init_alt_bn128_params(); libff::init_alt_bn128_params();
if (encryptedKey == nullptr && bufLen < 100)
throw std::exception();
if (_keyString == nullptr) if (_keyString == nullptr)
throw std::exception(); return false;
std::string ks(_keyString); std::string ks(_keyString);
// std::string keyString = "4160780231445160889237664391382223604184857153814275770598791864649971919844"; // std::string keyString =
// "4160780231445160889237664391382223604184857153814275770598791864649971919844";
auto key = keyFromString(ks); auto key = keyFromString(ks);
auto s1 = stringFromKey(key); auto s1 = stringFromKey(key);
if (s1->compare(ks) != 0) if (s1->compare(ks) != 0)
throw std::exception(); return false;
if (s1->size() < 10) if (s1->size() < 10)
throw std::exception(); return false;
if (s1->size() >= 100) if (s1->size() >= 100)
throw std::exception(); return false;
strncpy(encryptedKey, s1->c_str(), 100);
return true;
} }
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
#define EXTERNC #define EXTERNC
#endif #endif
EXTERNC void import_key(const char* _keyString, char* encryptedKey, uint64_t bufLen); EXTERNC bool check_key(const char* _keyString);
#endif //SGXD_BLSUTILS_H #endif //SGXD_BLSUTILS_H
...@@ -115,10 +115,25 @@ void encrypt_key(int *err_status, char* key, char* encrypted_key) { ...@@ -115,10 +115,25 @@ void encrypt_key(int *err_status, char* key, char* encrypted_key) {
*err_status = -1; *err_status = -1;
if (strnlen(key) == 100) if (strnlen(key) >=128)
return; return;
import_key(key, encrypted_key, 100); *err_status = -3;
check_key(key);
uint32_t sealedLen = sgx_calc_sealed_data_size(0, strlen(key) + 1);
*err_status = -3;
if (sealedLen > 1024) {
return;
}
*err_status = -4;
if (sgx_seal_data(0, NULL, strlen(key) + 1, key, sealedLen, encrypted_key) != SGX_SUCCESS)
return;
*err_status = 0; *err_status = 0;
} }
......
...@@ -23,8 +23,8 @@ enclave { ...@@ -23,8 +23,8 @@ enclave {
public void encrypt_key ( public void encrypt_key (
[user_check] int *err_status, [user_check] int *err_status,
[in, count = 100] char* key, [in, count = 128] char* key,
[out, count = 100] char* encrypted_key); [out, count = 1024] char* encrypted_key);
}; };
......
...@@ -117,17 +117,19 @@ int main (int argc, char *argv[]) ...@@ -117,17 +117,19 @@ int main (int argc, char *argv[])
const char* key = "4160780231445160889237664391382223604184857153814275770598791864649971919844"; const char* key = "4160780231445160889237664391382223604184857153814275770598791864649971919844";
char keyArray[100]; char keyArray[128];
char encryptedKey[100]; char encryptedKey[1024];
strncpy(keyArray, key, 100); strncpy(keyArray, key, 128);
int err_status = -2; int err_status = -2;
status= encrypt_key(eid, &err_status, keyArray, encryptedKey); status= encrypt_key(eid, &err_status, keyArray, encryptedKey);
if ( status != SGX_SUCCESS ) { if ( status != SGX_SUCCESS ) {
fprintf(stderr, "ECALL encrypt_key: 0x%04x\n", status); fprintf(stderr, "ECALL encrypt_key: 0x%04x\n", status);
return 1; return 1;
...@@ -136,7 +138,7 @@ int main (int argc, char *argv[]) ...@@ -136,7 +138,7 @@ int main (int argc, char *argv[])
gmp_printf("Encrypt key completed with status: %d \n", err_status); gmp_printf("Encrypt key completed with status: %d \n", err_status);
gmp_printf("Result: %s \n", encryptedKey); //gmp_printf("Result: %s \n", encryptedKey);
return 0; return 0;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment