<s> """<STR_LIT>""" <EOL> import struct <EOL> __all__ = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> import pyhsm . defines <EOL> import pyhsm . exception <EOL> from pyhsm . cmd import YHSM_Cmd <EOL> YHSM_AEAD_File_Marker = '<STR_LIT>' <EOL> YHSM_AEAD_CRLF_File_Marker = YHSM_AEAD_File_Marker [ : - <NUM_LIT:1> ] + '<STR_LIT:\r\n>' <EOL> class YHSM_AEAD_Cmd ( YHSM_Cmd ) : <EOL> """<STR_LIT>""" <EOL> nonce = '<STR_LIT>' <EOL> key_handle = <NUM_LIT:0> <EOL> status = <NUM_LIT:0> <EOL> response = None <EOL> def __repr__ ( self ) : <EOL> if self . executed : <EOL> return '<STR_LIT>' % ( <EOL> self . __class__ . __name__ , <EOL> hex ( id ( self ) ) , <EOL> self . nonce . encode ( '<STR_LIT>' ) , <EOL> self . key_handle , <EOL> pyhsm . defines . status2str ( self . status ) <EOL> ) <EOL> else : <EOL> return '<STR_LIT>' % ( <EOL> self . __class__ . __name__ , <EOL> hex ( id ( self ) ) <EOL> ) <EOL> def parse_result ( self , data ) : <EOL> """<STR_LIT>""" <EOL> nonce , key_handle , self . status , num_bytes = struct . unpack_from ( "<STR_LIT>" % ( pyhsm . defines . YSM_AEAD_NONCE_SIZE ) , data , <NUM_LIT:0> ) <EOL> pyhsm . util . validate_cmd_response_hex ( '<STR_LIT>' , key_handle , self . key_handle ) <EOL> if self . status == pyhsm . defines . YSM_STATUS_OK : <EOL> pyhsm . util . validate_cmd_response_nonce ( nonce , self . nonce ) <EOL> offset = pyhsm . defines . YSM_AEAD_NONCE_SIZE + <NUM_LIT:6> <EOL> aead = data [ offset : offset + num_bytes ] <EOL> self . response = YHSM_GeneratedAEAD ( nonce , key_handle , aead ) <EOL> return self . response <EOL> else : <EOL> raise pyhsm . exception . YHSM_CommandFailed ( pyhsm . defines . cmd2str ( self . command ) , self . status ) <EOL> class YHSM_Cmd_AEAD_Generate ( YHSM_AEAD_Cmd ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , stick , nonce , key_handle , data ) : <EOL> self . nonce = pyhsm . util . input_validate_nonce ( nonce , pad = True ) <EOL> self . key_handle = pyhsm . util . input_validate_key_handle ( key_handle ) <EOL> self . data = pyhsm . util . input_validate_yubikey_secret ( data ) <EOL> fmt = "<STR_LIT>" % ( pyhsm . defines . YSM_AEAD_NONCE_SIZE , len ( self . data ) ) <EOL> packed = struct . pack ( fmt , nonce , key_handle , len ( self . data ) , self . data ) <EOL> YHSM_AEAD_Cmd . __init__ ( self , stick , pyhsm . defines . YSM_AEAD_GENERATE , packed ) <EOL> class YHSM_Cmd_AEAD_Random_Generate ( YHSM_AEAD_Cmd ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , stick , nonce , key_handle , num_bytes ) : <EOL> self . nonce = pyhsm . util . input_validate_nonce ( nonce , pad = True ) <EOL> self . key_handle = pyhsm . util . input_validate_key_handle ( key_handle ) <EOL> self . num_bytes = pyhsm . util . input_validate_int ( num_bytes , '<STR_LIT>' ) <EOL> fmt = "<STR_LIT>" % ( pyhsm . defines . YSM_AEAD_NONCE_SIZE ) <EOL> packed = struct . pack ( fmt , nonce , key_handle , num_bytes ) <EOL> YHSM_AEAD_Cmd . __init__ ( self , stick , pyhsm . defines . YSM_RANDOM_AEAD_GENERATE , packed ) <EOL> class YHSM_Cmd_AEAD_Buffer_Generate ( YHSM_AEAD_Cmd ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , stick , nonce , key_handle ) : <EOL> self . nonce = pyhsm . util . input_validate_nonce ( nonce , pad = True ) <EOL> self . key_handle = pyhsm . util . input_validate_key_handle ( key_handle ) <EOL> packed = struct . pack ( "<STR_LIT>" % ( pyhsm . defines . YSM_AEAD_NONCE_SIZE ) , self . nonce , self . key_handle ) <EOL> YHSM_AEAD_Cmd . __init__ ( self , stick , pyhsm . defines . YSM_BUFFER_AEAD_GENERATE , packed ) <EOL> class YHSM_Cmd_AEAD_Decrypt_Cmp ( YHSM_Cmd ) : <EOL> """<STR_LIT>""" <EOL> status = None <EOL> def __init__ ( self , stick , nonce , key_handle , aead , cleartext ) : <EOL> aead = pyhsm . util . input_validate_aead ( aead ) <EOL> expected_ct_len = len ( aead ) - pyhsm . defines . YSM_AEAD_MAC_SIZE <EOL> cleartext = pyhsm . util . input_validate_str ( cleartext , '<STR_LIT>' , exact_len = expected_ct_len ) <EOL> self . nonce = pyhsm . util . input_validate_nonce ( nonce , pad = True ) <EOL> self . key_handle = pyhsm . util . input_validate_key_handle ( key_handle ) <EOL> data = cleartext + aead <EOL> if len ( data ) > pyhsm . defines . YSM_MAX_PKT_SIZE - <NUM_LIT:10> : <EOL> raise pyhsm . exception . YHSM_InputTooLong ( <EOL> '<STR_LIT>' , pyhsm . defines . YSM_MAX_PKT_SIZE - <NUM_LIT:10> , len ( data ) ) <EOL> fmt = "<STR_LIT>" % ( pyhsm . defines . YSM_AEAD_NONCE_SIZE , len ( data ) ) <EOL> packed = struct . pack ( fmt , self . nonce , key_handle , len ( data ) , data ) <EOL> YHSM_Cmd . __init__ ( self , stick , pyhsm . defines . YSM_AEAD_DECRYPT_CMP , packed ) <EOL> def parse_result ( self , data ) : <EOL> fmt = "<STR_LIT>" % ( pyhsm . defines . YSM_AEAD_NONCE_SIZE ) <EOL> nonce , key_handle , self . status = struct . unpack ( fmt , data ) <EOL> pyhsm . util . validate_cmd_response_str ( '<STR_LIT>' , nonce , self . nonce ) <EOL> pyhsm . util . validate_cmd_response_hex ( '<STR_LIT>' , key_handle , self . key_handle ) <EOL> if self . status == pyhsm . defines . YSM_STATUS_OK : <EOL> return True <EOL> if self . status == pyhsm . defines . YSM_MISMATCH : <EOL> return False <EOL> else : <EOL> raise pyhsm . exception . YHSM_CommandFailed ( pyhsm . defines . cmd2str ( self . command ) , self . status ) <EOL> class YHSM_GeneratedAEAD ( ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , nonce , key_handle , aead ) : <EOL> self . nonce = nonce <EOL> self . key_handle = key_handle <EOL> self . data = aead <EOL> def __repr__ ( self ) : <EOL> nonce_str = "<STR_LIT:None>" <EOL> if self . nonce is not None : <EOL> nonce_str = self . nonce . encode ( '<STR_LIT>' ) <EOL> return '<STR_LIT>' % ( <EOL> self . __class__ . __name__ , <EOL> hex ( id ( self ) ) , <EOL> nonce_str , <EOL> self . key_handle , <EOL> len ( self . data ) <EOL> ) <EOL> def save ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> aead_f = open ( filename , "<STR_LIT:wb>" ) <EOL> fmt = "<STR_LIT>" % ( pyhsm . defines . YSM_AEAD_NONCE_SIZE , len ( self . data ) ) <EOL> version = <NUM_LIT:1> <EOL> packed = struct . pack ( fmt , version , self . key_handle , self . nonce , self . data ) <EOL> aead_f . write ( YHSM_AEAD_File_Marker + packed ) <EOL> aead_f . close ( ) <EOL> def load ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> aead_f = open ( filename , "<STR_LIT:rb>" ) <EOL> buf = aead_f . read ( <NUM_LIT> ) <EOL> if buf . startswith ( YHSM_AEAD_CRLF_File_Marker ) : <EOL> buf = YHSM_AEAD_File_Marker + buf [ len ( YHSM_AEAD_CRLF_File_Marker ) : ] <EOL> if buf . startswith ( YHSM_AEAD_File_Marker ) : <EOL> if buf [ len ( YHSM_AEAD_File_Marker ) ] == chr ( <NUM_LIT:1> ) : <EOL> fmt = "<STR_LIT>" % ( pyhsm . defines . YSM_AEAD_NONCE_SIZE ) <EOL> self . key_handle , self . nonce = struct . unpack_from ( fmt , buf , len ( YHSM_AEAD_File_Marker ) + <NUM_LIT:1> ) <EOL> self . data = buf [ len ( YHSM_AEAD_File_Marker ) + <NUM_LIT:1> + struct . calcsize ( fmt ) : ] <EOL> else : <EOL> raise pyhsm . exception . YHSM_Error ( '<STR_LIT>' ) <EOL> else : <EOL> self . data = buf [ : pyhsm . defines . YSM_MAX_KEY_SIZE + pyhsm . defines . YSM_BLOCK_SIZE ] <EOL> aead_f . close ( ) <EOL> class YHSM_YubiKeySecret ( ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , key , uid ) : <EOL> self . key = pyhsm . util . input_validate_str ( key , '<STR_LIT:key>' , exact_len = pyhsm . defines . KEY_SIZE ) <EOL> self . uid = pyhsm . util . input_validate_str ( uid , '<STR_LIT>' , max_len = pyhsm . defines . UID_SIZE ) <EOL> def pack ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . key + self . uid . ljust ( pyhsm . defines . UID_SIZE , chr ( <NUM_LIT:0> ) ) </s>
