|
XenevaOS
|
#include <stdint.h>#include <limits.h>#include "common.h"#include "constant_time_internal.h"#include "mbedtls/constant_time.h"#include "mbedtls/error.h"#include "mbedtls/platform_util.h"#include <string.h>
Functions | |
| int | mbedtls_ct_memcmp (const void *a, const void *b, size_t n) |
| void | mbedtls_ct_memcpy_if (mbedtls_ct_condition_t condition, unsigned char *dest, const unsigned char *src1, const unsigned char *src2, size_t len) |
| 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) |
Variables | |
| volatile mbedtls_ct_uint_t | mbedtls_ct_zero = 0 |
| int mbedtls_ct_memcmp | ( | const void * | a, |
| const void * | b, | ||
| size_t | n | ||
| ) |
Constant-time functions Constant-time buffer comparison without branches.
This is equivalent to the standard memcmp function, but is likely to be compiled to code using bitwise operations rather than a branch, such that the time taken is constant w.r.t. the data pointed to by a and b, and w.r.t. whether a and b are equal or not. It is not constant-time w.r.t. n .
This function can be used to write constant-time code by replacing branches with bit operations using masks.
| a | Pointer to the first buffer, containing at least n bytes. May not be NULL. |
| b | Pointer to the second buffer, containing at least n bytes. May not be NULL. |
| n | The number of bytes to compare. |
| 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).
| 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. |
| 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.
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.| 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. |
| volatile mbedtls_ct_uint_t mbedtls_ct_zero = 0 |
Constant-time functions
Copyright The Mbed TLS Contributors SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later