U
    ´cif  ã                   @   sš   d dl Z d dlmZ d dlmZ d dlmZ d dlmZ eee	e
f e	dœdd„Zeee	e
f e	d	œd
d„ZG dd„ deƒZeee	e
f edœdd„ZdS )é    N)Údeque)Ú
ModuleType)ÚUnion)Ústrxor)ÚcipherÚ	plaintextÚreturnc           
         s®   ‡ fdd„t dtˆ ƒdƒD ƒ}t|ƒ}d|d  }|d }t|dd … ƒ}t d|d ƒD ]F}t d|¡}|  || ¡  ¡}	t|	d d… |ƒ}| |	dd … ¡ qX|d 	|¡ S )	Nc                    s   g | ]}ˆ ||d  … ‘qS ©é   © ©Ú.0Úi©r   r   ú>/tmp/pip-unpacked-wheel-juw9_yux/Cryptodome/Cipher/_mode_kw.pyÚ
<listcomp>   s     zW.<locals>.<listcomp>r   r
   é   é   ú>Qó    )
ÚrangeÚlenr   ÚstructÚpackZencryptÚpopleftr   ÚappendÚjoin)
r   r   ÚSÚnÚsÚAÚRÚtÚt_64Úctr   r   r   ÚW
   s    r%   )r   Ú
ciphertextr   c           
         s¬   ‡ fdd„t dtˆ ƒdƒD ƒ}t|ƒ}d|d  }|d }t|dd … ƒ}t |ddƒD ]F}t d|¡}|  t||ƒ| ¡  ¡}	|	d d… }| |	dd … ¡ qV|d	 	|¡ S )
Nc                    s   g | ]}ˆ ||d  … ‘qS r	   r   r   ©r&   r   r   r      s     zW_inverse.<locals>.<listcomp>r   r
   r   r   éÿÿÿÿr   r   )
r   r   r   r   r   Zdecryptr   ÚpopÚ
appendleftr   )
r   r&   ÚCr   r   r    r!   r"   r#   Úptr   r'   r   Ú	W_inverse   s    r-   c                   @   sX   e Zd ZdZeeeef dœdd„Zeeef edœdd„Z	eeef edœd	d
„Z
dS )ÚKWModea  Key Wrap (KW) mode.

    This is a deterministic Authenticated Encryption (AE) mode
    for protecting cryptographic keys. See `NIST SP800-38F`_.

    It provides both confidentiality and authenticity, and it designed
    so that any bit of the ciphertext depends on all bits of the plaintext.

    This mode is only available for ciphers that operate on 128 bits blocks
    (e.g., AES).

    .. _`NIST SP800-38F`: http://csrc.nist.gov/publications/nistpubs/800-38F/SP-800-38F.pdf

    :undocumented: __init__
    )ÚfactoryÚkeyc                 C   s:   |j | _ | j dkrtdƒ‚|| _| ||j¡| _d| _d S )Né   zKKey Wrap mode is only available for ciphers that operate on 128 bits blocksF)Ú
block_sizeÚ
ValueErrorÚ_factoryÚnewZMODE_ECBÚ_cipherÚ_done)Úselfr/   r0   r   r   r   Ú__init__?   s    
zKWMode.__init__)r   r   c                 C   sd   | j rtdƒ‚t|ƒd r"tdƒ‚t|ƒdk r6tdƒ‚t|ƒdkrJtdƒ‚t| jd| ƒ}d	| _ |S )
a  Encrypt and authenticate (wrap) a cryptographic key.

        Args:
          plaintext:
            The cryptographic key to wrap.
            It must be at least 16 bytes long, and its length
            must be a multiple of 8.

        Returns:
            The wrapped key.
        ú(The cipher cannot be used more than oncer
   z2The plaintext must have length multiple of 8 bytesr1   z,The plaintext must be at least 16 bytes longl        zThe plaintext is too longó   ¦¦¦¦¦¦¦¦T)r7   r3   r   r%   r6   )r8   r   Úresr   r   r   ÚsealL   s    zKWMode.seal)r&   r   c                 C   sl   | j rtdƒ‚t|ƒd r"tdƒ‚t|ƒdk r6tdƒ‚t| j|ƒ}|dd… dkrZtdƒ‚d	| _ |dd… S )
aw  Decrypt and authenticate (unwrap) a cryptographic key.

        Args:
          ciphertext:
            The cryptographic key to unwrap.
            It must be at least 24 bytes long, and its length
            must be a multiple of 8.

        Returns:
            The original key.

        Raises: ValueError
           If the ciphertext or the key are not valid.
        r:   r
   z3The ciphertext must have length multiple of 8 bytesé   z-The ciphertext must be at least 24 bytes longNr;   zIncorrect integrity check valueT)r7   r3   r   r-   r6   )r8   r&   r,   r   r   r   Úunseali   s    zKWMode.unsealN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   ÚbytesÚ	bytearrayr9   r=   r?   r   r   r   r   r.   .   s   
þr.   )r/   Úkwargsr   c              
   K   sJ   z|d }W n2 t k
r> } ztdt|ƒ ƒ‚W 5 d}~X Y nX t| |ƒS )a  Create a new block cipher in Key Wrap mode.

    Args:
      factory:
        A block cipher module, taken from `Cryptodome.Cipher`.
        The cipher must have block length of 16 bytes, such as AES.

    Keywords:
      key:
        The secret key to use to seal or unseal.
    r0   zMissing parameter:N)ÚKeyErrorÚ	TypeErrorÚstrr.   )r/   rF   r0   Úer   r   r   Ú_create_kw_cipher‹   s
    "rK   )r   Úcollectionsr   Útypesr   Útypingr   ZCryptodome.Util.strxorr   rD   rE   r%   r-   Úobjectr.   rK   r   r   r   r   Ú<module>   s"   
 ÿ
 ÿ]
 ÿ