blob: cb156bc9b72c06b904bc40070e2e34283a2c959c [file] [log] [blame]
gabor-mezei-armd1125342021-07-12 16:31:22 +02001/**
2 * Constant-time functions
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#include "common.h"
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +020021#include "constant_time.h"
22
23/* constant-time buffer comparison */
24int mbedtls_ssl_safer_memcmp( const void *a, const void *b, size_t n )
25{
26 size_t i;
27 volatile const unsigned char *A = (volatile const unsigned char *) a;
28 volatile const unsigned char *B = (volatile const unsigned char *) b;
29 volatile unsigned char diff = 0;
30
31 for( i = 0; i < n; i++ )
32 {
33 /* Read volatile data in order before computing diff.
34 * This avoids IAR compiler warning:
35 * 'the order of volatile accesses is undefined ..' */
36 unsigned char x = A[i], y = B[i];
37 diff |= x ^ y;
38 }
39
40 return( diff );
41}
42
43/* Compare the contents of two buffers in constant time.
44 * Returns 0 if the contents are bitwise identical, otherwise returns
45 * a non-zero value.
46 * This is currently only used by GCM and ChaCha20+Poly1305.
47 */
48int mbedtls_constant_time_memcmp( const void *v1, const void *v2,
49 size_t len )
50{
51 const unsigned char *p1 = (const unsigned char*) v1;
52 const unsigned char *p2 = (const unsigned char*) v2;
53 size_t i;
54 unsigned char diff;
55
56 for( diff = 0, i = 0; i < len; i++ )
57 diff |= p1[i] ^ p2[i];
58
59 return( (int)diff );
60}
61
62/* constant-time buffer comparison */
63unsigned char mbedtls_nist_kw_safer_memcmp( const void *a, const void *b, size_t n )
64{
65 size_t i;
66 volatile const unsigned char *A = (volatile const unsigned char *) a;
67 volatile const unsigned char *B = (volatile const unsigned char *) b;
68 volatile unsigned char diff = 0;
69
70 for( i = 0; i < n; i++ )
71 {
72 /* Read volatile data in order before computing diff.
73 * This avoids IAR compiler warning:
74 * 'the order of volatile accesses is undefined ..' */
75 unsigned char x = A[i], y = B[i];
76 diff |= x ^ y;
77 }
78
79 return( diff );
80}
81
82/* constant-time buffer comparison */
83int mbedtls_safer_memcmp( const void *a, const void *b, size_t n )
84{
85 size_t i;
86 const unsigned char *A = (const unsigned char *) a;
87 const unsigned char *B = (const unsigned char *) b;
88 unsigned char diff = 0;
89
90 for( i = 0; i < n; i++ )
91 diff |= A[i] ^ B[i];
92
93 return( diff );
94}