From 1020c3c4ac4e1e7e9305567118f04735e611d58f Mon Sep 17 00:00:00 2001 From: Alexander Kopachov Date: Fri, 28 Apr 2023 09:55:34 +0200 Subject: [PATCH] Refactoring (#144) --- services/config/token_info_iterator.c | 16 ++++++---------- services/hmac/hmac_common.h | 1 - services/hmac/hmac_sha256.c | 1 + services/hmac/sha1.c | 8 +++----- services/hmac/sha256.c | 7 ++----- services/hmac/sha512.c | 7 ++----- services/hmac/sha_pad_buffer.c | 11 +++++++++++ services/hmac/sha_pad_buffer.h | 4 ++++ types/token_info.c | 22 ++++++++++++++++++++++ types/token_info.h | 12 ++++++++++-- 10 files changed, 61 insertions(+), 28 deletions(-) create mode 100644 services/hmac/sha_pad_buffer.c create mode 100644 services/hmac/sha_pad_buffer.h diff --git a/services/config/token_info_iterator.c b/services/config/token_info_iterator.c index 9b7dd5550cf..86936bf33d4 100644 --- a/services/config/token_info_iterator.c +++ b/services/config/token_info_iterator.c @@ -68,7 +68,9 @@ static bool seek_to_token(size_t token_index, TokenInfoIteratorContext* context) direction = StreamDirectionBackward; } - stream_seek(stream, context->last_seek_offset, StreamOffsetFromStart); + if (!stream_seek(stream, context->last_seek_offset, StreamOffsetFromStart)) { + return false; + } if(token_index_diff != 0) { long i = 0; @@ -89,10 +91,6 @@ static bool seek_to_token(size_t token_index, TokenInfoIteratorContext* context) context->last_seek_offset = stream_tell(stream); context->last_seek_index = token_index; - } else { - if(!stream_seek(stream, context->last_seek_offset, StreamOffsetFromStart)) { - return false; - } } return true; @@ -450,6 +448,7 @@ bool totp_token_info_iterator_go_to(TokenInfoIteratorContext* context, size_t to if(flipper_format_read_string( context->config_file, TOTP_CONFIG_KEY_TOKEN_SECRET, temp_str)) { + if(token_info_set_secret( tokenInfo, furi_string_get_cstr(temp_str), @@ -495,11 +494,8 @@ bool totp_token_info_iterator_go_to(TokenInfoIteratorContext* context, size_t to } uint32_t temp_data32; - if(flipper_format_read_uint32( - context->config_file, TOTP_CONFIG_KEY_TOKEN_ALGO, &temp_data32, 1) && - temp_data32 <= STEAM) { - tokenInfo->algo = (TokenHashAlgo)temp_data32; - } else { + if(!flipper_format_read_uint32(context->config_file, TOTP_CONFIG_KEY_TOKEN_ALGO, &temp_data32, 1)|| + !token_info_set_algo_from_int(tokenInfo, temp_data32)) { tokenInfo->algo = SHA1; } diff --git a/services/hmac/hmac_common.h b/services/hmac/hmac_common.h index 0cd56ed999e..3499cb800b8 100644 --- a/services/hmac/hmac_common.h +++ b/services/hmac/hmac_common.h @@ -1,5 +1,4 @@ #include -#include "sha256.h" #include "memxor.h" #define IPAD 0x36 diff --git a/services/hmac/hmac_sha256.c b/services/hmac/hmac_sha256.c index c51f24b4d79..00ac2a177ce 100644 --- a/services/hmac/hmac_sha256.c +++ b/services/hmac/hmac_sha256.c @@ -15,6 +15,7 @@ along with this program. If not, see . */ #include "hmac_sha256.h" +#include "sha256.h" #define GL_HMAC_NAME 256 #define GL_HMAC_BLOCKSIZE 64 diff --git a/services/hmac/sha1.c b/services/hmac/sha1.c index ecf22fc972f..29f22e3c30c 100644 --- a/services/hmac/sha1.c +++ b/services/hmac/sha1.c @@ -27,6 +27,8 @@ #include #include +#include "sha_pad_buffer.h" + #ifdef WORDS_BIGENDIAN #define SWAP(n) (n) #else @@ -34,10 +36,6 @@ #define SWAP(n) swap_uint32(n) #endif -/* This array contains the bytes used to pad the buffer to the next - 64-byte boundary. (RFC 1321, 3.1: Step 1) */ -static const unsigned char fillbuf[64] = {0x80, 0 /* , 0, 0, ... */}; - /* Take a pointer to a 160 bit block of data (five 32 bit ints) and initialize it to the start constants of the SHA1 algorithm. This must be called before using hash in the call to sha1_hash. */ @@ -87,7 +85,7 @@ void* sha1_finish_ctx(struct sha1_ctx* ctx, void* resbuf) { ctx->buffer[size - 2] = SWAP((ctx->total[1] << 3) | (ctx->total[0] >> 29)); ctx->buffer[size - 1] = SWAP(ctx->total[0] << 3); - memcpy(&((char*)ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes); + sha_pad_buffer(&((uint8_t*)ctx->buffer)[bytes], (size - 2) * 4 - bytes); /* Process last bytes. */ sha1_process_block(ctx->buffer, size * 4, ctx); diff --git a/services/hmac/sha256.c b/services/hmac/sha256.c index 89ca67c2bec..09ba272e74f 100644 --- a/services/hmac/sha256.c +++ b/services/hmac/sha256.c @@ -25,6 +25,7 @@ #include #include +#include "sha_pad_buffer.h" #ifdef WORDS_BIGENDIAN #define SWAP(n) (n) @@ -33,10 +34,6 @@ #define SWAP(n) swap_uint32(n) #endif -/* This array contains the bytes used to pad the buffer to the next - 64-byte boundary. */ -static const unsigned char fillbuf[64] = {0x80, 0 /* , 0, 0, ... */}; - /* Takes a pointer to a 256 bit block of data (eight 32 bit ints) and initializes it to the start constants of the SHA256 algorithm. This @@ -91,7 +88,7 @@ static void sha256_conclude_ctx(struct sha256_ctx* ctx) { set_uint32((char*)&ctx->buffer[size - 2], SWAP((ctx->total[1] << 3) | (ctx->total[0] >> 29))); set_uint32((char*)&ctx->buffer[size - 1], SWAP(ctx->total[0] << 3)); - memcpy(&((char*)ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes); + sha_pad_buffer(&((uint8_t*)ctx->buffer)[bytes], (size - 2) * 4 - bytes); /* Process last bytes. */ sha256_process_block(ctx->buffer, size * 4, ctx); diff --git a/services/hmac/sha512.c b/services/hmac/sha512.c index b56dd0f2eba..ffe2864fbd9 100644 --- a/services/hmac/sha512.c +++ b/services/hmac/sha512.c @@ -27,13 +27,10 @@ #include #include "byteswap.h" +#include "sha_pad_buffer.h" #define SWAP(n) swap_uint64(n) -/* This array contains the bytes used to pad the buffer to the next - 128-byte boundary. */ -static const unsigned char fillbuf[128] = {0x80, 0 /* , 0, 0, ... */}; - /* Takes a pointer to a 512 bit block of data (eight 64 bit ints) and initializes it to the start constants of the SHA512 algorithm. This @@ -90,7 +87,7 @@ static void sha512_conclude_ctx(struct sha512_ctx* ctx) { SWAP(u64or(u64shl(ctx->total[1], 3), u64shr(ctx->total[0], 61)))); set_uint64((char*)&ctx->buffer[size - 1], SWAP(u64shl(ctx->total[0], 3))); - memcpy(&((char*)ctx->buffer)[bytes], fillbuf, (size - 2) * 8 - bytes); + sha_pad_buffer(&((uint8_t*)ctx->buffer)[bytes], (size - 2) * 8 - bytes); /* Process last bytes. */ sha512_process_block(ctx->buffer, size * 8, ctx); diff --git a/services/hmac/sha_pad_buffer.c b/services/hmac/sha_pad_buffer.c new file mode 100644 index 00000000000..618178de857 --- /dev/null +++ b/services/hmac/sha_pad_buffer.c @@ -0,0 +1,11 @@ +#include "sha_pad_buffer.h" +#include + +void sha_pad_buffer(uint8_t* buffer, size_t size) { + if (size > 0) { + buffer[0] = 0x80; + if (size > 1) { + memset(&buffer[1], 0, size - 1); + } + } +} \ No newline at end of file diff --git a/services/hmac/sha_pad_buffer.h b/services/hmac/sha_pad_buffer.h new file mode 100644 index 00000000000..7dba40fa968 --- /dev/null +++ b/services/hmac/sha_pad_buffer.h @@ -0,0 +1,4 @@ +#include +#include + +void sha_pad_buffer(uint8_t* buffer, size_t size); \ No newline at end of file diff --git a/types/token_info.c b/types/token_info.c index 2f108033bea..d6052ddb9b4 100644 --- a/types/token_info.c +++ b/types/token_info.c @@ -117,6 +117,28 @@ bool token_info_set_algo_from_str(TokenInfo* token_info, const FuriString* str) return false; } +bool token_info_set_algo_from_int(TokenInfo* token_info, uint8_t algo_code) { + switch (algo_code) + { + case SHA1: + token_info->algo = SHA1; + break; + case SHA256: + token_info->algo = SHA256; + break; + case SHA512: + token_info->algo = SHA512; + break; + case STEAM: + token_info->algo = STEAM; + break; + default: + return false; + } + + return true; +} + char* token_info_get_algo_as_cstr(const TokenInfo* token_info) { switch(token_info->algo) { case SHA1: diff --git a/types/token_info.h b/types/token_info.h index 138ad32b126..0d73dd06158 100644 --- a/types/token_info.h +++ b/types/token_info.h @@ -168,7 +168,7 @@ void token_info_free(TokenInfo* token_info); /** * @brief Encrypts & sets plain token secret to the given instance of \c TokenInfo * @param token_info instance where secret should be updated - * @param base32_token_secret plain token secret in Base32 format + * @param plain_token_secret plain token secret * @param token_secret_length plain token secret length * @param plain_token_secret_encoding plain token secret encoding * @param iv initialization vecor (IV) to be used for encryption @@ -201,10 +201,18 @@ bool token_info_set_duration_from_int(TokenInfo* token_info, uint8_t duration); * @brief Sets token hashing algorithm from \c str value * @param token_info instance whichs token hashing algorithm should be updated * @param str desired token algorithm - * @return \c true if token hahsing algorithm has been updated; \c false otherwise + * @return \c true if token hashing algorithm has been updated; \c false otherwise */ bool token_info_set_algo_from_str(TokenInfo* token_info, const FuriString* str); +/** + * @brief Sets token hashing algorithm from \c algo_code code + * @param token_info instance whichs token hashing algorithm should be updated + * @param algo_code desired token algorithm code + * @return \c true if token hashing algorithm has been updated; \c false otherwise + */ +bool token_info_set_algo_from_int(TokenInfo* token_info, uint8_t algo_code); + /** * @brief Gets token hahsing algorithm name as C-string * @param token_info instance which token hahsing algorithm name should be returned