diff --git a/package/gluon-lib-ecdsa/src/ecdsa.c b/package/gluon-lib-ecdsa/src/ecdsa.c index 252d489d..628d6bf7 100644 --- a/package/gluon-lib-ecdsa/src/ecdsa.c +++ b/package/gluon-lib-ecdsa/src/ecdsa.c @@ -32,12 +32,17 @@ limitations under the License. #define ECDSA "gluon.ecdsa" +// TODO: fix all the memory leaks + static bool verify(lua_State *L, const char *data, const char *sig, const char *key) { struct verify_params params = { - .good_signatures = 1, - .data = data + .good_signatures = 1 }; + if (!hash_data(¶ms, data)) { + return luaL_error(L, "failed hashing data"); + } + if (!load_signatures(¶ms, 1, &sig, false)) { return luaL_error(L, "failed loading signature"); } diff --git a/package/gluon-lib-ecdsa/src/ecdsa_util.c b/package/gluon-lib-ecdsa/src/ecdsa_util.c index 856a987f..54e8ad32 100644 --- a/package/gluon-lib-ecdsa/src/ecdsa_util.c +++ b/package/gluon-lib-ecdsa/src/ecdsa_util.c @@ -11,16 +11,9 @@ #include bool do_verify(struct verify_params* params) { - ecdsa_sha256_context_t hash_ctx; - ecdsa_sha256_init(&hash_ctx); - ecdsa_sha256_update(&hash_ctx, params->data, strlen(params->data)); - - ecc_int256_t hash; - ecdsa_sha256_final(&hash_ctx, hash.p); - ecdsa_verify_context_t ctxs[params->n_signatures]; for (size_t i = 0; i < params->n_signatures; i++) - ecdsa_verify_prepare_legacy(&ctxs[i], &hash, params->signatures[i]); + ecdsa_verify_prepare_legacy(&ctxs[i], ¶ms->hash, params->signatures[i]); long unsigned int good_signatures = ecdsa_verify_list_legacy(ctxs, params->n_signatures, params->pubkeys, params->n_pubkeys); @@ -31,6 +24,16 @@ bool do_verify(struct verify_params* params) { return true; } +int hash_data(struct verify_params* params, const char* data) { + ecdsa_sha256_context_t hash_ctx; + ecdsa_sha256_init(&hash_ctx); + ecdsa_sha256_update(&hash_ctx, data, strlen(data)); + + ecdsa_sha256_final(&hash_ctx, params->hash.p); + + return 1; +} + int load_pubkeys(struct verify_params* params, const size_t n_pubkeys, const char **pubkeys_str, const bool ignore_pubkeys) { params->n_pubkeys = n_pubkeys; params->pubkeys = safe_malloc(n_pubkeys * sizeof(ecc_25519_work_t)); diff --git a/package/gluon-lib-ecdsa/src/ecdsa_util.h b/package/gluon-lib-ecdsa/src/ecdsa_util.h index 34576d1f..3a13e179 100644 --- a/package/gluon-lib-ecdsa/src/ecdsa_util.h +++ b/package/gluon-lib-ecdsa/src/ecdsa_util.h @@ -2,7 +2,7 @@ #include struct verify_params { - const char* data; + ecc_int256_t hash; size_t n_signatures; ecdsa_signature_t **signatures; @@ -14,5 +14,6 @@ struct verify_params { }; bool do_verify(struct verify_params* params); +int hash_data(struct verify_params* params, const char* data); int load_pubkeys(struct verify_params* params, const size_t n_pubkeys, const char **pubkeys_str, const bool ignore_pubkeys); int load_signatures(struct verify_params* params, const size_t n_signatures, const char **signatures_str, const bool ignore_signatures);