Skip to content

Commit c3b33dd

Browse files
switch to self-made memcmp
1 parent 87543fa commit c3b33dd

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

examples/examples_util.h

+18
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,21 @@ static void print_buf_plain(const unsigned char *buf, size_t len) {
126126
}
127127
printf("\n}\n");
128128
}
129+
130+
/** Semantics like memcmp. Variable-time.
131+
*
132+
* We use this to avoid possible compiler bugs with memcmp, e.g.
133+
* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95189
134+
*/
135+
static /*SECP256K1_INLINE*/ int secp256k1_memcmp_var(const void *s1, const void *s2, size_t n) {
136+
const unsigned char *p1 = s1, *p2 = s2;
137+
size_t i;
138+
139+
for (i = 0; i < n; i++) {
140+
int diff = p1[i] - p2[i];
141+
if (diff != 0) {
142+
return diff;
143+
}
144+
}
145+
return 0;
146+
}

examples/silentpayments.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ const unsigned char* label_lookup(
107107
printf("label = 33\n");
108108
PRINT_BUF(label33, 33);
109109
printf("\n\n");
110-
if (memcmp(cache->entries[i].label, label33, 33) == 0) {
110+
if (secp256k1_memcmp_var(cache->entries[i].label, label33, 33) == 0) {
111111
return cache->entries[i].label_tweak;
112112
}
113113
}

src/modules/silentpayments/main_impl.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ int secp256k1_silentpayments_recipient_scan_outputs(
571571
* label1 = tx_output - P_output */
572572
secp256k1_gej_add_ge_var(&label_gej, &tx_output_gej, &P_output_negated_ge, NULL);
573573
secp256k1_ge_set_gej(&label_ge, &label_gej);
574-
if (!secp256k1_eckey_pubkey_serialize(&label_ge, label33, &len, 1)) { exit(1) }
574+
if (!secp256k1_eckey_pubkey_serialize(&label_ge, label33, &len, 1)) { exit(1); }
575575
label_tweak = label_lookup(label33, label_context);
576576
if (label_tweak != NULL) {
577577
found = 1;
@@ -584,7 +584,7 @@ int secp256k1_silentpayments_recipient_scan_outputs(
584584
* label2 = -tx_output - P_output */
585585
secp256k1_gej_add_ge_var(&label_gej, &label_gej, &P_output_negated_ge, NULL);
586586
secp256k1_ge_set_gej(&label_ge, &label_gej);
587-
if (!secp256k1_eckey_pubkey_serialize(&label_ge, label33, &len, 1)) { exit(1) }
587+
if (!secp256k1_eckey_pubkey_serialize(&label_ge, label33, &len, 1)) { exit(1); }
588588
label_tweak = label_lookup(label33, label_context);
589589
if (label_tweak != NULL) {
590590
found = 1;

0 commit comments

Comments
 (0)