@ -10,7 +10,9 @@ class Storage:
self . lib = c . cdll . LoadLibrary ( fname )
self . lib = c . cdll . LoadLibrary ( fname )
self . flash_size = c . cast ( self . lib . FLASH_SIZE , c . POINTER ( c . c_uint32 ) ) [ 0 ]
self . flash_size = c . cast ( self . lib . FLASH_SIZE , c . POINTER ( c . c_uint32 ) ) [ 0 ]
self . flash_buffer = c . create_string_buffer ( self . flash_size )
self . flash_buffer = c . create_string_buffer ( self . flash_size )
c . cast ( self . lib . FLASH_BUFFER , c . POINTER ( c . c_void_p ) ) [ 0 ] = c . addressof ( self . flash_buffer )
c . cast ( self . lib . FLASH_BUFFER , c . POINTER ( c . c_void_p ) ) [ 0 ] = c . addressof (
self . flash_buffer
)
def init ( self , salt : bytes ) - > None :
def init ( self , salt : bytes ) - > None :
self . lib . storage_init ( 0 , salt , c . c_uint16 ( len ( salt ) ) )
self . lib . storage_init ( 0 , salt , c . c_uint16 ( len ( salt ) ) )
@ -31,14 +33,18 @@ class Storage:
return self . lib . storage_get_pin_rem ( )
return self . lib . storage_get_pin_rem ( )
def change_pin ( self , oldpin : int , newpin : int ) - > bool :
def change_pin ( self , oldpin : int , newpin : int ) - > bool :
return sectrue == self . lib . storage_change_pin ( c . c_uint32 ( oldpin ) , c . c_uint32 ( newpin ) )
return sectrue == self . lib . storage_change_pin (
c . c_uint32 ( oldpin ) , c . c_uint32 ( newpin )
)
def get ( self , key : int ) - > bytes :
def get ( self , key : int ) - > bytes :
val_len = c . c_uint16 ( )
val_len = c . c_uint16 ( )
if sectrue != self . lib . storage_get ( c . c_uint16 ( key ) , None , 0 , c . byref ( val_len ) ) :
if sectrue != self . lib . storage_get ( c . c_uint16 ( key ) , None , 0 , c . byref ( val_len ) ) :
raise RuntimeError ( " Failed to find key in storage. " )
raise RuntimeError ( " Failed to find key in storage. " )
s = c . create_string_buffer ( val_len . value )
s = c . create_string_buffer ( val_len . value )
if sectrue != self . lib . storage_get ( c . c_uint16 ( key ) , s , val_len , c . byref ( val_len ) ) :
if sectrue != self . lib . storage_get (
c . c_uint16 ( key ) , s , val_len , c . byref ( val_len )
) :
raise RuntimeError ( " Failed to get value from storage. " )
raise RuntimeError ( " Failed to get value from storage. " )
return s . raw
return s . raw
@ -47,7 +53,9 @@ class Storage:
raise RuntimeError ( " Failed to set value in storage. " )
raise RuntimeError ( " Failed to set value in storage. " )
def set_counter ( self , key : int , count : int ) - > bool :
def set_counter ( self , key : int , count : int ) - > bool :
return sectrue == self . lib . storage_set_counter ( c . c_uint16 ( key ) , c . c_uint32 ( count ) )
return sectrue == self . lib . storage_set_counter (
c . c_uint16 ( key ) , c . c_uint32 ( count )
)
def next_counter ( self , key : int ) - > int :
def next_counter ( self , key : int ) - > int :
count = c . c_uint32 ( )
count = c . c_uint32 ( )
@ -61,7 +69,10 @@ class Storage:
def _dump ( self ) - > bytes :
def _dump ( self ) - > bytes :
# return just sectors 4 and 16 of the whole flash
# return just sectors 4 and 16 of the whole flash
return [ self . flash_buffer [ 0x010000 : 0x010000 + 0x10000 ] , self . flash_buffer [ 0x110000 : 0x110000 + 0x10000 ] ]
return [
self . flash_buffer [ 0x010000 : 0x010000 + 0x10000 ] ,
self . flash_buffer [ 0x110000 : 0x110000 + 0x10000 ] ,
]
def _get_flash_buffer ( self ) - > bytes :
def _get_flash_buffer ( self ) - > bytes :
return bytes ( self . flash_buffer )
return bytes ( self . flash_buffer )