Blame SOURCES/0002-enable-openssl.patch

c0b7e4
diff --git a/configure.ac b/configure.ac
c0b7e4
index 9f8b31b..38781da 100755
c0b7e4
--- a/configure.ac
c0b7e4
+++ b/configure.ac
c0b7e4
@@ -529,6 +529,20 @@ AC_ARG_ENABLE([thread-sanitizer],
c0b7e4
   AS_HELP_STRING([--enable-thread-sanitizer], [enable ThreadSanitizer support for detecting data races]))
c0b7e4
 AC_ARG_ENABLE([memory-sanitizer],
c0b7e4
   AS_HELP_STRING([--enable-memory-sanitizer], [enable MemorySanitizer support for detecting uninitialized memory reads]))
c0b7e4
+AC_ARG_WITH([crypto],
c0b7e4
+  AS_HELP_STRING([--with-crypto=<internal|openssl>], [choose between different implementations of cryptographic functions(default value is --with-crypto=internal)]))
c0b7e4
+
c0b7e4
+#if openssl, else use internal as default
c0b7e4
+AS_IF([test x"${with_crypto}" = x"openssl"], [
c0b7e4
+  AC_CHECK_LIB([crypto], [EVP_DigestInit], [LIBS="$LIBS -lcrypto"], [], [])
c0b7e4
+  if test "$ac_cv_lib_crypto_EVP_DigestInit" = no; then
c0b7e4
+    AC_MSG_ERROR([build with openssl has been specified but openssl library was not found on your system])
c0b7e4
+  else
c0b7e4
+    AC_DEFINE([CRYPTO_OPENSSL], [1], [Compile with openssl support])
c0b7e4
+  fi
c0b7e4
+], [test x"${with_crypto}" = x"internal" || test x"${with_crypto}" = x"" ], [AC_DEFINE([CRYPTO_INTERNAL], [1], [Compile with internal cryptographic implementation])
c0b7e4
+], [AC_MSG_ERROR([Unknown value for --with-crypto])]
c0b7e4
+)
c0b7e4
 
c0b7e4
 AS_IF([test "${enable_clippy_only}" != "yes"], [
c0b7e4
 AC_CHECK_HEADERS([json-c/json.h])
c0b7e4
diff --git a/lib/subdir.am b/lib/subdir.am
c0b7e4
index 0b7af18..0533e24 100644
c0b7e4
--- a/lib/subdir.am
c0b7e4
+++ b/lib/subdir.am
c0b7e4
@@ -41,7 +41,6 @@ lib_libfrr_la_SOURCES = \
c0b7e4
 	lib/libfrr.c \
c0b7e4
 	lib/linklist.c \
c0b7e4
 	lib/log.c \
c0b7e4
-	lib/md5.c \
c0b7e4
 	lib/memory.c \
c0b7e4
 	lib/memory_vty.c \
c0b7e4
 	lib/module.c \
c0b7e4
diff --git a/lib/subdir.am b/lib/subdir.am
c0b7e4
index 0533e24..b3d3700 100644
c0b7e4
--- a/lib/subdir.am
c0b7e4
+++ b/lib/subdir.am
c0b7e4
@@ -170,7 +170,6 @@ pkginclude_HEADERS += \
c0b7e4
 	lib/libospf.h \
c0b7e4
 	lib/linklist.h \
c0b7e4
 	lib/log.h \
c0b7e4
-	lib/md5.h \
c0b7e4
 	lib/memory.h \
c0b7e4
 	lib/memory_vty.h \
c0b7e4
 	lib/module.h \
c0b7e4
diff --git a/lib/subdir.am b/lib/subdir.am
c0b7e4
index 53f7115..cea866f 100644
c0b7e4
--- a/lib/subdir.am
c0b7e4
+++ b/lib/subdir.am
c0b7e4
@@ -64,7 +64,6 @@ lib_libfrr_la_SOURCES = \
c0b7e4
 	lib/ringbuf.c \
c0b7e4
 	lib/routemap.c \
c0b7e4
 	lib/sbuf.c \
c0b7e4
-	lib/sha256.c \
c0b7e4
 	lib/sigevent.c \
c0b7e4
 	lib/skiplist.c \
c0b7e4
 	lib/sockopt.c \
c0b7e4
@@ -191,7 +190,6 @@ pkginclude_HEADERS += \
c0b7e4
 	lib/ringbuf.h \
c0b7e4
 	lib/routemap.h \
c0b7e4
 	lib/sbuf.h \
c0b7e4
-	lib/sha256.h \
c0b7e4
 	lib/sigevent.h \
c0b7e4
 	lib/skiplist.h \
c0b7e4
 	lib/smux.h \
c0b7e4
diff --git a/lib/zebra.h b/lib/zebra.h
c0b7e4
index 22239f8e60..a308d46cc9 100644
c0b7e4
--- a/lib/zebra.h
c0b7e4
+++ b/lib/zebra.h
c0b7e4
@@ -134,6 +134,11 @@ typedef unsigned char uint8_t;
c0b7e4
 #endif
c0b7e4
 #endif
c0b7e4
 
c0b7e4
+#ifdef CRYPTO_OPENSSL
c0b7e4
+#include <openssl/evp.h>
c0b7e4
+#include <openssl/hmac.h>
c0b7e4
+#endif
c0b7e4
+
c0b7e4
 #include "openbsd-tree.h"
c0b7e4
 
c0b7e4
 #include <netinet/in.h>
c0b7e4
diff --git a/ospfd/ospf_packet.c b/ospfd/ospf_packet.c
c0b7e4
index 6bc8c25153..b951e94ae6 100644
c0b7e4
--- a/ospfd/ospf_packet.c
c0b7e4
+++ b/ospfd/ospf_packet.c
c0b7e4
@@ -33,7 +33,9 @@
c0b7e4
 #include "log.h"
c0b7e4
 #include "sockopt.h"
c0b7e4
 #include "checksum.h"
c0b7e4
+#if  !defined(CRYPTO_OPENSSL) && !defined(HAVE_NETTLE)
c0b7e4
 #include "md5.h"
c0b7e4
+#endif
c0b7e4
 #include "vrf.h"
c0b7e4
 #include "lib_errors.h"
c0b7e4
 
c0b7e4
@@ -332,7 +334,11 @@ static unsigned int ospf_packet_max(struct ospf_interface *oi)
c0b7e4
 static int ospf_check_md5_digest(struct ospf_interface *oi,
c0b7e4
 				 struct ospf_header *ospfh)
c0b7e4
 {
c0b7e4
+#ifdef CRYPTO_OPENSSL
c0b7e4
+	EVP_MD_CTX *ctx;
c0b7e4
+#else
c0b7e4
 	MD5_CTX ctx;
c0b7e4
+#endif
c0b7e4
 	unsigned char digest[OSPF_AUTH_MD5_SIZE];
c0b7e4
 	struct crypt_key *ck;
c0b7e4
 	struct ospf_neighbor *nbr;
c0b7e4
@@ -361,11 +367,21 @@ static int ospf_check_md5_digest(struct ospf_interface *oi,
c0b7e4
 	}
c0b7e4
 
c0b7e4
 	/* Generate a digest for the ospf packet - their digest + our digest. */
c0b7e4
+#ifdef CRYPTO_OPENSSL
c0b7e4
+	unsigned int md5_size = OSPF_AUTH_MD5_SIZE;
c0b7e4
+	ctx = EVP_MD_CTX_new();
c0b7e4
+	EVP_DigestInit(ctx, EVP_md5());
c0b7e4
+	EVP_DigestUpdate(ctx, ospfh, length);
c0b7e4
+	EVP_DigestUpdate(ctx, ck->auth_key, OSPF_AUTH_MD5_SIZE);
c0b7e4
+	EVP_DigestFinal(ctx, digest, &md5_size);
c0b7e4
+	EVP_MD_CTX_free(ctx);
c0b7e4
+#elif CRYPTO_INTERNAL
c0b7e4
 	memset(&ctx, 0, sizeof(ctx));
c0b7e4
 	MD5Init(&ctx;;
c0b7e4
 	MD5Update(&ctx, ospfh, length);
c0b7e4
 	MD5Update(&ctx, ck->auth_key, OSPF_AUTH_MD5_SIZE);
c0b7e4
 	MD5Final(digest, &ctx;;
c0b7e4
+#endif
c0b7e4
 
c0b7e4
 	/* compare the two */
c0b7e4
 	if (memcmp((caddr_t)ospfh + length, digest, OSPF_AUTH_MD5_SIZE)) {
c0b7e4
@@ -389,7 +404,11 @@ static int ospf_make_md5_digest(struct ospf_interface *oi,
c0b7e4
 {
c0b7e4
 	struct ospf_header *ospfh;
c0b7e4
 	unsigned char digest[OSPF_AUTH_MD5_SIZE] = {0};
c0b7e4
+#ifdef CRYPTO_OPENSSL
c0b7e4
+	EVP_MD_CTX *ctx;
c0b7e4
+#else
c0b7e4
 	MD5_CTX ctx;
c0b7e4
+#endif
c0b7e4
 	void *ibuf;
c0b7e4
 	uint32_t t;
c0b7e4
 	struct crypt_key *ck;
c0b7e4
@@ -422,11 +441,21 @@ static int ospf_make_md5_digest(struct ospf_interface *oi,
c0b7e4
 	}
c0b7e4
 
c0b7e4
 	/* Generate a digest for the entire packet + our secret key. */
c0b7e4
+#ifdef CRYPTO_OPENSSL
c0b7e4
+	unsigned int md5_size = OSPF_AUTH_MD5_SIZE;
c0b7e4
+	ctx = EVP_MD_CTX_new();
c0b7e4
+	EVP_DigestInit(ctx, EVP_md5());
c0b7e4
+	EVP_DigestUpdate(ctx, ibuf, ntohs(ospfh->length));
c0b7e4
+	EVP_DigestUpdate(ctx, auth_key, OSPF_AUTH_MD5_SIZE);
c0b7e4
+	EVP_DigestFinal(ctx, digest, &md5_size);
c0b7e4
+	EVP_MD_CTX_free(ctx);
c0b7e4
+#elif CRYPTO_INTERNAL
c0b7e4
 	memset(&ctx, 0, sizeof(ctx));
c0b7e4
 	MD5Init(&ctx;;
c0b7e4
 	MD5Update(&ctx, ibuf, ntohs(ospfh->length));
c0b7e4
 	MD5Update(&ctx, auth_key, OSPF_AUTH_MD5_SIZE);
c0b7e4
 	MD5Final(digest, &ctx;;
c0b7e4
+#endif
c0b7e4
 
c0b7e4
 	/* Append md5 digest to the end of the stream. */
c0b7e4
 	stream_put(op->s, digest, OSPF_AUTH_MD5_SIZE);
c0b7e4
diff --git a/ripd/ripd.c b/ripd/ripd.c
c0b7e4
index e0ff0430f8..b311ac5717 100644
c0b7e4
--- a/ripd/ripd.c
c0b7e4
+++ b/ripd/ripd.c
c0b7e4
@@ -37,7 +37,9 @@
c0b7e4
 #include "if_rmap.h"
c0b7e4
 #include "plist.h"
c0b7e4
 #include "distribute.h"
c0b7e4
+#if !defined(CRYPTO_OPENSSL) && !defined(HAVE_NETTLE)
c0b7e4
 #include "md5.h"
c0b7e4
+#endif
c0b7e4
 #include "keychain.h"
c0b7e4
 #include "privs.h"
c0b7e4
 #include "lib_errors.h"
c0b7e4
@@ -870,7 +872,11 @@ static int rip_auth_md5(struct rip_packet *packet, struct sockaddr_in *from,
c0b7e4
 	struct rip_md5_data *md5data;
c0b7e4
 	struct keychain *keychain;
c0b7e4
 	struct key *key;
c0b7e4
+#ifdef CRYPTO_OPENSSL
c0b7e4
+	EVP_MD_CTX *ctx;
c0b7e4
+#else
c0b7e4
 	MD5_CTX ctx;
c0b7e4
+#endif
c0b7e4
 	uint8_t digest[RIP_AUTH_MD5_SIZE];
c0b7e4
 	uint16_t packet_len;
c0b7e4
 	char auth_str[RIP_AUTH_MD5_SIZE];
c0b7e4
@@ -934,11 +940,21 @@ static int rip_auth_md5(struct rip_packet *packet, struct sockaddr_in *from,
c0b7e4
 		return 0;
c0b7e4
 
c0b7e4
 	/* MD5 digest authentication. */
c0b7e4
+#ifdef CRYPTO_OPENSSL
c0b7e4
+	unsigned int md5_size = RIP_AUTH_MD5_SIZE;
c0b7e4
+	ctx = EVP_MD_CTX_new();
c0b7e4
+	EVP_DigestInit(ctx, EVP_md5());
c0b7e4
+	EVP_DigestUpdate(ctx, packet, packet_len + RIP_HEADER_SIZE);
c0b7e4
+	EVP_DigestUpdate(ctx, auth_str, RIP_AUTH_MD5_SIZE);
c0b7e4
+	EVP_DigestFinal(ctx, digest, &md5_size);
c0b7e4
+	EVP_MD_CTX_free(ctx);
c0b7e4
+#elif CRYPTO_INTERNAL
c0b7e4
 	memset(&ctx, 0, sizeof(ctx));
c0b7e4
 	MD5Init(&ctx;;
c0b7e4
 	MD5Update(&ctx, packet, packet_len + RIP_HEADER_SIZE);
c0b7e4
 	MD5Update(&ctx, auth_str, RIP_AUTH_MD5_SIZE);
c0b7e4
 	MD5Final(digest, &ctx;;
c0b7e4
+#endif
c0b7e4
 
c0b7e4
 	if (memcmp(md5data->digest, digest, RIP_AUTH_MD5_SIZE) == 0)
c0b7e4
 		return packet_len;
c0b7e4
@@ -1063,7 +1078,11 @@ static void rip_auth_md5_set(struct stream *s, struct rip_interface *ri,
c0b7e4
 			     size_t doff, char *auth_str, int authlen)
c0b7e4
 {
c0b7e4
 	unsigned long len;
c0b7e4
+#ifdef CRYPTO_OPENSSL
c0b7e4
+	EVP_MD_CTX *ctx;
c0b7e4
+#else
c0b7e4
 	MD5_CTX ctx;
c0b7e4
+#endif
c0b7e4
 	unsigned char digest[RIP_AUTH_MD5_SIZE];
c0b7e4
 
c0b7e4
 	/* Make it sure this interface is configured as MD5
c0b7e4
@@ -1092,11 +1111,21 @@ static void rip_auth_md5_set(struct stream *s, struct rip_interface *ri,
c0b7e4
 	stream_putw(s, RIP_AUTH_DATA);
c0b7e4
 
c0b7e4
 	/* Generate a digest for the RIP packet. */
c0b7e4
+#ifdef CRYPTO_OPENSSL
c0b7e4
+	unsigned int md5_size = RIP_AUTH_MD5_SIZE;
c0b7e4
+	ctx = EVP_MD_CTX_new();
c0b7e4
+	EVP_DigestInit(ctx, EVP_md5());
c0b7e4
+	EVP_DigestUpdate(ctx, STREAM_DATA(s), stream_get_endp(s));
c0b7e4
+	EVP_DigestUpdate(ctx, auth_str, RIP_AUTH_MD5_SIZE);
c0b7e4
+	EVP_DigestFinal(ctx, digest, &md5_size);
c0b7e4
+	EVP_MD_CTX_free(ctx);
c0b7e4
+#elif CRYPTO_INTERNAL
c0b7e4
 	memset(&ctx, 0, sizeof(ctx));
c0b7e4
 	MD5Init(&ctx;;
c0b7e4
 	MD5Update(&ctx, STREAM_DATA(s), stream_get_endp(s));
c0b7e4
 	MD5Update(&ctx, auth_str, RIP_AUTH_MD5_SIZE);
c0b7e4
 	MD5Final(digest, &ctx;;
c0b7e4
+#endif
c0b7e4
 
c0b7e4
 	/* Copy the digest to the packet. */
c0b7e4
 	stream_write(s, digest, RIP_AUTH_MD5_SIZE);
c0b7e4
diff --git a/isisd/isis_tlvs.c b/isisd/isis_tlvs.c
c0b7e4
index 488dfedae4..862d675e84 100644
c0b7e4
--- a/isisd/isis_tlvs.c
c0b7e4
+++ b/isisd/isis_tlvs.c
c0b7e4
@@ -22,7 +22,9 @@
c0b7e4
  */
c0b7e4
 #include <zebra.h>
c0b7e4
 
c0b7e4
+#ifdef CRYPTO_INTERNAL
c0b7e4
 #include "md5.h"
c0b7e4
+#endif
c0b7e4
 #include "memory.h"
c0b7e4
 #include "stream.h"
c0b7e4
 #include "sbuf.h"
c0b7e4
@@ -2770,8 +2772,13 @@ static void update_auth_hmac_md5(struct isis_auth *auth, struct stream *s,
c0b7e4
 		safe_auth_md5(s, &checksum, &rem_lifetime);
c0b7e4
 
c0b7e4
 	memset(STREAM_DATA(s) + auth->offset, 0, 16);
c0b7e4
+#ifdef CRYPTO_OPENSSL
c0b7e4
+	uint8_t* result = (uint8_t*)HMAC(EVP_md5(), auth->passwd, auth->plength, STREAM_DATA(s), stream_get_endp(s), NULL, NULL);
c0b7e4
+	memcpy(digest, result, 16);
c0b7e4
+#elif  CRYPTO_INTERNAL
c0b7e4
 	hmac_md5(STREAM_DATA(s), stream_get_endp(s), auth->passwd,
c0b7e4
 		 auth->plength, digest);
c0b7e4
+#endif
c0b7e4
 	memcpy(auth->value, digest, 16);
c0b7e4
 	memcpy(STREAM_DATA(s) + auth->offset, digest, 16);
c0b7e4
 
c0b7e4
@@ -3310,8 +3317,13 @@ static bool auth_validator_hmac_md5(struct isis_passwd *passwd,
c0b7e4
 		safe_auth_md5(stream, &checksum, &rem_lifetime);
c0b7e4
 
c0b7e4
 	memset(STREAM_DATA(stream) + auth->offset, 0, 16);
c0b7e4
+#ifdef CRYPTO_OPENSSL
c0b7e4
+	uint8_t* result = (uint8_t*)HMAC(EVP_md5(), passwd->passwd, passwd->len, STREAM_DATA(stream), stream_get_endp(stream), NULL, NULL);
c0b7e4
+	memcpy(digest, result, 16);
c0b7e4
+#elif  CRYPTO_INTERNAL
c0b7e4
 	hmac_md5(STREAM_DATA(stream), stream_get_endp(stream), passwd->passwd,
c0b7e4
 		 passwd->len, digest);
c0b7e4
+#endif
c0b7e4
 	memcpy(STREAM_DATA(stream) + auth->offset, auth->value, 16);
c0b7e4
 
c0b7e4
 	bool rv = !memcmp(digest, auth->value, 16);
c0b7e4
diff --git a/isisd/isis_lsp.c b/isisd/isis_lsp.c
c0b7e4
index 1991666..2e4fe55 100644
c0b7e4
--- a/isisd/isis_lsp.c
c0b7e4
+++ b/isisd/isis_lsp.c
c0b7e4
@@ -35,7 +35,9 @@
c0b7e4
 #include "hash.h"
c0b7e4
 #include "if.h"
c0b7e4
 #include "checksum.h"
c0b7e4
+#ifdef CRYPTO_INTERNAL
c0b7e4
 #include "md5.h"
c0b7e4
+#endif
c0b7e4
 #include "table.h"
c0b7e4
 #include "srcdest_table.h"
c0b7e4
 #include "lib_errors.h"
c0b7e4
diff --git a/isisd/isis_pdu.c b/isisd/isis_pdu.c
c0b7e4
index 9c63311..7cf594c 100644
c0b7e4
--- a/isisd/isis_pdu.c
c0b7e4
+++ b/isisd/isis_pdu.c
c0b7e4
@@ -33,7 +33,9 @@
c0b7e4
 #include "prefix.h"
c0b7e4
 #include "if.h"
c0b7e4
 #include "checksum.h"
c0b7e4
+#ifdef CRYPTO_INTERNAL
c0b7e4
 #include "md5.h"
c0b7e4
+#endif
c0b7e4
 #include "lib_errors.h"
c0b7e4
 
c0b7e4
 #include "isisd/dict.h"
c0b7e4
diff --git a/isisd/isis_te.c b/isisd/isis_te.c
c0b7e4
index 4ea6c2c..72ff0d2 100644
c0b7e4
--- a/isisd/isis_te.c
c0b7e4
+++ b/isisd/isis_te.c
c0b7e4
@@ -38,7 +38,9 @@
c0b7e4
 #include "if.h"
c0b7e4
 #include "vrf.h"
c0b7e4
 #include "checksum.h"
c0b7e4
+#ifdef CRYPTO_INTERNAL
c0b7e4
 #include "md5.h"
c0b7e4
+#endif
c0b7e4
 #include "sockunion.h"
c0b7e4
 #include "network.h"
c0b7e4
 #include "sbuf.h"