+
    THis                        R t ^ RIt^ RIHt RR.tRtRtRtRtR	t	R
t
R;s;s;ssR tR t]P"                  ! R4      tR tR tR tR t]! ^1RR4      tR tR tRR ltRR ltR# )a  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
N)excexpand_des_keydes_encrypt_blocki l   l    l   ~}{w l   l   sg c            	     B    Rp RpRpW3W"3W"3W"3W3W"3W"3W!33s RsRsRsR# )z3delay loading tables until they are actually neededN))    r       r       r       r	      r
    $  r      r    $  r   )r                       l                     l                    l                   @ l         @  l        @ l        @  l         D l         D  l        D l        D  )r   r   @   r       r   @   r       r   @   r       r   @   r   )r                       @l         @          l         l          Hl         H            l           l          @ l         @ l           l          l          H l         H )r   r      r           r          r       r!     r"          r#         r$   )r                       l                   l         l          l                 l         l         l                 l        l         l        )r   r      r*      r+     r,      @r-      Pr.     @r/     Pr0   )r          @          l       @                    @    l            l       @                      @   l           l       @               l       @    l            l       @    )r   r      r8      r9     r:      r;     r<      r=     r>   )r              l               @l       @l       @l      @          l                  l        l        @ l       @ l       @ l      @ )r   r     @ rD      rE     @rF      rG    @ rH     rI    @rJ   )r                l                 l                l                    l           l           l          l           l          l          l         )r   r    @  rP      rQ   @  rR      rS   @  rT      rU   @  rV   )r                     l                    l          l          l                   l         l         l        l          !l         !l         !l        !)r   r           r[           r\          r]      r^           r_           r`          ra   )r                   l                 l         l         l                 l       " l        l       "          l       "  l         l       " ))r   r   r   r   r   r   r   r   rE   rE   rE   rE      rg   rg   rg   )r   rG   r[   l       r   i (  l        l    (   r*     l        l       i   i ( l        l    (   )r   r   r   r   r\   r\   r\   r\   r;   r;   r;   r;          ri   ri   ri   )r   r   rb   l       rD   l       l       l      r?   l       l       l      i  @l      l      l     )r   r   r   r   r   r   r   r   r+   r+   r+   r+   @  rj   rj   rj   )r   rS   r
   i  r8      i  i  r-   i  @i  @i @i  @i  @i @i @)r   r   r   r   r9   r9   r9   r9   r^   r^   r^   r^      rl   rl   rl   )r   r   r   i   r!   r"   $   i$  rW   l       l        l       l       l      l   $    l   $   )r   rQ   rK     rd   l         l         l        rZ   l         l         l        l         l        l        l       )r   r%   r2   l          rA   l        @ l        `l        ` r              l            l           l        @   l        @  l        `   l        `  )r   rP   r   i @  rX   l    @     l      @   l    @ @   r&   l    @     l      @   l    @ @   l         l    @    l      @  l    @ @  )r   r3   r5   r7   r              l         0 l         0  re   l            l           l            l          l           l         0 l         0  )r   rL   r   l           r(   l                    l          r1   l       @         @    l       @   l       @l       @ l       @   l       @  )r   rY   rO               r@   l          l           l           r   l          (l           l          ( l         l         (l          l         ( )r   r   r   l         @rM            l         @l        @r   r   l         @l        @l       l        l        @l       @)r   r'   rc   l         r   l         @l        @ l        @rB   l         l         l        l         A l         Al        A l        A))r   r   r   r   r
   r
   r
   r
   r9   r9   r9   r9     ru   ru   ru   )r   r;   rP   i @ rb          l    @   l    @  r   i   i @ i @ l       l      l    @  l    @ )r   r   r   r   rQ   rQ   rQ   rQ   r-   r-   r-   r-     @rw   rw   rw   )r   r   rW   l      @ r8   i   l       l     @ r^   i   l        l      @ i  i  l       l     @ )r   r   r   r   r   r   r   r   r   r   r   r           rx   rx   rx   )r   rG   r\   l       r*   rh   l        l       r!   i  l       l      i  i l       l      )r   r   r   r   r+   r+   r+   r+   rS   rS   rS   rS     ry   ry   ry   )r   r   r[   l   @    r   r   l     @  l   @ @  rK   i@  l       l   @   i   i@  l     A  l   @ A  )r   rE   r?   i   rY   l          l          l          rO   l           l           l           rs   l           l           l           )r   r   r&            r3   rp   l           l          r   l         P l         @l         Pl         @  l         P  l         @ l         P )r   rD   r   i  @ rL   l         l          l         r   l         @l          @l         @l          Bl         Bl          Bl         B)r   r'   r(   r)   r   l           rq   l          rZ   l          l         l                    l           l          l          )r   r@   rc   l       r   l           l           l          r%   l         l         l        ro   l          l          l         )r   rM   rB   l         r2   l        !l          l        ! r   rt   l          l         l          l        ! l          l        ! )r   re   r   l       0 rA   l        @l       @l       0@rd   rf   l         l       0          @ l        @ l       @ l       0@ )r   r   rX            r1   l       @  l       @  l       @ r5   l          l         ( l         (r6   l       @  l       @ ( l       @ ())r     rn   i        l      l     l           l     l      l    l     l    l    l   )r                       l                  @l         Dl        @l        D         @  l         @ l         D  l         D l        @@ l        @D l        D@ l        DD )r   r+   rS   ry   r*   r,      r^            )r   rA   r1          @@r3           @   r4          @@   rd   r|          @         @@                    @         @          @@  )r   r   r   r	   r               r               r               )r   rB   r@   rC   r                                   rZ                             r{                                )r   @     @       l   D@        @i@@l   @     l   D@     @ iD@ l   @     l   DD     i D@iDD@l   @    l   DD    )r                   l                   l        "  l          l      "           l       l        l      l          l       " l         l      " ))@     @ " r   r        @ " r          "        r   r   r        @                @    r   r   r   r   r        @           " r   r   r   r   r   r   r        @ r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )@   @      r             @    @                     @     r   r      @       r3   r                  @     r   r3   r*      @      r   r              r   r   r   r   r   r   r   r   r*   r   r   r   r   r   r   r   r   r   r   r   r   r*   r   r   r   r   r   r   r   r   r   r3   r   r   r   r   r*   r   r   r   r3   r   r   )@@       rd                r   r                   r        rS                 rS       r   r   r   rd   r   r   r   r   r   r          r   r   r   r   r   r   rd   rS   r   r   r   r   r   r   r   r   r   r   r   r   rS   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rd   r   )@     @ @       @r       @ @r    @      @ @       @     @        @r+          @ @r   r   r   r+         @      @rA   r   r   r   r   rA   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rA   r   r   r   r   rA   r+   r   r   r   r   r   r   r   r   r   r   r   r   r+   r   r   )@r~   r1   r        @       @                  @       @ r1   r                    @         r        @ r   r   r~   rr             r   r   r   r   r   r   r   rr   r   r   r   r   r   r   rr   r   r1   r   r   r   r~   r   r   r   r   r~   r   rr   r   r   r   r   r   r   r   r1   r   r   r   r   r   r   r   r   )@                @     r       @   r   rB          !  r            !      @  !        @   rn           r           @  !  r   r       @     r   rB   r   r   r   r   r   rn   r   r   r   r   r   r   rn   r   r   r   r   r   r   rn   rB   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rB   r   r   r   r   r   r   r   )@r   rZ                @          @    !   @             r   r^      @      !   @      !         r           r   r   r   r   r   r   r   r   r   r      @        @    !r   r   r   r   rZ   r^   r   r   r   r   r   rZ   r^   r   r   r   r   r   rZ   r   r   r   r   r   r   r   r   r   r   r^   r   r   r   r   r   r   r   r   )@       @                 @  r                  @                @  r   r          r          @          @           @  r  r@   r  r   r   r  r  r   r  r   r   r  r   r   r@   r   r   r   r@   r  r  r   r   r   r  r   r  r   r   r   r   r   r   r  r   r  r  r  r   r  r  r   r   r  r   r   r   r@   r  ))r   r@   rB   rC   rZ   r   r   r   r   r   r   r   r{   r   r   r   )r   rW   r   l       r   l         l         l        r&   l         l         l        rz   l        l        l       )r   r   r   r	   r   r   r   r   r   r   r   r   r   r   r   r   )r      i   i  i   i  i  i i   i  i  i i  i i i)r   r(   r   l         r'   r)   l         l        rO   l           l           l          l           l          l          l         )r   re   r2   l         r   l         @ l         @ l         @ r   l          @l          @l          @l         @@l         @@l         @@l         @@)r   r8   rK   i  r;   r<   i  i rb   l       l       l      rv   l      l      l     )r   rQ   rG   i  r9   i  i  i r?   i  i  i i  i i i)r   r1   rA   r   rd   r   r|   r   r3   r4   r   r   r   r   r   r   )r   r\   r[   r]   rX   l         l         l        r   l         l         l        r}   l        l        l       )r   rS   r+   ry   r^   r   r   r   r*   r   r,   r   r   r   r   r   )r         i  i   i  i  i i   i  i  i i  i i i)r   rM   r%   l         rL   rN   l         l        r   l           ro   l          l           l          l          l         )r   r   rc   l       r5   l          l          l         rY   l          l          l         l           l          l          l         )r   r   rP   i@@  rD   i@ @ i @@ i@@@ r-   i@  @i @ @i@@ @i  @@i@ @@i @@@i@@@@)r   r!   r
   i  r   r"   i  i rE   i  i  i i  i i i)PCXROTIE3264SPECF6464)PC1ROTPC2ROTAPC2ROTBs      4/usr/lib/python3/dist-packages/passlib/crypto/des.py_load_tablesr  I   sp    aFN	aGH	aGb	 
								F QFlQCpaF    c                Z    ^ pV F"  pW#V ^,          ,          ,          pV ^,          p K$  	  V# )z`Returns the permutation of the given 32-bit or 64-bit code with
the specified permutation table. )cpoutrs   &&  r  _permuter    s3    
 CSz	a  Jr  z>Qc                 ,    \         P                  V 4      # )N_uint64_structpackvalues   &r  _pack64r!    s    u%%r  c                 :    \         P                  V 4      ^ ,          # )r   r  unpackr  s   &r  	_unpack64r%    s      '**r  c                 :    \         P                  V 4      R ,          # ):r  NNr  r  s   &r  _pack56r'    s    u%b))r  c                 H    \         P                  R V ,           4      ^ ,          # )    r#  r  s   &r  	_unpack56r*    s      51!44r  c                  a  \        S \        4      '       d   \        S 4      ^8w  d   \        R4      hMh\        S \        4      '       d;   S ^ 8  g   S \
        8  d   \        R4      h\        \        \        S 4      4      4      # \        P                  ! S RR4      h\        S 4      o \        V 3R l\         4       4      # )zDconvert DES from 7 bytes to 8 bytes (by inserting empty parity bits)zkey must be 7 bytes in sizez'key must be 56-bit non-negative integerbytes or intkeyc              3   R   <"   T F  pSV,	          ^,          ^,          x  K  	  R# 5i)   Nr  ).0shiftr-  s   & r  	<genexpr>!expand_des_key.<locals>.<genexpr>  s"     H<%3%<4'A--<s   $')
isinstancebyteslen
ValueErrorintINT_56_MASKr%  r   r'  r   ExpectedTypeErrorr*  _EXPAND_ITER)r-  s   fr  r   r     s    #us8q=:;; 	C		7cK'FGG566##C??
C.C H<HHHr  c                   \        V \        4      '       d9   \        V 4      ^8w  d   \        R4      h\	        \        \        V 4      4      4      # \        V \        4      '       d   V ^ 8  g   V \        8  d   \        R4      hM\        P                  ! V RR4      hV ^,          p ^ p^ pV^88  d+   W^,          V,          ,          pV ^,          p V^,          pK1  V\        ( ,          '       d   Q hV# )zGconvert DES key from 8 bytes to 7 bytes (by discarding the parity bits)zkey must be 8 bytes in size'key must be 64-bit non-negative integerr,  r-  )r4  r5  r6  r7  r'  shrink_des_keyr%  r8  INT_64_MASKr   r:  )r-  resultoffsets   &  r  r>  r>    s    #us8q=:;;~in566#s7cK'FGG ( ##C??AICFF
2+:&((	!+%%&&Mr  c                   \        V \        4      '       dD   \        V 4      ^8X  d   \        V 4      p M\        V 4      ^8w  d   \	        R4      h\        V 4      p M\        P                  ! V RR4      h\        V\        4      '       d(   \        V4      ^8w  d   \	        R4      h\        V4      pM\        P                  ! VRR4      h\        WW#4      p\        V4      # )aC  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.
zkey must be 7 or 8 bytesr5  r-  zinput block must be 8 bytesinput)
r4  r5  r6  r   r7  r%  r   r:  des_encrypt_int_blockr!  )r-  rC  saltroundsr@  s   &&&& r  r   r     s    B #us8q= %CX]788n##C%88 %u:?:;;% ##E7G<< #3t<F 6?r  c                   V^8  d   \        R4      hV^ 8  g   V\        8  d   \        R4      h\        V \        4      '       g   \        P
                  ! V RR4      hV ^ 8  g   V \        8  d   \        R4      h\        V\        4      '       g   \        P
                  ! VRR4      hV^ 8  g   V\        8  d   \        R4      h\        f   \        4        \        w  rErgrrR p\        V! V 4      4      pV^?,          ^,          VR	,          ^,          ,          VR
,          ^,	          ,          VR,          ^,	          ,          pV^ 8X  d   ^ ;rMcV^,	          R,          VR,          ,          p\        V\        4      pV^ ,	          R,          V^,	          R,          ,          p\        V\        4      pV'       EdK   V^,          pV EF5  w  ppV^ ,	          V,          V,          pV^ ,          V,          V,          V,          pVVV^:,	          ^?,          ,          VV^2,	          ^?,          ,          ,          VV^*,	          ^?,          ,          ,          VV^",	          ^?,          ,          ,          VV^,	          ^?,          ,          ,          V	V^,	          ^?,          ,          ,          V
V^
,	          ^?,          ,          ,          VV^,	          ^?,          ,          ,          ,          pV^ ,	          V,          V,          pV^ ,          V,          V,          V,          pVVV^:,	          ^?,          ,          VV^2,	          ^?,          ,          ,          VV^*,	          ^?,          ,          ,          VV^",	          ^?,          ,          ,          VV^,	          ^?,          ,          ,          V	V^,	          ^?,          ,          ,          V
V^
,	          ^?,          ,          ,          VV^,	          ^?,          ,          ,          ,          pEK8  	  YrEKS  V^,	          R,          V^!,          R,          ,          V^#,	          R,          ,          V^,          R,          ,          p\        V\        4      # )a]  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.
zrounds must be positive integerz(salt must be 24-bit non-negative integerr8  r-  r=  rC  z)input must be 64-bit non-negative integerc              3     "   \          F7  w  r\        W4      p\        W24      p V\        ,          V \        ,          3x  K9  	  R# 5i)zCgiven 64-bit key, iterates over the 8 (even,odd) key schedule pairsN)r
  r  _KS_MASK)ks_oddp_evenp_oddks_evens   &   r  _iter_key_schedule1des_encrypt_int_block.<locals>._iter_key_schedule  s:     #MFv.Gg-FH$fx&777 $s   AAi  i  i   l   *UU iUUUUl       <<xxl       C il   pa )r7  INT_24_MASKr4  r8  r   r:  r?  r
  r  r  listr  r  r  )r-  rC  rE  rF  SPE0SPE1SPE2SPE3SPE4SPE5SPE6SPE7rN  ks_listLRrM  rJ  kBCs   &&&&                 r  rD  rD  <  s   D z:;;ax4+%CDD c3##C66
Qw##BCC eS!!##E5'::qyEK'DEE ~ 692DD8 %c*+G /b	 8O"	$8O!	# 8O"	$ 	 z	ArkZ'EJ,>?QrkZ'UaZ:,EFQ
 &!  'OGVr'Q$&AbA!G+Aa2g%&R4'()R4'() R4'() R4'(	)
 R4'() R4'() Q$'(	A r'Q$&AbA!F*Aa2g%&R4'()R4'() R4'() R4'(	)
 R4'() R4'() Q$'(	A%  '< 1 q&&	&G))	+G))	+ F((	*  Avr  i)r   r  )__doc__structpasslibr   __all__rP  r9  r?  _KDATA_MASK_KPARITY_MASKrI  r
  r  r  r  r  r  Structr  r!  r%  r'  r*  ranger;  r   r>  r   rD  r  r  r  <module>rh     s   %V      !"  "& % % %#cL;	 t$&+*5 RR I(,6rNr  