cryptnox-sdk-cpp 1.0.0
Platform-independent C++ core SDK for Cryptnox Hardware Wallet
Loading...
Searching...
No Matches
CW_Utils.cpp
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: LGPL-3.0-or-later
3 * Copyright (c) 2026 Cryptnox SA
4 */
5
16
17#include "CW_Utils.h"
18
22bool CW_Utils::secure_compare(const uint8_t* a, const uint8_t* b, size_t len) {
23 bool ret = false;
24 if ((a != NULL) && (b != NULL) && (len > 0U)) {
25 uint8_t diff = 0U;
26 for (size_t i = 0U; i < len; i++) {
27 diff |= a[i] ^ b[i];
28 }
29 ret = (diff == 0U);
30 }
31 return ret;
32}
33
37void CW_Utils::secure_wipe(uint8_t* buf, size_t len) {
38 if ((buf != NULL) && (len > 0U)) {
39 volatile uint8_t* p = buf;
40 for (size_t i = 0U; i < len; i++) {
41 p[i] = 0U;
42 }
43 }
44}
45
50bool CW_Utils::safe_memcpy(uint8_t* dst, size_t dstSize,
51 const uint8_t* src, size_t count) {
52 bool ret = false;
53 if ((dst != NULL) && (src != NULL) && (count > 0U) && (count <= dstSize)) {
54 bool overlap = (dst < (src + count)) && (src < (dst + dstSize));
55 if (!overlap) {
56 memcpy(dst, src, count);
57 ret = true;
58 }
59 }
60 return ret;
61}
Platform-independent security and memory utilities.
static bool safe_memcpy(uint8_t *dst, size_t dstSize, const uint8_t *src, size_t count)
Safe memcpy — validates pointers, sizes, and checks for overlap.
Definition CW_Utils.cpp:50
static bool secure_compare(const uint8_t *a, const uint8_t *b, size_t len)
Constant-time buffer comparison, resistant to timing side-channel attacks.
Definition CW_Utils.cpp:22
static void secure_wipe(uint8_t *buf, size_t len)
Securely zero a buffer, guaranteed not to be optimised away.
Definition CW_Utils.cpp:37