Blame SOURCES/00146-hashlib-fips.patch

f992f7
diff --git a/Lib/hashlib.py b/Lib/hashlib.py
f992f7
index bbd06b9..72361bb 100644
f992f7
--- a/Lib/hashlib.py
f992f7
+++ b/Lib/hashlib.py
23b3e9
@@ -6,9 +6,12 @@
23b3e9
 
23b3e9
 __doc__ = """hashlib module - A common interface to many hash functions.
23b3e9
 
23b3e9
-new(name, string='') - returns a new hash object implementing the
23b3e9
-                       given hash function; initializing the hash
23b3e9
-                       using the given string data.
23b3e9
+new(name, string='', usedforsecurity=True)
23b3e9
+     - returns a new hash object implementing the given hash function;
23b3e9
+       initializing the hash using the given string data.
23b3e9
+
23b3e9
+       "usedforsecurity" is a non-standard extension for better supporting
23b3e9
+       FIPS-compliant environments (see below)
23b3e9
 
23b3e9
 Named constructor functions are also available, these are much faster
23b3e9
 than using new():
f992f7
@@ -25,6 +28,20 @@ the zlib module.
23b3e9
 Choose your hash function wisely.  Some have known collision weaknesses.
23b3e9
 sha384 and sha512 will be slow on 32 bit platforms.
23b3e9
 
23b3e9
+Our implementation of hashlib uses OpenSSL.
23b3e9
+
23b3e9
+OpenSSL has a "FIPS mode", which, if enabled, may restrict the available hashes
23b3e9
+to only those that are compliant with FIPS regulations.  For example, it may
23b3e9
+deny the use of MD5, on the grounds that this is not secure for uses such as
23b3e9
+authentication, system integrity checking, or digital signatures.   
23b3e9
+
23b3e9
+If you need to use such a hash for non-security purposes (such as indexing into
23b3e9
+a data structure for speed), you can override the keyword argument
23b3e9
+"usedforsecurity" from True to False to signify that your code is not relying
23b3e9
+on the hash for security purposes, and this will allow the hash to be usable
23b3e9
+even in FIPS mode.  This is not a standard feature of Python 2.7's hashlib, and
23b3e9
+is included here to better support FIPS mode.
23b3e9
+
23b3e9
 Hash objects have these methods:
23b3e9
  - update(arg): Update the hash object with the string arg. Repeated calls
23b3e9
                 are equivalent to a single call with the concatenation of all
f992f7
@@ -69,66 +86,31 @@ __all__ = __always_supported + ('new', 'algorithms_guaranteed',
f992f7
                                 'pbkdf2_hmac')
23b3e9
 
23b3e9
 
23b3e9
-def __get_builtin_constructor(name):
23b3e9
-    try:
23b3e9
-        if name in ('SHA1', 'sha1'):
23b3e9
-            import _sha
23b3e9
-            return _sha.new
23b3e9
-        elif name in ('MD5', 'md5'):
23b3e9
-            import _md5
23b3e9
-            return _md5.new
23b3e9
-        elif name in ('SHA256', 'sha256', 'SHA224', 'sha224'):
23b3e9
-            import _sha256
23b3e9
-            bs = name[3:]
23b3e9
-            if bs == '256':
23b3e9
-                return _sha256.sha256
23b3e9
-            elif bs == '224':
23b3e9
-                return _sha256.sha224
23b3e9
-        elif name in ('SHA512', 'sha512', 'SHA384', 'sha384'):
23b3e9
-            import _sha512
23b3e9
-            bs = name[3:]
23b3e9
-            if bs == '512':
23b3e9
-                return _sha512.sha512
23b3e9
-            elif bs == '384':
23b3e9
-                return _sha512.sha384
23b3e9
-    except ImportError:
23b3e9
-        pass  # no extension module, this hash is unsupported.
23b3e9
-
23b3e9
-    raise ValueError('unsupported hash type ' + name)
23b3e9
-
23b3e9
-
23b3e9
 def __get_openssl_constructor(name):
23b3e9
     try:
23b3e9
         f = getattr(_hashlib, 'openssl_' + name)
23b3e9
         # Allow the C module to raise ValueError.  The function will be
23b3e9
         # defined but the hash not actually available thanks to OpenSSL.
23b3e9
-        f()
23b3e9
+        #
23b3e9
+        # We pass "usedforsecurity=False" to disable FIPS-based restrictions:
23b3e9
+        # at this stage we're merely seeing if the function is callable,
23b3e9
+        # rather than using it for actual work.
23b3e9
+        f(usedforsecurity=False)
23b3e9
         # Use the C function directly (very fast)
23b3e9
         return f
23b3e9
     except (AttributeError, ValueError):
23b3e9
-        return __get_builtin_constructor(name)
23b3e9
+        raise
23b3e9
 
23b3e9
-
23b3e9
-def __py_new(name, string=''):
23b3e9
-    """new(name, string='') - Return a new hashing object using the named algorithm;
23b3e9
-    optionally initialized with a string.
23b3e9
-    """
23b3e9
-    return __get_builtin_constructor(name)(string)
23b3e9
-
23b3e9
-
23b3e9
-def __hash_new(name, string=''):
23b3e9
+def __hash_new(name, string='', usedforsecurity=True):
23b3e9
     """new(name, string='') - Return a new hashing object using the named algorithm;
23b3e9
     optionally initialized with a string.
23b3e9
+    Override 'usedforsecurity' to False when using for non-security purposes in
23b3e9
+    a FIPS environment
23b3e9
     """
23b3e9
     try:
23b3e9
-        return _hashlib.new(name, string)
23b3e9
+        return _hashlib.new(name, string, usedforsecurity)
23b3e9
     except ValueError:
23b3e9
-        # If the _hashlib module (OpenSSL) doesn't support the named
23b3e9
-        # hash, try using our builtin implementations.
23b3e9
-        # This allows for SHA224/256 and SHA384/512 support even though
23b3e9
-        # the OpenSSL library prior to 0.9.8 doesn't provide them.
23b3e9
-        return __get_builtin_constructor(name)(string)
23b3e9
-
23b3e9
+        raise
23b3e9
 
23b3e9
 try:
23b3e9
     import _hashlib
f992f7
@@ -137,8 +119,8 @@ try:
f992f7
     algorithms_available = algorithms_available.union(
f992f7
         _hashlib.openssl_md_meth_names)
23b3e9
 except ImportError:
23b3e9
-    new = __py_new
23b3e9
-    __get_hash = __get_builtin_constructor
23b3e9
+    # We don't build the legacy modules
23b3e9
+    raise
23b3e9
 
23b3e9
 for __func_name in __always_supported:
23b3e9
     # try them all, some may not work due to the OpenSSL
f992f7
@@ -218,4 +200,4 @@ except ImportError:
23b3e9
 
23b3e9
 # Cleanup locals()
23b3e9
 del __always_supported, __func_name, __get_hash
23b3e9
-del __py_new, __hash_new, __get_openssl_constructor
23b3e9
+del __hash_new, __get_openssl_constructor
f992f7
diff --git a/Lib/test/test_hashlib.py b/Lib/test/test_hashlib.py
b54164
index b8d6388..e0520ed 100644
f992f7
--- a/Lib/test/test_hashlib.py
f992f7
+++ b/Lib/test/test_hashlib.py
f992f7
@@ -34,6 +34,19 @@ def hexstr(s):
23b3e9
         r = r + h[(i >> 4) & 0xF] + h[i & 0xF]
23b3e9
     return r
23b3e9
 
23b3e9
+def openssl_enforces_fips():
23b3e9
+    # Use the "openssl" command (if present) to try to determine if the local
23b3e9
+    # OpenSSL is configured to enforce FIPS
23b3e9
+    from subprocess import Popen, PIPE
23b3e9
+    try:
23b3e9
+        p = Popen(['openssl', 'md5'],
23b3e9
+                  stdin=PIPE, stdout=PIPE, stderr=PIPE)
23b3e9
+    except OSError:
23b3e9
+        # "openssl" command not found
23b3e9
+        return False
23b3e9
+    stdout, stderr = p.communicate(input=b'abc')
23b3e9
+    return b'unknown cipher' in stderr
23b3e9
+OPENSSL_ENFORCES_FIPS = openssl_enforces_fips()
23b3e9
 
23b3e9
 class HashLibTestCase(unittest.TestCase):
23b3e9
     supported_hash_names = ( 'md5', 'MD5', 'sha1', 'SHA1',
f992f7
@@ -63,10 +76,10 @@ class HashLibTestCase(unittest.TestCase):
23b3e9
         # of hashlib.new given the algorithm name.
23b3e9
         for algorithm, constructors in self.constructors_to_test.items():
23b3e9
             constructors.add(getattr(hashlib, algorithm))
23b3e9
-            def _test_algorithm_via_hashlib_new(data=None, _alg=algorithm):
23b3e9
+            def _test_algorithm_via_hashlib_new(data=None, _alg=algorithm, usedforsecurity=True):
23b3e9
                 if data is None:
23b3e9
-                    return hashlib.new(_alg)
23b3e9
-                return hashlib.new(_alg, data)
23b3e9
+                    return hashlib.new(_alg, usedforsecurity=usedforsecurity)
23b3e9
+                return hashlib.new(_alg, data, usedforsecurity=usedforsecurity)
23b3e9
             constructors.add(_test_algorithm_via_hashlib_new)
23b3e9
 
23b3e9
         _hashlib = self._conditional_import_module('_hashlib')
f992f7
@@ -80,28 +93,13 @@ class HashLibTestCase(unittest.TestCase):
23b3e9
                 if constructor:
23b3e9
                     constructors.add(constructor)
23b3e9
 
23b3e9
-        _md5 = self._conditional_import_module('_md5')
23b3e9
-        if _md5:
23b3e9
-            self.constructors_to_test['md5'].add(_md5.new)
23b3e9
-        _sha = self._conditional_import_module('_sha')
23b3e9
-        if _sha:
23b3e9
-            self.constructors_to_test['sha1'].add(_sha.new)
23b3e9
-        _sha256 = self._conditional_import_module('_sha256')
23b3e9
-        if _sha256:
23b3e9
-            self.constructors_to_test['sha224'].add(_sha256.sha224)
23b3e9
-            self.constructors_to_test['sha256'].add(_sha256.sha256)
23b3e9
-        _sha512 = self._conditional_import_module('_sha512')
23b3e9
-        if _sha512:
23b3e9
-            self.constructors_to_test['sha384'].add(_sha512.sha384)
23b3e9
-            self.constructors_to_test['sha512'].add(_sha512.sha512)
23b3e9
-
23b3e9
         super(HashLibTestCase, self).__init__(*args, **kwargs)
23b3e9
 
23b3e9
     def test_hash_array(self):
23b3e9
         a = array.array("b", range(10))
23b3e9
         constructors = self.constructors_to_test.itervalues()
23b3e9
         for cons in itertools.chain.from_iterable(constructors):
23b3e9
-            c = cons(a)
23b3e9
+            c = cons(a, usedforsecurity=False)
23b3e9
             c.hexdigest()
23b3e9
 
23b3e9
     def test_algorithms_attribute(self):
f992f7
@@ -122,28 +120,9 @@ class HashLibTestCase(unittest.TestCase):
23b3e9
         self.assertRaises(ValueError, hashlib.new, 'spam spam spam spam spam')
23b3e9
         self.assertRaises(TypeError, hashlib.new, 1)
23b3e9
 
23b3e9
-    def test_get_builtin_constructor(self):
23b3e9
-        get_builtin_constructor = hashlib.__dict__[
23b3e9
-                '__get_builtin_constructor']
23b3e9
-        self.assertRaises(ValueError, get_builtin_constructor, 'test')
23b3e9
-        try:
23b3e9
-            import _md5
23b3e9
-        except ImportError:
23b3e9
-            pass
23b3e9
-        # This forces an ImportError for "import _md5" statements
23b3e9
-        sys.modules['_md5'] = None
23b3e9
-        try:
23b3e9
-            self.assertRaises(ValueError, get_builtin_constructor, 'md5')
23b3e9
-        finally:
23b3e9
-            if '_md5' in locals():
23b3e9
-                sys.modules['_md5'] = _md5
23b3e9
-            else:
23b3e9
-                del sys.modules['_md5']
23b3e9
-        self.assertRaises(TypeError, get_builtin_constructor, 3)
23b3e9
-
23b3e9
     def test_hexdigest(self):
23b3e9
         for name in self.supported_hash_names:
23b3e9
-            h = hashlib.new(name)
23b3e9
+            h = hashlib.new(name, usedforsecurity=False)
23b3e9
             self.assertTrue(hexstr(h.digest()) == h.hexdigest())
23b3e9
 
23b3e9
     def test_large_update(self):
f992f7
@@ -153,16 +132,16 @@ class HashLibTestCase(unittest.TestCase):
23b3e9
         abcs = aas + bees + cees
23b3e9
 
23b3e9
         for name in self.supported_hash_names:
23b3e9
-            m1 = hashlib.new(name)
23b3e9
+            m1 = hashlib.new(name, usedforsecurity=False)
23b3e9
             m1.update(aas)
23b3e9
             m1.update(bees)
23b3e9
             m1.update(cees)
23b3e9
 
23b3e9
-            m2 = hashlib.new(name)
23b3e9
+            m2 = hashlib.new(name, usedforsecurity=False)
23b3e9
             m2.update(abcs)
23b3e9
             self.assertEqual(m1.digest(), m2.digest(), name+' update problem.')
23b3e9
 
23b3e9
-            m3 = hashlib.new(name, abcs)
23b3e9
+            m3 = hashlib.new(name, abcs, usedforsecurity=False)
23b3e9
             self.assertEqual(m1.digest(), m3.digest(), name+' new problem.')
23b3e9
 
23b3e9
     def check(self, name, data, digest):
f992f7
@@ -170,7 +149,7 @@ class HashLibTestCase(unittest.TestCase):
23b3e9
         # 2 is for hashlib.name(...) and hashlib.new(name, ...)
23b3e9
         self.assertGreaterEqual(len(constructors), 2)
23b3e9
         for hash_object_constructor in constructors:
23b3e9
-            computed = hash_object_constructor(data).hexdigest()
23b3e9
+            computed = hash_object_constructor(data, usedforsecurity=False).hexdigest()
23b3e9
             self.assertEqual(
23b3e9
                     computed, digest,
23b3e9
                     "Hash algorithm %s constructed using %s returned hexdigest"
f992f7
@@ -195,7 +174,8 @@ class HashLibTestCase(unittest.TestCase):
23b3e9
 
23b3e9
     def check_unicode(self, algorithm_name):
23b3e9
         # Unicode objects are not allowed as input.
23b3e9
-        expected = hashlib.new(algorithm_name, str(u'spam')).hexdigest()
23b3e9
+        expected = hashlib.new(algorithm_name, str(u'spam'),
23b3e9
+                               usedforsecurity=False).hexdigest()
23b3e9
         self.check(algorithm_name, u'spam', expected)
23b3e9
 
23b3e9
     def test_unicode(self):
f992f7
@@ -394,6 +374,70 @@ class HashLibTestCase(unittest.TestCase):
23b3e9
         self.assertEqual(expected_hash, hasher.hexdigest())
23b3e9
 
21eb11
 
23b3e9
+    def test_issue9146(self):
23b3e9
+        # Ensure that various ways to use "MD5" from "hashlib" don't segfault:
23b3e9
+        m = hashlib.md5(usedforsecurity=False)
23b3e9
+        m.update(b'abc\n')
23b3e9
+        self.assertEquals(m.hexdigest(), "0bee89b07a248e27c83fc3d5951213c1")
23b3e9
+        
23b3e9
+        m = hashlib.new('md5', usedforsecurity=False)
23b3e9
+        m.update(b'abc\n')
23b3e9
+        self.assertEquals(m.hexdigest(), "0bee89b07a248e27c83fc3d5951213c1")
23b3e9
+        
23b3e9
+        m = hashlib.md5(b'abc\n', usedforsecurity=False)
23b3e9
+        self.assertEquals(m.hexdigest(), "0bee89b07a248e27c83fc3d5951213c1")
23b3e9
+        
23b3e9
+        m = hashlib.new('md5', b'abc\n', usedforsecurity=False)
23b3e9
+        self.assertEquals(m.hexdigest(), "0bee89b07a248e27c83fc3d5951213c1")
23b3e9
+
23b3e9
+    def assertRaisesUnknownCipher(self, callable_obj=None, *args, **kwargs):
23b3e9
+        try:
23b3e9
+            callable_obj(*args, **kwargs)
23b3e9
+        except ValueError, e:
23b3e9
+            if not e.args[0].endswith('unknown cipher'):
23b3e9
+                self.fail('Incorrect exception raised')
23b3e9
+        else:
23b3e9
+            self.fail('Exception was not raised')
23b3e9
+
23b3e9
+    @unittest.skipUnless(OPENSSL_ENFORCES_FIPS,
23b3e9
+                         'FIPS enforcement required for this test.')
23b3e9
+    def test_hashlib_fips_mode(self):        
23b3e9
+        # Ensure that we raise a ValueError on vanilla attempts to use MD5
23b3e9
+        # in hashlib in a FIPS-enforced setting:
23b3e9
+        self.assertRaisesUnknownCipher(hashlib.md5)
23b3e9
+        self.assertRaisesUnknownCipher(hashlib.new, 'md5')
23b3e9
+
23b3e9
+    @unittest.skipUnless(OPENSSL_ENFORCES_FIPS,
23b3e9
+                         'FIPS enforcement required for this test.')
23b3e9
+    def test_hashopenssl_fips_mode(self):
23b3e9
+        # Verify the _hashlib module's handling of md5:
23b3e9
+        import _hashlib
23b3e9
+
23b3e9
+        assert hasattr(_hashlib, 'openssl_md5')
23b3e9
+
23b3e9
+        # Ensure that _hashlib raises a ValueError on vanilla attempts to
23b3e9
+        # use MD5 in a FIPS-enforced setting:
23b3e9
+        self.assertRaisesUnknownCipher(_hashlib.openssl_md5)
23b3e9
+        self.assertRaisesUnknownCipher(_hashlib.new, 'md5')
23b3e9
+
23b3e9
+        # Ensure that in such a setting we can whitelist a callsite with
23b3e9
+        # usedforsecurity=False and have it succeed:
23b3e9
+        m = _hashlib.openssl_md5(usedforsecurity=False)
23b3e9
+        m.update('abc\n')
23b3e9
+        self.assertEquals(m.hexdigest(), "0bee89b07a248e27c83fc3d5951213c1")
23b3e9
+        
23b3e9
+        m = _hashlib.new('md5', usedforsecurity=False)
23b3e9
+        m.update('abc\n')
23b3e9
+        self.assertEquals(m.hexdigest(), "0bee89b07a248e27c83fc3d5951213c1")
23b3e9
+        
23b3e9
+        m = _hashlib.openssl_md5('abc\n', usedforsecurity=False)
23b3e9
+        self.assertEquals(m.hexdigest(), "0bee89b07a248e27c83fc3d5951213c1")
23b3e9
+        
23b3e9
+        m = _hashlib.new('md5', 'abc\n', usedforsecurity=False)
23b3e9
+        self.assertEquals(m.hexdigest(), "0bee89b07a248e27c83fc3d5951213c1")
23b3e9
+        
23b3e9
+
23b3e9
+
21eb11
 class KDFTests(unittest.TestCase):
21eb11
     pbkdf2_test_vectors = [
21eb11
         (b'password', b'salt', 1, None),
f992f7
diff --git a/Modules/Setup.dist b/Modules/Setup.dist
cd3c85
index b8f9702..00e5f18 100644
f992f7
--- a/Modules/Setup.dist
f992f7
+++ b/Modules/Setup.dist
f992f7
@@ -252,14 +252,14 @@ imageop imageop.c	# Operations on images
21eb11
 # Message-Digest Algorithm, described in RFC 1321.  The necessary files
21eb11
 # md5.c and md5.h are included here.
21eb11
 
21eb11
-_md5 md5module.c md5.c
21eb11
+#_md5 md5module.c md5.c
21eb11
 
21eb11
 
21eb11
 # The _sha module implements the SHA checksum algorithms.
21eb11
 # (NIST's Secure Hash Algorithms.)
21eb11
-_sha shamodule.c
21eb11
-_sha256 sha256module.c
21eb11
-_sha512 sha512module.c
21eb11
+#_sha shamodule.c
21eb11
+#_sha256 sha256module.c
21eb11
+#_sha512 sha512module.c
21eb11
 
21eb11
 
21eb11
 # SGI IRIX specific modules -- off by default.
f992f7
diff --git a/Modules/_hashopenssl.c b/Modules/_hashopenssl.c
cd3c85
index 5df08e5..382ab9a 100644
f992f7
--- a/Modules/_hashopenssl.c
f992f7
+++ b/Modules/_hashopenssl.c
f992f7
@@ -36,6 +36,7 @@
23b3e9
 #endif
23b3e9
 
23b3e9
 /* EVP is the preferred interface to hashing in OpenSSL */
23b3e9
+#include <openssl/ssl.h>
23b3e9
 #include <openssl/evp.h>
21eb11
 #include <openssl/err.h>
f992f7
 /* We use the object interface to discover what hashes OpenSSL supports. */
f992f7
@@ -81,9 +82,19 @@ typedef struct {
23b3e9
 static PyTypeObject EVPtype;
23b3e9
 
f992f7
 
f992f7
-#define DEFINE_CONSTS_FOR_NEW(Name)  \
f992f7
-    static PyObject *CONST_ ## Name ## _name_obj = NULL; \
f992f7
-    static EVP_MD_CTX *CONST_new_ ## Name ## _ctx_p = NULL;
23b3e9
+/* Struct to hold all the cached information we need on a specific algorithm.
23b3e9
+   We have one of these per algorithm */
23b3e9
+typedef struct {
23b3e9
+    PyObject *name_obj;
23b3e9
+    EVP_MD_CTX ctxs[2];
23b3e9
+    /* ctx_ptrs will point to ctxs unless an error occurred, when it will
23b3e9
+       be NULL: */
23b3e9
+    EVP_MD_CTX *ctx_ptrs[2];
23b3e9
+    PyObject *error_msgs[2];
23b3e9
+} EVPCachedInfo;
f992f7
+ 
23b3e9
+#define DEFINE_CONSTS_FOR_NEW(Name) \
23b3e9
+    static EVPCachedInfo cached_info_ ##Name;
23b3e9
 
23b3e9
 DEFINE_CONSTS_FOR_NEW(md5)
23b3e9
 DEFINE_CONSTS_FOR_NEW(sha1)
cd3c85
@@ -167,6 +178,48 @@ EVP_hash(EVPobject *self, const void *vp, Py_ssize_t len)
23b3e9
     }
23b3e9
 }
23b3e9
 
23b3e9
+static void
23b3e9
+mc_ctx_init(EVP_MD_CTX *ctx, int usedforsecurity)
23b3e9
+{
23b3e9
+    EVP_MD_CTX_init(ctx);
23b3e9
+
23b3e9
+    /*
23b3e9
+      If the user has declared that this digest is being used in a
23b3e9
+      non-security role (e.g. indexing into a data structure), set
23b3e9
+      the exception flag for openssl to allow it
23b3e9
+    */
23b3e9
+    if (!usedforsecurity) {
23b3e9
+#ifdef EVP_MD_CTX_FLAG_NON_FIPS_ALLOW
23b3e9
+        EVP_MD_CTX_set_flags(ctx,
23b3e9
+                             EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
23b3e9
+#endif
23b3e9
+    }
23b3e9
+}
23b3e9
+
23b3e9
+/* Get an error msg for the last error as a PyObject */
23b3e9
+static PyObject *
23b3e9
+error_msg_for_last_error(void)
23b3e9
+{
23b3e9
+    char *errstr;
23b3e9
+
23b3e9
+    errstr = ERR_error_string(ERR_peek_last_error(), NULL);
23b3e9
+    ERR_clear_error();
23b3e9
+
23b3e9
+    return PyString_FromString(errstr); /* Can be NULL */
23b3e9
+}
23b3e9
+
23b3e9
+static void
23b3e9
+set_evp_exception(void)
23b3e9
+{
23b3e9
+    char *errstr;
23b3e9
+
23b3e9
+    errstr = ERR_error_string(ERR_peek_last_error(), NULL);
23b3e9
+    ERR_clear_error();
23b3e9
+
23b3e9
+    PyErr_SetString(PyExc_ValueError, errstr);
23b3e9
+}
23b3e9
+
23b3e9
+
23b3e9
 /* Internal methods for a hash object */
23b3e9
 
23b3e9
 static void
cd3c85
@@ -388,14 +441,15 @@ EVP_repr(PyObject *self)
23b3e9
 static int
23b3e9
 EVP_tp_init(EVPobject *self, PyObject *args, PyObject *kwds)
23b3e9
 {
23b3e9
-    static char *kwlist[] = {"name", "string", NULL};
23b3e9
+    static char *kwlist[] = {"name", "string", "usedforsecurity", NULL};
23b3e9
     PyObject *name_obj = NULL;
23b3e9
+    int usedforsecurity = 1;
23b3e9
     Py_buffer view = { 0 };
23b3e9
     char *nameStr;
23b3e9
     const EVP_MD *digest;
23b3e9
 
23b3e9
-    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|s*:HASH", kwlist,
23b3e9
-                                     &name_obj, &view)) {
23b3e9
+    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|s*i:HASH", kwlist,
23b3e9
+                                     &name_obj, &view, &usedforsecurity)) {
23b3e9
         return -1;
23b3e9
     }
23b3e9
 
cd3c85
@@ -411,7 +465,12 @@ EVP_tp_init(EVPobject *self, PyObject *args, PyObject *kwds)
23b3e9
         PyBuffer_Release(&view);
23b3e9
         return -1;
23b3e9
     }
f992f7
-    EVP_DigestInit(self->ctx, digest);
f992f7
+    mc_ctx_init(self->ctx, usedforsecurity);
f992f7
+    if (!EVP_DigestInit_ex(self->ctx, digest, NULL)) {
23b3e9
+        set_evp_exception();
23b3e9
+        PyBuffer_Release(&view);
23b3e9
+        return -1;
23b3e9
+    }
23b3e9
 
23b3e9
     self->name = name_obj;
23b3e9
     Py_INCREF(self->name);
cd3c85
@@ -495,7 +554,8 @@ static PyTypeObject EVPtype = {
23b3e9
 static PyObject *
23b3e9
 EVPnew(PyObject *name_obj,
23b3e9
        const EVP_MD *digest, const EVP_MD_CTX *initial_ctx,
23b3e9
-       const unsigned char *cp, Py_ssize_t len)
23b3e9
+       const unsigned char *cp, Py_ssize_t len,
23b3e9
+       int usedforsecurity)
23b3e9
 {
23b3e9
     EVPobject *self;
23b3e9
 
cd3c85
@@ -510,7 +570,12 @@ EVPnew(PyObject *name_obj,
23b3e9
     if (initial_ctx) {
f992f7
         EVP_MD_CTX_copy(self->ctx, initial_ctx);
23b3e9
     } else {
f992f7
-        EVP_DigestInit(self->ctx, digest);
f992f7
+        mc_ctx_init(self->ctx, usedforsecurity);
f992f7
+        if (!EVP_DigestInit_ex(self->ctx, digest, NULL)) {
23b3e9
+            set_evp_exception();
23b3e9
+            Py_DECREF(self);
23b3e9
+            return NULL;
23b3e9
+        }
23b3e9
     }
23b3e9
 
23b3e9
     if (cp && len) {
cd3c85
@@ -534,20 +599,28 @@ PyDoc_STRVAR(EVP_new__doc__,
23b3e9
 An optional string argument may be provided and will be\n\
23b3e9
 automatically hashed.\n\
23b3e9
 \n\
23b3e9
-The MD5 and SHA1 algorithms are always supported.\n");
23b3e9
+The MD5 and SHA1 algorithms are always supported.\n\
23b3e9
+\n\
23b3e9
+An optional \"usedforsecurity=True\" keyword argument is provided for use in\n\
23b3e9
+environments that enforce FIPS-based restrictions.  Some implementations of\n\
23b3e9
+OpenSSL can be configured to prevent the usage of non-secure algorithms (such\n\
23b3e9
+as MD5).  If you have a non-security use for these algorithms (e.g. a hash\n\
23b3e9
+table), you can override this argument by marking the callsite as\n\
23b3e9
+\"usedforsecurity=False\".");
23b3e9
 
23b3e9
 static PyObject *
23b3e9
 EVP_new(PyObject *self, PyObject *args, PyObject *kwdict)
23b3e9
 {
23b3e9
-    static char *kwlist[] = {"name", "string", NULL};
23b3e9
+    static char *kwlist[] = {"name", "string", "usedforsecurity", NULL};
23b3e9
     PyObject *name_obj = NULL;
23b3e9
     Py_buffer view = { 0 };
23b3e9
     PyObject *ret_obj;
23b3e9
     char *name;
23b3e9
     const EVP_MD *digest;
23b3e9
+    int usedforsecurity = 1;
23b3e9
 
23b3e9
-    if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O|s*:new", kwlist,
23b3e9
-                                     &name_obj, &view)) {
23b3e9
+    if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O|s*i:new", kwlist,
23b3e9
+                                     &name_obj, &view, &usedforsecurity)) {
23b3e9
         return NULL;
23b3e9
     }
23b3e9
 
cd3c85
@@ -560,7 +633,7 @@ EVP_new(PyObject *self, PyObject *args, PyObject *kwdict)
23b3e9
     digest = EVP_get_digestbyname(name);
23b3e9
 
23b3e9
     ret_obj = EVPnew(name_obj, digest, NULL, (unsigned char*)view.buf,
23b3e9
-                     view.len);
23b3e9
+                     view.len, usedforsecurity);
23b3e9
     PyBuffer_Release(&view);
23b3e9
 
23b3e9
     return ret_obj;
cd3c85
@@ -820,51 +893,111 @@ generate_hash_name_list(void)
f992f7
 
23b3e9
 
23b3e9
 /*
23b3e9
- *  This macro generates constructor function definitions for specific
23b3e9
- *  hash algorithms.  These constructors are much faster than calling
f992f7
- *  the generic one passing it a python string and are noticeably
23b3e9
- *  faster than calling a python new() wrapper.  Thats important for
23b3e9
+ *  This macro and function generates a family of constructor function
23b3e9
+ *  definitions for specific hash algorithms.  These constructors are much
23b3e9
+ *  faster than calling the generic one passing it a python string and are
23b3e9
+ *  noticably faster than calling a python new() wrapper.  That's important for
23b3e9
  *  code that wants to make hashes of a bunch of small strings.
23b3e9
  */
23b3e9
 #define GEN_CONSTRUCTOR(NAME)  \
23b3e9
     static PyObject * \
23b3e9
-    EVP_new_ ## NAME (PyObject *self, PyObject *args) \
21eb11
+    EVP_new_ ## NAME (PyObject *self, PyObject *args, PyObject *kwdict) \
23b3e9
     { \
23b3e9
-        Py_buffer view = { 0 }; \
23b3e9
-        PyObject *ret_obj; \
23b3e9
-     \
23b3e9
-        if (!PyArg_ParseTuple(args, "|s*:" #NAME , &view)) { \
23b3e9
-            return NULL; \
23b3e9
-        } \
23b3e9
-     \
23b3e9
-        ret_obj = EVPnew( \
23b3e9
-                    CONST_ ## NAME ## _name_obj, \
23b3e9
-                    NULL, \
23b3e9
-                    CONST_new_ ## NAME ## _ctx_p, \
23b3e9
-                    (unsigned char*)view.buf, view.len); \
23b3e9
-        PyBuffer_Release(&view); \
23b3e9
-        return ret_obj; \
21eb11
+        return implement_specific_EVP_new(self, args, kwdict,       \
21eb11
+                                          "|s*i:" #NAME,            \
21eb11
+                                          &cached_info_ ## NAME );  \
23b3e9
     }
23b3e9
 
23b3e9
+static PyObject *
23b3e9
+implement_specific_EVP_new(PyObject *self, PyObject *args, PyObject *kwdict,
23b3e9
+                           const char *format,
23b3e9
+                           EVPCachedInfo *cached_info)
23b3e9
+{
21eb11
+    static char *kwlist[] = {"string", "usedforsecurity", NULL};
23b3e9
+    Py_buffer view = { 0 };
23b3e9
+    int usedforsecurity = 1;
23b3e9
+    int idx;
23b3e9
+    PyObject *ret_obj = NULL;
23b3e9
+
23b3e9
+    assert(cached_info);
23b3e9
+
23b3e9
+    if (!PyArg_ParseTupleAndKeywords(args, kwdict, format, kwlist,
23b3e9
+                                     &view, &usedforsecurity)) {
23b3e9
+        return NULL;
23b3e9
+    }
23b3e9
+
23b3e9
+    idx = usedforsecurity ? 1 : 0;
23b3e9
+
23b3e9
+    /*
23b3e9
+     * If an error occurred during creation of the global content, the ctx_ptr
23b3e9
+     * will be NULL, and the error_msg will hopefully be non-NULL:
23b3e9
+     */
23b3e9
+    if (cached_info->ctx_ptrs[idx]) {
23b3e9
+        /* We successfully initialized this context; copy it: */
23b3e9
+        ret_obj = EVPnew(cached_info->name_obj,
23b3e9
+                         NULL,
23b3e9
+                         cached_info->ctx_ptrs[idx],
23b3e9
+                         (unsigned char*)view.buf, view.len,
23b3e9
+                         usedforsecurity);
23b3e9
+    } else {
23b3e9
+        /* Some kind of error happened initializing the global context for
23b3e9
+           this (digest, usedforsecurity) pair.
23b3e9
+           Raise an exception with the saved error message: */
23b3e9
+        if (cached_info->error_msgs[idx]) {
23b3e9
+            PyErr_SetObject(PyExc_ValueError, cached_info->error_msgs[idx]);
23b3e9
+        } else {
23b3e9
+            PyErr_SetString(PyExc_ValueError, "Error initializing hash");
23b3e9
+        }
23b3e9
+    }
23b3e9
+
23b3e9
+    PyBuffer_Release(&view);
23b3e9
+
23b3e9
+    return ret_obj;
23b3e9
+}
23b3e9
+
23b3e9
 /* a PyMethodDef structure for the constructor */
23b3e9
 #define CONSTRUCTOR_METH_DEF(NAME)  \
23b3e9
-    {"openssl_" #NAME, (PyCFunction)EVP_new_ ## NAME, METH_VARARGS, \
23b3e9
+    {"openssl_" #NAME, (PyCFunction)EVP_new_ ## NAME, \
23b3e9
+        METH_VARARGS |METH_KEYWORDS, \
23b3e9
         PyDoc_STR("Returns a " #NAME \
23b3e9
                   " hash object; optionally initialized with a string") \
23b3e9
     }
23b3e9
 
23b3e9
-/* used in the init function to setup a constructor: initialize OpenSSL
23b3e9
-   constructor constants if they haven't been initialized already.  */
23b3e9
-#define INIT_CONSTRUCTOR_CONSTANTS(NAME)  do { \
23b3e9
-    if (CONST_ ## NAME ## _name_obj == NULL) { \
23b3e9
-    CONST_ ## NAME ## _name_obj = PyString_FromString(#NAME); \
23b3e9
-        if (EVP_get_digestbyname(#NAME)) { \
f992f7
-            CONST_new_ ## NAME ## _ctx_p = EVP_MD_CTX_new(); \
23b3e9
-            EVP_DigestInit(CONST_new_ ## NAME ## _ctx_p, EVP_get_digestbyname(#NAME)); \
23b3e9
-        } \
23b3e9
-    } \
23b3e9
+/*
23b3e9
+  Macro/function pair to set up the constructors.
23b3e9
+
23b3e9
+  Try to initialize a context for each hash twice, once with
23b3e9
+  EVP_MD_CTX_FLAG_NON_FIPS_ALLOW and once without.
21eb11
+
23b3e9
+  Any that have errors during initialization will end up wit a NULL ctx_ptrs
23b3e9
+  entry, and err_msgs will be set (unless we're very low on memory)
23b3e9
+*/
23b3e9
+#define INIT_CONSTRUCTOR_CONSTANTS(NAME)  do {    \
23b3e9
+    init_constructor_constant(&cached_info_ ## NAME, #NAME); \
23b3e9
 } while (0);
23b3e9
 
23b3e9
+static void
23b3e9
+init_constructor_constant(EVPCachedInfo *cached_info, const char *name)
23b3e9
+{
23b3e9
+    assert(cached_info);
23b3e9
+    cached_info->name_obj = PyString_FromString(name);
23b3e9
+    if (EVP_get_digestbyname(name)) {
23b3e9
+        int i;
23b3e9
+        for (i=0; i<2; i++) {
23b3e9
+            mc_ctx_init(&cached_info->ctxs[i], i);
23b3e9
+            if (EVP_DigestInit_ex(&cached_info->ctxs[i],
23b3e9
+                                  EVP_get_digestbyname(name), NULL)) {
23b3e9
+                /* Success: */
23b3e9
+                cached_info->ctx_ptrs[i] = &cached_info->ctxs[i];
23b3e9
+            } else {
23b3e9
+                /* Failure: */
23b3e9
+                cached_info->ctx_ptrs[i] = NULL;
23b3e9
+                cached_info->error_msgs[i] = error_msg_for_last_error();
23b3e9
+            }
23b3e9
+        }
23b3e9
+    }
23b3e9
+}
23b3e9
+
23b3e9
 GEN_CONSTRUCTOR(md5)
23b3e9
 GEN_CONSTRUCTOR(sha1)
23b3e9
 #ifdef _OPENSSL_SUPPORTS_SHA2
cd3c85
@@ -903,6 +1036,8 @@ init_hashlib(void)
23b3e9
 
cd3c85
 #if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
b54164
     /* Load all digest algorithms and initialize cpuid */
23b3e9
+    SSL_load_error_strings();
23b3e9
+    SSL_library_init();
b54164
     OPENSSL_add_all_algorithms_noconf();
f992f7
     ERR_load_crypto_strings();
b54164
 #endif
f992f7
diff --git a/setup.py b/setup.py
cd3c85
index 77e3a40..b401ab8 100644
f992f7
--- a/setup.py
f992f7
+++ b/setup.py
cd3c85
@@ -920,21 +920,6 @@ class PyBuildExt(build_ext):
f992f7
                 print ("warning: openssl 0x%08x is too old for _hashlib" %
f992f7
                        openssl_ver)
f992f7
                 missing.append('_hashlib')
f992f7
-        if COMPILED_WITH_PYDEBUG or not have_usable_openssl:
f992f7
-            # The _sha module implements the SHA1 hash algorithm.
f992f7
-            exts.append( Extension('_sha', ['shamodule.c']) )
f992f7
-            # The _md5 module implements the RSA Data Security, Inc. MD5
f992f7
-            # Message-Digest Algorithm, described in RFC 1321.  The
f992f7
-            # necessary files md5.c and md5.h are included here.
f992f7
-            exts.append( Extension('_md5',
f992f7
-                            sources = ['md5module.c', 'md5.c'],
f992f7
-                            depends = ['md5.h']) )
f992f7
-
f992f7
-        min_sha2_openssl_ver = 0x00908000
f992f7
-        if COMPILED_WITH_PYDEBUG or openssl_ver < min_sha2_openssl_ver:
f992f7
-            # OpenSSL doesn't do these until 0.9.8 so we'll bring our own hash
f992f7
-            exts.append( Extension('_sha256', ['sha256module.c']) )
f992f7
-            exts.append( Extension('_sha512', ['sha512module.c']) )
f992f7
 
f992f7
         # Modules that provide persistent dictionary-like semantics.  You will
f992f7
         # probably want to arrange for at least one of them to be available on