3
uQh                 @   s   d Z ddlZddlZddlZddlZyddlZW n ek
rD   Y nX ddlZddlZej	e
Zdd Zy
e ZW n8 ek
r Z zejdeee dZW Y ddZ[X nX esdZdZn:ejdZejdZdZG dd	 d	ejZd
d Zdd ZdS )zM
Helper module around the system's libssl library if available for IGE mode.
    Nc              C   s\  t jjd} tjdkr|tj \}}}|jd^}}}t|dksXt|dkr|t|dkr|t jjdpzt jjdpzt jjd} | std	yt j	j
| }W n tk
r   Y nX |S yt jjj}W n( tk
r   tjjd
dddg}Y nX xl|D ]X}	tjj|	rxFtj|	D ]8\}
}}| |krtjjtjj|
| } t j	j
| S qW qW td|  d S )Nssldarwin.
      z	libssl.46z	libssl.44z	libssl.42zno library called "ssl" foundz~/libz/usr/local/libz/libz/usr/libz1no absolute path for "%s" and cannot load by name)ctypesutilfind_librarysysplatformmac_versplitintOSErrorcdllLoadLibraryZmacholibZdyldZDEFAULT_LIBRARY_FALLBACKAttributeErrorospath
expanduserisdirwalkrealpathjoin)libreleaseZ_version_infoZ_machinevermajor_Zlibsslpathsr   rootfiles r"   :/tmp/pip-build-2nz6shyl/telethon/telethon/crypto/libssl.py_find_ssl_lib   s:    
$


r$   z#Failed to load SSL library: %s (%s)   r   c               @   s0   e Zd ZdZdejded   fdejfgZdS )AES_KEYz$Helper class representing an AES keyZrd_key   r%   roundsN)	__name__
__module____qualname____doc__r   c_uint32	AES_MAXNRc_uint_fields_r"   r"   r"   r#   r&   [   s   r&   c             C   s   t  }tjdt| }tjt| | }tjt| | }tjt| }tjt|  |  }tjt|   }tj||tj| tj	tj|tj||tj|tj|t
 t|S )N   )r&   r   c_intlenc_ubytec_size_t_libsslZAES_set_decrypt_keybyrefAES_ige_encryptAES_DECRYPTbytes)Zcipher_textkeyivaes_keykey_lenin_lenin_ptrout_ptrr"   r"   r#   decrypt_igeb   s     rB   c             C   s   t  }tjdt| }tjt| | }tjt| | }tjt| }tjt|  |  }tjt|   }tj||tj| tj	tj|tj||tj|tj|t
 t|S )Nr1   )r&   r   r2   r3   r4   r5   r6   ZAES_set_encrypt_keyr7   r8   AES_ENCRYPTr:   )Z
plain_textr;   r<   r=   r>   r?   r@   rA   r"   r"   r#   encrypt_igex   s     rD   )r,   r   ctypes.utilr   r
   Zctypes.macholib.dyldImportErrorloggingr   	getLoggerr)   Z__log__r$   r6   r   einfotyperB   rD   r2   rC   r9   r.   	Structurer&   r"   r"   r"   r#   <module>   s4   
8


