add support for multiple TOTP keys
This commit is contained in:
parent
7fc4395298
commit
8b3c02ace5
@ -1,7 +1,3 @@
|
|||||||
/**
|
|
||||||
* TODO:
|
|
||||||
* - Support for multiple codes
|
|
||||||
*/
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "totp_face.h"
|
#include "totp_face.h"
|
||||||
@ -9,25 +5,38 @@
|
|||||||
#include "watch_utility.h"
|
#include "watch_utility.h"
|
||||||
#include "TOTP.h"
|
#include "TOTP.h"
|
||||||
|
|
||||||
// test key: JBSWY3DPEHPK3PXP
|
|
||||||
// Use https://cryptii.com/pipes/base32-to-hex to convert base32 to hex
|
// Use https://cryptii.com/pipes/base32-to-hex to convert base32 to hex
|
||||||
// Use https://totp.danhersam.com/ to generate test codes for verification
|
// Use https://totp.danhersam.com/ to generate test codes for verification
|
||||||
static uint8_t hmacKey[] = {0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x21, 0xde, 0xad, 0xbe, 0xef}; // Secret key
|
|
||||||
|
|
||||||
|
static const uint8_t num_keys = 2;
|
||||||
static const uint32_t TIMESTEP = 30;
|
static uint8_t keys[] = {
|
||||||
|
0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x21, 0xde, 0xad, 0xbe, 0xef, // 1 - JBSWY3DPEHPK3PXP
|
||||||
|
0x5c, 0x0d, 0x27, 0x6b, 0x6d, 0x9a, 0x01, 0x22, 0x20, 0x4f // 2 - E9M348K0ADIDFBC2
|
||||||
|
};
|
||||||
|
static const uint8_t key_sizes[] = {
|
||||||
|
10,
|
||||||
|
10
|
||||||
|
};
|
||||||
|
static const uint32_t timesteps[] = {
|
||||||
|
30,
|
||||||
|
30
|
||||||
|
};
|
||||||
|
static const char labels[][2] = {
|
||||||
|
{ 'a', 'b' },
|
||||||
|
{ 'c', 'd' }
|
||||||
|
};
|
||||||
|
|
||||||
void totp_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr) {
|
void totp_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr) {
|
||||||
(void) settings;
|
(void) settings;
|
||||||
(void) watch_face_index;
|
(void) watch_face_index;
|
||||||
if (*context_ptr == NULL) *context_ptr = malloc(sizeof(totp_state_t));
|
if (*context_ptr == NULL) *context_ptr = malloc(sizeof(totp_state_t));
|
||||||
TOTP(hmacKey, sizeof(hmacKey), TIMESTEP);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void totp_face_activate(movement_settings_t *settings, void *context) {
|
void totp_face_activate(movement_settings_t *settings, void *context) {
|
||||||
(void) settings;
|
(void) settings;
|
||||||
memset(context, 0, sizeof(totp_state_t));
|
memset(context, 0, sizeof(totp_state_t));
|
||||||
totp_state_t *totp_state = (totp_state_t *)context;
|
totp_state_t *totp_state = (totp_state_t *)context;
|
||||||
|
TOTP(keys, key_sizes[0], timesteps[0]);
|
||||||
totp_state->timestamp = watch_utility_date_time_to_unix_time(watch_rtc_get_date_time(), movement_timezone_offsets[settings->bit.time_zone] * 60);
|
totp_state->timestamp = watch_utility_date_time_to_unix_time(watch_rtc_get_date_time(), movement_timezone_offsets[settings->bit.time_zone] * 60);
|
||||||
totp_state->current_code = getCodeFromTimestamp(totp_state->timestamp);
|
totp_state->current_code = getCodeFromTimestamp(totp_state->timestamp);
|
||||||
}
|
}
|
||||||
@ -39,19 +48,20 @@ bool totp_face_loop(movement_event_t event, movement_settings_t *settings, void
|
|||||||
char buf[14];
|
char buf[14];
|
||||||
uint8_t valid_for;
|
uint8_t valid_for;
|
||||||
div_t result;
|
div_t result;
|
||||||
|
uint8_t index = totp_state->current_index;
|
||||||
|
|
||||||
switch (event.event_type) {
|
switch (event.event_type) {
|
||||||
case EVENT_TICK:
|
case EVENT_TICK:
|
||||||
totp_state->timestamp++;
|
totp_state->timestamp++;
|
||||||
// fall through
|
// fall through
|
||||||
case EVENT_ACTIVATE:
|
case EVENT_ACTIVATE:
|
||||||
result = div(totp_state->timestamp, TIMESTEP);
|
result = div(totp_state->timestamp, timesteps[index]);
|
||||||
if (result.quot != totp_state->steps) {
|
if (result.quot != totp_state->steps) {
|
||||||
totp_state->current_code = getCodeFromTimestamp(totp_state->timestamp);
|
totp_state->current_code = getCodeFromTimestamp(totp_state->timestamp);
|
||||||
totp_state->steps = result.quot;
|
totp_state->steps = result.quot;
|
||||||
}
|
}
|
||||||
valid_for = TIMESTEP - result.rem;
|
valid_for = timesteps[index] - result.rem;
|
||||||
sprintf(buf, "2f%2d%06lu", valid_for, totp_state->current_code);
|
sprintf(buf, "%c%c%2d%06lu", labels[index][0], labels[index][1], valid_for, totp_state->current_code);
|
||||||
|
|
||||||
watch_display_string(buf, 0);
|
watch_display_string(buf, 0);
|
||||||
break;
|
break;
|
||||||
@ -64,8 +74,18 @@ bool totp_face_loop(movement_event_t event, movement_settings_t *settings, void
|
|||||||
case EVENT_TIMEOUT:
|
case EVENT_TIMEOUT:
|
||||||
movement_move_to_face(0);
|
movement_move_to_face(0);
|
||||||
break;
|
break;
|
||||||
case EVENT_ALARM_BUTTON_DOWN:
|
|
||||||
case EVENT_ALARM_BUTTON_UP:
|
case EVENT_ALARM_BUTTON_UP:
|
||||||
|
if (index + 1 < num_keys) {
|
||||||
|
totp_state->current_key_offset += key_sizes[index];
|
||||||
|
totp_state->current_index++;
|
||||||
|
} else {
|
||||||
|
// wrap around to first key
|
||||||
|
totp_state->current_key_offset = 0;
|
||||||
|
totp_state->current_index = 0;
|
||||||
|
}
|
||||||
|
TOTP(keys + totp_state->current_key_offset, key_sizes[totp_state->current_index], timesteps[totp_state->current_index]);
|
||||||
|
break;
|
||||||
|
case EVENT_ALARM_BUTTON_DOWN:
|
||||||
case EVENT_ALARM_LONG_PRESS:
|
case EVENT_ALARM_LONG_PRESS:
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
@ -7,7 +7,8 @@ typedef struct {
|
|||||||
uint32_t timestamp;
|
uint32_t timestamp;
|
||||||
uint8_t steps;
|
uint8_t steps;
|
||||||
uint32_t current_code;
|
uint32_t current_code;
|
||||||
|
uint8_t current_index;
|
||||||
|
uint8_t current_key_offset;
|
||||||
} totp_state_t;
|
} totp_state_t;
|
||||||
|
|
||||||
void totp_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr);
|
void totp_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user