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 | |
gabor-mezei-arm | fdb7118 | 2021-09-27 16:11:12 +0200 | [diff] [blame] | 33 | #include <string.h> |
Andrzej Kurek | 1c7a998 | 2023-05-30 09:21:20 -0400 | [diff] [blame] | 34 | |
| 35 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 36 | #include "psa/crypto.h" |
Andrzej Kurek | 0064484 | 2023-05-30 05:45:00 -0400 | [diff] [blame] | 37 | /* Define a local translating function to save code size by not using too many |
| 38 | * arguments in each translating place. */ |
| 39 | static int local_err_translation(psa_status_t status) |
| 40 | { |
| 41 | return psa_status_to_mbedtls(status, psa_to_ssl_errors, |
Andrzej Kurek | 1e4a030 | 2023-05-30 09:45:17 -0400 | [diff] [blame] | 42 | ARRAY_LENGTH(psa_to_ssl_errors), |
Andrzej Kurek | 0064484 | 2023-05-30 05:45:00 -0400 | [diff] [blame] | 43 | psa_generic_status_to_mbedtls); |
| 44 | } |
| 45 | #define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status) |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 46 | #endif |
gabor-mezei-arm | 3f90fd5 | 2021-09-27 12:55:33 +0200 | [diff] [blame] | 47 | |
Dave Rodgman | 58c80f4 | 2023-06-12 18:19:46 +0100 | [diff] [blame] | 48 | #if !defined(MBEDTLS_CT_ASM) |
| 49 | /* |
Dave Rodgman | 1ab0b48 | 2023-06-12 18:22:18 +0100 | [diff] [blame] | 50 | * 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 Rodgman | 58c80f4 | 2023-06-12 18:19:46 +0100 | [diff] [blame] | 54 | volatile mbedtls_ct_uint_t mbedtls_ct_zero = 0; |
| 55 | #endif |
| 56 | |
Dave Rodgman | 36dfc5a | 2022-12-22 15:04:43 +0000 | [diff] [blame] | 57 | /* |
Dave Rodgman | 051225d | 2022-12-30 21:25:35 +0000 | [diff] [blame] | 58 | * Define MBEDTLS_EFFICIENT_UNALIGNED_VOLATILE_ACCESS where assembly is present to |
| 59 | * perform fast unaligned access to volatile data. |
Dave Rodgman | 36dfc5a | 2022-12-22 15:04:43 +0000 | [diff] [blame] | 60 | * |
| 61 | * This is needed because mbedtls_get_unaligned_uintXX etc don't support volatile |
| 62 | * memory accesses. |
| 63 | * |
Dave Rodgman | 051225d | 2022-12-30 21:25:35 +0000 | [diff] [blame] | 64 | * Some of these definitions could be moved into alignment.h but for now they are |
| 65 | * only used here. |
Dave Rodgman | 36dfc5a | 2022-12-22 15:04:43 +0000 | [diff] [blame] | 66 | */ |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 67 | #if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS) && \ |
Dave Rodgman | 9fbb0cf | 2023-06-28 18:52:02 +0100 | [diff] [blame] | 68 | ((defined(MBEDTLS_CT_ARM_ASM) && (UINTPTR_MAX == 0xfffffffful)) || \ |
| 69 | defined(MBEDTLS_CT_AARCH64_ASM)) |
Dave Rodgman | 63e89b4 | 2023-06-21 11:55:17 +0100 | [diff] [blame] | 70 | /* 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] | 71 | #define MBEDTLS_EFFICIENT_UNALIGNED_VOLATILE_ACCESS |
| 72 | |
Dave Rodgman | 36dfc5a | 2022-12-22 15:04:43 +0000 | [diff] [blame] | 73 | static 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 Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 80 | #if defined(MBEDTLS_CT_ARM_ASM) |
TTornblom | e4f6d79 | 2020-04-16 13:53:38 +0200 | [diff] [blame] | 81 | __asm volatile ("ldr %0, [%1]" : "=r" (r) : "r" (p) :); |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 82 | #elif defined(MBEDTLS_CT_AARCH64_ASM) |
Dave Rodgman | 5b5dd01 | 2023-06-21 16:36:47 +0100 | [diff] [blame] | 83 | 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] | 84 | #else |
Antonio de Angelis | 1ee4d12 | 2023-08-16 12:26:37 +0100 | [diff] [blame^] | 85 | #error "No assembly defined for mbedtls_get_unaligned_volatile_uint32" |
Dave Rodgman | 36dfc5a | 2022-12-22 15:04:43 +0000 | [diff] [blame] | 86 | #endif |
Dave Rodgman | 051225d | 2022-12-30 21:25:35 +0000 | [diff] [blame] | 87 | return r; |
Dave Rodgman | 36dfc5a | 2022-12-22 15:04:43 +0000 | [diff] [blame] | 88 | } |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 89 | #endif /* defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS) && |
| 90 | (defined(MBEDTLS_CT_ARM_ASM) || defined(MBEDTLS_CT_AARCH64_ASM)) */ |
Dave Rodgman | 36dfc5a | 2022-12-22 15:04:43 +0000 | [diff] [blame] | 91 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 92 | int mbedtls_ct_memcmp(const void *a, |
| 93 | const void *b, |
| 94 | size_t n) |
gabor-mezei-arm | db9a38c | 2021-09-27 11:28:54 +0200 | [diff] [blame] | 95 | { |
Dave Rodgman | 36dfc5a | 2022-12-22 15:04:43 +0000 | [diff] [blame] | 96 | size_t i = 0; |
Dave Rodgman | 7658b63 | 2023-01-11 17:39:33 +0000 | [diff] [blame] | 97 | /* |
| 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-arm | db9a38c | 2021-09-27 11:28:54 +0200 | [diff] [blame] | 103 | volatile const unsigned char *A = (volatile const unsigned char *) a; |
| 104 | volatile const unsigned char *B = (volatile const unsigned char *) b; |
Dave Rodgman | 7658b63 | 2023-01-11 17:39:33 +0000 | [diff] [blame] | 105 | uint32_t diff = 0; |
gabor-mezei-arm | db9a38c | 2021-09-27 11:28:54 +0200 | [diff] [blame] | 106 | |
Dave Rodgman | 051225d | 2022-12-30 21:25:35 +0000 | [diff] [blame] | 107 | #if defined(MBEDTLS_EFFICIENT_UNALIGNED_VOLATILE_ACCESS) |
Dave Rodgman | 36dfc5a | 2022-12-22 15:04:43 +0000 | [diff] [blame] | 108 | 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-arm | db9a38c | 2021-09-27 11:28:54 +0200 | [diff] [blame] | 116 | /* 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 Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 123 | return (int) diff; |
gabor-mezei-arm | db9a38c | 2021-09-27 11:28:54 +0200 | [diff] [blame] | 124 | } |
| 125 | |
Gabor Mezei | e212379 | 2021-10-18 17:05:06 +0200 | [diff] [blame] | 126 | #if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT) |
| 127 | |
Dave Rodgman | 15c142b | 2023-05-17 12:20:11 +0100 | [diff] [blame] | 128 | 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] | 129 | { |
Dave Rodgman | 1714a9b | 2023-07-31 12:37:01 +0100 | [diff] [blame] | 130 | volatile unsigned char *buf = start; |
Dave Rodgman | 15c142b | 2023-05-17 12:20:11 +0100 | [diff] [blame] | 131 | for (size_t i = 0; i < total; i++) { |
Dave Rodgman | b7825ce | 2023-08-10 11:58:18 +0100 | [diff] [blame] | 132 | mbedtls_ct_condition_t no_op = mbedtls_ct_uint_gt(total - offset, i); |
Dave Rodgman | 1714a9b | 2023-07-31 12:37:01 +0100 | [diff] [blame] | 133 | /* The first `total - offset` passes are a no-op. The last |
| 134 | * `offset` passes shift the data one byte to the left and |
| 135 | * zero out the last byte. */ |
| 136 | for (size_t n = 0; n < total - 1; n++) { |
| 137 | unsigned char current = buf[n]; |
| 138 | unsigned char next = buf[n+1]; |
| 139 | buf[n] = mbedtls_ct_uint_if(no_op, current, next); |
| 140 | } |
Dave Rodgman | 98ddc01 | 2023-08-10 12:11:31 +0100 | [diff] [blame] | 141 | buf[total-1] = mbedtls_ct_uint_if_else_0(no_op, buf[total-1]); |
gabor-mezei-arm | 394aeaa | 2021-09-27 13:31:06 +0200 | [diff] [blame] | 142 | } |
| 143 | } |
gabor-mezei-arm | dee0fd3 | 2021-09-27 13:34:25 +0200 | [diff] [blame] | 144 | |
Gabor Mezei | e212379 | 2021-10-18 17:05:06 +0200 | [diff] [blame] | 145 | #endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */ |
| 146 | |
Dave Rodgman | 7fe6e6f | 2023-05-17 12:34:56 +0100 | [diff] [blame] | 147 | void mbedtls_ct_memcpy_if(mbedtls_ct_condition_t condition, |
| 148 | unsigned char *dest, |
| 149 | const unsigned char *src1, |
| 150 | const unsigned char *src2, |
| 151 | size_t len) |
| 152 | { |
| 153 | const uint32_t mask = (uint32_t) condition; |
| 154 | const uint32_t not_mask = (uint32_t) ~mbedtls_ct_compiler_opaque(condition); |
| 155 | |
Dave Rodgman | 07f8537 | 2023-07-31 12:27:49 +0100 | [diff] [blame] | 156 | /* If src2 is NULL, setup src2 so that we read from the destination address. |
| 157 | * |
| 158 | * This means that if src2 == NULL && condition is false, the result will be a |
| 159 | * no-op because we read from dest and write the same data back into dest. |
| 160 | */ |
Dave Rodgman | 7fe6e6f | 2023-05-17 12:34:56 +0100 | [diff] [blame] | 161 | if (src2 == NULL) { |
| 162 | src2 = dest; |
| 163 | } |
| 164 | |
| 165 | /* dest[i] = c1 == c2 ? src[i] : dest[i] */ |
| 166 | size_t i = 0; |
| 167 | #if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS) |
| 168 | for (; (i + 4) <= len; i += 4) { |
| 169 | uint32_t a = mbedtls_get_unaligned_uint32(src1 + i) & mask; |
| 170 | uint32_t b = mbedtls_get_unaligned_uint32(src2 + i) & not_mask; |
| 171 | mbedtls_put_unaligned_uint32(dest + i, a | b); |
| 172 | } |
| 173 | #endif /* MBEDTLS_EFFICIENT_UNALIGNED_ACCESS */ |
| 174 | for (; i < len; i++) { |
| 175 | dest[i] = (src1[i] & mask) | (src2[i] & not_mask); |
| 176 | } |
| 177 | } |
| 178 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 179 | void mbedtls_ct_memcpy_offset(unsigned char *dest, |
| 180 | const unsigned char *src, |
| 181 | size_t offset, |
| 182 | size_t offset_min, |
| 183 | size_t offset_max, |
| 184 | size_t len) |
gabor-mezei-arm | 0e7f71e | 2021-09-27 13:57:45 +0200 | [diff] [blame] | 185 | { |
Gabor Mezei | 63bbba5 | 2021-10-18 16:17:57 +0200 | [diff] [blame] | 186 | size_t offsetval; |
gabor-mezei-arm | 0e7f71e | 2021-09-27 13:57:45 +0200 | [diff] [blame] | 187 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 188 | for (offsetval = offset_min; offsetval <= offset_max; offsetval++) { |
Dave Rodgman | b7825ce | 2023-08-10 11:58:18 +0100 | [diff] [blame] | 189 | mbedtls_ct_memcpy_if(mbedtls_ct_uint_eq(offsetval, offset), dest, src + offsetval, NULL, |
Dave Rodgman | 585f7f7 | 2023-05-17 17:45:33 +0100 | [diff] [blame] | 190 | len); |
gabor-mezei-arm | 0e7f71e | 2021-09-27 13:57:45 +0200 | [diff] [blame] | 191 | } |
| 192 | } |
gabor-mezei-arm | 1349ffd | 2021-09-27 14:28:31 +0200 | [diff] [blame] | 193 | |
Dave Rodgman | debf867 | 2023-05-17 12:12:44 +0100 | [diff] [blame] | 194 | #if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT) |
| 195 | |
| 196 | void mbedtls_ct_zeroize_if(mbedtls_ct_condition_t condition, void *buf, size_t len) |
| 197 | { |
| 198 | uint32_t mask = (uint32_t) ~condition; |
| 199 | uint8_t *p = (uint8_t *) buf; |
| 200 | size_t i = 0; |
| 201 | #if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS) |
| 202 | for (; (i + 4) <= len; i += 4) { |
| 203 | mbedtls_put_unaligned_uint32((void *) (p + i), |
| 204 | mbedtls_get_unaligned_uint32((void *) (p + i)) & mask); |
| 205 | } |
| 206 | #endif |
| 207 | for (; i < len; i++) { |
| 208 | p[i] = p[i] & mask; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | #endif /* defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT) */ |