[njs] Unifying hash function prototypes.

Dmitry Volyntsev xeioex at nginx.com
Tue Jan 9 00:57:13 UTC 2024


details:   https://hg.nginx.org/njs/rev/721475693b80
branches:  
changeset: 2252:721475693b80
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Mon Jan 08 16:40:42 2024 -0800
description:
Unifying hash function prototypes.

This fixes UndefinedBehaviorSanitizer warning "call to function through
pointer to incorrect function type".

Found by UndefinedBehaviorSanitizer.

diffstat:

 external/njs_crypto_module.c |  71 +++++++++++++++++--------------------------
 src/njs_hash.h               |  32 +++++++++++++++++++
 src/njs_main.h               |   4 +-
 src/njs_md5.c                |  10 +++---
 src/njs_md5.h                |  23 --------------
 src/njs_sha1.c               |  10 +++---
 src/njs_sha1.h               |  24 --------------
 src/njs_sha2.c               |  10 +++---
 src/njs_sha2.h               |  24 --------------
 9 files changed, 77 insertions(+), 131 deletions(-)

diffs (424 lines):

diff -r 57071ecadeb5 -r 721475693b80 external/njs_crypto_module.c
--- a/external/njs_crypto_module.c	Mon Jan 08 16:40:27 2024 -0800
+++ b/external/njs_crypto_module.c	Mon Jan 08 16:40:42 2024 -0800
@@ -6,16 +6,14 @@
 
 
 #include <njs.h>
-#include <njs_md5.h>
-#include <njs_sha1.h>
-#include <njs_sha2.h>
+#include <njs_hash.h>
 #include <njs_string.h>
 #include <njs_buffer.h>
 
 
-typedef void (*njs_hash_init)(void *ctx);
-typedef void (*njs_hash_update)(void *ctx, const void *data, size_t size);
-typedef void (*njs_hash_final)(u_char *result, void *ctx);
+typedef void (*njs_hash_init)(njs_hash_t *ctx);
+typedef void (*njs_hash_update)(njs_hash_t *ctx, const void *data, size_t size);
+typedef void (*njs_hash_final)(u_char result[32], njs_hash_t *ctx);
 
 typedef njs_int_t (*njs_digest_encode)(njs_vm_t *vm, njs_value_t *value,
     const njs_str_t *src);
@@ -31,24 +29,13 @@ typedef struct {
 } njs_hash_alg_t;
 
 typedef struct {
-    union {
-        njs_md5_t       md5;
-        njs_sha1_t      sha1;
-        njs_sha2_t      sha2;
-    } u;
-
+    njs_hash_t          ctx;
     njs_hash_alg_t      *alg;
 } njs_digest_t;
 
 typedef struct {
     u_char              opad[64];
-
-    union {
-        njs_md5_t       md5;
-        njs_sha1_t      sha1;
-        njs_sha2_t      sha2;
-    } u;
-
+    njs_hash_t          ctx;
     njs_hash_alg_t      *alg;
 } njs_hmac_t;
 
@@ -85,25 +72,25 @@ static njs_hash_alg_t njs_hash_algorithm
    {
      njs_str("md5"),
      16,
-     (njs_hash_init) njs_md5_init,
-     (njs_hash_update) njs_md5_update,
-     (njs_hash_final) njs_md5_final
+     njs_md5_init,
+     njs_md5_update,
+     njs_md5_final
    },
 
    {
      njs_str("sha1"),
      20,
-     (njs_hash_init) njs_sha1_init,
-     (njs_hash_update) njs_sha1_update,
-     (njs_hash_final) njs_sha1_final
+     njs_sha1_init,
+     njs_sha1_update,
+     njs_sha1_final
    },
 
    {
      njs_str("sha256"),
      32,
-     (njs_hash_init) njs_sha2_init,
-     (njs_hash_update) njs_sha2_update,
-     (njs_hash_final) njs_sha2_final
+     njs_sha2_init,
+     njs_sha2_update,
+     njs_sha2_final
    },
 
    {
@@ -312,7 +299,7 @@ njs_crypto_create_hash(njs_vm_t *vm, njs
 
     dgst->alg = alg;
 
-    alg->init(&dgst->u);
+    alg->init(&dgst->ctx);
 
     return njs_vm_external_create(vm, retval, njs_crypto_hash_proto_id,
                                   dgst, 0);
@@ -390,10 +377,10 @@ njs_hash_prototype_update(njs_vm_t *vm, 
     }
 
     if (!hmac) {
-        dgst->alg->update(&dgst->u, data.start, data.length);
+        dgst->alg->update(&dgst->ctx, data.start, data.length);
 
     } else {
-        ctx->alg->update(&ctx->u, data.start, data.length);
+        ctx->alg->update(&ctx->ctx, data.start, data.length);
     }
 
     njs_value_assign(retval, this);
@@ -450,17 +437,17 @@ njs_hash_prototype_digest(njs_vm_t *vm, 
 
     if (!hmac) {
         alg = dgst->alg;
-        alg->final(digest, &dgst->u);
+        alg->final(digest, &dgst->ctx);
         dgst->alg = NULL;
 
     } else {
         alg = ctx->alg;
-        alg->final(hash1, &ctx->u);
+        alg->final(hash1, &ctx->ctx);
 
-        alg->init(&ctx->u);
-        alg->update(&ctx->u, ctx->opad, 64);
-        alg->update(&ctx->u, hash1, alg->size);
-        alg->final(digest, &ctx->u);
+        alg->init(&ctx->ctx);
+        alg->update(&ctx->ctx, ctx->opad, 64);
+        alg->update(&ctx->ctx, hash1, alg->size);
+        alg->final(digest, &ctx->ctx);
         ctx->alg = NULL;
     }
 
@@ -562,9 +549,9 @@ njs_crypto_create_hmac(njs_vm_t *vm, njs
     ctx->alg = alg;
 
     if (key.length > sizeof(key_buf)) {
-        alg->init(&ctx->u);
-        alg->update(&ctx->u, key.start, key.length);
-        alg->final(digest, &ctx->u);
+        alg->init(&ctx->ctx);
+        alg->update(&ctx->ctx, key.start, key.length);
+        alg->final(digest, &ctx->ctx);
 
         memcpy(key_buf, digest, alg->size);
         njs_explicit_memzero(key_buf + alg->size, sizeof(key_buf) - alg->size);
@@ -583,8 +570,8 @@ njs_crypto_create_hmac(njs_vm_t *vm, njs
          key_buf[i] ^= 0x36;
     }
 
-    alg->init(&ctx->u);
-    alg->update(&ctx->u, key_buf, 64);
+    alg->init(&ctx->ctx);
+    alg->update(&ctx->ctx, key_buf, 64);
 
     return njs_vm_external_create(vm, retval, njs_crypto_hmac_proto_id,
                                   ctx, 0);
diff -r 57071ecadeb5 -r 721475693b80 src/njs_hash.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/njs_hash.h	Mon Jan 08 16:40:42 2024 -0800
@@ -0,0 +1,32 @@
+
+/*
+ * Copyright (C) Dmitry Volyntsev
+ * Copyright (C) NGINX, Inc.
+ */
+
+
+#ifndef _NJS_HASH_H_INCLUDED_
+#define _NJS_HASH_H_INCLUDED_
+
+
+typedef struct {
+    uint64_t  bytes;
+    uint32_t  a, b, c, d, e, f, g, h;
+    u_char    buffer[64];
+} njs_hash_t;
+
+
+NJS_EXPORT void njs_md5_init(njs_hash_t *ctx);
+NJS_EXPORT void njs_md5_update(njs_hash_t *ctx, const void *data, size_t size);
+NJS_EXPORT void njs_md5_final(u_char result[32], njs_hash_t *ctx);
+
+NJS_EXPORT void njs_sha1_init(njs_hash_t *ctx);
+NJS_EXPORT void njs_sha1_update(njs_hash_t *ctx, const void *data, size_t size);
+NJS_EXPORT void njs_sha1_final(u_char result[32], njs_hash_t *ctx);
+
+NJS_EXPORT void njs_sha2_init(njs_hash_t *ctx);
+NJS_EXPORT void njs_sha2_update(njs_hash_t *ctx, const void *data, size_t size);
+NJS_EXPORT void njs_sha2_final(u_char result[32], njs_hash_t *ctx);
+
+
+#endif /* _NJS_HASH_H_INCLUDED_ */
diff -r 57071ecadeb5 -r 721475693b80 src/njs_main.h
--- a/src/njs_main.h	Mon Jan 08 16:40:27 2024 -0800
+++ b/src/njs_main.h	Mon Jan 08 16:40:42 2024 -0800
@@ -41,9 +41,7 @@
 
 #include <njs_regex.h>
 
-#include <njs_md5.h>
-#include <njs_sha1.h>
-#include <njs_sha2.h>
+#include <njs_hash.h>
 
 #include <njs.h>
 #include <njs_value.h>
diff -r 57071ecadeb5 -r 721475693b80 src/njs_md5.c
--- a/src/njs_md5.c	Mon Jan 08 16:40:27 2024 -0800
+++ b/src/njs_md5.c	Mon Jan 08 16:40:42 2024 -0800
@@ -9,12 +9,12 @@
 #include <njs_main.h>
 
 
-static const u_char *njs_md5_body(njs_md5_t *ctx, const u_char *data,
+static const u_char *njs_md5_body(njs_hash_t *ctx, const u_char *data,
     size_t size);
 
 
 void
-njs_md5_init(njs_md5_t *ctx)
+njs_md5_init(njs_hash_t *ctx)
 {
     ctx->a = 0x67452301;
     ctx->b = 0xefcdab89;
@@ -26,7 +26,7 @@ njs_md5_init(njs_md5_t *ctx)
 
 
 void
-njs_md5_update(njs_md5_t *ctx, const void *data, size_t size)
+njs_md5_update(njs_hash_t *ctx, const void *data, size_t size)
 {
     size_t  used, free;
 
@@ -57,7 +57,7 @@ njs_md5_update(njs_md5_t *ctx, const voi
 
 
 void
-njs_md5_final(u_char result[16], njs_md5_t *ctx)
+njs_md5_final(u_char result[32], njs_hash_t *ctx)
 {
     size_t  used, free;
 
@@ -152,7 +152,7 @@ njs_md5_final(u_char result[16], njs_md5
  */
 
 static const u_char *
-njs_md5_body(njs_md5_t *ctx, const u_char *data, size_t size)
+njs_md5_body(njs_hash_t *ctx, const u_char *data, size_t size)
 {
     uint32_t       a, b, c, d;
     uint32_t       saved_a, saved_b, saved_c, saved_d;
diff -r 57071ecadeb5 -r 721475693b80 src/njs_md5.h
--- a/src/njs_md5.h	Mon Jan 08 16:40:27 2024 -0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,23 +0,0 @@
-
-/*
- * Copyright (C) Igor Sysoev
- * Copyright (C) Nginx, Inc.
- */
-
-
-#ifndef _NJS_MD5_H_INCLUDED_
-#define _NJS_MD5_H_INCLUDED_
-
-
-typedef struct {
-    uint64_t  bytes;
-    uint32_t  a, b, c, d;
-    u_char    buffer[64];
-} njs_md5_t;
-
-
-NJS_EXPORT void njs_md5_init(njs_md5_t *ctx);
-NJS_EXPORT void njs_md5_update(njs_md5_t *ctx, const void *data, size_t size);
-NJS_EXPORT void njs_md5_final(u_char result[16], njs_md5_t *ctx);
-
-#endif /* _NJS_MD5_H_INCLUDED_ */
diff -r 57071ecadeb5 -r 721475693b80 src/njs_sha1.c
--- a/src/njs_sha1.c	Mon Jan 08 16:40:27 2024 -0800
+++ b/src/njs_sha1.c	Mon Jan 08 16:40:42 2024 -0800
@@ -10,12 +10,12 @@
 #include <njs_main.h>
 
 
-static const u_char *njs_sha1_body(njs_sha1_t *ctx, const u_char *data,
+static const u_char *njs_sha1_body(njs_hash_t *ctx, const u_char *data,
     size_t size);
 
 
 void
-njs_sha1_init(njs_sha1_t *ctx)
+njs_sha1_init(njs_hash_t *ctx)
 {
     ctx->a = 0x67452301;
     ctx->b = 0xefcdab89;
@@ -28,7 +28,7 @@ njs_sha1_init(njs_sha1_t *ctx)
 
 
 void
-njs_sha1_update(njs_sha1_t *ctx, const void *data, size_t size)
+njs_sha1_update(njs_hash_t *ctx, const void *data, size_t size)
 {
     size_t  used, free;
 
@@ -59,7 +59,7 @@ njs_sha1_update(njs_sha1_t *ctx, const v
 
 
 void
-njs_sha1_final(u_char result[20], njs_sha1_t *ctx)
+njs_sha1_final(u_char result[32], njs_hash_t *ctx)
 {
     size_t  used, free;
 
@@ -152,7 +152,7 @@ njs_sha1_final(u_char result[20], njs_sh
  */
 
 static const u_char *
-njs_sha1_body(njs_sha1_t *ctx, const u_char *data, size_t size)
+njs_sha1_body(njs_hash_t *ctx, const u_char *data, size_t size)
 {
     uint32_t       a, b, c, d, e, temp;
     uint32_t       saved_a, saved_b, saved_c, saved_d, saved_e;
diff -r 57071ecadeb5 -r 721475693b80 src/njs_sha1.h
--- a/src/njs_sha1.h	Mon Jan 08 16:40:27 2024 -0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,24 +0,0 @@
-
-/*
- * Copyright (C) Igor Sysoev
- * Copyright (C) NGINX, Inc.
- */
-
-
-#ifndef _NJS_SHA1_H_INCLUDED_
-#define _NJS_SHA1_H_INCLUDED_
-
-
-typedef struct {
-    uint64_t  bytes;
-    uint32_t  a, b, c, d, e;
-    u_char    buffer[64];
-} njs_sha1_t;
-
-
-NJS_EXPORT void njs_sha1_init(njs_sha1_t *ctx);
-NJS_EXPORT void njs_sha1_update(njs_sha1_t *ctx, const void *data, size_t size);
-NJS_EXPORT void njs_sha1_final(u_char result[20], njs_sha1_t *ctx);
-
-
-#endif /* _NJS_SHA1_H_INCLUDED_ */
diff -r 57071ecadeb5 -r 721475693b80 src/njs_sha2.c
--- a/src/njs_sha2.c	Mon Jan 08 16:40:27 2024 -0800
+++ b/src/njs_sha2.c	Mon Jan 08 16:40:42 2024 -0800
@@ -10,12 +10,12 @@
 #include <njs_main.h>
 
 
-static const u_char *njs_sha2_body(njs_sha2_t *ctx, const u_char *data,
+static const u_char *njs_sha2_body(njs_hash_t *ctx, const u_char *data,
     size_t size);
 
 
 void
-njs_sha2_init(njs_sha2_t *ctx)
+njs_sha2_init(njs_hash_t *ctx)
 {
     ctx->a = 0x6a09e667;
     ctx->b = 0xbb67ae85;
@@ -31,7 +31,7 @@ njs_sha2_init(njs_sha2_t *ctx)
 
 
 void
-njs_sha2_update(njs_sha2_t *ctx, const void *data, size_t size)
+njs_sha2_update(njs_hash_t *ctx, const void *data, size_t size)
 {
     size_t  used, free;
 
@@ -62,7 +62,7 @@ njs_sha2_update(njs_sha2_t *ctx, const v
 
 
 void
-njs_sha2_final(u_char result[32], njs_sha2_t *ctx)
+njs_sha2_final(u_char result[32], njs_hash_t *ctx)
 {
     size_t  used, free;
 
@@ -172,7 +172,7 @@ njs_sha2_final(u_char result[32], njs_sh
  */
 
 static const u_char *
-njs_sha2_body(njs_sha2_t *ctx, const u_char *data, size_t size)
+njs_sha2_body(njs_hash_t *ctx, const u_char *data, size_t size)
 {
     uint32_t       a, b, c, d, e, f, g, h, s0, s1, temp1, temp2;
     uint32_t       saved_a, saved_b, saved_c, saved_d, saved_e, saved_f,
diff -r 57071ecadeb5 -r 721475693b80 src/njs_sha2.h
--- a/src/njs_sha2.h	Mon Jan 08 16:40:27 2024 -0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,24 +0,0 @@
-
-/*
- * Copyright (C) Dmitry Volyntsev
- * Copyright (C) NGINX, Inc.
- */
-
-
-#ifndef _NJS_SHA2_H_INCLUDED_
-#define _NJS_SHA2_H_INCLUDED_
-
-
-typedef struct {
-    uint64_t  bytes;
-    uint32_t  a, b, c, d, e, f, g, h;
-    u_char    buffer[64];
-} njs_sha2_t;
-
-
-NJS_EXPORT void njs_sha2_init(njs_sha2_t *ctx);
-NJS_EXPORT void njs_sha2_update(njs_sha2_t *ctx, const void *data, size_t size);
-NJS_EXPORT void njs_sha2_final(u_char result[32], njs_sha2_t *ctx);
-
-
-#endif /* _NJS_SHA2_H_INCLUDED_ */


More information about the nginx-devel mailing list