
    ih^                         d dl Z d dlmZ d dlmZ d dlmZ d dlmZ dedee	e
f   de	fd	Zded
ee	e
f   de	fdZ G d de      Zdedee	e
f   defdZy)    N)deque)
ModuleType)Union)strxorcipher	plaintextreturnc                    t        dt        |      d      D cg c]
  }|||dz     }}t        |      }d|dz
  z  }|d   }t        |dd        }t        d|dz         D ]]  }t        j                  d|      }	| j                  ||j                         z         }
t        |
d d |	      }|j                  |
dd         _ |dj                  |      z   S c c}w )Nr            >Q    )
rangelenr   structpackencryptpopleftr   appendjoin)r   r   iSnsARtt_64cts              T/var/www/html/dev/engine/venv/lib/python3.12/site-packages/Crypto/Cipher/_mode_kw.pyWr"   
   s     $)C	NA#>?a1QqS	?A?AA	QUA	!AaeA1a!e_ {{4#^^A		O,2bq64 	AB	 sxx{? 	@s   C
ciphertextc                    t        dt        |      d      D cg c]
  }|||dz     }}t        |      }d|dz
  z  }|d   }t        |dd        }t        |dd      D ]]  }t        j                  d|      }	| j                  t        ||	      |j                         z         }
|
d d }|j                  |
dd         _ |dj                  |      z   S c c}w )Nr   r   r   r   r   r   )
r   r   r   r   r   decryptr   pop
appendleftr   )r   r#   r   Cr   r   r   r   r   r   pts              r!   	W_inverser+      s     %*!S_a$@AqAac	AAAAA	QUA	!AaeA1a_ {{4#^^F1dOaeeg56rF	RV	 sxx{? 	Bs   Cc                   ^    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
)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                     |j                   | _         | j                   dk7  rt        d      || _        |j                  ||j                        | _        d| _        y )N   zKKey Wrap mode is only available for ciphers that operate on 128 bits blocksF)
block_size
ValueError_factorynewMODE_ECB_cipher_done)selfr.   r/   s      r!   __init__zKWMode.__init__?   sZ     ",,??b  @ A A  {{3(8(89
r   r   r	   c                 
   | j                   rt        d      t        |      dz  rt        d      t        |      dk  rt        d      t        |      dk\  rt        d      t        | j                  d|z         }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)r8   r3   r   r"   r7   )r9   r   ress      r!   sealzKWMode.sealL   s     ::GHHy>AQRRy>BKLLy>U"899AIMN

r   r#   c                     | j                   rt        d      t        |      dz  rt        d      t        |      dk  rt        d      t        | j                  |      }|dd dk7  rt        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)r8   r3   r   r+   r7   )r9   r#   r*   s      r!   unsealzKWMode.unseali   s      ::GHHz?QRSSz?RLMMt||Z0bq688>??
!"vr   N)__name__
__module____qualname____doc__r   r   bytes	bytearrayr:   r?   rB    r   r!   r-   r-   .   s`     $E9,-eE9$45 % :ui'7!8 U r   r-   r.   kwargsc                 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 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.   rJ   r/   es       r!   _create_kw_cipherrP      sF    7Um '3  7,s1v5667s    	838)r   collectionsr   typesr   typingr   Crypto.Util.strxorr   rG   rH   r"   r+   objectr-   rP   rI   r   r!   <module>rV      s        %j ui'(-2$j y 016;$ZV Zz z   %eY&6 7 <B r   