Path: blob/main/crypto/openssl/providers/implementations/kdfs/krb5kdf.c
108504 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 || CRYPTO_memcmp(&key[8], &key[16], 8) == 0) {257return 0;258}259260return 1;261}262#endif263264/*265* N-fold(K) where blocksize is N, and constant_len is K266* Note: Here |= denotes concatenation267*268* L = lcm(N,K)269* R = L/K270*271* for r: 1 -> R272* s |= constant rot 13*(r-1))273*274* block = 0275* for k: 1 -> K276* block += s[N(k-1)..(N-1)k] (ones'-complement addition)277*278* Optimizing for space we compute:279* for each l in L-1 -> 0:280* s[l] = (constant rot 13*(l/K))[l%k]281* block[l % N] += s[l] (with carry)282* finally add carry if any283*/284static void n_fold(unsigned char *block, unsigned int blocksize,285const unsigned char *constant, size_t constant_len)286{287unsigned int tmp, gcd, remainder, lcm, carry;288int b, l;289290if (constant_len == blocksize) {291memcpy(block, constant, constant_len);292return;293}294295/* Least Common Multiple of lengths: LCM(a,b)*/296gcd = blocksize;297remainder = constant_len;298/* Calculate Great Common Divisor first GCD(a,b) */299while (remainder != 0) {300tmp = gcd % remainder;301gcd = remainder;302remainder = tmp;303}304/* resulting a is the GCD, LCM(a,b) = |a*b|/GCD(a,b) */305lcm = blocksize * constant_len / gcd;306307/* now spread out the bits */308memset(block, 0, blocksize);309310/* last to first to be able to bring carry forward */311carry = 0;312for (l = lcm - 1; l >= 0; l--) {313unsigned int rotbits, rshift, rbyte;314315/* destination byte in block is l % N */316b = l % blocksize;317/* Our virtual s buffer is R = L/K long (K = constant_len) */318/* So we rotate backwards from R-1 to 0 (none) rotations */319rotbits = 13 * (l / constant_len);320/* find the byte on s where rotbits falls onto */321rbyte = l - (rotbits / 8);322/* calculate how much shift on that byte */323rshift = rotbits & 0x07;324/* rbyte % constant_len gives us the unrotated byte in the325* constant buffer, get also the previous byte then326* appropriately shift them to get the rotated byte we need */327tmp = (constant[(rbyte - 1) % constant_len] << (8 - rshift)328| constant[rbyte % constant_len] >> rshift)329& 0xff;330/* add with carry to any value placed by previous passes */331tmp += carry + block[b];332block[b] = tmp & 0xff;333/* save any carry that may be left */334carry = tmp >> 8;335}336337/* if any carry is left at the end, add it through the number */338for (b = blocksize - 1; b >= 0 && carry != 0; b--) {339carry += block[b];340block[b] = carry & 0xff;341carry >>= 8;342}343}344345static int cipher_init(EVP_CIPHER_CTX *ctx,346const EVP_CIPHER *cipher, ENGINE *engine,347const unsigned char *key, size_t key_len)348{349int klen, ret;350351ret = EVP_EncryptInit_ex(ctx, cipher, engine, NULL, NULL);352if (!ret)353goto out;354/* set the key len for the odd variable key len cipher */355klen = EVP_CIPHER_CTX_get_key_length(ctx);356if (key_len != (size_t)klen) {357ret = EVP_CIPHER_CTX_set_key_length(ctx, key_len);358if (ret <= 0) {359ret = 0;360goto out;361}362}363ret = EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL);364if (!ret)365goto out;366/* we never want padding, either the length requested is a multiple of367* the cipher block size or we are passed a cipher that can cope with368* partial blocks via techniques like cipher text stealing */369ret = EVP_CIPHER_CTX_set_padding(ctx, 0);370if (!ret)371goto out;372373out:374return ret;375}376377static int KRB5KDF(const EVP_CIPHER *cipher, ENGINE *engine,378const unsigned char *key, size_t key_len,379const unsigned char *constant, size_t constant_len,380unsigned char *okey, size_t okey_len)381{382EVP_CIPHER_CTX *ctx = NULL;383unsigned char block[EVP_MAX_BLOCK_LENGTH * 2];384unsigned char *plainblock, *cipherblock;385size_t blocksize;386size_t cipherlen;387size_t osize;388#ifndef OPENSSL_NO_DES389int des3_no_fixup = 0;390#endif391int ret;392393if (key_len != okey_len) {394#ifndef OPENSSL_NO_DES395/* special case for 3des, where the caller may be requesting396* the random raw key, instead of the fixed up key */397if (EVP_CIPHER_get_nid(cipher) == NID_des_ede3_cbc && key_len == 24 && okey_len == 21) {398des3_no_fixup = 1;399} else {400#endif401ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_OUTPUT_BUFFER_SIZE);402return 0;403#ifndef OPENSSL_NO_DES404}405#endif406}407408ctx = EVP_CIPHER_CTX_new();409if (ctx == NULL)410return 0;411412ret = cipher_init(ctx, cipher, engine, key, key_len);413if (!ret)414goto out;415416/* Initialize input block */417blocksize = EVP_CIPHER_CTX_get_block_size(ctx);418419if (blocksize == 0) {420ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CIPHER);421ret = 0;422goto out;423}424425if (constant_len > blocksize) {426ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CONSTANT_LENGTH);427ret = 0;428goto out;429}430431n_fold(block, blocksize, constant, constant_len);432plainblock = block;433cipherblock = block + EVP_MAX_BLOCK_LENGTH;434435for (osize = 0; osize < okey_len; osize += cipherlen) {436int olen;437438ret = EVP_EncryptUpdate(ctx, cipherblock, &olen,439plainblock, blocksize);440if (!ret)441goto out;442cipherlen = olen;443ret = EVP_EncryptFinal_ex(ctx, cipherblock, &olen);444if (!ret)445goto out;446if (olen != 0) {447ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_FINAL_BLOCK_LENGTH);448ret = 0;449goto out;450}451452/* write cipherblock out */453if (cipherlen > okey_len - osize)454cipherlen = okey_len - osize;455memcpy(okey + osize, cipherblock, cipherlen);456457if (okey_len > osize + cipherlen) {458/* we need to reinitialize cipher context per spec */459ret = EVP_CIPHER_CTX_reset(ctx);460if (!ret)461goto out;462ret = cipher_init(ctx, cipher, engine, key, key_len);463if (!ret)464goto out;465466/* also swap block offsets so last ciphertext becomes new467* plaintext */468plainblock = cipherblock;469if (cipherblock == block) {470cipherblock += EVP_MAX_BLOCK_LENGTH;471} else {472cipherblock = block;473}474}475}476477#ifndef OPENSSL_NO_DES478if (EVP_CIPHER_get_nid(cipher) == NID_des_ede3_cbc && !des3_no_fixup) {479ret = fixup_des3_key(okey);480if (!ret) {481ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY);482goto out;483}484}485#endif486487ret = 1;488489out:490EVP_CIPHER_CTX_free(ctx);491OPENSSL_cleanse(block, EVP_MAX_BLOCK_LENGTH * 2);492return ret;493}494495496