Coverage Report

Created: 2026-09-13 06:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/providers/implementations/ciphers/cipher_tdes_common.c
Line
Count
Source
1
/*
2
 * Copyright 2019-2026 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.
13
 */
14
#include "internal/deprecated.h"
15
16
#include <openssl/rand.h>
17
#include <openssl/proverr.h>
18
#include "prov/ciphercommon.h"
19
#include "cipher_tdes.h"
20
#include "prov/implementations.h"
21
#include "prov/providercommon.h"
22
23
struct tdes_get_param_list_st {
24
    struct ossl_cipher_get_param_list_st common;
25
    OSSL_PARAM *decrypt;
26
};
27
28
struct tdes_get_ctx_param_list_st {
29
    struct ossl_cipher_get_ctx_param_list_st common;
30
    OSSL_PARAM *rand;
31
#ifdef FIPS_MODULE
32
    OSSL_PARAM *ind;
33
#endif
34
};
35
36
struct tdes_set_ctx_param_list_st {
37
    struct ossl_cipher_set_ctx_param_list_st common;
38
#ifdef FIPS_MODULE
39
    OSSL_PARAM *ind;
40
#endif
41
};
42
43
#define tdes_get_params_st tdes_get_param_list_st
44
#define tdes_get_ctx_params_st tdes_get_ctx_param_list_st
45
#define tdes_set_ctx_params_st tdes_set_ctx_param_list_st
46
47
#include "providers/implementations/ciphers/cipher_tdes.inc"
48
49
void *ossl_tdes_newctx(void *provctx, int mode, size_t kbits, size_t blkbits,
50
    size_t ivbits, uint64_t flags, const PROV_CIPHER_HW *hw)
51
0
{
52
0
    PROV_TDES_CTX *tctx;
53
54
0
    CIPHER_PROV_CHECK(provctx, DES_EDE3_ECB);
55
56
0
    tctx = OPENSSL_zalloc(sizeof(*tctx));
57
0
    if (tctx != NULL) {
58
0
        OSSL_FIPS_IND_INIT(tctx)
59
0
        ossl_cipher_generic_initkey(tctx, kbits, blkbits, ivbits, mode, flags,
60
0
            hw, provctx);
61
0
    }
62
0
    return tctx;
63
0
}
64
65
void *ossl_tdes_dupctx(void *ctx)
66
0
{
67
0
    PROV_TDES_CTX *in = (PROV_TDES_CTX *)ctx;
68
0
    PROV_TDES_CTX *ret;
69
70
0
    if (!ossl_prov_is_running())
71
0
        return NULL;
72
73
0
    ret = OPENSSL_malloc(sizeof(*ret));
74
0
    if (ret == NULL)
75
0
        return NULL;
76
0
    OSSL_FIPS_IND_COPY(ret, in)
77
0
    in->base.hw->copyctx(&ret->base, &in->base);
78
79
0
    return ret;
80
0
}
81
82
void ossl_tdes_freectx(void *vctx)
83
0
{
84
0
    PROV_TDES_CTX *ctx = (PROV_TDES_CTX *)vctx;
85
86
0
    ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
87
0
    OPENSSL_clear_free(ctx, sizeof(*ctx));
88
0
}
89
90
#ifdef FIPS_MODULE
91
static int tdes_encrypt_check_approved(PROV_TDES_CTX *ctx, int enc)
92
{
93
    /* Triple-DES encryption is not approved in FIPS 140-3 */
94
    if (enc
95
        && !OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0,
96
            ctx->base.libctx, "Triple-DES", "Encryption",
97
            FIPS_CONFIG_TDES_ENCRYPT_DISABLED))
98
        return 0;
99
    return 1;
100
}
101
#endif
102
103
static int tdes_init(void *vctx, const unsigned char *key, size_t keylen,
104
    const unsigned char *iv, size_t ivlen,
105
    const OSSL_PARAM params[], int enc)
106
0
{
107
0
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
108
109
0
    if (!ossl_prov_is_running())
110
0
        return 0;
111
112
0
    ctx->num = 0;
113
0
    ctx->bufsz = 0;
114
0
    ctx->enc = enc;
115
116
0
    if (iv != NULL) {
117
0
        if (!ossl_cipher_generic_initiv(ctx, iv, ivlen))
118
0
            return 0;
119
0
    } else if (ctx->iv_set
120
0
        && (ctx->mode == EVP_CIPH_CBC_MODE
121
0
            || ctx->mode == EVP_CIPH_CFB_MODE
122
0
            || ctx->mode == EVP_CIPH_OFB_MODE)) {
123
        /* reset IV to keep compatibility with 1.1.1 */
124
0
        memcpy(ctx->iv, ctx->oiv, ctx->ivlen);
125
0
    }
126
127
0
    if (key != NULL) {
128
0
        if (keylen != ctx->keylen) {
129
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
130
0
            return 0;
131
0
        }
132
0
        if (!ctx->hw->init(ctx, key, ctx->keylen))
133
0
            return 0;
134
0
        ctx->key_set = 1;
135
0
    }
136
0
    if (!ossl_tdes_set_ctx_params(ctx, params))
137
0
        return 0;
138
#ifdef FIPS_MODULE
139
    if (!tdes_encrypt_check_approved((PROV_TDES_CTX *)ctx, enc))
140
        return 0;
141
#endif
142
0
    return 1;
143
0
}
144
145
int ossl_tdes_einit(void *vctx, const unsigned char *key, size_t keylen,
146
    const unsigned char *iv, size_t ivlen,
147
    const OSSL_PARAM params[])
148
0
{
149
0
    return tdes_init(vctx, key, keylen, iv, ivlen, params, 1);
150
0
}
151
152
int ossl_tdes_dinit(void *vctx, const unsigned char *key, size_t keylen,
153
    const unsigned char *iv, size_t ivlen,
154
    const OSSL_PARAM params[])
155
0
{
156
0
    return tdes_init(vctx, key, keylen, iv, ivlen, params, 0);
157
0
}
158
159
static int tdes_generatekey(PROV_CIPHER_CTX *ctx, void *ptr)
160
0
{
161
0
    DES_cblock *deskey = ptr;
162
0
    size_t kl = ctx->keylen;
163
164
0
    if (kl == 0 || RAND_priv_bytes_ex(ctx->libctx, ptr, kl, 0) <= 0)
165
0
        return 0;
166
0
    DES_set_odd_parity(deskey);
167
0
    if (kl >= 16) {
168
0
        DES_set_odd_parity(deskey + 1);
169
0
        if (kl >= 24)
170
0
            DES_set_odd_parity(deskey + 2);
171
0
    }
172
0
    return 1;
173
0
}
174
175
const OSSL_PARAM *ossl_tdes_gettable_ctx_params(ossl_unused void *cctx,
176
    ossl_unused void *provctx)
177
12
{
178
12
    return tdes_get_ctx_params_list;
179
12
}
180
181
int ossl_tdes_get_ctx_params(void *vctx, OSSL_PARAM params[])
182
0
{
183
0
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
184
0
    struct tdes_get_ctx_param_list_st p;
185
186
0
    if (ctx == NULL || !tdes_get_ctx_params_decoder(params, &p))
187
0
        return 0;
188
189
0
    if (!ossl_cipher_common_get_ctx_params(ctx, &p.common))
190
0
        return 0;
191
192
0
    if (p.rand != NULL && !tdes_generatekey(ctx, p.rand->data)) {
193
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY);
194
0
        return 0;
195
0
    }
196
0
    if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM((PROV_TDES_CTX *)vctx, p.ind))
197
0
        return 0;
198
0
    return 1;
199
0
}
200
201
const OSSL_PARAM *ossl_tdes_settable_ctx_params(ossl_unused void *cctx,
202
    ossl_unused void *provctx)
203
0
{
204
0
    return tdes_set_ctx_params_list;
205
0
}
206
207
int ossl_tdes_set_ctx_params(void *vctx, const OSSL_PARAM params[])
208
0
{
209
0
    PROV_TDES_CTX *ctx = (PROV_TDES_CTX *)vctx;
210
0
    struct tdes_set_ctx_param_list_st p;
211
212
0
    if (ctx == NULL || !tdes_set_ctx_params_decoder(params, &p))
213
0
        return 0;
214
0
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, p.ind))
215
0
        return 0;
216
217
0
    return ossl_cipher_common_set_ctx_params(&ctx->base, &p.common);
218
0
}
219
220
const OSSL_PARAM *ossl_tdes_gettable_params(ossl_unused void *provctx)
221
0
{
222
0
    return tdes_get_params_list;
223
0
}
224
225
int ossl_tdes_get_params(OSSL_PARAM params[], unsigned int md, uint64_t flags,
226
    size_t kbits, size_t blkbits, size_t ivbits)
227
11
{
228
#ifdef FIPS_MODULE
229
    const int decrypt_only = 1;
230
#else
231
11
    const int decrypt_only = 0;
232
11
#endif
233
11
    struct tdes_get_param_list_st p;
234
235
11
    if (!tdes_get_params_decoder(params, &p))
236
0
        return 0;
237
238
11
    if (p.decrypt != NULL && !OSSL_PARAM_set_int(p.decrypt, decrypt_only)) {
239
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
240
0
        return 0;
241
0
    }
242
243
11
    return ossl_cipher_common_get_params(&p.common, md, flags, kbits, blkbits,
244
11
        ivbits);
245
11
}