Skip to content

Commit

Permalink
Refactoring (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
akopachov authored Nov 7, 2022
1 parent 64d33e0 commit eb60a9c
Show file tree
Hide file tree
Showing 21 changed files with 32 additions and 194 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/pvsstudio.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
name: PVS-Studio build analysis
on: workflow_dispatch
on:
workflow_dispatch:
pull_request:
types: [opened, synchronize, reopened]
jobs:
build-analyze:
runs-on: ubuntu-latest
Expand Down
3 changes: 0 additions & 3 deletions .github/workflows/sonarcloud.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ on:
workflow_dispatch:
pull_request:
types: [opened, synchronize, reopened]
push:
branches:
- master
jobs:
build:
name: Build and analyze
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ It is like [Google Authenticator](https://play.google.com/store/apps/details?id=
## SAST Tools

[PVS-Studio](https://pvs-studio.com/en/pvs-studio/?utm_source=github&utm_medium=organic&utm_campaign=open_source) - static analyzer for C, C++, C#, and Java code.

[SonarCloud](https://www.sonarsource.com/products/sonarcloud/) - cloud-based code quality and security service

## Support
Expand Down
2 changes: 1 addition & 1 deletion flipperzero-firmware_unleashed
2 changes: 2 additions & 0 deletions totp/scenes/add_new_token/totp_scene_add_new_token.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ bool totp_scene_add_new_token_handle_event(PluginEvent* const event, PluginState
}
break;
}
default: break;
}
break;
case InputKeyBack:
Expand All @@ -294,6 +295,7 @@ bool totp_scene_add_new_token_handle_event(PluginEvent* const event, PluginState
totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
}
break;
default: break;
}

return true;
Expand Down
1 change: 1 addition & 0 deletions totp/scenes/app_settings/totp_app_settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ bool totp_scene_app_settings_handle_event(
}
break;
}
default: break;
}

return true;
Expand Down
3 changes: 2 additions & 1 deletion totp/scenes/authenticate/totp_scene_authenticate.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void totp_scene_authenticate_render(Canvas* const canvas, PluginState* plugin_st
const uint8_t PIN_ASTERISK_RADIUS = 3;
const uint8_t PIN_ASTERISK_STEP = (PIN_ASTERISK_RADIUS << 1) + 2;
if(scene_state->code_length > 0) {
uint8_t left_start_x = (scene_state->code_length - 1) * PIN_ASTERISK_STEP >> 1;
uint8_t left_start_x = ((scene_state->code_length - 1) * PIN_ASTERISK_STEP) >> 1;
for(uint8_t i = 0; i < scene_state->code_length; i++) {
canvas_draw_disc(
canvas,
Expand Down Expand Up @@ -152,6 +152,7 @@ bool totp_scene_authenticate_handle_event(
scene_state->code_length--;
}
break;
default: break;
}

return true;
Expand Down
2 changes: 2 additions & 0 deletions totp/scenes/generate_token/totp_scene_generate_token.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ TOTP_ALGO get_totp_algo_impl(TokenHashAlgo algo) {
return TOTP_ALGO_SHA256;
case SHA512:
return TOTP_ALGO_SHA512;
default: break;
}

return NULL;
Expand Down Expand Up @@ -299,6 +300,7 @@ bool totp_scene_generate_token_handle_event(
break;
case InputKeyBack:
break;
default: break;
}

return true;
Expand Down
4 changes: 4 additions & 0 deletions totp/scenes/scene_director.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ void totp_scene_director_activate_scene(
break;
case TotpSceneNone:
break;
default: break;
}

plugin_state->current_scene = scene;
Expand All @@ -55,6 +56,7 @@ void totp_scene_director_deactivate_active_scene(PluginState* const plugin_state
break;
case TotpSceneNone:
break;
default: break;
}
}

Expand Down Expand Up @@ -85,6 +87,7 @@ void totp_scene_director_render(Canvas* const canvas, PluginState* const plugin_
break;
case TotpSceneNone:
break;
default: break;
}
}

Expand Down Expand Up @@ -116,6 +119,7 @@ bool totp_scene_director_handle_event(PluginEvent* const event, PluginState* con
break;
case TotpSceneNone:
break;
default: break;
}

return processing;
Expand Down
2 changes: 2 additions & 0 deletions totp/scenes/token_menu/totp_scene_token_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ bool totp_scene_token_menu_handle_event(const PluginEvent* const event, PluginSt
}
break;
}
default: break;
}
break;
case InputKeyBack: {
Expand All @@ -184,6 +185,7 @@ bool totp_scene_token_menu_handle_event(const PluginEvent* const event, PluginSt
}
break;
}
default: break;
}

return true;
Expand Down
1 change: 1 addition & 0 deletions totp/services/cli/commands/add/add.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ static bool token_info_set_digits_from_str(TokenInfo* token_info, const FuriStri
case '8':
token_info->digits = TOTP_8_DIGITS;
return true;
default: break;
}

return false;
Expand Down
2 changes: 2 additions & 0 deletions totp/services/cli/commands/list/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ static char* get_algo_as_cstr(TokenHashAlgo algo) {
return TOTP_CONFIG_TOKEN_ALGO_SHA256_NAME;
case SHA512:
return TOTP_CONFIG_TOKEN_ALGO_SHA512_NAME;
default: break;
}

return "UNKNOWN";
Expand All @@ -24,6 +25,7 @@ static uint8_t get_digits_as_int(TokenDigitsCount digits) {
return 6;
case TOTP_8_DIGITS:
return 8;
default: break;
}

return 6;
Expand Down
3 changes: 3 additions & 0 deletions totp/services/config/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ static uint8_t token_info_get_digits_as_int(const TokenInfo* token_info) {
return 6;
case TOTP_8_DIGITS:
return 8;
default: break;
}

return 6;
Expand All @@ -29,6 +30,7 @@ static void token_info_set_digits_from_int(TokenInfo* token_info, uint8_t digits
case 8:
token_info->digits = TOTP_8_DIGITS;
break;
default: break;
}
}

Expand All @@ -40,6 +42,7 @@ static char* token_info_get_algo_as_cstr(const TokenInfo* token_info) {
return TOTP_CONFIG_TOKEN_ALGO_SHA256_NAME;
case SHA512:
return TOTP_CONFIG_TOKEN_ALGO_SHA512_NAME;
default: break;
}

return NULL;
Expand Down
7 changes: 0 additions & 7 deletions totp/services/hmac/sha1.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
*/

/* Specification. */
#if HAVE_OPENSSL_SHA1
#define GL_OPENSSL_INLINE _GL_EXTERN_INLINE
#endif
#include "sha1.h"

#include <stdint.h>
Expand All @@ -37,8 +34,6 @@
#define SWAP(n) swap_uint32(n)
#endif

#if !HAVE_OPENSSL_SHA1

/* 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, ... */};
Expand Down Expand Up @@ -266,8 +261,6 @@ void sha1_process_block(const void* buffer, size_t len, struct sha1_ctx* ctx) {
}
}

#endif

/*
* Hey Emacs!
* Local Variables:
Expand Down
21 changes: 0 additions & 21 deletions totp/services/hmac/sha1.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,12 @@
#include <stdio.h>
#include <stdint.h>

#if HAVE_OPENSSL_SHA1
#ifndef OPENSSL_API_COMPAT
#define OPENSSL_API_COMPAT 0x10101000L /* FIXME: Use OpenSSL 1.1+ API. */
#endif
#include <openssl/sha.h>
#endif

#ifdef __cplusplus
extern "C" {
#endif

#define SHA1_DIGEST_SIZE 20

#if HAVE_OPENSSL_SHA1
#define GL_OPENSSL_NAME 1
#include "gl_openssl.h"
#else
/* Structure to save state of computation between the single steps. */
struct sha1_ctx {
uint32_t A;
Expand Down Expand Up @@ -83,16 +72,6 @@ extern void* sha1_read_ctx(const struct sha1_ctx* ctx, void* restrict resbuf);
digest. */
extern void* sha1_buffer(const char* buffer, size_t len, void* restrict resblock);

#endif

/* Compute SHA1 message digest for bytes read from STREAM.
STREAM is an open file stream. Regular files are handled more efficiently.
The contents of STREAM from its current position to its end will be read.
The case that the last operation on STREAM was an 'ungetc' is not supported.
The resulting message digest number will be written into the 20 bytes
beginning at RESBLOCK. */
extern int sha1_stream(FILE* stream, void* resblock);

#ifdef __cplusplus
}
#endif
Expand Down
50 changes: 1 addition & 49 deletions totp/services/hmac/sha256.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* sha256.c - Functions to compute SHA256 and SHA224 message digest of files or
/* sha256.c - Functions to compute SHA256 message digest of files or
memory blocks according to the NIST specification FIPS-180-2.
Copyright (C) 2005-2006, 2008-2022 Free Software Foundation, Inc.
Expand All @@ -21,9 +21,6 @@
*/

/* Specification. */
#if HAVE_OPENSSL_SHA256
#define GL_OPENSSL_INLINE _GL_EXTERN_INLINE
#endif
#include "sha256.h"

#include <stdint.h>
Expand All @@ -36,8 +33,6 @@
#define SWAP(n) swap_uint32(n)
#endif

#if !HAVE_OPENSSL_SHA256

/* 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, ... */};
Expand All @@ -61,20 +56,6 @@ void sha256_init_ctx(struct sha256_ctx* ctx) {
ctx->buflen = 0;
}

void sha224_init_ctx(struct sha256_ctx* ctx) {
ctx->state[0] = 0xc1059ed8UL;
ctx->state[1] = 0x367cd507UL;
ctx->state[2] = 0x3070dd17UL;
ctx->state[3] = 0xf70e5939UL;
ctx->state[4] = 0xffc00b31UL;
ctx->state[5] = 0x68581511UL;
ctx->state[6] = 0x64f98fa7UL;
ctx->state[7] = 0xbefa4fa4UL;

ctx->total[0] = ctx->total[1] = 0;
ctx->buflen = 0;
}

/* Copy the value from v into the memory location pointed to by *CP,
If your architecture allows unaligned access, this is equivalent to
* (__typeof__ (v) *) cp = v */
Expand All @@ -93,15 +74,6 @@ void* sha256_read_ctx(const struct sha256_ctx* ctx, void* resbuf) {
return resbuf;
}

void* sha224_read_ctx(const struct sha256_ctx* ctx, void* resbuf) {
int i;
char* r = resbuf;

for(i = 0; i < 7; i++) set_uint32(r + i * sizeof ctx->state[0], SWAP(ctx->state[i]));

return resbuf;
}

/* Process the remaining bytes in the internal buffer and the usual
prolog according to the standard and write the result to RESBUF. */
static void sha256_conclude_ctx(struct sha256_ctx* ctx) {
Expand Down Expand Up @@ -130,11 +102,6 @@ void* sha256_finish_ctx(struct sha256_ctx* ctx, void* resbuf) {
return sha256_read_ctx(ctx, resbuf);
}

void* sha224_finish_ctx(struct sha256_ctx* ctx, void* resbuf) {
sha256_conclude_ctx(ctx);
return sha224_read_ctx(ctx, resbuf);
}

/* Compute SHA256 message digest for LEN bytes beginning at BUFFER. The
result is always in little endian byte order, so that a byte-wise
output yields to the wanted ASCII representation of the message
Expand All @@ -152,19 +119,6 @@ void* sha256_buffer(const char* buffer, size_t len, void* resblock) {
return sha256_finish_ctx(&ctx, resblock);
}

void* sha224_buffer(const char* buffer, size_t len, void* resblock) {
struct sha256_ctx ctx;

/* Initialize the computation context. */
sha224_init_ctx(&ctx);

/* Process whole buffer but last len % 64 bytes. */
sha256_process_bytes(buffer, len, &ctx);

/* Put result in desired memory area. */
return sha224_finish_ctx(&ctx, resblock);
}

void sha256_process_bytes(const void* buffer, size_t len, struct sha256_ctx* ctx) {
/* When we already have some bits in our internal buffer concatenate
both inputs first. */
Expand Down Expand Up @@ -324,8 +278,6 @@ void sha256_process_block(const void* buffer, size_t len, struct sha256_ctx* ctx
}
}

#endif

/*
* Hey Emacs!
* Local Variables:
Expand Down
Loading

0 comments on commit eb60a9c

Please sign in to comment.