#include <stdint.h>
#include <stddef.h>
#include "common.h"
#include "constant_time_impl.h"
Go to the source code of this file.
◆ MBEDTLS_CT_FALSE
◆ MBEDTLS_CT_TRUE
◆ mbedtls_ct_condition_t
Constant-time functions
Copyright The Mbed TLS Contributors SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
◆ mbedtls_ct_int_t
◆ mbedtls_ct_uint_t
◆ mbedtls_ct_memcpy_if()
| void mbedtls_ct_memcpy_if |
( |
mbedtls_ct_condition_t |
condition, |
|
|
unsigned char * |
dest, |
|
|
const unsigned char * |
src1, |
|
|
const unsigned char * |
src2, |
|
|
size_t |
len |
|
) |
| |
Conditional memcpy.
Functionally equivalent to:
if (condition) { memcpy(dest, src1, len); } else { if (src2 != NULL) memcpy(dest, src2, len); }
It will always read len bytes from src1. If src2 != NULL, it will always read len bytes from src2. If src2 == NULL, it will instead read len bytes from dest (as if src2 == dest).
- Parameters
-
| condition | The condition |
| dest | Secret. Destination pointer. |
| src1 | Secret. Pointer to copy from (if condition == MBEDTLS_CT_TRUE). This may be equal to dest, but may not overlap in other ways. |
| src2 | Secret (contents only - may branch to determine if this parameter is NULL). Pointer to copy from (if condition == MBEDTLS_CT_FALSE and src2 is not NULL). May be NULL. This may be equal to dest, but may not overlap it in other ways. It may overlap with src1. |
| len | Number of bytes to copy. |
◆ mbedtls_ct_memcpy_offset()
| void mbedtls_ct_memcpy_offset |
( |
unsigned char * |
dest, |
|
|
const unsigned char * |
src, |
|
|
size_t |
offset, |
|
|
size_t |
offset_min, |
|
|
size_t |
offset_max, |
|
|
size_t |
len |
|
) |
| |
Copy data from a secret position.
Functionally equivalent to:
memcpy(dst, src + offset, len)
This function copies len bytes from src + offset to dst, with a code flow and memory access pattern that does not depend on offset, but only on offset_min, offset_max and len.
- Note
- This function reads from
dest, but the value that is read does not influence the result and this function's behavior is well-defined regardless of the contents of the buffers. This may result in false positives from static or dynamic analyzers, especially if dest is not initialized.
- Parameters
-
| dest | Secret. The destination buffer. This must point to a writable buffer of at least len bytes. |
| src | Secret. The base of the source buffer. This must point to a readable buffer of at least offset_max + len bytes. Shouldn't overlap with dest |
| offset | Secret. The offset in the source buffer from which to copy. This must be no less than offset_min and no greater than offset_max. |
| offset_min | The minimal value of offset. |
| offset_max | The maximal value of offset. |
| len | The number of bytes to copy. |