
    g                         S r SSKrSSKJr  SSKJrJrJrJrJr  SS/r	Sr
SrS	rS
rSrSrS=q=q=qqS rS r\R,                  " S5      rS rS rS rS r\" SSS5      rS rS rSS jrSS jr g)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)join_byte_valuesbyte_elem_valueiranger   	int_typesexpand_des_keydes_encrypt_blocki l   l    l   ~}{w l   l   sg c            	      @    Sn SnSnX4X"4X"4X"4X4X"4X"4X!44q SqSqSqg)z3delay loading tables until they are actually needed))r   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/     r0      @r1      Pr2     @r3     Pr4   )r          @          l       @                    @    l            l       @                      @   l           l       @               l       @    l            l       @    )r   r      r<      r=     r>      r?     r@      rA     rB   )r              l               @l       @l       @l      @          l                  l        l        @ l       @ l       @ l      @ )r   r     @ rH      rI     @rJ      rK    @ rL     rM    @rN   )r                l                 l                l                    l           l           l          l           l          l          l         )r   r    @  rT      rU   @  rV      rW   @  rX      rY   @  rZ   )r                     l                    l          l          l                   l         l         l        l          !l         !l         !l        !)r   r           r_           r`          ra      rb           rc           rd          re   )r                   l                 l         l         l                 l       " l        l       "          l       "  l         l       " ))r   r   r   r   r   r   r   r   rI   rI   rI   rI      rk   rk   rk   )r   rK   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?          rm   rm   rm   )r   r#   rf   l       rH   l       l       l      rC   l       l       l      i  @l      l      l     )r   r   r   r   r   r   r   r   r/   r/   r/   r/   @  rn   rn   rn   )r   rW   r   i  r<      i  i  r1   i  @i  @i @i  @i  @i @i @)r   r   r   r   r=   r=   r=   r=   rb   rb   rb   rb      rp   rp   rp   )r   r"   r   i   r%   r&   $   i$  r[   l       l        l       l       l      l   $    l   $   )r   rU   rO     rh   l         l         l        r^   l         l         l        l         l        l        l       )r   r)   r6   l          rE   l        @ l        `l        ` r!              l            l           l        @   l        @  l        `   l        `  )r   rT   r   i @  r\   l    @     l      @   l    @ @   r*   l    @     l      @   l    @ @   l         l    @    l      @  l    @ @  )r   r7   r9   r;   r              l         0 l         0  ri   l            l           l            l          l           l         0 l         0  )r   rP   r   l           r,   l                    l          r5   l       @         @    l       @   l       @l       @ l       @   l       @  )r   r]   rS               rD   l          l           l           r    l          (l           l          ( l         l         (l          l         ( )r   r   r   l         @rQ            l         @l        @r   r   l         @l        @l       l        l        @l       @)r   r+   rg   l         r   l         @l        @ l        @rF   l         l         l        l         A l         Al        A l        A))r   r   r   r   r   r   r   r   r=   r=   r=   r=     ry   ry   ry   )r   r?   rT   i @ rf          l    @   l    @  r"   i   i @ i @ l       l      l    @  l    @ )r   r   r   r   rU   rU   rU   rU   r1   r1   r1   r1     @r{   r{   r{   )r   r   r[   l      @ r<   i   l       l     @ rb   i   l        l      @ i  i  l       l     @ )r   r   r   r   r   r   r   r   r#   r#   r#   r#           r|   r|   r|   )r   rK   r`   l       r.   rl   l        l       r%   i  l       l      i  i l       l      )r   r   r   r   r/   r/   r/   r/   rW   rW   rW   rW     r}   r}   r}   )r   r   r_   l   @    r   r   l     @  l   @ @  rO   i@  l       l   @   i   i@  l     A  l   @ A  )r   rI   rC   i   r]   l          l          l          rS   l           l           l           rw   l           l           l           )r   r   r*            r7   rt   l           l          r   l         P l         @l         Pl         @  l         P  l         @ l         P )r   rH   r   i  @ rP   l         l          l         r   l         @l          @l         @l          Bl         Bl          Bl         B)r   r+   r,   r-   r   l           ru   l          r^   l          l         l                    l           l          l          )r   rD   rg   l       r!   l           l           l          r)   l         l         l        rs   l          l          l         )r   rQ   rF   l         r6   l        !l          l        ! r   rx   l          l         l          l        ! l          l        ! )r   ri   r   l       0 rE   l        @l       @l       0@rh   rj   l         l       0          @ l        @ l       @ l       0@ )r   r    r\            r5   l       @  l       @  l       @ r9   l          l         ( l         (r:   l       @  l       @ ( l       @ ())r     rr   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/   rW   r}   r.   r0      rb            )r   rE   r5          @@r7           @   r8          @@   rh   r          @         @@                    @         @          @@  )r   r   r   r   r               r               r               )r   rF   rD   rG   r                                   r^                             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      @       r7   r                  @     r   r7   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   r7   r   r   r   r   r.   r   r   r   r7   r   r   )@@       rh                r   r                   r        rW                 rW       r   r   r   rh   r   r   r   r   r   r          r   r   r   r   r   r   rh   rW   r   r   r   r   r   r   r   r   r   r   r   r   rW   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rh   r   )@     @ @       @r       @ @r    @      @ @       @     @        @r/          @ @r   r   r   r/         @      @rE   r   r   r   r   rE   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rE   r   r   r   r   rE   r/   r   r   r   r   r   r   r   r   r   r   r   r   r/   r   r   )@r   r5   r        @       @                  @       @ r5   r                    @         r        @ r   r   r   rv             r   r   r   r   r   r   r   rv   r   r   r   r   r   r   rv   r   r5   r   r   r   r   r   r   r   r   r   r   rv   r   r   r   r   r   r   r   r5   r   r   r   r   r   r   r   r   )@                @     r       @   r   rF          !  r            !      @  !        @   rr           r           @  !  r   r       @     r   rF   r   r   r   r   r   rr   r   r   r   r   r   r   rr   r   r   r   r   r   r   rr   rF   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rF   r   r   r   r   r   r   r   )@r   r^                @          @    !   @             r   rb      @      !   @      !         r           r   r   r   r   r   r   r   r   r   r      @        @    !r   r   r   r   r^   rb   r   r   r   r   r   r^   rb   r   r   r   r   r   r^   r   r   r   r   r   r   r   r   r   r   rb   r   r   r   r   r   r   r   r   )@       @                 @  r                  @                @  r   r          r         @          @           @  r  rD   r  r  r  r  r
  r   r  r   r   r	  r   r   rD   r  r   r   rD   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   rD   r  ))r   rD   rF   rG   r^   r   r   r   r   r   r   r   r   r   r   r   )r   r[   r#   l       r   l         l         l        r*   l         l         l        r~   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        rS   l           l           l          l           l          l          l         )r   ri   r6   l         r   l         @ l         @ l         @ r   l          @l          @l          @l         @@l         @@l         @@l         @@)r   r<   rO   i  r?   r@   i  i rf   l       l       l      rz   l      l      l     )r   rU   rK   i  r=   i  i  i rC   i  i  i i  i i i)r   r5   rE   r   rh   r   r   r   r7   r8   r   r   r   r   r   r   )r   r`   r_   ra   r\   l         l         l        r    l         l         l        r   l        l        l       )r   rW   r/   r}   rb   r   r   r   r.   r   r0   r   r   r   r   r   )r         i  i   i  i  i i   i  i  i i  i i i)r   rQ   r)   l         rP   rR   l         l        r!   l           rs   l          l           l          l          l         )r   r   rg   l       r9   l          l          l         r]   l          l          l         l           l          l          l         )r   r   rT   i@@  rH   i@ @ i @@ i@@@ r1   i@  @i @ @i@@ @i  @@i@ @@i @@@i@@@@)r   r%   r   i  r"   r&   i  i rI   i  i  i i  i i iN)PCXROTIE3264SPECF6464)PC1ROTPC2ROTAPC2ROTBs      K/home/matz/Project1/venv/lib/python3.13/site-packages/passlib/crypto/des.py_load_tablesr  Q   sk    A
FNA
GHA
Gb  2 2 2 2	F!
FLA	
CPAF    c                 :    SnU H  nX#U S-     -  nU S-  n M     U$ )z`Returns the permutation of the given 32-bit or 64-bit code with
the specified permutation table.r      r%    )cpoutrs       r  _permuter  9  s2    
 C3x	a  Jr  z>Qc                 ,    [         R                  U 5      $ )N_uint64_structpackvalues    r  _pack64r&  J  s    u%%r  c                 2    [         R                  U 5      S   $ )Nr   r"  unpackr$  s    r  	_unpack64r*  M  s      '**r  c                 2    [         R                  U 5      SS  $ )Nr  r!  r$  s    r  _pack56r,  P  s    u%ab))r  c                 8    [         R                  SU -   5      S   $ )N    r   r(  r$  s    r  	_unpack56r/  S  s      51!44r  1   ic                 v  ^  [        T [        5      (       a  [        T 5      S:w  a  [        S5      eOe[        T [        5      (       a8  T S:  d
  T [
        :  a  [        S5      e[        [        [        T 5      5      5      $ [        R                  " T SS5      e[        T 5      m [        U 4S j[         5       5      $ )zDconvert DES from 7 bytes to 8 bytes (by inserting empty parity bits)   zkey must be 7 bytes in sizer   z'key must be 56-bit non-negative integerbytes or intkeyc              3   :   >#    U  H  nTU-	  S -  S-  v   M     g7f)   r  Nr  ).0shiftr4  s     r  	<genexpr>!expand_des_key.<locals>.<genexpr>u  s     O,c5jD014,s   )
isinstancebyteslen
ValueErrorr   INT_56_MASKr*  r   r,  r   ExpectedTypeErrorr/  r   _EXPAND_ITER)r4  s   `r  r   r   c  s    #us8q=:;; 	C	#	#7cK'FGG566##C??
C.C O,OOOr  c                    [        U [        5      (       a7  [        U 5      S:w  a  [        S5      e[	        [        [        U 5      5      5      $ [        U [        5      (       a  U S:  d
  U [        :  a  [        S5      eO[        R                  " U SS5      eU S-  n SnSnUS:  a  XS	-  U-  -  nU S-  n US
-  nUS:  a  M  U[        ) -  (       a   eU$ )zGconvert DES key from 8 bytes to 7 bytes (by discarding the parity bits)rU   zkey must be 8 bytes in sizer   'key must be 64-bit non-negative integerr3  r4  r  8   r6  r2  )r;  r<  r=  r>  r,  shrink_des_keyr*  r   INT_64_MASKr   r@  )r4  resultoffsets      r  rE  rE  w  s    #us8q=:;;~in566	C	#	#7cK'FGG ( ##C??AICFF
2+:&&	! 2+ +%&&Mr  c                    [        U [        5      (       aA  [        U 5      S:X  a  [        U 5      n O[        U 5      S:w  a  [	        S5      e[        U 5      n O[        R                  " U SS5      e[        U[        5      (       a&  [        U5      S:w  a  [	        S5      e[        U5      nO[        R                  " USS5      e[        XX#5      n[        U5      $ )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.
r2  rU   zkey must be 7 or 8 bytesr<  r4  zinput block must be 8 bytesinput)
r;  r<  r=  r   r>  r*  r   r@  des_encrypt_int_blockr&  )r4  rJ  saltroundsrG  s        r  r	   r	     s    B #us8q= %CX]788n##C%88 %u:?:;;% ##E7G<< #3t<F 6?r  c                 J   US:  a  [        S5      eUS:  d
  U[        :  a  [        S5      e[        U [        5      (       d  [        R
                  " U SS5      eU S:  d
  U [        :  a  [        S5      e[        U[        5      (       d  [        R
                  " USS5      eUS:  d
  U[        :  a  [        S	5      e[        c
  [        5         [        u  pEpgppS
 n[        U" U 5      5      nUS-  S-  US-  S-  -  US-  S-	  -  US-  S-	  -  nUS:X  a  S=pO?US-	  S-  US-  -  n[        U[        5      nUS-	  S-  US-	  S-  -  n[        U[        5      nU(       Ga  US-  nU H  u  nnUS-	  U-  U-  nUS-  U-  U-  U-  nXUS-	  S-     UUS-	  S-     -  UUS-	  S-     -  UUS-	  S-     -  UUS-	  S-     -  U	US-	  S-     -  U
US-	  S-     -  UUS-	  S-     -  -  nUS-	  U-  U-  nUS-  U-  U-  U-  nXUS-	  S-     UUS-	  S-     -  UUS-	  S-     -  UUS-	  S-     -  UUS-	  S-     -  U	US-	  S-     -  U
US-	  S-     -  UUS-	  S-     -  -  nM     XpU(       a  GM  US-	  S-  US-  S -  -  US!-	  S"-  -  US-  S#-  -  n[        U[        5      $ )$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.
r  zrounds must be positive integerr   z(salt must be 24-bit non-negative integerintr4  rC  rJ  z)input must be 64-bit non-negative integerc              3   ~   #    [          H/  u  p[        X5      n[        X25      n U[        -  U [        -  4v   M1     g7f)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   ;=?      i     i  r  i   rW      l   *UU iUUUUr   :   2   *   "      
      l       <<xx!   l       C #   il   pa )r>  INT_24_MASKr;  r   r   r@  rF  r  r  r  listr  r  r  )r4  rJ  rL  rM  SPE0SPE1SPE2SPE3SPE4SPE5SPE6SPE7rV  ks_listLRrU  rR  kBCs                        r  rK  rK    s   D z:;;ax4+%CDD c9%%##C66	qC+%BCC eY''##E5'::	ek)DEE ~ 692DD8 %c*+G /b	 
/b	 	"
/a		! /b	 	" 	 z	ArkZ'EJ,>?QrkZ'UaZ:,EFQ
 !  'OGVR%1$AB!a')A2t|$tQUDL'992t|$%'+QUDL'9:2t|$%'+QUDL'9: 2t|$% (,QT4K'89 :A
 R%1$AB!a&(A2t|$tQUDL'992t|$%'+QUDL'9:2t|$%'+QUDL'9: 2t|$% (,QT4K'89 :A  '$ 1- &8 d))e))+ e))	+ d))+  Avr  )r   r  )!__doc__structpasslibr   passlib.utils.compatr   r   r   r   __all__re  r?  rF  _KDATA_MASK_KPARITY_MASKrQ  r  r  r  r  r  r  Structr"  r&  r*  r,  r/  rA  r   rE  r	   rK  r  r  r  <module>r}     s   %\  ; ;    !"  "& % % %#_P	 t$&+*5 bBP(06pEr  