summaryrefslogtreecommitdiff
path: root/passlib/hash/sha_crypt.py
blob: 22bd59052d6bc9cd951ef9f7bd2f36b59249d64d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
"""passlib.hash.sha_crypt - implements SHA-256-Crypt & SHA-512-Crypt

Implementation written based on specification at `<http://www.akkadia.org/drepper/SHA-crypt.txt>`_.
It should be byte-compatible with unix shadow hashes beginning with ``$5$``.
"""
#=========================================================
#imports
#=========================================================
from __future__ import with_statement
#core
import re
import hashlib
import logging; log = logging.getLogger(__name__)
import time
import os
#site
#libs
from passlib.hash.base import CryptAlgorithm, HashInfo
from passlib.util import classproperty, abstractmethod, is_seq, srandom, H64
#pkg
#local
__all__ = [
    'Sha256Crypt',
    'Sha512Crypt',
]

#=========================================================
#ids 5,6 -- sha
#algorithm defined on this page:
#   http://people.redhat.com/drepper/SHA-crypt.txt
#=========================================================
class _ShaCrypt(CryptAlgorithm):
    "common code for used by Sha(256|512)Crypt Classes"
    #=========================================================
    #algorithm info
    #=========================================================
    #hash_bits, name filled in for subclass
    salt_bits = 96
    has_rounds = True
    has_named_rounds = True

    #tuning the round aliases
    rounds_per_second = 156000 #last tuned 2009-7-6 on a 2gz system
    fast_rounds = int(rounds_per_second * .25)
    medium_rounds = int(rounds_per_second * .75)
    slow_rounds = int(rounds_per_second * 1.5)

    #=========================================================
    #internals required from subclass
    #=========================================================
    _key = None #alg id (5, 6) of specific sha alg
    _hash = None #callable to use for hashing
    _chunk_size = None #bytes at a time to input secret
    _hash_size = None #bytes in hash
    _pat = None #regexp for sha variant

    @abstractmethod
    def _encode(self, result):
        "encode raw result into h64 style"

    #=========================================================
    #core sha crypt algorithm
    #=========================================================
    @classmethod
    def _sha_crypt_raw(self, rounds, salt, secret):
        "perform sha crypt, returning just the checksum"
        #setup alg-specific parameters
        hash = self._hash
        chunk_size = self._chunk_size

        #init salt
        if salt is None:
            salt = H64.randstr(16)
        elif len(salt) > 16:
            salt = salt[:16] #spec says to use up to first chars 16 only

        #init rounds
        if rounds == -1:
            real_rounds = 5000
        else:
            if rounds < 1000:
                rounds = 1000
            if rounds > 999999999:
                rounds = 999999999
            real_rounds = rounds

        def extend(source, size_ref):
            size = len(size_ref)
            return source * int(size/chunk_size) + source[:size % chunk_size]

        #calc digest B
        b = hash()
        b.update(secret)
        b.update(salt)
        b.update(secret)
        b_result = b.digest()
        b_extend = extend(b_result, secret)

        #begin digest A
        a = hash()
        a.update(secret)
        a.update(salt)
        a.update(b_extend)

        #for each bit in slen, add B or SECRET
        value = len(secret)
        while value > 0:
            if value % 2:
                a.update(b_result)
            else:
                a.update(secret)
            value >>= 1

        #finish A
        a_result = a.digest()

        #calc DP
        dp = hash()
        dp.update(secret * len(secret))
        dp_result = extend(dp.digest(), secret)

        #calc DS
        ds = hash()
        for i in xrange(0, 16+ord(a_result[0])):
            ds.update(salt)
        ds_result = extend(ds.digest(), salt) #aka 'S'

        #calc digest C
        last_result = a_result
        for i in xrange(0, real_rounds):
            c = hash()
            if i % 2:
                c.update(dp_result)
            else:
                c.update(last_result)
            if i % 3:
                c.update(ds_result)
            if i % 7:
                c.update(dp_result)
            if i % 2:
                c.update(last_result)
            else:
                c.update(dp_result)
            last_result = c.digest()

        #encode result using 256/512 specific func
        out = self._encode(last_result)
        assert len(out) == self._hash_size, "wrong length: %r" % (out,)
        return HashInfo(self._key, salt, out, rounds=rounds)

    @classmethod
    def _sha_crypt(self, rounds, salt, secret):
        rec = self._sha_crypt_raw(rounds, salt, secret)
        if rec.rounds == -1:
            return "$%s$%s$%s" % (rec.alg, rec.salt, rec.chk)
        else:
            return "$%s$rounds=%d$%s$%s" % (rec.alg, rec.rounds, rec.salt, rec.chk)

    #=========================================================
    #frontend helpers
    #=========================================================
    @classmethod
    def identify(self, hash):
        "identify bcrypt hash"
        if hash is None:
            return False
        return self._pat.match(hash) is not None

    @classmethod
    def _parse(self, hash):
        "parse bcrypt hash"
        m = self._pat.match(hash)
        if not m:
            raise ValueError, "invalid sha hash/salt"
        alg, rounds, salt, chk = m.group("alg", "rounds", "salt", "chk")
        if rounds is None:
            rounds = -1 #indicate we're using the default mode
        else:
            rounds = int(rounds)
        assert alg == self._key
        return HashInfo(alg, salt, chk, rounds=rounds, source=hash)

    @classmethod
    def encrypt(self, secret, hash=None, rounds=None, keep_salt=False):
        """encrypt using sha256/512-crypt.

        In addition to the normal options that :meth:`CryptAlgorithm.encrypt` takes,
        this function also accepts the following:

        :param rounds:
            Optionally specify the number of rounds to use.
            This can be one of "fast", "medium", "slow",
            or an integer in the range 1000...999999999.

            See :attr:`CryptAlgorithm.has_named_rounds` for details
            on the meaning of "fast", "medium" and "slow".
        """
        salt = None
        if hash:
            rec = self._parse(hash)
            if keep_salt:
                salt = rec.salt
            if rounds is None:
                rounds = rec.rounds
        rounds = self._norm_rounds(rounds)
        return self._sha_crypt(rounds, salt, secret)

    @classmethod
    def _norm_rounds(self, rounds):
        if isinstance(rounds, int):
            return rounds
        elif rounds == "fast" or rounds is None:
            return self.fast_rounds
        elif rounds == "slow":
            return self.slow_rounds
        else:
            if rounds != "medium":
                log.warning("unknown rounds alias %r, using 'medium'", rounds)
            return self.medium_rounds

    @classmethod
    def verify(self, secret, hash):
        if hash is None:
            return False
        rec = self._parse(hash)
        other = self._sha_crypt_raw(rec.rounds, rec.salt, secret)
        return other.chk == rec.chk

    #=========================================================
    #eoc
    #=========================================================

class Sha256Crypt(_ShaCrypt):
    """This class implements the SHA-256 Crypt Algorithm,
    according to the specification at `<http://people.redhat.com/drepper/SHA-crypt.txt>`_.
    It should be byte-compatible with unix shadow hashes beginning with ``$5$``.

    See Sha512Crypt for usage examples and details.
    """
    #=========================================================
    #algorithm info
    #=========================================================
    name='sha-256-crypt'
    hash_bits = 256

    #=========================================================
    #internals
    #=========================================================
    _hash = hashlib.sha256
    _key = '5'
    _chunk_size = 32
    _hash_size = 43

    @classmethod
    def _encode(self, result):
        out = ''
        a, b, c = 0, 10, 20
        while a < 30:
            out += H64.encode_3_offsets(result, c, b, a)
            a, b, c = c+1, a+1, b+1
        assert a == 30, "loop went to far: %r" % (a,)
        out += H64.encode_2_offsets(result, 30, 31)
        return out

    #=========================================================
    #frontend
    #=========================================================
    _pat = re.compile(r"""
        ^
        \$(?P<alg>5)
        (\$rounds=(?P<rounds>\d+))?
        \$(?P<salt>[A-Za-z0-9./]+)
        (\$(?P<chk>[A-Za-z0-9./]+))?
        $
        """, re.X)

    #=========================================================
    #eof
    #=========================================================

class Sha512Crypt(_ShaCrypt):
    """This class implements the SHA-512 Crypt Algorithm,
    according to the specification at `http://people.redhat.com/drepper/SHA-crypt.txt`_.
    It should be byte-compatible with unix shadow hashes beginning with ``$6$``.

    This implementation is based on a pure-python translation
    of the original specification.

    .. note::
        This is *not* just the raw SHA-512 hash of the password,
        which is sometimes incorrectly referred to as sha512-crypt.
        This is a variable-round descendant of md5-crypt,
        and is comparable in strength to bcrypt.

    Usage Example::

        >>> from passlib.hash import Sha512Crypt
        >>> crypt = Sha512Crypt()
        >>> #to encrypt a new secret with this algorithm
        >>> hash = crypt.encrypt("forget me not")
        >>> hash
        '$6$rounds=11949$KkBupsnnII6YXqgT$O8qAEcEgDyJlMC4UB3buST8vE1PsPPABA.0lQIUARTNnlLPZyBRVXAvqqynVByGRLTRMIorkcR0bsVQS5i3Xw1'
        >>> #to verify an existing secret
        >>> crypt.verify("forget me not", hash)
        True
        >>> crypt.verify("i forgot it", hash)
        False

    .. automethod:: encrypt
    """
    #=========================================================
    #algorithm info
    #=========================================================
    name='sha-512-crypt'
    hash_bits = 512

    #=========================================================
    #internals
    #=========================================================
    _hash = hashlib.sha512
    _key = '6'
    _chunk_size = 64
    _hash_size = 86

    @classmethod
    def _encode(self, result):
        out = ''
        a, b, c = 0, 21, 42
        while c < 63:
            out += H64.encode_3_offsets(result, c, b, a)
            a, b, c = b+1, c+1, a+1
        assert c == 63, "loop to far: %r" % (c,)
        out += H64.encode_1_offset(result, 63)
        return out

    #=========================================================
    #frontend
    #=========================================================

    _pat = re.compile(r"""
        ^
        \$(?P<alg>6)
        (\$rounds=(?P<rounds>\d+))?
        \$(?P<salt>[A-Za-z0-9./]+)
        (\$(?P<chk>[A-Za-z0-9./]+))?
        $
        """, re.X)

    #=========================================================
    #eof
    #=========================================================

#=========================================================
# eof
#=========================================================