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