Blame SOURCES/00146-hashlib-fips.patch

dd3773
diff --git a/Lib/hashlib.py b/Lib/hashlib.py
dd3773
index 98d2d79..fa6cdbc 100644
dd3773
--- a/Lib/hashlib.py
dd3773
+++ b/Lib/hashlib.py
dd3773
@@ -24,6 +24,16 @@ the zlib module.
dd3773
 Choose your hash function wisely.  Some have known collision weaknesses.
dd3773
 sha384 and sha512 will be slow on 32 bit platforms.
dd3773
 
dd3773
+If the underlying implementation supports "FIPS mode", and this is enabled, it
dd3773
+may restrict the available hashes to only those that are compliant with FIPS
dd3773
+regulations.  For example, it may deny the use of MD5, on the grounds that this
dd3773
+is not secure for uses such as authentication, system integrity checking, or
dd3773
+digital signatures.   If you need to use such a hash for non-security purposes
dd3773
+(such as indexing into a data structure for speed), you can override the keyword
dd3773
+argument "usedforsecurity" from True to False to signify that your code is not
dd3773
+relying on the hash for security purposes, and this will allow the hash to be
dd3773
+usable even in FIPS mode.
dd3773
+
dd3773
 Hash objects have these methods:
dd3773
  - update(data): Update the hash object with the bytes in data. Repeated calls
dd3773
                  are equivalent to a single call with the concatenation of all
dd3773
@@ -67,6 +77,19 @@ algorithms_available = set(__always_supported)
dd3773
 __all__ = __always_supported + ('new', 'algorithms_guaranteed',
dd3773
                                 'algorithms_available', 'pbkdf2_hmac')
dd3773
 
dd3773
+import functools
dd3773
+def __ignore_usedforsecurity(func):
dd3773
+    """Used for sha3_* functions. Until OpenSSL implements them, we want
dd3773
+    to use them from Python _sha3 module, but we want them to accept
dd3773
+    usedforsecurity argument too."""
dd3773
+    # TODO: remove this function when OpenSSL implements sha3
dd3773
+    @functools.wraps(func)
dd3773
+    def inner(*args, **kwargs):
dd3773
+        if 'usedforsecurity' in kwargs:
dd3773
+            kwargs.pop('usedforsecurity')
dd3773
+        return func(*args, **kwargs)
dd3773
+    return inner
dd3773
+
dd3773
 
dd3773
 __builtin_constructor_cache = {}
dd3773
 
dd3773
@@ -121,24 +144,33 @@ def __get_openssl_constructor(name):
dd3773
         f = getattr(_hashlib, 'openssl_' + name)
dd3773
         # Allow the C module to raise ValueError.  The function will be
dd3773
         # defined but the hash not actually available thanks to OpenSSL.
dd3773
-        f()
dd3773
+        # We pass "usedforsecurity=False" to disable FIPS-based restrictions:
dd3773
+        # at this stage we're merely seeing if the function is callable,
dd3773
+        # rather than using it for actual work.
dd3773
+        f(usedforsecurity=False)
dd3773
         # Use the C function directly (very fast)
dd3773
         return f
dd3773
     except (AttributeError, ValueError):
dd3773
+        # TODO: We want to just raise here when OpenSSL implements sha3
dd3773
+        # because we want to make sure that Fedora uses everything from OpenSSL
dd3773
         return __get_builtin_constructor(name)
dd3773
 
dd3773
 
dd3773
-def __py_new(name, data=b'', **kwargs):
dd3773
-    """new(name, data=b'', **kwargs) - Return a new hashing object using the
dd3773
-    named algorithm; optionally initialized with data (which must be
dd3773
-    a bytes-like object).
dd3773
+def __py_new(name, data=b'', *, usedforsecurity=True, **kwargs):
dd3773
+    """new(name, data=b'', usedforsecurity=True) - Return a new hashing object using
dd3773
+    the named algorithm; optionally initialized with data (which must be bytes).
dd3773
+    The 'usedforsecurity' keyword argument does nothing, and is for compatibilty
dd3773
+    with the OpenSSL implementation
dd3773
     """
dd3773
     return __get_builtin_constructor(name)(data, **kwargs)
dd3773
 
dd3773
 
dd3773
-def __hash_new(name, data=b'', **kwargs):
dd3773
-    """new(name, data=b'') - Return a new hashing object using the named algorithm;
dd3773
-    optionally initialized with data (which must be a bytes-like object).
dd3773
+def __hash_new(name, data=b'', *, usedforsecurity=True, **kwargs):
dd3773
+    """new(name, data=b'', usedforsecurity=True) - Return a new hashing object using
dd3773
+    the named algorithm; optionally initialized with data (which must be bytes).
dd3773
+
dd3773
+    Override 'usedforsecurity' to False when using for non-security purposes in
dd3773
+    a FIPS environment
dd3773
     """
dd3773
     if name in {'blake2b', 'blake2s'}:
dd3773
         # Prefer our blake2 implementation.
dd3773
@@ -147,12 +179,10 @@ def __hash_new(name, data=b'', **kwargs):
dd3773
         # salt, personal, tree hashing or SSE.
dd3773
         return __get_builtin_constructor(name)(data, **kwargs)
dd3773
     try:
dd3773
-        return _hashlib.new(name, data)
dd3773
+        return _hashlib.new(name, data, usedforsecurity)
dd3773
     except ValueError:
dd3773
-        # If the _hashlib module (OpenSSL) doesn't support the named
dd3773
-        # hash, try using our builtin implementations.
dd3773
-        # This allows for SHA224/256 and SHA384/512 support even though
dd3773
-        # the OpenSSL library prior to 0.9.8 doesn't provide them.
dd3773
+        # TODO: We want to just raise here when OpenSSL implements sha3
dd3773
+        # because we want to make sure that Fedora uses everything from OpenSSL
dd3773
         return __get_builtin_constructor(name)(data)
dd3773
 
dd3773
 
dd3773
@@ -163,8 +193,8 @@ try:
dd3773
     algorithms_available = algorithms_available.union(
dd3773
             _hashlib.openssl_md_meth_names)
dd3773
 except ImportError:
dd3773
-    new = __py_new
dd3773
-    __get_hash = __get_builtin_constructor
dd3773
+    # We don't build the legacy modules
dd3773
+    raise
dd3773
 
dd3773
 try:
dd3773
     # OpenSSL's PKCS5_PBKDF2_HMAC requires OpenSSL 1.0+ with HMAC and SHA
dd3773
@@ -241,7 +271,10 @@ for __func_name in __always_supported:
dd3773
     # try them all, some may not work due to the OpenSSL
dd3773
     # version not supporting that algorithm.
dd3773
     try:
dd3773
-        globals()[__func_name] = __get_hash(__func_name)
dd3773
+        func = __get_hash(__func_name)
dd3773
+        if __func_name.startswith(('sha3_', 'blake2', 'shake_')):
dd3773
+            func = __ignore_usedforsecurity(func)
dd3773
+        globals()[__func_name] = func
dd3773
     except ValueError:
dd3773
         import logging
dd3773
         logging.exception('code for hash %s was not found.', __func_name)
dd3773
@@ -249,4 +282,5 @@ for __func_name in __always_supported:
dd3773
 
dd3773
 # Cleanup locals()
dd3773
 del __always_supported, __func_name, __get_hash
dd3773
-del __py_new, __hash_new, __get_openssl_constructor
dd3773
+del __hash_new, __get_openssl_constructor
dd3773
+del __ignore_usedforsecurity
dd3773
diff --git a/Lib/test/test_hashlib.py b/Lib/test/test_hashlib.py
dd3773
index 9711856..254dbd3 100644
dd3773
--- a/Lib/test/test_hashlib.py
dd3773
+++ b/Lib/test/test_hashlib.py
dd3773
@@ -27,7 +27,22 @@ from http.client import HTTPException
dd3773
 COMPILED_WITH_PYDEBUG = hasattr(sys, 'gettotalrefcount')
dd3773
 
dd3773
 c_hashlib = import_fresh_module('hashlib', fresh=['_hashlib'])
dd3773
-py_hashlib = import_fresh_module('hashlib', blocked=['_hashlib'])
dd3773
+# skipped on Fedora, since we always use OpenSSL implementation
dd3773
+# py_hashlib = import_fresh_module('hashlib', blocked=['_hashlib'])
dd3773
+
dd3773
+def openssl_enforces_fips():
dd3773
+    # Use the "openssl" command (if present) to try to determine if the local
dd3773
+    # OpenSSL is configured to enforce FIPS
dd3773
+    from subprocess import Popen, PIPE
dd3773
+    try:
dd3773
+        p = Popen(['openssl', 'md5'],
dd3773
+                  stdin=PIPE, stdout=PIPE, stderr=PIPE)
dd3773
+    except OSError:
dd3773
+        # "openssl" command not found
dd3773
+        return False
dd3773
+    stdout, stderr = p.communicate(input=b'abc')
dd3773
+    return b'unknown cipher' in stderr
dd3773
+OPENSSL_ENFORCES_FIPS = openssl_enforces_fips()
dd3773
 
dd3773
 try:
dd3773
     import _blake2
dd3773
@@ -71,6 +86,17 @@ def read_vectors(hash_name):
dd3773
             yield parts
dd3773
 
dd3773
 
dd3773
+# hashlib and _hashlib-based functions support a "usedforsecurity" keyword
dd3773
+# argument, and FIPS mode requires that it be used overridden with a False
dd3773
+# value for these selftests to work.  Other cryptographic code within Python
dd3773
+# doesn't support this keyword.
dd3773
+# Modify a function to one in which "usedforsecurity=False" is added to the
dd3773
+# keyword arguments:
dd3773
+def suppress_fips(f):
dd3773
+    def g(*args, **kwargs):
dd3773
+        return f(*args, usedforsecurity=False, **kwargs)
dd3773
+    return g
dd3773
+
dd3773
 class HashLibTestCase(unittest.TestCase):
dd3773
     supported_hash_names = ( 'md5', 'MD5', 'sha1', 'SHA1',
dd3773
                              'sha224', 'SHA224', 'sha256', 'SHA256',
dd3773
@@ -109,11 +135,11 @@ class HashLibTestCase(unittest.TestCase):
dd3773
         # For each algorithm, test the direct constructor and the use
dd3773
         # of hashlib.new given the algorithm name.
dd3773
         for algorithm, constructors in self.constructors_to_test.items():
dd3773
-            constructors.add(getattr(hashlib, algorithm))
dd3773
-            def _test_algorithm_via_hashlib_new(data=None, _alg=algorithm, **kwargs):
dd3773
+            constructors.add(suppress_fips(getattr(hashlib, algorithm)))
dd3773
+            def _test_algorithm_via_hashlib_new(data=None, _alg=algorithm, usedforsecurity=True, **kwargs):
dd3773
                 if data is None:
dd3773
-                    return hashlib.new(_alg, **kwargs)
dd3773
-                return hashlib.new(_alg, data, **kwargs)
dd3773
+                    return suppress_fips(hashlib.new)(_alg, **kwargs)
dd3773
+                return suppress_fips(hashlib.new)(_alg, data, **kwargs)
dd3773
             constructors.add(_test_algorithm_via_hashlib_new)
dd3773
 
dd3773
         _hashlib = self._conditional_import_module('_hashlib')
dd3773
@@ -125,26 +151,12 @@ class HashLibTestCase(unittest.TestCase):
dd3773
             for algorithm, constructors in self.constructors_to_test.items():
dd3773
                 constructor = getattr(_hashlib, 'openssl_'+algorithm, None)
dd3773
                 if constructor:
dd3773
-                    constructors.add(constructor)
dd3773
+                    constructors.add(suppress_fips(constructor))
dd3773
 
dd3773
         def add_builtin_constructor(name):
dd3773
             constructor = getattr(hashlib, "__get_builtin_constructor")(name)
dd3773
             self.constructors_to_test[name].add(constructor)
dd3773
 
dd3773
-        _md5 = self._conditional_import_module('_md5')
dd3773
-        if _md5:
dd3773
-            add_builtin_constructor('md5')
dd3773
-        _sha1 = self._conditional_import_module('_sha1')
dd3773
-        if _sha1:
dd3773
-            add_builtin_constructor('sha1')
dd3773
-        _sha256 = self._conditional_import_module('_sha256')
dd3773
-        if _sha256:
dd3773
-            add_builtin_constructor('sha224')
dd3773
-            add_builtin_constructor('sha256')
dd3773
-        _sha512 = self._conditional_import_module('_sha512')
dd3773
-        if _sha512:
dd3773
-            add_builtin_constructor('sha384')
dd3773
-            add_builtin_constructor('sha512')
dd3773
         if _blake2:
dd3773
             add_builtin_constructor('blake2s')
dd3773
             add_builtin_constructor('blake2b')
dd3773
@@ -219,9 +231,6 @@ class HashLibTestCase(unittest.TestCase):
dd3773
             else:
dd3773
                 del sys.modules['_md5']
dd3773
         self.assertRaises(TypeError, get_builtin_constructor, 3)
dd3773
-        constructor = get_builtin_constructor('md5')
dd3773
-        self.assertIs(constructor, _md5.md5)
dd3773
-        self.assertEqual(sorted(builtin_constructor_cache), ['MD5', 'md5'])
dd3773
 
dd3773
     def test_hexdigest(self):
dd3773
         for cons in self.hash_constructors:
dd3773
@@ -840,6 +849,65 @@ class HashLibTestCase(unittest.TestCase):
dd3773
 
dd3773
         self.assertEqual(expected_hash, hasher.hexdigest())
dd3773
 
dd3773
+    def test_issue9146(self):
dd3773
+        # Ensure that various ways to use "MD5" from "hashlib" don't segfault:
dd3773
+        m = hashlib.md5(usedforsecurity=False)
dd3773
+        m.update(b'abc\n')
dd3773
+        self.assertEquals(m.hexdigest(), "0bee89b07a248e27c83fc3d5951213c1")
dd3773
+
dd3773
+        m = hashlib.new('md5', usedforsecurity=False)
dd3773
+        m.update(b'abc\n')
dd3773
+        self.assertEquals(m.hexdigest(), "0bee89b07a248e27c83fc3d5951213c1")
dd3773
+
dd3773
+        m = hashlib.md5(b'abc\n', usedforsecurity=False)
dd3773
+        self.assertEquals(m.hexdigest(), "0bee89b07a248e27c83fc3d5951213c1")
dd3773
+
dd3773
+        m = hashlib.new('md5', b'abc\n', usedforsecurity=False)
dd3773
+        self.assertEquals(m.hexdigest(), "0bee89b07a248e27c83fc3d5951213c1")
dd3773
+
dd3773
+    @unittest.skipUnless(OPENSSL_ENFORCES_FIPS,
dd3773
+                         'FIPS enforcement required for this test.')
dd3773
+    def test_hashlib_fips_mode(self):
dd3773
+        # Ensure that we raise a ValueError on vanilla attempts to use MD5
dd3773
+        # in hashlib in a FIPS-enforced setting:
dd3773
+        with self.assertRaisesRegexp(ValueError, '.*unknown cipher'):
dd3773
+            m = hashlib.md5()
dd3773
+
dd3773
+        if not self._conditional_import_module('_md5'):
dd3773
+            with self.assertRaisesRegexp(ValueError, '.*unknown cipher'):
dd3773
+                m = hashlib.new('md5')
dd3773
+
dd3773
+    @unittest.skipUnless(OPENSSL_ENFORCES_FIPS,
dd3773
+                         'FIPS enforcement required for this test.')
dd3773
+    def test_hashopenssl_fips_mode(self):
dd3773
+        # Verify the _hashlib module's handling of md5:
dd3773
+        _hashlib = self._conditional_import_module('_hashlib')
dd3773
+        if _hashlib:
dd3773
+            assert hasattr(_hashlib, 'openssl_md5')
dd3773
+
dd3773
+            # Ensure that _hashlib raises a ValueError on vanilla attempts to
dd3773
+            # use MD5 in a FIPS-enforced setting:
dd3773
+            with self.assertRaisesRegexp(ValueError, '.*unknown cipher'):
dd3773
+                m = _hashlib.openssl_md5()
dd3773
+            with self.assertRaisesRegexp(ValueError, '.*unknown cipher'):
dd3773
+                m = _hashlib.new('md5')
dd3773
+
dd3773
+            # Ensure that in such a setting we can whitelist a callsite with
dd3773
+            # usedforsecurity=False and have it succeed:
dd3773
+            m = _hashlib.openssl_md5(usedforsecurity=False)
dd3773
+            m.update(b'abc\n')
dd3773
+            self.assertEquals(m.hexdigest(), "0bee89b07a248e27c83fc3d5951213c1")
dd3773
+
dd3773
+            m = _hashlib.new('md5', usedforsecurity=False)
dd3773
+            m.update(b'abc\n')
dd3773
+            self.assertEquals(m.hexdigest(), "0bee89b07a248e27c83fc3d5951213c1")
dd3773
+
dd3773
+            m = _hashlib.openssl_md5(b'abc\n', usedforsecurity=False)
dd3773
+            self.assertEquals(m.hexdigest(), "0bee89b07a248e27c83fc3d5951213c1")
dd3773
+
dd3773
+            m = _hashlib.new('md5', b'abc\n', usedforsecurity=False)
dd3773
+            self.assertEquals(m.hexdigest(), "0bee89b07a248e27c83fc3d5951213c1")
dd3773
+
dd3773
 
dd3773
 class KDFTests(unittest.TestCase):
dd3773
 
dd3773
@@ -930,6 +998,7 @@ class KDFTests(unittest.TestCase):
dd3773
             iterations=1, dklen=None)
dd3773
         self.assertEqual(out, self.pbkdf2_results['sha1'][0][0])
dd3773
 
dd3773
+    @unittest.skip('skipped on Fedora, as we always use OpenSSL pbkdf2_hmac')
dd3773
     def test_pbkdf2_hmac_py(self):
dd3773
         self._test_pbkdf2_hmac(py_hashlib.pbkdf2_hmac)
dd3773
 
dd3773
diff --git a/Modules/_hashopenssl.c b/Modules/_hashopenssl.c
dd3773
index 84edd72..cc602c4 100644
dd3773
--- a/Modules/_hashopenssl.c
dd3773
+++ b/Modules/_hashopenssl.c
dd3773
@@ -20,6 +20,7 @@
dd3773
 
dd3773
 
dd3773
 /* EVP is the preferred interface to hashing in OpenSSL */
dd3773
+#include <openssl/ssl.h>
dd3773
 #include <openssl/evp.h>
dd3773
 /* We use the object interface to discover what hashes OpenSSL supports. */
dd3773
 #include <openssl/objects.h>
dd3773
@@ -61,10 +62,19 @@ typedef struct {
dd3773
 
dd3773
 static PyTypeObject EVPtype;
dd3773
 
dd3773
+/* Struct to hold all the cached information we need on a specific algorithm.
dd3773
+   We have one of these per algorithm */
dd3773
+typedef struct {
dd3773
+    PyObject *name_obj;
dd3773
+    EVP_MD_CTX ctxs[2];
dd3773
+    /* ctx_ptrs will point to ctxs unless an error occurred, when it will
dd3773
+       be NULL: */
dd3773
+    EVP_MD_CTX *ctx_ptrs[2];
dd3773
+    PyObject *error_msgs[2];
dd3773
+} EVPCachedInfo;
dd3773
 
dd3773
-#define DEFINE_CONSTS_FOR_NEW(Name)  \
dd3773
-    static PyObject *CONST_ ## Name ## _name_obj = NULL; \
dd3773
-    static EVP_MD_CTX *CONST_new_ ## Name ## _ctx_p = NULL;
dd3773
+#define DEFINE_CONSTS_FOR_NEW(Name) \
dd3773
+    static EVPCachedInfo cached_info_ ##Name;
dd3773
 
dd3773
 DEFINE_CONSTS_FOR_NEW(md5)
dd3773
 DEFINE_CONSTS_FOR_NEW(sha1)
dd3773
@@ -139,15 +149,54 @@ EVP_hash(EVPobject *self, const void *vp, Py_ssize_t len)
dd3773
             process = MUNCH_SIZE;
dd3773
         else
dd3773
             process = Py_SAFE_DOWNCAST(len, Py_ssize_t, unsigned int);
dd3773
-        if (!EVP_DigestUpdate(self->ctx, (const void*)cp, process)) {
dd3773
-            _setException(PyExc_ValueError);
dd3773
-            break;
dd3773
-        }
dd3773
+        EVP_DigestUpdate(self->ctx, (const void*)cp, process);
dd3773
         len -= process;
dd3773
         cp += process;
dd3773
     }
dd3773
 }
dd3773
 
dd3773
+static void
dd3773
+mc_ctx_init(EVP_MD_CTX *ctx, int usedforsecurity)
dd3773
+{
dd3773
+    EVP_MD_CTX_init(ctx);
dd3773
+
dd3773
+    /*
dd3773
+      If the user has declared that this digest is being used in a
dd3773
+      non-security role (e.g. indexing into a data structure), set
dd3773
+      the exception flag for openssl to allow it
dd3773
+    */
dd3773
+    if (!usedforsecurity) {
dd3773
+#ifdef EVP_MD_CTX_FLAG_NON_FIPS_ALLOW
dd3773
+        EVP_MD_CTX_set_flags(ctx,
dd3773
+                             EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
dd3773
+#endif
dd3773
+    }
dd3773
+}
dd3773
+
dd3773
+/* Get an error msg for the last error as a PyObject */
dd3773
+static PyObject *
dd3773
+error_msg_for_last_error(void)
dd3773
+{
dd3773
+    char *errstr;
dd3773
+
dd3773
+    errstr = ERR_error_string(ERR_peek_last_error(), NULL);
dd3773
+    ERR_clear_error();
dd3773
+
dd3773
+    return PyUnicode_FromString(errstr); /* Can be NULL */
dd3773
+}
dd3773
+
dd3773
+static void
dd3773
+set_evp_exception(void)
dd3773
+{
dd3773
+    char *errstr;
dd3773
+
dd3773
+    errstr = ERR_error_string(ERR_peek_last_error(), NULL);
dd3773
+    ERR_clear_error();
dd3773
+
dd3773
+    PyErr_SetString(PyExc_ValueError, errstr);
dd3773
+}
dd3773
+
dd3773
+
dd3773
 /* Internal methods for a hash object */
dd3773
 
dd3773
 static void
dd3773
@@ -212,10 +261,7 @@ EVP_digest(EVPobject *self, PyObject *unused)
dd3773
         return _setException(PyExc_ValueError);
dd3773
     }
dd3773
     digest_size = EVP_MD_CTX_size(temp_ctx);
dd3773
-    if (!EVP_DigestFinal(temp_ctx, digest, NULL)) {
dd3773
-        _setException(PyExc_ValueError);
dd3773
-        return NULL;
dd3773
-    }
dd3773
+    EVP_DigestFinal(temp_ctx, digest, NULL);
dd3773
 
dd3773
     retval = PyBytes_FromStringAndSize((const char *)digest, digest_size);
dd3773
     EVP_MD_CTX_free(temp_ctx);
dd3773
@@ -243,10 +289,7 @@ EVP_hexdigest(EVPobject *self, PyObject *unused)
dd3773
         return _setException(PyExc_ValueError);
dd3773
     }
dd3773
     digest_size = EVP_MD_CTX_size(temp_ctx);
dd3773
-    if (!EVP_DigestFinal(temp_ctx, digest, NULL)) {
dd3773
-        _setException(PyExc_ValueError);
dd3773
-        return NULL;
dd3773
-    }
dd3773
+    EVP_DigestFinal(temp_ctx, digest, NULL);
dd3773
 
dd3773
     EVP_MD_CTX_free(temp_ctx);
dd3773
 
dd3773
@@ -342,15 +385,16 @@ EVP_repr(EVPobject *self)
dd3773
 static int
dd3773
 EVP_tp_init(EVPobject *self, PyObject *args, PyObject *kwds)
dd3773
 {
dd3773
-    static char *kwlist[] = {"name", "string", NULL};
dd3773
+    static char *kwlist[] = {"name", "string", "usedforsecurity", NULL};
dd3773
     PyObject *name_obj = NULL;
dd3773
     PyObject *data_obj = NULL;
dd3773
+    int usedforsecurity = 1;
dd3773
     Py_buffer view;
dd3773
     char *nameStr;
dd3773
     const EVP_MD *digest;
dd3773
 
dd3773
-    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:HASH", kwlist,
dd3773
-                                     &name_obj, &data_obj)) {
dd3773
+    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|Oi:HASH", kwlist,
dd3773
+                                     &name_obj, &data_obj, &usedforsecurity)) {
dd3773
         return -1;
dd3773
     }
dd3773
 
dd3773
@@ -371,11 +415,11 @@ EVP_tp_init(EVPobject *self, PyObject *args, PyObject *kwds)
dd3773
             PyBuffer_Release(&view);
dd3773
         return -1;
dd3773
     }
dd3773
-    if (!EVP_DigestInit(self->ctx, digest)) {
dd3773
-        _setException(PyExc_ValueError);
dd3773
-        if (data_obj)
dd3773
-            PyBuffer_Release(&view);
dd3773
-        return -1;
dd3773
+    mc_ctx_init(self->ctx, usedforsecurity);
dd3773
+    if (!EVP_DigestInit_ex(self->ctx, digest, NULL)) {
dd3773
+        set_evp_exception();
dd3773
+        PyBuffer_Release(&view);
dd3773
+        Py_RETURN_NONE;
dd3773
     }
dd3773
 
dd3773
     Py_INCREF(name_obj);
dd3773
@@ -460,7 +504,8 @@ static PyTypeObject EVPtype = {
dd3773
 static PyObject *
dd3773
 EVPnew(PyObject *name_obj,
dd3773
        const EVP_MD *digest, const EVP_MD_CTX *initial_ctx,
dd3773
-       const unsigned char *cp, Py_ssize_t len)
dd3773
+       const unsigned char *cp, Py_ssize_t len,
dd3773
+       int usedforsecurity)
dd3773
 {
dd3773
     EVPobject *self;
dd3773
 
dd3773
@@ -475,8 +520,9 @@ EVPnew(PyObject *name_obj,
dd3773
     if (initial_ctx) {
dd3773
         EVP_MD_CTX_copy(self->ctx, initial_ctx);
dd3773
     } else {
dd3773
-        if (!EVP_DigestInit(self->ctx, digest)) {
dd3773
-            _setException(PyExc_ValueError);
dd3773
+        mc_ctx_init(self->ctx, usedforsecurity);
dd3773
+        if (!EVP_DigestInit_ex(self->ctx, digest, NULL)) {
dd3773
+            set_evp_exception();
dd3773
             Py_DECREF(self);
dd3773
             return NULL;
dd3773
         }
dd3773
@@ -503,21 +549,29 @@ PyDoc_STRVAR(EVP_new__doc__,
dd3773
 An optional string argument may be provided and will be\n\
dd3773
 automatically hashed.\n\
dd3773
 \n\
dd3773
-The MD5 and SHA1 algorithms are always supported.\n");
dd3773
+The MD5 and SHA1 algorithms are always supported.\n\
dd3773
+\n\
dd3773
+An optional \"usedforsecurity=True\" keyword argument is provided for use in\n\
dd3773
+environments that enforce FIPS-based restrictions.  Some implementations of\n\
dd3773
+OpenSSL can be configured to prevent the usage of non-secure algorithms (such\n\
dd3773
+as MD5).  If you have a non-security use for these algorithms (e.g. a hash\n\
dd3773
+table), you can override this argument by marking the callsite as\n\
dd3773
+\"usedforsecurity=False\".");
dd3773
 
dd3773
 static PyObject *
dd3773
 EVP_new(PyObject *self, PyObject *args, PyObject *kwdict)
dd3773
 {
dd3773
-    static char *kwlist[] = {"name", "string", NULL};
dd3773
+    static char *kwlist[] = {"name", "string", "usedforsecurity", NULL};
dd3773
     PyObject *name_obj = NULL;
dd3773
     PyObject *data_obj = NULL;
dd3773
+    int usedforsecurity = 1;
dd3773
     Py_buffer view = { 0 };
dd3773
     PyObject *ret_obj;
dd3773
     char *name;
dd3773
     const EVP_MD *digest;
dd3773
 
dd3773
-    if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O|O:new", kwlist,
dd3773
-                                     &name_obj, &data_obj)) {
dd3773
+    if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O|Oi:new", kwlist,
dd3773
+                                     &name_obj, &data_obj, &usedforsecurity)) {
dd3773
         return NULL;
dd3773
     }
dd3773
 
dd3773
@@ -531,7 +585,8 @@ EVP_new(PyObject *self, PyObject *args, PyObject *kwdict)
dd3773
 
dd3773
     digest = EVP_get_digestbyname(name);
dd3773
 
dd3773
-    ret_obj = EVPnew(name_obj, digest, NULL, (unsigned char*)view.buf, view.len);
dd3773
+    ret_obj = EVPnew(name_obj, digest, NULL, (unsigned char*)view.buf, view.len,
dd3773
+                     usedforsecurity);
dd3773
 
dd3773
     if (data_obj)
dd3773
         PyBuffer_Release(&view);
dd3773
@@ -919,66 +974,116 @@ generate_hash_name_list(void)
dd3773
 
dd3773
 
dd3773
 /*
dd3773
- *  This macro generates constructor function definitions for specific
dd3773
- *  hash algorithms.  These constructors are much faster than calling
dd3773
- *  the generic one passing it a python string and are noticeably
dd3773
- *  faster than calling a python new() wrapper.  That is important for
dd3773
+ *  This macro and function generates a family of constructor function
dd3773
+ *  definitions for specific hash algorithms.  These constructors are much
dd3773
+ *  faster than calling the generic one passing it a python string and are
dd3773
+ *  noticably faster than calling a python new() wrapper.  That's important for
dd3773
  *  code that wants to make hashes of a bunch of small strings.
dd3773
  *  The first call will lazy-initialize, which reports an exception
dd3773
  *  if initialization fails.
dd3773
  */
dd3773
 #define GEN_CONSTRUCTOR(NAME)  \
dd3773
     static PyObject * \
dd3773
-    EVP_new_ ## NAME (PyObject *self, PyObject *args) \
dd3773
+    EVP_new_ ## NAME (PyObject *self, PyObject *args, PyObject *kwdict) \
dd3773
     { \
dd3773
-        PyObject *data_obj = NULL; \
dd3773
-        Py_buffer view = { 0 }; \
dd3773
-        PyObject *ret_obj; \
dd3773
-     \
dd3773
-        if (!PyArg_ParseTuple(args, "|O:" #NAME , &data_obj)) { \
dd3773
-            return NULL; \
dd3773
-        } \
dd3773
-     \
dd3773
-        if (CONST_new_ ## NAME ## _ctx_p == NULL) { \
dd3773
-            EVP_MD_CTX *ctx_p = EVP_MD_CTX_new(); \
dd3773
-            if (!EVP_get_digestbyname(#NAME) || \
dd3773
-                !EVP_DigestInit(ctx_p, EVP_get_digestbyname(#NAME))) { \
dd3773
-                _setException(PyExc_ValueError); \
dd3773
-                EVP_MD_CTX_free(ctx_p); \
dd3773
-                return NULL; \
dd3773
-            } \
dd3773
-            CONST_new_ ## NAME ## _ctx_p = ctx_p; \
dd3773
-        } \
dd3773
-     \
dd3773
-        if (data_obj) \
dd3773
-            GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view); \
dd3773
-     \
dd3773
-        ret_obj = EVPnew( \
dd3773
-                    CONST_ ## NAME ## _name_obj, \
dd3773
-                    NULL, \
dd3773
-                    CONST_new_ ## NAME ## _ctx_p, \
dd3773
-                    (unsigned char*)view.buf, \
dd3773
-                    view.len); \
dd3773
-     \
dd3773
-        if (data_obj) \
dd3773
-            PyBuffer_Release(&view); \
dd3773
-        return ret_obj; \
dd3773
+       return implement_specific_EVP_new(self, args, kwdict,      \
dd3773
+                                         "|Oi:" #NAME,            \
dd3773
+                                         &cached_info_ ## NAME ); \
dd3773
+    }
dd3773
+
dd3773
+static PyObject *
dd3773
+implement_specific_EVP_new(PyObject *self, PyObject *args, PyObject *kwdict,
dd3773
+                           const char *format,
dd3773
+                           EVPCachedInfo *cached_info)
dd3773
+{
dd3773
+    static char *kwlist[] = {"string", "usedforsecurity", NULL};
dd3773
+    PyObject *data_obj = NULL;
dd3773
+    Py_buffer view = { 0 };
dd3773
+    int usedforsecurity = 1;
dd3773
+    int idx;
dd3773
+    PyObject *ret_obj = NULL;
dd3773
+
dd3773
+    assert(cached_info);
dd3773
+
dd3773
+    if (!PyArg_ParseTupleAndKeywords(args, kwdict, format, kwlist,
dd3773
+                                     &data_obj, &usedforsecurity)) {
dd3773
+        return NULL;
dd3773
     }
dd3773
 
dd3773
+    if (data_obj)
dd3773
+       GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view);
dd3773
+
dd3773
+    idx = usedforsecurity ? 1 : 0;
dd3773
+
dd3773
+    /*
dd3773
+     * If an error occurred during creation of the global content, the ctx_ptr
dd3773
+     * will be NULL, and the error_msg will hopefully be non-NULL:
dd3773
+     */
dd3773
+    if (cached_info->ctx_ptrs[idx]) {
dd3773
+        /* We successfully initialized this context; copy it: */
dd3773
+        ret_obj = EVPnew(cached_info->name_obj,
dd3773
+                         NULL,
dd3773
+                         cached_info->ctx_ptrs[idx],
dd3773
+                         (unsigned char*)view.buf, view.len,
dd3773
+                         usedforsecurity);
dd3773
+    } else {
dd3773
+        /* Some kind of error happened initializing the global context for
dd3773
+           this (digest, usedforsecurity) pair.
dd3773
+           Raise an exception with the saved error message: */
dd3773
+        if (cached_info->error_msgs[idx]) {
dd3773
+            PyErr_SetObject(PyExc_ValueError, cached_info->error_msgs[idx]);
dd3773
+        } else {
dd3773
+            PyErr_SetString(PyExc_ValueError, "Error initializing hash");
dd3773
+        }
dd3773
+     }
dd3773
+
dd3773
+    if (data_obj)
dd3773
+        PyBuffer_Release(&view);
dd3773
+
dd3773
+    return ret_obj;
dd3773
+}
dd3773
+
dd3773
 /* a PyMethodDef structure for the constructor */
dd3773
 #define CONSTRUCTOR_METH_DEF(NAME)  \
dd3773
-    {"openssl_" #NAME, (PyCFunction)EVP_new_ ## NAME, METH_VARARGS, \
dd3773
+    {"openssl_" #NAME, (PyCFunction)EVP_new_ ## NAME, \
dd3773
+        METH_VARARGS|METH_KEYWORDS, \
dd3773
         PyDoc_STR("Returns a " #NAME \
dd3773
                   " hash object; optionally initialized with a string") \
dd3773
     }
dd3773
 
dd3773
-/* used in the init function to setup a constructor: initialize OpenSSL
dd3773
-   constructor constants if they haven't been initialized already.  */
dd3773
-#define INIT_CONSTRUCTOR_CONSTANTS(NAME)  do { \
dd3773
-    if (CONST_ ## NAME ## _name_obj == NULL) { \
dd3773
-        CONST_ ## NAME ## _name_obj = PyUnicode_FromString(#NAME); \
dd3773
-    } \
dd3773
+/*
dd3773
+  Macro/function pair to set up the constructors.
dd3773
+
dd3773
+  Try to initialize a context for each hash twice, once with
dd3773
+  EVP_MD_CTX_FLAG_NON_FIPS_ALLOW and once without.
dd3773
+
dd3773
+  Any that have errors during initialization will end up with a NULL ctx_ptrs
dd3773
+  entry, and err_msgs will be set (unless we're very low on memory)
dd3773
+*/
dd3773
+#define INIT_CONSTRUCTOR_CONSTANTS(NAME)  do {    \
dd3773
+    init_constructor_constant(&cached_info_ ## NAME, #NAME); \
dd3773
 } while (0);
dd3773
+static void
dd3773
+init_constructor_constant(EVPCachedInfo *cached_info, const char *name)
dd3773
+{
dd3773
+    assert(cached_info);
dd3773
+    cached_info->name_obj = PyUnicode_FromString(name);
dd3773
+    if (EVP_get_digestbyname(name)) {
dd3773
+        int i;
dd3773
+        for (i=0; i<2; i++) {
dd3773
+            mc_ctx_init(&cached_info->ctxs[i], i);
dd3773
+            if (EVP_DigestInit_ex(&cached_info->ctxs[i],
dd3773
+                                  EVP_get_digestbyname(name), NULL)) {
dd3773
+                /* Success: */
dd3773
+                cached_info->ctx_ptrs[i] = &cached_info->ctxs[i];
dd3773
+            } else {
dd3773
+                /* Failure: */
dd3773
+              cached_info->ctx_ptrs[i] = NULL;
dd3773
+              cached_info->error_msgs[i] = error_msg_for_last_error();
dd3773
+            }
dd3773
+        }
dd3773
+    }
dd3773
+}
dd3773
 
dd3773
 GEN_CONSTRUCTOR(md5)
dd3773
 GEN_CONSTRUCTOR(sha1)
dd3773
@@ -1026,16 +1131,10 @@ PyInit__hashlib(void)
dd3773
 {
dd3773
     PyObject *m, *openssl_md_meth_names;
dd3773
 
dd3773
-#ifndef OPENSSL_VERSION_1_1
dd3773
-    /* Load all digest algorithms and initialize cpuid */
dd3773
-    OPENSSL_add_all_algorithms_noconf();
dd3773
-    ERR_load_crypto_strings();
dd3773
-#endif
dd3773
+    SSL_load_error_strings();
dd3773
+    SSL_library_init();
dd3773
 
dd3773
-    /* TODO build EVP_functions openssl_* entries dynamically based
dd3773
-     * on what hashes are supported rather than listing many
dd3773
-     * but having some be unsupported.  Only init appropriate
dd3773
-     * constants. */
dd3773
+    OpenSSL_add_all_digests();
dd3773
 
dd3773
     Py_TYPE(&EVPtype) = &PyType_Type;
dd3773
     if (PyType_Ready(&EVPtype) < 0)