blob: c7077c352379492e5b8494cec327feba85f4f6e7 [file] [log] [blame]
gabor-mezei-armd1125342021-07-12 16:31:22 +02001/**
2 * Constant-time functions
3 *
4 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
gabor-mezei-armd1125342021-07-12 16:31:22 +02006 */
7
Gilles Peskine449bd832023-01-11 14:50:10 +01008/*
Gabor Mezei642eeb22021-11-03 16:13:32 +01009 * The following functions are implemented without using comparison operators, as those
Gabor Mezeieab90bc2021-10-18 16:09:41 +020010 * might be translated to branches by some compilers on some platforms.
11 */
12
Dave Rodgman4f267702023-09-11 19:05:51 +010013#include <stdint.h>
Dave Rodgman40a41d02023-05-17 11:59:56 +010014#include <limits.h>
15
gabor-mezei-armd1125342021-07-12 16:31:22 +020016#include "common.h"
Gabor Mezei22c9a6f2021-10-20 12:09:35 +020017#include "constant_time_internal.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020018#include "mbedtls/constant_time.h"
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +020019#include "mbedtls/error.h"
gabor-mezei-arm5b3a32d2021-09-29 10:50:31 +020020#include "mbedtls/platform_util.h"
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +020021
gabor-mezei-armfdb71182021-09-27 16:11:12 +020022#include <string.h>
Andrzej Kurek1c7a9982023-05-30 09:21:20 -040023
24#if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
25#include "psa/crypto.h"
Andrzej Kurek00644842023-05-30 05:45:00 -040026/* Define a local translating function to save code size by not using too many
27 * arguments in each translating place. */
28static int local_err_translation(psa_status_t status)
29{
30 return psa_status_to_mbedtls(status, psa_to_ssl_errors,
Andrzej Kurek1e4a0302023-05-30 09:45:17 -040031 ARRAY_LENGTH(psa_to_ssl_errors),
Andrzej Kurek00644842023-05-30 05:45:00 -040032 psa_generic_status_to_mbedtls);
33}
34#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050035#endif
gabor-mezei-arm3f90fd52021-09-27 12:55:33 +020036
Dave Rodgman58c80f42023-06-12 18:19:46 +010037#if !defined(MBEDTLS_CT_ASM)
38/*
Dave Rodgman1ab0b482023-06-12 18:22:18 +010039 * Define an object with the value zero, such that the compiler cannot prove that it
40 * has the value zero (because it is volatile, it "may be modified in ways unknown to
41 * the implementation").
42 */
Dave Rodgman58c80f42023-06-12 18:19:46 +010043volatile mbedtls_ct_uint_t mbedtls_ct_zero = 0;
44#endif
45
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000046/*
Dave Rodgman051225d2022-12-30 21:25:35 +000047 * Define MBEDTLS_EFFICIENT_UNALIGNED_VOLATILE_ACCESS where assembly is present to
48 * perform fast unaligned access to volatile data.
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000049 *
50 * This is needed because mbedtls_get_unaligned_uintXX etc don't support volatile
51 * memory accesses.
52 *
Dave Rodgman051225d2022-12-30 21:25:35 +000053 * Some of these definitions could be moved into alignment.h but for now they are
54 * only used here.
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000055 */
Dave Rodgman40a41d02023-05-17 11:59:56 +010056#if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS) && \
Dave Rodgman9fbb0cf2023-06-28 18:52:02 +010057 ((defined(MBEDTLS_CT_ARM_ASM) && (UINTPTR_MAX == 0xfffffffful)) || \
58 defined(MBEDTLS_CT_AARCH64_ASM))
Dave Rodgman63e89b42023-06-21 11:55:17 +010059/* We check pointer sizes to avoid issues with them not matching register size requirements */
Dave Rodgman40a41d02023-05-17 11:59:56 +010060#define MBEDTLS_EFFICIENT_UNALIGNED_VOLATILE_ACCESS
61
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000062static inline uint32_t mbedtls_get_unaligned_volatile_uint32(volatile const unsigned char *p)
63{
64 /* This is UB, even where it's safe:
65 * return *((volatile uint32_t*)p);
66 * so instead the same thing is expressed in assembly below.
67 */
68 uint32_t r;
Dave Rodgman40a41d02023-05-17 11:59:56 +010069#if defined(MBEDTLS_CT_ARM_ASM)
Antonio de Angelisf1adc2a2023-08-16 12:31:54 +010070 asm volatile ("ldr %0, [%1]" : "=r" (r) : "r" (p) :);
Dave Rodgman40a41d02023-05-17 11:59:56 +010071#elif defined(MBEDTLS_CT_AARCH64_ASM)
Dave Rodgman5b5dd012023-06-21 16:36:47 +010072 asm volatile ("ldr %w0, [%1]" : "=r" (r) : MBEDTLS_ASM_AARCH64_PTR_CONSTRAINT(p) :);
Dave Rodgman40a41d02023-05-17 11:59:56 +010073#else
Antonio de Angelis1ee4d122023-08-16 12:26:37 +010074#error "No assembly defined for mbedtls_get_unaligned_volatile_uint32"
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000075#endif
Dave Rodgman051225d2022-12-30 21:25:35 +000076 return r;
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000077}
Dave Rodgman40a41d02023-05-17 11:59:56 +010078#endif /* defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS) &&
79 (defined(MBEDTLS_CT_ARM_ASM) || defined(MBEDTLS_CT_AARCH64_ASM)) */
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000080
Gilles Peskine449bd832023-01-11 14:50:10 +010081int mbedtls_ct_memcmp(const void *a,
82 const void *b,
83 size_t n)
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +020084{
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000085 size_t i = 0;
Dave Rodgman7658b632023-01-11 17:39:33 +000086 /*
87 * `A` and `B` are cast to volatile to ensure that the compiler
88 * generates code that always fully reads both buffers.
89 * Otherwise it could generate a test to exit early if `diff` has all
90 * bits set early in the loop.
91 */
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +020092 volatile const unsigned char *A = (volatile const unsigned char *) a;
93 volatile const unsigned char *B = (volatile const unsigned char *) b;
Dave Rodgman7658b632023-01-11 17:39:33 +000094 uint32_t diff = 0;
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +020095
Dave Rodgman051225d2022-12-30 21:25:35 +000096#if defined(MBEDTLS_EFFICIENT_UNALIGNED_VOLATILE_ACCESS)
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000097 for (; (i + 4) <= n; i += 4) {
98 uint32_t x = mbedtls_get_unaligned_volatile_uint32(A + i);
99 uint32_t y = mbedtls_get_unaligned_volatile_uint32(B + i);
100 diff |= x ^ y;
101 }
102#endif
103
104 for (; i < n; i++) {
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +0200105 /* Read volatile data in order before computing diff.
106 * This avoids IAR compiler warning:
107 * 'the order of volatile accesses is undefined ..' */
108 unsigned char x = A[i], y = B[i];
109 diff |= x ^ y;
110 }
111
Dave Rodgman98926d52023-09-12 09:29:33 +0100112
Dave Rodgman50b0a352023-09-12 09:30:44 +0100113#if (INT_MAX < INT32_MAX)
Dave Rodgman98926d52023-09-12 09:29:33 +0100114 /* We don't support int smaller than 32-bits, but if someone tried to build
115 * with this configuration, there is a risk that, for differing data, the
116 * only bits set in diff are in the top 16-bits, and would be lost by a
117 * simple cast from uint32 to int.
118 * This would have significant security implications, so protect against it. */
119#error "mbedtls_ct_memcmp() requires minimum 32-bit ints"
Dave Rodgman4f267702023-09-11 19:05:51 +0100120#else
Dave Rodgmanbd589442023-09-12 12:38:53 +0100121 /* The bit-twiddling ensures that when we cast uint32_t to int, we are casting
122 * a value that is in the range 0..INT_MAX - a value larger than this would
123 * result in implementation defined behaviour.
124 *
125 * This ensures that the value returned by the function is non-zero iff
126 * diff is non-zero.
127 */
128 return (int) ((diff & 0xffff) | (diff >> 16));
Dave Rodgman4f267702023-09-11 19:05:51 +0100129#endif
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +0200130}
131
Dave Rodgman9c140072023-09-18 18:20:27 +0100132#if defined(MBEDTLS_NIST_KW_C)
133
134int mbedtls_ct_memcmp_partial(const void *a,
135 const void *b,
136 size_t n,
137 size_t skip_head,
138 size_t skip_tail)
139{
140 unsigned int diff = 0;
141
142 volatile const unsigned char *A = (volatile const unsigned char *) a;
143 volatile const unsigned char *B = (volatile const unsigned char *) b;
144
145 size_t valid_end = n - skip_tail;
146
147 for (size_t i = 0; i < n; i++) {
148 unsigned char x = A[i], y = B[i];
Dave Rodgmanc2630fa2023-09-19 14:13:41 +0100149 unsigned int d = x ^ y;
Dave Rodgman9c140072023-09-18 18:20:27 +0100150 mbedtls_ct_condition_t valid = mbedtls_ct_bool_and(mbedtls_ct_uint_ge(i, skip_head),
151 mbedtls_ct_uint_lt(i, valid_end));
152 diff |= mbedtls_ct_uint_if_else_0(valid, d);
153 }
154
Dave Rodgmanc2630fa2023-09-19 14:13:41 +0100155 /* Since we go byte-by-byte, the only bits set will be in the bottom 8 bits, so the
156 * cast from uint to int is safe. */
157 return (int) diff;
Dave Rodgman9c140072023-09-18 18:20:27 +0100158}
159
160#endif
161
Gabor Mezeie2123792021-10-18 17:05:06 +0200162#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
163
Dave Rodgman15c142b2023-05-17 12:20:11 +0100164void mbedtls_ct_memmove_left(void *start, size_t total, size_t offset)
gabor-mezei-arm394aeaa2021-09-27 13:31:06 +0200165{
Dave Rodgman1714a9b2023-07-31 12:37:01 +0100166 volatile unsigned char *buf = start;
Dave Rodgman15c142b2023-05-17 12:20:11 +0100167 for (size_t i = 0; i < total; i++) {
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100168 mbedtls_ct_condition_t no_op = mbedtls_ct_uint_gt(total - offset, i);
Dave Rodgman1714a9b2023-07-31 12:37:01 +0100169 /* The first `total - offset` passes are a no-op. The last
170 * `offset` passes shift the data one byte to the left and
171 * zero out the last byte. */
172 for (size_t n = 0; n < total - 1; n++) {
173 unsigned char current = buf[n];
174 unsigned char next = buf[n+1];
175 buf[n] = mbedtls_ct_uint_if(no_op, current, next);
176 }
Dave Rodgman98ddc012023-08-10 12:11:31 +0100177 buf[total-1] = mbedtls_ct_uint_if_else_0(no_op, buf[total-1]);
gabor-mezei-arm394aeaa2021-09-27 13:31:06 +0200178 }
179}
gabor-mezei-armdee0fd32021-09-27 13:34:25 +0200180
Gabor Mezeie2123792021-10-18 17:05:06 +0200181#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */
182
Dave Rodgman7fe6e6f2023-05-17 12:34:56 +0100183void mbedtls_ct_memcpy_if(mbedtls_ct_condition_t condition,
184 unsigned char *dest,
185 const unsigned char *src1,
186 const unsigned char *src2,
187 size_t len)
188{
Dave Rodgman42391b42023-05-19 10:33:21 +0100189#if defined(MBEDTLS_CT_SIZE_64)
190 const uint64_t mask = (uint64_t) condition;
191 const uint64_t not_mask = (uint64_t) ~mbedtls_ct_compiler_opaque(condition);
192#else
Dave Rodgman7fe6e6f2023-05-17 12:34:56 +0100193 const uint32_t mask = (uint32_t) condition;
194 const uint32_t not_mask = (uint32_t) ~mbedtls_ct_compiler_opaque(condition);
Dave Rodgman42391b42023-05-19 10:33:21 +0100195#endif
Dave Rodgman7fe6e6f2023-05-17 12:34:56 +0100196
Dave Rodgman07f85372023-07-31 12:27:49 +0100197 /* If src2 is NULL, setup src2 so that we read from the destination address.
198 *
199 * This means that if src2 == NULL && condition is false, the result will be a
200 * no-op because we read from dest and write the same data back into dest.
201 */
Dave Rodgman7fe6e6f2023-05-17 12:34:56 +0100202 if (src2 == NULL) {
203 src2 = dest;
204 }
205
206 /* dest[i] = c1 == c2 ? src[i] : dest[i] */
207 size_t i = 0;
208#if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS)
Dave Rodgman42391b42023-05-19 10:33:21 +0100209#if defined(MBEDTLS_CT_SIZE_64)
210 for (; (i + 8) <= len; i += 8) {
211 uint64_t a = mbedtls_get_unaligned_uint64(src1 + i) & mask;
212 uint64_t b = mbedtls_get_unaligned_uint64(src2 + i) & not_mask;
213 mbedtls_put_unaligned_uint64(dest + i, a | b);
214 }
215#else
Dave Rodgman7fe6e6f2023-05-17 12:34:56 +0100216 for (; (i + 4) <= len; i += 4) {
217 uint32_t a = mbedtls_get_unaligned_uint32(src1 + i) & mask;
218 uint32_t b = mbedtls_get_unaligned_uint32(src2 + i) & not_mask;
219 mbedtls_put_unaligned_uint32(dest + i, a | b);
220 }
Dave Rodgman42391b42023-05-19 10:33:21 +0100221#endif /* defined(MBEDTLS_CT_SIZE_64) */
Dave Rodgman7fe6e6f2023-05-17 12:34:56 +0100222#endif /* MBEDTLS_EFFICIENT_UNALIGNED_ACCESS */
223 for (; i < len; i++) {
224 dest[i] = (src1[i] & mask) | (src2[i] & not_mask);
225 }
226}
227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228void mbedtls_ct_memcpy_offset(unsigned char *dest,
229 const unsigned char *src,
230 size_t offset,
231 size_t offset_min,
232 size_t offset_max,
233 size_t len)
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200234{
Gabor Mezei63bbba52021-10-18 16:17:57 +0200235 size_t offsetval;
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200236
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 for (offsetval = offset_min; offsetval <= offset_max; offsetval++) {
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100238 mbedtls_ct_memcpy_if(mbedtls_ct_uint_eq(offsetval, offset), dest, src + offsetval, NULL,
Dave Rodgman585f7f72023-05-17 17:45:33 +0100239 len);
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200240 }
241}
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +0200242
Dave Rodgmandebf8672023-05-17 12:12:44 +0100243#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
244
245void mbedtls_ct_zeroize_if(mbedtls_ct_condition_t condition, void *buf, size_t len)
246{
247 uint32_t mask = (uint32_t) ~condition;
248 uint8_t *p = (uint8_t *) buf;
249 size_t i = 0;
250#if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS)
251 for (; (i + 4) <= len; i += 4) {
252 mbedtls_put_unaligned_uint32((void *) (p + i),
253 mbedtls_get_unaligned_uint32((void *) (p + i)) & mask);
254 }
255#endif
256 for (; i < len; i++) {
257 p[i] = p[i] & mask;
258 }
259}
260
261#endif /* defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT) */