Add and use result macros for all HMACs.

This commit is contained in:
Stephen Heumann 2024-04-15 18:39:36 -05:00
parent 0ae8f97c3c
commit d3d3dbbad9
5 changed files with 40 additions and 24 deletions

View File

@ -44,8 +44,8 @@ int main(void) {
hmac_sha256_compute(hmac_sha256_context, msg, sizeof(msg)-1); hmac_sha256_compute(hmac_sha256_context, msg, sizeof(msg)-1);
printf("HMAC-SHA256: "); printf("HMAC-SHA256: ");
for (int i = 0; i < sizeof(hmac_sha256_context->u[0].ctx.hash); i++) { for (int i = 0; i < sizeof(hmac_sha256_result(hmac_sha256_context)); i++) {
printf("%02x", hmac_sha256_context->u[0].ctx.hash[i]); printf("%02x", hmac_sha256_result(hmac_sha256_context)[i]);
} }
printf("\n"); printf("\n");
@ -54,8 +54,8 @@ int main(void) {
hmac_sha1_compute(hmac_sha1_context, msg, sizeof(msg)-1); hmac_sha1_compute(hmac_sha1_context, msg, sizeof(msg)-1);
printf("HMAC-SHA1: "); printf("HMAC-SHA1: ");
for (int i = 0; i < sizeof(hmac_sha1_context->u[0].ctx.hash); i++) { for (int i = 0; i < sizeof(hmac_sha1_result(hmac_sha1_context)); i++) {
printf("%02x", hmac_sha1_context->u[0].ctx.hash[i]); printf("%02x", hmac_sha1_result(hmac_sha1_context)[i]);
} }
printf("\n"); printf("\n");
@ -64,8 +64,8 @@ int main(void) {
hmac_md5_compute(hmac_md5_context, msg, sizeof(msg)-1); hmac_md5_compute(hmac_md5_context, msg, sizeof(msg)-1);
printf("HMAC-MD5: "); printf("HMAC-MD5: ");
for (int i = 0; i < sizeof(hmac_md5_context->u[0].ctx.hash); i++) { for (int i = 0; i < sizeof(hmac_md5_result(hmac_md5_context)); i++) {
printf("%02x", hmac_md5_context->u[0].ctx.hash[i]); printf("%02x", hmac_md5_result(hmac_md5_context)[i]);
} }
printf("\n"); printf("\n");
@ -75,8 +75,8 @@ int main(void) {
hmac_md4_compute(hmac_md4_context, msg, sizeof(msg)-1); hmac_md4_compute(hmac_md4_context, msg, sizeof(msg)-1);
printf("HMAC-MD4: "); printf("HMAC-MD4: ");
for (int i = 0; i < sizeof(hmac_md4_context->u[0].ctx.hash); i++) { for (int i = 0; i < sizeof(hmac_md4_result(hmac_md4_context)); i++) {
printf("%02x", hmac_md4_context->u[0].ctx.hash[i]); printf("%02x", hmac_md4_result(hmac_md4_context)[i]);
} }
printf("\n"); printf("\n");
@ -86,8 +86,8 @@ int main(void) {
hmac_sha256_finalize(hmac_sha256_context); hmac_sha256_finalize(hmac_sha256_context);
printf("HMAC-SHA256 (incremental calculation): "); printf("HMAC-SHA256 (incremental calculation): ");
for (int i = 0; i < sizeof(hmac_sha256_context->u[0].ctx.hash); i++) { for (int i = 0; i < sizeof(hmac_sha256_result(hmac_sha256_context)); i++) {
printf("%02x", hmac_sha256_context->u[0].ctx.hash[i]); printf("%02x", hmac_sha256_result(hmac_sha256_context)[i]);
} }
printf("\n"); printf("\n");
@ -97,8 +97,8 @@ int main(void) {
hmac_sha1_finalize(hmac_sha1_context); hmac_sha1_finalize(hmac_sha1_context);
printf("HMAC-SHA1 (incremental calculation): "); printf("HMAC-SHA1 (incremental calculation): ");
for (int i = 0; i < sizeof(hmac_sha1_context->u[0].ctx.hash); i++) { for (int i = 0; i < sizeof(hmac_sha1_result(hmac_sha1_context)); i++) {
printf("%02x", hmac_sha1_context->u[0].ctx.hash[i]); printf("%02x", hmac_sha1_result(hmac_sha1_context)[i]);
} }
printf("\n"); printf("\n");
@ -108,8 +108,8 @@ int main(void) {
hmac_md5_finalize(hmac_md5_context); hmac_md5_finalize(hmac_md5_context);
printf("HMAC-MD5 (incremental calculation): "); printf("HMAC-MD5 (incremental calculation): ");
for (int i = 0; i < sizeof(hmac_md5_context->u[0].ctx.hash); i++) { for (int i = 0; i < sizeof(hmac_md5_result(hmac_md5_context)); i++) {
printf("%02x", hmac_md5_context->u[0].ctx.hash[i]); printf("%02x", hmac_md5_result(hmac_md5_context)[i]);
} }
printf("\n"); printf("\n");
@ -119,8 +119,8 @@ int main(void) {
hmac_md4_finalize(hmac_md4_context); hmac_md4_finalize(hmac_md4_context);
printf("HMAC-MD4 (incremental calculation): "); printf("HMAC-MD4 (incremental calculation): ");
for (int i = 0; i < sizeof(hmac_md4_context->u[0].ctx.hash); i++) { for (int i = 0; i < sizeof(hmac_md4_result(hmac_md4_context)); i++) {
printf("%02x", hmac_md4_context->u[0].ctx.hash[i]); printf("%02x", hmac_md4_result(hmac_md4_context)[i]);
} }
printf("\n"); printf("\n");
} }

8
md4.h
View File

@ -77,15 +77,19 @@ void hmac_md4_update(struct hmac_md4_context *context,
/* /*
* Finish HMAC-MD4 processing and generate the final HMAC. * Finish HMAC-MD4 processing and generate the final HMAC.
* The result will be in context->u[0].ctx.hash.
*/ */
void hmac_md4_finalize(struct hmac_md4_context *context); void hmac_md4_finalize(struct hmac_md4_context *context);
/* /*
* Compute the HMAC-MD4 of a message as a single operation. * Compute the HMAC-MD4 of a message as a single operation.
* The result will be in context->u[0].ctx.hash.
* The context can be reused for multiple hmac_md4_compute operations. * The context can be reused for multiple hmac_md4_compute operations.
*/ */
void hmac_md4_compute(struct hmac_md4_context *context, void hmac_md4_compute(struct hmac_md4_context *context,
const unsigned char *message, const unsigned char *message,
unsigned long message_length); unsigned long message_length);
/*
* Get the result of an HMAC-MD4 computation following hmac_md4_finalize
* or hmac_md4_compute.
*/
#define hmac_md4_result(context) ((context)->u[0].ctx.hash)

8
md5.h
View File

@ -77,15 +77,19 @@ void hmac_md5_update(struct hmac_md5_context *context,
/* /*
* Finish HMAC-MD5 processing and generate the final HMAC. * Finish HMAC-MD5 processing and generate the final HMAC.
* The result will be in context->u[0].ctx.hash.
*/ */
void hmac_md5_finalize(struct hmac_md5_context *context); void hmac_md5_finalize(struct hmac_md5_context *context);
/* /*
* Compute the HMAC-MD5 of a message as a single operation. * Compute the HMAC-MD5 of a message as a single operation.
* The result will be in context->u[0].ctx.hash.
* The context can be reused for multiple hmac_md5_compute operations. * The context can be reused for multiple hmac_md5_compute operations.
*/ */
void hmac_md5_compute(struct hmac_md5_context *context, void hmac_md5_compute(struct hmac_md5_context *context,
const unsigned char *message, const unsigned char *message,
unsigned long message_length); unsigned long message_length);
/*
* Get the result of an HMAC-MD5 computation following hmac_md5_finalize
* or hmac_md5_compute.
*/
#define hmac_md5_result(context) ((context)->u[0].ctx.hash)

8
sha1.h
View File

@ -77,15 +77,19 @@ void hmac_sha1_update(struct hmac_sha1_context *context,
/* /*
* Finish HMAC-SHA1 processing and generate the final HMAC. * Finish HMAC-SHA1 processing and generate the final HMAC.
* The result will be in context->u[0].ctx.hash.
*/ */
void hmac_sha1_finalize(struct hmac_sha1_context *context); void hmac_sha1_finalize(struct hmac_sha1_context *context);
/* /*
* Compute the HMAC-SHA1 of a message as a single operation. * Compute the HMAC-SHA1 of a message as a single operation.
* The result will be in context->u[0].ctx.hash.
* The context can be reused for multiple hmac_sha1_compute operations. * The context can be reused for multiple hmac_sha1_compute operations.
*/ */
void hmac_sha1_compute(struct hmac_sha1_context *context, void hmac_sha1_compute(struct hmac_sha1_context *context,
const unsigned char *message, const unsigned char *message,
unsigned long message_length); unsigned long message_length);
/*
* Get the result of an HMAC-SHA1 computation following hmac_sha1_finalize
* or hmac_sha1_compute.
*/
#define hmac_sha1_result(context) ((context)->u[0].ctx.hash)

View File

@ -85,15 +85,19 @@ void hmac_sha256_update(struct hmac_sha256_context *context,
/* /*
* Finish HMAC-SHA256 processing and generate the final HMAC. * Finish HMAC-SHA256 processing and generate the final HMAC.
* The result will be in context->u[0].ctx.hash.
*/ */
void hmac_sha256_finalize(struct hmac_sha256_context *context); void hmac_sha256_finalize(struct hmac_sha256_context *context);
/* /*
* Compute the HMAC-SHA256 of a message as a single operation. * Compute the HMAC-SHA256 of a message as a single operation.
* The result will be in context->u[0].ctx.hash.
* The context can be reused for multiple hmac_sha256_compute operations. * The context can be reused for multiple hmac_sha256_compute operations.
*/ */
void hmac_sha256_compute(struct hmac_sha256_context *context, void hmac_sha256_compute(struct hmac_sha256_context *context,
const unsigned char *message, const unsigned char *message,
unsigned long message_length); unsigned long message_length);
/*
* Get the result of an HMAC-SHA256 computation following hmac_sha256_finalize
* or hmac_sha256_compute.
*/
#define hmac_sha256_result(context) ((context)->u[0].ctx.hash)