Coverage Report

Created: 2025-08-11 07:04

/src/openssl34/providers/implementations/kdfs/argon2.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2022-2024 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 *
9
 * RFC 9106 Argon2 (see https://www.rfc-editor.org/rfc/rfc9106.txt)
10
 *
11
 */
12
13
#include <stdlib.h>
14
#include <stddef.h>
15
#include <stdarg.h>
16
#include <string.h>
17
#include <openssl/e_os2.h>
18
#include <openssl/evp.h>
19
#include <openssl/objects.h>
20
#include <openssl/crypto.h>
21
#include <openssl/kdf.h>
22
#include <openssl/err.h>
23
#include <openssl/core_names.h>
24
#include <openssl/params.h>
25
#include <openssl/thread.h>
26
#include <openssl/proverr.h>
27
#include "internal/thread.h"
28
#include "internal/numbers.h"
29
#include "internal/endian.h"
30
#include "crypto/evp.h"
31
#include "prov/implementations.h"
32
#include "prov/provider_ctx.h"
33
#include "prov/providercommon.h"
34
#include "prov/blake2.h"
35
36
#if defined(OPENSSL_NO_DEFAULT_THREAD_POOL) && defined(OPENSSL_NO_THREAD_POOL)
37
# define ARGON2_NO_THREADS
38
#endif
39
40
#if !defined(OPENSSL_THREADS)
41
# define ARGON2_NO_THREADS
42
#endif
43
44
#ifndef OPENSSL_NO_ARGON2
45
46
391
# define ARGON2_MIN_LANES 1u
47
368
# define ARGON2_MAX_LANES 0xFFFFFFu
48
438
# define ARGON2_MIN_THREADS 1u
49
368
# define ARGON2_MAX_THREADS 0xFFFFFFu
50
9.95k
# define ARGON2_SYNC_POINTS 4u
51
838
# define ARGON2_MIN_OUT_LENGTH 4u
52
# define ARGON2_MAX_OUT_LENGTH 0xFFFFFFFFu
53
947
# define ARGON2_MIN_MEMORY (2 * ARGON2_SYNC_POINTS)
54
# define ARGON2_MIN(a, b) ((a) < (b) ? (a) : (b))
55
# define ARGON2_MAX_MEMORY 0xFFFFFFFFu
56
403
# define ARGON2_MIN_TIME 1u
57
# define ARGON2_MAX_TIME 0xFFFFFFFFu
58
# define ARGON2_MIN_PWD_LENGTH 0u
59
572
# define ARGON2_MAX_PWD_LENGTH 0xFFFFFFFFu
60
# define ARGON2_MIN_AD_LENGTH 0u
61
493
# define ARGON2_MAX_AD_LENGTH 0xFFFFFFFFu
62
651
# define ARGON2_MIN_SALT_LENGTH 8u
63
493
# define ARGON2_MAX_SALT_LENGTH 0xFFFFFFFFu
64
# define ARGON2_MIN_SECRET 0u
65
493
# define ARGON2_MAX_SECRET 0xFFFFFFFFu
66
7.36M
# define ARGON2_BLOCK_SIZE 1024
67
7.36M
# define ARGON2_QWORDS_IN_BLOCK ((ARGON2_BLOCK_SIZE) / 8)
68
# define ARGON2_OWORDS_IN_BLOCK ((ARGON2_BLOCK_SIZE) / 16)
69
# define ARGON2_HWORDS_IN_BLOCK ((ARGON2_BLOCK_SIZE) / 32)
70
# define ARGON2_512BIT_WORDS_IN_BLOCK ((ARGON2_BLOCK_SIZE) / 64)
71
18.4k
# define ARGON2_ADDRESSES_IN_BLOCK 128
72
7.75k
# define ARGON2_PREHASH_DIGEST_LENGTH 64
73
# define ARGON2_PREHASH_SEED_LENGTH \
74
3.10k
    (ARGON2_PREHASH_DIGEST_LENGTH + (2 * sizeof(uint32_t)))
75
76
572
# define ARGON2_DEFAULT_OUTLEN 64u
77
572
# define ARGON2_DEFAULT_T_COST 3u
78
572
# define ARGON2_DEFAULT_M_COST ARGON2_MIN_MEMORY
79
572
# define ARGON2_DEFAULT_LANES  1u
80
572
# define ARGON2_DEFAULT_THREADS 1u
81
572
# define ARGON2_DEFAULT_VERSION ARGON2_VERSION_NUMBER
82
83
# undef G
84
# define G(a, b, c, d)                                                        \
85
3.36M
    do {                                                                      \
86
3.36M
        a = a + b + 2 * mul_lower(a, b);                                      \
87
3.36M
        d = rotr64(d ^ a, 32);                                                \
88
3.36M
        c = c + d + 2 * mul_lower(c, d);                                      \
89
3.36M
        b = rotr64(b ^ c, 24);                                                \
90
3.36M
        a = a + b + 2 * mul_lower(a, b);                                      \
91
3.36M
        d = rotr64(d ^ a, 16);                                                \
92
3.36M
        c = c + d + 2 * mul_lower(c, d);                                      \
93
3.36M
        b = rotr64(b ^ c, 63);                                                \
94
3.36M
    } while ((void)0, 0)
95
96
# undef PERMUTATION_P
97
# define PERMUTATION_P(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11,      \
98
                       v12, v13, v14, v15)                                    \
99
420k
    do {                                                                      \
100
420k
        G(v0, v4, v8, v12);                                                   \
101
420k
        G(v1, v5, v9, v13);                                                   \
102
420k
        G(v2, v6, v10, v14);                                                  \
103
420k
        G(v3, v7, v11, v15);                                                  \
104
420k
        G(v0, v5, v10, v15);                                                  \
105
420k
        G(v1, v6, v11, v12);                                                  \
106
420k
        G(v2, v7, v8, v13);                                                   \
107
420k
        G(v3, v4, v9, v14);                                                   \
108
420k
    } while ((void)0, 0)
109
110
# undef PERMUTATION_P_COLUMN
111
# define PERMUTATION_P_COLUMN(x, i)                                           \
112
210k
    do {                                                                      \
113
210k
        uint64_t *base = &x[16 * i];                                          \
114
210k
        PERMUTATION_P(                                                        \
115
210k
            *base,        *(base + 1),  *(base + 2),  *(base + 3),            \
116
210k
            *(base + 4),  *(base + 5),  *(base + 6),  *(base + 7),            \
117
210k
            *(base + 8),  *(base + 9),  *(base + 10), *(base + 11),           \
118
210k
            *(base + 12), *(base + 13), *(base + 14), *(base + 15)            \
119
210k
        );                                                                    \
120
210k
    } while ((void)0, 0)
121
122
# undef PERMUTATION_P_ROW
123
# define PERMUTATION_P_ROW(x, i)                                              \
124
210k
    do {                                                                      \
125
210k
        uint64_t *base = &x[2 * i];                                           \
126
210k
        PERMUTATION_P(                                                        \
127
210k
            *base,        *(base + 1),  *(base + 16),  *(base + 17),          \
128
210k
            *(base + 32), *(base + 33), *(base + 48),  *(base + 49),          \
129
210k
            *(base + 64), *(base + 65), *(base + 80),  *(base + 81),          \
130
210k
            *(base + 96), *(base + 97), *(base + 112), *(base + 113)          \
131
210k
        );                                                                    \
132
210k
    } while ((void)0, 0)
133
134
typedef struct {
135
    uint64_t v[ARGON2_QWORDS_IN_BLOCK];
136
} BLOCK;
137
138
typedef enum {
139
    ARGON2_VERSION_10 = 0x10,
140
    ARGON2_VERSION_13 = 0x13,
141
    ARGON2_VERSION_NUMBER = ARGON2_VERSION_13
142
} ARGON2_VERSION;
143
144
typedef enum {
145
    ARGON2_D  = 0,
146
    ARGON2_I  = 1,
147
    ARGON2_ID = 2
148
} ARGON2_TYPE;
149
150
typedef struct {
151
    uint32_t pass;
152
    uint32_t lane;
153
    uint8_t slice;
154
    uint32_t index;
155
} ARGON2_POS;
156
157
typedef struct {
158
    void *provctx;
159
    uint32_t outlen;
160
    uint8_t *pwd;
161
    uint32_t pwdlen;
162
    uint8_t *salt;
163
    uint32_t saltlen;
164
    uint8_t *secret;
165
    uint32_t secretlen;
166
    uint8_t *ad;
167
    uint32_t adlen;
168
    uint32_t t_cost;
169
    uint32_t m_cost;
170
    uint32_t lanes;
171
    uint32_t threads;
172
    uint32_t version;
173
    uint32_t early_clean;
174
    ARGON2_TYPE type;
175
    BLOCK *memory;
176
    uint32_t passes;
177
    uint32_t memory_blocks;
178
    uint32_t segment_length;
179
    uint32_t lane_length;
180
    OSSL_LIB_CTX *libctx;
181
    EVP_MD *md;
182
    EVP_MAC *mac;
183
    char *propq;
184
} KDF_ARGON2;
185
186
typedef struct {
187
    ARGON2_POS pos;
188
    KDF_ARGON2 *ctx;
189
} ARGON2_THREAD_DATA;
190
191
static OSSL_FUNC_kdf_newctx_fn kdf_argon2i_new;
192
static OSSL_FUNC_kdf_newctx_fn kdf_argon2d_new;
193
static OSSL_FUNC_kdf_newctx_fn kdf_argon2id_new;
194
static OSSL_FUNC_kdf_freectx_fn kdf_argon2_free;
195
static OSSL_FUNC_kdf_reset_fn kdf_argon2_reset;
196
static OSSL_FUNC_kdf_derive_fn kdf_argon2_derive;
197
static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_argon2_settable_ctx_params;
198
static OSSL_FUNC_kdf_set_ctx_params_fn kdf_argon2_set_ctx_params;
199
200
static void kdf_argon2_init(KDF_ARGON2 *ctx, ARGON2_TYPE t);
201
static void *kdf_argon2d_new(void *provctx);
202
static void *kdf_argon2i_new(void *provctx);
203
static void *kdf_argon2id_new(void *provctx);
204
static void kdf_argon2_free(void *vctx);
205
static int kdf_argon2_derive(void *vctx, unsigned char *out, size_t outlen,
206
                             const OSSL_PARAM params[]);
207
static void kdf_argon2_reset(void *vctx);
208
static int kdf_argon2_ctx_set_threads(KDF_ARGON2 *ctx, uint32_t threads);
209
static int kdf_argon2_ctx_set_lanes(KDF_ARGON2 *ctx, uint32_t lanes);
210
static int kdf_argon2_ctx_set_t_cost(KDF_ARGON2 *ctx, uint32_t t_cost);
211
static int kdf_argon2_ctx_set_m_cost(KDF_ARGON2 *ctx, uint32_t m_cost);
212
static int kdf_argon2_ctx_set_out_length(KDF_ARGON2 *ctx, uint32_t outlen);
213
static int kdf_argon2_ctx_set_secret(KDF_ARGON2 *ctx, const OSSL_PARAM *p);
214
static int kdf_argon2_ctx_set_pwd(KDF_ARGON2 *ctx, const OSSL_PARAM *p);
215
static int kdf_argon2_ctx_set_salt(KDF_ARGON2 *ctx, const OSSL_PARAM *p);
216
static int kdf_argon2_ctx_set_ad(KDF_ARGON2 *ctx, const OSSL_PARAM *p);
217
static int kdf_argon2_set_ctx_params(void *vctx, const OSSL_PARAM params[]);
218
static int kdf_argon2_get_ctx_params(void *vctx, OSSL_PARAM params[]);
219
static int kdf_argon2_ctx_set_version(KDF_ARGON2 *ctx, uint32_t version);
220
static const OSSL_PARAM *kdf_argon2_settable_ctx_params(ossl_unused void *ctx,
221
                                                        ossl_unused void *p_ctx);
222
static const OSSL_PARAM *kdf_argon2_gettable_ctx_params(ossl_unused void *ctx,
223
                                                        ossl_unused void *p_ctx);
224
225
static ossl_inline uint64_t load64(const uint8_t *src);
226
static ossl_inline void store32(uint8_t *dst, uint32_t w);
227
static ossl_inline void store64(uint8_t *dst, uint64_t w);
228
static ossl_inline uint64_t rotr64(const uint64_t w, const unsigned int c);
229
static ossl_inline uint64_t mul_lower(uint64_t x, uint64_t y);
230
231
static void init_block_value(BLOCK *b, uint8_t in);
232
static void copy_block(BLOCK *dst, const BLOCK *src);
233
static void xor_block(BLOCK *dst, const BLOCK *src);
234
static void load_block(BLOCK *dst, const void *input);
235
static void store_block(void *output, const BLOCK *src);
236
static void fill_first_blocks(uint8_t *blockhash, const KDF_ARGON2 *ctx);
237
static void fill_block(const BLOCK *prev, const BLOCK *ref, BLOCK *next,
238
                       int with_xor);
239
240
static void next_addresses(BLOCK *address_block, BLOCK *input_block,
241
                           const BLOCK *zero_block);
242
static int data_indep_addressing(const KDF_ARGON2 *ctx, uint32_t pass,
243
                                 uint8_t slice);
244
static uint32_t index_alpha(const KDF_ARGON2 *ctx, uint32_t pass,
245
                            uint8_t slice, uint32_t index,
246
                            uint32_t pseudo_rand, int same_lane);
247
248
static void fill_segment(const KDF_ARGON2 *ctx, uint32_t pass, uint32_t lane,
249
                         uint8_t slice);
250
251
# if !defined(ARGON2_NO_THREADS)
252
static uint32_t fill_segment_thr(void *thread_data);
253
static int fill_mem_blocks_mt(KDF_ARGON2 *ctx);
254
# endif
255
256
static int fill_mem_blocks_st(KDF_ARGON2 *ctx);
257
static ossl_inline int fill_memory_blocks(KDF_ARGON2 *ctx);
258
259
static void initial_hash(uint8_t *blockhash, KDF_ARGON2 *ctx);
260
static int initialize(KDF_ARGON2 *ctx);
261
static void finalize(const KDF_ARGON2 *ctx, void *out);
262
263
static int blake2b(EVP_MD *md, EVP_MAC *mac, void *out, size_t outlen,
264
                   const void *in, size_t inlen, const void *key,
265
                   size_t keylen);
266
static int blake2b_long(EVP_MD *md, EVP_MAC *mac, unsigned char *out,
267
                        size_t outlen, const void *in, size_t inlen);
268
269
static ossl_inline uint64_t load64(const uint8_t *src)
270
334k
{
271
334k
    return
272
334k
      (((uint64_t)src[0]) << 0)
273
334k
    | (((uint64_t)src[1]) << 8)
274
334k
    | (((uint64_t)src[2]) << 16)
275
334k
    | (((uint64_t)src[3]) << 24)
276
334k
    | (((uint64_t)src[4]) << 32)
277
334k
    | (((uint64_t)src[5]) << 40)
278
334k
    | (((uint64_t)src[6]) << 48)
279
334k
    | (((uint64_t)src[7]) << 56);
280
334k
}
281
282
static ossl_inline void store32(uint8_t *dst, uint32_t w)
283
9.21k
{
284
9.21k
    dst[0] = (uint8_t)(w >> 0);
285
9.21k
    dst[1] = (uint8_t)(w >> 8);
286
9.21k
    dst[2] = (uint8_t)(w >> 16);
287
9.21k
    dst[3] = (uint8_t)(w >> 24);
288
9.21k
}
289
290
static ossl_inline void store64(uint8_t *dst, uint64_t w)
291
31.2k
{
292
31.2k
    dst[0] = (uint8_t)(w >> 0);
293
31.2k
    dst[1] = (uint8_t)(w >> 8);
294
31.2k
    dst[2] = (uint8_t)(w >> 16);
295
31.2k
    dst[3] = (uint8_t)(w >> 24);
296
31.2k
    dst[4] = (uint8_t)(w >> 32);
297
31.2k
    dst[5] = (uint8_t)(w >> 40);
298
31.2k
    dst[6] = (uint8_t)(w >> 48);
299
31.2k
    dst[7] = (uint8_t)(w >> 56);
300
31.2k
}
301
302
static ossl_inline uint64_t rotr64(const uint64_t w, const unsigned int c)
303
13.4M
{
304
13.4M
    return (w >> c) | (w << (64 - c));
305
13.4M
}
306
307
static ossl_inline uint64_t mul_lower(uint64_t x, uint64_t y)
308
13.4M
{
309
13.4M
    const uint64_t m = 0xFFFFFFFFUL;
310
13.4M
    return (x & m) * (y & m);
311
13.4M
}
312
313
static void init_block_value(BLOCK *b, uint8_t in)
314
5.65k
{
315
5.65k
    memset(b->v, in, sizeof(b->v));
316
5.65k
}
317
318
static void copy_block(BLOCK *dst, const BLOCK *src)
319
79.0k
{
320
79.0k
    memcpy(dst->v, src->v, sizeof(uint64_t) * ARGON2_QWORDS_IN_BLOCK);
321
79.0k
}
322
323
static void xor_block(BLOCK *dst, const BLOCK *src)
324
53.6k
{
325
53.6k
    int i;
326
327
6.91M
    for (i = 0; i < ARGON2_QWORDS_IN_BLOCK; ++i)
328
6.86M
        dst->v[i] ^= src->v[i];
329
53.6k
}
330
331
static void load_block(BLOCK *dst, const void *input)
332
2.61k
{
333
2.61k
    unsigned i;
334
335
337k
    for (i = 0; i < ARGON2_QWORDS_IN_BLOCK; ++i)
336
334k
        dst->v[i] = load64((const uint8_t *)input + i * sizeof(dst->v[i]));
337
2.61k
}
338
339
static void store_block(void *output, const BLOCK *src)
340
244
{
341
244
    unsigned i;
342
343
31.4k
    for (i = 0; i < ARGON2_QWORDS_IN_BLOCK; ++i)
344
31.2k
        store64((uint8_t *)output + i * sizeof(src->v[i]), src->v[i]);
345
244
}
346
347
static void fill_first_blocks(uint8_t *blockhash, const KDF_ARGON2 *ctx)
348
244
{
349
244
    uint32_t l;
350
244
    uint8_t blockhash_bytes[ARGON2_BLOCK_SIZE];
351
352
    /*
353
     * Make the first and second block in each lane as G(H0||0||i)
354
     * or G(H0||1||i).
355
     */
356
1.55k
    for (l = 0; l < ctx->lanes; ++l) {
357
1.30k
        store32(blockhash + ARGON2_PREHASH_DIGEST_LENGTH, 0);
358
1.30k
        store32(blockhash + ARGON2_PREHASH_DIGEST_LENGTH + 4, l);
359
1.30k
        blake2b_long(ctx->md, ctx->mac, blockhash_bytes, ARGON2_BLOCK_SIZE,
360
1.30k
                     blockhash, ARGON2_PREHASH_SEED_LENGTH);
361
1.30k
        load_block(&ctx->memory[l * ctx->lane_length + 0],
362
1.30k
                   blockhash_bytes);
363
1.30k
        store32(blockhash + ARGON2_PREHASH_DIGEST_LENGTH, 1);
364
1.30k
        blake2b_long(ctx->md, ctx->mac, blockhash_bytes, ARGON2_BLOCK_SIZE,
365
1.30k
                     blockhash, ARGON2_PREHASH_SEED_LENGTH);
366
1.30k
        load_block(&ctx->memory[l * ctx->lane_length + 1],
367
1.30k
                   blockhash_bytes);
368
1.30k
    }
369
244
    OPENSSL_cleanse(blockhash_bytes, ARGON2_BLOCK_SIZE);
370
244
}
371
372
static void fill_block(const BLOCK *prev, const BLOCK *ref,
373
                       BLOCK *next, int with_xor)
374
26.2k
{
375
26.2k
    BLOCK blockR, tmp;
376
26.2k
    unsigned i;
377
378
26.2k
    copy_block(&blockR, ref);
379
26.2k
    xor_block(&blockR, prev);
380
26.2k
    copy_block(&tmp, &blockR);
381
382
26.2k
    if (with_xor)
383
0
        xor_block(&tmp, next);
384
385
236k
    for (i = 0; i < 8; ++i)
386
210k
        PERMUTATION_P_COLUMN(blockR.v, i);
387
388
236k
    for (i = 0; i < 8; ++i)
389
210k
        PERMUTATION_P_ROW(blockR.v, i);
390
391
26.2k
    copy_block(next, &tmp);
392
26.2k
    xor_block(next, &blockR);
393
26.2k
}
394
395
static void next_addresses(BLOCK *address_block, BLOCK *input_block,
396
                           const BLOCK *zero_block)
397
2.82k
{
398
2.82k
    input_block->v[6]++;
399
2.82k
    fill_block(zero_block, input_block, address_block, 0);
400
2.82k
    fill_block(zero_block, address_block, address_block, 0);
401
2.82k
}
402
403
static int data_indep_addressing(const KDF_ARGON2 *ctx, uint32_t pass,
404
                                 uint8_t slice)
405
27.1k
{
406
27.1k
    switch (ctx->type) {
407
9.71k
    case ARGON2_I:
408
9.71k
        return 1;
409
6.81k
    case ARGON2_ID:
410
6.81k
        return (pass == 0) && (slice < ARGON2_SYNC_POINTS / 2);
411
10.6k
    case ARGON2_D:
412
10.6k
    default:
413
10.6k
        return 0;
414
27.1k
    }
415
27.1k
}
416
417
/*
418
 * Pass 0 (pass = 0):
419
 * This lane: all already finished segments plus already constructed blocks
420
 *            in this segment
421
 * Other lanes: all already finished segments
422
 *
423
 * Pass 1+:
424
 * This lane: (SYNC_POINTS - 1) last segments plus already constructed
425
 *            blocks in this segment
426
 * Other lanes: (SYNC_POINTS - 1) last segments
427
 */
428
static uint32_t index_alpha(const KDF_ARGON2 *ctx, uint32_t pass,
429
                            uint8_t slice, uint32_t index,
430
                            uint32_t pseudo_rand, int same_lane)
431
20.6k
{
432
20.6k
    uint32_t ref_area_sz;
433
20.6k
    uint64_t rel_pos;
434
20.6k
    uint32_t start_pos, abs_pos;
435
436
20.6k
    start_pos = 0;
437
20.6k
    switch (pass) {
438
20.6k
    case 0:
439
20.6k
        if (slice == 0)
440
3.19k
            ref_area_sz = index - 1;
441
17.4k
        else if (same_lane)
442
7.64k
            ref_area_sz = slice * ctx->segment_length + index - 1;
443
9.78k
        else
444
9.78k
            ref_area_sz = slice * ctx->segment_length +
445
9.78k
                ((index == 0) ? (-1) : 0);
446
20.6k
        break;
447
0
    default:
448
0
        if (same_lane)
449
0
            ref_area_sz = ctx->lane_length - ctx->segment_length + index - 1;
450
0
        else
451
0
            ref_area_sz = ctx->lane_length - ctx->segment_length +
452
0
                ((index == 0) ? (-1) : 0);
453
0
        if (slice != ARGON2_SYNC_POINTS - 1)
454
0
            start_pos = (slice + 1) * ctx->segment_length;
455
0
        break;
456
20.6k
    }
457
458
20.6k
    rel_pos = pseudo_rand;
459
20.6k
    rel_pos = rel_pos * rel_pos >> 32;
460
20.6k
    rel_pos = ref_area_sz - 1 - (ref_area_sz * rel_pos >> 32);
461
20.6k
    abs_pos = (start_pos + rel_pos) % ctx->lane_length;
462
463
20.6k
    return abs_pos;
464
20.6k
}
465
466
static void fill_segment(const KDF_ARGON2 *ctx, uint32_t pass, uint32_t lane,
467
                         uint8_t slice)
468
5.22k
{
469
5.22k
    BLOCK *ref_block = NULL, *curr_block = NULL;
470
5.22k
    BLOCK address_block, input_block, zero_block;
471
5.22k
    uint64_t rnd, ref_index, ref_lane;
472
5.22k
    uint32_t prev_offset;
473
5.22k
    uint32_t start_idx;
474
5.22k
    uint32_t j;
475
5.22k
    uint32_t curr_offset; /* Offset of the current block */
476
477
5.22k
    memset(&input_block, 0, sizeof(BLOCK));
478
479
5.22k
    if (ctx == NULL)
480
0
        return;
481
482
5.22k
    if (data_indep_addressing(ctx, pass, slice)) {
483
2.82k
        init_block_value(&zero_block, 0);
484
2.82k
        init_block_value(&input_block, 0);
485
486
2.82k
        input_block.v[0] = pass;
487
2.82k
        input_block.v[1] = lane;
488
2.82k
        input_block.v[2] = slice;
489
2.82k
        input_block.v[3] = ctx->memory_blocks;
490
2.82k
        input_block.v[4] = ctx->passes;
491
2.82k
        input_block.v[5] = ctx->type;
492
2.82k
    }
493
494
5.22k
    start_idx = 0;
495
496
    /* We've generated the first two blocks. Generate the 1st block of addrs. */
497
5.22k
    if ((pass == 0) && (slice == 0)) {
498
1.30k
        start_idx = 2;
499
1.30k
        if (data_indep_addressing(ctx, pass, slice))
500
879
            next_addresses(&address_block, &input_block, &zero_block);
501
1.30k
    }
502
503
5.22k
    curr_offset = lane * ctx->lane_length + slice * ctx->segment_length
504
5.22k
        + start_idx;
505
506
5.22k
    if ((curr_offset % ctx->lane_length) == 0)
507
0
        prev_offset = curr_offset + ctx->lane_length - 1;
508
5.22k
    else
509
5.22k
        prev_offset = curr_offset - 1;
510
511
25.8k
    for (j = start_idx; j < ctx->segment_length; ++j, ++curr_offset, ++prev_offset) {
512
20.6k
        if (curr_offset % ctx->lane_length == 1)
513
0
            prev_offset = curr_offset - 1;
514
515
        /* Taking pseudo-random value from the previous block. */
516
20.6k
        if (data_indep_addressing(ctx, pass, slice)) {
517
9.24k
            if (j % ARGON2_ADDRESSES_IN_BLOCK == 0)
518
1.94k
                next_addresses(&address_block, &input_block, &zero_block);
519
9.24k
            rnd = address_block.v[j % ARGON2_ADDRESSES_IN_BLOCK];
520
11.3k
        } else {
521
11.3k
            rnd = ctx->memory[prev_offset].v[0];
522
11.3k
        }
523
524
        /* Computing the lane of the reference block */
525
20.6k
        ref_lane = ((rnd >> 32)) % ctx->lanes;
526
        /* Can not reference other lanes yet */
527
20.6k
        if ((pass == 0) && (slice == 0))
528
3.19k
            ref_lane = lane;
529
530
        /* Computing the number of possible reference block within the lane. */
531
20.6k
        ref_index = index_alpha(ctx, pass, slice, j, rnd & 0xFFFFFFFF,
532
20.6k
                                ref_lane == lane);
533
534
        /* Creating a new block */
535
20.6k
        ref_block = ctx->memory + ctx->lane_length * ref_lane + ref_index;
536
20.6k
        curr_block = ctx->memory + curr_offset;
537
20.6k
        if (ARGON2_VERSION_10 == ctx->version) {
538
            /* Version 1.2.1 and earlier: overwrite, not XOR */
539
5.89k
            fill_block(ctx->memory + prev_offset, ref_block, curr_block, 0);
540
5.89k
            continue;
541
5.89k
        }
542
543
14.7k
        fill_block(ctx->memory + prev_offset, ref_block, curr_block,
544
14.7k
                   pass == 0 ? 0 : 1);
545
14.7k
    }
546
5.22k
}
547
548
# if !defined(ARGON2_NO_THREADS)
549
550
static uint32_t fill_segment_thr(void *thread_data)
551
0
{
552
0
    ARGON2_THREAD_DATA *my_data;
553
554
0
    my_data = (ARGON2_THREAD_DATA *) thread_data;
555
0
    fill_segment(my_data->ctx, my_data->pos.pass, my_data->pos.lane,
556
0
                 my_data->pos.slice);
557
558
0
    return 0;
559
0
}
560
561
static int fill_mem_blocks_mt(KDF_ARGON2 *ctx)
562
0
{
563
0
    uint32_t r, s, l, ll;
564
0
    void **t;
565
0
    ARGON2_THREAD_DATA *t_data;
566
567
0
    t = OPENSSL_zalloc(sizeof(void *)*ctx->lanes);
568
0
    t_data = OPENSSL_zalloc(ctx->lanes * sizeof(ARGON2_THREAD_DATA));
569
570
0
    if (t == NULL || t_data == NULL)
571
0
        goto fail;
572
573
0
    for (r = 0; r < ctx->passes; ++r) {
574
0
        for (s = 0; s < ARGON2_SYNC_POINTS; ++s) {
575
0
            for (l = 0; l < ctx->lanes; ++l) {
576
0
                ARGON2_POS p;
577
0
                if (l >= ctx->threads) {
578
0
                    if (ossl_crypto_thread_join(t[l - ctx->threads], NULL) == 0)
579
0
                        goto fail;
580
0
                    if (ossl_crypto_thread_clean(t[l - ctx->threads]) == 0)
581
0
                        goto fail;
582
0
                    t[l] = NULL;
583
0
                }
584
585
0
                p.pass = r;
586
0
                p.lane = l;
587
0
                p.slice = (uint8_t)s;
588
0
                p.index = 0;
589
590
0
                t_data[l].ctx = ctx;
591
0
                memcpy(&(t_data[l].pos), &p, sizeof(ARGON2_POS));
592
0
                t[l] = ossl_crypto_thread_start(ctx->libctx, &fill_segment_thr,
593
0
                                                (void *) &t_data[l]);
594
0
                if (t[l] == NULL) {
595
0
                    for (ll = 0; ll < l; ++ll) {
596
0
                        if (ossl_crypto_thread_join(t[ll], NULL) == 0)
597
0
                            goto fail;
598
0
                        if (ossl_crypto_thread_clean(t[ll]) == 0)
599
0
                            goto fail;
600
0
                        t[ll] = NULL;
601
0
                    }
602
0
                    goto fail;
603
0
                }
604
0
            }
605
0
            for (l = ctx->lanes - ctx->threads; l < ctx->lanes; ++l) {
606
0
                if (ossl_crypto_thread_join(t[l], NULL) == 0)
607
0
                    goto fail;
608
0
                if (ossl_crypto_thread_clean(t[l]) == 0)
609
0
                    goto fail;
610
0
                t[l] = NULL;
611
0
            }
612
0
        }
613
0
    }
614
615
0
    OPENSSL_free(t_data);
616
0
    OPENSSL_free(t);
617
618
0
    return 1;
619
620
0
fail:
621
0
    if (t_data != NULL)
622
0
        OPENSSL_free(t_data);
623
0
    if (t != NULL)
624
0
        OPENSSL_free(t);
625
0
    return 0;
626
0
}
627
628
# endif /* !defined(ARGON2_NO_THREADS) */
629
630
static int fill_mem_blocks_st(KDF_ARGON2 *ctx)
631
244
{
632
244
    uint32_t r, s, l;
633
634
488
    for (r = 0; r < ctx->passes; ++r)
635
1.22k
        for (s = 0; s < ARGON2_SYNC_POINTS; ++s)
636
6.20k
            for (l = 0; l < ctx->lanes; ++l)
637
5.22k
                fill_segment(ctx, r, l, s);
638
244
    return 1;
639
244
}
640
641
static ossl_inline int fill_memory_blocks(KDF_ARGON2 *ctx)
642
244
{
643
244
# if !defined(ARGON2_NO_THREADS)
644
244
    return ctx->threads == 1 ? fill_mem_blocks_st(ctx) : fill_mem_blocks_mt(ctx);
645
# else
646
    return ctx->threads == 1 ? fill_mem_blocks_st(ctx) : 0;
647
# endif
648
244
}
649
650
static void initial_hash(uint8_t *blockhash, KDF_ARGON2 *ctx)
651
244
{
652
244
    EVP_MD_CTX *mdctx;
653
244
    uint8_t value[sizeof(uint32_t)];
654
244
    unsigned int tmp;
655
244
    uint32_t args[7];
656
657
244
    if (ctx == NULL || blockhash == NULL)
658
0
        return;
659
660
244
    args[0] = ctx->lanes;
661
244
    args[1] = ctx->outlen;
662
244
    args[2] = ctx->m_cost;
663
244
    args[3] = ctx->t_cost;
664
244
    args[4] = ctx->version;
665
244
    args[5] = (uint32_t) ctx->type;
666
244
    args[6] = ctx->pwdlen;
667
668
244
    mdctx = EVP_MD_CTX_create();
669
244
    if (mdctx == NULL || EVP_DigestInit_ex(mdctx, ctx->md, NULL) != 1)
670
0
        goto fail;
671
672
1.95k
    for (tmp = 0; tmp < sizeof(args) / sizeof(uint32_t); ++tmp) {
673
1.70k
        store32((uint8_t *) &value, args[tmp]);
674
1.70k
        if (EVP_DigestUpdate(mdctx, &value, sizeof(value)) != 1)
675
0
            goto fail;
676
1.70k
    }
677
678
244
    if (ctx->pwd != NULL) {
679
244
        if (EVP_DigestUpdate(mdctx, ctx->pwd, ctx->pwdlen) != 1)
680
0
            goto fail;
681
244
        if (ctx->early_clean) {
682
209
            OPENSSL_cleanse(ctx->pwd, ctx->pwdlen);
683
209
            ctx->pwdlen = 0;
684
209
        }
685
244
    }
686
687
244
    store32((uint8_t *) &value, ctx->saltlen);
688
689
244
    if (EVP_DigestUpdate(mdctx, &value, sizeof(value)) != 1)
690
0
        goto fail;
691
692
244
    if (ctx->salt != NULL)
693
244
        if (EVP_DigestUpdate(mdctx, ctx->salt, ctx->saltlen) != 1)
694
0
            goto fail;
695
696
244
    store32((uint8_t *) &value, ctx->secretlen);
697
244
    if (EVP_DigestUpdate(mdctx, &value, sizeof(value)) != 1)
698
0
        goto fail;
699
700
244
    if (ctx->secret != NULL) {
701
244
        if (EVP_DigestUpdate(mdctx, ctx->secret, ctx->secretlen) != 1)
702
0
            goto fail;
703
244
        if (ctx->early_clean) {
704
209
            OPENSSL_cleanse(ctx->secret, ctx->secretlen);
705
209
            ctx->secretlen = 0;
706
209
        }
707
244
    }
708
709
244
    store32((uint8_t *) &value, ctx->adlen);
710
244
    if (EVP_DigestUpdate(mdctx, &value, sizeof(value)) != 1)
711
0
        goto fail;
712
713
244
    if (ctx->ad != NULL)
714
244
        if (EVP_DigestUpdate(mdctx, ctx->ad, ctx->adlen) != 1)
715
0
            goto fail;
716
717
244
    tmp = ARGON2_PREHASH_DIGEST_LENGTH;
718
244
    if (EVP_DigestFinal_ex(mdctx, blockhash, &tmp) != 1)
719
0
        goto fail;
720
721
244
fail:
722
244
    EVP_MD_CTX_destroy(mdctx);
723
244
}
724
725
static int initialize(KDF_ARGON2 *ctx)
726
244
{
727
244
    uint8_t blockhash[ARGON2_PREHASH_SEED_LENGTH];
728
729
244
    if (ctx == NULL)
730
0
        return 0;
731
732
244
    if (ctx->memory_blocks * sizeof(BLOCK) / sizeof(BLOCK) != ctx->memory_blocks)
733
0
        return 0;
734
735
244
    if (ctx->type != ARGON2_D)
736
141
        ctx->memory = OPENSSL_secure_zalloc(ctx->memory_blocks *
737
244
                                            sizeof(BLOCK));
738
103
    else
739
103
        ctx->memory = OPENSSL_zalloc(ctx->memory_blocks *
740
244
                                     sizeof(BLOCK));
741
742
244
    if (ctx->memory == NULL) {
743
0
        ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_MEMORY_SIZE,
744
0
                       "cannot allocate required memory");
745
0
        return 0;
746
0
    }
747
748
244
    initial_hash(blockhash, ctx);
749
244
    OPENSSL_cleanse(blockhash + ARGON2_PREHASH_DIGEST_LENGTH,
750
244
                    ARGON2_PREHASH_SEED_LENGTH - ARGON2_PREHASH_DIGEST_LENGTH);
751
244
    fill_first_blocks(blockhash, ctx);
752
244
    OPENSSL_cleanse(blockhash, ARGON2_PREHASH_SEED_LENGTH);
753
754
244
    return 1;
755
244
}
756
757
static void finalize(const KDF_ARGON2 *ctx, void *out)
758
244
{
759
244
    BLOCK blockhash;
760
244
    uint8_t blockhash_bytes[ARGON2_BLOCK_SIZE];
761
244
    uint32_t last_block_in_lane;
762
244
    uint32_t l;
763
764
244
    if (ctx == NULL)
765
0
        return;
766
767
244
    copy_block(&blockhash, ctx->memory + ctx->lane_length - 1);
768
769
    /* XOR the last blocks */
770
1.30k
    for (l = 1; l < ctx->lanes; ++l) {
771
1.06k
        last_block_in_lane = l * ctx->lane_length + (ctx->lane_length - 1);
772
1.06k
        xor_block(&blockhash, ctx->memory + last_block_in_lane);
773
1.06k
    }
774
775
    /* Hash the result */
776
244
    store_block(blockhash_bytes, &blockhash);
777
244
    blake2b_long(ctx->md, ctx->mac, out, ctx->outlen, blockhash_bytes,
778
244
                 ARGON2_BLOCK_SIZE);
779
244
    OPENSSL_cleanse(blockhash.v, ARGON2_BLOCK_SIZE);
780
244
    OPENSSL_cleanse(blockhash_bytes, ARGON2_BLOCK_SIZE);
781
782
244
    if (ctx->type != ARGON2_D)
783
141
        OPENSSL_secure_clear_free(ctx->memory,
784
244
                                  ctx->memory_blocks * sizeof(BLOCK));
785
103
    else
786
103
        OPENSSL_clear_free(ctx->memory,
787
244
                           ctx->memory_blocks * sizeof(BLOCK));
788
244
}
789
790
static int blake2b_mac(EVP_MAC *mac, void *out, size_t outlen, const void *in,
791
                       size_t inlen, const void *key, size_t keylen)
792
0
{
793
0
    int ret = 0;
794
0
    size_t par_n = 0, out_written;
795
0
    EVP_MAC_CTX *ctx = NULL;
796
0
    OSSL_PARAM par[3];
797
798
0
    if ((ctx = EVP_MAC_CTX_new(mac)) == NULL)
799
0
        goto fail;
800
801
0
    par[par_n++] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
802
0
                                                     (void *) key, keylen);
803
0
    par[par_n++] = OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_SIZE, &outlen);
804
0
    par[par_n++] = OSSL_PARAM_construct_end();
805
806
0
    ret = EVP_MAC_CTX_set_params(ctx, par) == 1
807
0
        && EVP_MAC_init(ctx, NULL, 0, NULL) == 1
808
0
        && EVP_MAC_update(ctx, in, inlen) == 1
809
0
        && EVP_MAC_final(ctx, out, (size_t *) &out_written, outlen) == 1;
810
811
0
fail:
812
0
    EVP_MAC_CTX_free(ctx);
813
0
    return ret;
814
0
}
815
816
static int blake2b_md(EVP_MD *md, void *out, size_t outlen, const void *in,
817
                      size_t inlen)
818
78.4k
{
819
78.4k
    int ret = 0;
820
78.4k
    EVP_MD_CTX *ctx = NULL;
821
78.4k
    OSSL_PARAM par[2];
822
823
78.4k
    if ((ctx = EVP_MD_CTX_create()) == NULL)
824
0
        return 0;
825
826
78.4k
    par[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE, &outlen);
827
78.4k
    par[1] = OSSL_PARAM_construct_end();
828
829
78.4k
    ret = EVP_DigestInit_ex2(ctx, md, par) == 1
830
78.4k
        && EVP_DigestUpdate(ctx, in, inlen) == 1
831
78.4k
        && EVP_DigestFinal_ex(ctx, out, NULL) == 1;
832
833
78.4k
    EVP_MD_CTX_free(ctx);
834
78.4k
    return ret;
835
78.4k
}
836
837
static int blake2b(EVP_MD *md, EVP_MAC *mac, void *out, size_t outlen,
838
                   const void *in, size_t inlen, const void *key, size_t keylen)
839
78.4k
{
840
78.4k
    if (out == NULL || outlen == 0)
841
0
        return 0;
842
843
78.4k
    if (key == NULL || keylen == 0)
844
78.4k
        return blake2b_md(md, out, outlen, in, inlen);
845
846
0
    return blake2b_mac(mac, out, outlen, in, inlen, key, keylen);
847
78.4k
}
848
849
static int blake2b_long(EVP_MD *md, EVP_MAC *mac, unsigned char *out,
850
                        size_t outlen, const void *in, size_t inlen)
851
2.85k
{
852
2.85k
    int ret = 0;
853
2.85k
    EVP_MD_CTX *ctx = NULL;
854
2.85k
    uint32_t outlen_curr;
855
2.85k
    uint8_t outbuf[BLAKE2B_OUTBYTES];
856
2.85k
    uint8_t inbuf[BLAKE2B_OUTBYTES];
857
2.85k
    uint8_t outlen_bytes[sizeof(uint32_t)] = {0};
858
2.85k
    OSSL_PARAM par[2];
859
2.85k
    size_t outlen_md;
860
861
2.85k
    if (out == NULL || outlen == 0)
862
0
        return 0;
863
864
    /* Ensure little-endian byte order */
865
2.85k
    store32(outlen_bytes, (uint32_t)outlen);
866
867
2.85k
    if ((ctx = EVP_MD_CTX_create()) == NULL)
868
0
        return 0;
869
870
2.85k
    outlen_md = (outlen <= BLAKE2B_OUTBYTES) ? outlen : BLAKE2B_OUTBYTES;
871
2.85k
    par[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE, &outlen_md);
872
2.85k
    par[1] = OSSL_PARAM_construct_end();
873
874
2.85k
    ret = EVP_DigestInit_ex2(ctx, md, par) == 1
875
2.85k
        && EVP_DigestUpdate(ctx, outlen_bytes, sizeof(outlen_bytes)) == 1
876
2.85k
        && EVP_DigestUpdate(ctx, in, inlen) == 1
877
2.85k
        && EVP_DigestFinal_ex(ctx, (outlen > BLAKE2B_OUTBYTES) ? outbuf : out,
878
2.85k
                              NULL) == 1;
879
880
2.85k
    if (ret == 0)
881
0
        goto fail;
882
883
2.85k
    if (outlen > BLAKE2B_OUTBYTES) {
884
2.61k
        memcpy(out, outbuf, BLAKE2B_OUTBYTES / 2);
885
2.61k
        out += BLAKE2B_OUTBYTES / 2;
886
2.61k
        outlen_curr = (uint32_t) outlen - BLAKE2B_OUTBYTES / 2;
887
888
78.4k
        while (outlen_curr > BLAKE2B_OUTBYTES) {
889
75.8k
            memcpy(inbuf, outbuf, BLAKE2B_OUTBYTES);
890
75.8k
            if (blake2b(md, mac, outbuf, BLAKE2B_OUTBYTES, inbuf,
891
75.8k
                        BLAKE2B_OUTBYTES, NULL, 0) != 1)
892
0
                goto fail;
893
75.8k
            memcpy(out, outbuf, BLAKE2B_OUTBYTES / 2);
894
75.8k
            out += BLAKE2B_OUTBYTES / 2;
895
75.8k
            outlen_curr -= BLAKE2B_OUTBYTES / 2;
896
75.8k
        }
897
898
2.61k
        memcpy(inbuf, outbuf, BLAKE2B_OUTBYTES);
899
2.61k
        if (blake2b(md, mac, outbuf, outlen_curr, inbuf, BLAKE2B_OUTBYTES,
900
2.61k
                    NULL, 0) != 1)
901
0
            goto fail;
902
2.61k
        memcpy(out, outbuf, outlen_curr);
903
2.61k
    }
904
2.85k
    ret = 1;
905
906
2.85k
fail:
907
2.85k
    EVP_MD_CTX_free(ctx);
908
2.85k
    return ret;
909
2.85k
}
910
911
static void kdf_argon2_init(KDF_ARGON2 *c, ARGON2_TYPE type)
912
572
{
913
572
    OSSL_LIB_CTX *libctx;
914
915
572
    libctx = c->libctx;
916
572
    memset(c, 0, sizeof(*c));
917
918
572
    c->libctx = libctx;
919
572
    c->outlen = ARGON2_DEFAULT_OUTLEN;
920
572
    c->t_cost = ARGON2_DEFAULT_T_COST;
921
572
    c->m_cost = ARGON2_DEFAULT_M_COST;
922
572
    c->lanes = ARGON2_DEFAULT_LANES;
923
572
    c->threads = ARGON2_DEFAULT_THREADS;
924
572
    c->version = ARGON2_DEFAULT_VERSION;
925
572
    c->type = type;
926
572
}
927
928
static void *kdf_argon2d_new(void *provctx)
929
239
{
930
239
    KDF_ARGON2 *ctx;
931
932
239
    if (!ossl_prov_is_running())
933
0
        return NULL;
934
935
239
    ctx = OPENSSL_zalloc(sizeof(*ctx));
936
239
    if (ctx == NULL) {
937
0
        ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
938
0
        return NULL;
939
0
    }
940
941
239
    ctx->libctx = PROV_LIBCTX_OF(provctx);
942
943
239
    kdf_argon2_init(ctx, ARGON2_D);
944
239
    return ctx;
945
239
}
946
947
static void *kdf_argon2i_new(void *provctx)
948
209
{
949
209
    KDF_ARGON2 *ctx;
950
951
209
    if (!ossl_prov_is_running())
952
0
        return NULL;
953
954
209
    ctx = OPENSSL_zalloc(sizeof(*ctx));
955
209
    if (ctx == NULL) {
956
0
        ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
957
0
        return NULL;
958
0
    }
959
960
209
    ctx->libctx = PROV_LIBCTX_OF(provctx);
961
962
209
    kdf_argon2_init(ctx, ARGON2_I);
963
209
    return ctx;
964
209
}
965
966
static void *kdf_argon2id_new(void *provctx)
967
124
{
968
124
    KDF_ARGON2 *ctx;
969
970
124
    if (!ossl_prov_is_running())
971
0
        return NULL;
972
973
124
    ctx = OPENSSL_zalloc(sizeof(*ctx));
974
124
    if (ctx == NULL) {
975
0
        ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
976
0
        return NULL;
977
0
    }
978
979
124
    ctx->libctx = PROV_LIBCTX_OF(provctx);
980
981
124
    kdf_argon2_init(ctx, ARGON2_ID);
982
124
    return ctx;
983
124
}
984
985
static void kdf_argon2_free(void *vctx)
986
572
{
987
572
    KDF_ARGON2 *ctx = (KDF_ARGON2 *)vctx;
988
989
572
    if (ctx == NULL)
990
0
        return;
991
992
572
    if (ctx->pwd != NULL)
993
572
        OPENSSL_clear_free(ctx->pwd, ctx->pwdlen);
994
995
572
    if (ctx->salt != NULL)
996
493
        OPENSSL_clear_free(ctx->salt, ctx->saltlen);
997
998
572
    if (ctx->secret != NULL)
999
493
        OPENSSL_clear_free(ctx->secret, ctx->secretlen);
1000
1001
572
    if (ctx->ad != NULL)
1002
493
        OPENSSL_clear_free(ctx->ad, ctx->adlen);
1003
1004
572
    EVP_MD_free(ctx->md);
1005
572
    EVP_MAC_free(ctx->mac);
1006
1007
572
    OPENSSL_free(ctx->propq);
1008
1009
572
    memset(ctx, 0, sizeof(*ctx));
1010
1011
572
    OPENSSL_free(ctx);
1012
572
}
1013
1014
static int kdf_argon2_derive(void *vctx, unsigned char *out, size_t outlen,
1015
                             const OSSL_PARAM params[])
1016
272
{
1017
272
    KDF_ARGON2 *ctx;
1018
272
    uint32_t memory_blocks, segment_length;
1019
1020
272
    ctx = (KDF_ARGON2 *)vctx;
1021
1022
272
    if (!ossl_prov_is_running() || !kdf_argon2_set_ctx_params(vctx, params))
1023
0
        return 0;
1024
1025
272
    if (ctx->mac == NULL)
1026
272
        ctx->mac = EVP_MAC_fetch(ctx->libctx, "blake2bmac", ctx->propq);
1027
272
    if (ctx->mac == NULL) {
1028
4
        ERR_raise_data(ERR_LIB_PROV, PROV_R_MISSING_MAC,
1029
4
                       "cannot fetch blake2bmac");
1030
4
        return 0;
1031
4
    }
1032
1033
268
    if (ctx->md == NULL)
1034
268
        ctx->md = EVP_MD_fetch(ctx->libctx, "blake2b512", ctx->propq);
1035
268
    if (ctx->md == NULL) {
1036
0
        ERR_raise_data(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST,
1037
0
                       "cannot fetch blake2b512");
1038
0
        return 0;
1039
0
    }
1040
1041
268
    if (ctx->salt == NULL || ctx->saltlen == 0) {
1042
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT);
1043
0
        return 0;
1044
0
    }
1045
1046
268
    if (outlen != ctx->outlen) {
1047
255
        if (OSSL_PARAM_locate((OSSL_PARAM *)params, "size") != NULL) {
1048
0
            ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
1049
0
            return 0;
1050
0
        }
1051
255
        if (!kdf_argon2_ctx_set_out_length(ctx, (uint32_t) outlen))
1052
0
            return 0;
1053
255
    }
1054
1055
268
    switch (ctx->type) {
1056
114
    case ARGON2_D:
1057
209
    case ARGON2_I:
1058
268
    case ARGON2_ID:
1059
268
        break;
1060
0
    default:
1061
0
        ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_MODE, "invalid Argon2 type");
1062
0
        return 0;
1063
268
    }
1064
1065
268
    if (ctx->threads > 1) {
1066
# ifdef ARGON2_NO_THREADS
1067
        ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_THREAD_POOL_SIZE,
1068
                       "requested %u threads, single-threaded mode supported only",
1069
                       ctx->threads);
1070
        return 0;
1071
# else
1072
20
        if (ctx->threads > ossl_get_avail_threads(ctx->libctx)) {
1073
20
            ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_THREAD_POOL_SIZE,
1074
20
                           "requested %u threads, available: %u",
1075
20
                           ctx->threads, ossl_get_avail_threads(ctx->libctx));
1076
20
            return 0;
1077
20
        }
1078
0
# endif
1079
0
        if (ctx->threads > ctx->lanes) {
1080
0
            ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_THREAD_POOL_SIZE,
1081
0
                           "requested more threads (%u) than lanes (%u)",
1082
0
                           ctx->threads, ctx->lanes);
1083
0
            return 0;
1084
0
        }
1085
0
    }
1086
1087
248
    if (ctx->m_cost < 8 * ctx->lanes) {
1088
4
        ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_MEMORY_SIZE,
1089
4
                       "m_cost must be greater or equal than 8 times the number of lanes");
1090
4
        return 0;
1091
4
    }
1092
1093
244
    memory_blocks = ctx->m_cost;
1094
244
    if (memory_blocks < 2 * ARGON2_SYNC_POINTS * ctx->lanes)
1095
0
        memory_blocks = 2 * ARGON2_SYNC_POINTS * ctx->lanes;
1096
1097
    /* Ensure that all segments have equal length */
1098
244
    segment_length = memory_blocks / (ctx->lanes * ARGON2_SYNC_POINTS);
1099
244
    memory_blocks = segment_length * (ctx->lanes * ARGON2_SYNC_POINTS);
1100
1101
244
    ctx->memory = NULL;
1102
244
    ctx->memory_blocks = memory_blocks;
1103
244
    ctx->segment_length = segment_length;
1104
244
    ctx->passes = ctx->t_cost;
1105
244
    ctx->lane_length = segment_length * ARGON2_SYNC_POINTS;
1106
1107
244
    if (initialize(ctx) != 1)
1108
0
        return 0;
1109
1110
244
    if (fill_memory_blocks(ctx) != 1)
1111
0
        return 0;
1112
1113
244
    finalize(ctx, out);
1114
1115
244
    return 1;
1116
244
}
1117
1118
static void kdf_argon2_reset(void *vctx)
1119
0
{
1120
0
    OSSL_LIB_CTX *libctx;
1121
0
    KDF_ARGON2 *ctx;
1122
0
    ARGON2_TYPE type;
1123
1124
0
    ctx = (KDF_ARGON2 *) vctx;
1125
0
    type = ctx->type;
1126
0
    libctx = ctx->libctx;
1127
1128
0
    EVP_MD_free(ctx->md);
1129
0
    EVP_MAC_free(ctx->mac);
1130
1131
0
    OPENSSL_free(ctx->propq);
1132
1133
0
    if (ctx->pwd != NULL)
1134
0
        OPENSSL_clear_free(ctx->pwd, ctx->pwdlen);
1135
1136
0
    if (ctx->salt != NULL)
1137
0
        OPENSSL_clear_free(ctx->salt, ctx->saltlen);
1138
1139
0
    if (ctx->secret != NULL)
1140
0
        OPENSSL_clear_free(ctx->secret, ctx->secretlen);
1141
1142
0
    if (ctx->ad != NULL)
1143
0
        OPENSSL_clear_free(ctx->ad, ctx->adlen);
1144
1145
0
    memset(ctx, 0, sizeof(*ctx));
1146
0
    ctx->libctx = libctx;
1147
0
    kdf_argon2_init(ctx, type);
1148
0
}
1149
1150
static int kdf_argon2_ctx_set_threads(KDF_ARGON2 *ctx, uint32_t threads)
1151
403
{
1152
403
    if (threads < ARGON2_MIN_THREADS) {
1153
35
        ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_THREAD_POOL_SIZE,
1154
35
                       "min threads: %u", ARGON2_MIN_THREADS);
1155
35
        return 0;
1156
35
    }
1157
1158
368
    if (threads > ARGON2_MAX_THREADS) {
1159
0
        ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_THREAD_POOL_SIZE,
1160
0
                       "max threads: %u", ARGON2_MAX_THREADS);
1161
0
        return 0;
1162
0
    }
1163
1164
368
    ctx->threads = threads;
1165
368
    return 1;
1166
368
}
1167
1168
static int kdf_argon2_ctx_set_lanes(KDF_ARGON2 *ctx, uint32_t lanes)
1169
368
{
1170
368
    if (lanes > ARGON2_MAX_LANES) {
1171
0
        ERR_raise_data(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER,
1172
0
                       "max lanes: %u", ARGON2_MAX_LANES);
1173
0
        return 0;
1174
0
    }
1175
1176
368
    if (lanes < ARGON2_MIN_LANES) {
1177
23
        ERR_raise_data(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER,
1178
23
                       "min lanes: %u", ARGON2_MIN_LANES);
1179
23
        return 0;
1180
23
    }
1181
1182
345
    ctx->lanes = lanes;
1183
345
    return 1;
1184
368
}
1185
1186
static int kdf_argon2_ctx_set_t_cost(KDF_ARGON2 *ctx, uint32_t t_cost)
1187
403
{
1188
    /* ARGON2_MAX_MEMORY == max m_cost value, so skip check  */
1189
1190
403
    if (t_cost < ARGON2_MIN_TIME) {
1191
0
        ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_ITERATION_COUNT,
1192
0
                       "min: %u", ARGON2_MIN_TIME);
1193
0
        return 0;
1194
0
    }
1195
1196
403
    ctx->t_cost = t_cost;
1197
403
    return 1;
1198
403
}
1199
1200
static int kdf_argon2_ctx_set_m_cost(KDF_ARGON2 *ctx, uint32_t m_cost)
1201
345
{
1202
    /* ARGON2_MAX_MEMORY == max m_cost value, so skip check */
1203
1204
345
    if (m_cost < ARGON2_MIN_MEMORY) {
1205
30
        ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_MEMORY_SIZE, "min: %u",
1206
30
                       ARGON2_MIN_MEMORY);
1207
30
        return 0;
1208
30
    }
1209
1210
315
    ctx->m_cost = m_cost;
1211
315
    return 1;
1212
345
}
1213
1214
static int kdf_argon2_ctx_set_out_length(KDF_ARGON2 *ctx, uint32_t outlen)
1215
748
{
1216
    /*
1217
     * ARGON2_MAX_OUT_LENGTH == max outlen value, so upper bounds checks
1218
     * are always satisfied; to suppress compiler if statement tautology
1219
     * warnings, these checks are skipped.
1220
     */
1221
1222
748
    if (outlen < ARGON2_MIN_OUT_LENGTH) {
1223
90
        ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_OUTPUT_LENGTH, "min: %u",
1224
90
                       ARGON2_MIN_OUT_LENGTH);
1225
90
        return 0;
1226
90
    }
1227
1228
658
    ctx->outlen = outlen;
1229
658
    return 1;
1230
748
}
1231
1232
static int kdf_argon2_ctx_set_secret(KDF_ARGON2 *ctx, const OSSL_PARAM *p)
1233
493
{
1234
493
    size_t buflen;
1235
1236
493
    if (p->data == NULL)
1237
0
        return 0;
1238
1239
493
    if (ctx->secret != NULL) {
1240
0
        OPENSSL_clear_free(ctx->secret, ctx->secretlen);
1241
0
        ctx->secret = NULL;
1242
0
        ctx->secretlen = 0U;
1243
0
    }
1244
1245
493
    if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->secret, 0, &buflen))
1246
0
        return 0;
1247
1248
493
    if (buflen > ARGON2_MAX_SECRET) {
1249
0
        OPENSSL_free(ctx->secret);
1250
0
        ctx->secret = NULL;
1251
0
        ctx->secretlen = 0U;
1252
0
        return 0;
1253
0
    }
1254
1255
493
    ctx->secretlen = (uint32_t) buflen;
1256
493
    return 1;
1257
493
}
1258
1259
static int kdf_argon2_ctx_set_pwd(KDF_ARGON2 *ctx, const OSSL_PARAM *p)
1260
572
{
1261
572
    size_t buflen;
1262
1263
572
    if (p->data == NULL)
1264
0
        return 0;
1265
1266
572
    if (ctx->pwd != NULL) {
1267
0
        OPENSSL_clear_free(ctx->pwd, ctx->pwdlen);
1268
0
        ctx->pwd = NULL;
1269
0
        ctx->pwdlen = 0U;
1270
0
    }
1271
1272
572
    if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->pwd, 0, &buflen))
1273
0
        return 0;
1274
1275
572
    if (buflen > ARGON2_MAX_PWD_LENGTH) {
1276
0
        ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH, "max: %u",
1277
0
                       ARGON2_MAX_PWD_LENGTH);
1278
0
        goto fail;
1279
0
    }
1280
1281
572
    ctx->pwdlen = (uint32_t) buflen;
1282
572
    return 1;
1283
1284
0
fail:
1285
0
    OPENSSL_free(ctx->pwd);
1286
0
    ctx->pwd = NULL;
1287
0
    ctx->pwdlen = 0U;
1288
0
    return 0;
1289
572
}
1290
1291
static int kdf_argon2_ctx_set_salt(KDF_ARGON2 *ctx, const OSSL_PARAM *p)
1292
572
{
1293
572
    size_t buflen;
1294
1295
572
    if (p->data == NULL)
1296
0
        return 0;
1297
1298
572
    if (ctx->salt != NULL) {
1299
0
        OPENSSL_clear_free(ctx->salt, ctx->saltlen);
1300
0
        ctx->salt = NULL;
1301
0
        ctx->saltlen = 0U;
1302
0
    }
1303
1304
572
    if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->salt, 0, &buflen))
1305
0
        return 0;
1306
1307
572
    if (buflen < ARGON2_MIN_SALT_LENGTH) {
1308
79
        ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH, "min: %u",
1309
79
                       ARGON2_MIN_SALT_LENGTH);
1310
79
        goto fail;
1311
79
    }
1312
1313
493
    if (buflen > ARGON2_MAX_SALT_LENGTH) {
1314
0
        ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH, "max: %u",
1315
0
                       ARGON2_MAX_SALT_LENGTH);
1316
0
        goto fail;
1317
0
    }
1318
1319
493
    ctx->saltlen = (uint32_t) buflen;
1320
493
    return 1;
1321
1322
79
fail:
1323
79
    OPENSSL_free(ctx->salt);
1324
79
    ctx->salt = NULL;
1325
79
    ctx->saltlen = 0U;
1326
79
    return 0;
1327
493
}
1328
1329
static int kdf_argon2_ctx_set_ad(KDF_ARGON2 *ctx, const OSSL_PARAM *p)
1330
493
{
1331
493
    size_t buflen;
1332
1333
493
    if (p->data == NULL)
1334
0
        return 0;
1335
1336
493
    if (ctx->ad != NULL) {
1337
0
        OPENSSL_clear_free(ctx->ad, ctx->adlen);
1338
0
        ctx->ad = NULL;
1339
0
        ctx->adlen = 0U;
1340
0
    }
1341
1342
493
    if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->ad, 0, &buflen))
1343
0
        return 0;
1344
1345
493
    if (buflen > ARGON2_MAX_AD_LENGTH) {
1346
0
        OPENSSL_free(ctx->ad);
1347
0
        ctx->ad = NULL;
1348
0
        ctx->adlen = 0U;
1349
0
        return 0;
1350
0
    }
1351
1352
493
    ctx->adlen = (uint32_t) buflen;
1353
493
    return 1;
1354
493
}
1355
1356
static void kdf_argon2_ctx_set_flag_early_clean(KDF_ARGON2 *ctx, uint32_t f)
1357
315
{
1358
315
    ctx->early_clean = !!(f);
1359
315
}
1360
1361
static int kdf_argon2_ctx_set_version(KDF_ARGON2 *ctx, uint32_t version)
1362
315
{
1363
315
    switch (version) {
1364
114
    case ARGON2_VERSION_10:
1365
272
    case ARGON2_VERSION_13:
1366
272
        ctx->version = version;
1367
272
        return 1;
1368
43
    default:
1369
43
        ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_MODE,
1370
43
                       "invalid Argon2 version");
1371
43
        return 0;
1372
315
    }
1373
315
}
1374
1375
static int set_property_query(KDF_ARGON2 *ctx, const char *propq)
1376
272
{
1377
272
    OPENSSL_free(ctx->propq);
1378
272
    ctx->propq = NULL;
1379
272
    if (propq != NULL) {
1380
272
        ctx->propq = OPENSSL_strdup(propq);
1381
272
        if (ctx->propq == NULL)
1382
0
            return 0;
1383
272
    }
1384
272
    EVP_MD_free(ctx->md);
1385
272
    ctx->md = NULL;
1386
272
    EVP_MAC_free(ctx->mac);
1387
272
    ctx->mac = NULL;
1388
272
    return 1;
1389
272
}
1390
1391
static int kdf_argon2_set_ctx_params(void *vctx, const OSSL_PARAM params[])
1392
293
{
1393
293
    const OSSL_PARAM *p;
1394
293
    KDF_ARGON2 *ctx;
1395
293
    uint32_t u32_value;
1396
1397
293
    if (params == NULL)
1398
94
        return 1;
1399
1400
199
    ctx = (KDF_ARGON2 *) vctx;
1401
199
    if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PASSWORD)) != NULL)
1402
199
        if (!kdf_argon2_ctx_set_pwd(ctx, p))
1403
0
            return 0;
1404
1405
199
    if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT)) != NULL)
1406
199
        if (!kdf_argon2_ctx_set_salt(ctx, p))
1407
27
            return 0;
1408
1409
172
    if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SECRET)) != NULL)
1410
172
        if (!kdf_argon2_ctx_set_secret(ctx, p))
1411
0
            return 0;
1412
1413
172
    if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_ARGON2_AD)) != NULL)
1414
172
        if (!kdf_argon2_ctx_set_ad(ctx, p))
1415
0
            return 0;
1416
1417
172
    if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SIZE)) != NULL) {
1418
172
        if (!OSSL_PARAM_get_uint32(p, &u32_value))
1419
0
            return 0;
1420
172
        if (!kdf_argon2_ctx_set_out_length(ctx, u32_value))
1421
36
            return 0;
1422
172
    }
1423
1424
136
    if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_ITER)) != NULL) {
1425
136
        if (!OSSL_PARAM_get_uint32(p, &u32_value))
1426
0
            return 0;
1427
136
        if (!kdf_argon2_ctx_set_t_cost(ctx, u32_value))
1428
0
            return 0;
1429
136
    }
1430
1431
136
    if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_THREADS)) != NULL) {
1432
136
        if (!OSSL_PARAM_get_uint32(p, &u32_value))
1433
0
            return 0;
1434
136
        if (!kdf_argon2_ctx_set_threads(ctx, u32_value))
1435
17
            return 0;
1436
136
    }
1437
1438
119
    if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_ARGON2_LANES)) != NULL) {
1439
119
        if (!OSSL_PARAM_get_uint32(p, &u32_value))
1440
0
            return 0;
1441
119
        if (!kdf_argon2_ctx_set_lanes(ctx, u32_value))
1442
4
            return 0;
1443
119
    }
1444
1445
115
    if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_ARGON2_MEMCOST)) != NULL) {
1446
115
        if (!OSSL_PARAM_get_uint32(p, &u32_value))
1447
0
            return 0;
1448
115
        if (!kdf_argon2_ctx_set_m_cost(ctx, u32_value))
1449
9
            return 0;
1450
115
    }
1451
1452
106
    if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_EARLY_CLEAN)) != NULL) {
1453
106
        if (!OSSL_PARAM_get_uint32(p, &u32_value))
1454
0
            return 0;
1455
106
        kdf_argon2_ctx_set_flag_early_clean(ctx, u32_value);
1456
106
    }
1457
1458
106
    if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_ARGON2_VERSION)) != NULL) {
1459
106
        if (!OSSL_PARAM_get_uint32(p, &u32_value))
1460
0
            return 0;
1461
106
        if (!kdf_argon2_ctx_set_version(ctx, u32_value))
1462
12
            return 0;
1463
106
    }
1464
1465
94
    if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PROPERTIES)) != NULL) {
1466
94
        if (p->data_type != OSSL_PARAM_UTF8_STRING
1467
94
            || !set_property_query(ctx, p->data))
1468
0
            return 0;
1469
94
    }
1470
1471
94
    return 1;
1472
94
}
1473
1474
static const OSSL_PARAM *kdf_argon2_settable_ctx_params(ossl_unused void *ctx,
1475
                                                        ossl_unused void *p_ctx)
1476
572
{
1477
572
    static const OSSL_PARAM known_settable_ctx_params[] = {
1478
572
        OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PASSWORD, NULL, 0),
1479
572
        OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0),
1480
572
        OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SECRET, NULL, 0),
1481
572
        OSSL_PARAM_octet_string(OSSL_KDF_PARAM_ARGON2_AD, NULL, 0),
1482
572
        OSSL_PARAM_uint32(OSSL_KDF_PARAM_SIZE, NULL),
1483
572
        OSSL_PARAM_uint32(OSSL_KDF_PARAM_ITER, NULL),
1484
572
        OSSL_PARAM_uint32(OSSL_KDF_PARAM_THREADS, NULL),
1485
572
        OSSL_PARAM_uint32(OSSL_KDF_PARAM_ARGON2_LANES, NULL),
1486
572
        OSSL_PARAM_uint32(OSSL_KDF_PARAM_ARGON2_MEMCOST, NULL),
1487
572
        OSSL_PARAM_uint32(OSSL_KDF_PARAM_EARLY_CLEAN, NULL),
1488
572
        OSSL_PARAM_uint32(OSSL_KDF_PARAM_ARGON2_VERSION, NULL),
1489
572
        OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
1490
572
        OSSL_PARAM_END
1491
572
    };
1492
1493
572
    return known_settable_ctx_params;
1494
572
}
1495
1496
static int kdf_argon2_get_ctx_params(void *vctx, OSSL_PARAM params[])
1497
0
{
1498
0
    OSSL_PARAM *p;
1499
1500
0
    (void) vctx;
1501
0
    if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
1502
0
        return OSSL_PARAM_set_size_t(p, SIZE_MAX);
1503
1504
0
    return -2;
1505
0
}
1506
1507
static const OSSL_PARAM *kdf_argon2_gettable_ctx_params(ossl_unused void *ctx,
1508
                                                        ossl_unused void *p_ctx)
1509
0
{
1510
0
    static const OSSL_PARAM known_gettable_ctx_params[] = {
1511
0
        OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
1512
0
        OSSL_PARAM_END
1513
0
    };
1514
1515
0
    return known_gettable_ctx_params;
1516
0
}
1517
1518
const OSSL_DISPATCH ossl_kdf_argon2i_functions[] = {
1519
    { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_argon2i_new },
1520
    { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_argon2_free },
1521
    { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_argon2_reset },
1522
    { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_argon2_derive },
1523
    { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
1524
      (void(*)(void))kdf_argon2_settable_ctx_params },
1525
    { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_argon2_set_ctx_params },
1526
    { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
1527
      (void(*)(void))kdf_argon2_gettable_ctx_params },
1528
    { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_argon2_get_ctx_params },
1529
    OSSL_DISPATCH_END
1530
};
1531
1532
const OSSL_DISPATCH ossl_kdf_argon2d_functions[] = {
1533
    { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_argon2d_new },
1534
    { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_argon2_free },
1535
    { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_argon2_reset },
1536
    { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_argon2_derive },
1537
    { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
1538
      (void(*)(void))kdf_argon2_settable_ctx_params },
1539
    { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_argon2_set_ctx_params },
1540
    { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
1541
      (void(*)(void))kdf_argon2_gettable_ctx_params },
1542
    { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_argon2_get_ctx_params },
1543
    OSSL_DISPATCH_END
1544
};
1545
1546
const OSSL_DISPATCH ossl_kdf_argon2id_functions[] = {
1547
    { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_argon2id_new },
1548
    { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_argon2_free },
1549
    { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_argon2_reset },
1550
    { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_argon2_derive },
1551
    { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
1552
      (void(*)(void))kdf_argon2_settable_ctx_params },
1553
    { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_argon2_set_ctx_params },
1554
    { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
1555
      (void(*)(void))kdf_argon2_gettable_ctx_params },
1556
    { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_argon2_get_ctx_params },
1557
    OSSL_DISPATCH_END
1558
};
1559
1560
#endif