Coverage Report

Created: 2025-06-13 06:58

/src/openssl32/providers/implementations/ciphers/cipher_chacha20.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
/* Dispatch functions for chacha20 cipher */
11
12
#include <openssl/proverr.h>
13
#include "cipher_chacha20.h"
14
#include "prov/implementations.h"
15
#include "prov/providercommon.h"
16
17
13.8k
#define CHACHA20_KEYLEN (CHACHA_KEY_SIZE)
18
6.53k
#define CHACHA20_BLKLEN (1)
19
7.01k
#define CHACHA20_IVLEN (CHACHA_CTR_SIZE)
20
6.53k
#define CHACHA20_FLAGS (PROV_CIPHER_FLAG_CUSTOM_IV)
21
22
static OSSL_FUNC_cipher_newctx_fn chacha20_newctx;
23
static OSSL_FUNC_cipher_freectx_fn chacha20_freectx;
24
static OSSL_FUNC_cipher_dupctx_fn chacha20_dupctx;
25
static OSSL_FUNC_cipher_get_params_fn chacha20_get_params;
26
static OSSL_FUNC_cipher_get_ctx_params_fn chacha20_get_ctx_params;
27
static OSSL_FUNC_cipher_set_ctx_params_fn chacha20_set_ctx_params;
28
static OSSL_FUNC_cipher_gettable_ctx_params_fn chacha20_gettable_ctx_params;
29
static OSSL_FUNC_cipher_settable_ctx_params_fn chacha20_settable_ctx_params;
30
#define chacha20_cipher ossl_cipher_generic_cipher
31
#define chacha20_update ossl_cipher_generic_stream_update
32
#define chacha20_final ossl_cipher_generic_stream_final
33
#define chacha20_gettable_params ossl_cipher_generic_gettable_params
34
35
void ossl_chacha20_initctx(PROV_CHACHA20_CTX *ctx)
36
6.51k
{
37
6.51k
    ossl_cipher_generic_initkey(ctx, CHACHA20_KEYLEN * 8,
38
6.51k
                                CHACHA20_BLKLEN * 8,
39
6.51k
                                CHACHA20_IVLEN * 8,
40
6.51k
                                0, CHACHA20_FLAGS,
41
6.51k
                                ossl_prov_cipher_hw_chacha20(CHACHA20_KEYLEN * 8),
42
6.51k
                                NULL);
43
6.51k
}
44
45
static void *chacha20_newctx(void *provctx)
46
1.72k
{
47
1.72k
    PROV_CHACHA20_CTX *ctx;
48
49
1.72k
    if (!ossl_prov_is_running())
50
0
        return NULL;
51
52
1.72k
    ctx = OPENSSL_zalloc(sizeof(*ctx));
53
1.72k
    if (ctx != NULL)
54
1.72k
        ossl_chacha20_initctx(ctx);
55
1.72k
    return ctx;
56
1.72k
}
57
58
static void chacha20_freectx(void *vctx)
59
1.72k
{
60
1.72k
    PROV_CHACHA20_CTX *ctx = (PROV_CHACHA20_CTX *)vctx;
61
62
1.72k
    if (ctx != NULL) {
63
1.72k
        ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
64
1.72k
        OPENSSL_clear_free(ctx, sizeof(*ctx));
65
1.72k
    }
66
1.72k
}
67
68
static void *chacha20_dupctx(void *vctx)
69
0
{
70
0
    PROV_CHACHA20_CTX *ctx = (PROV_CHACHA20_CTX *)vctx;
71
0
    PROV_CHACHA20_CTX *dupctx = NULL;
72
73
0
    if (ctx != NULL) {
74
0
        dupctx = OPENSSL_memdup(ctx, sizeof(*dupctx));
75
0
        if (dupctx != NULL && dupctx->base.tlsmac != NULL && dupctx->base.alloced) {
76
0
            dupctx->base.tlsmac = OPENSSL_memdup(dupctx->base.tlsmac,
77
0
                                                 dupctx->base.tlsmacsize);
78
0
            if (dupctx->base.tlsmac == NULL) {
79
0
                OPENSSL_free(dupctx);
80
0
                dupctx = NULL;
81
0
            }
82
0
        }
83
0
    }
84
0
    return dupctx;
85
0
}
86
87
static int chacha20_get_params(OSSL_PARAM params[])
88
25
{
89
25
    return ossl_cipher_generic_get_params(params, 0, CHACHA20_FLAGS,
90
25
                                          CHACHA20_KEYLEN * 8,
91
25
                                          CHACHA20_BLKLEN * 8,
92
25
                                          CHACHA20_IVLEN * 8);
93
25
}
94
95
static int chacha20_get_ctx_params(void *vctx, OSSL_PARAM params[])
96
1.25k
{
97
1.25k
    OSSL_PARAM *p;
98
99
1.25k
    p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
100
1.25k
    if (p != NULL && !OSSL_PARAM_set_size_t(p, CHACHA20_IVLEN)) {
101
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
102
0
        return 0;
103
0
    }
104
1.25k
    p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
105
1.25k
    if (p != NULL && !OSSL_PARAM_set_size_t(p, CHACHA20_KEYLEN)) {
106
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
107
0
        return 0;
108
0
    }
109
110
1.25k
    return 1;
111
1.25k
}
112
113
static const OSSL_PARAM chacha20_known_gettable_ctx_params[] = {
114
    OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
115
    OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL),
116
    OSSL_PARAM_END
117
};
118
const OSSL_PARAM *chacha20_gettable_ctx_params(ossl_unused void *cctx,
119
                                               ossl_unused void *provctx)
120
25
{
121
25
    return chacha20_known_gettable_ctx_params;
122
25
}
123
124
static int chacha20_set_ctx_params(void *vctx, const OSSL_PARAM params[])
125
106k
{
126
106k
    const OSSL_PARAM *p;
127
106k
    size_t len;
128
129
106k
    if (params == NULL)
130
106k
        return 1;
131
132
0
    p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
133
0
    if (p != NULL) {
134
0
        if (!OSSL_PARAM_get_size_t(p, &len)) {
135
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
136
0
            return 0;
137
0
        }
138
0
        if (len != CHACHA20_KEYLEN) {
139
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
140
0
            return 0;
141
0
        }
142
0
    }
143
0
    p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_IVLEN);
144
0
    if (p != NULL) {
145
0
        if (!OSSL_PARAM_get_size_t(p, &len)) {
146
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
147
0
            return 0;
148
0
        }
149
0
        if (len != CHACHA20_IVLEN) {
150
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
151
0
            return 0;
152
0
        }
153
0
    }
154
0
    return 1;
155
0
}
156
157
static const OSSL_PARAM chacha20_known_settable_ctx_params[] = {
158
    OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
159
    OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL),
160
    OSSL_PARAM_END
161
};
162
const OSSL_PARAM *chacha20_settable_ctx_params(ossl_unused void *cctx,
163
                                               ossl_unused void *provctx)
164
1
{
165
1
    return chacha20_known_settable_ctx_params;
166
1
}
167
168
int ossl_chacha20_einit(void *vctx, const unsigned char *key, size_t keylen,
169
                        const unsigned char *iv, size_t ivlen,
170
                        const OSSL_PARAM params[])
171
96.7k
{
172
96.7k
    int ret;
173
174
    /* The generic function checks for ossl_prov_is_running() */
175
96.7k
    ret = ossl_cipher_generic_einit(vctx, key, keylen, iv, ivlen, NULL);
176
96.7k
    if (ret && iv != NULL) {
177
93.2k
        PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
178
93.2k
        PROV_CIPHER_HW_CHACHA20 *hw = (PROV_CIPHER_HW_CHACHA20 *)ctx->hw;
179
180
93.2k
        hw->initiv(ctx);
181
93.2k
    }
182
96.7k
    if (ret && !chacha20_set_ctx_params(vctx, params))
183
0
        ret = 0;
184
96.7k
    return ret;
185
96.7k
}
186
187
int ossl_chacha20_dinit(void *vctx, const unsigned char *key, size_t keylen,
188
                        const unsigned char *iv, size_t ivlen,
189
                        const OSSL_PARAM params[])
190
81.4k
{
191
81.4k
    int ret;
192
193
    /* The generic function checks for ossl_prov_is_running() */
194
81.4k
    ret = ossl_cipher_generic_dinit(vctx, key, keylen, iv, ivlen, NULL);
195
81.4k
    if (ret && iv != NULL) {
196
78.4k
        PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
197
78.4k
        PROV_CIPHER_HW_CHACHA20 *hw = (PROV_CIPHER_HW_CHACHA20 *)ctx->hw;
198
199
78.4k
        hw->initiv(ctx);
200
78.4k
    }
201
81.4k
    if (ret && !chacha20_set_ctx_params(vctx, params))
202
0
        ret = 0;
203
81.4k
    return ret;
204
81.4k
}
205
206
/* ossl_chacha20_functions */
207
const OSSL_DISPATCH ossl_chacha20_functions[] = {
208
    { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))chacha20_newctx },
209
    { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))chacha20_freectx },
210
    { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))chacha20_dupctx },
211
    { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))ossl_chacha20_einit },
212
    { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))ossl_chacha20_dinit },
213
    { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))chacha20_update },
214
    { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))chacha20_final },
215
    { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))chacha20_cipher},
216
    { OSSL_FUNC_CIPHER_GET_PARAMS, (void (*)(void))chacha20_get_params },
217
    { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, (void (*)(void))chacha20_gettable_params },
218
    { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, (void (*)(void))chacha20_get_ctx_params },
219
    { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,
220
        (void (*)(void))chacha20_gettable_ctx_params },
221
    { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, (void (*)(void))chacha20_set_ctx_params },
222
    { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,
223
        (void (*)(void))chacha20_settable_ctx_params },
224
    OSSL_DISPATCH_END
225
};
226