diff --git a/ChangeLog b/ChangeLog
index f09e0dcf34e914d93ce22f46ab47a0d32d62317f..523675b616789adb59964f1eb1632acd50671e38 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2013-02-27  Niels Möller  <nisse@lysator.liu.se>
 
+	* configure.ac (OPT_ASM_SOURCES): New substituted variable.
+	(asm_replace_list, asm_optional_list): New variables. For files in
+	asm_optional_list, also add them to OPT_ASM_SOURCES and define
+	appropriate HAVE_NATIVE_* symbols found.
+
+	* Makefile.in (OPT_ASM_SOURCES): New variable. Used for setting
+	hogweed_OBJS and hogweed_PURE_OBJS.
+
 	* testsuite/ecc-mod-test.c: Increased test count.
 
 	* ecc-384.c (ecc_384_modp): Fixed typo which broke carry handling
diff --git a/Makefile.in b/Makefile.in
index a327b0bf4af06ba96d9748bb582a07dcecde0f10..ad48a5ce3c46dab8f9bd99f350230f678cadb073 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -14,6 +14,8 @@ INSTALL_SCRIPT = @INSTALL_SCRIPT@
 INSTALL_STRIP_PROGRAM = $(INSTALL_PROGRAM) -s
 MKDIR_P = @MKDIR_P@
 
+OPT_ASM_SOURCES = @OPT_ASM_SOURCES@
+
 SUBDIRS = tools testsuite examples
 
 include config.make
@@ -177,8 +179,8 @@ DISTFILES = $(SOURCES) $(HEADERS) getopt.h .bootstrap run-tests \
 nettle_OBJS = $(nettle_SOURCES:.c=.$(OBJEXT)) $(LIBOBJS)
 nettle_PURE_OBJS = $(nettle_OBJS:.$(OBJEXT)=.p$(OBJEXT))
 
-hogweed_OBJS = $(hogweed_SOURCES:.c=.$(OBJEXT))
-hogweed_PURE_OBJS = $(hogweed_OBJS:.$(OBJEXT)=.p$(OBJEXT))
+hogweed_OBJS = $(hogweed_SOURCES:.c=.$(OBJEXT)) $(OPT_ASM_SOURCES:.asm=.$(OBJEXT))
+hogweed_PURE_OBJS = $(hogweed_OBJS:.$(OBJEXT)=.p$(OBJEXT)) $(OPT_ASM_SOURCES:.asm=.p$(OBJEXT))
 
 libnettle.a: $(nettle_OBJS)
 	-rm -f $@
diff --git a/configure.ac b/configure.ac
index 848601d42f48de1ac177f212e69e6ef0c70dfcd2..26776bb2e133cbd114c1075c8f873aae86b219f3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -237,25 +237,50 @@ if test "x$enable_assembler" = xyes ; then
       ;;
   esac
 fi
-# echo "enable_assembler: $enable_assembler, asm_path: $asm_path"
+
+# Files which replace a C source file (or otherwise don't correspond
+# to a new object file).
+asm_replace_list="aes-encrypt-internal.asm aes-decrypt-internal.asm \
+		arcfour-crypt.asm camellia-crypt-internal.asm \
+		md5-compress.asm memxor.asm \
+		salsa20-crypt.asm salsa20-core-internal.asm \
+		serpent-encrypt.asm serpent-decrypt.asm \
+		sha1-compress.asm machine.m4"
+# Assembler files which generate additional object files if they are used.
+asm_optional_list=""
+
+if test "x$enable_public_key" = "xyes" ; then
+  asm_optional_list="$asm_search_list ecc-192-modp.asm ecc-256-redc.asm"
+fi
+
+OPT_ASM_SOURCES=""
+
 asm_file_list=""
 
 if test "x$enable_assembler" = xyes ; then
   if test -n "$asm_path"; then
     AC_MSG_NOTICE([Looking for assembler files in $asm_path/.])
-    for tmp_f in aes-encrypt-internal.asm aes-decrypt-internal.asm \
-		 arcfour-crypt.asm camellia-crypt-internal.asm \
-		 md5-compress.asm memxor.asm \
-		 salsa20-crypt.asm salsa20-core-internal.asm \
-		 serpent-encrypt.asm serpent-decrypt.asm \
-		 sha1-compress.asm machine.m4; do
-#       echo "Looking for $srcdir/$asm_path/$tmp_f"
+    for tmp_f in $asm_replace_list ; do
       if test -f "$srcdir/$asm_path/$tmp_f"; then
-#        echo found
 	asm_file_list="$asm_file_list $tmp_f"
         AC_CONFIG_LINKS($tmp_f:$asm_path/$tmp_f)
       fi
     done
+    dnl Workaround for AC_CONFIG_LINKS, which complains if we use the
+    dnl same destination argument $tmp_f multiple times.
+    for tmp_o in $asm_optional_list ; do
+      if test -f "$srcdir/$asm_path/$tmp_o"; then
+	asm_file_list="$asm_file_list $tmp_o"
+        AC_CONFIG_LINKS($tmp_o:$asm_path/$tmp_o)
+	while read tmp_func ; do
+	  AC_DEFINE_UNQUOTED(HAVE_NATIVE_$tmp_func)
+	  eval HAVE_NATIVE_$tmp_func=yes
+	done <<EOF
+[`sed -n 's/[^ 	]*PROLOGUE(_*\(nettle_\)*\([^)]*\)).*$/\2/p' < "$srcdir/$asm_path/$tmp_o"`]
+EOF
+	OPT_ASM_SOURCES="$OPT_SOURCES $tmp_o"
+      fi
+    done	
     if test -z "$asm_file_list"; then
       enable_assembler=no
       AC_MSG_WARN([No assembler files found.])
@@ -263,6 +288,22 @@ if test "x$enable_assembler" = xyes ; then
   fi
 fi
 
+AC_SUBST([OPT_ASM_SOURCES])
+
+AH_VERBATIM([HAVE_NATIVE],
+[/* Define to 1 each of the following for which a native (ie. CPU specific)
+    implementation of the corresponding routine exists.  */
+#undef HAVE_NATIVE_ecc_192_modp
+#undef HAVE_NATIVE_ecc_192_redc
+#undef HAVE_NATIVE_ecc_224_modp
+#undef HAVE_NATIVE_ecc_224_redc
+#undef HAVE_NATIVE_ecc_256_modp
+#undef HAVE_NATIVE_ecc_256_redc
+#undef HAVE_NATIVE_ecc_384_modp
+#undef HAVE_NATIVE_ecc_384_redc
+#undef HAVE_NATIVE_ecc_521_modp
+#undef HAVE_NATIVE_ecc_521_redc])
+
 # Besides getting correct dependencies, the explicit rules also tell
 # make that the .s files "ought to exist", so they are preferred over
 # .c files.