Use TEE_Malloc()/TEE_Free() instead of malloc()/free() 82/297282/2
authorDongsun Lee <ds73.lee@samsung.com>
Wed, 16 Aug 2023 07:35:29 +0000 (16:35 +0900)
committerDong Sun Lee <ds73.lee@samsung.com>
Wed, 16 Aug 2023 08:26:36 +0000 (08:26 +0000)
- In some implementations, free() doesn't allow a null pointer.

Change-Id: Ic783908c644d3c35b133d6a0401b52402e79b81a

ta/src/cipher_ctx_list.c
ta/src/cmd_exec.c
ta/src/crypto_derive.c
ta/src/der_parse.c
ta/src/internal.c

index c257c92c8d228b0e9a879875c381e36844a878c2..43f828260cd92a003306448c95381ddd4541cec4 100644 (file)
@@ -25,7 +25,7 @@ static void free_node(listNode *node)
 {
     TEE_FreeOperation(node->oper);
 
-    free(node);
+    TEE_Free(node);
 }
 
 static listNode *find_end_remove_expired()
@@ -80,7 +80,7 @@ listNode *ctxl_add_last_node(TEE_OperationHandle oper, uint32_t tag_len_bits)
 
     end = find_end_remove_expired();
     listNode *newNode;
-    newNode = (listNode *) malloc(sizeof(listNode));
+    newNode = (listNode *) TEE_Malloc(sizeof(listNode), 0);
     newNode->id = (uint32_t) oper; // Use the pointer of operation.
     newNode->oper = oper;
     newNode->tag_len_bits = tag_len_bits;
index 59641d7ad432ef3544b8fa05f675a45bba4af140..395fbecfaae0dc1944bcb22e44c9e3f3a454bd70 100644 (file)
@@ -23,7 +23,6 @@
  */
 
 #include <stdlib.h>
-#include <malloc.h>
 #include <tee_internal_api.h>
 #include <km_ta_defines.h>
 #include <internal.h>
@@ -436,10 +435,10 @@ static TEE_Result KM_GenerateAsymKey(void *bufferPtr,
 clean:
        TEE_FreeTransientObject(privHndl);
        TEE_FreeTransientObject(pubHndl);
-       free(pubEncKey.data);
-       free(privEncKey.data);
-       free(pubKeyTag.data);
-       free(privKeyTag.data);
+       TEE_Free(pubEncKey.data);
+       TEE_Free(privEncKey.data);
+       TEE_Free(pubKeyTag.data);
+       TEE_Free(privKeyTag.data);
 
        return ret;
 }
@@ -463,7 +462,7 @@ static TEE_Result KM_InputDataDigest(uint32_t hash,
 
        // allocate memory
        tmpDigestSize = KM_DigestSizeFromHash(hash);
-       tmpDigest = malloc(tmpDigestSize);
+       tmpDigest = TEE_Malloc(tmpDigestSize, 0);
        if (tmpDigest == NULL) {
                LOG("Failed to allocate buffer for digest");
                ret = TEE_ERROR_OUT_OF_MEMORY;
@@ -483,7 +482,7 @@ static TEE_Result KM_InputDataDigest(uint32_t hash,
 
 clean:
        if (ret != TEE_SUCCESS)
-               free(tmpDigest);
+               TEE_Free(tmpDigest);
        TEE_FreeOperation(digestOperation);
        return ret;
 }
@@ -565,7 +564,7 @@ TEE_Result KM_ExecCmdGenerateKeyPwd(TEE_Param param[4])
        }
 
        tag_size = pwd_data.tag_len_bits / 8;
-       tag = malloc(tag_size);
+       tag = TEE_Malloc(tag_size, 0);
        if (tag == NULL) {
                LOG("Failed to allocate memory for key's tag");
                ret = TEE_ERROR_OUT_OF_MEMORY;
@@ -601,7 +600,7 @@ TEE_Result KM_ExecCmdGenerateKeyPwd(TEE_Param param[4])
 
 clean:
        TEE_FreeTransientObject(key);
-       free(tag);
+       TEE_Free(tag);
        return ret;
 }
 
@@ -721,7 +720,7 @@ static TEE_Result KM_GetSecretValue(KM_BinaryData *secret_id, KM_BinaryData* sec
                out_len = KM_MaxObjectSizeBytes(&secret_info);
        }
 
-       output.data = malloc(out_len);
+       output.data = TEE_Malloc(out_len, 0);
        if(output.data == NULL) {
                LOG("Failed to assign memory. requested size=%d", out_len);
                ret = TEE_ERROR_OUT_OF_MEMORY;
@@ -760,7 +759,7 @@ static TEE_Result KM_GetSecretValue(KM_BinaryData *secret_id, KM_BinaryData* sec
 clean:
        TEE_CloseObject(key_handle);
        if(ret != TEE_SUCCESS)
-               free(output.data);
+               TEE_Free(output.data);
 
        return ret;
 }
@@ -874,7 +873,7 @@ TEE_Result KM_ExecCmdDeriveKbkdf(TEE_Param param[4])
        }
 
        // execute KBKDF
-       derived_key.data = (unsigned char *) malloc(outlen);
+       derived_key.data = (unsigned char *) TEE_Malloc(outlen, 0);
        derived_key.data_size = outlen;
        if (derived_key.data == NULL) {
                LOG("Failed to allocate input buffer for derived_key");
@@ -925,10 +924,10 @@ TEE_Result KM_ExecCmdDeriveKbkdf(TEE_Param param[4])
        LOGD_ID("Derive KBKDF key: ", obj_id.data, obj_id.data_size);
 clean:
        TEE_CloseObject(key_handle);
-       free(key_in.data);
-       free(derived_key.data);
-       free(enc_drv_key.data);
-       free(tag.data);
+       TEE_Free(key_in.data);
+       TEE_Free(derived_key.data);
+       TEE_Free(enc_drv_key.data);
+       TEE_Free(tag.data);
 
        return ret;
 }
@@ -992,9 +991,9 @@ static TEE_Result KM_CopyEcdhPrivateAttributes(TEE_ObjectHandle dest_key,
        }
 
 clean:
-       free(data_priv.data);
-       free(data_pub_x.data);
-       free(data_pub_y.data);
+       TEE_Free(data_priv.data);
+       TEE_Free(data_pub_x.data);
+       TEE_Free(data_pub_y.data);
        return ret;
 }
 
@@ -1195,9 +1194,9 @@ TEE_Result KM_ExecCmdDeriveEcdh(TEE_Param param[4])
 clean:
        TEE_CloseObject(prv_key);
        TEE_FreeTransientObject(secret_hndl);
-       free(secret.data);
-       free(enc_secret.data);
-       free(tag.data);
+       TEE_Free(secret.data);
+       TEE_Free(enc_secret.data);
+       TEE_Free(tag.data);
        return ret;
 }
 
@@ -1266,7 +1265,7 @@ static TEE_Result KM_DoCipherWrappedKeyWithRsa(TEE_ObjectHandle wkey,
                goto clean;
        }
 
-       out = malloc(out_size);
+       out = TEE_Malloc(out_size, 0);
        if (out == NULL) {
                LOG("Failed to allocate output buffer");
                ret = TEE_ERROR_OUT_OF_MEMORY;
@@ -1291,7 +1290,7 @@ static TEE_Result KM_DoCipherWrappedKeyWithRsa(TEE_ObjectHandle wkey,
 clean:
        TEE_FreeOperation(operation);
        if (ret != TEE_SUCCESS)
-               free(out);
+               TEE_Free(out);
        return ret;
 }
 
@@ -1325,7 +1324,7 @@ static TEE_Result KM_DoCipherWrappedKeyWithAesGcm(TEE_ObjectHandle wkey,
        else
                out_buff_size -= tag_len;
 
-       out = malloc(out_buff_size);
+       out = TEE_Malloc(out_buff_size, 0);
        if (out == NULL) {
                LOG("Failed to allocate output buffer");
                ret = TEE_ERROR_OUT_OF_MEMORY;
@@ -1382,7 +1381,7 @@ static TEE_Result KM_DoCipherWrappedKeyWithAesGcm(TEE_ObjectHandle wkey,
 clean:
        TEE_FreeOperation(operation);
        if (ret != TEE_SUCCESS)
-               free(out);
+               TEE_Free(out);
        return ret;
 }
 
@@ -1410,7 +1409,7 @@ static TEE_Result KM_DoCipherWrappedKeyWithAes(TEE_ObjectHandle wkey,
                out_size = KM_CalculatePaddedSize(KM_PADDING_PKCS7, AES_BLOCK_SIZE, input->data_size);
 
                in_padded_size = out_size;
-               in_padded = malloc(in_padded_size);
+               in_padded = TEE_Malloc(in_padded_size, 0);
                if (in_padded == NULL) {
                        LOG("Failed to allocate input buffer for padded data");
                        ret = TEE_ERROR_OUT_OF_MEMORY;
@@ -1425,7 +1424,7 @@ static TEE_Result KM_DoCipherWrappedKeyWithAes(TEE_ObjectHandle wkey,
                }
        }
 
-       out = malloc(out_size);
+       out = TEE_Malloc(out_size, 0);
        if (out == NULL) {
                LOG("Failed to allocate output buffer");
                ret = TEE_ERROR_OUT_OF_MEMORY;
@@ -1468,9 +1467,9 @@ static TEE_Result KM_DoCipherWrappedKeyWithAes(TEE_ObjectHandle wkey,
 
 clean:
        TEE_FreeOperation(operation);
-       free(in_padded);
+       TEE_Free(in_padded);
        if (ret != TEE_SUCCESS)
-               free(out);
+               TEE_Free(out);
        return ret;
 }
 
@@ -1623,9 +1622,9 @@ TEE_Result KM_ExecCmdImportWrappedKey(TEE_Param param[4])
 clean:
        TEE_CloseObject(wkey);
        TEE_CloseObject(key);
-       free(tag.data);
-       free(ekey_data.data);
-       free(ekey_data_enc.data);
+       TEE_Free(tag.data);
+       TEE_Free(ekey_data.data);
+       TEE_Free(ekey_data_enc.data);
        return ret;
 }
 
@@ -1744,8 +1743,8 @@ TEE_Result KM_ExecCmdExportWrappedKey(TEE_Param param[4])
 clean:
        TEE_CloseObject(wkey);
        TEE_CloseObject(ktw_key);
-       free(wrapped_key.data);
-       free(ktw_data.data);
+       TEE_Free(wrapped_key.data);
+       TEE_Free(ktw_data.data);
        return ret;
 }
 
@@ -1811,7 +1810,7 @@ TEE_Result KM_ExecCmdSymmetric(uint32_t commandID, TEE_Param param[4])
                out_size = KM_CalculatePaddedSize(KM_PADDING_PKCS7, AES_BLOCK_SIZE, input_data.data_size);
 
                in_padded_size = out_size;
-               in_padded = malloc(in_padded_size);
+               in_padded = TEE_Malloc(in_padded_size, 0);
                if (in_padded == NULL) {
                        LOG("Failed to allocate input buffer for padded data");
                        ret = TEE_ERROR_OUT_OF_MEMORY;
@@ -1826,7 +1825,7 @@ TEE_Result KM_ExecCmdSymmetric(uint32_t commandID, TEE_Param param[4])
                }
        }
 
-       out = malloc(out_size);
+       out = TEE_Malloc(out_size, 0);
        if (out == NULL) {
                LOG("Failed to allocate output buffer");
                ret = TEE_ERROR_OUT_OF_MEMORY;
@@ -1883,8 +1882,8 @@ TEE_Result KM_ExecCmdSymmetric(uint32_t commandID, TEE_Param param[4])
        }
 
 clean:
-       free(in_padded);
-       free(out);
+       TEE_Free(in_padded);
+       TEE_Free(out);
        TEE_CloseObject(key);
        TEE_FreeOperation(operation);
        return ret;
@@ -1991,7 +1990,7 @@ TEE_Result KM_ExecCmdAuth(uint32_t commandID, TEE_Param param[4])
        }
 
        out_size = KM_CalculatePaddedSize(KM_PADDING_PKCS7, AES_BLOCK_SIZE, input.data_size);
-       out = malloc(out_size);
+       out = TEE_Malloc(out_size, 0);
 
        if (out == NULL) {
                LOG("Failed to allocate output buffer");
@@ -2007,7 +2006,7 @@ TEE_Result KM_ExecCmdAuth(uint32_t commandID, TEE_Param param[4])
 
        if (CMD_ENCRYPT == commandID) {
                out_tag_size = tag_len_bits / 8; // convert tag size to bytes
-               out_tag = malloc(out_tag_size);
+               out_tag = TEE_Malloc(out_tag_size, 0);
                if (out_tag == NULL) {
                        LOG("Failed to allocate tag buffer");
                        ret = TEE_ERROR_OUT_OF_MEMORY;
@@ -2051,8 +2050,8 @@ TEE_Result KM_ExecCmdAuth(uint32_t commandID, TEE_Param param[4])
 
        LOG("Crypto auth finished");
 clean:
-       free(out);
-       free(out_tag);
+       TEE_Free(out);
+       TEE_Free(out_tag);
        TEE_CloseObject(key);
        TEE_FreeOperation(op);
        return ret;
@@ -2130,7 +2129,7 @@ TEE_Result KM_ExecCmdAsymmetric(uint32_t commandID, TEE_Param param[4])
        }
 
        // memory for output
-       out = malloc(out_size);
+       out = TEE_Malloc(out_size, 0);
        if (out == NULL) {
                LOG("Failed to allocate output buffer");
                ret = TEE_ERROR_OUT_OF_MEMORY;
@@ -2159,7 +2158,7 @@ TEE_Result KM_ExecCmdAsymmetric(uint32_t commandID, TEE_Param param[4])
 clean:
        TEE_CloseObject(key);
        TEE_FreeOperation(operation);
-       free(out);
+       TEE_Free(out);
        return ret;
 }
 
@@ -2248,7 +2247,7 @@ TEE_Result KM_ExecCmdSign(TEE_Param param[4])
        if (param[0].value.a == ALGO_ECDSA_SV)
                outSize *= 2;
 
-       out = malloc(outSize);
+       out = TEE_Malloc(outSize, 0);
        if (out == NULL) {
                LOG("Failed to allocate output buffer");
                ret = TEE_ERROR_OUT_OF_MEMORY;
@@ -2271,8 +2270,8 @@ TEE_Result KM_ExecCmdSign(TEE_Param param[4])
 
 clean:
        TEE_CloseObject(key);
-       free(out);
-       free(digest);
+       TEE_Free(out);
+       TEE_Free(digest);
        TEE_FreeOperation(operation);
        return ret;
 }
@@ -2368,7 +2367,7 @@ TEE_Result KM_ExecCmdVerify(TEE_Param param[4])
 
 clean:
        TEE_CloseObject(key);
-       free(digest);
+       TEE_Free(digest);
        TEE_FreeOperation(operation);
        return ret;
 }
@@ -2476,7 +2475,7 @@ TEE_Result KM_ExecCmdSaveData(TEE_Param param[4])
        // Decryption
        if (dataEncIV.data_size) {
                plainData.data_size = dataToSave.data_size;
-               plainData.data = malloc(plainData.data_size);
+               plainData.data = TEE_Malloc(plainData.data_size, 0);
                if (plainData.data == NULL) {
                        LOG("Failed to allocate object buffer for decryption data");
                        ret = TEE_ERROR_OUT_OF_MEMORY;
@@ -2502,14 +2501,14 @@ TEE_Result KM_ExecCmdSaveData(TEE_Param param[4])
                        }
                        dataToSave = encData;
                }
-               tmp = malloc(dataToSave.data_size);
+               tmp = TEE_Malloc(dataToSave.data_size, 0);
                if (!tmp) {
                        LOG("Allocating memory has failed.");
                        goto clean;
                }
                memcpy(tmp, dataToSave.data, dataToSave.data_size);
                ret = KM_SaveData(tmp, dataToSave.data_size, objId.data, objId.data_size);
-               free(tmp);
+               TEE_Free(tmp);
                if (ret != TEE_SUCCESS) {
                        LOG("Failed to save data to storage");
                        goto clean;
@@ -2523,7 +2522,7 @@ TEE_Result KM_ExecCmdSaveData(TEE_Param param[4])
 
                if (with_pwd) {
                        tag.data_size = pwdData.tag_len_bits / 8;
-                       tag.data = malloc(tag.data_size);
+                       tag.data = TEE_Malloc(tag.data_size, 0);
                        if (tag.data == NULL) {
                                LOG("Failed to allocate memory for key's tag");
                                ret = TEE_ERROR_OUT_OF_MEMORY;
@@ -2531,7 +2530,7 @@ TEE_Result KM_ExecCmdSaveData(TEE_Param param[4])
                        }
 
                        encKeyBufSize = dataSizeBits_flag / 8;
-                       encKeyBuf = malloc(encKeyBufSize);
+                       encKeyBuf = TEE_Malloc(encKeyBufSize, 0);
                        if (encKeyBuf == NULL) {
                                LOG("Failed to allocate memory for key encryption");
                                ret = TEE_ERROR_OUT_OF_MEMORY;
@@ -2594,10 +2593,10 @@ TEE_Result KM_ExecCmdSaveData(TEE_Param param[4])
 
 clean:
        TEE_CloseObject(key);
-       free(plainData.data);
-       free(encData.data);
-       free(tag.data);
-       free(encKeyBuf);
+       TEE_Free(plainData.data);
+       TEE_Free(encData.data);
+       TEE_Free(tag.data);
+       TEE_Free(encKeyBuf);
        return ret;
 }
 
@@ -2877,7 +2876,7 @@ TEE_Result KM_ExecCmdCipherUpdate(TEE_Param param[4])
        op = ctx->oper;
 
        output.data_size = data.data_size + KM_ENCRYPTION_OVERHEAD;
-       output.data = malloc(output.data_size);
+       output.data = TEE_Malloc(output.data_size, 0);
        if (output.data == NULL) {
                LOG("Failed to allocate buffer for output of KM_ExecCmdCipherUpdate()");
                ret = TEE_ERROR_OUT_OF_MEMORY;
@@ -2899,7 +2898,7 @@ TEE_Result KM_ExecCmdCipherUpdate(TEE_Param param[4])
 
 clean:
        if (output.data != NULL)
-               free(output.data);
+               TEE_Free(output.data);
        return ret;
 }
 
@@ -2965,7 +2964,7 @@ TEE_Result KM_ExecCmdCipherFinalize(TEE_Param param[4])
                        goto clean;
                }
                output.data_size = tag_len_bytes;
-               output.data = malloc(output.data_size);
+               output.data = TEE_Malloc(output.data_size, 0);
                if (output.data == NULL) {
                        LOG("Failed to allocate buffer for tag of KM_ExecCmdCipherFinalize()");
                        ret = TEE_ERROR_OUT_OF_MEMORY;
@@ -2993,9 +2992,9 @@ clean:
        // Remove Stored Operation. Operation & Key will be freed too.
        ctxl_delete_node_id(op_id);
        if (dummy_output.data != NULL)
-               free(dummy_output.data);
+               TEE_Free(dummy_output.data);
        if (output.data != NULL)
-               free(output.data);
+               TEE_Free(output.data);
        return ret;
 }
 
index b58193f650fceac6f700ca5b8f151e768f36a56f..a9f2130d31c4ef88acfe985b37bcb3d654963ae2 100644 (file)
@@ -99,7 +99,7 @@ TEE_Result KM_DeriveKey(void *pwd, uint32_t pwd_size, void *salt, uint32_t salt_
        if (hmacKeySize < 10) {
                hmacKeySize = 10;
        }
-       hmacKeyBuffer = (char*)malloc(hmacKeySize);
+       hmacKeyBuffer = (char*)TEE_Malloc(hmacKeySize, 0);
        if (!hmacKeyBuffer) {
                LOG("Failed to allocate temp input memory for HMAC Key");
                return TEE_ERROR_OUT_OF_MEMORY;
@@ -123,21 +123,21 @@ TEE_Result KM_DeriveKey(void *pwd, uint32_t pwd_size, void *salt, uint32_t salt_
                goto end;
        }
 
-       hmacInBuffer = (char*)malloc(hmacInBufferSize);
+       hmacInBuffer = (char*)TEE_Malloc(hmacInBufferSize, 0);
        if (!hmacInBuffer) {
                LOG("Failed to allocate temp input memory for HMAC operation");
                ret = TEE_ERROR_OUT_OF_MEMORY;
                goto end;
        }
 
-       blockBuffer = (char*)malloc(blockBufferSize);
+       blockBuffer = (char*)TEE_Malloc(blockBufferSize, 0);
        if (!blockBuffer) {
                LOG("Failed to allocate block memory for HMAC operation");
                ret = TEE_ERROR_OUT_OF_MEMORY;
                goto end;
        }
 
-       keyBuffer = (char*)malloc(keyBytesSize);
+       keyBuffer = (char*)TEE_Malloc(keyBytesSize, 0);
        if (!keyBuffer) {
                LOG("Failed to allocate memory for derived key");
                ret = TEE_ERROR_OUT_OF_MEMORY;
@@ -201,10 +201,10 @@ TEE_Result KM_DeriveKey(void *pwd, uint32_t pwd_size, void *salt, uint32_t salt_
 end:
        if (hmacOp != TEE_HANDLE_NULL)
                TEE_FreeOperation(hmacOp);
-       free(hmacKeyBuffer);
-       free(hmacInBuffer);
-       free(blockBuffer);
-       free(keyBuffer);
+       TEE_Free(hmacKeyBuffer);
+       TEE_Free(hmacInBuffer);
+       TEE_Free(blockBuffer);
+       TEE_Free(keyBuffer);
 
        return ret;
 }
@@ -317,7 +317,7 @@ TEE_Result KM_KbkdfHmacCtr(
 
        if (fixedInputLen > 0) {
                inputLen = fixedInputLen + rLen;
-               input = (unsigned char*)calloc(inputLen, sizeof(unsigned char));
+               input = (unsigned char*)TEE_Malloc(inputLen, 0);
                if(input == NULL) {
                        LOG("Failed to allocate memory. requested_memory_size=%d", inputLen);
                        ret = TEE_ERROR_OUT_OF_MEMORY;
@@ -342,7 +342,7 @@ TEE_Result KM_KbkdfHmacCtr(
                }
        } else {
                inputLen = rLen + LabelLen + separatorLen + ContextLen + lLen;
-               input = (unsigned char*)calloc(inputLen, sizeof(unsigned char));
+               input = (unsigned char*)TEE_Malloc(inputLen, 0);
 
                switch(ctrLoc) {
                        case KBKDF_LOC_BEFORE_FIXED: {
@@ -366,7 +366,7 @@ TEE_Result KM_KbkdfHmacCtr(
                }
        }
 
-       K = (unsigned char*)calloc(macLen * n, sizeof(unsigned char));
+       K = (unsigned char*)TEE_Malloc(macLen * n, 0);
 
        for (i = 0; i < n; i++) {
                ctr++;
@@ -390,8 +390,8 @@ TEE_Result KM_KbkdfHmacCtr(
 
 clean:
        TEE_FreeOperation(hmacOp);
-       free(input);
-       free(K);
+       TEE_Free(input);
+       TEE_Free(K);
 
        return TEE_SUCCESS;
 }
index f251c684c6b8b6bd98d75f8c3c19e891a3e64dbd..e572ae9a93e74490da0531339c1ef289fa667656 100644 (file)
@@ -17,7 +17,6 @@
 #include <stdbool.h>
 #include <string.h>
 #include <sys/types.h> // ssize_t
-#include <malloc.h>
 
 #include <log.h>
 #include <der_parse.h>
@@ -296,7 +295,7 @@ TEE_Result ecPublicToDer(const uint32_t curve, DerData *x, DerData *y, DerData *
        size_t len_total =  1 + toLenSize(len_algo) + len_algo +
                                                1 + toLenSize(len_xy) + len_xy;
 
-       der = (uint8_t *) malloc( 1 + toLenSize(len_total) + len_total); // 1: TAG Size, + Length Size + len_total: Total Body Size
+       der = (uint8_t *) TEE_Malloc( 1 + toLenSize(len_total) + len_total, 0); // 1: TAG Size, + Length Size + len_total: Total Body Size
        if(der == NULL) {
                ret = TEE_ERROR_OUT_OF_MEMORY;
                goto clean;
@@ -362,7 +361,7 @@ TEE_Result ecPublicToDer(const uint32_t curve, DerData *x, DerData *y, DerData *
 
 clean:
        if(ret != TEE_SUCCESS)
-               free(der);
+               TEE_Free(der);
 
        return ret;
 }
index f24368630034e34b0a6682001abeacdb422c5105..af75a985f6c3f6df2d13fe51f9e06ad028044a5c 100644 (file)
@@ -28,7 +28,6 @@
 
 #include <assert.h>
 #include <stdlib.h>
-#include <malloc.h>
 #include <crypto_padding.h>
 #include <crypto_derive.h>
 #include <crypto_symmetric.h>
@@ -304,7 +303,7 @@ TEE_Result KM_EncryptBinary(const KM_PwdData *pwd, KM_BinaryData *input,
        TEE_Result ret = TEE_SUCCESS;
 
        tag->data_size = pwd->tag_len_bits / 8;
-       tag->data = malloc(tag->data_size);
+       tag->data = TEE_Malloc(tag->data_size, 0);
        if (tag->data == NULL) {
                LOG("Failed to allocate memory for data encryption key's tag");
                ret = TEE_ERROR_OUT_OF_MEMORY;
@@ -319,7 +318,7 @@ TEE_Result KM_EncryptBinary(const KM_PwdData *pwd, KM_BinaryData *input,
                goto clean;
        }
 
-       output->data = malloc(output->data_size);
+       output->data = TEE_Malloc(output->data_size, 0);
        if (output->data == NULL) {
                LOG("Failed to allocate object buffer for encrypting data");
                ret = TEE_ERROR_OUT_OF_MEMORY;
@@ -334,8 +333,8 @@ TEE_Result KM_EncryptBinary(const KM_PwdData *pwd, KM_BinaryData *input,
        }
 clean:
        if(ret != TEE_SUCCESS) {
-               free(tag->data);
-               free(output->data);
+               TEE_Free(tag->data);
+               TEE_Free(output->data);
        }
        return ret;
 }
@@ -368,7 +367,7 @@ TEE_Result KM_EncryptKey(TEE_ObjectHandle keyHndl, KM_PwdData *pwd,
                return TEE_ERROR_BAD_PARAMETERS;
        }
 
-       keyBuf = malloc(keyBufSize);
+       keyBuf = TEE_Malloc(keyBufSize, 0);
        if (keyBuf == NULL) {
                LOG("Failed to allocate memory for key encryption");
                ret = TEE_ERROR_OUT_OF_MEMORY;
@@ -376,7 +375,7 @@ TEE_Result KM_EncryptKey(TEE_ObjectHandle keyHndl, KM_PwdData *pwd,
        }
 
        encKeyBufSize = keyBufSize;
-       encKeyBuf = malloc(encKeyBufSize);
+       encKeyBuf = TEE_Malloc(encKeyBufSize, 0);
        if (encKeyBuf == NULL) {
                LOG("Failed to allocate memory for key encryption");
                ret = TEE_ERROR_OUT_OF_MEMORY;
@@ -407,8 +406,8 @@ TEE_Result KM_EncryptKey(TEE_ObjectHandle keyHndl, KM_PwdData *pwd,
        }
 
 out:
-       free(encKeyBuf);
-       free(keyBuf);
+       TEE_Free(encKeyBuf);
+       TEE_Free(keyBuf);
 
        return ret;
 }
@@ -429,7 +428,7 @@ TEE_Result KM_GetValueAttribute(TEE_ObjectHandle keyHndl, uint32_t attrId, KM_Bi
        }
 
        attr->data_size = sizeof(uint32_t) * 2; // for a and b of TEE_Attribute.content.value
-       attr->data = malloc(attr->data_size);
+       attr->data = TEE_Malloc(attr->data_size, 0);
        if (attr->data == NULL) {
                LOG("Failed to allocate memory for key attribute.");
                ret = TEE_ERROR_OUT_OF_MEMORY;
@@ -446,7 +445,7 @@ TEE_Result KM_GetValueAttribute(TEE_ObjectHandle keyHndl, uint32_t attrId, KM_Bi
 
 clean:
        if (ret != TEE_SUCCESS) {
-               free(attr->data);
+               TEE_Free(attr->data);
                attr->data = NULL;
                attr->data_size = 0;
        }
@@ -483,7 +482,7 @@ TEE_Result KM_GetBufferAttribute(TEE_ObjectHandle keyHndl, uint32_t attrId, KM_B
        }
 
        attr->data_size = attrSize;
-       attr->data = malloc(attr->data_size);
+       attr->data = TEE_Malloc(attr->data_size, 0);
        if (attr->data == NULL) {
                LOG("Failed to allocate memory for key  attribute.");
                ret = TEE_ERROR_OUT_OF_MEMORY;
@@ -497,7 +496,7 @@ TEE_Result KM_GetBufferAttribute(TEE_ObjectHandle keyHndl, uint32_t attrId, KM_B
 
 clean:
        if (ret != TEE_SUCCESS) {
-               free(attr->data);
+               TEE_Free(attr->data);
                attr->data = NULL;
                attr->data_size = 0;
        }
@@ -568,7 +567,7 @@ static TEE_Result KM_EncryptAsymKeyInternal(TEE_ObjectHandle keyHndl, const KM_P
        }
 
        // allocate key data buffer
-       keyDataBuffer = malloc(totalSize);
+       keyDataBuffer = TEE_Malloc(totalSize, 0);
        if (keyDataBuffer == NULL) {
                LOG("Failed to allocate memory for key data");
                ret = TEE_ERROR_OUT_OF_MEMORY;
@@ -601,7 +600,7 @@ static TEE_Result KM_EncryptAsymKeyInternal(TEE_ObjectHandle keyHndl, const KM_P
 
        // allocate buffer for encrypted key
        tmpEncKey.data_size = totalSize;
-       tmpEncKey.data = malloc(tmpEncKey.data_size);
+       tmpEncKey.data = TEE_Malloc(tmpEncKey.data_size, 0);
        if (tmpEncKey.data == NULL) {
                LOG("Failed to allocate memory for encrypted key");
                ret = TEE_ERROR_OUT_OF_MEMORY;
@@ -610,7 +609,7 @@ static TEE_Result KM_EncryptAsymKeyInternal(TEE_ObjectHandle keyHndl, const KM_P
 
        // allocate buffer for tag
        tmpTag.data_size = (pwd->tag_len_bits + 7) >> 3;
-       tmpTag.data = malloc(tmpTag.data_size);
+       tmpTag.data = TEE_Malloc(tmpTag.data_size, 0);
        if (tmpTag.data == NULL) {
                LOG("Failed to allocate memory for key tag");
                ret = TEE_ERROR_OUT_OF_MEMORY;
@@ -634,16 +633,16 @@ static TEE_Result KM_EncryptAsymKeyInternal(TEE_ObjectHandle keyHndl, const KM_P
 
 clean:
        if (ret != TEE_SUCCESS) {
-               free(tmpEncKey.data);
+               TEE_Free(tmpEncKey.data);
                encKey->data = NULL;
-               free(tmpTag.data);
+               TEE_Free(tmpTag.data);
                tag->data = NULL;
        }
 
        for (i = 0; i < attrsCount; i++) {
-               free(attributes[i].data);
+               TEE_Free(attributes[i].data);
        }
-       free(keyDataBuffer);
+       TEE_Free(keyDataBuffer);
 
        return ret;
 }
@@ -705,7 +704,7 @@ static TEE_Result KM_DecryptAsymKeyData(const KM_BinaryData *id,
        TEE_GetObjectInfo(hndl, &info);
 
        keyBuf.data_size = info.dataSize;
-       keyBuf.data = malloc(keyBuf.data_size);
+       keyBuf.data = TEE_Malloc(keyBuf.data_size, 0);
        if (keyBuf.data == NULL) {
                LOG("Failed to allocate memory for encrypted key read");
                ret = TEE_ERROR_OUT_OF_MEMORY;
@@ -721,7 +720,7 @@ static TEE_Result KM_DecryptAsymKeyData(const KM_BinaryData *id,
 
        // allocate memory for result
        tmpDecKeyBuf.data_size = keyBuf.data_size;
-       tmpDecKeyBuf.data = malloc(tmpDecKeyBuf.data_size);
+       tmpDecKeyBuf.data = TEE_Malloc(tmpDecKeyBuf.data_size, 0);
        if (tmpDecKeyBuf.data == NULL) {
                LOG("Failed to allocate memory for key decryption");
                ret = TEE_ERROR_OUT_OF_MEMORY;
@@ -743,8 +742,8 @@ static TEE_Result KM_DecryptAsymKeyData(const KM_BinaryData *id,
 
 out:
        TEE_CloseObject(hndl);
-       free(keyBuf.data);
-       free(tmpDecKeyBuf.data);
+       TEE_Free(keyBuf.data);
+       TEE_Free(tmpDecKeyBuf.data);
        return ret;
 }
 
@@ -797,11 +796,11 @@ TEE_Result KM_EcpublickeyToDer(TEE_ObjectHandle keyHndl, KM_BinaryData *der)
        der->data_size = der_out.size;
 clean:
        if (ret != TEE_SUCCESS) {
-               free(der->data);
+               TEE_Free(der->data);
                der->data = NULL;
        }
-       free(x.data);
-       free(y.data);
+       TEE_Free(x.data);
+       TEE_Free(y.data);
        return ret;
 }
 
@@ -1021,7 +1020,7 @@ TEE_Result KM_DecryptKey(void *id, size_t id_size, KM_PwdData* pwd,
                return TEE_ERROR_GENERIC;
        }
 
-       keyBuf = malloc(keyBufSize);
+       keyBuf = TEE_Malloc(keyBufSize, 0);
        if (keyBuf == NULL) {
                LOG("Failed to allocate memory for key encryption");
                ret = TEE_ERROR_OUT_OF_MEMORY;
@@ -1029,7 +1028,7 @@ TEE_Result KM_DecryptKey(void *id, size_t id_size, KM_PwdData* pwd,
        }
 
        decKeyBufSize = keyBufSize;
-       decKeyBuf = malloc(decKeyBufSize);
+       decKeyBuf = TEE_Malloc(decKeyBufSize, 0);
        if (decKeyBuf == NULL) {
                LOG("Failed to allocate memory for key decryption");
                ret = TEE_ERROR_OUT_OF_MEMORY;
@@ -1060,8 +1059,8 @@ TEE_Result KM_DecryptKey(void *id, size_t id_size, KM_PwdData* pwd,
 
 out:
        TEE_CloseObject(objHandle);
-       free(decKeyBuf);
-       free(keyBuf);
+       TEE_Free(decKeyBuf);
+       TEE_Free(keyBuf);
        return ret;
 }
 
@@ -1130,7 +1129,7 @@ TEE_Result KM_DecryptAsymKey(const KM_BinaryData *id, KM_PwdData* pwd, TEE_Objec
        }
 
 out:
-       free(decKeyBuf.data);
+       TEE_Free(decKeyBuf.data);
        return ret;
 }
 
@@ -1320,7 +1319,7 @@ TEE_Result KM_GetData(void *data, uint32_t expected_data_size, KM_BinaryData *ob
                        goto clean;
                }
 
-               buffer.data = malloc(info.dataSize);
+               buffer.data = TEE_Malloc(info.dataSize, 0);
                if(buffer.data == NULL) {
                        LOG("Failed to assign memrory. requested size=%d", info.dataSize);
                        ret = TEE_ERROR_OUT_OF_MEMORY;
@@ -1357,7 +1356,7 @@ TEE_Result KM_GetData(void *data, uint32_t expected_data_size, KM_BinaryData *ob
 
 clean:
        TEE_CloseObject(hndl);
-       free(buffer.data);
+       TEE_Free(buffer.data);
 
        return ret;
 }
@@ -1409,7 +1408,7 @@ TEE_Result KM_GetDataSize(uint32_t *data_size, KM_BinaryData *obj_id,
        }
 
 clean:
-       free(der.data);
+       TEE_Free(der.data);
        TEE_CloseObject(hndl);
 
        return ret;