Coverage Report

Created: 2025-06-13 06:56

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