Skip to content

Commit f79a7ad

Browse files
committed
Add valgrind uninit check to cmovs output
1 parent a39c2b0 commit f79a7ad

7 files changed

+26
-11
lines changed

src/field_10x26_impl.h

+2
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,7 @@ static void secp256k1_fe_sqr(secp256k1_fe *r, const secp256k1_fe *a) {
10971097

10981098
static SECP256K1_INLINE void secp256k1_fe_cmov(secp256k1_fe *r, const secp256k1_fe *a, int flag) {
10991099
uint32_t mask0, mask1;
1100+
VG_CHECK_VERIFY(r->n, sizeof(r->n));
11001101
mask0 = flag + ~((uint32_t)0);
11011102
mask1 = ~mask0;
11021103
r->n[0] = (r->n[0] & mask0) | (a->n[0] & mask1);
@@ -1119,6 +1120,7 @@ static SECP256K1_INLINE void secp256k1_fe_cmov(secp256k1_fe *r, const secp256k1_
11191120

11201121
static SECP256K1_INLINE void secp256k1_fe_storage_cmov(secp256k1_fe_storage *r, const secp256k1_fe_storage *a, int flag) {
11211122
uint32_t mask0, mask1;
1123+
VG_CHECK_VERIFY(r->n, sizeof(r->n));
11221124
mask0 = flag + ~((uint32_t)0);
11231125
mask1 = ~mask0;
11241126
r->n[0] = (r->n[0] & mask0) | (a->n[0] & mask1);

src/field_5x52_impl.h

+2
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ static void secp256k1_fe_sqr(secp256k1_fe *r, const secp256k1_fe *a) {
449449

450450
static SECP256K1_INLINE void secp256k1_fe_cmov(secp256k1_fe *r, const secp256k1_fe *a, int flag) {
451451
uint64_t mask0, mask1;
452+
VG_CHECK_VERIFY(r->n, sizeof(r->n));
452453
mask0 = flag + ~((uint64_t)0);
453454
mask1 = ~mask0;
454455
r->n[0] = (r->n[0] & mask0) | (a->n[0] & mask1);
@@ -466,6 +467,7 @@ static SECP256K1_INLINE void secp256k1_fe_cmov(secp256k1_fe *r, const secp256k1_
466467

467468
static SECP256K1_INLINE void secp256k1_fe_storage_cmov(secp256k1_fe_storage *r, const secp256k1_fe_storage *a, int flag) {
468469
uint64_t mask0, mask1;
470+
VG_CHECK_VERIFY(r->n, sizeof(r->n));
469471
mask0 = flag + ~((uint64_t)0);
470472
mask1 = ~mask0;
471473
r->n[0] = (r->n[0] & mask0) | (a->n[0] & mask1);

src/scalar_4x64_impl.h

+1
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,7 @@ SECP256K1_INLINE static void secp256k1_scalar_mul_shift_var(secp256k1_scalar *r,
948948

949949
static SECP256K1_INLINE void secp256k1_scalar_cmov(secp256k1_scalar *r, const secp256k1_scalar *a, int flag) {
950950
uint64_t mask0, mask1;
951+
VG_CHECK_VERIFY(r->d, sizeof(r->d));
951952
mask0 = flag + ~((uint64_t)0);
952953
mask1 = ~mask0;
953954
r->d[0] = (r->d[0] & mask0) | (a->d[0] & mask1);

src/scalar_8x32_impl.h

+1
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,7 @@ SECP256K1_INLINE static void secp256k1_scalar_mul_shift_var(secp256k1_scalar *r,
720720

721721
static SECP256K1_INLINE void secp256k1_scalar_cmov(secp256k1_scalar *r, const secp256k1_scalar *a, int flag) {
722722
uint32_t mask0, mask1;
723+
VG_CHECK_VERIFY(r->d, sizeof(r->d));
723724
mask0 = flag + ~((uint32_t)0);
724725
mask1 = ~mask0;
725726
r->d[0] = (r->d[0] & mask0) | (a->d[0] & mask1);

src/scalar_low_impl.h

+1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ SECP256K1_INLINE static int secp256k1_scalar_eq(const secp256k1_scalar *a, const
116116

117117
static SECP256K1_INLINE void secp256k1_scalar_cmov(secp256k1_scalar *r, const secp256k1_scalar *a, int flag) {
118118
uint32_t mask0, mask1;
119+
VG_CHECK_VERIFY(r, sizeof(*r));
119120
mask0 = flag + ~((uint32_t)0);
120121
mask1 = ~mask0;
121122
*r = (*r & mask0) | (*a & mask1);

src/tests.c

-11
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,6 @@ void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
3232
#include "contrib/lax_der_parsing.c"
3333
#include "contrib/lax_der_privatekey_parsing.c"
3434

35-
#if !defined(VG_CHECK)
36-
# if defined(VALGRIND)
37-
# include <valgrind/memcheck.h>
38-
# define VG_UNDEF(x,y) VALGRIND_MAKE_MEM_UNDEFINED((x),(y))
39-
# define VG_CHECK(x,y) VALGRIND_CHECK_MEM_IS_DEFINED((x),(y))
40-
# else
41-
# define VG_UNDEF(x,y)
42-
# define VG_CHECK(x,y)
43-
# endif
44-
#endif
45-
4635
static int count = 64;
4736
static secp256k1_context *ctx = NULL;
4837

src/util.h

+19
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,25 @@ static SECP256K1_INLINE void secp256k1_callback_call(const secp256k1_callback *
6969
#define VERIFY_SETUP(stmt)
7070
#endif
7171

72+
/* Define `VG_UNDEF` and `VG_CHECK` when VALGRIND is defined */
73+
#if !defined(VG_CHECK)
74+
# if defined(VALGRIND)
75+
# include <valgrind/memcheck.h>
76+
# define VG_UNDEF(x,y) VALGRIND_MAKE_MEM_UNDEFINED((x),(y))
77+
# define VG_CHECK(x,y) VALGRIND_CHECK_MEM_IS_DEFINED((x),(y))
78+
# else
79+
# define VG_UNDEF(x,y)
80+
# define VG_CHECK(x,y)
81+
# endif
82+
#endif
83+
84+
/* Like `VG_CHECK` but on VERIFY only */
85+
#if defined(VERIFY)
86+
#define VG_CHECK_VERIFY(x,y) VG_CHECK((x), (y))
87+
#else
88+
#define VG_CHECK_VERIFY(x,y)
89+
#endif
90+
7291
static SECP256K1_INLINE void *checked_malloc(const secp256k1_callback* cb, size_t size) {
7392
void *ret = malloc(size);
7493
if (ret == NULL) {

0 commit comments

Comments
 (0)