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