blob: 20e215a79bb6452a86a2cae9db47eb68509b7dbf [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
Dave Rodgman378280e2023-07-31 16:34:19 +010033#include "../tests/include/test/constant_flow.h"
Dave Rodgmanfa5a4bb2023-07-28 16:13:52 +010034
gabor-mezei-armfdb71182021-09-27 16:11:12 +020035#include <string.h>
Andrzej Kurek1c7a9982023-05-30 09:21:20 -040036
37#if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
38#include "psa/crypto.h"
Andrzej Kurek00644842023-05-30 05:45:00 -040039/* Define a local translating function to save code size by not using too many
40 * arguments in each translating place. */
41static int local_err_translation(psa_status_t status)
42{
43 return psa_status_to_mbedtls(status, psa_to_ssl_errors,
Andrzej Kurek1e4a0302023-05-30 09:45:17 -040044 ARRAY_LENGTH(psa_to_ssl_errors),
Andrzej Kurek00644842023-05-30 05:45:00 -040045 psa_generic_status_to_mbedtls);
46}
47#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050048#endif
gabor-mezei-arm3f90fd52021-09-27 12:55:33 +020049
Dave Rodgman58c80f42023-06-12 18:19:46 +010050#if !defined(MBEDTLS_CT_ASM)
51/*
Dave Rodgman1ab0b482023-06-12 18:22:18 +010052 * Define an object with the value zero, such that the compiler cannot prove that it
53 * has the value zero (because it is volatile, it "may be modified in ways unknown to
54 * the implementation").
55 */
Dave Rodgman58c80f42023-06-12 18:19:46 +010056volatile mbedtls_ct_uint_t mbedtls_ct_zero = 0;
57#endif
58
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000059/*
Dave Rodgman051225d2022-12-30 21:25:35 +000060 * Define MBEDTLS_EFFICIENT_UNALIGNED_VOLATILE_ACCESS where assembly is present to
61 * perform fast unaligned access to volatile data.
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000062 *
63 * This is needed because mbedtls_get_unaligned_uintXX etc don't support volatile
64 * memory accesses.
65 *
Dave Rodgman051225d2022-12-30 21:25:35 +000066 * Some of these definitions could be moved into alignment.h but for now they are
67 * only used here.
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000068 */
Dave Rodgman40a41d02023-05-17 11:59:56 +010069#if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS) && \
Dave Rodgman9fbb0cf2023-06-28 18:52:02 +010070 ((defined(MBEDTLS_CT_ARM_ASM) && (UINTPTR_MAX == 0xfffffffful)) || \
71 defined(MBEDTLS_CT_AARCH64_ASM))
Dave Rodgman63e89b42023-06-21 11:55:17 +010072/* We check pointer sizes to avoid issues with them not matching register size requirements */
Dave Rodgman40a41d02023-05-17 11:59:56 +010073#define MBEDTLS_EFFICIENT_UNALIGNED_VOLATILE_ACCESS
74
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000075static inline uint32_t mbedtls_get_unaligned_volatile_uint32(volatile const unsigned char *p)
76{
77 /* This is UB, even where it's safe:
78 * return *((volatile uint32_t*)p);
79 * so instead the same thing is expressed in assembly below.
80 */
81 uint32_t r;
Dave Rodgman40a41d02023-05-17 11:59:56 +010082#if defined(MBEDTLS_CT_ARM_ASM)
Dave Rodgman4610d4b2023-01-30 09:26:48 +000083 asm volatile ("ldr %0, [%1]" : "=r" (r) : "r" (p) :);
Dave Rodgman40a41d02023-05-17 11:59:56 +010084#elif defined(MBEDTLS_CT_AARCH64_ASM)
Dave Rodgman5b5dd012023-06-21 16:36:47 +010085 asm volatile ("ldr %w0, [%1]" : "=r" (r) : MBEDTLS_ASM_AARCH64_PTR_CONSTRAINT(p) :);
Dave Rodgman40a41d02023-05-17 11:59:56 +010086#else
87#error No assembly defined for mbedtls_get_unaligned_volatile_uint32
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000088#endif
Dave Rodgman051225d2022-12-30 21:25:35 +000089 return r;
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000090}
Dave Rodgman40a41d02023-05-17 11:59:56 +010091#endif /* defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS) &&
92 (defined(MBEDTLS_CT_ARM_ASM) || defined(MBEDTLS_CT_AARCH64_ASM)) */
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000093
Gilles Peskine449bd832023-01-11 14:50:10 +010094int mbedtls_ct_memcmp(const void *a,
95 const void *b,
96 size_t n)
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +020097{
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000098 size_t i = 0;
Dave Rodgman7658b632023-01-11 17:39:33 +000099 /*
100 * `A` and `B` are cast to volatile to ensure that the compiler
101 * generates code that always fully reads both buffers.
102 * Otherwise it could generate a test to exit early if `diff` has all
103 * bits set early in the loop.
104 */
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +0200105 volatile const unsigned char *A = (volatile const unsigned char *) a;
106 volatile const unsigned char *B = (volatile const unsigned char *) b;
Dave Rodgman7658b632023-01-11 17:39:33 +0000107 uint32_t diff = 0;
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +0200108
Dave Rodgman051225d2022-12-30 21:25:35 +0000109#if defined(MBEDTLS_EFFICIENT_UNALIGNED_VOLATILE_ACCESS)
Dave Rodgman36dfc5a2022-12-22 15:04:43 +0000110 for (; (i + 4) <= n; i += 4) {
111 uint32_t x = mbedtls_get_unaligned_volatile_uint32(A + i);
112 uint32_t y = mbedtls_get_unaligned_volatile_uint32(B + i);
113 diff |= x ^ y;
114 }
115#endif
116
117 for (; i < n; i++) {
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +0200118 /* Read volatile data in order before computing diff.
119 * This avoids IAR compiler warning:
120 * 'the order of volatile accesses is undefined ..' */
121 unsigned char x = A[i], y = B[i];
122 diff |= x ^ y;
123 }
124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 return (int) diff;
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +0200126}
127
Gabor Mezeie2123792021-10-18 17:05:06 +0200128#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
129
Dave Rodgman15c142b2023-05-17 12:20:11 +0100130void mbedtls_ct_memmove_left(void *start, size_t total, size_t offset)
gabor-mezei-arm394aeaa2021-09-27 13:31:06 +0200131{
Dave Rodgman1714a9b2023-07-31 12:37:01 +0100132 volatile unsigned char *buf = start;
Dave Rodgman15c142b2023-05-17 12:20:11 +0100133 for (size_t i = 0; i < total; i++) {
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100134 mbedtls_ct_condition_t no_op = mbedtls_ct_uint_gt(total - offset, i);
Dave Rodgman1714a9b2023-07-31 12:37:01 +0100135 /* The first `total - offset` passes are a no-op. The last
136 * `offset` passes shift the data one byte to the left and
137 * zero out the last byte. */
138 for (size_t n = 0; n < total - 1; n++) {
139 unsigned char current = buf[n];
140 unsigned char next = buf[n+1];
141 buf[n] = mbedtls_ct_uint_if(no_op, current, next);
142 }
143 buf[total-1] = mbedtls_ct_uint_if0(no_op, buf[total-1]);
gabor-mezei-arm394aeaa2021-09-27 13:31:06 +0200144 }
145}
gabor-mezei-armdee0fd32021-09-27 13:34:25 +0200146
Gabor Mezeie2123792021-10-18 17:05:06 +0200147#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */
148
Dave Rodgman7fe6e6f2023-05-17 12:34:56 +0100149void mbedtls_ct_memcpy_if(mbedtls_ct_condition_t condition,
150 unsigned char *dest,
151 const unsigned char *src1,
152 const unsigned char *src2,
153 size_t len)
154{
155 const uint32_t mask = (uint32_t) condition;
156 const uint32_t not_mask = (uint32_t) ~mbedtls_ct_compiler_opaque(condition);
157
Dave Rodgman07f85372023-07-31 12:27:49 +0100158 /* If src2 is NULL, setup src2 so that we read from the destination address.
159 *
160 * This means that if src2 == NULL && condition is false, the result will be a
161 * no-op because we read from dest and write the same data back into dest.
162 */
Dave Rodgman7fe6e6f2023-05-17 12:34:56 +0100163 if (src2 == NULL) {
164 src2 = dest;
165 }
166
167 /* dest[i] = c1 == c2 ? src[i] : dest[i] */
168 size_t i = 0;
169#if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS)
170 for (; (i + 4) <= len; i += 4) {
171 uint32_t a = mbedtls_get_unaligned_uint32(src1 + i) & mask;
172 uint32_t b = mbedtls_get_unaligned_uint32(src2 + i) & not_mask;
173 mbedtls_put_unaligned_uint32(dest + i, a | b);
174 }
175#endif /* MBEDTLS_EFFICIENT_UNALIGNED_ACCESS */
176 for (; i < len; i++) {
177 dest[i] = (src1[i] & mask) | (src2[i] & not_mask);
178 }
179}
180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181void mbedtls_ct_memcpy_offset(unsigned char *dest,
182 const unsigned char *src,
183 size_t offset,
184 size_t offset_min,
185 size_t offset_max,
186 size_t len)
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200187{
Gabor Mezei63bbba52021-10-18 16:17:57 +0200188 size_t offsetval;
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200189
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 for (offsetval = offset_min; offsetval <= offset_max; offsetval++) {
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100191 mbedtls_ct_memcpy_if(mbedtls_ct_uint_eq(offsetval, offset), dest, src + offsetval, NULL,
Dave Rodgman585f7f72023-05-17 17:45:33 +0100192 len);
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200193 }
194}
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +0200195
Dave Rodgmandebf8672023-05-17 12:12:44 +0100196#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
197
198void mbedtls_ct_zeroize_if(mbedtls_ct_condition_t condition, void *buf, size_t len)
199{
200 uint32_t mask = (uint32_t) ~condition;
201 uint8_t *p = (uint8_t *) buf;
202 size_t i = 0;
203#if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS)
204 for (; (i + 4) <= len; i += 4) {
205 mbedtls_put_unaligned_uint32((void *) (p + i),
206 mbedtls_get_unaligned_uint32((void *) (p + i)) & mask);
207 }
208#endif
209 for (; i < len; i++) {
210 p[i] = p[i] & mask;
211 }
212}
213
214#endif /* defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT) */