Coverage Report

Created: 2025-12-10 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/providers/implementations/ciphers/cipher_chacha20_poly1305.c
Line
Count
Source
1
/*
2
 * Copyright 2019-2025 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_poly1305 cipher */
11
12
#include <string.h>
13
#include <openssl/proverr.h>
14
#include "cipher_chacha20_poly1305.h"
15
#include "prov/implementations.h"
16
#include "prov/providercommon.h"
17
#include "providers/implementations/ciphers/cipher_chacha20_poly1305.inc"
18
19
1
#define CHACHA20_POLY1305_KEYLEN CHACHA_KEY_SIZE
20
1
#define CHACHA20_POLY1305_BLKLEN 1
21
0
#define CHACHA20_POLY1305_MAX_IVLEN 12
22
0
#define CHACHA20_POLY1305_MODE 0
23
1
#define CHACHA20_POLY1305_FLAGS (PROV_CIPHER_FLAG_AEAD \
24
1
    | PROV_CIPHER_FLAG_CUSTOM_IV)
25
26
static OSSL_FUNC_cipher_newctx_fn chacha20_poly1305_newctx;
27
static OSSL_FUNC_cipher_freectx_fn chacha20_poly1305_freectx;
28
static OSSL_FUNC_cipher_dupctx_fn chacha20_poly1305_dupctx;
29
static OSSL_FUNC_cipher_encrypt_init_fn chacha20_poly1305_einit;
30
static OSSL_FUNC_cipher_decrypt_init_fn chacha20_poly1305_dinit;
31
static OSSL_FUNC_cipher_get_params_fn chacha20_poly1305_get_params;
32
static OSSL_FUNC_cipher_get_ctx_params_fn chacha20_poly1305_get_ctx_params;
33
static OSSL_FUNC_cipher_set_ctx_params_fn chacha20_poly1305_set_ctx_params;
34
static OSSL_FUNC_cipher_cipher_fn chacha20_poly1305_cipher;
35
static OSSL_FUNC_cipher_final_fn chacha20_poly1305_final;
36
static OSSL_FUNC_cipher_gettable_ctx_params_fn chacha20_poly1305_gettable_ctx_params;
37
static OSSL_FUNC_cipher_settable_ctx_params_fn chacha20_poly1305_settable_ctx_params;
38
#define chacha20_poly1305_gettable_params ossl_cipher_generic_gettable_params
39
#define chacha20_poly1305_update chacha20_poly1305_cipher
40
41
static void *chacha20_poly1305_newctx(void *provctx)
42
0
{
43
0
    PROV_CHACHA20_POLY1305_CTX *ctx;
44
45
0
    if (!ossl_prov_is_running())
46
0
        return NULL;
47
48
0
    ctx = OPENSSL_zalloc(sizeof(*ctx));
49
0
    if (ctx != NULL) {
50
0
        ossl_cipher_generic_initkey(&ctx->base, CHACHA20_POLY1305_KEYLEN * 8,
51
0
            CHACHA20_POLY1305_BLKLEN * 8,
52
0
            CHACHA20_POLY1305_IVLEN * 8,
53
0
            CHACHA20_POLY1305_MODE,
54
0
            CHACHA20_POLY1305_FLAGS,
55
0
            ossl_prov_cipher_hw_chacha20_poly1305(
56
0
                CHACHA20_POLY1305_KEYLEN * 8),
57
0
            NULL);
58
0
        ctx->tls_payload_length = NO_TLS_PAYLOAD_LENGTH;
59
0
        ossl_chacha20_initctx(&ctx->chacha);
60
0
    }
61
0
    return ctx;
62
0
}
63
64
static void *chacha20_poly1305_dupctx(void *provctx)
65
0
{
66
0
    PROV_CHACHA20_POLY1305_CTX *ctx = provctx;
67
0
    PROV_CHACHA20_POLY1305_CTX *dctx = NULL;
68
69
0
    if (ctx == NULL)
70
0
        return NULL;
71
0
    dctx = OPENSSL_memdup(ctx, sizeof(*ctx));
72
0
    if (dctx != NULL && dctx->base.tlsmac != NULL && dctx->base.alloced) {
73
0
        dctx->base.tlsmac = OPENSSL_memdup(dctx->base.tlsmac,
74
0
            dctx->base.tlsmacsize);
75
0
        if (dctx->base.tlsmac == NULL) {
76
0
            OPENSSL_free(dctx);
77
0
            dctx = NULL;
78
0
        }
79
0
    }
80
0
    return dctx;
81
0
}
82
83
static void chacha20_poly1305_freectx(void *vctx)
84
0
{
85
0
    PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx;
86
87
0
    if (ctx != NULL) {
88
0
        ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
89
0
        OPENSSL_clear_free(ctx, sizeof(*ctx));
90
0
    }
91
0
}
92
93
static int chacha20_poly1305_get_params(OSSL_PARAM params[])
94
1
{
95
1
    return ossl_cipher_generic_get_params(params, 0, CHACHA20_POLY1305_FLAGS,
96
1
        CHACHA20_POLY1305_KEYLEN * 8,
97
1
        CHACHA20_POLY1305_BLKLEN * 8,
98
1
        CHACHA20_POLY1305_IVLEN * 8);
99
1
}
100
101
static int chacha20_poly1305_get_ctx_params(void *vctx, OSSL_PARAM params[])
102
0
{
103
0
    PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx;
104
0
    struct chacha20_poly1305_get_ctx_params_st p;
105
106
0
    if (ctx == NULL || !chacha20_poly1305_get_ctx_params_decoder(params, &p))
107
0
        return 0;
108
109
0
    if (p.ivlen != NULL
110
0
        && !OSSL_PARAM_set_size_t(p.ivlen, CHACHA20_POLY1305_IVLEN)) {
111
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
112
0
        return 0;
113
0
    }
114
115
0
    if (p.keylen != NULL
116
0
        && !OSSL_PARAM_set_size_t(p.keylen, CHACHA20_POLY1305_KEYLEN)) {
117
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
118
0
        return 0;
119
0
    }
120
121
0
    if (p.taglen != NULL
122
0
        && !OSSL_PARAM_set_size_t(p.taglen, ctx->tag_len)) {
123
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
124
0
        return 0;
125
0
    }
126
127
0
    if (p.pad != NULL
128
0
        && !OSSL_PARAM_set_size_t(p.pad, ctx->tls_aad_pad_sz)) {
129
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
130
0
        return 0;
131
0
    }
132
133
0
    if (p.tag != NULL) {
134
0
        if (p.tag->data_type != OSSL_PARAM_OCTET_STRING) {
135
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
136
0
            return 0;
137
0
        }
138
0
        if (!ctx->base.enc) {
139
0
            ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_SET);
140
0
            return 0;
141
0
        }
142
0
        if (p.tag->data_size == 0 || p.tag->data_size > POLY1305_BLOCK_SIZE) {
143
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG_LENGTH);
144
0
            return 0;
145
0
        }
146
0
        memcpy(p.tag->data, ctx->tag, p.tag->data_size);
147
0
    }
148
0
    return 1;
149
0
}
150
151
static const OSSL_PARAM *chacha20_poly1305_gettable_ctx_params(ossl_unused void *cctx, ossl_unused void *provctx)
152
1
{
153
1
    return chacha20_poly1305_get_ctx_params_list;
154
1
}
155
156
static const OSSL_PARAM *chacha20_poly1305_settable_ctx_params(
157
    ossl_unused void *cctx, ossl_unused void *provctx)
158
0
{
159
0
    return chacha20_poly1305_set_ctx_params_list;
160
0
}
161
162
static int chacha20_poly1305_set_ctx_params(void *vctx,
163
    const OSSL_PARAM params[])
164
0
{
165
0
    size_t len;
166
0
    PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx;
167
0
    PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->base.hw;
168
0
    struct chacha20_poly1305_set_ctx_params_st p;
169
170
0
    if (!chacha20_poly1305_set_ctx_params_decoder(params, &p))
171
0
        return 0;
172
173
0
    if (p.keylen != NULL) {
174
0
        if (!OSSL_PARAM_get_size_t(p.keylen, &len)) {
175
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
176
0
            return 0;
177
0
        }
178
0
        if (len != CHACHA20_POLY1305_KEYLEN) {
179
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
180
0
            return 0;
181
0
        }
182
0
    }
183
184
0
    if (p.ivlen != NULL) {
185
0
        if (!OSSL_PARAM_get_size_t(p.ivlen, &len)) {
186
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
187
0
            return 0;
188
0
        }
189
0
        if (len != CHACHA20_POLY1305_MAX_IVLEN) {
190
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
191
0
            return 0;
192
0
        }
193
0
    }
194
195
0
    if (p.tag != NULL) {
196
0
        if (p.tag->data_type != OSSL_PARAM_OCTET_STRING) {
197
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
198
0
            return 0;
199
0
        }
200
0
        if (p.tag->data_size == 0 || p.tag->data_size > POLY1305_BLOCK_SIZE) {
201
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG_LENGTH);
202
0
            return 0;
203
0
        }
204
0
        if (p.tag->data != NULL) {
205
0
            if (ctx->base.enc) {
206
0
                ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_NEEDED);
207
0
                return 0;
208
0
            }
209
0
            memcpy(ctx->tag, p.tag->data, p.tag->data_size);
210
0
        }
211
0
        ctx->tag_len = p.tag->data_size;
212
0
    }
213
214
0
    if (p.aad != NULL) {
215
0
        if (p.aad->data_type != OSSL_PARAM_OCTET_STRING) {
216
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
217
0
            return 0;
218
0
        }
219
0
        len = hw->tls_init(&ctx->base, p.aad->data, p.aad->data_size);
220
0
        if (len == 0) {
221
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DATA);
222
0
            return 0;
223
0
        }
224
0
        ctx->tls_aad_pad_sz = len;
225
0
    }
226
227
0
    if (p.fixed != NULL) {
228
0
        if (p.fixed->data_type != OSSL_PARAM_OCTET_STRING) {
229
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
230
0
            return 0;
231
0
        }
232
0
        if (hw->tls_iv_set_fixed(&ctx->base, p.fixed->data,
233
0
                p.fixed->data_size)
234
0
            == 0) {
235
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
236
0
            return 0;
237
0
        }
238
0
    }
239
0
    return 1;
240
0
}
241
242
static int chacha20_poly1305_einit(void *vctx, const unsigned char *key,
243
    size_t keylen, const unsigned char *iv,
244
    size_t ivlen, const OSSL_PARAM params[])
245
0
{
246
0
    int ret;
247
248
    /* The generic function checks for ossl_prov_is_running() */
249
0
    ret = ossl_cipher_generic_einit(vctx, key, keylen, iv, ivlen, NULL);
250
0
    if (ret && iv != NULL) {
251
0
        PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
252
0
        PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw;
253
254
0
        hw->initiv(ctx);
255
0
    }
256
0
    if (ret && !chacha20_poly1305_set_ctx_params(vctx, params))
257
0
        ret = 0;
258
0
    return ret;
259
0
}
260
261
static int chacha20_poly1305_dinit(void *vctx, const unsigned char *key,
262
    size_t keylen, const unsigned char *iv,
263
    size_t ivlen, const OSSL_PARAM params[])
264
0
{
265
0
    int ret;
266
267
    /* The generic function checks for ossl_prov_is_running() */
268
0
    ret = ossl_cipher_generic_dinit(vctx, key, keylen, iv, ivlen, NULL);
269
0
    if (ret && iv != NULL) {
270
0
        PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
271
0
        PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw;
272
273
0
        hw->initiv(ctx);
274
0
    }
275
0
    if (ret && !chacha20_poly1305_set_ctx_params(vctx, params))
276
0
        ret = 0;
277
0
    return ret;
278
0
}
279
280
static int chacha20_poly1305_cipher(void *vctx, unsigned char *out,
281
    size_t *outl, size_t outsize,
282
    const unsigned char *in, size_t inl)
283
0
{
284
0
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
285
0
    PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw;
286
287
0
    if (!ossl_prov_is_running())
288
0
        return 0;
289
290
0
    if (inl == 0) {
291
0
        *outl = 0;
292
0
        return 1;
293
0
    }
294
295
0
    if (outsize < inl) {
296
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
297
0
        return 0;
298
0
    }
299
300
0
    if (!hw->aead_cipher(ctx, out, outl, in, inl))
301
0
        return 0;
302
303
0
    return 1;
304
0
}
305
306
static int chacha20_poly1305_final(void *vctx, unsigned char *out, size_t *outl,
307
    size_t outsize)
308
0
{
309
0
    PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx;
310
0
    PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->base.hw;
311
312
0
    if (!ossl_prov_is_running())
313
0
        return 0;
314
315
    /* The tag must be set before actually decrypting data */
316
0
    if (!ctx->base.enc && ctx->tag_len == 0) {
317
0
        ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_SET);
318
0
        return 0;
319
0
    }
320
321
0
    if (hw->aead_cipher((PROV_CIPHER_CTX *)ctx, out, outl, NULL, 0) <= 0)
322
0
        return 0;
323
324
0
    *outl = 0;
325
0
    return 1;
326
0
}
327
328
/* ossl_chacha20_ossl_poly1305_functions */
329
const OSSL_DISPATCH ossl_chacha20_ossl_poly1305_functions[] = {
330
    { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))chacha20_poly1305_newctx },
331
    { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))chacha20_poly1305_freectx },
332
    { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))chacha20_poly1305_dupctx },
333
    { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))chacha20_poly1305_einit },
334
    { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))chacha20_poly1305_dinit },
335
    { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))chacha20_poly1305_update },
336
    { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))chacha20_poly1305_final },
337
    { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))chacha20_poly1305_cipher },
338
    { OSSL_FUNC_CIPHER_GET_PARAMS,
339
        (void (*)(void))chacha20_poly1305_get_params },
340
    { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,
341
        (void (*)(void))chacha20_poly1305_gettable_params },
342
    { OSSL_FUNC_CIPHER_GET_CTX_PARAMS,
343
        (void (*)(void))chacha20_poly1305_get_ctx_params },
344
    { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,
345
        (void (*)(void))chacha20_poly1305_gettable_ctx_params },
346
    { OSSL_FUNC_CIPHER_SET_CTX_PARAMS,
347
        (void (*)(void))chacha20_poly1305_set_ctx_params },
348
    { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,
349
        (void (*)(void))chacha20_poly1305_settable_ctx_params },
350
    OSSL_DISPATCH_END
351
};