+
      xiT  c                  s    R t ^ RIHt ^ RIt^ RIt^ RIHt ^ RIHtH	t	 ]'       d   ^ RI
Ht ^ RIHt R R ltRR	 R
 lltRR R lltRR R lltRR R llt ! R R4      t ! R R4      tR# )zModule that handles encrypted PSKC values.

This module defines an Encryption class that handles the encryption key,
algorithms and decryption.

The encryption key can be derived using the KeyDerivation class.
)annotationsN)Sequence)TYPE_CHECKINGType)BlockCipherAlgorithm)PSKCc                    V ^8  d   QhRRRR/# )   	algorithm
str | NonereturnSequence[int] Zformat   "4/usr/lib/python3.14/site-packages/pskc/encryption.py__annotate__r   *   s     G GZ GM G    c                s   ^ RI Hp V f	   V! R4      hV P                  R4      '       g/   V P                  R4      '       g   V P                  R4      '       d   \        V RR 4      ^,          .# V P                  R4      '       g   V P                  R4      '       d   ^^.# V P                  R	4      '       g/   V P                  R
4      '       g   V P                  R4      '       d   \        V RR 4      ^,          .# V P                  R4      '       g/   V P                  R4      '       g   V P                  R4      '       d   \        V RR 4      ^,          .# V P                  R4      '       g/   V P                  R4      '       g   V P                  R4      '       d   \        V RR 4      ^,          .# V! RV ,          4      h)z=Return the possible key lengths for the configured algorithm.DecryptionErrorNNo algorithm specified#aes128-cbc#aes192-cbc#aes256-cbc#tripledes-cbc#kw-tripledes
#kw-aes128
#kw-aes192
#kw-aes256#camellia128-cbc#camellia192-cbc#camellia256-cbc#kw-camellia128#kw-camellia192#kw-camellia256Unsupported algorithm: %riii)pskc.exceptionsr   endswithint)r   r      & r   algorithm_key_lengthsr(   *   s   /677			M	*	*}--}--Ib$%*++			,	-	-//Bx			L	)	)|,,|,,IbcN#q())


/
0
0122122Ib$%*++


.
/
/011011IbcN#q())9IEFFr   c          
     ,    V ^8  d   QhRRRRRRRRRR/# )	r   r   Type[BlockCipherAlgorithm]keybytes
ciphertextivbytes | Noner
   r   r   r   r   r   r   F   s:     1 1)1	1 1 		1
 1r   c                s   ^ RI Hp ^ RIHp ^ RIHpHp ^ RIHp V'       g,   VRV P                  ^,           pW P                  ^,          R pV! V ! V4      VP                  V4      V! 4       R7      p	V	P                  4       p
VP                  V P                  4      P                  4       p VP                  V
P                  V4      V
P                  4       ,           4      VP                  4       ,           #   \          d
    T! R4      hi ; i)6Decrypt the ciphertext and return the plaintext value.default_backendpaddingCiphermodesr   NZbackendzInvalid padding)cryptography.hazmat.backendsr2   cryptography.hazmat.primitivesr4   &cryptography.hazmat.primitives.ciphersr6   r7   r$   r   
block_sizeCBC	decryptorPKCS7unpadderupdatefinalize
ValueError)r   r+   r-   r.   r2   r4   r6   r7   r   cipherr>   r@   s   &&&&        r   _decrypt_cbcrE   F   s     =6D/2--23 4 4 9 :;
#		"/@BF  "I}}Y112;;=H1Z( !"$,$5$5$78 	8  1/001s   -A	C7 7Dc          
     ,    V ^8  d   QhRRRRRRRRRR/# )	r   r   r	   r+   r/   r-   r,   r.   r
   r   r   r   r   r   r   `   s0     " "z " "% "\ "ej "r   c                
   ^ RI Hp ^ RIHp Vf	   V! R4      hV f	   V! R4      h\	        V4      \        V 4      9  d	   V! R4      hV P                  R4      '       g/   V P                  R4      '       g   V P                  R4      '       d   \        VP                  WV4      # V P                  R	4      '       d   \        VP                  WV4      # V P                  R
4      '       g/   V P                  R4      '       g   V P                  R4      '       d   ^ RI
Hp V! W!4      # V P                  R4      '       d   ^ RIHp V! W!4      # V P                  R4      '       g/   V P                  R4      '       g   V P                  R4      '       d   \        VP                  WV4      # V P                  R4      '       g/   V P                  R4      '       g   V P                  R4      '       d   ^ RI
Hp V! W!VP                  R7      # Q h)r0   
algorithmsr   No key availabler   Invalid key lengthr   r   r   r   r   r   r   )unwrapr   r   r   r   r    r!   r"   r   )r;   rI   r$   r   lenr(   r%   rE   AES	TripleDESpskc.crypto.aeskwrL   pskc.crypto.tripledeskwCamellia)r   r+   r-   r.   rI   r   Zeaskw_unwrapZtripledeskw_unwrap   &&&&    r   decryptrU   `   s   A/
{011677
3x,Y77233-((}--}--JNNCR@@			,	-	-J00#2FF			L	)	)|,,|,,<J,,			O	,	,H!*22


/
0
0122122J//"EE


.
/
/011011<Jz7J7JKK5r   c          
     r)   )	r   r   r*   r+   r,   	plaintextr.   r/   r
   r   r   r   r   r   r      s2      6 U u Zf rw r   c                s   ^ RI Hp ^ RIHp ^ RIHpHp T;'       g(    \        P                  ! V P                  ^,          4      pV! V ! V4      VP                  V4      V! 4       R7      pVP                  4       p	VP                  V P                  4      P                  4       p
W9P                  V
P                  V4      V
P                  4       ,           4      ,           V	P                  4       ,           # )<Encrypt the provided value with the key using the algorithm.r1   r3   r5   r8   )r9   r2   r:   r4   r;   r6   r7   osurandomr<   r=   	encryptorr?   padderrA   rB   )r   r+   rV   r.   r2   r4   r6   r7   rD   rZ   r[   s   &&&&       r   _encrypt_cbcr\      s    <6D		4	4rzz)..!34B#		"/@BF  "I]]9//0779F
MM)$v'88: 	:	r   c          
     rF   )	r   r   r	   r+   r/   rV   r,   r.   r
   r   r   r   r   r   r      s0     " "z " " "L "di "r   c                rG   )rW   rH   )EncryptionErrorrJ   r   rK   r   r   r   r   r   r   r   )wrapr   r   r   r   r    r!   r"   rM   )r;   rI   r$   r]   rN   r(   r%   r\   rO   rP   rQ   r^   rR   rS   )r   r+   rV   r.   rI   r]   Z
aeskw_wrapZtripledeskw_wraprT   r   encryptr_      s   A/
{011677
3x,Y77233-((}--}--JNNCB??			,	-	-J00#"EE			L	)	)|,,|,,8)))			O	,	,D	//


/
0
0122122J//DD


.
/
/0110118)J4G4GHH5r   c                  s    ] tR t^tRtR R lt]R R l4       t]P                  R R l4       t]R R	 l4       t	]	P                  R
 R l4       t	R R lt
R R ltRR R lltRtR# )KeyDerivationa9  Handle key derivation.

The algorithm property contains the key derivation algorithm to use. For
PBDKF2 the following parameters are set:

  pbkdf2_salt: salt value
  pbkdf2_iterations: number of iterations to use
  pbkdf2_key_length: required key length in bytes
  pbkdf2_prf: name of pseudorandom function used
c                   V ^8  d   QhRR/# r   r
   Noner   r   r   r   r   KeyDerivation.__annotate__   s     , ,$ ,r   c                	sL    R V n         R V n        R V n        R V n        R V n        R # N)
_algorithmpbkdf2_saltpbkdf2_iterationspbkdf2_key_length_pbkdf2_prfself   &r   __init__ZKeyDerivation.__init__   s)    &*)--1-1'+r   c               ra   r   r
   r	   r   r   r   r   r   rd            : r   c                B    V P                   '       d   V P                   # R# )z*Provide the key derivation algorithm used.Nrf   rk   rm   r   r   KeyDerivation.algorithm        ?????"r   c               r   r   valuer	   r
   rc   r   r   r   r   r   rd           5 5z 5d 5r   c                	,    ^ RI Hp V! V4      V n        R# i    )normalise_algorithmNpskc.algorithmsrz   rf   rl   rv   rz      && r   r   rs          7-e4r   c               ra   ro   r   r   r   r   r   rd      s      J r   c                rq   )z.Provide the PBKDF2 pseudorandom function used.N)rj   rk   rm   r   
pbkdf2_prfKeyDerivation.pbkdf2_prf   s     ###r   c               r   ru   r   r   r   r   r   rd      s     6 6
 6t 6r   c                	rx   ry   )r|   rz   rj   r}   r~   r   r   r      s    7.u5r   c               r   r   passwordstr | bytes | bytearrayr
   r,   r   r   r   r   r   rd      s     K K&= K% Kr   c                sX   ^ RI Hp ^ RIHp RpV P                  '       dU   \
        P                  ! RV P                  4      pV'       d   VP                  R4      pMV! RV P                  ,          4      h\        WP                  V P                  V P                  34      '       g	   V! R4      h\        V\        R4      4      '       g   VP                  4       p V! WAV P                  V P                  V P                  4      #   \         d    T! R	T P                  ,          4      hi ; i)
z4Derive an encryption key from the provided password.)pbkdf2_hmacKeyDerivationErrorZsha1z!^(.*#)?hmac-(?P<hash>[a-z0-9-]+)$ZhashzUnsupported PRF: %rzIncomplete PBKDF2 configurationr   z%Pseudorandom function unsupported: %r)Zhashlibr   r$   r   r   reZsearchZgroupZallrg   ri   rh   Z
isinstanceZtypeZencoderC   )rl   r   r   r   prfZmatchs   &&    r   derive_pbkdf2ZKeyDerivation.derive_pbkdf2   s	   '6???II4dooGEkk&)()DOO;= =H..0F0F**, - -$%FGG(DI..(H	Kt//1G1G&&( (  	K$7$//IK K	Ks   (D %D)c               r   r   r   r   r   r   r   rd     s     	> 	>6 	>5 	>r   c                s    ^ RI Hp V P                  f	   V! R4      hV P                  P                  R4      '       d   V P	                  V4      # V! RV P                  ,          4      h)zDerive a key from the password.r   r   z#pbkdf2r#   )r$   r   r   r%   r   )rl   r   r   r~   r   deriveZKeyDerivation.derive  sZ    6>>!$%=>>>>""9--%%h//$+dnn<> >r   Nc               s4    V ^8  d   QhRRRRRRRRR	RR
RRR/# )r   r   r   saltr/   salt_lengthr&   
key_length
int | None
iterationsr   r	   r
   r,   r   r   r   r   r   rd     sP     , ,), , 	,
 , , , 
,r   c                s    RV n         Vf   \        P                  ! V4      pW n        V'       d   WPn        MV P                  f   RV n        V'       d   W@n        V'       d   W`n        V P                  V4      # )z+Configure PBKDF2 key derivation properties.Zpbkdf2i )r   rX   rY   rg   rh   ri   r   r   )rl   r   r   r   r   r   r   s   &&&&&&&r   setup_pbkdf2ZKeyDerivation.setup_pbkdf2  sc     "<::k*D%/"##+%+D"%/"!O!!(++r   )rf   rj   r   rh   ri   r   rg   )Ni   NNN)__name__
__module____qualname____firstlineno____doc__rn   propertyr   setterr   r   r   r   __static_attributes__r   r   r   r`   r`      s}    	,   5 5   6 6K6	>, ,r   r`   c                  s   ] tR tRtRtR R lt]R R l4       t]P                  R R l4       t]R	 R
 l4       t	]	P                  R R l4       t	]R R l4       t
R R ltRRRRRRRRRR/R R lltRRRRRRRRRRRRRR/R R lltRRRRRRRRRRRRRRR^RRR R/
R! R" llt]R# R$ l4       tR,R% R& lltR' R( ltR) R* ltR+tR# )-
Encryptioni%  a  Class for handling encryption keys that are used in the PSKC file.

Encryption generally uses a symmetric key that is used to encrypt some
of the information stored in PSKC files (typically the seed). This
class provides the following values:

  id: identifier of the key
  algorithm: the encryption algorithm used
  key_names: list of names for the key
  key_name: (first) name of the key (usually there is only one)
  key: the key value itself (binary form)
  iv: optional initialization vector for CBC based encryption
  fields: a list of Key fields that will be encrypted on writing

The key can either be assigned to the key property or derived using the
`derive_key()` method.
c               r   )r   pskcr   r
   rc   r   r   r   r   r   Encryption.__annotate__8  s     $ $T $d $r   c                	s    Wn         R V n        R V n        . V n        R V n        R V n        \        4       V n        . V n        R # re   )	r   idrf   	key_namesr+   r.   r`   
derivationfields)rl   r      &&r   rn   ZEncryption.__init__8  s:    	"&*$&!% $'/!#r   c               ra   ro   r   r   r   r   r   r   C  s      * r   c                sP    V P                   '       d   V P                   ^ ,          # R# )z$Provide the name of the (first) key.Nr   rk   rm   r   key_nameEncryption.key_nameB  s     >>>>>!$$r   c               r   ru   r   r   r   r   r   r   J  s        j  T  r   c                	s8    V'       d   V.V n         R # . V n         R # re   r   )rl   rv   r   r   r   r   I  s    #WDNDNr   c               ra   ro   r   r   r   r   r   r   Q  rp   r   c                rq   )z&Provide the encryption algorithm used.Nrr   rk   rm   r   r   Encryption.algorithmP  rt   r   c               r   ru   r   r   r   r   r   r   X  rw   r   c                	rx   ry   r{   r}   r~   r   r   r   W  r   r   c               ra   )r   r
   Zboolr   r   r   r   r   r   ]  s     	 	d 	r   c                s    ^ RI Hp  V P                  P                   F>  pVP                  VP
                  VP                  3 VP                  VP                  3 K@  	  R#   T d     R# i ; i)z5Test whether the PSKC file requires a decryption key.r   TF)	r$   r   r   keyssecretcountertime_offsettime_interval
time_drift)rl   r   r+   s   &  r   is_encryptedZEncryption.is_encrypted\  s_     	4	yy~~

CKK8!!3>>1 &
   		s   AA" "A-,A-c               r   )r   r   r   r
   rc   r   r   r   r   r   r   h  s     	4 	4#: 	4t 	4r   c                sF    V P                   P                  V4      V n        R# )a  Derive a key from the password.

The supplied password, together with the information embedded in the PSKC
file (generally algorithm, salt, etc.) is used to create a decryption key.

This function may raise a :exc:`~pskc.exceptions.KeyDerivationError`
exception if key derivation fails.
N)r   r   r+   )rl   r   r   r   
derive_keyZEncryption.derive_keyh  s     ??))(3r   r   Nr   r   r   r   c               s0    V ^8  d   QhRRRRRRRRRRRR	/# )
r   r   r	   r   r   r   list[str] | Noner   r
   rc   r   r   r   r   r   r   s  sH     " " " 	"
 " $" !" 
"r   c               	s^   Ve   Wn         Ve   W n        Ve   W0n        Ve   W@n        Ve   WPn        V P                  '       g   RV n        V P                  '       g	   R.V n        V P                  P                  R4      '       d'   V P                  P                  P                  4        R # R # )Nz
aes128-cbcr   z-cbc)	r   r   r   r   r   r%   r   macZsetup)rl   r   r   r   r   r   s   &$$$$$r   _setup_encryptionZEncryption._setup_encryptions  s     >G &N$M &N K~~~)DN{{{#*DK>>""6**IIMM! +r   r+   r   c               s8    V ^8  d   QhRRRRRRRRRRR	R
RR
RR/# )r   r+   r/   r   r	   r   r   r   r   r   r   r   r
   rc   r   r   r   r   r   r     s\     - - - 	-
 - - - $- !- 
-r   c               s    V P                  VVVVVR7       V'       g4   T;'       g    V P                  R,          p\        P                  ! V4      pWn        R# )a  Configure pre-shared key encryption when writing the file.

:param key: the encryption key to use
:param id: encryption key identifier
:param algorithm: encryption algorithm
:param key_length: encryption key length in bytes
:param key_name: a name for the key
:param key_names: a number of names for the key
:param fields: a list of fields to encrypt

This is a utility function to easily set up encryption. Encryption can
also be set up by manually by setting the correct
:class:`~pskc.encryption.Encryption` properties.

This method will generate a key if required and set the passed values.
By default AES128-CBC encryption will be configured and unless a key is
specified one of the correct length will be generated. If the algorithm
does not provide integrity checks (e.g. CBC-mode algorithms) integrity
checking in the PSKC file will be set up using
:func:`~pskc.mac.MAC.setup()`.

By default only the :attr:`~pskc.key.Key.secret` property will be
encrypted when writing the file.
r   r   r   r   r   N)r   r(   rX   rY   r+   )rl   r+   r   r   r   r   r   r   s   &$$$$$$$r   setup_preshared_keyZEncryption.setup_preshared_key  sW    F 	 	 	
 #EEt'A'A"'EJ**Z(Cr   r   r   r   r   c               sH    V ^8  d   QhRRRRRRRRRRR	R
RRRRRRRR
RRRR/# )r   r   r   r   r	   r   r   r   r   r   r   r   r   r/   r   r&   r   r   r
   rc   r   r   r   r   r   r     s     1
 1
)1
 	1

 1
 1
 $1
 1
 !1
 1
 1
 1
 1
 
1
r   c       
   	     s    V P                  VVVVVR7       V P                  P                  TTT	T;'       g    V P                  R,          V
VR7      V n        R# )a  Configure password-based PSKC encryption when writing the file.

:param password: the password to use (required)
:param id: encryption key identifier
:param algorithm: encryption algorithm
:param key_length: encryption key length in bytes
:param key_name: a name for the key
:param key_names: a number of names for the key
:param fields: a list of fields to encrypt
:param salt: PBKDF2 salt
:param salt_length: used when generating random salt
:param iterations: number of PBKDF2 iterations
:param prf: PBKDF2 pseudorandom function

Defaults for the above parameters are similar to those for
:func:`setup_preshared_key()` but the password parameter is required.

By default 12000 iterations will be used and a random salt with the
length of the to-be-generated encryption key will be used.
r   )r   r   r   r   r   Nr   )r   r   r   r(   r+   )rl   r   r   r   r   r   r   r   r   r   r   r   s   &&$$$$$$$$$$r   r   ZEncryption.setup_pbkdf2  si    F 	 	 	
 ??//#!CCT%?%?%C! 0 
r   c               ra   )r   r
   r   r   r   r   r   r   r     s     5 5} 5r   c                s,    \        V P                  4      # )z>Provide the possible key lengths for the configured algorithm.)r(   r   rk   rm   r   r(   Z Encryption.algorithm_key_lengths  s     %T^^44r   c               s$    V ^8  d   QhRRRRRR/# )r   cipher_valuer,   r   r	   r
   r   r   r   r   r   r     s'     J J% JJ JRW Jr   c                sl    \        T;'       g    V P                  V P                  WP                  4      # )z8Decrypt the cipher_value and return the plaintext value.)rU   r   r+   r.   )rl   r   r   s   &&&r   decrypt_valueZEncryption.decrypt_value  s,    ''<J 	Jr   c               s     V ^8  d   QhRRRR/# )r   rV   r,   r
   r   r   r   r   r   r     s      u  r   c                s    \        V P                  V P                  WP                  4      pV P                  '       d   V\	        V P                  4      R pV# )z7Encrypt the provided value and return the cipher_value.N)r_   r   r+   r.   rN   )rl   rV   r   r~   r   encrypt_valueZEncryption.encrypt_value  s?    t~~txxGGL777'DGG6Lr   c               ra   rb   r   r   r   r   r   r     s     * *4 *r   c                sT   V P                   P                   FX  pVP                  Vn        VP                  Vn        VP                  Vn        VP
                  Vn        VP                  Vn        KZ  	  RV P                   P                  n        RV P                   P                  n	        RV n
        RV n        . V n        RV n	        RV n        . V n        RV P                  n        RV P                  n        RV P                  n        RV P                  n        RV P                  n        R# )zDecrypt all values and remove the encryption from the PSKC file.

This can be used to read and encrypted PSKC file, decrypt the file,
remove the encryption and output an unencrypted PSKC file or to replace
the encryption algorithm.
N)r   r   r   r   r   r   r   r   r   r+   r   r   r.   r   r   rg   rh   ri   r   )rl   r+   r'   r   remove_encryptionZEncryption.remove_encryption  s     99>>CCJ++CK!ooCO # 1 1C ^^CN " #'		 		$(!&*#,0),0)%)"r   )
rf   r   r   r   r   r.   r+   r   r   r   re   )r   r   r   r   r   rn   r   r   r   r   r   r   r   r   r   r(   r   r   r   r   r   r   r   r   r   %  s   $$   __      5 5 	 		4" " !%	"
  $" '+" $(":- !- 	-
 !%- "&-  $- '+- $(-^1
 	1

 !%1
  $1
 '+1
 "&1
 $(1
 "1
 1
 "&1
 1
f 5 5J
* *r   r   re   )r   Z
__future__r    rX   r   Zcollections.abcr   Ztypingr   r   r;   r   r   r   r(   rE   rU   r\   r_   r`   r   r   r   r   <module>r      sY   * # 	 	 $ &KG814"J "Jh, h,V{* {*r   