]> git.kaiwu.me - njs.git/commitdiff
Unifying hash function prototypes.
authorDmitry Volyntsev <xeioex@nginx.com>
Tue, 9 Jan 2024 00:40:42 +0000 (16:40 -0800)
committerDmitry Volyntsev <xeioex@nginx.com>
Tue, 9 Jan 2024 00:40:42 +0000 (16:40 -0800)
This fixes UndefinedBehaviorSanitizer warning "call to function through
pointer to incorrect function type".

Found by UndefinedBehaviorSanitizer.

external/njs_crypto_module.c
src/njs_hash.h [new file with mode: 0644]
src/njs_main.h
src/njs_md5.c
src/njs_md5.h [deleted file]
src/njs_sha1.c
src/njs_sha1.h [deleted file]
src/njs_sha2.c
src/njs_sha2.h [deleted file]

index af5ddf2fb37a8357554348a7122f863a2992e954..b66501bdf4a7ef4b33a595bc34aa3b1142b279db 100644 (file)
@@ -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_algorithms[] = {
    {
      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_value_t *args, njs_uint_t nargs,
 
     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, njs_value_t *args, njs_uint_t nargs,
     }
 
     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, njs_value_t *args, njs_uint_t nargs,
 
     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_value_t *args, njs_uint_t nargs,
     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_value_t *args, njs_uint_t nargs,
          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 --git a/src/njs_hash.h b/src/njs_hash.h
new file mode 100644 (file)
index 0000000..acc5e45
--- /dev/null
@@ -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_ */
index dc764c684e1cd44be0dfd5acc29b6c5e101f3782..29aba719fd550f9d764203017f2e62939aea0ce0 100644 (file)
@@ -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>
index 4c455b30d63bf27b3ae8ae0a2194ed2b2f4a8d3d..a6b0080387940b41df42e23df678aa3ae7c2ba48 100644 (file)
@@ -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 void *data, size_t size)
 
 
 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_t *ctx)
  */
 
 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 --git a/src/njs_md5.h b/src/njs_md5.h
deleted file mode 100644 (file)
index 23800ee..0000000
+++ /dev/null
@@ -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_ */
index fdadec2b1cc8062a60e0152ed8c6fbc7d4d29cc3..0f5f4e9b28606eee7f63e636b470513245ccabdf 100644 (file)
 #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 void *data, size_t size)
 
 
 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_sha1_t *ctx)
  */
 
 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 --git a/src/njs_sha1.h b/src/njs_sha1.h
deleted file mode 100644 (file)
index 1bbd94e..0000000
+++ /dev/null
@@ -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_ */
index 6beba26078c602d8dfc68d30fab4e959964b0c1c..8d051e491784fa3950fa62335f62327a20feeb04 100644 (file)
 #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 void *data, size_t size)
 
 
 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_sha2_t *ctx)
  */
 
 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 --git a/src/njs_sha2.h b/src/njs_sha2.h
deleted file mode 100644 (file)
index 5a82a8d..0000000
+++ /dev/null
@@ -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_ */