a fWc@sdZddlZddlmZddlmZmZmZmZmZddgZ dZ dZ d Z d Z d Zd Zdaaaad dZddZedZddZddZddZddZedddZddZddZd#d dZd$d!d"Z dS)%apasslib.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 N)exc)join_byte_valuesbyte_elem_valueiranger int_typesexpand_des_keydes_encrypt_blockilll~}{wllsgc CsPd}d}d}||f||f||f||f||f||f||f||ffadadadadS)z3delay loading tables until they are actually needed))rr r r r r $r r $r)rlll@l@l@l@lDlDlDlD)rr@r r@ r r@ r r@ r)r@l@llHlHll@l@lllHlH)rrr r!r"r#r$r%r&)rllllllllll)rrr,r-r.@r/Pr0@r1Pr2)r@ l@ @l l@  @ l l@  l@ l l@ )rrr:r;r<r=r>r?r@)rl@l@l@l@lll@l@l@l@)rr@rFrG@rHrI@rJrK@rL)rllllllllll)rr@rRrS@rTrU@rVrW@rX)rl l l l llll!l!l!l!)rrr]r^r_r` ra rb rc)rllll l"l l" l"l l"))rrrrrrrrrGrGrGrG ririri)rrIr]lr i(l l(r,l l i i(l l( )rrrrr^r^r^r^r=r=r=r=rkrkrk)rr!rdlrFlllrAllli@lll)rrrrrrrrr-r-r-r-@rlrlrl)rrUr ir:iir/i@i@i@i@i@i@i@)rrrrr;r;r;r;r`r`r`r`rnrnrn)rr r i r#r$$i$rYll l lll$l$)rrSrMrflllr\lllllll)rr'r4l rCl@l`l`rl l l@l@l`l`)rrRri@ rZl@l@l@@r(l@l@l@@ll@l@l@@)rr5r7r9rl0l0rgl l l l l l 0l 0)rrNrlr*llr3l@@l@l@l@l@l@)rr[rQ rBl ll rl(ll(ll(ll()rrrl@rOl@l@rrl@l@lll@l@)rr)relrl@l@l@rDllllAlAlAlA))rrrrr r r r r;r;r;r;rwrwrw)rr=rRi@rdl@l@r ii@i@lll@l@)rrrrrSrSrSrSr/r/r/r/@ryryry)rrrYl@r:i ll@r`i l l @ii l l @)rrrrr r r r r!r!r!r! rzrzrz)rrIr^lr,rjl l r#illiil l )rrrrr-r-r-r-rUrUrUrUr{r{r{)rrr]l@rrl@l@@rMi@ll@i i@ lAl@A)rrGrAi r[l l l rQlllrul l l )rrr(r5rrllrlPl@lPl@lPl@lP)rrFr i @rNll l rl@l @l @lBlBl Bl B)rr)r*r+rlrslr\llllll)rrBrelrlllr'lllrqlll)rrOrDlr4l!l l!rrvlll l!l l!)rrgrl0rCl @l@l0@rfrhll0@l @l@l0@)rrrZr3l@l@l@r7l l(l(r8l@ l@(l@())rrpillllllllll)r  l @l Dl @l D@l @l Dl Dl@@l @Dl D@l DD)rr-rUr{r,r.r`)rrCr3@@r5@r6@@rfr~@@@@@@@)rr r r r r r )rrDrBrErr\r})r@@ lD@ @i@@l@ lD@ @iD@l@ lDD iD@iDD@l@ lDD )rl l"l l"llll l"l l"))@@"rr @"r " rrr@  @ rrrrr @ "rrrrr rr @rrrrrrrrrrrrrrrrrr rrrrrrrrrrrrrrr rrr)@@ r @@ @ rr@ r5r @ rr5r,@ rr rrrrrrrrr,rrrrrrrrrrrrr,rrrrrrrrrr5rrrrr,rrrr5rr)@@rfrrrrUrUrrrrfrrrrrrrrrrrrrfrUrrrrrrrrrrrrrUrrrrrrrrrrrrrrrfr)@@@@r@@r@@@@@@r-@@rrrr-@@rCrrrrrCrrrrrrrrrrrrrrrrrrrCrrrrrCr-rrrrrrrrrrrrr-rr)@rr3r@@@@r3r@r@rrrrtrrrrrrrrtrrrrrrrtrr3rrrrrrrrrrrtrrrrrrrr3rrrrrrrr)@ @ r@rrD!r!@!@rpr @!rr@ rrDrrrrrrprrrrrrrprrrrrrrprDrrrrrrrrrrrrrrrDrrrrrrr)@rr\ @ @ !@ rr`@ !@! r rrrrrrrrrr@  @!rrrrr\r`rrrrrr\r`rrrrrr\rrrrrrrrrrr`rrrrrrrr)@ @ @r @  @rr r@ @ @rrBrrrrrrrr rrrrrBrrrrBrrrr rrrrrrrrrrrrrrrrrrrrrrr rrBr))rrBrDrEr\rrrrrrrr}rrr)rrYr!lrlllr(lllr|lll)rr r r rrrrrrrrrrrr)riiiiiiiiiiiiii)rr*rlr)r+llrQlllllll)rrgr4l rl @l @l @rl @l @l @l@@l @@l @@l @@)rr:rMir=r>iirdlllrxlll)rrSrIir;iiirAiiiiiii)rr3rCrrfrr~rr5r6rrrrrr)rr^r]r_rZlllrlllrlll)rrUr-r{r`rrrr,rr.rrrrr)riiiiiiiiiiiii)rrOr'lrNrPllrlrqlllll)rrrelr7l l l r[l l l l l l l )rrrRi@@rFi@@i@@i@@@r/i@@i@@i@@@i@@i@@@i@@@i@@@@)rr#r ir r$iirGiiiiiiiN)PCXROTIE3264SPECF6464)ZPC1ROTZPC2ROTAZPC2ROTBr6/usr/lib/python3.9/site-packages/passlib/crypto/des.py _load_tablesQs GDQ     & rcCs*d}|D]}|||d@O}|dL}q|S)zdReturns the permutation of the given 32-bit or 64-bit code with the specified permutation table.rr#r)cpoutrrrr_permute9s  rz>QcCs t|S)N_uint64_structpackvaluerrr_pack64JsrcCst|dS)Nrrunpackrrrr _unpack64Msr!cCst|ddS)Nr rrrrr_pack56Psr"cCstd|dS)Nrrrrrr _unpack56Ssr$1ics~ttr tdkr`tdn@ttrRdks:tkrBtdtttSt ddt t fddt DS) zDconvert DES from 7 bytes to 8 bytes (by inserting empty parity bits)zkey must be 7 bytes in sizerz'key must be 56-bit non-negative integer bytes or intkeyc3s|]}|?d@d>VqdS)r Nr).0shiftr(rr uz!expand_des_key..) isinstancebyteslen ValueErrorr INT_56_MASKr!rr"rExpectedTypeErrorr$r _EXPAND_ITERr,rr,rrcs    cCst|tr.t|dkrtdttt|St|trR|dksH|tkr`tdnt |dd|dL}d}d}|dkr||d @|>O}|dL}|d 7}qp|S) zGconvert DES key from 8 bytes to 7 bytes (by discarding the parity bits)rSzkey must be 8 bytes in sizer'key must be 64-bit non-negative integerr'r(r 8r)r&) r/r0r1r2r"shrink_des_keyr!r INT_64_MASKrr4)r(resultoffsetrrrr8ws      r8r cCst|tr>t|dkr t|}nt|dkr4tdt|}nt|ddt|trtt|dkrjtdt|}nt|ddt||||}t |S)aencrypt 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. r&rSzkey must be 7 or 8 bytesr0r(zinput block must be 8 bytesinput) r/r0r1rr2r!rr4des_encrypt_int_blockr)r(r<saltroundsr:rrrrs!        cCs|dkrtd|dks |tkr(tdt|tsBt|ddn|dksR|tkrZtdt|tstt|ddn|dks|tkrtd td urtt \}}}}}} } } d d } t | |} |d @d>|d@d>B|d@d?B|d@d?B}|dkrd}}n@|d?d@|d@B}t |t }|d?d@|d?d@B}t |t }|r|d8}| D]Z\}}|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}qX||}}qF|d?d @|d!>d"@B|d#?d$@B|d>d%@B}t |t S)&aencrypt 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. r zrounds must be positive integerrz(salt must be 24-bit non-negative integerintr(r6r<z)input must be 64-bit non-negative integerNcss8tD].\}}t||}t||}|t@|t@fVqdS)zCgiven 64-bit key, iterates over the 8 (even,odd) key schedule pairsN)r r_KS_MASK)ks_oddZp_evenZp_oddks_evenrrr_iter_key_schedule s   z1des_encrypt_int_block.._iter_key_schedule?i ir irUl*UUiUUUUr :2*" l<r?ZSPE0ZSPE1ZSPE2ZSPE3ZSPE4ZSPE5ZSPE6ZSPE7rDZks_listLRrCrBkBCrrrr=s"                   r=)rr )rr )!__doc__structZpasslibrZpasslib.utils.compatrrrr__all__rRr3r9Z _KDATA_MASKZ _KPARITY_MASKrAr r rrrrStructrrr!r"r$r5rr8rr=rrrrs8.  k   8