ff!bgdZddlmZddlmZmZmZddlmZdZ dZ Gdde Z d S) )generate construct ElGamalKey)Random)generate_probable_safe_primetest_probable_prime COMPOSITEIntegerc t}t|||_|jdz dz } tt jd|j|d|j|_|jdvr@|jdz |jzdkrW|j|j}|jdz |zdkr t jd|jdz ||_t|j|j|j|_ |S)a Randomly generate a fresh, new ElGamal key. The key will be safe for use for both encryption and signature (although it should be used for **only one** purpose). Args: bits (int): Key length, or size (in bits) of the modulus *p*. The recommended value is 2048. randfunc (callable): Random number generation function; it should accept a single integer *N* and return a string of random *N* random bytes. Return: an :class:`ElGamalKey` object ) exact_bitsrandfunc min_inclusive max_exclusiver)rrr) rrppowr random_rangeginversexy)bitsrobjqginvs z/builddir/build/BUILD/imunify360-venv-2.3.5/opt/imunify360/venv/lib64/python3.11/site-packages/Crypto/PublicKey/ElGamal.pyrr"s&  C )D8 L L LCE qAG(q365.68889:CECC 5F??  EAI ! # #  u}}SU## EAI  " "    q/2uQw*2 4 4 4CE suce $ $CE Jc t}t|dvrtdtt|D]3}|j|}t ||t ||4t|jtk}||j dkp|j |jkz}|t|j |jdz |jdkz}||j dkp|j |jkz}t|dkrJ||j dkp|j |jkz}|t|j |j |j|j kz}|rtd|S)aConstruct an ElGamal key from a tuple of valid ElGamal components. The modulus *p* must be a prime. The following conditions must apply: .. math:: \begin{align} &1 < g < p-1 \\ &g^{p-1} = 1 \text{ mod } 1 \\ &1 < x < p-1 \\ &g^x = y \text{ mod } p \end{align} Args: tup (tuple): A tuple with either 3 or 4 integers, in the following order: 1. Modulus (*p*). 2. Generator (*g*). 3. Public key (*y*). 4. Private key (*x*). Optional. Raises: ValueError: when the key being imported fails the most basic ElGamal validity checks. Returns: an :class:`ElGamalKey` object )z%argument for construct() wrong lengthrr#zInvalid ElGamal key components)rlen ValueErrorrange_keydatasetattrr rrr rrrr)tuprifield fmt_errors rrr`sQ@  C 3xxu@AAA 3s88__-- QUGCFOO,,,,#CE**i7I )SUCE\)I SUCE!GSU++Q..I q(CE35L(I 3xx{{SUAX-- Ssu--su44 ;9::: Jr ceZdZdZgdZddZdZdZdZdZ d Z d Z d Z d Z d ZdZdZdZdZdZdZdZdZdZdS)raPClass defining an ElGamal key. Do not instantiate directly. Use :func:`generate` or :func:`construct` instead. :ivar p: Modulus :vartype d: integer :ivar g: Generator :vartype e: integer :ivar y: Public key component :vartype y: integer :ivar x: Private key component :vartype x: integer )rrrrNcH|tjj}||_dSN)rnewread _randfunc)selfrs r__init__zElGamalKey.__init__s!  z||(H!r ct|j||j}t|j||j|z|jz}t |t |gSr/)rrrrint)r3MKabs r_encryptzElGamalKey._encryptsO dfa  46 " "1 $ /AAr ct|dstdtjd|jdz |j}t |j||j|dz|jz}t ||j|j}| |j|dz|jz}|t |j ||jz|jz}t|S)Nr(Private key not available in this objectrrrr) hasattr TypeErrorr rrr2rrrrrr6)r3r7ra_blindaxplaintext_blind plaintexts r_decryptzElGamalKey._decryptsc"" HFGG G  q/3vax*.. : : :tvq$&))AaD0DF: w ' '::df--!4?$s461df'='==G 9~~r ct|dstd|jdz }t|}||dkrt dt |j||j}t||j|zz |z}|dkr ||z}|dk || |z|z}t|t|gS)Nrr=rzBad K value: GCD(K,p-1)!=1r) r>r?rr gcdr%rrrrr6)r3r7r8p1r9tr:s r_signzElGamalKey._signsc"" HFGG G 6!8 AJJ EE"IIqLL9:: : dfa  1::dfQh " $ccQrT1acc QYYr]]?b AAr cDd|D}|ddks|d|jdz krdSt|j|d|j}|t|d|d|jz|jz}t|j||j}||krdSdS)Nc,g|]}t|Sr ).0rs r z&ElGamalKey._verify..s'''awqzz'''r rr)rrrr)r3r7sigv1v2s r_verifyzElGamalKey._verifys''3''' q6!88s1vdfQh1 tvs1vtv & & s3q63q646** *df 4 tvq$& ! ! r661qr c*t|drdSdS)z&Whether this is an ElGamal private keyrrr)r>r3s r has_privatezElGamalKey.has_privates  4   11r cdSNTrMrUs r can_encryptzElGamalKey.can_encrypttr cdSrXrMrUs rcan_signzElGamalKey.can_signrZr cDt|j|j|jfS)zfA matching ElGamal public key. Returns: a new :class:`ElGamalKey` object )rrrrrUs r publickeyzElGamalKey.publickeys $&$&$&1222r c t|t|krdSd}|jD]2}|o-t|j|dt|j|dk}3|S)NFT)boolrVr'getattrkey)r3otherresultcomps r__eq__zElGamalKey.__eq__s   "" # #tE,=,=,?,?'@'@ @ @5M A ADA4!>!>!(D$!?!?"@FF r c.|| Sr/)rf)r3rcs r__ne__zElGamalKey.__ne__s;;u%%%%r cddlm}|)Nr) PicklingError)picklerj)r3rjs r __getstate__zElGamalKey.__getstate__s((((((r ctr/NotImplementedError)r3r7r8s rsignzElGamalKey.sign !!r ctr/rn)r3r7 signatures rverifyzElGamalKey.verifyrqr ctr/rn)r3rDr8s rencryptzElGamalKey.encryptrqr ctr/rn)r3 ciphertexts rdecryptzElGamalKey.decryptrqr ctr/rnr3r7Bs rblindzElGamalKey.blindrqr ctr/rnr{s runblindzElGamalKey.unblindrqr ctr/rnrUs rsizezElGamalKey.sizerqr r/)__name__ __module__ __qualname____doc__r'r4r;rErJrSrVrYr\r^rfrhrlrprtrvryr}rrrMr rrrsM8" ! !H""""             333&&&"""""""""""""""""""""""r rN) __all__CryptorCrypto.Math.Primalityrrr Crypto.Math.Numbersr rrobjectrrMr rrs4 2 1 1EEEEEEEEEE''''''<<<|222hJ"J"J"J"J"J"J"J"J"J"r