
    #	h#                        S SK Jr  S SKrS SKJr  S SKJrJrJrJ	r	  S SK
JrJrJrJrJr  S SKJr   " S S\R$                  5      r " S	 S
\R$                  5      r " S S5      r " S S\5      r " S S\5      rg)    )annotationsN)utils)AlreadyFinalized
InvalidKeyUnsupportedAlgorithm_Reasons)cipherscmacconstant_timehasheshmac)KeyDerivationFunctionc                      \ rS rSrSrSrg)Mode   ctr N)__name__
__module____qualname____firstlineno__CounterMode__static_attributes__r       Z/var/www/html/env/lib/python3.13/site-packages/cryptography/hazmat/primitives/kdf/kbkdf.pyr   r      s    Kr   r   c                       \ rS rSrSrSrSrSrg)CounterLocation   before_fixedafter_fixedmiddle_fixedr   N)r   r   r   r   BeforeFixed
AfterFixedMiddleFixedr   r   r   r   r   r      s     KJ Kr   r   c                  n    \ rS rSr                    SS jr\S	S j5       rS
S jrSS jrSr	g)_KBKDFDeriver$   c                   [        U5      (       d   e[        U[        5      (       d  [        S5      e[        U[        5      (       d  [        S5      eUc  U[        R
                  L a  [        S5      eUb  U[        R
                  :w  a  [        S5      eUb   [        U[        5      (       d  [        S5      eUb  US:  a  [        S5      eU(       d  U	(       a  U
(       a  [        S5      eUb  U R                  U5      (       d  [        S	5      eUc  U
c  [        S
5      eUb   [        U[        5      (       d  [        S5      eUS:X  a  [        S5      eUc  SnU	c  Sn	[        R                  " SU5        [        R                  " SU	5        Xl        X l        X0l        X@l        XPl        X`l        Xpl        Xl        Xl        SU l        Xl        g )Nzmode must be of type Modez(location must be of type CounterLocationzPlease specify a break_locationzJbreak_location is ignored when location is not CounterLocation.MiddleFixedz!break_location must be an integerr   z)break_location must be a positive integerz9When supplying fixed data, label and context are ignored.zrlen must be between 1 and 4zPlease specify an llenzllen must be an integerzllen must be non-zeror   labelcontextF)callable
isinstancer   	TypeErrorr   r$   
ValueErrorint_valid_byte_lengthr   _check_bytes_prf_mode_length_rlen_llen	_location_break_location_label_context_used_fixed_data)selfprfmodelengthrlenllenlocationbreak_locationr)   r*   fixeds              r   __init___KBKDFDeriver.__init__%   s    }}}$%%788(O44FGG!h/2M2M&M>?? &O777/ 
 %j.M.M?@@%.1*<HIIW%K  <t66t<<;<<<EM566JtS$9$956619455=E?G7E*9g.	


!-
 r   c                    [        U [        5      (       d  [        S5      e[        R                  " SU 5      nS[        U5      s=::  a  S::  d   g  gg)Nzvalue must be of type int      FT)r,   r/   r-   r   int_to_byteslen)value	value_bins     r   r0    _KBKDFDeriver._valid_byte_lengthq   sL    %%%788&&q%0	C	N'a' (r   c                   U R                   (       a  [        e[        R                  " SU5        SU l         U R                  * U-  * nS/n[        R
                  " SU R                  5      nU[        S[        U5      S-  5      S-
  :  a  [        S5      eU R                  5       nU R                  [        R                  :X  a  SnUnOU R                  [        R                  :X  a  UnSnO`[        U R                   ["        5      (       a$  U R                   [        U5      :  a  [        S5      eUS U R                    nX`R                   S  n[%        SUS-   5       Hk  n	U R'                  U5      n
[        R
                  " XR                  5      nX{-   U-   nU
R)                  U5        UR+                  U
R-                  5       5        Mm     SR/                  U5      S U R                   $ )	Nkey_materialTr   rI         zThere are too many iterations.z"break_location offset > len(fixed))r;   r   r   _check_bytesliker4   rK   r5   powrL   r.   _generate_fixed_inputr7   r   r"   r#   r,   r8   r/   ranger2   updateappendfinalizejoin)r=   rQ   prf_output_sizeroundsoutputr_binrE   data_before_ctrdata_after_ctrihcounter
input_datas                r   derive_KBKDFDeriver.derive{   s   ::""~|<
 LL=O34 ""1djj1C3u:>*Q..=>>**,>>_888!O"N^^999#O N$$c &&U3 !EFF#$:d&:&:;O"#7#7#9:Nq&1*%A		,'A((JJ7G(2^CJHHZ MM!**,' & xx$,,//r   c                (   U R                   (       a+  [        U R                   [        5      (       a  U R                   $ [        R                  " U R
                  S-  U R                  5      nSR                  U R                  SU R                  U/5      $ )NrS   r       )
r<   r,   bytesr   rK   r4   r6   r[   r9   r:   )r=   l_vals     r   rV   #_KBKDFDeriver._generate_fixed_input   sh    
4+;+;U C C###""4<<!#3TZZ@xxgt}}eDEEr   )r8   r:   r<   r9   r4   r6   r7   r3   r2   r5   r;   N)r>   ztyping.Callabler?   r   r@   r/   rA   r/   rB   
int | NonerC   r   rD   rm   r)   bytes | Noner*   rn   rE   rn   )rM   r/   returnbool)rQ   rj   r\   r/   ro   rj   )ro   rj   )
r   r   r   r   rF   staticmethodr0   rf   rV   r   r   r   r   r&   r&   $   s    J!J! J! 	J!
 J! J! "J! #J! J! J! J!X  .0`Fr   r&   c                  t    \ rS rSr S	SS.                     S
S jjjrSS jrSS jrSS jrSrg)	KBKDFHMAC   NrD   c               4   [        U[        R                  5      (       d  [        S[        R
                  5      eSSKJn  UR                  U5      (       d  [        S[        R
                  5      eXl	        [        U R                  UUUUUUUUU	5
      U l        g )Nz5Algorithm supplied is not a supported hash algorithm.r   backendz5Algorithm supplied is not a supported hmac algorithm.)r,   r   HashAlgorithmr   r   UNSUPPORTED_HASH,cryptography.hazmat.backends.openssl.backendrx   hmac_supported
_algorithmr&   r2   _deriver)r=   	algorithmr?   r@   rA   rB   rC   r)   r*   rE   rx   rD   ossls                r   rF   KBKDFHMAC.__init__   s     )V%9%9::&G)) 
	
 ""9--&G)) 
 $%II
r   c                B    [         R                  " XR                  5      $ N)r   HMACr}   r=   rQ   s     r   r2   KBKDFHMAC._prf   s    yy77r   c                `    U R                   R                  XR                  R                  5      $ r   )r~   rf   r}   digest_sizer   s     r   rf   KBKDFHMAC.derive   s!    }}##L//2M2MNNr   c                f    [         R                  " U R                  U5      U5      (       d  [        eg r   r   bytes_eqrf   r   r=   rQ   expected_keys      r   verifyKBKDFHMAC.verify   *    %%dkk,&?NN Or   )r}   r~   r   )r   zhashes.HashAlgorithmr?   r   r@   r/   rA   r/   rB   rm   rC   r   r)   rn   r*   rn   rE   rn   rx   
typing.AnyrD   rm   )rQ   rj   ro   z	hmac.HMACrQ   rj   ro   rj   rQ   rj   r   rj   ro   None	r   r   r   r   rF   r2   rf   r   r   r   r   r   rs   rs      s     #,
 &*,
',
 ,
 	,

 ,
 ,
 ",
 ,
 ,
 ,
 ,
 #,
\8Or   rs   c                  p    \ rS rSr S	SS.                   S
S jjjrSS jrSS jrSS jrSrg)	KBKDFCMAC   Nru   c                  [        U[        R                  5      (       a  [        U[        R                  5      (       d  [	        S[
        R                  5      eXl        S U l        [        U R                  UUUUUUUUU	5
      U l        g )N7Algorithm supplied is not a supported cipher algorithm.)
issubclassr	   BlockCipherAlgorithmCipherAlgorithmr   r   UNSUPPORTED_CIPHERr}   _cipherr&   r2   r~   )r=   r   r?   r@   rA   rB   rC   r)   r*   rE   rx   rD   s               r   rF   KBKDFCMAC.__init__   s     w33
 
Iw'>'>??&I++ 
 $<@%II
r   c                `    U R                   c   e[        R                  " U R                   5      $ r   )r   r
   CMAC)r=   _s     r   r2   KBKDFCMAC._prf  s%    ||'''yy&&r   c                0   U R                  U5      U l        U R                  c   eSSKJn  UR	                  U R                  5      (       d  [        S[        R                  5      eU R                  R                  XR                  R                  S-  5      $ )Nr   rw   r   rS   )r}   r   r{   rx   cmac_algorithm_supportedr   r   r   r~   rf   
block_size)r=   rQ   r   s      r   rf   KBKDFCMAC.derive  s}    |4||'''	
 ,,T\\::&I++ 
 }}##L,,2I2IQ2NOOr   c                f    [         R                  " U R                  U5      U5      (       d  [        eg r   r   r   s      r   r   KBKDFCMAC.verify,  r   r   )r}   r   r~   r   )r?   r   r@   r/   rA   r/   rB   rm   rC   r   r)   rn   r*   rn   rE   rn   rx   r   rD   rm   )r   rj   ro   z	cmac.CMACr   r   r   r   r   r   r   r      s     #%
 &*%
 %
 	%

 %
 %
 "%
 %
 %
 %
 %
 #%
N'
P"r   r   )
__future__r   typingcryptographyr   cryptography.exceptionsr   r   r   r   cryptography.hazmat.primitivesr	   r
   r   r   r   "cryptography.hazmat.primitives.kdfr   Enumr   r   r&   rs   r   r   r   r   <module>r      sv   
 #     E5:: !ejj !MF MF`7% 7t@% @r   