Coverage Report

Created: 2025-11-16 06:40

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