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