Merge branch 'collinstuart/constant-time-compare'

pull/1/head
Michael Kirk 7 years ago
commit d5e61dac9f

@ -10,7 +10,7 @@ NS_ASSUME_NONNULL_BEGIN
- (BOOL)ows_constantTimeIsEqualToData:(NSData *)other - (BOOL)ows_constantTimeIsEqualToData:(NSData *)other
{ {
BOOL isEqual = YES; volatile UInt8 isEqual = 0;
if (self.length != other.length) { if (self.length != other.length) {
return NO; return NO;
@ -21,10 +21,10 @@ NS_ASSUME_NONNULL_BEGIN
for (int i = 0; i < self.length; i++) { for (int i = 0; i < self.length; i++) {
// rather than returning as soon as we find a discrepency, we compare the rest of // rather than returning as soon as we find a discrepency, we compare the rest of
// the byte stream to maintain a constant time comparison // the byte stream to maintain a constant time comparison
isEqual = isEqual && (leftBytes[i] == rightBytes[i]); isEqual |= leftBytes[i] ^ rightBytes[i];
} }
return isEqual; return isEqual == 0;
} }
@end @end

Loading…
Cancel
Save