Functions
CryptoUtils.h File Reference

Detailed Description

A collection of utility functions for cryptographic purposes.

============================================================================

#include <stdint.h>
#include <stdbool.h>
#include <string.h>
Include dependency graph for CryptoUtils.h:

Go to the source code of this file.

Functions

bool CryptoUtils_buffersMatch (const void *buffer0, const void *buffer1, size_t bufferByteLength)
 Compares two buffers for equality without branching. More...
 
bool CryptoUtils_buffersMatchWordAligned (const uint32_t *buffer0, const uint32_t *buffer1, size_t bufferByteLength)
 Compares two buffers for equality word-by-word without branching. More...
 
void CryptoUtils_reverseBufferBytewise (void *buffer, size_t bufferByteLength)
 Reverses the byte order in a buffer of a given length. More...
 
void CryptoUtils_reverseCopyPad (const void *source, void *destination, size_t sourceLength)
 Reverses, copies, and pads an array of words. More...
 

Function Documentation

§ CryptoUtils_buffersMatch()

bool CryptoUtils_buffersMatch ( const void *  buffer0,
const void *  buffer1,
size_t  bufferByteLength 
)

Compares two buffers for equality without branching.

Note
This is not a drop-in replacement for memcmp!

Most memcmp implementations break out of their comparison loop immediately once a mismatch is detected to save execution time. For cryptographic purposes, this is a flaw.

This function compares two buffers without branching thus requiring a an amount of time that does not vary with the content of buffer0 and buffer1.

Parameters
buffer0Buffer to compare against buffer1.
buffer1Buffer tp compare against buffer0
bufferByteLengthLength in bytes of buffer0 and buffer1.
Return values
trueThe contents of the buffers match.
falseThe contents of the buffers do not match.

§ CryptoUtils_buffersMatchWordAligned()

bool CryptoUtils_buffersMatchWordAligned ( const uint32_t *  buffer0,
const uint32_t *  buffer1,
size_t  bufferByteLength 
)

Compares two buffers for equality word-by-word without branching.

Note
This is not a drop-in replacement for memcmp!

Most memcmp implementations break out of their comparison loop immediately once a mismatch is detected to save execution time. For cryptographic purposes, this is a flaw.

This function compares two buffers without branching thus requiring a an amount of time that does not vary with the content of buffer0 and buffer1.

Unlike CryptoUtils_buffersMatch(), this function expects buffer0 and buffer1 to be 32-bit aligned. It will only perform 32-bit aligned accesses to memory. This is needed to access the registers of certain peripherals.

Parameters
buffer0Buffer to compare against buffer1.
buffer1Buffer tp compare against buffer0
bufferByteLengthLength in bytes of buffer0 and buffer1. Must be evenly divisible by sizeof(uint32_t). This function will return false if bufferByteLength is not evenly divisible by sizeof(uin32_t).
Return values
trueThe contents of the buffers match.
falseThe contents of the buffers do not match.

§ CryptoUtils_reverseBufferBytewise()

void CryptoUtils_reverseBufferBytewise ( void *  buffer,
size_t  bufferByteLength 
)

Reverses the byte order in a buffer of a given length.

The left-most byte will become the right-most byte and vice versa.

Parameters
bufferBuffer containing the data to be reversed.
bufferByteLengthLength in bytes of buffer.

§ CryptoUtils_reverseCopyPad()

void CryptoUtils_reverseCopyPad ( const void *  source,
void *  destination,
size_t  sourceLength 
)

Reverses, copies, and pads an array of words.

The source array is reversed byte-wise and copied into the destination array. Writes are done word-wise. If sourceLength is not a multiple of 4, any remaining bytes up to the next word boundary are padded with 0.

The length of the destination array must be a multiple of 4, rounded up to the padded sourceLength if required.

Parameters
sourceSource array
destinationDestination array
sourceLengthLength of the source array (multiple of 4)
© Copyright 1995-2020, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale