From cc4275694be97f365096eefb9b7513b41bcfdcd4 Mon Sep 17 00:00:00 2001 From: Joey Castillo Date: Thu, 12 May 2022 10:41:04 -0400 Subject: WIP: totp watch face read from file --- movement/lib/base32/base32.c | 221 ++++++++++++++++++++++++++ movement/lib/base32/base32.h | 66 ++++++++ movement/make/Makefile | 2 + movement/movement_config.h | 1 + movement/watch_faces/complication/totp_face.c | 69 ++++++-- movement/watch_faces/complication/totp_face.h | 5 +- 6 files changed, 348 insertions(+), 16 deletions(-) create mode 100644 movement/lib/base32/base32.c create mode 100644 movement/lib/base32/base32.h (limited to 'movement') diff --git a/movement/lib/base32/base32.c b/movement/lib/base32/base32.c new file mode 100644 index 00000000..cd978e29 --- /dev/null +++ b/movement/lib/base32/base32.c @@ -0,0 +1,221 @@ +/** + * base32 (de)coder implementation as specified by RFC4648. + * + * Copyright (c) 2010 Adrien Kunysz + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + **/ + +#include // assert() +#include // CHAR_BIT + +#include "base32.h" + +/** + * Let this be a sequence of plain data before encoding: + * + * 01234567 01234567 01234567 01234567 01234567 + * +--------+--------+--------+--------+--------+ + * |< 0 >< 1| >< 2 ><|.3 >< 4.|>< 5 ><.|6 >< 7 >| + * +--------+--------+--------+--------+--------+ + * + * There are 5 octets of 8 bits each in each sequence. + * There are 8 blocks of 5 bits each in each sequence. + * + * You probably want to refer to that graph when reading the algorithms in this + * file. We use "octet" instead of "byte" intentionnaly as we really work with + * 8 bits quantities. This implementation will probably not work properly on + * systems that don't have exactly 8 bits per (unsigned) char. + **/ + +static size_t min(size_t x, size_t y) +{ + return x < y ? x : y; +} + +static const unsigned char PADDING_CHAR = '='; + +/** + * Pad the given buffer with len padding characters. + */ +static void pad(unsigned char *buf, int len) +{ + for (int i = 0; i < len; i++) + buf[i] = PADDING_CHAR; +} + +/** + * This convert a 5 bits value into a base32 character. + * Only the 5 least significant bits are used. + */ +static unsigned char encode_char(unsigned char c) +{ + static unsigned char base32[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; + return base32[c & 0x1F]; // 0001 1111 +} + +/** + * Decode given character into a 5 bits value. + * Returns -1 iff the argument given was an invalid base32 character + * or a padding character. + */ +static int decode_char(unsigned char c) +{ + char retval = -1; + + if (c >= 'A' && c <= 'Z') + retval = c - 'A'; + if (c >= '2' && c <= '7') + retval = c - '2' + 26; + + assert(retval == -1 || ((retval & 0x1F) == retval)); + + return retval; +} + +/** + * Given a block id between 0 and 7 inclusive, this will return the index of + * the octet in which this block starts. For example, given 3 it will return 1 + * because block 3 starts in octet 1: + * + * +--------+--------+ + * | ......<|.3 >....| + * +--------+--------+ + * octet 1 | octet 2 + */ +static int get_octet(int block) +{ + assert(block >= 0 && block < 8); + return (block*5) / 8; +} + +/** + * Given a block id between 0 and 7 inclusive, this will return how many bits + * we can drop at the end of the octet in which this block starts. + * For example, given block 0 it will return 3 because there are 3 bits + * we don't care about at the end: + * + * +--------+- + * |< 0 >...| + * +--------+- + * + * Given block 1, it will return -2 because there + * are actually two bits missing to have a complete block: + * + * +--------+- + * |.....< 1|.. + * +--------+- + **/ +static int get_offset(int block) +{ + assert(block >= 0 && block < 8); + return (8 - 5 - (5*block) % 8); +} + +/** + * Like "b >> offset" but it will do the right thing with negative offset. + * We need this as bitwise shifting by a negative offset is undefined + * behavior. + */ +static unsigned char shift_right(unsigned char byte, char offset) +{ + if (offset > 0) + return byte >> offset; + else + return byte << -offset; +} + +static unsigned char shift_left(unsigned char byte, char offset) +{ + return shift_right(byte, - offset); +} + +/** + * Encode a sequence. A sequence is no longer than 5 octets by definition. + * Thus passing a length greater than 5 to this function is an error. Encoding + * sequences shorter than 5 octets is supported and padding will be added to the + * output as per the specification. + */ +static void encode_sequence(const unsigned char *plain, int len, unsigned char *coded) +{ + assert(CHAR_BIT == 8); // not sure this would work otherwise + assert(len >= 0 && len <= 5); + + for (int block = 0; block < 8; block++) { + int octet = get_octet(block); // figure out which octet this block starts in + int junk = get_offset(block); // how many bits do we drop from this octet? + + if (octet >= len) { // we hit the end of the buffer + pad(&coded[block], 8 - block); + return; + } + + unsigned char c = shift_right(plain[octet], junk); // first part + + if (junk < 0 // is there a second part? + && octet < len - 1) // is there still something to read? + { + c |= shift_right(plain[octet+1], 8 + junk); + } + coded[block] = encode_char(c); + } +} + +void base32_encode(const unsigned char *plain, size_t len, unsigned char *coded) +{ + // All the hard work is done in encode_sequence(), + // here we just need to feed it the data sequence by sequence. + for (size_t i = 0, j = 0; i < len; i += 5, j += 8) { + encode_sequence(&plain[i], min(len - i, 5), &coded[j]); + } +} + +static int decode_sequence(const unsigned char *coded, unsigned char *plain) +{ + assert(CHAR_BIT == 8); + assert(coded && plain); + + plain[0] = 0; + for (int block = 0; block < 8; block++) { + int offset = get_offset(block); + int octet = get_octet(block); + + int c = decode_char(coded[block]); + if (c < 0) // invalid char, stop here + return octet; + + plain[octet] |= shift_left(c, offset); + if (offset < 0) { // does this block overflows to next octet? + assert(octet < 4); + plain[octet+1] = shift_left(c, 8 + offset); + } + } + return 5; +} + +size_t base32_decode(const unsigned char *coded, unsigned char *plain) +{ + size_t written = 0; + for (size_t i = 0, j = 0; ; i += 8, j += 5) { + int n = decode_sequence(&coded[i], &plain[j]); + written += n; + if (n < 5) + return written; + } +} diff --git a/movement/lib/base32/base32.h b/movement/lib/base32/base32.h new file mode 100644 index 00000000..abf2d034 --- /dev/null +++ b/movement/lib/base32/base32.h @@ -0,0 +1,66 @@ +/** + * base32 (de)coder implementation as specified by RFC4648. + * + * Copyright (c) 2010 Adrien Kunysz + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + **/ + +#ifndef __BASE32_H_ +#define __BASE32_H_ + +#include // size_t + +/** + * Returns the length of the output buffer required to encode len bytes of + * data into base32. This is a macro to allow users to define buffer size at + * compilation time. + */ +#define BASE32_LEN(len) (((len)/5)*8 + ((len) % 5 ? 8 : 0)) + +/** + * Returns the length of the output buffer required to decode a base32 string + * of len characters. Please note that len must be a multiple of 8 as per + * definition of a base32 string. This is a macro to allow users to define + * buffer size at compilation time. + */ +#define UNBASE32_LEN(len) (((len)/8)*5) + +/** + * Encode the data pointed to by plain into base32 and store the + * result at the address pointed to by coded. The "coded" argument + * must point to a location that has enough available space + * to store the whole coded string. The resulting string will only + * contain characters from the [A-Z2-7=] set. The "len" arguments + * define how many bytes will be read from the "plain" buffer. + **/ +void base32_encode(const unsigned char *plain, size_t len, unsigned char *coded); + +/** + * Decode the null terminated string pointed to by coded and write + * the decoded data into the location pointed to by plain. The + * "plain" argument must point to a location that has enough available + * space to store the whole decoded string. + * Returns the length of the decoded string. This may be less than + * expected due to padding. If an invalid base32 character is found + * in the coded string, decoding will stop at that point. + **/ +size_t base32_decode(const unsigned char *coded, unsigned char *plain); + +#endif diff --git a/movement/make/Makefile b/movement/make/Makefile index 1c880fc2..cf18c37c 100755 --- a/movement/make/Makefile +++ b/movement/make/Makefile @@ -21,6 +21,7 @@ INCLUDES += \ -I../lib/sunriset/ \ -I../lib/vsop87/ \ -I../lib/astrolib/ \ + -I../lib/base32/ \ # If you add any other source files you wish to compile, add them after ../app.c # Note that you will need to add a backslash at the end of any line you wish to continue, i.e. @@ -34,6 +35,7 @@ SRCS += \ ../lib/sunriset/sunriset.c \ ../lib/vsop87/vsop87a_milli.c \ ../lib/astrolib/astrolib.c \ + ../lib/base32/base32.c \ ../../littlefs/lfs.c \ ../../littlefs/lfs_util.c \ ../movement.c \ diff --git a/movement/movement_config.h b/movement/movement_config.h index 94456776..d320f2ec 100644 --- a/movement/movement_config.h +++ b/movement/movement_config.h @@ -29,6 +29,7 @@ const watch_face_t watch_faces[] = { simple_clock_face, + totp_face, world_clock_face, sunrise_sunset_face, moon_phase_face, diff --git a/movement/watch_faces/complication/totp_face.c b/movement/watch_faces/complication/totp_face.c index 468e6a8c..53236e1f 100644 --- a/movement/watch_faces/complication/totp_face.c +++ b/movement/watch_faces/complication/totp_face.c @@ -8,28 +8,57 @@ #include "watch.h" #include "watch_utility.h" #include "TOTP.h" +#include "base32.h" +#include "filesystem.h" // test key: JBSWY3DPEHPK3PXP // Use https://cryptii.com/pipes/base32-to-hex to convert base32 to hex // 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 uint8_t last_filename_index = 0; static const uint32_t TIMESTEP = 30; +static void read_totp_file(totp_state_t *totp_state) { + totp_state->valid = false; + char filename[12]; + sprintf(filename, "totp%d.txt", totp_state->my_filename_index); + int32_t size = filesystem_get_file_size(filename); + if (size > 0) { + char *buf = malloc(size + 1); + filesystem_read_file(filename, buf, size); + char *site_name = strtok(buf, " \n"); + char *hmac_string = strtok(NULL, " \n"); + if (strlen(hmac_string) == 16) { + memcpy(totp_state->site_name, site_name, min(strlen(site_name), 2)); + base32_decode(hmac_string, totp_state->hmac_key); + totp_state->valid = true; + } + free(buf); + } +} + void totp_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr) { (void) settings; (void) watch_face_index; - if (*context_ptr == NULL) *context_ptr = malloc(sizeof(totp_state_t)); - TOTP(hmacKey, sizeof(hmacKey), TIMESTEP); + if (*context_ptr == NULL) { + *context_ptr = malloc(sizeof(totp_state_t)); + (((totp_state_t *)(*context_ptr))->my_filename_index) = ++last_filename_index; + read_totp_file((totp_state_t *)(*context_ptr)); + } } void totp_face_activate(movement_settings_t *settings, void *context) { (void) settings; - memset(context, 0, sizeof(totp_state_t)); totp_state_t *totp_state = (totp_state_t *)context; - 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); + if (!totp_state->valid) { + read_totp_file(totp_state); + } + if (totp_state->valid) { + TOTP(totp_state->hmac_key, sizeof(totp_state->hmac_key), TIMESTEP); + totp_state->steps = 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->current_code = getCodeFromTimestamp(totp_state->timestamp); + } } bool totp_face_loop(movement_event_t event, movement_settings_t *settings, void *context) { @@ -45,15 +74,25 @@ bool totp_face_loop(movement_event_t event, movement_settings_t *settings, void totp_state->timestamp++; // fall through case EVENT_ACTIVATE: - result = div(totp_state->timestamp, TIMESTEP); - if (result.quot != totp_state->steps) { - totp_state->current_code = getCodeFromTimestamp(totp_state->timestamp); - totp_state->steps = result.quot; + if (totp_state->valid) { + result = div(totp_state->timestamp, TIMESTEP); + if (result.quot != totp_state->steps) { + totp_state->current_code = getCodeFromTimestamp(totp_state->timestamp); + totp_state->steps = result.quot; + } + valid_for = TIMESTEP - result.rem; + sprintf(buf, "%s%2d%06lu", totp_state->site_name, valid_for, totp_state->current_code); + watch_display_string(buf, 0); + } else { + watch_display_string("2f noCode", 0); + read_totp_file(totp_state); + if (totp_state->valid) { + TOTP(totp_state->hmac_key, sizeof(totp_state->hmac_key), TIMESTEP); + totp_state->steps = 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->current_code = getCodeFromTimestamp(totp_state->timestamp); + } } - valid_for = TIMESTEP - result.rem; - sprintf(buf, "2f%2d%06lu", valid_for, totp_state->current_code); - - watch_display_string(buf, 0); break; case EVENT_MODE_BUTTON_UP: movement_move_to_next_face(); diff --git a/movement/watch_faces/complication/totp_face.h b/movement/watch_faces/complication/totp_face.h index dfa4a6d0..c1376f94 100644 --- a/movement/watch_faces/complication/totp_face.h +++ b/movement/watch_faces/complication/totp_face.h @@ -4,10 +4,13 @@ #include "movement.h" typedef struct { + char site_name[3]; + uint8_t hmac_key[10]; uint32_t timestamp; uint8_t steps; uint32_t current_code; - + uint8_t my_filename_index; + bool valid; } totp_state_t; void totp_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr); -- cgit v1.2.3