Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/providers/implementations/ciphers/cipher_tdes_common.c
Line
Count
Source
1
/*
2
 * Copyright 2019-2024 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
4.26k
{
26
4.26k
    PROV_TDES_CTX *tctx;
27
28
4.26k
    if (!ossl_prov_is_running())
29
0
        return NULL;
30
31
4.26k
    tctx = OPENSSL_zalloc(sizeof(*tctx));
32
4.26k
    if (tctx != NULL) {
33
4.26k
        OSSL_FIPS_IND_INIT(tctx)
34
4.26k
        ossl_cipher_generic_initkey(tctx, kbits, blkbits, ivbits, mode, flags,
35
4.26k
            hw, provctx);
36
4.26k
    }
37
4.26k
    return tctx;
38
4.26k
}
39
40
void *ossl_tdes_dupctx(void *ctx)
41
152
{
42
152
    PROV_TDES_CTX *in = (PROV_TDES_CTX *)ctx;
43
152
    PROV_TDES_CTX *ret;
44
45
152
    if (!ossl_prov_is_running())
46
0
        return NULL;
47
48
152
    ret = OPENSSL_malloc(sizeof(*ret));
49
152
    if (ret == NULL)
50
0
        return NULL;
51
152
    OSSL_FIPS_IND_COPY(ret, in)
52
152
    in->base.hw->copyctx(&ret->base, &in->base);
53
54
152
    return ret;
55
152
}
56
57
void ossl_tdes_freectx(void *vctx)
58
4.42k
{
59
4.42k
    PROV_TDES_CTX *ctx = (PROV_TDES_CTX *)vctx;
60
61
4.42k
    ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
62
4.42k
    OPENSSL_clear_free(ctx, sizeof(*ctx));
63
4.42k
}
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
2.87k
{
79
2.87k
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
80
81
2.87k
    if (!ossl_prov_is_running())
82
0
        return 0;
83
84
2.87k
    ctx->num = 0;
85
2.87k
    ctx->bufsz = 0;
86
2.87k
    ctx->enc = enc;
87
88
2.87k
    if (iv != NULL) {
89
2.83k
        if (!ossl_cipher_generic_initiv(ctx, iv, ivlen))
90
0
            return 0;
91
2.83k
    } 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
2.87k
    if (key != NULL) {
100
2.74k
        if (keylen != ctx->keylen) {
101
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
102
0
            return 0;
103
0
        }
104
2.74k
        if (!ctx->hw->init(ctx, key, ctx->keylen))
105
0
            return 0;
106
2.74k
        ctx->key_set = 1;
107
2.74k
    }
108
2.87k
    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
2.87k
    return 1;
115
2.87k
}
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
1.73k
{
121
1.73k
    return tdes_init(vctx, key, keylen, iv, ivlen, params, 1);
122
1.73k
}
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
2.69k
{
128
2.69k
    return tdes_init(vctx, key, keylen, iv, ivlen, params, 0);
129
2.69k
}
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
88.7k
{
154
88.7k
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
155
88.7k
    OSSL_PARAM *p;
156
157
88.7k
    if (!ossl_cipher_generic_get_ctx_params(vctx, params))
158
0
        return 0;
159
160
88.7k
    p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_RANDOM_KEY);
161
88.7k
    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
88.7k
    if (!OSSL_FIPS_IND_GET_CTX_PARAM((PROV_TDES_CTX *)vctx, params))
166
0
        return 0;
167
88.7k
    return 1;
168
88.7k
}
169
170
CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_START(ossl_tdes)
171
OSSL_PARAM_uint(OSSL_CIPHER_PARAM_PADDING, NULL),
172
    OSSL_PARAM_uint(OSSL_CIPHER_PARAM_NUM, NULL),
173
    OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_CIPHER_PARAM_FIPS_ENCRYPT_CHECK)
174
        CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_END(ossl_tdes)
175
176
            int ossl_tdes_set_ctx_params(void *vctx, const OSSL_PARAM params[])
177
5.54k
{
178
5.54k
    if (!OSSL_FIPS_IND_SET_CTX_PARAM((PROV_TDES_CTX *)vctx,
179
5.54k
            OSSL_FIPS_IND_SETTABLE0, params,
180
5.54k
            OSSL_CIPHER_PARAM_FIPS_ENCRYPT_CHECK))
181
0
        return 0;
182
5.54k
    return ossl_cipher_generic_set_ctx_params(vctx, params);
183
5.54k
}
184
185
int ossl_tdes_get_params(OSSL_PARAM params[], unsigned int md, uint64_t flags,
186
    size_t kbits, size_t blkbits, size_t ivbits)
187
121k
{
188
#ifdef FIPS_MODULE
189
    const int decrypt_only = 1;
190
#else
191
121k
    const int decrypt_only = 0;
192
121k
#endif
193
121k
    OSSL_PARAM *p;
194
195
121k
    p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_DECRYPT_ONLY);
196
121k
    if (p != NULL && !OSSL_PARAM_set_int(p, decrypt_only)) {
197
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
198
0
        return 0;
199
0
    }
200
201
121k
    return ossl_cipher_generic_get_params(params, md, flags,
202
121k
        kbits, blkbits, ivbits);
203
121k
}