a a@sddlZddlZddlmZddlmZmZmZddlm Z ddl m Z Gdddej dZ Gd d d ej dZGd d d ej dZGd ddej dZGdddeZee GdddeZeeee eeGdddeZeeGdddeZdS)N)utils)AlreadyFinalizedAlreadyUpdatedNotYetFinalized)CipherAlgorithm)modesc@sLeZdZejeedddZejeedddZejedddZ d S) CipherContextdatareturncCsdS)zk Processes the provided bytes through the cipher and returns the results as bytes. Nselfr r r Q/usr/lib64/python3.9/site-packages/cryptography/hazmat/primitives/ciphers/base.pyupdateszCipherContext.updatecCsdS)z Processes the provided bytes and writes the resulting data into the provided buffer. Returns the number of bytes written. Nr rr bufr r r update_intoszCipherContext.update_intor cCsdS)zM Returns the results of processing the final block as bytes. Nr rr r rfinalize"szCipherContext.finalizeN) __name__ __module__ __qualname__abcabstractmethodbytesrintrrr r r rrs r) metaclassc@s"eZdZejeddddZdS)AEADCipherContextNr cCsdS)z3 Authenticates the provided bytes. Nr r r r rauthenticate_additional_data*sz.AEADCipherContext.authenticate_additional_data)rrrrrrr r r r rr)src@s"eZdZejeedddZdS)AEADDecryptionContexttagr cCsdS)z Returns the results of processing the final block as bytes and allows delayed passing of the authentication tag. Nr )rr#r r rfinalize_with_tag2sz'AEADDecryptionContext.finalize_with_tagN)rrrrrrr$r r r rr!1sr!c@s eZdZejedddZdS)AEADEncryptionContextrcCsdS)zb Returns tag bytes. This is only available after encryption is finalized. Nr rr r rr#;szAEADEncryptionContext.tagN)rrrrabstractpropertyrr#r r r rr%:sr%c@sBeZdZd eejejejdddZ ddZ ddZ d d Z dS) CipherN) algorithmmodebackendcCs4t|tstd|dur$||||_||_dS)Nz&Expected interface of CipherAlgorithm.) isinstancer TypeErrorZvalidate_for_algorithmr(r))rr(r)r*r r r__init__Ds   zCipher.__init__cCsLt|jtjr"|jjdur"tdddlm}||j |j}|j |ddS)Nz0Authentication tag must be None when encrypting.rr*Tencrypt) r+r)rModeWithAuthenticationTagr# ValueError,cryptography.hazmat.backends.openssl.backendr*Zcreate_symmetric_encryption_ctxr( _wrap_ctxrr*ctxr r r encryptorTs  zCipher.encryptorcCs*ddlm}||j|j}|j|ddS)Nrr.Fr/)r3r*Zcreate_symmetric_decryption_ctxr(r)r4r5r r r decryptoras  zCipher.decryptorcCs0t|jtjr$|rt|St|Snt|SdSN)r+r)rr1_AEADEncryptionContext_AEADCipherContext_CipherContext)rr6r0r r rr4is  zCipher._wrap_ctx)N) rrrrtypingOptionalrZModeAnyr-r7r8r4r r r rr'Cs   r'c@sBeZdZddZeedddZeedddZedd d Zd S) r<cCs ||_dSr9)_ctxrr6r r rr-usz_CipherContext.__init__r cCs|jdurtd|j|SNContext was already finalized.)r@rrr r r rrxs z_CipherContext.updatecCs |jdurtd|j||SrB)r@rrrr r rr}s z_CipherContext.update_intorcCs&|jdurtd|j}d|_|SrB)r@rrr r r rrs   z_CipherContext.finalizeN) rrrr-rrrrrr r r rr<ssr<c@sreZdZddZeddddZeeddd Zeedd d Zed d dZ eedddZ eddddZ dS)r;cCs"||_d|_d|_d|_d|_dS)NrF)r@_bytes_processed_aad_bytes_processed_tag_updatedrAr r rr-s z_AEADCipherContext.__init__N) data_sizer cCsV|jdurtdd|_|j|7_|j|jjjkrRtd|jjj|jjjdS)NrCTz+{} has a maximum encrypted byte limit of {}) r@rrGrD_modeZ_MAX_ENCRYPTED_BYTESr2formatname)rrHr r r _check_limits z_AEADCipherContext._check_limitr cCs|t||j|Sr9)rLlenr@rr r r rrsz_AEADCipherContext.updatecCs|t||j||Sr9)rLrMr@rrr r rrsz_AEADCipherContext.update_intorcCs0|jdurtd|j}|jj|_d|_|SrB)r@rrr#rFr r r rrs    z_AEADCipherContext.finalizer"cCs2|jdurtd|j|}|jj|_d|_|SrB)r@rr$r#rF)rr#r r r rr$s    z$_AEADCipherContext.finalize_with_tagcCsn|jdurtd|jr td|jt|7_|j|jjjkr^td |jjj |jjj|j |dS)NrCz'Update has been called on this context.z%{} has a maximum AAD byte limit of {}) r@rrGrrErMrIZ_MAX_AAD_BYTESr2rJrKr r r r rr s z/_AEADCipherContext.authenticate_additional_data) rrrr-rrLrrrrr$r r r r rr;s r;c@seZdZeedddZdS)r:rcCs&|jdurtd|jdus J|jS)Nz4You must finalize encryption before getting the tag.)r@rrFrr r rr#s  z_AEADEncryptionContext.tagN)rrrpropertyrr#r r r rr:sr:)rr=Z cryptographyrZcryptography.exceptionsrrrZ/cryptography.hazmat.primitives._cipheralgorithmrZ&cryptography.hazmat.primitives.ciphersrABCMetarrr!r%objectr'Zregister_interfacer<r;r:r r r rs$     0=