Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/symcrypt/lib/equal.c
15010 views
1
//
2
// equal.c Memory comparison routine that is safe against side channels.
3
//
4
// Copyright (c) Microsoft Corporation. Licensed under the MIT license.
5
//
6
7
#include "precomp.h"
8
9
BOOLEAN
10
SYMCRYPT_CALL
11
SymCryptEqual( _In_reads_( cbBytes ) PCBYTE pbSrc1,
12
_In_reads_( cbBytes ) PCBYTE pbSrc2,
13
SIZE_T cbBytes )
14
{
15
UINT32 neq = 0;
16
BYTE b;
17
volatile BYTE * p1 = (volatile BYTE *) pbSrc1;
18
volatile BYTE * p2 = (volatile BYTE *) pbSrc2;
19
20
//
21
// We use forced-access memory reads to ensure that the compiler doesn't get
22
// smart and implement an early-out solution.
23
//
24
25
while( cbBytes >= 4 )
26
{
27
neq |= SYMCRYPT_FORCE_READ32( (volatile UINT32 *) p1 ) ^ SYMCRYPT_FORCE_READ32( (volatile UINT32 *) p2 );
28
p1 += 4;
29
p2 += 4;
30
cbBytes -= 4;
31
}
32
33
// We have to deal with the remaining bytes using a separate accumulator to work around an issue in the ARM64 compiler.
34
if( cbBytes > 0 )
35
{
36
b = 0;
37
while( cbBytes > 0 )
38
{
39
b |= SYMCRYPT_FORCE_READ8( p1 ) ^ SYMCRYPT_FORCE_READ8( p2 );
40
p1++;
41
p2++;
42
cbBytes--;
43
}
44
neq |= b;
45
}
46
47
return neq == 0;
48
}
49
50