Skip to content

Commit af74960

Browse files
Section on constant-flow testing
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
1 parent 4c244c0 commit af74960

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

kb/development/test_suites.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,19 @@ In a test case that uses PSA crypto only when building with `MBEDTLS_USE_PSA_CRY
256256
257257
See [`<test/psa_crypto_helpers.h>`](https://github.com/Mbed-TLS/mbedtls-framework/blob/development/tests/include/test/psa_crypto_helpers.h) for more complex cases.
258258
259+
### Constant-flow testing
260+
261+
We run some tests with [MemorySanitizer (MSan)](https://github.com/google/sanitizers/wiki/memorysanitizer) and [Valgrind](https://valgrind.org/docs/manual/mc-manual.html) configured to detect secret-dependent control flow: branches or memory addresses computed from secret data. These tests detect library code that could leak secret data through timing side channels to local attackers via shared hardware components such as a memory cache or a branch predictor. We refer to such tests as “constant-time” or more accurately “constant-flow” testing.
262+
263+
Constant-flow testing was added relatively recently in the history of the project, and many functions that should be constant-flow are not tested. However, constant-flow testing is preferred when writing new code that claims to be constant-flow, and especially when fixing a timing side channel.
264+
265+
In unit tests, use the following macros, from [`<test/constant_flow.h>`](https://github.com/Mbed-TLS/mbedtls-framework/blob/main/tests/include/test/constant_flow.h):
266+
267+
* `TEST_CF_SECRET(buffer, size)`: marks the given buffer as secret. Call this on keys, plaintext and other confidential data before passing it to library functions.
268+
* `TEST_CF_PUBLIC(buffer, size)`: marks the given buffer as public. Call this on outputs before testing their content.
269+
270+
Note that you need to call `TEST_CF_PUBLIC` before `TEST_MEMORY_COMPARE`. However, it is not needed with scalar comparison assertions (`TEST_EQUAL`, etc.), which make a public copy of its argument before comparing them.
271+
259272
## Guidance on writing unit test data
260273
261274
### Document the test data

kb/testing/testing-constant-flow.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Tools for testing constant-flow code
22

3+
*This document is an investigation into test tooling. For usage in Mbed TLS and TF-PSA-Crypto unit tests, see “[Mbed TLS test guidelines — Constant-flow testing](../development/test_suites.md#constant-flow-testing)”.*
4+
35
Code that manipulates secret values (private keys, etc.) needs to be constant-flow (often called constant-time, though the requirements are actually stricter than "the total running time is a constant"), that is contain no branches that depend on secret values, and no memory accesses at addresses depending on a secret value, in order to avoid leaking the secret value through side channels.
46

57
Ideally, this should not only be enforced by code review, but also tested or checked by tools. This pages list some available options.

0 commit comments

Comments
 (0)