![]() |
![]() |
A collection of utility functions for cryptographic purposes.
============================================================================
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
Go to the source code of this file.
Functions | |
bool | CryptoUtils_buffersMatch (const volatile void *volatile buffer0, const volatile void *volatile buffer1, size_t bufferByteLength) |
Compares two buffers for equality without branching. More... | |
bool | CryptoUtils_buffersMatchWordAligned (const volatile uint32_t *volatile buffer0, const volatile uint32_t *volatile buffer1, size_t bufferByteLength) |
Compares two buffers for equality word-by-word without branching. More... | |
bool | CryptoUtils_isBufferAllZeros (const void *buffer, size_t bufferByteLength) |
Check whether the provided buffer only contains 0x00 bytes. More... | |
void | CryptoUtils_memset (void *dest, size_t destSize, uint8_t val, size_t count) |
Copies val into the first count bytes of the buffer pointed to by dest . More... | |
void | CryptoUtils_reverseBufferBytewise (void *buffer, size_t bufferByteLength) |
Reverses the byte order in a buffer of a given length. More... | |
void | CryptoUtils_copyPad (const void *source, uint32_t *destination, size_t sourceLength) |
Copies and pads an array of words. More... | |
void | CryptoUtils_reverseCopyPad (const void *source, uint32_t *destination, size_t sourceLength) |
Reverses, copies, and pads an array of words. More... | |
void | CryptoUtils_reverseCopy (const void *source, void *destination, size_t sourceLength) |
Reverses and copies an array of bytes. More... | |
bool CryptoUtils_buffersMatch | ( | const volatile void *volatile | buffer0, |
const volatile void *volatile | buffer1, | ||
size_t | bufferByteLength | ||
) |
Compares two buffers for equality without branching.
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
.
buffer0 | Buffer to compare against buffer1 . |
buffer1 | Buffer tp compare against buffer0 |
bufferByteLength | Length in bytes of buffer0 and buffer1 . |
true | The contents of the buffers match. |
false | The contents of the buffers do not match. |
bool CryptoUtils_buffersMatchWordAligned | ( | const volatile uint32_t *volatile | buffer0, |
const volatile uint32_t *volatile | buffer1, | ||
size_t | bufferByteLength | ||
) |
Compares two buffers for equality word-by-word without branching.
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.
buffer0 | Buffer to compare against buffer1 . |
buffer1 | Buffer tp compare against buffer0 |
bufferByteLength | Length 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). |
true | The contents of the buffers match. |
false | The contents of the buffers do not match. |
bool CryptoUtils_isBufferAllZeros | ( | const void * | buffer, |
size_t | bufferByteLength | ||
) |
Check whether the provided buffer only contains 0x00 bytes.
buffer | Buffer to search for non-zero bytes |
bufferByteLength | Length of buffer in bytes |
true | The buffer contained only bytes with value 0x00 |
false | The buffer contained at least on non-zero byte |
void CryptoUtils_memset | ( | void * | dest, |
size_t | destSize, | ||
uint8_t | val, | ||
size_t | count | ||
) |
Copies val
into the first count
bytes of the buffer pointed to by dest
.
dest | Pointer to destination buffer |
destSize | Size of destination buffer in bytes |
val | Fill byte value |
count | Number of bytes to fill |
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.
buffer | Buffer containing the data to be reversed. |
bufferByteLength | Length in bytes of buffer . |
void CryptoUtils_copyPad | ( | const void * | source, |
uint32_t * | destination, | ||
size_t | sourceLength | ||
) |
Copies and pads an array of words.
The source
array is 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.
source | Source array |
destination | Destination array |
sourceLength | Length of the source array |
void CryptoUtils_reverseCopyPad | ( | const void * | source, |
uint32_t * | 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.
source | Source array |
destination | Destination array |
sourceLength | Length of the source array |
void CryptoUtils_reverseCopy | ( | const void * | source, |
void * | destination, | ||
size_t | sourceLength | ||
) |
Reverses and copies an array of bytes.
The source
array is reversed byte-wise and copied into the destination
array.
source | Source array |
destination | Destination array |
sourceLength | Length of the source array |