| 1 |
/* |
| 2 |
* Copyright 1999-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 |
/* clang-format off */ |
| 10 |
{- |
| 11 |
use OpenSSL::paramnames qw(produce_param_decoder); |
| 12 |
-} |
| 13 |
/* clang-format on */ |
| 14 |
|
| 15 |
#include <openssl/trace.h> |
| 16 |
#include <stdlib.h> |
| 17 |
#include <stdarg.h> |
| 18 |
#include <string.h> |
| 19 |
#include <openssl/evp.h> |
| 20 |
#include <openssl/kdf.h> |
| 21 |
#include <openssl/core_names.h> |
| 22 |
#include <openssl/proverr.h> |
| 23 |
#include "internal/common.h" |
| 24 |
#include "internal/cryptlib.h" |
| 25 |
#include "internal/numbers.h" |
| 26 |
#include "internal/skey.h" |
| 27 |
#include "crypto/evp.h" |
| 28 |
#include "crypto/types.h" |
| 29 |
#include "prov/provider_ctx.h" |
| 30 |
#include "prov/providercommon.h" |
| 31 |
#include "prov/implementations.h" |
| 32 |
#include "prov/provider_util.h" |
| 33 |
|
| 34 |
static OSSL_FUNC_kdf_newctx_fn kdf_pbkdf1_new; |
| 35 |
static OSSL_FUNC_kdf_dupctx_fn kdf_pbkdf1_dup; |
| 36 |
static OSSL_FUNC_kdf_freectx_fn kdf_pbkdf1_free; |
| 37 |
static OSSL_FUNC_kdf_reset_fn kdf_pbkdf1_reset; |
| 38 |
static OSSL_FUNC_kdf_derive_fn kdf_pbkdf1_derive; |
| 39 |
static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_pbkdf1_settable_ctx_params; |
| 40 |
static OSSL_FUNC_kdf_set_ctx_params_fn kdf_pbkdf1_set_ctx_params; |
| 41 |
static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_pbkdf1_gettable_ctx_params; |
| 42 |
static OSSL_FUNC_kdf_get_ctx_params_fn kdf_pbkdf1_get_ctx_params; |
| 43 |
static OSSL_FUNC_kdf_set_skey_fn kdf_pbkdf1_set_skey; |
| 44 |
static OSSL_FUNC_kdf_derive_skey_fn kdf_pbkdf1_derive_skey; |
| 45 |
|
| 46 |
typedef struct { |
| 47 |
void *provctx; |
| 48 |
PROV_DIGEST digest; |
| 49 |
unsigned char *pass; |
| 50 |
size_t pass_len; |
| 51 |
unsigned char *salt; |
| 52 |
size_t salt_len; |
| 53 |
uint64_t iter; |
| 54 |
} KDF_PBKDF1; |
| 55 |
|
| 56 |
/* |
| 57 |
* PKCS5 PBKDF1 compatible key/IV generation as specified in: |
| 58 |
* https://tools.ietf.org/html/rfc8018#page-10 |
| 59 |
*/ |
| 60 |
|
| 61 |
static int kdf_pbkdf1_do_derive(const unsigned char *pass, size_t passlen, |
| 62 |
const unsigned char *salt, size_t saltlen, |
| 63 |
uint64_t iter, const EVP_MD *md_type, |
| 64 |
unsigned char *out, size_t n) |
| 65 |
{ |
| 66 |
uint64_t i; |
| 67 |
int mdsize, ret = 0; |
| 68 |
unsigned char md_tmp[EVP_MAX_MD_SIZE]; |
| 69 |
EVP_MD_CTX *ctx = NULL; |
| 70 |
|
| 71 |
ctx = EVP_MD_CTX_new(); |
| 72 |
if (ctx == NULL) { |
| 73 |
ERR_raise(ERR_LIB_PROV, ERR_R_EVP_LIB); |
| 74 |
goto err; |
| 75 |
} |
| 76 |
|
| 77 |
if (!EVP_DigestInit_ex(ctx, md_type, NULL) |
| 78 |
|| !EVP_DigestUpdate(ctx, pass, passlen) |
| 79 |
|| !EVP_DigestUpdate(ctx, salt, saltlen) |
| 80 |
|| !EVP_DigestFinal_ex(ctx, md_tmp, NULL)) |
| 81 |
goto err; |
| 82 |
mdsize = EVP_MD_size(md_type); |
| 83 |
if (mdsize <= 0) |
| 84 |
goto err; |
| 85 |
if (n > (size_t)mdsize) { |
| 86 |
ERR_raise(ERR_LIB_PROV, PROV_R_LENGTH_TOO_LARGE); |
| 87 |
goto err; |
| 88 |
} |
| 89 |
|
| 90 |
for (i = 1; i < iter; i++) { |
| 91 |
if (!EVP_DigestInit_ex(ctx, md_type, NULL)) |
| 92 |
goto err; |
| 93 |
if (!EVP_DigestUpdate(ctx, md_tmp, mdsize)) |
| 94 |
goto err; |
| 95 |
if (!EVP_DigestFinal_ex(ctx, md_tmp, NULL)) |
| 96 |
goto err; |
| 97 |
} |
| 98 |
|
| 99 |
memcpy(out, md_tmp, n); |
| 100 |
ret = 1; |
| 101 |
err: |
| 102 |
OPENSSL_cleanse(md_tmp, EVP_MAX_MD_SIZE); |
| 103 |
EVP_MD_CTX_free(ctx); |
| 104 |
return ret; |
| 105 |
} |
| 106 |
|
| 107 |
static void *kdf_pbkdf1_new(void *provctx) |
| 108 |
{ |
| 109 |
KDF_PBKDF1 *ctx; |
| 110 |
|
| 111 |
if (!ossl_prov_is_running()) |
| 112 |
return NULL; |
| 113 |
|
| 114 |
ctx = OPENSSL_zalloc(sizeof(*ctx)); |
| 115 |
if (ctx == NULL) |
| 116 |
return NULL; |
| 117 |
ctx->provctx = provctx; |
| 118 |
return ctx; |
| 119 |
} |
| 120 |
|
| 121 |
static void kdf_pbkdf1_cleanup(KDF_PBKDF1 *ctx) |
| 122 |
{ |
| 123 |
ossl_prov_digest_reset(&ctx->digest); |
| 124 |
OPENSSL_free(ctx->salt); |
| 125 |
OPENSSL_clear_free(ctx->pass, ctx->pass_len); |
| 126 |
memset(ctx, 0, sizeof(*ctx)); |
| 127 |
} |
| 128 |
|
| 129 |
static void kdf_pbkdf1_free(void *vctx) |
| 130 |
{ |
| 131 |
KDF_PBKDF1 *ctx = (KDF_PBKDF1 *)vctx; |
| 132 |
|
| 133 |
if (ctx != NULL) { |
| 134 |
kdf_pbkdf1_cleanup(ctx); |
| 135 |
OPENSSL_free(ctx); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
static void kdf_pbkdf1_reset(void *vctx) |
| 140 |
{ |
| 141 |
KDF_PBKDF1 *ctx = (KDF_PBKDF1 *)vctx; |
| 142 |
void *provctx = ctx->provctx; |
| 143 |
|
| 144 |
kdf_pbkdf1_cleanup(ctx); |
| 145 |
ctx->provctx = provctx; |
| 146 |
} |
| 147 |
|
| 148 |
static void *kdf_pbkdf1_dup(void *vctx) |
| 149 |
{ |
| 150 |
const KDF_PBKDF1 *src = (const KDF_PBKDF1 *)vctx; |
| 151 |
KDF_PBKDF1 *dest; |
| 152 |
|
| 153 |
dest = kdf_pbkdf1_new(src->provctx); |
| 154 |
if (dest != NULL) { |
| 155 |
if (!ossl_prov_memdup(src->salt, src->salt_len, |
| 156 |
&dest->salt, &dest->salt_len) |
| 157 |
|| !ossl_prov_memdup(src->pass, src->pass_len, |
| 158 |
&dest->pass, &dest->pass_len) |
| 159 |
|| !ossl_prov_digest_copy(&dest->digest, &src->digest)) |
| 160 |
goto err; |
| 161 |
dest->iter = src->iter; |
| 162 |
} |
| 163 |
return dest; |
| 164 |
|
| 165 |
err: |
| 166 |
kdf_pbkdf1_free(dest); |
| 167 |
return NULL; |
| 168 |
} |
| 169 |
|
| 170 |
static int kdf_pbkdf1_set_membuf(unsigned char **buffer, size_t *buflen, |
| 171 |
const OSSL_PARAM *p) |
| 172 |
{ |
| 173 |
OPENSSL_clear_free(*buffer, *buflen); |
| 174 |
*buffer = NULL; |
| 175 |
*buflen = 0; |
| 176 |
|
| 177 |
if (p->data_size == 0) { |
| 178 |
if ((*buffer = OPENSSL_malloc(1)) == NULL) |
| 179 |
return 0; |
| 180 |
} else if (p->data != NULL) { |
| 181 |
if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen)) |
| 182 |
return 0; |
| 183 |
} |
| 184 |
return 1; |
| 185 |
} |
| 186 |
|
| 187 |
static int kdf_pbkdf1_set_membuf_skey(unsigned char **buffer, size_t *buflen, |
| 188 |
const PROV_SKEY *pskey) |
| 189 |
{ |
| 190 |
OPENSSL_clear_free(*buffer, *buflen); |
| 191 |
*buffer = NULL; |
| 192 |
*buflen = 0; |
| 193 |
|
| 194 |
if (pskey->length == 0) { |
| 195 |
if ((*buffer = OPENSSL_malloc(1)) == NULL) |
| 196 |
return 0; |
| 197 |
} else if (pskey->data != NULL) { |
| 198 |
*buffer = OPENSSL_malloc(pskey->length); |
| 199 |
if (*buffer == NULL) |
| 200 |
return 0; |
| 201 |
|
| 202 |
memcpy(*buffer, pskey->data, pskey->length); |
| 203 |
*buflen = pskey->length; |
| 204 |
} |
| 205 |
return 1; |
| 206 |
} |
| 207 |
|
| 208 |
static int kdf_pbkdf1_derive(void *vctx, unsigned char *key, size_t keylen, |
| 209 |
const OSSL_PARAM params[]) |
| 210 |
{ |
| 211 |
KDF_PBKDF1 *ctx = (KDF_PBKDF1 *)vctx; |
| 212 |
const EVP_MD *md; |
| 213 |
|
| 214 |
if (!ossl_prov_is_running() || !kdf_pbkdf1_set_ctx_params(ctx, params)) |
| 215 |
return 0; |
| 216 |
|
| 217 |
if (ctx->pass == NULL) { |
| 218 |
ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS); |
| 219 |
return 0; |
| 220 |
} |
| 221 |
|
| 222 |
if (ctx->salt == NULL) { |
| 223 |
ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT); |
| 224 |
return 0; |
| 225 |
} |
| 226 |
|
| 227 |
md = ossl_prov_digest_md(&ctx->digest); |
| 228 |
return kdf_pbkdf1_do_derive(ctx->pass, ctx->pass_len, ctx->salt, ctx->salt_len, |
| 229 |
ctx->iter, md, key, keylen); |
| 230 |
} |
| 231 |
|
| 232 |
/* clang-format off */ |
| 233 |
{- produce_param_decoder('pbkdf1_set_ctx_params', |
| 234 |
(['OSSL_KDF_PARAM_PROPERTIES', 'propq', 'utf8_string'], |
| 235 |
['OSSL_ALG_PARAM_ENGINE', 'engine', 'utf8_string', 'hidden'], |
| 236 |
['OSSL_KDF_PARAM_DIGEST', 'digest', 'utf8_string'], |
| 237 |
['OSSL_KDF_PARAM_PASSWORD', 'pw', 'octet_string'], |
| 238 |
['OSSL_KDF_PARAM_SALT', 'salt', 'octet_string'], |
| 239 |
['OSSL_KDF_PARAM_ITER', 'iter', 'uint64'], |
| 240 |
)); -} |
| 241 |
/* clang-format on */ |
| 242 |
|
| 243 |
static int kdf_pbkdf1_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
| 244 |
{ |
| 245 |
struct pbkdf1_set_ctx_params_st p; |
| 246 |
KDF_PBKDF1 *ctx = vctx; |
| 247 |
OSSL_LIB_CTX *libctx; |
| 248 |
|
| 249 |
if (ctx == NULL || !pbkdf1_set_ctx_params_decoder(params, &p)) |
| 250 |
return 0; |
| 251 |
|
| 252 |
libctx = PROV_LIBCTX_OF(ctx->provctx); |
| 253 |
|
| 254 |
if (!ossl_prov_digest_load(&ctx->digest, p.digest, |
| 255 |
p.propq, p.engine, libctx)) |
| 256 |
return 0; |
| 257 |
|
| 258 |
if (p.pw != NULL && !kdf_pbkdf1_set_membuf(&ctx->pass, &ctx->pass_len, p.pw)) |
| 259 |
return 0; |
| 260 |
|
| 261 |
if (p.salt != NULL && !kdf_pbkdf1_set_membuf(&ctx->salt, &ctx->salt_len, p.salt)) |
| 262 |
return 0; |
| 263 |
|
| 264 |
if (p.iter != NULL && !OSSL_PARAM_get_uint64(p.iter, &ctx->iter)) |
| 265 |
return 0; |
| 266 |
return 1; |
| 267 |
} |
| 268 |
|
| 269 |
static const OSSL_PARAM *kdf_pbkdf1_settable_ctx_params(ossl_unused void *ctx, |
| 270 |
ossl_unused void *p_ctx) |
| 271 |
{ |
| 272 |
return pbkdf1_set_ctx_params_list; |
| 273 |
} |
| 274 |
|
| 275 |
/* clang-format off */ |
| 276 |
{- produce_param_decoder('pbkdf1_get_ctx_params', |
| 277 |
(['OSSL_KDF_PARAM_SIZE', 'size', 'size_t'], |
| 278 |
)); -} |
| 279 |
/* clang-format on */ |
| 280 |
|
| 281 |
static int kdf_pbkdf1_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
| 282 |
{ |
| 283 |
struct pbkdf1_get_ctx_params_st p; |
| 284 |
KDF_PBKDF1 *ctx = vctx; |
| 285 |
|
| 286 |
if (ctx == NULL || !pbkdf1_get_ctx_params_decoder(params, &p)) |
| 287 |
return 0; |
| 288 |
|
| 289 |
if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, SIZE_MAX)) |
| 290 |
return 0; |
| 291 |
return 1; |
| 292 |
} |
| 293 |
|
| 294 |
static const OSSL_PARAM *kdf_pbkdf1_gettable_ctx_params(ossl_unused void *ctx, |
| 295 |
ossl_unused void *p_ctx) |
| 296 |
{ |
| 297 |
return pbkdf1_get_ctx_params_list; |
| 298 |
} |
| 299 |
|
| 300 |
static int kdf_pbkdf1_set_skey(void *vctx, void *skeydata, const char *paramname) |
| 301 |
{ |
| 302 |
KDF_PBKDF1 *ctx = (KDF_PBKDF1 *)vctx; |
| 303 |
PROV_SKEY *pskey = (PROV_SKEY *)skeydata; |
| 304 |
|
| 305 |
if (paramname == NULL || skeydata == NULL) |
| 306 |
return 0; |
| 307 |
|
| 308 |
if (strcmp(paramname, OSSL_KDF_PARAM_PASSWORD) == 0) |
| 309 |
return kdf_pbkdf1_set_membuf_skey(&ctx->pass, &ctx->pass_len, pskey); |
| 310 |
|
| 311 |
if (strcmp(paramname, OSSL_KDF_PARAM_SALT) == 0) |
| 312 |
return kdf_pbkdf1_set_membuf_skey(&ctx->salt, &ctx->salt_len, pskey); |
| 313 |
|
| 314 |
return 0; |
| 315 |
} |
| 316 |
|
| 317 |
static void *kdf_pbkdf1_derive_skey(void *vctx, const char *key_type ossl_unused, void *provctx, |
| 318 |
OSSL_FUNC_skeymgmt_import_fn *import, |
| 319 |
size_t keylen, const OSSL_PARAM params[]) |
| 320 |
{ |
| 321 |
unsigned char *key = NULL; |
| 322 |
void *ret = NULL; |
| 323 |
OSSL_PARAM import_params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
| 324 |
|
| 325 |
if (import == NULL || keylen == 0) |
| 326 |
return NULL; |
| 327 |
|
| 328 |
if ((key = OPENSSL_zalloc(keylen)) == NULL) |
| 329 |
return NULL; |
| 330 |
|
| 331 |
if (kdf_pbkdf1_derive(vctx, key, keylen, params) == 0) { |
| 332 |
OPENSSL_free(key); |
| 333 |
return NULL; |
| 334 |
} |
| 335 |
|
| 336 |
import_params[0] = OSSL_PARAM_construct_octet_string(OSSL_SKEY_PARAM_RAW_BYTES, |
| 337 |
(void *)key, keylen); |
| 338 |
|
| 339 |
ret = import(provctx, OSSL_SKEYMGMT_SELECT_SECRET_KEY, import_params); |
| 340 |
OPENSSL_free(key); |
| 341 |
|
| 342 |
return ret; |
| 343 |
} |
| 344 |
|
| 345 |
const OSSL_DISPATCH ossl_kdf_pbkdf1_functions[] = { |
| 346 |
{ OSSL_FUNC_KDF_NEWCTX, (void (*)(void))kdf_pbkdf1_new }, |
| 347 |
{ OSSL_FUNC_KDF_DUPCTX, (void (*)(void))kdf_pbkdf1_dup }, |
| 348 |
{ OSSL_FUNC_KDF_FREECTX, (void (*)(void))kdf_pbkdf1_free }, |
| 349 |
{ OSSL_FUNC_KDF_RESET, (void (*)(void))kdf_pbkdf1_reset }, |
| 350 |
{ OSSL_FUNC_KDF_DERIVE, (void (*)(void))kdf_pbkdf1_derive }, |
| 351 |
{ OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, |
| 352 |
(void (*)(void))kdf_pbkdf1_settable_ctx_params }, |
| 353 |
{ OSSL_FUNC_KDF_SET_CTX_PARAMS, (void (*)(void))kdf_pbkdf1_set_ctx_params }, |
| 354 |
{ OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, |
| 355 |
(void (*)(void))kdf_pbkdf1_gettable_ctx_params }, |
| 356 |
{ OSSL_FUNC_KDF_GET_CTX_PARAMS, (void (*)(void))kdf_pbkdf1_get_ctx_params }, |
| 357 |
{ OSSL_FUNC_KDF_SET_SKEY, (void (*)(void))kdf_pbkdf1_set_skey }, |
| 358 |
{ OSSL_FUNC_KDF_DERIVE_SKEY, (void (*)(void))kdf_pbkdf1_derive_skey }, |
| 359 |
OSSL_DISPATCH_END |
| 360 |
}; |