
    i;                         S SK Jr  S SKJr  S SKJr  S SKJr  S SKJ	r	  S SK
Jr  SS/r " S	 S\5      r " S
 S\5      r " S S\5      r " S S\5      rSS jrg)    )DerSequence)long_to_bytes)Integer)HMAC)EccKey)DsaKeyDssSigSchemenewc                   <    \ rS rSrSrS rS rS rS rS r	S r
S	rg
)r	   -   z_A (EC)DSA signature object.
Do not instantiate directly.
Use :func:`Crypto.Signature.DSS.new`.
c                     Xl         X l        X0l        U R                  R                  5       U l        U R                  S-
  S-  S-   U l        g)zCreate a new Digital Signature Standard (DSS) object.

Do not instantiate this object directly,
use `Crypto.Signature.DSS.new` instead.
      N)_key	_encoding_ordersize_in_bits_order_bits_order_bytes)selfkeyencodingorders       O/var/www/html/trading/venv/lib/python3.13/site-packages/Crypto/Signature/DSS.py__init__DssSigScheme.__init__3   sE     	!;;335!--1a7!;    c                 6    U R                   R                  5       $ )zJReturn ``True`` if this signature object can be used
for signing messages.)r   has_private)r   s    r   can_signDssSigScheme.can_signA   s     yy$$&&r   c                     [        S5      eNzTo be provided by subclassesNotImplementedErrorr   msg_hashs     r   _compute_nonceDssSigScheme._compute_nonceG       !"@AAr   c                     [        S5      er#   r$   r&   s     r   _valid_hashDssSigScheme._valid_hashJ   r*   r   c           	          U R                   R                  5       (       d  [        S5      eU R                  U5      (       d  [	        S5      eU R                  U5      n[        R                  " UR                  5       SU R                   5      nU R                   R                  X25      nU R                  S:X  a7  SR                  U Vs/ s H  n[        XPR                  5      PM     sn5      nU$ [        U5      R                  5       nU$ s  snf )a  Compute the DSA/ECDSA signature of a message.

Args:
  msg_hash (hash object):
    The hash that was carried out over the message.
    The object belongs to the :mod:`Crypto.Hash` package.
    Under mode ``'fips-186-3'``, the hash must be a FIPS
    approved secure hash (SHA-2 or SHA-3).

:return: The signature as ``bytes``
:raise ValueError: if the hash algorithm is incompatible to the (EC)DSA key
:raise TypeError: if the (EC)DSA key has no private half
zPrivate key is needed to signHash is not sufficiently strongNbinaryr   )r   r   	TypeErrorr,   
ValueErrorr(   r   
from_bytesdigestr   _signr   joinr   r   encode)r   r'   noncezsig_pairxoutputs          r   signDssSigScheme.signM   s     yy$$&&;<<))>?? ##H- x01C$2C2CDE99??1, >>X%XX(02(01  -Q0A0AB(02 3F  !*113F2s   Dc                    U R                  U5      (       d  [        S5      eU R                  S:X  al  [        U5      SU R                  -  :w  a  [        S5      eUSU R                   X R                  S 4 Vs/ s H  n[
        R                  " U5      PM     snu  pEOc [        5       R                  USS9n[        U5      S:w  d  UR                  5       (       d  [        S	5      e[        US
   5      [        US   5      pTS
Us=:  a  U R                  :  a  O  OS
Us=:  a  U R                  :  d  O  [        S5      e[
        R                  " UR                  5       SU R                   5      nU R                  R                  XtU45      nU(       d  [        S5      egs  snf ! [        [        4 a    [        S5      ef = f)a  Check if a certain (EC)DSA signature is authentic.

Args:
  msg_hash (hash object):
    The hash that was carried out over the message.
    This is an object belonging to the :mod:`Crypto.Hash` module.
    Under mode ``'fips-186-3'``, the hash must be a FIPS
    approved secure hash (SHA-2 or SHA-3).

  signature (``bytes``):
    The signature that needs to be validated.

:raise ValueError: if the signature is not authentic
r/   r0      z'The signature is not authentic (length)NT)strictz$The signature is not authentic (DER)z,The signature is not authentic (DER content)r   r   z"The signature is not authentic (d)zThe signature is not authenticF)r,   r2   r   lenr   r   r3   r   decode
IndexErrorhasOnlyIntsr   r4   r   _verify)	r   r'   	signaturer;   r_primes_primeder_seqr9   results	            r   verifyDssSigScheme.verifyz   s     ))>??>>X%9~!d&7&7"78 !JKK*34FT5F5F*G*34E4E4F*G*I J*IA !( 2 21 5*I JGWI%-..y.F 7|q (;(;(=(= !OPP&wqz2GGAJ4GWG)dkk)1w3L3LABBx01C$2C2CDE""1&89=>>) J 
+ I !GHHIs   8 F F% %G)r   r   r   r   r   N)__name__
__module____qualname____firstlineno____doc__r   r    r(   r,   r=   rL   __static_attributes__ r   r   r	   r	   -   s(    
<'BB+Z*r   c                   F   ^  \ rS rSrU 4S jrS rS rS rS rS r	Sr
U =r$ )	DeterministicDsaSigScheme   c                 :   > [         [        U ]  XU5        X@l        g N)superrV   r   _private_key)r   r   r   r   private_key	__class__s        r   r   "DeterministicDsaSigScheme.__init__   s    '7uM'r   c                     [         R                  " U5      nU R                  R                  5       n[	        U5      S-  nXC:  a  X$U-
  -  nU$ )zSee 2.3.2 in RFC6979r   )r   r3   r   r   rB   )r   bstrrK   q_lenb_lens        r   	_bits2int#DeterministicDsaSigScheme._bits2int   sI     ##D)((*D	A=&Fr   c                 d    SUs=:  a  U R                   :  d   e   e[        XR                  5      $ )zSee 2.3.3 in RFC6979r   )r   r   r   )r   	int_mod_qs     r   _int2octets%DeterministicDsaSigScheme._int2octets   s2     9*t{{*****Y(9(9::r   c                     U R                  U5      nX R                  :  a  UnOX R                  -
  nU R                  U5      $ )zSee 2.3.4 in RFC6979)rc   r   rg   )r   r`   z1z2s       r   _bits2octets&DeterministicDsaSigScheme._bits2octets   s>     ^^D!Bkk!B##r   c                 b   UR                  5       nSUR                  -  nSUR                  -  nS H~  n[        R                  " UX5-   U R	                  U R
                  5      -   U R                  U5      -   U5      R                  5       n[        R                  " XCU5      R                  5       nM     SnSUs=:  a  U R                  :  d  O  US:w  aM  [        R                  " XCS-   U5      R                  5       n[        R                  " XCU5      R                  5       nSn[        U5      U R                  :  aD  [        R                  " XCU5      R                  5       nXs-  n[        U5      U R                  :  a  MD  U R                  U5      nSUs=:  a  U R                  :  d  M   U$   NU$ )z!Generate k in a deterministic way       )rp   ro   r   r   )r4   digest_sizer   r
   rg   r[   rl   r   rB   r   rc   )r   mhashh1mask_vnonce_kint_octr8   mask_ts           r   r(   (DeterministicDsaSigScheme._compute_nonce   s   
 \\^5,,,E---)Ghhw%/#//0A0AB C#004 56;= >DVX 
 XXgu5<<>F * u*t{{*{((7W,<#(**0&( '59@@B F f+ 1 11'59@@B  f+ 1 11
 NN6*E! u*t{{*" # +" r   c                     g)NTrT   r&   s     r   r,   %DeterministicDsaSigScheme._valid_hash   s    r   )r[   )rN   rO   rP   rQ   r   rc   rg   rl   r(   r,   rS   __classcell__r]   s   @r   rV   rV      s'    (	;$&P r   rV   c                   8   ^  \ rS rSrSrU 4S jrS rS rSrU =r	$ )FipsDsaSigScheme   ))i      )      )r      )i   r   c                    > [         [        U ]  XU5        X@l        [	        UR
                  5      R                  5       nXPR                  4U R                  ;  a  SXPR                  4-  n[        U5      eg )Nz+L/N (%d, %d) is not compliant to FIPS 186-3)
rZ   r   r   	_randfuncr   pr   r   _fips_186_3_L_Nr2   )r   r   r   r   randfuncLerrorr]   s          r   r   FipsDsaSigScheme.__init__  sm    .seD!CEEN'') (<(<<B++,-EU## =r   c                 V    [         R                  " SU R                  U R                  S9$ Nr   )min_inclusivemax_exclusiver   )r   random_ranger   r   r&   s     r   r(   FipsDsaSigScheme._compute_nonce  s%    ##!26++-1^^= 	=r   c                 d    UR                   S:H  =(       d    UR                   R                  S5      $ )z*Verify that SHA-1, SHA-2 or SHA-3 are usedz1.3.14.3.2.26z2.16.840.1.101.3.4.2.)oid
startswithr&   s     r   r,   FipsDsaSigScheme._valid_hash  s-    / A''(?@	Br   r   )
rN   rO   rP   rQ   r   r   r(   r,   rS   r|   r}   s   @r   r   r      s     
O$=B Br   r   c                   4   ^  \ rS rSrU 4S jrS rS rSrU =r$ )FipsEcDsaSigSchemei  c                 :   > [         [        U ]  XU5        X@l        g rY   )rZ   r   r   r   )r   r   r   r   r   r]   s        r   r   FipsEcDsaSigScheme.__init__  s     $0F!r   c                 ~    [         R                  " SU R                  R                  R                  U R
                  S9$ r   )r   r   r   _curver   r   r&   s     r   r(   !FipsEcDsaSigScheme._compute_nonce  s1    ##!26))2B2B2H2H-1^^= 	=r   c                     U R                   R                  R                  5       nSnSnSnSnX4-   U-   U-   n UR                  U;   nU$ ! [         a    Sn U$ f = f)zpVerify that the strength of the hash matches or exceeds
the strength of the EC. We fail if the hash is too weak.)z2.16.840.1.101.3.4.2.4z2.16.840.1.101.3.4.2.7z2.16.840.1.101.3.4.2.5)z2.16.840.1.101.3.4.2.1z2.16.840.1.101.3.4.2.8z2.16.840.1.101.3.4.2.6)z2.16.840.1.101.3.4.2.2z2.16.840.1.101.3.4.2.9)z2.16.840.1.101.3.4.2.3z2.16.840.1.101.3.4.2.10F)r   pointQr   r   AttributeError)	r   r'   modulus_bitssha224sha256sha384sha512shsrK   s	            r   r,   FipsEcDsaSigScheme._valid_hash"  sy     yy''446 `_EFo&/	\\S(F   	F	s   A	 	AAr   )	rN   rO   rP   rQ   r   r(   r,   rS   r|   r}   s   @r   r   r     s    "=
 r   r   Nc                 b   US;  a  [        SU-  5      e[        U [        5      (       aD  U R                  R                  nSnU R
                  R                  S5      (       d  [        S5      eOM[        U [        5      (       a  [        U R                  5      nSnO [        S[        [        U 5      5      -   5      eU R                  5       (       a  [        X5      nOSnUS	:X  a  [        XXF5      $ US
:X  a-  [        U [        5      (       a  [        XXC5      $ [!        XXC5      $ [        SU-  5      e)a	  Create a signature object :class:`DssSigScheme` that
can perform (EC)DSA signature or verification.

.. note::
    Refer to `NIST SP 800 Part 1 Rev 4`_ (or newer release) for an
    overview of the recommended key lengths.

Args:
    key (:class:`Crypto.PublicKey.DSA` or :class:`Crypto.PublicKey.ECC`):
        The key to use for computing the signature (*private* keys only)
        or for verifying one.
        For DSA keys, let ``L`` and ``N`` be the bit lengths of the modulus ``p``
        and of ``q``: the pair ``(L,N)`` must appear in the following list,
        in compliance to section 4.2 of `FIPS 186-4`_:

        - (1024, 160) *legacy only; do not create new signatures with this*
        - (2048, 224) *deprecated; do not create new signatures with this*
        - (2048, 256)
        - (3072, 256)

        For ECC, only keys over P-224, P-256, P-384, and P-521 are accepted.

    mode (string):
        The parameter can take these values:

        - ``'fips-186-3'``. The signature generation is randomized and carried out
          according to `FIPS 186-3`_: the nonce ``k`` is taken from the RNG.
        - ``'deterministic-rfc6979'``. The signature generation is not
          randomized. See RFC6979_.

    encoding (string):
        How the signature is encoded. This value determines the output of
        :meth:`sign` and the input to :meth:`verify`.

        The following values are accepted:

        - ``'binary'`` (default), the signature is the raw concatenation
          of ``r`` and ``s``. It is defined in the IEEE P.1363 standard.
          For DSA, the size in bytes of the signature is ``N/4`` bytes
          (e.g. 64 for ``N=256``).
          For ECDSA, the signature is always twice the length of a point
          coordinate (e.g. 64 bytes for P-256).

        - ``'der'``, the signature is a ASN.1 DER SEQUENCE
          with two INTEGERs (``r`` and ``s``). It is defined in RFC3279_.
          The size of the signature is variable.

    randfunc (callable):
        A function that returns random ``bytes``, of a given length.
        If omitted, the internal RNG is used.
        Only applicable for the *'fips-186-3'* mode.

.. _FIPS 186-3: http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf
.. _FIPS 186-4: http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf
.. _NIST SP 800 Part 1 Rev 4: http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-57pt1r4.pdf
.. _RFC6979: http://tools.ietf.org/html/rfc6979
.. _RFC3279: https://tools.ietf.org/html/rfc3279#section-2.2.2
)r0   derzUnknown encoding '%s'dNISTz ECC key is not on a NIST P curver;   zUnsupported key type Nzdeterministic-rfc6979z
fips-186-3zUnknown DSS mode '%s')r2   
isinstancer   r   r   curver   r   r   qstrtyper   getattrrV   r   r   )r   moder   r   r   private_key_attrr\   s          r   r
   r
   6  s   D ((08;<<#v

  yy##F++?@@ ,	C	 	 03tCy>ABB
c4&&(KK		c6""%cUEE#C5CC04788r   )r0   N)Crypto.Util.asn1r   Crypto.Util.numberr   Crypto.Math.Numbersr   Crypto.Hashr   Crypto.PublicKey.ECCr   Crypto.PublicKey.DSAr   __all__objectr	   rV   r   r   r
   rT   r   r   <module>r      sc   D ) , '  ' '5
!w6 wtK K\B| BD >]9r   