Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
API, impl and tests for Message Digests
- Loading branch information
Showing
4 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |