blob: ebd0c6a19d94108ed44ab7875017b86b7af60b5f [file] [log] [blame]
Manuel Pégourié-Gonnard6240def2020-07-10 09:35:54 +02001/**
2 * \file constant_flow.h
3 *
4 * \brief This file contains tools to ensure tested code has constant flow.
5 */
6
7/*
Dan Handley50118142020-08-20 11:20:12 +01008 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard6240def2020-07-10 09:35:54 +02009 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
Manuel Pégourié-Gonnard6240def2020-07-10 09:35:54 +020022 */
23
24#ifndef TEST_CONSTANT_FLOW_H
25#define TEST_CONSTANT_FLOW_H
26
27#if !defined(MBEDTLS_CONFIG_FILE)
28#include "mbedtls/config.h"
29#else
30#include MBEDTLS_CONFIG_FILE
31#endif
32
Manuel Pégourié-Gonnard73afa372020-08-19 10:27:38 +020033/*
34 * This file defines the two macros
35 *
36 * #define TEST_CF_SECRET(ptr, size)
37 * #define TEST_CF_PUBLIC(ptr, size)
38 *
39 * that can be used in tests to mark a memory area as secret (no branch or
40 * memory access should depend on it) or public (default, only needs to be
41 * marked explicitly when it was derived from secret data).
42 *
43 * Arguments:
44 * - ptr: a pointer to the memory area to be marked
45 * - size: the size in bytes of the memory area
46 *
47 * Implementation:
48 * The basic idea is that of ctgrind <https://github.com/agl/ctgrind>: we can
49 * re-use tools that were designed for checking use of uninitialized memory.
50 * This file contains two implementations: one based on MemorySanitizer, the
51 * other on valgrind's memcheck. If none of them is enabled, dummy macros that
52 * do nothing are defined for convenience.
Gilles Peskine9603a442022-11-29 16:01:41 +010053 *
54 * \note #TEST_CF_SECRET must be called directly from within a .function file,
55 * not indirectly via a macro defined under tests/include or a function
56 * under tests/src. This is because we only run Valgrind for constant
57 * flow on test suites that have greppable annotations inside them (see
58 * `skip_suites_without_constant_flow` in `tests/scripts/all.sh`).
Manuel Pégourié-Gonnard73afa372020-08-19 10:27:38 +020059 */
60
Manuel Pégourié-Gonnard6240def2020-07-10 09:35:54 +020061#if defined(MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN)
62#include <sanitizer/msan_interface.h>
63
64/* Use macros to avoid messing up with origin tracking */
65#define TEST_CF_SECRET __msan_allocated_memory
66// void __msan_allocated_memory(const volatile void* data, size_t size);
67#define TEST_CF_PUBLIC __msan_unpoison
68// void __msan_unpoison(const volatile void *a, size_t size);
69
Manuel Pégourié-Gonnard73afa372020-08-19 10:27:38 +020070#elif defined(MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND)
71#include <valgrind/memcheck.h>
72
73#define TEST_CF_SECRET VALGRIND_MAKE_MEM_UNDEFINED
74// VALGRIND_MAKE_MEM_UNDEFINED(_qzz_addr, _qzz_len)
75#define TEST_CF_PUBLIC VALGRIND_MAKE_MEM_DEFINED
76// VALGRIND_MAKE_MEM_DEFINED(_qzz_addr, _qzz_len)
77
78#else /* MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN ||
79 MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND */
Manuel Pégourié-Gonnard6240def2020-07-10 09:35:54 +020080
81#define TEST_CF_SECRET(ptr, size)
82#define TEST_CF_PUBLIC(ptr, size)
83
Manuel Pégourié-Gonnard73afa372020-08-19 10:27:38 +020084#endif /* MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN ||
85 MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND */
Manuel Pégourié-Gonnard6240def2020-07-10 09:35:54 +020086
87#endif /* TEST_CONSTANT_FLOW_H */