Current File : //usr/lib/python2.7/site-packages/passlib/crypto/des.pyc
�
�
5Xc@sdZddlZddlmZddlmZmZmZmZmZddgZ	dZ
dZd	Zd
Z
dZdZeaaaad
�Zd�Zejd�Zd�Zd�Zd�Zd�Zeddd�Zd�Zd�Zddd�Z ddd�Z!dS(s�passlib.crypto.des -- DES block encryption routines

History
=======
These routines (which have since been drastically modified for python)
are based on a Java implementation of the des-crypt algorithm,
found at `<http://www.dynamic.net.au/christos/crypt/UnixCrypt2.txt>`_.

The copyright & license for that source is as follows::

    UnixCrypt.java 0.9 96/11/25
    Copyright (c) 1996 Aki Yoshida. All rights reserved.
    Permission to use, copy, modify and distribute this software
    for non-commercial or commercial purposes and without fee is
    hereby granted provided that this copyright notice appears in
    all copies.

    ---

    Unix crypt(3C) utility
    @version 0.9, 11/25/96
    @author  Aki Yoshida

    ---

    modified April 2001
    by Iris Van den Broeke, Daniel Deville

    ---
    Unix Crypt.
    Implements the one way cryptography used by Unix systems for
    simple password protection.
    @version $Id: UnixCrypt2.txt,v 1.1.1.1 2005/09/13 22:20:13 christos Exp $
    @author Greg Wilkins (gregw)

The netbsd des-crypt implementation has some nice notes on how this all works -
    http://fxr.googlebit.com/source/lib/libcrypt/crypt.c?v=NETBSD-CURRENT
i����N(texc(tjoin_byte_valuestbyte_elem_valuetirangeRt	int_typestexpand_des_keytdes_encrypt_blocki���I�������l����l�~�}�{�wIl���s�gcGCsfd�d�d�d�d�d�d�d�d�d�d�d�d�d�d�d�f}d�d�dddddddddd	d
ddd
f}ddddddddddddddddf}||f||f||f||f||f||f||f||ffaddd d!d"d#d$d%fad&d'd(d)d*d+d,d-fad.d/d0d1d2d3d4d5d6d7d8d9d:d;d<d=fad�S(>s3delay loading tables until they are actually neededii i i  ii$i i $I II I�I� I�I� II II I�I� I�I� i@i i@ i i@ i  i@  IIIIII	I	I@I@IHIHIAIAIIIIiIIiiIII@II@I�I@�I�I@�I�I�@I�I�@I��I�@�I��I�@�iiii@iPi@iPIIIIIIIIIIIIIIIi�ii�i�i��i�i��iI I III I I I I  I  I0I0I 0I 0i@ii@ii@ii@i�I@I�@I@I�@I@@I�@@lllllllli@ii@ii@ii@IIIIIIII I I!I!I I I!I!IIIiIIII�II�II�II�II�II�II�II�i Ii(I I(iIIi i(I I(I�I�I@I@�I@�II�I�i@I@I@�I@�i@ii�i�i�i@i@i@i�@i�@i�@i�@ii i$i$II I III$I$i�II�I�I I� I� I0I0I�0I�0IHIPIIXI@@I@IH@I@IP@I@IX@i@ I@I I@ I@I I@ II@I I@ IIIIIIIIIII@ I�@I� I�@ I@I I@ I�I�@I� I�@ l I l�l� Ill(I I l�l�(I�I@�I@I@�II�IDID�IDID�I�I�II�I �I$I$�I I �I$I$�ii@�I��I@�I@��i�i@i@�I�I��I@�I@��i@I i� I�I� i II i�i� I�I� I IIIiIIiiIIiI@I I@ i@�I�I@�i� i@� I� I@� iIIIllll l l IIII
II
II
II
i @I@@I @I @@I@I I @I@I@@I @I @@I� I�� I�I� I��I  I� I�  I�� I I @I@I @I @IDI DI @@ID@I D@I@ I@I(I@(I�I@�I�I@�I�I@�IIIIIIIIIIIIIIIIIIIIi�i��I�I��I��I���I�I��I��I���I��I���I���I����I�I�I��I�I��I��I���l@l @l Dl  Dl�@@l� @Dl� D@l�  DDiiiiiiiiiIIIIIIIIIi  i  i   i  i  i   i   i   i    I  I  I   I  I  I   I   I   I    i@I@ID@i@i@@I@@ID@@i@iD@I@@IDD@iD@iDD@I@D@IDDD@I@I@ID@I@I@@I@@ID@@I@ID@I@@IDD@ID@IDD@I@D@IDDD@I ��I  ��I ��I �I �I �I  �I �I��I  �I@�I�i@I@I�I@�I@II@�I@�I�i@I@@I@I@I@I@I@@I@@II@@I@I�@II�@i@I�I�@I@I�I�I�@I@I�@ I� I�@I@ I�I� I@I�@ I�@I@ I�@I @I�  I� @I @I�  @I  I� i� I  @I� @I I@I@@ I@@I@ I@ I@ I@I@ I@@I@@ l @I � l�@I  l@I �l �@I� l�@l �@l @IIIIIIIIIIiiiiiiiiiiiiiiiI��I��I���lllllllIIIIIIIIIIIi��i��i���I��I��I���I���I���I����iiiiiiiiiiiIIIIIIIIIiiiiiiiiiiiiiiiI@@I@@I@@@I@@I@@@I@@I@@@I@@@I@@@@IIIIIIIIIIIi@@i@@i@@i@@@i@@i@@i@@@i@@i@@@i@@@i@@@@iiiiiiiiiiN(iii i i i i  i  iii$i$i i i $i $(iI II I�I� I�I� II II I�I� I�I� (iii@i@i i i@ i@ i i i@ i@ i  i  i@  i@  (iIIIIII	I	I@I@IHIHIAIAIIII(iiiiIIIIiiiiIIII(iI@II@I�I@�I�I@�I�I�@I�I�@I��I�@�I��I�@�(iiiiiiiii@i@iPiPi@i@iPiP(iIIIIIIIIIIIIIII(iii�i�iii�i�i�i�i��i��i�i�i��i��(iiI I III I I I I  I  I0I0I 0I 0(iii@i@iii@i@iii@i@iii@i@(ii�I@I�@I@I�@I@@I�@@llllllll(iii@i@iii@i@iii@i@iii@i@(iIIIIIIII I I!I!I I I!I!(iiIIIIIIiiIIIIII(iI�II�II�II�II�II�II�II�(iiiii i i i iiiii i i i (iiIIi i(I I(iiIIi i(I I((iiiiIIIIi�i�i�i�I�I�I�I�(iII�I�i@I@I@�I@�iII�I�i@I@I@�I@�(iiiii@i@i@i@iiiii@i@i@i@(iiiii�i�i�i�i@i@i@i@i�@i�@i�@i�@(iiiiiiiiiiiiiiii(iii i iii$i$III I III$I$(iii�i�III�I�I I I� I� I0I0I�0I�0(iI@IIHIIPIIXI@I@@I@IH@I@IP@I@IX@(ii@i i@ II@I I@ II@I I@ II@I I@ (iIIIIIIIIIIIIIII(iI@I I@ I�I�@I� I�@ II@I I@ I�I�@I� I�@ (iIll I I l�l� IIll(I I l�l�((iI�II�I@I@�I@I@�II�II�IDID�IDID�(iI�II�II�II�I I �I$I$�I I �I$I$�(iiiiiiiiiiiiiiii(ii�i@i@�I�I��I@�I@��ii�i@i@�I�I��I@�I@��(iiiiiiiii@i@i@i@i@i@i@i@(ii II i�i� I�I� ii II i�i� I�I� (iiiii i i i IIIII I I I (iiIIiiIIiiIIiiII(iiiiiiiiiiiiiiii(ii@II@i i@ I I@ i�i@�I�I@�i� i@� I� I@� (iiiiIIIIlllll l l l (iIIIIIIIII
II
II
II
(ii@i i @I@I@@I @I @@II@I I @I@I@@I @I @@(iI�I�I��I I� I� I�� I I�I� I��I  I� I�  I�� (iI II I@I @I@I @I@I @IDI DI@@I @@ID@I D@(iI@I I@ II@I(I@(I�I@�I�I@�I�I@�I�I@�(iIIIIIIIIIIIIIII(iIIIIIIIIIIIIIII(ii�i�i��I�I��I��I���I�I��I��I���I��I���I���I����(iI�I�I��I�I��I��I���l@l @l Dl  Dl�@@l� @Dl� D@l�  DD(iiiiiiiiiiiiiiii(iIIIIIIIIIIIIIII(ii i i  i i  i  i   i i  i  i   i  i   i   i    (iI I I  I I  I  I   I I  I  I   I  I   I   I    (ii@I@ID@i@i@@I@@ID@@i@iD@I@@IDD@iD@iDD@I@D@IDDD@(iI@I@ID@I@I@@I@@ID@@I@ID@I@@IDD@ID@IDD@I@D@IDDD@(@I ��I�i  I  ��i I ��I �i  I ��I ��I �I �I  �i iI �I�i I �I��I  ��I �I �I �i I�I��I  �I�I  �I  �iiI  ��I �I �I ��I�I �I �I  �I�I��i  I ��i i  I �I  ��I��I �I  �i I �I �iI�i I  �I ��i I  �I�I ��(@I@�iI�i@I@I�I@�I�I�I@II@�II@�i@IiI@�I@I�I�i@iII@�I�I@�I@i@iI�I@�II@�I@�I�I@�II@ii@I�iI@I�i@I�I@�I@�I�iI@II@�I�i@I@iI�I@�I@�Ii@I�(@i@I@@II@I@i@I@I@I@I@I@@iI@@IiI@@iI@I@@III@@I@i@I@@I@I@I@@I@ii@I@I@@IiI@II@I@@I@iI@@I@I@@I@i@I@@iI@i@i@I@@I@I@I@I@I@iI@@Ii@I@II@@(@I�@II�I�@ii@I�I�@I@I�iI�I�I�@i@iI�@I@II�I@I�i@II�iI�@I@II�@I�@i@I�@I�i@I�I@II�i@I�iII�@iI�@I@IiI�@I�@i@I�@I�II�@I�@I@i@I�I�iI�I@(@i�II@I�@ I� I�@I@ I�II I� I@I�@ I� I�@iI@i�I I@ I�@I@ iI� I I�@ I�@ I I�I@I@ I�@I�@I�@ I I�II I� I�@i�I@I�@ iI@ i�I@I I�@ I@iI�@ I� I�@I@ II@I� I�@I@ I I@ I�I� (@I�@I @iI�  I @I I� @i I @I�  @I  i�I� I�@i� I  @i I� @I� @iI I@I�  I� @I�  @i� i�I @I@I  I  @I� I @i�I� I  @I�  I @iI� i�I I� @i I @I�  @I  I@I�  @I  i I� @I�@i� I  @iI I�@I� @I�  i� I @I@I� @(@I@I I I@I@@ I@@I@ iiI@ I@ I@I@I@ I@I@ I@ I@I@@I@@ iI I@I@ I@@I@@ I@ I@I@@ I@@I iI@@ I@I@@I@ I@I iI@@I@ I@@ I@ iI I@I@I iI@ I I@ I@ I@I@@ iI@ I@I@@I@@ I@I@ I@I@@(@l @I � l�@iI  l@I �l �@l@i I� l�@l�@l �@l @I �I l�@l@I  l �@l @iI� i I�l �@l @I�I I � l@I�I l @l �@l�@i iI� l @l �@I  l@I � l@l@I  l �@I�I �l @I� l�@l �@I �l@I � l�@ii l @I l�@(iI I I  I I  I  I   I I  I  I   I  I   I   I    (iIIIIIIIIIIIIIII(ii i i  i i  i  i   i i  i  i   i  i   i   i    (iiiiiiiiiiiiiiii(iI�I�I��I�I��I��I���llllllll(iIIIIIIIIIIIIIII(ii�i�i��i�i��i��i���I�I��I��I���I��I���I���I����(iiiiiiiiiiiiiiii(iIIIIIIIIIIIIIII(iIIIIIIIIIIIIIII(iiiiiiiiiiiiiiii(iiiiiiiiiiiiiiii(iI@I@I@@I@I@@I@@I@@@I@I@@I@@I@@@I@@I@@@I@@@I@@@@(iIIIIIIIIIIIIIII(ii@i@i@@i@i@@i@@i@@@i@i@@i@@i@@@i@@i@@@i@@@i@@@@(iiiiiiiiiiiiiiii(tPCXROTtIE3264tSPEtCF6464(tPC1ROTtPC2ROTAtPC2ROTB((s6/usr/lib/python2.7/site-packages/passlib/crypto/des.pyt_load_tablesQsH
							cCs7d}x*|D]"}|||d@O}|dL}q
W|S(sdReturns the permutation of the given 32-bit or 64-bit code with
    the specified permutation table.iii((tctptouttr((s6/usr/lib/python2.7/site-packages/passlib/crypto/des.pyt_permute9s

s>QcCs
tj|�S(N(t_uint64_structtpack(tvalue((s6/usr/lib/python2.7/site-packages/passlib/crypto/des.pyt_pack64JscCstj|�dS(Ni(Rtunpack(R((s6/usr/lib/python2.7/site-packages/passlib/crypto/des.pyt	_unpack64MscCstj|�dS(Ni(RR(R((s6/usr/lib/python2.7/site-packages/passlib/crypto/des.pyt_pack56PscCstjd|�dS(Nti(RR(R((s6/usr/lib/python2.7/site-packages/passlib/crypto/des.pyt	_unpack56Ssi1i����cs�t�t�r3t��dkr�td��q�nat�t�r�dksZ�tkritd��nttt����St	j
�dd��t���t�fd�t
D��S(sDconvert DES from 7 bytes to 8 bytes (by inserting empty parity bits)iskey must be 7 bytes in sizeis'key must be 56-bit non-negative integersbytes or inttkeyc3s!|]}�|?d@d>VqdS(iiN((t.0tshift(R(s6/usr/lib/python2.7/site-packages/passlib/crypto/des.pys	<genexpr>us(t
isinstancetbytestlent
ValueErrorRtINT_56_MASKRRRRtExpectedTypeErrorRRt_EXPAND_ITER(R((Rs6/usr/lib/python2.7/site-packages/passlib/crypto/des.pyRcscCs�t|t�rFt|�dkr0td��nttt|���St|t�r|dksm|tkr�td��q�nt	j
|dd��|dL}d}d}x6|dkr�||d	@|>O}|dL}|d
7}q�W|t@s�t�|S(sGconvert DES key from 8 bytes to 7 bytes (by discarding the parity bits)iskey must be 8 bytes in sizeis'key must be 64-bit non-negative integersbytes or intRii8ii(R R!R"R#Rtshrink_des_keyRRtINT_64_MASKRR%tAssertionError(Rtresulttoffset((s6/usr/lib/python2.7/site-packages/passlib/crypto/des.pyR'ws"

iicCs�t|t�r`t|�dkr0t|�}n!t|�dkrQtd��nt|�}ntj|dd��t|t�r�t|�dkr�td��nt|�}ntj|dd��t||||�}t	|�S(s�encrypt single block of data using DES, operates on 8-byte strings.

    :arg key:
        DES key as 7 byte string, or 8 byte string with parity bits
        (parity bit values are ignored).

    :arg input:
        plaintext block to encrypt, as 8 byte string.

    :arg salt:
        Optional 24-bit integer used to mutate the base DES algorithm in a
        manner specific to :class:`~passlib.hash.des_crypt` and its variants.
        The default value ``0`` provides the normal (unsalted) DES behavior.
        The salt functions as follows:
        if the ``i``'th bit of ``salt`` is set,
        bits ``i`` and ``i+24`` are swapped in the DES E-box output.

    :arg rounds:
        Optional number of rounds of to apply the DES key schedule.
        the default (``rounds=1``) provides the normal DES behavior,
        but :class:`~passlib.hash.des_crypt` and its variants use
        alternate rounds values.

    :raises TypeError: if any of the provided args are of the wrong type.
    :raises ValueError:
        if any of the input blocks are the wrong size,
        or the salt/rounds values are out of range.

    :returns:
        resulting 8-byte ciphertext block.
    iiskey must be 7 or 8 bytesR!Rsinput block must be 8 bytestinput(
R R!R"RR#RRR%tdes_encrypt_int_blockR(RR,tsalttroundsR*((s6/usr/lib/python2.7/site-packages/passlib/crypto/des.pyR�s!cCs�|dkrtd��n|dks3|tkrBtd��nt|t�sitj|dd��n'|dks�|tkr�td��nt|t�s�tj|dd��n'|dks�|tkr�td	��ntd$kr�t	�nt
\}}}}}}	}
}d
�}t||��}
|d@d>|d
@d>B|d@d?B|d@d?B}|dkrxd}}nN|d?d@|d@B}t|t
�}|d?d@|d?d@B}t|t
�}x�|r\|d8}xp|
D]h\}}|d?|A|@}|d>|A|A|A}|||d?d@||d?d@A||d?d@A||d?d@A||d?d@A|	|d?d@A|
|d?d@A||d?d@AN}|d?|A|@}|d>|A|A|A}|||d?d@||d?d@A||d?d@A||d?d@A||d?d@A|	|d?d@A|
|d?d@A||d?d@AN}q�W||}}q�W|d?d@|d>d @B|d!?d"@B|d>d#@B}t|t�S(%s�encrypt single block of data using DES, operates on 64-bit integers.

    this function is essentially the same as :func:`des_encrypt_block`,
    except that it operates on integers, and will NOT automatically
    expand 56-bit keys if provided (since there's no way to detect them).

    :arg key:
        DES key as 64-bit integer (the parity bits are ignored).

    :arg input:
        input block as 64-bit integer

    :arg salt:
        optional 24-bit integer used to mutate the base DES algorithm.
        defaults to ``0`` (no mutation applied).

    :arg rounds:
        optional number of rounds of to apply the DES key schedule.
        defaults to ``1``.

    :raises TypeError: if any of the provided args are of the wrong type.
    :raises ValueError:
        if any of the input blocks are the wrong size,
        or the salt/rounds values are out of range.

    :returns:
        resulting ciphertext as 64-bit integer.
    isrounds must be positive integeris(salt must be 24-bit non-negative integertintRs'key must be 64-bit non-negative integerR,s)input must be 64-bit non-negative integercssLxEtD]=\}}t||�}t||�}|t@|t@fVqWdS(sCgiven 64-bit key, iterates over the 8 (even,odd) key schedule pairsN(RRt_KS_MASK(tks_oddtp_eventp_oddtks_even((s6/usr/lib/python2.7/site-packages/passlib/crypto/des.pyt_iter_key_schedulesi?ii�ii�ii�iiI����iUUUUi i:i2i*i"ii
iIi!l�C�i#iI����N(R#tINT_24_MASKR RRR%R(RtNoneRR	tlistRRR
(RR,R.R/tSPE0tSPE1tSPE2tSPE3tSPE4tSPE5tSPE6tSPE7R6tks_listtLtRR5R2tktBtC((s6/usr/lib/python2.7/site-packages/passlib/crypto/des.pyR-�sP"
	#
	
��#("t__doc__tstructtpasslibRtpasslib.utils.compatRRRRt__all__R7R$R(t_KDATA_MASKt
_KPARITY_MASKR1R8RRR	R
RRtStructRRRRRR&RR'RR-(((s6/usr/lib/python2.7/site-packages/passlib/crypto/des.pyt<module>&s2	(		��							8