Skip to content

Commit 1020c3c

Browse files
authored
Refactoring (#144)
1 parent 4d3e208 commit 1020c3c

File tree

10 files changed

+61
-28
lines changed

10 files changed

+61
-28
lines changed

services/config/token_info_iterator.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ static bool seek_to_token(size_t token_index, TokenInfoIteratorContext* context)
6868
direction = StreamDirectionBackward;
6969
}
7070

71-
stream_seek(stream, context->last_seek_offset, StreamOffsetFromStart);
71+
if (!stream_seek(stream, context->last_seek_offset, StreamOffsetFromStart)) {
72+
return false;
73+
}
7274

7375
if(token_index_diff != 0) {
7476
long i = 0;
@@ -89,10 +91,6 @@ static bool seek_to_token(size_t token_index, TokenInfoIteratorContext* context)
8991

9092
context->last_seek_offset = stream_tell(stream);
9193
context->last_seek_index = token_index;
92-
} else {
93-
if(!stream_seek(stream, context->last_seek_offset, StreamOffsetFromStart)) {
94-
return false;
95-
}
9694
}
9795

9896
return true;
@@ -450,6 +448,7 @@ bool totp_token_info_iterator_go_to(TokenInfoIteratorContext* context, size_t to
450448

451449
if(flipper_format_read_string(
452450
context->config_file, TOTP_CONFIG_KEY_TOKEN_SECRET, temp_str)) {
451+
453452
if(token_info_set_secret(
454453
tokenInfo,
455454
furi_string_get_cstr(temp_str),
@@ -495,11 +494,8 @@ bool totp_token_info_iterator_go_to(TokenInfoIteratorContext* context, size_t to
495494
}
496495

497496
uint32_t temp_data32;
498-
if(flipper_format_read_uint32(
499-
context->config_file, TOTP_CONFIG_KEY_TOKEN_ALGO, &temp_data32, 1) &&
500-
temp_data32 <= STEAM) {
501-
tokenInfo->algo = (TokenHashAlgo)temp_data32;
502-
} else {
497+
if(!flipper_format_read_uint32(context->config_file, TOTP_CONFIG_KEY_TOKEN_ALGO, &temp_data32, 1)||
498+
!token_info_set_algo_from_int(tokenInfo, temp_data32)) {
503499
tokenInfo->algo = SHA1;
504500
}
505501

services/hmac/hmac_common.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include <string.h>
2-
#include "sha256.h"
32
#include "memxor.h"
43

54
#define IPAD 0x36

services/hmac/hmac_sha256.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
along with this program. If not, see <https://www.gnu.org/licenses/>. */
1616

1717
#include "hmac_sha256.h"
18+
#include "sha256.h"
1819

1920
#define GL_HMAC_NAME 256
2021
#define GL_HMAC_BLOCKSIZE 64

services/hmac/sha1.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,15 @@
2727
#include <stdint.h>
2828
#include <string.h>
2929

30+
#include "sha_pad_buffer.h"
31+
3032
#ifdef WORDS_BIGENDIAN
3133
#define SWAP(n) (n)
3234
#else
3335
#include "byteswap.h"
3436
#define SWAP(n) swap_uint32(n)
3537
#endif
3638

37-
/* This array contains the bytes used to pad the buffer to the next
38-
64-byte boundary. (RFC 1321, 3.1: Step 1) */
39-
static const unsigned char fillbuf[64] = {0x80, 0 /* , 0, 0, ... */};
40-
4139
/* Take a pointer to a 160 bit block of data (five 32 bit ints) and
4240
initialize it to the start constants of the SHA1 algorithm. This
4341
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) {
8785
ctx->buffer[size - 2] = SWAP((ctx->total[1] << 3) | (ctx->total[0] >> 29));
8886
ctx->buffer[size - 1] = SWAP(ctx->total[0] << 3);
8987

90-
memcpy(&((char*)ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes);
88+
sha_pad_buffer(&((uint8_t*)ctx->buffer)[bytes], (size - 2) * 4 - bytes);
9189

9290
/* Process last bytes. */
9391
sha1_process_block(ctx->buffer, size * 4, ctx);

services/hmac/sha256.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include <stdint.h>
2727
#include <string.h>
28+
#include "sha_pad_buffer.h"
2829

2930
#ifdef WORDS_BIGENDIAN
3031
#define SWAP(n) (n)
@@ -33,10 +34,6 @@
3334
#define SWAP(n) swap_uint32(n)
3435
#endif
3536

36-
/* This array contains the bytes used to pad the buffer to the next
37-
64-byte boundary. */
38-
static const unsigned char fillbuf[64] = {0x80, 0 /* , 0, 0, ... */};
39-
4037
/*
4138
Takes a pointer to a 256 bit block of data (eight 32 bit ints) and
4239
initializes it to the start constants of the SHA256 algorithm. This
@@ -91,7 +88,7 @@ static void sha256_conclude_ctx(struct sha256_ctx* ctx) {
9188
set_uint32((char*)&ctx->buffer[size - 2], SWAP((ctx->total[1] << 3) | (ctx->total[0] >> 29)));
9289
set_uint32((char*)&ctx->buffer[size - 1], SWAP(ctx->total[0] << 3));
9390

94-
memcpy(&((char*)ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes);
91+
sha_pad_buffer(&((uint8_t*)ctx->buffer)[bytes], (size - 2) * 4 - bytes);
9592

9693
/* Process last bytes. */
9794
sha256_process_block(ctx->buffer, size * 4, ctx);

services/hmac/sha512.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,10 @@
2727
#include <string.h>
2828

2929
#include "byteswap.h"
30+
#include "sha_pad_buffer.h"
3031

3132
#define SWAP(n) swap_uint64(n)
3233

33-
/* This array contains the bytes used to pad the buffer to the next
34-
128-byte boundary. */
35-
static const unsigned char fillbuf[128] = {0x80, 0 /* , 0, 0, ... */};
36-
3734
/*
3835
Takes a pointer to a 512 bit block of data (eight 64 bit ints) and
3936
initializes it to the start constants of the SHA512 algorithm. This
@@ -90,7 +87,7 @@ static void sha512_conclude_ctx(struct sha512_ctx* ctx) {
9087
SWAP(u64or(u64shl(ctx->total[1], 3), u64shr(ctx->total[0], 61))));
9188
set_uint64((char*)&ctx->buffer[size - 1], SWAP(u64shl(ctx->total[0], 3)));
9289

93-
memcpy(&((char*)ctx->buffer)[bytes], fillbuf, (size - 2) * 8 - bytes);
90+
sha_pad_buffer(&((uint8_t*)ctx->buffer)[bytes], (size - 2) * 8 - bytes);
9491

9592
/* Process last bytes. */
9693
sha512_process_block(ctx->buffer, size * 8, ctx);

services/hmac/sha_pad_buffer.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "sha_pad_buffer.h"
2+
#include <string.h>
3+
4+
void sha_pad_buffer(uint8_t* buffer, size_t size) {
5+
if (size > 0) {
6+
buffer[0] = 0x80;
7+
if (size > 1) {
8+
memset(&buffer[1], 0, size - 1);
9+
}
10+
}
11+
}

services/hmac/sha_pad_buffer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include <stddef.h>
2+
#include <stdint.h>
3+
4+
void sha_pad_buffer(uint8_t* buffer, size_t size);

types/token_info.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,28 @@ bool token_info_set_algo_from_str(TokenInfo* token_info, const FuriString* str)
117117
return false;
118118
}
119119

120+
bool token_info_set_algo_from_int(TokenInfo* token_info, uint8_t algo_code) {
121+
switch (algo_code)
122+
{
123+
case SHA1:
124+
token_info->algo = SHA1;
125+
break;
126+
case SHA256:
127+
token_info->algo = SHA256;
128+
break;
129+
case SHA512:
130+
token_info->algo = SHA512;
131+
break;
132+
case STEAM:
133+
token_info->algo = STEAM;
134+
break;
135+
default:
136+
return false;
137+
}
138+
139+
return true;
140+
}
141+
120142
char* token_info_get_algo_as_cstr(const TokenInfo* token_info) {
121143
switch(token_info->algo) {
122144
case SHA1:

types/token_info.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ void token_info_free(TokenInfo* token_info);
168168
/**
169169
* @brief Encrypts & sets plain token secret to the given instance of \c TokenInfo
170170
* @param token_info instance where secret should be updated
171-
* @param base32_token_secret plain token secret in Base32 format
171+
* @param plain_token_secret plain token secret
172172
* @param token_secret_length plain token secret length
173173
* @param plain_token_secret_encoding plain token secret encoding
174174
* @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);
201201
* @brief Sets token hashing algorithm from \c str value
202202
* @param token_info instance whichs token hashing algorithm should be updated
203203
* @param str desired token algorithm
204-
* @return \c true if token hahsing algorithm has been updated; \c false otherwise
204+
* @return \c true if token hashing algorithm has been updated; \c false otherwise
205205
*/
206206
bool token_info_set_algo_from_str(TokenInfo* token_info, const FuriString* str);
207207

208+
/**
209+
* @brief Sets token hashing algorithm from \c algo_code code
210+
* @param token_info instance whichs token hashing algorithm should be updated
211+
* @param algo_code desired token algorithm code
212+
* @return \c true if token hashing algorithm has been updated; \c false otherwise
213+
*/
214+
bool token_info_set_algo_from_int(TokenInfo* token_info, uint8_t algo_code);
215+
208216
/**
209217
* @brief Gets token hahsing algorithm name as C-string
210218
* @param token_info instance which token hahsing algorithm name should be returned

0 commit comments

Comments
 (0)