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 | 1349ffd | 2021-09-27 14:28:31 +0200 | [diff] [blame] | 26 | #if defined(MBEDTLS_SSL_TLS_C) |
| 27 | #include "ssl_misc.h" |
| 28 | #endif |
| 29 | |
gabor-mezei-arm | db9a38c | 2021-09-27 11:28:54 +0200 | [diff] [blame] | 30 | #include <stddef.h> |
| 31 | |
gabor-mezei-arm | db9a38c | 2021-09-27 11:28:54 +0200 | [diff] [blame] | 32 | |
gabor-mezei-arm | 4602564 | 2021-07-19 15:19:19 +0200 | [diff] [blame] | 33 | int mbedtls_cf_memcmp( const void *a, |
| 34 | const void *b, |
| 35 | size_t n ); |
gabor-mezei-arm | 340948e | 2021-09-27 11:40:03 +0200 | [diff] [blame] | 36 | |
| 37 | unsigned mbedtls_cf_uint_mask( unsigned value ); |
gabor-mezei-arm | 3733bf8 | 2021-09-27 11:49:42 +0200 | [diff] [blame] | 38 | |
gabor-mezei-arm | 396438c | 2021-08-10 20:56:21 +0200 | [diff] [blame] | 39 | size_t mbedtls_cf_size_mask( size_t value ); |
gabor-mezei-arm | c76227d | 2021-09-27 11:53:54 +0200 | [diff] [blame] | 40 | |
gabor-mezei-arm | 9cb5569 | 2021-08-11 15:07:02 +0200 | [diff] [blame^] | 41 | #if defined(MBEDTLS_BIGNUM_C) |
| 42 | |
| 43 | mbedtls_mpi_uint mbedtls_cf_mpi_uint_mask( mbedtls_mpi_uint value ); |
| 44 | |
| 45 | #endif /* MBEDTLS_BIGNUM_C */ |
| 46 | |
gabor-mezei-arm | 2dcd768 | 2021-09-27 16:29:52 +0200 | [diff] [blame] | 47 | size_t mbedtls_cf_size_mask_lt( size_t x, |
| 48 | size_t y ); |
gabor-mezei-arm | 16fc57b | 2021-09-27 11:58:31 +0200 | [diff] [blame] | 49 | |
gabor-mezei-arm | 2dcd768 | 2021-09-27 16:29:52 +0200 | [diff] [blame] | 50 | size_t mbedtls_cf_size_mask_ge( size_t x, |
| 51 | size_t y ); |
gabor-mezei-arm | 8d1d5fd | 2021-09-27 12:15:19 +0200 | [diff] [blame] | 52 | |
gabor-mezei-arm | 2dcd768 | 2021-09-27 16:29:52 +0200 | [diff] [blame] | 53 | size_t mbedtls_cf_size_bool_eq( size_t x, |
| 54 | size_t y ); |
gabor-mezei-arm | 5a85442 | 2021-09-27 12:25:07 +0200 | [diff] [blame] | 55 | |
gabor-mezei-arm | 87ac5be | 2021-08-10 20:36:09 +0200 | [diff] [blame] | 56 | unsigned mbedtls_cf_size_gt( size_t x, |
| 57 | size_t y ); |
gabor-mezei-arm | 3f90fd5 | 2021-09-27 12:55:33 +0200 | [diff] [blame] | 58 | |
| 59 | #if defined(MBEDTLS_BIGNUM_C) |
| 60 | |
| 61 | unsigned mbedtls_cf_mpi_uint_lt( const mbedtls_mpi_uint x, |
| 62 | const mbedtls_mpi_uint y ); |
| 63 | |
| 64 | #endif /* MBEDTLS_BIGNUM_C */ |
gabor-mezei-arm | b2dbf2c | 2021-09-27 12:59:30 +0200 | [diff] [blame] | 65 | |
gabor-mezei-arm | 87ac5be | 2021-08-10 20:36:09 +0200 | [diff] [blame] | 66 | unsigned mbedtls_cf_uint_if( unsigned condition, |
gabor-mezei-arm | 2dcd768 | 2021-09-27 16:29:52 +0200 | [diff] [blame] | 67 | unsigned if1, |
| 68 | unsigned if0 ); |
gabor-mezei-arm | b2dbf2c | 2021-09-27 12:59:30 +0200 | [diff] [blame] | 69 | |
gabor-mezei-arm | 87ac5be | 2021-08-10 20:36:09 +0200 | [diff] [blame] | 70 | size_t mbedtls_cf_size_if( unsigned condition, |
gabor-mezei-arm | 2dcd768 | 2021-09-27 16:29:52 +0200 | [diff] [blame] | 71 | size_t if1, |
| 72 | size_t if0 ); |
gabor-mezei-arm | 65cefdb | 2021-09-27 15:47:00 +0200 | [diff] [blame] | 73 | |
gabor-mezei-arm | 87ac5be | 2021-08-10 20:36:09 +0200 | [diff] [blame] | 74 | int mbedtls_cf_cond_select_sign( unsigned char condition, |
| 75 | int if1, |
| 76 | int if0 ); |
gabor-mezei-arm | be8d98b | 2021-09-27 13:17:15 +0200 | [diff] [blame] | 77 | |
| 78 | #if defined(MBEDTLS_BIGNUM_C) |
| 79 | |
| 80 | void mbedtls_cf_mpi_uint_cond_assign( size_t n, |
| 81 | mbedtls_mpi_uint *dest, |
| 82 | const mbedtls_mpi_uint *src, |
gabor-mezei-arm | 87ac5be | 2021-08-10 20:36:09 +0200 | [diff] [blame] | 83 | unsigned char condition ); |
gabor-mezei-arm | be8d98b | 2021-09-27 13:17:15 +0200 | [diff] [blame] | 84 | |
| 85 | #endif /* MBEDTLS_BIGNUM_C */ |
gabor-mezei-arm | 394aeaa | 2021-09-27 13:31:06 +0200 | [diff] [blame] | 86 | |
| 87 | void mbedtls_cf_mem_move_to_left( void *start, |
| 88 | size_t total, |
| 89 | size_t offset ); |
gabor-mezei-arm | dee0fd3 | 2021-09-27 13:34:25 +0200 | [diff] [blame] | 90 | |
gabor-mezei-arm | 87ac5be | 2021-08-10 20:36:09 +0200 | [diff] [blame] | 91 | void mbedtls_cf_memcpy_if_eq( unsigned char *dest, |
gabor-mezei-arm | dee0fd3 | 2021-09-27 13:34:25 +0200 | [diff] [blame] | 92 | const unsigned char *src, |
| 93 | size_t len, |
| 94 | size_t c1, size_t c2 ); |
gabor-mezei-arm | 0e7f71e | 2021-09-27 13:57:45 +0200 | [diff] [blame] | 95 | |
| 96 | /** Copy data from a secret position with constant flow. |
| 97 | * |
| 98 | * This function copies \p len bytes from \p src_base + \p offset_secret to \p |
| 99 | * dst, with a code flow and memory access pattern that does not depend on \p |
| 100 | * offset_secret, but only on \p offset_min, \p offset_max and \p len. |
| 101 | * |
| 102 | * \param dst The destination buffer. This must point to a writable |
| 103 | * buffer of at least \p len bytes. |
| 104 | * \param src_base The base of the source buffer. This must point to a |
| 105 | * readable buffer of at least \p offset_max + \p len |
| 106 | * bytes. |
| 107 | * \param offset_secret The offset in the source buffer from which to copy. |
| 108 | * This must be no less than \p offset_min and no greater |
| 109 | * than \p offset_max. |
| 110 | * \param offset_min The minimal value of \p offset_secret. |
| 111 | * \param offset_max The maximal value of \p offset_secret. |
| 112 | * \param len The number of bytes to copy. |
| 113 | */ |
| 114 | void mbedtls_cf_memcpy_offset( unsigned char *dst, |
| 115 | const unsigned char *src_base, |
| 116 | size_t offset_secret, |
gabor-mezei-arm | 2dcd768 | 2021-09-27 16:29:52 +0200 | [diff] [blame] | 117 | size_t offset_min, |
| 118 | size_t offset_max, |
gabor-mezei-arm | 0e7f71e | 2021-09-27 13:57:45 +0200 | [diff] [blame] | 119 | size_t len ); |
gabor-mezei-arm | 1349ffd | 2021-09-27 14:28:31 +0200 | [diff] [blame] | 120 | |
| 121 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC) |
| 122 | |
| 123 | /** Compute the HMAC of variable-length data with constant flow. |
| 124 | * |
| 125 | * This function computes the HMAC of the concatenation of \p add_data and \p |
| 126 | * data, and does with a code flow and memory access pattern that does not |
| 127 | * depend on \p data_len_secret, but only on \p min_data_len and \p |
| 128 | * max_data_len. In particular, this function always reads exactly \p |
| 129 | * max_data_len bytes from \p data. |
| 130 | * |
| 131 | * \param ctx The HMAC context. It must have keys configured |
| 132 | * with mbedtls_md_hmac_starts() and use one of the |
| 133 | * following hashes: SHA-384, SHA-256, SHA-1 or MD-5. |
| 134 | * It is reset using mbedtls_md_hmac_reset() after |
| 135 | * the computation is complete to prepare for the |
| 136 | * next computation. |
| 137 | * \param add_data The additional data prepended to \p data. This |
| 138 | * must point to a readable buffer of \p add_data_len |
| 139 | * bytes. |
| 140 | * \param add_data_len The length of \p add_data in bytes. |
| 141 | * \param data The data appended to \p add_data. This must point |
| 142 | * to a readable buffer of \p max_data_len bytes. |
| 143 | * \param data_len_secret The length of the data to process in \p data. |
| 144 | * This must be no less than \p min_data_len and no |
| 145 | * greater than \p max_data_len. |
| 146 | * \param min_data_len The minimal length of \p data in bytes. |
| 147 | * \param max_data_len The maximal length of \p data in bytes. |
| 148 | * \param output The HMAC will be written here. This must point to |
| 149 | * a writable buffer of sufficient size to hold the |
| 150 | * HMAC value. |
| 151 | * |
| 152 | * \retval 0 on success. |
| 153 | * \retval MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED |
| 154 | * The hardware accelerator failed. |
| 155 | */ |
gabor-mezei-arm | 2dcd768 | 2021-09-27 16:29:52 +0200 | [diff] [blame] | 156 | int mbedtls_cf_hmac( mbedtls_md_context_t *ctx, |
| 157 | const unsigned char *add_data, |
| 158 | size_t add_data_len, |
| 159 | const unsigned char *data, |
| 160 | size_t data_len_secret, |
| 161 | size_t min_data_len, |
| 162 | size_t max_data_len, |
| 163 | unsigned char *output ); |
gabor-mezei-arm | 1349ffd | 2021-09-27 14:28:31 +0200 | [diff] [blame] | 164 | |
| 165 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */ |
gabor-mezei-arm | fdb7118 | 2021-09-27 16:11:12 +0200 | [diff] [blame] | 166 | |
| 167 | #if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT) |
| 168 | |
| 169 | int mbedtls_cf_rsaes_pkcs1_v15_unpadding( size_t ilen, |
| 170 | size_t *olen, |
| 171 | unsigned char *output, |
| 172 | size_t output_max_len, |
| 173 | unsigned char *buf ); |
| 174 | |
| 175 | #endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */ |