Fixes

parent d9d10219
...@@ -110,3 +110,61 @@ bool sign(const char* _encryptedKeyHex, const char* _hashHex, size_t _t, size_t ...@@ -110,3 +110,61 @@ bool sign(const char* _encryptedKeyHex, const char* _hashHex, size_t _t, size_t
} }
char *encryptBLSKeyShare2Hex(int *errStatus, char *err_string, const char *_key) {
char *keyArray = (char *) calloc(BUF_LEN, 1);
uint8_t *encryptedKey = (uint8_t *) calloc(BUF_LEN, 1);
char *errMsg = (char *) calloc(BUF_LEN, 1);
strncpy((char *) keyArray, (char *) _key, BUF_LEN);
*errStatus = -1;
unsigned int encryptedLen = 0;
status = encrypt_key(eid, errStatus, errMsg, keyArray, encryptedKey, &encryptedLen);
if (status != SGX_SUCCESS) {
*errStatus = -1;
return nullptr;
}
if (*errStatus != 0) {
return nullptr;
}
char *result = (char *) calloc(2 * BUF_LEN, 1);
carray2Hex(encryptedKey, encryptedLen, result);
return result;
}
char *decryptBLSKeyShareFromHex(int *errStatus, char *errMsg, const char *_encryptedKey) {
*errStatus = -1;
uint64_t decodedLen = 0;
uint8_t decoded[BUF_LEN];
if (!(hex2carray(_encryptedKey, &decodedLen, decoded))) {
return nullptr;
}
char *plaintextKey = (char *) calloc(BUF_LEN, 1);
status = decrypt_key(eid, errStatus, errMsg, decoded, decodedLen, plaintextKey);
if (status != SGX_SUCCESS) {
return nullptr;
}
if (*errStatus != 0) {
return nullptr;
}
return plaintextKey;
}
\ No newline at end of file
...@@ -29,6 +29,8 @@ EXTERNC bool hex2carray(const char * _hex, uint64_t *_bin_len, ...@@ -29,6 +29,8 @@ EXTERNC bool hex2carray(const char * _hex, uint64_t *_bin_len,
EXTERNC char *encryptBLSKeyShare2Hex(int *errStatus, char *err_string, const char *_key);
EXTERNC char *decryptBLSKeyShareFromHex(int *errStatus, char *errMsg, const char *_encryptedKey);
#endif //SGXWALLET_BLSCRYPTO_H #endif //SGXWALLET_BLSCRYPTO_H
...@@ -65,63 +65,6 @@ sgx_enclave_id_t eid; ...@@ -65,63 +65,6 @@ sgx_enclave_id_t eid;
sgx_status_t status; sgx_status_t status;
int updated; int updated;
char *encryptKey2Hex(int *errStatus, char *err_string, const char *_key) {
char *keyArray = (char *) calloc(BUF_LEN, 1);
uint8_t *encryptedKey = (uint8_t *) calloc(BUF_LEN, 1);
char *errMsg = (char *) calloc(BUF_LEN, 1);
strncpy((char *) keyArray, (char *) _key, BUF_LEN);
*errStatus = -1;
unsigned int encryptedLen = 0;
status = encrypt_key(eid, errStatus, errMsg, keyArray, encryptedKey, &encryptedLen);
if (status != SGX_SUCCESS) {
*errStatus = -1;
return nullptr;
}
if (*errStatus != 0) {
return nullptr;
}
char *result = (char *) calloc(2 * BUF_LEN, 1);
carray2Hex(encryptedKey, encryptedLen, result);
return result;
}
char *decryptKeyFromHex(int *errStatus, char *errMsg, const char *_encryptedKey) {
*errStatus = -1;
uint64_t decodedLen = 0;
uint8_t decoded[BUF_LEN];
if (!(hex2carray(_encryptedKey, &decodedLen, decoded))) {
return nullptr;
}
char *plaintextKey = (char *) calloc(BUF_LEN, 1);
status = decrypt_key(eid, errStatus, errMsg, decoded, decodedLen, plaintextKey);
if (status != SGX_SUCCESS) {
return nullptr;
}
if (*errStatus != 0) {
return nullptr;
}
return plaintextKey;
}
#define TEST_KEY "4160780231445160889237664391382223604184857153814275770598791864649971919844" #define TEST_KEY "4160780231445160889237664391382223604184857153814275770598791864649971919844"
...@@ -139,7 +82,7 @@ char* encryptTestKey() { ...@@ -139,7 +82,7 @@ char* encryptTestKey() {
char *errMsg = (char *) calloc(BUF_LEN, 1); char *errMsg = (char *) calloc(BUF_LEN, 1);
char *encryptedKeyHex = encryptKey2Hex(&errStatus, errMsg, key); char *encryptedKeyHex = encryptBLSKeyShare2Hex(&errStatus, errMsg, key);
REQUIRE(encryptedKeyHex != nullptr); REQUIRE(encryptedKeyHex != nullptr);
REQUIRE(errStatus == 0); REQUIRE(errStatus == 0);
...@@ -176,7 +119,7 @@ TEST_CASE("BLS key encrypt/decrypt", "[bls-key-encrypt-decrypt]") { ...@@ -176,7 +119,7 @@ TEST_CASE("BLS key encrypt/decrypt", "[bls-key-encrypt-decrypt]") {
char* encryptedKey = encryptTestKey(); char* encryptedKey = encryptTestKey();
REQUIRE(encryptedKey != nullptr); REQUIRE(encryptedKey != nullptr);
char* plaintextKey = decryptKeyFromHex(&errStatus, errMsg, encryptedKey); char* plaintextKey = decryptBLSKeyShareFromHex(&errStatus, errMsg, encryptedKey);
REQUIRE(errStatus == 0); REQUIRE(errStatus == 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