3 l_b@sddlmZmZmZddlZddlmZddlmZddl m Z ddl m Z ddl mZdd Zdd d Zd d ZdddZdddZdddZGdddeZdS))absolute_importdivisionprint_functionN) _get_backend)Cipher)AES)ECB)bytes_eqc Cstt|t|j}t|}xttdD]h}xbt|D]V}|j|||}tjdtj d|ddd|||dA}|dd||<q6Wq(W|j dkst |dj |S)Nz>Qri) rrr encryptorlenrangeupdatestructpackunpackfinalizeAssertionErrorjoin) wrapping_keyarbackendrnjibr /usr/lib64/python3.6/keywrap.py _wrap_cores,r"csxt|}t|d krtdtdkr0tdtddkrHtdd }fd d tdtdD}t||||S) N z/The wrapping key must be a valid AES key lengthz)The key to wrap must be at least 16 bytesr rz-The key to wrap must be a multiple of 8 bytesscsg|]}||dqS)r r ).0r) key_to_wrapr r! 1sz aes_key_wrap..)r#r$r%)rr ValueErrorrr")rr'rrrr )r'r! aes_key_wrap%s  r*c Cstt|t|j}t|}xttdD]p}xjtt|D]Z}tjdtj d|d|||dA||}|j |} | dd}| dd||<q>Wq,W|j dkst ||fS)Nr z>Qrr r r i) rrr decryptorrreversedrrrrrrr) rrrrr+rrrZatrrr r r! _unwrap_core5s"   r-cst|}t|d krtddtjdt}dtdd}d|tdkrtt|t|j}|j |}|j d kst |Sfd d t d tdD}t ||||SdS)Nr#r$r%z/The wrapping key must be a valid AES key lengthsYYz>ir r csg|]}||dqS)r r )r&r)r'r r!r(\sz-aes_key_wrap_with_padding..r)r#r$r%)rrr)rrrrrrrrrrr")rr'raivZpadrrrr )r'r!aes_key_wrap_with_paddingLs   r0c st|}tdkrtdt|dkr0tdtdkrtt|t|j}|j}|j dkslt |dd}|dd}d}nLfd d t d tdD}|j d } t|}t || ||\}}dj|}tjd |d d\} d|| }t|dd d s^d|d| ko.d|kn s^|d krdt|| dd| rdt|d krr|S|d| SdS)Nr#zMust be at least 16 bytesr$r%z/The wrapping key must be a valid AES key lengthr r r csg|]}||dqS)r r )r&r) wrapped_keyr r!r(qsz/aes_key_unwrap_with_padding..rz>IsYYr.)r#r$r%)rr InvalidUnwrapr)rrrr+rrrrpopr-rrrr ) rr1rr+rrdatarrZ encrypted_aivZmlir )r1r!aes_key_unwrap_with_padding`s4         *& r6cst|}tdkrtdtddkr4tdt|d krHtdd }fd d tdtdD}|jd}t||||\}}t||std j|S)Nr$zMust be at least 24 bytesr rz-The wrapped key must be a multiple of 8 bytesr#r%z/The wrapping key must be a valid AES key lengthscsg|]}||dqS)r r )r&r)r1r r!r(sz"aes_key_unwrap..r )r#r$r%) rrr3r)rr4r-r r)rr1rr/rrr )r1r!aes_key_unwraps    r7c@s eZdZdS)r3N)__name__ __module__ __qualname__r r r r!r3sr3)N)N)N)N)Z __future__rrrrZcryptography.hazmat.backendsrZ&cryptography.hazmat.primitives.ciphersrZ1cryptography.hazmat.primitives.ciphers.algorithmsrZ,cryptography.hazmat.primitives.ciphers.modesrZ,cryptography.hazmat.primitives.constant_timer r"r*r-r0r6r7 Exceptionr3r r r r!s        +