mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-05-21 05:04:46 -07:00
Update TOTP
This commit is contained in:
@@ -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;
|
||||
@@ -495,11 +493,9 @@ 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;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
#include <string.h>
|
||||
#include "sha256.h"
|
||||
#include "memxor.h"
|
||||
|
||||
#define IPAD 0x36
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>. */
|
||||
|
||||
#include "hmac_sha256.h"
|
||||
#include "sha256.h"
|
||||
|
||||
#define GL_HMAC_NAME 256
|
||||
#define GL_HMAC_BLOCKSIZE 64
|
||||
|
||||
@@ -27,6 +27,8 @@
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#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);
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#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);
|
||||
|
||||
@@ -27,13 +27,10 @@
|
||||
#include <string.h>
|
||||
|
||||
#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);
|
||||
|
||||
11
applications/external/totp/services/hmac/sha_pad_buffer.c
vendored
Normal file
11
applications/external/totp/services/hmac/sha_pad_buffer.c
vendored
Normal file
@@ -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
applications/external/totp/services/hmac/sha_pad_buffer.h
vendored
Normal file
4
applications/external/totp/services/hmac/sha_pad_buffer.h
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
void sha_pad_buffer(uint8_t* buffer, size_t size);
|
||||
21
applications/external/totp/types/token_info.c
vendored
21
applications/external/totp/types/token_info.c
vendored
@@ -117,6 +117,27 @@ 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:
|
||||
|
||||
12
applications/external/totp/types/token_info.h
vendored
12
applications/external/totp/types/token_info.h
vendored
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user