Blame SOURCES/00165-crypt-module-salt-backport.patch

925e6b
diff -up Python-2.7.3/Doc/library/crypt.rst.crypt-module-salt-backport Python-2.7.3/Doc/library/crypt.rst
925e6b
--- Python-2.7.3/Doc/library/crypt.rst.crypt-module-salt-backport	2012-04-09 19:07:28.000000000 -0400
925e6b
+++ Python-2.7.3/Doc/library/crypt.rst	2013-02-19 16:44:20.465334062 -0500
925e6b
@@ -16,9 +16,9 @@
925e6b
 
925e6b
 This module implements an interface to the :manpage:`crypt(3)` routine, which is
925e6b
 a one-way hash function based upon a modified DES algorithm; see the Unix man
925e6b
-page for further details.  Possible uses include allowing Python scripts to
925e6b
-accept typed passwords from the user, or attempting to crack Unix passwords with
925e6b
-a dictionary.
925e6b
+page for further details.  Possible uses include storing hashed passwords
925e6b
+so you can check passwords without storing the actual password, or attempting
925e6b
+to crack Unix passwords with a dictionary.
925e6b
 
925e6b
 .. index:: single: crypt(3)
925e6b
 
925e6b
@@ -27,15 +27,81 @@ the :manpage:`crypt(3)` routine in the r
925e6b
 extensions available on the current implementation will also  be available on
925e6b
 this module.
925e6b
 
925e6b
+Hashing Methods
925e6b
+---------------
925e6b
 
925e6b
-.. function:: crypt(word, salt)
925e6b
+The :mod:`crypt` module defines the list of hashing methods (not all methods
925e6b
+are available on all platforms):
925e6b
+
925e6b
+.. data:: METHOD_SHA512
925e6b
+
925e6b
+   A Modular Crypt Format method with 16 character salt and 86 character
925e6b
+   hash.  This is the strongest method.
925e6b
+
925e6b
+.. versionadded:: 3.3
925e6b
+
925e6b
+.. data:: METHOD_SHA256
925e6b
+
925e6b
+   Another Modular Crypt Format method with 16 character salt and 43
925e6b
+   character hash.
925e6b
+
925e6b
+.. versionadded:: 3.3
925e6b
+
925e6b
+.. data:: METHOD_MD5
925e6b
+
925e6b
+   Another Modular Crypt Format method with 8 character salt and 22
925e6b
+   character hash.
925e6b
+
925e6b
+.. versionadded:: 3.3
925e6b
+
925e6b
+.. data:: METHOD_CRYPT
925e6b
+
925e6b
+   The traditional method with a 2 character salt and 13 characters of
925e6b
+   hash.  This is the weakest method.
925e6b
+
925e6b
+.. versionadded:: 3.3
925e6b
+
925e6b
+
925e6b
+Module Attributes
925e6b
+-----------------
925e6b
+
925e6b
+
925e6b
+.. attribute:: methods
925e6b
+
925e6b
+   A list of available password hashing algorithms, as
925e6b
+   ``crypt.METHOD_*`` objects.  This list is sorted from strongest to
925e6b
+   weakest, and is guaranteed to have at least ``crypt.METHOD_CRYPT``.
925e6b
+
925e6b
+.. versionadded:: 3.3
925e6b
+
925e6b
+
925e6b
+Module Functions
925e6b
+----------------
925e6b
+
925e6b
+The :mod:`crypt` module defines the following functions:
925e6b
+
925e6b
+.. function:: crypt(word, salt=None)
925e6b
 
925e6b
    *word* will usually be a user's password as typed at a prompt or  in a graphical
925e6b
-   interface.  *salt* is usually a random two-character string which will be used
925e6b
-   to perturb the DES algorithm in one of 4096 ways.  The characters in *salt* must
925e6b
-   be in the set ``[./a-zA-Z0-9]``.  Returns the hashed password as a string, which
925e6b
-   will be composed of characters from the same alphabet as the salt (the first two
925e6b
-   characters represent the salt itself).
925e6b
+   interface.  The optional *salt* is either a string as returned from
925e6b
+   :func:`mksalt`, one of the ``crypt.METHOD_*`` values (though not all
925e6b
+   may be available on all platforms), or a full encrypted password
925e6b
+   including salt, as returned by this function.  If *salt* is not
925e6b
+   provided, the strongest method will be used (as returned by
925e6b
+   :func:`methods`.
925e6b
+
925e6b
+   Checking a password is usually done by passing the plain-text password
925e6b
+   as *word* and the full results of a previous :func:`crypt` call,
925e6b
+   which should be the same as the results of this call.
925e6b
+
925e6b
+   *salt* (either a random 2 or 16 character string, possibly prefixed with
925e6b
+   ``$digit$`` to indicate the method) which will be used to perturb the
925e6b
+   encryption algorithm.  The characters in *salt* must be in the set
925e6b
+   ``[./a-zA-Z0-9]``, with the exception of Modular Crypt Format which
925e6b
+   prefixes a ``$digit$``.
925e6b
+
925e6b
+   Returns the hashed password as a string, which will be composed of
925e6b
+   characters from the same alphabet as the salt.
925e6b
 
925e6b
    .. index:: single: crypt(3)
925e6b
 
925e6b
@@ -43,6 +109,27 @@ this module.
925e6b
    different sizes in the *salt*, it is recommended to use  the full crypted
925e6b
    password as salt when checking for a password.
925e6b
 
925e6b
+.. versionchanged:: 3.3
925e6b
+   Before version 3.3, *salt*  must be specified as a string and cannot
925e6b
+   accept ``crypt.METHOD_*`` values (which don't exist anyway).
925e6b
+
925e6b
+
925e6b
+.. function:: mksalt(method=None)
925e6b
+
925e6b
+   Return a randomly generated salt of the specified method.  If no
925e6b
+   *method* is given, the strongest method available as returned by
925e6b
+   :func:`methods` is used.
925e6b
+
925e6b
+   The return value is a string either of 2 characters in length for
925e6b
+   ``crypt.METHOD_CRYPT``, or 19 characters starting with ``$digit$`` and
925e6b
+   16 random characters from the set ``[./a-zA-Z0-9]``, suitable for
925e6b
+   passing as the *salt* argument to :func:`crypt`.
925e6b
+
925e6b
+.. versionadded:: 3.3
925e6b
+
925e6b
+Examples
925e6b
+--------
925e6b
+
925e6b
 A simple example illustrating typical use::
925e6b
 
925e6b
    import crypt, getpass, pwd
925e6b
@@ -59,3 +146,11 @@ A simple example illustrating typical us
925e6b
        else:
925e6b
            return 1
925e6b
 
925e6b
+To generate a hash of a password using the strongest available method and
925e6b
+check it against the original::
925e6b
+
925e6b
+   import crypt
925e6b
+
925e6b
+   hashed = crypt.crypt(plaintext)
925e6b
+   if hashed != crypt.crypt(plaintext, hashed):
925e6b
+      raise "Hashed version doesn't validate against original"
925e6b
diff -up Python-2.7.3/Lib/crypt.py.crypt-module-salt-backport Python-2.7.3/Lib/crypt.py
925e6b
--- Python-2.7.3/Lib/crypt.py.crypt-module-salt-backport	2013-02-19 16:44:20.465334062 -0500
925e6b
+++ Python-2.7.3/Lib/crypt.py	2013-02-19 16:49:56.425311089 -0500
925e6b
@@ -0,0 +1,71 @@
925e6b
+"""Wrapper to the POSIX crypt library call and associated functionality.
925e6b
+
925e6b
+Note that the ``methods`` and ``METHOD_*`` attributes are non-standard
925e6b
+extensions to Python 2.7, backported from 3.3"""
925e6b
+
925e6b
+import _crypt
925e6b
+import string as _string
925e6b
+from random import SystemRandom as _SystemRandom
925e6b
+from collections import namedtuple as _namedtuple
925e6b
+
925e6b
+
925e6b
+_saltchars = _string.ascii_letters + _string.digits + './'
925e6b
+_sr = _SystemRandom()
925e6b
+
925e6b
+
925e6b
+class _Method(_namedtuple('_Method', 'name ident salt_chars total_size')):
925e6b
+
925e6b
+    """Class representing a salt method per the Modular Crypt Format or the
925e6b
+    legacy 2-character crypt method."""
925e6b
+
925e6b
+    def __repr__(self):
925e6b
+        return '<crypt.METHOD_%s>' % self.name
925e6b
+
925e6b
+
925e6b
+def mksalt(method=None):
925e6b
+    """Generate a salt for the specified method.
925e6b
+
925e6b
+    If not specified, the strongest available method will be used.
925e6b
+
925e6b
+    This is a non-standard extension to Python 2.7, backported from 3.3
925e6b
+    """
925e6b
+    if method is None:
925e6b
+        method = methods[0]
925e6b
+    s = '$%s$' % method.ident if method.ident else ''
925e6b
+    s += ''.join(_sr.sample(_saltchars, method.salt_chars))
925e6b
+    return s
925e6b
+
925e6b
+
925e6b
+def crypt(word, salt=None):
925e6b
+    """Return a string representing the one-way hash of a password, with a salt
925e6b
+    prepended.
925e6b
+
925e6b
+    If ``salt`` is not specified or is ``None``, the strongest
925e6b
+    available method will be selected and a salt generated.  Otherwise,
925e6b
+    ``salt`` may be one of the ``crypt.METHOD_*`` values, or a string as
925e6b
+    returned by ``crypt.mksalt()``.
925e6b
+
925e6b
+    Note that these are non-standard extensions to Python 2.7's crypt.crypt()
925e6b
+    entrypoint, backported from 3.3: the standard Python 2.7 crypt.crypt()
925e6b
+    entrypoint requires two strings as the parameters, and does not support
925e6b
+    keyword arguments.
925e6b
+    """
925e6b
+    if salt is None or isinstance(salt, _Method):
925e6b
+        salt = mksalt(salt)
925e6b
+    return _crypt.crypt(word, salt)
925e6b
+
925e6b
+
925e6b
+#  available salting/crypto methods
925e6b
+METHOD_CRYPT = _Method('CRYPT', None, 2, 13)
925e6b
+METHOD_MD5 = _Method('MD5', '1', 8, 34)
925e6b
+METHOD_SHA256 = _Method('SHA256', '5', 16, 63)
925e6b
+METHOD_SHA512 = _Method('SHA512', '6', 16, 106)
925e6b
+
925e6b
+methods = []
925e6b
+for _method in (METHOD_SHA512, METHOD_SHA256, METHOD_MD5):
925e6b
+    _result = crypt('', _method)
925e6b
+    if _result and len(_result) == _method.total_size:
925e6b
+        methods.append(_method)
925e6b
+methods.append(METHOD_CRYPT)
925e6b
+del _result, _method
925e6b
+
925e6b
diff -up Python-2.7.3/Lib/test/test_crypt.py.crypt-module-salt-backport Python-2.7.3/Lib/test/test_crypt.py
925e6b
--- Python-2.7.3/Lib/test/test_crypt.py.crypt-module-salt-backport	2012-04-09 19:07:31.000000000 -0400
925e6b
+++ Python-2.7.3/Lib/test/test_crypt.py	2013-02-19 16:44:20.465334062 -0500
925e6b
@@ -10,6 +10,25 @@ class CryptTestCase(unittest.TestCase):
925e6b
         if test_support.verbose:
925e6b
             print 'Test encryption: ', c
925e6b
 
925e6b
+    def test_salt(self):
925e6b
+        self.assertEqual(len(crypt._saltchars), 64)
925e6b
+        for method in crypt.methods:
925e6b
+            salt = crypt.mksalt(method)
925e6b
+            self.assertEqual(len(salt),
925e6b
+                    method.salt_chars + (3 if method.ident else 0))
925e6b
+
925e6b
+    def test_saltedcrypt(self):
925e6b
+        for method in crypt.methods:
925e6b
+            pw = crypt.crypt('assword', method)
925e6b
+            self.assertEqual(len(pw), method.total_size)
925e6b
+            pw = crypt.crypt('assword', crypt.mksalt(method))
925e6b
+            self.assertEqual(len(pw), method.total_size)
925e6b
+
925e6b
+    def test_methods(self):
925e6b
+        # Gurantee that METHOD_CRYPT is the last method in crypt.methods.
925e6b
+        self.assertTrue(len(crypt.methods) >= 1)
925e6b
+        self.assertEqual(crypt.METHOD_CRYPT, crypt.methods[-1])
925e6b
+
925e6b
 def test_main():
925e6b
     test_support.run_unittest(CryptTestCase)
925e6b
 
925e6b
diff -up Python-2.7.3/Modules/cryptmodule.c.crypt-module-salt-backport Python-2.7.3/Modules/cryptmodule.c
925e6b
--- Python-2.7.3/Modules/cryptmodule.c.crypt-module-salt-backport	2012-04-09 19:07:34.000000000 -0400
925e6b
+++ Python-2.7.3/Modules/cryptmodule.c	2013-02-19 16:44:20.466334063 -0500
925e6b
@@ -43,7 +43,7 @@ static PyMethodDef crypt_methods[] = {
925e6b
 };
925e6b
 
925e6b
 PyMODINIT_FUNC
925e6b
-initcrypt(void)
925e6b
+init_crypt(void)
925e6b
 {
925e6b
-    Py_InitModule("crypt", crypt_methods);
925e6b
+    Py_InitModule("_crypt", crypt_methods);
925e6b
 }
925e6b
diff -up Python-2.7.3/Modules/Setup.dist.crypt-module-salt-backport Python-2.7.3/Modules/Setup.dist
925e6b
--- Python-2.7.3/Modules/Setup.dist.crypt-module-salt-backport	2013-02-19 16:44:20.463334063 -0500
925e6b
+++ Python-2.7.3/Modules/Setup.dist	2013-02-19 16:44:20.466334063 -0500
925e6b
@@ -221,7 +221,7 @@ _ssl _ssl.c \
925e6b
 #
925e6b
 # First, look at Setup.config; configure may have set this for you.
925e6b
 
925e6b
-crypt cryptmodule.c # -lcrypt	# crypt(3); needs -lcrypt on some systems
925e6b
+_crypt _cryptmodule.c -lcrypt	# crypt(3); needs -lcrypt on some systems
925e6b
 
925e6b
 
925e6b
 # Some more UNIX dependent modules -- off by default, since these
925e6b
diff -up Python-2.7.3/setup.py.crypt-module-salt-backport Python-2.7.3/setup.py
925e6b
--- Python-2.7.3/setup.py.crypt-module-salt-backport	2013-02-19 16:44:20.425334067 -0500
925e6b
+++ Python-2.7.3/setup.py	2013-02-19 16:44:20.466334063 -0500
925e6b
@@ -693,7 +693,7 @@ class PyBuildExt(build_ext):
925e6b
             libs = ['crypt']
925e6b
         else:
925e6b
             libs = []
925e6b
-        exts.append( Extension('crypt', ['cryptmodule.c'], libraries=libs) )
925e6b
+        exts.append( Extension('_crypt', ['_cryptmodule.c'], libraries=libs) )
925e6b
 
925e6b
         # CSV files
925e6b
         exts.append( Extension('_csv', ['_csv.c']) )