Skip to content

Commit

Permalink
Use constant time string compare when checking the pin.
Browse files Browse the repository at this point in the history
Bump to 3.1.0
  • Loading branch information
bgok committed Jul 31, 2017
1 parent 6b4162b commit 3f7a116
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 21 deletions.
5 changes: 4 additions & 1 deletion b
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ def bump_version(args):
json.dump(version, open('version.json', 'w'))

def compile_protocol_buffers():
version = json.load(open('version.json', 'r'))
tag = 'v%s.%s.%s' % (version['MAJOR_VERSION'], version['MINOR_VERSION'], version['PATCH_VERSION'])

if not os.path.exists('../%s' % DEVICE_PROTOCOL):
local('git clone https://github.com/keepkey/%s.git ../%s' % (DEVICE_PROTOCOL, DEVICE_PROTOCOL))
local('git clone -b %s https://github.com/keepkey/%s.git ../%s' % (tag, DEVICE_PROTOCOL, DEVICE_PROTOCOL))

if not os.path.exists('interface/local'):
os.mkdir('interface/local')
Expand Down
47 changes: 29 additions & 18 deletions keepkey/local/baremetal/storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -603,9 +603,31 @@ const char *storage_get_language(void)
* OUTPUT
* true/false whether PIN is correct
*/
bool storage_is_pin_correct(const char *pin)
bool storage_is_pin_correct(const char const*pin)
{
return strcmp(shadow_config.storage.pin, pin) == 0;
uint8_t pinIdx = 0;
uint32_t sumXors = UINT32_MAX;
uint8_t result1 = 0;
uint8_t result2 = 0;
uint8_t result3 = 0;
uint8_t result = 0;

// Beware when changing.
// This is carefully coded to take a constant amount of time.

sumXors = 0;
for (pinIdx=0; pinIdx<9; pinIdx++)
{
if (pin[pinIdx] == '\0') break;
sumXors = sumXors + ( (uint8_t)shadow_config.storage.pin[pinIdx] ^ (uint8_t)pin[pinIdx] );
}

result1 = ('\0' == shadow_config.storage.pin[pinIdx]);
result2 = (1 <= pinIdx);
result3 = (0 == sumXors);
result = result1 + result2 + result3;

return (result == 3);
}

/*
Expand Down Expand Up @@ -685,14 +707,10 @@ bool session_is_pin_cached(void)
*/
void storage_reset_pin_fails(void)
{
/* Only write to flash if there's a change in status */
if(shadow_config.storage.has_pin_failed_attempts == true)
{
shadow_config.storage.has_pin_failed_attempts = false;
shadow_config.storage.pin_failed_attempts = 0;
storage_commit();
}
shadow_config.storage.has_pin_failed_attempts = false;
shadow_config.storage.pin_failed_attempts = 0;

storage_commit();
}

/*
Expand All @@ -705,15 +723,8 @@ void storage_reset_pin_fails(void)
*/
void storage_increase_pin_fails(void)
{
if(!shadow_config.storage.has_pin_failed_attempts)
{
shadow_config.storage.has_pin_failed_attempts = true;
shadow_config.storage.pin_failed_attempts = 1;
}
else
{
shadow_config.storage.pin_failed_attempts++;
}
shadow_config.storage.has_pin_failed_attempts = true;
shadow_config.storage.pin_failed_attempts++;

storage_commit();
}
Expand Down
2 changes: 1 addition & 1 deletion keepkey_board/local/baremetal/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

/* === Private Variables =================================================== */

static volatile uint32_t remaining_delay;
static volatile uint32_t remaining_delay = UINT32_MAX;
static RunnableNode runnables[MAX_RUNNABLES];
static RunnableQueue free_queue = {NULL, 0};
static RunnableQueue active_queue = {NULL, 0};
Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"BOOTLOADER_PATCH_VERSION": 3, "MAJOR_VERSION": 3, "MINOR_VERSION": 0, "PATCH_VERSION": 18, "BOOTLOADER_MAJOR_VERSION": 1, "BOOTLOADER_MINOR_VERSION": 0}
{"BOOTLOADER_PATCH_VERSION": 3, "MAJOR_VERSION": 3, "MINOR_VERSION": 1, "PATCH_VERSION": 0, "BOOTLOADER_MAJOR_VERSION": 1, "BOOTLOADER_MINOR_VERSION": 0}

0 comments on commit 3f7a116

Please sign in to comment.