Skip to content
Merged
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
4 changes: 2 additions & 2 deletions apps/wolfsshd/auth.c
Original file line number Diff line number Diff line change
Expand Up @@ -487,11 +487,11 @@ static int CheckPasswordUnix(const char* usr, const byte* pw, word32 pwSz, WOLFS
}

if (pwStr != NULL) {
ForceZero(pwStr, pwSz + 1);
WS_FORCEZERO(pwStr, pwSz + 1);
WFREE(pwStr, NULL, DYNTYPE_STRING);
}
if (storedHashCpy != NULL) {
ForceZero(storedHashCpy, (word32)WSTRLEN(storedHashCpy) + 1);
WS_FORCEZERO(storedHashCpy, (word32)WSTRLEN(storedHashCpy) + 1);
WFREE(storedHashCpy, NULL, DYNTYPE_STRING);
}

Expand Down
4 changes: 2 additions & 2 deletions src/agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ static int PostLock(WOLFSSH_AGENT_CTX* agent,
#else
WLOG(WS_LOG_AGENT, "Locking with passphrase");
#endif
ForceZero(pp, passphraseSz);
WS_FORCEZERO(pp, passphraseSz);
WFREE(pp, agent->heap, DYNTYPE_STRING);
}

Expand Down Expand Up @@ -421,7 +421,7 @@ static int PostUnlock(WOLFSSH_AGENT_CTX* agent,
#else
WLOG(WS_LOG_AGENT, "Unlocking with passphrase");
#endif
ForceZero(pp, passphraseSz);
WS_FORCEZERO(pp, passphraseSz);
WFREE(pp, agent->heap, DYNTYPE_STRING);
}

Expand Down
60 changes: 30 additions & 30 deletions src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ static void HandshakeInfoFree(HandshakeInfo* hs, void* heap)
if (hs->kexHashId != WC_HASH_TYPE_NONE) {
wc_HashFree(&hs->kexHash, (enum wc_HashType)hs->kexHashId);
}
ForceZero(hs, sizeof(HandshakeInfo));
WS_FORCEZERO(hs, sizeof(HandshakeInfo));
WFREE(hs, heap, DYNTYPE_HS);
Comment thread
ejohnstown marked this conversation as resolved.
}
}
Expand Down Expand Up @@ -1160,7 +1160,7 @@ void CtxResourceFree(WOLFSSH_CTX* ctx)

for (i = 0; i < ctx->privateKeyCount; i++) {
if (ctx->privateKey[i].key != NULL) {
ForceZero(ctx->privateKey[i].key, ctx->privateKey[i].keySz);
WS_FORCEZERO(ctx->privateKey[i].key, ctx->privateKey[i].keySz);
WFREE(ctx->privateKey[i].key, ctx->heap, DYNTYPE_PRIVKEY);
ctx->privateKey[i].key = NULL;
ctx->privateKey[i].keySz = 0;
Expand Down Expand Up @@ -1554,10 +1554,10 @@ void SshResourceFree(WOLFSSH* ssh, void* heap)
ShrinkBuffer(&ssh->inputBuffer, 1);
ShrinkBuffer(&ssh->outputBuffer, 1);
ShrinkBuffer(&ssh->extDataBuffer, 1);
ForceZero(ssh->k, ssh->kSz);
WS_FORCEZERO(ssh->k, ssh->kSz);
HandshakeInfoFree(ssh->handshake, heap);
ForceZero(&ssh->keys, sizeof(Keys));
ForceZero(&ssh->peerKeys, sizeof(Keys));
WS_FORCEZERO(&ssh->keys, sizeof(Keys));
WS_FORCEZERO(&ssh->peerKeys, sizeof(Keys));
if (ssh->rng) {
wc_FreeRng(ssh->rng);
WFREE(ssh->rng, heap, DYNTYPE_RNG);
Expand Down Expand Up @@ -1590,7 +1590,7 @@ void SshResourceFree(WOLFSSH* ssh, void* heap)
ssh->scpConfirmMsgSz = 0;
}
if (ssh->scpFileBuffer) {
ForceZero(ssh->scpFileBuffer, ssh->scpFileBufferSz);
WS_FORCEZERO(ssh->scpFileBuffer, ssh->scpFileBufferSz);
WFREE(ssh->scpFileBuffer, heap, DYNTYPE_BUFFER);
ssh->scpFileBuffer = NULL;
ssh->scpFileBufferSz = 0;
Expand Down Expand Up @@ -2674,7 +2674,7 @@ static int UpdateHostCertificates(WOLFSSH_CTX* ctx,
* software key on the certificate slot and mark it TPM-backed. */
if (keyIsTpm) {
if (ctx->privateKey[certHint].key != NULL) {
ForceZero(ctx->privateKey[certHint].key,
WS_FORCEZERO(ctx->privateKey[certHint].key,
ctx->privateKey[certHint].keySz);
WFREE(ctx->privateKey[certHint].key,
ctx->heap, DYNTYPE_PRIVKEY);
Expand All @@ -2698,7 +2698,7 @@ static int UpdateHostCertificates(WOLFSSH_CTX* ctx,
WMEMCPY(key, ctx->privateKey[keyHint].key, keySz);

if (ctx->privateKey[certHint].key != NULL) {
ForceZero(ctx->privateKey[certHint].key,
WS_FORCEZERO(ctx->privateKey[certHint].key,
ctx->privateKey[certHint].keySz);
WFREE(ctx->privateKey[certHint].key,
ctx->heap, DYNTYPE_PRIVKEY);
Expand Down Expand Up @@ -2801,7 +2801,7 @@ static int SetHostPrivateKey(WOLFSSH_CTX* ctx,

if (pvtKey->publicKeyFmt == keyId) {
if (pvtKey->key != NULL) {
ForceZero(pvtKey->key, pvtKey->keySz);
WS_FORCEZERO(pvtKey->key, pvtKey->keySz);
WFREE(pvtKey->key, ctx->heap, dynamicType);
}
}
Expand Down Expand Up @@ -2859,7 +2859,7 @@ int wolfSSH_SetHostTpmKey(WOLFSSH_CTX* ctx, byte keyId)
pvtKey->publicKeyFmt = keyId;
}
else if (pvtKey->key != NULL) {
ForceZero(pvtKey->key, pvtKey->keySz);
WS_FORCEZERO(pvtKey->key, pvtKey->keySz);
WFREE(pvtKey->key, ctx->heap, DYNTYPE_PRIVKEY);
}

Expand All @@ -2874,7 +2874,7 @@ int wolfSSH_SetHostTpmKey(WOLFSSH_CTX* ctx, byte keyId)
for (certIdx = 0; certIdx < ctx->privateKeyCount; certIdx++) {
if (ctx->privateKey[certIdx].publicKeyFmt == certId) {
if (ctx->privateKey[certIdx].key != NULL) {
ForceZero(ctx->privateKey[certIdx].key,
WS_FORCEZERO(ctx->privateKey[certIdx].key,
ctx->privateKey[certIdx].keySz);
WFREE(ctx->privateKey[certIdx].key, ctx->heap,
DYNTYPE_PRIVKEY);
Expand Down Expand Up @@ -2961,7 +2961,7 @@ int wolfSSH_ProcessBuffer(WOLFSSH_CTX* ctx,
if (type == BUFTYPE_PRIVKEY) {
/* wc_KeyPemToDer may have written partial key material;
* zeroize before free on the private-key path. */
ForceZero(der, inSz);
WS_FORCEZERO(der, inSz);
}
WFREE(der, heap, dynamicType);
return WS_BAD_FILE_E;
Expand All @@ -2979,7 +2979,7 @@ int wolfSSH_ProcessBuffer(WOLFSSH_CTX* ctx,
ret = IdentifyAsn1Key(der, derSz, 1, ctx->heap, NULL);
if (ret < 0) {
if (der != NULL) {
ForceZero(der, derSz);
WS_FORCEZERO(der, derSz);
WFREE(der, heap, dynamicType);
}
return ret;
Expand Down Expand Up @@ -3108,7 +3108,7 @@ int GenerateKey(byte hashId, byte keyId,
ret = wc_HashFinal(&hash, enmhashId, lastBlock);
if (ret == WS_SUCCESS)
WMEMCPY(key, lastBlock, remainder);
ForceZero(lastBlock, sizeof(lastBlock));
WS_FORCEZERO(lastBlock, sizeof(lastBlock));
}
}
else {
Expand Down Expand Up @@ -3154,7 +3154,7 @@ int GenerateKey(byte hashId, byte keyId,
ret = wc_HashFinal(&hash, enmhashId, lastBlock);
if (ret == WS_SUCCESS)
WMEMCPY(key + runningKeySz, lastBlock, remainder);
ForceZero(lastBlock, sizeof(lastBlock));
WS_FORCEZERO(lastBlock, sizeof(lastBlock));
}
}
}
Expand Down Expand Up @@ -6315,7 +6315,7 @@ static int KeyAgreeDh_client(WOLFSSH* ssh, byte hashId,
ret = WS_CRYPTO_FAILED;
}
}
ForceZero(ssh->handshake->x, ssh->handshake->xSz);
WS_FORCEZERO(ssh->handshake->x, ssh->handshake->xSz);
wc_FreeDhKey(&ssh->handshake->privKey.dh);

WLOG(WS_LOG_DEBUG, "Leaving KeyAgreeDh_client(), ret = %d", ret);
Expand Down Expand Up @@ -6709,7 +6709,7 @@ static int KeyAgreeEcdhMlKem_client(WOLFSSH* ssh, byte hashId,
}

if (sharedSecretHash) {
ForceZero(sharedSecretHash, sharedSecretHashSz);
WS_FORCEZERO(sharedSecretHash, sharedSecretHashSz);
WFREE(sharedSecretHash, ssh->ctx->heap, DYNTYPE_PRIVKEY);
}

Expand Down Expand Up @@ -8980,7 +8980,7 @@ static int DoUserAuthRequestMlDsa(WOLFSSH* ssh,
}

if (checkData != NULL) {
ForceZero(checkData, checkDataSz);
WS_FORCEZERO(checkData, checkDataSz);
WFREE(checkData, ssh->ctx->heap, DYNTYPE_TEMP);
}

Expand Down Expand Up @@ -11541,7 +11541,7 @@ static int DoPacket(WOLFSSH* ssh, byte* bufferConsumed)
in += AES_BLOCK_SIZE;
sz -= AES_BLOCK_SIZE;
}
ForceZero(scratch, sizeof(scratch));
WS_FORCEZERO(scratch, sizeof(scratch));

return ret;
}
Expand Down Expand Up @@ -13612,7 +13612,7 @@ static int KeyAgreeDh_server(WOLFSSH* ssh, byte hashId, byte* f, word32* fSz)
ssh->handshake->e, ssh->handshake->eSz);
PRIVATE_KEY_LOCK();
}
ForceZero(y_ptr, ySz);
WS_FORCEZERO(y_ptr, ySz);
if (keyInited)
wc_FreeDhKey(privKey);
}
Expand Down Expand Up @@ -14081,7 +14081,7 @@ static int KeyAgreeEcdhMlKem_server(WOLFSSH* ssh, byte hashId,
}

if (sharedSecretHash) {
ForceZero(sharedSecretHash, sharedSecretHashSz);
WS_FORCEZERO(sharedSecretHash, sharedSecretHashSz);
WFREE(sharedSecretHash, ssh->ctx->heap, DYNTYPE_PRIVKEY);
}

Expand Down Expand Up @@ -14968,7 +14968,7 @@ int SendKexDhReply(WOLFSSH* ssh)

WLOG(WS_LOG_DEBUG, "Leaving SendKexDhReply(), ret = %d", ret);
if (sigKeyBlock_ptr) {
ForceZero(sigKeyBlock_ptr, sizeof(struct wolfSSH_sigKeyBlockFull));
WS_FORCEZERO(sigKeyBlock_ptr, sizeof(struct wolfSSH_sigKeyBlockFull));
WFREE(sigKeyBlock_ptr, heap, DYNTYPE_PRIVKEY);
}
#ifdef WOLFSSH_SMALL_STACK
Expand Down Expand Up @@ -16414,7 +16414,7 @@ static int BuildUserAuthRequestRsa(WOLFSSH* ssh,
*idx = begin;

if (checkData != NULL) {
ForceZero(checkData, checkDataSz);
WS_FORCEZERO(checkData, checkDataSz);
WFREE(checkData, ssh->ctx->heap, DYNTYPE_TEMP);
}

Expand Down Expand Up @@ -16594,7 +16594,7 @@ static int BuildUserAuthRequestRsaCert(WOLFSSH* ssh,
*idx = begin;

if (checkData != NULL) {
ForceZero(checkData, checkDataSz);
WS_FORCEZERO(checkData, checkDataSz);
WFREE(checkData, ssh->ctx->heap, DYNTYPE_TEMP);
}

Expand Down Expand Up @@ -16861,7 +16861,7 @@ static int BuildUserAuthRequestEcc(WOLFSSH* ssh,
*idx = begin;

if (checkData != NULL) {
ForceZero(checkData, checkDataSz);
WS_FORCEZERO(checkData, checkDataSz);
WFREE(checkData, ssh->ctx->heap, DYNTYPE_TEMP);
}

Expand Down Expand Up @@ -17121,7 +17121,7 @@ static int BuildUserAuthRequestEccCert(WOLFSSH* ssh,
*idx = begin;

if (checkData != NULL) {
ForceZero(checkData, checkDataSz);
WS_FORCEZERO(checkData, checkDataSz);
WFREE(checkData, ssh->ctx->heap, DYNTYPE_TEMP);
}

Expand Down Expand Up @@ -17276,7 +17276,7 @@ static int BuildUserAuthRequestEd25519(WOLFSSH* ssh,
*idx = begin;

if (checkData != NULL) {
ForceZero(checkData, checkDataSz);
WS_FORCEZERO(checkData, checkDataSz);
WFREE(checkData, keySig->heap, DYNTYPE_TEMP);
}

Expand Down Expand Up @@ -17523,7 +17523,7 @@ static int BuildUserAuthRequestMlDsa(WOLFSSH* ssh,
*idx = begin;

if (checkData != NULL) {
ForceZero(checkData, checkDataSz);
WS_FORCEZERO(checkData, checkDataSz);
WFREE(checkData, keySig->heap, DYNTYPE_TEMP);
}

Expand Down Expand Up @@ -17936,7 +17936,7 @@ int SendUserAuthKeyboardResponse(WOLFSSH* ssh)
if (ret != WS_WANT_WRITE && ret != WS_SUCCESS)
PurgePacket(ssh);

ForceZero(&authData, sizeof(WS_UserAuthData));
WS_FORCEZERO(&authData, sizeof(WS_UserAuthData));

WLOG(WS_LOG_DEBUG, "Leaving SendUserAuthKeyboardResponse(), ret = %d", ret);

Expand Down Expand Up @@ -18145,7 +18145,7 @@ int SendUserAuthRequest(WOLFSSH* ssh, byte authType, int addSig)
if (ret != WS_WANT_WRITE && ret != WS_SUCCESS)
PurgePacket(ssh);

ForceZero(&authData, sizeof(WS_UserAuthData));
WS_FORCEZERO(&authData, sizeof(WS_UserAuthData));
WLOG(WS_LOG_DEBUG, "Leaving SendUserAuthRequest(), ret = %d", ret);

if (keySig_ptr)
Expand Down
6 changes: 4 additions & 2 deletions src/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,15 @@ STATIC INLINE void c32toa(word32 u32, byte* c)
}


#ifdef WOLFSSH_NO_FORCEZERO
/* Make sure compiler doesn't skip */
STATIC INLINE void ForceZero(void* mem, word32 length)
STATIC INLINE void wolfSSH_ForceZero(void* mem, size_t len)
{
volatile byte* z = (volatile byte*)mem;

while (length--) *z++ = 0;
while (len--) *z++ = 0;
}
#endif


/* check all length bytes for equality, return 0 on success */
Expand Down
6 changes: 3 additions & 3 deletions src/ssh.c
Original file line number Diff line number Diff line change
Expand Up @@ -2054,7 +2054,7 @@ static int DoPemKey(const byte* in, word32 inSz, byte** out,
}
else {
WLOG(WS_LOG_DEBUG, "Unable to identify PEM key");
ForceZero(newKey, newKeySz);
WS_FORCEZERO(newKey, newKeySz);
if (*out == NULL) {
WFREE(newKey, heap, DYNTYPE_PRIVKEY);
}
Expand Down Expand Up @@ -2138,7 +2138,7 @@ static int DoOpenSshKey(const byte* in, word32 inSz, byte** out,
}
else {
WLOG(WS_LOG_DEBUG, "Unable to identify key");
ForceZero(newKey, newKeySz);
WS_FORCEZERO(newKey, newKeySz);
if (*out == NULL) {
WFREE(newKey, heap, DYNTYPE_PRIVKEY);
}
Expand Down Expand Up @@ -2295,7 +2295,7 @@ int wolfSSH_ReadKey_file(const char* name,
}

WFCLOSE(NULL, file);
ForceZero(in, inSz);
WS_FORCEZERO(in, inSz);
WFREE(in, heap, DYNTYPE_FILE);

return ret;
Expand Down
6 changes: 3 additions & 3 deletions src/wolfsftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ static int wolfSSH_SFTP_buffer_set_size(WS_SFTP_BUFFER* buffer, word32 sz)
#ifndef WOLFSSH_NO_SFTP_BUFFER_ZERO
/* wipe any payload in the region being trimmed off before shrinking */
if (buffer->data != NULL && sz < buffer->sz) {
ForceZero(buffer->data + sz, buffer->sz - sz);
WS_FORCEZERO(buffer->data + sz, buffer->sz - sz);
}
#endif
buffer->sz = sz;
Expand Down Expand Up @@ -801,7 +801,7 @@ static void wolfSSH_SFTP_buffer_free(WOLFSSH* ssh, WS_SFTP_BUFFER* buffer)
if (ssh != NULL && buffer != NULL) {
if (buffer->data != NULL) {
#ifndef WOLFSSH_NO_SFTP_BUFFER_ZERO
ForceZero(buffer->data, buffer->sz);
WS_FORCEZERO(buffer->data, buffer->sz);
#endif
WFREE(buffer->data, ssh->ctx->heap, DYNTYPE_BUFFER);
buffer->data = NULL;
Expand Down Expand Up @@ -1434,7 +1434,7 @@ static void wolfSSH_SFTP_RecvSetSend(WOLFSSH* ssh, byte* buf, int sz)
/* free up existing data if needed */
if (buf != state->buffer.data && state->buffer.data != NULL) {
#ifndef WOLFSSH_NO_SFTP_BUFFER_ZERO
ForceZero(state->buffer.data, state->buffer.sz);
WS_FORCEZERO(state->buffer.data, state->buffer.sz);
#endif
WFREE(state->buffer.data, ssh->ctx->heap, DYNTYPE_BUFFER);
state->buffer.data = NULL;
Expand Down
Loading
Loading