瀏覽代碼

Use mpz_pown_sec where available

Nils Maier 11 年之前
父節點
當前提交
aa02545fba
共有 2 個文件被更改,包括 14 次插入0 次删除
  1. 4 0
      configure.ac
  2. 10 0
      src/LibgmpDHKeyExchange.cc

+ 4 - 0
configure.ac

@@ -427,6 +427,10 @@ if test "x$have_openssl" != "xyes"; then
     AC_SEARCH_LIBS([__gmpz_init], [gmp], [have_libgmp=yes], [have_libgmp=no])
     if test "x$have_libgmp" = "xyes"; then
       AC_DEFINE([HAVE_LIBGMP], [1], [Define to 1 if you have libgmp.])
+      AC_CHECK_FUNCS([__gmpz_powm_sec], [have_mpz_powm_sec=yes])
+      if test "x$have_mpz_powm_sec" = "xyes"; then
+        AC_DEFINE([HAVE_GMP_SEC], [1], [Define to 1 if you have a GMP with sec functions.])
+      fi
     else
       AC_MSG_WARN([libgmp not found])
       if test "x$with_libgmp_requested" = "xyes"; then

+ 10 - 0
src/LibgmpDHKeyExchange.cc

@@ -86,7 +86,11 @@ void DHKeyExchange::init
 
 void DHKeyExchange::generatePublicKey()
 {
+#if HAVE_GMP_SEC
+  mpz_powm_sec(publicKey_, generator_, privateKey_, prime_);
+#else // HAVE_GMP_SEC
   mpz_powm(publicKey_, generator_, privateKey_, prime_);
+#endif // HAVE_GMP_SEC
 }
 
 size_t DHKeyExchange::getPublicKey(unsigned char* out, size_t outLength) const
@@ -126,7 +130,13 @@ size_t DHKeyExchange::computeSecret
   mpz_import(peerPublicKey, peerPublicKeyLength, 1, 1, 1, 0, peerPublicKeyData);
   mpz_t secret;
   mpz_init(secret);
+
+#if HAVE_GMP_SEC
+  mpz_powm_sec(secret, peerPublicKey, privateKey_, prime_);
+#else // HAVE_GMP_SEC
   mpz_powm(secret, peerPublicKey, privateKey_, prime_);
+#endif // HAVE_GMP_SEC
+
   mpz_clear(peerPublicKey);
 
   memset(out, 0, outLength);