blob: e6deb45b1f6700cb72be18c323bb4da2d50d09da [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
gabor-mezei-arm04087df2021-09-27 16:29:52 +020032int mbedtls_ssl_safer_memcmp( const void *a,
33 const void *b,
34 size_t n );
gabor-mezei-arm944c1072021-09-27 11:28:54 +020035
gabor-mezei-arm04087df2021-09-27 16:29:52 +020036int mbedtls_constant_time_memcmp( const void *v1,
37 const void *v2,
38 size_t len );
gabor-mezei-arm944c1072021-09-27 11:28:54 +020039
gabor-mezei-arm04087df2021-09-27 16:29:52 +020040unsigned char mbedtls_nist_kw_safer_memcmp( const void *a,
41 const void *b,
42 size_t n );
gabor-mezei-arm944c1072021-09-27 11:28:54 +020043
gabor-mezei-arm04087df2021-09-27 16:29:52 +020044int mbedtls_safer_memcmp( const void *a,
45 const void *b,
46 size_t n );
gabor-mezei-armc11cac92021-09-27 11:40:03 +020047
48
49unsigned mbedtls_cf_uint_mask( unsigned value );
gabor-mezei-armd361ccd2021-09-27 11:49:42 +020050
51size_t mbedtls_cf_size_mask( size_t bit );
gabor-mezei-arm4d6b1462021-09-27 11:53:54 +020052
gabor-mezei-arm04087df2021-09-27 16:29:52 +020053size_t mbedtls_cf_size_mask_lt( size_t x,
54 size_t y );
gabor-mezei-arma2bcabc2021-09-27 11:58:31 +020055
gabor-mezei-arm04087df2021-09-27 16:29:52 +020056size_t mbedtls_cf_size_mask_ge( size_t x,
57 size_t y );
gabor-mezei-arm96584dd2021-09-27 12:15:19 +020058
gabor-mezei-arm04087df2021-09-27 16:29:52 +020059size_t mbedtls_cf_size_bool_eq( size_t x,
60 size_t y );
gabor-mezei-arm9d7bf092021-09-27 12:25:07 +020061
gabor-mezei-arm04087df2021-09-27 16:29:52 +020062unsigned mbedtls_cf_size_gt( size_t size,
63 size_t max );
gabor-mezei-arm097d4f52021-09-27 12:55:33 +020064
65#if defined(MBEDTLS_BIGNUM_C)
66
67unsigned mbedtls_cf_mpi_uint_lt( const mbedtls_mpi_uint x,
68 const mbedtls_mpi_uint y );
69
70#endif /* MBEDTLS_BIGNUM_C */
gabor-mezei-arm75332532021-09-27 12:59:30 +020071
gabor-mezei-arm04087df2021-09-27 16:29:52 +020072unsigned mbedtls_cf_uint_if( unsigned cond,
73 unsigned if1,
74 unsigned if0 );
gabor-mezei-arm75332532021-09-27 12:59:30 +020075
gabor-mezei-arm04087df2021-09-27 16:29:52 +020076size_t mbedtls_cf_size_if( unsigned cond,
77 size_t if1,
78 size_t if0 );
gabor-mezei-armbc3a2882021-09-27 15:47:00 +020079
gabor-mezei-arm04087df2021-09-27 16:29:52 +020080int mbedtls_cf_cond_select_sign( int a,
81 int b,
82 unsigned char second );
gabor-mezei-arm043192d2021-09-27 13:17:15 +020083
84#if defined(MBEDTLS_BIGNUM_C)
85
86void mbedtls_cf_mpi_uint_cond_assign( size_t n,
87 mbedtls_mpi_uint *dest,
88 const mbedtls_mpi_uint *src,
89 unsigned char assign );
90
91#endif /* MBEDTLS_BIGNUM_C */
gabor-mezei-arm7b23c0b2021-09-27 13:31:06 +020092
93void mbedtls_cf_mem_move_to_left( void *start,
94 size_t total,
95 size_t offset );
gabor-mezei-armee06feb2021-09-27 13:34:25 +020096
97void mbedtls_cf_memcpy_if_eq( unsigned char *dst,
98 const unsigned char *src,
99 size_t len,
100 size_t c1, size_t c2 );
gabor-mezei-arm0f7b9e42021-09-27 13:57:45 +0200101
102/** Copy data from a secret position with constant flow.
103 *
104 * This function copies \p len bytes from \p src_base + \p offset_secret to \p
105 * dst, with a code flow and memory access pattern that does not depend on \p
106 * offset_secret, but only on \p offset_min, \p offset_max and \p len.
107 *
108 * \param dst The destination buffer. This must point to a writable
109 * buffer of at least \p len bytes.
110 * \param src_base The base of the source buffer. This must point to a
111 * readable buffer of at least \p offset_max + \p len
112 * bytes.
113 * \param offset_secret The offset in the source buffer from which to copy.
114 * This must be no less than \p offset_min and no greater
115 * than \p offset_max.
116 * \param offset_min The minimal value of \p offset_secret.
117 * \param offset_max The maximal value of \p offset_secret.
118 * \param len The number of bytes to copy.
119 */
120void mbedtls_cf_memcpy_offset( unsigned char *dst,
121 const unsigned char *src_base,
122 size_t offset_secret,
gabor-mezei-arm04087df2021-09-27 16:29:52 +0200123 size_t offset_min,
124 size_t offset_max,
gabor-mezei-arm0f7b9e42021-09-27 13:57:45 +0200125 size_t len );
gabor-mezei-armcb4317b2021-09-27 14:28:31 +0200126
127#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
128
129/** Compute the HMAC of variable-length data with constant flow.
130 *
131 * This function computes the HMAC of the concatenation of \p add_data and \p
132 * data, and does with a code flow and memory access pattern that does not
133 * depend on \p data_len_secret, but only on \p min_data_len and \p
134 * max_data_len. In particular, this function always reads exactly \p
135 * max_data_len bytes from \p data.
136 *
137 * \param ctx The HMAC context. It must have keys configured
138 * with mbedtls_md_hmac_starts() and use one of the
139 * following hashes: SHA-384, SHA-256, SHA-1 or MD-5.
140 * It is reset using mbedtls_md_hmac_reset() after
141 * the computation is complete to prepare for the
142 * next computation.
143 * \param add_data The additional data prepended to \p data. This
144 * must point to a readable buffer of \p add_data_len
145 * bytes.
146 * \param add_data_len The length of \p add_data in bytes.
147 * \param data The data appended to \p add_data. This must point
148 * to a readable buffer of \p max_data_len bytes.
149 * \param data_len_secret The length of the data to process in \p data.
150 * This must be no less than \p min_data_len and no
151 * greater than \p max_data_len.
152 * \param min_data_len The minimal length of \p data in bytes.
153 * \param max_data_len The maximal length of \p data in bytes.
154 * \param output The HMAC will be written here. This must point to
155 * a writable buffer of sufficient size to hold the
156 * HMAC value.
157 *
158 * \retval 0 on success.
159 * \retval MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED
160 * The hardware accelerator failed.
161 */
gabor-mezei-arm04087df2021-09-27 16:29:52 +0200162int mbedtls_cf_hmac( mbedtls_md_context_t *ctx,
163 const unsigned char *add_data,
164 size_t add_data_len,
165 const unsigned char *data,
166 size_t data_len_secret,
167 size_t min_data_len,
168 size_t max_data_len,
169 unsigned char *output );
gabor-mezei-armcb4317b2021-09-27 14:28:31 +0200170
171#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
gabor-mezei-armf52941e2021-09-27 16:11:12 +0200172
173#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
174
175int mbedtls_cf_rsaes_pkcs1_v15_unpadding( int mode,
176 size_t ilen,
177 size_t *olen,
178 unsigned char *output,
179 size_t output_max_len,
180 unsigned char *buf );
181
182#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */