9bc9710
diff --git a/make/autoconf/jdk-options.m4 b/make/autoconf/jdk-options.m4
9bc9710
--- a/make/autoconf/jdk-options.m4
9bc9710
+++ b/make/autoconf/jdk-options.m4
9bc9710
@@ -267,9 +267,10 @@
9bc9710
 #
9bc9710
 AC_DEFUN_ONCE([JDKOPT_DETECT_INTREE_EC],
9bc9710
 [
9bc9710
+  AC_REQUIRE([LIB_SETUP_MISC_LIBS])
9bc9710
   AC_MSG_CHECKING([if elliptic curve crypto implementation is present])
9bc9710
 
9bc9710
-  if test -d "${TOPDIR}/src/jdk.crypto.ec/share/native/libsunec/impl"; then
9bc9710
+  if test "x${system_nss}" = "xyes" -o -d "${TOPDIR}/src/jdk.crypto.ec/share/native/libsunec/impl"; then
9bc9710
     ENABLE_INTREE_EC=true
9bc9710
     AC_MSG_RESULT([yes])
9bc9710
   else
9bc9710
diff --git a/make/autoconf/libraries.m4 b/make/autoconf/libraries.m4
9bc9710
--- a/make/autoconf/libraries.m4
9bc9710
+++ b/make/autoconf/libraries.m4
9bc9710
@@ -178,6 +178,48 @@
9bc9710
   AC_SUBST(LIBDL)
9bc9710
   LIBS="$save_LIBS"
9bc9710
 
9bc9710
+  ###############################################################################
9bc9710
+  #
9bc9710
+  # Check for the NSS libraries
9bc9710
+  #
9bc9710
+
9bc9710
+  AC_MSG_CHECKING([whether to build the Sun EC provider against the system NSS libraries])
9bc9710
+
9bc9710
+  # default is bundled
9bc9710
+  DEFAULT_SYSTEM_NSS=no
9bc9710
+
9bc9710
+  AC_ARG_ENABLE([system-nss], [AS_HELP_STRING([--enable-system-nss],
9bc9710
+     [build the SunEC provider using the system NSS libraries @<:@disabled@:>@])],
9bc9710
+  [
9bc9710
+    case "${enableval}" in
9bc9710
+      yes)
9bc9710
+        system_nss=yes
9bc9710
+        ;;
9bc9710
+      *)
9bc9710
+        system_nss=no
9bc9710
+        ;;
9bc9710
+    esac
9bc9710
+  ],
9bc9710
+  [
9bc9710
+    system_nss=${DEFAULT_SYSTEM_NSS}
9bc9710
+  ])
9bc9710
+  AC_MSG_RESULT([$system_nss])
9bc9710
+
9bc9710
+  if test "x${system_nss}" = "xyes"; then
9bc9710
+      PKG_CHECK_MODULES(NSS_SOFTTKN, nss-softokn >= 3.16.1, [NSS_SOFTOKN_FOUND=yes], [NSS_SOFTOKN_FOUND=no])
9bc9710
+      PKG_CHECK_MODULES(NSS, nss >= 3.16.1, [NSS_FOUND=yes], [NSS_FOUND=no])
9bc9710
+      if test "x${NSS_SOFTOKN_FOUND}" = "xyes" -a "x${NSS_FOUND}" = "xyes"; then
9bc9710
+          NSS_LIBS="$NSS_SOFTOKN_LIBS $NSS_LIBS -lfreebl";
9bc9710
+	  USE_EXTERNAL_NSS=true
9bc9710
+      else
9bc9710
+	  AC_MSG_ERROR([--enable-system-nss specified, but NSS not found.])
9bc9710
+      fi
9bc9710
+  else
9bc9710
+      USE_EXTERNAL_NSS=false
9bc9710
+  fi
9bc9710
+  AC_SUBST(USE_EXTERNAL_NSS)
9bc9710
+
9bc9710
+
9bc9710
   # Deprecated libraries, keep the flags for backwards compatibility
9bc9710
   if test "x$OPENJDK_TARGET_OS" = "xwindows"; then
9bc9710
     BASIC_DEPRECATED_ARG_WITH([dxsdk])
9bc9710
diff --git a/make/autoconf/spec.gmk.in b/make/autoconf/spec.gmk.in
9bc9710
--- a/make/autoconf/spec.gmk.in
9bc9710
+++ b/make/autoconf/spec.gmk.in
9bc9710
@@ -795,6 +795,10 @@
9bc9710
 # Libraries
9bc9710
 #
9bc9710
 
9bc9710
+USE_EXTERNAL_NSS:=@USE_EXTERNAL_NSS@
9bc9710
+NSS_LIBS:=@NSS_LIBS@
9bc9710
+NSS_CFLAGS:=@NSS_CFLAGS@
9bc9710
+
9bc9710
 USE_EXTERNAL_LCMS:=@USE_EXTERNAL_LCMS@
9bc9710
 LCMS_CFLAGS:=@LCMS_CFLAGS@
9bc9710
 LCMS_LIBS:=@LCMS_LIBS@
9bc9710
diff --git a/make/lib/Lib-jdk.crypto.ec.gmk b/make/lib/Lib-jdk.crypto.ec.gmk
9bc9710
--- a/make/lib/Lib-jdk.crypto.ec.gmk
9bc9710
+++ b/make/lib/Lib-jdk.crypto.ec.gmk
9bc9710
@@ -38,6 +38,11 @@
9bc9710
     BUILD_LIBSUNEC_CXXFLAGS_JDKLIB := $(CXXFLAGS_JDKLIB)
9bc9710
   endif
9bc9710
 
9bc9710
+  ifeq ($(USE_EXTERNAL_NSS), true)
9bc9710
+    BUILD_LIBSUNEC_CFLAGS_JDKLIB += $(NSS_CFLAGS) -DSYSTEM_NSS -DNSS_ENABLE_ECC
9bc9710
+    BUILD_LIBSUNEC_CXXFLAGS_JDKLIB += $(NSS_CFLAGS) -DSYSTEM_NSS -DNSS_ENABLE_ECC
9bc9710
+  endif
9bc9710
+
9bc9710
   $(eval $(call SetupJdkLibrary, BUILD_LIBSUNEC, \
9bc9710
       NAME := sunec, \
9bc9710
       TOOLCHAIN := TOOLCHAIN_LINK_CXX, \
Severin Gehwolf 7c23b90
@@ -47,9 +52,11 @@
Severin Gehwolf 7c23b90
       CXXFLAGS := $(BUILD_LIBSUNEC_CXXFLAGS_JDKLIB), \
Severin Gehwolf 7c23b90
       DISABLED_WARNINGS_gcc := sign-compare implicit-fallthrough, \
Severin Gehwolf 7c23b90
       DISABLED_WARNINGS_microsoft := 4101 4244 4146 4018, \
Severin Gehwolf 7c23b90
-      LDFLAGS := $(LDFLAGS_JDKLIB) $(LDFLAGS_CXX_JDK), \
Severin Gehwolf 7c23b90
+      LDFLAGS := $(subst -Xlinker --as-needed,, \
Severin Gehwolf 7c23b90
+                 $(subst -Wl$(COMMA)--as-needed,, $(LDFLAGS_JDKLIB))) $(LDFLAGS_CXX_JDK), \
9bc9710
       LDFLAGS_macosx := $(call SET_SHARED_LIBRARY_ORIGIN), \
9bc9710
       LIBS := $(LIBCXX), \
9bc9710
+      LIBS_linux := -lc $(NSS_LIBS), \
9bc9710
   ))
9bc9710
 
9bc9710
   TARGETS += $(BUILD_LIBSUNEC)
9bc9710
diff --git a/src/java.base/unix/native/include/jni_md.h b/src/java.base/unix/native/include/jni_md.h
9bc9710
--- a/src/java.base/unix/native/include/jni_md.h
9bc9710
+++ b/src/java.base/unix/native/include/jni_md.h
9bc9710
@@ -41,6 +41,11 @@
9bc9710
   #define JNIEXPORT
9bc9710
   #define JNIIMPORT
9bc9710
 #endif
9bc9710
+#if (defined(__GNUC__)) || __has_attribute(unused)
9bc9710
+  #define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
9bc9710
+#else
9bc9710
+  #define UNUSED(x) UNUSED_ ## x
9bc9710
+#endif
9bc9710
 
9bc9710
 #define JNICALL
9bc9710
 
9bc9710
diff --git a/src/jdk.crypto.ec/share/classes/sun/security/ec/SunEC.java b/src/jdk.crypto.ec/share/classes/sun/security/ec/SunEC.java
9bc9710
--- a/src/jdk.crypto.ec/share/classes/sun/security/ec/SunEC.java
9bc9710
+++ b/src/jdk.crypto.ec/share/classes/sun/security/ec/SunEC.java
9bc9710
@@ -61,6 +61,7 @@
9bc9710
             AccessController.doPrivileged(new PrivilegedAction<Void>() {
9bc9710
                 public Void run() {
9bc9710
                     System.loadLibrary("sunec"); // check for native library
9bc9710
+                    initialize();
9bc9710
                     return null;
9bc9710
                 }
9bc9710
             });
9bc9710
@@ -293,6 +294,11 @@
9bc9710
             "ECDH", "sun.security.ec.ECDHKeyAgreement", null, ATTRS));
9bc9710
     }
9bc9710
 
9bc9710
+    /**
9bc9710
+     * Initialize the native code.
9bc9710
+     */
9bc9710
+    private static native void initialize();
9bc9710
+
9bc9710
     private void putXDHEntries() {
9bc9710
 
9bc9710
         HashMap<String, String> ATTRS = new HashMap<>(1);
9bc9710
diff --git a/src/jdk.crypto.ec/share/native/libsunec/ECC_JNI.cpp b/src/jdk.crypto.ec/share/native/libsunec/ECC_JNI.cpp
9bc9710
--- a/src/jdk.crypto.ec/share/native/libsunec/ECC_JNI.cpp
9bc9710
+++ b/src/jdk.crypto.ec/share/native/libsunec/ECC_JNI.cpp
9bc9710
@@ -25,7 +25,11 @@
9bc9710
 
9bc9710
 #include <jni.h>
9bc9710
 #include "jni_util.h"
9bc9710
+#ifdef SYSTEM_NSS
9bc9710
+#include "ecc_impl.h"
9bc9710
+#else
9bc9710
 #include "impl/ecc_impl.h"
9bc9710
+#endif
9bc9710
 #include "sun_security_ec_ECDHKeyAgreement.h"
9bc9710
 #include "sun_security_ec_ECKeyPairGenerator.h"
9bc9710
 #include "sun_security_ec_ECDSASignature.h"
9bc9710
@@ -33,6 +37,13 @@
9bc9710
 #define INVALID_PARAMETER_EXCEPTION \
9bc9710
         "java/security/InvalidParameterException"
9bc9710
 #define KEY_EXCEPTION   "java/security/KeyException"
9bc9710
+#define INTERNAL_ERROR "java/lang/InternalError"
9bc9710
+
9bc9710
+#ifdef SYSTEM_NSS
9bc9710
+#define SYSTEM_UNUSED(x) UNUSED(x)
9bc9710
+#else
9bc9710
+#define SYSTEM_UNUSED(x) x
9bc9710
+#endif
9bc9710
 
9bc9710
 extern "C" {
9bc9710
 
9bc9710
@@ -55,8 +66,13 @@
9bc9710
 /*
9bc9710
  * Deep free of the ECParams struct
9bc9710
  */
9bc9710
-void FreeECParams(ECParams *ecparams, jboolean freeStruct)
9bc9710
+void FreeECParams(ECParams *ecparams, jboolean SYSTEM_UNUSED(freeStruct))
9bc9710
 {
9bc9710
+#ifdef SYSTEM_NSS
9bc9710
+    // Needs to be freed using the matching method to the one
9bc9710
+    // that allocated it. PR_TRUE means the memory is zeroed.
9bc9710
+    PORT_FreeArena(ecparams->arena, PR_TRUE);
9bc9710
+#else
9bc9710
     // Use B_FALSE to free the SECItem->data element, but not the SECItem itself
9bc9710
     // Use B_TRUE to free both
9bc9710
 
9bc9710
@@ -70,6 +86,7 @@
9bc9710
     SECITEM_FreeItem(&ecparams->curveOID, B_FALSE);
9bc9710
     if (freeStruct)
9bc9710
         free(ecparams);
9bc9710
+#endif
9bc9710
 }
9bc9710
 
9bc9710
 jbyteArray getEncodedBytes(JNIEnv *env, SECItem *hSECItem)
9bc9710
@@ -139,7 +156,7 @@
9bc9710
  */
9bc9710
 JNIEXPORT jobjectArray
9bc9710
 JNICALL Java_sun_security_ec_ECKeyPairGenerator_generateECKeyPair
9bc9710
-  (JNIEnv *env, jclass clazz, jint keySize, jbyteArray encodedParams, jbyteArray seed)
9bc9710
+  (JNIEnv *env, jclass UNUSED(clazz), jint UNUSED(keySize), jbyteArray encodedParams, jbyteArray seed)
9bc9710
 {
9bc9710
     ECPrivateKey *privKey = NULL; // contains both public and private values
9bc9710
     ECParams *ecparams = NULL;
9bc9710
@@ -171,8 +188,17 @@
9bc9710
     env->GetByteArrayRegion(seed, 0, jSeedLength, pSeedBuffer);
9bc9710
 
9bc9710
     // Generate the new keypair (using the supplied seed)
9bc9710
+#ifdef SYSTEM_NSS
9bc9710
+    if (RNG_RandomUpdate((unsigned char *) pSeedBuffer, jSeedLength)
9bc9710
+        != SECSuccess) {
9bc9710
+        ThrowException(env, KEY_EXCEPTION);
9bc9710
+        goto cleanup;
9bc9710
+    }
9bc9710
+    if (EC_NewKey(ecparams, &privKey) != SECSuccess) {    
9bc9710
+#else    
9bc9710
     if (EC_NewKey(ecparams, &privKey, (unsigned char *) pSeedBuffer,
9bc9710
         jSeedLength, 0) != SECSuccess) {
9bc9710
+#endif
9bc9710
         ThrowException(env, KEY_EXCEPTION);
9bc9710
         goto cleanup;
9bc9710
     }
9bc9710
@@ -219,10 +245,15 @@
9bc9710
         }
9bc9710
         if (privKey) {
9bc9710
             FreeECParams(&privKey->ecParams, false);
9bc9710
+#ifndef SYSTEM_NSS
9bc9710
+	    // The entire ECPrivateKey is allocated in the arena
9bc9710
+	    // when using system NSS, so only the in-tree version
9bc9710
+	    // needs to clear these manually.
9bc9710
             SECITEM_FreeItem(&privKey->version, B_FALSE);
9bc9710
             SECITEM_FreeItem(&privKey->privateValue, B_FALSE);
9bc9710
             SECITEM_FreeItem(&privKey->publicValue, B_FALSE);
9bc9710
             free(privKey);
9bc9710
+#endif
9bc9710
         }
9bc9710
 
9bc9710
         if (pSeedBuffer) {
9bc9710
@@ -240,7 +271,7 @@
9bc9710
  */
9bc9710
 JNIEXPORT jbyteArray
9bc9710
 JNICALL Java_sun_security_ec_ECDSASignature_signDigest
9bc9710
-  (JNIEnv *env, jclass clazz, jbyteArray digest, jbyteArray privateKey, jbyteArray encodedParams, jbyteArray seed, jint timing)
9bc9710
+  (JNIEnv *env, jclass UNUSED(clazz), jbyteArray digest, jbyteArray privateKey, jbyteArray encodedParams, jbyteArray seed, jint timing)
9bc9710
 {
9bc9710
     jbyte* pDigestBuffer = NULL;
9bc9710
     jint jDigestLength = env->GetArrayLength(digest);
9bc9710
@@ -299,8 +330,18 @@
9bc9710
     env->GetByteArrayRegion(seed, 0, jSeedLength, pSeedBuffer);
9bc9710
 
9bc9710
     // Sign the digest (using the supplied seed)
9bc9710
+#ifdef SYSTEM_NSS
9bc9710
+    if (RNG_RandomUpdate((unsigned char *) pSeedBuffer, jSeedLength)
9bc9710
+        != SECSuccess) {
9bc9710
+        ThrowException(env, KEY_EXCEPTION);
9bc9710
+        goto cleanup;
9bc9710
+    }
9bc9710
+    if (ECDSA_SignDigest(&privKey, &signature_item, &digest_item)
9bc9710
+        != SECSuccess) {    
9bc9710
+#else    
9bc9710
     if (ECDSA_SignDigest(&privKey, &signature_item, &digest_item,
9bc9710
         (unsigned char *) pSeedBuffer, jSeedLength, 0, timing) != SECSuccess) {
9bc9710
+#endif
9bc9710
         ThrowException(env, KEY_EXCEPTION);
9bc9710
         goto cleanup;
9bc9710
     }
9bc9710
@@ -349,7 +390,7 @@
9bc9710
  */
9bc9710
 JNIEXPORT jboolean
9bc9710
 JNICALL Java_sun_security_ec_ECDSASignature_verifySignedDigest
9bc9710
-  (JNIEnv *env, jclass clazz, jbyteArray signedDigest, jbyteArray digest, jbyteArray publicKey, jbyteArray encodedParams)
9bc9710
+  (JNIEnv *env, jclass UNUSED(clazz), jbyteArray signedDigest, jbyteArray digest, jbyteArray publicKey, jbyteArray encodedParams)
9bc9710
 {
9bc9710
     jboolean isValid = false;
9bc9710
 
9bc9710
@@ -406,9 +447,10 @@
9bc9710
 
9bc9710
 cleanup:
9bc9710
     {
9bc9710
-        if (params_item.data)
9bc9710
+        if (params_item.data) {
9bc9710
             env->ReleaseByteArrayElements(encodedParams,
9bc9710
                 (jbyte *) params_item.data, JNI_ABORT);
9bc9710
+	}
9bc9710
 
9bc9710
         if (pubKey.publicValue.data)
9bc9710
             env->ReleaseByteArrayElements(publicKey,
9bc9710
@@ -434,7 +476,7 @@
9bc9710
  */
9bc9710
 JNIEXPORT jbyteArray
9bc9710
 JNICALL Java_sun_security_ec_ECDHKeyAgreement_deriveKey
9bc9710
-  (JNIEnv *env, jclass clazz, jbyteArray privateKey, jbyteArray publicKey, jbyteArray encodedParams)
9bc9710
+  (JNIEnv *env, jclass UNUSED(clazz), jbyteArray privateKey, jbyteArray publicKey, jbyteArray encodedParams)
9bc9710
 {
9bc9710
     jbyteArray jSecret = NULL;
9bc9710
     ECParams *ecparams = NULL;
9bc9710
@@ -510,9 +552,10 @@
9bc9710
             env->ReleaseByteArrayElements(publicKey,
9bc9710
                 (jbyte *) publicValue_item.data, JNI_ABORT);
9bc9710
 
9bc9710
-        if (params_item.data)
9bc9710
+        if (params_item.data) {
9bc9710
             env->ReleaseByteArrayElements(encodedParams,
9bc9710
                 (jbyte *) params_item.data, JNI_ABORT);
9bc9710
+	}
9bc9710
 
9bc9710
         if (ecparams)
9bc9710
             FreeECParams(ecparams, true);
9bc9710
@@ -521,4 +564,28 @@
9bc9710
     return jSecret;
9bc9710
 }
9bc9710
 
9bc9710
+JNIEXPORT void
9bc9710
+JNICALL Java_sun_security_ec_SunEC_initialize
9bc9710
+  (JNIEnv *env, jclass UNUSED(clazz))
9bc9710
+{
9bc9710
+#ifdef SYSTEM_NSS
9bc9710
+    if (SECOID_Init() != SECSuccess) {
9bc9710
+        ThrowException(env, INTERNAL_ERROR);
9bc9710
+    }
9bc9710
+    if (RNG_RNGInit() != SECSuccess) {
9bc9710
+        ThrowException(env, INTERNAL_ERROR);
9bc9710
+    }
9bc9710
+#endif
9bc9710
+}
9bc9710
+
9bc9710
+JNIEXPORT void
9bc9710
+JNICALL JNI_OnUnload
9bc9710
+  (JavaVM *vm, void *reserved)
9bc9710
+{
9bc9710
+#ifdef SYSTEM_NSS
9bc9710
+    RNG_RNGShutdown();
9bc9710
+    SECOID_Shutdown();
9bc9710
+#endif
9bc9710
+}
9bc9710
+
9bc9710
 } /* extern "C" */
9bc9710
diff --git a/src/jdk.crypto.ec/share/native/libsunec/ecc_impl.h b/src/jdk.crypto.ec/share/native/libsunec/ecc_impl.h
9bc9710
new file mode 100644
9bc9710
--- /dev/null
9bc9710
+++ b/src/jdk.crypto.ec/share/native/libsunec/ecc_impl.h
9bc9710
@@ -0,0 +1,298 @@
9bc9710
+/*
9bc9710
+ * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
9bc9710
+ * Use is subject to license terms.
9bc9710
+ *
9bc9710
+ * This library is free software; you can redistribute it and/or
9bc9710
+ * modify it under the terms of the GNU Lesser General Public
9bc9710
+ * License as published by the Free Software Foundation; either
9bc9710
+ * version 2.1 of the License, or (at your option) any later version.
9bc9710
+ *
9bc9710
+ * This library is distributed in the hope that it will be useful,
9bc9710
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9bc9710
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
9bc9710
+ * Lesser General Public License for more details.
9bc9710
+ *
9bc9710
+ * You should have received a copy of the GNU Lesser General Public License
9bc9710
+ * along with this library; if not, write to the Free Software Foundation,
9bc9710
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
9bc9710
+ *
9bc9710
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9bc9710
+ * or visit www.oracle.com if you need additional information or have any
9bc9710
+ * questions.
9bc9710
+ */
9bc9710
+
9bc9710
+/* *********************************************************************
9bc9710
+ *
9bc9710
+ * The Original Code is the Netscape security libraries.
9bc9710
+ *
9bc9710
+ * The Initial Developer of the Original Code is
9bc9710
+ * Netscape Communications Corporation.
9bc9710
+ * Portions created by the Initial Developer are Copyright (C) 1994-2000
9bc9710
+ * the Initial Developer. All Rights Reserved.
9bc9710
+ *
9bc9710
+ * Contributor(s):
9bc9710
+ *   Dr Vipul Gupta <vipul.gupta@sun.com> and
9bc9710
+ *   Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories
9bc9710
+ *
9bc9710
+ * Last Modified Date from the Original Code: May 2017
9bc9710
+ *********************************************************************** */
9bc9710
+
9bc9710
+#ifndef _ECC_IMPL_H
9bc9710
+#define _ECC_IMPL_H
9bc9710
+
9bc9710
+#ifdef __cplusplus
9bc9710
+extern "C" {
9bc9710
+#endif
9bc9710
+
9bc9710
+#include <sys/types.h>
9bc9710
+
9bc9710
+#ifdef SYSTEM_NSS
9bc9710
+#include <secitem.h>
9bc9710
+#include <secerr.h>
9bc9710
+#include <keythi.h>
9bc9710
+#ifdef LEGACY_NSS
9bc9710
+#include <softoken.h>
9bc9710
+#else
9bc9710
+#include <blapi.h>
9bc9710
+#endif
9bc9710
+#else
9bc9710
+#include "ecl-exp.h"
9bc9710
+#endif
9bc9710
+
9bc9710
+/*
9bc9710
+ * Multi-platform definitions
9bc9710
+ */
9bc9710
+#ifdef __linux__
9bc9710
+#define B_FALSE FALSE
9bc9710
+#define B_TRUE TRUE
9bc9710
+typedef unsigned char uint8_t;
9bc9710
+typedef unsigned long ulong_t;
9bc9710
+typedef enum { B_FALSE, B_TRUE } boolean_t;
9bc9710
+#endif /* __linux__ */
9bc9710
+
9bc9710
+#ifdef _ALLBSD_SOURCE
9bc9710
+#include <stdint.h>
9bc9710
+#define B_FALSE FALSE
9bc9710
+#define B_TRUE TRUE
9bc9710
+typedef unsigned long ulong_t;
9bc9710
+typedef enum boolean { B_FALSE, B_TRUE } boolean_t;
9bc9710
+#endif /* _ALLBSD_SOURCE */
9bc9710
+
9bc9710
+#ifdef AIX
9bc9710
+#define B_FALSE FALSE
9bc9710
+#define B_TRUE TRUE
9bc9710
+typedef unsigned char uint8_t;
9bc9710
+typedef unsigned long ulong_t;
9bc9710
+#endif /* AIX */
9bc9710
+
9bc9710
+#ifdef _WIN32
9bc9710
+typedef unsigned char uint8_t;
9bc9710
+typedef unsigned long ulong_t;
9bc9710
+typedef enum boolean { B_FALSE, B_TRUE } boolean_t;
9bc9710
+#define strdup _strdup          /* Replace POSIX name with ISO C++ name */
9bc9710
+#endif /* _WIN32 */
9bc9710
+
9bc9710
+#ifndef _KERNEL
9bc9710
+#include <stdlib.h>
9bc9710
+#endif  /* _KERNEL */
9bc9710
+
9bc9710
+#define EC_MAX_DIGEST_LEN 1024  /* max digest that can be signed */
9bc9710
+#define EC_MAX_POINT_LEN 145    /* max len of DER encoded Q */
9bc9710
+#define EC_MAX_VALUE_LEN 72     /* max len of ANSI X9.62 private value d */
9bc9710
+#define EC_MAX_SIG_LEN 144      /* max signature len for supported curves */
9bc9710
+#define EC_MIN_KEY_LEN  112     /* min key length in bits */
9bc9710
+#define EC_MAX_KEY_LEN  571     /* max key length in bits */
9bc9710
+#define EC_MAX_OID_LEN 10       /* max length of OID buffer */
9bc9710
+
9bc9710
+/*
9bc9710
+ * Various structures and definitions from NSS are here.
9bc9710
+ */
9bc9710
+
9bc9710
+#ifndef SYSTEM_NSS
9bc9710
+#ifdef _KERNEL
9bc9710
+#define PORT_ArenaAlloc(a, n, f)        kmem_alloc((n), (f))
9bc9710
+#define PORT_ArenaZAlloc(a, n, f)       kmem_zalloc((n), (f))
9bc9710
+#define PORT_ArenaGrow(a, b, c, d)      NULL
9bc9710
+#define PORT_ZAlloc(n, f)               kmem_zalloc((n), (f))
9bc9710
+#define PORT_Alloc(n, f)                kmem_alloc((n), (f))
9bc9710
+#else
9bc9710
+#define PORT_ArenaAlloc(a, n, f)        malloc((n))
9bc9710
+#define PORT_ArenaZAlloc(a, n, f)       calloc(1, (n))
9bc9710
+#define PORT_ArenaGrow(a, b, c, d)      NULL
9bc9710
+#define PORT_ZAlloc(n, f)               calloc(1, (n))
9bc9710
+#define PORT_Alloc(n, f)                malloc((n))
9bc9710
+#endif
9bc9710
+
9bc9710
+#define PORT_NewArena(b)                (char *)12345
9bc9710
+#define PORT_ArenaMark(a)               NULL
9bc9710
+#define PORT_ArenaUnmark(a, b)
9bc9710
+#define PORT_ArenaRelease(a, m)
9bc9710
+#define PORT_FreeArena(a, b)
9bc9710
+#define PORT_Strlen(s)                  strlen((s))
9bc9710
+#define PORT_SetError(e)
9bc9710
+
9bc9710
+#define PRBool                          boolean_t
9bc9710
+#define PR_TRUE                         B_TRUE
9bc9710
+#define PR_FALSE                        B_FALSE
9bc9710
+
9bc9710
+#ifdef _KERNEL
9bc9710
+#define PORT_Assert                     ASSERT
9bc9710
+#define PORT_Memcpy(t, f, l)            bcopy((f), (t), (l))
9bc9710
+#else
9bc9710
+#define PORT_Assert                     assert
9bc9710
+#define PORT_Memcpy(t, f, l)            memcpy((t), (f), (l))
9bc9710
+#endif
9bc9710
+
9bc9710
+#endif
9bc9710
+
9bc9710
+#define CHECK_OK(func) if (func == NULL) goto cleanup
9bc9710
+#define CHECK_SEC_OK(func) if (SECSuccess != (rv = func)) goto cleanup
9bc9710
+
9bc9710
+#ifndef SYSTEM_NSS
9bc9710
+typedef enum {
9bc9710
+        siBuffer = 0,
9bc9710
+        siClearDataBuffer = 1,
9bc9710
+        siCipherDataBuffer = 2,
9bc9710
+        siDERCertBuffer = 3,
9bc9710
+        siEncodedCertBuffer = 4,
9bc9710
+        siDERNameBuffer = 5,
9bc9710
+        siEncodedNameBuffer = 6,
9bc9710
+        siAsciiNameString = 7,
9bc9710
+        siAsciiString = 8,
9bc9710
+        siDEROID = 9,
9bc9710
+        siUnsignedInteger = 10,
9bc9710
+        siUTCTime = 11,
9bc9710
+        siGeneralizedTime = 12
9bc9710
+} SECItemType;
9bc9710
+
9bc9710
+typedef struct SECItemStr SECItem;
9bc9710
+
9bc9710
+struct SECItemStr {
9bc9710
+        SECItemType type;
9bc9710
+        unsigned char *data;
9bc9710
+        unsigned int len;
9bc9710
+};
9bc9710
+
9bc9710
+typedef SECItem SECKEYECParams;
9bc9710
+
9bc9710
+typedef enum { ec_params_explicit,
9bc9710
+               ec_params_named
9bc9710
+} ECParamsType;
9bc9710
+
9bc9710
+typedef enum { ec_field_GFp = 1,
9bc9710
+               ec_field_GF2m
9bc9710
+} ECFieldType;
9bc9710
+
9bc9710
+struct ECFieldIDStr {
9bc9710
+    int         size;   /* field size in bits */
9bc9710
+    ECFieldType type;
9bc9710
+    union {
9bc9710
+        SECItem  prime; /* prime p for (GFp) */
9bc9710
+        SECItem  poly;  /* irreducible binary polynomial for (GF2m) */
9bc9710
+    } u;
9bc9710
+    int         k1;     /* first coefficient of pentanomial or
9bc9710
+                         * the only coefficient of trinomial
9bc9710
+                         */
9bc9710
+    int         k2;     /* two remaining coefficients of pentanomial */
9bc9710
+    int         k3;
9bc9710
+};
9bc9710
+typedef struct ECFieldIDStr ECFieldID;
9bc9710
+
9bc9710
+struct ECCurveStr {
9bc9710
+        SECItem a;      /* contains octet stream encoding of
9bc9710
+                         * field element (X9.62 section 4.3.3)
9bc9710
+                         */
9bc9710
+        SECItem b;
9bc9710
+        SECItem seed;
9bc9710
+};
9bc9710
+typedef struct ECCurveStr ECCurve;
9bc9710
+
9bc9710
+typedef void PRArenaPool;
9bc9710
+
9bc9710
+struct ECParamsStr {
9bc9710
+    PRArenaPool * arena;
9bc9710
+    ECParamsType  type;
9bc9710
+    ECFieldID     fieldID;
9bc9710
+    ECCurve       curve;
9bc9710
+    SECItem       base;
9bc9710
+    SECItem       order;
9bc9710
+    int           cofactor;
9bc9710
+    SECItem       DEREncoding;
9bc9710
+    ECCurveName   name;
9bc9710
+    SECItem       curveOID;
9bc9710
+};
9bc9710
+typedef struct ECParamsStr ECParams;
9bc9710
+
9bc9710
+struct ECPublicKeyStr {
9bc9710
+    ECParams ecParams;
9bc9710
+    SECItem publicValue;   /* elliptic curve point encoded as
9bc9710
+                            * octet stream.
9bc9710
+                            */
9bc9710
+};
9bc9710
+typedef struct ECPublicKeyStr ECPublicKey;
9bc9710
+
9bc9710
+struct ECPrivateKeyStr {
9bc9710
+    ECParams ecParams;
9bc9710
+    SECItem publicValue;   /* encoded ec point */
9bc9710
+    SECItem privateValue;  /* private big integer */
9bc9710
+    SECItem version;       /* As per SEC 1, Appendix C, Section C.4 */
9bc9710
+};
9bc9710
+typedef struct ECPrivateKeyStr ECPrivateKey;
9bc9710
+
9bc9710
+typedef enum _SECStatus {
9bc9710
+        SECBufferTooSmall = -3,
9bc9710
+        SECWouldBlock = -2,
9bc9710
+        SECFailure = -1,
9bc9710
+        SECSuccess = 0
9bc9710
+} SECStatus;
9bc9710
+#endif
9bc9710
+
9bc9710
+#ifdef _KERNEL
9bc9710
+#define RNG_GenerateGlobalRandomBytes(p,l) ecc_knzero_random_generator((p), (l))
9bc9710
+#else
9bc9710
+/*
9bc9710
+ This function is no longer required because the random bytes are now
9bc9710
+ supplied by the caller. Force a failure.
9bc9710
+*/
9bc9710
+#ifndef SYSTEM_NSS
9bc9710
+#define RNG_GenerateGlobalRandomBytes(p,l) SECFailure
9bc9710
+#endif
9bc9710
+#endif
9bc9710
+#define CHECK_MPI_OK(func) if (MP_OKAY > (err = func)) goto cleanup
9bc9710
+#define MP_TO_SEC_ERROR(err)
9bc9710
+
9bc9710
+#define SECITEM_TO_MPINT(it, mp)                                        \
9bc9710
+        CHECK_MPI_OK(mp_read_unsigned_octets((mp), (it).data, (it).len))
9bc9710
+
9bc9710
+extern int ecc_knzero_random_generator(uint8_t *, size_t);
9bc9710
+extern ulong_t soft_nzero_random_generator(uint8_t *, ulong_t);
9bc9710
+
9bc9710
+#ifdef SYSTEM_NSS
9bc9710
+#define EC_DecodeParams(a,b,c) EC_DecodeParams(a,b)
9bc9710
+#define ECDSA_VerifyDigest(a,b,c,d) ECDSA_VerifyDigest(a,b,c)
9bc9710
+#define ECDH_Derive(a,b,c,d,e,f) ECDH_Derive(a,b,c,d,e)
9bc9710
+#else
9bc9710
+extern SECStatus EC_DecodeParams(const SECItem *, ECParams **, int);
9bc9710
+
9bc9710
+extern SECItem * SECITEM_AllocItem(PRArenaPool *, SECItem *, unsigned int, int);
9bc9710
+extern SECStatus SECITEM_CopyItem(PRArenaPool *, SECItem *, const SECItem *,
9bc9710
+    int);
9bc9710
+extern void SECITEM_FreeItem(SECItem *, boolean_t);
9bc9710
+
9bc9710
+/* This function has been modified to accept an array of random bytes */
9bc9710
+extern SECStatus EC_NewKey(ECParams *ecParams, ECPrivateKey **privKey,
9bc9710
+    const unsigned char* random, int randomlen, int);
9bc9710
+/* This function has been modified to accept an array of random bytes */
9bc9710
+extern SECStatus ECDSA_SignDigest(ECPrivateKey *, SECItem *, const SECItem *,
9bc9710
+    const unsigned char* random, int randomlen, int, int timing);
9bc9710
+extern SECStatus ECDSA_VerifyDigest(ECPublicKey *, const SECItem *,
9bc9710
+    const SECItem *, int);
9bc9710
+extern SECStatus ECDH_Derive(SECItem *, ECParams *, SECItem *, boolean_t,
9bc9710
+    SECItem *, int);
9bc9710
+#endif
9bc9710
+
9bc9710
+#ifdef  __cplusplus
9bc9710
+}
9bc9710
+#endif
9bc9710
+
9bc9710
+#endif /* _ECC_IMPL_H */