From f810597c6ce050233a7c499435c351d38777ef43 Mon Sep 17 00:00:00 2001 From: WolverinDEV Date: Sun, 7 Apr 2019 17:05:16 +0200 Subject: [PATCH] Changed the SHA512 context system --- CMakeLists.txt | 1 + include/sha512.h | 19 +++---------------- src/sha512.c | 7 +++++++ src/sha512_openssl.c | 23 +++++++++++++++++------ 4 files changed, 28 insertions(+), 22 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 413f62e..71c48d5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,7 @@ if(USE_OPENSSL) add_definitions(-DUSE_OPENSSL) set(SOURCE ${SOURCE} src/sha512_openssl.c) else() + message(FATAL_ERROR "Not supported yet!") set(SOURCE ${SOURCE} src/sha512.c) endif() diff --git a/include/sha512.h b/include/sha512.h index 0e5507f..7a79c6d 100644 --- a/include/sha512.h +++ b/include/sha512.h @@ -3,22 +3,9 @@ #include -#ifdef USE_OPENSSL - #include - - typedef SHA512_CTX sha512_context; -#else - #include - - #include "fixedint.h" - - /* state */ - typedef struct sha512_context_ { - uint64_t length, state[8]; - size_t curlen; - unsigned char buf[128]; - } sha512_context; -#endif +typedef struct { + void* context; +} sha512_context; typedef struct sha512_functions_ { int(*_ed_sha512_init)(sha512_context*); diff --git a/src/sha512.c b/src/sha512.c index 36ef024..7440e78 100644 --- a/src/sha512.c +++ b/src/sha512.c @@ -12,6 +12,13 @@ #include "../include/fixedint.h" #include "../include/sha512.h" +/* state */ +typedef struct sha512_context_ { + uint64_t length, state[8]; + size_t curlen; + unsigned char buf[128]; +} sha512_context; + /* the K array */ static const uint64_t K[80] = { UINT64_C(0x428a2f98d728ae22), UINT64_C(0x7137449123ef65cd), diff --git a/src/sha512_openssl.c b/src/sha512_openssl.c index 5a1384b..a92045e 100644 --- a/src/sha512_openssl.c +++ b/src/sha512_openssl.c @@ -1,17 +1,28 @@ #include +#include #include "../include/sha512.h" -int _ed_sha512_init(sha512_context * md) { - return SHA512_Init(md) != 1; /* Returns 0 on success */ +int _ed_sha512_init(sha512_context* md) { + assert(!md->context); + + md->context = malloc(sizeof(SHA512_CTX)); + return SHA512_Init(md->context) != 1; /* Returns 0 on success */ } -int _ed_sha512_final(sha512_context * md, unsigned char *out) { - return SHA512_Final(out, md) != 1; /* Returns 0 on success */ +int _ed_sha512_final(sha512_context* md, unsigned char *out) { + assert(md->context); + + int result = SHA512_Final(out, md->context) != 1; /* Returns 0 on success */ + free(md->context); + md->context = 0; + return result; } -int _ed_sha512_update(sha512_context * md, const unsigned char *in, size_t inlen) { - return SHA512_Update(md, in, inlen) != 1; /* Returns 0 on success */ +int _ed_sha512_update(sha512_context* md, const unsigned char *in, size_t inlen) { + assert(md->context); + + return SHA512_Update(md->context, in, inlen) != 1; /* Returns 0 on success */ } sha512_functions _ed_sha512_functions = {