
    %hC              	          S SK r S SKrS SKrS SKJr  S SKJrJrJrJ	r	J
r
Jr  SSKJr  SSKJr  SSKJrJrJrJrJrJrJr   " S S	5      r " S
 S\5      r S SKJrJr  S SKJr   " S S\5      r " S S\5      r S\!S\!S\!4S jr"S\!S\!S\!4S jr#S\!S\!S\!4S jr$S\!S\!S\!4S jr%S\!S\!S\!S\!4S jr&S\!S\!S\!S\!4S jr' " S$ S%5      r)\!" / S&Q5      r*S\!S\!4S' jr+ " S( S)5      r, " S* S+5      r- " S, S-\5      r. " S. S/5      r/g! \( a_     " S S\5      r " S S\5      r S\!S\!S\!4S jr"S\!S\!S\!4S jr#S\!S\!S\!4S  jr$S\!S\!S\!4S! jr%S\!S\!S\!S\!4S" jr&S\!S\!S\!S\!4S# jr' Nf = f)0    N)IntEnum)AnyDictOptionalTupleUnioncast   )logger_warningDependencyError)ArrayObjectByteStringObjectDictionaryObject	PdfObjectStreamObjectTextStringObjectcreate_string_objectc                   8    \ rS rSrS\S\4S jrS\S\4S jrSrg)	CryptBase/   datareturnc                     U$ N selfr   s     J/var/www/auris/envauris/lib/python3.13/site-packages/PyPDF2/_encryption.pyencryptCryptBase.encrypt0           c                     U$ r   r   r   s     r   decryptCryptBase.decrypt3   r"   r#   r   N)__name__
__module____qualname____firstlineno__bytesr    r%   __static_attributes__r   r#   r   r   r   /   s(    E e E e r#   r   c                       \ rS rSrSrg)CryptIdentity7   r   N)r'   r(   r)   r*   r,   r   r#   r   r.   r.   7   s    r#   r.   )AESARC4)padc                   J    \ rS rSrS\SS4S jrS\S\4S jrS\S\4S jrS	rg)
CryptRC4?   keyr   Nc                     Xl         g r   r6   r   r6   s     r   __init__CryptRC4.__init__@       Hr#   r   c                 `    [         R                  " U R                  5      R                  U5      $ r   )r1   
ARC4Cipherr6   r    r   s     r   r    CryptRC4.encryptC        ??488,44T::r#   c                 `    [         R                  " U R                  5      R                  U5      $ r   )r1   r>   r6   r%   r   s     r   r%   CryptRC4.decryptF   r@   r#   r8   	r'   r(   r)   r*   r+   r:   r    r%   r,   r   r#   r   r4   r4   ?   s;    	 	$ 		; 	;% 	;	; 	;% 	;r#   r4   c                   J    \ rS rSrS\SS4S jrS\S\4S jrS\S\4S jrS	rg)
CryptAESI   r6   r   Nc                     Xl         g r   r8   r9   s     r   r:   CryptAES.__init__J   r<   r#   r   c           	      H  ^ [        [        S [        S5       5       5      5      nS[        U5      S-  -
  mU[        [        U4S j[        T5       5       5      5      -  n[        R
                  " U R                  [        R                  U5      nX#R                  U5      -   $ )Nc              3   P   #    U  H  n[         R                  " S S5      v   M     g7f)r      N)randomrandint.0_s     r   	<genexpr>#CryptAES.encrypt.<locals>.<genexpr>N   s      KA3!7!7   $&   c              3   (   >#    U  H  nTv   M	     g 7fr   r   )rO   rP   ps     r   rQ   rR   P   s     #8x!Axs   )	r+   	bytearrayrangelenr0   newr6   MODE_CBCr    )r   r   ivaesrV   s       @r   r    CryptAES.encryptM   sx    y Kr KKLBSY^#AE)#8uQx#8899D''$((CLL"5CD)))r#   c                 
   US S nUSS  n[         R                  " U R                  [         R                  U5      n[	        U5      S-  (       a  [        US5      nUR                  U5      n[	        U5      S:X  a  U$ US US   *  $ )NrT   r   )r0   rZ   r6   r[   rY   r2   r%   )r   r   r\   r]   ds        r   r%   CryptAES.decryptT   sx    crB9D''$((CLL"5C4y2~4}D!A1v{AbE6{"r#   r8   rC   r   r#   r   rE   rE   I   s;    	 	$ 		* 	*% 	*
	# 
	#% 
	#r#   rE   r6   r   r   c                 L    [         R                  " U 5      R                  U5      $ r   )r1   r>   r    r6   r   s     r   RC4_encryptre   `       s#++D11r#   c                 L    [         R                  " U 5      R                  U5      $ r   )r1   r>   r%   rd   s     r   RC4_decryptrh   c   rf   r#   c                 j    [         R                  " U [         R                  5      R                  U5      $ r   )r0   rZ   MODE_ECBr    rd   s     r   AES_ECB_encryptrk   f   "    wwsCLL)11$77r#   c                 j    [         R                  " U [         R                  5      R                  U5      $ r   )r0   rZ   rj   r%   rd   s     r   AES_ECB_decryptrn   i   rl   r#   r\   c                 l    [         R                  " U [         R                  U5      R                  U5      $ r   )r0   rZ   r[   r    r6   r\   r   s      r   AES_CBC_encryptrq   l   $    wwsCLL"-55d;;r#   c                 l    [         R                  " U [         R                  U5      R                  U5      $ r   )r0   rZ   r[   r%   rp   s      r   AES_CBC_decryptrt   o   rr   r#   c                   J    \ rS rSrS\SS4S jrS\S\4S jrS\S\4S jrS	rg)
r4   t   r6   r   Nc                    [        [        S5      5      U l        Sn[        S5       Ha  nX R                  U   -   X[        U5      -     -   S-  nU R                  U   U R                  U   sU R                  U'   U R                  U'   Mc     g )N   r   )listrX   SrY   )r   r6   jis       r   r:   r;   u   su    %*%DFA3Z]SSX%66#='+vvay$&&)$q	466!9  r#   r   c                 H   [        U R                  5      n[        S [        [        U5      5       5       5      nSu  pE[        [        U5      5       H:  nUS-   S-  nXRU   -   S-  nX%   X$   sX$'   X%'   X"U   X%   -   S-     nX   U-  X6'   M<     [	        [        U5      5      $ )Nc              3   &   #    U  H  nS v   M	     g7fr   Nr   rN   s     r   rQ   #CryptRC4.encrypt.<locals>.<genexpr>~   s     3"2Qq"2   )r   r   r
   rx   )ry   rz   rX   rY   r+   rW   )r   r   rz   outr|   r{   kxs           r   r    r?   |   s    TVVA3%D	"233CDA3t9%UcM1X$T14
adtad{c)*1 & 3((r#   c                 $    U R                  U5      $ r   )r    r   s     r   r%   rB      s    <<%%r#   )rz   rC   r   r#   r   r4   r4   t   s;    	< 	<$ 	<
	) 
	)% 
	)	& 	&% 	&r#   c                   J    \ rS rSrS\SS4S jrS\S\4S jrS\S\4S jrS	rg)
rE      r6   r   Nc                     g r   r   r9   s     r   r:   rH      s    r#   r   c                     [        S5      eNz*PyCryptodome is required for AES algorithmr   r   s     r   r    r^          !"NOOr#   c                     [        S5      er   r   r   s     r   r%   rb      r   r#   r   rC   r   r#   r   rE   rE      sA    	 	$ 		P 	P% 	P	P 	P% 	Pr#   c                 6    [        U 5      R                  U5      $ r   )r4   r    rd   s     r   re   re          }$$T**r#   c                 6    [        U 5      R                  U5      $ r   )r4   r%   rd   s     r   rh   rh      r   r#   c                     [        S5      er   r   rd   s     r   rk   rk          JKKr#   c                     [        S5      er   r   rd   s     r   rn   rn      r   r#   c                     [        S5      er   r   rp   s      r   rq   rq      r   r#   c                     [        S5      er   r   rp   s      r   rt   rt      r   r#   c                   R    \ rS rSrS\S\S\SS4S jrS\S\4S	 jrS\S\4S
 jrSr	g)CryptFilter   stmCryptstrCryptefCryptr   Nc                 (    Xl         X l        X0l        g r   )r   r   r   )r   r   r   r   s       r   r:   CryptFilter.__init__   s     ! r#   objc                     [         $ r   )NotImplemented)r   r   s     r   encrypt_objectCryptFilter.encrypt_object   s    r#   c                 D   [        U[        [        45      (       a2  U R                  R	                  UR
                  5      n[        U5      nU$ [        U[        5      (       a,  U R                  R	                  UR                  5      Ul	        U$ [        U[        5      (       a7  [        UR                  5       5       H  u  p4U R                  U5      X'   M     U$ [        U[        5      (       a0  [        [!        U5      5       H  nU R                  X   5      X'   M     U$ r   )
isinstancer   r   r   r%   original_bytesr   r   r   _datar   ry   itemsdecrypt_objectr   rX   rY   )r   r   r   dictkeyvaluer|   s         r   r   CryptFilter.decrypt_object   s    c,.>?@@==((););<D&t,C 
 \**--cii8CI 
 -.."&syy{"3#2259 #4
 
 [))3s8_,,SV4 %
r#   )r   r   r   )
r'   r(   r)   r*   r   r:   r   r   r   r,   r   r#   r   r   r      sM    !-6AJ	) 	 ) 	 r#   r   ) (      N   ^   r   u      A   d   r   r   V   rK      r
      .   r   r         h   >      r            r   S   i   z   c                     U [         -   S S $ )N    )_PADDING)r   s    r   _paddingr      s    8OSb!!r#   c                   $   \ rS rSr\S\S\S\S\S\S\S\S	\4S
 j5       r\S\S\S\S	\4S j5       r	\S\S\S\S	\4S j5       r
\S\S\S\S	\4S j5       r\S\S\S\S\S\S\S\S\S	\4S j5       r\S\S\S\S\S\S\S\S\S	\4S j5       rSrg)AlgV4   passwordrevkey_sizeo_entryP	id1_entrymetadata_encryptedr   c                    [        U 5      n[        R                  " U5      nUR                  U5        UR                  [        R
                  " SU5      5        UR                  U5        US:  a  USL a  UR                  S5        UR                  5       n	US-  n
US:  a9  [        S5       H*  n[        R                  " U	SU
 5      R                  5       n	M,     U	SU
 $ )	u  
Algorithm 2: Computing an encryption key.

a) Pad or truncate the password string to exactly 32 bytes. If the
   password string is more than 32 bytes long,
   use only its first 32 bytes; if it is less than 32 bytes long, pad it
   by appending the required number of
   additional bytes from the beginning of the following padding string:
        < 28 BF 4E 5E 4E 75 8A 41 64 00 4E 56 FF FA 01 08
        2E 2E 00 B6 D0 68 3E 80 2F 0C A9 FE 64 53 69 7A >
   That is, if the password string is n bytes long, append
   the first 32 - n bytes of the padding string to the end
   of the password string. If the password string is empty (zero-length),
   meaning there is no user password,
   substitute the entire padding string in its place.

b) Initialize the MD5 hash function and pass the result of step (a)
   as input to this function.
c) Pass the value of the encryption dictionary’s O entry to the
   MD5 hash function. ("Algorithm 3: Computing
   the encryption dictionary’s O (owner password) value" shows how the
   O value is computed.)
d) Convert the integer value of the P entry to a 32-bit unsigned binary
   number and pass these bytes to the
   MD5 hash function, low-order byte first.
e) Pass the first element of the file’s file identifier array (the value
   of the ID entry in the document’s trailer
   dictionary; see Table 15) to the MD5 hash function.
f) (Security handlers of revision 4 or greater) If document metadata is
   not being encrypted, pass 4 bytes with
   the value 0xFFFFFFFF to the MD5 hash function.
g) Finish the hash.
h) (Security handlers of revision 3 or greater) Do the following
   50 times: Take the output from the previous
   MD5 hash and pass the first n bytes of the output as input into a new
   MD5 hash, where n is the number of
   bytes of the encryption key as defined by the value of the encryption
   dictionary’s Length entry.
i) Set the encryption key to the first n bytes of the output from the
   final MD5 hash, where n shall always be 5
   for security handlers of revision 2 but, for security handlers of
   revision 3 or greater, shall depend on the
   value of the encryption dictionary’s Length entry.
<I   F   r      2   N)r   hashlibmd5updatestructpackdigestrX   )r   r   r   r   r   r   r   au_hashu_hash_digestlengthrP   s               r   compute_keyAlgV4.compute_key   s    l XQgfkk$*+i !8*e3MM-.Q!82Y 'M'6,B C J J L Wf%%r#   owner_passwordc                     [        U 5      n[        R                  " U5      R                  5       nUS:  a6  [	        S5       H'  n[        R                  " U5      R                  5       nM)     USUS-   nU$ )ub  
Algorithm 3: Computing the encryption dictionary’s O (owner password) value.

a) Pad or truncate the owner password string as described in step (a)
   of "Algorithm 2: Computing an encryption key".
   If there is no owner password, use the user password instead.
b) Initialize the MD5 hash function and pass the result of step (a) as
   input to this function.
c) (Security handlers of revision 3 or greater) Do the following 50 times:
   Take the output from the previous
   MD5 hash and pass it as input into a new MD5 hash.
d) Create an RC4 encryption key using the first n bytes of the output
   from the final MD5 hash, where n shall
   always be 5 for security handlers of revision 2 but, for security
   handlers of revision 3 or greater, shall
   depend on the value of the encryption dictionary’s Length entry.
e) Pad or truncate the user password string as described in step (a) of
   "Algorithm 2: Computing an encryption key".
f) Encrypt the result of step (e), using an RC4 encryption function with
   the encryption key obtained in step (d).
g) (Security handlers of revision 3 or greater) Do the following 19 times:
   Take the output from the previous
   invocation of the RC4 function and pass it as input to a new
   invocation of the function; use an encryption
   key generated by taking each byte of the encryption key obtained in
   step (d) and performing an XOR
   (exclusive or) operation between that byte and the single-byte value
   of the iteration counter (from 1 to 19).
h) Store the output from the final invocation of the RC4 function as
   the value of the O entry in the encryption dictionary.
r   r   Nr   )r   r   r   r   rX   )r   r   r   r   o_hash_digestrP   rc4_keys          r   compute_O_value_keyAlgV4.compute_O_value_key2  sh    B ^$A--/!82Y 'M : A A C   (a-0r#   r   user_passwordc                    ^ [        U5      n[        X5      nUS:  a<  [        SS5       H,  m[        [	        U4S jU  5       5      5      n[        XT5      nM.     U$ )z See :func:`compute_O_value_key`.r   r
      c              3   ,   >#    U  H	  oT-  v   M     g 7fr   r   rO   r   r|   s     r   rQ   (AlgV4.compute_O_value.<locals>.<genexpr>d       %=W!eW   )r   re   rX   r+   rW   )r   r   r   r   rc4_encr6   r|   s         @r   compute_O_valueAlgV4.compute_O_value]  sV     ]#g)!81b\I%=W%==>%c3 " r#   r6   c                 L  ^ US::  a  [        U [        5      nU$  [        R                  " [        5      nUR	                  U5        [        XR                  5       5      n[        SS5       H,  m[        [        U4S jU  5       5      5      n[        Xe5      nM.     [        U5      $ )u  
Algorithm 4: Computing the encryption dictionary’s U (user password) value.

(Security handlers of revision 2)

a) Create an encryption key based on the user password string, as
   described in "Algorithm 2: Computing an encryption key".
b) Encrypt the 32-byte padding string shown in step (a) of
   "Algorithm 2: Computing an encryption key", using an RC4 encryption
   function with the encryption key from the preceding step.
c) Store the result of step (b) as the value of the U entry in the
   encryption dictionary.
   r
   r   c              3   ,   >#    U  H	  oT-  v   M     g 7fr   r   r   s     r   rQ   (AlgV4.compute_U_value.<locals>.<genexpr>  s     %9S!eSr   )
re   r   r   r   r   r   rX   r+   rW   r   )r6   r   r   r   r   r   r   r|   s          @r   compute_U_valueAlgV4.compute_U_valueh  s     !8X.EL	2 X&i c==?3q"AI%9S%99:G!'3G    r#   u_entryc           	          [         R                  XX#XVU5      n[         R                  XU5      n	US:  a
  U	SS n	USS nX:w  a  SnU$ )u  
Algorithm 6: Authenticating the user password.

a) Perform all but the last step of "Algorithm 4: Computing the encryption dictionary’s U (user password)
   value (Security handlers of revision 2)" or "Algorithm 5: Computing the encryption dictionary’s U (user
   password) value (Security handlers of revision 3 or greater)" using the supplied password string.
b) If the result of step (a) is equal to the value of the encryption dictionary’s U entry (comparing on the first 16
   bytes in the case of security handlers of revision 3 or greater), the password supplied is the correct user
   password. The key obtained in step (a) (that is, in the first step of "Algorithm 4: Computing the encryption
   dictionary’s U (user password) value (Security handlers of revision 2)" or "Algorithm 5: Computing the
   encryption dictionary’s U (user password) value (Security handlers of revision 3 or greater)") shall be used
   to decrypt the document.
r   NrT   r#   )r   r   r   )
r   r   r   r   r   r   r   r   r6   u_values
             r   verify_user_passwordAlgV4.verify_user_password  s_    0 1AS
 '')<!8crlGcrlGC
r#   c           
      
  ^ [         R                  XU5      nUS::  a  [        X5      n	O?Un	[        SSS5       H,  m[	        [        U4S jU 5       5      5      n
[        X5      n	M.     [         R                  U	UUUUUUU5      $ )u  
Algorithm 7: Authenticating the owner password.

a) Compute an encryption key from the supplied password string, as described in steps (a) to (d) of
   "Algorithm 3: Computing the encryption dictionary’s O (owner password) value".
b) (Security handlers of revision 2 only) Decrypt the value of the encryption dictionary’s O entry, using an RC4
   encryption function with the encryption key computed in step (a).
   (Security handlers of revision 3 or greater) Do the following 20 times: Decrypt the value of the encryption
   dictionary’s O entry (first iteration) or the output from the previous iteration (all subsequent iterations),
   using an RC4 encryption function with a different encryption key at each iteration. The key shall be
   generated by taking the original key (obtained in step (a)) and performing an XOR (exclusive or) operation
   between each byte of the key and the single-byte value of the iteration counter (from 19 to 0).
c) The result of step (b) purports to be the user password. Authenticate this user password using "Algorithm 6:
   Authenticating the user password". If it is correct, the password supplied is the correct owner password.
r      r`   c              3   ,   >#    U  H	  oT-  v   M     g 7fr   r   r   s     r   rQ   .AlgV4.verify_owner_password.<locals>.<genexpr>  r   r   )r   r   rh   rX   r+   rW   r   )r   r   r   r   r   r   r   r   r   r   r6   r|   s              @r   verify_owner_passwordAlgV4.verify_owner_password  s    4 ++NJ!8'9M#M2r2&I%=W%==> +C ? ' ))	
 		
r#   r   N)r'   r(   r)   r*   staticmethodr+   intboolr   r   r   r   r   r  r,   r   r#   r   r   r      s   A&A&A& A& 	A&
 A& A& !A& 
A& A&F (E ( (s (u ( (T  u 3 5   1!U 1! 1! 1!5 1! 1!f       	 
       !  
   D +
+
+
 +
 	+

 +
 +
 +
 !+
 
+
 +
r#   r   c                   j   \ rS rSr\S\S\S\S\S\S\4S j5       r\S\S\S\S	\S\4
S
 j5       r\S\S\S\S\S\4
S j5       r	\S\S\S\S\
S\
4
S j5       r\S\S\S\S\S\
S\\\4   4S j5       r\S\S\S\\\4   4S j5       r\S\S\S\S\\\4   4S j5       r\S\S\S\
S\4S j5       rSrg)AlgV5i  Rr   o_valueoe_valuer   r   c                     USS n[         R                  XUSS USS 5      USS :w  a  g[        S [        S5       5       5      n[         R                  XUSS USS 5      n[	        XeU5      nU$ )	u  
Algorithm 3.2a Computing an encryption key.

To understand the algorithm below, it is necessary to treat the O and U strings in the Encrypt dictionary
as made up of three sections. The first 32 bytes are a hash value (explained below). The next 8 bytes are
called the Validation Salt. The final 8 bytes are called the Key Salt.

1. The password string is generated from Unicode input by processing the input string with the SASLprep
   (IETF RFC 4013) profile of stringprep (IETF RFC 3454), and then converting to a UTF-8 representation.
2. Truncate the UTF-8 representation to 127 bytes if it is longer than 127 bytes.
3. Test the password against the owner key by computing the SHA-256 hash of the UTF-8 password
   concatenated with the 8 bytes of owner Validation Salt, concatenated with the 48-byte U string. If the
   32-byte result matches the first 32 bytes of the O string, this is the owner password.
   Compute an intermediate owner key by computing the SHA-256 hash of the UTF-8 password
   concatenated with the 8 bytes of owner Key Salt, concatenated with the 48-byte U string. The 32-byte
   result is the key used to decrypt the 32-byte OE string using AES-256 in CBC mode with no padding and
   an initialization vector of zero. The 32-byte result is the file encryption key.
4. Test the password against the user key by computing the SHA-256 hash of the UTF-8 password
   concatenated with the 8 bytes of user Validation Salt. If the 32 byte result matches the first 32 bytes of
   the U string, this is the user password.
   Compute an intermediate user key by computing the SHA-256 hash of the UTF-8 password
   concatenated with the 8 bytes of user Key Salt. The 32-byte result is the key used to decrypt the 32-byte
   UE string using AES-256 in CBC mode with no padding and an initialization vector of zero. The 32-byte
   result is the file encryption key.
5. Decrypt the 16-byte Perms string using AES-256 in ECB mode with an initialization vector of zero and
   the file encryption key as the key. Verify that bytes 9-11 of the result are the characters ‘a’, ‘d’, ‘b’. Bytes
   0-3 of the decrypted Perms entry, treated as a little-endian integer, are the user permissions. They
   should match the value in the P key.
N   r   r   0   r#   c              3   &   #    U  H  nS v   M	     g7fr   r   rN   s     r   rQ   .AlgV5.verify_owner_password.<locals>.<genexpr>       (i1ir   rT   r  calculate_hashr+   rX   rt   )r	  r   r
  r  r   r\   tmp_keyr6   s           r   r  AlgV5.verify_owner_password  s    B DS>  gbngcrlKs| (eBi((&&qGBrNGCRLQg84
r#   ue_valuec                     USS n[         R                  XUSS S5      USS :w  a  g[        S [        S5       5       5      n[         R                  XUSS S5      n[	        XTU5      $ )	z"See :func:`verify_owner_password`.Nr  r   r   r#   c              3   &   #    U  H  nS v   M	     g7fr   r   rN   s     r   rQ   -AlgV5.verify_user_password.<locals>.<genexpr>#  r  r   rT   r  r  )r	  r   r   r  r\   r  s         r   r   AlgV5.verify_user_password  sp    
 DS>WR^SAWSb\Q(eBi((&&qGBrNCHwH55r#   saltudatac                    [         R                  " X-   U-   5      R                  5       nU S:  a  U$ Sn US-  nX-   U-   n[        US S USS US-  5      n[         R                  [         R                  [         R
                  4[        US S 5      S-     nU" U5      R                  5       nUS:  a  US   US-
  ::  a  OM  US S $ )	N   r   r
   rT   r   @   r   r`   )r   sha256r   rq   sha384sha512sum)	r	  r   r  r  KcountK1Ehash_fns	            r   r  AlgV5.calculate_hash'  s     NN8?U23::<q5HQJE%B#2"R"r':A !CR&kAo	G
 
!!#A{qu
2  "vr#   r6   permsrV   r   c                 |    U(       a  SOSn[         R                  " SU5      S-   U-   S-   n[        X5      nXVSS :H  $ )zBSee :func:`verify_owner_password` and :func:`compute_Perms_value`.   T   Fr   r      adbNr   )r   r   rn   )r6   r*  rV   r   b8p1p2s          r   verify_permsAlgV5.verify_perms<  sE    
 (TT[[q!$77"<vES(W}r#   r   r   c                     [         R                  X5      u  pV[         R                  XU5      u  px[         R                  X#U5      n	UUUUU	S.$ )N)/U/UE/O/OE/Perms)r  r   r   compute_Perms_value)
r   r   r6   rV   r   r   r  r
  r  r*  s
             r   generate_valuesAlgV5.generate_valuesF  sX     "11-E!11.wO))#2DE
 	
r#   c                 D   [        S [        S5       5       5      nUSS nUSS n[        R                  " X-   5      R	                  5       U-   U-   n[        R                  " X-   5      R	                  5       n[        S [        S5       5       5      n[        XgU5      nXX4$ )u(  
Algorithm 3.8 Computing the encryption dictionary’s U (user password) and UE (user encryption key) values

1. Generate 16 random bytes of data using a strong random number generator. The first 8 bytes are the
   User Validation Salt. The second 8 bytes are the User Key Salt. Compute the 32-byte SHA-256 hash of
   the password concatenated with the User Validation Salt. The 48-byte string consisting of the 32-byte
   hash followed by the User Validation Salt followed by the User Key Salt is stored as the U key.
2. Compute the 32-byte SHA-256 hash of the password concatenated with the User Key Salt. Using this
   hash as the key, encrypt the file encryption key using AES-256 in CBC mode with no padding and an
   initialization vector of zero. The resulting 32-byte string is stored as the UE key.
c              3   P   #    U  H  n[         R                  " S S5      v   M     g7fr   rx   NrL   	randrangerN   s     r   rQ   (AlgV5.compute_U_value.<locals>.<genexpr>f        Iy!V--a55yrS   rT   Nr   c              3   &   #    U  H  nS v   M	     g7fr   r   rN   s     r   rQ   rB  l  r  r   r+   rX   r   r   r   rq   )	r   r6   random_bytesval_saltkey_saltr   r  r\   r  s	            r   r   AlgV5.compute_U_valueY  s     IuRyII##..!45<<>IHT..!45<<>(eBi(("74  r#   c                 P   [        S [        S5       5       5      nUSS nUSS n[        R                  " X-   U-   5      R	                  5       U-   U-   n[        R                  " X-   U-   5      R	                  5       n[        S [        S5       5       5      n[        XxU5      n	Xi4$ )u  
Algorithm 3.9 Computing the encryption dictionary’s O (owner password) and OE (owner encryption key) values.

1. Generate 16 random bytes of data using a strong random number generator. The first 8 bytes are the
   Owner Validation Salt. The second 8 bytes are the Owner Key Salt. Compute the 32-byte SHA-256 hash
   of the password concatenated with the Owner Validation Salt and then concatenated with the 48-byte
   U string as generated in Algorithm 3.8. The 48-byte string consisting of the 32-byte hash followed by
   the Owner Validation Salt followed by the Owner Key Salt is stored as the O key.
2. Compute the 32-byte SHA-256 hash of the password concatenated with the Owner Key Salt and then
   concatenated with the 48-byte U string as generated in Algorithm 3.8. Using this hash as the key,
   encrypt the file encryption key using AES-256 in CBC mode with no padding and an initialization vector
   of zero. The resulting 32-byte string is stored as the OE key.
c              3   P   #    U  H  n[         R                  " S S5      v   M     g7fr?  r@  rN   s     r   rQ   (AlgV5.compute_O_value.<locals>.<genexpr>  rC  rS   rT   Nr   c              3   &   #    U  H  nS v   M	     g7fr   r   rN   s     r   rQ   rL    r  r   rE  )
r   r6   r   rF  rG  rH  r
  r  r\   r  s
             r   r   AlgV5.compute_O_valuep  s    " IuRyII##NN8.89@@BXMPXX 	 ..!4w!>?FFH(eBi(("74  r#   c                     U(       a  SOSn[        S [        S5       5       5      n[        R                  " SU5      S-   U-   S-   U-   n[	        X5      nU$ )u.  
Algorithm 3.10 Computing the encryption dictionary’s Perms (permissions) value

1. Extend the permissions (contents of the P integer) to 64 bits by setting the upper 32 bits to all 1’s. (This
   allows for future extension without changing the format.)
2. Record the 8 bytes of permission in the bytes 0-7 of the block, low order byte first.
3. Set byte 8 to the ASCII value ' T ' or ' F ' according to the EncryptMetadata Boolean.
4. Set bytes 9-11 to the ASCII characters ' a ', ' d ', ' b '.
5. Set bytes 12-15 to 4 bytes of random data, which will be ignored.
6. Encrypt the 16-byte block using AES-256 in ECB mode with an initialization vector of zero, using the file
   encryption key as the key. The result (16 bytes) is stored as the Perms string, and checked for validity
   when the file is opened.
r,  r-  c              3   P   #    U  H  n[         R                  " S S5      v   M     g7fr?  r@  rN   s     r   rQ   ,AlgV5.compute_Perms_value.<locals>.<genexpr>  s      >X6##As++XrS   r   r   r   r.  )r+   rX   r   r   rk   )r6   rV   r   r/  rrr   r*  s          r   r:  AlgV5.compute_Perms_value  sU     (TT>U1X>>{{4#&99B>G"L*r#   r   N)r'   r(   r)   r*   r  r  r+   r  r   r  r  r2  r   r   r;  r   r   r   r:  r,   r   r#   r   r  r    s   )))*/);@)KP)	) )V 	6	6	6*/	6;@	6		6 	6 #  e E e  (  %(>B	  


 
 	

 !
 
c3h
 
$ !% !e !eUl8K ! !, !!#!.3!	ue|	! !8  3 D U  r#   r  c                        \ rS rSrSrSrSrSrg)PasswordTypei  r   r
   r   r   N)r'   r(   r)   r*   NOT_DECRYPTEDUSER_PASSWORDOWNER_PASSWORDr,   r   r#   r   rU  rU    s    MMNr#   rU  c                      \ rS rSrS\S\S\S\S\S\S\S	S
4S jrS	\	4S jr
S\S\S\S	\4S jr\S\S\S\S\S	\4
S j5       rS\\\4   S	\4S jrS\S	\\\4   4S jrS\S	\\\4   4S jr\S\S\S	S 4S j5       rSrg
)
Encryptioni  algValgRentryfirst_id_entryStmFStrFEFFr   Nc                     Xl         X l        X0l        UR                  SS5      U l        X@l        XPl        X`l        Xpl        [        R                  U l        S U l        g )Nz/Lengthr   )r[  r\  r]  getr   r   r_  r`  ra  rU  rV  _password_type_key)r   r[  r\  r]  r^  r_  r`  ra  s           r   r:   Encryption.__init__  sP     		
		)R0'		 +88%)	r#   c                 <    U R                   [        R                  :g  $ r   )rd  rU  rV  )r   s    r   is_decryptedEncryption.is_decrypted  s    ""l&@&@@@r#   r   idnum
generationc                    [         R                  " SU5      SS n[         R                  " SU5      SS nU R                  (       d   eU R                  nU R                  S:X  a  SOU R                  S-  nUSU U-   U-   n[
        R                  " U5      n	U	R                  5       S[        US-   S5       n
U	R                  S	5        U	R                  5       S[        US-   S5       nUnU R                  U R                  XU5      nU R                  U R                  XU5      nU R                  U R                  XU5      n[        XU5      nUR                  U5      $ )
u  
Algorithm 1: Encryption of data using the RC4 or AES algorithms.

a) Obtain the object number and generation number from the object identifier of the string or stream to be
   encrypted (see 7.3.10, "Indirect Objects"). If the string is a direct object, use the identifier of the indirect
   object containing it.
b) For all strings and streams without crypt filter specifier; treating the object number and generation number
   as binary integers, extend the original n-byte encryption key to n + 5 bytes by appending the low-order 3
   bytes of the object number and the low-order 2 bytes of the generation number in that order, low-order byte
   first. (n is 5 unless the value of V in the encryption dictionary is greater than 1, in which case n is the value
   of Length divided by 8.)
   If using the AES algorithm, extend the encryption key an additional 4 bytes by adding the value “sAlT”,
   which corresponds to the hexadecimal values 0x73, 0x41, 0x6C, 0x54. (This addition is done for backward
   compatibility and is not intended to provide additional security.)
c) Initialize the MD5 hash function and pass the result of step (b) as input to this function.
d) Use the first (n + 5) bytes, up to a maximum of 16, of the output from the MD5 hash as the key for the RC4
   or AES symmetric key algorithms, along with the string or stream data to be encrypted.
   If using the AES algorithm, the Cipher Block Chaining (CBC) mode, which requires an initialization vector,
   is used. The block size parameter is set to 16 bytes, and the initialization vector is a 16-byte random
   number that is stored as the first 16 bytes of the encrypted stream or string.

Algorithm 3.1a Encryption of data using the AES algorithm
1. Use the 32-byte file encryption key for the AES-256 symmetric key algorithm, along with the string or
   stream data to be encrypted.
   Use the AES algorithm in Cipher Block Chaining (CBC) mode, which requires an initialization vector. The
   block size parameter is set to 16 bytes, and the initialization vector is a 16-byte random number that is
   stored as the first 16 bytes of the encrypted stream or string.
   The output is the encrypted data to be stored in the PDF file.
z<iNr   r   r
      r   rT   s   sAlT)r   r   re  r[  r   r   r   r   minr   
_get_cryptr_  r`  ra  r   r   )r   r   rj  rk  pack1pack2r6   nkey_datakey_hashr   
aes128_key
aes256_keyr   StrCryptr   cfs                    r   r   Encryption.decrypt_object  s1   < D%(!,D*-bq1yyyiiaAT]]a%7r7U?U*;;x(//#$4c!a%n5 __&'7QUB8
 
??499g:N??499g:N//$((GLW5  %%r#   methodr   ru  rv  c                 |    U S:X  a  [        U5      $ U S:X  a  [        U5      $ U S:X  a
  [        5       $ [        U5      $ )N/AESV3/AESV2	/Identity)rE   r.   r4   )rz  r   ru  rv  s       r   ro  Encryption._get_crypt  sE     XJ''XJ''{" ?"G$$r#   r   c                 D   [        U[        5      (       a   UR                  S5      nOUnU R                  S::  a  U R                  U5      OU R                  U5      u  p4U[        R                  :w  a  X@l	        X0l
        U$ ! [         a    UR                  S5      n Nsf = f)Nzlatin-1zutf-8r   )r   strencode	Exceptionr[  	verify_v4	verify_v5rU  rV  rd  re  )r   r   pwdr6   rcs        r   verifyEncryption.verify  s    h$$/ooi0 C)-a$..%T^^C=P+++"$I	  /oog./s   B BBc           
         [        [        U R                  S   5      n[        [        U R                  S   5      nUS-   S-  nU R                  R                  S5      nU(       a  UR                  OSn[        [
        U R                  S   R                  5       5      R                  n[        [
        U R                  S   R                  5       5      R                  n[        R                  UUU R                  UUUU R                  U5      nU(       a  U[        R                  4$ [        R                  UUU R                  UUUU R                  U5      nU(       a  U[        R                  4$ S[        R                   4$ )	N/R/P        /EncryptMetadataTr7  r5  r#   )r	   r  r]  rc  r   r   
get_objectr   r   r  r   r   rU  rX  r   rW  rV  )	r   r   r	  r   emr   r   r   r6   s	            r   r  Encryption.verify_v4  sD   djj&'djj&'_+ZZ^^./)+RXX'D)9)D)D)FGVV'D)9)D)D)FGVV ))MMNN	
 3333((MMNN	
 2222L....r#   c                    [        [        U R                  S   R                  5       5      R                  n[        [        U R                  S   R                  5       5      R                  n[        [        U R                  S   R                  5       5      R                  n[        [        U R                  S   R                  5       5      R                  n[
        R                  U R                  XXC5      n[        R                  nU(       d1  [
        R                  U R                  XU5      n[        R                  nU(       d  S[        R                  4$ [        [        U R                  S   R                  5       5      R                  n[        [        U R                  S   5      n	U	S-   S-  n	U R                  R                  S	S
5      n
[
        R                  XhX5      (       d  [!        S["        5        Xg4$ )Nr7  r5  r8  r6  r#   r9  r  r  r  Tzignore '/Perms' verify failed)r	   r   r]  r  r   r  r  r\  rU  rX  r   rW  rV  r  rc  r2  r   r'   )r   r   r   r   oe_entryue_entryr6   r  r*  r   r   s              r   r  Encryption.verify_v5<  s   'D)9)D)D)FGVV'D)9)D)D)FGVV($**U*;*F*F*HIXX($**U*;*F*F*HIXX ))IIx(
 ((,,TYY8TC++B2222 %tzz(';'F'F'HIXXdjj&'_+!ZZ^^,>E!!#aDD:HEwr#   encryption_entryc           	      H   U R                  S5      nUS:w  a  [        S5      eSU ;   a  [        S5      eSnSnSnU R                  SS5      nUS	;  a  [        S
U S35      eUS:  a  U S   nU R                  SS5      nU R                  SS5      nU R                  SU5      nUS:w  a  Xs   S   nUS:w  a  Xt   S   nUS:w  a  Xu   S   nSnX8;  a  [        S5      eXH;  a  [        SU S35      eXX;  a  [        SU S35      e[        [        U S   5      n	[	        XiXX4U5      $ )Nz/Filterz	/Standardz1only Standard PDF encryption handler is availablez
/SubFilterz/SubFilter NOT supported/V2z/Vr   )r
   r   r   r   rm  zEncryption V=z NOT supportedr   z/CFz/StmFr~  z/StrFz/EFFz/CFM)r~  r  r}  r|  z!StmF Method {StmF} NOT supported!zStrF Method z NOT supported!zEFF Method r  )rc  NotImplementedErrorr	   r  rZ  )
r  r^  filterr_  r`  ra  Vfiltersallowed_methodsr	  s
             r   readEncryption.readW  sp   !%%i0[ %C  ++%&@AA  q)O#%aS&GHH6&u-G#''=D#''=D"&&vt4C{"}V,{"}V,k!l6*FO*)*MNN*)Lo*NOO))KuO*LMM&t,-! 0$cRRr#   )
ra  r_  r`  re  rd  r\  r[  r]  r   r   )r'   r(   r)   r*   r  r   r+   r  r:   r  rh  r   r   r  r   ro  r   rU  r  r   r  r  r  r,   r   r#   r   rZ  rZ    sG   ** *  	*
 * * * * 
*2Ad A3&) 3&C 3&S 3&Y 3&j 
%
%#
%16
%DI
%	
% 
%uUCZ0 \ #/% #/E%2E,F #/J% E%2E,F 6 'S/ 'S 'S< 'S 'Sr#   rZ  )0r   rL   r   enumr   typingr   r   r   r   r   r	   _utilsr   errorsr   genericr   r   r   r   r   r   r   r   r.   Crypto.Cipherr0   r1   Crypto.Util.Paddingr2   r4   rE   r+   re   rh   rk   rn   rq   rt   ImportErrorr   r   r   r   r  rU  rZ  r   r#   r   <module>r     sb  8     : : " #   	I 	jL'';9 ;#9 #.2 2e 2 22 2e 2 28U 8% 8E 88U 8% 8E 8<U < <U <u <<U < <U <u <r 6 !#L"5 "U "~
 ~
Br rj7 VS VSo  3L&9 &.P9 P+ +e + ++ +e + +LU L% LE LLU L% LE LLU L LU Lu LLU L LU Lu Le3Ls   A*D A"E-,E-