XenevaOS
Loading...
Searching...
No Matches
Functions | Variables
constant_time.c File Reference
#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>
Include dependency graph for constant_time.c:

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
 

Function Documentation

◆ mbedtls_ct_memcmp()

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.

Parameters
aPointer to the first buffer, containing at least n bytes. May not be NULL.
bPointer to the second buffer, containing at least n bytes. May not be NULL.
nThe number of bytes to compare.
Returns
Zero if the contents of the two buffers are the same, otherwise non-zero.

◆ 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
conditionThe condition
destSecret. Destination pointer.
src1Secret. Pointer to copy from (if condition == MBEDTLS_CT_TRUE). This may be equal to dest, but may not overlap in other ways.
src2Secret (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.
lenNumber 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
destSecret. The destination buffer. This must point to a writable buffer of at least len bytes.
srcSecret. 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
offsetSecret. 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_minThe minimal value of offset.
offset_maxThe maximal value of offset.
lenThe number of bytes to copy.

Variable Documentation

◆ mbedtls_ct_zero

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