o )%a@sBddlZddlZddlmZddlmZmZmZmZm Z ddl m Z ddl m Z ddlmZddlmZGdd d ejd ZGd d d ejd ZGd ddejd ZGdddejd ZGdddejd ZGdddeZeeGdddeZeeeeeeGdddeZeeGdddeZdS)N)utils)AlreadyFinalizedAlreadyUpdatedNotYetFinalizedUnsupportedAlgorithm_Reasons) _get_backend) CipherBackend)CipherAlgorithm)modesc@ eZdZejdefddZdS)BlockCipherAlgorithmreturncCdS)zK The size of a block as an integer in bits (e.g. 64, 128). NselfrrM/usr/lib/python3/dist-packages/cryptography/hazmat/primitives/ciphers/base.py block_sizezBlockCipherAlgorithm.block_sizeN)__name__ __module__ __qualname__abcabstractpropertyintrrrrrr r ) metaclassc@sPeZdZejdedefddZejdedefddZejdefddZ d S) CipherContextdatarcCr)zk Processes the provided bytes through the cipher and returns the results as bytes. Nrrrrrrupdate rzCipherContext.updatecCr)z Processes the provided bytes and writes the resulting data into the provided buffer. Returns the number of bytes written. Nrrrbufrrr update_into'rzCipherContext.update_intocCr)zM Returns the results of processing the final block as bytes. Nrrrrrfinalize.rzCipherContext.finalizeN) rrrrabstractmethodbytesr!rr$r%rrrrrsrc@s$eZdZejdeddfddZdS)AEADCipherContextrrNcCr)z3 Authenticates the provided bytes. Nrr rrrauthenticate_additional_data6rz.AEADCipherContext.authenticate_additional_data)rrrrr&r'r)rrrrr(5r(c@s$eZdZejdedefddZdS)AEADDecryptionContexttagrcCr)z Returns the results of processing the final block as bytes and allows delayed passing of the authentication tag. Nr)rr,rrrfinalize_with_tag>rz'AEADDecryptionContext.finalize_with_tagN)rrrrr&r'r-rrrrr+=r*r+c@r )AEADEncryptionContextrcCr)zb Returns tag bytes. This is only available after encryption is finalized. Nrrrrrr,GrzAEADEncryptionContext.tagN)rrrrrr'r,rrrrr.Frr.c@sBeZdZ d dedejejfddZddZ dd Z d d Z dS) CipherN algorithmmodecCsXt|}t|tstdtjt|tstd|dur!||||_ ||_ ||_ dS)Nz0Backend object does not implement CipherBackend.z&Expected interface of CipherAlgorithm.) r isinstancer rrBACKEND_MISSING_INTERFACEr TypeErrorvalidate_for_algorithmr0r1_backend)rr0r1backendrrr__init__Ps    zCipher.__init__cCsBt|jtjr|jjdurtd|j|j|j}|j |ddS)Nz0Authentication tag must be None when encrypting.Tencrypt) r2r1r ModeWithAuthenticationTagr, ValueErrorr6create_symmetric_encryption_ctxr0 _wrap_ctxrctxrrr encryptorgs zCipher.encryptorcCs |j|j|j}|j|ddS)NFr9)r6create_symmetric_decryption_ctxr0r1r>r?rrr decryptorrszCipher.decryptorcCs*t|jtjr|r t|St|St|SN)r2r1r r;_AEADEncryptionContext_AEADCipherContext_CipherContext)rr@r:rrrr>xs zCipher._wrap_ctxrD) rrrr typingOptionalr Moder8rArCr>rrrrr/Os   r/c@sFeZdZddZdedefddZdedefddZdefd d Zd S) rGcCs ||_dSrD)_ctxr?rrrr8s z_CipherContext.__init__rrcCs|jdur td|j|SNContext was already finalized.)rKrr!r rrrr!s  z_CipherContext.updatecCs |jdur td|j||SrL)rKrr$r"rrrr$s z_CipherContext.update_intocCs&|jdur td|j}d|_|SrL)rKrr%r rrrr%s  z_CipherContext.finalizeN) rrrr8r'r!rr$r%rrrrrGs rGc@sxeZdZddZdefddZdedefdd Zdedefd d Zdefd d Z dedefddZ deddfddZ dS)rFcCs"||_d|_d|_d|_d|_dS)NrF)rK_bytes_processed_aad_bytes_processed_tag_updatedr?rrrr8s  z_AEADCipherContext.__init__ data_sizecCsV|jdur tdd|_|j|7_|j|jjjkr)td|jjj|jjjdS)NrMTz+{} has a maximum encrypted byte limit of {}) rKrrQrN_mode_MAX_ENCRYPTED_BYTESr<formatname)rrRrrr _check_limits z_AEADCipherContext._check_limitrrcCs|t||j|SrD)rWlenrKr!r rrrr!s z_AEADCipherContext.updatecCs|t||j||SrD)rWrXrKr$r"rrrr$sz_AEADCipherContext.update_intocCs0|jdur td|j}|jj|_d|_|SrL)rKrr%r,rPr rrrr%s   z_AEADCipherContext.finalizer,cCs2|jdur td|j|}|jj|_d|_|SrL)rKrr-r,rP)rr,rrrrr-s   z$_AEADCipherContext.finalize_with_tagNcCsn|jdur td|jrtd|jt|7_|j|jjjkr/td |jjj |jjj|j |dS)NrMz'Update has been called on this context.z%{} has a maximum AAD byte limit of {}) rKrrQrrOrXrS_MAX_AAD_BYTESr<rUrVr)r rrrr)s z/_AEADCipherContext.authenticate_additional_data) rrrr8rrWr'r!r$r%r-r)rrrrrFs rFc@seZdZedefddZdS)rErcCs&|jdur td|jdusJ|jS)Nz4You must finalize encryption before getting the tag.)rKrrPrrrrr,s z_AEADEncryptionContext.tagN)rrrpropertyr'r,rrrrrEsrE)rrH cryptographyrcryptography.exceptionsrrrrrcryptography.hazmat.backendsr'cryptography.hazmat.backends.interfacesr /cryptography.hazmat.primitives._cipheralgorithmr &cryptography.hazmat.primitives.ciphersr ABCMetar rr(r+r.objectr/register_interfacerGrFrErrrrs,       3=