Changed the SHA512 context system

This commit is contained in:
WolverinDEV 2019-04-07 17:05:16 +02:00
parent fc8ade72c5
commit f810597c6c
4 changed files with 28 additions and 22 deletions

View File

@ -22,6 +22,7 @@ if(USE_OPENSSL)
add_definitions(-DUSE_OPENSSL) add_definitions(-DUSE_OPENSSL)
set(SOURCE ${SOURCE} src/sha512_openssl.c) set(SOURCE ${SOURCE} src/sha512_openssl.c)
else() else()
message(FATAL_ERROR "Not supported yet!")
set(SOURCE ${SOURCE} src/sha512.c) set(SOURCE ${SOURCE} src/sha512.c)
endif() endif()

View File

@ -3,22 +3,9 @@
#include <assert.h> #include <assert.h>
#ifdef USE_OPENSSL typedef struct {
#include <openssl/sha.h> void* context;
typedef SHA512_CTX sha512_context;
#else
#include <stddef.h>
#include "fixedint.h"
/* state */
typedef struct sha512_context_ {
uint64_t length, state[8];
size_t curlen;
unsigned char buf[128];
} sha512_context; } sha512_context;
#endif
typedef struct sha512_functions_ { typedef struct sha512_functions_ {
int(*_ed_sha512_init)(sha512_context*); int(*_ed_sha512_init)(sha512_context*);

View File

@ -12,6 +12,13 @@
#include "../include/fixedint.h" #include "../include/fixedint.h"
#include "../include/sha512.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 */ /* the K array */
static const uint64_t K[80] = { static const uint64_t K[80] = {
UINT64_C(0x428a2f98d728ae22), UINT64_C(0x7137449123ef65cd), UINT64_C(0x428a2f98d728ae22), UINT64_C(0x7137449123ef65cd),

View File

@ -1,17 +1,28 @@
#include <openssl/sha.h> #include <openssl/sha.h>
#include <stdlib.h>
#include "../include/sha512.h" #include "../include/sha512.h"
int _ed_sha512_init(sha512_context* md) { int _ed_sha512_init(sha512_context* md) {
return SHA512_Init(md) != 1; /* Returns 0 on success */ 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) { int _ed_sha512_final(sha512_context* md, unsigned char *out) {
return SHA512_Final(out, md) != 1; /* Returns 0 on success */ 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) { 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 */ assert(md->context);
return SHA512_Update(md->context, in, inlen) != 1; /* Returns 0 on success */
} }
sha512_functions _ed_sha512_functions = { sha512_functions _ed_sha512_functions = {