2022-09-19 02:51:11 -04:00
|
|
|
/*
|
2024-09-06 06:46:18 -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"
|
2023-02-27 11:00:23 -05:00
|
|
|
#include "assert.h"
|
2024-09-18 05:11:13 -04:00
|
|
|
|
2024-09-12 04:26:25 -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++)
|
2023-03-21 09:11:08 -04:00
|
|
|
*s = (uint8_t)c;
|
2022-09-19 02:51:11 -04:00
|
|
|
|
2024-03-21 07:32:08 -04:00
|
|
|
/*@ -temptrans @*/
|
2022-09-19 02:51:11 -04:00
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
2023-02-27 11:00:23 -05:00
|
|
|
void memcpy_s(void *dest, size_t destsize, const void *src, size_t n)
|
2022-09-19 02:51:11 -04:00
|
|
|
{
|
2023-02-27 11:00:23 -05: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;
|
|
|
|
|
2023-03-21 09:11:08 -04:00
|
|
|
for (size_t i = 0; i < n; i++) {
|
2024-03-21 07:32:08 -04:00
|
|
|
/*@ -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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-27 11:00:23 -05:00
|
|
|
void wordcpy_s(void *dest, size_t destsize, const void *src, size_t n)
|
2022-09-19 02:51:11 -04:00
|
|
|
{
|
2023-02-27 11:00:23 -05: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;
|
|
|
|
|
2023-03-21 09:11:08 -04:00
|
|
|
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
|
|
|
|
2023-03-21 09:11:08 -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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-12 02:55:35 -04:00
|
|
|
return res;
|
2022-09-19 02:51:11 -04:00
|
|
|
}
|
2024-02-02 08:20:21 -05:00
|
|
|
|
|
|
|
void secure_wipe(void *v, size_t n)
|
|
|
|
{
|
|
|
|
volatile uint8_t *p = (volatile uint8_t *)v;
|
|
|
|
while (n--)
|
|
|
|
*p++ = 0;
|
|
|
|
}
|