3 lá˜_Uã@sŽddlmZmZmZddlZddlZddlmZddlm Z m Z m Z m Z m Z ddlmZddlmZddlmZejejƒGdd „d eƒƒZejejƒGd d „d eƒƒZejejƒGd d „d eƒƒZejejƒGdd„deƒƒZejejƒGdd„deƒƒZejejƒGdd„deƒƒZGdd„deƒZejeƒGdd„deƒƒZejeƒejeƒejeƒGdd„deƒƒƒƒZ ejeƒGdd„de ƒƒZ!dS)é)Úabsolute_importÚdivisionÚprint_functionN)Úutils)ÚAlreadyFinalizedÚAlreadyUpdatedÚNotYetFinalizedÚUnsupportedAlgorithmÚ_Reasons)Ú _get_backend)Ú CipherBackend)Úmodesc@s(eZdZejdd„ƒZejdd„ƒZdS)ÚCipherAlgorithmcCsdS)zE A string naming this mode (e.g. "AES", "Camellia"). N©)Úselfrrú/usr/lib64/python3.6/base.pyÚnameszCipherAlgorithm.namecCsdS)zW The size of the key being used as an integer in bits (e.g. 128, 256). Nr)rrrrÚkey_size szCipherAlgorithm.key_sizeN)Ú__name__Ú __module__Ú __qualname__ÚabcÚabstractpropertyrrrrrrrsrc@seZdZejdd„ƒZdS)Ú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@s6eZdZejdd„ƒZejdd„ƒZejdd„ƒZdS)Ú CipherContextcCsdS)zk Processes the provided bytes through the cipher and returns the results as bytes. Nr)rÚdatarrrÚupdate2szCipherContext.updatecCsdS)z“ Processes the provided bytes and writes the resulting data into the provided buffer. Returns the number of bytes written. Nr)rrÚbufrrrÚ update_into9szCipherContext.update_intocCsdS)zM Returns the results of processing the final block as bytes. Nr)rrrrÚfinalize@szCipherContext.finalizeN)rrrrÚabstractmethodrrr rrrrr0src@seZdZejdd„ƒZdS)ÚAEADCipherContextcCsdS)z3 Authenticates the provided bytes. Nr)rrrrrÚauthenticate_additional_dataIsz.AEADCipherContext.authenticate_additional_dataN)rrrrr!r#rrrrr"Gsr"c@seZdZejdd„ƒZdS)ÚAEADDecryptionContextcCsdS)zŠ Returns the results of processing the final block as bytes and allows delayed passing of the authentication tag. Nr)rÚtagrrrÚfinalize_with_tagRsz'AEADDecryptionContext.finalize_with_tagN)rrrrr!r&rrrrr$Psr$c@seZdZejdd„ƒZdS)ÚAEADEncryptionContextcCsdS)zb Returns tag bytes. This is only available after encryption is finalized. Nr)rrrrr%\szAEADEncryptionContext.tagN)rrrrrr%rrrrr'Zsr'c@s.eZdZd dd„Zdd„Zdd„Zdd „ZdS) ÚCipherNcCsXt|ƒ}t|tƒstdtjƒ‚t|tƒs0tdƒ‚|dk rB|j|ƒ||_ ||_ ||_ dS)Nz0Backend object does not implement CipherBackend.z&Expected interface of CipherAlgorithm.) r Ú isinstancer r r ZBACKEND_MISSING_INTERFACErÚ TypeErrorZvalidate_for_algorithmÚ algorithmÚmodeÚ_backend)rr+r,ZbackendrrrÚ__init__es   zCipher.__init__cCsBt|jtjƒr"|jjdk r"tdƒ‚|jj|j|jƒ}|j |dd�S)Nz0Authentication tag must be None when encrypting.T)Úencrypt) r)r,r ÚModeWithAuthenticationTagr%Ú ValueErrorr-Zcreate_symmetric_encryption_ctxr+Ú _wrap_ctx)rÚctxrrrÚ encryptorws  zCipher.encryptorcCs |jj|j|jƒ}|j|dd�S)NF)r/)r-Zcreate_symmetric_decryption_ctxr+r,r2)rr3rrrÚ decryptor‚s zCipher.decryptorcCs0t|jtjƒr$|rt|ƒSt|ƒSnt|ƒSdS)N)r)r,r r0Ú_AEADEncryptionContextÚ_AEADCipherContextÚ_CipherContext)rr3r/rrrr2ˆs  zCipher._wrap_ctx)N)rrrr.r4r5r2rrrrr(ds  r(c@s,eZdZdd„Zdd„Zdd„Zdd„Zd S) r8cCs ||_dS)N)Ú_ctx)rr3rrrr.”sz_CipherContext.__init__cCs|jdkrtdƒ‚|jj|ƒS)NzContext was already finalized.)r9rr)rrrrrr—s z_CipherContext.updatecCs |jdkrtdƒ‚|jj||ƒS)NzContext was already finalized.)r9rr)rrrrrrrœs z_CipherContext.update_intocCs&|jdkrtdƒ‚|jjƒ}d|_|S)NzContext was already finalized.)r9rr )rrrrrr ¡s   z_CipherContext.finalizeN)rrrr.rrr rrrrr8’sr8c@sDeZdZdd„Zdd„Zdd„Zdd„Zd d „Zd d „Zd d„Z dS)r7cCs"||_d|_d|_d|_d|_dS)NrF)r9Ú_bytes_processedÚ_aad_bytes_processedÚ_tagÚ_updated)rr3rrrr.­s z_AEADCipherContext.__init__cCsV|jdkrtdƒ‚d|_|j|7_|j|jjjkrRtdj|jjj|jjjƒƒ‚dS)NzContext was already finalized.Tz+{} has a maximum encrypted byte limit of {}) r9rr=r:Ú_modeZ_MAX_ENCRYPTED_BYTESr1Úformatr)rZ data_sizerrrÚ _check_limit´s z_AEADCipherContext._check_limitcCs|jt|ƒƒ|jj|ƒS)N)r@Úlenr9r)rrrrrrÀsz_AEADCipherContext.updatecCs|jt|ƒƒ|jj||ƒS)N)r@rAr9r)rrrrrrrÄsz_AEADCipherContext.update_intocCs0|jdkrtdƒ‚|jjƒ}|jj|_d|_|S)NzContext was already finalized.)r9rr r%r<)rrrrrr Ès    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@rrr r&r#rrrrr7©s r7c@seZdZedd„ƒZdS)r6cCs|jdk rtdƒ‚|jS)Nz4You must finalize encryption before getting the tag.)r9rr<)rrrrr%ës z_AEADEncryptionContext.tagN)rrrÚpropertyr%rrrrr6ésr6)"Z __future__rrrrZsixZ cryptographyrZcryptography.exceptionsrrrr r Zcryptography.hazmat.backendsr Z'cryptography.hazmat.backends.interfacesr Z&cryptography.hazmat.primitives.ciphersr Z add_metaclassÚABCMetaÚobjectrrrr"r$r'r(Zregister_interfacer8r7r6rrrrÚs8      . =