Coverage Report

Created: 2024-11-21 07:03

/src/SymCrypt/lib/equal.c
Line
Count
Source (jump to first uncovered line)
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
0
{
15
0
    UINT32 neq = 0;
16
0
    BYTE b;
17
0
    volatile BYTE * p1 = (volatile BYTE *) pbSrc1;
18
0
    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
0
    while( cbBytes >= 4 )
26
0
    {
27
0
        neq |= SYMCRYPT_FORCE_READ32( (volatile UINT32 *) p1 ) ^ SYMCRYPT_FORCE_READ32( (volatile UINT32 *) p2 );
28
0
        p1 += 4;
29
0
        p2 += 4;
30
0
        cbBytes -= 4;
31
0
    }
32
33
    // We have to deal with the remaining bytes using a separate accumulator to work around an issue in the ARM64 compiler.
34
0
    if( cbBytes > 0 )
35
0
    {
36
0
        b = 0;
37
0
        while( cbBytes > 0 )
38
0
        {
39
0
            b |= SYMCRYPT_FORCE_READ8( p1 ) ^ SYMCRYPT_FORCE_READ8( p2 );
40
0
            p1++;
41
0
            p2++;
42
0
            cbBytes--;
43
0
        }
44
0
        neq |= b;
45
0
    }
46
47
0
    return neq == 0;
48
0
}
49