/* Binding of the Argon2 reference implementation for Louise * (packages/argon2/src/lib.lse). Functions follow the extern convention of * louise_rt.h: borrowed arguments, owned results. */ #include #include #include "argon2.h" #include "core.h" #include "encoding.h" #include "louise_rt.h" /* Length of the raw hash, in bytes (the encoded string holds it in base64). */ #define HASH_LEN 32 /* Whether the three cost Ints fit Argon2's parameters. */ static int costs(tl_value t_cost, tl_value m_cost, tl_value parallelism, int64_t *t, int64_t *m, int64_t *p) { /* An Int may be big (ADR 0014): too big here. */ return tl_int_to_i64(t_cost, t) && tl_int_to_i64(m_cost, m) && tl_int_to_i64(parallelism, p) && *t >= 1 && *m >= 8 && *p >= 1 && *t <= UINT32_MAX && *m <= UINT32_MAX && *p <= UINT32_MAX; } /* A borrowed string's bytes, as the non-const pointer argon2_context * wants; NULL when empty (no secret, no associated data). Argon2 only * reads them: ARGON2_FLAG_CLEAR_PASSWORD and _SECRET are never set. */ static uint8_t *bytes_of(tl_value s) { return tl_string_len(s) == 0 ? NULL : (uint8_t *)tl_string_ptr(s); } /* hash(password, salt, t_cost, m_cost_kib, parallelism) * -> Result: the encoded hash, or the error message. */ tl_value louise_argon2_hash(tl_ctx *ctx, tl_value password, tl_value salt, tl_value t_cost, tl_value m_cost, tl_value parallelism) { int64_t t, m, p; if (!costs(t_cost, m_cost, parallelism, &t, &m, &p)) return tl_error(ctx, tl_string_cstr(ctx, "invalid argon2 parameters")); size_t encoded_len = argon2_encodedlen((uint32_t)t, (uint32_t)m, (uint32_t)p, (uint32_t)tl_string_len(salt), HASH_LEN, Argon2_id); char encoded[512]; if (encoded_len > sizeof encoded) return tl_error(ctx, tl_string_cstr(ctx, "argon2 salt too long")); int rc = argon2id_hash_encoded((uint32_t)t, (uint32_t)m, (uint32_t)p, tl_string_ptr(password), tl_string_len(password), tl_string_ptr(salt), tl_string_len(salt), HASH_LEN, encoded, sizeof encoded); if (rc != ARGON2_OK) return tl_error(ctx, tl_string_cstr(ctx, argon2_error_message(rc))); return tl_ok(ctx, tl_string_cstr(ctx, encoded)); } /* verify(encoded, password) -> Result: Ok(True) on a match, * Ok(False) on a mismatch, or the error message (e.g. a malformed hash). */ tl_value louise_argon2_verify(tl_ctx *ctx, tl_value encoded, tl_value password) { int rc = argon2id_verify(tl_string_ptr(encoded), tl_string_ptr(password), tl_string_len(password)); if (rc == ARGON2_OK) return tl_ok(ctx, tl_bool(true)); if (rc == ARGON2_VERIFY_MISMATCH) return tl_ok(ctx, tl_bool(false)); return tl_error(ctx, tl_string_cstr(ctx, argon2_error_message(rc))); } /* hash_keyed(password, secret, ad, salt, t_cost, m_cost_kib, parallelism) * -> Result: louise_argon2_hash with RFC 9106's secret * value K and associated data X, built as the reference argon2_hash * builds its context. The encoded string records neither. */ tl_value louise_argon2_hash_keyed(tl_ctx *ctx, tl_value password, tl_value secret, tl_value ad, tl_value salt, tl_value t_cost, tl_value m_cost, tl_value parallelism) { int64_t t, m, p; if (!costs(t_cost, m_cost, parallelism, &t, &m, &p)) return tl_error(ctx, tl_string_cstr(ctx, "invalid argon2 parameters")); if (tl_string_len(password) > ARGON2_MAX_PWD_LENGTH || tl_string_len(salt) > ARGON2_MAX_SALT_LENGTH || tl_string_len(secret) > ARGON2_MAX_SECRET || tl_string_len(ad) > ARGON2_MAX_AD_LENGTH) return tl_error(ctx, tl_string_cstr(ctx, "argon2 input too long")); uint8_t out[HASH_LEN]; argon2_context c = {0}; c.out = out; c.outlen = HASH_LEN; c.pwd = bytes_of(password); c.pwdlen = (uint32_t)tl_string_len(password); c.salt = bytes_of(salt); c.saltlen = (uint32_t)tl_string_len(salt); c.secret = bytes_of(secret); c.secretlen = (uint32_t)tl_string_len(secret); c.ad = bytes_of(ad); c.adlen = (uint32_t)tl_string_len(ad); c.t_cost = (uint32_t)t; c.m_cost = (uint32_t)m; c.lanes = (uint32_t)p; c.threads = (uint32_t)p; c.version = ARGON2_VERSION_13; c.flags = ARGON2_DEFAULT_FLAGS; int rc = argon2_ctx(&c, Argon2_id); char encoded[512]; if (rc == ARGON2_OK && encode_string(encoded, sizeof encoded, &c, Argon2_id) != ARGON2_OK) rc = ARGON2_ENCODING_FAIL; clear_internal_memory(out, sizeof out); if (rc != ARGON2_OK) { clear_internal_memory(encoded, sizeof encoded); return tl_error(ctx, tl_string_cstr(ctx, argon2_error_message(rc))); } return tl_ok(ctx, tl_string_cstr(ctx, encoded)); } /* verify_keyed(encoded, password, secret) -> Result: * louise_argon2_verify with RFC 9106's secret value K, as the reference * argon2_verify decodes and checks (decode_string clears the secret, so * it is set after decoding). */ tl_value louise_argon2_verify_keyed(tl_ctx *ctx, tl_value encoded, tl_value password, tl_value secret) { size_t encoded_len = tl_string_len(encoded); if (tl_string_len(password) > ARGON2_MAX_PWD_LENGTH || tl_string_len(secret) > ARGON2_MAX_SECRET) return tl_error(ctx, tl_string_cstr(ctx, "argon2 input too long")); if (encoded_len == 0 || encoded_len > UINT32_MAX) return tl_error(ctx, tl_string_cstr(ctx, argon2_error_message(ARGON2_DECODING_FAIL))); argon2_context c = {0}; uint8_t *desired = NULL; c.saltlen = (uint32_t)encoded_len; c.outlen = (uint32_t)encoded_len; c.salt = malloc(c.saltlen); c.out = malloc(c.outlen); int rc = ARGON2_OK; if (!c.salt || !c.out) rc = ARGON2_MEMORY_ALLOCATION_ERROR; if (rc == ARGON2_OK) { c.pwd = bytes_of(password); c.pwdlen = (uint32_t)tl_string_len(password); rc = decode_string(&c, tl_string_ptr(encoded), Argon2_id); } if (rc == ARGON2_OK) { desired = c.out; c.out = malloc(c.outlen); if (!c.out) rc = ARGON2_MEMORY_ALLOCATION_ERROR; } if (rc == ARGON2_OK) { c.secret = bytes_of(secret); c.secretlen = (uint32_t)tl_string_len(secret); rc = argon2_verify_ctx(&c, (char *)desired, Argon2_id); } free(c.salt); free(c.out); free(desired); if (rc == ARGON2_OK) return tl_ok(ctx, tl_bool(true)); if (rc == ARGON2_VERIFY_MISMATCH) return tl_ok(ctx, tl_bool(false)); return tl_error(ctx, tl_string_cstr(ctx, argon2_error_message(rc))); }