Merge pull request #2464 from SparkiDev/rshift_neg

Use constant time comparison in MaskMac with scanStart and macStart
This commit is contained in:
toddouska
2019-09-13 11:56:12 -07:00
committed by GitHub
3 changed files with 12 additions and 5 deletions

View File

@ -13241,8 +13241,8 @@ static byte MaskMac(const byte* data, int sz, int macSz, byte* expMac)
unsigned char started, notEnded; unsigned char started, notEnded;
unsigned char good = 0; unsigned char good = 0;
scanStart &= (~scanStart) >> (sizeof(int) * 8 - 1); scanStart &= ctMaskIntGTE(scanStart, 0);
macStart &= (~macStart) >> (sizeof(int) * 8 - 1); macStart &= ctMaskIntGTE(macStart, 0);
/* Div on Intel has different speeds depending on value. /* Div on Intel has different speeds depending on value.
* Use a bitwise AND or mod a specific value (converted to mul). */ * Use a bitwise AND or mod a specific value (converted to mul). */

View File

@ -332,6 +332,12 @@ WC_STATIC WC_INLINE byte ctMaskGTE(int a, int b)
return (((word32)a - b ) >> 31) - 1; return (((word32)a - b ) >> 31) - 1;
} }
/* Constant time - mask set when a >= b. */
WC_STATIC WC_INLINE int ctMaskIntGTE(int a, int b)
{
return (((word32)a - b ) >> 31) - 1;
}
/* Constant time - mask set when a < b. */ /* Constant time - mask set when a < b. */
WC_STATIC WC_INLINE byte ctMaskLT(int a, int b) WC_STATIC WC_INLINE byte ctMaskLT(int a, int b)
{ {
@ -347,18 +353,18 @@ WC_STATIC WC_INLINE byte ctMaskLTE(int a, int b)
/* Constant time - mask set when a == b. */ /* Constant time - mask set when a == b. */
WC_STATIC WC_INLINE byte ctMaskEq(int a, int b) WC_STATIC WC_INLINE byte ctMaskEq(int a, int b)
{ {
return 0 - (a == b); return (~ctMaskGT(a, b)) & (~ctMaskLT(a, b));
} }
WC_STATIC WC_INLINE word16 ctMask16Eq(int a, int b) WC_STATIC WC_INLINE word16 ctMask16Eq(int a, int b)
{ {
return 0 - (a == b); return (~ctMaskGT(a, b)) & (~ctMaskLT(a, b));
} }
/* Constant time - mask set when a != b. */ /* Constant time - mask set when a != b. */
WC_STATIC WC_INLINE byte ctMaskNotEq(int a, int b) WC_STATIC WC_INLINE byte ctMaskNotEq(int a, int b)
{ {
return 0 - (a != b); return ctMaskGT(a, b) | ctMaskLT(a, b);
} }
/* Constant time - select a when mask is set and b otherwise. */ /* Constant time - select a when mask is set and b otherwise. */

View File

@ -94,6 +94,7 @@ word32 btoi(byte b);
WOLFSSL_LOCAL byte ctMaskGT(int a, int b); WOLFSSL_LOCAL byte ctMaskGT(int a, int b);
WOLFSSL_LOCAL byte ctMaskGTE(int a, int b); WOLFSSL_LOCAL byte ctMaskGTE(int a, int b);
WOLFSSL_LOCAL int ctMaskIntGTE(int a, int b);
WOLFSSL_LOCAL byte ctMaskLT(int a, int b); WOLFSSL_LOCAL byte ctMaskLT(int a, int b);
WOLFSSL_LOCAL byte ctMaskLTE(int a, int b); WOLFSSL_LOCAL byte ctMaskLTE(int a, int b);
WOLFSSL_LOCAL byte ctMaskEq(int a, int b); WOLFSSL_LOCAL byte ctMaskEq(int a, int b);