
    ÿig;                         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dS )    )DerSequence)long_to_bytes)Integer)HMAC)EccKey)DsaKeyDssSigSchemenewc                   6    e Zd ZdZd Zd Zd Zd Zd Zd Z	dS )	r	   zoA (EC)DSA signature object.
    Do not instantiate directly.
    Use :func:`Cryptodome.Signature.DSS.new`.
    c                     || _         || _        || _        | j                                        | _        | j        dz
  dz  dz   | _        dS )zCreate a new Digital Signature Standard (DSS) object.

        Do not instantiate this object directly,
        use `Cryptodome.Signature.DSS.new` instead.
              N)_key	_encoding_ordersize_in_bits_order_bits_order_bytes)selfkeyencodingorders       T/var/www/html/afkarena/venv/lib/python3.11/site-packages/Cryptodome/Signature/DSS.py__init__zDssSigScheme.__init__3   sO     	!;3355!-1a7!;    c                 4    | 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     y$$&&&r   c                      t          d          NzTo be provided by subclassesNotImplementedErrorr   msg_hashs     r   _compute_noncezDssSigScheme._compute_nonceG       !"@AAAr   c                      t          d          r    r!   r#   s     r   _valid_hashzDssSigScheme._valid_hashJ   r&   r   c                      j                                         st          d                               |          st	          d                               |          }t          j        |                                d j	                           } j         
                    ||          } j        dk    r"d                     fd|D                       }n!t          |                                          }|S )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:`Cryptodome.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   c                 :    g | ]}t          |j                  S  )r   r   ).0xr   s     r   
<listcomp>z%DssSigScheme.sign.<locals>.<listcomp>k   s6     2 2 2#$  -Q0ABB 2 2 2r   )r   r   	TypeErrorr(   
ValueErrorr%   r   
from_bytesdigestr   _signr   joinr   encode)r   r$   noncezsig_pairoutputs   `     r   signzDssSigScheme.signM   s    y$$&& 	=;<<<)) 	@>??? ##H-- x001C$2C1CDEE9??1e,, >X%%XX 2 2 2 2(02 2 2 3 3FF !**1133Fr   c                    |                      |          st          d          | j        dk    rVt          |          d| j        z  k    rt          d          d |d| j                 || j        d         fD             \  }}n	 t                                          |d          }n$# t          t          f$ r t          d	          w xY wt          |          dk    s|                                st          d
          t          |d                   t          |d                   }}d|cxk     r| j
        k     rn nd|cxk     r| j
        k     sn t          d          t          j        |                                d| j                           }| j                            |||f          }|st          d          dS )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:`Cryptodome.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)c                 6    g | ]}t          j        |          S r-   )r   r3   )r.   r/   s     r   r0   z'DssSigScheme.verify.<locals>.<listcomp>   s9      J  J  J$% !( 21 5 5  J  J  Jr   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   decode
IndexErrorhasOnlyIntsr   r   r3   r4   r   _verify)r   r$   	signaturer_primes_primeder_seqr9   results           r   verifyzDssSigScheme.verifyz   s
     )) 	@>???>X%%9~~!d&7"788 !JKKK J  J*34FT5F4F*G*3D4E4F4F*G*I J  J  JGWWI%--..y.FF
+ I I I !GHHHI7||q  (;(;(=(=  !OPPP&wqz22GGAJ4G4GWGG))))dk)))))1w3L3L3L3L3L3L3L3LABBBx001C$2C1CDEE""1w&899 	?=>>>us   #B+ +!CN)
__name__
__module____qualname____doc__r   r   r%   r(   r<   rK   r-   r   r   r	   r	   -   s         
< < <' ' 'B B BB 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                 j    t          t          |                               |||           || _        d S N)superrQ   r   _private_key)r   r   r   r   private_key	__class__s        r   r   z"DeterministicDsaSigScheme.__init__   s4    '..77XuMMM'r   c                     t          j        |          }| j                                        }t	          |          dz  }||k    r|||z
  z  }|S )zSee 2.3.2 in RFC6979r   )r   r3   r   r   rA   )r   bstrrJ   q_lenb_lens        r   	_bits2intz#DeterministicDsaSigScheme._bits2int   sR     #D))((**D		A5==&Fr   c                 X    d|cxk     r| j         k     sn J t          || j                  S )zSee 2.3.3 in RFC6979r   )r   r   r   )r   	int_mod_qs     r   _int2octetsz%DeterministicDsaSigScheme._int2octets   s>     9****t{******Y(9:::r   c                     |                      |          }|| j        k     r|}n
|| j        z
  }|                     |          S )zSee 2.3.4 in RFC6979)r\   r   r_   )r   rY   z1z2s       r   _bits2octetsz&DeterministicDsaSigScheme._bits2octets   sI     ^^D!!BBdk!B###r   c                 X   |                                 }d|j        z  }d|j        z  }dD ]}t          j        |||z   |                     | j                  z   |                     |          z   |                                           }t          j        |||                                           }d}d|cxk     r| j        k     sn |dk    rSt          j        ||dz   |                                           }t          j        |||                                           }d}t          |          | j	        k     rEt          j        |||                                           }||z  }t          |          | j	        k     E| 
                    |          }d|cxk     r| j        k     n |S )z!Generate k in a deterministic way       )rf   re   r   r   )r4   digest_sizer   r
   r_   rU   rc   r   rA   r   r\   )r   mhashh1mask_vnonce_kint_octr8   mask_ts           r   r%   z(DeterministicDsaSigScheme._compute_nonce   s   
 \\^^5,,E--) 	? 	?Ghw%/#//0ABB C#0044 56;= = >DVXX 
 Xgvu55<<>>FFu****t{****{{(7FW,<#(* **0&(( '6599@@BB F f++ 111'6599@@BB&  f++ 111
 NN6**E! u****t{*****" r   c                     dS )NTr-   r#   s     r   r(   z%DeterministicDsaSigScheme._valid_hash   s    tr   )
rL   rM   rN   r   r\   r_   rc   r%   r(   __classcell__rW   s   @r   rQ   rQ      s        ( ( ( ( (	 	 	; ; ;$ $ $& & &P      r   rQ   c                   .     e Zd ZdZ fdZd Zd Z xZS )FipsDsaSigScheme))i      )      )ru      )i   rw   c                    t          t          |                               |||           || _        t	          |j                                                  }|| j        f| j        vrd|| j        fz  }t          |          d S )Nz+L/N (%d, %d) is not compliant to FIPS 186-3)
rT   rs   r   	_randfuncr   pr   r   _fips_186_3_L_Nr2   )r   r   r   r   randfuncLerrorrW   s          r   r   zFipsDsaSigScheme.__init__  s    %%..sHeDDD!CENN''))t (<<<B4+,-EU### =<r   c                 D    t          j        d| j        | j                  S Nr   )min_inclusivemax_exclusiver|   )r   random_ranger   ry   r#   s     r   r%   zFipsDsaSigScheme._compute_nonce  s(    #!26+-1^= = = 	=r   c                 L    |j         dk    p|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   )rL   rM   rN   r{   r   r%   r(   rp   rq   s   @r   rs   rs      sd        
O$ $ $ $ $= = =B B B B B B Br   rs   c                   *     e Zd Z fdZd Zd Z xZS )FipsEcDsaSigSchemec                 j    t          t          |                               |||           || _        d S rS   )rT   r   r   ry   )r   r   r   r   r|   rW   s        r   r   zFipsEcDsaSigScheme.__init__  s1     $''00hFFF!r   c                 X    t          j        d| j        j        j        | j                  S r   )r   r   r   _curver   ry   r#   s     r   r%   z!FipsEcDsaSigScheme._compute_nonce  s.    #!26)2B2H-1^= = = 	=r   c                     | j         j                                        }d}d}d}d}||z   |z   |z   }	 |j        |v }n# t          $ r d}Y nw xY w|S )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shsrJ   s	            r   r(   zFipsEcDsaSigScheme._valid_hash"  s     y'4466 `_EFvo&/	\S(FF 	 	 	FFF	s   	= AA)rL   rM   rN   r   r%   r(   rp   rq   s   @r   r   r     sV        " " " " "= = =
      r   r   r+   Nc                    |dvrt          d|z            t          | t                    r8| j        j        }d}| j                            d          st          d          nXt          | t                    rt          | j	                  }d}n,t          dt          t          |                     z             |                                 rt          | |          }nd}|d	k    rt          | |||          S |d
k    r9t          | 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:`Cryptodome.PublicKey.DSA` or :class:`Cryptodome.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 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   getattrrQ   r   rs   )r   moder   r|   r   private_key_attrrV   s          r   r
   r
   6  sb   D (((08;<<<#v 	C
 y##F++ 	A?@@@	A	C	 	  C03tCyy>>ABBB
 c#344&&&(h{KKK			c6"" 	D%c8UHEEE#C5(CCC047888r   )r+   N)Cryptodome.Util.asn1r   Cryptodome.Util.numberr   Cryptodome.Math.Numbersr   Cryptodome.Hashr   Cryptodome.PublicKey.ECCr   Cryptodome.PublicKey.DSAr   __all__objectr	   rQ   rs   r   r
   r-   r   r   <module>r      se  D - , , , , , 0 0 0 0 0 0 + + + + + +             + + + + + + + + + + + +5
!w w w w w6 w w wtK K K K K K K K\B B B B B| B B BD       >]9 ]9 ]9 ]9 ]9 ]9r   