tobogganSe connecter

louise

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

143 lignes · 6597 octetsBrut · Historique
1/* Binding of the Argon2 reference implementation for Louise
2 * (packages/argon2/src/lib.lse). Functions follow the extern convention of
3 * louise_rt.h: borrowed arguments, owned results. */
4#include <stdint.h>
5#include <stdlib.h>
6
7#include "argon2.h"
8#include "core.h"
9#include "encoding.h"
10#include "louise_rt.h"
11
12/* Length of the raw hash, in bytes (the encoded string holds it in base64). */
13#define HASH_LEN 32
14
15/* Whether the three cost Ints fit Argon2's parameters. */
16static int costs(tl_value t_cost, tl_value m_cost, tl_value parallelism, int64_t *t, int64_t *m,
17 int64_t *p) {
18 /* An Int may be big (ADR 0014): too big here. */
19 return tl_int_to_i64(t_cost, t) && tl_int_to_i64(m_cost, m) && tl_int_to_i64(parallelism, p)
20 && *t >= 1 && *m >= 8 && *p >= 1 && *t <= UINT32_MAX && *m <= UINT32_MAX
21 && *p <= UINT32_MAX;
22}
23
24/* A borrowed string's bytes, as the non-const pointer argon2_context
25 * wants; NULL when empty (no secret, no associated data). Argon2 only
26 * reads them: ARGON2_FLAG_CLEAR_PASSWORD and _SECRET are never set. */
27static uint8_t *bytes_of(tl_value s) {
28 return tl_string_len(s) == 0 ? NULL : (uint8_t *)tl_string_ptr(s);
29}
30
31/* hash(password, salt, t_cost, m_cost_kib, parallelism)
32 * -> Result<String, String>: the encoded hash, or the error message. */
33tl_value louise_argon2_hash(tl_ctx *ctx, tl_value password, tl_value salt, tl_value t_cost,
34 tl_value m_cost, tl_value parallelism) {
35 int64_t t, m, p;
36 if (!costs(t_cost, m_cost, parallelism, &t, &m, &p))
37 return tl_error(ctx, tl_string_cstr(ctx, "invalid argon2 parameters"));
38 size_t encoded_len = argon2_encodedlen((uint32_t)t, (uint32_t)m, (uint32_t)p,
39 (uint32_t)tl_string_len(salt), HASH_LEN, Argon2_id);
40 char encoded[512];
41 if (encoded_len > sizeof encoded)
42 return tl_error(ctx, tl_string_cstr(ctx, "argon2 salt too long"));
43 int rc = argon2id_hash_encoded((uint32_t)t, (uint32_t)m, (uint32_t)p, tl_string_ptr(password),
44 tl_string_len(password), tl_string_ptr(salt),
45 tl_string_len(salt), HASH_LEN, encoded, sizeof encoded);
46 if (rc != ARGON2_OK) return tl_error(ctx, tl_string_cstr(ctx, argon2_error_message(rc)));
47 return tl_ok(ctx, tl_string_cstr(ctx, encoded));
48}
49
50/* verify(encoded, password) -> Result<Bool, String>: Ok(True) on a match,
51 * Ok(False) on a mismatch, or the error message (e.g. a malformed hash). */
52tl_value louise_argon2_verify(tl_ctx *ctx, tl_value encoded, tl_value password) {
53 int rc = argon2id_verify(tl_string_ptr(encoded), tl_string_ptr(password),
54 tl_string_len(password));
55 if (rc == ARGON2_OK) return tl_ok(ctx, tl_bool(true));
56 if (rc == ARGON2_VERIFY_MISMATCH) return tl_ok(ctx, tl_bool(false));
57 return tl_error(ctx, tl_string_cstr(ctx, argon2_error_message(rc)));
58}
59
60/* hash_keyed(password, secret, ad, salt, t_cost, m_cost_kib, parallelism)
61 * -> Result<String, String>: louise_argon2_hash with RFC 9106's secret
62 * value K and associated data X, built as the reference argon2_hash
63 * builds its context. The encoded string records neither. */
64tl_value louise_argon2_hash_keyed(tl_ctx *ctx, tl_value password, tl_value secret, tl_value ad,
65 tl_value salt, tl_value t_cost, tl_value m_cost,
66 tl_value parallelism) {
67 int64_t t, m, p;
68 if (!costs(t_cost, m_cost, parallelism, &t, &m, &p))
69 return tl_error(ctx, tl_string_cstr(ctx, "invalid argon2 parameters"));
70 if (tl_string_len(password) > ARGON2_MAX_PWD_LENGTH || tl_string_len(salt) > ARGON2_MAX_SALT_LENGTH
71 || tl_string_len(secret) > ARGON2_MAX_SECRET || tl_string_len(ad) > ARGON2_MAX_AD_LENGTH)
72 return tl_error(ctx, tl_string_cstr(ctx, "argon2 input too long"));
73 uint8_t out[HASH_LEN];
74 argon2_context c = {0};
75 c.out = out;
76 c.outlen = HASH_LEN;
77 c.pwd = bytes_of(password);
78 c.pwdlen = (uint32_t)tl_string_len(password);
79 c.salt = bytes_of(salt);
80 c.saltlen = (uint32_t)tl_string_len(salt);
81 c.secret = bytes_of(secret);
82 c.secretlen = (uint32_t)tl_string_len(secret);
83 c.ad = bytes_of(ad);
84 c.adlen = (uint32_t)tl_string_len(ad);
85 c.t_cost = (uint32_t)t;
86 c.m_cost = (uint32_t)m;
87 c.lanes = (uint32_t)p;
88 c.threads = (uint32_t)p;
89 c.version = ARGON2_VERSION_13;
90 c.flags = ARGON2_DEFAULT_FLAGS;
91 int rc = argon2_ctx(&c, Argon2_id);
92 char encoded[512];
93 if (rc == ARGON2_OK && encode_string(encoded, sizeof encoded, &c, Argon2_id) != ARGON2_OK)
94 rc = ARGON2_ENCODING_FAIL;
95 clear_internal_memory(out, sizeof out);
96 if (rc != ARGON2_OK) {
97 clear_internal_memory(encoded, sizeof encoded);
98 return tl_error(ctx, tl_string_cstr(ctx, argon2_error_message(rc)));
99 }
100 return tl_ok(ctx, tl_string_cstr(ctx, encoded));
101}
102
103/* verify_keyed(encoded, password, secret) -> Result<Bool, String>:
104 * louise_argon2_verify with RFC 9106's secret value K, as the reference
105 * argon2_verify decodes and checks (decode_string clears the secret, so
106 * it is set after decoding). */
107tl_value louise_argon2_verify_keyed(tl_ctx *ctx, tl_value encoded, tl_value password,
108 tl_value secret) {
109 size_t encoded_len = tl_string_len(encoded);
110 if (tl_string_len(password) > ARGON2_MAX_PWD_LENGTH || tl_string_len(secret) > ARGON2_MAX_SECRET)
111 return tl_error(ctx, tl_string_cstr(ctx, "argon2 input too long"));
112 if (encoded_len == 0 || encoded_len > UINT32_MAX)
113 return tl_error(ctx, tl_string_cstr(ctx, argon2_error_message(ARGON2_DECODING_FAIL)));
114 argon2_context c = {0};
115 uint8_t *desired = NULL;
116 c.saltlen = (uint32_t)encoded_len;
117 c.outlen = (uint32_t)encoded_len;
118 c.salt = malloc(c.saltlen);
119 c.out = malloc(c.outlen);
120 int rc = ARGON2_OK;
121 if (!c.salt || !c.out) rc = ARGON2_MEMORY_ALLOCATION_ERROR;
122 if (rc == ARGON2_OK) {
123 c.pwd = bytes_of(password);
124 c.pwdlen = (uint32_t)tl_string_len(password);
125 rc = decode_string(&c, tl_string_ptr(encoded), Argon2_id);
126 }
127 if (rc == ARGON2_OK) {
128 desired = c.out;
129 c.out = malloc(c.outlen);
130 if (!c.out) rc = ARGON2_MEMORY_ALLOCATION_ERROR;
131 }
132 if (rc == ARGON2_OK) {
133 c.secret = bytes_of(secret);
134 c.secretlen = (uint32_t)tl_string_len(secret);
135 rc = argon2_verify_ctx(&c, (char *)desired, Argon2_id);
136 }
137 free(c.salt);
138 free(c.out);
139 free(desired);
140 if (rc == ARGON2_OK) return tl_ok(ctx, tl_bool(true));
141 if (rc == ARGON2_VERIFY_MISMATCH) return tl_ok(ctx, tl_bool(false));
142 return tl_error(ctx, tl_string_cstr(ctx, argon2_error_message(rc)));
143}