040c39b
diff -up libgcrypt-1.6.2/cipher/md.c.fips-ctor libgcrypt-1.6.2/cipher/md.c
040c39b
--- libgcrypt-1.6.2/cipher/md.c.fips-ctor	2014-08-21 14:50:39.000000000 +0200
eea2829
+++ libgcrypt-1.6.2/cipher/md.c	2015-02-25 13:57:21.175704866 +0100
040c39b
@@ -413,11 +413,8 @@ md_enable (gcry_md_hd_t hd, int algorith
040c39b
 
040c39b
   if (!err && algorithm == GCRY_MD_MD5 && fips_mode ())
040c39b
     {
040c39b
-      _gcry_inactivate_fips_mode ("MD5 used");
040c39b
       if (_gcry_enforced_fips_mode () )
040c39b
         {
040c39b
-          /* We should never get to here because we do not register
040c39b
-             MD5 in enforced fips mode. But better throw an error.  */
040c39b
           err = GPG_ERR_DIGEST_ALGO;
040c39b
         }
040c39b
     }
040c39b
diff -up libgcrypt-1.6.2/src/global.c.fips-ctor libgcrypt-1.6.2/src/global.c
eea2829
--- libgcrypt-1.6.2/src/global.c.fips-ctor	2015-02-25 13:57:21.174704842 +0100
eea2829
+++ libgcrypt-1.6.2/src/global.c	2015-02-25 14:03:07.066864208 +0100
eea2829
@@ -132,6 +132,34 @@ global_init (void)
040c39b
 }
040c39b
 
040c39b
 
040c39b
+#ifndef FIPS_MODULE_PATH
040c39b
+#define FIPS_MODULE_PATH "/etc/system-fips"
040c39b
+#endif
040c39b
+
040c39b
+void __attribute__ ((constructor)) _gcry_global_constructor (void)
040c39b
+{
040c39b
+  int rv;
040c39b
+
040c39b
+  rv = access (FIPS_MODULE_PATH, F_OK);
040c39b
+  if (rv < 0 && errno != ENOENT)
040c39b
+    rv = 0;
040c39b
+
040c39b
+  if (!rv)
040c39b
+    {
eea2829
+      int no_secmem_save;
eea2829
+
eea2829
+      /* it should be always 0 at this point but let's keep on the safe side */
eea2829
+      no_secmem_save = no_secure_memory;
eea2829
+      no_secure_memory = 1;
040c39b
+      /* force selftests */
040c39b
+      global_init ();
040c39b
+      if (fips_mode ())
040c39b
+         _gcry_random_initialize (1);
040c39b
+      _gcry_fips_run_selftests (0);
eea2829
+      no_secure_memory = no_secmem_save;
040c39b
+    }
040c39b
+}
040c39b
+
040c39b
 /* This function is called by the macro fips_is_operational and makes
040c39b
    sure that the minimal initialization has been done.  This is far
040c39b
    from a perfect solution and hides problems with an improper
da1d908
@@ -542,8 +570,7 @@ _gcry_vcontrol (enum gcry_ctl_cmds cmd,
eea2829
 
eea2829
     case GCRYCTL_FIPS_MODE_P:
eea2829
       if (fips_mode ()
eea2829
-          && !_gcry_is_fips_mode_inactive ()
eea2829
-          && !no_secure_memory)
eea2829
+          && !_gcry_is_fips_mode_inactive ())
da1d908
 	rc = GPG_ERR_GENERAL; /* Used as TRUE value */
eea2829
       break;
eea2829
 
eea2829
@@ -635,7 +662,7 @@ _gcry_vcontrol (enum gcry_ctl_cmds cmd,
040c39b
       break;
040c39b
 
040c39b
     case GCRYCTL_SET_ENFORCED_FIPS_FLAG:
040c39b
-      if (!any_init_done)
040c39b
+      if (fips_mode ())
040c39b
         {
040c39b
           /* Not yet initialized at all.  Set the enforced fips mode flag */
040c39b
           _gcry_set_preferred_rng_type (0);