+
    :if&  c                   s   ^ RI t ^ RIt^ RIt^ RIHt ^ RIHt ^ RIHt ^ RI	H
t
 ^RIHtHtHtHtHt ]P"                  ! ]4      tRt ! R R	]4      t ! R
 R]] P,                  R7      tRtR R ltR R ltR R ltR R lt^t]^,
          t^@t]^,           t ^@t!^t"^ t#^t$^t%^t&^t'^t(^t)^t*R t+R t,R t- ! R R4      t.R# )    N)Event)sleep)Callable)	LOG_LEVEL)USB_INTERFACECommandError
ConnectionTimeoutErrorVersionZcbdefghijklnrtuvc                   s    ] tR t^-tRtRtR# )CommandRejectedErrorz.The issues command was rejected by the YubiKey N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       5/usr/lib/python3.14/site-packages/yubikit/core/otp.pyr
   r
   -   s    8r   r
   c                   s   a  ] tR t^1t o ]P
                  t]P                  V 3R lR l4       t	]P                  V 3R lR l4       t
RtV tR# )OtpConnectionc                    < V ^8  d   QhRS[ /#    returnbytesformat__classdict__   "r   __annotate__OtpConnection.__annotate__5   s     - - -r   c                    R# )zReads an 8 byte feature reportNr   self   &r   receiveZOtpConnection.receive4       r   c                s$   < V ^8  d   QhRS[ RR/# )r   datar   Nr   r   r   r   r   r    9   s     . . .4 .r   c                r!   )zWrites an 8 byte feature reportNr   )r#   r'      &&r   sendZOtpConnection.send8   r&   r   r   N)r   r   r   r   r   ZOTPZusb_interfaceabcZabstractmethodr%   r)   r   __classdictcell__r      @r   r   r   1   sB     !%%M- - 	. .r   r   )Z	metaclassi  c                0    V ^8  d   QhR\         R\        /# r   r'   r   )r   intr      "r   r   r   @   s     	 	 	# 	r   c                 s    R p\        \        V 4      4       FH  pWV,          ,          p\        ^4       F'  pV^,          pV^,          pV^8X  g   K  VR,          pK)  	  KJ  	  VR ,          # )i  i  )rangelen)r'   ZcrcindexiZjs   &    r   calculate_crcr7   @   sb    
Cs4y!E{qAaAAICAvv	  " <r   c                r.   r/   )r   Zboolr1   r2   r   r   r   L   s     2 2E 2d 2r   c                 s&    \        V 4      \        8H  # N)r7   CRC_OK_RESIDUALr'   r$   r   	check_crcr;   L   s    /11r   c                r.   r/   )r   strr1   r2   r   r   r   P   s     U U U# Ur   c                s2    RP                  R V  4       4      # )zHEncode a bytes-like object using Modhex (modified hexadecimal) encoding. c              3   s|   "   T F2  p\         V^,	          ,          \         V^,          ,          ,           x  K4  	  R# 5i   N)MODHEX_ALPHABET).0Zb   & r   	<genexpr>Z modhex_encode.<locals>.<genexpr>R   s*     Tt!?16*_QW-EEEts   :<)Zjoinr:   r$   r   modhex_encoderD   P   s    77TtTTTr   c                r.   )r   stringr   )r<   r   r1   r2   r   r   r   U   s      # % r   c           	     s   a  \        S 4      ^,          '       d   \        R4      h\        V 3R l\        ^ \        S 4      ^4       4       4      # )z0Decode the Modhex (modified hexadecimal) string.zLength must be a multiple of 2c              3   s   <"   T FP  p\         P                  SV,          4      ^,          \         P                  SV^,           ,          4      ,          x  KR  	  R# 5ir>   )r@   r5   )rA   r6   rE   s   & r   rC   Z modhex_decode.<locals>.<genexpr>Z   sH      )A 	fQi(A-0E0EfQQRUm0TTT)s   AA)r4   
ValueErrorr   r3   )rE   s   fr   modhex_decoderG   U   sC    
6{Q9:: q#f+q)  r   c                s2    VR9   ;'       g    \        V 4      # )zHAll-zero packets are skipped, except for the very first and last packets)r    i	   )Zany)Zpacketseqr(   r   _should_sendrI   v   s    &=''CK'r   c                 s`    V\         P                  ! R V \        V4      4      ,           R,           # )z<BHs      )structpackr7   )slotpayloadr(   r   _format_framerN   {   s$    V[[mG.DEE	QQr   c                 s    V \         ,          pW!^,           8H  ;'       g6    V^ 8H  ;'       d)    V^ 8  ;'       d    V \        ,          \        ,          ^ 8H  # )   )STATUS_OFFSET_PROG_SEQSTATUS_OFFSET_TOUCH_LOWCONFIG_SLOTS_PROGRAMMED_MASK)reportZprev_seqZnext_seq   && r   _is_sequence_updatedrU      sb     ,-H!|#  A 	P 	PqL	P 	P*+.JJaOr   c                   s   a  ] tR t^t o RtV 3R lR ltV 3R lR ltRV 3R lR lltR	 tV 3R
 lR lt	R t
R tR tR tRtV tR# )OtpProtocolz&An implementation of the OTP protocol.c                r   )r   otp_connection)r   r   r   r   r   OtpProtocol.__annotate__   s     	 	} 	r   c                s    Wn         V P                  4       p\        P                  ! VR ,          4      V n        V P                  ^ ,          ^8X  d    V P                  ^R4       R# R#   \         d     R# i ; i):rO   r?   NNs3   ccccccccccccccccccccccccccccccccccccccccccccccccccc)
connection_receiver	   Z
from_bytesversionsend_and_receiver
   )r#   rW   rS   rT   r   __init__ZOtpProtocol.__init__   sf    ())&+6<<?a%%dI6   ( s   A( (A76A7c                s   < V ^8  d   QhRR/# )r   r   Nr   r   r   r   r   rX      s        t  r   c                s:    V P                   P                  4        R # r8   )rY   closer"   r$   r   r^   ZOtpProtocol.close   s    r   Nc          
      sx   < V ^8  d   QhRS[ RS[R,          RS[R,          RS[S[ .R3,          R,          RS[/# )r   rL   r'   Neventon_keepaliver   )r0   r   r   r   r   r   r   r   rX      sU       dl t|	
 ud{+d2 
r   c                s   T;'       g    RP                  \        R4      p\        V4      \        8  d   \        R4      hV'       g   R p\	        W4      p\
        P                  \        P                  RVP                  4       4       T P                  V P                  V4      T;'       g    \        4       V4      p\
        P                  \        P                  RVP                  4       4       V# )a  Sends a command to the YubiKey, and reads the response.

If the command results in a configuration update, the programming sequence
number is verified and the updated status bytes are returned.

:param slot:  The slot to send to.
:param data:  The data payload to send.
:param state: Optional CommandState for listening for user presence requirement
    and for cancelling a command.
:return: Response data (including CRC) in the case of data, or an updated status
    struct.
r       zPayload too large for HID framec                 s    R # r8   r   )Zxr$   r   Z<lambda>Z.OtpProtocol.send_and_receive.<locals>.<lambda>   s    Tr   zSEND: %szRECV: %s)ZljustSLOT_DATA_SIZEr4   rF   rN   loggerZlogr   ZTRAFFICZhex_read_frame_send_framer   )r#   rL   r'   r_   r`   rM   Zframeresponses   &&&&&   r   r\   ZOtpProtocol.send_and_receive   s    & ;;3%%ne<w<.(>??)Ld,

9$$j%))+>##U#U%5%5eg|
 	

9$$j(,,.Ar   c                s    V P                   P                  4       p\        V4      \        8w  d    \	        R \        V4       R\         R24      hV# )z#Incorrect feature report size (was z, expected Z))rY   r%   r4   FEATURE_RPT_SIZE	Exception)r#   rS   rB   r   rZ   ZOtpProtocol._receive   sS    ((*v;**5c&k] C,-Q0  r   c                r   r   r   r   r   r   r   rX      s     % %U %r   c                s(    V P                  4       ^R # )zReceive status bytes from YubiKey.

:return: Status bytes (first 3 bytes are the firmware version).
:raises IOException: in case of communication error.
)rZ   r"   r$   r   read_statusZOtpProtocol.read_status   s     }}q$$r   c                s    \        ^4       F<  pV P                  4       \        ,          \        ,          ^ 8X  d    R# \	        R4       K>  	  \        R4      h)z:Sleep for up to ~1s waiting for the WRITE flag to be unsetNg?z6Timeout waiting for YubiKey to become ready to receive)r3   rZ   FEATURE_RPT_DATA_SIZESLOT_WRITE_FLAGr   rh   )r#   Z_rB   r   _await_ready_to_writeZ!OtpProtocol._await_ready_to_write   sA    rA 56HQN$K  PQQr   c                sJ   V P                  4       \        ,          p^ pV'       d~   VR\         V\        R r\        WC4      '       dQ   V\        P
                  ! R^V,          4      ,          pV P                  4        V P                  P                  V4       V^,          pK  V# )zSends a 70 byte frameNz>B)	rZ   rP   rk   rI   rJ   rK   rm   rY   r)   )r#   Zbufprog_seqrH   rS   s   &&   r   re   ZOtpProtocol._send_frame   s    ==?#9:445s;P;Q7RCF((&++dD3J77**,$$V,1HCr   c                s   Rp^ pRp  V P                  4       pV\        ,          pV\        ,          ^ 8w  dR   WX\        ,          8X  d   WGR\         ,          pV^,          pK[  ^ V\        ,          8X  d   V P	                  4        V# K  V^ 8X  dH   V'       d   \        R4      h\        Wq4      '       d   V^R # V'       d   \        R4      h\        R4      hV\        ,          ^ 8w  d   V! \        4       RpRp	MV! \        4       R	p	VP                  V	4      '       g   EK  V P	                  4        \        R
4      h  \         d(    \        P                  R4       T P	                  4        h i ; i)zReads one framer   FTNzIncomplete transferzTimed out waiting for touchzNo datag?g{Gz?zCommand cancelled by Eventz"Keyboard interrupt, reset state...ri   )rZ   rk   RESP_PENDING_FLAGSEQUENCE_MASK_reset_staterh   rU   r   r
   RESP_TIMEOUT_WAIT_FLAGSTATUS_UPNEEDEDSTATUS_PROCESSINGZwaitZKeyboardInterruptrc   Zdebug)
r#   rn   r_   r`   rf   rH   Zneeds_touchrS   Zstatus_byteZtimeouts
   &&&&      r   rd   ZOtpProtocol._read_frame   sO   %	$%:;"339]:; +A,A$BBq{]:;))+' < !A%'(=>>-f??%a|+$*+HII29==#&<<B$_5&*"%$%67"&zz'**))+*+GHH  	LL=>	s+   A>D= D=  D= 9D= AD= "D= =2E/c                sd    V P                   P                  RP                  \        R4      4       R# )z'Reset the state of YubiKey from readings   ra   N)rY   r)   Zrjustrg   r"   r$   r   rq   ZOtpProtocol._reset_state  s     W]]+;UCDr   )rY   r[   )NNN)r   r   r   r   r   r]   r^   r\   rZ   rj   rm   re   rd   rq   r   r+   r,   r-   r   rV   rV      sS     0	 	    B% %R+ZE Er   rV   )/r*   ZloggingrJ   Z	threadingr   Ztimer   Ztypingr   Zyubikit.loggingr   r=   r   r   r   r   r	   Z	getLoggerr   rc   r@   r
   ZABCMetar   r9   r7   r;   rD   rG   rg   rk   rb   Z
FRAME_SIZEro   rl   rr   ZDUMMY_REPORT_WRITErp   rP   rQ   rR   rt   rs   rI   rN   rU   rV   r   r   r   <module>ru      s   8       % L L			8	$ %9< 9	.J#++ 	. 	2U
  (1, a
     )  (
R	HE HEr   