Coverage Report

Created: 2026-07-23 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/providers/implementations/kdfs/krb5kdf.c
Line
Count
Source
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
10
/*
11
 * DES low level APIs are deprecated for public use, but still ok for internal
12
 * use.  We access the DES_set_odd_parity(3) function here.
13
 */
14
#include "internal/deprecated.h"
15
16
#include <stdlib.h>
17
#include <stdarg.h>
18
#include <string.h>
19
20
#include <openssl/core_names.h>
21
#include <openssl/des.h>
22
#include <openssl/evp.h>
23
#include <openssl/kdf.h>
24
#include <openssl/proverr.h>
25
26
#include "internal/cryptlib.h"
27
#include "crypto/evp.h"
28
#include "internal/numbers.h"
29
#include "prov/implementations.h"
30
#include "prov/provider_ctx.h"
31
#include "prov/provider_util.h"
32
#include "prov/providercommon.h"
33
#include "providers/implementations/kdfs/krb5kdf.inc"
34
35
/* KRB5 KDF defined in RFC 3961, Section 5.1 */
36
37
static OSSL_FUNC_kdf_newctx_fn krb5kdf_new;
38
static OSSL_FUNC_kdf_dupctx_fn krb5kdf_dup;
39
static OSSL_FUNC_kdf_freectx_fn krb5kdf_free;
40
static OSSL_FUNC_kdf_reset_fn krb5kdf_reset;
41
static OSSL_FUNC_kdf_derive_fn krb5kdf_derive;
42
static OSSL_FUNC_kdf_settable_ctx_params_fn krb5kdf_settable_ctx_params;
43
static OSSL_FUNC_kdf_set_ctx_params_fn krb5kdf_set_ctx_params;
44
static OSSL_FUNC_kdf_gettable_ctx_params_fn krb5kdf_gettable_ctx_params;
45
static OSSL_FUNC_kdf_get_ctx_params_fn krb5kdf_get_ctx_params;
46
47
static int KRB5KDF(const EVP_CIPHER *cipher,
48
    const unsigned char *key, size_t key_len,
49
    const unsigned char *constant, size_t constant_len,
50
    unsigned char *okey, size_t okey_len);
51
52
typedef struct {
53
    void *provctx;
54
    PROV_CIPHER cipher;
55
    unsigned char *key;
56
    size_t key_len;
57
    unsigned char *constant;
58
    size_t constant_len;
59
} KRB5KDF_CTX;
60
61
static void *krb5kdf_new(void *provctx)
62
302
{
63
302
    KRB5KDF_CTX *ctx;
64
65
302
    if (!ossl_prov_is_running())
66
0
        return NULL;
67
68
302
    if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
69
0
        return NULL;
70
302
    ctx->provctx = provctx;
71
302
    return ctx;
72
302
}
73
74
static void krb5kdf_free(void *vctx)
75
302
{
76
302
    KRB5KDF_CTX *ctx = (KRB5KDF_CTX *)vctx;
77
78
302
    if (ctx != NULL) {
79
302
        krb5kdf_reset(ctx);
80
302
        OPENSSL_free(ctx);
81
302
    }
82
302
}
83
84
static void krb5kdf_reset(void *vctx)
85
302
{
86
302
    KRB5KDF_CTX *ctx = (KRB5KDF_CTX *)vctx;
87
302
    void *provctx = ctx->provctx;
88
89
302
    ossl_prov_cipher_reset(&ctx->cipher);
90
302
    OPENSSL_clear_free(ctx->key, ctx->key_len);
91
302
    OPENSSL_clear_free(ctx->constant, ctx->constant_len);
92
302
    memset(ctx, 0, sizeof(*ctx));
93
302
    ctx->provctx = provctx;
94
302
}
95
96
static int krb5kdf_set_membuf(unsigned char **dst, size_t *dst_len,
97
    const OSSL_PARAM *p)
98
570
{
99
570
    OPENSSL_clear_free(*dst, *dst_len);
100
570
    *dst = NULL;
101
570
    *dst_len = 0;
102
570
    return OSSL_PARAM_get_octet_string(p, (void **)dst, 0, dst_len);
103
570
}
104
105
static void *krb5kdf_dup(void *vctx)
106
0
{
107
0
    const KRB5KDF_CTX *src = (const KRB5KDF_CTX *)vctx;
108
0
    KRB5KDF_CTX *dest;
109
110
0
    dest = krb5kdf_new(src->provctx);
111
0
    if (dest != NULL) {
112
0
        if (!ossl_prov_memdup(src->key, src->key_len,
113
0
                &dest->key, &dest->key_len)
114
0
            || !ossl_prov_memdup(src->constant, src->constant_len,
115
0
                &dest->constant, &dest->constant_len)
116
0
            || !ossl_prov_cipher_copy(&dest->cipher, &src->cipher))
117
0
            goto err;
118
0
    }
119
0
    return dest;
120
121
0
err:
122
0
    krb5kdf_free(dest);
123
0
    return NULL;
124
0
}
125
126
static int krb5kdf_derive(void *vctx, unsigned char *key, size_t keylen,
127
    const OSSL_PARAM params[])
128
285
{
129
285
    KRB5KDF_CTX *ctx = (KRB5KDF_CTX *)vctx;
130
285
    const EVP_CIPHER *cipher;
131
132
285
    if (!ossl_prov_is_running() || !krb5kdf_set_ctx_params(ctx, params))
133
0
        return 0;
134
135
285
    cipher = ossl_prov_cipher_cipher(&ctx->cipher);
136
285
    if (cipher == NULL) {
137
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CIPHER);
138
0
        return 0;
139
0
    }
140
285
    if (ctx->key == NULL) {
141
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
142
0
        return 0;
143
0
    }
144
285
    if (ctx->constant == NULL) {
145
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CONSTANT);
146
0
        return 0;
147
0
    }
148
149
285
    return KRB5KDF(cipher, ctx->key, ctx->key_len,
150
285
        ctx->constant, ctx->constant_len,
151
285
        key, keylen);
152
285
}
153
154
static int krb5kdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
155
220
{
156
220
    struct krb5kdf_set_ctx_params_st p;
157
220
    KRB5KDF_CTX *ctx = vctx;
158
220
    OSSL_LIB_CTX *provctx;
159
160
220
    if (ctx == NULL || !krb5kdf_set_ctx_params_decoder(params, &p))
161
0
        return 0;
162
163
220
    provctx = PROV_LIBCTX_OF(ctx->provctx);
164
165
220
    if (!ossl_prov_cipher_load(&ctx->cipher, p.cipher, p.propq, provctx))
166
6
        return 0;
167
168
214
    if (p.key != NULL && !krb5kdf_set_membuf(&ctx->key, &ctx->key_len, p.key))
169
0
        return 0;
170
171
214
    if (p.cnst != NULL
172
107
        && !krb5kdf_set_membuf(&ctx->constant, &ctx->constant_len, p.cnst))
173
0
        return 0;
174
175
214
    return 1;
176
214
}
177
178
static const OSSL_PARAM *krb5kdf_settable_ctx_params(ossl_unused void *ctx,
179
    ossl_unused void *provctx)
180
302
{
181
302
    return krb5kdf_set_ctx_params_list;
182
302
}
183
184
static int krb5kdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
185
0
{
186
0
    struct krb5kdf_get_ctx_params_st p;
187
0
    KRB5KDF_CTX *ctx = (KRB5KDF_CTX *)vctx;
188
189
0
    if (ctx == NULL || !krb5kdf_get_ctx_params_decoder(params, &p))
190
0
        return 0;
191
192
0
    if (p.size != NULL) {
193
0
        const EVP_CIPHER *cipher = ossl_prov_cipher_cipher(&ctx->cipher);
194
0
        size_t len;
195
196
0
        if (cipher != NULL)
197
0
            len = EVP_CIPHER_get_key_length(cipher);
198
0
        else
199
0
            len = EVP_MAX_KEY_LENGTH;
200
201
0
        if (!OSSL_PARAM_set_size_t(p.size, len))
202
0
            return 0;
203
0
    }
204
0
    return 1;
205
0
}
206
207
static const OSSL_PARAM *krb5kdf_gettable_ctx_params(ossl_unused void *ctx,
208
    ossl_unused void *provctx)
209
0
{
210
0
    return krb5kdf_get_ctx_params_list;
211
0
}
212
213
const OSSL_DISPATCH ossl_kdf_krb5kdf_functions[] = {
214
    { OSSL_FUNC_KDF_NEWCTX, (void (*)(void))krb5kdf_new },
215
    { OSSL_FUNC_KDF_DUPCTX, (void (*)(void))krb5kdf_dup },
216
    { OSSL_FUNC_KDF_FREECTX, (void (*)(void))krb5kdf_free },
217
    { OSSL_FUNC_KDF_RESET, (void (*)(void))krb5kdf_reset },
218
    { OSSL_FUNC_KDF_DERIVE, (void (*)(void))krb5kdf_derive },
219
    { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
220
        (void (*)(void))krb5kdf_settable_ctx_params },
221
    { OSSL_FUNC_KDF_SET_CTX_PARAMS,
222
        (void (*)(void))krb5kdf_set_ctx_params },
223
    { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
224
        (void (*)(void))krb5kdf_gettable_ctx_params },
225
    { OSSL_FUNC_KDF_GET_CTX_PARAMS,
226
        (void (*)(void))krb5kdf_get_ctx_params },
227
    OSSL_DISPATCH_END
228
};
229
230
#ifndef OPENSSL_NO_DES
231
/*
232
 * DES3 is a special case, it requires a random-to-key function and its
233
 * input truncated to 21 bytes of the 24 produced by the cipher.
234
 * See RFC3961 6.3.1
235
 */
236
static int fixup_des3_key(unsigned char *key)
237
0
{
238
0
    unsigned char *cblock;
239
0
    int i, j;
240
241
0
    for (i = 2; i >= 0; i--) {
242
0
        cblock = &key[i * 8];
243
0
        memmove(cblock, &key[i * 7], 7);
244
0
        cblock[7] = 0;
245
0
        for (j = 0; j < 7; j++)
246
0
            cblock[7] |= (cblock[j] & 1) << (j + 1);
247
0
        DES_set_odd_parity((DES_cblock *)cblock);
248
0
    }
249
250
    /* fail if keys are such that triple des degrades to single des */
251
0
    if (CRYPTO_memcmp(&key[0], &key[8], 8) == 0 || CRYPTO_memcmp(&key[8], &key[16], 8) == 0) {
252
0
        return 0;
253
0
    }
254
255
0
    return 1;
256
0
}
257
#endif
258
259
/*
260
 * N-fold(K) where blocksize is N, and constant_len is K
261
 * Note: Here |= denotes concatenation
262
 *
263
 * L = lcm(N,K)
264
 * R = L/K
265
 *
266
 * for r: 1 -> R
267
 *   s |= constant rot 13*(r-1))
268
 *
269
 * block = 0
270
 * for k: 1 -> K
271
 *   block += s[N(k-1)..(N-1)k] (ones'-complement addition)
272
 *
273
 * Optimizing for space we compute:
274
 * for each l in L-1 -> 0:
275
 *   s[l] = (constant rot 13*(l/K))[l%k]
276
 *   block[l % N] += s[l] (with carry)
277
 * finally add carry if any
278
 */
279
static void n_fold(unsigned char *block, unsigned int blocksize,
280
    const unsigned char *constant, unsigned int constant_len)
281
131
{
282
131
    unsigned int tmp, gcd, remainder, lcm, carry;
283
131
    int b, l;
284
285
131
    if (constant_len == blocksize) {
286
10
        memcpy(block, constant, constant_len);
287
10
        return;
288
10
    }
289
290
    /* Least Common Multiple of lengths: LCM(a,b)*/
291
121
    gcd = blocksize;
292
121
    remainder = constant_len;
293
    /* Calculate Great Common Divisor first GCD(a,b) */
294
210
    while (remainder != 0) {
295
89
        tmp = gcd % remainder;
296
89
        gcd = remainder;
297
89
        remainder = tmp;
298
89
    }
299
    /* resulting a is the GCD, LCM(a,b) = |a*b|/GCD(a,b) */
300
121
    lcm = blocksize * constant_len / gcd;
301
302
    /* now spread out the bits */
303
121
    memset(block, 0, blocksize);
304
305
    /* last to first to be able to bring carry forward */
306
121
    carry = 0;
307
3.70k
    for (l = lcm - 1; l >= 0; l--) {
308
3.58k
        unsigned int rotbits, rshift, rbyte;
309
310
        /* destination byte in block is l % N */
311
3.58k
        b = l % blocksize;
312
        /* Our virtual s buffer is R = L/K long (K = constant_len) */
313
        /* So we rotate backwards from R-1 to 0 (none) rotations */
314
3.58k
        rotbits = 13 * (l / constant_len);
315
        /* find the byte on s where rotbits falls onto */
316
3.58k
        rbyte = l - (rotbits / 8);
317
        /* calculate how much shift on that byte */
318
3.58k
        rshift = rotbits & 0x07;
319
        /* rbyte % constant_len gives us the unrotated byte in the
320
         * constant buffer, get also the previous byte then
321
         * appropriately shift them to get the rotated byte we need */
322
3.58k
        tmp = (constant[(rbyte - 1) % constant_len] << (8 - rshift)
323
3.58k
                  | constant[rbyte % constant_len] >> rshift)
324
3.58k
            & 0xff;
325
        /* add with carry to any value placed by previous passes */
326
3.58k
        tmp += carry + block[b];
327
3.58k
        block[b] = tmp & 0xff;
328
        /* save any carry that may be left */
329
3.58k
        carry = tmp >> 8;
330
3.58k
    }
331
332
    /* if any carry is left at the end, add it through the number */
333
160
    for (b = blocksize - 1; b >= 0 && carry != 0; b--) {
334
39
        carry += block[b];
335
39
        block[b] = carry & 0xff;
336
39
        carry >>= 8;
337
39
    }
338
121
}
339
340
static int cipher_init(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
341
    const unsigned char *key, size_t key_len)
342
1.40k
{
343
1.40k
    int klen, ret;
344
345
1.40k
    ret = EVP_EncryptInit_ex(ctx, cipher, NULL, NULL, NULL);
346
1.40k
    if (!ret)
347
0
        goto out;
348
    /* set the key len for the odd variable key len cipher */
349
1.40k
    klen = EVP_CIPHER_CTX_get_key_length(ctx);
350
1.40k
    if (key_len != (size_t)klen) {
351
54
        ret = EVP_CIPHER_CTX_set_key_length(ctx, (int)key_len);
352
54
        if (ret <= 0) {
353
54
            ret = 0;
354
54
            goto out;
355
54
        }
356
54
    }
357
1.35k
    ret = EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL);
358
1.35k
    if (!ret)
359
2
        goto out;
360
    /* we never want padding, either the length requested is a multiple of
361
     * the cipher block size or we are passed a cipher that can cope with
362
     * partial blocks via techniques like cipher text stealing */
363
1.34k
    ret = EVP_CIPHER_CTX_set_padding(ctx, 0);
364
1.34k
    if (!ret)
365
0
        goto out;
366
367
1.40k
out:
368
1.40k
    return ret;
369
1.34k
}
370
371
static int KRB5KDF(const EVP_CIPHER *cipher,
372
    const unsigned char *key, size_t key_len,
373
    const unsigned char *constant, size_t constant_len,
374
    unsigned char *okey, size_t okey_len)
375
285
{
376
285
    EVP_CIPHER_CTX *ctx = NULL;
377
285
    unsigned char block[EVP_MAX_BLOCK_LENGTH * 2];
378
285
    unsigned char *plainblock, *cipherblock;
379
285
    size_t blocksize;
380
285
    size_t cipherlen;
381
285
    size_t osize;
382
285
#ifndef OPENSSL_NO_DES
383
285
    int des3_no_fixup = 0;
384
285
#endif
385
285
    int ret;
386
387
285
    if (key_len != okey_len) {
388
69
#ifndef OPENSSL_NO_DES
389
        /* special case for 3des, where the caller may be requesting
390
         * the random raw key, instead of the fixed up key  */
391
69
        if (EVP_CIPHER_get_nid(cipher) == NID_des_ede3_cbc && key_len == 24 && okey_len == 21) {
392
0
            des3_no_fixup = 1;
393
69
        } else {
394
69
#endif
395
69
            ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_OUTPUT_BUFFER_SIZE);
396
69
            return 0;
397
69
#ifndef OPENSSL_NO_DES
398
69
        }
399
69
#endif
400
69
    }
401
402
216
    ctx = EVP_CIPHER_CTX_new();
403
216
    if (ctx == NULL)
404
0
        return 0;
405
406
216
    ret = cipher_init(ctx, cipher, key, key_len);
407
216
    if (!ret)
408
56
        goto out;
409
410
    /* Initialize input block */
411
160
    blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
412
413
160
    if (blocksize == 0) {
414
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CIPHER);
415
0
        ret = 0;
416
0
        goto out;
417
0
    }
418
419
160
    if (constant_len > blocksize) {
420
29
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CONSTANT_LENGTH);
421
29
        ret = 0;
422
29
        goto out;
423
29
    }
424
425
131
    n_fold(block, (unsigned int)blocksize, constant, (unsigned int)constant_len);
426
131
    plainblock = block;
427
131
    cipherblock = block + EVP_MAX_BLOCK_LENGTH;
428
429
1.39k
    for (osize = 0; osize < okey_len; osize += cipherlen) {
430
1.31k
        int olen;
431
432
1.31k
        ret = EVP_EncryptUpdate(ctx, cipherblock, &olen,
433
1.31k
            plainblock, (int)blocksize);
434
1.31k
        if (!ret)
435
53
            goto out;
436
1.26k
        cipherlen = olen;
437
1.26k
        ret = EVP_EncryptFinal_ex(ctx, cipherblock, &olen);
438
1.26k
        if (!ret)
439
0
            goto out;
440
1.26k
        if (olen != 0) {
441
0
            ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_FINAL_BLOCK_LENGTH);
442
0
            ret = 0;
443
0
            goto out;
444
0
        }
445
446
        /* write cipherblock out */
447
1.26k
        if (cipherlen > okey_len - osize)
448
0
            cipherlen = okey_len - osize;
449
1.26k
        memcpy(okey + osize, cipherblock, cipherlen);
450
451
1.26k
        if (okey_len > osize + cipherlen) {
452
            /* we need to reinitialize cipher context per spec */
453
1.18k
            ret = EVP_CIPHER_CTX_reset(ctx);
454
1.18k
            if (!ret)
455
0
                goto out;
456
1.18k
            ret = cipher_init(ctx, cipher, key, key_len);
457
1.18k
            if (!ret)
458
0
                goto out;
459
460
            /* also swap block offsets so last ciphertext becomes new
461
             * plaintext */
462
1.18k
            plainblock = cipherblock;
463
1.18k
            if (cipherblock == block) {
464
555
                cipherblock += EVP_MAX_BLOCK_LENGTH;
465
633
            } else {
466
633
                cipherblock = block;
467
633
            }
468
1.18k
        }
469
1.26k
    }
470
471
78
#ifndef OPENSSL_NO_DES
472
78
    if (EVP_CIPHER_get_nid(cipher) == NID_des_ede3_cbc && !des3_no_fixup) {
473
0
        ret = fixup_des3_key(okey);
474
0
        if (!ret) {
475
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY);
476
0
            goto out;
477
0
        }
478
0
    }
479
78
#endif
480
481
78
    ret = 1;
482
483
216
out:
484
216
    EVP_CIPHER_CTX_free(ctx);
485
216
    OPENSSL_cleanse(block, EVP_MAX_BLOCK_LENGTH * 2);
486
216
    return ret;
487
78
}