6cf9b8
diff -up openssh-7.4p1/cipher.c.fips openssh-7.4p1/cipher.c
6cf9b8
--- openssh-7.4p1/cipher.c.fips	2016-12-23 16:37:49.290741582 +0100
6cf9b8
+++ openssh-7.4p1/cipher.c	2016-12-23 16:37:49.300741586 +0100
802815
@@ -39,6 +39,8 @@
802815
 
802815
 #include <sys/types.h>
802815
 
802815
+#include <openssl/fips.h>
802815
+
802815
 #include <string.h>
802815
 #include <stdarg.h>
802815
 #include <stdio.h>
6cf9b8
@@ -116,6 +118,20 @@ static const struct sshcipher ciphers[]
802815
 	{ NULL,		SSH_CIPHER_INVALID, 0, 0, 0, 0, 0, 0, NULL }
802815
 };
802815
 
190035
+static const struct sshcipher fips_ciphers[] = {
802815
+	{ "none",	SSH_CIPHER_NONE, 8, 0, 0, 0, 0, 0, EVP_enc_null },
802815
+	{ "3des-cbc",	SSH_CIPHER_SSH2, 8, 24, 0, 0, 0, 1, EVP_des_ede3_cbc },
802815
+	{ "aes128-cbc",	SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 1, EVP_aes_128_cbc },
802815
+	{ "aes192-cbc",	SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 1, EVP_aes_192_cbc },
802815
+	{ "aes256-cbc",	SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
802815
+	{ "rijndael-cbc@lysator.liu.se",
802815
+			SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
802815
+	{ "aes128-ctr",	SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 0, EVP_aes_128_ctr },
802815
+	{ "aes192-ctr",	SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 0, EVP_aes_192_ctr },
802815
+	{ "aes256-ctr",	SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 0, EVP_aes_256_ctr },
802815
+	{ NULL,		SSH_CIPHER_INVALID, 0, 0, 0, 0, 0, 0, NULL }
802815
+};
190035
+
802815
 /*--*/
802815
 
190035
 /* Returns a comma-separated list of supported ciphers. */
6cf9b8
@@ -126,7 +142,7 @@ cipher_alg_list(char sep, int auth_only)
802815
 	size_t nlen, rlen = 0;
190035
 	const struct sshcipher *c;
802815
 
802815
-	for (c = ciphers; c->name != NULL; c++) {
802815
+	for (c = FIPS_mode() ? fips_ciphers : ciphers; c->name != NULL; c++) {
802815
 		if (c->number != SSH_CIPHER_SSH2)
802815
 			continue;
802815
 		if (auth_only && c->auth_len == 0)
6cf9b8
@@ -222,7 +238,7 @@ const struct sshcipher *
802815
 cipher_by_name(const char *name)
802815
 {
190035
 	const struct sshcipher *c;
802815
-	for (c = ciphers; c->name != NULL; c++)
802815
+	for (c = FIPS_mode() ? fips_ciphers : ciphers; c->name != NULL; c++)
802815
 		if (strcmp(c->name, name) == 0)
802815
 			return c;
802815
 	return NULL;
6cf9b8
@@ -232,7 +248,7 @@ const struct sshcipher *
802815
 cipher_by_number(int id)
802815
 {
190035
 	const struct sshcipher *c;
802815
-	for (c = ciphers; c->name != NULL; c++)
802815
+	for (c = FIPS_mode() ? fips_ciphers : ciphers; c->name != NULL; c++)
802815
 		if (c->number == id)
802815
 			return c;
802815
 	return NULL;
6cf9b8
@@ -273,7 +289,7 @@ cipher_number(const char *name)
190035
 	const struct sshcipher *c;
802815
 	if (name == NULL)
802815
 		return -1;
802815
-	for (c = ciphers; c->name != NULL; c++)
802815
+	for (c = FIPS_mode() ? fips_ciphers : ciphers; c->name != NULL; c++)
802815
 		if (strcasecmp(c->name, name) == 0)
802815
 			return c->number;
802815
 	return -1;
6cf9b8
diff -up openssh-7.4p1/cipher-ctr.c.fips openssh-7.4p1/cipher-ctr.c
6cf9b8
--- openssh-7.4p1/cipher-ctr.c.fips	2016-12-23 16:37:49.225741551 +0100
6cf9b8
+++ openssh-7.4p1/cipher-ctr.c	2016-12-23 16:37:49.297741585 +0100
535d34
@@ -179,7 +179,8 @@ evp_aes_128_ctr(void)
535d34
 	aes_ctr.do_cipher = ssh_aes_ctr;
535d34
 #ifndef SSH_OLD_EVP
535d34
 	aes_ctr.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH |
535d34
-	    EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV;
535d34
+	    EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV |
535d34
+	    EVP_CIPH_FLAG_FIPS;
535d34
 #endif
535d34
 	return (&aes_ctr);
535d34
 }
6cf9b8
diff -up openssh-7.4p1/dh.h.fips openssh-7.4p1/dh.h
6cf9b8
--- openssh-7.4p1/dh.h.fips	2016-12-19 05:59:41.000000000 +0100
6cf9b8
+++ openssh-7.4p1/dh.h	2016-12-23 16:37:49.297741585 +0100
6cf9b8
@@ -51,6 +51,7 @@ u_int	 dh_estimate(int);
13bf5b
  * Miniumum increased in light of DH precomputation attacks.
13bf5b
  */
13073f
 #define DH_GRP_MIN	2048
802815
+#define DH_GRP_MIN_FIPS	2048
802815
 #define DH_GRP_MAX	8192
802815
 
802815
 /*
6cf9b8
diff -up openssh-7.4p1/entropy.c.fips openssh-7.4p1/entropy.c
6cf9b8
--- openssh-7.4p1/entropy.c.fips	2016-12-23 16:37:49.219741548 +0100
6cf9b8
+++ openssh-7.4p1/entropy.c	2016-12-23 16:37:49.297741585 +0100
132f8f
@@ -217,6 +217,9 @@ seed_rng(void)
802815
 		fatal("OpenSSL version mismatch. Built against %lx, you "
802815
 		    "have %lx", (u_long)OPENSSL_VERSION_NUMBER, SSLeay());
802815
 
802815
+	/* clean the PRNG status when exiting the program */
802815
+	atexit(RAND_cleanup);
802815
+
802815
 #ifndef OPENSSL_PRNG_ONLY
802815
 	if (RAND_status() == 1) {
802815
 		debug3("RNG is ready, skipping seeding");
6cf9b8
diff -up openssh-7.4p1/kex.c.fips openssh-7.4p1/kex.c
6cf9b8
--- openssh-7.4p1/kex.c.fips	2016-12-23 16:37:49.290741582 +0100
6cf9b8
+++ openssh-7.4p1/kex.c	2016-12-23 16:37:49.300741586 +0100
190035
@@ -35,6 +35,7 @@
190035
 #ifdef WITH_OPENSSL
802815
 #include <openssl/crypto.h>
5878eb
 #include <openssl/dh.h>
802815
+#include <openssl/fips.h>
190035
 #endif
802815
 
132f8f
 #include "ssh2.h"
6cf9b8
@@ -125,6 +126,23 @@ static const struct kexalg kexalgs[] = {
802815
 	{ NULL, -1, -1, -1},
802815
 };
802815
 
802815
+static const struct kexalg kexalgs_fips[] = {
802815
+#ifdef HAVE_EVP_SHA256
802815
+	{ KEX_DHGEX_SHA256, KEX_DH_GEX_SHA256, 0, SSH_DIGEST_SHA256 },
802815
+#endif
802815
+#ifdef OPENSSL_HAS_ECC
802815
+	{ KEX_ECDH_SHA2_NISTP256, KEX_ECDH_SHA2,
802815
+	    NID_X9_62_prime256v1, SSH_DIGEST_SHA256 },
802815
+	{ KEX_ECDH_SHA2_NISTP384, KEX_ECDH_SHA2, NID_secp384r1,
802815
+	    SSH_DIGEST_SHA384 },
802815
+# ifdef OPENSSL_HAS_NISTP521
802815
+	{ KEX_ECDH_SHA2_NISTP521, KEX_ECDH_SHA2, NID_secp521r1,
802815
+	    SSH_DIGEST_SHA512 },
802815
+# endif
802815
+#endif
580f98
+	{ NULL, -1, -1, -1},
802815
+};
802815
+
802815
 char *
802815
 kex_alg_list(char sep)
802815
 {
6cf9b8
@@ -152,7 +170,7 @@ kex_alg_by_name(const char *name)
802815
 {
802815
 	const struct kexalg *k;
802815
 
802815
-	for (k = kexalgs; k->name != NULL; k++) {
802815
+	for (k = (FIPS_mode() ? kexalgs_fips : kexalgs); k->name != NULL; k++) {
802815
 		if (strcmp(k->name, name) == 0)
802815
 			return k;
802815
 #ifdef GSSAPI
6cf9b8
@@ -178,7 +196,10 @@ kex_names_valid(const char *names)
802815
 	for ((p = strsep(&cp, ",")); p && *p != '\0';
802815
 	    (p = strsep(&cp, ","))) {
802815
 		if (kex_alg_by_name(p) == NULL) {
802815
-			error("Unsupported KEX algorithm \"%.100s\"", p);
802815
+			if (FIPS_mode())
802815
+				error("\"%.100s\" is not allowed in FIPS mode", p);
802815
+			else
802815
+				error("Unsupported KEX algorithm \"%.100s\"", p);
802815
 			free(s);
802815
 			return 0;
802815
 		}
6cf9b8
diff -up openssh-7.4p1/kexgexc.c.fips openssh-7.4p1/kexgexc.c
6cf9b8
--- openssh-7.4p1/kexgexc.c.fips	2016-12-19 05:59:41.000000000 +0100
6cf9b8
+++ openssh-7.4p1/kexgexc.c	2016-12-23 16:38:38.727763540 +0100
535d34
@@ -28,6 +28,7 @@
802815
 
132f8f
 #ifdef WITH_OPENSSL
802815
 
802815
+#include <openssl/fips.h>
802815
 #include <sys/types.h>
802815
 
6cf9b8
 #include <openssl/dh.h>
535d34
@@ -63,7 +64,7 @@ kexgex_client(struct ssh *ssh)
132f8f
 
132f8f
 	nbits = dh_estimate(kex->dh_need * 8);
08fe9e
 
132f8f
-	kex->min = DH_GRP_MIN;
132f8f
+	kex->min = FIPS_mode() ? DH_GRP_MIN_FIPS : DH_GRP_MIN;
132f8f
 	kex->max = DH_GRP_MAX;
535d34
 	kex->nbits = nbits;
535d34
 	if (datafellows & SSH_BUG_DHGEX_LARGE)
6cf9b8
diff -up openssh-7.4p1/kexgexs.c.fips openssh-7.4p1/kexgexs.c
6cf9b8
--- openssh-7.4p1/kexgexs.c.fips	2016-12-23 16:37:49.297741585 +0100
6cf9b8
+++ openssh-7.4p1/kexgexs.c	2016-12-23 16:39:35.009776626 +0100
13bf5b
@@ -83,9 +83,9 @@ input_kex_dh_gex_request(int type, u_int
535d34
 	kex->nbits = nbits;
13bf5b
 	kex->min = min;
535d34
 	kex->max = max;
6cf9b8
-	min = MAXIMUM(DH_GRP_MIN, min);
6cf9b8
+	min = MAXIMUM(FIPS_mode() ? DH_GRP_MIN_FIPS : DH_GRP_MIN, min);
6cf9b8
 	max = MINIMUM(DH_GRP_MAX, max);
6cf9b8
-	nbits = MAXIMUM(DH_GRP_MIN, nbits);
6cf9b8
+	nbits = MAXIMUM(FIPS_mode() ? DH_GRP_MIN_FIPS : DH_GRP_MIN, nbits);
6cf9b8
 	nbits = MINIMUM(DH_GRP_MAX, nbits);
535d34
 
535d34
 	if (kex->max < kex->min || kex->nbits < kex->min ||
6cf9b8
diff -up openssh-7.4p1/mac.c.fips openssh-7.4p1/mac.c
6cf9b8
--- openssh-7.4p1/mac.c.fips	2016-12-23 16:37:49.291741582 +0100
6cf9b8
+++ openssh-7.4p1/mac.c	2016-12-23 16:37:49.298741585 +0100
802815
@@ -27,6 +27,8 @@
802815
 
802815
 #include <sys/types.h>
802815
 
802815
+#include <openssl/fips.h>
802815
+
802815
 #include <string.h>
132f8f
 #include <stdio.h>
132f8f
 
132f8f
@@ -54,7 +56,7 @@ struct macalg {
802815
 	int		etm;		/* Encrypt-then-MAC */
802815
 };
802815
 
802815
-static const struct macalg macs[] = {
802815
+static const struct macalg all_macs[] = {
802815
 	/* Encrypt-and-MAC (encrypt-and-authenticate) variants */
802815
 	{ "hmac-sha1",				SSH_DIGEST, SSH_DIGEST_SHA1, 0, 0, 0, 0 },
802815
 	{ "hmac-sha1-96",			SSH_DIGEST, SSH_DIGEST_SHA1, 96, 0, 0, 0 },
6cf9b8
@@ -89,6 +91,24 @@ static const struct macalg macs[] = {
802815
 	{ NULL,					0, 0, 0, 0, 0, 0 }
802815
 };
802815
 
802815
+static const struct macalg fips_macs[] = {
802815
+	/* Encrypt-and-MAC (encrypt-and-authenticate) variants */
802815
+	{ "hmac-sha1",				SSH_DIGEST, SSH_DIGEST_SHA1, 0, 0, 0, 0 },
802815
+#ifdef HAVE_EVP_SHA256
802815
+	{ "hmac-sha2-256",			SSH_DIGEST, SSH_DIGEST_SHA256, 0, 0, 0, 0 },
802815
+	{ "hmac-sha2-512",			SSH_DIGEST, SSH_DIGEST_SHA512, 0, 0, 0, 0 },
802815
+#endif
802815
+
802815
+	/* Encrypt-then-MAC variants */
802815
+	{ "hmac-sha1-etm@openssh.com",		SSH_DIGEST, SSH_DIGEST_SHA1, 0, 0, 0, 1 },
802815
+#ifdef HAVE_EVP_SHA256
802815
+	{ "hmac-sha2-256-etm@openssh.com",	SSH_DIGEST, SSH_DIGEST_SHA256, 0, 0, 0, 1 },
802815
+	{ "hmac-sha2-512-etm@openssh.com",	SSH_DIGEST, SSH_DIGEST_SHA512, 0, 0, 0, 1 },
802815
+#endif
802815
+
802815
+	{ NULL,					0, 0, 0, 0, 0, 0 }
802815
+};
802815
+
802815
 /* Returns a list of supported MACs separated by the specified char. */
802815
 char *
802815
 mac_alg_list(char sep)
6cf9b8
@@ -97,7 +117,7 @@ mac_alg_list(char sep)
802815
 	size_t nlen, rlen = 0;
802815
 	const struct macalg *m;
802815
 
802815
-	for (m = macs; m->name != NULL; m++) {
802815
+	for (m = FIPS_mode() ? fips_macs : all_macs; m->name != NULL; m++) {
802815
 		if (ret != NULL)
802815
 			ret[rlen++] = sep;
802815
 		nlen = strlen(m->name);
6cf9b8
@@ -136,7 +156,7 @@ mac_setup(struct sshmac *mac, char *name
802815
 {
802815
 	const struct macalg *m;
802815
 
802815
-	for (m = macs; m->name != NULL; m++) {
802815
+	for (m = FIPS_mode() ? fips_macs : all_macs; m->name != NULL; m++) {
802815
 		if (strcmp(name, m->name) != 0)
802815
 			continue;
132f8f
 		if (mac != NULL)
6cf9b8
diff -up openssh-7.4p1/Makefile.in.fips openssh-7.4p1/Makefile.in
6cf9b8
--- openssh-7.4p1/Makefile.in.fips	2016-12-23 16:37:49.291741582 +0100
6cf9b8
+++ openssh-7.4p1/Makefile.in	2016-12-23 16:37:49.298741585 +0100
6cf9b8
@@ -169,25 +169,25 @@ libssh.a: $(LIBSSH_OBJS)
535d34
 	$(RANLIB) $@
535d34
 
535d34
 ssh$(EXEEXT): $(LIBCOMPAT) libssh.a $(SSHOBJS)
535d34
-	$(LD) -o $@ $(SSHOBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(SSHLIBS) $(LIBS) $(GSSLIBS)
535d34
+	$(LD) -o $@ $(SSHOBJS) $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(SSHLIBS) $(LIBS) $(GSSLIBS)
535d34
 
535d34
 sshd$(EXEEXT): libssh.a	$(LIBCOMPAT) $(SSHDOBJS)
535d34
-	$(LD) -o $@ $(SSHDOBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(SSHDLIBS) $(LIBS) $(GSSLIBS) $(K5LIBS)
535d34
+	$(LD) -o $@ $(SSHDOBJS) $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(SSHDLIBS) $(LIBS) $(GSSLIBS) $(K5LIBS)
535d34
 
5878eb
 scp$(EXEEXT): $(LIBCOMPAT) libssh.a scp.o progressmeter.o
5878eb
 	$(LD) -o $@ scp.o progressmeter.o bufaux.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
535d34
 
535d34
 ssh-add$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-add.o
535d34
-	$(LD) -o $@ ssh-add.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
535d34
+	$(LD) -o $@ ssh-add.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
535d34
 
535d34
 ssh-agent$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-agent.o ssh-pkcs11-client.o
535d34
-	$(LD) -o $@ ssh-agent.o ssh-pkcs11-client.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
535d34
+	$(LD) -o $@ ssh-agent.o ssh-pkcs11-client.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
535d34
 
535d34
 ssh-keygen$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keygen.o
535d34
-	$(LD) -o $@ ssh-keygen.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
535d34
+	$(LD) -o $@ ssh-keygen.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
535d34
 
13073f
 ssh-keysign$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keysign.o readconf.o
13073f
-	$(LD) -o $@ ssh-keysign.o readconf.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
13073f
+	$(LD) -o $@ ssh-keysign.o readconf.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
535d34
 
535d34
 ssh-pkcs11-helper$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-pkcs11-helper.o ssh-pkcs11.o
535d34
 	$(LD) -o $@ ssh-pkcs11-helper.o ssh-pkcs11.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh -lopenbsd-compat $(LIBS)
6cf9b8
@@ -205,7 +205,7 @@ ssh-cavs$(EXEEXT): $(LIBCOMPAT) libssh.a
13073f
 	$(LD) -o $@ ssh-cavs.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
535d34
 
13073f
 ssh-keyscan$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keyscan.o
13073f
-	$(LD) -o $@ ssh-keyscan.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh $(LIBS)
13073f
+	$(LD) -o $@ ssh-keyscan.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh -lfipscheck $(LIBS)
535d34
 
535d34
 sftp-server$(EXEEXT): $(LIBCOMPAT) libssh.a sftp.o sftp-common.o sftp-server.o sftp-server-main.o
535d34
 	$(LD) -o $@ sftp-server.o sftp-common.o sftp-server-main.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
6cf9b8
diff -up openssh-7.4p1/myproposal.h.fips openssh-7.4p1/myproposal.h
6cf9b8
--- openssh-7.4p1/myproposal.h.fips	2016-12-19 05:59:41.000000000 +0100
6cf9b8
+++ openssh-7.4p1/myproposal.h	2016-12-23 16:37:49.300741586 +0100
6cf9b8
@@ -138,6 +138,26 @@
13bf5b
 
13bf5b
 #define KEX_CLIENT_MAC KEX_SERVER_MAC
802815
 
802815
+#define KEX_DEFAULT_KEX_FIPS		\
802815
+	KEX_ECDH_METHODS \
6cf9b8
+	KEX_SHA2_METHODS
802815
+#define	KEX_FIPS_ENCRYPT \
802815
+	"aes128-ctr,aes192-ctr,aes256-ctr," \
802815
+	"aes128-cbc,3des-cbc," \
802815
+	"aes192-cbc,aes256-cbc,rijndael-cbc@lysator.liu.se"
802815
+#ifdef HAVE_EVP_SHA256
802815
+#define	KEX_FIPS_MAC \
802815
+	"hmac-sha1," \
802815
+	"hmac-sha2-256," \
802815
+	"hmac-sha2-512," \
802815
+	"hmac-sha1-etm@openssh.com," \
802815
+	"hmac-sha2-256-etm@openssh.com," \
802815
+	"hmac-sha2-512-etm@openssh.com"
802815
+#else
802815
+#define        KEX_FIPS_MAC \
802815
+       "hmac-sha1"
802815
+#endif
190035
+
13bf5b
 #else /* WITH_OPENSSL */
802815
 
190035
 #define KEX_SERVER_KEX		\
bdb932
diff -up openssh-7.4p1/pam_ssh_agent_auth-0.10.3/pam_user_key_allowed2.c.fips openssh-7.4p1/pam_ssh_agent_auth-0.10.3/pam_user_key_allowed2.c
bdb932
--- openssh-7.4p1/pam_ssh_agent_auth-0.10.3/pam_user_key_allowed2.c.fips	2016-12-23 16:37:49.185741531 +0100
bdb932
+++ openssh-7.4p1/pam_ssh_agent_auth-0.10.3/pam_user_key_allowed2.c	2016-12-23 16:37:49.300741586 +0100
6cf9b8
@@ -55,6 +55,7 @@
6cf9b8
 #include "secure_filename.h"
6cf9b8
 #include "uidswap.h"
6cf9b8
 #include <unistd.h>
6cf9b8
+#include <openssl/crypto.h>
6cf9b8
 
6cf9b8
 #include "identity.h"
6cf9b8
 
6cf9b8
@@ -104,7 +105,8 @@ pamsshagentauth_check_authkeys_file(FILE
6cf9b8
             found_key = 1;
6cf9b8
             logit("matching key found: file/command %s, line %lu", file,
6cf9b8
                                   linenum);
6cf9b8
-            fp = sshkey_fingerprint(found, SSH_DIGEST_MD5, SSH_FP_HEX);
6cf9b8
+            fp = sshkey_fingerprint(found, FIPS_mode() ? SSH_DIGEST_SHA1 : SSH_DIGEST_MD5,
6cf9b8
+				SSH_FP_HEX);
6cf9b8
             logit("Found matching %s key: %s",
6cf9b8
                                   key_type(found), fp);
6cf9b8
             free(fp);
6cf9b8
diff -up openssh-7.4p1/readconf.c.fips openssh-7.4p1/readconf.c
6cf9b8
--- openssh-7.4p1/readconf.c.fips	2016-12-23 16:37:49.274741574 +0100
6cf9b8
+++ openssh-7.4p1/readconf.c	2016-12-23 16:37:49.298741585 +0100
6cf9b8
@@ -2110,9 +2110,12 @@ fill_default_options(Options * options)
4df30a
 	}
3f5513
 	if (options->update_hostkeys == -1)
3f5513
 		options->update_hostkeys = 0;
3f5513
-	if (kex_assemble_names(KEX_CLIENT_ENCRYPT, &options->ciphers) != 0 ||
3f5513
-	    kex_assemble_names(KEX_CLIENT_MAC, &options->macs) != 0 ||
3f5513
-	    kex_assemble_names(KEX_CLIENT_KEX, &options->kex_algorithms) != 0 ||
3f5513
+	if (kex_assemble_names((FIPS_mode() ? KEX_FIPS_ENCRYPT
3f5513
+	        : KEX_CLIENT_ENCRYPT), &options->ciphers) != 0 ||
3f5513
+	    kex_assemble_names((FIPS_mode() ? KEX_FIPS_MAC
3f5513
+	        : KEX_CLIENT_MAC), &options->macs) != 0 ||
3f5513
+	    kex_assemble_names((FIPS_mode() ? KEX_DEFAULT_KEX_FIPS
3f5513
+	        : KEX_CLIENT_KEX), &options->kex_algorithms) != 0 ||
3f5513
 	    kex_assemble_names(KEX_DEFAULT_PK_ALG,
3f5513
 	    &options->hostbased_key_types) != 0 ||
3f5513
 	    kex_assemble_names(KEX_DEFAULT_PK_ALG,
6cf9b8
diff -up openssh-7.4p1/sandbox-seccomp-filter.c.fips openssh-7.4p1/sandbox-seccomp-filter.c
6cf9b8
--- openssh-7.4p1/sandbox-seccomp-filter.c.fips	2016-12-23 16:37:49.292741583 +0100
6cf9b8
+++ openssh-7.4p1/sandbox-seccomp-filter.c	2016-12-23 16:37:49.300741586 +0100
6cf9b8
@@ -118,6 +118,9 @@ static const struct sock_filter preauth_
6cf9b8
 #ifdef __NR_open
6cf9b8
 	SC_DENY(open, EACCES),
6cf9b8
 #endif
6cf9b8
+#ifdef __NR_socket
6cf9b8
+	SC_DENY(socket, EACCES),
6cf9b8
+#endif
6cf9b8
 #ifdef __NR_openat
6cf9b8
 	SC_DENY(openat, EACCES),
6cf9b8
 #endif
6cf9b8
diff -up openssh-7.4p1/servconf.c.fips openssh-7.4p1/servconf.c
6cf9b8
--- openssh-7.4p1/servconf.c.fips	2016-12-23 16:37:49.285741579 +0100
6cf9b8
+++ openssh-7.4p1/servconf.c	2016-12-23 16:37:49.299741586 +0100
6cf9b8
@@ -185,9 +185,12 @@ option_clear_or_none(const char *o)
13bf5b
 static void
13bf5b
 assemble_algorithms(ServerOptions *o)
13bf5b
 {
13bf5b
-	if (kex_assemble_names(KEX_SERVER_ENCRYPT, &o->ciphers) != 0 ||
13bf5b
-	    kex_assemble_names(KEX_SERVER_MAC, &o->macs) != 0 ||
13bf5b
-	    kex_assemble_names(KEX_SERVER_KEX, &o->kex_algorithms) != 0 ||
3f5513
+	if (kex_assemble_names((FIPS_mode() ? KEX_FIPS_ENCRYPT
13bf5b
+	        : KEX_SERVER_ENCRYPT), &o->ciphers) != 0 ||
3f5513
+	    kex_assemble_names((FIPS_mode() ? KEX_FIPS_MAC
13bf5b
+	        : KEX_SERVER_MAC), &o->macs) != 0 ||
3f5513
+	    kex_assemble_names((FIPS_mode() ? KEX_DEFAULT_KEX_FIPS
13bf5b
+	        : KEX_SERVER_KEX), &o->kex_algorithms) != 0 ||
3f5513
 	    kex_assemble_names(KEX_DEFAULT_PK_ALG,
13bf5b
 	    &o->hostkeyalgorithms) != 0 ||
3f5513
 	    kex_assemble_names(KEX_DEFAULT_PK_ALG,
6cf9b8
@@ -2390,8 +2393,10 @@ dump_config(ServerOptions *o)
535d34
 	/* string arguments */
535d34
 	dump_cfg_string(sPidFile, o->pid_file);
535d34
 	dump_cfg_string(sXAuthLocation, o->xauth_location);
535d34
-	dump_cfg_string(sCiphers, o->ciphers ? o->ciphers : KEX_SERVER_ENCRYPT);
535d34
-	dump_cfg_string(sMacs, o->macs ? o->macs : KEX_SERVER_MAC);
535d34
+	dump_cfg_string(sCiphers, o->ciphers ? o->ciphers : FIPS_mode()
535d34
+		? KEX_FIPS_ENCRYPT : KEX_SERVER_ENCRYPT);
535d34
+	dump_cfg_string(sMacs, o->macs ? o->macs : FIPS_mode()
535d34
+		? KEX_FIPS_MAC : KEX_SERVER_MAC);
535d34
 	dump_cfg_string(sBanner, o->banner != NULL ? o->banner : "none");
535d34
 	dump_cfg_string(sForceCommand, o->adm_forced_command);
535d34
 	dump_cfg_string(sChrootDirectory, o->chroot_directory);
6cf9b8
@@ -2406,8 +2411,8 @@ dump_config(ServerOptions *o)
535d34
 	dump_cfg_string(sAuthorizedPrincipalsCommand, o->authorized_principals_command);
535d34
 	dump_cfg_string(sAuthorizedPrincipalsCommandUser, o->authorized_principals_command_user);
535d34
 	dump_cfg_string(sHostKeyAgent, o->host_key_agent);
535d34
-	dump_cfg_string(sKexAlgorithms,
535d34
-	    o->kex_algorithms ? o->kex_algorithms : KEX_SERVER_KEX);
535d34
+	dump_cfg_string(sKexAlgorithms, o->kex_algorithms ? o->kex_algorithms :
535d34
+		FIPS_mode() ? KEX_DEFAULT_KEX_FIPS : KEX_SERVER_KEX);
535d34
 	dump_cfg_string(sHostbasedAcceptedKeyTypes, o->hostbased_key_types ?
535d34
 	    o->hostbased_key_types : KEX_DEFAULT_PK_ALG);
3f5513
 	dump_cfg_string(sHostKeyAlgorithms, o->hostkeyalgorithms ?
6cf9b8
diff -up openssh-7.4p1/ssh.c.fips openssh-7.4p1/ssh.c
6cf9b8
--- openssh-7.4p1/ssh.c.fips	2016-12-19 05:59:41.000000000 +0100
6cf9b8
+++ openssh-7.4p1/ssh.c	2016-12-23 16:37:49.299741586 +0100
6cf9b8
@@ -76,6 +76,8 @@
802815
 #include <openssl/evp.h>
802815
 #include <openssl/err.h>
190035
 #endif
802815
+#include <openssl/fips.h>
802815
+#include <fipscheck.h>
802815
 #include "openbsd-compat/openssl-compat.h"
802815
 #include "openbsd-compat/sys-queue.h"
802815
 
6cf9b8
@@ -530,6 +532,14 @@ main(int ac, char **av)
802815
 	sanitise_stdfd();
802815
 
802815
 	__progname = ssh_get_progname(av[0]);
802815
+        SSLeay_add_all_algorithms();
802815
+	if (access("/etc/system-fips", F_OK) == 0)
580f98
+		if (! FIPSCHECK_verify(NULL, NULL)){
802815
+			if (FIPS_mode())
802815
+				fatal("FIPS integrity verification test failed.");
802815
+			else
802815
+				logit("FIPS integrity verification test failed.");
580f98
+	}
802815
 
802815
 #ifndef HAVE_SETPROCTITLE
802815
 	/* Prepare for later setproctitle emulation */
6cf9b8
@@ -609,6 +619,9 @@ main(int ac, char **av)
5878eb
 	    "ACD:E:F:GI:J:KL:MNO:PQ:R:S:TVw:W:XYy")) != -1) {
802815
 		switch (opt) {
802815
 		case '1':
802815
+			if (FIPS_mode()) {
802815
+				fatal("Protocol 1 not allowed in the FIPS mode.");
802815
+			}
802815
 			options.protocol = SSH_PROTO_1;
802815
 			break;
802815
 		case '2':
6cf9b8
@@ -964,7 +977,6 @@ main(int ac, char **av)
802815
 	host_arg = xstrdup(host);
802815
 
190035
 #ifdef WITH_OPENSSL
802815
-	OpenSSL_add_all_algorithms();
802815
 	ERR_load_crypto_strings();
190035
 #endif
802815
 
6cf9b8
@@ -1175,6 +1187,10 @@ main(int ac, char **av)
802815
 
802815
 	seed_rng();
802815
 
802815
+	if (FIPS_mode()) {
802815
+		logit("FIPS mode initialized");
802815
+	}
802815
+
802815
 	if (options.user == NULL)
802815
 		options.user = xstrdup(pw->pw_name);
802815
 
6cf9b8
@@ -1263,6 +1279,12 @@ main(int ac, char **av)
802815
 
802815
 	timeout_ms = options.connection_timeout * 1000;
802815
 
802815
+	if (FIPS_mode()) {
802815
+		options.protocol &= SSH_PROTO_2;
802815
+		if (options.protocol == 0)
802815
+			fatal("Protocol 2 disabled by configuration but required in the FIPS mode.");
802815
+	}
802815
+
802815
 	/* Open a connection to the remote host. */
802815
 	if (ssh_connect(host, addrs, &hostaddr, options.port,
802815
 	    options.address_family, options.connection_attempts,
6cf9b8
diff -up openssh-7.4p1/sshconnect2.c.fips openssh-7.4p1/sshconnect2.c
6cf9b8
--- openssh-7.4p1/sshconnect2.c.fips	2016-12-23 16:37:49.275741574 +0100
6cf9b8
+++ openssh-7.4p1/sshconnect2.c	2016-12-23 16:37:49.299741586 +0100
3f5513
@@ -44,6 +44,8 @@
802815
 #include <vis.h>
802815
 #endif
802815
 
802815
+#include <openssl/fips.h>
802815
+
802815
 #include "openbsd-compat/sys-queue.h"
802815
 
802815
 #include "xmalloc.h"
6cf9b8
@@ -172,21 +174,26 @@ ssh_kex2(char *host, struct sockaddr *ho
802815
 
802815
 #ifdef GSSAPI
802815
 	if (options.gss_keyex) {
802815
-		/* Add the GSSAPI mechanisms currently supported on this 
802815
-		 * client to the key exchange algorithm proposal */
9a804f
-		orig = options.kex_algorithms;
132f8f
-
132f8f
-		if (options.gss_trust_dns)
b487a6
-			gss_host = (char *)get_canonical_hostname(active_state, 1);
132f8f
-		else
132f8f
-			gss_host = host;
132f8f
-
bc4ef0
-		gss = ssh_gssapi_client_mechanisms(gss_host,
bc4ef0
-		    options.gss_client_identity, options.gss_kex_algorithms);
132f8f
-		if (gss) {
132f8f
-			debug("Offering GSSAPI proposal: %s", gss);
9a804f
-			xasprintf(&options.kex_algorithms,
132f8f
-			    "%s,%s", gss, orig);
802815
+		if (FIPS_mode()) {
802815
+			logit("Disabling GSSAPIKeyExchange. Not usable in FIPS mode");
802815
+			options.gss_keyex = 0;
802815
+		} else {
802815
+			/* Add the GSSAPI mechanisms currently supported on this
802815
+			 * client to the key exchange algorithm proposal */
9a804f
+			orig = options.kex_algorithms;
132f8f
+
802815
+			if (options.gss_trust_dns)
b487a6
+				gss_host = (char *)get_canonical_hostname(active_state, 1);
802815
+			else
802815
+				gss_host = host;
132f8f
+
bc4ef0
+			gss = ssh_gssapi_client_mechanisms(gss_host,
bc4ef0
+			    options.gss_client_identity, options.gss_kex_algorithms);
802815
+			if (gss) {
802815
+				debug("Offering GSSAPI proposal: %s", gss);
9a804f
+				xasprintf(&options.kex_algorithms,
802815
+				    "%s,%s", gss, orig);
802815
+			}
802815
 		}
802815
 	}
802815
 #endif
6cf9b8
diff -up openssh-7.4p1/sshd.c.fips openssh-7.4p1/sshd.c
6cf9b8
--- openssh-7.4p1/sshd.c.fips	2016-12-23 16:37:49.293741583 +0100
6cf9b8
+++ openssh-7.4p1/sshd.c	2016-12-23 16:37:49.299741586 +0100
190035
@@ -66,6 +66,7 @@
190035
 #include <grp.h>
190035
 #include <pwd.h>
190035
 #include <signal.h>
190035
+#include <syslog.h>
190035
 #include <stdarg.h>
190035
 #include <stdio.h>
190035
 #include <stdlib.h>
132f8f
@@ -77,6 +78,8 @@
802815
 #include <openssl/dh.h>
802815
 #include <openssl/bn.h>
802815
 #include <openssl/rand.h>
802815
+#include <openssl/fips.h>
802815
+#include <fipscheck.h>
802815
 #include "openbsd-compat/openssl-compat.h"
190035
 #endif
802815
 
6cf9b8
@@ -1475,6 +1478,18 @@ main(int ac, char **av)
802815
 #endif
802815
 	__progname = ssh_get_progname(av[0]);
802815
 
802815
+        SSLeay_add_all_algorithms();
802815
+	if (access("/etc/system-fips", F_OK) == 0)
802815
+		if (! FIPSCHECK_verify(NULL, NULL)) {
802815
+			openlog(__progname, LOG_PID, LOG_AUTHPRIV);
802815
+			if (FIPS_mode()) {
802815
+				syslog(LOG_CRIT, "FIPS integrity verification test failed.");
802815
+				cleanup_exit(255);
802815
+			}
802815
+			else
802815
+				syslog(LOG_INFO, "FIPS integrity verification test failed.");
802815
+			closelog();
802815
+		}
802815
 	/* Save argv. Duplicate so setproctitle emulation doesn't clobber it */
802815
 	saved_argc = ac;
802815
 	rexec_argc = ac;
6cf9b8
@@ -1623,7 +1638,7 @@ main(int ac, char **av)
802815
 	else
802815
 		closefrom(REEXEC_DEVCRYPTO_RESERVED_FD);
802815
 
190035
-#ifdef WITH_OPENSSL
190035
+#if 0 /* FIPS */
190035
 	OpenSSL_add_all_algorithms();
190035
 #endif
190035
 
6cf9b8
@@ -1937,6 +1952,10 @@ main(int ac, char **av)
802815
 	/* Reinitialize the log (because of the fork above). */
802815
 	log_init(__progname, options.log_level, options.log_facility, log_stderr);
802815
 
802815
+	if (FIPS_mode()) {
802815
+		logit("FIPS mode initialized");
802815
+	}
802815
+
802815
 	/* Chdir to the root directory so that the current disk can be
802815
 	   unmounted if desired. */
802815
 	if (chdir("/") == -1)
6cf9b8
@@ -2309,10 +2328,14 @@ do_ssh2_kex(void)
802815
 	if (strlen(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS]) == 0)
802815
 		orig = NULL;
802815
 
802815
-	if (options.gss_keyex)
802815
-		gss = ssh_gssapi_server_mechanisms();
802815
-	else
802815
-		gss = NULL;
802815
+	if (options.gss_keyex) {
802815
+		if (FIPS_mode()) {
802815
+			logit("Disabling GSSAPIKeyExchange. Not usable in FIPS mode");
802815
+			options.gss_keyex = 0;
802815
+		} else {
802815
+			gss = ssh_gssapi_server_mechanisms();
802815
+		}
802815
+	}
802815
 
802815
 	if (gss && orig)
802815
 		xasprintf(&newstr, "%s,%s", gss, orig);
6cf9b8
diff -up openssh-7.4p1/sshkey.c.fips openssh-7.4p1/sshkey.c
6cf9b8
--- openssh-7.4p1/sshkey.c.fips	2016-12-23 16:37:49.293741583 +0100
6cf9b8
+++ openssh-7.4p1/sshkey.c	2016-12-23 16:37:49.300741586 +0100
6cf9b8
@@ -34,6 +34,7 @@
190035
 #include <openssl/evp.h>
190035
 #include <openssl/err.h>
190035
 #include <openssl/pem.h>
190035
+#include <openssl/fips.h>
132f8f
 #endif
190035
 
190035
 #include "crypto_api.h"
6cf9b8
@@ -56,6 +57,7 @@
84d398
 #include "digest.h"
84d398
 #define SSHKEY_INTERNAL
84d398
 #include "sshkey.h"
84d398
+#include "log.h"
84d398
 #include "match.h"
209c7a
 #include "xmalloc.h"
84d398
 
6cf9b8
@@ -1580,6 +1582,8 @@ rsa_generate_private_key(u_int bits, RSA
190035
 	}
190035
 	if (!BN_set_word(f4, RSA_F4) ||
190035
 	    !RSA_generate_key_ex(private, bits, f4, NULL)) {
190035
+			if (FIPS_mode())
190035
+				logit("%s: the key length might be unsupported by FIPS mode approved key generation method", __func__);
190035
 		ret = SSH_ERR_LIBCRYPTO_ERROR;
190035
 		goto out;
190035
 	}