
    tZh                        	 d dl Z	 e d dlZd dlZd dlZd dl	Z	ddl
mZmZmZmZ ddlmZmZmZmZmZmZmZ ddlmZmZmZmZmZmZ ddlmZm Z m!Z!m"Z"m#Z# ddl$m%Z%m&Z&m'Z'm(Z( ddl)m*Z*  G d	 d
ejV                        Z, G d dejV                        Z- G d dejV                        Z. G d dejV                        Z/dZ0 ee0      Z1 e2e1e3      sJ  ejh                  e1      Z5e5jl                  Z7dZ8 e9e8      dz  d k(  sJ  e	jt                         Z:e:jw                  e8       e:jy                         Z= e2e=e3      sJ e5j}                  e8e      Z? e2e?e3      sJ e5j}                  e8e      Z@ e2e@e3      sJ e5j}                  e8e      ZA e2eAd    e3      sJ g ZBdd fdefdeCfdd fdd fdd fdd  fd!d" ffD ]  \  ZDZEd#eDv rd$ ZFneEZFd%e?ed& fd'e@ed( fd)eAed* ffD ]g  \  ZGZHZIZJd+e7j                  e8fd,e7j                  e=ffD ]?  \  ZMZNZOeBj                   ej                  eHeIeJeEeNeOd-j                  eDeGeM      .             A i  ej                  j                  d/eB      d0        ZUd1ZV e9eV      d2k(  sJ g ZWdd3 fdefdeCfdd4 fdd5 fdd6 fdd7 fd!d8 ffD ](  \  ZDZXeWj                   ej                  eXeD.             * ej                  j                  d9eW      d:        ZYdZ0 ee0      Z1 e2e1e3      sJ ej                  j                  d9eWdd;       d<        ZZd=Z[ej                  j                  d9eW      d>        Z\ej                  j                  d9eW      d?        Z]ej                  j                  d9eW      d@        Z^ej                  j                  d9eW      dA        Z_dB Z`dC ZadD ZbdE ZcdF Zdy# e$ r d dlZY w xY w# e$ r eZY w xY w)G    N   )VerifyingKey
SigningKeyMalformedPointErrorBadSignatureError)unpemUnexpectedDERencode_sequence
encode_oidencode_bitstringencode_integerencode_octet_string)sigencode_stringsigencode_dersigencode_stringssigdecode_stringsigdecode_dersigdecode_strings)NIST256pCurveBRAINPOOLP160r1Ed25519Ed448)PointPointJacobiCurveFpINFINITY)generator_brainpoolp160r1c                   n    e Zd ZdZed        Zd Zd Zd Zd Z	d Z
d Zd	 Zd
 Zd Zd Zd Zd Zd Zy)TestVerifyingKeyFromStringzc
    Verify that ecdsa.keys.VerifyingKey.from_string() can be used with
    bytes-like objects
    c                 Z    d| _         t        j                  | j                         | _        y )Ns0   LZ
zϗo8}X!h^pIYxao+Gv)	key_bytesr   from_stringvk)clss    k/var/www/html/wine-match-dev/backend/winematch-backend/venv/lib/python3.12/site-packages/ecdsa/test_keys.py
setUpClassz%TestVerifyingKeyFromString.setUpClass2   s&    / 	
 ))#--8    c                 r   | j                  | j                         | j                  | j                  t               | j	                  | j                  j
                  j                  j                         d       | j	                  | j                  j
                  j                  j                         d       y )Nl   !Xp?|}rIWoWeD l   vq.y[Or~Vh]Ip<#'
)	assertIsNotNoner$   assertIsInstancer   assertEqualpubkeypointxyselfs    r&   
test_bytesz%TestVerifyingKeyFromString.test_bytes;   s    TWW%dgg|4GGNN  ""$E	
 	GGNN  ""$F	
r(   c                     t        j                  t        | j                              }| j	                  | j
                  j                         |j                                y N)r   r#   bufferr"   r,   r$   	to_stringr2   r$   s     r&   test_bytes_memoryviewz0TestVerifyingKeyFromString.test_bytes_memoryviewG   s>    %%fT^^&<=**,blln=r(   c                     t        j                  t        | j                              }| j	                  | j
                  j                         |j                                y r5   r   r#   	bytearrayr"   r,   r$   r7   r8   s     r&   test_bytearrayz)TestVerifyingKeyFromString.test_bytearrayL   s>    %%i&?@**,blln=r(   c                     t        j                  t        t        | j                                    }| j                  | j                  j                         |j                                y r5   )r   r#   r6   r<   r"   r,   r$   r7   r8   s     r&   test_bytesarray_memoryviewz5TestVerifyingKeyFromString.test_bytesarray_memoryviewQ   sC    %%fYt~~-F&GH**,blln=r(   c                     t        j                   d| j                        }t        j                  |      }| j	                  | j
                  j                         |j                                y NBarrayr"   r   r#   r,   r$   r7   r2   arrr$   s      r&   test_array_array_of_bytesz4TestVerifyingKeyFromString.test_array_array_of_bytesV   I    kk#t~~.%%c***,blln=r(   c                     t        j                   d| j                        }t        j                  t	        |            }| j                  | j                  j                         |j                                y rA   rD   r"   r   r#   r6   r,   r$   r7   rE   s      r&   $test_array_array_of_bytes_memoryviewz?TestVerifyingKeyFromString.test_array_array_of_bytes_memoryview\   M    kk#t~~.%%fSk2**,blln=r(   c                     t        j                   d| j                        }t        j                  |      }| j	                  | j
                  j                         |j                                y NIrC   rE   s      r&   test_array_array_of_intsz3TestVerifyingKeyFromString.test_array_array_of_intsb   rH   r(   c                     t        j                   d| j                        }t        j                  t	        |            }| j                  | j                  j                         |j                                y rN   rJ   rE   s      r&   #test_array_array_of_ints_memoryviewz>TestVerifyingKeyFromString.test_array_array_of_ints_memoryviewh   rL   r(   c                     t        j                  d| j                  z         }| j                  | j                  j                         |j                                y N   r   r#   r"   r,   r$   r7   r8   s     r&   test_bytes_uncompressedz2TestVerifyingKeyFromString.test_bytes_uncompressedn   s>    %%g&>?**,blln=r(   c                     t        j                  t        d| j                  z               }| j	                  | j
                  j                         |j                                y rT   r;   r8   s     r&   test_bytearray_uncompressedz6TestVerifyingKeyFromString.test_bytearray_uncompresseds   sC    %%i$..0H&IJ**,blln=r(   c                     t        j                  d| j                  d d z         }| j                  | j                  j                         |j                                y N      rV   r8   s     r&   test_bytes_compressedz0TestVerifyingKeyFromString.test_bytes_compressedx   sE    %%gs0C&CD**,blln=r(   c                     t        j                  t        d| j                  d d z               }| j	                  | j
                  j                         |j                                y r[   r;   r8   s     r&   test_bytearray_compressedz4TestVerifyingKeyFromString.test_bytearray_compressed}   sJ    %%i$..":M0M&NO**,blln=r(   c                     | j                  t              5  t        j                  dt               d d d        y # 1 sw Y   y xY w)Ns   AAA)assertRaisesr   r   r#   r   r1   s    r&   .test_ed25519_VerifyingKey_from_string_importedzITestVerifyingKeyFromString.test_ed25519_VerifyingKey_from_string_imported   s6    23 	6$$VW5	6 	6 	6s	   :AN)__name__
__module____qualname____doc__classmethodr'   r3   r9   r=   r?   rG   rK   rP   rR   rW   rY   r^   r`   rc    r(   r&   r    r    ,   s\    
 9 9

>
>
>
>>>>>
>
>
>
6r(   r    c                       e Zd ZdZed        Zd Zd Zd Zd Z	d Z
d Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z"d  Z#d! Z$d" Z%d# Z&d$ Z'y%)&TestVerifyingKeyFromDerza
    Verify that ecdsa.keys.VerifyingKey.from_der() can be used with
    bytes-like objects.
    c                 b   d}d}|| _         t        |      | _        t        | j                  t              sJ t        j                  |      | _        t        j                  |      | _	        d}t        j                  |      | _
        t        j                  t        j                        | _        y )N-----BEGIN EC PRIVATE KEY-----
MF8CAQEEGF7IQgvW75JSqULpiQQ8op9WH6Uldw6xxaAKBggqhkjOPQMBAaE0AzIA
BLiBd9CE7xf15FY5QIAoNg+fWbSk1yZOYtoGUdzkejWkxbRc9RWTQjqLVXucIJnz
bA==
-----END EC PRIVATE KEY-----
z-----BEGIN PUBLIC KEY-----
MEkwEwYHKoZIzj0CAQYIKoZIzj0DAQEDMgAEuIF30ITvF/XkVjlAgCg2D59ZtKTX
Jk5i2gZR3OR6NaTFtFz1FZNCOotVe5wgmfNs
-----END PUBLIC KEY-----
z-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE4H3iRbG4TSrsSRb/gusPQB/4YcN8
Poqzgjau4kfxBPyZimeRfuY/9g/wMmPuhGl4BUve51DsnKJFRr8psk0ieA==
-----END PUBLIC KEY-----
)key_pemr   r"   
isinstancebytesr   from_pemr$   r   skvk2generatecurvesk2)r%   prv_key_strkey_strs      r&   r'   z"TestVerifyingKeyFromDer.setUpClass   s    - 	) 	 g#--///&&w/$$[1) 	 ''0%%bhh/r(   c                     d}t        j                  |      }t        j                  dt              }| j	                  ||       y )N  -----BEGIN PUBLIC KEY-----
MIIBSzCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAA
AAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA////
///////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSd
NgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5
RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA
//////////+85vqtpxeehPO5ysL8YyVRAgEBA0IABIr1UkgYs5jmbFc7it1/YI2X
T//IlaEjMNZft1owjqpBYH2ErJHk4U5Pp4WvWq1xmHwIZlsH7Ig4KmefCfR6SmU=
-----END PUBLIC KEY-----sA   RHlW;`Oȕ#0_Z0A`}NOZq|f[8*g	zJeru   )r   rq   r#   r   r,   )r2   pub_key_strpkpk_exps       r&   &test_load_key_with_explicit_parametersz>TestVerifyingKeyFromDer.test_load_key_with_explicit_parameters   sJ    ' 	 "";/))$
 
 	V$r(   c                     d}| j                  t              5  t        j                  |dg       d d d        y # 1 sw Y   y xY w)Nrz   named_curvevalid_curve_encodings)rb   r	   r   rq   )r2   r|   s     r&   2test_load_key_with_explicit_with_explicit_disabledzJTestVerifyingKeyFromDer.test_load_key_with_explicit_with_explicit_disabled   sF    ' 	 }- 	!!M?	 	 		   :Ac                     | j                  t              5 }t        j                  | j                  dg       d d d        | j                  dt        j                               y # 1 sw Y   /xY w)Nraw)valid_encodingszenabled (raw) encodings)rb   r   r   from_derr"   assertInstr	exceptionr2   es     r&   "test_load_key_with_disabled_formatz:TestVerifyingKeyFromDer.test_load_key_with_disabled_format   s\    23 	Kq!!$..5'J	K 	/Q[[1AB	K 	Ks   #A''A0c                     t        j                  | j                  t        j                        }| j                  |j                  t        j                         y r5   )r   r   r"   hashlibsha256assertIsdefault_hashfuncr8   s     r&   test_custom_hashfuncz,TestVerifyingKeyFromDer.test_custom_hashfunc   s4    ""4>>7>>Bb))7>>:r(   c                     t        j                  | j                  t        j                        }| j                  |j                  t        j                         y r5   )r   rq   rn   r   r   r   r   r8   s     r&   "test_from_pem_with_custom_hashfuncz:TestVerifyingKeyFromDer.test_from_pem_with_custom_hashfunc   s4    ""4<<@b))7>>:r(   c                     t        j                  | j                        }| j                  | j                  j                         |j                                y r5   )r   r   r"   r,   r$   r7   r8   s     r&   r3   z"TestVerifyingKeyFromDer.test_bytes   s9    ""4>>2**,blln=r(   c                     t        j                  t        | j                              }| j	                  | j
                  j                         |j                                y r5   )r   r   r6   r"   r,   r$   r7   r8   s     r&   r9   z-TestVerifyingKeyFromDer.test_bytes_memoryview   s>    ""6$..#9:**,blln=r(   c                     t        j                  t        | j                              }| j	                  | j
                  j                         |j                                y r5   )r   r   r<   r"   r,   r$   r7   r8   s     r&   r=   z&TestVerifyingKeyFromDer.test_bytearray   s>    ""9T^^#<=**,blln=r(   c                     t        j                  t        t        | j                                    }| j                  | j                  j                         |j                                y r5   )r   r   r6   r<   r"   r,   r$   r7   r8   s     r&   r?   z2TestVerifyingKeyFromDer.test_bytesarray_memoryview   sC    ""6)DNN*C#DE**,blln=r(   c                     t        j                   d| j                        }t        j                  |      }| j	                  | j
                  j                         |j                                y rA   )rD   r"   r   r   r,   r$   r7   rE   s      r&   rG   z1TestVerifyingKeyFromDer.test_array_array_of_bytes   sI    kk#t~~.""3'**,blln=r(   c                     t        j                   d| j                        }t        j                  t	        |            }| j                  | j                  j                         |j                                y rA   )rD   r"   r   r   r6   r,   r$   r7   rE   s      r&   rK   z<TestVerifyingKeyFromDer.test_array_array_of_bytes_memoryview   sM    kk#t~~.""6#;/**,blln=r(   c                 p    | j                  | j                  | j                  j                         k(         y r5   )
assertTruer$   rr   get_verifying_keyr1   s    r&   test_equality_on_verifying_keysz7TestVerifyingKeyFromDer.test_equality_on_verifying_keys  s$    477#<#<#>>?r(   c                 T    | j                  | j                  | j                  k(         y r5   )assertFalser$   rs   r1   s    r&   !test_inequality_on_verifying_keysz9TestVerifyingKeyFromDer.test_inequality_on_verifying_keys	  s    DHH,-r(   c                 @    | j                  | j                  d k(         y r5   )r   r$   r1   s    r&   1test_inequality_on_verifying_keys_not_implementedzITestVerifyingKeyFromDer.test_inequality_on_verifying_keys_not_implemented  s    D)r(   c                 d    | j                  | j                  | j                  j                         y r5   )assertNotEqualr$   rv   verifying_keyr1   s    r&   *test_VerifyingKey_inequality_on_same_curvezBTestVerifyingKeyFromDer.test_VerifyingKey_inequality_on_same_curve  s     DGGTXX%;%;<r(   c                 P    | j                  | j                  | j                         y r5   )r   rr   rv   r1   s    r&   (test_SigningKey_inequality_on_same_curvez@TestVerifyingKeyFromDer.test_SigningKey_inequality_on_same_curve  s    DGGTXX.r(   c                 T    | j                  | j                  | j                  k(         y r5   )r   r$   rr   r1   s    r&   test_inequality_on_wrong_typesz6TestVerifyingKeyFromDer.test_inequality_on_wrong_types  s    DGG+,r(   c                 B   | j                   j                  j                  }t        |j	                         |j                         |j                               }t        j                  || j                   j                        }| j                  || j                   k(         y r5   )
r$   r-   r.   r   ru   r/   r0   r   from_public_pointr   )r2   pjr.   r$   s       r&   test_from_public_point_oldz2TestVerifyingKeyFromDer.test_from_public_point_old  sc    WW^^!!bhhj"$$&"$$&1++E477==Adgg&r(   c                     t        j                  t        j                  j	                         t              }t        |j                        }| j                  d|       y )NzVerifyingKey.from_string(bytearray(b'K\x0c\xfbZH\x8e\x8c\x8c\x07\xee\xda\xfb\xe1\x97\xcd\x90\x18\x02\x15h]\xfe\xbe\xcbB\xba\xe6r\x10\xae\xf1P'), Ed25519, None))r   r#   r   	generatorto_bytesreprr   r,   )r2   rr   strings      r&    test_ed25519_VerifyingKey_repr__z8TestVerifyingKeyFromDer.test_ed25519_VerifyingKey_repr__   sK    ##G$5$5$>$>$@'Jb&&'1 	
r(   c                     t         j                  }| j                  t              5 }t	        j
                  |t                d d d        | j                  dt        j                               y # 1 sw Y   /xY w)Nzincompatible with Edwards)	r   r   rb   
ValueErrorr   r   r   r   r   )r2   r.   r   s      r&   test_edwards_from_public_pointz6TestVerifyingKeyFromDer.test_edwards_from_public_point,  s\    !!z* 	;a**5':	; 	13q{{3CD	; 	;s   A//A8c                    t        j                  t        j                  j	                         t              }|j
                  }t        j                  |j                         t              }|j                          | j                  ||       y r5   )
r   r#   r   r   r   r   r   r7   
precomputer,   r2   rr   r$   rs   s       r&   &test_edwards_precompute_no_side_effectz>TestVerifyingKeyFromDer.test_edwards_precompute_no_side_effect3  s_    ##G$5$5$>$>$@'J&&r||~w?
S!r(   c           	         t        t        t        t        j                         t	        t        t        j                  j                               d      t	        dd            }| j                  t              5 }t        j                  |       d d d        | j                  dt        j                               y # 1 sw Y   /xY w)Nr       ztrailing junk after public key)r
   r   r   oidr   rp   r   r   rb   r	   r   r   r   r   r   r2   der_strr   s      r&   $test_parse_malfomed_eddsa_der_pubkeyz<TestVerifyingKeyFromDer.test_parse_malfomed_eddsa_der_pubkey;  s    !J45U7#4#4#=#=#?@!DWa(
 }- 	+!!'*	+ 	6AKK8HI	+ 	+s   9B==Cc                     | j                  t              5 }t        j                  ddt               d d d        | j                  dt        j                               y # 1 sw Y   /xY wNr(   zunsupported for Edwards)rb   r   r   from_public_key_recoveryr   r   r   r   r   s     r&   %test_edwards_from_public_key_recoveryz=TestVerifyingKeyFromDer.test_edwards_from_public_key_recoveryG  sW    z* 	Ea11#sGD	E 	/Q[[1AB	E 	E   A  A)c                     | j                  t              5 }t        j                  ddt               d d d        | j                  dt        j                               y # 1 sw Y   /xY wr   )rb   r   r   $from_public_key_recovery_with_digestr   r   r   r   r   s     r&   1test_edwards_from_public_key_recovery_with_digestzITestVerifyingKeyFromDer.test_edwards_from_public_key_recovery_with_digestM  sV    z* 	a==S'	
 	/Q[[1AB	 	r   c                    d}t        j                  |      }| j                  |j                  t               | j                  |j                  t               d}t        j                  |t              }| j                  ||       y )Nq-----BEGIN PUBLIC KEY-----
MCowBQYDK2VwAyEAIwBQ0NZkIiiO41WJfm5BV42u3kQm7lYnvIXmCy8qy2U=
-----END PUBLIC KEY-----
    # Pd"(U~nAWD&V'/*e)	r   rq   r+   ru   r   r   r   r#   r,   )r2   vk_pemr$   vk_strvk_2s        r&   test_load_ed25519_from_pemz2TestVerifyingKeyFromDer.test_load_ed25519_from_pemU  ss    ) 	 ""6*bhh.bhh(P 	
 ''8T"r(   c                     d}t        j                  |t              }d}| j                  ||j	                                y )Nr   sq   -----BEGIN PUBLIC KEY-----
MCowBQYDK2VwAyEAIwBQ0NZkIiiO41WJfm5BV42u3kQm7lYnvIXmCy8qy2U=
-----END PUBLIC KEY-----
)r   r#   r   r,   to_pemr2   r   r$   r   s       r&   test_export_ed255_to_pemz0TestVerifyingKeyFromDer.test_export_ed255_to_pemj  sC    P 	
 %%fg6* 	 	-r(   c                     d}t        j                  |t              }d}| j                  ||j	                                y )Nr   sQ   ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICMAUNDWZCIojuNViX5uQVeNrt5EJu5WJ7yF5gsvKstl
)r   r#   r   r,   to_ssh)r2   r   r$   vk_sshs       r&   test_export_ed255_to_sshz0TestVerifyingKeyFromDer.test_export_ed255_to_sshz  s<    P 	
 %%fg6f-r(   c                     t        j                  t              }|j                  }t	        j
                  |j                               }| j                  ||       y r5   )r   rt   r   r   r   rq   r   r,   r   s       r&   test_ed25519_export_importz2TestVerifyingKeyFromDer.test_ed25519_export_import  sC      )##BIIK0S!r(   c                 |    d}t        j                  |      }d}d}| j                  |j                  ||             y )Nr      data
s@   dGj3yEl PǏ`u/'ӖXF\P)խF<hMꍨyr   rq   r   verifyr2   r   r$   datasigs        r&   test_ed25519_sig_verifyz/TestVerifyingKeyFromDer.test_ed25519_sig_verify  sG    ) 	 ""6*P 	 			#t,-r(   c                     d}t        j                  |      }d}d}| j                  t              5  |j	                  ||       d d d        y # 1 sw Y   y xY w)Nr   r   s@   Gj3yEl PǏ`u/'ӖXF\P)խF<hMꍨy)r   rq   rb   r   r   r   s        r&   !test_ed25519_sig_verify_malformedz9TestVerifyingKeyFromDer.test_ed25519_sig_verify_malformed  sb    ) 	 ""6*P 	 01 	!IIc4 	! 	! 	!s   AAc                    d}t        j                  |      }| j                  |j                  t               | j                  |j                  t               d}t        j                  |t              }| j                  ||       y )N-----BEGIN PUBLIC KEY-----
MEMwBQYDK2VxAzoAeQtetSu7CMEzE+XWB10Bg47LCA0giNikOxHzdp+tZ/eK/En0
dTdYD2ll94g58MhSnBiBQB9A1MMA
-----END PUBLIC KEY-----
9   y^+3] ؤ;vgIu7Xie9R@@ )	r   rq   r+   ru   r   r   r   r#   r,   )r2   pem_strr$   r   rs   s        r&   test_ed448_from_pemz+TestVerifyingKeyFromDer.test_ed448_from_pem  sr    ) 	 ""7+bhh.bhh&4 	 &&vu5S!r(   c                     d}t        j                  |t              }d}| j                  ||j	                                y )Nr   s   -----BEGIN PUBLIC KEY-----
MEMwBQYDK2VxAzoAeQtetSu7CMEzE+XWB10Bg47LCA0giNikOxHzdp+tZ/eK/En0dTdYD2ll94g5
8MhSnBiBQB9A1MMA
-----END PUBLIC KEY-----
)r   r#   r   r,   r   r   s       r&   test_ed448_to_pemz)TestVerifyingKeyFromDer.test_ed448_to_pem  sB    4 	 %%fe4* 	 	-r(   c                     t        j                  t              }|j                  }t	        j
                  |j                               }| j                  ||       y r5   )r   rt   r   r   r   rq   r   r,   r   s       r&   test_ed448_export_importz0TestVerifyingKeyFromDer.test_ed448_export_import  sC      '##BIIK0S!r(   c                 |    d}t        j                  |      }d}d}| j                  |j                  ||             y )Nr   r   sr   h,p5"5Q3= To0WE>Q`jC_28k	 x{]~1?0/U!L-LbS0fK@Э
 r   )r2   r   r$   r   r   s        r&   test_ed448_sig_verifyz-TestVerifyingKeyFromDer.test_ed448_sig_verify  sF    ) 	 ""7+ 	 			#t,-r(   N)(rd   re   rf   rg   rh   r'   r   r   r   r   r   r3   r9   r=   r?   rG   rK   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   ri   r(   r&   rk   rk      s    
 0 0>%0"C;
;
>
>
>
>
>>@.*=/-'

E"
JCC#*. 
.".*!,"0.$".r(   rk   c                       e Zd ZdZed        Zd Zd Zd Zd Z	d Z
d Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)TestSigningKeyz_
    Verify that ecdsa.keys.SigningKey.from_der() can be used with
    bytes-like objects.
    c                     d}t        j                  |      | _        d}t        j                  |      | _        d}t        j                  |      | _        y )Nrm   z-----BEGIN PRIVATE KEY-----
MG8CAQAwEwYHKoZIzj0CAQYIKoZIzj0DAQEEVTBTAgEBBBheyEIL1u+SUqlC6YkE
PKKfVh+lJXcOscWhNAMyAAS4gXfQhO8X9eRWOUCAKDYPn1m0pNcmTmLaBlHc5Ho1
pMW0XPUVk0I6i1V7nCCZ82w=
-----END PRIVATE KEY-----
z-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIKlL2EAm5NPPZuXwxRf4nXMk0A80y6UUbiQ17be/qFhRoAoGCCqGSM49
AwEHoUQDQgAE4H3iRbG4TSrsSRb/gusPQB/4YcN8Poqzgjau4kfxBPyZimeRfuY/
9g/wMmPuhGl4BUve51DsnKJFRr8psk0ieA==
-----END EC PRIVATE KEY-----
)r   rq   sk1	sk1_pkcs8rv   )r%   rw   s     r&   r'   zTestSigningKey.setUpClass  s\    - 	 %%k2* 	 #++K8- 	 %%k2r(   c                 \    | j                  | j                  j                  d      d       y )Npkcs8formatsq   0o0*H=*H=U0S^BRB<V%wš42 wЄV9@(6Y&NbQz5Ŵ\B:U{ l)r,   r   to_derr1   s    r&   test_to_der_pkcs8z TestSigningKey.test_to_der_pkcs8)  s'    HHOO7O+$	
r(   c                     d}t        j                  |      }t        j                  dt              }| j	                  ||       y )N:  -----BEGIN PRIVATE KEY-----
MIIBeQIBADCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAAB
AAAAAAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA
///////////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMV
AMSdNgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg
9KE5RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8A
AAAA//////////+85vqtpxeehPO5ysL8YyVRAgEBBG0wawIBAQQgIXtREfUmR16r
ZbmvDGD2lAEFPZa2DLPyz0czSja58yChRANCAASK9VJIGLOY5mxXO4rdf2CNl0//
yJWhIzDWX7daMI6qQWB9hKyR5OFOT6eFr1qtcZh8CGZbB+yIOCpnnwn0ekpl
-----END PRIVATE KEY-----
s    !{Q&G^e`=G3J6 r{   )r   rq   r#   r   r,   )r2   rw   rr   rv   s       r&   'test_decoding_explicit_curve_parametersz6TestSigningKey.test_decoding_explicit_curve_parameters4  sJ    	* 	   -$$ 	
 	S!r(   c                     d}| j                  t              5  t        j                  |dg       d d d        y # 1 sw Y   y xY w)Nr   r   r   )rb   r	   r   rq   )r2   rw   s     r&   >test_decoding_explicit_curve_parameters_with_explicit_disabledzMTestSigningKey.test_decoding_explicit_curve_parameters_with_explicit_disabledM  sF    	* 	 }- 	M?	 	 	r   c                    t        j                  | j                  j                  j                  | j                  j
                        }| j                  | j                  |       | j                  | j                  |       y r5   )r   from_secret_exponentr   privkeysecret_multiplierru   r,   r   r2   rr   s     r&   test_equality_on_signing_keysz,TestSigningKey.test_equality_on_signing_keys`  sY    ,,HH..
 	2&,r(   c                     | j                   j                  d      }| j                  |       | j                   j                  }| j                  |j	                  |d             y )Nr(   )r   signr   r   r   r2   r   r$   s      r&   test_verify_with_empty_messagez-TestSigningKey.test_verify_with_empty_messageg  sF    hhmmC XX##		#s+,r(   c                     | j                   j                  d      }| j                   j                  }|j                          | j	                  |j                  |d             y )N   message)r   r  r   r   r   r   r  s      r&   test_verify_with_precomputez*TestSigningKey.test_verify_with_precomputep  sC    hhmmJ'XX##
		#z23r(   c                     | j                   j                  }|j                          | j                  j                  }| j	                  ||       y r5   )r   r   r   r   r,   )r2   vk1rs   s      r&   *test_compare_verifying_key_with_precomputez9TestSigningKey.test_compare_verifying_key_with_precomputey  s:    hh$$nn**c"r(   c                     | j                   j                  d      }| j                   j                  }|j                  d       | j	                  |j                  |d             y )Ns   other messageT)lazy)rv   r  r   r   r   r   r  s      r&    test_verify_with_lazy_precomputez/TestSigningKey.test_verify_with_lazy_precompute  sJ    hhmm,-XX##
4 		#'789r(   c                 P    | j                  | j                  | j                         y r5   )r   r   rv   r1   s    r&   test_inequality_on_signing_keysz.TestSigningKey.test_inequality_on_signing_keys  s    DHHdhh/r(   c                 <    | j                  | j                  d        y r5   )r   r   r1   s    r&   /test_inequality_on_signing_keys_not_implementedz>TestSigningKey.test_inequality_on_signing_keys_not_implemented  s    DHHd+r(   c                     d}t        j                  |      }t        j                  dt              }| j	                  ||       y )Nw-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEIDS6x9FO1PG8T4xIPg8Zd0z8uL6sVGZFEZrX17gHC/XU
-----END PRIVATE KEY-----
    4NOH>wLTfE׸)r   rq   r#   r   r,   r2   r   rr   sk_strs       r&   test_ed25519_from_pemz$TestSigningKey.test_ed25519_from_pem  sI    * 	   )''P
 	V$r(   c           	      j   t        t        d      t        t        t        j                   t        d            t        t        d                  }| j                  t              5 }t        j                  |       d d d        | j                  dt        j                               y # 1 sw Y   /xY w)Nr       AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAzNon NULL parametersr
   r   r   r   r   r   rb   r	   r   r   r   r   r   r   s      r&   'test_ed25519_from_der_bad_alg_id_paramsz6TestSigningKey.test_ed25519_from_der_bad_alg_id_params  s    !1J4nQ6GH 3I >?
 }- 	)(	) 	+S-=>	) 	)s   %B))B2c           	      \   t        t        d      t        t        t        j                         t        t        d      dz               }| j                  t              5 }t        j                  |       d d d        | j                  dt        j                               y # 1 sw Y   /xY w)Nr   r"     Bz+trailing junk after the encoded private keyr#  r   s      r&   )test_ed25519_from_der_junk_after_priv_keyz8TestSigningKey.test_ed25519_from_der_junk_after_priv_key  s    !1GKK(   3I > EF
 }- 	)(	) 	93q{{;K	
	) 	)s   B""B+c                     t        j                  dt              }d}|j                  |t              }| j                  |d       y )Nr  r  	sigencodes@   ,v>%~>&
a@|`X@<7%i{}ݱ|
})"+E )r   r#   r   r  r   r,   )r2   r  msgr   s       r&   test_ed25519_signz TestSigningKey.test_ed25519_sign  sK    ''P
 kk#k7D	
r(   c                     t        j                  dt              }| j                  t              5 }|j                  d       d d d        | j                  dt        j                               y # 1 sw Y   /xY wNr  s   aaaaaaaaaaaaaaaaaaaaMethod unsupported for Edwards)	r   r#   r   rb   r   sign_digest_deterministicr   r   r   r2   r  r   s      r&   &test_ed25519_sign_digest_deterministicz5TestSigningKey.test_ed25519_sign_digest_deterministic  sj    ''P

 z* 	8a,,Y7	8 	6AKK8HI	8 	8   A00A9c                     t        j                  dt              }| j                  t              5 }|j                  d       d d d        | j                  dt        j                               y # 1 sw Y   /xY wr.  )	r   r#   r   rb   r   sign_digestr   r   r   r1  s      r&   test_ed25519_sign_digestz'TestSigningKey.test_ed25519_sign_digest  sj    ''P

 z* 	*ay)	* 	6AKK8HI	* 	*r3  c                     t        j                  dt              }| j                  t              5 }|j                  d       d d d        | j                  dt        j                               y # 1 sw Y   /xY w)Nr     r/  )	r   r#   r   rb   r   sign_numberr   r   r   r1  s      r&   test_ed25519_sign_numberz'TestSigningKey.test_ed25519_sign_number  sj    ''P

 z* 	#ar"	# 	6AKK8HI	# 	#r3  c                     d}t        j                  |      }| j                  t              5 }|j	                  d       d d d        | j                  dt        j                               y # 1 sw Y   /xY w)Nr  ssleayr   zOnly PKCS#8 format)r   rq   rb   r   r   r   r   r   )r2   r   rr   r   s       r&   test_ed25519_to_der_ssleayz)TestSigningKey.test_ed25519_to_der_ssleay  sk    * 	   )z* 	'aIIXI&	' 	*C,<=	' 	's   A..A7c                     t        j                  dt              }d}| j                  |j	                  d      |       y )Nr  sw   -----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEIDS6x9FO1PG8T4xIPg8Zd0z8uL6sVGZFEZrX17gHC/XU
-----END PRIVATE KEY-----
r   r   )r   r#   r   r,   r   r2   rr   r   s      r&   test_ed25519_to_pemz"TestSigningKey.test_ed25519_to_pem  sB    ##P
+ 	 	'2G<r(   c                 |    t        j                  dt              }d}| j                  |j	                         |       y )Nr  s  -----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZWQyNTUx
OQAAACAjAFDQ1mQiKI7jVYl+bkFXja7eRCbuVie8heYLLyrLZQAAAIgAAAAAAAAAAAAAAAtzc2gt
ZWQyNTUxOQAAACAjAFDQ1mQiKI7jVYl+bkFXja7eRCbuVie8heYLLyrLZQAAAEA0usfRTtTxvE+M
SD4PGXdM/Li+rFRmRRGa19e4Bwv11CMAUNDWZCIojuNViX5uQVeNrt5EJu5WJ7yF5gsvKstlAAAA
AAECAwQF
-----END OPENSSH PRIVATE KEY-----
)r   r#   r   r,   r   )r2   rr   ssh_strs      r&   test_ed25519_to_sshz"TestSigningKey.test_ed25519_to_ssh  s=    ##P
3 	 	g.r(   c                     t        j                  t              }t        j                  |j	                  d            }| j                  ||       y Nr   r   )r   rt   r   rq   r   r,   r2   rr   decodeds      r&   test_ed25519_to_and_from_pemz+TestSigningKey.test_ed25519_to_and_from_pem   s=      )%%biiwi&?@W%r(   c                 x    t        j                  t        t        j                        }| j                  |       y )N)entropy)r   rt   r   osurandomr*   r  s     r&   test_ed25519_custom_entropyz*TestSigningKey.test_ed25519_custom_entropy'  s&      "**=R r(   c                     | j                  t              5 }t        j                  dt               d d d        | j                  dt        j                               y # 1 sw Y   /xY w)NiIr{   z don't support setting the secret)rb   r   r   r  r   r   r   r   r   s     r&   !test_ed25519_from_secret_exponentz0TestSigningKey.test_ed25519_from_secret_exponent,  sU    z* 	Ga++JgF	G 	8#akk:JK	G 	Gr   c                     d}t        j                  |      }t        j                  dt              }| j	                  ||       y )Nz-----BEGIN PRIVATE KEY-----
MEcCAQAwBQYDK2VxBDsEOTyFuXqFLXgJlV8uDqcOw9nG4IqzLiZ/i5NfBDoHPzmP
OP0JMYaLGlTzwovmvCDJ2zLaezu9NLz9aQ==
-----END PRIVATE KEY-----
9   <z-x	_..&_:?98	1T 2{;4i)r   rq   r#   r   r,   r  s       r&   r   z"TestSigningKey.test_ed448_from_pem2  sJ    * 	   )''4 
 	V$r(   c                     t        j                  dt              }d}| j                  |j	                  d      |       y )NrQ  s   -----BEGIN PRIVATE KEY-----
MEcCAQAwBQYDK2VxBDsEOTyFuXqFLXgJlV8uDqcOw9nG4IqzLiZ/i5NfBDoHPzmPOP0JMYaLGlTz
wovmvCDJ2zLaezu9NLz9aQ==
-----END PRIVATE KEY-----
r   r   )r   r#   r   r,   r   r?  s      r&   r   z TestSigningKey.test_ed448_to_pemE  sC    ##4 
+ 	 	'2G<r(   c                     t        j                  t              }t        j                  |j	                  d            }| j                  ||       y rE  )r   rt   r   rq   r   r,   rF  s      r&   test_ed448_encode_decodez'TestSigningKey.test_ed448_encode_decodeV  s=      '%%biiwi&?@"%r(   N) rd   re   rf   rg   rh   r'   r   r  r  r	  r  r  r  r  r  r  r   r$  r'  r,  r2  r6  r:  r=  r@  rC  rH  rM  rO  r   r   rT  ri   r(   r&   r   r     s    
 3 38	
"2&--4#:0,%"
?
 
$	J	J	J>=/&&!
L%&="&r(   r   c                   @    e Zd Zed        Zd Zd Zd Zd Zd Z	d Z
y)	TestTrivialCurvec                     t        ddd      }t        |ddddd	      }t        d
||d      | _        t	        j
                  d| j                  t        j                        | _        y )N   T   :      W   r      T)r   toy_p8)r   r[  r      )hashfunc)	r   r   r   	toy_curver   r  r   sha1rr   )r%   ru   gens      r&   r'   zTestTrivialCurve.setUpClass_  s[     R$%B3$?hsI>00MM\\
r(   c                 ~    | j                   j                  }| j                  ||j                         z  t               y r5   )ra  r   r,   orderr   )r2   rc  s     r&   test_generator_sanityz&TestTrivialCurve.test_generator_sanityq  s,    nn&&syy{*H5r(   c                 l    | j                  | j                  j                  j                         d       y )Ns   )r,   rr   r   r7   r1   s    r&   test_public_key_sanityz'TestTrivialCurve.test_public_key_sanityv  s%    ..88:KHr(   c                     | j                   j                  d      }| j                  |d       | j                  | j                   j                  j                  |d             y )Nr  s   -.rr   sign_deterministicr,   r   r   r   )r2   r   s     r&   test_deterministic_signz(TestTrivialCurve.test_deterministic_signy  sI    gg((4e$--44S*EFr(   c                    t        j                  d      }| j                  j                  |      }| j	                  t        |      d       | j                  | j                  j                  j                  ||             y )N    r[  )	rK  rL  rr   rk  r,   lenr   r   r   r2   r+  r   s      r&   &test_deterministic_sign_random_messagez7TestTrivialCurve.test_deterministic_sign_random_message  sZ    jjngg((-S1%--44S#>?r(   c                     d}| j                   j                  |      }| j                  |d       | j                  | j                   j                  j                  ||             y )Ns    Os   6rj  rp  s      r&   /test_deterministic_sign_that_rises_R_zero_errorz@TestTrivialCurve.test_deterministic_sign_that_rises_R_zero_error  sP     gg((-k*--44S#>?r(   c                     d}| j                   j                  |      }| j                  |d       | j                  | j                   j                  j                  ||             y )Ns   ms   Ilrj  rp  s      r&   /test_deterministic_sign_that_rises_S_zero_errorz@TestTrivialCurve.test_deterministic_sign_that_rises_S_zero_error  sN    gg((-k*--44S#>?r(   N)rd   re   rf   rh   r'   rf  rh  rl  rq  rs  ru  ri   r(   r&   rV  rV  ^  s7    
 
"6
IG@@@r(   rV  rm   sd   some string for signingcontents don't really matterbut do include also some crazy values:  	
      r)  rp   c                     | S r5   ri   r/   s    r&   <lambda>ry         r(   zbytes memoryviewr<   zbytearray memoryviewc                 *    t        t        |             S r5   r6   r<   rx  s    r&   ry  ry        vil'; r(   zarray.array of bytesc                 .    t        j                   d|       S rA   rD   rx  s    r&   ry  ry        u{{3': r(   zarray.array of bytes memoryviewc                 @    t        t        j                  d|             S rA   r6   rD   rx  s    r&   ry  ry        &S!9L2M r(   zarray.array of intsc                 .    t        j                   d|       S rN   r  rx  s    r&   ry  ry        ekk#q&9 r(   zarray.array of ints memoryviewc                 @    t        t        j                  d|             S rN   r  rx  s    r&   ry  ry        C8K1L r(   intsc                     | S r5   ri   rx  s    r&   ry  ry    s     r(   r   c                     t        |       S r5   convrx  s    r&   ry  ry    s
    T!W r(   derc                     t        |       S r5   r  rx  s    r&   ry  ry    s
    $q' r(   stringsc                 &    t        d | D              S )Nc              3   2   K   | ]  }t        |        y wr5   r  ).0is     r&   	<genexpr>z<lambda>.<locals>.<genexpr>  s     /DG/s   )tuplerx  s    r&   ry  ry    s    e/Q// r(   r   verify_digestz{2}-{0}-{1})idz1signature,decoder,mod_apply,fun,vrf_mthd,vrf_datac                 :     ||       } || ||      |      sJ y )N)	sigdecoderi   )	signaturedecoder	mod_applyfunvrf_mthdvrf_datar   s          r&   test_VerifyingKey_verifyr    s$     I
CCX':::r(   s   ^BRB<V%wr]   c                     | S r5   ri   rx  s    r&   ry  ry    rz  r(   c                 *    t        t        |             S r5   r|  rx  s    r&   ry  ry    r}  r(   c                 .    t        j                   d|       S rA   r  rx  s    r&   ry  ry    r  r(   c                 @    t        t        j                  d|             S rA   r  rx  s    r&   ry  ry    r  r(   c                 .    t        j                   d|       S rN   r  rx  s    r&   ry  ry    r  r(   c                 @    t        t        j                  d|             S rN   r  rx  s    r&   ry  ry    r  r(   convertc                 x     | t               }t        j                  |      }|j                         t         k(  sJ y r5   )prv_key_bytesr   r#   r7   r  keyrr   s      r&   test_SigningKey_from_stringr    s0    
-
 C				$B<<>]***r(   c                 x     | t               }t        j                  |      }|j                         t        k(  sJ y r5   )r"   r   r   r7   r  r  s      r&   test_SigningKey_from_derr    s0    
)
C			S	!B<<>]***r(   s   
c                     t         j                   | t               | t                    }t        j                  |t               y N)extra_entropy)rr   rk  r   r  r$   r   r  r   s     r&   "test_SigningKey_sign_deterministicr    s5    


W]%;   C IIc4r(   c                     t         j                   | t               | t                    }t        j                  |t               y r  )rr   r0  	data_hashr  r$   r   r   r  s     r&   )test_SigningKey_sign_digest_deterministicr  (  s6    

&
&	'-*@ ' C IIc4r(   c                 v    t         j                   | t                    }t        j	                  |t               y r5   )rr   r  r   r$   r   r  s     r&   test_SigningKey_signr  1  s"    
'''$-
 CIIc4r(   c                 v    t         j                   | t                    }t        j	                  |t
               y r5   )rr   r5  r  r$   r   r   r  s     r&   test_SigningKey_sign_digestr  8  s#    
..+
,CIIc4r(   c                      t        j                  t        j                  dz
  t              } | j                  }| j                  d      }|j                  |d      sJ y )Nr   r{   s   hello)r   r  r   re  r   r  r   )rr   r$   r   s      r&   #test_SigningKey_with_unlikely_valuer  ?  sJ    		(	(!);8	LB			B
''(
C99S(###r(   c                  j   t         } t        | j                         | j                         | j	                         | j                               } t        d| j                         | d      }t        j                  d|      }t        j                  dt              }|j                  |j                  k(  sJ y )Nr   )
r      $   r  r  r[     r   r   r      )r   r   ru   r/   r0   re  r   r   r  r   r  )r   ru   rr   rv   s       r&   +test_SigningKey_with_custom_curve_old_pointr  F  s    )I	I '	E 
	(	(U	3B

)
)"o
>C::$$$r(   c                      t        j                  dt              } t        j                  dt              }| j                  |j                  k(  rJ y )Nr[  )r   r  r   r   r   r   rv   s     r&   2test_VerifyingKey_inequality_with_different_curvesr  ]  sC    

)
)!_
=C

)
)!X
6C!!S%6%66776r(   c                      t        j                  dt              } t        j                  dt              }| j                  |j                  k(  rJ y )Nr[  r  )r   r  r   r   r  s     r&   9test_VerifyingKey_inequality_with_different_secret_pointsr  d  sC    

)
)!_
=C

)
)!_
=C!!S%6%66776r(   c                  \    d} t        j                  |       }|j                  t        k(  sJ y )Nz-----BEGIN PRIVATE KEY-----
    MFMCAQEwBQYDK2VwBCIEICc2F2ag1n1QP0jY+g9qWx5sDkx0s/HdNi3cSRHw+zsI
    oSMDIQA+HQ2xCif8a/LMWR2m5HaCm5I2pKe/cc8OiRANMHxjKQ==
    -----END PRIVATE KEY-----)r   rq   ru   r   )pemrr   s     r&   &test_SigningKey_from_pem_pkcs8v2_EdDSAr  k  s-    !C
 
		S	!B88wr(   )e	unittest2unittestImportErrorr6   	NameError
memoryviewrK  rD   pytestr   keysr   r   r   r   r  r   r	   r
   r   r   r   r   utilr   r   r   r   r   r   curvesr   r   r   r   r   ellipticcurver   r   r   r   ecdsar   TestCaser    rk   r   rV  rw   r"   ro   rp   r   rr   r   r$   r   ro  rb  updatedigestr  r  sig_rawsig_dersig_strings	verifiersr<   modifierr  r  
sig_formatr  r  r  r   r  method_namer  r  appendparamr   markparametrizer  r  
convertersr  r  r  r  r  r  r  r  r  r  r  r  r  ri   r(   r&   <module>r     s   
 
        E D @ @ ,X6!2!2 X6v|.h// |.~U&X&& U&p
4@x(( 4@r%  +	)U# ##Z#*  4y1} w||~ D KKM	)U# ##
''$"2'
3'5! !!
''$-'
0'5! !!ggd&7g8+a.%( ((	k );<:;&(MN9:%'LM	 &MHc 	)+<=	(9:/		
	6 1
Iw	 ryy$'b..	:0
 	+K8 $++Hj+N
		&R 7;;#  =R 
k );<:;&(MN9:%'LM	 
:Hg lfll7x89
: J/+ 0+%  +	)U# ## JsO4+ 5+ 4 J/ 0 J/ 0 J/ 0 J/ 0$%.88Q#  
  Fs"   O  O  	OOOO