From 0a20345c27eb02034dd4f855f41c3a38ca5e4f12 Mon Sep 17 00:00:00 2001 From: YI Date: Mon, 18 Dec 2023 17:45:13 +0800 Subject: [PATCH] add fake hash function to only copy inputs --- Makefile | 2 +- src/hash/copy.c | 59 ++++++++++++++++++++++++++++++++++++++++++++ src/hash/copy.h | 23 +++++++++++++++++ src/hash/hash_algs.h | 9 +++++++ src/lib_ecc_types.h | 1 + 5 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 src/hash/copy.c create mode 100644 src/hash/copy.h diff --git a/Makefile b/Makefile index 4ccfe990..18191bbc 100644 --- a/Makefile +++ b/Makefile @@ -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)) diff --git a/src/hash/copy.c b/src/hash/copy.c new file mode 100644 index 00000000..103336bc --- /dev/null +++ b/src/hash/copy.c @@ -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); +} diff --git a/src/hash/copy.h b/src/hash/copy.h new file mode 100644 index 00000000..caa74cad --- /dev/null +++ b/src/hash/copy.h @@ -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__ */ diff --git a/src/hash/hash_algs.h b/src/hash/hash_algs.h index 842c4c3a..042ac2d9 100644 --- a/src/hash/hash_algs.h +++ b/src/hash/hash_algs.h @@ -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" @@ -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, diff --git a/src/lib_ecc_types.h b/src/lib_ecc_types.h index fa39b341..b7834910 100644 --- a/src/lib_ecc_types.h +++ b/src/lib_ecc_types.h @@ -77,6 +77,7 @@ typedef enum { #ifdef WITH_HASH_SHA3_512 SHA3_512 = 10, #endif + COPY = 11, } hash_alg_type; /* All curves we support */