mirror of
https://github.com/oxen-io/session-ios.git
synced 2023-12-13 21:30:14 +01:00
cc94573e9b
- fix case when second part of the && conditional is skipped when data is not equal - isEqual variable marked volatile to prevent case when it doesn't equal 0, the loop can break early since it can never be 0 again - tested with Fastest O3 and Whole Module optimization (App Store Release) // FREEBIE
32 lines
746 B
Objective-C
32 lines
746 B
Objective-C
//
|
|
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
|
//
|
|
|
|
#import "NSData+OWSConstantTimeCompare.h"
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
@implementation NSData (OWSConstantTimeCompare)
|
|
|
|
- (BOOL)ows_constantTimeIsEqualToData:(NSData *)other
|
|
{
|
|
volatile UInt8 isEqual = 0;
|
|
|
|
if (self.length != other.length) {
|
|
return NO;
|
|
}
|
|
|
|
UInt8 *leftBytes = (UInt8 *)self.bytes;
|
|
UInt8 *rightBytes = (UInt8 *)other.bytes;
|
|
for (int i = 0; i < self.length; i++) {
|
|
// rather than returning as soon as we find a discrepency, we compare the rest of
|
|
// the byte stream to maintain a constant time comparison
|
|
isEqual |= leftBytes[i] ^ rightBytes[i];
|
|
}
|
|
|
|
return isEqual == 0;
|
|
}
|
|
|
|
@end
|
|
|
|
NS_ASSUME_NONNULL_END
|