gabor-mezei-arm | db9a38c | 2021-09-27 11:28:54 +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 | |
| 20 | #include "common.h" |
| 21 | |
gabor-mezei-arm | 3f90fd5 | 2021-09-27 12:55:33 +0200 | [diff] [blame] | 22 | #if defined(MBEDTLS_BIGNUM_C) |
| 23 | #include "mbedtls/bignum.h" |
| 24 | #endif |
| 25 | |
gabor-mezei-arm | db9a38c | 2021-09-27 11:28:54 +0200 | [diff] [blame] | 26 | #include <stddef.h> |
| 27 | |
| 28 | int mbedtls_ssl_safer_memcmp( const void *a, const void *b, size_t n ); |
| 29 | |
| 30 | int mbedtls_constant_time_memcmp( const void *v1, const void *v2, size_t len ); |
| 31 | |
| 32 | unsigned char mbedtls_nist_kw_safer_memcmp( const void *a, const void *b, size_t n ); |
| 33 | |
| 34 | int mbedtls_safer_memcmp( const void *a, const void *b, size_t n ); |
gabor-mezei-arm | 340948e | 2021-09-27 11:40:03 +0200 | [diff] [blame] | 35 | |
| 36 | |
| 37 | unsigned mbedtls_cf_uint_mask( unsigned value ); |
gabor-mezei-arm | 3733bf8 | 2021-09-27 11:49:42 +0200 | [diff] [blame] | 38 | |
| 39 | size_t mbedtls_cf_size_mask( size_t bit ); |
gabor-mezei-arm | c76227d | 2021-09-27 11:53:54 +0200 | [diff] [blame] | 40 | |
| 41 | size_t mbedtls_cf_size_mask_lt( size_t x, size_t y ); |
gabor-mezei-arm | 16fc57b | 2021-09-27 11:58:31 +0200 | [diff] [blame] | 42 | |
| 43 | size_t mbedtls_cf_size_mask_ge( size_t x, size_t y ); |
gabor-mezei-arm | 8d1d5fd | 2021-09-27 12:15:19 +0200 | [diff] [blame] | 44 | |
| 45 | size_t mbedtls_cf_size_bool_eq( size_t x, size_t y ); |
gabor-mezei-arm | 5a85442 | 2021-09-27 12:25:07 +0200 | [diff] [blame] | 46 | |
| 47 | unsigned mbedtls_cf_size_gt( size_t size, size_t max ); |
gabor-mezei-arm | 3f90fd5 | 2021-09-27 12:55:33 +0200 | [diff] [blame] | 48 | |
| 49 | #if defined(MBEDTLS_BIGNUM_C) |
| 50 | |
| 51 | unsigned mbedtls_cf_mpi_uint_lt( const mbedtls_mpi_uint x, |
| 52 | const mbedtls_mpi_uint y ); |
| 53 | |
| 54 | #endif /* MBEDTLS_BIGNUM_C */ |
gabor-mezei-arm | b2dbf2c | 2021-09-27 12:59:30 +0200 | [diff] [blame] | 55 | |
| 56 | unsigned mbedtls_cf_uint_if( unsigned cond, unsigned if1, unsigned if0 ); |
| 57 | |
gabor-mezei-arm | d3230d5 | 2021-09-27 13:03:57 +0200 | [diff] [blame] | 58 | int mbedtls_cf_cond_select_sign( int a, int b, unsigned char second ); |
gabor-mezei-arm | be8d98b | 2021-09-27 13:17:15 +0200 | [diff] [blame] | 59 | |
| 60 | #if defined(MBEDTLS_BIGNUM_C) |
| 61 | |
| 62 | void mbedtls_cf_mpi_uint_cond_assign( size_t n, |
| 63 | mbedtls_mpi_uint *dest, |
| 64 | const mbedtls_mpi_uint *src, |
| 65 | unsigned char assign ); |
| 66 | |
| 67 | #endif /* MBEDTLS_BIGNUM_C */ |
gabor-mezei-arm | 394aeaa | 2021-09-27 13:31:06 +0200 | [diff] [blame] | 68 | |
| 69 | void mbedtls_cf_mem_move_to_left( void *start, |
| 70 | size_t total, |
| 71 | size_t offset ); |
gabor-mezei-arm | dee0fd3 | 2021-09-27 13:34:25 +0200 | [diff] [blame] | 72 | |
| 73 | void mbedtls_cf_memcpy_if_eq( unsigned char *dst, |
| 74 | const unsigned char *src, |
| 75 | size_t len, |
| 76 | size_t c1, size_t c2 ); |
gabor-mezei-arm | 0e7f71e | 2021-09-27 13:57:45 +0200 | [diff] [blame^] | 77 | |
| 78 | /** Copy data from a secret position with constant flow. |
| 79 | * |
| 80 | * This function copies \p len bytes from \p src_base + \p offset_secret to \p |
| 81 | * dst, with a code flow and memory access pattern that does not depend on \p |
| 82 | * offset_secret, but only on \p offset_min, \p offset_max and \p len. |
| 83 | * |
| 84 | * \param dst The destination buffer. This must point to a writable |
| 85 | * buffer of at least \p len bytes. |
| 86 | * \param src_base The base of the source buffer. This must point to a |
| 87 | * readable buffer of at least \p offset_max + \p len |
| 88 | * bytes. |
| 89 | * \param offset_secret The offset in the source buffer from which to copy. |
| 90 | * This must be no less than \p offset_min and no greater |
| 91 | * than \p offset_max. |
| 92 | * \param offset_min The minimal value of \p offset_secret. |
| 93 | * \param offset_max The maximal value of \p offset_secret. |
| 94 | * \param len The number of bytes to copy. |
| 95 | */ |
| 96 | void mbedtls_cf_memcpy_offset( unsigned char *dst, |
| 97 | const unsigned char *src_base, |
| 98 | size_t offset_secret, |
| 99 | size_t offset_min, size_t offset_max, |
| 100 | size_t len ); |