crypto/curve25519: Add value barriers to curve25519 fe_cmov and fe_cswap

Unlike Fiat-Crypto's generated select, the hand-written fe_cmov and
fe_cswap helpers in crypto/curve25519/curve25519.cc constructed their
bitmasks without value_barrier_w. Newer versions of Clang 21 at -O2
recognize the repeated masked selects in x25519_ge_scalarmult_small_precomp
and turn the table scan into a secret-dependent branch and indexed loads.

x25519_ge_scalarmult_small_precomp is used on the password-derived
scalar in SPAKE2 and, in OPENSSL_SMALL builds, for Ed25519 and X25519
base-point multiplication. fe_cmov and fe_cswap are also used in SPAKE2
point multiplication, Elligator 2 for CPace, and the generic C X25519 and
Ed25519 paths when assembly implementations are not used.

Add value_barrier_w to fe_cswap and both fe_cmov overloads, and
annotate SPAKE2, CPace, and Elligator 2 for Valgrind-based
constant-time validation.

Benchmark has shown zero or negligible performance impact across the Ed25519 and Curve25519 benchmark suites, with a more observable 0.95% slow-down on SPAKE2.

Thanks to Basavesh Ammanaghatta Shivakumar and Swarn Priya (Washington
State University) for reporting this.

Fixed: 586640372
Signed-off-by: Xiangfei Ding <xfding@google.com>
Change-Id: I765c3231d0aa53f0c965b8f08736ab096a6a6964
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/106088
Reviewed-by: David Benjamin <davidben@google.com>
Presubmit-BoringSSL-Verified: boringssl-scoped@luci-project-accounts.iam.gserviceaccount.com <boringssl-scoped@luci-project-accounts.iam.gserviceaccount.com>
diff --git a/crypto/curve25519/cpace.cc b/crypto/curve25519/cpace.cc
index 99529a7..7da3e2c 100644
--- a/crypto/curve25519/cpace.cc
+++ b/crypto/curve25519/cpace.cc
@@ -56,6 +56,7 @@
 void CPACE_CTX_free(CPACE_CTX *ctx) { Delete(bssl::FromOpaque(ctx)); }
 
 namespace {
+
 constexpr uint64_t leb128_len(uint64_t len) {
   if (len < 128) {
     return 1;
@@ -85,6 +86,7 @@
   SHA512_Update(&hasher, lv_tag, lv_tag_len);
   SHA512_Update(&hasher, data.data(), data.size());
 }
+
 }  // namespace
 
 
@@ -132,7 +134,9 @@
   map_to_curve_curve25519_elligator2(g, v, gen_str_hash);
   // sample_scalar() = sample_random_bytes(G.field_size_bytes)
   RAND_bytes(our_private_key, 32);
+  CONSTTIME_SECRET(our_private_key, sizeof(our_private_key));
   auto rc = X25519(our_public_key, our_private_key, g);
+  CONSTTIME_DECLASSIFY(our_public_key, sizeof(our_public_key));
   OPENSSL_memcpy(msg, our_public_key, 32);
   if (!rc) {
     // This will happen with negligible probability.
diff --git a/crypto/curve25519/cpace_test.cc b/crypto/curve25519/cpace_test.cc
index f488600..a93ecdd 100644
--- a/crypto/curve25519/cpace_test.cc
+++ b/crypto/curve25519/cpace_test.cc
@@ -44,12 +44,14 @@
 
 struct CPACERun {
   bool Run() {
+    auto alice_pw = SecretCopy(alice_password);
+    auto bob_pw = SecretCopy(bob_password);
     UniquePtr<CPACE_CTX> alice(CPACE_CTX_new(
-        cpace_role_initiator, alice_password.data(), alice_password.size(),
+        cpace_role_initiator, alice_pw.data(), alice_pw.size(),
         alice_channel_id.data(), alice_channel_id.size(), alice_aad.data(),
         alice_aad.size(), alice_sid.data(), alice_sid.size()));
     UniquePtr<CPACE_CTX> bob(CPACE_CTX_new(
-        cpace_role_responder, bob_password.data(), bob_password.size(),
+        cpace_role_responder, bob_pw.data(), bob_pw.size(),
         bob_channel_id.data(), bob_channel_id.size(), bob_aad.data(),
         bob_aad.size(), bob_sid.data(), bob_sid.size()));
 
@@ -87,6 +89,8 @@
       return false;
     }
 
+    CONSTTIME_DECLASSIFY(alice_key, sizeof(alice_key));
+    CONSTTIME_DECLASSIFY(bob_key, sizeof(bob_key));
     key_matches_ = (CRYPTO_memcmp(alice_key, bob_key, sizeof(alice_key)) == 0);
     sid_matches_ = !pass_sid_out || (CRYPTO_memcmp(alice_sid_out, bob_sid_out,
                                                    sizeof(alice_sid_out)) == 0);
@@ -278,12 +282,14 @@
 
   if (!ya_priv.empty()) {
     OPENSSL_memcpy(alice_impl->our_private_key, ya_priv.data(), 32);
+    CONSTTIME_SECRET(alice_impl->our_private_key, 32);
   }
   OPENSSL_memcpy(alice_impl->our_public_key, ya_msg.data(), 32);
   alice_impl->state = CpaceCtx::cpace_state_msg_generated;
 
   if (!yb_priv.empty()) {
     OPENSSL_memcpy(bob_impl->our_private_key, yb_priv.data(), 32);
+    CONSTTIME_SECRET(bob_impl->our_private_key, 32);
   }
   OPENSSL_memcpy(bob_impl->our_public_key, yb_msg.data(), 32);
   bob_impl->state = CpaceCtx::cpace_state_msg_generated;
@@ -295,6 +301,8 @@
                                 alice_isk, alice_sid_out));
   ASSERT_TRUE(CPACE_process_msg(bob.get(), ya_msg.data(), kAda, sizeof(kAda),
                                 bob_isk, bob_sid_out));
+  CONSTTIME_DECLASSIFY(alice_isk, sizeof(alice_isk));
+  CONSTTIME_DECLASSIFY(bob_isk, sizeof(bob_isk));
 
   if (!expected_isk.empty()) {
     EXPECT_EQ(Bytes(alice_isk), Bytes(expected_isk));
diff --git a/crypto/curve25519/curve25519.cc b/crypto/curve25519/curve25519.cc
index 2ddfca6..76216ee 100644
--- a/crypto/curve25519/curve25519.cc
+++ b/crypto/curve25519/curve25519.cc
@@ -263,7 +263,7 @@
 //
 // Preconditions: b in {0,1}.
 void fe_cswap(fe *f, fe *g, fe_limb_t b) {
-  b = 0 - b;
+  b = value_barrier_w(0 - b);
   for (unsigned i = 0; i < FE_NUM_LIMBS; i++) {
     fe_limb_t x = f->v[i] ^ g->v[i];
     x &= b;
@@ -296,7 +296,7 @@
   // TODO(davidben): Switch to fiat's calling convention, or ask fiat to emit a
   // different one.
 
-  b = 0 - b;
+  b = value_barrier_w(0 - b);
   for (unsigned i = 0; i < FE_NUM_LIMBS; i++) {
     fe_limb_t x = f->v[i] ^ g->v[i];
     x &= b;
@@ -309,7 +309,7 @@
 //
 // Preconditions: b in {0,1}.
 void fe_cmov(fe *f, const fe *g, fe_limb_t b) {
-  b = 0 - b;
+  b = value_barrier_w(0 - b);
   for (unsigned i = 0; i < FE_NUM_LIMBS; i++) {
     fe_limb_t x = f->v[i] ^ g->v[i];
     x &= b;
diff --git a/crypto/curve25519/curve25519_test.cc b/crypto/curve25519/curve25519_test.cc
index 9fc0036..9ef3482 100644
--- a/crypto/curve25519/curve25519_test.cc
+++ b/crypto/curve25519/curve25519_test.cc
@@ -149,9 +149,10 @@
     std::reverse(expected_qy.begin(), expected_qy.end());
 
     uint8_t qx[32], qy[32];
+    CONSTTIME_SECRET(u_bytes.data(), u_bytes.size());
     map_to_curve_curve25519_elligator2(qx, qy, u_bytes.data());
-    EXPECT_EQ(Bytes(qx), Bytes(expected_qx));
-    EXPECT_EQ(Bytes(qy), Bytes(expected_qy));
+    EXPECT_EQ(Bytes(Declassified(qx)), Bytes(expected_qx));
+    EXPECT_EQ(Bytes(Declassified(qy)), Bytes(expected_qy));
   }
 }
 
@@ -166,9 +167,10 @@
       0x29, 0xfa, 0x29, 0xbe, 0xbd, 0x51, 0x09, 0x25, 0x12, 0xa7, 0x82,
       0x9f, 0xdd, 0xe7, 0xd3, 0x14, 0xb6, 0x2f, 0x05, 0xa7, 0x3f};
 
+  auto u_secret = SecretCopy(u_bytes);
   uint8_t g[32], v[32];
-  map_to_curve_curve25519_elligator2(g, v, u_bytes);
-  EXPECT_EQ(Bytes(g), Bytes(expected_g));
+  map_to_curve_curve25519_elligator2(g, v, u_secret.data());
+  EXPECT_EQ(Bytes(Declassified(g)), Bytes(expected_g));
 }
 
 #endif
diff --git a/crypto/curve25519/spake25519.cc b/crypto/curve25519/spake25519.cc
index fc56561..e043e15 100644
--- a/crypto/curve25519/spake25519.cc
+++ b/crypto/curve25519/spake25519.cc
@@ -369,6 +369,7 @@
 
   uint8_t private_tmp[64];
   RAND_bytes(private_tmp, sizeof(private_tmp));
+  CONSTTIME_SECRET(private_tmp, sizeof(private_tmp));
   x25519_sc_reduce(private_tmp);
   // Multiply by the cofactor (eight) so that we'll clear it when operating on
   // the peer's point later in the protocol.
@@ -427,7 +428,7 @@
                 constant_time_eq_w(password_scalar.words[0] & 4, 4));
     scalar_add(&password_scalar, &tmp);
 
-    assert((password_scalar.words[0] & 7) == 0);
+    declassify_assert((password_scalar.words[0] & 7) == 0);
   }
 
   OPENSSL_memcpy(ctx->password_scalar, password_scalar.words,
@@ -449,6 +450,7 @@
   ge_p2 Pstar_proj;
   x25519_ge_p1p1_to_p2(&Pstar_proj, &Pstar);
   x25519_ge_tobytes(ctx->my_msg, &Pstar_proj);
+  CONSTTIME_DECLASSIFY(ctx->my_msg, sizeof(ctx->my_msg));
 
   OPENSSL_memcpy(out, ctx->my_msg, sizeof(ctx->my_msg));
   *out_len = sizeof(ctx->my_msg);
diff --git a/crypto/curve25519/spake25519_test.cc b/crypto/curve25519/spake25519_test.cc
index 02cc870..6706bd2 100644
--- a/crypto/curve25519/spake25519_test.cc
+++ b/crypto/curve25519/spake25519_test.cc
@@ -61,6 +61,9 @@
     uint8_t bob_msg[SPAKE2_MAX_MSG_SIZE];
     size_t alice_msg_len, bob_msg_len;
 
+    CONSTTIME_SECRET(alice_password.data(), alice_password.size());
+    CONSTTIME_SECRET(bob_password.data(), bob_password.size());
+
     if (!SPAKE2_generate_msg(
             alice.get(), alice_msg, &alice_msg_len, sizeof(alice_msg),
             reinterpret_cast<const uint8_t *>(alice_password.data()),
@@ -87,6 +90,8 @@
       return false;
     }
 
+    CONSTTIME_DECLASSIFY(alice_key, alice_key_len);
+    CONSTTIME_DECLASSIFY(bob_key, bob_key_len);
     key_matches_ = (alice_key_len == bob_key_len &&
                     OPENSSL_memcmp(alice_key, bob_key, alice_key_len) == 0);