B
    ˆufq(  ã               @  sh   d dl mZ d dlZd dlmZmZmZ d dlmZ d dl	m
Z
mZ ejrVd dlmZ G dd„ dƒZdS )	é    )ÚannotationsN)Ú
InvalidTagÚUnsupportedAlgorithmÚ_Reasons)Úciphers)Ú
algorithmsÚmodes)ÚBackendc               @  sŒ   e Zd ZdZdZdZddddœdd	„Zd
d
dœdd„Zd
d
ddœdd„Zd
dœdd„Z	d
d
dœdd„Z
d
ddœdd„Zeddœdd„ƒZdS )Ú_CipherContexté   r   i    r	   ÚintÚNone)ÚbackendÚ	operationÚreturnc             C  sR  || _ || _|| _|| _d | _t| jtjƒr<| jjd | _	nd| _	| j j
 ¡ }| j j || j j
j¡}| j j}y|t|ƒt|ƒf }W n4 tk
r¸   td |j|r¨|jn|¡tjƒ‚Y nX || j ||ƒ}|| j jjkr"d|j› d}	|d k	r|	d|j› d7 }	|	d | j  ¡ ¡7 }	t|	tjƒ‚t|tjƒrB| j j |j¡}
njt|tjƒrb| j j |j¡}
nJt|tjƒr‚| j j |j ¡}
n*t|t!j"ƒr¢| j j |j ¡}
n
| j jj}
| j j
 #||| j jj| j jj| j jj|¡}| j  $|d	k¡ | j j
 %|t&|j'ƒ¡}| j  $|d	k¡ t|tj(ƒr”| j j
 )|| j j
j*t&|
ƒ| j jj¡}| j  $|d	k¡ |j+d k	r”| j j
 )|| j j
j,t&|j+ƒ|j+¡}| j  $|d	k¡ |j+| _| j j
 #|| j jj| j jj| j j |j'¡|
|¡}| j  -¡ }| j j
}|d	kr$|j.sþ|d	  /|j0|j1¡s|j2r$|d	  /|j3|j4¡r$t5d
ƒ‚| j j$|d	k|d | j j
 6|d	¡ || _7d S )Né   r   z6cipher {} in {} mode is not supported by this backend.zcipher ú zin z mode z_is not supported by this backend (Your version of OpenSSL may be too old. Current version: {}.)r   z+In XTS mode duplicated keys are not allowed)Úerrors)8Ú_backendZ_cipherÚ_modeÚ
_operationÚ_tagÚ
isinstancer   ZBlockCipherAlgorithmÚ
block_sizeÚ_block_size_bytesÚ_libZEVP_CIPHER_CTX_newÚ_ffiÚgcZEVP_CIPHER_CTX_freeZ_cipher_registryÚtypeÚKeyErrorr   ÚformatÚnamer   ZUNSUPPORTED_CIPHERÚNULLZopenssl_version_textr   ZModeWithInitializationVectorÚfrom_bufferZinitialization_vectorZModeWithTweakZtweakZModeWithNonceÚnoncer   ZChaCha20ZEVP_CipherInit_exÚopenssl_assertZEVP_CIPHER_CTX_set_key_lengthÚlenÚkeyÚGCMÚEVP_CIPHER_CTX_ctrlZEVP_CTRL_AEAD_SET_IVLENÚtagÚEVP_CTRL_AEAD_SET_TAGÚ_consume_errorsZCRYPTOGRAPHY_IS_LIBRESSLÚ_lib_reason_matchÚERR_LIB_EVPZEVP_R_XTS_DUPLICATED_KEYSÚCryptography_HAS_PROVIDERSÚERR_LIB_PROVZPROV_R_XTS_DUPLICATED_KEYSÚ
ValueErrorZEVP_CIPHER_CTX_set_paddingÚ_ctx)Úselfr   ÚcipherÚmoder   ÚctxÚregistryÚadapterZ
evp_cipherÚmsgZiv_nonceÚresr   Úlib© r<   ún/home/ankuromar296_gmail_com/myenv/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/ciphers.pyÚ__init__   s¨    




z_CipherContext.__init__Úbytes)Údatar   c             C  s2   t t|ƒ| j d ƒ}|  ||¡}t|d |… ƒS )Nr   )Ú	bytearrayr&   r   Úupdate_intor?   )r3   r@   ÚbufÚnr<   r<   r=   Úupdate   s    z_CipherContext.update)r@   rC   r   c             C  s
  t |ƒ}t |ƒ|| j d k r:td t |ƒ| j d ¡ƒ‚d}d}| jj d¡}| jjj|dd}| jj |¡}x”||kr|| }	|| }
t| j	|| ƒ}| jj
 | j|	||
|¡}|dkrÞt| jtjƒrÞ| j ¡  tdƒ‚n| j |dk¡ ||7 }||d 7 }qrW |S )Nr   z1buffer must be at least {} bytes for this payloadr   zint *T)Zrequire_writablezeIn XTS mode you must supply at least a full block in the first update call. For AES this is 16 bytes.)r&   r   r1   r    r   r   Únewr#   ÚminÚ_MAX_CHUNK_SIZEr   ÚEVP_CipherUpdater2   r   r   r   ZXTSr,   r%   )r3   r@   rC   Ztotal_data_lenZdata_processedZ	total_outÚoutlenZ
baseoutbufZ	baseinbufZoutbufZinbufZinlenr:   r<   r<   r=   rB   ”   s0    
z_CipherContext.update_into)r   c             C  s˜  | j | jkr,t| jtjƒr,| jd kr,tdƒ‚| jj	 
d| j¡}| jj	 
d¡}| jj | j||¡}|dkrè| j ¡ }|sˆt| jtjƒrˆt‚| jj}| jj|d  |j|j¡pØ|jrÄ|d  |j|j¡pØ|joØ|d j|jk|d tdƒ‚t| jtjƒr^| j | jkr^| jj	 
d| j¡}| jj | j| jjj| j|¡}| j |dk¡ | jj	 |¡d d … | _ | jj !| j¡}| j |dk¡ | jj	 |¡d |d … S )Nz4Authentication tag must be provided when decrypting.zunsigned char[]zint *r   )r   zFThe length of the provided data is not a multiple of the block length.r   )"r   Ú_DECRYPTr   r   r   ZModeWithAuthenticationTagr*   r1   r   r   rF   r   r   ZEVP_CipherFinal_exr2   r,   r(   r   r%   r-   r.   Z'EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTHr/   r0   ZPROV_R_WRONG_FINAL_BLOCK_LENGTHZCRYPTOGRAPHY_IS_BORINGSSLÚreasonZ*CIPHER_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTHÚ_ENCRYPTr)   ZEVP_CTRL_AEAD_GET_TAGÚbufferr   ZEVP_CIPHER_CTX_reset)r3   rC   rJ   r:   r   r;   Ztag_bufr<   r<   r=   Úfinalize¸   sR    


z_CipherContext.finalize)r*   r   c             C  s€   t |ƒ}|| jjk r(td | jj¡ƒ‚n|| jkrBtd | j¡ƒ‚| jj | j	| jjj
t |ƒ|¡}| j |dk¡ || _|  ¡ S )Nz.Authentication tag must be {} bytes or longer.z0Authentication tag cannot be more than {} bytes.r   )r&   r   Z_min_tag_lengthr1   r    r   r   r   r)   r2   r+   r%   r   rO   )r3   r*   Ztag_lenr:   r<   r<   r=   Úfinalize_with_tagø   s    

z _CipherContext.finalize_with_tagc             C  sN   | j j d¡}| j j | j| j jj|| j j |¡t|ƒ¡}| j  	|dk¡ d S )Nzint *r   )
r   r   rF   r   rI   r2   r"   r#   r&   r%   )r3   r@   rJ   r:   r<   r<   r=   Úauthenticate_additional_data  s    
z+_CipherContext.authenticate_additional_datazbytes | Nonec             C  s   | j S )N)r   )r3   r<   r<   r=   r*     s    z_CipherContext.tagN)Ú__name__Ú
__module__Ú__qualname__rM   rK   rH   r>   rE   rB   rO   rP   rQ   Úpropertyr*   r<   r<   r<   r=   r
      s   y$@r
   )Ú
__future__r   ÚtypingZcryptography.exceptionsr   r   r   Zcryptography.hazmat.primitivesr   Z&cryptography.hazmat.primitives.ciphersr   r   ÚTYPE_CHECKINGZ,cryptography.hazmat.backends.openssl.backendr	   r
   r<   r<   r<   r=   Ú<module>   s   