CVE-2021-36218: Out-of-bounds Write (CWE-787)
value: An issue was discovered in SKALE sgxwallet 1.58.3. sgx_disp_ippsAES_GCMEncrypt allows an out-of-bounds write, resulting in a segfault and compromised enclave. This issue describes a buffer overflow, which was resolved prior to v1.77.0 and not reproducible in latest sgxwallet v1.77.0
CWE description: The product writes data past the end, or before the beginning, of the intended buffer.
CVSS v3 base score: 7.5
Published: 2021-09-27T14:15Z
Repository: https://github.com/skalenetwork/sgxwallet
Language: C++
Files changed: BLSCrypto.cpp, DKGCrypto.cpp, ECDSACrypto.cpp, SEKManager.cpp, SGXWalletServer.cpp, secure_enclave/AESUtils.c, secure_enclave/AESUtils.h, secure_enclave/secure_enclave.c, secure_enclave/secure_enclave.edl, testw.cpp
Commit date: 2020-09-08 15:30:59 +0300
Commit message: SKALE-3205-restart

Diff: diff --git a/BLSCrypto.cpp b/BLSCrypto.cpp
index 3da541b..46a14bb 100644
--- a/BLSCrypto.cpp
+++ b/BLSCrypto.cpp
@@ -84,11 +84,11 @@ void carray2Hex(const unsigned char *d, uint64_t _len, char *_hexArray,
     char hexval[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
                        '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
 
     CHECK_STATE(_hexArrayLen > 2 * _len);
 
-    for (int j = 0; j < _len; j++) {
+    for (uint64_t j = 0; j < _len; j++) {
         _hexArray[j * 2] = hexval[((d[j] >> 4) & 0xF)];
         _hexArray[j * 2 + 1] = hexval[(d[j]) & 0x0F];
     }
 
     _hexArray[_len * 2] = 0;
@@ -103,11 +103,11 @@ bool hex2carray(const char *_hex, uint64_t *_bin_len,
     CHECK_STATE(_hex);
     CHECK_STATE(_bin);
     CHECK_STATE(_bin_len)
 
 
-    int len = strnlen(_hex, 2 * _max_length + 1);
+    uint64_t len = strnlen(_hex, 2 * _max_length + 1);
 
     CHECK_STATE(len != 2 * _max_length + 1);
 
     CHECK_STATE(len <= 2 * _max_length );
 
@@ -115,11 +115,11 @@ bool hex2carray(const char *_hex, uint64_t *_bin_len,
     if (len == 0 && len % 2 == 1)
         return false;
 
     *_bin_len = len / 2;
 
-    for (int i = 0; i < len / 2; i++) {
+    for (uint64_t i = 0; i < len / 2; i++) {
         int high = char2int((char) _hex[i * 2]);
         int low = char2int((char) _hex[i * 2 + 1]);
 
         if (high < 0 || low < 0) {
             return false;
@@ -245,11 +245,12 @@ string encryptBLSKeyShare2Hex(int *errStatus, char *err_string, const char *_key
 
     vector<char> errMsg(BUF_LEN, 0);
 
     strncpy(keyArray->data(), _key, BUF_LEN);
     *errStatus = 0;
-    unsigned int encryptedLen = 0;
+
+    uint64_t encryptedLen = 0;
 
     sgx_status_t status = trustedEncryptKeyAES(eid, errStatus, errMsg.data(), keyArray->data(), encryptedKey->data(), &encryptedLen);
 
     HANDLE_TRUSTED_FUNCTION_ERROR(status, *errStatus, errMsg.data());
 
diff --git a/DKGCrypto.cpp b/DKGCrypto.cpp
index 6531549..baa6efa 100644
--- a/DKGCrypto.cpp
+++ b/DKGCrypto.cpp
@@ -133,17 +133,17 @@ string convertG2ToString(const libff::alt_bn128_G2 &elem, int base, const string
 }
 
 string gen_dkg_poly(int _t) {
     vector<char> errMsg(BUF_LEN, 0);
     int errStatus = 0;
-    uint32_t enc_len = 0;
+    uint64_t enc_len = 0;
 
     vector <uint8_t> encrypted_dkg_secret(BUF_LEN, 0);
 
+    sgx_status_t status = trustedGenDkgSecretAES(
+            eid, &errStatus,errMsg.data(), encrypted_dkg_secret.data(), &enc_len, _t);
 
-
-    sgx_status_t status = trustedGenDkgSecretAES(eid, &errStatus, errMsg.data(), encrypted_dkg_secret.data(), &enc_len, _t);
     HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg.data());
 
     uint64_t length = enc_len;;
 
     vector<char> hexEncrPoly(BUF_LEN, 0);
@@ -212,11 +212,11 @@ getSecretShares(const string &_polyName, const char *_encryptedPolyHex, const ve
 
     string result;
 
     for (int i = 0; i < _n; i++) {
         vector <uint8_t> encryptedSkey(BUF_LEN, 0);
-        uint32_t decLen;
+        uint64_t decLen;
         vector<char> currentShare(193, 0);
         vector<char> sShareG2(320, 0);
 
         string pub_keyB = _publicKeys.at(i);
         vector<char> pubKeyB(129, 0);
@@ -298,11 +298,11 @@ bool createBLSShare(const string &blsKeyName, const char *s_shares, const char *
     SAFE_UINT8_BUF(encr_key,BUF_LEN);
     if (!hex2carray(encryptedKeyHex, &decKeyLen, encr_key, BUF_LEN)) {
         throw SGXException(INVALID_HEX, "Invalid encryptedKeyHex");
     }
 
-    uint32_t enc_bls_len = 0;
+    uint64_t enc_bls_len = 0;
 
     sgx_status_t status = trustedCreateBlsKeyAES(eid, &errStatus, errMsg.data(), s_shares, encr_key, decKeyLen, encr_bls_key,
                                                  &enc_bls_len);
 
     HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg.data());
diff --git a/ECDSACrypto.cpp b/ECDSACrypto.cpp
index dc8229d..ad59774 100644
--- a/ECDSACrypto.cpp
+++ b/ECDSACrypto.cpp
@@ -52,11 +52,11 @@ vector <string> genECDSAKey() {
     int errStatus = 0;
     vector <uint8_t> encr_pr_key(BUF_LEN, 0);
     vector<char> pub_key_x(BUF_LEN, 0);
     vector<char> pub_key_y(BUF_LEN, 0);
 
-    uint32_t enc_len = 0;
+    uint64_t enc_len = 0;
 
     sgx_status_t status = trustedGenerateEcdsaKeyAES(eid, &errStatus,
                                         errMsg.data(), encr_pr_key.data(), &enc_len,
                                         pub_key_x.data(), pub_key_y.data());
 
diff --git a/SEKManager.cpp b/SEKManager.cpp
index f73b7c6..7ac6692 100644
--- a/SEKManager.cpp
+++ b/SEKManager.cpp
@@ -50,11 +50,11 @@ bool case_insensitive_match(string s1, string s2) {
 }
 
 void create_test_key() {
     int errStatus = 0;
     vector<char> errMsg(1024, 0);
-    uint32_t enc_len;
+    uint64_t enc_len;
 
     SAFE_UINT8_BUF(encrypted_key, BUF_LEN);
 
     string key = TEST_VALUE;
 
@@ -107,11 +107,11 @@ shared_ptr <vector<uint8_t>> check_and_set_SEK(const string &SEK) {
     vector<char> errMsg(BUF_LEN, 0);
     int err_status = 0;
 
     auto encrypted_SEK = make_shared < vector < uint8_t >> (BUF_LEN, 0);
 
-    uint32_t l = 0;
+    uint64_t l = 0;
 
     sgx_status_t status = trustedSetSEK_backup(eid, &err_status, errMsg.data(), encrypted_SEK->data(), &l,
                                                SEK.c_str());
 
     encrypted_SEK->resize(l);
@@ -125,11 +125,11 @@ shared_ptr <vector<uint8_t>> check_and_set_SEK(const string &SEK) {
 
 void gen_SEK() {
     vector<char> errMsg(1024, 0);
     int err_status = 0;
     vector <uint8_t> encrypted_SEK(1024, 0);
-    uint32_t enc_len = 0;
+    uint64_t enc_len = 0;
 
     SAFE_CHAR_BUF(SEK, 65);
 
     spdlog::info("Generating backup key. Will be stored in backup_key.txt ... ");
 
diff --git a/SGXWalletServer.cpp b/SGXWalletServer.cpp
index a1d9375..fd5bd93 100644
--- a/SGXWalletServer.cpp
+++ b/SGXWalletServer.cpp
@@ -561,11 +561,11 @@ Json::Value SGXWalletServer::calculateAllBLSPublicKeysImpl(const Json::Value& pu
             public_shares[i] = publicShares[i].asString();
         }
 
         vector<string> public_keys = calculateAllBlsPublicKeys(public_shares);
 
-        if (public_keys.size() != n) {
+        if (public_keys.size() != (uint64_t)n) {
             throw SGXException(UNKNOWN_ERROR, "");
         }
 
         for (int i = 0; i < n; ++i) {
             result["publicKeys"][i] = public_keys[i];
diff --git a/secure_enclave/AESUtils.c b/secure_enclave/AESUtils.c
index 3f2b121..ef70ab1 100644
--- a/secure_enclave/AESUtils.c
+++ b/secure_enclave/AESUtils.c
@@ -25,16 +25,28 @@
 #include "sgx_trts.h"
 #include "sgx_tcrypto.h"
 #include "stdlib.h"
 #include <string.h>
 
+
 #include "AESUtils.h"
 
 sgx_aes_gcm_128bit_key_t AES_key;
 sgx_aes_gcm_128bit_key_t AES_DH_key;
 
-int AES_encrypt(char *message, uint8_t *encr_message, uint64_t encrLen) {
+
+#define SAFE_CHAR_BUF(__X__, __Y__)  ;char __X__ [ __Y__ ]; memset(__X__, 0, __Y__);
+
+int AES_encrypt(char *message, uint8_t *encr_message, uint64_t encrBufLen, unsigned  char type,
+                unsigned char decryptable, uint64_t* resultLen) {
+
+
+
+    if (!type) {
+        LOG_ERROR("Null type in AES_encrypt");
+        return -1;
+    }
 
     if (!message) {
         LOG_ERROR("Null message in AES_encrypt");
         return -1;
     }
@@ -44,23 +56,35 @@ int AES_encrypt(char *message, uint8_t *encr_message, uint64_t encrLen) {
         return -2;
     }
 
     uint64_t len = strlen(message) + 1;
 
-    if (len + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE > encrLen ) {
+    if (2 + len + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE > encrBufLen ) {
         LOG_ERROR("Output buffer too small");
         return -3;
     }
 
+    SAFE_CHAR_BUF(fullMessage, len + 2);
+
+    fullMessage[0] = type;
+    fullMessage[1] = decryptable;
+
+    strncpy(fullMessage + 2, message, len );
+
+    len = len + 2;
+    message = fullMessage;
+
     sgx_read_rand(encr_message + SGX_AESGCM_MAC_SIZE, SGX_AESGCM_IV_SIZE);
 
-    sgx_status_t status = sgx_rijndael128GCM_encrypt(&AES_key, (uint8_t*)message, strlen(message),
+    sgx_status_t status = sgx_rijndael128GCM_encrypt(&AES_key, (uint8_t*)message, len,
                                                      encr_message + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE,
                                                      encr_message + SGX_AESGCM_MAC_SIZE, SGX_AESGCM_IV_SIZE,
                                                      NULL, 0,
                                                      (sgx_aes_gcm_128bit_tag_t *) encr_message);
 
+    *resultLen = len + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE;
+
     return status;
 }
 
 int AES_decrypt(uint8_t *encr_message, uint64_t length, char *message, uint64_t msgLen) {
 
@@ -94,82 +118,22 @@ int AES_decrypt(uint8_t *encr_message, uint64_t length, char *message, uint64_t
                                                    (unsigned char*) message,
                                                    encr_message + SGX_AESGCM_MAC_SIZE, SGX_AESGCM_IV_SIZE,
                                                    NULL, 0,
                                                    (sgx_aes_gcm_128bit_tag_t *)encr_message);
 
-  return status;
-}
-
-
-
-
-int AES_encrypt_DH(char *message, uint8_t *encr_message, uint64_t encrLen) {
-
-    if (!message) {
-        LOG_ERROR("Null message in AES_encrypt_DH");
-        return -1;
-    }
-
-    if (!encr_message) {
-        LOG_ERROR("Null encr message in AES_encrypt_DH");
-        return -2;
-    }
-
-    uint64_t len = strlen(message) + 1;
-
-    if (len + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE > encrLen ) {
-        LOG_ERROR("Output buffer too small");
-        return -3;
-    }
-
-    sgx_read_rand(encr_message + SGX_AESGCM_MAC_SIZE, SGX_AESGCM_IV_SIZE);
-
-    sgx_status_t status = sgx_rijndael128GCM_encrypt(&AES_DH_key, (uint8_t*)message, strlen(message),
-                                                     encr_message + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE,
-                                                     encr_message + SGX_AESGCM_MAC_SIZE, SGX_AESGCM_IV_SIZE,
-                                                     NULL, 0,
-                                                     (sgx_aes_gcm_128bit_tag_t *) encr_message);
+  for (int i = 2; i < strlen(message) + 1; i++) {
+      message[i - 2 ] = message[i];
+  }
 
-    return status;
+  return status;
 }
 
-int AES_decrypt_DH(uint8_t *encr_message, uint64_t length, char *message, uint64_t msgLen) {
 
-    if (!message) {
-        LOG_ERROR("Null message in AES_encrypt_DH");
-        return -1;
-    }
-
-    if (!encr_message) {
-        LOG_ERROR("Null encr message in AES_encrypt_DH");
-        return -2;
-    }
-
-
-    if (length < SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE) {
-        LOG_ERROR("length < SGX_AESGCM_MAC_SIZE - SGX_AESGCM_IV_SIZE");
-        return -1;
-    }
 
 
 
-    uint64_t len = length - SGX_AESGCM_MAC_SIZE - SGX_AESGCM_IV_SIZE;
 
-    if (msgLen < len) {
-        LOG_ERROR("Output buffer not large enough");
-        return -2;
-    }
-
-    sgx_status_t status = sgx_rijndael128GCM_decrypt(&AES_DH_key,
-                                                     encr_message + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE, len,
-                                                     (unsigned char*) message,
-                                                     encr_message + SGX_AESGCM_MAC_SIZE, SGX_AESGCM_IV_SIZE,
-                                                     NULL, 0,
-                                                     (sgx_aes_gcm_128bit_tag_t *)encr_message);
-
-    return status;
-}
 
 
 
 
 
diff --git a/secure_enclave/AESUtils.h b/secure_enclave/AESUtils.h
index e72e036..f897ba9 100644
--- a/secure_enclave/AESUtils.h
+++ b/secure_enclave/AESUtils.h
@@ -25,15 +25,24 @@
 #define SGXD_AESUTILS_H
 
 extern sgx_aes_gcm_128bit_key_t AES_key;
 extern sgx_aes_gcm_128bit_key_t AES_DH_key;
 
-int AES_encrypt(char *message, uint8_t *encr_message, uint64_t encrLen);
+int AES_encrypt(char *message, uint8_t *encr_message, uint64_t encrLen,
+                unsigned char type, unsigned char decryptable, uint64_t* resultLen);
 int AES_decrypt(uint8_t *encr_message, uint64_t length, char *message, uint64_t msgLen) ;
 
 int AES_encrypt_DH(char *message, uint8_t *encr_message, uint64_t encrLen);
 int AES_decrypt_DH(uint8_t *encr_message, uint64_t length, char *message, uint64_t msgLen) ;
 
 void derive_DH_Key();
 
+#define ECDSA '1'
+#define BLS '2'
+#define DKG '3'
+
+#define DECRYPTABLE '1'
+#define NON_DECRYPTABLE '2'
+
+
 
 #endif //SGXD_AESUTILS_H
diff --git a/secure_enclave/secure_enclave.c b/secure_enclave/secure_enclave.c
index 3783565..fe2ec9c 100644
--- a/secure_enclave/secure_enclave.c
+++ b/secure_enclave/secure_enclave.c
@@ -120,11 +120,11 @@ unsigned char *globalRandom = NULL;
         LOG_ERROR(__FUNCTION__); \
         LOG_ERROR("This function shouldnt be called twice. Aborting!"); \
         abort(); \
     } else {called = true;};
 
-void trustedEnclaveInit(uint32_t _logLevel) {
+void trustedEnclaveInit(uint64_t _logLevel) {
     CALL_ONCE
     LOG_INFO(__FUNCTION__);
 
     globalLogLevel_ = _logLevel;
 
@@ -230,11 +230,11 @@ void get_global_random(unsigned char *_randBuff, uint64_t _size) {
     memcpy(_randBuff, globalRandom, _size);
 }
 
 
 void sealHexSEK(int *errStatus, char *errString,
-                        uint8_t *encrypted_sek, uint32_t *enc_len, char *sek_hex) {
+                        uint8_t *encrypted_sek, uint64_t *enc_len, char *sek_hex) {
     CALL_ONCE
     LOG_INFO(__FUNCTION__);
     INIT_ERROR_STATE
 
     CHECK_STATE(encrypted_sek);
@@ -253,19 +253,19 @@ void sealHexSEK(int *errStatus, char *errString,
 
     sgx_status_t status = sgx_seal_data_ex(SGX_KEYPOLICY_MRENCLAVE, attribute_mask, misc, 0, NULL, plaintextLen, (uint8_t *) sek_hex, sealedLen,
                                            (sgx_sealed_data_t *) encrypted_sek);
     CHECK_STATUS("seal SEK failed after SEK generation");
 
-    uint32_t encrypt_text_length = sgx_get_encrypt_txt_len((const sgx_sealed_data_t *)encrypted_sek);
+    uint64_t encrypt_text_length = sgx_get_encrypt_txt_len((const sgx_sealed_data_t *)encrypted_sek);
 
     CHECK_STATE(encrypt_text_length = plaintextLen);
 
 
     SAFE_CHAR_BUF(unsealedKey, BUF_LEN);
     uint32_t decLen = BUF_LEN;
 
-    uint32_t add_text_length = sgx_get_add_mac_txt_len((const sgx_sealed_data_t *)encrypted_sek);
+    uint64_t add_text_length = sgx_get_add_mac_txt_len((const sgx_sealed_data_t *)encrypted_sek);
     CHECK_STATE(add_text_length == 0);
     CHECK_STATE(sgx_is_within_enclave(encrypted_sek,sizeof(sgx_sealed_data_t)));
     status = sgx_unseal_data((const sgx_sealed_data_t *)encrypted_sek, NULL, NULL,
                              (uint8_t *) unsealedKey, &decLen );
 
@@ -277,11 +277,11 @@ void sealHexSEK(int *errStatus, char *errString,
     LOG_INFO(__FUNCTION__ );
     LOG_INFO("SGX call completed");
 }
 
 void trustedGenerateSEK(int *errStatus, char *errString,
-                        uint8_t *encrypted_sek, uint32_t *enc_len, char *sek_hex) {
+                        uint8_t *encrypted_sek, uint64_t *enc_len, char *sek_hex) {
     CALL_ONCE
     LOG_INFO(__FUNCTION__);
     INIT_ERROR_STATE
 
 
@@ -339,11 +339,11 @@ void trustedSetSEK(int *errStatus, char *errString, uint8_t *encrypted_sek) {
     LOG_INFO(__FUNCTION__ );
     LOG_INFO("SGX call completed");
 }
 
 void trustedSetSEK_backup(int *errStatus, char *errString,
-                          uint8_t *encrypted_sek, uint32_t *enc_len, const char *sek_hex) {
+                          uint8_t *encrypted_sek, uint64_t *enc_len, const char *sek_hex) {
     CALL_ONCE
     LOG_INFO(__FUNCTION__);
     INIT_ERROR_STATE
 
     CHECK_STATE(encrypted_sek);
@@ -368,11 +368,11 @@ void trustedSetSEK_backup(int *errStatus, char *errString,
 }
 
 
 
 void trustedGenerateEcdsaKeyAES(int *errStatus, char *errString,
-                                uint8_t *encryptedPrivateKey, uint32_t *enc_len, char *pub_key_x, char *pub_key_y) {
+                                uint8_t *encryptedPrivateKey, uint64_t *enc_len, char *pub_key_x, char *pub_key_y) {
     LOG_INFO(__FUNCTION__);
     INIT_ERROR_STATE
 
     CHECK_STATE(encryptedPrivateKey);
     CHECK_STATE(pub_key_x);
@@ -408,26 +408,25 @@ void trustedGenerateEcdsaKeyAES(int *errStatus, char *errString,
     for (int i = 0; i < n_zeroes; i++) {
         pub_key_y[i] = '0';
     }
     strncpy(pub_key_y + n_zeroes, arr_y, 1024 - n_zeroes);
 
-    SAFE_CHAR_BUF(skey_str, ECDSA_SKEY_LEN);SAFE_CHAR_BUF(arr_skey_str, mpz_sizeinbase(skey, ECDSA_SKEY_BASE) + 2);
+    SAFE_CHAR_BUF(skey_str, BUF_LEN);
+    SAFE_CHAR_BUF(arr_skey_str, mpz_sizeinbase(skey, ECDSA_SKEY_BASE) + 2);
     mpz_get_str(arr_skey_str, ECDSA_SKEY_BASE, skey);
     n_zeroes = 64 - strlen(arr_skey_str);
     for (int i = 0; i < n_zeroes; i++) {
         skey_str[i] = '0';
     }
     strncpy(skey_str + n_zeroes, arr_skey_str, 65 - n_zeroes);
-    skey_str[ECDSA_SKEY_LEN - 1] = 0;
     snprintf(errString, BUF_LEN, "skey len is %d\n", (int) strlen(skey_str));
 
-    int status = AES_encrypt((char *) skey_str, encryptedPrivateKey, BUF_LEN);
+    int status = AES_encrypt((char *) skey_str, encryptedPrivateKey, BUF_LEN,
+                             ECDSA, NON_DECRYPTABLE, enc_len);
     CHECK_STATUS("ecdsa private key encryption failed");
 
-    *enc_len = strlen(skey_str) + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE;
-
-    status = AES_decrypt(encryptedPrivateKey, *enc_len, skey_str, ECDSA_SKEY_LEN);
+    status = AES_decrypt(encryptedPrivateKey, *enc_len, skey_str, BUF_LEN);
 
     CHECK_STATUS2("ecdsa private key decr failed with status %d");
 
     SET_SUCCESS
     clean:
@@ -437,15 +436,15 @@ void trustedGenerateEcdsaKeyAES(int *errStatus, char *errString,
     LOG_INFO(__FUNCTION__ );
     LOG_INFO("SGX call completed");
 }
 
 void trustedGetPublicEcdsaKeyAES(int *errStatus, char *errString,
-                                 uint8_t *encryptedPrivateKey, uint32_t enc_len, char *pub_key_x, char *pub_key_y) {
+                                 uint8_t *encryptedPrivateKey, uint64_t enc_len, char *pub_key_x, char *pub_key_y) {
     LOG_DEBUG(__FUNCTION__);
     INIT_ERROR_STATE
 
-    SAFE_CHAR_BUF(skey, ECDSA_SKEY_LEN);
+    SAFE_CHAR_BUF(skey, BUF_LEN);
 
     mpz_t privateKeyMpz;
     mpz_init(privateKeyMpz);
     point pKey = point_init();
 
@@ -453,11 +452,11 @@ void trustedGetPublicEcdsaKeyAES(int *errStatus, char *errString,
 
     CHECK_STATE(encryptedPrivateKey);
     CHECK_STATE(pub_key_x);
     CHECK_STATE(pub_key_y);
 
-    int status = AES_decrypt(encryptedPrivateKey, enc_len, skey, ECDSA_SKEY_LEN);
+    int status = AES_decrypt(encryptedPrivateKey, enc_len, skey, BUF_LEN);
     CHECK_STATUS2("AES_decrypt failed with status %d");
 
     skey[enc_len - SGX_AESGCM_MAC_SIZE - SGX_AESGCM_IV_SIZE] = '\0';
     strncpy(errString, skey, 1024);
 
@@ -513,30 +512,30 @@ void trustedGetPublicEcdsaKeyAES(int *errStatus, char *errString,
 
 }
 
 static uint64_t sigCounter = 0;
 
-void trustedEcdsaSignAES(int *errStatus, char *errString, uint8_t *encryptedPrivateKey, uint32_t enc_len,
+void trustedEcdsaSignAES(int *errStatus, char *errString, uint8_t *encryptedPrivateKey, uint64_t enc_len,
                          const char *hash, char *sigR, char *sigS, uint8_t *sig_v, int base) {
     LOG_DEBUG(__FUNCTION__);
 
     INIT_ERROR_STATE
 
     CHECK_STATE(encryptedPrivateKey);
     CHECK_STATE(hash);
     CHECK_STATE(sigR);
     CHECK_STATE(sigS);
 
-    SAFE_CHAR_BUF(skey, ECDSA_SKEY_LEN);
+    SAFE_CHAR_BUF(skey, BUF_LEN);
 
     mpz_t privateKeyMpz;
     mpz_init(privateKeyMpz);
     mpz_t msgMpz;
     mpz_init(msgMpz);
     signature sign = signature_init();
 
-    int status = AES_decrypt(encryptedPrivateKey, enc_len, skey, ECDSA_SKEY_LEN);
+    int status = AES_decrypt(encryptedPrivateKey, enc_len, skey, BUF_LEN);
 
     CHECK_STATUS2("aes decrypt failed with status %d");
 
     skey[enc_len - SGX_AESGCM_MAC_SIZE - SGX_AESGCM_IV_SIZE] = '\0';
 
@@ -598,21 +597,21 @@ void trustedEcdsaSignAES(int *errStatus, char *errString, uint8_t *encryptedPriv
     LOG_DEBUG("SGX call completed");
 }
 
 
 void trustedDecryptKeyAES(int *errStatus, char *errString, uint8_t *encryptedPrivateKey,
-                          uint32_t enc_len, char *key) {
+                          uint64_t enc_len, char *key) {
 
     LOG_DEBUG(__FUNCTION__);
     INIT_ERROR_STATE
 
     CHECK_STATE(encryptedPrivateKey);
     CHECK_STATE(key);
 
     *errStatus = -9;
 
-    int status = AES_decrypt_DH(encryptedPrivateKey, enc_len, key, 3072);
+    int status = AES_decrypt(encryptedPrivateKey, enc_len, key, 3072);
 
     if (status != 0) {
         *errStatus = status;
         snprintf(errString, BUF_LEN, "aes decrypt failed with status %d", status);
         LOG_ERROR(errString);
@@ -634,30 +633,29 @@ void trustedDecryptKeyAES(int *errStatus, char *errString, uint8_t *encryptedPri
     ;
 }
 
 
 void trustedEncryptKeyAES(int *errStatus, char *errString, const char *key,
-                          uint8_t *encryptedPrivateKey, uint32_t *enc_len) {
+                          uint8_t *encryptedPrivateKey, uint64_t *enc_len) {
     LOG_INFO(__FUNCTION__);
 
     *errString = 0;
     *errStatus = UNKNOWN_ERROR;
 
     CHECK_STATE(key);
     CHECK_STATE(encryptedPrivateKey);
 
     *errStatus = UNKNOWN_ERROR;
 
-    int status = AES_encrypt_DH((char *)key, encryptedPrivateKey, BUF_LEN);
+    int status = AES_encrypt((char *)key, encryptedPrivateKey, BUF_LEN,
+                             DKG, DECRYPTABLE, enc_len);
 
     CHECK_STATUS2("AES encrypt failed with status %d");
 
-    *enc_len = strlen(key) + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE;
-
     SAFE_CHAR_BUF(decryptedKey, BUF_LEN);
 
-    status = AES_decrypt_DH(encryptedPrivateKey, *enc_len, decryptedKey, BUF_LEN);
+    status = AES_decrypt(encryptedPrivateKey, *enc_len, decryptedKey, BUF_LEN);
 
     CHECK_STATUS2("trustedDecryptKey failed with status %d");
 
     uint64_t decryptedKeyLen = strnlen(decryptedKey, MAX_KEY_LENGTH);
 
@@ -669,10 +667,12 @@ void trustedEncryptKeyAES(int *errStatus, char *errString, const char *key,
 
     *errStatus = -8;
 
     if (strncmp(key, decryptedKey, MAX_KEY_LENGTH) != 0) {
         snprintf(errString, BUF_LEN, "Decrypted key does not match original key");
+        LOG_ERROR(key);
+        LOG_ERROR(decryptedKey);
         LOG_ERROR(errString);
         goto clean;
     }
 
     SET_SUCCESS
@@ -682,11 +682,11 @@ void trustedEncryptKeyAES(int *errStatus, char *errString, const char *key,
     LOG_INFO("SGX call completed");
 }
 
 
 void trustedBlsSignMessageAES(int *errStatus, char *errString, uint8_t *encryptedPrivateKey,
-                              uint32_t enc_len, char *_hashX,
+                              uint64_t enc_len, char *_hashX,
                               char *_hashY, char *signature) {
     LOG_DEBUG(__FUNCTION__);
     INIT_ERROR_STATE
 
     CHECK_STATE(encryptedPrivateKey);
@@ -724,11 +724,11 @@ void trustedBlsSignMessageAES(int *errStatus, char *errString, uint8_t *encrypte
     ;
     LOG_DEBUG("SGX call completed");
 }
 
 void
-trustedGenDkgSecretAES(int *errStatus, char *errString, uint8_t *encrypted_dkg_secret, uint32_t *enc_len, size_t _t) {
+trustedGenDkgSecretAES(int *errStatus, char *errString, uint8_t *encrypted_dkg_secret, uint64_t *enc_len, size_t _t) {
     LOG_INFO(__FUNCTION__);
     INIT_ERROR_STATE
 
     CHECK_STATE(encrypted_dkg_secret);
 
@@ -736,15 +736,16 @@ trustedGenDkgSecretAES(int *errStatus, char *errString, uint8_t *encrypted_dkg_s
 
     int status = gen_dkg_poly(dkg_secret, _t);
 
     CHECK_STATUS("gen_dkg_poly failed")
 
-    status = AES_encrypt(dkg_secret, encrypted_dkg_secret, 3 * BUF_LEN);
+    status = AES_encrypt(dkg_secret, encrypted_dkg_secret, 3 * BUF_LEN,
+                         DKG, DECRYPTABLE, enc_len);
 
     CHECK_STATUS("SGX AES encrypt DKG poly failed");
 
-    *enc_len = strlen(dkg_secret) + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE;
+
 
     SAFE_CHAR_BUF(decr_dkg_secret, DKG_BUFER_LENGTH);
 
     status = AES_decrypt(encrypted_dkg_secret, *enc_len, decr_dkg_secret,
                          DKG_BUFER_LENGTH);
@@ -766,11 +767,11 @@ trustedGenDkgSecretAES(int *errStatus, char *errString, uint8_t *encrypted_dkg_s
     LOG_INFO("SGX call completed");
 }
 
 void
 trustedDecryptDkgSecretAES(int *errStatus, char *errString, uint8_t *encrypted_dkg_secret,
-                           uint32_t enc_len,
+                           uint64_t enc_len,
                            uint8_t *decrypted_dkg_secret) {
     LOG_INFO(__FUNCTION__);
     INIT_ERROR_STATE
 
     CHECK_STATE(encrypted_dkg_secret);
@@ -788,11 +789,11 @@ trustedDecryptDkgSecretAES(int *errStatus, char *errString, uint8_t *encrypted_d
     LOG_INFO(__FUNCTION__ );
     LOG_INFO("SGX call completed");
 }
 
 
-void trustedSetEncryptedDkgPolyAES(int *errStatus, char *errString, uint8_t *encrypted_poly, uint32_t enc_len) {
+void trustedSetEncryptedDkgPolyAES(int *errStatus, char *errString, uint8_t *encrypted_poly, uint64_t enc_len) {
     LOG_INFO(__FUNCTION__);
     INIT_ERROR_STATE
 
     CHECK_STATE(encrypted_poly);
 
@@ -808,59 +809,59 @@ void trustedSetEncryptedDkgPolyAES(int *errStatus, char *errString, uint8_t *enc
     ;
     LOG_INFO(__FUNCTION__ );
     LOG_INFO("SGX call completed");
 }
 
-void trustedGetEncryptedSecretShareAES(int *errStatus, char *errString, uint8_t *encrypted_skey, uint32_t *dec_len,
+void trustedGetEncryptedSecretShareAES(int *errStatus, char *errString, uint8_t *encrypted_skey, uint64_t *dec_len,
                                        char *result_str, char *s_shareG2, char *pub_keyB, uint8_t _t, uint8_t _n,
                                        uint8_t ind) {
 
     LOG_INFO(__FUNCTION__);
     INIT_ERROR_STATE
 
-    uint32_t enc_len;
+    uint64_t enc_len;
     int status;
 
     CHECK_STATE(encrypted_skey);
     CHECK_STATE(result_str);
     CHECK_STATE(s_shareG2);
     CHECK_STATE(pub_keyB);
 
     LOG_DEBUG(__FUNCTION__);
 
-    SAFE_CHAR_BUF(skey, ECDSA_SKEY_LEN);
+    SAFE_CHAR_BUF(skey, BUF_LEN);
 
     SAFE_CHAR_BUF(pub_key_x, BUF_LEN);SAFE_CHAR_BUF(pub_key_y, BUF_LEN);
 
     trustedGenerateEcdsaKeyAES(&status, errString, encrypted_skey, &enc_len, pub_key_x, pub_key_y);
 
     CHECK_STATUS("trustedGenerateEcdsaKeyAES failed");
 
-    status = AES_decrypt(encrypted_skey, enc_len, skey, ECDSA_SKEY_LEN);
+    status = AES_decrypt(encrypted_skey, enc_len, skey, BUF_LEN);
 
     skey[ECDSA_SKEY_LEN - 1] = 0;
 
     CHECK_STATUS2("AES_decrypt failed (in trustedGetEncryptedSecretShareAES) with status %d");
 
     *dec_len = enc_len;
 
-    SAFE_CHAR_BUF(common_key, ECDSA_SKEY_LEN);
+    SAFE_CHAR_BUF(common_key, BUF_LEN);
 
     status = gen_session_key(skey, pub_keyB, common_key);
 
     CHECK_STATUS("gen_session_key failed")
 
-    SAFE_CHAR_BUF(s_share, ECDSA_SKEY_LEN);
+    SAFE_CHAR_BUF(s_share, BUF_LEN);
 
     status = calc_secret_share(getThreadLocalDecryptedDkgPoly(), s_share, _t, _n, ind);
     CHECK_STATUS("calc secret share failed")
 
 
     status = calc_secret_shareG2(s_share, s_shareG2);
     CHECK_STATUS("invalid decr secret share");
 
-    SAFE_CHAR_BUF(cypher, ECDSA_SKEY_LEN);
+    SAFE_CHAR_BUF(cypher, BUF_LEN);
     status=xor_encrypt(common_key, s_share, cypher);
 
     CHECK_STATUS("xor_encrypt failed")
 
     strncpy(result_str, cypher, strlen(cypher));
@@ -873,11 +874,11 @@ void trustedGetEncryptedSecretShareAES(int *errStatus, char *errString, uint8_t
     ;
     LOG_INFO(__FUNCTION__ );
     LOG_INFO("SGX call completed");
 }
 
-void trustedGetPublicSharesAES(int *errStatus, char *errString, uint8_t *encrypted_dkg_secret, uint32_t enc_len,
+void trustedGetPublicSharesAES(int *errStatus, char *errString, uint8_t *encrypted_dkg_secret, uint64_t enc_len,
                                char *public_shares,
                                unsigned _t, unsigned _n) {
     LOG_INFO(__FUNCTION__);
 
     INIT_ERROR_STATE
@@ -911,30 +912,30 @@ void trustedDkgVerifyAES(int *errStatus, char *errString, const char *public_sha
 
     CHECK_STATE(public_shares);
     CHECK_STATE(s_share);
     CHECK_STATE(encryptedPrivateKey);
 
-    SAFE_CHAR_BUF(skey, ECDSA_SKEY_LEN);
+    SAFE_CHAR_BUF(skey,BUF_LEN);
 
     mpz_t s;
     mpz_init(s);
 
-    int status = AES_decrypt(encryptedPrivateKey, enc_len, skey, ECDSA_SKEY_LEN);
+    int status = AES_decrypt(encryptedPrivateKey, enc_len, skey, BUF_LEN);
 
     CHECK_STATUS2("AES_decrypt failed (in trustedDkgVerifyAES) with status %d");
 
-    SAFE_CHAR_BUF(encr_sshare, ECDSA_SKEY_LEN);
+    SAFE_CHAR_BUF(encr_sshare, BUF_LEN);
 
     strncpy(encr_sshare, s_share, ECDSA_SKEY_LEN - 1);
 
-    SAFE_CHAR_BUF(common_key, ECDSA_SKEY_LEN);
+    SAFE_CHAR_BUF(common_key, BUF_LEN);
 
     status = session_key_recover(skey, s_share, common_key);
 
     CHECK_STATUS("session_key_recover failed");
 
-    SAFE_CHAR_BUF(decr_sshare, ECDSA_SKEY_LEN);
+    SAFE_CHAR_BUF(decr_sshare, BUF_LEN);
 
     status=xor_decrypt(common_key, encr_sshare, decr_sshare);
 
     CHECK_STATUS("xor_decrypt failed")
 
@@ -952,21 +953,21 @@ void trustedDkgVerifyAES(int *errStatus, char *errString, const char *public_sha
     LOG_INFO("SGX call completed");
 }
 
 void trustedCreateBlsKeyAES(int *errStatus, char *errString, const char *s_shares,
                             uint8_t *encryptedPrivateKey, uint64_t key_len, uint8_t *encr_bls_key,
-                            uint32_t *enc_bls_key_len) {
+                            uint64_t *enc_bls_key_len) {
 
     LOG_INFO(__FUNCTION__);
 
     INIT_ERROR_STATE
 
     CHECK_STATE(s_shares);
     CHECK_STATE(encryptedPrivateKey);
     CHECK_STATE(encr_bls_key);
 
-    SAFE_CHAR_BUF(skey, ECDSA_SKEY_LEN);
+    SAFE_CHAR_BUF(skey, BUF_LEN);
 
     mpz_t sum;
     mpz_init(sum);
     mpz_set_ui(sum, 0);
 
@@ -976,11 +977,11 @@ void trustedCreateBlsKeyAES(int *errStatus, char *errString, const char *s_share
 
     mpz_t bls_key;
     mpz_init(bls_key);
 
 
-    int status = AES_decrypt(encryptedPrivateKey, key_len, skey, ECDSA_SKEY_LEN);
+    int status = AES_decrypt(encryptedPrivateKey, key_len, skey, BUF_LEN);
     CHECK_STATUS2("aes decrypt failed with status %d");
 
     skey[ECDSA_SKEY_LEN - 1] = 0;
 
     int num_shares = strlen(s_shares) / 192;
@@ -1036,16 +1037,14 @@ void trustedCreateBlsKeyAES(int *errStatus, char *errString, const char *s_share
         key_share[i] = '0';
     }
     strncpy(key_share + n_zeroes, arr_skey_str, 65 - n_zeroes);
     key_share[BLS_KEY_LENGTH - 1] = 0;
 
-    status = AES_encrypt(key_share, encr_bls_key, BUF_LEN);
+    status = AES_encrypt(key_share, encr_bls_key, BUF_LEN, BLS, NON_DECRYPTABLE, enc_bls_key_len);
 
     CHECK_STATUS2("aes encrypt bls private key failed with status %d ");
 
-    *enc_bls_key_len = strlen(key_share) + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE;
-
     SET_SUCCESS
     clean:
 
     mpz_clear(bls_key);
     mpz_clear(sum);
@@ -1062,13 +1061,13 @@ trustedGetBlsPubKeyAES(int *errStatus, char *errString, uint8_t *encryptedPrivat
     INIT_ERROR_STATE
 
     CHECK_STATE(bls_pub_key);
     CHECK_STATE(encryptedPrivateKey);
 
-    SAFE_CHAR_BUF(skey_hex, ECDSA_SKEY_LEN);
+    SAFE_CHAR_BUF(skey_hex, BUF_LEN);
 
-    int status = AES_decrypt(encryptedPrivateKey, key_len, skey_hex, ECDSA_SKEY_LEN);
+    int status = AES_decrypt(encryptedPrivateKey, key_len, skey_hex, BUF_LEN);
 
     CHECK_STATUS2("AES decrypt failed %d");
 
     skey_hex[ECDSA_SKEY_LEN - 1] = 0;
 
diff --git a/secure_enclave/secure_enclave.edl b/secure_enclave/secure_enclave.edl
index b5b5783..6ac8c7a 100644
--- a/secure_enclave/secure_enclave.edl
+++ b/secure_enclave/secure_enclave.edl
@@ -9,18 +9,18 @@
 enclave {
 
 	trusted {
 		include "sgx_tgmp.h"
 
-		public void trustedEnclaveInit(uint32_t _logLevel);
+		public void trustedEnclaveInit(uint64_t _logLevel);
 
 
         public void trustedGenerateSEK(
                                 [out] int *errStatus,
                                 [out, count = SMALL_BUF_SIZE] char *err_string,
                                 [out, count = SMALL_BUF_SIZE] uint8_t *encrypted_SEK,
-                                [out] uint32_t *enc_len,
+                                [out] uint64_t *enc_len,
                                 [out, count = 65] char* hex_SEK);
 
         public void trustedSetSEK(
                                 [out] int *errStatus,
                                 [out, count = SMALL_BUF_SIZE] char *err_string,
@@ -28,34 +28,34 @@ enclave {
 
         public void trustedSetSEK_backup(
                                 [out] int *errStatus,
                                 [out, count = SMALL_BUF_SIZE] char *err_string,
                                 [out, count = SMALL_BUF_SIZE] uint8_t *encrypted_SEK,
-                                [out] uint32_t *enc_len,
+                                [out] uint64_t *enc_len,
                                 [in, string] const char* SEK_hex);
 
         public void trustedGenerateEcdsaKeyAES (
                                 [out] int *errStatus,
                                 [out, count = SMALL_BUF_SIZE] char* err_string,
                                 [out, count = SMALL_BUF_SIZE] uint8_t* encrypted_key,
-                                [out] uint32_t *enc_len,
+                                [out] uint64_t *enc_len,
                                 [out, count = SMALL_BUF_SIZE] char * pub_key_x,
                                 [out, count = SMALL_BUF_SIZE] char * pub_key_y);
 
         public void trustedGetPublicEcdsaKeyAES(
                                 [out] int *errStatus,
                                 [out, count = SMALL_BUF_SIZE] char* err_string,
                                 [in, count = SMALL_BUF_SIZE] uint8_t* encrypted_key,
-                                uint32_t dec_len,
+                                uint64_t dec_len,
                                 [out, count = SMALL_BUF_SIZE] char * pub_key_x,
                                 [out, count = SMALL_BUF_SIZE] char * pub_key_y);
 
         public void trustedEcdsaSignAES(
                                 [out] int *errStatus,
                                 [out, count = SMALL_BUF_SIZE] char* err_string,
                                 [in, count = SMALL_BUF_SIZE] uint8_t* encrypted_key,
-                                uint32_t enc_len,
+                                uint64_t enc_len,
                                 [in, string] const char* hash,
                                 [out, count = SMALL_BUF_SIZE] char* sig_r,
                                 [out, count = SMALL_BUF_SIZE] char* sig_s,
                                 [out] uint8_t* sig_v,
                                 int base);
@@ -63,44 +63,44 @@ enclave {
         public void trustedEncryptKeyAES (
                                 [out] int *errStatus,
                                 [out, count = SMALL_BUF_SIZE] char* err_string,
                                 [in, count = SMALL_BUF_SIZE] const char* key,
                                 [out, count = SMALL_BUF_SIZE] uint8_t* encrypted_key,
-                                [out] uint32_t *enc_len);
+                                [out] uint64_t *enc_len);
 
         public void trustedDecryptKeyAES (
                                 [out] int *errStatus,
                                 [out, count = SMALL_BUF_SIZE] char* err_string,
                                 [in, count = SMALL_BUF_SIZE] uint8_t* encrypted_key,
-                                uint32_t enc_len,
+                                uint64_t enc_len,
                                 [out, count = SMALL_BUF_SIZE] char* key );
 
         public void trustedGenDkgSecretAES (
                                 [out] int *errStatus,
                                 [out, count = SMALL_BUF_SIZE] char* err_string,
                                 [out, count = 3072] uint8_t* encrypted_dkg_secret,
-                                [out] uint32_t * enc_len, size_t _t);
+                                [out] uint64_t * enc_len, size_t _t);
 
         public void trustedDecryptDkgSecretAES (
                                 [out] int *errStatus,
                                 [out, count = SMALL_BUF_SIZE] char* err_string,
                                 [in, count = 3050] uint8_t* encrypted_dkg_secret,
-                                uint32_t enc_len,
+                                uint64_t enc_len,
                                 [out, count = 3072] uint8_t* decrypted_dkg_secret
                                 );
 
         public void trustedSetEncryptedDkgPolyAES(
                                 [out] int *errStatus,
                                 [out, count = SMALL_BUF_SIZE] char* err_string,
                                 [in, count = 3050] uint8_t* encrypted_poly,
-                                uint32_t enc_len);
+                                uint64_t enc_len);
 
         public void trustedGetEncryptedSecretShareAES(
                                 [out]int *errStatus,
                                 [out, count = SMALL_BUF_SIZE] char *err_string,
                                 [out, count = SMALL_BUF_SIZE] uint8_t *encrypted_skey,
-                                [out] uint32_t* dec_len,
+                                [out] uint64_t* dec_len,
                                 [out, count = 193] char* result_str,
                                 [out, count = 320] char* s_shareG2,
                                 [in, string] char* pub_keyB,
                                 uint8_t _t,
                                 uint8_t _n,
@@ -108,11 +108,11 @@ enclave {
 
         public void trustedGetPublicSharesAES(
                                 [out] int *errStatus,
                                 [out, count = SMALL_BUF_SIZE] char* err_string,
                                 [in, count = 3050] uint8_t* encrypted_dkg_secret,
-                                uint32_t enc_len,
+                                uint64_t enc_len,
                                 [out, count = 10000] char* public_shares,
                                 unsigned _t,
                                 unsigned _n);
 
         public void trustedDkgVerifyAES(
@@ -131,17 +131,17 @@ enclave {
                                 [out, count = SMALL_BUF_SIZE] char* err_string,
                                 [in, count = 6145] const char* s_shares,
                                 [in, count = SMALL_BUF_SIZE] uint8_t* encrypted_key,
                                 uint64_t key_len,
                                 [out, count = SMALL_BUF_SIZE] uint8_t * encr_bls_key,
-                                [out] uint32_t *enc_bls_key_len);
+                                [out] uint64_t *enc_bls_key_len);
 
         public void trustedBlsSignMessageAES (
                                 [out] int *errStatus,
                                 [out, count = TINY_BUF_SIZE] char* err_string,
                                 [in, count = TINY_BUF_SIZE] uint8_t* encrypted_key,
-                                uint32_t enc_len,
+                                uint64_t enc_len,
                                 [in, string] char* hashX ,
                                 [in, string] char* hashY,
                                 [out, count = SMALL_BUF_SIZE] char* signature);
 
         public void trustedGetBlsPubKeyAES(
diff --git a/testw.cpp b/testw.cpp
index 5b0c7cd..588aa51 100644
--- a/testw.cpp
+++ b/testw.cpp
@@ -125,11 +125,11 @@ TEST_CASE_METHOD(TestFixture, "ECDSA AES keygen and signature test", "[ecdsa-aes
     int errStatus = 0;
     vector <uint8_t> encrPrivKey(BUF_LEN, 0);
     vector<char> pubKeyX(BUF_LEN, 0);
     vector<char> pubKeyY(BUF_LEN, 0);
 
-    uint32_t encLen = 0;
+    uint64_t encLen = 0;
     PRINT_SRC_LINE
     auto status = trustedGenerateEcdsaKeyAES(eid, &errStatus, errMsg.data(), encrPrivKey.data(), &encLen,
                                              pubKeyX.data(),
                                              pubKeyY.data());
     REQUIRE(status == SGX_SUCCESS);
@@ -158,11 +158,11 @@ TEST_CASE_METHOD(TestFixture, "ECDSA AES key gen", "[ecdsa-aes-key-gen]") {
     vector<char> errMsg(BUF_LEN, 0);
     int errStatus = 0;
     vector <uint8_t> encrPrivKey(BUF_LEN, 0);
     vector<char> pubKeyX(BUF_LEN, 0);
     vector<char> pubKeyY(BUF_LEN, 0);
-    uint32_t encLen = 0;
+    uint64_t encLen = 0;
     PRINT_SRC_LINE
     auto status = trustedGenerateEcdsaKeyAES(eid, &errStatus, errMsg.data(), encrPrivKey.data(), &encLen,
                                              pubKeyX.data(),
                                              pubKeyY.data());
 
@@ -175,11 +175,12 @@ TEST_CASE_METHOD(TestFixture, "ECDSA AES get public key", "[ecdsa-aes-get-pub-ke
     int errStatus = 0;
     vector<char> errMsg(BUF_LEN, 0);
     vector <uint8_t> encPrivKey(BUF_LEN, 0);
     vector<char> pubKeyX(BUF_LEN, 0);
     vector<char> pubKeyY(BUF_LEN, 0);
-    uint32_t encLen = 0;
+
+    uint64_t encLen = 0;
 
     PRINT_SRC_LINE
     auto status = trustedGenerateEcdsaKeyAES(eid, &errStatus, errMsg.data(), encPrivKey.data(), &encLen, pubKeyX.data(),
                                              pubKeyY.data());
 
@@ -289,11 +290,11 @@ TEST_CASE_METHOD(TestFixture, "BLS key encrypt", "[bls-key-encrypt]") {
 TEST_CASE_METHOD(TestFixture, "DKG AES gen test", "[dkg-aes-gen]") {
     vector <uint8_t> encryptedDKGSecret(BUF_LEN, 0);
     vector<char> errMsg(BUF_LEN, 0);
 
     int errStatus = 0;
-    uint32_t encLen = 0;
+    uint64_t encLen = 0;
 
     PRINT_SRC_LINE
     auto status = trustedGenDkgSecretAES(eid, &errStatus, errMsg.data(), encryptedDKGSecret.data(), &encLen, 32);
     REQUIRE(status == SGX_SUCCESS);
     REQUIRE(errStatus == SGX_SUCCESS);
@@ -312,11 +313,11 @@ TEST_CASE_METHOD(TestFixture, "DKG AES gen test", "[dkg-aes-gen]") {
 TEST_CASE_METHOD(TestFixture, "DKG AES public shares test", "[dkg-aes-pub-shares]") {
     vector <uint8_t> encryptedDKGSecret(BUF_LEN, 0);
     vector<char> errMsg(BUF_LEN, 0);
 
     int errStatus = 0;
-    uint32_t encLen = 0;
+    uint64_t encLen = 0;
 
     unsigned t = 32, n = 32;
     PRINT_SRC_LINE
     auto status = trustedGenDkgSecretAES(eid, &errStatus, errMsg.data(), encryptedDKGSecret.data(), &encLen, n);
     REQUIRE(status == SGX_SUCCESS);
@@ -361,11 +362,11 @@ TEST_CASE_METHOD(TestFixture, "DKG AES public shares test", "[dkg-aes-pub-shares
 TEST_CASE_METHOD(TestFixture, "DKG AES encrypted secret shares test", "[dkg-aes-encr-sshares]") {
     vector<char> errMsg(BUF_LEN, 0);
     vector<char> result(BUF_LEN, 0);
 
     int errStatus = 0;
-    uint32_t encLen = 0;
+    uint64_t encLen = 0;
 
     vector <uint8_t> encryptedDKGSecret(BUF_LEN, 0);
     PRINT_SRC_LINE
     auto status = trustedGenDkgSecretAES(eid, &errStatus, errMsg.data(), encryptedDKGSecret.data(), &encLen, 2);
     REQUIRE(status == SGX_SUCCESS);
@@ -692,11 +693,11 @@ TEST_CASE_METHOD(TestFixture, "AES_DKG test", "[aes-dkg]") {
 }
 
 TEST_CASE_METHOD(TestFixture, "AES encrypt/decrypt", "[aes-encrypt-decrypt]") {
     int errStatus = 0;
     vector<char> errMsg(BUF_LEN, 0);
-    uint32_t encLen;
+    uint64_t encLen;
     string key = SAMPLE_AES_KEY;
     vector <uint8_t> encrypted_key(BUF_LEN, 0);
 
     PRINT_SRC_LINE
     auto status = trustedEncryptKeyAES(eid, &errStatus, errMsg.data(), key.c_str(), encrypted_key.data(), &encLen);

Vulnerable code:     for (int j = 0; j < _len; j++) {
    int len = strnlen(_hex, 2 * _max_length + 1);
    for (int i = 0; i < len / 2; i++) {
    unsigned int encryptedLen = 0;
    uint32_t enc_len = 0;

    sgx_status_t status = trustedGenDkgSecretAES(eid, &errStatus, errMsg.data(), encrypted_dkg_secret.data(), &enc_len, _t);
        uint32_t decLen;
    uint32_t enc_bls_len = 0;
    uint32_t enc_len = 0;
    uint32_t enc_len;
    uint32_t l = 0;
    uint32_t enc_len = 0;
        if (public_keys.size() != n) {
int AES_encrypt(char *message, uint8_t *encr_message, uint64_t encrLen) {
    if (len + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE > encrLen ) {
    sgx_status_t status = sgx_rijndael128GCM_encrypt(&AES_key, (uint8_t*)message, strlen(message),
  return status;
}




int AES_encrypt_DH(char *message, uint8_t *encr_message, uint64_t encrLen) {

    if (!message) {
        LOG_ERROR("Null message in AES_encrypt_DH");
        return -1;
    }

    if (!encr_message) {
        LOG_ERROR("Null encr message in AES_encrypt_DH");
        return -2;
    }

    uint64_t len = strlen(message) + 1;

    if (len + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE > encrLen ) {
        LOG_ERROR("Output buffer too small");
        return -3;
    }

    sgx_read_rand(encr_message + SGX_AESGCM_MAC_SIZE, SGX_AESGCM_IV_SIZE);

    sgx_status_t status = sgx_rijndael128GCM_encrypt(&AES_DH_key, (uint8_t*)message, strlen(message),
                                                     encr_message + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE,
                                                     encr_message + SGX_AESGCM_MAC_SIZE, SGX_AESGCM_IV_SIZE,
                                                     NULL, 0,
                                                     (sgx_aes_gcm_128bit_tag_t *) encr_message);
    return status;
int AES_decrypt_DH(uint8_t *encr_message, uint64_t length, char *message, uint64_t msgLen) {
    if (!message) {
        LOG_ERROR("Null message in AES_encrypt_DH");
        return -1;
    }

    if (!encr_message) {
        LOG_ERROR("Null encr message in AES_encrypt_DH");
        return -2;
    }


    if (length < SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE) {
        LOG_ERROR("length < SGX_AESGCM_MAC_SIZE - SGX_AESGCM_IV_SIZE");
        return -1;
    }
    uint64_t len = length - SGX_AESGCM_MAC_SIZE - SGX_AESGCM_IV_SIZE;
    if (msgLen < len) {
        LOG_ERROR("Output buffer not large enough");
        return -2;
    }

    sgx_status_t status = sgx_rijndael128GCM_decrypt(&AES_DH_key,
                                                     encr_message + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE, len,
                                                     (unsigned char*) message,
                                                     encr_message + SGX_AESGCM_MAC_SIZE, SGX_AESGCM_IV_SIZE,
                                                     NULL, 0,
                                                     (sgx_aes_gcm_128bit_tag_t *)encr_message);

    return status;
}
int AES_encrypt(char *message, uint8_t *encr_message, uint64_t encrLen);
void trustedEnclaveInit(uint32_t _logLevel) {
                        uint8_t *encrypted_sek, uint32_t *enc_len, char *sek_hex) {
    uint32_t encrypt_text_length = sgx_get_encrypt_txt_len((const sgx_sealed_data_t *)encrypted_sek);
    uint32_t add_text_length = sgx_get_add_mac_txt_len((const sgx_sealed_data_t *)encrypted_sek);
                        uint8_t *encrypted_sek, uint32_t *enc_len, char *sek_hex) {
                          uint8_t *encrypted_sek, uint32_t *enc_len, const char *sek_hex) {
                                uint8_t *encryptedPrivateKey, uint32_t *enc_len, char *pub_key_x, char *pub_key_y) {
    SAFE_CHAR_BUF(skey_str, ECDSA_SKEY_LEN);SAFE_CHAR_BUF(arr_skey_str, mpz_sizeinbase(skey, ECDSA_SKEY_BASE) + 2);
    skey_str[ECDSA_SKEY_LEN - 1] = 0;
    int status = AES_encrypt((char *) skey_str, encryptedPrivateKey, BUF_LEN);
    *enc_len = strlen(skey_str) + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE;

    status = AES_decrypt(encryptedPrivateKey, *enc_len, skey_str, ECDSA_SKEY_LEN);
                                 uint8_t *encryptedPrivateKey, uint32_t enc_len, char *pub_key_x, char *pub_key_y) {
    SAFE_CHAR_BUF(skey, ECDSA_SKEY_LEN);
    int status = AES_decrypt(encryptedPrivateKey, enc_len, skey, ECDSA_SKEY_LEN);
void trustedEcdsaSignAES(int *errStatus, char *errString, uint8_t *encryptedPrivateKey, uint32_t enc_len,
    SAFE_CHAR_BUF(skey, ECDSA_SKEY_LEN);
    int status = AES_decrypt(encryptedPrivateKey, enc_len, skey, ECDSA_SKEY_LEN);
                          uint32_t enc_len, char *key) {
    int status = AES_decrypt_DH(encryptedPrivateKey, enc_len, key, 3072);
                          uint8_t *encryptedPrivateKey, uint32_t *enc_len) {
    int status = AES_encrypt_DH((char *)key, encryptedPrivateKey, BUF_LEN);
    *enc_len = strlen(key) + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE;

    status = AES_decrypt_DH(encryptedPrivateKey, *enc_len, decryptedKey, BUF_LEN);
                              uint32_t enc_len, char *_hashX,
trustedGenDkgSecretAES(int *errStatus, char *errString, uint8_t *encrypted_dkg_secret, uint32_t *enc_len, size_t _t) {
    status = AES_encrypt(dkg_secret, encrypted_dkg_secret, 3 * BUF_LEN);
    *enc_len = strlen(dkg_secret) + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE;
                           uint32_t enc_len,
void trustedSetEncryptedDkgPolyAES(int *errStatus, char *errString, uint8_t *encrypted_poly, uint32_t enc_len) {
void trustedGetEncryptedSecretShareAES(int *errStatus, char *errString, uint8_t *encrypted_skey, uint32_t *dec_len,
    uint32_t enc_len;
    SAFE_CHAR_BUF(skey, ECDSA_SKEY_LEN);
    status = AES_decrypt(encrypted_skey, enc_len, skey, ECDSA_SKEY_LEN);
    SAFE_CHAR_BUF(common_key, ECDSA_SKEY_LEN);
    SAFE_CHAR_BUF(s_share, ECDSA_SKEY_LEN);
    SAFE_CHAR_BUF(cypher, ECDSA_SKEY_LEN);
void trustedGetPublicSharesAES(int *errStatus, char *errString, uint8_t *encrypted_dkg_secret, uint32_t enc_len,
    SAFE_CHAR_BUF(skey, ECDSA_SKEY_LEN);
    int status = AES_decrypt(encryptedPrivateKey, enc_len, skey, ECDSA_SKEY_LEN);
    SAFE_CHAR_BUF(encr_sshare, ECDSA_SKEY_LEN);
    SAFE_CHAR_BUF(common_key, ECDSA_SKEY_LEN);
    SAFE_CHAR_BUF(decr_sshare, ECDSA_SKEY_LEN);
                            uint32_t *enc_bls_key_len) {
    SAFE_CHAR_BUF(skey, ECDSA_SKEY_LEN);
    int status = AES_decrypt(encryptedPrivateKey, key_len, skey, ECDSA_SKEY_LEN);
    status = AES_encrypt(key_share, encr_bls_key, BUF_LEN);
    *enc_bls_key_len = strlen(key_share) + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE;

    SAFE_CHAR_BUF(skey_hex, ECDSA_SKEY_LEN);
    int status = AES_decrypt(encryptedPrivateKey, key_len, skey_hex, ECDSA_SKEY_LEN);
		public void trustedEnclaveInit(uint32_t _logLevel);
                                [out] uint32_t *enc_len,
                                [out] uint32_t *enc_len,
                                [out] uint32_t *enc_len,
                                uint32_t dec_len,
                                uint32_t enc_len,
                                [out] uint32_t *enc_len);
                                uint32_t enc_len,
                                [out] uint32_t * enc_len, size_t _t);
                                uint32_t enc_len,
                                uint32_t enc_len);
                                [out] uint32_t* dec_len,
                                uint32_t enc_len,
                                [out] uint32_t *enc_bls_key_len);
                                uint32_t enc_len,
    uint32_t encLen = 0;
    uint32_t encLen = 0;
    uint32_t encLen = 0;
    uint32_t encLen = 0;
    uint32_t encLen = 0;
    uint32_t encLen = 0;
    uint32_t encLen;
Fixed code:     for (uint64_t j = 0; j < _len; j++) {
    uint64_t len = strnlen(_hex, 2 * _max_length + 1);
    for (uint64_t i = 0; i < len / 2; i++) {

    uint64_t encryptedLen = 0;
    uint64_t enc_len = 0;
    sgx_status_t status = trustedGenDkgSecretAES(
            eid, &errStatus,errMsg.data(), encrypted_dkg_secret.data(), &enc_len, _t);
        uint64_t decLen;
    uint64_t enc_bls_len = 0;
    uint64_t enc_len = 0;
    uint64_t enc_len;
    uint64_t l = 0;
    uint64_t enc_len = 0;
        if (public_keys.size() != (uint64_t)n) {


#define SAFE_CHAR_BUF(__X__, __Y__)  ;char __X__ [ __Y__ ]; memset(__X__, 0, __Y__);

int AES_encrypt(char *message, uint8_t *encr_message, uint64_t encrBufLen, unsigned  char type,
                unsigned char decryptable, uint64_t* resultLen) {



    if (!type) {
        LOG_ERROR("Null type in AES_encrypt");
        return -1;
    }
    if (2 + len + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE > encrBufLen ) {
    SAFE_CHAR_BUF(fullMessage, len + 2);

    fullMessage[0] = type;
    fullMessage[1] = decryptable;

    strncpy(fullMessage + 2, message, len );

    len = len + 2;
    message = fullMessage;

    sgx_status_t status = sgx_rijndael128GCM_encrypt(&AES_key, (uint8_t*)message, len,
    *resultLen = len + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE;

  for (int i = 2; i < strlen(message) + 1; i++) {
      message[i - 2 ] = message[i];
  }
  return status;
int AES_encrypt(char *message, uint8_t *encr_message, uint64_t encrLen,
                unsigned char type, unsigned char decryptable, uint64_t* resultLen);
#define ECDSA '1'
#define BLS '2'
#define DKG '3'

#define DECRYPTABLE '1'
#define NON_DECRYPTABLE '2'


void trustedEnclaveInit(uint64_t _logLevel) {
                        uint8_t *encrypted_sek, uint64_t *enc_len, char *sek_hex) {
    uint64_t encrypt_text_length = sgx_get_encrypt_txt_len((const sgx_sealed_data_t *)encrypted_sek);
    uint64_t add_text_length = sgx_get_add_mac_txt_len((const sgx_sealed_data_t *)encrypted_sek);
                        uint8_t *encrypted_sek, uint64_t *enc_len, char *sek_hex) {
                          uint8_t *encrypted_sek, uint64_t *enc_len, const char *sek_hex) {
                                uint8_t *encryptedPrivateKey, uint64_t *enc_len, char *pub_key_x, char *pub_key_y) {
    SAFE_CHAR_BUF(skey_str, BUF_LEN);
    SAFE_CHAR_BUF(arr_skey_str, mpz_sizeinbase(skey, ECDSA_SKEY_BASE) + 2);
    int status = AES_encrypt((char *) skey_str, encryptedPrivateKey, BUF_LEN,
                             ECDSA, NON_DECRYPTABLE, enc_len);
    status = AES_decrypt(encryptedPrivateKey, *enc_len, skey_str, BUF_LEN);
                                 uint8_t *encryptedPrivateKey, uint64_t enc_len, char *pub_key_x, char *pub_key_y) {
    SAFE_CHAR_BUF(skey, BUF_LEN);
    int status = AES_decrypt(encryptedPrivateKey, enc_len, skey, BUF_LEN);
void trustedEcdsaSignAES(int *errStatus, char *errString, uint8_t *encryptedPrivateKey, uint64_t enc_len,
    SAFE_CHAR_BUF(skey, BUF_LEN);
    int status = AES_decrypt(encryptedPrivateKey, enc_len, skey, BUF_LEN);
                          uint64_t enc_len, char *key) {
    int status = AES_decrypt(encryptedPrivateKey, enc_len, key, 3072);
                          uint8_t *encryptedPrivateKey, uint64_t *enc_len) {
    int status = AES_encrypt((char *)key, encryptedPrivateKey, BUF_LEN,
                             DKG, DECRYPTABLE, enc_len);
    status = AES_decrypt(encryptedPrivateKey, *enc_len, decryptedKey, BUF_LEN);
        LOG_ERROR(key);
        LOG_ERROR(decryptedKey);
                              uint64_t enc_len, char *_hashX,
trustedGenDkgSecretAES(int *errStatus, char *errString, uint8_t *encrypted_dkg_secret, uint64_t *enc_len, size_t _t) {
    status = AES_encrypt(dkg_secret, encrypted_dkg_secret, 3 * BUF_LEN,
                         DKG, DECRYPTABLE, enc_len);

                           uint64_t enc_len,
void trustedSetEncryptedDkgPolyAES(int *errStatus, char *errString, uint8_t *encrypted_poly, uint64_t enc_len) {
void trustedGetEncryptedSecretShareAES(int *errStatus, char *errString, uint8_t *encrypted_skey, uint64_t *dec_len,
    uint64_t enc_len;
    SAFE_CHAR_BUF(skey, BUF_LEN);
    status = AES_decrypt(encrypted_skey, enc_len, skey, BUF_LEN);
    SAFE_CHAR_BUF(common_key, BUF_LEN);
    SAFE_CHAR_BUF(s_share, BUF_LEN);
    SAFE_CHAR_BUF(cypher, BUF_LEN);
void trustedGetPublicSharesAES(int *errStatus, char *errString, uint8_t *encrypted_dkg_secret, uint64_t enc_len,
    SAFE_CHAR_BUF(skey,BUF_LEN);
    int status = AES_decrypt(encryptedPrivateKey, enc_len, skey, BUF_LEN);
    SAFE_CHAR_BUF(encr_sshare, BUF_LEN);
    SAFE_CHAR_BUF(common_key, BUF_LEN);
    SAFE_CHAR_BUF(decr_sshare, BUF_LEN);
                            uint64_t *enc_bls_key_len) {
    SAFE_CHAR_BUF(skey, BUF_LEN);
    int status = AES_decrypt(encryptedPrivateKey, key_len, skey, BUF_LEN);
    status = AES_encrypt(key_share, encr_bls_key, BUF_LEN, BLS, NON_DECRYPTABLE, enc_bls_key_len);
    SAFE_CHAR_BUF(skey_hex, BUF_LEN);
    int status = AES_decrypt(encryptedPrivateKey, key_len, skey_hex, BUF_LEN);
		public void trustedEnclaveInit(uint64_t _logLevel);
                                [out] uint64_t *enc_len,
                                [out] uint64_t *enc_len,
                                [out] uint64_t *enc_len,
                                uint64_t dec_len,
                                uint64_t enc_len,
                                [out] uint64_t *enc_len);
                                uint64_t enc_len,
                                [out] uint64_t * enc_len, size_t _t);
                                uint64_t enc_len,
                                uint64_t enc_len);
                                [out] uint64_t* dec_len,
                                uint64_t enc_len,
                                [out] uint64_t *enc_bls_key_len);
                                uint64_t enc_len,
    uint64_t encLen = 0;
    uint64_t encLen = 0;

    uint64_t encLen = 0;
    uint64_t encLen = 0;
    uint64_t encLen = 0;
    uint64_t encLen = 0;
    uint64_t encLen;