Path: blob/main/crypto/openssl/providers/implementations/kdfs/krb5kdf.c
48383 views
/*1* Copyright 2018-2025 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89/*10* DES low level APIs are deprecated for public use, but still ok for internal11* use. We access the DES_set_odd_parity(3) function here.12*/13#include "internal/deprecated.h"1415#include <stdlib.h>16#include <stdarg.h>17#include <string.h>1819#include <openssl/core_names.h>20#include <openssl/des.h>21#include <openssl/evp.h>22#include <openssl/kdf.h>23#include <openssl/proverr.h>2425#include "internal/cryptlib.h"26#include "crypto/evp.h"27#include "internal/numbers.h"28#include "prov/implementations.h"29#include "prov/provider_ctx.h"30#include "prov/provider_util.h"31#include "prov/providercommon.h"3233/* KRB5 KDF defined in RFC 3961, Section 5.1 */3435static OSSL_FUNC_kdf_newctx_fn krb5kdf_new;36static OSSL_FUNC_kdf_dupctx_fn krb5kdf_dup;37static OSSL_FUNC_kdf_freectx_fn krb5kdf_free;38static OSSL_FUNC_kdf_reset_fn krb5kdf_reset;39static OSSL_FUNC_kdf_derive_fn krb5kdf_derive;40static OSSL_FUNC_kdf_settable_ctx_params_fn krb5kdf_settable_ctx_params;41static OSSL_FUNC_kdf_set_ctx_params_fn krb5kdf_set_ctx_params;42static OSSL_FUNC_kdf_gettable_ctx_params_fn krb5kdf_gettable_ctx_params;43static OSSL_FUNC_kdf_get_ctx_params_fn krb5kdf_get_ctx_params;4445static int KRB5KDF(const EVP_CIPHER *cipher, ENGINE *engine,46const unsigned char *key, size_t key_len,47const unsigned char *constant, size_t constant_len,48unsigned char *okey, size_t okey_len);4950typedef struct {51void *provctx;52PROV_CIPHER cipher;53unsigned char *key;54size_t key_len;55unsigned char *constant;56size_t constant_len;57} KRB5KDF_CTX;5859static void *krb5kdf_new(void *provctx)60{61KRB5KDF_CTX *ctx;6263if (!ossl_prov_is_running())64return NULL;6566if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)67return NULL;68ctx->provctx = provctx;69return ctx;70}7172static void krb5kdf_free(void *vctx)73{74KRB5KDF_CTX *ctx = (KRB5KDF_CTX *)vctx;7576if (ctx != NULL) {77krb5kdf_reset(ctx);78OPENSSL_free(ctx);79}80}8182static void krb5kdf_reset(void *vctx)83{84KRB5KDF_CTX *ctx = (KRB5KDF_CTX *)vctx;85void *provctx = ctx->provctx;8687ossl_prov_cipher_reset(&ctx->cipher);88OPENSSL_clear_free(ctx->key, ctx->key_len);89OPENSSL_clear_free(ctx->constant, ctx->constant_len);90memset(ctx, 0, sizeof(*ctx));91ctx->provctx = provctx;92}9394static int krb5kdf_set_membuf(unsigned char **dst, size_t *dst_len,95const OSSL_PARAM *p)96{97OPENSSL_clear_free(*dst, *dst_len);98*dst = NULL;99*dst_len = 0;100return OSSL_PARAM_get_octet_string(p, (void **)dst, 0, dst_len);101}102103static void *krb5kdf_dup(void *vctx)104{105const KRB5KDF_CTX *src = (const KRB5KDF_CTX *)vctx;106KRB5KDF_CTX *dest;107108dest = krb5kdf_new(src->provctx);109if (dest != NULL) {110if (!ossl_prov_memdup(src->key, src->key_len,111&dest->key, &dest->key_len)112|| !ossl_prov_memdup(src->constant, src->constant_len,113&dest->constant , &dest->constant_len)114|| !ossl_prov_cipher_copy(&dest->cipher, &src->cipher))115goto err;116}117return dest;118119err:120krb5kdf_free(dest);121return NULL;122}123124static int krb5kdf_derive(void *vctx, unsigned char *key, size_t keylen,125const OSSL_PARAM params[])126{127KRB5KDF_CTX *ctx = (KRB5KDF_CTX *)vctx;128const EVP_CIPHER *cipher;129ENGINE *engine;130131if (!ossl_prov_is_running() || !krb5kdf_set_ctx_params(ctx, params))132return 0;133134cipher = ossl_prov_cipher_cipher(&ctx->cipher);135if (cipher == NULL) {136ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CIPHER);137return 0;138}139if (ctx->key == NULL) {140ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);141return 0;142}143if (ctx->constant == NULL) {144ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CONSTANT);145return 0;146}147engine = ossl_prov_cipher_engine(&ctx->cipher);148return KRB5KDF(cipher, engine, ctx->key, ctx->key_len,149ctx->constant, ctx->constant_len,150key, keylen);151}152153static int krb5kdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])154{155const OSSL_PARAM *p;156KRB5KDF_CTX *ctx = vctx;157OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx);158159if (ossl_param_is_empty(params))160return 1;161162if (!ossl_prov_cipher_load_from_params(&ctx->cipher, params, provctx))163return 0;164165if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KEY)) != NULL)166if (!krb5kdf_set_membuf(&ctx->key, &ctx->key_len, p))167return 0;168169if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_CONSTANT))170!= NULL)171if (!krb5kdf_set_membuf(&ctx->constant, &ctx->constant_len, p))172return 0;173174return 1;175}176177static const OSSL_PARAM *krb5kdf_settable_ctx_params(ossl_unused void *ctx,178ossl_unused void *provctx)179{180static const OSSL_PARAM known_settable_ctx_params[] = {181OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),182OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_CIPHER, NULL, 0),183OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0),184OSSL_PARAM_octet_string(OSSL_KDF_PARAM_CONSTANT, NULL, 0),185OSSL_PARAM_END186};187return known_settable_ctx_params;188}189190static int krb5kdf_get_ctx_params(void *vctx, OSSL_PARAM params[])191{192KRB5KDF_CTX *ctx = (KRB5KDF_CTX *)vctx;193const EVP_CIPHER *cipher;194size_t len;195OSSL_PARAM *p;196197cipher = ossl_prov_cipher_cipher(&ctx->cipher);198if (cipher)199len = EVP_CIPHER_get_key_length(cipher);200else201len = EVP_MAX_KEY_LENGTH;202203if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)204return OSSL_PARAM_set_size_t(p, len);205return -2;206}207208static const OSSL_PARAM *krb5kdf_gettable_ctx_params(ossl_unused void *ctx,209ossl_unused void *provctx)210{211static const OSSL_PARAM known_gettable_ctx_params[] = {212OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),213OSSL_PARAM_END214};215return known_gettable_ctx_params;216}217218const OSSL_DISPATCH ossl_kdf_krb5kdf_functions[] = {219{ OSSL_FUNC_KDF_NEWCTX, (void(*)(void))krb5kdf_new },220{ OSSL_FUNC_KDF_DUPCTX, (void(*)(void))krb5kdf_dup },221{ OSSL_FUNC_KDF_FREECTX, (void(*)(void))krb5kdf_free },222{ OSSL_FUNC_KDF_RESET, (void(*)(void))krb5kdf_reset },223{ OSSL_FUNC_KDF_DERIVE, (void(*)(void))krb5kdf_derive },224{ OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,225(void(*)(void))krb5kdf_settable_ctx_params },226{ OSSL_FUNC_KDF_SET_CTX_PARAMS,227(void(*)(void))krb5kdf_set_ctx_params },228{ OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,229(void(*)(void))krb5kdf_gettable_ctx_params },230{ OSSL_FUNC_KDF_GET_CTX_PARAMS,231(void(*)(void))krb5kdf_get_ctx_params },232OSSL_DISPATCH_END233};234235#ifndef OPENSSL_NO_DES236/*237* DES3 is a special case, it requires a random-to-key function and its238* input truncated to 21 bytes of the 24 produced by the cipher.239* See RFC3961 6.3.1240*/241static int fixup_des3_key(unsigned char *key)242{243unsigned char *cblock;244int i, j;245246for (i = 2; i >= 0; i--) {247cblock = &key[i * 8];248memmove(cblock, &key[i * 7], 7);249cblock[7] = 0;250for (j = 0; j < 7; j++)251cblock[7] |= (cblock[j] & 1) << (j + 1);252DES_set_odd_parity((DES_cblock *)cblock);253}254255/* fail if keys are such that triple des degrades to single des */256if (CRYPTO_memcmp(&key[0], &key[8], 8) == 0 ||257CRYPTO_memcmp(&key[8], &key[16], 8) == 0) {258return 0;259}260261return 1;262}263#endif264265/*266* N-fold(K) where blocksize is N, and constant_len is K267* Note: Here |= denotes concatenation268*269* L = lcm(N,K)270* R = L/K271*272* for r: 1 -> R273* s |= constant rot 13*(r-1))274*275* block = 0276* for k: 1 -> K277* block += s[N(k-1)..(N-1)k] (ones'-complement addition)278*279* Optimizing for space we compute:280* for each l in L-1 -> 0:281* s[l] = (constant rot 13*(l/K))[l%k]282* block[l % N] += s[l] (with carry)283* finally add carry if any284*/285static void n_fold(unsigned char *block, unsigned int blocksize,286const unsigned char *constant, size_t constant_len)287{288unsigned int tmp, gcd, remainder, lcm, carry;289int b, l;290291if (constant_len == blocksize) {292memcpy(block, constant, constant_len);293return;294}295296/* Least Common Multiple of lengths: LCM(a,b)*/297gcd = blocksize;298remainder = constant_len;299/* Calculate Great Common Divisor first GCD(a,b) */300while (remainder != 0) {301tmp = gcd % remainder;302gcd = remainder;303remainder = tmp;304}305/* resulting a is the GCD, LCM(a,b) = |a*b|/GCD(a,b) */306lcm = blocksize * constant_len / gcd;307308/* now spread out the bits */309memset(block, 0, blocksize);310311/* last to first to be able to bring carry forward */312carry = 0;313for (l = lcm - 1; l >= 0; l--) {314unsigned int rotbits, rshift, rbyte;315316/* destination byte in block is l % N */317b = l % blocksize;318/* Our virtual s buffer is R = L/K long (K = constant_len) */319/* So we rotate backwards from R-1 to 0 (none) rotations */320rotbits = 13 * (l / constant_len);321/* find the byte on s where rotbits falls onto */322rbyte = l - (rotbits / 8);323/* calculate how much shift on that byte */324rshift = rotbits & 0x07;325/* rbyte % constant_len gives us the unrotated byte in the326* constant buffer, get also the previous byte then327* appropriately shift them to get the rotated byte we need */328tmp = (constant[(rbyte-1) % constant_len] << (8 - rshift)329| constant[rbyte % constant_len] >> rshift)330& 0xff;331/* add with carry to any value placed by previous passes */332tmp += carry + block[b];333block[b] = tmp & 0xff;334/* save any carry that may be left */335carry = tmp >> 8;336}337338/* if any carry is left at the end, add it through the number */339for (b = blocksize - 1; b >= 0 && carry != 0; b--) {340carry += block[b];341block[b] = carry & 0xff;342carry >>= 8;343}344}345346static int cipher_init(EVP_CIPHER_CTX *ctx,347const EVP_CIPHER *cipher, ENGINE *engine,348const unsigned char *key, size_t key_len)349{350int klen, ret;351352ret = EVP_EncryptInit_ex(ctx, cipher, engine, NULL, NULL);353if (!ret)354goto out;355/* set the key len for the odd variable key len cipher */356klen = EVP_CIPHER_CTX_get_key_length(ctx);357if (key_len != (size_t)klen) {358ret = EVP_CIPHER_CTX_set_key_length(ctx, key_len);359if (ret <= 0) {360ret = 0;361goto out;362}363}364ret = EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL);365if (!ret)366goto out;367/* we never want padding, either the length requested is a multiple of368* the cipher block size or we are passed a cipher that can cope with369* partial blocks via techniques like cipher text stealing */370ret = EVP_CIPHER_CTX_set_padding(ctx, 0);371if (!ret)372goto out;373374out:375return ret;376}377378static int KRB5KDF(const EVP_CIPHER *cipher, ENGINE *engine,379const unsigned char *key, size_t key_len,380const unsigned char *constant, size_t constant_len,381unsigned char *okey, size_t okey_len)382{383EVP_CIPHER_CTX *ctx = NULL;384unsigned char block[EVP_MAX_BLOCK_LENGTH * 2];385unsigned char *plainblock, *cipherblock;386size_t blocksize;387size_t cipherlen;388size_t osize;389#ifndef OPENSSL_NO_DES390int des3_no_fixup = 0;391#endif392int ret;393394if (key_len != okey_len) {395#ifndef OPENSSL_NO_DES396/* special case for 3des, where the caller may be requesting397* the random raw key, instead of the fixed up key */398if (EVP_CIPHER_get_nid(cipher) == NID_des_ede3_cbc &&399key_len == 24 && okey_len == 21) {400des3_no_fixup = 1;401} else {402#endif403ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_OUTPUT_BUFFER_SIZE);404return 0;405#ifndef OPENSSL_NO_DES406}407#endif408}409410ctx = EVP_CIPHER_CTX_new();411if (ctx == NULL)412return 0;413414ret = cipher_init(ctx, cipher, engine, key, key_len);415if (!ret)416goto out;417418/* Initialize input block */419blocksize = EVP_CIPHER_CTX_get_block_size(ctx);420421if (blocksize == 0) {422ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CIPHER);423ret = 0;424goto out;425}426427if (constant_len > blocksize) {428ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CONSTANT_LENGTH);429ret = 0;430goto out;431}432433n_fold(block, blocksize, constant, constant_len);434plainblock = block;435cipherblock = block + EVP_MAX_BLOCK_LENGTH;436437for (osize = 0; osize < okey_len; osize += cipherlen) {438int olen;439440ret = EVP_EncryptUpdate(ctx, cipherblock, &olen,441plainblock, blocksize);442if (!ret)443goto out;444cipherlen = olen;445ret = EVP_EncryptFinal_ex(ctx, cipherblock, &olen);446if (!ret)447goto out;448if (olen != 0) {449ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_FINAL_BLOCK_LENGTH);450ret = 0;451goto out;452}453454/* write cipherblock out */455if (cipherlen > okey_len - osize)456cipherlen = okey_len - osize;457memcpy(okey + osize, cipherblock, cipherlen);458459if (okey_len > osize + cipherlen) {460/* we need to reinitialize cipher context per spec */461ret = EVP_CIPHER_CTX_reset(ctx);462if (!ret)463goto out;464ret = cipher_init(ctx, cipher, engine, key, key_len);465if (!ret)466goto out;467468/* also swap block offsets so last ciphertext becomes new469* plaintext */470plainblock = cipherblock;471if (cipherblock == block) {472cipherblock += EVP_MAX_BLOCK_LENGTH;473} else {474cipherblock = block;475}476}477}478479#ifndef OPENSSL_NO_DES480if (EVP_CIPHER_get_nid(cipher) == NID_des_ede3_cbc && !des3_no_fixup) {481ret = fixup_des3_key(okey);482if (!ret) {483ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY);484goto out;485}486}487#endif488489ret = 1;490491out:492EVP_CIPHER_CTX_free(ctx);493OPENSSL_cleanse(block, EVP_MAX_BLOCK_LENGTH * 2);494return ret;495}496497498499