mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-06-30 19:12:48 -05:00
Use a timing-safe memory compare to verify MACs.
Now that we have modes in which the MAC verification happens before any other crypto operation and hence will be the only thing seen by an attacker, it seems like about time we got round to doing it in a cautious way that tries to prevent the attacker from using our memcmp as a timing oracle. So, here's an smemeq() function which has the semantics of !memcmp but attempts to run in time dependent only on the length parameter. All the MAC implementations now use this in place of !memcmp to verify the MAC on input data.
This commit is contained in:
16
misc.c
16
misc.c
@ -1019,3 +1019,19 @@ int validate_manual_hostkey(char *key)
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
int smemeq(const void *av, const void *bv, size_t len)
|
||||
{
|
||||
const unsigned char *a = (const unsigned char *)av;
|
||||
const unsigned char *b = (const unsigned char *)bv;
|
||||
unsigned val = 0;
|
||||
|
||||
while (len-- > 0) {
|
||||
val |= *a++ ^ *b++;
|
||||
}
|
||||
/* Now val is 0 iff we want to return 1, and in the range
|
||||
* 0x01..0xFF iff we want to return 0. So subtracting from 0x100
|
||||
* will clear bit 8 iff we want to return 0, and leave it set iff
|
||||
* we want to return 1, so then we can just shift down. */
|
||||
return (0x100 - val) >> 8;
|
||||
}
|
||||
|
Reference in New Issue
Block a user