Coverage Report

Created: 2023-09-28 22:20

/src/mbedtls/library/common.h
Line
Count
Source (jump to first uncovered line)
1
/**
2
 * \file common.h
3
 *
4
 * \brief Utility macros for internal use in the library
5
 */
6
/*
7
 *  Copyright The Mbed TLS Contributors
8
 *  SPDX-License-Identifier: Apache-2.0
9
 *
10
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may
11
 *  not use this file except in compliance with the License.
12
 *  You may obtain a copy of the License at
13
 *
14
 *  http://www.apache.org/licenses/LICENSE-2.0
15
 *
16
 *  Unless required by applicable law or agreed to in writing, software
17
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18
 *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
 *  See the License for the specific language governing permissions and
20
 *  limitations under the License.
21
 */
22
23
#ifndef MBEDTLS_LIBRARY_COMMON_H
24
#define MBEDTLS_LIBRARY_COMMON_H
25
26
#include "mbedtls/build_info.h"
27
#include "alignment.h"
28
29
#include <stddef.h>
30
#include <stdint.h>
31
#include <stddef.h>
32
33
/** Helper to define a function as static except when building invasive tests.
34
 *
35
 * If a function is only used inside its own source file and should be
36
 * declared `static` to allow the compiler to optimize for code size,
37
 * but that function has unit tests, define it with
38
 * ```
39
 * MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... }
40
 * ```
41
 * and declare it in a header in the `library/` directory with
42
 * ```
43
 * #if defined(MBEDTLS_TEST_HOOKS)
44
 * int mbedtls_foo(...);
45
 * #endif
46
 * ```
47
 */
48
#if defined(MBEDTLS_TEST_HOOKS)
49
#define MBEDTLS_STATIC_TESTABLE
50
#else
51
#define MBEDTLS_STATIC_TESTABLE static
52
#endif
53
54
#if defined(MBEDTLS_TEST_HOOKS)
55
extern void (*mbedtls_test_hook_test_fail)(const char *test, int line, const char *file);
56
#define MBEDTLS_TEST_HOOK_TEST_ASSERT(TEST) \
57
    do { \
58
        if ((!(TEST)) && ((*mbedtls_test_hook_test_fail) != NULL)) \
59
        { \
60
            (*mbedtls_test_hook_test_fail)( #TEST, __LINE__, __FILE__); \
61
        } \
62
    } while (0)
63
#else
64
#define MBEDTLS_TEST_HOOK_TEST_ASSERT(TEST)
65
#endif /* defined(MBEDTLS_TEST_HOOKS) */
66
67
/** Allow library to access its structs' private members.
68
 *
69
 * Although structs defined in header files are publicly available,
70
 * their members are private and should not be accessed by the user.
71
 */
72
#define MBEDTLS_ALLOW_PRIVATE_ACCESS
73
74
/** Return an offset into a buffer.
75
 *
76
 * This is just the addition of an offset to a pointer, except that this
77
 * function also accepts an offset of 0 into a buffer whose pointer is null.
78
 * (`p + n` has undefined behavior when `p` is null, even when `n == 0`.
79
 * A null pointer is a valid buffer pointer when the size is 0, for example
80
 * as the result of `malloc(0)` on some platforms.)
81
 *
82
 * \param p     Pointer to a buffer of at least n bytes.
83
 *              This may be \p NULL if \p n is zero.
84
 * \param n     An offset in bytes.
85
 * \return      Pointer to offset \p n in the buffer \p p.
86
 *              Note that this is only a valid pointer if the size of the
87
 *              buffer is at least \p n + 1.
88
 */
89
static inline unsigned char *mbedtls_buffer_offset(
90
    unsigned char *p, size_t n)
91
0
{
92
0
    return p == NULL ? NULL : p + n;
93
0
}
Unexecuted instantiation: bignum_helpers.c:mbedtls_buffer_offset
Unexecuted instantiation: certs.c:mbedtls_buffer_offset
Unexecuted instantiation: ssl_msg.c:mbedtls_buffer_offset
Unexecuted instantiation: ssl_tls.c:mbedtls_buffer_offset
Unexecuted instantiation: ssl_tls12_client.c:mbedtls_buffer_offset
Unexecuted instantiation: ssl_tls12_server.c:mbedtls_buffer_offset
Unexecuted instantiation: debug.c:mbedtls_buffer_offset
Unexecuted instantiation: ssl_ciphersuites.c:mbedtls_buffer_offset
Unexecuted instantiation: ssl_client.c:mbedtls_buffer_offset
Unexecuted instantiation: ssl_debug_helpers_generated.c:mbedtls_buffer_offset
Unexecuted instantiation: x509.c:mbedtls_buffer_offset
Unexecuted instantiation: x509_crt.c:mbedtls_buffer_offset
Unexecuted instantiation: asn1parse.c:mbedtls_buffer_offset
Unexecuted instantiation: bignum.c:mbedtls_buffer_offset
Unexecuted instantiation: bignum_core.c:mbedtls_buffer_offset
Unexecuted instantiation: bignum_mod.c:mbedtls_buffer_offset
Unexecuted instantiation: bignum_mod_raw.c:mbedtls_buffer_offset
Unexecuted instantiation: cipher.c:mbedtls_buffer_offset
Unexecuted instantiation: cipher_wrap.c:mbedtls_buffer_offset
Unexecuted instantiation: constant_time.c:mbedtls_buffer_offset
Unexecuted instantiation: ctr_drbg.c:mbedtls_buffer_offset
Unexecuted instantiation: des.c:mbedtls_buffer_offset
Unexecuted instantiation: dhm.c:mbedtls_buffer_offset
Unexecuted instantiation: ecdh.c:mbedtls_buffer_offset
Unexecuted instantiation: ecp.c:mbedtls_buffer_offset
Unexecuted instantiation: ecp_curves.c:mbedtls_buffer_offset
Unexecuted instantiation: entropy.c:mbedtls_buffer_offset
Unexecuted instantiation: entropy_poll.c:mbedtls_buffer_offset
Unexecuted instantiation: gcm.c:mbedtls_buffer_offset
Unexecuted instantiation: hash_info.c:mbedtls_buffer_offset
Unexecuted instantiation: md.c:mbedtls_buffer_offset
Unexecuted instantiation: md5.c:mbedtls_buffer_offset
Unexecuted instantiation: nist_kw.c:mbedtls_buffer_offset
Unexecuted instantiation: oid.c:mbedtls_buffer_offset
Unexecuted instantiation: pem.c:mbedtls_buffer_offset
Unexecuted instantiation: pk.c:mbedtls_buffer_offset
Unexecuted instantiation: pk_wrap.c:mbedtls_buffer_offset
Unexecuted instantiation: pkparse.c:mbedtls_buffer_offset
Unexecuted instantiation: pkwrite.c:mbedtls_buffer_offset
Unexecuted instantiation: platform.c:mbedtls_buffer_offset
Unexecuted instantiation: platform_util.c:mbedtls_buffer_offset
Unexecuted instantiation: psa_crypto.c:mbedtls_buffer_offset
Unexecuted instantiation: psa_crypto_client.c:mbedtls_buffer_offset
Unexecuted instantiation: psa_crypto_driver_wrappers.c:mbedtls_buffer_offset
Unexecuted instantiation: psa_crypto_ecp.c:mbedtls_buffer_offset
Unexecuted instantiation: psa_crypto_hash.c:mbedtls_buffer_offset
Unexecuted instantiation: psa_crypto_mac.c:mbedtls_buffer_offset
Unexecuted instantiation: psa_crypto_rsa.c:mbedtls_buffer_offset
Unexecuted instantiation: psa_crypto_slot_management.c:mbedtls_buffer_offset
Unexecuted instantiation: psa_crypto_storage.c:mbedtls_buffer_offset
Unexecuted instantiation: psa_its_file.c:mbedtls_buffer_offset
Unexecuted instantiation: ripemd160.c:mbedtls_buffer_offset
Unexecuted instantiation: rsa.c:mbedtls_buffer_offset
Unexecuted instantiation: rsa_alt_helpers.c:mbedtls_buffer_offset
Unexecuted instantiation: sha1.c:mbedtls_buffer_offset
Unexecuted instantiation: sha256.c:mbedtls_buffer_offset
Unexecuted instantiation: sha512.c:mbedtls_buffer_offset
Unexecuted instantiation: timing.c:mbedtls_buffer_offset
Unexecuted instantiation: aes.c:mbedtls_buffer_offset
Unexecuted instantiation: aesni.c:mbedtls_buffer_offset
Unexecuted instantiation: aria.c:mbedtls_buffer_offset
Unexecuted instantiation: asn1write.c:mbedtls_buffer_offset
Unexecuted instantiation: base64.c:mbedtls_buffer_offset
Unexecuted instantiation: camellia.c:mbedtls_buffer_offset
Unexecuted instantiation: ccm.c:mbedtls_buffer_offset
Unexecuted instantiation: chacha20.c:mbedtls_buffer_offset
Unexecuted instantiation: chachapoly.c:mbedtls_buffer_offset
Unexecuted instantiation: cmac.c:mbedtls_buffer_offset
Unexecuted instantiation: ecdsa.c:mbedtls_buffer_offset
Unexecuted instantiation: hmac_drbg.c:mbedtls_buffer_offset
Unexecuted instantiation: pkcs12.c:mbedtls_buffer_offset
Unexecuted instantiation: pkcs5.c:mbedtls_buffer_offset
Unexecuted instantiation: poly1305.c:mbedtls_buffer_offset
Unexecuted instantiation: psa_crypto_aead.c:mbedtls_buffer_offset
Unexecuted instantiation: psa_crypto_cipher.c:mbedtls_buffer_offset
94
95
/** Return an offset into a read-only buffer.
96
 *
97
 * Similar to mbedtls_buffer_offset(), but for const pointers.
98
 *
99
 * \param p     Pointer to a buffer of at least n bytes.
100
 *              This may be \p NULL if \p n is zero.
101
 * \param n     An offset in bytes.
102
 * \return      Pointer to offset \p n in the buffer \p p.
103
 *              Note that this is only a valid pointer if the size of the
104
 *              buffer is at least \p n + 1.
105
 */
106
static inline const unsigned char *mbedtls_buffer_offset_const(
107
    const unsigned char *p, size_t n)
108
0
{
109
0
    return p == NULL ? NULL : p + n;
110
0
}
Unexecuted instantiation: bignum_helpers.c:mbedtls_buffer_offset_const
Unexecuted instantiation: certs.c:mbedtls_buffer_offset_const
Unexecuted instantiation: ssl_msg.c:mbedtls_buffer_offset_const
Unexecuted instantiation: ssl_tls.c:mbedtls_buffer_offset_const
Unexecuted instantiation: ssl_tls12_client.c:mbedtls_buffer_offset_const
Unexecuted instantiation: ssl_tls12_server.c:mbedtls_buffer_offset_const
Unexecuted instantiation: debug.c:mbedtls_buffer_offset_const
Unexecuted instantiation: ssl_ciphersuites.c:mbedtls_buffer_offset_const
Unexecuted instantiation: ssl_client.c:mbedtls_buffer_offset_const
Unexecuted instantiation: ssl_debug_helpers_generated.c:mbedtls_buffer_offset_const
Unexecuted instantiation: x509.c:mbedtls_buffer_offset_const
Unexecuted instantiation: x509_crt.c:mbedtls_buffer_offset_const
Unexecuted instantiation: asn1parse.c:mbedtls_buffer_offset_const
Unexecuted instantiation: bignum.c:mbedtls_buffer_offset_const
Unexecuted instantiation: bignum_core.c:mbedtls_buffer_offset_const
Unexecuted instantiation: bignum_mod.c:mbedtls_buffer_offset_const
Unexecuted instantiation: bignum_mod_raw.c:mbedtls_buffer_offset_const
Unexecuted instantiation: cipher.c:mbedtls_buffer_offset_const
Unexecuted instantiation: cipher_wrap.c:mbedtls_buffer_offset_const
Unexecuted instantiation: constant_time.c:mbedtls_buffer_offset_const
Unexecuted instantiation: ctr_drbg.c:mbedtls_buffer_offset_const
Unexecuted instantiation: des.c:mbedtls_buffer_offset_const
Unexecuted instantiation: dhm.c:mbedtls_buffer_offset_const
Unexecuted instantiation: ecdh.c:mbedtls_buffer_offset_const
Unexecuted instantiation: ecp.c:mbedtls_buffer_offset_const
Unexecuted instantiation: ecp_curves.c:mbedtls_buffer_offset_const
Unexecuted instantiation: entropy.c:mbedtls_buffer_offset_const
Unexecuted instantiation: entropy_poll.c:mbedtls_buffer_offset_const
Unexecuted instantiation: gcm.c:mbedtls_buffer_offset_const
Unexecuted instantiation: hash_info.c:mbedtls_buffer_offset_const
Unexecuted instantiation: md.c:mbedtls_buffer_offset_const
Unexecuted instantiation: md5.c:mbedtls_buffer_offset_const
Unexecuted instantiation: nist_kw.c:mbedtls_buffer_offset_const
Unexecuted instantiation: oid.c:mbedtls_buffer_offset_const
Unexecuted instantiation: pem.c:mbedtls_buffer_offset_const
Unexecuted instantiation: pk.c:mbedtls_buffer_offset_const
Unexecuted instantiation: pk_wrap.c:mbedtls_buffer_offset_const
Unexecuted instantiation: pkparse.c:mbedtls_buffer_offset_const
Unexecuted instantiation: pkwrite.c:mbedtls_buffer_offset_const
Unexecuted instantiation: platform.c:mbedtls_buffer_offset_const
Unexecuted instantiation: platform_util.c:mbedtls_buffer_offset_const
Unexecuted instantiation: psa_crypto.c:mbedtls_buffer_offset_const
Unexecuted instantiation: psa_crypto_client.c:mbedtls_buffer_offset_const
Unexecuted instantiation: psa_crypto_driver_wrappers.c:mbedtls_buffer_offset_const
Unexecuted instantiation: psa_crypto_ecp.c:mbedtls_buffer_offset_const
Unexecuted instantiation: psa_crypto_hash.c:mbedtls_buffer_offset_const
Unexecuted instantiation: psa_crypto_mac.c:mbedtls_buffer_offset_const
Unexecuted instantiation: psa_crypto_rsa.c:mbedtls_buffer_offset_const
Unexecuted instantiation: psa_crypto_slot_management.c:mbedtls_buffer_offset_const
Unexecuted instantiation: psa_crypto_storage.c:mbedtls_buffer_offset_const
Unexecuted instantiation: psa_its_file.c:mbedtls_buffer_offset_const
Unexecuted instantiation: ripemd160.c:mbedtls_buffer_offset_const
Unexecuted instantiation: rsa.c:mbedtls_buffer_offset_const
Unexecuted instantiation: rsa_alt_helpers.c:mbedtls_buffer_offset_const
Unexecuted instantiation: sha1.c:mbedtls_buffer_offset_const
Unexecuted instantiation: sha256.c:mbedtls_buffer_offset_const
Unexecuted instantiation: sha512.c:mbedtls_buffer_offset_const
Unexecuted instantiation: timing.c:mbedtls_buffer_offset_const
Unexecuted instantiation: aes.c:mbedtls_buffer_offset_const
Unexecuted instantiation: aesni.c:mbedtls_buffer_offset_const
Unexecuted instantiation: aria.c:mbedtls_buffer_offset_const
Unexecuted instantiation: asn1write.c:mbedtls_buffer_offset_const
Unexecuted instantiation: base64.c:mbedtls_buffer_offset_const
Unexecuted instantiation: camellia.c:mbedtls_buffer_offset_const
Unexecuted instantiation: ccm.c:mbedtls_buffer_offset_const
Unexecuted instantiation: chacha20.c:mbedtls_buffer_offset_const
Unexecuted instantiation: chachapoly.c:mbedtls_buffer_offset_const
Unexecuted instantiation: cmac.c:mbedtls_buffer_offset_const
Unexecuted instantiation: ecdsa.c:mbedtls_buffer_offset_const
Unexecuted instantiation: hmac_drbg.c:mbedtls_buffer_offset_const
Unexecuted instantiation: pkcs12.c:mbedtls_buffer_offset_const
Unexecuted instantiation: pkcs5.c:mbedtls_buffer_offset_const
Unexecuted instantiation: poly1305.c:mbedtls_buffer_offset_const
Unexecuted instantiation: psa_crypto_aead.c:mbedtls_buffer_offset_const
Unexecuted instantiation: psa_crypto_cipher.c:mbedtls_buffer_offset_const
111
112
/**
113
 * Perform a fast block XOR operation, such that
114
 * r[i] = a[i] ^ b[i] where 0 <= i < n
115
 *
116
 * \param   r Pointer to result (buffer of at least \p n bytes). \p r
117
 *            may be equal to either \p a or \p b, but behaviour when
118
 *            it overlaps in other ways is undefined.
119
 * \param   a Pointer to input (buffer of at least \p n bytes)
120
 * \param   b Pointer to input (buffer of at least \p n bytes)
121
 * \param   n Number of bytes to process.
122
 */
123
inline void mbedtls_xor(unsigned char *r, const unsigned char *a, const unsigned char *b, size_t n)
124
618k
{
125
618k
    size_t i = 0;
126
618k
#if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS)
127
3.11M
    for (; (i + 4) <= n; i += 4) {
128
2.49M
        uint32_t x = mbedtls_get_unaligned_uint32(a + i) ^ mbedtls_get_unaligned_uint32(b + i);
129
2.49M
        mbedtls_put_unaligned_uint32(r + i, x);
130
2.49M
    }
131
618k
#endif
132
619k
    for (; i < n; i++) {
133
1.23k
        r[i] = a[i] ^ b[i];
134
1.23k
    }
135
618k
}
136
137
/* Fix MSVC C99 compatible issue
138
 *      MSVC support __func__ from visual studio 2015( 1900 )
139
 *      Use MSVC predefine macro to avoid name check fail.
140
 */
141
#if (defined(_MSC_VER) && (_MSC_VER <= 1900))
142
#define /*no-check-names*/ __func__ __FUNCTION__
143
#endif
144
145
/* Define `asm` for compilers which don't define it. */
146
/* *INDENT-OFF* */
147
#ifndef asm
148
17.7M
#define asm __asm__
149
#endif
150
/* *INDENT-ON* */
151
152
#endif /* MBEDTLS_LIBRARY_COMMON_H */