3 L]@sddlmZmZmZddlZddlZddlmZddlm Z m Z m Z m Z m Z ddlmZddlmZejejGdddeZejejGd d d eZejejGd d d eZejejGd ddeZejejGdddeZejejGdddeZGdddeZejeGdddeZejeejeejeGdddeZejeGdddeZdS))absolute_importdivisionprint_functionN)utils)AlreadyFinalizedAlreadyUpdatedNotYetFinalizedUnsupportedAlgorithm_Reasons) CipherBackend)modesc@s(eZdZejddZejddZdS)CipherAlgorithmcCsdS)zE A string naming this mode (e.g. "AES", "Camellia"). N)selfrrU/tmp/pip-install-wfra5znf/cryptography/cryptography/hazmat/primitives/ciphers/base.pynameszCipherAlgorithm.namecCsdS)zW The size of the key being used as an integer in bits (e.g. 128, 256). Nr)rrrrkey_sizeszCipherAlgorithm.key_sizeN)__name__ __module__ __qualname__abcabstractpropertyrrrrrrr sr c@seZdZejddZdS)BlockCipherAlgorithmcCsdS)zK The size of a block as an integer in bits (e.g. 64, 128). Nr)rrrr block_size%szBlockCipherAlgorithm.block_sizeN)rrrrrrrrrrr#src@s6eZdZejddZejddZejddZdS) CipherContextcCsdS)zk Processes the provided bytes through the cipher and returns the results as bytes. Nr)rdatarrrupdate.szCipherContext.updatecCsdS)z Processes the provided bytes and writes the resulting data into the provided buffer. Returns the number of bytes written. Nr)rrbufrrr update_into5szCipherContext.update_intocCsdS)zM Returns the results of processing the final block as bytes. Nr)rrrrfinalize<szCipherContext.finalizeN)rrrrabstractmethodrrrrrrrr,src@seZdZejddZdS)AEADCipherContextcCsdS)z3 Authenticates the provided bytes. Nr)rrrrrauthenticate_additional_dataEsz.AEADCipherContext.authenticate_additional_dataN)rrrrr r"rrrrr!Csr!c@seZdZejddZdS)AEADDecryptionContextcCsdS)z Returns the results of processing the final block as bytes and allows delayed passing of the authentication tag. Nr)rtagrrrfinalize_with_tagNsz'AEADDecryptionContext.finalize_with_tagN)rrrrr r%rrrrr#Lsr#c@seZdZejddZdS)AEADEncryptionContextcCsdS)zb Returns tag bytes. This is only available after encryption is finalized. Nr)rrrrr$XszAEADEncryptionContext.tagN)rrrrrr$rrrrr&Vsr&c@s,eZdZddZddZddZddZd S) CiphercCsPt|tstdtjt|ts(td|dk r:|j|||_||_ ||_ dS)Nz0Backend object does not implement CipherBackend.z&Expected interface of CipherAlgorithm.) isinstancer r r ZBACKEND_MISSING_INTERFACEr TypeErrorZvalidate_for_algorithm algorithmmode_backend)rr*r+backendrrr__init__as   zCipher.__init__cCsBt|jtjr"|jjdk r"td|jj|j|j}|j |ddS)Nz0Authentication tag must be None when encrypting.T)encrypt) r(r+r ModeWithAuthenticationTagr$ ValueErrorr,Zcreate_symmetric_encryption_ctxr* _wrap_ctx)rctxrrr encryptorrs  zCipher.encryptorcCs |jj|j|j}|j|ddS)NF)r/)r,Zcreate_symmetric_decryption_ctxr*r+r2)rr3rrr decryptor}s zCipher.decryptorcCs0t|jtjr$|rt|St|Snt|SdS)N)r(r+r r0_AEADEncryptionContext_AEADCipherContext_CipherContext)rr3r/rrrr2s  zCipher._wrap_ctxN)rrrr.r4r5r2rrrrr'`s r'c@s,eZdZddZddZddZddZd S) r8cCs ||_dS)N)_ctx)rr3rrrr.sz_CipherContext.__init__cCs|jdkrtd|jj|S)NzContext was already finalized.)r9rr)rrrrrrs z_CipherContext.updatecCs |jdkrtd|jj||S)NzContext was already finalized.)r9rr)rrrrrrrs z_CipherContext.update_intocCs&|jdkrtd|jj}d|_|S)NzContext was already finalized.)r9rr)rrrrrrs   z_CipherContext.finalizeN)rrrr.rrrrrrrr8sr8c@sDeZdZddZddZddZddZd d Zd d Zd dZ dS)r7cCs"||_d|_d|_d|_d|_dS)NrF)r9_bytes_processed_aad_bytes_processed_tag_updated)rr3rrrr.s z_AEADCipherContext.__init__cCsV|jdkrtdd|_|j|7_|j|jjjkrRtdj|jjj|jjjdS)NzContext was already finalized.Tz+{} has a maximum encrypted byte limit of {}) r9rr=r:_modeZ_MAX_ENCRYPTED_BYTESr1formatr)rZ data_sizerrr _check_limits z_AEADCipherContext._check_limitcCs|jt||jj|S)N)r@lenr9r)rrrrrrsz_AEADCipherContext.updatecCs|jt||jj||S)N)r@rAr9r)rrrrrrrsz_AEADCipherContext.update_intocCs0|jdkrtd|jj}|jj|_d|_|S)NzContext was already finalized.)r9rrr$r<)rrrrrrs    z_AEADCipherContext.finalizecCs2|jdkrtd|jj|}|jj|_d|_|S)NzContext was already finalized.)r9rr%r$r<)rr$rrrrr%s    z$_AEADCipherContext.finalize_with_tagcCsn|jdkrtd|jr td|jt|7_|j|jjjkr^tdj |jjj |jjj|jj |dS)NzContext was already finalized.z'Update has been called on this context.z%{} has a maximum AAD byte limit of {}) r9rr=rr;rAr>Z_MAX_AAD_BYTESr1r?rr")rrrrrr"s z/_AEADCipherContext.authenticate_additional_dataN) rrrr.r@rrrr%r"rrrrr7s r7c@seZdZeddZdS)r6cCs|jdk rtd|jS)Nz4You must finalize encryption before getting the tag.)r9rr<)rrrrr$s z_AEADEncryptionContext.tagN)rrrpropertyr$rrrrr6sr6) __future__rrrrsixZ cryptographyrZcryptography.exceptionsrrrr r Z'cryptography.hazmat.backends.interfacesr Z&cryptography.hazmat.primitives.ciphersr add_metaclassABCMetaobjectr rrr!r#r&r'Zregister_interfacer8r7r6rrrrs6     - =