
    Y]2                     *    d dl Z  G d de          ZdS )    Nc            	           e Zd Zg dZg dZg dZg dZg dZg dg dg dg d	g d
g dg dg dgZg dZ	g dZ
d ZddZd Zed             Zed             Zd Zd Zd Zd Zd Zd ZdS )DES)88   0   (                r   9   1   )   !         	      :   2   *   "         
      ;   3   +   #   >   6   .   &               =   5   -   %               <   4   ,   $                           )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )0r.   r
   r      r   r7   r   r8   r&   r/   r5   r   r%   r   r:   r;   r         r'   r   r9   r6   r   r   r   r$   r3   r"   r!   r,   '   r   r2   r   /   r   r   r#   7   r   r1   r*   r   r   r   r4      )@r   r   r   r   r   r   r   r   r   r   r   r   r8   r9   r:   r;   r(   r)   r*   r+   r,   r-   r.   r/   ?   rA   r@   r?   rB   r<   r>   r=   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r0   r1   r2   r3   r4   r5   r6   r7   r    r!   r"   r#   r$   r%   r&   r'   )0rB   r   r   r   r;   r7   r;   r7   r/   r'   r=   r   r=   r   r   r   r:   r6   r:   r6   r.   r&   r>   r
   r>   r
   r   r   r9   r5   r9   r5   r-   r%   r<   r	   r<   r	   r   r   r8   r4   r8   r4   r,   r$   rB   r   )@r&   r7   r.   r   r   r>   r:   r   r;   r   r'   r6   r/   r   r   r=   r   r>   r=   r7   r&   r   r.   r   r   r'   r6   r:   r   r/   r;   r   r7   r   r&   r   r.   r'   r   r:   r>   r6   r   r=   r;   r   r/   r   r>   r6   r   r   r7   r   r   r=   r/   r:   r;   r&   r   r   r'   r.   )@r>   r   r   r&   r'   r:   r;   r7   r   r=   r   r.   r6   r   r/   r   r;   r.   r7   r=   r>   r   r   r&   r6   r   r   r   r'   r   r:   r/   r   r&   r=   r:   r   r7   r.   r   r/   r   r6   r'   r   r;   r   r>   r.   r   r   r   r;   r>   r7   r   r:   r'   r=   r6   r   r/   r&   r   )@r   r   r   r&   r'   r;   r>   r/   r   r.   r6   r=   r:   r7   r   r   r.   r=   r   r   r;   r7   r'   r   r   r   r/   r&   r6   r:   r>   r   r.   r'   r7   r   r   r>   r;   r   r:   r   r   r6   r/   r   r&   r=   r   r   r.   r   r'   r   r   r=   r7   r>   r&   r;   r:   r/   r   r6   )@r=   r.   r&   r;   r   r'   r   r   r   r   r   r/   r:   r6   r7   r>   r.   r   r:   r/   r'   r>   r   r;   r7   r=   r   r6   r   r   r&   r   r   r'   r   r   r6   r:   r=   r.   r>   r   r;   r&   r/   r   r   r7   r;   r>   r   r'   r   r   r.   r   r   r7   r/   r:   r6   r=   r   r&   )@r   r6   r7   r   r=   r   r:   r'   r   r/   r;   r>   r.   r   r&   r   r&   r:   r   r6   r7   r=   r.   r   r/   r   r>   r   r;   r   r   r'   r7   r   r   r:   r   r.   r=   r   r>   r   r6   r/   r'   r;   r   r&   r:   r   r6   r=   r   r&   r   r.   r'   r>   r   r   r   r7   r/   r;   )@r6   r   r   r>   r   r   r'   r   r   r.   r;   r7   r&   r=   r/   r:   r   r>   r7   r   r=   r6   r   r/   r'   r   r.   r&   r   r:   r;   r   r   r&   r>   r/   r   r   r6   r;   r=   r   r7   r   r   r.   r:   r'   r7   r;   r   r6   r   r/   r>   r   r:   r&   r   r=   r'   r   r   r.   )@r7   r:   r   r&   r>   r   r   r.   r;   r6   r   r=   r/   r   r'   r   r.   r   r:   r=   r7   r   r   r   r&   r;   r/   r6   r   r>   r   r'   r   r7   r:   r.   r6   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   r;   r6   )@r.   r   r   r7   r'   r>   r:   r   r   r   r;   r&   r/   r   r6   r=   r   r>   r.   r   r   r;   r=   r7   r6   r/   r'   r:   r   r&   r   r   r=   r:   r7   r   r   r6   r&   r   r   r'   r   r.   r>   r;   r/   r   r   r   r&   r=   r7   r   r   r.   r>   r6   r   r   r;   r/   r'   r:   ) r>   r'   r9   r5   r4   r:   r8   r
   r   r&   r%   r   r7   r   r$   r   r   r=   r<   r.   rB   r   r   r   r   r6   r,   r/   r-   r   r;   r	   )@r?   r=   r@   r>   rA   r<   rC   rB   r#   r'   r"   r&   r!   r%   r    r$   r+   r/   r*   r.   r)   r-   r(   r,   r3   r7   r2   r6   r1   r5   r0   r4   r   r;   r   r:   r   r9   r   r8   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r
   r   r	   c                     t          |          dk    rt          d          || _        |                     | j                  | _        dS )a  
        Creates a DES cipher class with the key initialised. This key must be
        8 bytes in length. This only supports the ECB cipher mode as that is
        what is used in the LM hash calculation.

        :param key: The 8-byte key to use in the Cipher
        r   z.DES encryption key should be 8 bytes in lengthN)len
ValueErrorkey_create_subkeys_subkeys)selfrG   s     //usr/lib/python3/dist-packages/ntlm_auth/des.py__init__zDES.__init__   sD     s88q==MNNN,,TX66    Tc                    d}t          dt          |          d          D ]_}|||dz            }t          |          }|dk    r|r|dd|z
  z  z  }n|dk    rt          d          ||                     |          z  }`|S )z
        DES encrypts the data based on the key it was initialised with.

        :param data: The bytes string to encrypt
        :param pad: Whether to right pad data with   to a multiple of 8
        :return: The encrypted bytes string
        rM   r   r       z,DES encryption must be a multiple of 8 bytes)rangerE   rF   _encode_block)rJ   datapadencrypted_dataiblockblock_lengths          rK   encryptzDES.encrypt   s     q#d))Q'' 	8 	8A1q5MEu::Lq  S A$455""  ") * * *d00777NNrM   c                     d}t          dt          |          d          D ]K}|||dz            }t          |          }|dk    rt          d          ||                     |          z  }L|S )z
        DES decrypts the data based on the key it was initialised with.

        :param data: The encrypted bytes string to decrypt
        :return: The decrypted bytes string
        rM   r   r   z,DES decryption must be a multiple of 8 bytes)rP   rE   rF   _decode_block)rJ   rR   decrypted_datarU   rV   rW   s         rK   decryptzDES.decrypt   s     q#d))Q'' 	8 	8A1q5MEu::Lq    ") * * * d00777NNrM   c                    t          |           dk    rt          dt          |           z            d}t          dd          D ]
}|dk    r't          j        d| ||dz                      d         }n|dk    r,t          j        d| dd                   d         }|dz  d	z  }n]t          j        d| |dz
  |                   d         }t          j        d| ||dz                      d         }|d|z
  z  d	z  ||z	  z  }|d
z  }|t          t                              |          dz             z  }|t          j        d|          z  }|S )uf  
        This takes in an a bytes string of 7 bytes and converts it to a bytes
        string of 8 bytes with the odd parity bit being set to every 8 bits,

        For example

        b""
        00000001 00000010 00000011 00000100 00000101 00000110 00000111

        is converted to

        b"a@)"
        00000001 10000000 10000000 01100001 01000000 00101001 00011001 00001110

        https://crypto.stackexchange.com/questions/15799/des-with-actual-7-byte-key

        :param key: 7-byte string sized key
        :return: 8-byte string with the parity bits sets from the 7-byte string
        r=   z3DES 7-byte key is not 7 bytes in length, actual: %drM   r   r   Br   r'      )	rE   rF   rP   structunpackintr   	bit_countpack)rG   new_keyrU   	new_value
next_values        rK   key56_to_key64zDES.key56_to_key64   sk   * s88q== *,/HH5 6 6 6 q! 	3 	3AAvv"M#s1QqS5z::1=		a"M#s1Q3x88;	&!^t3		"M#s1q57|<<Q?	#]3Aa!eG==a@
'AE2d:jAoM	 "I-I "CCMM),D,Ds,J(J$K$KKIv{3	222GGrM   c                 ,    d}| r| | dz
  z  } |dz  }| |S )Nr   r    )rU   counts     rK   rd   zDES.bit_count   s8      	QJAQJE  	 rM   c                 *  	
 |                      |          

fd| j        D             }|dd         g}|dd         g}t          | j                  D ]c\  }}|                    |                     ||         |                     |                    |                     ||         |                     dt                      }t          dd          D ];}||         ||         z   		fd| j        D             }|                    |           <|S )Nc                      g | ]
}|         S rk   rk   ).0xkey_bitss     rK   
<listcomp>z'DES._create_subkeys.<locals>.<listcomp>   s    333AHQK333rM   r   r4   r   r   r   c                      g | ]
}|         S rk   rk   )ro   rp   cds     rK   rr   z'DES._create_subkeys.<locals>.<listcomp>	  s    444Q2a5444rM   )		_get_bits_pc1	enumerate_shift_indexesappend_shift_bitslistrP   _pc2)rJ   rG   pc1_bitscdrU   shift_indexsubkeyssubkey_bitsrt   rq   s            @@rK   rH   zDES._create_subkeys   s)   >>#&& 4333333 ad^be_ ((;<< 	: 	:NA{HHT%%adK88999HHT%%adK889999&&q" 	( 	(A1!B4444$)444KNN;''''rM   c                 l    d gdz  }t          d          D ]}||z   }|dk    r|dz
  }||         ||<   |S )Nr4   )rP   )rJ   bitsshiftsnew_bitsrU   r   s         rK   rz   zDES._shift_bits  sU    6B;r 	, 	,Af*Kb  )B.{+HQKKrM   c                 X   g }t          t          |                    D ]	}t          j        d|||dz                      d         }|                    |dz  rdnd           |                    |dz  rdnd           |                    |dz  rdnd           |                    |dz  rdnd           |                    |dz  rdnd           |                    |d	z  rdnd           |                    |d
z  rdnd           |                    |dz  rdnd           |S )Nr^   r   r      @   r   r
   r   r7   r   )rP   rE   ra   rb   ry   )rJ   rR   r   rU   bs        rK   ru   zDES._get_bits  sF   s4yy!! 		. 		.Ac4!a%=11!4AKKQX,1---KKQX,1---KKQX,1---KKQX,1---KKQX,1---KKQX,1---KKQX,1---KKQX,1----rM   c           
        	
 |                      |          fd| j        D             }|dd         g|dd         g}t          d          D ]v
|                     |
         | j        
                   		
fdt          d          D             }                    |
                    |                    |           w|d         d         z   fd| j        D             }d}t          ddd	          D ]R
t          d
                    d |

d	z            D                       d          }|t          j
        d|          z  }S|S )Nc                      g | ]
}|         S rk   rk   )ro   rp   
block_bitss     rK   rr   z%DES._encode_block.<locals>.<listcomp>(  s    ...jm...rM   r   r   r   r
   c                 Z    g | ]'}t          |                  |         k              (S rk   rc   )ro   kcomputed_blockrU   ls     rK   rr   z%DES._encode_block.<locals>.<listcomp>.  s3    JJJ1S*ad1g566JJJrM   c                      g | ]
}|         S rk   rk   )ro   rp   rls     rK   rr   z%DES._encode_block.<locals>.<listcomp>5  s    888A"Q%888rM   rM   r    c                 ,    g | ]}t          |          S rk   strro   rp   s     rK   rr   z%DES._encode_block.<locals>.<listcomp>8      !J!J!JQ#a&&!J!J!JrM   r   r^   )ru   _iprP   _compute_blockrI   ry   	_final_iprc   joinra   re   )rJ   rV   lrrnew_rencrypted_bitsencrypted_bytesi_byter   r   rU   r   r   s           @@@@@rK   rQ   zDES._encode_block&  sp   ^^E**
....TX..."XJ2YKr 	 	A!001t}Q7GHHNJJJJJJb		JJJEHHQqTNNNHHUOOOO rUQrU]8888888q"a 	8 	8A!J!J.1q52I!J!J!JKKQOOFv{3777OOrM   c           
         |                      |          }d gdz  }t          | j                  D ]\  }|         ||<   d gdz  d gdz  }|dd         d<   |dd         |d<   t          ddd          D ]\|                     |dz            | j                           fd	t          d          D             }|dz            <   ||<   ]|d         d         z   }d gdz  }t          | j                  D ]\  }|         ||<   d
}	t          ddd          D ]Rt          d                    d |dz            D                       d          }
|	t          j
        d|
          z  }	S|	S )Nr   r   r   r   r
   r>   r   c                 `    g | ]*}t          |         d z            |         k              +S )r   r   )ro   r   r   rU   r   s     rK   rr   z%DES._decode_block.<locals>.<listcomp>I  s7    NNNqS*aAhqk9::NNNrM   rM   r   r   c                 ,    g | ]}t          |          S rk   r   r   s     rK   rr   z%DES._decode_block.<locals>.<listcomp>T  r   rM   r   r^   )ru   rw   r   rP   r   rI   r   rc   r   ra   re   )rJ   rV   r   r   idxr   new_lr   decrypted_bitsdecrypted_bytesr   r   rU   r   s              @@@rK   rZ   zDES._decode_block=  s   ^^E**
Vb[// 	$ 	$FAs mBsGGFRKFRK1R4"2b5	"r2r"" 	 	A!001q54=;KLLNNNNNNNE"IINNNEQU8AaDAaDDqTAaD[")) 	( 	(FAs"$Q%N3q"a 	8 	8A!J!J.1q52I!J!J!JKKQOOFv{3777OOrM   c                    fd| j         D             fdt          d          D             }g d}t          ddd          D ]$}|||dz            }t          |d                   t          |d                   g}d |dd         D             }t          d	                    |          d
          }	t          d	                    |          d
          }
|	dz  |
z   }| j        |         |         }|dz  }                    |dz  rdnd                               |dz  rdnd                               |d
z  rdnd                               |dz  rdnd           &fd| j        D             }|S )Nc                      g | ]
}|         S rk   rk   )ro   rp   rV   s     rK   rr   z&DES._compute_block.<locals>.<listcomp>Z  s    BBBq%(BBBrM   c                 N    g | ]!}t          |         |         k              "S rk   r   )ro   rU   expanded_blockrG   s     rK   rr   z&DES._compute_block.<locals>.<listcomp>[  s/    III!SQ>!#4455IIIrM   r   r   r'   r   c                 ,    g | ]}t          |          S rk   r   r   s     rK   rr   z&DES._compute_block.<locals>.<listcomp>d  s    ???a3q66???rM   r   r   r   r
   r   r7   c                      g | ]
}|         S rk   rk   )ro   rp   
s_box_perms     rK   rr   z&DES._compute_block.<locals>.<listcomp>q  s    666z!}666rM   )_e_bit_selectionrP   r   rc   r   _s_boxesry   _p)rJ   rV   rG   	new_block
s_box_iterrU   current_blockrow_bitscolumn_bits	s_box_rows_box_columns_box_addresss_box_valuefinal_blockr   r   s    ``           @@rK   r   zDES._compute_blockY  s   BBBBD,ABBBIIIIIuRyyIII	 

q"a 	= 	=A%aAg.MM!,--s=3D/E/EFH??=2+>???KBGGH--q11Irww{33Q77L&^|;M-
3MBK!OJ;#4;aa!<<<;#4;aa!<<<;#4;aa!<<<;#4;aa!<<<<6666dg666rM   N)T)__name__
__module____qualname__rv   rx   r|   r   r   r   r   r   rL   rX   r\   staticmethodri   rd   rH   rz   ru   rQ   rZ   r   rk   rM   rK   r   r   	   s       	 	 	D FEEN	 	 	D	 	 	C	 	 		
 	
 	
	
 	
 	
	
 	
 	
	
 	
 	
	
 	
 	
	
 	
 	
	
 	
 	
	
 	
 	
W1Hh
 
 
B	 	 	I7 7 7   *  & + + \+Z   \  0      .  8    rM   r   )ra   objectr   rk   rM   rK   <module>r      sN    
i i i i i& i i i i irM   