Coverage Report

Created: 2025-06-13 06:58

/src/openssl32/providers/implementations/ciphers/cipher_tdes_common.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2019-2023 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
1.91k
{
26
1.91k
    PROV_TDES_CTX *tctx;
27
28
1.91k
    if (!ossl_prov_is_running())
29
0
        return NULL;
30
31
1.91k
    tctx = OPENSSL_zalloc(sizeof(*tctx));
32
1.91k
    if (tctx != NULL)
33
1.91k
        ossl_cipher_generic_initkey(tctx, kbits, blkbits, ivbits, mode, flags,
34
1.91k
                                    hw, provctx);
35
1.91k
    return tctx;
36
1.91k
}
37
38
void *ossl_tdes_dupctx(void *ctx)
39
0
{
40
0
    PROV_TDES_CTX *in = (PROV_TDES_CTX *)ctx;
41
0
    PROV_TDES_CTX *ret;
42
43
0
    if (!ossl_prov_is_running())
44
0
        return NULL;
45
46
0
    ret = OPENSSL_malloc(sizeof(*ret));
47
0
    if (ret == NULL)
48
0
        return NULL;
49
0
    in->base.hw->copyctx(&ret->base, &in->base);
50
51
0
    return ret;
52
0
}
53
54
void ossl_tdes_freectx(void *vctx)
55
1.91k
{
56
1.91k
    PROV_TDES_CTX *ctx = (PROV_TDES_CTX *)vctx;
57
58
1.91k
    ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
59
1.91k
    OPENSSL_clear_free(ctx,  sizeof(*ctx));
60
1.91k
}
61
62
static int tdes_init(void *vctx, const unsigned char *key, size_t keylen,
63
                     const unsigned char *iv, size_t ivlen,
64
                     const OSSL_PARAM params[], int enc)
65
1.35k
{
66
1.35k
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
67
68
1.35k
    if (!ossl_prov_is_running())
69
0
        return 0;
70
71
1.35k
    ctx->num = 0;
72
1.35k
    ctx->bufsz = 0;
73
1.35k
    ctx->enc = enc;
74
75
1.35k
    if (iv != NULL) {
76
1.35k
        if (!ossl_cipher_generic_initiv(ctx, iv, ivlen))
77
0
            return 0;
78
1.35k
    } else if (ctx->iv_set
79
0
               && (ctx->mode == EVP_CIPH_CBC_MODE
80
0
                   || ctx->mode == EVP_CIPH_CFB_MODE
81
0
                   || ctx->mode == EVP_CIPH_OFB_MODE)) {
82
        /* reset IV to keep compatibility with 1.1.1 */
83
0
        memcpy(ctx->iv, ctx->oiv, ctx->ivlen);
84
0
    }
85
86
1.35k
    if (key != NULL) {
87
1.35k
        if (keylen != ctx->keylen) {
88
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
89
0
            return 0;
90
0
        }
91
1.35k
        if (!ctx->hw->init(ctx, key, ctx->keylen))
92
0
            return 0;
93
1.35k
        ctx->key_set = 1;
94
1.35k
    }
95
1.35k
    return ossl_cipher_generic_set_ctx_params(ctx, params);
96
1.35k
}
97
98
int ossl_tdes_einit(void *vctx, const unsigned char *key, size_t keylen,
99
                    const unsigned char *iv, size_t ivlen,
100
                    const OSSL_PARAM params[])
101
647
{
102
647
    return tdes_init(vctx, key, keylen, iv, ivlen, params, 1);
103
647
}
104
105
int ossl_tdes_dinit(void *vctx, const unsigned char *key, size_t keylen,
106
                    const unsigned char *iv, size_t ivlen,
107
                    const OSSL_PARAM params[])
108
1.26k
{
109
1.26k
    return tdes_init(vctx, key, keylen, iv, ivlen, params, 0);
110
1.26k
}
111
112
CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(ossl_tdes)
113
    OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY, NULL, 0),
114
CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(ossl_tdes)
115
116
static int tdes_generatekey(PROV_CIPHER_CTX *ctx, void *ptr)
117
0
{
118
119
0
    DES_cblock *deskey = ptr;
120
0
    size_t kl = ctx->keylen;
121
122
0
    if (kl == 0 || RAND_priv_bytes_ex(ctx->libctx, ptr, kl, 0) <= 0)
123
0
        return 0;
124
0
    DES_set_odd_parity(deskey);
125
0
    if (kl >= 16) {
126
0
        DES_set_odd_parity(deskey + 1);
127
0
        if (kl >= 24)
128
0
            DES_set_odd_parity(deskey + 2);
129
0
    }
130
0
    return 1;
131
0
}
132
133
int ossl_tdes_get_ctx_params(void *vctx, OSSL_PARAM params[])
134
7.93k
{
135
7.93k
    PROV_CIPHER_CTX  *ctx = (PROV_CIPHER_CTX *)vctx;
136
7.93k
    OSSL_PARAM *p;
137
138
7.93k
    if (!ossl_cipher_generic_get_ctx_params(vctx, params))
139
0
        return 0;
140
141
7.93k
    p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_RANDOM_KEY);
142
7.93k
    if (p != NULL && !tdes_generatekey(ctx, p->data)) {
143
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY);
144
0
        return 0;
145
0
    }
146
7.93k
    return 1;
147
7.93k
}