ReactOS 0.4.17-dev-1005-g171e1de
equal.c
Go to the documentation of this file.
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
11SymCryptEqual( _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 {
40 p1++;
41 p2++;
42 cbBytes--;
43 }
44 neq |= b;
45 }
46
47 return neq == 0;
48}
unsigned char BOOLEAN
Definition: actypes.h:127
BOOLEAN SYMCRYPT_CALL SymCryptEqual(_In_reads_(cbBytes) PCBYTE pbSrc1, _In_reads_(cbBytes) PCBYTE pbSrc2, SIZE_T cbBytes)
Definition: equal.c:11
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
#define b
Definition: ke_i.h:79
#define _In_reads_(s)
Definition: no_sal2.h:168
#define SYMCRYPT_FORCE_READ8( _p)
Definition: symcrypt.h:416
#define SYMCRYPT_FORCE_READ32(_p)
Definition: symcrypt.h:418
#define SYMCRYPT_CALL
const BYTE * PCBYTE
ULONG_PTR SIZE_T
Definition: typedefs.h:80
uint32_t UINT32
Definition: typedefs.h:59
unsigned char BYTE
Definition: xxhash.c:193