Skip to content

Commit 6bdc6a7

Browse files
authored
Merge pull request #9618 from SparkiDev/volatile_multi_statement
Multiple volatile variables in a C statement undefined
2 parents 26b8795 + 1aa79af commit 6bdc6a7

4 files changed

Lines changed: 27 additions & 17 deletions

File tree

src/internal.c

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21209,13 +21209,11 @@ static byte MaskMac(const byte* data, int sz, int macSz, byte* expMac)
2120921209
int i, j;
2121021210
int r = 0;
2121121211
unsigned char mac[WC_MAX_DIGEST_SIZE];
21212-
volatile int scanStart = sz - 1 - TLS_MAX_PAD_SZ - macSz;
21212+
int scanStart = sz - 1 - TLS_MAX_PAD_SZ - macSz;
2121321213
volatile int macEnd = sz - 1 - data[sz - 1];
21214-
volatile int macStart = macEnd - macSz;
21214+
int macStart = macEnd - macSz;
2121521215
volatile int maskScanStart;
2121621216
volatile int maskMacStart;
21217-
volatile unsigned char started;
21218-
volatile unsigned char notEnded;
2121921217
unsigned char good = 0;
2122021218

2122121219
maskScanStart = ctMaskIntGTE(scanStart, 0);
@@ -21225,22 +21223,31 @@ static byte MaskMac(const byte* data, int sz, int macSz, byte* expMac)
2122521223

2122621224
/* Div on Intel has different speeds depending on value.
2122721225
* Use a bitwise AND or mod a specific value (converted to mul). */
21228-
if ((macSz & (macSz - 1)) == 0)
21229-
r = (macSz - (scanStart - macStart)) & (macSz - 1);
21226+
if ((macSz & (macSz - 1)) == 0) {
21227+
r = macSz - scanStart;
21228+
r += macStart;
21229+
r &= (macSz - 1);
21230+
}
2123021231
#ifndef NO_SHA
21231-
else if (macSz == WC_SHA_DIGEST_SIZE)
21232-
r = (macSz - (scanStart - macStart)) % WC_SHA_DIGEST_SIZE;
21232+
else if (macSz == WC_SHA_DIGEST_SIZE) {
21233+
r = macSz - scanStart;
21234+
r += macStart;
21235+
r %= WC_SHA_DIGEST_SIZE;
21236+
}
2123321237
#endif
2123421238
#ifdef WOLFSSL_SHA384
21235-
else if (macSz == WC_SHA384_DIGEST_SIZE)
21236-
r = (macSz - (scanStart - macStart)) % WC_SHA384_DIGEST_SIZE;
21239+
else if (macSz == WC_SHA384_DIGEST_SIZE) {
21240+
r = macSz - scanStart;
21241+
r += macStart;
21242+
r %= WC_SHA384_DIGEST_SIZE;
21243+
}
2123721244
#endif
2123821245

2123921246
XMEMSET(mac, 0, (size_t)(macSz));
2124021247
for (i = scanStart; i < sz; i += macSz) {
2124121248
for (j = 0; j < macSz && j + i < sz; j++) {
21242-
started = ctMaskGTE(i + j, macStart);
21243-
notEnded = ctMaskLT(i + j, macEnd);
21249+
unsigned char started = ctMaskGTE(i + j, macStart);
21250+
unsigned char notEnded = ctMaskLT(i + j, macEnd);
2124421251
mac[j] |= started & notEnded & data[i + j];
2124521252
}
2124621253
}

wolfcrypt/src/aes.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10355,7 +10355,8 @@ int WARN_UNUSED_RESULT AES_GCM_decrypt_C(
1035510355
/* now use res as a mask for constant time return of ret, unless tag
1035610356
* mismatch, whereupon AES_GCM_AUTH_E is returned.
1035710357
*/
10358-
ret = (ret & ~res) | (res & WC_NO_ERR_TRACE(AES_GCM_AUTH_E));
10358+
ret = (ret & ~res);
10359+
ret |= (res & WC_NO_ERR_TRACE(AES_GCM_AUTH_E));
1035910360
#endif
1036010361
return ret;
1036110362
}

wolfcrypt/src/misc.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,9 @@ WC_MISC_STATIC WC_INLINE void ctMaskCopy(byte mask, byte* dst, byte* src,
775775
#if !defined(WOLFSSL_NO_CT_OPS) && !defined(WOLFSSL_NO_CT_MAX_MIN) && \
776776
defined(WORD64_AVAILABLE)
777777
volatile word32 gte_mask = (word32)ctMaskWord32GTE(a, b);
778-
return (a & ~gte_mask) | (b & gte_mask);
778+
word32 r = (a & ~gte_mask);
779+
r |= (b & gte_mask);
780+
return r;
779781
#else /* WOLFSSL_NO_CT_OPS */
780782
return a > b ? b : a;
781783
#endif /* WOLFSSL_NO_CT_OPS */

wolfcrypt/src/sp_int.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18254,22 +18254,22 @@ int sp_to_unsigned_bin_len_ct(const sp_int* a, byte* out, int outSz)
1825418254
/* Start at the end of the buffer - least significant byte. */
1825518255
int j;
1825618256
unsigned int i;
18257-
volatile sp_int_digit mask = (sp_int_digit)-1;
18257+
byte mask = (byte)-1;
1825818258
sp_int_digit d;
1825918259

1826018260
/* Put each digit in. */
1826118261
i = 0;
1826218262
for (j = outSz - 1; j >= 0; ) {
1826318263
unsigned int b;
18264-
volatile unsigned int notFull = (i < (unsigned int)a->used - 1);
18264+
volatile byte notFull = ctMaskLT((int)i, (int)a->used - 1);
1826518265

1826618266
d = a->dp[i];
1826718267
/* Place each byte of a digit into the buffer. */
1826818268
for (b = 0; (j >= 0) && (b < SP_WORD_SIZEOF); b++) {
1826918269
out[j--] = (byte)(d & mask);
1827018270
d >>= 8;
1827118271
}
18272-
mask &= (sp_int_digit)(-(int)notFull);
18272+
mask &= notFull;
1827318273
i += (unsigned int)(1 & mask);
1827418274
}
1827518275
}

0 commit comments

Comments
 (0)