Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow usage of user defined malloc, realloc and free functions #11

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions utils/ibmtss/tsserror.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,5 @@
#define TSS_RC_NO_SESSION_SLOT 0x000b0090 /* TSS context has no session slot for handle */
#define TSS_RC_NO_OBJECTPUBLIC_SLOT 0x000b0091 /* TSS context has no object public slot for handle */
#define TSS_RC_NO_NVPUBLIC_SLOT 0x000b0092 /* TSS context has no NV public slot for handle */
#define TSS_RC_PROPERTY_ALREADY_SET 0x000b0093 /* A property has already been set and cannot be set again */
#endif
10 changes: 9 additions & 1 deletion utils/ibmtss/tssutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,16 @@ extern "C" {
typedef TPM_RC (*UnmarshalFunctionFlag_t)(void *target, uint8_t **buffer, uint32_t *size, BOOL allowNull);
typedef TPM_RC (*MarshalFunction_t)(void *source, uint16_t *written, uint8_t **buffer, uint32_t *size);

typedef void* (*TSS_CUST_MALLOC)(size_t size);
typedef void* (*TSS_CUST_REALLOC)(void* ptr, size_t size);
typedef void (*TSS_CUST_FREE)(void* ptr);

LIB_EXPORT
TPM_RC TSS_Malloc(unsigned char **buffer, uint32_t size);
LIB_EXPORT
TPM_RC TSS_Realloc(unsigned char **buffer, uint32_t size);

LIB_EXPORT
TPM_RC TSS_Free(unsigned char** buffer);
LIB_EXPORT
TPM_RC TSS_Structure_Marshal(uint8_t **buffer,
uint16_t *written,
Expand Down Expand Up @@ -96,6 +101,9 @@ extern "C" {
uint16_t TSS_GetDigestSize(TPM_ALG_ID hashAlg);
#endif

LIB_EXPORT
TPM_RC TSS_SetMemoryFunctions(TSS_CUST_MALLOC custom_malloc, TSS_CUST_REALLOC custom_realloc, TSS_CUST_FREE custom_free);

#ifdef __cplusplus
}
#endif
Expand Down
7 changes: 3 additions & 4 deletions utils/tss.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ TPM_RC TSS_Create(TSS_CONTEXT **tssContext)
/* the likely cause of a failure is a bad environment variable */
if (rc != 0) {
if (tssVerbose) printf("TSS_Create: TSS_Context_Init() failed\n");
free(*tssContext);
*tssContext = NULL;
TSS_Free((unsigned char**)tssContext);
}
}
/* allocate and initialize the lower layer TSS context */
Expand Down Expand Up @@ -168,7 +167,7 @@ TPM_RC TSS_Delete(TSS_CONTEXT *tssContext)
memset(tssContext->sessions[i].sessionData,
0, tssContext->sessions[i].sessionDataLength);
}
free(tssContext->sessions[i].sessionData);
TSS_Free(&tssContext->sessions[i].sessionData);
tssContext->sessions[i].sessionData = NULL;
tssContext->sessions[i].sessionDataLength = 0;
}
Expand All @@ -181,7 +180,7 @@ TPM_RC TSS_Delete(TSS_CONTEXT *tssContext)
#endif
#endif
rc = TSS_Close(tssContext);
free(tssContext);
TSS_Free((unsigned char**)&tssContext);
}
return rc;
}
Expand Down
42 changes: 21 additions & 21 deletions utils/tss20.c
Original file line number Diff line number Diff line change
Expand Up @@ -1549,9 +1549,9 @@ static TPM_RC TSS_Execute_valist(TSS_CONTEXT *tssContext,
/* cleanup */
for (i = 0 ; i < MAX_SESSION_NUM ; i++) {
TSS_HmacSession_FreeContext(session[i]);
free(authCommand[i]); /* @1 */
free(authResponse[i]); /* @2 */
free(names[i]); /* @3 */
TSS_Free((unsigned char**)&authCommand[i]); /* @1 */
TSS_Free((unsigned char**)&authResponse[i]); /* @2 */
TSS_Free((unsigned char**)&names[i]); /* @3 */
}
return rc;
}
Expand Down Expand Up @@ -1664,7 +1664,7 @@ void TSS_HmacSession_FreeContext(struct TSS_HMAC_CONTEXT *session)
{
if (session != NULL) {
TSS_HmacSession_InitContext(session);
free(session);
TSS_Free((unsigned char**)&session);
}
return;
}
Expand Down Expand Up @@ -1798,7 +1798,7 @@ static TPM_RC TSS_HmacSession_SaveSession(TSS_CONTEXT *tssContext,
sessionFilename);
}
if (tssContext->tssEncryptSessions) {
free(outBuffer); /* @2 */
TSS_Free(&outBuffer); /* @2 */
}
#else /* no file support, save to context */
if (rc == 0) {
Expand All @@ -1807,7 +1807,7 @@ static TPM_RC TSS_HmacSession_SaveSession(TSS_CONTEXT *tssContext,
written, buffer);
}
#endif
free(buffer); /* @1 */
TSS_Free(&buffer); /* @1 */
return rc;
}

Expand Down Expand Up @@ -1874,10 +1874,10 @@ static TPM_RC TSS_HmacSession_LoadSession(TSS_CONTEXT *tssContext,
}
#ifndef TPM_TSS_NOFILE
if (tssContext->tssEncryptSessions) {
free(inData); /* @2 */
TSS_Free(&inData); /* @2 */
}
#endif
free(buffer); /* @1 */
TSS_Free(&buffer); /* @1 */
return rc;
}

Expand Down Expand Up @@ -1958,7 +1958,7 @@ static TPM_RC TSS_HmacSession_DeleteData(TSS_CONTEXT *tssContext,
/* erase any secrets */
memset(tssContext->sessions[slotIndex].sessionData, 0,
tssContext->sessions[slotIndex].sessionDataLength);
free(tssContext->sessions[slotIndex].sessionData);
TSS_Free(&tssContext->sessions[slotIndex].sessionData);
tssContext->sessions[slotIndex].sessionData = NULL;
tssContext->sessions[slotIndex].sessionDataLength = 0;
}
Expand Down Expand Up @@ -2792,7 +2792,7 @@ static TPM_RC TSS_ObjectPublic_GetName(TPM2B_NAME *name,
/* set the size */
name->t.size = sizeInBytes + (uint16_t)sizeof(TPMI_ALG_HASH);
}
free(buffer); /* @1 */
TSS_Free(&buffer); /* @1 */
return rc;
}

Expand Down Expand Up @@ -3029,7 +3029,7 @@ static TPM_RC TSS_NVPublic_GetName(TPM2B_NAME *name,
/* set the size */
name->t.size = sizeInBytes + (uint16_t)sizeof(TPMI_ALG_HASH);
}
free(buffer); /* @1 */
TSS_Free(&buffer); /* @1 */
return rc;
}

Expand Down Expand Up @@ -3797,8 +3797,8 @@ static TPM_RC TSS_Command_DecryptXor(TSS_AUTH_CONTEXT *tssAuthContext,
if (tssVverbose) TSS_PrintAll("TSS_Command_DecryptXor: encrypt out",
encryptParamBuffer, paramSize);
}
free(mask);
free(encryptParamBuffer);
TSS_Free(&mask);
TSS_Free(&encryptParamBuffer);
return rc;
}

Expand Down Expand Up @@ -3894,7 +3894,7 @@ static TPM_RC TSS_Command_DecryptAes(TSS_AUTH_CONTEXT *tssAuthContext,
if (rc == 0) {
rc = TSS_SetCommandDecryptParam(tssAuthContext, paramSize, encryptParamBuffer);
}
free(encryptParamBuffer); /* @1 */
TSS_Free(&encryptParamBuffer); /* @1 */
return rc;
}

Expand Down Expand Up @@ -4055,8 +4055,8 @@ static TPM_RC TSS_Response_EncryptXor(TSS_AUTH_CONTEXT *tssAuthContext,
rc = TSS_SetResponseDecryptParam(tssAuthContext,
paramSize, decryptParamBuffer);
}
free(mask); /* @1 */
free(decryptParamBuffer); /* @2 */
TSS_Free(&mask); /* @1 */
TSS_Free(&decryptParamBuffer); /* @2 */
return rc;
}

Expand Down Expand Up @@ -4148,7 +4148,7 @@ static TPM_RC TSS_Response_EncryptAes(TSS_AUTH_CONTEXT *tssAuthContext,
rc = TSS_SetResponseDecryptParam(tssAuthContext,
paramSize, decryptParamBuffer);
}
free(decryptParamBuffer); /* @1 */
TSS_Free(&decryptParamBuffer); /* @1 */
return rc;
}

Expand Down Expand Up @@ -4235,7 +4235,7 @@ static TPM_RC TSS_CA_HierarchyChangeAuth(TSS_CONTEXT *tssContext,
session = session;
handleNumber = handleNumber;
#endif /* TPM_TSS_NOCRYPTO */
free(password); /* @1 */
TSS_Free(&password); /* @1 */
return rc;
}

Expand Down Expand Up @@ -4274,7 +4274,7 @@ static TPM_RC TSS_CA_NV_ChangeAuth(TSS_CONTEXT *tssContext,
session = session;
handleNumber = handleNumber;
#endif /* TPM_TSS_NOCRYPTO */
free(password); /* @1 */
TSS_Free(&password); /* @1 */
return rc;
}

Expand Down Expand Up @@ -5144,7 +5144,7 @@ static TPM_RC TSS_PO_PolicyAuthValue(TSS_CONTEXT *tssContext,
session->isAuthValueNeeded = TRUE;
rc = TSS_HmacSession_SaveSession(tssContext, session);
}
free(session); /* @1 */
TSS_Free((unsigned char**)&session); /* @1 */
return rc;
}

Expand All @@ -5170,7 +5170,7 @@ static TPM_RC TSS_PO_PolicyPassword(TSS_CONTEXT *tssContext,
session->isAuthValueNeeded = FALSE;
rc = TSS_HmacSession_SaveSession(tssContext, session);
}
free(session); /* @1 */
TSS_Free((unsigned char**)&session); /* @1 */
return rc;
}

Expand Down
2 changes: 1 addition & 1 deletion utils/tssauth.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ TPM_RC TSS_AuthDelete(TSS_AUTH_CONTEXT *tssAuthContext)
{
if (tssAuthContext != NULL) {
TSS_InitAuthContext(tssAuthContext);
free(tssAuthContext);
TSS_Free((unsigned char**)&tssAuthContext);
}
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion utils/tssauth20.c
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,7 @@ TPM_RC TSS_Marshal(TSS_AUTH_CONTEXT *tssAuthContext,
printf("TSS_Marshal: Invalid command parameter\n");
}
}
free(target); /* @1 */
TSS_Free((unsigned char**)&target); /* @1 */
}
#endif
/* back fill the correct commandSize */
Expand Down
10 changes: 5 additions & 5 deletions utils/tsscrypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ TPM_RC TSS_RSAPublicEncrypt(unsigned char *encrypt_data, /* encrypted data */
EVP_PKEY_CTX_free(ctx); /* @1 */
#endif
TSS_RsaFree(rsa_pub_key); /* @3 */
free(padded_data); /* @2 */
TSS_Free(&padded_data); /* @2 */
return rc;
}

Expand Down Expand Up @@ -1310,8 +1310,8 @@ TPM_RC TSS_AES_KeyFree(void *tssSessionEncKey,

#ifndef TPM_TSS_NOFILE
#if OPENSSL_VERSION_NUMBER < 0x30000000
free(tssSessionEncKey);
free(tssSessionDecKey);
TSS_Free(&tssSessionEncKey);
TSS_Free(&tssSessionDecKey);
#else
EVP_CIPHER_CTX_free(tssSessionEncKey);
EVP_CIPHER_CTX_free(tssSessionDecKey);
Expand Down Expand Up @@ -1438,7 +1438,7 @@ TPM_RC TSS_AES_KeyGenerate(void *tssSessionEncKey,

}
#endif
free(envKeyBin); /* @1 */
TSS_Free(&envKeyBin); /* @1 */
return rc;
}

Expand Down Expand Up @@ -1520,7 +1520,7 @@ TPM_RC TSS_AES_Encrypt(void *tssSessionEncKey,
}
}
#endif
free(decrypt_data_pad); /* @1 */
TSS_Free(&decrypt_data_pad); /* @1 */
return rc;
}

Expand Down
4 changes: 2 additions & 2 deletions utils/tsscryptoh.c
Original file line number Diff line number Diff line change
Expand Up @@ -542,8 +542,8 @@ TPM_RC TSS_RSA_padding_add_PKCS1_OAEP(unsigned char *em, uint32_t emLen,
/* 2.i. 0x00, maskedSeed, and maskedDb to form EM */
/* NOTE Created directly in em */
}
free(dbMask); /* @1 */
free(seed); /* @2 */
TSS_Free(&dbMask); /* @1 */
TSS_Free(&seed); /* @2 */
return rc;
}

Expand Down
11 changes: 5 additions & 6 deletions utils/tssfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ TPM_RC TSS_File_ReadBinaryFile(unsigned char **data, /* must be freed by cal
}
if (rc != 0) {
if (tssVerbose) printf("TSS_File_ReadBinaryFile: Error reading %s\n", filename);
free(*data);
*data = NULL;
TSS_Free(data);
}
return rc;
}
Expand Down Expand Up @@ -208,7 +207,7 @@ TPM_RC TSS_File_ReadStructure(void *structure,
buffer1 = buffer;
rc = unmarshalFunction(structure, &buffer1, &ilength);
}
free(buffer); /* @1 */
TSS_Free(&buffer); /* @1 */
return rc;
}

Expand Down Expand Up @@ -240,7 +239,7 @@ TPM_RC TSS_File_ReadStructureFlag(void *structure,
buffer1 = buffer;
rc = unmarshalFunction(structure, &buffer1, &ilength, allowNull);
}
free(buffer); /* @1 */
TSS_Free(&buffer); /* @1 */
return rc;
}

Expand Down Expand Up @@ -268,7 +267,7 @@ TPM_RC TSS_File_WriteStructure(void *structure,
written,
filename);
}
free(buffer); /* @1 */
TSS_Free(&buffer); /* @1 */
return rc;
}

Expand Down Expand Up @@ -300,7 +299,7 @@ TPM_RC TSS_File_Read2B(TPM2B *tpm2b,
if (rc == 0) {
rc = TSS_TPM2B_Create(tpm2b, buffer, (uint16_t)length, targetSize);
}
free(buffer); /* @1 */
TSS_Free(&buffer); /* @1 */
return rc;
}

Expand Down
3 changes: 2 additions & 1 deletion utils/tssresponsecode.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,8 @@ const RC_TABLE tssTable [] = {
{TSS_RC_FAIL, "TSS_RC_FAIL - TSS internal failure"},
{TSS_RC_NO_SESSION_SLOT, "TSS_RC_NO_SESSION_SLOT - TSS context has no session slot for handle"},
{TSS_RC_NO_OBJECTPUBLIC_SLOT, "TSS_RC_NO_OBJECTPUBLIC_SLOT - TSS context has no object public slot for handle"},
{TSS_RC_NO_NVPUBLIC_SLOT, "TSS_RC_NO_NVPUBLIC_SLOT -TSS context has no NV public slot for handle"},
{TSS_RC_NO_NVPUBLIC_SLOT, "TSS_RC_NO_NVPUBLIC_SLOT - TSS context has no NV public slot for handle"},
{TSS_RC_PROPERTY_ALREADY_SET, "TSS_RC_PROPERTY_ALREADY_SET - A property has already been set and cannot be set again"},
};

#ifdef TPM_WINDOWS
Expand Down
Loading