-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCW_Utils.cpp
More file actions
61 lines (56 loc) · 1.77 KB
/
Copy pathCW_Utils.cpp
File metadata and controls
61 lines (56 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* SPDX-License-Identifier: LGPL-3.0-or-later
* Copyright (c) 2026 Cryptnox SA
*/
/**
* @file CW_Utils.cpp
* @brief Implementation of the platform-independent utility helpers.
*
* @ref CW_Utils::secure_compare iterates over the full length to avoid
* leaking byte-position information through timing. @ref CW_Utils::secure_wipe
* uses a volatile pointer so the writes cannot be optimised away.
* @ref CW_Utils::safe_memcpy validates pointers, length, and source/destination
* overlap before delegating to memcpy.
*/
#include "CW_Utils.h"
/**
* @brief Constant-time buffer comparison, resistant to timing side-channel attacks.
*/
bool CW_Utils::secure_compare(const uint8_t* a, const uint8_t* b, size_t len) {
bool ret = false;
if ((a != NULL) && (b != NULL) && (len > 0U)) {
uint8_t diff = 0U;
for (size_t i = 0U; i < len; i++) {
diff |= a[i] ^ b[i];
}
ret = (diff == 0U);
}
return ret;
}
/**
* @brief Securely zero a buffer, guaranteed not to be optimised away.
*/
void CW_Utils::secure_wipe(uint8_t* buf, size_t len) {
if ((buf != NULL) && (len > 0U)) {
volatile uint8_t* p = buf;
for (size_t i = 0U; i < len; i++) {
p[i] = 0U;
}
}
}
/**
* @brief Safe memcpy — checks src, dst and size before copying.
* @return true if copy succeeded, false otherwise.
*/
bool CW_Utils::safe_memcpy(uint8_t* dst, size_t dstSize,
const uint8_t* src, size_t count) {
bool ret = false;
if ((dst != NULL) && (src != NULL) && (count > 0U) && (count <= dstSize)) {
bool overlap = (dst < (src + count)) && (src < (dst + dstSize));
if (!overlap) {
memcpy(dst, src, count);
ret = true;
}
}
return ret;
}