summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIván Ávalos <avalos@disroot.org>2023-06-30 18:02:51 -0600
committerIván Ávalos <avalos@disroot.org>2023-07-26 11:56:15 -0600
commit85e7c09f260b258f372f93ac58407d75b02be49e (patch)
tree278471ee8fbae58c1aca04d76063d9ca049c07a7
parent1abf5e57f94cbdae801a6d1125b8531f2f2b82c7 (diff)
downloadquickjs-tart-85e7c09f260b258f372f93ac58407d75b02be49e.tar.gz
quickjs-tart-85e7c09f260b258f372f93ac58407d75b02be49e.tar.bz2
quickjs-tart-85e7c09f260b258f372f93ac58407d75b02be49e.zip
Add native QuickJS function for argon2id
-rw-r--r--tart_module.c71
1 files changed, 71 insertions, 0 deletions
diff --git a/tart_module.c b/tart_module.c
index 4dfa35a..01cd990 100644
--- a/tart_module.c
+++ b/tart_module.c
@@ -454,6 +454,75 @@ static JSValue js_talercrypto_hash(JSContext *ctx, JSValue this_val,
return make_js_ta_copy(ctx, h, crypto_hash_BYTES);
}
+static JSValue js_talercrypto_hash_argon2id(JSContext *ctx, JSValue this_val,
+ int argc, JSValueConst *argv)
+{
+ size_t pw_len;
+ size_t salt_len;
+ uint32_t iters;
+ uint32_t mem_size;
+ uint32_t hash_len;
+ uint8_t *pw;
+ uint8_t *salt;
+ uint8_t *hash = NULL;
+
+ JSValue ret_val;
+
+ // password: Uint8Array
+ pw = JS_GetArrayBuffer(ctx, &pw_len, argv[0]);
+ if (!pw) {
+ goto exception;
+ }
+
+ // salt: Uint8Array
+ salt = JS_GetArrayBuffer(ctx, &salt_len, argv[1]);
+ if (!salt) {
+ goto exception;
+ }
+ if (salt_len != crypto_pwhash_SALTBYTES) {
+ JS_ThrowTypeError(ctx, "invalid salt size");
+ goto exception;
+ }
+
+ // iterations: number
+ if (0 != JS_ToUint32(ctx, &iters, argv[2])) {
+ goto exception;
+ }
+
+ // memorySize: number (kibibytes)
+ if (0 != JS_ToUint32(ctx, &mem_size, argv[3])) {
+ goto exception;
+ }
+
+ // hashLength: number
+ if (0 != JS_ToUint32(ctx, &hash_len, argv[4])) {
+ goto exception;
+ }
+
+ hash = malloc(hash_len);
+
+ if (crypto_pwhash(hash,
+ hash_len,
+ (const char*) pw,
+ pw_len,
+ salt,
+ iters,
+ mem_size * 1024,
+ crypto_pwhash_ALG_ARGON2ID13) != 0) {
+ JS_ThrowInternalError(ctx, "crypto_pwhash() call failed");
+ goto exception;
+ }
+ ret_val = make_js_ta_copy(ctx, hash, hash_len);
+ done:
+ if (NULL != hash) {
+ free(hash);
+ }
+ return ret_val;
+ exception:
+ ret_val = JS_EXCEPTION;
+ goto done;
+}
+
static JSValue js_talercrypto_eddsa_key_get_public(JSContext *ctx, JSValue this_val,
int argc, JSValueConst *argv)
{
@@ -1310,6 +1379,7 @@ static JSValue js_talercrypto_hash_state_finish(JSContext *ctx, JSValue this_val
}
+
static const JSCFunctionListEntry tart_talercrypto_funcs[] = {
JS_CFUNC_DEF("structuredClone", 1, js_structured_clone),
JS_CFUNC_DEF("encodeUtf8", 1, js_encode_utf8),
@@ -1321,6 +1391,7 @@ static const JSCFunctionListEntry tart_talercrypto_funcs[] = {
JS_CFUNC_DEF("hashStateInit", 0, js_talercrypto_hash_state_init),
JS_CFUNC_DEF("hashStateUpdate", 2, js_talercrypto_hash_state_update),
JS_CFUNC_DEF("hashStateFinish", 1, js_talercrypto_hash_state_finish),
+ JS_CFUNC_DEF("hashArgon2id", 5, js_talercrypto_hash_argon2id),
JS_CFUNC_DEF("eddsaGetPublic", 1, js_talercrypto_eddsa_key_get_public),
JS_CFUNC_DEF("ecdheGetPublic", 1, js_talercrypto_ecdhe_key_get_public),
JS_CFUNC_DEF("eddsaSign", 2, js_talercrypto_eddsa_sign),