
    ,h;                         d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ ddgZ G d	 de      Z G d
 de      Z G d de      Z G d de      ZddZy)    )DerSequence)long_to_bytes)Integer)HMAC)EccKey)DsaKeyDssSigSchemenewc                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	r	   zkA (EC)DSA signature object.
    Do not instantiate directly.
    Use :func:`Crypto.Signature.DSS.new`.
    c                     || _         || _        || _        | j                  j                         | _        | j                  dz
  dz  dz   | _        y)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       V/var/www/html/Resume-Scraper/venv/lib/python3.12/site-packages/Crypto/Signature/DSS.py__init__zDssSigScheme.__init__3   sK     	!;;335!--1a7!;    c                 6    | j                   j                         S )zRReturn ``True`` if this signature object can be used
        for signing messages.)r   has_private)r   s    r   can_signzDssSigScheme.can_signA   s     yy$$&&r   c                     t        d      NzTo be provided by subclassesNotImplementedErrorr   msg_hashs     r   _compute_noncezDssSigScheme._compute_nonceG       !"@AAr   c                     t        d      r    r!   r#   s     r   _valid_hashzDssSigScheme._valid_hashJ   r&   r   c           	         | j                   j                         st        d      | j                  |      st	        d      | j                  |      }t        j                  |j                         d| j                         }| j                   j                  ||      }| j                  dk(  r5dj                  |D cg c]  }t        || j                         c}      }|S t        |      j                         }|S c c}w )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   signzDssSigScheme.signM   s     yy$$&;<<)>?? ##H- x01C$2C2CDE99??1e, >>X%XX(02#$  -Q0A0AB 2 3F  !*113F2s   =C>c                    | j                  |      st        d      | j                  dk(  rit        |      d| j                  z  k7  rt        d      |d| j                   || j                  d fD cg c]  }t        j                  |       c}\  }}na	 t               j                  |d      }t        |      dk7  s|j                         st        d	      t        |d
         t        |d         }}d
|cxk  r| j                  k  r.n t        d      d
|cxk  r| j                  k  st        d       t        d      t        j                  |j                         d| j                         }| j                  j                  |||f      }|st        d      yc c}w # t        t        f$ r t        d      w xY w)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*   r+      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(   r-   r   lenr   r   r.   r   decode
IndexErrorhasOnlyIntsr   r/   r   _verify)	r   r$   	signaturer6   r_primes_primeder_seqr4   results	            r   verifyzDssSigScheme.verifyz   s     )>??>>X%9~!d&7&7"78 !JKK*34FT5F5F*G*3D4E4E4F*G*I J$% !( 2 21 5  JGWI%-..y.F 7|q (;(;(= !OPP&wqz2GGAJ4GWGG)dkk)ABB 45w3L3LABB 4MABBx01C$2C2CDE""1w&89=>>) J 
+ I !GHHIs   2F F% %G N)
__name__
__module____qualname____doc__r   r   r%   r(   r8   rF    r   r   r	   r	   -   s(    
<'BB+Z*r   c                   <     e Zd Z fdZd Zd Zd Zd Zd Z xZ	S )DeterministicDsaSigSchemec                 >    t         t        |   |||       || _        y N)superrM   r   _private_key)r   r   r   r   private_key	__class__s        r   r   z"DeterministicDsaSigScheme.__init__   s    '7XuM'r   c                     t        j                  |      }| j                  j                         }t	        |      dz  }||kD  r|||z
  z  }|S )zSee 2.3.2 in RFC6979r   )r   r.   r   r   r<   )r   bstrrE   q_lenb_lens        r   	_bits2intz#DeterministicDsaSigScheme._bits2int   sM     ##D)((*D	A5=&Fr   c                 b    d|cxk  r| j                   k  sJ  J t        || j                        S )zSee 2.3.3 in RFC6979r   )r   r   r   )r   	int_mod_qs     r   _int2octetsz%DeterministicDsaSigScheme._int2octets   s1     9*t{{****Y(9(9::r   c                     | j                  |      }|| j                  k  r|}n|| j                  z
  }| j                  |      S )zSee 2.3.4 in RFC6979)rX   r   r[   )r   rU   z1z2s       r   _bits2octetsz&DeterministicDsaSigScheme._bits2octets   sB     ^^D!Bdkk!B##r   c                 L   |j                         }d|j                  z  }d|j                  z  }dD ]}  }t        j                  |||z   | j	                  | j
                        z   | j                  |      z   |      j                         }t        j                  |||      j                         } d}d|cxk  r| j                  k  sn |dk7  rMt        j                  ||dz   |      j                         }t        j                  |||      j                         }d}t        |      | j                  k  rCt        j                  |||      j                         }||z  }t        |      | j                  k  rC| j                  |      }d|cxk  r| j                  k  s|S  |S )z!Generate k in a deterministic way       )rb   ra   r   r   )r/   digest_sizer   r
   r[   rQ   r_   r   r<   r   rX   )r   mhashh1mask_vnonce_kint_octr3   mask_ts           r   r%   z(DeterministicDsaSigScheme._compute_nonce   s   
 \\^5,,,E---) 	?Ghhw%/#//0A0AB C#004 56;= >DVX 
 XXgvu5<<>F	? u*t{{*{((7FW,<#(**0&( '659@@B F f+ 1 11'659@@B&  f+ 1 11
 NN6*E! u*t{{*" # +" r   c                      y)NTrK   r#   s     r   r(   z%DeterministicDsaSigScheme._valid_hash   s    r   )
rG   rH   rI   r   rX   r[   r_   r%   r(   __classcell__rS   s   @r   rM   rM      s"    (	;$&Pr   rM   c                   .     e Zd ZdZ fdZd Zd Z xZS )FipsDsaSigScheme))i      )      )rq      )i   rs   c                     t         t        |   |||       || _        t	        |j
                        j                         }|| j                  f| j                  vrd|| j                  fz  }t        |      y )Nz+L/N (%d, %d) is not compliant to FIPS 186-3)
rP   ro   r   	_randfuncr   pr   r   _fips_186_3_L_Nr-   )r   r   r   r   randfuncLerrorrS   s          r   r   zFipsDsaSigScheme.__init__  su    .sHeD!CEEN'')t (<(<<B4++,-EU## =r   c                 Z    t        j                  d| j                  | j                        S Nr   )min_inclusivemax_exclusiverx   )r   random_ranger   ru   r#   s     r   r%   zFipsDsaSigScheme._compute_nonce  s%    ##!26++-1^^= 	=r   c                 Z    |j                   dk(  xs |j                   j                  d      S )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(   zFipsDsaSigScheme._valid_hash  s-    / A''(?@	Br   )rG   rH   rI   rw   r   r%   r(   rl   rm   s   @r   ro   ro      s    
O$=Br   ro   c                   *     e Zd Z fdZd Zd Z xZS )FipsEcDsaSigSchemec                 >    t         t        |   |||       || _        y rO   )rP   r   r   ru   )r   r   r   r   rx   rS   s        r   r   zFipsEcDsaSigScheme.__init__  s     $0hF!r   c                     t        j                  d| j                  j                  j                  | j
                        S r|   )r   r   r   _curver   ru   r#   s     r   r%   z!FipsEcDsaSigScheme._compute_nonce  s1    ##!26))2B2B2H2H-1^^= 	=r   c                     | j                   j                  j                         }d}d}d}d}||z   |z   |z   }	 |j                  |v }|S # t        $ r d}Y |S w xY w)zxVerify 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shsrE   s	            r   r(   zFipsEcDsaSigScheme._valid_hash"  s{     yy''446 `_EFvo&/	\\S(F   	F	s   A	 	AA)rG   rH   rI   r   r%   r(   rl   rm   s   @r   r   r     s    "=
r   r   Nc                 4   |dvrt        d|z        t        | t              r>| j                  j                  }d}| j
                  j                  d      sSt        d      t        | t              rt        | j                        }d}n t        dt        t        |             z         | j                         rt        | |      }nd}|d	k(  rt        | |||      S |d
k(  r,t        | t              rt        | |||      S t!        | |||      S t        d|z        )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
    )r+   derzUnknown encoding '%s'dNISTz ECC key is not on a NIST P curver6   zUnsupported key type Nzdeterministic-rfc6979z
fips-186-3zUnknown DSS mode '%s')r-   
isinstancer   r   r   curver   r   r   qstrtyper   getattrrM   r   ro   )r   moder   rx   r   private_key_attrrR   s          r   r
   r
   6  s   D ((08;<<#v

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