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_des.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_des.h"
20
#include "prov/implementations.h"
21
#include "prov/providercommon.h"
22
23
struct des_get_ctx_param_list_st {
24
    struct ossl_cipher_get_ctx_param_list_st common;
25
    OSSL_PARAM *rand;
26
};
27
28
#define des_get_ctx_params_st des_get_ctx_param_list_st
29
30
#include "providers/implementations/ciphers/cipher_des.inc"
31
32
#define DES_FLAGS PROV_CIPHER_FLAG_RAND_KEY
33
34
static OSSL_FUNC_cipher_freectx_fn des_freectx;
35
static OSSL_FUNC_cipher_encrypt_init_fn des_einit;
36
static OSSL_FUNC_cipher_decrypt_init_fn des_dinit;
37
static OSSL_FUNC_cipher_get_ctx_params_fn des_get_ctx_params;
38
static OSSL_FUNC_cipher_gettable_ctx_params_fn des_gettable_ctx_params;
39
40
static void *des_newctx(void *provctx, size_t kbits, size_t blkbits,
41
    size_t ivbits, unsigned int mode, uint64_t flags,
42
    const PROV_CIPHER_HW *hw)
43
0
{
44
0
    PROV_DES_CTX *ctx;
45
46
0
    if (!ossl_prov_is_running())
47
0
        return NULL;
48
49
0
    ctx = OPENSSL_zalloc(sizeof(*ctx));
50
0
    if (ctx != NULL)
51
0
        ossl_cipher_generic_initkey(ctx, kbits, blkbits, ivbits, mode, flags,
52
0
            hw, provctx);
53
0
    return ctx;
54
0
}
55
56
static void *des_dupctx(void *ctx)
57
0
{
58
0
    PROV_DES_CTX *in = (PROV_DES_CTX *)ctx;
59
0
    PROV_DES_CTX *ret;
60
61
0
    if (!ossl_prov_is_running())
62
0
        return NULL;
63
64
0
    ret = OPENSSL_malloc(sizeof(*ret));
65
0
    if (ret == NULL)
66
0
        return NULL;
67
0
    in->base.hw->copyctx(&ret->base, &in->base);
68
69
0
    return ret;
70
0
}
71
72
static void des_freectx(void *vctx)
73
0
{
74
0
    PROV_DES_CTX *ctx = (PROV_DES_CTX *)vctx;
75
76
0
    ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
77
0
    OPENSSL_clear_free(ctx, sizeof(*ctx));
78
0
}
79
80
static int des_init(void *vctx, const unsigned char *key, size_t keylen,
81
    const unsigned char *iv, size_t ivlen,
82
    const OSSL_PARAM params[], int enc)
83
0
{
84
0
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
85
86
0
    if (!ossl_prov_is_running())
87
0
        return 0;
88
89
0
    ctx->num = 0;
90
0
    ctx->bufsz = 0;
91
0
    ctx->enc = enc;
92
93
0
    if (iv != NULL) {
94
0
        if (!ossl_cipher_generic_initiv(ctx, iv, ivlen))
95
0
            return 0;
96
0
    } else if (ctx->iv_set) {
97
        /* reset IV to keep compatibility with 1.1.1 */
98
0
        memcpy(ctx->iv, ctx->oiv, ctx->ivlen);
99
0
    }
100
101
0
    if (key != NULL) {
102
0
        if (keylen != ctx->keylen) {
103
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
104
0
            return 0;
105
0
        }
106
0
        if (!ctx->hw->init(ctx, key, keylen))
107
0
            return 0;
108
0
        ctx->key_set = 1;
109
0
    }
110
0
    return ossl_cipher_generic_set_ctx_params(ctx, params);
111
0
}
112
113
static int des_einit(void *vctx, const unsigned char *key, size_t keylen,
114
    const unsigned char *iv, size_t ivlen,
115
    const OSSL_PARAM params[])
116
0
{
117
0
    return des_init(vctx, key, keylen, iv, ivlen, params, 1);
118
0
}
119
120
static int des_dinit(void *vctx, const unsigned char *key, size_t keylen,
121
    const unsigned char *iv, size_t ivlen,
122
    const OSSL_PARAM params[])
123
0
{
124
0
    return des_init(vctx, key, keylen, iv, ivlen, params, 0);
125
0
}
126
127
static int des_generatekey(PROV_CIPHER_CTX *ctx, void *ptr)
128
0
{
129
0
    DES_cblock *deskey = ptr;
130
0
    size_t kl = ctx->keylen;
131
132
0
    if (kl == 0 || RAND_priv_bytes_ex(ctx->libctx, ptr, kl, 0) <= 0)
133
0
        return 0;
134
0
    DES_set_odd_parity(deskey);
135
0
    return 1;
136
0
}
137
138
static const OSSL_PARAM *des_gettable_ctx_params(ossl_unused void *cctx,
139
    ossl_unused void *provctx)
140
6
{
141
6
    return des_get_ctx_params_list;
142
6
}
143
144
static int des_get_ctx_params(void *vctx, OSSL_PARAM params[])
145
0
{
146
0
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
147
0
    struct des_get_ctx_param_list_st p;
148
149
0
    if (ctx == NULL || !des_get_ctx_params_decoder(params, &p))
150
0
        return 0;
151
152
0
    if (!ossl_cipher_common_get_ctx_params(ctx, &p.common))
153
0
        return 0;
154
155
0
    if (p.rand != NULL && !des_generatekey(ctx, p.rand->data)) {
156
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY);
157
0
        return 0;
158
0
    }
159
0
    return 1;
160
0
}
161
162
#define IMPLEMENT_des_cipher(type, lcmode, UCMODE, flags,                                \
163
    kbits, blkbits, ivbits, block)                                                       \
164
    static OSSL_FUNC_cipher_newctx_fn type##_##lcmode##_newctx;                          \
165
    static void *des_##lcmode##_newctx(void *provctx)                                    \
166
0
    {                                                                                    \
167
0
        return des_newctx(provctx, kbits, blkbits, ivbits,                               \
168
0
            EVP_CIPH_##UCMODE##_MODE, flags,                                             \
169
0
            ossl_prov_cipher_hw_des_##lcmode());                                         \
170
0
    }                                                                                    \
Unexecuted instantiation: cipher_des.c:des_ecb_newctx
Unexecuted instantiation: cipher_des.c:des_cbc_newctx
Unexecuted instantiation: cipher_des.c:des_ofb64_newctx
Unexecuted instantiation: cipher_des.c:des_cfb64_newctx
Unexecuted instantiation: cipher_des.c:des_cfb1_newctx
Unexecuted instantiation: cipher_des.c:des_cfb8_newctx
171
    static OSSL_FUNC_cipher_get_params_fn des_##lcmode##_get_params;                     \
172
    static int des_##lcmode##_get_params(OSSL_PARAM params[])                            \
173
6
    {                                                                                    \
174
6
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,          \
175
6
            flags, kbits, blkbits, ivbits);                                              \
176
6
    }                                                                                    \
cipher_des.c:des_ecb_get_params
Line
Count
Source
173
1
    {                                                                                    \
174
1
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,          \
175
1
            flags, kbits, blkbits, ivbits);                                              \
176
1
    }                                                                                    \
cipher_des.c:des_cbc_get_params
Line
Count
Source
173
1
    {                                                                                    \
174
1
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,          \
175
1
            flags, kbits, blkbits, ivbits);                                              \
176
1
    }                                                                                    \
cipher_des.c:des_ofb64_get_params
Line
Count
Source
173
1
    {                                                                                    \
174
1
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,          \
175
1
            flags, kbits, blkbits, ivbits);                                              \
176
1
    }                                                                                    \
cipher_des.c:des_cfb64_get_params
Line
Count
Source
173
1
    {                                                                                    \
174
1
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,          \
175
1
            flags, kbits, blkbits, ivbits);                                              \
176
1
    }                                                                                    \
cipher_des.c:des_cfb1_get_params
Line
Count
Source
173
1
    {                                                                                    \
174
1
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,          \
175
1
            flags, kbits, blkbits, ivbits);                                              \
176
1
    }                                                                                    \
cipher_des.c:des_cfb8_get_params
Line
Count
Source
173
1
    {                                                                                    \
174
1
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,          \
175
1
            flags, kbits, blkbits, ivbits);                                              \
176
1
    }                                                                                    \
177
    const OSSL_DISPATCH ossl_##des_##lcmode##_functions[] = {                            \
178
        { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))des_einit },                    \
179
        { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))des_dinit },                    \
180
        { OSSL_FUNC_CIPHER_UPDATE,                                                       \
181
            (void (*)(void))ossl_cipher_generic_##block##_update },                      \
182
        { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))ossl_cipher_generic_##block##_final }, \
183
        { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))ossl_cipher_generic_cipher },         \
184
        { OSSL_FUNC_CIPHER_NEWCTX,                                                       \
185
            (void (*)(void))des_##lcmode##_newctx },                                     \
186
        { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))des_dupctx },                         \
187
        { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))des_freectx },                       \
188
        { OSSL_FUNC_CIPHER_GET_PARAMS,                                                   \
189
            (void (*)(void))des_##lcmode##_get_params },                                 \
190
        { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,                                              \
191
            (void (*)(void))ossl_cipher_generic_gettable_params },                       \
192
        { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, (void (*)(void))des_get_ctx_params },         \
193
        { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,                                          \
194
            (void (*)(void))des_gettable_ctx_params },                                   \
195
        { OSSL_FUNC_CIPHER_SET_CTX_PARAMS,                                               \
196
            (void (*)(void))ossl_cipher_generic_set_ctx_params },                        \
197
        { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,                                          \
198
            (void (*)(void))ossl_cipher_generic_settable_ctx_params },                   \
199
        OSSL_DISPATCH_END                                                                \
200
    }
201
202
/* ossl_des_ecb_functions */
203
IMPLEMENT_des_cipher(des, ecb, ECB, DES_FLAGS, 64, 64, 0, block);
204
/* ossl_des_cbc_functions */
205
IMPLEMENT_des_cipher(des, cbc, CBC, DES_FLAGS, 64, 64, 64, block);
206
/* ossl_des_ofb64_functions */
207
IMPLEMENT_des_cipher(des, ofb64, OFB, DES_FLAGS, 64, 8, 64, stream);
208
/* ossl_des_cfb64_functions */
209
IMPLEMENT_des_cipher(des, cfb64, CFB, DES_FLAGS, 64, 8, 64, stream);
210
/* ossl_des_cfb1_functions */
211
IMPLEMENT_des_cipher(des, cfb1, CFB, DES_FLAGS, 64, 8, 64, stream);
212
/* ossl_des_cfb8_functions */
213
IMPLEMENT_des_cipher(des, cfb8, CFB, DES_FLAGS, 64, 8, 64, stream);