a zec@sddlZddlZddlZddlZddlZddlZddlZddlmZddl m Z z(ddl m Zddl mZddl mZWneydZdZdZYn0ddZGd d d ejZGd d d ejZGd ddejZGdddejZGdddejZedkr edS)N)hashlib_helper)_compare_digest)HMAC)hmac_new)compare_digestcstfdd}|S)NcsHt,tjdtd|i|WdS1s:0YdS)Nignore)category)warningscatch_warningsfilterwarningsDeprecationWarning)argskwargsfuncB/opt/bitninja-python-dojo/embedded/lib/python3.9/test/test_hmac.pywrappers  zignore_warning..wrapper) functoolswraps)rrrrrignore_warningsrc@seZdZddZejdddddZejdddd d Zd d Zejd ddddZ ejdddddZ ejdddddZ ejdddddZ edddZ ddZdS)TestVectorsTestCasec Csxtj|||d}|||||t|||jd|||j |||j |tj|||d}|||||t|||jd|||j |||j |tj||d}| } | d| ||||tj |||d}|||||t|||jd|||j |||j |tj |d|d}| ||||tj ||d}| ||||tj |||d}||||tj|||dt||tj|||dt|tjdiL|tj|||dt||tj|||dt|Wdn1s0Ytdurtt|||d}|||||t|||jd|||j |||j |t||d}| } | d| ||||dS)N digestmodzhmac-s test update)digestzhmac._openssl_md_meths)hmacr assertEqual hexdigestupperrbinasciiZ unhexlifyname digest_size block_sizecopyupdatenewunittestZmockZpatch c_hmac_new) selfkeydatarhashfunchashnamer!r"hh2rrr asssert_hmac#sx    $    z TestVectorsTestCase.asssert_hmacmd5T)Zopensslcsnfdd}|ddd|ddd|d d d |ttd d dd|ddd|ddd|ddddS)Nc sj|||tjdddddS)Nr0@r+r,r!r")r/hashlibr0r)r*rr(rrmd5testpsz5TestVectorsTestCase.test_md5_vectors..md5test Hi ThereZ 9294727A3638BB1C13F48EF8158BFC9DJefewhat do ya want for nothing?Z 750c783e6ab0b503eaa86e310a5db738s2Z 56be34521d144c88dbb8c733f0e8b3f62Z 697eaf0aca3a3aea3a75164746ffaa79s Test With TruncationZ 56461ef2342edc00f9bab995690efd4cP6Test Using Larger Than Block-Size Key - Hash Key FirstZ 6b1ab7fe4bd7bf8f0b62e6ce61b9d0cdITest Using Larger Than Block-Size Key and Larger Than One Block-Size DataZ 6f630fad67cda0ee1fb1f562db3aa53ebytesrange)r(r7rr6rtest_md5_vectorsls: z$TestVectorsTestCase.test_md5_vectorssha1csnfdd}|ddd|ddd|d d d |ttd d dd|ddd|ddd|ddddS)Nc sj|||tjdddddS)NrHr2r3)r/r4rHr5r6rrshatestsz5TestVectorsTestCase.test_sha_vectors..shatest r9Z(b617318655057264e28bc0b6fb378c8ef146be00r:r;Z(effcdf6ae5eb2fa2d27416d5f184df9c259a7c79r<Z(125d7342b9ac11cd91a39af48aa17b4f63f175d3r=r>r?Z(4c9007f4026250c6bc8414f9bf50c86c2d7235das r@Z(4c1a03424b55e07fe7f27be1d58bb9324a9a5a04rArBZ(aa4ae5e15272d00e95705637ce8a3b55ed402112rCZ(e8e99d0f45237d786d6bbaa7965c7808bbff1a91rD)r(rJrr6rtest_sha_vectorss: z$TestVectorsTestCase.test_sha_vectorsc sfdd}|ddtjdtjdtjdtjdid |d d tjd tjd tjdtjdid |ddtjdtjdtjdtjdid |tddtddDdtjdtjdtjdtjdid |dd tjd!tjd"tjd#tjd$id |dd%tjd&tjd'tjd(tjd)id dS)*Nc s$|}j|||ddS)Nr3)r/)r)r* hexdigestsrr"r! hash_namer+r(rrhmactestsz9TestVectorsTestCase._rfc4231_test_cases..hmactestrKr9Z8896fb1128abbdf196832107cd49df33f47b4b1169912ba4f53684b22Z@b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7Z`afd03944d84895626b0825f4ab46907f15f9dadbe4101ec682aa034c7cebc59cfaea9ea9076ede7f4af152e8b2fa9cb6Z87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cdedaa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854)r)r*rNr:r;Z8a30e01098bc6dbbf45690f3a7e9e6d0f8bbea2a39e6148008fd05e44Z@5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843Z`af45d2e376484031617f78d2b58a6b1b9c7ef464f5a01b47e42ec3736322445e8e2240ca5e69e2c78b3239ecfab21649Z164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737rLr<Z87fb3cb3588c6c1f6ffa9694d7d6ad2649365b0c1f65d69d1ec8333eaZ@773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565feZ`88062608d3e6ad8a0aa2ace014c8a86f0aa635d947ac9febe83ef4e55966144b2a5ab39dc13814b94e3ab6e101a34f27Zfa73b0089d56a284efb0f0756c890be9b1b5dbdd8ee81a3655f83e33b2279d39bf3e848279a722c806b485a47e67c807b946a337bee8942674278859e13292fbcss|] }|VqdSNr).0xrrr z:TestVectorsTestCase._rfc4231_test_cases..r=r>r?Z86c11506874013cac6a2abc1bb382627cec6a90d86efc012de7afec5aZ@82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665bZ`3e8a69b7783c25851933ab6290af6ca77a9981480850009cc5577c6e1f573b4e6801dd23c4a7d679ccf8a386c674cffbZb0ba465637458c6990e5a8c5f61d4af7e576d97ff94b872de76f8050361ee3dba91ca5c11aa25eb4d679275cc5788063a5f19741120c4f2de2adebeb10a298ddsrBZ895e9a0db962095adaebe9b2d6f0dbce2d499f112f2d2b7273fa6870eZ@60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54Z`4ece084485813e9088d2c63a041bc5b44f9ef1012a2b588f3cd11f05033ac4c60c2ef6ab4030fe8296248df163f44952Z80b24263c7c1a3ebb71493c1dd7be8b49b46d1f41b4aeec1121b013783f8f3526b56d037e05f2598bd0fd2215d6a1e5295e64f73f63f0aec8b915a985d786598sThis is a test using a larger than block-size key and a larger than block-size data. The key needs to be hashed before being used by the HMAC algorithm.Z83a854166ac5d9f023f54d517d0b39dbd946770db9c2b95c9f6f565d1Z@9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2Z`6617178e941f020d351e2f254e8fd32c602420feb0b8fb9adccebb82461e99c5a678cc31e799176d3860e6110c46523eZe37b6a775dc87dbaa4dfa9f96e5e3ffddebd71f8867289865df5a32d20cdc944b6022cac3c4982b10d5eeb55c3e4de15134676fb6de0446065c97440fa8c6a58)r4sha224sha256sha384sha512rErF)r(r+rPr!r"rQrrOr_rfc4231_test_casessb z'TestVectorsTestCase._rfc4231_test_casesrWcCs|tjddddS)NrWr2)r[r4rWr6rrrtest_sha224_rfc42315sz'TestVectorsTestCase.test_sha224_rfc4231rXcCs|tjddddS)NrX r2)r[r4rXr6rrrtest_sha256_rfc42319sz'TestVectorsTestCase.test_sha256_rfc4231rYcCs|tjddddS)NrY0)r[r4rYr6rrrtest_sha384_rfc4231=sz'TestVectorsTestCase.test_sha384_rfc4231rZcCs|tjddddS)NrZr2ra)r[r4rZr6rrrtest_sha512_rfc4231Asz'TestVectorsTestCase.test_sha512_rfc4231c CsGdddt}ttdt|t*tjdd|d|dWdn1s`0Yd|_ |t*tjdd|d|d Wdn1s0YWdn1s0YdS) Nc@s(eZdZdZddZddZddZdS) zJTestVectorsTestCase.test_legacy_block_size_warnings..MockCrazyHashz#Ain't no block_size attribute here.cWstj||_|jj|_dSrR)r4rX_xr!)r(r rrr__init__Is zSTestVectorsTestCase.test_legacy_block_size_warnings..MockCrazyHash.__init__cSs|j|dSrR)rdr$)r(vrrrr$LszQTestVectorsTestCase.test_legacy_block_size_warnings..MockCrazyHash.updatecSs |jSrR)rdrr6rrrrNszQTestVectorsTestCase.test_legacy_block_size_warnings..MockCrazyHash.digestN)__name__ __module__ __qualname____doc__rer$rrrrr MockCrazyHashGsrkerrorabrz)Expected warning about missing block_sizer=z'Expected warning about small block_size) objectr r simplefilterRuntimeWarning assertRaisesrrfailr")r(rkrrrtest_legacy_block_size_warningsEs   ( z3TestVectorsTestCase.test_legacy_block_size_warningscCs|td(d}d}tj||ddWdn1s:0Y|tdt||Wdn1sr0Y|td tj||ddWdn1s0YdS)z5The digestmod parameter is required as of Python 3.8.zrequired.*digestmodr8r9Nr)msgr)assertRaisesRegex TypeErrorrrr%)r(r)r*rrrtest_with_digestmod_no_default\s.*z2TestVectorsTestCase.test_with_digestmod_no_defaultN)rgrhrir/rrequires_hashdigestrGrMr[r]r_rbrcrtryrrrrr!s"I  )  'w         rc@seZdZdZedddZedddZedddZedd d Z edd d Z edd dZ edddZ e edudddZdS)ConstructorTestCaseZ@6c845b47f52b3b47f6590c502db7825aad757bf4fadc8fa972f7cd2e76a5bdebrXcCs4ztjdddWnty.|dYn0dS)NkeyrXrz+Standard constructor call raised exception.)rr Exceptionrsr6rrr test_normalns zConstructorTestCase.test_normalcCs<|ttjddd}Wdn1s.0YdSNr)rXr)rrrxrrr(r-rrrtest_with_str_keyvs z%ConstructorTestCase.test_with_str_keycCs<|ttjddd}Wdn1s.0YdSr)rrrxrr%rrrrtest_dot_new_with_str_key}s z-ConstructorTestCase.test_dot_new_with_str_keycCsHztjdddd}Wnty0|dYn0|||jdS)Nr| hash this!rXrz5Constructor call with text argument raised exception.)rrr}rsrrexpectedrrrr test_withtexts  z!ConstructorTestCase.test_withtextcCsPztjtdtddd}Wnty8|dYn0|||jdS)Nr|rrXrz;Constructor call with bytearray arguments raised exception.)rr bytearrayr}rsrrrrrrrtest_with_bytearrays  z'ConstructorTestCase.test_with_bytearraycCsLztjdtddd}Wnty4|dYn0|||jdS)Nr|rrXrz6Constructor call with memoryview msg raised exception.)rr memoryviewr}rsrrrrrrrtest_with_memoryview_msgs  z,ConstructorTestCase.test_with_memoryview_msgcCs6ztddtj}Wnty0|dYn0dS)Nr|rVz6Constructor call with hashlib.sha256 raised exception.)rrr4rXr}rsrrrrtest_withmodules z#ConstructorTestCase.test_withmoduleNz need _hashlibcCs6|tdtWdn1s(0YdS)Nzcannot create 'HMAC' instance)rwrxC_HMACr6rrrtest_internal_typessz'ConstructorTestCase.test_internal_types)rgrhrirrrzr~rrrrrrr&Z skipUnlessrrrrrrr{hs$       r{c@seZdZedddZdS)SanityTestCaserXcCsVz4tjddd}|d|||WntyP|dYn0dS)Ns my secret keyrXrscompute the hash of this text!z3Exception raised during normal usage of HMAC class.)rrr$rrr#r}rsrrrrtest_exercise_all_methodss   z(SanityTestCase.test_exercise_all_methodsN)rgrhrirrzrrrrrrsrc@sfeZdZedddZedddZedddZeddd Zedd d Z d S) CopyTestCaserXcCsbtjddd}|}||j|jkd|t|jt|jd|t|jt|jddS)Nr|rXrz digest constructors don't match.zTypes of inner don't match.zTypes of outer don't match.) rrr# assertTrue _digest_consrtype_inner_outerr(h1r.rrrtest_attributesszCopyTestCase.test_attributescCstjddd}|}|t|t|kd|t|jt|jkd|t|jt|jkd||j|j||j|j ||j |j dS)Nr|rXr"No real copy of the HMAC instance.z&No real copy of the attribute 'inner'.z&No real copy of the attribute 'outer'.) rrr#ridrrrinnerouterr digest_consrrrr test_realcopyszCopyTestCase.test_realcopycCsBtjddd}||j|j||j|j||j|jdS)Nr|rXr) rrrrrrrrr)r(rrrrtest_propertiesszCopyTestCase.test_propertiescCsPtjddd}|d|}|||d|||ddS)Nr|rXrsome random text-Digest of copy doesn't match original digest.3Hexdigest of copy doesn't match original hexdigest.)rrr$r#rrrrrrr test_equalitys zCopyTestCase.test_equalitycCshtjddd}|d|}|t|t|kd|||d|||ddS)Nr|rXrrrrr) rr%r$r#rrrrrrrrrtest_equality_news zCopyTestCase.test_equality_newN) rgrhrirrzrrrrrrrrrrs    rc@s<eZdZddZddZeedudddZd d Z dS) CompareDigestTestCasecCs6|tjtdur$|tjtn|tjtdSrR)_test_compare_digestrropenssl_compare_digestZassertIsoperator_compare_digestr6rrrtest_hmac_compare_digests z.CompareDigestTestCase.test_hmac_compare_digestcCs|tdSrR)rrr6rrrtest_operator_compare_digestsz2CompareDigestTestCase.test_operator_compare_digestNztest requires _hashlibcCs|tdSrR)rrr6rrrtest_openssl_compare_digest sz1CompareDigestTestCase.test_openssl_compare_digestcCsd\}}|t|||d\}}|t|||d\}}|t|||d\}}|t|||d\}}|t|||d\}}||||d\}}||||d\}}||||d \}}||||d \}}||||d \}}||||td td }}||||td td }}||||td td}}||||td d }}||||||||td d }}||||||||td d}}||||||||d\}}||||d\}}||||d\}}||||d\}}|t|||d\}}|t|||d\}}|t|||d\}}|t|||d\}}|t|||Gdddt}|d|d}}|||||dd}}|||||d|d}}||||Gdddt}|d |d }}|||||d d }}|||||d |d}}||||dS)N)d)rfoobar)rr)foobarr)rr)rfoo)ޭsޭ)rfoobaz)rs)rr)rrrrr)rr)Zfoor)rfoobaz)rr=)fooärc@seZdZddZdS)z9CompareDigestTestCase._test_compare_digest..mystrcSsdSNFrr(otherrrr__eq__bsz@CompareDigestTestCase._test_compare_digest..mystr.__eq__Nrgrhrirrrrrmystrasrrrc@seZdZddZdS)z;CompareDigestTestCase._test_compare_digest..mybytescSsdSrrrrrrrmszBCompareDigestTestCase._test_compare_digest..mybytes.__eq__Nrrrrrmybyteslsr)rrrxZ assertFalserrstrrE)r(rabrrrrrrsz*CompareDigestTestCase._test_compare_digest) rgrhrirrr&ZskipIfrrrrrrrrs  r__main__)rrrr4r&Z unittest.mockr Z test.supportr _operatorrrZ_hashlibrrrr'rr ImportErrorrZTestCaserr{rrrrgmainrrrrs4       IG?z