U
    cc                     @   s   d Z ddlZddlmZ ddlmZ ddlmZmZ ddl	m
Z
 ddlmZmZmZ ddlmZ ed	d
\ZZdd ed	d
D \ZZeded  Zed ZG dd deZdS )z
Standard SSH key exchange ("kex" if you wanna sound cool).  Diffie-Hellman of
1024 bit key halves, using a known "p" prime and "g" generator.
    N)sha1)util)max_byte	zero_byte)Message)byte_chrlong	byte_mask)SSHException       c                 C   s   g | ]}t |qS  )r   ).0cr   r   7/tmp/pip-unpacked-wheel-rglolp_m/paramiko/kex_group1.py
<listcomp>#   s     r            c                   @   sL   e Zd ZdZdZdZeZdd Zdd Z	dd	 Z
d
d Zdd Zdd ZdS )	KexGroup1lE   8{3If?E yZ3V58noPe?a-tBLy3W[<p6m5P&aF!33*w& ARM;L}.c|&A@h\&&#-Dvd    zdiffie-hellman-group1-sha1c                 C   s(   || _ td| _td| _td| _d S )Nr   )	transportr   xef)selfr   r   r   r   __init__2   s    

zKexGroup1.__init__c                 C   s   |    | jjr4t| j| j| j| _| jt	 d S t| j| j| j| _
t }|t || j
 | j| | jt d S )N)_generate_xr   server_modepowGr   Pr   Z_expect_packet_MSG_KEXDH_INITr   r   add_bytec_MSG_KEXDH_INIT	add_mpint_send_message_MSG_KEXDH_REPLY)r   mr   r   r   	start_kex8   s    
zKexGroup1.start_kexc                 C   sJ   | j jr|tkr| |S | j js4|tkr4| |S d}t||d S )Nz*KexGroup1 asked to handle packet type {:d})r   r   r"   _parse_kexdh_initr'   _parse_kexdh_replyr
   format)r   ptyper(   msgr   r   r   
parse_nextG   s    

zKexGroup1.parse_nextc                 C   sX   t d}t|d d|dd   }|d d tkr |d d tkr qHq t|| _d S )N   r   r      r   )osurandomr	   b7fffffffffffffffb0000000000000000r   Zinflate_longr   )r   Zx_bytesr   r   r   r   Q   s    
zKexGroup1._generate_xc                 C   s   |  }| | _| jdk s,| j| jd kr4td| }t| j| j| j}t }|	| j
j| j
j| j
j| j
j || || j || j || | j
|| |   | j
|| | j
  d S )Nr1   zServer kex "f" is out of range)Z
get_string	get_mpintr   r!   r
   Z
get_binaryr   r   r   addr   local_versionremote_versionlocal_kex_initremote_kex_init
add_stringr%   r   _set_K_H	hash_algoasbytesdigestZ_verify_key_activate_outbound)r   r(   Zhost_keysigKhmr   r   r   r+   b   s(    


zKexGroup1._parse_kexdh_replyc                 C   s  |  | _| jdk s$| j| jd kr,tdt| j| j| j}| j  }t	 }|
| jj| jj| jj| jj || || j || j || | |  }| j|| | j || jj}t	 }|t || || j || | j| | j  d S )Nr1   zClient kex "e" is out of range)r6   r   r!   r
   r   r   r   Zget_server_keyr?   r   r7   r9   r8   r;   r:   r<   r%   r   r>   r@   r=   Zsign_ssh_dataZhost_key_typer#   c_MSG_KEXDH_REPLYr&   rA   )r   r(   rC   keyrD   HrB   r   r   r   r*   {   s:    



 


zKexGroup1._parse_kexdh_initN)__name__
__module____qualname__r!   r    namer   r>   r   r)   r/   r   r+   r*   r   r   r   r   r   )   s   
r   )__doc__r2   hashlibr   Zparamikor   Zparamiko.commonr   r   Zparamiko.messager   Zparamiko.py3compatr   r   r	   Zparamiko.ssh_exceptionr
   ranger"   r'   r$   rE   r4   r5   objectr   r   r   r   r   <module>   s   