U q`@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@s eZdZejedddZdS)BlockCipherAlgorithmreturncCsdS)zK The size of a block as an integer in bits (e.g. 64, 128). NselfrrR/tmp/pip-target-nv4zd3e_/lib/python/cryptography/hazmat/primitives/ciphers/base.py block_sizeszBlockCipherAlgorithm.block_sizeN)__name__ __module__ __qualname__abcabstractpropertyintrrrrrr sr ) metaclassc@sLeZdZejeedddZejeedddZejedddZ d S) CipherContextdatarcCsdS)zk Processes the provided bytes through the cipher and returns the results as bytes. Nrrrrrrupdate szCipherContext.updatecCsdS)z Processes the provided bytes and writes the resulting data into the provided buffer. Returns the number of bytes written. Nrrrbufrrr update_into'szCipherContext.update_intor cCsdS)zM Returns the results of processing the final block as bytes. Nrrrrrfinalize.szCipherContext.finalizeN) rrrrabstractmethodbytesrrr"r#rrrrrs rc@s"eZdZejeddddZdS)AEADCipherContextNrcCsdS)z3 Authenticates the provided bytes. Nrrrrrauthenticate_additional_data6sz.AEADCipherContext.authenticate_additional_data)rrrrr$r%r'rrrrr&5sr&c@s"eZdZejeedddZdS)AEADDecryptionContexttagrcCsdS)z Returns the results of processing the final block as bytes and allows delayed passing of the authentication tag. Nr)rr*rrrfinalize_with_tag>sz'AEADDecryptionContext.finalize_with_tagN)rrrrr$r%r+rrrrr(=sr(c@s eZdZejedddZdS)AEADEncryptionContextr cCsdS)zb Returns tag bytes. This is only available after encryption is finalized. Nrrrrrr*GszAEADEncryptionContext.tagN)rrrrrr%r*rrrrr,Fsr,c@s>eZdZd eejejdddZddZ ddZ d d Z dS) CipherN) algorithmmodecCsXt|}t|tstdtjt|ts0td|dk rB||||_ ||_ ||_ dS)Nz0Backend object does not implement CipherBackend.z&Expected interface of CipherAlgorithm.) r isinstancer rrZBACKEND_MISSING_INTERFACEr TypeErrorZvalidate_for_algorithmr.r/_backend)rr.r/backendrrr__init__Ps   zCipher.__init__cCsBt|jtjr"|jjdk r"td|j|j|j}|j |ddS)Nz0Authentication tag must be None when encrypting.Tencrypt) r0r/r ModeWithAuthenticationTagr* ValueErrorr2Zcreate_symmetric_encryption_ctxr. _wrap_ctxrctxrrr encryptorgs zCipher.encryptorcCs |j|j|j}|j|ddS)NFr5)r2Zcreate_symmetric_decryption_ctxr.r/r9r:rrr decryptorrs zCipher.decryptorcCs0t|jtjr$|rt|St|Snt|SdSN)r0r/r r7_AEADEncryptionContext_AEADCipherContext_CipherContext)rr;r6rrrr9xs  zCipher._wrap_ctx)N) rrrr typingOptionalr ZModer4r<r=r9rrrrr-Os  r-c@sBeZdZddZeedddZeedddZedd d Zd S) rAcCs ||_dSr>)_ctxr:rrrr4sz_CipherContext.__init__rcCs|jdkrtd|j|SNContext was already finalized.)rDrrrrrrrs z_CipherContext.updatecCs |jdkrtd|j||SrE)rDrr"r rrrr"s z_CipherContext.update_intor cCs&|jdkrtd|j}d|_|SrE)rDrr#rrrrr#s   z_CipherContext.finalizeN) rrrr4r%rrr"r#rrrrrAsrAc@speZdZddZedddZeedddZeedd d Zed d d Z eedddZ eddddZ dS)r@cCs"||_d|_d|_d|_d|_dS)NrF)rD_bytes_processed_aad_bytes_processed_tag_updatedr:rrrr4s z_AEADCipherContext.__init__) data_sizecCsV|jdkrtdd|_|j|7_|j|jjjkrRtd|jjj|jjjdS)NrFTz+{} has a maximum encrypted byte limit of {}) rDrrJrG_modeZ_MAX_ENCRYPTED_BYTESr8formatname)rrKrrr _check_limits z_AEADCipherContext._check_limitrcCs|t||j|Sr>)rOlenrDrrrrrrsz_AEADCipherContext.updatecCs|t||j||Sr>)rOrPrDr"r rrrr"sz_AEADCipherContext.update_intor cCs0|jdkrtd|j}|jj|_d|_|SrE)rDrr#r*rIrrrrr#s    z_AEADCipherContext.finalizer)cCs2|jdkrtd|j|}|jj|_d|_|SrE)rDrr+r*rI)rr*rrrrr+s    z$_AEADCipherContext.finalize_with_tagNcCsn|jdkrtd|jr td|jt|7_|j|jjjkr^td |jjj |jjj|j |dS)NrFz'Update has been called on this context.z%{} has a maximum AAD byte limit of {}) rDrrJrrHrPrLZ_MAX_AAD_BYTESr8rMrNr'rrrrr's z/_AEADCipherContext.authenticate_additional_data) rrrr4rrOr%rr"r#r+r'rrrrr@s r@c@seZdZeedddZdS)r?r cCs&|jdk rtd|jdk s t|jS)Nz4You must finalize encryption before getting the tag.)rDrrIAssertionErrorrrrrr*s  z_AEADEncryptionContext.tagN)rrrpropertyr%r*rrrrr?sr?)rrBZ cryptographyrZcryptography.exceptionsrrrrrZcryptography.hazmat.backendsrZ'cryptography.hazmat.backends.interfacesr Z/cryptography.hazmat.primitives._cipheralgorithmr Z&cryptography.hazmat.primitives.ciphersr ABCMetar rr&r(r,objectr-Zregister_interfacerAr@r?rrrrs*       3=