| 1 |
/* |
| 2 |
* Copyright 2018-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 <stdlib.h> |
| 16 |
#include <stdarg.h> |
| 17 |
#include <string.h> |
| 18 |
#include <openssl/evp.h> |
| 19 |
#include <openssl/kdf.h> |
| 20 |
#include <openssl/core_names.h> |
| 21 |
#include <openssl/proverr.h> |
| 22 |
#include "internal/cryptlib.h" |
| 23 |
#include "internal/numbers.h" |
| 24 |
#include "crypto/evp.h" |
| 25 |
#include "prov/provider_ctx.h" |
| 26 |
#include "prov/providercommon.h" |
| 27 |
#include "prov/implementations.h" |
| 28 |
#include "prov/provider_util.h" |
| 29 |
#include "prov/securitycheck.h" |
| 30 |
|
| 31 |
/* See RFC 4253, Section 7.2 */ |
| 32 |
static OSSL_FUNC_kdf_newctx_fn kdf_sshkdf_new; |
| 33 |
static OSSL_FUNC_kdf_dupctx_fn kdf_sshkdf_dup; |
| 34 |
static OSSL_FUNC_kdf_freectx_fn kdf_sshkdf_free; |
| 35 |
static OSSL_FUNC_kdf_reset_fn kdf_sshkdf_reset; |
| 36 |
static OSSL_FUNC_kdf_derive_fn kdf_sshkdf_derive; |
| 37 |
static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_sshkdf_settable_ctx_params; |
| 38 |
static OSSL_FUNC_kdf_set_ctx_params_fn kdf_sshkdf_set_ctx_params; |
| 39 |
static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_sshkdf_gettable_ctx_params; |
| 40 |
static OSSL_FUNC_kdf_get_ctx_params_fn kdf_sshkdf_get_ctx_params; |
| 41 |
|
| 42 |
static int SSHKDF(const EVP_MD *evp_md, |
| 43 |
const unsigned char *key, size_t key_len, |
| 44 |
const unsigned char *xcghash, size_t xcghash_len, |
| 45 |
const unsigned char *session_id, size_t session_id_len, |
| 46 |
char type, unsigned char *okey, size_t okey_len); |
| 47 |
|
| 48 |
typedef struct { |
| 49 |
void *provctx; |
| 50 |
PROV_DIGEST digest; |
| 51 |
unsigned char *key; /* K */ |
| 52 |
size_t key_len; |
| 53 |
unsigned char *xcghash; /* H */ |
| 54 |
size_t xcghash_len; |
| 55 |
char type; /* X */ |
| 56 |
unsigned char *session_id; |
| 57 |
size_t session_id_len; |
| 58 |
OSSL_FIPS_IND_DECLARE |
| 59 |
} KDF_SSHKDF; |
| 60 |
|
| 61 |
static void *kdf_sshkdf_new(void *provctx) |
| 62 |
{ |
| 63 |
KDF_SSHKDF *ctx; |
| 64 |
|
| 65 |
if (!ossl_prov_is_running()) |
| 66 |
return NULL; |
| 67 |
|
| 68 |
if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) != NULL) { |
| 69 |
ctx->provctx = provctx; |
| 70 |
OSSL_FIPS_IND_INIT(ctx) |
| 71 |
} |
| 72 |
return ctx; |
| 73 |
} |
| 74 |
|
| 75 |
static void kdf_sshkdf_free(void *vctx) |
| 76 |
{ |
| 77 |
KDF_SSHKDF *ctx = (KDF_SSHKDF *)vctx; |
| 78 |
|
| 79 |
if (ctx != NULL) { |
| 80 |
kdf_sshkdf_reset(ctx); |
| 81 |
OPENSSL_free(ctx); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
static void kdf_sshkdf_reset(void *vctx) |
| 86 |
{ |
| 87 |
KDF_SSHKDF *ctx = (KDF_SSHKDF *)vctx; |
| 88 |
void *provctx = ctx->provctx; |
| 89 |
|
| 90 |
ossl_prov_digest_reset(&ctx->digest); |
| 91 |
OPENSSL_clear_free(ctx->key, ctx->key_len); |
| 92 |
OPENSSL_clear_free(ctx->xcghash, ctx->xcghash_len); |
| 93 |
OPENSSL_clear_free(ctx->session_id, ctx->session_id_len); |
| 94 |
memset(ctx, 0, sizeof(*ctx)); |
| 95 |
ctx->provctx = provctx; |
| 96 |
} |
| 97 |
|
| 98 |
static void *kdf_sshkdf_dup(void *vctx) |
| 99 |
{ |
| 100 |
const KDF_SSHKDF *src = (const KDF_SSHKDF *)vctx; |
| 101 |
KDF_SSHKDF *dest; |
| 102 |
|
| 103 |
dest = kdf_sshkdf_new(src->provctx); |
| 104 |
if (dest != NULL) { |
| 105 |
if (!ossl_prov_memdup(src->key, src->key_len, |
| 106 |
&dest->key, &dest->key_len) |
| 107 |
|| !ossl_prov_memdup(src->xcghash, src->xcghash_len, |
| 108 |
&dest->xcghash, &dest->xcghash_len) |
| 109 |
|| !ossl_prov_memdup(src->session_id, src->session_id_len, |
| 110 |
&dest->session_id, &dest->session_id_len) |
| 111 |
|| !ossl_prov_digest_copy(&dest->digest, &src->digest)) |
| 112 |
goto err; |
| 113 |
dest->type = src->type; |
| 114 |
OSSL_FIPS_IND_COPY(dest, src) |
| 115 |
} |
| 116 |
return dest; |
| 117 |
|
| 118 |
err: |
| 119 |
kdf_sshkdf_free(dest); |
| 120 |
return NULL; |
| 121 |
} |
| 122 |
|
| 123 |
static int sshkdf_set_membuf(unsigned char **dst, size_t *dst_len, |
| 124 |
const OSSL_PARAM *p) |
| 125 |
{ |
| 126 |
OPENSSL_clear_free(*dst, *dst_len); |
| 127 |
*dst = NULL; |
| 128 |
*dst_len = 0; |
| 129 |
return OSSL_PARAM_get_octet_string(p, (void **)dst, 0, dst_len); |
| 130 |
} |
| 131 |
|
| 132 |
#ifdef FIPS_MODULE |
| 133 |
static int fips_digest_check_passed(KDF_SSHKDF *ctx, const EVP_MD *md) |
| 134 |
{ |
| 135 |
OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx); |
| 136 |
/* |
| 137 |
* Perform digest check |
| 138 |
* |
| 139 |
* According to NIST SP 800-135r1 section 5.2, the valid hash functions are |
| 140 |
* specified in FIPS 180-3. ACVP also only lists the same set of hash |
| 141 |
* functions. |
| 142 |
*/ |
| 143 |
int digest_unapproved = !EVP_MD_is_a(md, SN_sha1) |
| 144 |
&& !EVP_MD_is_a(md, SN_sha224) |
| 145 |
&& !EVP_MD_is_a(md, SN_sha256) |
| 146 |
&& !EVP_MD_is_a(md, SN_sha384) |
| 147 |
&& !EVP_MD_is_a(md, SN_sha512); |
| 148 |
|
| 149 |
if (digest_unapproved) { |
| 150 |
if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0, |
| 151 |
libctx, "SSHKDF", "Digest", |
| 152 |
ossl_fips_config_sshkdf_digest_check)) { |
| 153 |
ERR_raise(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED); |
| 154 |
return 0; |
| 155 |
} |
| 156 |
} |
| 157 |
return 1; |
| 158 |
} |
| 159 |
|
| 160 |
static int fips_key_check_passed(KDF_SSHKDF *ctx) |
| 161 |
{ |
| 162 |
OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx); |
| 163 |
int key_approved = ossl_kdf_check_key_size(ctx->key_len); |
| 164 |
|
| 165 |
if (!key_approved) { |
| 166 |
if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE1, |
| 167 |
libctx, "SSHKDF", "Key size", |
| 168 |
ossl_fips_config_sshkdf_key_check)) { |
| 169 |
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
| 170 |
return 0; |
| 171 |
} |
| 172 |
} |
| 173 |
return 1; |
| 174 |
} |
| 175 |
#endif |
| 176 |
|
| 177 |
static int kdf_sshkdf_derive(void *vctx, unsigned char *key, size_t keylen, |
| 178 |
const OSSL_PARAM params[]) |
| 179 |
{ |
| 180 |
KDF_SSHKDF *ctx = (KDF_SSHKDF *)vctx; |
| 181 |
const EVP_MD *md; |
| 182 |
|
| 183 |
if (!ossl_prov_is_running() || !kdf_sshkdf_set_ctx_params(ctx, params)) |
| 184 |
return 0; |
| 185 |
|
| 186 |
md = ossl_prov_digest_md(&ctx->digest); |
| 187 |
if (md == NULL) { |
| 188 |
ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST); |
| 189 |
return 0; |
| 190 |
} |
| 191 |
if (ctx->key == NULL) { |
| 192 |
ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY); |
| 193 |
return 0; |
| 194 |
} |
| 195 |
if (ctx->xcghash == NULL) { |
| 196 |
ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_XCGHASH); |
| 197 |
return 0; |
| 198 |
} |
| 199 |
if (ctx->session_id == NULL) { |
| 200 |
ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SESSION_ID); |
| 201 |
return 0; |
| 202 |
} |
| 203 |
if (ctx->type == 0) { |
| 204 |
ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_TYPE); |
| 205 |
return 0; |
| 206 |
} |
| 207 |
|
| 208 |
return SSHKDF(md, ctx->key, ctx->key_len, |
| 209 |
ctx->xcghash, ctx->xcghash_len, |
| 210 |
ctx->session_id, ctx->session_id_len, |
| 211 |
ctx->type, key, keylen); |
| 212 |
} |
| 213 |
|
| 214 |
/* clang-format off */ |
| 215 |
{- produce_param_decoder('sshkdf_set_ctx_params', |
| 216 |
(['OSSL_KDF_PARAM_PROPERTIES', 'propq', 'utf8_string'], |
| 217 |
['OSSL_ALG_PARAM_ENGINE', 'engine', 'utf8_string', 'hidden'], |
| 218 |
['OSSL_KDF_PARAM_DIGEST', 'digest', 'utf8_string'], |
| 219 |
['OSSL_KDF_PARAM_KEY', 'key', 'octet_string'], |
| 220 |
['OSSL_KDF_PARAM_SSHKDF_XCGHASH', 'xcg', 'octet_string'], |
| 221 |
['OSSL_KDF_PARAM_SSHKDF_SESSION_ID', 'sid', 'octet_string'], |
| 222 |
['OSSL_KDF_PARAM_SSHKDF_TYPE', 'type', 'utf8_string'], |
| 223 |
['OSSL_KDF_PARAM_FIPS_DIGEST_CHECK', 'ind_d', 'int', 'fips'], |
| 224 |
['OSSL_KDF_PARAM_FIPS_KEY_CHECK', 'ind_k', 'int', 'fips'], |
| 225 |
)); -} |
| 226 |
/* clang-format on */ |
| 227 |
|
| 228 |
static int kdf_sshkdf_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
| 229 |
{ |
| 230 |
struct sshkdf_set_ctx_params_st p; |
| 231 |
KDF_SSHKDF *ctx = vctx; |
| 232 |
OSSL_LIB_CTX *provctx; |
| 233 |
|
| 234 |
if (ctx == NULL || !sshkdf_set_ctx_params_decoder(params, &p)) |
| 235 |
return 0; |
| 236 |
|
| 237 |
provctx = PROV_LIBCTX_OF(ctx->provctx); |
| 238 |
|
| 239 |
if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, p.ind_d)) |
| 240 |
return 0; |
| 241 |
if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE1, p.ind_k)) |
| 242 |
return 0; |
| 243 |
|
| 244 |
if (p.digest != NULL) { |
| 245 |
const EVP_MD *md = NULL; |
| 246 |
|
| 247 |
if (!ossl_prov_digest_load(&ctx->digest, p.digest, |
| 248 |
p.propq, p.engine, provctx)) |
| 249 |
return 0; |
| 250 |
|
| 251 |
md = ossl_prov_digest_md(&ctx->digest); |
| 252 |
if (EVP_MD_xof(md)) { |
| 253 |
ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED); |
| 254 |
return 0; |
| 255 |
} |
| 256 |
|
| 257 |
#ifdef FIPS_MODULE |
| 258 |
if (!fips_digest_check_passed(ctx, md)) |
| 259 |
return 0; |
| 260 |
#endif |
| 261 |
} |
| 262 |
|
| 263 |
if (p.key != NULL) { |
| 264 |
if (!sshkdf_set_membuf(&ctx->key, &ctx->key_len, p.key)) |
| 265 |
return 0; |
| 266 |
|
| 267 |
#ifdef FIPS_MODULE |
| 268 |
if (!fips_key_check_passed(ctx)) |
| 269 |
return 0; |
| 270 |
#endif |
| 271 |
} |
| 272 |
|
| 273 |
if (p.xcg != NULL |
| 274 |
&& !sshkdf_set_membuf(&ctx->xcghash, &ctx->xcghash_len, p.xcg)) |
| 275 |
return 0; |
| 276 |
|
| 277 |
if (p.sid != NULL |
| 278 |
&& !sshkdf_set_membuf(&ctx->session_id, &ctx->session_id_len, p.sid)) |
| 279 |
return 0; |
| 280 |
|
| 281 |
if (p.type != NULL) { |
| 282 |
const char *kdftype; |
| 283 |
|
| 284 |
if (!OSSL_PARAM_get_utf8_string_ptr(p.type, &kdftype)) |
| 285 |
return 0; |
| 286 |
/* Expect one character (byte in this case) */ |
| 287 |
if (kdftype == NULL || p.type->data_size != 1) |
| 288 |
return 0; |
| 289 |
if (kdftype[0] < 65 || kdftype[0] > 70) { |
| 290 |
ERR_raise(ERR_LIB_PROV, PROV_R_VALUE_ERROR); |
| 291 |
return 0; |
| 292 |
} |
| 293 |
ctx->type = kdftype[0]; |
| 294 |
} |
| 295 |
return 1; |
| 296 |
} |
| 297 |
|
| 298 |
static const OSSL_PARAM *kdf_sshkdf_settable_ctx_params(ossl_unused void *ctx, |
| 299 |
ossl_unused void *p_ctx) |
| 300 |
{ |
| 301 |
return sshkdf_set_ctx_params_list; |
| 302 |
} |
| 303 |
|
| 304 |
/* clang-format off */ |
| 305 |
{- produce_param_decoder('sshkdf_get_ctx_params', |
| 306 |
(['OSSL_KDF_PARAM_SIZE', 'size', 'size_t'], |
| 307 |
['OSSL_KDF_PARAM_FIPS_APPROVED_INDICATOR', 'ind', 'int', 'fips'], |
| 308 |
)); -} |
| 309 |
/* clang-format on */ |
| 310 |
|
| 311 |
static int kdf_sshkdf_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
| 312 |
{ |
| 313 |
KDF_SSHKDF *ctx = (KDF_SSHKDF *)vctx; |
| 314 |
struct sshkdf_get_ctx_params_st p; |
| 315 |
|
| 316 |
if (ctx == NULL || !sshkdf_get_ctx_params_decoder(params, &p)) |
| 317 |
return 0; |
| 318 |
|
| 319 |
if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, SIZE_MAX)) |
| 320 |
return 0; |
| 321 |
|
| 322 |
if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(ctx, p.ind)) |
| 323 |
return 0; |
| 324 |
return 1; |
| 325 |
} |
| 326 |
|
| 327 |
static const OSSL_PARAM *kdf_sshkdf_gettable_ctx_params(ossl_unused void *ctx, |
| 328 |
ossl_unused void *p_ctx) |
| 329 |
{ |
| 330 |
return sshkdf_get_ctx_params_list; |
| 331 |
} |
| 332 |
|
| 333 |
const OSSL_DISPATCH ossl_kdf_sshkdf_functions[] = { |
| 334 |
{ OSSL_FUNC_KDF_NEWCTX, (void (*)(void))kdf_sshkdf_new }, |
| 335 |
{ OSSL_FUNC_KDF_DUPCTX, (void (*)(void))kdf_sshkdf_dup }, |
| 336 |
{ OSSL_FUNC_KDF_FREECTX, (void (*)(void))kdf_sshkdf_free }, |
| 337 |
{ OSSL_FUNC_KDF_RESET, (void (*)(void))kdf_sshkdf_reset }, |
| 338 |
{ OSSL_FUNC_KDF_DERIVE, (void (*)(void))kdf_sshkdf_derive }, |
| 339 |
{ OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, |
| 340 |
(void (*)(void))kdf_sshkdf_settable_ctx_params }, |
| 341 |
{ OSSL_FUNC_KDF_SET_CTX_PARAMS, (void (*)(void))kdf_sshkdf_set_ctx_params }, |
| 342 |
{ OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, |
| 343 |
(void (*)(void))kdf_sshkdf_gettable_ctx_params }, |
| 344 |
{ OSSL_FUNC_KDF_GET_CTX_PARAMS, (void (*)(void))kdf_sshkdf_get_ctx_params }, |
| 345 |
OSSL_DISPATCH_END |
| 346 |
}; |
| 347 |
|
| 348 |
static int SSHKDF(const EVP_MD *evp_md, |
| 349 |
const unsigned char *key, size_t key_len, |
| 350 |
const unsigned char *xcghash, size_t xcghash_len, |
| 351 |
const unsigned char *session_id, size_t session_id_len, |
| 352 |
char type, unsigned char *okey, size_t okey_len) |
| 353 |
{ |
| 354 |
EVP_MD_CTX *md = NULL; |
| 355 |
unsigned char digest[EVP_MAX_MD_SIZE]; |
| 356 |
unsigned int dsize = 0; |
| 357 |
size_t cursize = 0; |
| 358 |
int ret = 0; |
| 359 |
|
| 360 |
md = EVP_MD_CTX_new(); |
| 361 |
if (md == NULL) |
| 362 |
return 0; |
| 363 |
|
| 364 |
if (!EVP_DigestInit_ex(md, evp_md, NULL)) |
| 365 |
goto out; |
| 366 |
|
| 367 |
if (!EVP_DigestUpdate(md, key, key_len)) |
| 368 |
goto out; |
| 369 |
|
| 370 |
if (!EVP_DigestUpdate(md, xcghash, xcghash_len)) |
| 371 |
goto out; |
| 372 |
|
| 373 |
if (!EVP_DigestUpdate(md, &type, 1)) |
| 374 |
goto out; |
| 375 |
|
| 376 |
if (!EVP_DigestUpdate(md, session_id, session_id_len)) |
| 377 |
goto out; |
| 378 |
|
| 379 |
if (!EVP_DigestFinal_ex(md, digest, &dsize)) |
| 380 |
goto out; |
| 381 |
|
| 382 |
if (okey_len < dsize) { |
| 383 |
memcpy(okey, digest, okey_len); |
| 384 |
ret = 1; |
| 385 |
goto out; |
| 386 |
} |
| 387 |
|
| 388 |
memcpy(okey, digest, dsize); |
| 389 |
|
| 390 |
for (cursize = dsize; cursize < okey_len; cursize += dsize) { |
| 391 |
|
| 392 |
if (!EVP_DigestInit_ex(md, evp_md, NULL)) |
| 393 |
goto out; |
| 394 |
|
| 395 |
if (!EVP_DigestUpdate(md, key, key_len)) |
| 396 |
goto out; |
| 397 |
|
| 398 |
if (!EVP_DigestUpdate(md, xcghash, xcghash_len)) |
| 399 |
goto out; |
| 400 |
|
| 401 |
if (!EVP_DigestUpdate(md, okey, cursize)) |
| 402 |
goto out; |
| 403 |
|
| 404 |
if (!EVP_DigestFinal_ex(md, digest, &dsize)) |
| 405 |
goto out; |
| 406 |
|
| 407 |
if (okey_len < cursize + dsize) { |
| 408 |
memcpy(okey + cursize, digest, okey_len - cursize); |
| 409 |
ret = 1; |
| 410 |
goto out; |
| 411 |
} |
| 412 |
|
| 413 |
memcpy(okey + cursize, digest, dsize); |
| 414 |
} |
| 415 |
|
| 416 |
ret = 1; |
| 417 |
|
| 418 |
out: |
| 419 |
EVP_MD_CTX_free(md); |
| 420 |
OPENSSL_cleanse(digest, EVP_MAX_MD_SIZE); |
| 421 |
return ret; |
| 422 |
} |