Skip to content

Commit 8e048b0

Browse files
sp example: Switch to self-made memcmp
1 parent 8518fcd commit 8e048b0

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

examples/examples_util.h

+18
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,21 @@ static void secure_erase(void *ptr, size_t len) {
106106
volatile_memset(ptr, 0, len);
107107
#endif
108108
}
109+
110+
/** Semantics like memcmp. Variable-time.
111+
*
112+
* We use this to avoid possible compiler bugs with memcmp, e.g.
113+
* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95189
114+
*/
115+
static /*SECP256K1_INLINE*/ int secp256k1_memcmp_var(const void *s1, const void *s2, size_t n) {
116+
const unsigned char *p1 = s1, *p2 = s2;
117+
size_t i;
118+
119+
for (i = 0; i < n; i++) {
120+
int diff = p1[i] - p2[i];
121+
if (diff != 0) {
122+
return diff;
123+
}
124+
}
125+
return 0;
126+
}

examples/silentpayments.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ const unsigned char* label_lookup(
101101
const struct labels_cache* cache = (const struct labels_cache*)cache_ptr;
102102
size_t i;
103103
for (i = 0; i < cache->entries_used; i++) {
104-
if (memcmp(cache->entries[i].label, label33, 33) == 0) {
104+
if (secp256k1_memcmp_var(cache->entries[i].label, label33, 33) == 0) {
105105
return cache->entries[i].label_tweak;
106106
}
107107
}

0 commit comments

Comments
 (0)