Coverage Report

Created: 2025-12-10 06:24

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-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.
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
void *ossl_tdes_newctx(void *provctx, int mode, size_t kbits, size_t blkbits,
24
    size_t ivbits, uint64_t flags, const PROV_CIPHER_HW *hw)
25
0
{
26
0
    PROV_TDES_CTX *tctx;
27
28
0
    if (!ossl_prov_is_running())
29
0
        return NULL;
30
31
0
    tctx = OPENSSL_zalloc(sizeof(*tctx));
32
0
    if (tctx != NULL) {
33
0
        OSSL_FIPS_IND_INIT(tctx)
34
0
        ossl_cipher_generic_initkey(tctx, kbits, blkbits, ivbits, mode, flags,
35
0
            hw, provctx);
36
0
    }
37
0
    return tctx;
38
0
}
39
40
void *ossl_tdes_dupctx(void *ctx)
41
0
{
42
0
    PROV_TDES_CTX *in = (PROV_TDES_CTX *)ctx;
43
0
    PROV_TDES_CTX *ret;
44
45
0
    if (!ossl_prov_is_running())
46
0
        return NULL;
47
48
0
    ret = OPENSSL_malloc(sizeof(*ret));
49
0
    if (ret == NULL)
50
0
        return NULL;
51
0
    OSSL_FIPS_IND_COPY(ret, in)
52
0
    in->base.hw->copyctx(&ret->base, &in->base);
53
54
0
    return ret;
55
0
}
56
57
void ossl_tdes_freectx(void *vctx)
58
0
{
59
0
    PROV_TDES_CTX *ctx = (PROV_TDES_CTX *)vctx;
60
61
0
    ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
62
0
    OPENSSL_clear_free(ctx, sizeof(*ctx));
63
0
}
64
65
#ifdef FIPS_MODULE
66
static int tdes_encrypt_check_approved(PROV_TDES_CTX *ctx, int enc)
67
{
68
    /* Triple-DES encryption is not approved in FIPS 140-3 */
69
    if (enc && !OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0, ctx->base.libctx, "Triple-DES", "Encryption", ossl_fips_config_tdes_encrypt_disallowed))
70
        return 0;
71
    return 1;
72
}
73
#endif
74
75
static int tdes_init(void *vctx, const unsigned char *key, size_t keylen,
76
    const unsigned char *iv, size_t ivlen,
77
    const OSSL_PARAM params[], int enc)
78
0
{
79
0
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
80
81
0
    if (!ossl_prov_is_running())
82
0
        return 0;
83
84
0
    ctx->num = 0;
85
0
    ctx->bufsz = 0;
86
0
    ctx->enc = enc;
87
88
0
    if (iv != NULL) {
89
0
        if (!ossl_cipher_generic_initiv(ctx, iv, ivlen))
90
0
            return 0;
91
0
    } else if (ctx->iv_set
92
0
        && (ctx->mode == EVP_CIPH_CBC_MODE
93
0
            || ctx->mode == EVP_CIPH_CFB_MODE
94
0
            || ctx->mode == EVP_CIPH_OFB_MODE)) {
95
        /* reset IV to keep compatibility with 1.1.1 */
96
0
        memcpy(ctx->iv, ctx->oiv, ctx->ivlen);
97
0
    }
98
99
0
    if (key != NULL) {
100
0
        if (keylen != ctx->keylen) {
101
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
102
0
            return 0;
103
0
        }
104
0
        if (!ctx->hw->init(ctx, key, ctx->keylen))
105
0
            return 0;
106
0
        ctx->key_set = 1;
107
0
    }
108
0
    if (!ossl_tdes_set_ctx_params(ctx, params))
109
0
        return 0;
110
#ifdef FIPS_MODULE
111
    if (!tdes_encrypt_check_approved((PROV_TDES_CTX *)ctx, enc))
112
        return 0;
113
#endif
114
0
    return 1;
115
0
}
116
117
int ossl_tdes_einit(void *vctx, const unsigned char *key, size_t keylen,
118
    const unsigned char *iv, size_t ivlen,
119
    const OSSL_PARAM params[])
120
0
{
121
0
    return tdes_init(vctx, key, keylen, iv, ivlen, params, 1);
122
0
}
123
124
int ossl_tdes_dinit(void *vctx, const unsigned char *key, size_t keylen,
125
    const unsigned char *iv, size_t ivlen,
126
    const OSSL_PARAM params[])
127
0
{
128
0
    return tdes_init(vctx, key, keylen, iv, ivlen, params, 0);
129
0
}
130
131
CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(ossl_tdes)
132
OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY, NULL, 0),
133
    OSSL_FIPS_IND_GETTABLE_CTX_PARAM()
134
        CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(ossl_tdes)
135
136
            static int tdes_generatekey(PROV_CIPHER_CTX *ctx, void *ptr)
137
0
{
138
0
    DES_cblock *deskey = ptr;
139
0
    size_t kl = ctx->keylen;
140
141
0
    if (kl == 0 || RAND_priv_bytes_ex(ctx->libctx, ptr, kl, 0) <= 0)
142
0
        return 0;
143
0
    DES_set_odd_parity(deskey);
144
0
    if (kl >= 16) {
145
0
        DES_set_odd_parity(deskey + 1);
146
0
        if (kl >= 24)
147
0
            DES_set_odd_parity(deskey + 2);
148
0
    }
149
0
    return 1;
150
0
}
151
152
int ossl_tdes_get_ctx_params(void *vctx, OSSL_PARAM params[])
153
0
{
154
0
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
155
0
    OSSL_PARAM *p;
156
157
0
    if (!ossl_cipher_generic_get_ctx_params(vctx, params))
158
0
        return 0;
159
160
0
    p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_RANDOM_KEY);
161
0
    if (p != NULL && !tdes_generatekey(ctx, p->data)) {
162
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY);
163
0
        return 0;
164
0
    }
165
0
    if (!OSSL_FIPS_IND_GET_CTX_PARAM((PROV_TDES_CTX *)vctx, params))
166
0
        return 0;
167
0
    return 1;
168
0
}
169
170
CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_START(ossl_tdes)
171
OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_CIPHER_PARAM_FIPS_ENCRYPT_CHECK)
172
CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_END(ossl_tdes)
173
174
int ossl_tdes_set_ctx_params(void *vctx, const OSSL_PARAM params[])
175
0
{
176
0
    if (!OSSL_FIPS_IND_SET_CTX_PARAM((PROV_TDES_CTX *)vctx,
177
0
            OSSL_FIPS_IND_SETTABLE0, params,
178
0
            OSSL_CIPHER_PARAM_FIPS_ENCRYPT_CHECK))
179
0
        return 0;
180
0
    return ossl_cipher_generic_set_ctx_params(vctx, params);
181
0
}
182
183
int ossl_tdes_get_params(OSSL_PARAM params[], unsigned int md, uint64_t flags,
184
    size_t kbits, size_t blkbits, size_t ivbits)
185
11
{
186
#ifdef FIPS_MODULE
187
    const int decrypt_only = 1;
188
#else
189
11
    const int decrypt_only = 0;
190
11
#endif
191
11
    OSSL_PARAM *p;
192
193
11
    p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_DECRYPT_ONLY);
194
11
    if (p != NULL && !OSSL_PARAM_set_int(p, decrypt_only)) {
195
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
196
0
        return 0;
197
0
    }
198
199
11
    return ossl_cipher_generic_get_params(params, md, flags,
200
11
        kbits, blkbits, ivbits);
201
11
}