Update TOTP

This commit is contained in:
MX
2023-04-28 14:04:16 +03:00
parent 96375e8244
commit e87256e01f
10 changed files with 60 additions and 28 deletions

View File

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

View File

@@ -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

View File

@@ -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);

View File

@@ -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);

View File

@@ -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);

View 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);
}
}
}

View File

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