tillitis-key/hw/application_fpga/fw/tk1/lib.c

78 lines
1.4 KiB
C
Raw Normal View History

2022-09-19 02:51:11 -04:00
/*
* Copyright (C) 2022-2024 - Tillitis AB
2022-09-19 02:51:11 -04:00
* SPDX-License-Identifier: GPL-2.0-only
*/
#include "lib.h"
#include "assert.h"
2024-09-18 05:11:13 -04:00
#include <stddef.h>
#include <stdint.h>
2024-09-18 05:11:13 -04:00
#include <stdbool.h>
2022-09-19 02:51:11 -04:00
void *memset(void *dest, int c, unsigned n)
{
uint8_t *s = dest;
for (; n; n--, s++)
*s = (uint8_t)c;
2022-09-19 02:51:11 -04:00
/*@ -temptrans @*/
2022-09-19 02:51:11 -04:00
return dest;
}
void memcpy_s(void *dest, size_t destsize, const void *src, size_t n)
2022-09-19 02:51:11 -04:00
{
assert(dest != NULL);
assert(src != NULL);
assert(destsize >= n);
2022-09-19 02:51:11 -04:00
uint8_t *src_byte = (uint8_t *)src;
uint8_t *dest_byte = (uint8_t *)dest;
for (size_t i = 0; i < n; i++) {
/*@ -nullderef @*/
/* splint complains that dest_byte and src_byte can be
* NULL, but it seems it doesn't understand assert.
* See above.
*/
2022-09-19 02:51:11 -04:00
dest_byte[i] = src_byte[i];
}
}
void wordcpy_s(void *dest, size_t destsize, const void *src, size_t n)
2022-09-19 02:51:11 -04:00
{
assert(dest != NULL);
assert(src != NULL);
assert(destsize >= n);
2022-09-19 02:51:11 -04:00
uint32_t *src_word = (uint32_t *)src;
uint32_t *dest_word = (uint32_t *)dest;
for (size_t i = 0; i < n; i++) {
2022-09-19 02:51:11 -04:00
dest_word[i] = src_word[i];
}
}
2024-09-18 05:11:13 -04:00
bool memeq(void *dest, const void *src, size_t n)
2022-09-19 02:51:11 -04:00
{
uint8_t *src_byte = (uint8_t *)src;
uint8_t *dest_byte = (uint8_t *)dest;
2024-09-18 05:11:13 -04:00
bool res = true;
2022-09-19 02:51:11 -04:00
for (size_t i = 0; i < n; i++) {
2022-09-19 02:51:11 -04:00
if (dest_byte[i] != src_byte[i]) {
2024-09-18 05:11:13 -04:00
res = false;
2022-09-19 02:51:11 -04:00
}
}
return res;
2022-09-19 02:51:11 -04:00
}
void secure_wipe(void *v, size_t n)
{
volatile uint8_t *p = (volatile uint8_t *)v;
while (n--)
*p++ = 0;
}