U q`!@s|ddlmZddlmZmZmZddlmZddlm Z e ej e ej e ej e ejGdddeZdS))utils) InvalidTagUnsupportedAlgorithm_Reasons)ciphers)modesc@sxeZdZdZdZdZddZeedddZee dd d Z ed d d Z eedddZ eddddZ edZdS)_CipherContextri?c Cs||_||_||_||_d|_t|jtjr<|jjd|_ nd|_ |jj }|jj ||jj j}|jj}z|t|t|f}Wn4tk rtd|j|r|jn|tjYnX||j||}||jj jkrd|} |dk r| d|7} | d|j7} t| tjt|tjr8|jj |j} njt|tjrX|jj |j} nJt|tjrx|jj |j } n*t|tjr|jj |j } n |jj j} |jj !|||jj j|jj j|jj j|} |j"| dk|jj #|t$|j%} |j"| dkt|tj&r|jj '||jj j(t$| |jj j} |j"| dk|j)dk r|jj '||jj j*t$|j)|j)} |j"| dk|j)|_|jj !||jj j|jj j|jj |j%| |} |j"| dk|jj +|d||_,dS)Nr z6cipher {} in {} mode is not supported by this backend.zcipher {0.name} zin {0.name} mode z_is not supported by this backend (Your version of OpenSSL may be too old. Current version: {}.)r)-_backendZ_cipher_mode _operation_tag isinstancerZBlockCipherAlgorithm block_size_block_size_bytes_libZEVP_CIPHER_CTX_new_ffigcZEVP_CIPHER_CTX_freeZ_cipher_registrytypeKeyErrorrformatnamerZUNSUPPORTED_CIPHERNULLZopenssl_version_textrZModeWithInitializationVector from_bufferZinitialization_vectorZ ModeWithTweakZtweakZ ModeWithNoncenonceZEVP_CipherInit_exopenssl_assertZEVP_CIPHER_CTX_set_key_lengthlenkeyGCMEVP_CIPHER_CTX_ctrlZEVP_CTRL_AEAD_SET_IVLENtagEVP_CTRL_AEAD_SET_TAGZEVP_CIPHER_CTX_set_padding_ctx) selfbackendciphermodeZ operationctxregistryadapterZ evp_ciphermsgZiv_nonceresr-S/tmp/pip-target-nv4zd3e_/lib/python/cryptography/hazmat/backends/openssl/ciphers.py__init__s       z_CipherContext.__init__)datareturncCs2tt||jd}|||}t|d|S)Nr ) bytearrayrr update_intobytes)r$r0bufnr-r-r.updateys z_CipherContext.updatec Cst|}t|||jdkr:tdt||jdd}d}|jjd}|jj|}|jj|}||kr||} ||} t|j ||} |jj |j | || | } |j | dk|| 7}||d7}ql|S)Nr z1buffer must be at least {} bytes for this payloadrint *)rr ValueErrorrr rnewrmin_MAX_CHUNK_SIZErEVP_CipherUpdater#r) r$r0r5Ztotal_data_lenZdata_processedZ total_outoutlenZ baseoutbufZ baseinbufZoutbufZinbufZinlenr,r-r-r.r3~s6z_CipherContext.update_into)r1cCsh|j|jkr,t|jtjr,|jdkr,td|jj d|j }|jj d}|jj |j||}|dkr|j}|st|jtjrt|jj|d|jj j|jj j|dtdt|jtjr.|j|jkr.|jj d|j }|jj |j|jj j|j |}|j|dk|jj |dd|_|jj |j}|j|dk|jj |d|dS)Nz4Authentication tag must be provided when decrypting.zunsigned char[]r8r)errorszFThe length of the provided data is not a multiple of the block length.r )r _DECRYPTrr rZModeWithAuthenticationTagr!r9r rr:rrZEVP_CipherFinal_exr#Z_consume_errorsrrrZ_lib_reason_matchZ ERR_LIB_EVPZ'EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH_ENCRYPTr ZEVP_CTRL_AEAD_GET_TAGbufferrZEVP_CIPHER_CTX_reset)r$r5r>r,r?Ztag_bufr-r-r.finalizesZ     z_CipherContext.finalize)r!r1cCs`t||jjkr"td|jj|jj|j|jjj t||}|j |dk||_ | S)Nz.Authentication tag must be {} bytes or longer.r) rr Z_min_tag_lengthr9rr rr r#r"rrrC)r$r!r,r-r-r.finalize_with_tagsz _CipherContext.finalize_with_tagNcCsN|jjd}|jj|j|jjj||jj|t|}|j |dkdS)Nr8r) r rr:rr=r#rrrr)r$r0r>r,r-r-r.authenticate_additional_datas z+_CipherContext.authenticate_additional_datar)__name__ __module__ __qualname__rAr@r<r/r4r7intr3rCrDrErZread_only_propertyr!r-r-r-r.r sd3 rN)Z cryptographyrZcryptography.exceptionsrrrZcryptography.hazmat.primitivesrZ&cryptography.hazmat.primitives.ciphersrZregister_interfaceZ CipherContextZAEADCipherContextZAEADEncryptionContextZAEADDecryptionContextobjectrr-r-r-r.s