blob: a8d142b856668960d56bd1c9b679af06e2b7240e [file] [log] [blame]
gabor-mezei-arm944c1072021-09-27 11:28:54 +02001/**
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-arm097d4f52021-09-27 12:55:33 +020022#if defined(MBEDTLS_BIGNUM_C)
23#include "mbedtls/bignum.h"
24#endif
25
gabor-mezei-armcb4317b2021-09-27 14:28:31 +020026#if defined(MBEDTLS_SSL_TLS_C)
27#include "mbedtls/ssl_internal.h"
28#endif
29
gabor-mezei-arm944c1072021-09-27 11:28:54 +020030#include <stddef.h>
31
32int mbedtls_ssl_safer_memcmp( const void *a, const void *b, size_t n );
33
34int mbedtls_constant_time_memcmp( const void *v1, const void *v2, size_t len );
35
36unsigned char mbedtls_nist_kw_safer_memcmp( const void *a, const void *b, size_t n );
37
38int mbedtls_safer_memcmp( const void *a, const void *b, size_t n );
gabor-mezei-armc11cac92021-09-27 11:40:03 +020039
40
41unsigned mbedtls_cf_uint_mask( unsigned value );
gabor-mezei-armd361ccd2021-09-27 11:49:42 +020042
43size_t mbedtls_cf_size_mask( size_t bit );
gabor-mezei-arm4d6b1462021-09-27 11:53:54 +020044
45size_t mbedtls_cf_size_mask_lt( size_t x, size_t y );
gabor-mezei-arma2bcabc2021-09-27 11:58:31 +020046
47size_t mbedtls_cf_size_mask_ge( size_t x, size_t y );
gabor-mezei-arm96584dd2021-09-27 12:15:19 +020048
49size_t mbedtls_cf_size_bool_eq( size_t x, size_t y );
gabor-mezei-arm9d7bf092021-09-27 12:25:07 +020050
51unsigned mbedtls_cf_size_gt( size_t size, size_t max );
gabor-mezei-arm097d4f52021-09-27 12:55:33 +020052
53#if defined(MBEDTLS_BIGNUM_C)
54
55unsigned mbedtls_cf_mpi_uint_lt( const mbedtls_mpi_uint x,
56 const mbedtls_mpi_uint y );
57
58#endif /* MBEDTLS_BIGNUM_C */
gabor-mezei-arm75332532021-09-27 12:59:30 +020059
60unsigned mbedtls_cf_uint_if( unsigned cond, unsigned if1, unsigned if0 );
61
gabor-mezei-arm5cec8b42021-09-27 13:03:57 +020062int mbedtls_cf_cond_select_sign( int a, int b, unsigned char second );
gabor-mezei-arm043192d2021-09-27 13:17:15 +020063
64#if defined(MBEDTLS_BIGNUM_C)
65
66void mbedtls_cf_mpi_uint_cond_assign( size_t n,
67 mbedtls_mpi_uint *dest,
68 const mbedtls_mpi_uint *src,
69 unsigned char assign );
70
71#endif /* MBEDTLS_BIGNUM_C */
gabor-mezei-arm7b23c0b2021-09-27 13:31:06 +020072
73void mbedtls_cf_mem_move_to_left( void *start,
74 size_t total,
75 size_t offset );
gabor-mezei-armee06feb2021-09-27 13:34:25 +020076
77void mbedtls_cf_memcpy_if_eq( unsigned char *dst,
78 const unsigned char *src,
79 size_t len,
80 size_t c1, size_t c2 );
gabor-mezei-arm0f7b9e42021-09-27 13:57:45 +020081
82/** Copy data from a secret position with constant flow.
83 *
84 * This function copies \p len bytes from \p src_base + \p offset_secret to \p
85 * dst, with a code flow and memory access pattern that does not depend on \p
86 * offset_secret, but only on \p offset_min, \p offset_max and \p len.
87 *
88 * \param dst The destination buffer. This must point to a writable
89 * buffer of at least \p len bytes.
90 * \param src_base The base of the source buffer. This must point to a
91 * readable buffer of at least \p offset_max + \p len
92 * bytes.
93 * \param offset_secret The offset in the source buffer from which to copy.
94 * This must be no less than \p offset_min and no greater
95 * than \p offset_max.
96 * \param offset_min The minimal value of \p offset_secret.
97 * \param offset_max The maximal value of \p offset_secret.
98 * \param len The number of bytes to copy.
99 */
100void mbedtls_cf_memcpy_offset( unsigned char *dst,
101 const unsigned char *src_base,
102 size_t offset_secret,
103 size_t offset_min, size_t offset_max,
104 size_t len );
gabor-mezei-armcb4317b2021-09-27 14:28:31 +0200105
106#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
107
108/** Compute the HMAC of variable-length data with constant flow.
109 *
110 * This function computes the HMAC of the concatenation of \p add_data and \p
111 * data, and does with a code flow and memory access pattern that does not
112 * depend on \p data_len_secret, but only on \p min_data_len and \p
113 * max_data_len. In particular, this function always reads exactly \p
114 * max_data_len bytes from \p data.
115 *
116 * \param ctx The HMAC context. It must have keys configured
117 * with mbedtls_md_hmac_starts() and use one of the
118 * following hashes: SHA-384, SHA-256, SHA-1 or MD-5.
119 * It is reset using mbedtls_md_hmac_reset() after
120 * the computation is complete to prepare for the
121 * next computation.
122 * \param add_data The additional data prepended to \p data. This
123 * must point to a readable buffer of \p add_data_len
124 * bytes.
125 * \param add_data_len The length of \p add_data in bytes.
126 * \param data The data appended to \p add_data. This must point
127 * to a readable buffer of \p max_data_len bytes.
128 * \param data_len_secret The length of the data to process in \p data.
129 * This must be no less than \p min_data_len and no
130 * greater than \p max_data_len.
131 * \param min_data_len The minimal length of \p data in bytes.
132 * \param max_data_len The maximal length of \p data in bytes.
133 * \param output The HMAC will be written here. This must point to
134 * a writable buffer of sufficient size to hold the
135 * HMAC value.
136 *
137 * \retval 0 on success.
138 * \retval MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED
139 * The hardware accelerator failed.
140 */
141int mbedtls_ssl_cf_hmac(
142 mbedtls_md_context_t *ctx,
143 const unsigned char *add_data, size_t add_data_len,
144 const unsigned char *data, size_t data_len_secret,
145 size_t min_data_len, size_t max_data_len,
146 unsigned char *output );
147
148#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */