mirror of
https://github.com/gadgethd/ukmesh.git
synced 2026-12-06 03:14:09 +00:00
85 lines
3.3 KiB
C
85 lines
3.3 KiB
C
// Reuse OpenSSL's keyed HMAC state for the many small messages in a rebuild.
|
|
// Each returned synchronous JS function owns one context on its Node thread.
|
|
#include <node_api.h>
|
|
#include <openssl/core_names.h>
|
|
#include <openssl/evp.h>
|
|
#include <openssl/params.h>
|
|
|
|
static napi_value mac_error(napi_env env, const char *message) {
|
|
napi_throw_error(env, NULL, message);
|
|
return NULL;
|
|
}
|
|
|
|
static void dispose_mac(napi_env env, void *data, void *hint) {
|
|
(void)env;
|
|
(void)hint;
|
|
EVP_MAC_CTX_free(data);
|
|
}
|
|
|
|
static int buffer_argument(napi_env env, napi_callback_info info,
|
|
void **bytes, size_t *length, void **context) {
|
|
napi_value arg;
|
|
size_t argc = 1;
|
|
bool is_buffer = false;
|
|
return napi_get_cb_info(env, info, &argc, &arg, NULL, context) == napi_ok &&
|
|
argc == 1 && napi_is_buffer(env, arg, &is_buffer) == napi_ok && is_buffer &&
|
|
napi_get_buffer_info(env, arg, bytes, length) == napi_ok;
|
|
}
|
|
|
|
static napi_value transport_code(napi_env env, napi_callback_info info) {
|
|
unsigned char digest[32];
|
|
void *data, *bound;
|
|
size_t length, written = 0;
|
|
napi_value result;
|
|
if (!buffer_argument(env, info, &data, &length, &bound))
|
|
return mac_error(env, "Region MAC requires a message buffer");
|
|
|
|
// A null key resets the operation using the key already installed.
|
|
EVP_MAC_CTX *ctx = bound;
|
|
if (!EVP_MAC_init(ctx, NULL, 0, NULL) || !EVP_MAC_update(ctx, data, length) ||
|
|
!EVP_MAC_final(ctx, digest, &written, sizeof(digest)) || written != sizeof(digest))
|
|
return mac_error(env, "Region HMAC computation failed");
|
|
uint32_t code = digest[0] | ((uint32_t)digest[1] << 8);
|
|
if (napi_create_uint32(env, code, &result) != napi_ok)
|
|
return mac_error(env, "Could not return region transport code");
|
|
return result;
|
|
}
|
|
|
|
static napi_value create_mac(napi_env env, napi_callback_info info) {
|
|
void *key;
|
|
size_t length;
|
|
napi_value function;
|
|
if (!buffer_argument(env, info, &key, &length, NULL) || length != 16)
|
|
return mac_error(env, "Region MAC requires a 16-byte key");
|
|
|
|
char algorithm[] = "SHA256";
|
|
OSSL_PARAM params[] = {
|
|
OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, algorithm, 0),
|
|
OSSL_PARAM_construct_end()
|
|
};
|
|
EVP_MAC *mac = EVP_MAC_fetch(NULL, "HMAC", NULL);
|
|
if (!mac) return mac_error(env, "OpenSSL HMAC is unavailable");
|
|
EVP_MAC_CTX *ctx = EVP_MAC_CTX_new(mac);
|
|
EVP_MAC_free(mac);
|
|
if (!ctx) return mac_error(env, "Could not allocate region HMAC context");
|
|
if (!EVP_MAC_init(ctx, key, length, params) ||
|
|
napi_create_function(env, "transportCode", NAPI_AUTO_LENGTH,
|
|
transport_code, ctx, &function) != napi_ok ||
|
|
napi_add_finalizer(env, function, ctx, dispose_mac, NULL, NULL) != napi_ok) {
|
|
EVP_MAC_CTX_free(ctx);
|
|
return mac_error(env, "Could not initialize region HMAC context");
|
|
}
|
|
return function;
|
|
}
|
|
|
|
static napi_value register_module(napi_env env, napi_value exports) {
|
|
(void)exports;
|
|
napi_value function;
|
|
if (napi_create_function(env, "createRegionMac", NAPI_AUTO_LENGTH,
|
|
create_mac, NULL, &function) != napi_ok)
|
|
return mac_error(env, "Could not export region HMAC");
|
|
return function;
|
|
}
|
|
|
|
NAPI_MODULE(NODE_GYP_MODULE_NAME, register_module)
|