
    ijW                        / S Qr SSKrSSKrSSKrSSKJrJrJrJrJ	r	  SSK
Jr  SSKJrJr  SSKJr  SSKJrJrJrJrJr  SSKJr  SS	KJrJrJr  SS
KJrJrJ r    " S S\!5      r"S r#SS jr$SS jr%S r&S r'S r(S r)S r*SS jr+\+r,Sr-g))generate	constructDsaKey
import_key    N)bchrbordtobytestostr
iter_range)Random)PKCS8PEM)SHA256)	DerObjectDerSequence
DerIntegerDerObjectIdDerBitString)Integer)test_probable_prime	COMPOSITEPROBABLY_PRIME)_expand_subject_public_key_info_create_subject_public_key_info _extract_subject_public_key_infoc                       \ rS rSrSr/ SQrS rS rS rS r	S r
S	 rS
 rS rS rS rS rS rS r  SS jr\r\rS rS rS rS rS rS rS rSrg)r   N   a  Class defining an actual DSA key.
Do not instantiate directly.
Use :func:`generate`, :func:`construct` or :func:`import_key` instead.

:ivar p: DSA modulus
:vartype p: integer

:ivar q: Order of the subgroup
:vartype q: integer

:ivar g: Generator
:vartype g: integer

:ivar y: Public key
:vartype y: integer

:ivar x: Private key
:vartype x: integer

:undocumented: exportKey, publickey
ygpqxc           	      D   [        UR                  5       5      n[        S5      nUR                  U5      (       d  [        S[	        X2-
  5      -  5      eX#-
  nU(       a2  U[        S5      :w  a#  [        S[	        U[        S5      -
  5      -  5      e[        U5      U l        g )Nr   r    r!   r"   z$Some DSA components are missing = %sr#   zUnknown DSA components = %s)setkeysissubset
ValueErrorstrdict_key)selfkey_dict	input_set
public_set	extra_sets        O/var/www/html/trading/venv/lib/python3.13/site-packages/Crypto/PublicKey/DSA.py__init__DsaKey.__init__g   s    (	./
""9--C !789 : :*	c&k1: S[!89: ; ;N	    c                    U R                  5       (       d  [        S5      eSUs=:  a  U R                  :  d  O  [        S5      eS Vs/ s H  o0R                  U   PM     snu  pEpg[
        R                  " SUS9nX-  R                  U5      n	XH-  n
[        XrU5      U-  nXU-  X-  -   -  U-  n[        [        X45      $ s  snf )Nz)DSA public key cannot be used for signing   zk is not between 2 and q-1)r#   r"   r!   r    )min_inclusivemax_exclusive)has_private	TypeErrorr"   r*   r-   r   random_rangeinversepowmapint)r.   mkcompr#   r"   r!   r    blind_factorinv_blind_kblind_xrss                r3   _signDsaKey._signs   s    !!GHHA9::2FG2F$iio2FG
a++!9:<#'003"aL11,w{:;q@3 Hs   	Cc                 0   Uu  p4S Vs/ s H  oPR                   U   PM     snu  pgpSUs=:  a  U:  a  O  gSUs=:  a  U:  d   g  g[        U5      R                  U5      n
X-  U-  nX-  U-  n[        XU5      [        XlU5      -  U-  U-  nX:H  $ s  snf )N)r   r"   r!   r    r   F)r-   r   r>   r?   )r.   rB   sigrH   rI   rD   r   r"   r!   r    wu1u2vs                 r3   _verifyDsaKey._verify   s    2FG2F$iio2FG
aA		 $%q919 $-AJq!eq[eq[]S]*Q.!3v Hs   Bc                      SU R                   ;   $ )z!Whether this is a DSA private keyr#   r-   r.   s    r3   r;   DsaKey.has_private   s     diir6   c                     g)NF rV   s    r3   can_encryptDsaKey.can_encrypt   s    r6   c                     g)NTrY   rV   s    r3   can_signDsaKey.can_sign   s    r6   c                 D   ^  [        U 4S jS 5       5      n[        U5      $ )zFA matching DSA public key.

Returns:
    a new :class:`DsaKey` object
c              3   D   >#    U  H  oTR                   U   4v   M     g 7fNrU   ).0rC   r.   s     r3   	<genexpr>$DsaKey.public_key.<locals>.<genexpr>   s      Q<PqTYYq\!2<Ps    r%   )r,   r   )r.   public_componentss   ` r3   
public_keyDsaKey.public_key   s#     ! Q<P QQ'((r6   c                 
   [        U R                  5       5      [        UR                  5       5      :w  a  gSnU R                   H;  nU=(       a/    [        U R                  US 5      [        UR                  US 5      :H  nM=     U$ )NFT)boolr;   _keydatagetattrr-   )r.   otherresultrD   s       r3   __eq__DsaKey.__eq__   ss      "#tE,=,=,?'@@MMD BD$!?!(T4!@"AF " r6   c                 .    U R                  U5      (       + $ ra   )rn   )r.   rl   s     r3   __ne__DsaKey.__ne__   s    ;;u%%%r6   c                     SSK Jn  Ue)Nr   )PicklingError)picklert   )r.   rt   s     r3   __getstate__DsaKey.__getstate__   s    (r6   c                 ^    S Vs/ s H  n[        U R                  U   5      PM     sn$ s  snf )z8The DSA domain parameters.

Returns
    tuple : (p,q,g)
)r!   r"   r    )rA   r-   )r.   rD   s     r3   domainDsaKey.domain   s*     2AADIIdO$AAAs   "*c                    / nU R                    Hf  nUS:X  a:  [        U R                  5      R                  5       nUR	                  SU4-  5        MC  [        X5      (       d  MU  UR	                  U5        Mh     U R                  5       (       a  UR	                  S5        SU R                  R                  [        U 5      SR                  U5      4-  $ )Nr!   zp(%d)privatez<%s @0x%x %s>,)rj   r   r!   size_in_bitsappendhasattrr;   	__class____name__idjoin)r.   attrsrC   bitss       r3   __repr__DsaKey.__repr__   s    ACxtvv335Ww./!!Q  LL#$.."9"92d8SXXe_!UUUr6   c                 f     [        U R                  U   5      $ ! [         a    [        U5      ef = fra   )rA   r-   KeyErrorAttributeError)r.   items     r3   __getattr__DsaKey.__getattr__   s4    	'tyy'' 	' &&	's    0Nc                    Ub  [        U5      nUc  [        R                  nUS:X  a  S Vs/ s H  o`R                  U   R	                  5       PM!     nnS nU Vs/ s H
  oh" U5      PM     n	nS/U	-   n
SR                  U
 Vs/ s H&  n[        R                  " S[        U5      5      U-   PM(     sn5      nS[        R                  " U5      SS	 -   $ [        U R                  U R                  U R                  /5      nU R                  5       (       a  Uc  S
nU(       aW  U(       d  Sn[!        U R"                  5      R%                  5       n[&        R(                  " U[*        UXMUS9nU(       a  SnOSnSnOUS:w  a  U(       a  [-        S5      eSU R                  U R                  U R                  U R.                  U R"                  /n[        U5      R%                  5       nSnO8U(       a  [-        S5      e[1        [*        [!        U R.                  5      U5      nSnUS:X  a  U$ US:X  a&  [2        R$                  " UUS-   X55      n[        U5      $ [-        SU-  5      es  snf s  snf s  snf )a_  Export this DSA key.

Args:
  format (string):
    The encoding for the output:

    - *'PEM'* (default). ASCII as per `RFC1421`_/ `RFC1423`_.
    - *'DER'*. Binary ASN.1 encoding.
    - *'OpenSSH'*. ASCII one-liner as per `RFC4253`_.
      Only suitable for public keys, not for private keys.

  passphrase (string):
    *Private keys only*. The pass phrase to protect the output.

  pkcs8 (boolean):
    *Private keys only*. If ``True`` (default), the key is encoded
    with `PKCS#8`_. If ``False``, it is encoded in the custom
    OpenSSL/OpenSSH container.

  protection (string):
    *Only in combination with a pass phrase*.
    The encryption scheme to use to protect the output.

    If :data:`pkcs8` takes value ``True``, this is the PKCS#8
    algorithm to use for deriving the secret and encrypting
    the private DSA key.
    For a complete list of algorithms, see :mod:`Crypto.IO.PKCS8`.
    The default is *PBKDF2WithHMAC-SHA1AndDES-EDE3-CBC*.

    If :data:`pkcs8` is ``False``, the obsolete PEM encryption scheme is
    used. It is based on MD5 for key derivation, and Triple DES for
    encryption. Parameter :data:`protection` is then ignored.

    The combination ``format='DER'`` and ``pkcs8=False`` is not allowed
    if a passphrase is present.

  randfunc (callable):
    A function that returns random bytes.
    By default it is :func:`Crypto.Random.get_random_bytes`.

Returns:
  byte string : the encoded key

Raises:
  ValueError : when the format is unknown or when you try to encrypt a private
    key with *DER* format and OpenSSL/OpenSSH.

.. warning::
    If you don't provide a pass phrase, the private key will be
    exported in the clear!

.. _RFC1421:    http://www.ietf.org/rfc/rfc1421.txt
.. _RFC1423:    http://www.ietf.org/rfc/rfc1423.txt
.. _RFC4253:    http://www.ietf.org/rfc/rfc4253.txt
.. _`PKCS#8`:   http://www.ietf.org/rfc/rfc5208.txt
NOpenSSH)r!   r"   r    r   c                 N    [        U S   5      S-  (       a  [        S5      U -   $ U $ )Nr      )r   r   r&   s    r3   funcDsaKey.export_key.<locals>.func  s%    1J%7Q;&Hr6      ssh-dssr6   >I   ssh-dss Tz"PBKDF2WithHMAC-SHA1AndDES-EDE3-CBC)
key_paramsrandfunczENCRYPTED PRIVATEPRIVATEr   z#DSA private key cannot be encryptedr   zDSA PRIVATEz*PKCS#8 is only meaningful for private keysPUBLICDERz KEYz3Unknown key format '%s'. Cannot export the DSA key.)r	   r   get_random_bytesr-   to_bytesr   structpacklenbinascii
b2a_base64r   r!   r"   r    r;   r   r#   encoder   wrapoidr*   r   r   r   )r.   formatpkcs8
passphrase
protectionr   r#   tup1r   tup2keypartskp	keystringparamsprivate_key
binary_keykey_typeintspem_strs                      r3   
export_keyDsaKey.export_key   s;   v ! ,J..HY5IJ5IIIaL))+5IDJ &**TDGTD*"|d*HGOPxV[[s2w7"<xPI !4!4Y!?!DDD dffdffdff56}!!EJ(0779"ZZ +S* *)1"

 2H(H!
U?z$%JKK466466466466466B(.557
( !MNN8 *466 2F<JHU?U?jj *Hv,= *G 7##NQWWXXs K + Qs   &II$-I)c                     [        S5      eNz'Use module Crypto.Signature.DSS insteadNotImplementedError)r.   MKs      r3   signDsaKey.signU      !"KLLr6   c                     [        S5      er   r   )r.   r   	signatures      r3   verifyDsaKey.verifyX  r   r6   c                     [         era   r   )r.   	plaintextr   s      r3   encryptDsaKey.encrypt[      !!r6   c                     [         era   r   )r.   
ciphertexts     r3   decryptDsaKey.decrypt^  r   r6   c                     [         era   r   r.   r   Bs      r3   blindDsaKey.blinda  r   r6   c                     [         era   r   r   s      r3   unblindDsaKey.unblindd  r   r6   c                     [         era   r   rV   s    r3   sizeDsaKey.sizeg  r   r6   rU   )r   NNNN)r   
__module____qualname____firstlineno____doc__rj   r4   rJ   rR   r;   rZ   r]   rf   rn   rq   rv   ry   r   r   r   	exportKey	publickeyr   r   r   r   r   r   r   __static_attributes__rY   r6   r3   r   r   N   s    , )H
# "	 
)&
BV' ?C,0{Y| IIMM"""""r6   r   c           
         SSSS.R                  U 5      nUc  [        SU -  5      e[        R                  S-  nX-   S-
  U-  S-
  nU S-
  XC-  -
  n[	        S5      nSUS-
  -  n[        Xa5      [        :w  ab  U" S	5      n[        R                  " [        R                  " U5      R                  5       5      US-
  -  n	X-  S-  n[        Xa5      [        :w  a  Mb  UR                  5       U:X  d   eSn
SU S-
  -  n [        US-   5       Vs/ s HC  n[        R                  " W[	        X-   5      R                  5       -   5      R                  5       PME     nnU Vs/ s H  n[        R                  " U5      PM     nn[        [        U5       Vs/ s H  oU   SX-  -  -  PM     snX   SU-  S-
  -  SXC-  -  -  5      n[	        X-   5      nUR                  5       U :X  d   eUUS
-  -  nUUS-
  -
  nUR                  5       U :X  a  [        UU5      [        :X  a  O
XS-   -  n
GM+  US-
  U-  n[        R                  " S5       H{  nWS-   [!        S5      -   [	        U5      R                  5       -   n	[        R                  " [        R                  " U	5      R                  5       5      n[#        UUU5      nUS:w  d  M{    O   UUWW4$ s  snf s  snf s  snf )z+Generate a new set of DSA domain parameters         )         zInvalid modulus length (%d)   r8      @      s   ggen)getr*   r   digest_sizer   r   r   
from_bytesnewdigestr~   r   r   sum	itertoolscountr   r?   )Lr   Noutlennb_r"   	upper_bitseedUoffsetjVrQ   iWXcr!   er   r    s                         r3   _generate_domainr  k  s    3(,,Q/Ay6:;;!#F	
aF"Q&A	
Q!*	B 	
Aa!eI
a
*n
<|vzz$/6689Y]KMA a
*n
<
 >>q !  Fa!eI
!!a%(+(! jj
 3 < < >>?FFH( 	
 +-.0Qg  #Q0Z]C]1qz*+]C!r'Q'A!*,=>@ AM"~~1$%$QKQK>>q q(+~=a% $ 
Q1A#7NT!W$wu~'>'>'@@vzz!}33561aL6 $ q!T?3+0Cs   
A
K KKc                    Uc  [         R                  nU(       a~  [        [        U5      u  p4n[	        U5      [
        :H  nU[	        U5      [
        :H  -  nXcS-
  U-  S:g  -  nXeS:*  =(       d    XS:  -  nU[        XTU5      S:g  -  nU(       a  [        S5      eO[        X5      u  p4pWUR                  5       nUR                  5       n	X:w  a  [        SX4-  5      eX4S;  a  [        SX4-  5      eSUs=:  a  U:  d  O  [        S5      e[        R                  " U	S-   US	9n
XS-
  -  S-   n[        X[U5      nXX4US
.n[        U5      $ )a  Generate a new DSA key pair.

The algorithm follows Appendix A.1/A.2 and B.1 of `FIPS 186-4`_,
respectively for domain generation and key pair generation.

Args:
  bits (integer):
    Key length, or size (in bits) of the DSA modulus *p*.
    It must be 1024, 2048 or 3072.

  randfunc (callable):
    Random number generation function; it accepts a single integer N
    and return a string of random data N bytes long.
    If not specified, :func:`Crypto.Random.get_random_bytes` is used.

  domain (tuple):
    The DSA domain parameters *p*, *q* and *g* as a list of 3
    integers. Size of *p* and *q* must comply to `FIPS 186-4`_.
    If not specified, the parameters are created anew.

Returns:
  :class:`DsaKey` : a new DSA key object

Raises:
  ValueError : when **bits** is too little, too big, or not a multiple of 64.

.. _FIPS 186-4: http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf
r8   r   zInvalid DSA domain parametersz?Mismatch between size of modulus (%d) and 'bits' parameter (%d)))r   r   )r   r   )r   r   )r   r   z;Lengths of p and q (%d, %d) are not compatibleto FIPS 186-3zIncorrent DSA generatorr   )
exact_bitsr   r   )r   r   r@   r   r   r   r?   r*   r  r~   randomr   )r   r   ry   r!   r"   r    	fmt_error_r   r   r  r#   r   r/   s                 r3   r   r     sx   < **gv&a (*i7	(+y88	1uka''	!V%qv%	Sq\Q&&	<==  &d5
a	A	Ay 69:	B C 	C 	
v 0 0 ),-62 3 	3 q919233 	!b&8<A	UaAA!A14H(r6   c           	      r   [        [        S[        [        U 5      5      5      n[	        U5      nSnU(       Gal  [        UR                  5      [        :H  nU[        UR                  5      [        :H  -  nXCR                  S-
  UR                  -  S:g  -  nXCR                  S:*  =(       d    UR                  UR                  :  -  nU[        UR                  UR                  UR                  5      S:g  -  nXCR                  S:*  =(       d    UR                  UR                  :  -  n[        US5      (       al  XCR                  S:*  =(       d    UR                  UR                  :  -  nU[        UR                  UR                  UR                  5      UR                  :g  -  nU(       a  [        S5      eU$ )an  Construct a DSA key from a tuple of valid DSA components.

Args:
  tup (tuple):
    A tuple of long integers, with 4 or 5 items
    in the following order:

        1. Public key (*y*).
        2. Sub-group generator (*g*).
        3. Modulus, finite field order (*p*).
        4. Sub-group order (*q*).
        5. Private key (*x*). Optional.

  consistency_check (boolean):
    If ``True``, the library will verify that the provided components
    fulfil the main DSA properties.

Raises:
  ValueError: when the key being imported fails the most basic DSA validity checks.

Returns:
  :class:`DsaKey` : a DSA key object
r   Fr8   r   r#   zInvalid DSA key components)r,   zipr@   r   r   r   r!   r   r"   r    r?   r   r   r#   r*   )tupconsistency_checkr/   keyr  s        r3   r   r     sI   2 C13w3DEFH

CI'.);	(/9<<	uuqyCEE)a//	UUaZ1355CEE>1	Ssuu-22	UUaZ1355CEE>1	3!5suu~5ISUUCEE3551SUU::I566Jr6   c                     U(       a  [        S5      e[        5       R                  U SSS9nUS   S:w  a  [        S5      eS Vs/ s H  oCU   PM	     nn[        U5      $ s  snf )Nz-DSA private key already comes with parameters   T)nr_elementsonly_ints_expectedr   zNo version found)r      r8   r      )r*   r   decoder   )encodedr   r   derrD   r  s         r3   _import_openssl_privater  !  sf    HII
-

wA$

OC
1v{+,,!0
1t9C
1S> 2s   Ac                 :   [        U 5      u  p4nU[        :w  a  [        S5      eU(       a  U(       a  [        S5      e[        5       R	                  U5      R
                  n[        [        5       R	                  U=(       d    U5      5      u  pxn	XiXx4n
[        U
5      $ )NzNo DSA subjectPublicKeyInfozToo many DSA parameters)	r   r   r*   r   r  valuelistr   r   )r  r   r   algoidencoded_key
emb_paramsr   r!   r"   r    r  s              r3   _import_subjectPublicKeyInfor  +  s    'Fw'O#F}677*233K(..A;=''(<*=>GA!,CS>r6   c                 2    [        U 5      n[        US U5      $ ra   )r   r  )r  r   r   sp_infos       r3   _import_x509_certr"  9  s    .w7G'v>>r6   c                 R   U(       a  [        S5      e[        R                  " X5      nUS   [        :w  a  [        S5      e[	        5       R                  US   5      R                  n[        [        5       R                  US   5      5      u  pVn[        XtU5      XuXd4n[        U5      $ )Nz"PKCS#8 already includes parametersr   zNo PKCS#8 encoded DSA keyr8   r   )r*   r   unwrapr   r   r  r  r  r   r?   r   )	r  r   r   rC   r#   r!   r"   r    r  s	            r3   _import_pkcs8r%  ?  s    =>>W)Ats{455AaD!''A;=''!-.GA!qQ<q
$CS>r6   c                     [         [        [        [        4nU H  n U" XU5      s  $    [	        S5      e! [         a     M)  f = f)z?Import a DSA key (public or private half), encoded in DER form.DSA key format is not supported)r  r  r"  r%  r*   )key_datar   r   	decodingsdecodings        r3   _import_key_derr+  K  sW     )-" I
 	H&99  6
77  		s   5
AAc                    [        U 5      n Ub  [        U5      nU R                  S5      (       a8  [        R                  " [	        U 5      U5      u  p#nU(       a  Sn[        X!S5      $ U R                  S5      (       a  [        R                  " U R                  S5      S   5      n/ n[        U5      S:  aM  [        R                  " SUSS 5      S   nUR                  USSU-    5        USU-   S n[        U5      S:  a  MM  US   S	:X  a3  S
 Vs/ s H  n[        R                  " Xh   5      PM     n	n[        U	5      $ [        U 5      S:  a  [!        U S   5      S:X  a  [        XS5      $ [#        S5      es  snf )a  Import a DSA key.

Args:
  extern_key (string or byte string):
    The DSA key to import.

    The following formats are supported for a DSA **public** key:

    - X.509 certificate (binary DER or PEM)
    - X.509 ``subjectPublicKeyInfo`` (binary DER or PEM)
    - OpenSSH (ASCII one-liner, see `RFC4253`_)

    The following formats are supported for a DSA **private** key:

    - `PKCS#8`_ ``PrivateKeyInfo`` or ``EncryptedPrivateKeyInfo``
      DER SEQUENCE (binary or PEM)
    - OpenSSL/OpenSSH custom format (binary or PEM)

    For details about the PEM encoding, see `RFC1421`_/`RFC1423`_.

  passphrase (string):
    In case of an encrypted private key, this is the pass phrase
    from which the decryption key is derived.

    Encryption may be applied either at the `PKCS#8`_ or at the PEM level.

Returns:
  :class:`DsaKey` : a DSA key object

Raises:
  ValueError : when the given key cannot be parsed (possibly because
    the pass phrase is wrong).

.. _RFC1421: http://www.ietf.org/rfc/rfc1421.txt
.. _RFC1423: http://www.ietf.org/rfc/rfc1423.txt
.. _RFC4253: http://www.ietf.org/rfc/rfc4253.txt
.. _PKCS#8: http://www.ietf.org/rfc/rfc5208.txt
Ns   -----r       r8   r   r   r   r   )r   r  r8   r   0   r'  )r	   
startswithr   r  r
   r+  r   
a2b_base64splitr   r   unpackr   r   r   r   r   r*   )

extern_keyr   r  markerenc_flagr   r   lengthr#   r  s
             r3   r   r   \  se   P $JZ(
X&&"%**U:->
"KhJs55[))''
(8(8(>q(AB	)nq ]]42A7:FOOIaF
34!!f*+.I )nq  A;*$<HILq7%%hk2LCIS>!
:tJqM2d:zt<<
6
77 Js   "E8z1.2.840.10040.4.1)NN)Tra   ).__all__r   r   r   Crypto.Util.py3compatr   r   r	   r
   r   Cryptor   	Crypto.IOr   r   Crypto.Hashr   Crypto.Util.asn1r   r   r   r   r   Crypto.Math.Numbersr   Crypto.Math.Primalityr   r   r   Crypto.PublicKeyr   r   r   objectr   r  r   r   r  r  r"  r%  r+  r   	importKeyr   rY   r6   r3   <module>rB     s   2 =    H H      (3 3@ @@Z"V Z"z2jFR.p?	8"C8N 	 r6   