Getters and setters for ECDSA_SIG
authorAnkit Shah <ankshah@fb.com>
Thu, 13 Jul 2017 17:06:49 +0000 (10:06 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Thu, 13 Jul 2017 17:08:30 +0000 (10:08 -0700)
Summary:
Added getters and setters for ECDSA_SIG to allow for compatibility
between OpenSSL 1.1.0 and 1.0.2

Reviewed By: knekritz

Differential Revision: D5408934

fbshipit-source-id: 7a3d9df774728c81270cc4da34c75202018e430d

folly/portability/OpenSSL.cpp
folly/portability/OpenSSL.h
folly/portability/test/OpenSSLPortabilityTest.cpp

index 492b830625ef3563f2f87bd535d7ab8246528f2e..f0dc9d726ceb1a21fd9e75d9510c9a3520063ad8 100644 (file)
@@ -333,6 +333,30 @@ void RSA_get0_crt_params(
   }
 }
 
+int ECDSA_SIG_set0(ECDSA_SIG* sig, BIGNUM* r, BIGNUM* s) {
+  // Based off of https://wiki.openssl.org/index.php/OpenSSL_1.1.0_Changes
+  if (r == nullptr || s == nullptr) {
+    return 0;
+  }
+  BN_clear_free(sig->r);
+  BN_clear_free(sig->s);
+  sig->r = r;
+  sig->s = s;
+  return 1;
+}
+
+void ECDSA_SIG_get0(
+    const ECDSA_SIG* sig,
+    const BIGNUM** pr,
+    const BIGNUM** ps) {
+  // Based off of https://wiki.openssl.org/index.php/OpenSSL_1.1.0_Changes
+  if (pr != nullptr) {
+    *pr = sig->r;
+  }
+  if (ps != nullptr) {
+    *ps = sig->s;
+  }
+}
 #endif
 }
 }
index 5d6da7785f7ec45ac985404e2dd863be5e6fb598..113a917531567d701abb78a86e7a86e867ac121a 100644 (file)
@@ -164,6 +164,8 @@ void RSA_get0_crt_params(
     const BIGNUM** dmp1,
     const BIGNUM** dmq1,
     const BIGNUM** iqmp);
+int ECDSA_SIG_set0(ECDSA_SIG* sig, BIGNUM* r, BIGNUM* s);
+void ECDSA_SIG_get0(const ECDSA_SIG* sig, const BIGNUM** pr, const BIGNUM** ps);
 #endif
 
 #if FOLLY_OPENSSL_IS_110
index 7c1bbf87c71fdd1cf2497449b12ddfdfdc541d45..a0cdc60752e3c4ddc53133c37bda8636980dcee9 100644 (file)
@@ -59,3 +59,19 @@ TEST(OpenSSLPortabilityTest, TestRSASetter) {
   EXPECT_FALSE(BN_cmp(n_public, n_public_actual));
   EXPECT_FALSE(BN_cmp(e_public, e_public_actual));
 }
+
+TEST(OpenSSLPortabilityTest, TestEcdsaSigPortability) {
+  EcdsaSigUniquePtr ecdsa(ECDSA_SIG_new());
+  BIGNUM* r = BN_new();
+  BIGNUM* s = BN_new();
+  BIGNUM* r_actual;
+  BIGNUM* s_actual;
+  EXPECT_TRUE(BN_set_bit(r, 1));
+  EXPECT_TRUE(BN_set_bit(s, 2));
+  EXPECT_TRUE(ECDSA_SIG_set0(ecdsa.get(), r, s));
+  ECDSA_SIG_get0(
+      ecdsa.get(), (const BIGNUM**)&r_actual, (const BIGNUM**)&s_actual);
+  // BN_cmp returns 0 if the two BIGNUMs are equal
+  EXPECT_FALSE(BN_cmp(r, r_actual));
+  EXPECT_FALSE(BN_cmp(s, s_actual));
+}