*/
#include <stdlib.h>
-#include <malloc.h>
#include <tee_internal_api.h>
#include <km_ta_defines.h>
#include <internal.h>
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;
}
// 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;
clean:
if (ret != TEE_SUCCESS)
- free(tmpDigest);
+ TEE_Free(tmpDigest);
TEE_FreeOperation(digestOperation);
return ret;
}
}
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;
clean:
TEE_FreeTransientObject(key);
- free(tag);
+ TEE_Free(tag);
return ret;
}
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;
clean:
TEE_CloseObject(key_handle);
if(ret != TEE_SUCCESS)
- free(output.data);
+ TEE_Free(output.data);
return ret;
}
}
// 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");
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;
}
}
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;
}
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;
}
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;
clean:
TEE_FreeOperation(operation);
if (ret != TEE_SUCCESS)
- free(out);
+ TEE_Free(out);
return ret;
}
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;
clean:
TEE_FreeOperation(operation);
if (ret != TEE_SUCCESS)
- free(out);
+ TEE_Free(out);
return ret;
}
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;
}
}
- 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;
clean:
TEE_FreeOperation(operation);
- free(in_padded);
+ TEE_Free(in_padded);
if (ret != TEE_SUCCESS)
- free(out);
+ TEE_Free(out);
return ret;
}
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;
}
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;
}
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;
}
}
- 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;
}
clean:
- free(in_padded);
- free(out);
+ TEE_Free(in_padded);
+ TEE_Free(out);
TEE_CloseObject(key);
TEE_FreeOperation(operation);
return ret;
}
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");
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;
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;
}
// 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;
clean:
TEE_CloseObject(key);
TEE_FreeOperation(operation);
- free(out);
+ TEE_Free(out);
return ret;
}
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;
clean:
TEE_CloseObject(key);
- free(out);
- free(digest);
+ TEE_Free(out);
+ TEE_Free(digest);
TEE_FreeOperation(operation);
return ret;
}
clean:
TEE_CloseObject(key);
- free(digest);
+ TEE_Free(digest);
TEE_FreeOperation(operation);
return ret;
}
// 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;
}
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;
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;
}
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;
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;
}
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;
clean:
if (output.data != NULL)
- free(output.data);
+ TEE_Free(output.data);
return ret;
}
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;
// 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;
}
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;
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;
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;
}
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;
}
} 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: {
}
}
- K = (unsigned char*)calloc(macLen * n, sizeof(unsigned char));
+ K = (unsigned char*)TEE_Malloc(macLen * n, 0);
for (i = 0; i < n; i++) {
ctr++;
clean:
TEE_FreeOperation(hmacOp);
- free(input);
- free(K);
+ TEE_Free(input);
+ TEE_Free(K);
return TEE_SUCCESS;
}
#include <assert.h>
#include <stdlib.h>
-#include <malloc.h>
#include <crypto_padding.h>
#include <crypto_derive.h>
#include <crypto_symmetric.h>
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;
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;
}
clean:
if(ret != TEE_SUCCESS) {
- free(tag->data);
- free(output->data);
+ TEE_Free(tag->data);
+ TEE_Free(output->data);
}
return ret;
}
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;
}
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;
}
out:
- free(encKeyBuf);
- free(keyBuf);
+ TEE_Free(encKeyBuf);
+ TEE_Free(keyBuf);
return ret;
}
}
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;
clean:
if (ret != TEE_SUCCESS) {
- free(attr->data);
+ TEE_Free(attr->data);
attr->data = NULL;
attr->data_size = 0;
}
}
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;
clean:
if (ret != TEE_SUCCESS) {
- free(attr->data);
+ TEE_Free(attr->data);
attr->data = NULL;
attr->data_size = 0;
}
}
// 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;
// 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;
// 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;
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;
}
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;
// 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;
out:
TEE_CloseObject(hndl);
- free(keyBuf.data);
- free(tmpDecKeyBuf.data);
+ TEE_Free(keyBuf.data);
+ TEE_Free(tmpDecKeyBuf.data);
return ret;
}
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;
}
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;
}
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;
out:
TEE_CloseObject(objHandle);
- free(decKeyBuf);
- free(keyBuf);
+ TEE_Free(decKeyBuf);
+ TEE_Free(keyBuf);
return ret;
}
}
out:
- free(decKeyBuf.data);
+ TEE_Free(decKeyBuf.data);
return ret;
}
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;
clean:
TEE_CloseObject(hndl);
- free(buffer.data);
+ TEE_Free(buffer.data);
return ret;
}
}
clean:
- free(der.data);
+ TEE_Free(der.data);
TEE_CloseObject(hndl);
return ret;