Skip to content

Commit

Permalink
API, impl and tests for Message Digests
Browse files Browse the repository at this point in the history
  • Loading branch information
pushkarnk committed Nov 28, 2023
1 parent d88007c commit a8f88ec
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Makefile
Expand Up @@ -5,13 +5,15 @@ build:
cc -I/usr/local/include/openssl/ -I./include -c -fPIC src/keyagreement.c -o build/bin/keyagreement.o && \
cc -I/usr/local/include/openssl/ -I./include -c -fPIC src/keyencapsulation.c -o build/bin/keyencapsulation.o && \
cc -I/usr/local/include/openssl/ -I./include -c -fPIC src/mac.c -o build/bin/mac.o && \
cc -I/usr/local/include/openssl/ -I./include -c -fPIC src/md.c -o build/bin/md.o && \
cc -shared -fPIC -Wl,-soname,libjssl.so -o build/bin/libjssl.so \
build/bin/init.o \
build/bin/drbg.o \
build/bin/cipher.o \
build/bin/keyagreement.o \
build/bin/keyencapsulation.o \
build/bin/mac.o \
build/bin/md.o \
-L/usr/local/lib64 -lcrypto -lssl

test-drbg: build
Expand All @@ -32,5 +34,8 @@ test-ke: build
test-mac: build
@mkdir -p build/test && cc -I./include/ -L./build/bin/ -L/usr/local/lib64 -o build/test/mac test/mac.c -ljssl && \
LD_LIBRARY_PATH=./build/bin ./build/test/mac 2>/dev/null
test-md: build
@mkdir -p build/test && cc -I./include/ -L./build/bin/ -L/usr/local/lib64 -o build/test/md test/md.c -ljssl && \
LD_LIBRARY_PATH=./build/bin ./build/test/md 2>/dev/null
clean:
@rm -rf build
12 changes: 12 additions & 0 deletions include/md.h
@@ -0,0 +1,12 @@
#include <openssl/evp.h>
#include "jssl.h"

typedef struct md_context {
EVP_MD_CTX *ossl_ctx;
OSSL_LIB_CTX *libctx;
} md_context;

md_context *md_init(OSSL_LIB_CTX *libctx, const char *algorithm);
int md_update(md_context *ctx, byte *input, size_t input_length);
int md_digest(md_context *ctx, byte *output, int *output_length);
void md_context_free(md_context *ctx);
39 changes: 39 additions & 0 deletions src/md.c
@@ -0,0 +1,39 @@
#include "md.h"

md_context *md_init(OSSL_LIB_CTX *libctx, const char *algorithm) {
md_context *new = (md_context*)malloc(sizeof(md_context));
new->libctx = libctx;
EVP_MD *md = EVP_MD_fetch(libctx, algorithm, NULL);
EVP_MD_CTX *ctx = EVP_MD_CTX_new();
if (!EVP_DigestInit_ex2(ctx, md, NULL)) {
EVP_MD_CTX_free(ctx);
EVP_MD_free(md);
free(new);
return NULL;
}
EVP_MD_free(md);
new->ossl_ctx = ctx;
return new;
}

int md_update(md_context *ctx, byte *input, size_t input_length) {
if (!EVP_DigestUpdate(ctx->ossl_ctx, input, input_length)) {
md_context_free(ctx);
return 0;
}
return 1;
}

int md_digest(md_context *ctx, byte *output, int *output_length) {
if (!EVP_DigestFinal_ex(ctx->ossl_ctx, output, output_length)) {
md_context_free(ctx);
return 0;
}
return 1;
}

void md_context_free(md_context *ctx) {
EVP_MD_CTX_free(ctx->ossl_ctx);
free(ctx);
}

79 changes: 79 additions & 0 deletions test/md.c
@@ -0,0 +1,79 @@
#include "md.h"
#include <openssl/evp.h>

char *message1 = "Namaste, World!";
char *message2 = "How are you all?";
char *message3 = "How are you all!";

int equal(byte *out1, int len1, byte *out2, int len2) {
if (len1 != len2) return 0;
for (int i = 0; i < len1; i++) {
if (out1[i] != out2[i]) return 0;
}
return 1;
}

void test_digest(const char *algo, OSSL_LIB_CTX *libctx) {
byte output1[EVP_MAX_MD_SIZE] = {0};
byte output2[EVP_MAX_MD_SIZE] = {0};
int len1 = 0, len2 = 0;

printf("Test MessageDigest of type %s: ", algo);
md_context *ctx = md_init(libctx, algo);

if (ctx == NULL) {
md_context_free(ctx);
printf("FAILED (init)\n");
}

if (!(md_update(ctx, message1, strlen(message1)) &&
md_update(ctx, message2, strlen(message2)))) {
md_context_free(ctx);
printf("FAILED (update)\n");
return;
}

if (!md_digest(ctx, output1, &len1)) {
md_context_free(ctx);
printf("FAILED (digest)\n");
return;
}
md_context_free(ctx);

md_context *ctx1 = md_init(libctx, algo);
if (!(md_update(ctx1, message1, strlen(message1)) &&
md_update(ctx1, message3, strlen(message3)))) {
md_context_free(ctx1);
printf("FAILED (update)\n");
return;
}

if (!md_digest(ctx1, output2, &len2)) {
md_context_free(ctx1);
printf("FAILED (digest)\n");
return;
}

if(equal(output1, len1, output2, len2)) {
printf("FAILED (digests match)\n");
return;
}

md_context_free(ctx1);
printf("PASSED\n");
}

int main(int argc, char ** argv) {
OSSL_LIB_CTX *libctx = load_openssl_fips_provider("/usr/local/ssl/openssl.cnf");
test_digest("SHA1", libctx);
test_digest("SHA2-224", libctx);
test_digest("SHA2-256", libctx);
test_digest("SHA2-384", libctx);
test_digest("SHA2-512", libctx);
test_digest("SHA3-224", libctx);
test_digest("SHA3-256", libctx);
test_digest("SHA3-384", libctx);
test_digest("SHA3-512", libctx);
test_digest("KECCAK-KMAC-128", libctx);
test_digest("KECCAK-KMAC-256", libctx);
}

0 comments on commit a8f88ec

Please sign in to comment.