1a8e1175bSopenharmony_ci/** 2a8e1175bSopenharmony_ci * Constant-time functions 3a8e1175bSopenharmony_ci * 4a8e1175bSopenharmony_ci * Copyright The Mbed TLS Contributors 5a8e1175bSopenharmony_ci * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 6a8e1175bSopenharmony_ci */ 7a8e1175bSopenharmony_ci 8a8e1175bSopenharmony_ci/* 9a8e1175bSopenharmony_ci * The following functions are implemented without using comparison operators, as those 10a8e1175bSopenharmony_ci * might be translated to branches by some compilers on some platforms. 11a8e1175bSopenharmony_ci */ 12a8e1175bSopenharmony_ci 13a8e1175bSopenharmony_ci#include <stdint.h> 14a8e1175bSopenharmony_ci#include <limits.h> 15a8e1175bSopenharmony_ci 16a8e1175bSopenharmony_ci#include "common.h" 17a8e1175bSopenharmony_ci#include "constant_time_internal.h" 18a8e1175bSopenharmony_ci#include "mbedtls/constant_time.h" 19a8e1175bSopenharmony_ci#include "mbedtls/error.h" 20a8e1175bSopenharmony_ci#include "mbedtls/platform_util.h" 21a8e1175bSopenharmony_ci 22a8e1175bSopenharmony_ci#include <string.h> 23a8e1175bSopenharmony_ci 24a8e1175bSopenharmony_ci#if !defined(MBEDTLS_CT_ASM) 25a8e1175bSopenharmony_ci/* 26a8e1175bSopenharmony_ci * Define an object with the value zero, such that the compiler cannot prove that it 27a8e1175bSopenharmony_ci * has the value zero (because it is volatile, it "may be modified in ways unknown to 28a8e1175bSopenharmony_ci * the implementation"). 29a8e1175bSopenharmony_ci */ 30a8e1175bSopenharmony_civolatile mbedtls_ct_uint_t mbedtls_ct_zero = 0; 31a8e1175bSopenharmony_ci#endif 32a8e1175bSopenharmony_ci 33a8e1175bSopenharmony_ci/* 34a8e1175bSopenharmony_ci * Define MBEDTLS_EFFICIENT_UNALIGNED_VOLATILE_ACCESS where assembly is present to 35a8e1175bSopenharmony_ci * perform fast unaligned access to volatile data. 36a8e1175bSopenharmony_ci * 37a8e1175bSopenharmony_ci * This is needed because mbedtls_get_unaligned_uintXX etc don't support volatile 38a8e1175bSopenharmony_ci * memory accesses. 39a8e1175bSopenharmony_ci * 40a8e1175bSopenharmony_ci * Some of these definitions could be moved into alignment.h but for now they are 41a8e1175bSopenharmony_ci * only used here. 42a8e1175bSopenharmony_ci */ 43a8e1175bSopenharmony_ci#if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS) && \ 44a8e1175bSopenharmony_ci ((defined(MBEDTLS_CT_ARM_ASM) && (UINTPTR_MAX == 0xfffffffful)) || \ 45a8e1175bSopenharmony_ci defined(MBEDTLS_CT_AARCH64_ASM)) 46a8e1175bSopenharmony_ci/* We check pointer sizes to avoid issues with them not matching register size requirements */ 47a8e1175bSopenharmony_ci#define MBEDTLS_EFFICIENT_UNALIGNED_VOLATILE_ACCESS 48a8e1175bSopenharmony_ci 49a8e1175bSopenharmony_cistatic inline uint32_t mbedtls_get_unaligned_volatile_uint32(volatile const unsigned char *p) 50a8e1175bSopenharmony_ci{ 51a8e1175bSopenharmony_ci /* This is UB, even where it's safe: 52a8e1175bSopenharmony_ci * return *((volatile uint32_t*)p); 53a8e1175bSopenharmony_ci * so instead the same thing is expressed in assembly below. 54a8e1175bSopenharmony_ci */ 55a8e1175bSopenharmony_ci uint32_t r; 56a8e1175bSopenharmony_ci#if defined(MBEDTLS_CT_ARM_ASM) 57a8e1175bSopenharmony_ci asm volatile ("ldr %0, [%1]" : "=r" (r) : "r" (p) :); 58a8e1175bSopenharmony_ci#elif defined(MBEDTLS_CT_AARCH64_ASM) 59a8e1175bSopenharmony_ci asm volatile ("ldr %w0, [%1]" : "=r" (r) : MBEDTLS_ASM_AARCH64_PTR_CONSTRAINT(p) :); 60a8e1175bSopenharmony_ci#else 61a8e1175bSopenharmony_ci#error "No assembly defined for mbedtls_get_unaligned_volatile_uint32" 62a8e1175bSopenharmony_ci#endif 63a8e1175bSopenharmony_ci return r; 64a8e1175bSopenharmony_ci} 65a8e1175bSopenharmony_ci#endif /* defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS) && 66a8e1175bSopenharmony_ci (defined(MBEDTLS_CT_ARM_ASM) || defined(MBEDTLS_CT_AARCH64_ASM)) */ 67a8e1175bSopenharmony_ci 68a8e1175bSopenharmony_ciint mbedtls_ct_memcmp(const void *a, 69a8e1175bSopenharmony_ci const void *b, 70a8e1175bSopenharmony_ci size_t n) 71a8e1175bSopenharmony_ci{ 72a8e1175bSopenharmony_ci size_t i = 0; 73a8e1175bSopenharmony_ci /* 74a8e1175bSopenharmony_ci * `A` and `B` are cast to volatile to ensure that the compiler 75a8e1175bSopenharmony_ci * generates code that always fully reads both buffers. 76a8e1175bSopenharmony_ci * Otherwise it could generate a test to exit early if `diff` has all 77a8e1175bSopenharmony_ci * bits set early in the loop. 78a8e1175bSopenharmony_ci */ 79a8e1175bSopenharmony_ci volatile const unsigned char *A = (volatile const unsigned char *) a; 80a8e1175bSopenharmony_ci volatile const unsigned char *B = (volatile const unsigned char *) b; 81a8e1175bSopenharmony_ci uint32_t diff = 0; 82a8e1175bSopenharmony_ci 83a8e1175bSopenharmony_ci#if defined(MBEDTLS_EFFICIENT_UNALIGNED_VOLATILE_ACCESS) 84a8e1175bSopenharmony_ci for (; (i + 4) <= n; i += 4) { 85a8e1175bSopenharmony_ci uint32_t x = mbedtls_get_unaligned_volatile_uint32(A + i); 86a8e1175bSopenharmony_ci uint32_t y = mbedtls_get_unaligned_volatile_uint32(B + i); 87a8e1175bSopenharmony_ci diff |= x ^ y; 88a8e1175bSopenharmony_ci } 89a8e1175bSopenharmony_ci#endif 90a8e1175bSopenharmony_ci 91a8e1175bSopenharmony_ci for (; i < n; i++) { 92a8e1175bSopenharmony_ci /* Read volatile data in order before computing diff. 93a8e1175bSopenharmony_ci * This avoids IAR compiler warning: 94a8e1175bSopenharmony_ci * 'the order of volatile accesses is undefined ..' */ 95a8e1175bSopenharmony_ci unsigned char x = A[i], y = B[i]; 96a8e1175bSopenharmony_ci diff |= x ^ y; 97a8e1175bSopenharmony_ci } 98a8e1175bSopenharmony_ci 99a8e1175bSopenharmony_ci 100a8e1175bSopenharmony_ci#if (INT_MAX < INT32_MAX) 101a8e1175bSopenharmony_ci /* We don't support int smaller than 32-bits, but if someone tried to build 102a8e1175bSopenharmony_ci * with this configuration, there is a risk that, for differing data, the 103a8e1175bSopenharmony_ci * only bits set in diff are in the top 16-bits, and would be lost by a 104a8e1175bSopenharmony_ci * simple cast from uint32 to int. 105a8e1175bSopenharmony_ci * This would have significant security implications, so protect against it. */ 106a8e1175bSopenharmony_ci#error "mbedtls_ct_memcmp() requires minimum 32-bit ints" 107a8e1175bSopenharmony_ci#else 108a8e1175bSopenharmony_ci /* The bit-twiddling ensures that when we cast uint32_t to int, we are casting 109a8e1175bSopenharmony_ci * a value that is in the range 0..INT_MAX - a value larger than this would 110a8e1175bSopenharmony_ci * result in implementation defined behaviour. 111a8e1175bSopenharmony_ci * 112a8e1175bSopenharmony_ci * This ensures that the value returned by the function is non-zero iff 113a8e1175bSopenharmony_ci * diff is non-zero. 114a8e1175bSopenharmony_ci */ 115a8e1175bSopenharmony_ci return (int) ((diff & 0xffff) | (diff >> 16)); 116a8e1175bSopenharmony_ci#endif 117a8e1175bSopenharmony_ci} 118a8e1175bSopenharmony_ci 119a8e1175bSopenharmony_ci#if defined(MBEDTLS_NIST_KW_C) 120a8e1175bSopenharmony_ci 121a8e1175bSopenharmony_ciint mbedtls_ct_memcmp_partial(const void *a, 122a8e1175bSopenharmony_ci const void *b, 123a8e1175bSopenharmony_ci size_t n, 124a8e1175bSopenharmony_ci size_t skip_head, 125a8e1175bSopenharmony_ci size_t skip_tail) 126a8e1175bSopenharmony_ci{ 127a8e1175bSopenharmony_ci unsigned int diff = 0; 128a8e1175bSopenharmony_ci 129a8e1175bSopenharmony_ci volatile const unsigned char *A = (volatile const unsigned char *) a; 130a8e1175bSopenharmony_ci volatile const unsigned char *B = (volatile const unsigned char *) b; 131a8e1175bSopenharmony_ci 132a8e1175bSopenharmony_ci size_t valid_end = n - skip_tail; 133a8e1175bSopenharmony_ci 134a8e1175bSopenharmony_ci for (size_t i = 0; i < n; i++) { 135a8e1175bSopenharmony_ci unsigned char x = A[i], y = B[i]; 136a8e1175bSopenharmony_ci unsigned int d = x ^ y; 137a8e1175bSopenharmony_ci mbedtls_ct_condition_t valid = mbedtls_ct_bool_and(mbedtls_ct_uint_ge(i, skip_head), 138a8e1175bSopenharmony_ci mbedtls_ct_uint_lt(i, valid_end)); 139a8e1175bSopenharmony_ci diff |= mbedtls_ct_uint_if_else_0(valid, d); 140a8e1175bSopenharmony_ci } 141a8e1175bSopenharmony_ci 142a8e1175bSopenharmony_ci /* Since we go byte-by-byte, the only bits set will be in the bottom 8 bits, so the 143a8e1175bSopenharmony_ci * cast from uint to int is safe. */ 144a8e1175bSopenharmony_ci return (int) diff; 145a8e1175bSopenharmony_ci} 146a8e1175bSopenharmony_ci 147a8e1175bSopenharmony_ci#endif 148a8e1175bSopenharmony_ci 149a8e1175bSopenharmony_ci#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT) 150a8e1175bSopenharmony_ci 151a8e1175bSopenharmony_civoid mbedtls_ct_memmove_left(void *start, size_t total, size_t offset) 152a8e1175bSopenharmony_ci{ 153a8e1175bSopenharmony_ci volatile unsigned char *buf = start; 154a8e1175bSopenharmony_ci for (size_t i = 0; i < total; i++) { 155a8e1175bSopenharmony_ci mbedtls_ct_condition_t no_op = mbedtls_ct_uint_gt(total - offset, i); 156a8e1175bSopenharmony_ci /* The first `total - offset` passes are a no-op. The last 157a8e1175bSopenharmony_ci * `offset` passes shift the data one byte to the left and 158a8e1175bSopenharmony_ci * zero out the last byte. */ 159a8e1175bSopenharmony_ci for (size_t n = 0; n < total - 1; n++) { 160a8e1175bSopenharmony_ci unsigned char current = buf[n]; 161a8e1175bSopenharmony_ci unsigned char next = buf[n+1]; 162a8e1175bSopenharmony_ci buf[n] = mbedtls_ct_uint_if(no_op, current, next); 163a8e1175bSopenharmony_ci } 164a8e1175bSopenharmony_ci buf[total-1] = mbedtls_ct_uint_if_else_0(no_op, buf[total-1]); 165a8e1175bSopenharmony_ci } 166a8e1175bSopenharmony_ci} 167a8e1175bSopenharmony_ci 168a8e1175bSopenharmony_ci#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */ 169a8e1175bSopenharmony_ci 170a8e1175bSopenharmony_civoid mbedtls_ct_memcpy_if(mbedtls_ct_condition_t condition, 171a8e1175bSopenharmony_ci unsigned char *dest, 172a8e1175bSopenharmony_ci const unsigned char *src1, 173a8e1175bSopenharmony_ci const unsigned char *src2, 174a8e1175bSopenharmony_ci size_t len) 175a8e1175bSopenharmony_ci{ 176a8e1175bSopenharmony_ci#if defined(MBEDTLS_CT_SIZE_64) 177a8e1175bSopenharmony_ci const uint64_t mask = (uint64_t) condition; 178a8e1175bSopenharmony_ci const uint64_t not_mask = (uint64_t) ~mbedtls_ct_compiler_opaque(condition); 179a8e1175bSopenharmony_ci#else 180a8e1175bSopenharmony_ci const uint32_t mask = (uint32_t) condition; 181a8e1175bSopenharmony_ci const uint32_t not_mask = (uint32_t) ~mbedtls_ct_compiler_opaque(condition); 182a8e1175bSopenharmony_ci#endif 183a8e1175bSopenharmony_ci 184a8e1175bSopenharmony_ci /* If src2 is NULL, setup src2 so that we read from the destination address. 185a8e1175bSopenharmony_ci * 186a8e1175bSopenharmony_ci * This means that if src2 == NULL && condition is false, the result will be a 187a8e1175bSopenharmony_ci * no-op because we read from dest and write the same data back into dest. 188a8e1175bSopenharmony_ci */ 189a8e1175bSopenharmony_ci if (src2 == NULL) { 190a8e1175bSopenharmony_ci src2 = dest; 191a8e1175bSopenharmony_ci } 192a8e1175bSopenharmony_ci 193a8e1175bSopenharmony_ci /* dest[i] = c1 == c2 ? src[i] : dest[i] */ 194a8e1175bSopenharmony_ci size_t i = 0; 195a8e1175bSopenharmony_ci#if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS) 196a8e1175bSopenharmony_ci#if defined(MBEDTLS_CT_SIZE_64) 197a8e1175bSopenharmony_ci for (; (i + 8) <= len; i += 8) { 198a8e1175bSopenharmony_ci uint64_t a = mbedtls_get_unaligned_uint64(src1 + i) & mask; 199a8e1175bSopenharmony_ci uint64_t b = mbedtls_get_unaligned_uint64(src2 + i) & not_mask; 200a8e1175bSopenharmony_ci mbedtls_put_unaligned_uint64(dest + i, a | b); 201a8e1175bSopenharmony_ci } 202a8e1175bSopenharmony_ci#else 203a8e1175bSopenharmony_ci for (; (i + 4) <= len; i += 4) { 204a8e1175bSopenharmony_ci uint32_t a = mbedtls_get_unaligned_uint32(src1 + i) & mask; 205a8e1175bSopenharmony_ci uint32_t b = mbedtls_get_unaligned_uint32(src2 + i) & not_mask; 206a8e1175bSopenharmony_ci mbedtls_put_unaligned_uint32(dest + i, a | b); 207a8e1175bSopenharmony_ci } 208a8e1175bSopenharmony_ci#endif /* defined(MBEDTLS_CT_SIZE_64) */ 209a8e1175bSopenharmony_ci#endif /* MBEDTLS_EFFICIENT_UNALIGNED_ACCESS */ 210a8e1175bSopenharmony_ci for (; i < len; i++) { 211a8e1175bSopenharmony_ci dest[i] = (src1[i] & mask) | (src2[i] & not_mask); 212a8e1175bSopenharmony_ci } 213a8e1175bSopenharmony_ci} 214a8e1175bSopenharmony_ci 215a8e1175bSopenharmony_civoid mbedtls_ct_memcpy_offset(unsigned char *dest, 216a8e1175bSopenharmony_ci const unsigned char *src, 217a8e1175bSopenharmony_ci size_t offset, 218a8e1175bSopenharmony_ci size_t offset_min, 219a8e1175bSopenharmony_ci size_t offset_max, 220a8e1175bSopenharmony_ci size_t len) 221a8e1175bSopenharmony_ci{ 222a8e1175bSopenharmony_ci size_t offsetval; 223a8e1175bSopenharmony_ci 224a8e1175bSopenharmony_ci for (offsetval = offset_min; offsetval <= offset_max; offsetval++) { 225a8e1175bSopenharmony_ci mbedtls_ct_memcpy_if(mbedtls_ct_uint_eq(offsetval, offset), dest, src + offsetval, NULL, 226a8e1175bSopenharmony_ci len); 227a8e1175bSopenharmony_ci } 228a8e1175bSopenharmony_ci} 229a8e1175bSopenharmony_ci 230a8e1175bSopenharmony_ci#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT) 231a8e1175bSopenharmony_ci 232a8e1175bSopenharmony_civoid mbedtls_ct_zeroize_if(mbedtls_ct_condition_t condition, void *buf, size_t len) 233a8e1175bSopenharmony_ci{ 234a8e1175bSopenharmony_ci uint32_t mask = (uint32_t) ~condition; 235a8e1175bSopenharmony_ci uint8_t *p = (uint8_t *) buf; 236a8e1175bSopenharmony_ci size_t i = 0; 237a8e1175bSopenharmony_ci#if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS) 238a8e1175bSopenharmony_ci for (; (i + 4) <= len; i += 4) { 239a8e1175bSopenharmony_ci mbedtls_put_unaligned_uint32((void *) (p + i), 240a8e1175bSopenharmony_ci mbedtls_get_unaligned_uint32((void *) (p + i)) & mask); 241a8e1175bSopenharmony_ci } 242a8e1175bSopenharmony_ci#endif 243a8e1175bSopenharmony_ci for (; i < len; i++) { 244a8e1175bSopenharmony_ci p[i] = p[i] & mask; 245a8e1175bSopenharmony_ci } 246a8e1175bSopenharmony_ci} 247a8e1175bSopenharmony_ci 248a8e1175bSopenharmony_ci#endif /* defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT) */ 249