5Xc@sdZddlZddlmZddlmZmZmZmZmZddgZ dZ dZ d Z d Z d Zd Zeaaaad ZdZejdZdZdZdZdZedddZdZdZdddZ dddZ!dS(spasslib.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 ``_. 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 iN(texc(tjoin_byte_valuestbyte_elem_valuetirangeRt int_typestexpand_des_keytdes_encrypt_blockiIll~}{wIlsgcGCsfddddddddddddddddf}dddddddddddd d d d 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=fadS(>s3delay loading tables until they are actually neededii i i ii$i i $I II II II II II II II i@i i@ i i@ i i@ IIIIII I I@I@IHIHIAIAIIIIiIIiiIII@II@II@II@II@II@II@II@iiii@iPi@iPIIIIIIIIIIIIIIIiiiiiiiiI I III I I I I I I0I0I 0I 0i@ii@ii@ii@iI@I@I@I@I@@I@@lllllllli@ii@ii@ii@IIIIIIII I I!I!I I I!I!IIIiIIIIIIIIIIIIIIIIIIi Ii(I I(iIIi i(I I(III@I@I@IIIi@I@I@I@i@iiiii@i@i@i@i@i@i@ii i$i$II I III$I$iIIII I I I0I0I0I0IHIPIIXI@@I@IH@I@IP@I@IX@i@ I@I I@ I@I I@ II@I I@ IIIIIIIIIII@ I@I I@ I@I I@ II@I I@ l I ll Ill(I I ll(II@I@I@IIIDIDIDIDIIIII I$I$I I I$I$ii@II@I@ii@i@III@I@i@I i II i II ii II I IIIiIIiiIIiI@I I@ i@II@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 II II I I I I I @I@I @I @IDI DI @@ID@I D@I@ I@I(I@(II@II@II@I III II IIII IIIIIIIIIIiiIIIIIIIIIIIIIIIIIIIl@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 II I@Ii@I@II@I@II@I@Ii@I@@I@I@I@I@I@@I@@II@@I@I@II@i@II@I@III@I@I@ I 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@@ l @I  l@I l@I l @I l@l @l @IIIIIIIIIIiiiiiiiiiiiiiiiIIIlllllllIIIIIIIIIIIiiiIIIIIIiiiiiiiiiiiIIIIIIIIIiiiiiiiiiiiiiiiI@@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 II II II II II II (iii@i@i i i@ i@ i i i@ i@ i i i@ i@ (iIIIIII I I@I@IHIHIAIAIIII(iiiiIIIIiiiiIIII(iI@II@II@II@II@II@II@II@(iiiiiiiii@i@iPiPi@i@iPiP(iIIIIIIIIIIIIIII(iiiiiiiiiiiiiiii(iiI I III I I I I I I0I0I 0I 0(iii@i@iii@i@iii@i@iii@i@(iiI@I@I@I@I@@I@@llllllll(iii@i@iii@i@iii@i@iii@i@(iIIIIIIII I I!I!I I I!I!(iiIIIIIIiiIIIIII(iIIIIIIIIIIIIIII(iiiii i i i iiiii i i i (iiIIi i(I I(iiIIi i(I I((iiiiIIIIiiiiIIII(iIIIi@I@I@I@iIIIi@I@I@I@(iiiii@i@i@i@iiiii@i@i@i@(iiiiiiiii@i@i@i@i@i@i@i@(iiiiiiiiiiiiiiii(iii i iii$i$III I III$I$(iiiiIIIII I I I I0I0I0I0(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@ II@I I@ II@I I@ II@I I@ (iIll I I ll IIll(I I ll((iIIII@I@I@I@IIIIIDIDIDID(iIIIIIIII I I$I$I I I$I$(iiiiiiiiiiiiiiii(iii@i@III@I@iii@i@III@I@(iiiiiiiii@i@i@i@i@i@i@i@(ii II ii II ii II ii II (iiiii i i i IIIII I I I (iiIIiiIIiiIIiiII(iiiiiiiiiiiiiiii(ii@II@i i@ I I@ ii@II@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 @@(iIIII I I I I II II I I I (iI II I@I @I@I @I@I @IDI DI@@I @@ID@I D@(iI@I I@ II@I(I@(II@II@II@II@(iIII IIII IIII IIII (iIIIIIIIIIIIIIII(iiiiIIIIIIIIIIII(iIIIIIIIl@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 Ii I i I I i I I I I I i iI Ii I II I I I i III II I iiI I I I II I I IIi I i i I I II I i I I iIi I I i I II (@I@iIi@I@II@III@II@II@i@IiI@I@IIi@iII@II@I@i@iII@II@I@II@II@ii@IiI@Ii@II@I@IiI@II@Ii@I@iII@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@III@ii@II@I@IiIII@i@iI@I@III@Ii@IIiI@I@II@I@i@I@Ii@II@IIi@IiII@iI@I@IiI@I@i@I@III@I@I@i@IIiII@(@iII@I@ I I@I@ III I I@I@ I I@iI@iI I@ I@I@ iI I I@ I@ I II@I@ I@I@I@ I III I I@iI@I@ iI@ iI@I I@ I@iI@ I I@I@ II@I I@I@ I I@ II (@I@I @iI I @I I @i I @I @I iI I@i I @i I @I @iI I@I I @I @i iI @I@I I @I I @iI I @I I @iI iI 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 Il @l @II I  l@II l @l @l@i iI l @l @I l@I  l@l@I l @II 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(iIIIIIIIllllllll(iIIIIIIIIIIIIIII(iiiiiiiiIIIIIIII(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 _unpack56Ssi1icsttr3tdkrtdqnattrdksZtkritdntttSt j ddt t fdt DS(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 us(t isinstancetbytestlent ValueErrorRt INT_56_MASKRRRRtExpectedTypeErrorRRt _EXPAND_ITER(R((Rs6/usr/lib/python2.7/site-packages/passlib/crypto/des.pyRcs cCst|trFt|dkr0tdnttt|St|tr|dksm|tkrtdqnt j |dd|dL}d}d}x6|dkr||d @|>O}|dL}|d 7}qW|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_keyRRt INT_64_MASKRR%(Rtresulttoffset((s6/usr/lib/python2.7/site-packages/passlib/crypto/des.pyR'ws   iicCst|tr`t|dkr0t|}n!t|dkrQtdnt|}ntj|ddt|trt|dkrtdnt|}ntj|ddt||||}t |S(sencrypt 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.pyRs!c Cs|dkrtdn|dks3|tkrBtdnt|tsitj|ddn'|dks|tkrtdnt|tstj|ddn'|dks|tkrtd ntd$krt 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}qW||}}qW|d?d@|d>d @B|d!?d"@B|d>d#@B}t |tS(%sencrypt 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_schedule si?iii iiiiiIiUUUUi i:i2i*i"ii iIi!lCi#iIN(R#t INT_24_MASKR RRR%R(RtNoneRR tlistRRR (RR+R-R.tSPE0tSPE1tSPE2tSPE3tSPE4tSPE5tSPE6tSPE7R5tks_listtLtRR4R1tktBtC((s6/usr/lib/python2.7/site-packages/passlib/crypto/des.pyR,sP"    #     #("t__doc__tstructtpasslibRtpasslib.utils.compatRRRRt__all__R6R$R(t _KDATA_MASKt _KPARITY_MASKR0R7RRR R RRtStructRRRRRR&RR'RR,(((s6/usr/lib/python2.7/site-packages/passlib/crypto/des.pyt&s2 (        8