3 l_@sdddlmZmZmZddlmZdZdZddZddZ dd Z d d Z d d Z ddZ ddZdS))absolute_importdivisionprint_function) InvalidTagcCsrddlm}m}m}t||r"dSt||rFdjt|jdjdSt||sTt djt|jdjdSdS)Nr)AESCCMAESGCMChaCha20Poly1305schacha20-poly1305z aes-{}-ccmasciiz aes-{}-gcm) +cryptography.hazmat.primitives.ciphers.aeadrrr isinstanceformatlen_keyencodeAssertionError)cipherrrr r/usr/lib64/python3.6/aead.py_aead_cipher_names  rc Cst|jj|}|j||jjk|jj}|jj||jj}|jj|||jj|jj|jjt |t k} |j| dk|jj |t |} |j| dk|jj ||jjt ||jj} |j| dk|tkr|jj ||jjt ||} |j| dkn4|jdr$|jj ||jj||jj} |j| dk|jj|} |jj|} |jj||jj|jj| | t |t k} |j| dk|S)Nrs-ccm)_libZEVP_get_cipherbynameopenssl_assert_ffiNULLZEVP_CIPHER_CTX_newgcZEVP_CIPHER_CTX_freeZEVP_CipherInit_exint_ENCRYPTZEVP_CIPHER_CTX_set_key_lengthrEVP_CIPHER_CTX_ctrlZEVP_CTRL_AEAD_SET_IVLEN_DECRYPTZEVP_CTRL_AEAD_SET_TAGendswithZ from_buffer) backend cipher_namekeynoncetagZtag_lenZ operationZ evp_cipherctxresZ nonce_ptrZkey_ptrrrr _aead_setupsN      r(cCs:|jjd}|jj||jj||jj|}|j|dkdS)Nzint *r)rnewrEVP_CipherUpdaterr)r!r&Zdata_lenZintptrr'rrr _set_lengthNs r+cCs:|jjd}|jj||jj||t|}|j|dkdS)Nzint *r)rr)rr*rrr)r!r&associated_dataoutlenr'rrr _process_aadVs r.cCs^|jjd}|jjdt|}|jj||||t|}|j|dk|jj||dddS)Nzint *zunsigned char[]r)rr)rrr*rbuffer)r!r&datar-bufr'rrr _process_data^s  r2cCsddlm}t|}t|||j|d|t}t||rDt||t|t |||t |||} |j j d} |j j||j j| } |j| dk|j| ddk|j j d|} |j j||j j|| } |j| dk|j j| dd} | | S)Nr)rzint *zunsigned char[])r rrr(rrr r+rr.r2rr)rEVP_CipherFinal_exrrrZEVP_CTRL_AEAD_GET_TAGr/)r!rr$r0r, tag_lengthrr"r&processed_datar-r'Ztag_bufr%rrr_encryptfs$     r6cCs2ddlm}t||krt|| d}|d| }t|}t|||j|||t} t||rpt || t|t || |t||r|j j d} |j j dt|} |j j| | | |t|} | dkr|jt|j j| | ddd} nBt|| |} |j j d} |j j| |j j| } | dkr.|jt| S)Nr)rzint *zunsigned char[]r)r rrrrr(rrr r+r.rr)rr*Z_consume_errorsr/r2r3r)r!rr$r0r,r4rr%r"r&r-r1r'r5rrr_decrypts4         r7N)Z __future__rrrZcryptography.exceptionsrrrrr(r+r.r2r6r7rrrrs 0