
    ih_                     h    d dl Z d dlmZ d dlmZ ddlmZmZ  G d de      Z	ded	ee
ef   d
e	fdZy)    N)
ModuleType)Union   )W	W_inversec                   ^    e Zd ZdZdedeeef   fdZdeeef   defdZ	deeef   defd	Z
y
)KWPModea  Key Wrap with Padding (KWP) 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                     |j                   | _         | j                   dk7  rt        d      || _        |j                  ||j                        | _        d| _        y )N   zXKey Wrap with Padding mode is only available for ciphers that operate on 128 bits blocksF)
block_size
ValueError_factorynewMODE_ECB_cipher_done)selfr
   r   s      U/var/www/html/dev/engine/venv/lib/python3.12/site-packages/Crypto/Cipher/_mode_kwp.py__init__zKWPMode.__init__   sZ     ",,??b  @ A A  {{3(8(89
    	plaintextreturnc                    | j                   rt        d      t        |      dk(  rt        d      t        |      dk\  rt        d      dt        |      z
  dz  }|d|z  z   }dt        j                  d	t        |            z   }t        |      dk(  r | j
                  j                  ||z         }|S t        | j
                  ||z         }|S )
zEncrypt and authenticate (wrap) a cryptographic key.

        Args:
          plaintext:
            The cryptographic key to wrap.

        Returns:
            The wrapped key.
        (The cipher cannot be used more than oncer   z%The plaintext must be at least 1 bytel        zThe plaintext is too long          YY>I)r   r   lenstructpackr   encryptr   )r   r   padlenpaddedAIVress         r   sealzKWPMode.seal'   s     ::GHHy>QDEEy>W$899c)n$)Wv--!FKKc)n$EEv;!,,&&sV|4C 
 DLL#,/C
r   
ciphertextc                 $   | j                   rt        d      t        |      dz  rt        d      t        |      dk  rt        d      t        |      dk(  r| j                  j	                  |      }nt        | j                  |      }|dd dk7  rt        d	      t        j                  d
|dd       d   }t        |      dz
  |z
  }|dk  s|dkD  rt        d	      |t        |      |z
  d d|z  k7  rt        d	      |dt        |      |z
   S )aw  Decrypt and authenticate (unwrap) a cryptographic key.

        Args:
          ciphertext:
            The cryptographic key to unwrap.
            It must be at least 16 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 bytesr   z-The ciphertext must be at least 24 bytes longN   r   zIncorrect decryptionr    r      r   )r   r   r!   r   decryptr   r"   unpack)r   r*   SPlenr%   s        r   unsealzKWPMode.unsealG   s     ::GHHz?QRSSz?RLMMz?b $$Z0A$,,
3ARa5''344}}T1Qq6*1-Q!d"A:!344SVf_'F"223443q6F?##r   N)__name__
__module____qualname____doc__r   r   bytes	bytearrayr   r)   r2    r   r   r	   r	   	   sa     $E9,-eE9$45 % @*$ui'7!8 *$U *$r   r	   r
   kwargsr   c                 v    	 |d   }t        | |      S # t         $ r}t        dt        |      z         d}~ww xY w)a!  Create a new block cipher in Key Wrap with Padding mode.

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

    Keywords:
      key:
        The secret key to use to seal or unseal.
    r   zMissing parameter:N)KeyError	TypeErrorstrr	   )r
   r:   r   es       r   _create_kwp_cipherr@   t   sF    7Um 7C    7,s1v5667s    	838)r"   typesr   typingr   _mode_kwr   r   objectr	   r7   r8   r@   r9   r   r   <module>rE      sF       "h$f h$V!
 !!&ui'7!8!=D!r   