blob: f2cdddf961faec7e07cafede382e5d6d17e5e1f1 [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
Gilles Peskine449bd832023-01-11 14:50:10 +010020/*
Gabor Mezei642eeb22021-11-03 16:13:32 +010021 * The following functions are implemented without using comparison operators, as those
Gabor Mezeieab90bc2021-10-18 16:09:41 +020022 * might be translated to branches by some compilers on some platforms.
23 */
24
Dave Rodgman40a41d02023-05-17 11:59:56 +010025#include <limits.h>
26
gabor-mezei-armd1125342021-07-12 16:31:22 +020027#include "common.h"
Gabor Mezei22c9a6f2021-10-20 12:09:35 +020028#include "constant_time_internal.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020029#include "mbedtls/constant_time.h"
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +020030#include "mbedtls/error.h"
gabor-mezei-arm5b3a32d2021-09-29 10:50:31 +020031#include "mbedtls/platform_util.h"
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +020032
gabor-mezei-armfdb71182021-09-27 16:11:12 +020033#include <string.h>
Andrzej Kurek1c7a9982023-05-30 09:21:20 -040034
35#if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
36#include "psa/crypto.h"
Andrzej Kurek00644842023-05-30 05:45:00 -040037/* Define a local translating function to save code size by not using too many
38 * arguments in each translating place. */
39static int local_err_translation(psa_status_t status)
40{
41 return psa_status_to_mbedtls(status, psa_to_ssl_errors,
Andrzej Kurek1e4a0302023-05-30 09:45:17 -040042 ARRAY_LENGTH(psa_to_ssl_errors),
Andrzej Kurek00644842023-05-30 05:45:00 -040043 psa_generic_status_to_mbedtls);
44}
45#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050046#endif
gabor-mezei-arm3f90fd52021-09-27 12:55:33 +020047
Dave Rodgman58c80f42023-06-12 18:19:46 +010048#if !defined(MBEDTLS_CT_ASM)
49/*
Dave Rodgman1ab0b482023-06-12 18:22:18 +010050 * Define an object with the value zero, such that the compiler cannot prove that it
51 * has the value zero (because it is volatile, it "may be modified in ways unknown to
52 * the implementation").
53 */
Dave Rodgman58c80f42023-06-12 18:19:46 +010054volatile mbedtls_ct_uint_t mbedtls_ct_zero = 0;
55#endif
56
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000057/*
Dave Rodgman051225d2022-12-30 21:25:35 +000058 * Define MBEDTLS_EFFICIENT_UNALIGNED_VOLATILE_ACCESS where assembly is present to
59 * perform fast unaligned access to volatile data.
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000060 *
61 * This is needed because mbedtls_get_unaligned_uintXX etc don't support volatile
62 * memory accesses.
63 *
Dave Rodgman051225d2022-12-30 21:25:35 +000064 * Some of these definitions could be moved into alignment.h but for now they are
65 * only used here.
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000066 */
Dave Rodgman40a41d02023-05-17 11:59:56 +010067#if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS) && \
Dave Rodgman9fbb0cf2023-06-28 18:52:02 +010068 ((defined(MBEDTLS_CT_ARM_ASM) && (UINTPTR_MAX == 0xfffffffful)) || \
69 defined(MBEDTLS_CT_AARCH64_ASM))
Dave Rodgman63e89b42023-06-21 11:55:17 +010070/* We check pointer sizes to avoid issues with them not matching register size requirements */
Dave Rodgman40a41d02023-05-17 11:59:56 +010071#define MBEDTLS_EFFICIENT_UNALIGNED_VOLATILE_ACCESS
72
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000073static inline uint32_t mbedtls_get_unaligned_volatile_uint32(volatile const unsigned char *p)
74{
75 /* This is UB, even where it's safe:
76 * return *((volatile uint32_t*)p);
77 * so instead the same thing is expressed in assembly below.
78 */
79 uint32_t r;
Dave Rodgman40a41d02023-05-17 11:59:56 +010080#if defined(MBEDTLS_CT_ARM_ASM)
Dave Rodgman4610d4b2023-01-30 09:26:48 +000081 asm volatile ("ldr %0, [%1]" : "=r" (r) : "r" (p) :);
Dave Rodgman40a41d02023-05-17 11:59:56 +010082#elif defined(MBEDTLS_CT_AARCH64_ASM)
Dave Rodgman5b5dd012023-06-21 16:36:47 +010083 asm volatile ("ldr %w0, [%1]" : "=r" (r) : MBEDTLS_ASM_AARCH64_PTR_CONSTRAINT(p) :);
Dave Rodgman40a41d02023-05-17 11:59:56 +010084#else
85#error No assembly defined for mbedtls_get_unaligned_volatile_uint32
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000086#endif
Dave Rodgman051225d2022-12-30 21:25:35 +000087 return r;
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000088}
Dave Rodgman40a41d02023-05-17 11:59:56 +010089#endif /* defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS) &&
90 (defined(MBEDTLS_CT_ARM_ASM) || defined(MBEDTLS_CT_AARCH64_ASM)) */
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000091
Gilles Peskine449bd832023-01-11 14:50:10 +010092int mbedtls_ct_memcmp(const void *a,
93 const void *b,
94 size_t n)
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +020095{
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000096 size_t i = 0;
Dave Rodgman7658b632023-01-11 17:39:33 +000097 /*
98 * `A` and `B` are cast to volatile to ensure that the compiler
99 * generates code that always fully reads both buffers.
100 * Otherwise it could generate a test to exit early if `diff` has all
101 * bits set early in the loop.
102 */
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +0200103 volatile const unsigned char *A = (volatile const unsigned char *) a;
104 volatile const unsigned char *B = (volatile const unsigned char *) b;
Dave Rodgman7658b632023-01-11 17:39:33 +0000105 uint32_t diff = 0;
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +0200106
Dave Rodgman051225d2022-12-30 21:25:35 +0000107#if defined(MBEDTLS_EFFICIENT_UNALIGNED_VOLATILE_ACCESS)
Dave Rodgman36dfc5a2022-12-22 15:04:43 +0000108 for (; (i + 4) <= n; i += 4) {
109 uint32_t x = mbedtls_get_unaligned_volatile_uint32(A + i);
110 uint32_t y = mbedtls_get_unaligned_volatile_uint32(B + i);
111 diff |= x ^ y;
112 }
113#endif
114
115 for (; i < n; i++) {
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +0200116 /* Read volatile data in order before computing diff.
117 * This avoids IAR compiler warning:
118 * 'the order of volatile accesses is undefined ..' */
119 unsigned char x = A[i], y = B[i];
120 diff |= x ^ y;
121 }
122
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 return (int) diff;
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +0200124}
125
Gabor Mezeie2123792021-10-18 17:05:06 +0200126#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
127
Dave Rodgman15c142b2023-05-17 12:20:11 +0100128void mbedtls_ct_memmove_left(void *start, size_t total, size_t offset)
gabor-mezei-arm394aeaa2021-09-27 13:31:06 +0200129{
Dave Rodgman8f5e5c12023-05-16 13:30:15 +0100130 /* Iterate over the array, reading each byte once and writing each byte once. */
Dave Rodgman15c142b2023-05-17 12:20:11 +0100131 for (size_t i = 0; i < total; i++) {
Dave Rodgman8f5e5c12023-05-16 13:30:15 +0100132 /* Each iteration, read one byte, and write it to start[i].
133 *
134 * The source address will either be the "true" source address, if it's in the range
135 * where data is getting moved, or (if the source address is off the end of the
136 * array), it will wrap back to the start.
137 *
138 * If the source address is out of range, mask it to zero.
139 */
140
141 // The address that we will read from
142 // TODO: if offset is marked as secret, this upsets Memsan.
143 size_t j = i + offset;
144
145 // Is the address off the end of the array?
146 mbedtls_ct_condition_t not_dummy = mbedtls_ct_bool_lt(j, total);
147
148 // Bring read address into range
149 j = j % total;
150
151 // Read a byte
Dave Rodgman585f7f72023-05-17 17:45:33 +0100152 uint8_t b = ((uint8_t *) start)[j];
Dave Rodgman8f5e5c12023-05-16 13:30:15 +0100153
154 // Set it to zero if it's out of range
155 b = mbedtls_ct_uint_if0(not_dummy, b);
156
157 // Write the byte to start[i]
Dave Rodgman585f7f72023-05-17 17:45:33 +0100158 ((uint8_t *) start)[i] = b;
gabor-mezei-arm394aeaa2021-09-27 13:31:06 +0200159 }
160}
gabor-mezei-armdee0fd32021-09-27 13:34:25 +0200161
Gabor Mezeie2123792021-10-18 17:05:06 +0200162#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */
163
Dave Rodgman7fe6e6f2023-05-17 12:34:56 +0100164void mbedtls_ct_memcpy_if(mbedtls_ct_condition_t condition,
165 unsigned char *dest,
166 const unsigned char *src1,
167 const unsigned char *src2,
168 size_t len)
169{
170 const uint32_t mask = (uint32_t) condition;
171 const uint32_t not_mask = (uint32_t) ~mbedtls_ct_compiler_opaque(condition);
172
173 /* If src2 is NULL and condition == 0, then this function has no effect.
174 * In this case, copy from dest back into dest. */
175 if (src2 == NULL) {
176 src2 = dest;
177 }
178
179 /* dest[i] = c1 == c2 ? src[i] : dest[i] */
180 size_t i = 0;
181#if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS)
182 for (; (i + 4) <= len; i += 4) {
183 uint32_t a = mbedtls_get_unaligned_uint32(src1 + i) & mask;
184 uint32_t b = mbedtls_get_unaligned_uint32(src2 + i) & not_mask;
185 mbedtls_put_unaligned_uint32(dest + i, a | b);
186 }
187#endif /* MBEDTLS_EFFICIENT_UNALIGNED_ACCESS */
188 for (; i < len; i++) {
189 dest[i] = (src1[i] & mask) | (src2[i] & not_mask);
190 }
191}
192
Gilles Peskine449bd832023-01-11 14:50:10 +0100193void mbedtls_ct_memcpy_offset(unsigned char *dest,
194 const unsigned char *src,
195 size_t offset,
196 size_t offset_min,
197 size_t offset_max,
198 size_t len)
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200199{
Gabor Mezei63bbba52021-10-18 16:17:57 +0200200 size_t offsetval;
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200201
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 for (offsetval = offset_min; offsetval <= offset_max; offsetval++) {
Dave Rodgman585f7f72023-05-17 17:45:33 +0100203 mbedtls_ct_memcpy_if(mbedtls_ct_bool_eq(offsetval, offset), dest, src + offsetval, NULL,
204 len);
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200205 }
206}
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +0200207
Dave Rodgmandebf8672023-05-17 12:12:44 +0100208#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
209
210void mbedtls_ct_zeroize_if(mbedtls_ct_condition_t condition, void *buf, size_t len)
211{
212 uint32_t mask = (uint32_t) ~condition;
213 uint8_t *p = (uint8_t *) buf;
214 size_t i = 0;
215#if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS)
216 for (; (i + 4) <= len; i += 4) {
217 mbedtls_put_unaligned_uint32((void *) (p + i),
218 mbedtls_get_unaligned_uint32((void *) (p + i)) & mask);
219 }
220#endif
221 for (; i < len; i++) {
222 p[i] = p[i] & mask;
223 }
224}
225
226#endif /* defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT) */