Skip to content

Commit 0c8a922

Browse files
committed
Use wolfCrypt's wc_ForceZero()
- Keep a gated fallback, wolfSSH_ForceZero(). - Add macro WS_FORCEZERO gated between wc_ForceZero() or wolfSSH_ForceZero(). - Use WS_FORCEZERO() in place of the local ForceZero(). - Change the fallback's length parameter from word32 to size_t to match wc_ForceZero()'s signature.
1 parent 9d1ca8f commit 0c8a922

7 files changed

Lines changed: 69 additions & 49 deletions

File tree

apps/wolfsshd/auth.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,11 +487,11 @@ static int CheckPasswordUnix(const char* usr, const byte* pw, word32 pwSz, WOLFS
487487
}
488488

489489
if (pwStr != NULL) {
490-
ForceZero(pwStr, pwSz + 1);
490+
WS_FORCEZERO(pwStr, pwSz + 1);
491491
WFREE(pwStr, NULL, DYNTYPE_STRING);
492492
}
493493
if (storedHashCpy != NULL) {
494-
ForceZero(storedHashCpy, (word32)WSTRLEN(storedHashCpy) + 1);
494+
WS_FORCEZERO(storedHashCpy, (word32)WSTRLEN(storedHashCpy) + 1);
495495
WFREE(storedHashCpy, NULL, DYNTYPE_STRING);
496496
}
497497

src/internal.c

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ static void HandshakeInfoFree(HandshakeInfo* hs, void* heap)
659659
if (hs->kexHashId != WC_HASH_TYPE_NONE) {
660660
wc_HashFree(&hs->kexHash, (enum wc_HashType)hs->kexHashId);
661661
}
662-
ForceZero(hs, sizeof(HandshakeInfo));
662+
WS_FORCEZERO(hs, sizeof(HandshakeInfo));
663663
WFREE(hs, heap, DYNTYPE_HS);
664664
}
665665
}
@@ -1160,7 +1160,7 @@ void CtxResourceFree(WOLFSSH_CTX* ctx)
11601160

11611161
for (i = 0; i < ctx->privateKeyCount; i++) {
11621162
if (ctx->privateKey[i].key != NULL) {
1163-
ForceZero(ctx->privateKey[i].key, ctx->privateKey[i].keySz);
1163+
WS_FORCEZERO(ctx->privateKey[i].key, ctx->privateKey[i].keySz);
11641164
WFREE(ctx->privateKey[i].key, ctx->heap, DYNTYPE_PRIVKEY);
11651165
ctx->privateKey[i].key = NULL;
11661166
ctx->privateKey[i].keySz = 0;
@@ -1554,10 +1554,10 @@ void SshResourceFree(WOLFSSH* ssh, void* heap)
15541554
ShrinkBuffer(&ssh->inputBuffer, 1);
15551555
ShrinkBuffer(&ssh->outputBuffer, 1);
15561556
ShrinkBuffer(&ssh->extDataBuffer, 1);
1557-
ForceZero(ssh->k, ssh->kSz);
1557+
WS_FORCEZERO(ssh->k, ssh->kSz);
15581558
HandshakeInfoFree(ssh->handshake, heap);
1559-
ForceZero(&ssh->keys, sizeof(Keys));
1560-
ForceZero(&ssh->peerKeys, sizeof(Keys));
1559+
WS_FORCEZERO(&ssh->keys, sizeof(Keys));
1560+
WS_FORCEZERO(&ssh->peerKeys, sizeof(Keys));
15611561
if (ssh->rng) {
15621562
wc_FreeRng(ssh->rng);
15631563
WFREE(ssh->rng, heap, DYNTYPE_RNG);
@@ -1590,7 +1590,7 @@ void SshResourceFree(WOLFSSH* ssh, void* heap)
15901590
ssh->scpConfirmMsgSz = 0;
15911591
}
15921592
if (ssh->scpFileBuffer) {
1593-
ForceZero(ssh->scpFileBuffer, ssh->scpFileBufferSz);
1593+
WS_FORCEZERO(ssh->scpFileBuffer, ssh->scpFileBufferSz);
15941594
WFREE(ssh->scpFileBuffer, heap, DYNTYPE_BUFFER);
15951595
ssh->scpFileBuffer = NULL;
15961596
ssh->scpFileBufferSz = 0;
@@ -2674,7 +2674,7 @@ static int UpdateHostCertificates(WOLFSSH_CTX* ctx,
26742674
* software key on the certificate slot and mark it TPM-backed. */
26752675
if (keyIsTpm) {
26762676
if (ctx->privateKey[certHint].key != NULL) {
2677-
ForceZero(ctx->privateKey[certHint].key,
2677+
WS_FORCEZERO(ctx->privateKey[certHint].key,
26782678
ctx->privateKey[certHint].keySz);
26792679
WFREE(ctx->privateKey[certHint].key,
26802680
ctx->heap, DYNTYPE_PRIVKEY);
@@ -2698,7 +2698,7 @@ static int UpdateHostCertificates(WOLFSSH_CTX* ctx,
26982698
WMEMCPY(key, ctx->privateKey[keyHint].key, keySz);
26992699

27002700
if (ctx->privateKey[certHint].key != NULL) {
2701-
ForceZero(ctx->privateKey[certHint].key,
2701+
WS_FORCEZERO(ctx->privateKey[certHint].key,
27022702
ctx->privateKey[certHint].keySz);
27032703
WFREE(ctx->privateKey[certHint].key,
27042704
ctx->heap, DYNTYPE_PRIVKEY);
@@ -2801,7 +2801,7 @@ static int SetHostPrivateKey(WOLFSSH_CTX* ctx,
28012801

28022802
if (pvtKey->publicKeyFmt == keyId) {
28032803
if (pvtKey->key != NULL) {
2804-
ForceZero(pvtKey->key, pvtKey->keySz);
2804+
WS_FORCEZERO(pvtKey->key, pvtKey->keySz);
28052805
WFREE(pvtKey->key, ctx->heap, dynamicType);
28062806
}
28072807
}
@@ -2859,7 +2859,7 @@ int wolfSSH_SetHostTpmKey(WOLFSSH_CTX* ctx, byte keyId)
28592859
pvtKey->publicKeyFmt = keyId;
28602860
}
28612861
else if (pvtKey->key != NULL) {
2862-
ForceZero(pvtKey->key, pvtKey->keySz);
2862+
WS_FORCEZERO(pvtKey->key, pvtKey->keySz);
28632863
WFREE(pvtKey->key, ctx->heap, DYNTYPE_PRIVKEY);
28642864
}
28652865

@@ -2874,7 +2874,7 @@ int wolfSSH_SetHostTpmKey(WOLFSSH_CTX* ctx, byte keyId)
28742874
for (certIdx = 0; certIdx < ctx->privateKeyCount; certIdx++) {
28752875
if (ctx->privateKey[certIdx].publicKeyFmt == certId) {
28762876
if (ctx->privateKey[certIdx].key != NULL) {
2877-
ForceZero(ctx->privateKey[certIdx].key,
2877+
WS_FORCEZERO(ctx->privateKey[certIdx].key,
28782878
ctx->privateKey[certIdx].keySz);
28792879
WFREE(ctx->privateKey[certIdx].key, ctx->heap,
28802880
DYNTYPE_PRIVKEY);
@@ -2961,7 +2961,7 @@ int wolfSSH_ProcessBuffer(WOLFSSH_CTX* ctx,
29612961
if (type == BUFTYPE_PRIVKEY) {
29622962
/* wc_KeyPemToDer may have written partial key material;
29632963
* zeroize before free on the private-key path. */
2964-
ForceZero(der, inSz);
2964+
WS_FORCEZERO(der, inSz);
29652965
}
29662966
WFREE(der, heap, dynamicType);
29672967
return WS_BAD_FILE_E;
@@ -2979,7 +2979,7 @@ int wolfSSH_ProcessBuffer(WOLFSSH_CTX* ctx,
29792979
ret = IdentifyAsn1Key(der, derSz, 1, ctx->heap, NULL);
29802980
if (ret < 0) {
29812981
if (der != NULL) {
2982-
ForceZero(der, derSz);
2982+
WS_FORCEZERO(der, derSz);
29832983
WFREE(der, heap, dynamicType);
29842984
}
29852985
return ret;
@@ -3108,7 +3108,7 @@ int GenerateKey(byte hashId, byte keyId,
31083108
ret = wc_HashFinal(&hash, enmhashId, lastBlock);
31093109
if (ret == WS_SUCCESS)
31103110
WMEMCPY(key, lastBlock, remainder);
3111-
ForceZero(lastBlock, sizeof(lastBlock));
3111+
WS_FORCEZERO(lastBlock, sizeof(lastBlock));
31123112
}
31133113
}
31143114
else {
@@ -3154,7 +3154,7 @@ int GenerateKey(byte hashId, byte keyId,
31543154
ret = wc_HashFinal(&hash, enmhashId, lastBlock);
31553155
if (ret == WS_SUCCESS)
31563156
WMEMCPY(key + runningKeySz, lastBlock, remainder);
3157-
ForceZero(lastBlock, sizeof(lastBlock));
3157+
WS_FORCEZERO(lastBlock, sizeof(lastBlock));
31583158
}
31593159
}
31603160
}
@@ -6303,7 +6303,7 @@ static int KeyAgreeDh_client(WOLFSSH* ssh, byte hashId,
63036303
ret = WS_CRYPTO_FAILED;
63046304
}
63056305
}
6306-
ForceZero(ssh->handshake->x, ssh->handshake->xSz);
6306+
WS_FORCEZERO(ssh->handshake->x, ssh->handshake->xSz);
63076307
wc_FreeDhKey(&ssh->handshake->privKey.dh);
63086308

63096309
WLOG(WS_LOG_DEBUG, "Leaving KeyAgreeDh_client(), ret = %d", ret);
@@ -6697,7 +6697,7 @@ static int KeyAgreeEcdhMlKem_client(WOLFSSH* ssh, byte hashId,
66976697
}
66986698

66996699
if (sharedSecretHash) {
6700-
ForceZero(sharedSecretHash, sharedSecretHashSz);
6700+
WS_FORCEZERO(sharedSecretHash, sharedSecretHashSz);
67016701
WFREE(sharedSecretHash, ssh->ctx->heap, DYNTYPE_PRIVKEY);
67026702
}
67036703

@@ -8962,7 +8962,7 @@ static int DoUserAuthRequestMlDsa(WOLFSSH* ssh,
89628962
}
89638963

89648964
if (checkData != NULL) {
8965-
ForceZero(checkData, checkDataSz);
8965+
WS_FORCEZERO(checkData, checkDataSz);
89668966
WFREE(checkData, ssh->ctx->heap, DYNTYPE_TEMP);
89678967
}
89688968

@@ -11523,7 +11523,7 @@ static int DoPacket(WOLFSSH* ssh, byte* bufferConsumed)
1152311523
in += AES_BLOCK_SIZE;
1152411524
sz -= AES_BLOCK_SIZE;
1152511525
}
11526-
ForceZero(scratch, sizeof(scratch));
11526+
WS_FORCEZERO(scratch, sizeof(scratch));
1152711527

1152811528
return ret;
1152911529
}
@@ -13497,7 +13497,7 @@ static int KeyAgreeDh_server(WOLFSSH* ssh, byte hashId, byte* f, word32* fSz)
1349713497
ssh->handshake->e, ssh->handshake->eSz);
1349813498
PRIVATE_KEY_LOCK();
1349913499
}
13500-
ForceZero(y_ptr, ySz);
13500+
WS_FORCEZERO(y_ptr, ySz);
1350113501
wc_FreeDhKey(privKey);
1350213502
}
1350313503
#ifdef WOLFSSH_SMALL_STACK
@@ -13965,7 +13965,7 @@ static int KeyAgreeEcdhMlKem_server(WOLFSSH* ssh, byte hashId,
1396513965
}
1396613966

1396713967
if (sharedSecretHash) {
13968-
ForceZero(sharedSecretHash, sharedSecretHashSz);
13968+
WS_FORCEZERO(sharedSecretHash, sharedSecretHashSz);
1396913969
WFREE(sharedSecretHash, ssh->ctx->heap, DYNTYPE_PRIVKEY);
1397013970
}
1397113971

@@ -14852,7 +14852,7 @@ int SendKexDhReply(WOLFSSH* ssh)
1485214852

1485314853
WLOG(WS_LOG_DEBUG, "Leaving SendKexDhReply(), ret = %d", ret);
1485414854
if (sigKeyBlock_ptr) {
14855-
ForceZero(sigKeyBlock_ptr, sizeof(struct wolfSSH_sigKeyBlockFull));
14855+
WS_FORCEZERO(sigKeyBlock_ptr, sizeof(struct wolfSSH_sigKeyBlockFull));
1485614856
WFREE(sigKeyBlock_ptr, heap, DYNTYPE_PRIVKEY);
1485714857
}
1485814858
#ifdef WOLFSSH_SMALL_STACK
@@ -16247,7 +16247,7 @@ static int BuildUserAuthRequestRsa(WOLFSSH* ssh,
1624716247
*idx = begin;
1624816248

1624916249
if (checkData != NULL) {
16250-
ForceZero(checkData, checkDataSz);
16250+
WS_FORCEZERO(checkData, checkDataSz);
1625116251
WFREE(checkData, ssh->ctx->heap, DYNTYPE_TEMP);
1625216252
}
1625316253

@@ -16427,7 +16427,7 @@ static int BuildUserAuthRequestRsaCert(WOLFSSH* ssh,
1642716427
*idx = begin;
1642816428

1642916429
if (checkData != NULL) {
16430-
ForceZero(checkData, checkDataSz);
16430+
WS_FORCEZERO(checkData, checkDataSz);
1643116431
WFREE(checkData, ssh->ctx->heap, DYNTYPE_TEMP);
1643216432
}
1643316433

@@ -16694,7 +16694,7 @@ static int BuildUserAuthRequestEcc(WOLFSSH* ssh,
1669416694
*idx = begin;
1669516695

1669616696
if (checkData != NULL) {
16697-
ForceZero(checkData, checkDataSz);
16697+
WS_FORCEZERO(checkData, checkDataSz);
1669816698
WFREE(checkData, ssh->ctx->heap, DYNTYPE_TEMP);
1669916699
}
1670016700

@@ -16954,7 +16954,7 @@ static int BuildUserAuthRequestEccCert(WOLFSSH* ssh,
1695416954
*idx = begin;
1695516955

1695616956
if (checkData != NULL) {
16957-
ForceZero(checkData, checkDataSz);
16957+
WS_FORCEZERO(checkData, checkDataSz);
1695816958
WFREE(checkData, ssh->ctx->heap, DYNTYPE_TEMP);
1695916959
}
1696016960

@@ -17109,7 +17109,7 @@ static int BuildUserAuthRequestEd25519(WOLFSSH* ssh,
1710917109
*idx = begin;
1711017110

1711117111
if (checkData != NULL) {
17112-
ForceZero(checkData, checkDataSz);
17112+
WS_FORCEZERO(checkData, checkDataSz);
1711317113
WFREE(checkData, keySig->heap, DYNTYPE_TEMP);
1711417114
}
1711517115

@@ -17356,7 +17356,7 @@ static int BuildUserAuthRequestMlDsa(WOLFSSH* ssh,
1735617356
*idx = begin;
1735717357

1735817358
if (checkData != NULL) {
17359-
ForceZero(checkData, checkDataSz);
17359+
WS_FORCEZERO(checkData, checkDataSz);
1736017360
WFREE(checkData, keySig->heap, DYNTYPE_TEMP);
1736117361
}
1736217362

@@ -17769,7 +17769,7 @@ int SendUserAuthKeyboardResponse(WOLFSSH* ssh)
1776917769
if (ret != WS_WANT_WRITE && ret != WS_SUCCESS)
1777017770
PurgePacket(ssh);
1777117771

17772-
ForceZero(&authData, sizeof(WS_UserAuthData));
17772+
WS_FORCEZERO(&authData, sizeof(WS_UserAuthData));
1777317773

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

@@ -17978,7 +17978,7 @@ int SendUserAuthRequest(WOLFSSH* ssh, byte authType, int addSig)
1797817978
if (ret != WS_WANT_WRITE && ret != WS_SUCCESS)
1797917979
PurgePacket(ssh);
1798017980

17981-
ForceZero(&authData, sizeof(WS_UserAuthData));
17981+
WS_FORCEZERO(&authData, sizeof(WS_UserAuthData));
1798217982
WLOG(WS_LOG_DEBUG, "Leaving SendUserAuthRequest(), ret = %d", ret);
1798317983

1798417984
if (keySig_ptr)

src/misc.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,15 @@ STATIC INLINE void c32toa(word32 u32, byte* c)
9898
}
9999

100100

101+
#ifdef WOLFSSH_NO_FORCEZERO
101102
/* Make sure compiler doesn't skip */
102-
STATIC INLINE void ForceZero(void* mem, word32 length)
103+
STATIC INLINE void wolfSSH_ForceZero(void* mem, size_t len)
103104
{
104105
volatile byte* z = (volatile byte*)mem;
105106

106-
while (length--) *z++ = 0;
107+
while (len--) *z++ = 0;
107108
}
109+
#endif
108110

109111

110112
/* check all length bytes for equality, return 0 on success */

src/ssh.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2054,7 +2054,7 @@ static int DoPemKey(const byte* in, word32 inSz, byte** out,
20542054
}
20552055
else {
20562056
WLOG(WS_LOG_DEBUG, "Unable to identify PEM key");
2057-
ForceZero(newKey, newKeySz);
2057+
WS_FORCEZERO(newKey, newKeySz);
20582058
if (*out == NULL) {
20592059
WFREE(newKey, heap, DYNTYPE_PRIVKEY);
20602060
}
@@ -2138,7 +2138,7 @@ static int DoOpenSshKey(const byte* in, word32 inSz, byte** out,
21382138
}
21392139
else {
21402140
WLOG(WS_LOG_DEBUG, "Unable to identify key");
2141-
ForceZero(newKey, newKeySz);
2141+
WS_FORCEZERO(newKey, newKeySz);
21422142
if (*out == NULL) {
21432143
WFREE(newKey, heap, DYNTYPE_PRIVKEY);
21442144
}
@@ -2295,7 +2295,7 @@ int wolfSSH_ReadKey_file(const char* name,
22952295
}
22962296

22972297
WFCLOSE(NULL, file);
2298-
ForceZero(in, inSz);
2298+
WS_FORCEZERO(in, inSz);
22992299
WFREE(in, heap, DYNTYPE_FILE);
23002300

23012301
return ret;

src/wolfsftp.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ static int wolfSSH_SFTP_buffer_set_size(WS_SFTP_BUFFER* buffer, word32 sz)
473473
#ifndef WOLFSSH_NO_SFTP_BUFFER_ZERO
474474
/* wipe any payload in the region being trimmed off before shrinking */
475475
if (buffer->data != NULL && sz < buffer->sz) {
476-
ForceZero(buffer->data + sz, buffer->sz - sz);
476+
WS_FORCEZERO(buffer->data + sz, buffer->sz - sz);
477477
}
478478
#endif
479479
buffer->sz = sz;
@@ -801,7 +801,7 @@ static void wolfSSH_SFTP_buffer_free(WOLFSSH* ssh, WS_SFTP_BUFFER* buffer)
801801
if (ssh != NULL && buffer != NULL) {
802802
if (buffer->data != NULL) {
803803
#ifndef WOLFSSH_NO_SFTP_BUFFER_ZERO
804-
ForceZero(buffer->data, buffer->sz);
804+
WS_FORCEZERO(buffer->data, buffer->sz);
805805
#endif
806806
WFREE(buffer->data, ssh->ctx->heap, DYNTYPE_BUFFER);
807807
buffer->data = NULL;
@@ -1434,7 +1434,7 @@ static void wolfSSH_SFTP_RecvSetSend(WOLFSSH* ssh, byte* buf, int sz)
14341434
/* free up existing data if needed */
14351435
if (buf != state->buffer.data && state->buffer.data != NULL) {
14361436
#ifndef WOLFSSH_NO_SFTP_BUFFER_ZERO
1437-
ForceZero(state->buffer.data, state->buffer.sz);
1437+
WS_FORCEZERO(state->buffer.data, state->buffer.sz);
14381438
#endif
14391439
WFREE(state->buffer.data, ssh->ctx->heap, DYNTYPE_BUFFER);
14401440
state->buffer.data = NULL;

0 commit comments

Comments
 (0)