gabor-mezei-arm | d112534 | 2021-07-12 16:31:22 +0200 | [diff] [blame] | 1 | /** |
| 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 Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 20 | /* |
Gabor Mezei | 642eeb2 | 2021-11-03 16:13:32 +0100 | [diff] [blame] | 21 | * The following functions are implemented without using comparison operators, as those |
Gabor Mezei | eab90bc | 2021-10-18 16:09:41 +0200 | [diff] [blame] | 22 | * might be translated to branches by some compilers on some platforms. |
| 23 | */ |
| 24 | |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 25 | #include <limits.h> |
| 26 | |
gabor-mezei-arm | d112534 | 2021-07-12 16:31:22 +0200 | [diff] [blame] | 27 | #include "common.h" |
Gabor Mezei | 22c9a6f | 2021-10-20 12:09:35 +0200 | [diff] [blame] | 28 | #include "constant_time_internal.h" |
Gabor Mezei | 765862c | 2021-10-19 12:22:25 +0200 | [diff] [blame] | 29 | #include "mbedtls/constant_time.h" |
gabor-mezei-arm | 1349ffd | 2021-09-27 14:28:31 +0200 | [diff] [blame] | 30 | #include "mbedtls/error.h" |
gabor-mezei-arm | 5b3a32d | 2021-09-29 10:50:31 +0200 | [diff] [blame] | 31 | #include "mbedtls/platform_util.h" |
gabor-mezei-arm | db9a38c | 2021-09-27 11:28:54 +0200 | [diff] [blame] | 32 | |
Dave Rodgman | fa5a4bb | 2023-07-28 16:13:52 +0100 | [diff] [blame] | 33 | #include "../tests/include/test/constant_flow.h" |
| 34 | |
gabor-mezei-arm | fdb7118 | 2021-09-27 16:11:12 +0200 | [diff] [blame] | 35 | #include <string.h> |
Andrzej Kurek | 1c7a998 | 2023-05-30 09:21:20 -0400 | [diff] [blame] | 36 | |
| 37 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 38 | #include "psa/crypto.h" |
Andrzej Kurek | 0064484 | 2023-05-30 05:45:00 -0400 | [diff] [blame] | 39 | /* Define a local translating function to save code size by not using too many |
| 40 | * arguments in each translating place. */ |
| 41 | static int local_err_translation(psa_status_t status) |
| 42 | { |
| 43 | return psa_status_to_mbedtls(status, psa_to_ssl_errors, |
Andrzej Kurek | 1e4a030 | 2023-05-30 09:45:17 -0400 | [diff] [blame] | 44 | ARRAY_LENGTH(psa_to_ssl_errors), |
Andrzej Kurek | 0064484 | 2023-05-30 05:45:00 -0400 | [diff] [blame] | 45 | psa_generic_status_to_mbedtls); |
| 46 | } |
| 47 | #define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status) |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 48 | #endif |
gabor-mezei-arm | 3f90fd5 | 2021-09-27 12:55:33 +0200 | [diff] [blame] | 49 | |
Dave Rodgman | 58c80f4 | 2023-06-12 18:19:46 +0100 | [diff] [blame] | 50 | #if !defined(MBEDTLS_CT_ASM) |
| 51 | /* |
Dave Rodgman | 1ab0b48 | 2023-06-12 18:22:18 +0100 | [diff] [blame] | 52 | * 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 Rodgman | 58c80f4 | 2023-06-12 18:19:46 +0100 | [diff] [blame] | 56 | volatile mbedtls_ct_uint_t mbedtls_ct_zero = 0; |
| 57 | #endif |
| 58 | |
Dave Rodgman | 36dfc5a | 2022-12-22 15:04:43 +0000 | [diff] [blame] | 59 | /* |
Dave Rodgman | 051225d | 2022-12-30 21:25:35 +0000 | [diff] [blame] | 60 | * Define MBEDTLS_EFFICIENT_UNALIGNED_VOLATILE_ACCESS where assembly is present to |
| 61 | * perform fast unaligned access to volatile data. |
Dave Rodgman | 36dfc5a | 2022-12-22 15:04:43 +0000 | [diff] [blame] | 62 | * |
| 63 | * This is needed because mbedtls_get_unaligned_uintXX etc don't support volatile |
| 64 | * memory accesses. |
| 65 | * |
Dave Rodgman | 051225d | 2022-12-30 21:25:35 +0000 | [diff] [blame] | 66 | * Some of these definitions could be moved into alignment.h but for now they are |
| 67 | * only used here. |
Dave Rodgman | 36dfc5a | 2022-12-22 15:04:43 +0000 | [diff] [blame] | 68 | */ |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 69 | #if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS) && \ |
Dave Rodgman | 9fbb0cf | 2023-06-28 18:52:02 +0100 | [diff] [blame] | 70 | ((defined(MBEDTLS_CT_ARM_ASM) && (UINTPTR_MAX == 0xfffffffful)) || \ |
| 71 | defined(MBEDTLS_CT_AARCH64_ASM)) |
Dave Rodgman | 63e89b4 | 2023-06-21 11:55:17 +0100 | [diff] [blame] | 72 | /* We check pointer sizes to avoid issues with them not matching register size requirements */ |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 73 | #define MBEDTLS_EFFICIENT_UNALIGNED_VOLATILE_ACCESS |
| 74 | |
Dave Rodgman | 36dfc5a | 2022-12-22 15:04:43 +0000 | [diff] [blame] | 75 | static 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 Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 82 | #if defined(MBEDTLS_CT_ARM_ASM) |
Dave Rodgman | 4610d4b | 2023-01-30 09:26:48 +0000 | [diff] [blame] | 83 | asm volatile ("ldr %0, [%1]" : "=r" (r) : "r" (p) :); |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 84 | #elif defined(MBEDTLS_CT_AARCH64_ASM) |
Dave Rodgman | 5b5dd01 | 2023-06-21 16:36:47 +0100 | [diff] [blame] | 85 | asm volatile ("ldr %w0, [%1]" : "=r" (r) : MBEDTLS_ASM_AARCH64_PTR_CONSTRAINT(p) :); |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 86 | #else |
| 87 | #error No assembly defined for mbedtls_get_unaligned_volatile_uint32 |
Dave Rodgman | 36dfc5a | 2022-12-22 15:04:43 +0000 | [diff] [blame] | 88 | #endif |
Dave Rodgman | 051225d | 2022-12-30 21:25:35 +0000 | [diff] [blame] | 89 | return r; |
Dave Rodgman | 36dfc5a | 2022-12-22 15:04:43 +0000 | [diff] [blame] | 90 | } |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 91 | #endif /* defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS) && |
| 92 | (defined(MBEDTLS_CT_ARM_ASM) || defined(MBEDTLS_CT_AARCH64_ASM)) */ |
Dave Rodgman | 36dfc5a | 2022-12-22 15:04:43 +0000 | [diff] [blame] | 93 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 94 | int mbedtls_ct_memcmp(const void *a, |
| 95 | const void *b, |
| 96 | size_t n) |
gabor-mezei-arm | db9a38c | 2021-09-27 11:28:54 +0200 | [diff] [blame] | 97 | { |
Dave Rodgman | 36dfc5a | 2022-12-22 15:04:43 +0000 | [diff] [blame] | 98 | size_t i = 0; |
Dave Rodgman | 7658b63 | 2023-01-11 17:39:33 +0000 | [diff] [blame] | 99 | /* |
| 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-arm | db9a38c | 2021-09-27 11:28:54 +0200 | [diff] [blame] | 105 | volatile const unsigned char *A = (volatile const unsigned char *) a; |
| 106 | volatile const unsigned char *B = (volatile const unsigned char *) b; |
Dave Rodgman | 7658b63 | 2023-01-11 17:39:33 +0000 | [diff] [blame] | 107 | uint32_t diff = 0; |
gabor-mezei-arm | db9a38c | 2021-09-27 11:28:54 +0200 | [diff] [blame] | 108 | |
Dave Rodgman | 051225d | 2022-12-30 21:25:35 +0000 | [diff] [blame] | 109 | #if defined(MBEDTLS_EFFICIENT_UNALIGNED_VOLATILE_ACCESS) |
Dave Rodgman | 36dfc5a | 2022-12-22 15:04:43 +0000 | [diff] [blame] | 110 | 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-arm | db9a38c | 2021-09-27 11:28:54 +0200 | [diff] [blame] | 118 | /* 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 Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 125 | return (int) diff; |
gabor-mezei-arm | db9a38c | 2021-09-27 11:28:54 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Gabor Mezei | e212379 | 2021-10-18 17:05:06 +0200 | [diff] [blame] | 128 | #if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT) |
| 129 | |
Dave Rodgman | 15c142b | 2023-05-17 12:20:11 +0100 | [diff] [blame] | 130 | void mbedtls_ct_memmove_left(void *start, size_t total, size_t offset) |
gabor-mezei-arm | 394aeaa | 2021-09-27 13:31:06 +0200 | [diff] [blame] | 131 | { |
Dave Rodgman | fa5a4bb | 2023-07-28 16:13:52 +0100 | [diff] [blame] | 132 | /* In case of inlining, ensure that code generated is independent of the value of offset |
| 133 | * (e.g., if the compiler knows that offset == 0, it might be able to optimise this function |
| 134 | * to a no-op). */ |
| 135 | size_t hidden_offset = mbedtls_ct_compiler_opaque(offset); |
| 136 | |
| 137 | /* During this loop, j will take every value from [0..total) exactly once, |
| 138 | * regardless of the value of hidden_offset (it only changes the initial |
| 139 | * value for j). |
| 140 | * |
| 141 | * For this reason, when testing, it is safe to mark hidden_offset as non-secret. |
| 142 | * This prevents the const-flow checkers from generating a false-positive. |
| 143 | */ |
| 144 | TEST_CF_PUBLIC(&hidden_offset, sizeof(hidden_offset)); |
| 145 | |
Dave Rodgman | 8f5e5c1 | 2023-05-16 13:30:15 +0100 | [diff] [blame] | 146 | /* Iterate over the array, reading each byte once and writing each byte once. */ |
Dave Rodgman | 15c142b | 2023-05-17 12:20:11 +0100 | [diff] [blame] | 147 | for (size_t i = 0; i < total; i++) { |
Dave Rodgman | 8f5e5c1 | 2023-05-16 13:30:15 +0100 | [diff] [blame] | 148 | /* Each iteration, read one byte, and write it to start[i]. |
| 149 | * |
| 150 | * The source address will either be the "true" source address, if it's in the range |
| 151 | * where data is getting moved, or (if the source address is off the end of the |
| 152 | * array), it will wrap back to the start. |
| 153 | * |
| 154 | * If the source address is out of range, mask it to zero. |
| 155 | */ |
| 156 | |
Dave Rodgman | fa5a4bb | 2023-07-28 16:13:52 +0100 | [diff] [blame] | 157 | // The offset that we will read from (if in range) |
| 158 | size_t j = i + hidden_offset; |
Dave Rodgman | 8f5e5c1 | 2023-05-16 13:30:15 +0100 | [diff] [blame] | 159 | |
| 160 | // Is the address off the end of the array? |
| 161 | mbedtls_ct_condition_t not_dummy = mbedtls_ct_bool_lt(j, total); |
| 162 | |
| 163 | // Bring read address into range |
| 164 | j = j % total; |
| 165 | |
| 166 | // Read a byte |
Dave Rodgman | 585f7f7 | 2023-05-17 17:45:33 +0100 | [diff] [blame] | 167 | uint8_t b = ((uint8_t *) start)[j]; |
Dave Rodgman | 8f5e5c1 | 2023-05-16 13:30:15 +0100 | [diff] [blame] | 168 | |
| 169 | // Set it to zero if it's out of range |
| 170 | b = mbedtls_ct_uint_if0(not_dummy, b); |
| 171 | |
| 172 | // Write the byte to start[i] |
Dave Rodgman | 585f7f7 | 2023-05-17 17:45:33 +0100 | [diff] [blame] | 173 | ((uint8_t *) start)[i] = b; |
gabor-mezei-arm | 394aeaa | 2021-09-27 13:31:06 +0200 | [diff] [blame] | 174 | } |
| 175 | } |
gabor-mezei-arm | dee0fd3 | 2021-09-27 13:34:25 +0200 | [diff] [blame] | 176 | |
Gabor Mezei | e212379 | 2021-10-18 17:05:06 +0200 | [diff] [blame] | 177 | #endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */ |
| 178 | |
Dave Rodgman | 7fe6e6f | 2023-05-17 12:34:56 +0100 | [diff] [blame] | 179 | void mbedtls_ct_memcpy_if(mbedtls_ct_condition_t condition, |
| 180 | unsigned char *dest, |
| 181 | const unsigned char *src1, |
| 182 | const unsigned char *src2, |
| 183 | size_t len) |
| 184 | { |
| 185 | const uint32_t mask = (uint32_t) condition; |
| 186 | const uint32_t not_mask = (uint32_t) ~mbedtls_ct_compiler_opaque(condition); |
| 187 | |
| 188 | /* If src2 is NULL and condition == 0, then this function has no effect. |
| 189 | * In this case, copy from dest back into dest. */ |
| 190 | if (src2 == NULL) { |
| 191 | src2 = dest; |
| 192 | } |
| 193 | |
| 194 | /* dest[i] = c1 == c2 ? src[i] : dest[i] */ |
| 195 | size_t i = 0; |
| 196 | #if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS) |
| 197 | for (; (i + 4) <= len; i += 4) { |
| 198 | uint32_t a = mbedtls_get_unaligned_uint32(src1 + i) & mask; |
| 199 | uint32_t b = mbedtls_get_unaligned_uint32(src2 + i) & not_mask; |
| 200 | mbedtls_put_unaligned_uint32(dest + i, a | b); |
| 201 | } |
| 202 | #endif /* MBEDTLS_EFFICIENT_UNALIGNED_ACCESS */ |
| 203 | for (; i < len; i++) { |
| 204 | dest[i] = (src1[i] & mask) | (src2[i] & not_mask); |
| 205 | } |
| 206 | } |
| 207 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 208 | void mbedtls_ct_memcpy_offset(unsigned char *dest, |
| 209 | const unsigned char *src, |
| 210 | size_t offset, |
| 211 | size_t offset_min, |
| 212 | size_t offset_max, |
| 213 | size_t len) |
gabor-mezei-arm | 0e7f71e | 2021-09-27 13:57:45 +0200 | [diff] [blame] | 214 | { |
Gabor Mezei | 63bbba5 | 2021-10-18 16:17:57 +0200 | [diff] [blame] | 215 | size_t offsetval; |
gabor-mezei-arm | 0e7f71e | 2021-09-27 13:57:45 +0200 | [diff] [blame] | 216 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 217 | for (offsetval = offset_min; offsetval <= offset_max; offsetval++) { |
Dave Rodgman | 585f7f7 | 2023-05-17 17:45:33 +0100 | [diff] [blame] | 218 | mbedtls_ct_memcpy_if(mbedtls_ct_bool_eq(offsetval, offset), dest, src + offsetval, NULL, |
| 219 | len); |
gabor-mezei-arm | 0e7f71e | 2021-09-27 13:57:45 +0200 | [diff] [blame] | 220 | } |
| 221 | } |
gabor-mezei-arm | 1349ffd | 2021-09-27 14:28:31 +0200 | [diff] [blame] | 222 | |
Dave Rodgman | debf867 | 2023-05-17 12:12:44 +0100 | [diff] [blame] | 223 | #if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT) |
| 224 | |
| 225 | void mbedtls_ct_zeroize_if(mbedtls_ct_condition_t condition, void *buf, size_t len) |
| 226 | { |
| 227 | uint32_t mask = (uint32_t) ~condition; |
| 228 | uint8_t *p = (uint8_t *) buf; |
| 229 | size_t i = 0; |
| 230 | #if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS) |
| 231 | for (; (i + 4) <= len; i += 4) { |
| 232 | mbedtls_put_unaligned_uint32((void *) (p + i), |
| 233 | mbedtls_get_unaligned_uint32((void *) (p + i)) & mask); |
| 234 | } |
| 235 | #endif |
| 236 | for (; i < len; i++) { |
| 237 | p[i] = p[i] & mask; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | #endif /* defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT) */ |