Skip to content

Commit 0a61997

Browse files
authored
Merge pull request #10045 from embhorn/zd21385
Fix IAR warning about volatile access
2 parents 5b2e8b1 + 1a1bdb2 commit 0a61997

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

src/internal.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41507,6 +41507,7 @@ static int DefTicketEncCb(WOLFSSL* ssl, byte key_name[WOLFSSL_TICKET_NAME_SZ],
4150741507
{
4150841508
RsaKey* key = (RsaKey*)ssl->hsKey;
4150941509
volatile int lenErrMask;
41510+
int mask;
4151041511

4151141512
ret = RsaDec(ssl,
4151241513
input + args->idx,
@@ -41533,8 +41534,11 @@ static int DefTicketEncCb(WOLFSSL* ssl, byte key_name[WOLFSSL_TICKET_NAME_SZ],
4153341534
goto exit_dcke;
4153441535

4153541536
lenErrMask = 0 - (SECRET_LEN != args->sigSz);
41536-
args->lastErr = (ret & (~lenErrMask)) |
41537-
(WC_NO_ERR_TRACE(RSA_PAD_E) & lenErrMask);
41537+
/* Snapshot volatile to avoid multiple volatile
41538+
* accesses per expression. */
41539+
mask = lenErrMask;
41540+
args->lastErr = (ret & (~mask)) |
41541+
(WC_NO_ERR_TRACE(RSA_PAD_E) & mask);
4153841542
ret = 0;
4153941543
break;
4154041544
} /* rsa_kea */

wolfcrypt/src/rsa.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1890,6 +1890,8 @@ static int RsaUnPad(const byte *pkcsBlock, unsigned int pkcsBlockLen,
18901890
volatile byte invalid = 0;
18911891
volatile byte minPad;
18921892
volatile int invalidMask;
1893+
byte inv;
1894+
word16 sep;
18931895

18941896
i = 0;
18951897
/* Decrypted with private key - unpad must be constant time. */
@@ -1900,18 +1902,24 @@ static int RsaUnPad(const byte *pkcsBlock, unsigned int pkcsBlockLen,
19001902
pastSep |= ctMask16Eq(pkcsBlock[j], 0x00);
19011903
}
19021904

1905+
/* Snapshot volatiles to avoid multiple volatile accesses per
1906+
* expression. */
1907+
inv = invalid;
1908+
sep = pastSep;
1909+
19031910
/* Minimum of 11 bytes of pre-message data - including leading 0x00. */
19041911
minPad = ctMaskLT(i, RSA_MIN_PAD_SZ);
1905-
invalid |= minPad;
1912+
inv |= minPad;
19061913
/* Must have seen separator. */
1907-
invalid |= (byte)~pastSep;
1914+
inv |= (byte)~sep;
19081915
/* First byte must be 0x00. */
1909-
invalid |= ctMaskNotEq(pkcsBlock[0], 0x00);
1916+
inv |= ctMaskNotEq(pkcsBlock[0], 0x00);
19101917
/* Check against expected block type: padValue */
1911-
invalid |= ctMaskNotEq(pkcsBlock[1], padValue);
1918+
inv |= ctMaskNotEq(pkcsBlock[1], padValue);
19121919

1920+
invalid = inv;
19131921
*output = (byte *)(pkcsBlock + i);
1914-
invalidMask = (int)-1 + (int)(invalid >> 7);
1922+
invalidMask = (int)-1 + (int)(inv >> 7);
19151923
ret = invalidMask & ((int)pkcsBlockLen - i);
19161924
}
19171925
#endif

0 commit comments

Comments
 (0)