Skip to content

Commit

Permalink
add fake hash function to only copy inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
contrun committed Dec 18, 2023
1 parent e60c718 commit 0a20345
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ endif

# Hash module

HASH_SRC = $(wildcard src/hash/sha*.c) src/hash/hash_algs.c
HASH_SRC = $(wildcard src/hash/sha*.c) src/hash/copy.c src/hash/hash_algs.c
HASH_OBJECTS = $(patsubst %.c, %.o, $(HASH_SRC))
HASH_DEPS = $(patsubst %.c, %.d, $(HASH_SRC))

Expand Down
59 changes: 59 additions & 0 deletions src/hash/copy.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "../lib_ecc_config.h"

#include "copy.h"

/* Init hash function */
void copy256_init(copy256_context *ctx)
{
MUST_HAVE(ctx != NULL);

ctx->copied_bytes = 0;
}

/* Update hash function */
void copy256_update(copy256_context *ctx, const u8 *input, u32 ilen)
{
u32 bytes_to_copy = ilen;
u16 left_bytes;

MUST_HAVE((ctx != NULL) && (input != NULL));

/* Nothing to process, return */
if (ilen == 0) {
return;
}

/* Get what's left in our local buffer */
left_bytes = COPY256_SIZE - (ctx->copied_bytes & 0x3F);

if (bytes_to_copy >= left_bytes) {
bytes_to_copy = left_bytes;
}
local_memcpy(ctx->buffer + ctx->copied_bytes, input, bytes_to_copy);
ctx->copied_bytes += bytes_to_copy;
return;
}

/* Finalize */
void copy256_final(copy256_context *ctx, u8 output[COPY256_SIZE])
{
MUST_HAVE((ctx != NULL) && (output != NULL));
MUST_HAVE(ctx->copied_bytes == COPY256_SIZE);
local_memcpy(ctx->buffer, output, COPY256_SIZE);
}

void copy256_scattered(const u8 **inputs, const u32 *ilens,
u8 output[COPY256_SIZE])
{
copy256_context ctx;
int pos = 0;

copy256_init(&ctx);

while (inputs[pos] != NULL) {
copy256_update(&ctx, inputs[pos], ilens[pos]);
pos += 1;
}

copy256_final(&ctx, output);
}
23 changes: 23 additions & 0 deletions src/hash/copy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef __COPY_H__
#define __COPY_H__

#include "../lib_ecc_config.h"
#include "../words/words.h"
#include "../utils/utils.h"

#define COPY256_SIZE 32

typedef struct {
u64 copied_bytes;
/* Internal buffer to handle updates in a block */
u8 buffer[COPY256_SIZE];
} copy256_context;

void copy256_init(copy256_context *ctx);
void copy256_update(copy256_context *ctx, const u8 *input, u32 ilen);
void copy256_final(copy256_context *ctx, u8 output[COPY256_SIZE]);
void copy256_scattered(const u8 **inputs, const u32 *ilens,
u8 output[COPY256_SIZE]);
void copy256(const u8 *input, u32 ilen, u8 output[COPY256_SIZE]);

#endif /* __COPY_H__ */
9 changes: 9 additions & 0 deletions src/hash/hash_algs.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "../lib_ecc_config.h"
#include "../lib_ecc_types.h"
#include "../words/words.h"
#include "copy.h"
#include "sha224.h"
#include "sha256.h"
#include "sha384.h"
Expand Down Expand Up @@ -245,6 +246,14 @@ static const hash_mapping hash_maps[] = {
#define MAX_HASH_ALG_NAME_LEN 9
#endif /* MAX_HASH_ALG_NAME_LEN */
#endif /* WITH_HASH_SHA3_512 */
{.type = COPY,
.name = "COPY256",
.digest_size = COPY256_SIZE,
.block_size = COPY256_SIZE,
.hfunc_init = (_hfunc_init) copy256_init,
.hfunc_update = (_hfunc_update) copy256_update,
.hfunc_finalize = (_hfunc_finalize) copy256_final,
.hfunc_scattered = copy256_scattered},
{.type = UNKNOWN_HASH_ALG, /* Needs to be kept last */
.name = "UNKNOWN",
.digest_size = 0,
Expand Down
1 change: 1 addition & 0 deletions src/lib_ecc_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ typedef enum {
#ifdef WITH_HASH_SHA3_512
SHA3_512 = 10,
#endif
COPY = 11,
} hash_alg_type;

/* All curves we support */
Expand Down

0 comments on commit 0a20345

Please sign in to comment.