Alexander Kopachov 2 лет назад
Родитель
Сommit
1020c3c4ac

+ 6 - 10
services/config/token_info_iterator.c

@@ -68,7 +68,9 @@ static bool seek_to_token(size_t token_index, TokenInfoIteratorContext* context)
         direction = StreamDirectionBackward;
         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) {
     if(token_index_diff != 0) {
         long i = 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_offset = stream_tell(stream);
         context->last_seek_index = token_index;
         context->last_seek_index = token_index;
-    } else {
-        if(!stream_seek(stream, context->last_seek_offset, StreamOffsetFromStart)) {
-            return false;
-        }
     }
     }
 
 
     return true;
     return true;
@@ -450,6 +448,7 @@ bool totp_token_info_iterator_go_to(TokenInfoIteratorContext* context, size_t to
 
 
         if(flipper_format_read_string(
         if(flipper_format_read_string(
                context->config_file, TOTP_CONFIG_KEY_TOKEN_SECRET, temp_str)) {
                context->config_file, TOTP_CONFIG_KEY_TOKEN_SECRET, temp_str)) {
+            
             if(token_info_set_secret(
             if(token_info_set_secret(
                    tokenInfo,
                    tokenInfo,
                    furi_string_get_cstr(temp_str),
                    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;
     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;
         tokenInfo->algo = SHA1;
     }
     }
 
 

+ 0 - 1
services/hmac/hmac_common.h

@@ -1,5 +1,4 @@
 #include <string.h>
 #include <string.h>
-#include "sha256.h"
 #include "memxor.h"
 #include "memxor.h"
 
 
 #define IPAD 0x36
 #define IPAD 0x36

+ 1 - 0
services/hmac/hmac_sha256.c

@@ -15,6 +15,7 @@
    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
 
 
 #include "hmac_sha256.h"
 #include "hmac_sha256.h"
+#include "sha256.h"
 
 
 #define GL_HMAC_NAME 256
 #define GL_HMAC_NAME 256
 #define GL_HMAC_BLOCKSIZE 64
 #define GL_HMAC_BLOCKSIZE 64

+ 3 - 5
services/hmac/sha1.c

@@ -27,6 +27,8 @@
 #include <stdint.h>
 #include <stdint.h>
 #include <string.h>
 #include <string.h>
 
 
+#include "sha_pad_buffer.h"
+
 #ifdef WORDS_BIGENDIAN
 #ifdef WORDS_BIGENDIAN
 #define SWAP(n) (n)
 #define SWAP(n) (n)
 #else
 #else
@@ -34,10 +36,6 @@
 #define SWAP(n) swap_uint32(n)
 #define SWAP(n) swap_uint32(n)
 #endif
 #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
 /* 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
    initialize it to the start constants of the SHA1 algorithm.  This
    must be called before using hash in the call to sha1_hash.  */
    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 - 2] = SWAP((ctx->total[1] << 3) | (ctx->total[0] >> 29));
     ctx->buffer[size - 1] = SWAP(ctx->total[0] << 3);
     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.  */
     /* Process last bytes.  */
     sha1_process_block(ctx->buffer, size * 4, ctx);
     sha1_process_block(ctx->buffer, size * 4, ctx);

+ 2 - 5
services/hmac/sha256.c

@@ -25,6 +25,7 @@
 
 
 #include <stdint.h>
 #include <stdint.h>
 #include <string.h>
 #include <string.h>
+#include "sha_pad_buffer.h"
 
 
 #ifdef WORDS_BIGENDIAN
 #ifdef WORDS_BIGENDIAN
 #define SWAP(n) (n)
 #define SWAP(n) (n)
@@ -33,10 +34,6 @@
 #define SWAP(n) swap_uint32(n)
 #define SWAP(n) swap_uint32(n)
 #endif
 #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
   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
   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 - 2], SWAP((ctx->total[1] << 3) | (ctx->total[0] >> 29)));
     set_uint32((char*)&ctx->buffer[size - 1], SWAP(ctx->total[0] << 3));
     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.  */
     /* Process last bytes.  */
     sha256_process_block(ctx->buffer, size * 4, ctx);
     sha256_process_block(ctx->buffer, size * 4, ctx);

+ 2 - 5
services/hmac/sha512.c

@@ -27,13 +27,10 @@
 #include <string.h>
 #include <string.h>
 
 
 #include "byteswap.h"
 #include "byteswap.h"
+#include "sha_pad_buffer.h"
 
 
 #define SWAP(n) swap_uint64(n)
 #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
   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
   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))));
         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)));
     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.  */
     /* Process last bytes.  */
     sha512_process_block(ctx->buffer, size * 8, ctx);
     sha512_process_block(ctx->buffer, size * 8, ctx);

+ 11 - 0
services/hmac/sha_pad_buffer.c

@@ -0,0 +1,11 @@
+#include "sha_pad_buffer.h"
+#include <string.h>
+
+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);
+        }
+    }
+}

+ 4 - 0
services/hmac/sha_pad_buffer.h

@@ -0,0 +1,4 @@
+#include <stddef.h>
+#include <stdint.h>
+
+void sha_pad_buffer(uint8_t* buffer, size_t size);

+ 22 - 0
types/token_info.c

@@ -117,6 +117,28 @@ bool token_info_set_algo_from_str(TokenInfo* token_info, const FuriString* str)
     return false;
     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) {
 char* token_info_get_algo_as_cstr(const TokenInfo* token_info) {
     switch(token_info->algo) {
     switch(token_info->algo) {
     case SHA1:
     case SHA1:

+ 10 - 2
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
  * @brief Encrypts & sets plain token secret to the given instance of \c TokenInfo
  * @param token_info instance where secret should be updated
  * @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 token_secret_length plain token secret length
  * @param plain_token_secret_encoding plain token secret encoding
  * @param plain_token_secret_encoding plain token secret encoding
  * @param iv initialization vecor (IV) to be used for encryption
  * @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
  * @brief Sets token hashing algorithm from \c str value
  * @param token_info instance whichs token hashing algorithm should be updated
  * @param token_info instance whichs token hashing algorithm should be updated
  * @param str desired token algorithm
  * @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);
 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
  * @brief Gets token hahsing algorithm name as C-string
  * @param token_info instance which token hahsing algorithm name should be returned
  * @param token_info instance which token hahsing algorithm name should be returned