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