Blame pr1983-rh1565658-support_using_the_system_installation_of_nss_with_the_sunec_provider_jdk12.patch

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