Blame SOURCES/00146-hashlib-fips.patch

23b3e9
diff -up Python-2.7.2/Lib/hashlib.py.hashlib-fips Python-2.7.2/Lib/hashlib.py
23b3e9
--- Python-2.7.2/Lib/hashlib.py.hashlib-fips	2011-06-11 11:46:24.000000000 -0400
23b3e9
+++ Python-2.7.2/Lib/hashlib.py	2011-09-14 00:21:26.194252001 -0400
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():
23b3e9
@@ -24,6 +27,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
23b3e9
@@ -63,74 +80,39 @@ algorithms = __always_supported
23b3e9
 __all__ = __always_supported + ('new', 'algorithms')
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
23b3e9
     new = __hash_new
23b3e9
     __get_hash = __get_openssl_constructor
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
23b3e9
@@ -143,4 +125,4 @@ for __func_name in __always_supported:
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
23b3e9
diff -up Python-2.7.2/Lib/test/test_hashlib.py.hashlib-fips Python-2.7.2/Lib/test/test_hashlib.py
23b3e9
--- Python-2.7.2/Lib/test/test_hashlib.py.hashlib-fips	2011-06-11 11:46:25.000000000 -0400
23b3e9
+++ Python-2.7.2/Lib/test/test_hashlib.py	2011-09-14 01:08:55.525254195 -0400
23b3e9
@@ -32,6 +32,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',
23b3e9
@@ -61,10 +74,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')
23b3e9
@@ -78,28 +91,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):
23b3e9
@@ -115,28 +113,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):
23b3e9
@@ -145,16 +125,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):
23b3e9
@@ -162,7 +142,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"
23b3e9
@@ -172,7 +152,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):
23b3e9
@@ -354,6 +335,70 @@ class HashLibTestCase(unittest.TestCase)
23b3e9
 
23b3e9
         self.assertEqual(expected_hash, hasher.hexdigest())
23b3e9
 
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
+
23b3e9
 def test_main():
23b3e9
     test_support.run_unittest(HashLibTestCase)
23b3e9
 
23b3e9
diff -up Python-2.7.2/Modules/_hashopenssl.c.hashlib-fips Python-2.7.2/Modules/_hashopenssl.c
23b3e9
--- Python-2.7.2/Modules/_hashopenssl.c.hashlib-fips	2011-06-11 11:46:26.000000000 -0400
23b3e9
+++ Python-2.7.2/Modules/_hashopenssl.c	2011-09-14 00:21:26.199252001 -0400
23b3e9
@@ -36,6 +36,8 @@
23b3e9
 #endif
23b3e9
 
23b3e9
 /* EVP is the preferred interface to hashing in OpenSSL */
23b3e9
+#include <openssl/ssl.h>
23b3e9
+#include <openssl/err.h>
23b3e9
 #include <openssl/evp.h>
23b3e9
 
23b3e9
 #define MUNCH_SIZE INT_MAX
23b3e9
@@ -65,11 +67,19 @@ typedef struct {
23b3e9
 
23b3e9
 static PyTypeObject EVPtype;
23b3e9
 
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;
23b3e9
 
23b3e9
-#define DEFINE_CONSTS_FOR_NEW(Name)  \
23b3e9
-    static PyObject *CONST_ ## Name ## _name_obj = NULL; \
23b3e9
-    static EVP_MD_CTX CONST_new_ ## Name ## _ctx; \
23b3e9
-    static EVP_MD_CTX *CONST_new_ ## Name ## _ctx_p = NULL;
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)
23b3e9
@@ -115,6 +125,48 @@ EVP_hash(EVPobject *self, const void *vp
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
23b3e9
@@ -313,14 +365,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
 
23b3e9
@@ -336,7 +389,12 @@ EVP_tp_init(EVPobject *self, PyObject *a
23b3e9
         PyBuffer_Release(&view);
23b3e9
         return -1;
23b3e9
     }
23b3e9
-    EVP_DigestInit(&self->ctx, digest);
23b3e9
+    mc_ctx_init(&self->ctx, usedforsecurity);
23b3e9
+    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);
23b3e9
@@ -420,7 +478,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
 
23b3e9
@@ -435,7 +494,12 @@ EVPnew(PyObject *name_obj,
23b3e9
     if (initial_ctx) {
23b3e9
         EVP_MD_CTX_copy(&self->ctx, initial_ctx);
23b3e9
     } else {
23b3e9
-        EVP_DigestInit(&self->ctx, digest);
23b3e9
+        mc_ctx_init(&self->ctx, usedforsecurity);
23b3e9
+        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) {
23b3e9
@@ -459,20 +523,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
 
23b3e9
@@ -484,58 +556,118 @@ EVP_new(PyObject *self, PyObject *args,
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;
23b3e9
 }
23b3e9
 
23b3e9
 /*
23b3e9
- *  This macro generates constructor function definitions for specific
23b3e9
- *  hash algorithms.  These constructors are much faster than calling
23b3e9
- *  the generic one passing it a python string and are noticably
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) \
23b3e9
+    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; \
23b3e9
+        return implement_specific_EVP_new(self, args, kwdict,      \
23b3e9
+                                          "|s*i:" #NAME,           \
23b3e9
+                                          &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
+{
23b3e9
+    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)) { \
23b3e9
-            CONST_new_ ## NAME ## _ctx_p = &CONST_new_ ## NAME ## _ctx; \
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.
23b3e9
+  
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
23b3e9
@@ -565,13 +700,10 @@ init_hashlib(void)
23b3e9
 {
23b3e9
     PyObject *m;
23b3e9
 
23b3e9
+    SSL_load_error_strings();
23b3e9
+    SSL_library_init();
23b3e9
     OpenSSL_add_all_digests();
23b3e9
 
23b3e9
-    /* TODO build EVP_functions openssl_* entries dynamically based
23b3e9
-     * on what hashes are supported rather than listing many
23b3e9
-     * but having some be unsupported.  Only init appropriate
23b3e9
-     * constants. */
23b3e9
-
23b3e9
     Py_TYPE(&EVPtype) = &PyType_Type;
23b3e9
     if (PyType_Ready(&EVPtype) < 0)
23b3e9
         return;
23b3e9
diff -up Python-2.7.2/Modules/Setup.dist.hashlib-fips Python-2.7.2/Modules/Setup.dist
23b3e9
--- Python-2.7.2/Modules/Setup.dist.hashlib-fips	2011-09-14 00:21:26.163252001 -0400
23b3e9
+++ Python-2.7.2/Modules/Setup.dist	2011-09-14 00:21:26.201252001 -0400
23b3e9
@@ -248,14 +248,14 @@ imageop imageop.c	# Operations on images
23b3e9
 # Message-Digest Algorithm, described in RFC 1321.  The necessary files
23b3e9
 # md5.c and md5.h are included here.
23b3e9
 
23b3e9
-_md5 md5module.c md5.c
23b3e9
+#_md5 md5module.c md5.c
23b3e9
 
23b3e9
 
23b3e9
 # The _sha module implements the SHA checksum algorithms.
23b3e9
 # (NIST's Secure Hash Algorithms.)
23b3e9
-_sha shamodule.c
23b3e9
-_sha256 sha256module.c
23b3e9
-_sha512 sha512module.c
23b3e9
+#_sha shamodule.c
23b3e9
+#_sha256 sha256module.c
23b3e9
+#_sha512 sha512module.c
23b3e9
 
23b3e9
 
23b3e9
 # SGI IRIX specific modules -- off by default.
23b3e9
diff -up Python-2.7.2/setup.py.hashlib-fips Python-2.7.2/setup.py
23b3e9
--- Python-2.7.2/setup.py.hashlib-fips	2011-09-14 00:21:25.722252001 -0400
23b3e9
+++ Python-2.7.2/setup.py	2011-09-14 00:21:26.203252001 -0400
23b3e9
@@ -768,21 +768,6 @@ class PyBuildExt(build_ext):
23b3e9
                 print ("warning: openssl 0x%08x is too old for _hashlib" %
23b3e9
                        openssl_ver)
23b3e9
                 missing.append('_hashlib')
23b3e9
-        if COMPILED_WITH_PYDEBUG or not have_usable_openssl:
23b3e9
-            # The _sha module implements the SHA1 hash algorithm.
23b3e9
-            exts.append( Extension('_sha', ['shamodule.c']) )
23b3e9
-            # The _md5 module implements the RSA Data Security, Inc. MD5
23b3e9
-            # Message-Digest Algorithm, described in RFC 1321.  The
23b3e9
-            # necessary files md5.c and md5.h are included here.
23b3e9
-            exts.append( Extension('_md5',
23b3e9
-                            sources = ['md5module.c', 'md5.c'],
23b3e9
-                            depends = ['md5.h']) )
23b3e9
-
23b3e9
-        min_sha2_openssl_ver = 0x00908000
23b3e9
-        if COMPILED_WITH_PYDEBUG or openssl_ver < min_sha2_openssl_ver:
23b3e9
-            # OpenSSL doesn't do these until 0.9.8 so we'll bring our own hash
23b3e9
-            exts.append( Extension('_sha256', ['sha256module.c']) )
23b3e9
-            exts.append( Extension('_sha512', ['sha512module.c']) )
23b3e9
 
23b3e9
         # Modules that provide persistent dictionary-like semantics.  You will
23b3e9
         # probably want to arrange for at least one of them to be available on