Coverage Report

Created: 2024-11-30 06:06

/src/vtpm-td/deps/spdm-rs/external/ring/crypto/internal.h
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2
 * All rights reserved.
3
 *
4
 * This package is an SSL implementation written
5
 * by Eric Young (eay@cryptsoft.com).
6
 * The implementation was written so as to conform with Netscapes SSL.
7
 *
8
 * This library is free for commercial and non-commercial use as long as
9
 * the following conditions are aheared to.  The following conditions
10
 * apply to all code found in this distribution, be it the RC4, RSA,
11
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12
 * included with this distribution is covered by the same copyright terms
13
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14
 *
15
 * Copyright remains Eric Young's, and as such any Copyright notices in
16
 * the code are not to be removed.
17
 * If this package is used in a product, Eric Young should be given attribution
18
 * as the author of the parts of the library used.
19
 * This can be in the form of a textual message at program startup or
20
 * in documentation (online or textual) provided with the package.
21
 *
22
 * Redistribution and use in source and binary forms, with or without
23
 * modification, are permitted provided that the following conditions
24
 * are met:
25
 * 1. Redistributions of source code must retain the copyright
26
 *    notice, this list of conditions and the following disclaimer.
27
 * 2. Redistributions in binary form must reproduce the above copyright
28
 *    notice, this list of conditions and the following disclaimer in the
29
 *    documentation and/or other materials provided with the distribution.
30
 * 3. All advertising materials mentioning features or use of this software
31
 *    must display the following acknowledgement:
32
 *    "This product includes cryptographic software written by
33
 *     Eric Young (eay@cryptsoft.com)"
34
 *    The word 'cryptographic' can be left out if the rouines from the library
35
 *    being used are not cryptographic related :-).
36
 * 4. If you include any Windows specific code (or a derivative thereof) from
37
 *    the apps directory (application code) you must include an acknowledgement:
38
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39
 *
40
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50
 * SUCH DAMAGE.
51
 *
52
 * The licence and distribution terms for any publically available version or
53
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
54
 * copied and put under another distribution licence
55
 * [including the GNU Public Licence.]
56
 */
57
/* ====================================================================
58
 * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
59
 *
60
 * Redistribution and use in source and binary forms, with or without
61
 * modification, are permitted provided that the following conditions
62
 * are met:
63
 *
64
 * 1. Redistributions of source code must retain the above copyright
65
 *    notice, this list of conditions and the following disclaimer.
66
 *
67
 * 2. Redistributions in binary form must reproduce the above copyright
68
 *    notice, this list of conditions and the following disclaimer in
69
 *    the documentation and/or other materials provided with the
70
 *    distribution.
71
 *
72
 * 3. All advertising materials mentioning features or use of this
73
 *    software must display the following acknowledgment:
74
 *    "This product includes software developed by the OpenSSL Project
75
 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76
 *
77
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78
 *    endorse or promote products derived from this software without
79
 *    prior written permission. For written permission, please contact
80
 *    openssl-core@openssl.org.
81
 *
82
 * 5. Products derived from this software may not be called "OpenSSL"
83
 *    nor may "OpenSSL" appear in their names without prior written
84
 *    permission of the OpenSSL Project.
85
 *
86
 * 6. Redistributions of any form whatsoever must retain the following
87
 *    acknowledgment:
88
 *    "This product includes software developed by the OpenSSL Project
89
 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90
 *
91
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
95
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102
 * OF THE POSSIBILITY OF SUCH DAMAGE.
103
 * ====================================================================
104
 *
105
 * This product includes cryptographic software written by Eric Young
106
 * (eay@cryptsoft.com).  This product includes software written by Tim
107
 * Hudson (tjh@cryptsoft.com). */
108
109
#ifndef OPENSSL_HEADER_CRYPTO_INTERNAL_H
110
#define OPENSSL_HEADER_CRYPTO_INTERNAL_H
111
112
#include <ring-core/base.h> // Must be first.
113
114
#include "ring-core/arm_arch.h"
115
#include "ring-core/check.h"
116
117
#if defined(__clang__)
118
// Don't require prototypes for functions defined in C that are only
119
// used from Rust.
120
#pragma GCC diagnostic ignored "-Wmissing-prototypes"
121
#endif
122
123
#if defined(__GNUC__) && \
124
    (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40800
125
// |alignas| and |alignof| were added in C11. GCC added support in version 4.8.
126
// Testing for __STDC_VERSION__/__cplusplus doesn't work because 4.7 already
127
// reports support for C11.
128
0
#define alignas(x) __attribute__ ((aligned (x)))
129
#elif defined(_MSC_VER) && !defined(__clang__)
130
#define alignas(x) __declspec(align(x))
131
#else
132
#include <stdalign.h>
133
#endif
134
135
// Some C compilers require a useless cast when dealing with arrays for the
136
// reason explained in
137
// https://gustedt.wordpress.com/2011/02/12/const-and-arrays/
138
#if defined(__clang__) || defined(_MSC_VER)
139
#define RING_CORE_POINTLESS_ARRAY_CONST_CAST(cast)
140
#else
141
#define RING_CORE_POINTLESS_ARRAY_CONST_CAST(cast) cast
142
#endif
143
144
// `uint8_t` isn't guaranteed to be 'unsigned char' and only 'char' and
145
// 'unsigned char' are allowed to alias according to ISO C.
146
typedef unsigned char aliasing_uint8_t;
147
148
#if (!defined(_MSC_VER) || defined(__clang__)) && defined(OPENSSL_64_BIT)
149
#define BORINGSSL_HAS_UINT128
150
typedef __int128_t int128_t;
151
typedef __uint128_t uint128_t;
152
#endif
153
154
// Pointer utility functions.
155
156
// buffers_alias returns one if |a| and |b| alias and zero otherwise.
157
static inline int buffers_alias(const void *a, size_t a_bytes,
158
0
                                const void *b, size_t b_bytes) {
159
  // Cast |a| and |b| to integers. In C, pointer comparisons between unrelated
160
  // objects are undefined whereas pointer to integer conversions are merely
161
  // implementation-defined. We assume the implementation defined it in a sane
162
  // way.
163
0
  uintptr_t a_u = (uintptr_t)a;
164
0
  uintptr_t b_u = (uintptr_t)b;
165
0
  return a_u + a_bytes > b_u && b_u + b_bytes > a_u;
166
0
}
Unexecuted instantiation: curve25519.c:buffers_alias
Unexecuted instantiation: limbs.c:buffers_alias
Unexecuted instantiation: mem.c:buffers_alias
Unexecuted instantiation: curve25519_64_adx.c:buffers_alias
Unexecuted instantiation: poly1305_vec.c:buffers_alias
Unexecuted instantiation: p256-nistz.c:buffers_alias
Unexecuted instantiation: aes_nohw.c:buffers_alias
Unexecuted instantiation: montgomery.c:buffers_alias
Unexecuted instantiation: montgomery_inv.c:buffers_alias
Unexecuted instantiation: gfp_p384.c:buffers_alias
Unexecuted instantiation: cpu_intel.c:buffers_alias
Unexecuted instantiation: ecp_nistz.c:buffers_alias
167
168
169
// Constant-time utility functions.
170
//
171
// The following methods return a bitmask of all ones (0xff...f) for true and 0
172
// for false. This is useful for choosing a value based on the result of a
173
// conditional in constant time. For example,
174
//
175
// if (a < b) {
176
//   c = a;
177
// } else {
178
//   c = b;
179
// }
180
//
181
// can be written as
182
//
183
// crypto_word_t lt = constant_time_lt_w(a, b);
184
// c = constant_time_select_w(lt, a, b);
185
186
#if defined(__GNUC__) || defined(__clang__)
187
#pragma GCC diagnostic push
188
#pragma GCC diagnostic ignored "-Wconversion"
189
#endif
190
#if defined(_MSC_VER) && !defined(__clang__)
191
#pragma warning(push)
192
// '=': conversion from 'crypto_word_t' to 'uint8_t', possible loss of data
193
#pragma warning(disable: 4242)
194
//  'initializing': conversion from 'crypto_word_t' to 'uint8_t', ...
195
#pragma warning(disable: 4244)
196
#endif
197
198
// crypto_word_t is the type that most constant-time functions use. Ideally we
199
// would like it to be |size_t|, but NaCl builds in 64-bit mode with 32-bit
200
// pointers, which means that |size_t| can be 32 bits when |BN_ULONG| is 64
201
// bits. Since we want to be able to do constant-time operations on a
202
// |BN_ULONG|, |crypto_word_t| is defined as an unsigned value with the native
203
// word length.
204
#if defined(OPENSSL_64_BIT)
205
typedef uint64_t crypto_word_t;
206
0
#define CRYPTO_WORD_BITS (64u)
207
#elif defined(OPENSSL_32_BIT)
208
typedef uint32_t crypto_word_t;
209
#define CRYPTO_WORD_BITS (32u)
210
#else
211
#error "Must define either OPENSSL_32_BIT or OPENSSL_64_BIT"
212
#endif
213
214
0
#define CONSTTIME_TRUE_W ~((crypto_word_t)0)
215
#define CONSTTIME_FALSE_W ((crypto_word_t)0)
216
217
// value_barrier_w returns |a|, but prevents GCC and Clang from reasoning about
218
// the returned value. This is used to mitigate compilers undoing constant-time
219
// code, until we can express our requirements directly in the language.
220
//
221
// Note the compiler is aware that |value_barrier_w| has no side effects and
222
// always has the same output for a given input. This allows it to eliminate
223
// dead code, move computations across loops, and vectorize.
224
0
static inline crypto_word_t value_barrier_w(crypto_word_t a) {
225
0
#if defined(__GNUC__) || defined(__clang__)
226
0
  __asm__("" : "+r"(a) : /* no inputs */);
227
0
#endif
228
0
  return a;
229
0
}
Unexecuted instantiation: curve25519.c:value_barrier_w
Unexecuted instantiation: limbs.c:value_barrier_w
Unexecuted instantiation: mem.c:value_barrier_w
Unexecuted instantiation: curve25519_64_adx.c:value_barrier_w
Unexecuted instantiation: poly1305_vec.c:value_barrier_w
Unexecuted instantiation: p256-nistz.c:value_barrier_w
Unexecuted instantiation: aes_nohw.c:value_barrier_w
Unexecuted instantiation: montgomery.c:value_barrier_w
Unexecuted instantiation: montgomery_inv.c:value_barrier_w
Unexecuted instantiation: gfp_p384.c:value_barrier_w
Unexecuted instantiation: cpu_intel.c:value_barrier_w
Unexecuted instantiation: ecp_nistz.c:value_barrier_w
230
231
// |value_barrier_u8| could be defined as above, but compilers other than
232
// clang seem to still materialize 0x00..00MM instead of reusing 0x??..??MM.
233
234
// constant_time_msb_w returns the given value with the MSB copied to all the
235
// other bits.
236
0
static inline crypto_word_t constant_time_msb_w(crypto_word_t a) {
237
0
  return 0u - (a >> (sizeof(a) * 8 - 1));
238
0
}
Unexecuted instantiation: curve25519.c:constant_time_msb_w
Unexecuted instantiation: limbs.c:constant_time_msb_w
Unexecuted instantiation: mem.c:constant_time_msb_w
Unexecuted instantiation: curve25519_64_adx.c:constant_time_msb_w
Unexecuted instantiation: poly1305_vec.c:constant_time_msb_w
Unexecuted instantiation: p256-nistz.c:constant_time_msb_w
Unexecuted instantiation: aes_nohw.c:constant_time_msb_w
Unexecuted instantiation: montgomery.c:constant_time_msb_w
Unexecuted instantiation: montgomery_inv.c:constant_time_msb_w
Unexecuted instantiation: gfp_p384.c:constant_time_msb_w
Unexecuted instantiation: cpu_intel.c:constant_time_msb_w
Unexecuted instantiation: ecp_nistz.c:constant_time_msb_w
239
240
// constant_time_is_zero returns 0xff..f if a == 0 and 0 otherwise.
241
0
static inline crypto_word_t constant_time_is_zero_w(crypto_word_t a) {
242
  // Here is an SMT-LIB verification of this formula:
243
  //
244
  // (define-fun is_zero ((a (_ BitVec 32))) (_ BitVec 32)
245
  //   (bvand (bvnot a) (bvsub a #x00000001))
246
  // )
247
  //
248
  // (declare-fun a () (_ BitVec 32))
249
  //
250
  // (assert (not (= (= #x00000001 (bvlshr (is_zero a) #x0000001f)) (= a #x00000000))))
251
  // (check-sat)
252
  // (get-model)
253
0
  return constant_time_msb_w(~a & (a - 1));
254
0
}
Unexecuted instantiation: curve25519.c:constant_time_is_zero_w
Unexecuted instantiation: limbs.c:constant_time_is_zero_w
Unexecuted instantiation: mem.c:constant_time_is_zero_w
Unexecuted instantiation: curve25519_64_adx.c:constant_time_is_zero_w
Unexecuted instantiation: poly1305_vec.c:constant_time_is_zero_w
Unexecuted instantiation: p256-nistz.c:constant_time_is_zero_w
Unexecuted instantiation: aes_nohw.c:constant_time_is_zero_w
Unexecuted instantiation: montgomery.c:constant_time_is_zero_w
Unexecuted instantiation: montgomery_inv.c:constant_time_is_zero_w
Unexecuted instantiation: gfp_p384.c:constant_time_is_zero_w
Unexecuted instantiation: cpu_intel.c:constant_time_is_zero_w
Unexecuted instantiation: ecp_nistz.c:constant_time_is_zero_w
255
256
0
static inline crypto_word_t constant_time_is_nonzero_w(crypto_word_t a) {
257
0
  return ~constant_time_is_zero_w(a);
258
0
}
Unexecuted instantiation: curve25519.c:constant_time_is_nonzero_w
Unexecuted instantiation: limbs.c:constant_time_is_nonzero_w
Unexecuted instantiation: mem.c:constant_time_is_nonzero_w
Unexecuted instantiation: curve25519_64_adx.c:constant_time_is_nonzero_w
Unexecuted instantiation: poly1305_vec.c:constant_time_is_nonzero_w
Unexecuted instantiation: p256-nistz.c:constant_time_is_nonzero_w
Unexecuted instantiation: aes_nohw.c:constant_time_is_nonzero_w
Unexecuted instantiation: montgomery.c:constant_time_is_nonzero_w
Unexecuted instantiation: montgomery_inv.c:constant_time_is_nonzero_w
Unexecuted instantiation: gfp_p384.c:constant_time_is_nonzero_w
Unexecuted instantiation: cpu_intel.c:constant_time_is_nonzero_w
Unexecuted instantiation: ecp_nistz.c:constant_time_is_nonzero_w
259
260
// constant_time_eq_w returns 0xff..f if a == b and 0 otherwise.
261
static inline crypto_word_t constant_time_eq_w(crypto_word_t a,
262
0
                                               crypto_word_t b) {
263
0
  return constant_time_is_zero_w(a ^ b);
264
0
}
Unexecuted instantiation: curve25519.c:constant_time_eq_w
Unexecuted instantiation: limbs.c:constant_time_eq_w
Unexecuted instantiation: mem.c:constant_time_eq_w
Unexecuted instantiation: curve25519_64_adx.c:constant_time_eq_w
Unexecuted instantiation: poly1305_vec.c:constant_time_eq_w
Unexecuted instantiation: p256-nistz.c:constant_time_eq_w
Unexecuted instantiation: aes_nohw.c:constant_time_eq_w
Unexecuted instantiation: montgomery.c:constant_time_eq_w
Unexecuted instantiation: montgomery_inv.c:constant_time_eq_w
Unexecuted instantiation: gfp_p384.c:constant_time_eq_w
Unexecuted instantiation: cpu_intel.c:constant_time_eq_w
Unexecuted instantiation: ecp_nistz.c:constant_time_eq_w
265
266
// constant_time_select_w returns (mask & a) | (~mask & b). When |mask| is all
267
// 1s or all 0s (as returned by the methods above), the select methods return
268
// either |a| (if |mask| is nonzero) or |b| (if |mask| is zero).
269
static inline crypto_word_t constant_time_select_w(crypto_word_t mask,
270
                                                   crypto_word_t a,
271
0
                                                   crypto_word_t b) {
272
  // Clang recognizes this pattern as a select. While it usually transforms it
273
  // to a cmov, it sometimes further transforms it into a branch, which we do
274
  // not want.
275
  //
276
  // Hiding the value of the mask from the compiler evades this transformation.
277
0
  mask = value_barrier_w(mask);
278
0
  return (mask & a) | (~mask & b);
279
0
}
Unexecuted instantiation: curve25519.c:constant_time_select_w
Unexecuted instantiation: limbs.c:constant_time_select_w
Unexecuted instantiation: mem.c:constant_time_select_w
Unexecuted instantiation: curve25519_64_adx.c:constant_time_select_w
Unexecuted instantiation: poly1305_vec.c:constant_time_select_w
Unexecuted instantiation: p256-nistz.c:constant_time_select_w
Unexecuted instantiation: aes_nohw.c:constant_time_select_w
Unexecuted instantiation: montgomery.c:constant_time_select_w
Unexecuted instantiation: montgomery_inv.c:constant_time_select_w
Unexecuted instantiation: gfp_p384.c:constant_time_select_w
Unexecuted instantiation: cpu_intel.c:constant_time_select_w
Unexecuted instantiation: ecp_nistz.c:constant_time_select_w
280
281
// constant_time_select_8 acts like |constant_time_select| but operates on
282
// 8-bit values.
283
static inline uint8_t constant_time_select_8(crypto_word_t mask, uint8_t a,
284
0
                                             uint8_t b) {
285
  // |mask| is a word instead of |uint8_t| to avoid materializing 0x000..0MM
286
  // Making both |mask| and its value barrier |uint8_t| would allow the compiler
287
  // to materialize 0x????..?MM instead, but only clang is that clever.
288
  // However, vectorization of bitwise operations seems to work better on
289
  // |uint8_t| than a mix of |uint64_t| and |uint8_t|, so |m| is cast to
290
  // |uint8_t| after the value barrier but before the bitwise operations.
291
0
  uint8_t m = value_barrier_w(mask);
292
0
  return (m & a) | (~m & b);
293
0
}
Unexecuted instantiation: curve25519.c:constant_time_select_8
Unexecuted instantiation: limbs.c:constant_time_select_8
Unexecuted instantiation: mem.c:constant_time_select_8
Unexecuted instantiation: curve25519_64_adx.c:constant_time_select_8
Unexecuted instantiation: poly1305_vec.c:constant_time_select_8
Unexecuted instantiation: p256-nistz.c:constant_time_select_8
Unexecuted instantiation: aes_nohw.c:constant_time_select_8
Unexecuted instantiation: montgomery.c:constant_time_select_8
Unexecuted instantiation: montgomery_inv.c:constant_time_select_8
Unexecuted instantiation: gfp_p384.c:constant_time_select_8
Unexecuted instantiation: cpu_intel.c:constant_time_select_8
Unexecuted instantiation: ecp_nistz.c:constant_time_select_8
294
295
// constant_time_conditional_memcpy copies |n| bytes from |src| to |dst| if
296
// |mask| is 0xff..ff and does nothing if |mask| is 0. The |n|-byte memory
297
// ranges at |dst| and |src| must not overlap, as when calling |memcpy|.
298
static inline void constant_time_conditional_memcpy(void *dst, const void *src,
299
                                                    const size_t n,
300
0
                                                    const crypto_word_t mask) {
301
0
  debug_assert_nonsecret(!buffers_alias(dst, n, src, n));
302
0
  uint8_t *out = (uint8_t *)dst;
303
0
  const uint8_t *in = (const uint8_t *)src;
304
0
  for (size_t i = 0; i < n; i++) {
305
0
    out[i] = constant_time_select_8(mask, in[i], out[i]);
306
0
  }
307
0
}
Unexecuted instantiation: curve25519.c:constant_time_conditional_memcpy
Unexecuted instantiation: limbs.c:constant_time_conditional_memcpy
Unexecuted instantiation: mem.c:constant_time_conditional_memcpy
Unexecuted instantiation: curve25519_64_adx.c:constant_time_conditional_memcpy
Unexecuted instantiation: poly1305_vec.c:constant_time_conditional_memcpy
Unexecuted instantiation: p256-nistz.c:constant_time_conditional_memcpy
Unexecuted instantiation: aes_nohw.c:constant_time_conditional_memcpy
Unexecuted instantiation: montgomery.c:constant_time_conditional_memcpy
Unexecuted instantiation: montgomery_inv.c:constant_time_conditional_memcpy
Unexecuted instantiation: gfp_p384.c:constant_time_conditional_memcpy
Unexecuted instantiation: cpu_intel.c:constant_time_conditional_memcpy
Unexecuted instantiation: ecp_nistz.c:constant_time_conditional_memcpy
308
309
// constant_time_conditional_memxor xors |n| bytes from |src| to |dst| if
310
// |mask| is 0xff..ff and does nothing if |mask| is 0. The |n|-byte memory
311
// ranges at |dst| and |src| must not overlap, as when calling |memcpy|.
312
static inline void constant_time_conditional_memxor(void *dst, const void *src,
313
                                                    const size_t n,
314
0
                                                    const crypto_word_t mask) {
315
0
  debug_assert_nonsecret(!buffers_alias(dst, n, src, n));
316
0
  aliasing_uint8_t *out = dst;
317
0
  const aliasing_uint8_t *in = src;
318
0
  for (size_t i = 0; i < n; i++) {
319
0
    out[i] ^= value_barrier_w(mask) & in[i];
320
0
  }
321
0
}
Unexecuted instantiation: curve25519.c:constant_time_conditional_memxor
Unexecuted instantiation: limbs.c:constant_time_conditional_memxor
Unexecuted instantiation: mem.c:constant_time_conditional_memxor
Unexecuted instantiation: curve25519_64_adx.c:constant_time_conditional_memxor
Unexecuted instantiation: poly1305_vec.c:constant_time_conditional_memxor
Unexecuted instantiation: p256-nistz.c:constant_time_conditional_memxor
Unexecuted instantiation: aes_nohw.c:constant_time_conditional_memxor
Unexecuted instantiation: montgomery.c:constant_time_conditional_memxor
Unexecuted instantiation: montgomery_inv.c:constant_time_conditional_memxor
Unexecuted instantiation: gfp_p384.c:constant_time_conditional_memxor
Unexecuted instantiation: cpu_intel.c:constant_time_conditional_memxor
Unexecuted instantiation: ecp_nistz.c:constant_time_conditional_memxor
322
323
#if defined(_MSC_VER) && !defined(__clang__)
324
// '=': conversion from 'int64_t' to 'int32_t', possible loss of data
325
#pragma warning(pop)
326
#endif
327
#if defined(__GNUC__) || defined(__clang__)
328
#pragma GCC diagnostic pop
329
#endif
330
331
#if defined(BORINGSSL_CONSTANT_TIME_VALIDATION)
332
333
// CONSTTIME_SECRET takes a pointer and a number of bytes and marks that region
334
// of memory as secret. Secret data is tracked as it flows to registers and
335
// other parts of a memory. If secret data is used as a condition for a branch,
336
// or as a memory index, it will trigger warnings in valgrind.
337
#define CONSTTIME_SECRET(ptr, len) VALGRIND_MAKE_MEM_UNDEFINED(ptr, len)
338
339
// CONSTTIME_DECLASSIFY takes a pointer and a number of bytes and marks that
340
// region of memory as public. Public data is not subject to constant-time
341
// rules.
342
#define CONSTTIME_DECLASSIFY(ptr, len) VALGRIND_MAKE_MEM_DEFINED(ptr, len)
343
344
#else
345
346
#define CONSTTIME_SECRET(ptr, len)
347
#define CONSTTIME_DECLASSIFY(ptr, len)
348
349
#endif  // BORINGSSL_CONSTANT_TIME_VALIDATION
350
351
0
static inline crypto_word_t constant_time_declassify_w(crypto_word_t v) {
352
0
  // Return |v| through a value barrier to be safe. Valgrind-based constant-time
353
0
  // validation is partly to check the compiler has not undone any constant-time
354
0
  // work. Any place |BORINGSSL_CONSTANT_TIME_VALIDATION| influences
355
0
  // optimizations, this validation is inaccurate.
356
0
  //
357
0
  // However, by sending pointers through valgrind, we likely inhibit escape
358
0
  // analysis. On local variables, particularly booleans, we likely
359
0
  // significantly impact optimizations.
360
0
  //
361
0
  // Thus, to be safe, stick a value barrier, in hopes of comparably inhibiting
362
0
  // compiler analysis.
363
0
  CONSTTIME_DECLASSIFY(&v, sizeof(v));
364
0
  return value_barrier_w(v);
365
0
}
Unexecuted instantiation: curve25519.c:constant_time_declassify_w
Unexecuted instantiation: limbs.c:constant_time_declassify_w
Unexecuted instantiation: mem.c:constant_time_declassify_w
Unexecuted instantiation: curve25519_64_adx.c:constant_time_declassify_w
Unexecuted instantiation: poly1305_vec.c:constant_time_declassify_w
Unexecuted instantiation: p256-nistz.c:constant_time_declassify_w
Unexecuted instantiation: aes_nohw.c:constant_time_declassify_w
Unexecuted instantiation: montgomery.c:constant_time_declassify_w
Unexecuted instantiation: montgomery_inv.c:constant_time_declassify_w
Unexecuted instantiation: gfp_p384.c:constant_time_declassify_w
Unexecuted instantiation: cpu_intel.c:constant_time_declassify_w
Unexecuted instantiation: ecp_nistz.c:constant_time_declassify_w
366
367
// Endianness conversions.
368
369
#if defined(__GNUC__) && __GNUC__ >= 2
370
0
static inline uint32_t CRYPTO_bswap4(uint32_t x) {
371
0
  return __builtin_bswap32(x);
372
0
}
Unexecuted instantiation: curve25519.c:CRYPTO_bswap4
Unexecuted instantiation: limbs.c:CRYPTO_bswap4
Unexecuted instantiation: mem.c:CRYPTO_bswap4
Unexecuted instantiation: curve25519_64_adx.c:CRYPTO_bswap4
Unexecuted instantiation: poly1305_vec.c:CRYPTO_bswap4
Unexecuted instantiation: p256-nistz.c:CRYPTO_bswap4
Unexecuted instantiation: aes_nohw.c:CRYPTO_bswap4
Unexecuted instantiation: montgomery.c:CRYPTO_bswap4
Unexecuted instantiation: montgomery_inv.c:CRYPTO_bswap4
Unexecuted instantiation: gfp_p384.c:CRYPTO_bswap4
Unexecuted instantiation: cpu_intel.c:CRYPTO_bswap4
Unexecuted instantiation: ecp_nistz.c:CRYPTO_bswap4
373
374
0
static inline uint64_t CRYPTO_bswap8(uint64_t x) {
375
0
  return __builtin_bswap64(x);
376
0
}
Unexecuted instantiation: curve25519.c:CRYPTO_bswap8
Unexecuted instantiation: limbs.c:CRYPTO_bswap8
Unexecuted instantiation: mem.c:CRYPTO_bswap8
Unexecuted instantiation: curve25519_64_adx.c:CRYPTO_bswap8
Unexecuted instantiation: poly1305_vec.c:CRYPTO_bswap8
Unexecuted instantiation: p256-nistz.c:CRYPTO_bswap8
Unexecuted instantiation: aes_nohw.c:CRYPTO_bswap8
Unexecuted instantiation: montgomery.c:CRYPTO_bswap8
Unexecuted instantiation: montgomery_inv.c:CRYPTO_bswap8
Unexecuted instantiation: gfp_p384.c:CRYPTO_bswap8
Unexecuted instantiation: cpu_intel.c:CRYPTO_bswap8
Unexecuted instantiation: ecp_nistz.c:CRYPTO_bswap8
377
#elif defined(_MSC_VER)
378
#pragma warning(push, 3)
379
#include <stdlib.h>
380
#pragma warning(pop)
381
#pragma intrinsic(_byteswap_uint64, _byteswap_ulong)
382
static inline uint32_t CRYPTO_bswap4(uint32_t x) {
383
  return _byteswap_ulong(x);
384
}
385
386
static inline uint64_t CRYPTO_bswap8(uint64_t x) {
387
  return _byteswap_uint64(x);
388
}
389
#endif
390
391
#if !defined(RING_CORE_NOSTDLIBINC)
392
#include <string.h>
393
#endif
394
395
0
static inline void *OPENSSL_memcpy(void *dst, const void *src, size_t n) {
396
0
#if !defined(RING_CORE_NOSTDLIBINC)
397
0
  if (n == 0) {
398
0
    return dst;
399
0
  }
400
0
  return memcpy(dst, src, n);
401
#else
402
  aliasing_uint8_t *d = dst;
403
  const aliasing_uint8_t *s = src;
404
  for (size_t i = 0; i < n; ++i) {
405
    d[i] = s[i];
406
  }
407
  return dst;
408
#endif
409
0
}
Unexecuted instantiation: curve25519.c:OPENSSL_memcpy
Unexecuted instantiation: limbs.c:OPENSSL_memcpy
Unexecuted instantiation: mem.c:OPENSSL_memcpy
Unexecuted instantiation: curve25519_64_adx.c:OPENSSL_memcpy
Unexecuted instantiation: poly1305_vec.c:OPENSSL_memcpy
Unexecuted instantiation: p256-nistz.c:OPENSSL_memcpy
Unexecuted instantiation: aes_nohw.c:OPENSSL_memcpy
Unexecuted instantiation: montgomery.c:OPENSSL_memcpy
Unexecuted instantiation: montgomery_inv.c:OPENSSL_memcpy
Unexecuted instantiation: gfp_p384.c:OPENSSL_memcpy
Unexecuted instantiation: cpu_intel.c:OPENSSL_memcpy
Unexecuted instantiation: ecp_nistz.c:OPENSSL_memcpy
410
411
0
static inline void *OPENSSL_memset(void *dst, int c, size_t n) {
412
0
#if !defined(RING_CORE_NOSTDLIBINC)
413
0
  if (n == 0) {
414
0
    return dst;
415
0
  }
416
0
  return memset(dst, c, n);
417
#else
418
  aliasing_uint8_t *d = dst;
419
  for (size_t i = 0; i < n; ++i) {
420
    d[i] = (aliasing_uint8_t)c;
421
  }
422
  return dst;
423
#endif
424
0
}
Unexecuted instantiation: curve25519.c:OPENSSL_memset
Unexecuted instantiation: limbs.c:OPENSSL_memset
Unexecuted instantiation: mem.c:OPENSSL_memset
Unexecuted instantiation: curve25519_64_adx.c:OPENSSL_memset
Unexecuted instantiation: poly1305_vec.c:OPENSSL_memset
Unexecuted instantiation: p256-nistz.c:OPENSSL_memset
Unexecuted instantiation: aes_nohw.c:OPENSSL_memset
Unexecuted instantiation: montgomery.c:OPENSSL_memset
Unexecuted instantiation: montgomery_inv.c:OPENSSL_memset
Unexecuted instantiation: gfp_p384.c:OPENSSL_memset
Unexecuted instantiation: cpu_intel.c:OPENSSL_memset
Unexecuted instantiation: ecp_nistz.c:OPENSSL_memset
425
426
427
// Loads and stores.
428
//
429
// The following functions load and store sized integers with the specified
430
// endianness. They use |memcpy|, and so avoid alignment or strict aliasing
431
// requirements on the input and output pointers.
432
433
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__)
434
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
435
#define RING_BIG_ENDIAN
436
#endif
437
#endif
438
439
0
static inline uint32_t CRYPTO_load_u32_le(const void *in) {
440
0
  uint32_t v;
441
0
  OPENSSL_memcpy(&v, in, sizeof(v));
442
#if defined(RING_BIG_ENDIAN)
443
  return CRYPTO_bswap4(v);
444
#else
445
0
  return v;
446
0
#endif
447
0
}
Unexecuted instantiation: curve25519.c:CRYPTO_load_u32_le
Unexecuted instantiation: limbs.c:CRYPTO_load_u32_le
Unexecuted instantiation: mem.c:CRYPTO_load_u32_le
Unexecuted instantiation: curve25519_64_adx.c:CRYPTO_load_u32_le
Unexecuted instantiation: poly1305_vec.c:CRYPTO_load_u32_le
Unexecuted instantiation: p256-nistz.c:CRYPTO_load_u32_le
Unexecuted instantiation: aes_nohw.c:CRYPTO_load_u32_le
Unexecuted instantiation: montgomery.c:CRYPTO_load_u32_le
Unexecuted instantiation: montgomery_inv.c:CRYPTO_load_u32_le
Unexecuted instantiation: gfp_p384.c:CRYPTO_load_u32_le
Unexecuted instantiation: cpu_intel.c:CRYPTO_load_u32_le
Unexecuted instantiation: ecp_nistz.c:CRYPTO_load_u32_le
448
449
0
static inline void CRYPTO_store_u32_le(void *out, uint32_t v) {
450
0
#if defined(RING_BIG_ENDIAN)
451
0
  v = CRYPTO_bswap4(v);
452
0
#endif
453
0
  OPENSSL_memcpy(out, &v, sizeof(v));
454
0
}
Unexecuted instantiation: curve25519.c:CRYPTO_store_u32_le
Unexecuted instantiation: limbs.c:CRYPTO_store_u32_le
Unexecuted instantiation: mem.c:CRYPTO_store_u32_le
Unexecuted instantiation: curve25519_64_adx.c:CRYPTO_store_u32_le
Unexecuted instantiation: poly1305_vec.c:CRYPTO_store_u32_le
Unexecuted instantiation: p256-nistz.c:CRYPTO_store_u32_le
Unexecuted instantiation: aes_nohw.c:CRYPTO_store_u32_le
Unexecuted instantiation: montgomery.c:CRYPTO_store_u32_le
Unexecuted instantiation: montgomery_inv.c:CRYPTO_store_u32_le
Unexecuted instantiation: gfp_p384.c:CRYPTO_store_u32_le
Unexecuted instantiation: cpu_intel.c:CRYPTO_store_u32_le
Unexecuted instantiation: ecp_nistz.c:CRYPTO_store_u32_le
455
456
0
static inline uint32_t CRYPTO_load_u32_be(const void *in) {
457
0
  uint32_t v;
458
0
  OPENSSL_memcpy(&v, in, sizeof(v));
459
0
#if !defined(RING_BIG_ENDIAN)
460
0
  return CRYPTO_bswap4(v);
461
#else
462
  return v;
463
#endif
464
0
}
Unexecuted instantiation: curve25519.c:CRYPTO_load_u32_be
Unexecuted instantiation: limbs.c:CRYPTO_load_u32_be
Unexecuted instantiation: mem.c:CRYPTO_load_u32_be
Unexecuted instantiation: curve25519_64_adx.c:CRYPTO_load_u32_be
Unexecuted instantiation: poly1305_vec.c:CRYPTO_load_u32_be
Unexecuted instantiation: p256-nistz.c:CRYPTO_load_u32_be
Unexecuted instantiation: aes_nohw.c:CRYPTO_load_u32_be
Unexecuted instantiation: montgomery.c:CRYPTO_load_u32_be
Unexecuted instantiation: montgomery_inv.c:CRYPTO_load_u32_be
Unexecuted instantiation: gfp_p384.c:CRYPTO_load_u32_be
Unexecuted instantiation: cpu_intel.c:CRYPTO_load_u32_be
Unexecuted instantiation: ecp_nistz.c:CRYPTO_load_u32_be
465
466
0
static inline void CRYPTO_store_u32_be(void *out, uint32_t v) {
467
0
#if !defined(RING_BIG_ENDIAN)
468
0
  v = CRYPTO_bswap4(v);
469
0
#endif
470
0
  OPENSSL_memcpy(out, &v, sizeof(v));
471
0
}
Unexecuted instantiation: curve25519.c:CRYPTO_store_u32_be
Unexecuted instantiation: limbs.c:CRYPTO_store_u32_be
Unexecuted instantiation: mem.c:CRYPTO_store_u32_be
Unexecuted instantiation: curve25519_64_adx.c:CRYPTO_store_u32_be
Unexecuted instantiation: poly1305_vec.c:CRYPTO_store_u32_be
Unexecuted instantiation: p256-nistz.c:CRYPTO_store_u32_be
Unexecuted instantiation: aes_nohw.c:CRYPTO_store_u32_be
Unexecuted instantiation: montgomery.c:CRYPTO_store_u32_be
Unexecuted instantiation: montgomery_inv.c:CRYPTO_store_u32_be
Unexecuted instantiation: gfp_p384.c:CRYPTO_store_u32_be
Unexecuted instantiation: cpu_intel.c:CRYPTO_store_u32_be
Unexecuted instantiation: ecp_nistz.c:CRYPTO_store_u32_be
472
473
0
static inline uint64_t CRYPTO_load_u64_le(const void *in) {
474
0
  uint64_t v;
475
0
  OPENSSL_memcpy(&v, in, sizeof(v));
476
#if defined(RING_BIG_ENDIAN)
477
  return CRYPTO_bswap8(v);
478
#else
479
0
  return v;
480
0
#endif
481
0
}
Unexecuted instantiation: curve25519.c:CRYPTO_load_u64_le
Unexecuted instantiation: limbs.c:CRYPTO_load_u64_le
Unexecuted instantiation: mem.c:CRYPTO_load_u64_le
Unexecuted instantiation: curve25519_64_adx.c:CRYPTO_load_u64_le
Unexecuted instantiation: poly1305_vec.c:CRYPTO_load_u64_le
Unexecuted instantiation: p256-nistz.c:CRYPTO_load_u64_le
Unexecuted instantiation: aes_nohw.c:CRYPTO_load_u64_le
Unexecuted instantiation: montgomery.c:CRYPTO_load_u64_le
Unexecuted instantiation: montgomery_inv.c:CRYPTO_load_u64_le
Unexecuted instantiation: gfp_p384.c:CRYPTO_load_u64_le
Unexecuted instantiation: cpu_intel.c:CRYPTO_load_u64_le
Unexecuted instantiation: ecp_nistz.c:CRYPTO_load_u64_le
482
483
0
static inline void CRYPTO_store_u64_le(void *out, uint64_t v) {
484
#if defined(RING_BIG_ENDIAN)
485
  v = CRYPTO_bswap8(v);
486
#endif
487
0
  OPENSSL_memcpy(out, &v, sizeof(v));
488
0
}
Unexecuted instantiation: curve25519.c:CRYPTO_store_u64_le
Unexecuted instantiation: limbs.c:CRYPTO_store_u64_le
Unexecuted instantiation: mem.c:CRYPTO_store_u64_le
Unexecuted instantiation: curve25519_64_adx.c:CRYPTO_store_u64_le
Unexecuted instantiation: poly1305_vec.c:CRYPTO_store_u64_le
Unexecuted instantiation: p256-nistz.c:CRYPTO_store_u64_le
Unexecuted instantiation: aes_nohw.c:CRYPTO_store_u64_le
Unexecuted instantiation: montgomery.c:CRYPTO_store_u64_le
Unexecuted instantiation: montgomery_inv.c:CRYPTO_store_u64_le
Unexecuted instantiation: gfp_p384.c:CRYPTO_store_u64_le
Unexecuted instantiation: cpu_intel.c:CRYPTO_store_u64_le
Unexecuted instantiation: ecp_nistz.c:CRYPTO_store_u64_le
489
490
0
static inline uint64_t CRYPTO_load_u64_be(const void *ptr) {
491
0
  uint64_t ret;
492
0
  OPENSSL_memcpy(&ret, ptr, sizeof(ret));
493
0
#if !defined(RING_BIG_ENDIAN)
494
0
  return CRYPTO_bswap8(ret);
495
0
#else
496
0
  return ret;
497
0
#endif
498
0
}
Unexecuted instantiation: curve25519.c:CRYPTO_load_u64_be
Unexecuted instantiation: limbs.c:CRYPTO_load_u64_be
Unexecuted instantiation: mem.c:CRYPTO_load_u64_be
Unexecuted instantiation: curve25519_64_adx.c:CRYPTO_load_u64_be
Unexecuted instantiation: poly1305_vec.c:CRYPTO_load_u64_be
Unexecuted instantiation: p256-nistz.c:CRYPTO_load_u64_be
Unexecuted instantiation: aes_nohw.c:CRYPTO_load_u64_be
Unexecuted instantiation: montgomery.c:CRYPTO_load_u64_be
Unexecuted instantiation: montgomery_inv.c:CRYPTO_load_u64_be
Unexecuted instantiation: gfp_p384.c:CRYPTO_load_u64_be
Unexecuted instantiation: cpu_intel.c:CRYPTO_load_u64_be
Unexecuted instantiation: ecp_nistz.c:CRYPTO_load_u64_be
499
500
0
static inline void CRYPTO_store_u64_be(void *out, uint64_t v) {
501
0
#if !defined(RING_BIG_ENDIAN)
502
0
  v = CRYPTO_bswap8(v);
503
0
#endif
504
0
  OPENSSL_memcpy(out, &v, sizeof(v));
505
0
}
Unexecuted instantiation: curve25519.c:CRYPTO_store_u64_be
Unexecuted instantiation: limbs.c:CRYPTO_store_u64_be
Unexecuted instantiation: mem.c:CRYPTO_store_u64_be
Unexecuted instantiation: curve25519_64_adx.c:CRYPTO_store_u64_be
Unexecuted instantiation: poly1305_vec.c:CRYPTO_store_u64_be
Unexecuted instantiation: p256-nistz.c:CRYPTO_store_u64_be
Unexecuted instantiation: aes_nohw.c:CRYPTO_store_u64_be
Unexecuted instantiation: montgomery.c:CRYPTO_store_u64_be
Unexecuted instantiation: montgomery_inv.c:CRYPTO_store_u64_be
Unexecuted instantiation: gfp_p384.c:CRYPTO_store_u64_be
Unexecuted instantiation: cpu_intel.c:CRYPTO_store_u64_be
Unexecuted instantiation: ecp_nistz.c:CRYPTO_store_u64_be
506
507
508
// Runtime CPU feature support
509
510
#if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
511
// OPENSSL_ia32cap_P contains the Intel CPUID bits when running on an x86 or
512
// x86-64 system.
513
//
514
//   Index 0:
515
//     EDX for CPUID where EAX = 1
516
//     Bit 20 is always zero
517
//     Bit 28 is adjusted to reflect whether the data cache is shared between
518
//       multiple logical cores
519
//     Bit 30 is used to indicate an Intel CPU
520
//   Index 1:
521
//     ECX for CPUID where EAX = 1
522
//     Bit 11 is used to indicate AMD XOP support, not SDBG
523
//   Index 2:
524
//     EBX for CPUID where EAX = 7
525
//   Index 3:
526
//     ECX for CPUID where EAX = 7
527
//
528
// Note: the CPUID bits are pre-adjusted for the OSXSAVE bit and the YMM and XMM
529
// bits in XCR0, so it is not necessary to check those.
530
extern uint32_t OPENSSL_ia32cap_P[4];
531
#endif
532
533
#endif  // OPENSSL_HEADER_CRYPTO_INTERNAL_H