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