
    :)di-                         d dl mZ d dlmZ d dlZd dlZd dlZd dlm	Z	 ddl
mZ ddlmZ dd	l
mZ dd
lmZmZmZmZmZmZ  G d de          ZdS )    )IOBase)BinaryION)log   )fields)EfuseDefineBlocks   )util)BaseCommandsNonCompositeTupleTupleParameteradd_force_write_alwaysadd_show_sensitive_info_optionprotect_optionsc                        e Zd ZdZej        Zdej        f fdZ	de
ez  fdZd Z	 	 	 	 dd	ee
         d
ee         dee
         dedededee         dz  fdZ	 	 	 ddedededefdZ xZS )ESP32C2CommandszESP32-C2clic                     t                                          |           t                                                      }|                    ddd                    |           dd                    t          j        j                   d          t          j
        ddt          d	t          |          d
z   t          t          j        |          t          j        d          t          j        t          j        j                  g                    t           t"          t$          t          j         fd                                                                        }|                    ddd          t          j
        dt          j        d                    t           t"          t$          t          j         fd                                                                        }d S )Nzburn-keyzBurn the key block with the specified name. Arguments are groups of block name, key file (containing 128/256 bits of binary key data) and key purpose.

Block is one of: [z, z]

Key purpose is one of: [])helpblock_keyfile_keypurposez<BLOCK> <KEYFILE> <KEYPURPOSE>r   rb)metavarclsnargs	max_aritytypec                     t          |                    d           \  }}}|                    d           | j        |d<    j        |||fi | dS )z*Burn the key block with the specified namer   force_write_alwaysshow_sensitive_infoN)zippopr!   burn_key)ctxkwargsblockkeyfile
keypurposeselfs        h/var/www/menimich/repos/protonApp/venv/lib/python3.11/site-packages/espefuse/efuse/esp32c2/operations.pyburn_key_cliz6ESP32C2Commands.add_cli_commands.<locals>.burn_key_cli&   sh    : *-fjj9S.T.T)U&E7JJJ+,,,,/,CF()DM%*???????    zburn-key-digestz.Parse an ECDSA public key and burn the digest.ziParse an ECDSA public key and burn the digest to higher 128-bits of BLOCK_KEY0. KEYFILE is in PEM format.)
short_helpr   r(   )r   c                 `    |                     d           | j        |d<    j        di | d S )Nr    r!    )r#   r!   burn_key_digest)r%   r&   r*   s     r+   burn_key_digest_cliz=ESP32C2Commands.add_cli_commands.<locals>.burn_key_digest_cliH   sE     JJ+,,,,/,CF() D **6*****r-   )superadd_cli_commandsr   get_blocks_for_keyscommandjoinr   EfuseKeyPurposeFieldKEY_PURPOSES_NAMEclickargumentr   lenr   ChoiceFiler   r   r   pass_context)r*   r   blocks_for_keysr,   r2   	__class__s   `    r+   r4   z ESP32C2Commands.add_cli_commands"   s     %%%+--AACC	c!%?!;!;c c (,yy1L1^'_'_c c c 
 

 

 
&4 /**Q."L11Jt$$L!<!NOO 

 

 

  
		'			@ 	@ 	@ 	@ 
	 
(	' 
 	 
!

 



 

6	@ 
G( 
 

 

 
	
4(8(8	9	9	9			'			+ 	+ 	+ 	+ 
	 
(	' 
 	 
 
:	9

 

	+ 	+ 	+r-   macc                 :   |                      |          }| j        d                             |           | j        d                             d           | j                            d          sd S |                                  t          j        d           d S )N
CUSTOM_MACCUSTOM_MAC_USEDr   Tcheck_batch_modeSuccessful.)_convert_mac_to_bytesefusessaveburn_allget_custom_macr   print)r*   rB   s     r+   burn_custom_maczESP32C2Commands.burn_custom_macZ   s    ((--L!&&s+++%&++A...{##T#:: 	F	-     r-   c                 $   t          j        d| j                                                   | j                                        dk    rEt          j        d| j        d                                          d           t          j        d| j        d                                                    t          j        d           t          j        d	| j        d
                                                    t          j        d| j        d                                                    t          j        d| j        d                                                    t          j        d| j        d                                                    d S d S )NzBlock version:r   z!Temperature Sensor Calibration = 
TEMP_CALIBCzADC OCode        = OCODEzADC1:zINIT_CODE_ATTEN0 = ADC1_INIT_CODE_ATTEN0zINIT_CODE_ATTEN3 = ADC1_INIT_CODE_ATTEN3zCAL_VOL_ATTEN0   = ADC1_CAL_VOL_ATTEN0zCAL_VOL_ATTEN3   = ADC1_CAL_VOL_ATTEN3)r   rN   rJ   get_block_versionget)r*   s    r+   adc_infozESP32C2Commands.adc_infoc   sK   	"DK$A$A$C$CDDD;((**a//I\$+l:S:W:W:Y:Y\\\]]]I+T[-A-E-E-G-GHHHIgI+T[9P-Q-U-U-W-WXXXI+T[9P-Q-U-U-W-WXXXI+T[9N-O-S-S-U-UVVVI+T[9N-O-S-S-U-UVVVVV 0/r-   FNblockskeyfileskeypurposesno_write_protectno_read_protectr!   digestc                 
   |"|dt          d |D                                }n!|dt          d |D                                }|dt          d |D                                }	|dt          d |D                                }
t          j        |
           t          |	          t          |          k    s t          |	          t          |
          k    rPt          j        d                    t          |	          t          |          t          |
                              dt          |	          cxk    rd	k    sn J d
            t          |	          d	k    rId|
v rdnd}ddg}||
d         |v o	|
d         |vz  }|r$t          j        dt          |
                     t          j        d           t          ||
          D ]#\  }}t          |t                    r)|                                }|                                 n|}|dk    r| j        d         }n|dk    r| j        d         }t          |          dk    rt          j        d           |dd         }nKt          |          |j        dz  k    r0t          j        dt          |           d|j        dz   d          t          |          dk    s
J d            n| j        d         }|j        dz  }t          j        d|j         d           d}|                    d           rd!}|ddd"         }t          j        d#                    |rt          j        |d          n%d                    d$gt          |          z                                 |rt          j        |           t          |          |k    r.t          j        d%t          |           d&| d'|dz   d(          |                    d           r| }nd}| }|                    |           |dk    rh| j        d)                                         rt          j        d*           n4t          j        d+           | j        d)                             d           |r(t          j        d,           |                                 |r(t          j        d-           |                                 t          j        d.           %|st          j        d/           |rt          j        d0           | j                            d1          sdS t          j        d2           dS )3a  Burn the key block with the specified name. Arguments are groups of block name,
        key file (containing 128/256 bits of binary key data) and key purpose.

        Args:
            blocks: List of eFuse block names to burn keys to.
            keyfiles: List of open files to read key data from.
            keypurposes: List of key purposes to burn.
            no_write_protect: If True, the write protection will NOT be enabled.
            no_read_protect: If True, the read protection will NOT be enabled.
            show_sensitive_info: If True, the sensitive information will be shown.
            digest: List of digests to burn.
        Nr   c                     g | ]}||S Nr0   ).0r(   s     r+   
<listcomp>z,ESP32C2Commands.burn_key.<locals>.<listcomp>   s    PPPWG<O<O<O<Or-   c                     g | ]}||S rc   r0   rd   names     r+   re   z,ESP32C2Commands.burn_key.<locals>.<listcomp>   s    EEE$D4D4D4D4Dr-   c                     g | ]}||S rc   r0   rg   s     r+   re   z,ESP32C2Commands.burn_key.<locals>.<listcomp>   s    AAAd0@T0@0@0@r-   c                     g | ]}||S rc   r0   rg   s     r+   re   z,ESP32C2Commands.burn_key.<locals>.<listcomp>   s    FFFdT5ET5E5E5Er-   zPThe number of blocks ({}), datafile ({}) and keypurpose ({}) should be the same.r   r	   zUnexpected caseXTS_AES_128_KEYTF+XTS_AES_128_KEY_DERIVED_FROM_128_EFUSE_BITSSECURE_BOOT_DIGESTz#These keypurposes are incompatible zBurn keys to blocks:BLOCK_KEY0_LOW_128BLOCK_KEY0_HI_128    ze	Programming only left-most 128-bits from SHA256 hash of public key to highest 128-bits of BLOCK KEY0      z6Wrong length of this file for SECURE_BOOT_DIGEST. Got z (expected 32 or )zOnly 16 bytes expected
BLOCK_KEY0z -  )endXTS_AES_z8	Reversing byte order for AES-XTS hardware peripheral...r   z-> [{}]z??zIncorrect key file size z. Key file must be z bytes (z bits) of raw binary key data.XTS_KEY_LENGTH_256z$	'XTS_KEY_LENGTH_256' is already '1'z	XTS_KEY_LENGTH_256 -> 1z	Disabling read to key block...z 	Disabling write to key block... z7Keys will remain writeable (due to --no-write-protect).z5Keys will remain readable (due to --no-read-protect).rF   rH   )r<   r
   check_duplicate_name_in_listesptool
FatalErrorformatlistr   rN   r"   
isinstancer   readcloserJ   bit_lenrh   
startswithhexifyr7   rK   rY   disable_readdisable_writerL   )r*   r[   r\   r]   r^   r_   r!   r`   datafile_listblock_name_listkeypurpose_listincompatiblepermitted_purposesdatafiler)   dataefuse	num_bytes
revers_msgread_protectwrite_protects                        r+   r$   zESP32C2Commands.burn_keyp   s8   . >$CPPPPPQQSMM #CEE&EEEFFHM !AAfAAABBD
 &FFkFFFGGI
 	)/:::3}#5#555_9M9MQTR
 R
 :
 :
 $66<f((#m*<*<c/>R>R7 7   C((----A-----/@---1$$#4#G#G44UL=$" "&88 A#A&.@@L  (Q$:O:OQQ   		()))$'$G$G G	 G	 Hj(F++  }}    JJJ$89333$78t99??IG    9DDYY%-1"444!,Q"4yyQ Q;@=A;MQ Q Q   4yyB(@L1*II(EJ((c2222J$$Z00 "X
DDbDzI  *6DKc***4&3t99"455     &	*%%%4yyI%%(Ms4yy M MY M M(1}M M M   $$Z00 %#22$ 00M JJt...;3488:: >IEFFFFI9:::K 45::1=== %	<===""$$$ &	=>>>##%%%IbMMMM 	QIOPPP 	OIMNNN{##T#:: 	F	-     r-   r(   c           	      D   t          j        |          }|dd         }| j        d         j        dz  }t	          |          |k    r9t          j        d                    t	          |          ||dz                      |                     dg|gdg||||g           dS )	a  Parse a RSA public key and burn the digest to key eFuse block.

        Args:
            keyfile: Open file to read key data from.
            no_write_protect: If True, the write protection will NOT be enabled.
            no_read_protect: If True, the read protection will NOT be enabled.
            show_sensitive_info: If True, the sensitive information will be shown.
        Nrq   ro   rr   zSIncorrect digest size {}. Digest must be {} bytes ({} bits) of raw binary key data.rt   rm   )r`   )		espsecure_digest_sbv2_public_keyrJ   r   r<   r{   r|   r}   r$   )r*   r(   r^   r_   r!   r`   r   s          r+   r1   zESP32C2Commands.burn_key_digest  s     27;;K 34<A	v;;)##$LLRFKKIMM M   	NI!"8 	 	
 	
 	
 	
 	
r-   )FFFN)FFF)__name__
__module____qualname__	CHIP_NAMEr   	EspEfuses	efuse_libr:   Groupr4   strbytesrO   rZ   r~   r   boolr$   r1   __classcell__)rA   s   @r+   r   r      s\       I I4+EK 4+ 4+ 4+ 4+ 4+ 4+p!3; ! ! ! !
W 
W 
W$ "' %$)%)S! S!S	S! x.S! #Y	S!
 S! S! "S! Ud"S! S! S! S!p "' %$)!
 !
!
 !
 	!

 "!
 !
 !
 !
 !
 !
 !
 !
r-   r   )ior   typingr   
rich_clickr:   r   r{   esptool.loggerr   ry   r   mem_definitionr   r
   base_operationsr   r   r   r   r   r   r   r0   r-   r+   <module>r      s"                                    - - - - - -                     J
 J
 J
 J
 J
l J
 J
 J
 J
 J
r-   