ó ö 5Xc@sdZddlZddlmZddlmZmZmZmZmZddgZ dZ dZ d Z d Z d Zd 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 ``_. 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 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=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@iii€ii@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@ II@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€ii€ii@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@ II@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 us(t isinstancetbytestlent ValueErrorRt INT_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|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   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.pyRs!c Csœ|dkrtdƒ‚n|dks3|tkrBtdƒ‚nt|tƒsitj|ddƒ‚n'|dks|tkrtdƒ‚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_schedule si?iiÀi iðiiüiiIªªªªiUUUUi i:i2i*i"ii iIi!lÀC‡i#iIððððN(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