Coverage Report

Created: 2024-11-21 07:03

/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 "cipher_chacha20.h"
14
#include "prov/implementations.h"
15
#include "prov/providercommon.h"
16
17
308
#define CHACHA20_KEYLEN (CHACHA_KEY_SIZE)
18
153
#define CHACHA20_BLKLEN (1)
19
153
#define CHACHA20_IVLEN (CHACHA_CTR_SIZE)
20
153
#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
151
{
37
151
    ossl_cipher_generic_initkey(ctx, CHACHA20_KEYLEN * 8,
38
151
                                CHACHA20_BLKLEN * 8,
39
151
                                CHACHA20_IVLEN * 8,
40
151
                                0, CHACHA20_FLAGS,
41
151
                                ossl_prov_cipher_hw_chacha20(CHACHA20_KEYLEN * 8),
42
151
                                NULL);
43
151
}
44
45
static void *chacha20_newctx(void *provctx)
46
6
{
47
6
    PROV_CHACHA20_CTX *ctx;
48
49
6
    if (!ossl_prov_is_running())
50
0
        return NULL;
51
52
6
    ctx = OPENSSL_zalloc(sizeof(*ctx));
53
6
    if (ctx != NULL)
54
6
        ossl_chacha20_initctx(ctx);
55
6
    return ctx;
56
6
}
57
58
static void chacha20_freectx(void *vctx)
59
7
{
60
7
    PROV_CHACHA20_CTX *ctx = (PROV_CHACHA20_CTX *)vctx;
61
62
7
    if (ctx != NULL) {
63
7
        ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
64
7
        OPENSSL_clear_free(ctx, sizeof(*ctx));
65
7
    }
66
7
}
67
68
static void *chacha20_dupctx(void *vctx)
69
1
{
70
1
    PROV_CHACHA20_CTX *ctx = (PROV_CHACHA20_CTX *)vctx;
71
1
    PROV_CHACHA20_CTX *dupctx = NULL;
72
73
1
    if (ctx != NULL) {
74
1
        dupctx = OPENSSL_memdup(ctx, sizeof(*dupctx));
75
1
        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
1
    }
84
1
    return dupctx;
85
1
}
86
87
static int chacha20_get_params(OSSL_PARAM params[])
88
2
{
89
2
    return ossl_cipher_generic_get_params(params, 0, CHACHA20_FLAGS,
90
2
                                          CHACHA20_KEYLEN * 8,
91
2
                                          CHACHA20_BLKLEN * 8,
92
2
                                          CHACHA20_IVLEN * 8);
93
2
}
94
95
static int chacha20_get_ctx_params(void *vctx, OSSL_PARAM params[])
96
2
{
97
2
    OSSL_PARAM *p;
98
99
2
    p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
100
2
    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
2
    p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
105
2
    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
2
    return 1;
111
2
}
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
2
{
121
2
    return chacha20_known_gettable_ctx_params;
122
2
}
123
124
static int chacha20_set_ctx_params(void *vctx, const OSSL_PARAM params[])
125
392
{
126
392
    const OSSL_PARAM *p;
127
392
    size_t len;
128
129
392
    if (ossl_param_is_empty(params))
130
390
        return 1;
131
132
2
    p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
133
2
    if (p != NULL) {
134
2
        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
2
        if (len != CHACHA20_KEYLEN) {
139
2
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
140
2
            return 0;
141
2
        }
142
2
    }
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
2
{
165
2
    return chacha20_known_settable_ctx_params;
166
2
}
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
388
{
172
388
    int ret;
173
174
    /* The generic function checks for ossl_prov_is_running() */
175
388
    ret = ossl_cipher_generic_einit(vctx, key, keylen, iv, ivlen, NULL);
176
388
    if (ret && iv != NULL) {
177
256
        PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
178
256
        PROV_CIPHER_HW_CHACHA20 *hw = (PROV_CIPHER_HW_CHACHA20 *)ctx->hw;
179
180
256
        hw->initiv(ctx);
181
256
    }
182
388
    if (ret && !chacha20_set_ctx_params(vctx, params))
183
0
        ret = 0;
184
388
    return ret;
185
388
}
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
2
{
191
2
    int ret;
192
193
    /* The generic function checks for ossl_prov_is_running() */
194
2
    ret = ossl_cipher_generic_dinit(vctx, key, keylen, iv, ivlen, NULL);
195
2
    if (ret && iv != NULL) {
196
0
        PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
197
0
        PROV_CIPHER_HW_CHACHA20 *hw = (PROV_CIPHER_HW_CHACHA20 *)ctx->hw;
198
199
0
        hw->initiv(ctx);
200
0
    }
201
2
    if (ret && !chacha20_set_ctx_params(vctx, params))
202
0
        ret = 0;
203
2
    return ret;
204
2
}
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