blob: 90fbcf2d27e972b0bfdf5953f6ec90d2a60ac9c6 [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 Kurek8a045ce2022-12-23 11:00:06 -050034#if defined(MBEDTLS_USE_PSA_CRYPTO)
35#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
36 psa_to_ssl_errors, \
37 psa_generic_status_to_mbedtls)
38#endif
gabor-mezei-arm3f90fd52021-09-27 12:55:33 +020039
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000040/*
Dave Rodgman051225d2022-12-30 21:25:35 +000041 * Define MBEDTLS_EFFICIENT_UNALIGNED_VOLATILE_ACCESS where assembly is present to
42 * perform fast unaligned access to volatile data.
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000043 *
44 * This is needed because mbedtls_get_unaligned_uintXX etc don't support volatile
45 * memory accesses.
46 *
Dave Rodgman051225d2022-12-30 21:25:35 +000047 * Some of these definitions could be moved into alignment.h but for now they are
48 * only used here.
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000049 */
Dave Rodgman40a41d02023-05-17 11:59:56 +010050#if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS) && \
51 (defined(MBEDTLS_CT_ARM_ASM) || defined(MBEDTLS_CT_AARCH64_ASM))
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000052
Dave Rodgman40a41d02023-05-17 11:59:56 +010053#define MBEDTLS_EFFICIENT_UNALIGNED_VOLATILE_ACCESS
54
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000055static inline uint32_t mbedtls_get_unaligned_volatile_uint32(volatile const unsigned char *p)
56{
57 /* This is UB, even where it's safe:
58 * return *((volatile uint32_t*)p);
59 * so instead the same thing is expressed in assembly below.
60 */
61 uint32_t r;
Dave Rodgman40a41d02023-05-17 11:59:56 +010062#if defined(MBEDTLS_CT_ARM_ASM)
Dave Rodgman4610d4b2023-01-30 09:26:48 +000063 asm volatile ("ldr %0, [%1]" : "=r" (r) : "r" (p) :);
Dave Rodgman40a41d02023-05-17 11:59:56 +010064#elif defined(MBEDTLS_CT_AARCH64_ASM)
Dave Rodgman4610d4b2023-01-30 09:26:48 +000065 asm volatile ("ldr %w0, [%1]" : "=r" (r) : "r" (p) :);
Dave Rodgman40a41d02023-05-17 11:59:56 +010066#else
67#error No assembly defined for mbedtls_get_unaligned_volatile_uint32
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000068#endif
Dave Rodgman051225d2022-12-30 21:25:35 +000069 return r;
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000070}
Dave Rodgman40a41d02023-05-17 11:59:56 +010071#endif /* defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS) &&
72 (defined(MBEDTLS_CT_ARM_ASM) || defined(MBEDTLS_CT_AARCH64_ASM)) */
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000073
Gilles Peskine449bd832023-01-11 14:50:10 +010074int mbedtls_ct_memcmp(const void *a,
75 const void *b,
76 size_t n)
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +020077{
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000078 size_t i = 0;
Dave Rodgman7658b632023-01-11 17:39:33 +000079 /*
80 * `A` and `B` are cast to volatile to ensure that the compiler
81 * generates code that always fully reads both buffers.
82 * Otherwise it could generate a test to exit early if `diff` has all
83 * bits set early in the loop.
84 */
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +020085 volatile const unsigned char *A = (volatile const unsigned char *) a;
86 volatile const unsigned char *B = (volatile const unsigned char *) b;
Dave Rodgman7658b632023-01-11 17:39:33 +000087 uint32_t diff = 0;
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +020088
Dave Rodgman051225d2022-12-30 21:25:35 +000089#if defined(MBEDTLS_EFFICIENT_UNALIGNED_VOLATILE_ACCESS)
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000090 for (; (i + 4) <= n; i += 4) {
91 uint32_t x = mbedtls_get_unaligned_volatile_uint32(A + i);
92 uint32_t y = mbedtls_get_unaligned_volatile_uint32(B + i);
93 diff |= x ^ y;
94 }
95#endif
96
97 for (; i < n; i++) {
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +020098 /* Read volatile data in order before computing diff.
99 * This avoids IAR compiler warning:
100 * 'the order of volatile accesses is undefined ..' */
101 unsigned char x = A[i], y = B[i];
102 diff |= x ^ y;
103 }
104
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 return (int) diff;
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +0200106}
107
Gabor Mezeie2123792021-10-18 17:05:06 +0200108#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
109
Dave Rodgman15c142b2023-05-17 12:20:11 +0100110void mbedtls_ct_memmove_left(void *start, size_t total, size_t offset)
gabor-mezei-arm394aeaa2021-09-27 13:31:06 +0200111{
Dave Rodgman8f5e5c12023-05-16 13:30:15 +0100112 /* Iterate over the array, reading each byte once and writing each byte once. */
Dave Rodgman15c142b2023-05-17 12:20:11 +0100113 for (size_t i = 0; i < total; i++) {
Dave Rodgman8f5e5c12023-05-16 13:30:15 +0100114 /* Each iteration, read one byte, and write it to start[i].
115 *
116 * The source address will either be the "true" source address, if it's in the range
117 * where data is getting moved, or (if the source address is off the end of the
118 * array), it will wrap back to the start.
119 *
120 * If the source address is out of range, mask it to zero.
121 */
122
123 // The address that we will read from
124 // TODO: if offset is marked as secret, this upsets Memsan.
125 size_t j = i + offset;
126
127 // Is the address off the end of the array?
128 mbedtls_ct_condition_t not_dummy = mbedtls_ct_bool_lt(j, total);
129
130 // Bring read address into range
131 j = j % total;
132
133 // Read a byte
Dave Rodgman585f7f72023-05-17 17:45:33 +0100134 uint8_t b = ((uint8_t *) start)[j];
Dave Rodgman8f5e5c12023-05-16 13:30:15 +0100135
136 // Set it to zero if it's out of range
137 b = mbedtls_ct_uint_if0(not_dummy, b);
138
139 // Write the byte to start[i]
Dave Rodgman585f7f72023-05-17 17:45:33 +0100140 ((uint8_t *) start)[i] = b;
gabor-mezei-arm394aeaa2021-09-27 13:31:06 +0200141 }
142}
gabor-mezei-armdee0fd32021-09-27 13:34:25 +0200143
Gabor Mezeie2123792021-10-18 17:05:06 +0200144#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */
145
Dave Rodgman7fe6e6f2023-05-17 12:34:56 +0100146void mbedtls_ct_memcpy_if(mbedtls_ct_condition_t condition,
147 unsigned char *dest,
148 const unsigned char *src1,
149 const unsigned char *src2,
150 size_t len)
151{
152 const uint32_t mask = (uint32_t) condition;
153 const uint32_t not_mask = (uint32_t) ~mbedtls_ct_compiler_opaque(condition);
154
155 /* If src2 is NULL and condition == 0, then this function has no effect.
156 * In this case, copy from dest back into dest. */
157 if (src2 == NULL) {
158 src2 = dest;
159 }
160
161 /* dest[i] = c1 == c2 ? src[i] : dest[i] */
162 size_t i = 0;
163#if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS)
164 for (; (i + 4) <= len; i += 4) {
165 uint32_t a = mbedtls_get_unaligned_uint32(src1 + i) & mask;
166 uint32_t b = mbedtls_get_unaligned_uint32(src2 + i) & not_mask;
167 mbedtls_put_unaligned_uint32(dest + i, a | b);
168 }
169#endif /* MBEDTLS_EFFICIENT_UNALIGNED_ACCESS */
170 for (; i < len; i++) {
171 dest[i] = (src1[i] & mask) | (src2[i] & not_mask);
172 }
173}
174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175void mbedtls_ct_memcpy_offset(unsigned char *dest,
176 const unsigned char *src,
177 size_t offset,
178 size_t offset_min,
179 size_t offset_max,
180 size_t len)
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200181{
Gabor Mezei63bbba52021-10-18 16:17:57 +0200182 size_t offsetval;
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 for (offsetval = offset_min; offsetval <= offset_max; offsetval++) {
Dave Rodgman585f7f72023-05-17 17:45:33 +0100185 mbedtls_ct_memcpy_if(mbedtls_ct_bool_eq(offsetval, offset), dest, src + offsetval, NULL,
186 len);
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200187 }
188}
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +0200189
Dave Rodgmandebf8672023-05-17 12:12:44 +0100190#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
191
192void mbedtls_ct_zeroize_if(mbedtls_ct_condition_t condition, void *buf, size_t len)
193{
194 uint32_t mask = (uint32_t) ~condition;
195 uint8_t *p = (uint8_t *) buf;
196 size_t i = 0;
197#if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS)
198 for (; (i + 4) <= len; i += 4) {
199 mbedtls_put_unaligned_uint32((void *) (p + i),
200 mbedtls_get_unaligned_uint32((void *) (p + i)) & mask);
201 }
202#endif
203 for (; i < len; i++) {
204 p[i] = p[i] & mask;
205 }
206}
207
208#endif /* defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT) */