Coverage Report

Created: 2026-08-12 06:14

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
40
static void *chacha20_poly1305_newctx(void *provctx)
41
0
{
42
0
    PROV_CHACHA20_POLY1305_CTX *ctx;
43
44
0
    if (!ossl_prov_is_running())
45
0
        return NULL;
46
47
0
    ctx = OPENSSL_zalloc(sizeof(*ctx));
48
0
    if (ctx != NULL) {
49
0
        ossl_cipher_generic_initkey(&ctx->base, CHACHA20_POLY1305_KEYLEN * 8,
50
0
            CHACHA20_POLY1305_BLKLEN * 8,
51
0
            CHACHA20_POLY1305_IVLEN * 8,
52
0
            CHACHA20_POLY1305_MODE,
53
0
            CHACHA20_POLY1305_FLAGS,
54
0
            ossl_prov_cipher_hw_chacha20_poly1305(
55
0
                CHACHA20_POLY1305_KEYLEN * 8),
56
0
            NULL);
57
0
        ctx->tls_payload_length = NO_TLS_PAYLOAD_LENGTH;
58
0
        ossl_chacha20_initctx(&ctx->chacha);
59
0
    }
60
0
    return ctx;
61
0
}
62
63
static void *chacha20_poly1305_dupctx(void *provctx)
64
0
{
65
0
    PROV_CHACHA20_POLY1305_CTX *ctx = provctx;
66
0
    PROV_CHACHA20_POLY1305_CTX *dctx = NULL;
67
68
0
    if (ctx == NULL)
69
0
        return NULL;
70
0
    dctx = OPENSSL_memdup(ctx, sizeof(*ctx));
71
0
    if (dctx != NULL
72
0
        && !ossl_cipher_generic_dupctx_tlsmac(&dctx->base, &ctx->base)) {
73
0
        OPENSSL_clear_free(dctx, sizeof(*dctx));
74
0
        return NULL;
75
0
    }
76
0
    return dctx;
77
0
}
78
79
static void chacha20_poly1305_freectx(void *vctx)
80
0
{
81
0
    PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx;
82
83
0
    if (ctx != NULL) {
84
0
        ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
85
0
        OPENSSL_clear_free(ctx, sizeof(*ctx));
86
0
    }
87
0
}
88
89
static int chacha20_poly1305_get_params(OSSL_PARAM params[])
90
1
{
91
1
    return ossl_cipher_generic_get_params(params, 0, CHACHA20_POLY1305_FLAGS,
92
1
        CHACHA20_POLY1305_KEYLEN * 8,
93
1
        CHACHA20_POLY1305_BLKLEN * 8,
94
1
        CHACHA20_POLY1305_IVLEN * 8);
95
1
}
96
97
static int chacha20_poly1305_get_ctx_params(void *vctx, OSSL_PARAM params[])
98
0
{
99
0
    PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx;
100
0
    struct chacha20_poly1305_get_ctx_params_st p;
101
102
0
    if (ctx == NULL || !chacha20_poly1305_get_ctx_params_decoder(params, &p))
103
0
        return 0;
104
105
0
    if (p.ivlen != NULL
106
0
        && !OSSL_PARAM_set_size_t(p.ivlen, CHACHA20_POLY1305_IVLEN)) {
107
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
108
0
        return 0;
109
0
    }
110
111
0
    if (p.keylen != NULL
112
0
        && !OSSL_PARAM_set_size_t(p.keylen, CHACHA20_POLY1305_KEYLEN)) {
113
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
114
0
        return 0;
115
0
    }
116
117
0
    if (p.taglen != NULL
118
0
        && !OSSL_PARAM_set_size_t(p.taglen, ctx->tag_len)) {
119
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
120
0
        return 0;
121
0
    }
122
123
0
    if (p.pad != NULL
124
0
        && !OSSL_PARAM_set_size_t(p.pad, ctx->tls_aad_pad_sz)) {
125
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
126
0
        return 0;
127
0
    }
128
129
0
    if (p.tag != NULL) {
130
0
        if (p.tag->data_type != OSSL_PARAM_OCTET_STRING) {
131
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
132
0
            return 0;
133
0
        }
134
0
        if (!ctx->base.enc) {
135
0
            ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_SET);
136
0
            return 0;
137
0
        }
138
0
        if (p.tag->data_size == 0 || p.tag->data_size > POLY1305_BLOCK_SIZE) {
139
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG_LENGTH);
140
0
            return 0;
141
0
        }
142
0
        memcpy(p.tag->data, ctx->tag, p.tag->data_size);
143
0
    }
144
0
    return 1;
145
0
}
146
147
static const OSSL_PARAM *chacha20_poly1305_gettable_ctx_params(ossl_unused void *cctx, ossl_unused void *provctx)
148
1
{
149
1
    return chacha20_poly1305_get_ctx_params_list;
150
1
}
151
152
static const OSSL_PARAM *chacha20_poly1305_settable_ctx_params(
153
    ossl_unused void *cctx, ossl_unused void *provctx)
154
0
{
155
0
    return chacha20_poly1305_set_ctx_params_list;
156
0
}
157
158
static int chacha20_poly1305_set_ctx_params(void *vctx,
159
    const OSSL_PARAM params[])
160
0
{
161
0
    size_t len;
162
0
    PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx;
163
0
    PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->base.hw;
164
0
    struct chacha20_poly1305_set_ctx_params_st p;
165
166
0
    if (!chacha20_poly1305_set_ctx_params_decoder(params, &p))
167
0
        return 0;
168
169
0
    if (p.keylen != NULL) {
170
0
        if (!OSSL_PARAM_get_size_t(p.keylen, &len)) {
171
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
172
0
            return 0;
173
0
        }
174
0
        if (len != CHACHA20_POLY1305_KEYLEN) {
175
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
176
0
            return 0;
177
0
        }
178
0
    }
179
180
0
    if (p.ivlen != NULL) {
181
0
        if (!OSSL_PARAM_get_size_t(p.ivlen, &len)) {
182
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
183
0
            return 0;
184
0
        }
185
0
        if (len != CHACHA20_POLY1305_MAX_IVLEN) {
186
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
187
0
            return 0;
188
0
        }
189
0
        ctx->iv_state = IV_STATE_UNINITIALISED;
190
0
    }
191
192
0
    if (p.tag != NULL) {
193
0
        if (p.tag->data_type != OSSL_PARAM_OCTET_STRING) {
194
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
195
0
            return 0;
196
0
        }
197
0
        if (p.tag->data_size == 0 || p.tag->data_size > POLY1305_BLOCK_SIZE) {
198
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG_LENGTH);
199
0
            return 0;
200
0
        }
201
0
        if (p.tag->data != NULL) {
202
0
            if (ctx->base.enc) {
203
0
                ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_NEEDED);
204
0
                return 0;
205
0
            }
206
0
            memcpy(ctx->tag, p.tag->data, p.tag->data_size);
207
0
        }
208
0
        ctx->tag_len = p.tag->data_size;
209
0
    }
210
211
0
    if (p.aad != NULL) {
212
0
        if (p.aad->data_type != OSSL_PARAM_OCTET_STRING) {
213
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
214
0
            return 0;
215
0
        }
216
0
        len = hw->tls_init(&ctx->base, p.aad->data, p.aad->data_size);
217
0
        if (len == 0) {
218
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DATA);
219
0
            return 0;
220
0
        }
221
0
        ctx->tls_aad_pad_sz = len;
222
0
    }
223
224
0
    if (p.fixed != NULL) {
225
0
        if (p.fixed->data_type != OSSL_PARAM_OCTET_STRING) {
226
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
227
0
            return 0;
228
0
        }
229
0
        if (hw->tls_iv_set_fixed(&ctx->base, p.fixed->data,
230
0
                p.fixed->data_size)
231
0
            == 0) {
232
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
233
0
            return 0;
234
0
        }
235
0
    }
236
0
    return 1;
237
0
}
238
239
static int chacha20_poly1305_einit(void *vctx, const unsigned char *key,
240
    size_t keylen, const unsigned char *iv,
241
    size_t ivlen, const OSSL_PARAM params[])
242
0
{
243
0
    int ret;
244
245
    /* The generic function checks for ossl_prov_is_running() */
246
0
    ret = ossl_cipher_generic_einit(vctx, key, keylen, iv, ivlen, NULL);
247
0
    if (ret && iv != NULL) {
248
0
        PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
249
0
        PROV_CHACHA20_POLY1305_CTX *cctx = (PROV_CHACHA20_POLY1305_CTX *)vctx;
250
0
        PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw;
251
252
0
        hw->initiv(ctx);
253
0
        cctx->iv_state = IV_STATE_BUFFERED;
254
0
    }
255
0
    if (ret && !chacha20_poly1305_set_ctx_params(vctx, params))
256
0
        ret = 0;
257
0
    return ret;
258
0
}
259
260
static int chacha20_poly1305_dinit(void *vctx, const unsigned char *key,
261
    size_t keylen, const unsigned char *iv,
262
    size_t ivlen, const OSSL_PARAM params[])
263
0
{
264
0
    int ret;
265
266
    /* The generic function checks for ossl_prov_is_running() */
267
0
    ret = ossl_cipher_generic_dinit(vctx, key, keylen, iv, ivlen, NULL);
268
0
    if (ret && iv != NULL) {
269
0
        PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
270
0
        PROV_CHACHA20_POLY1305_CTX *cctx = (PROV_CHACHA20_POLY1305_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
        cctx->iv_state = IV_STATE_BUFFERED;
275
0
    }
276
0
    if (ret && !chacha20_poly1305_set_ctx_params(vctx, params))
277
0
        ret = 0;
278
0
    return ret;
279
0
}
280
281
static int chacha20_poly1305_cipher(void *vctx, unsigned char *out,
282
    size_t *outl, size_t outsize,
283
    const unsigned char *in, size_t inl)
284
0
{
285
0
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
286
0
    PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw;
287
288
0
    if (!ossl_prov_is_running())
289
0
        return 0;
290
291
0
    if (outsize < inl) {
292
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
293
0
        return 0;
294
0
    }
295
296
0
    if (!hw->aead_cipher(ctx, out, outl, in, inl))
297
0
        return 0;
298
299
0
    return 1;
300
0
}
301
302
static int chacha20_poly1305_update(void *vctx, unsigned char *out,
303
    size_t *outl, size_t outsize,
304
    const unsigned char *in, size_t inl)
305
0
{
306
0
    PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx;
307
308
0
    if (ctx->iv_state == IV_STATE_FINISHED)
309
0
        return 0;
310
311
    /*
312
     * a zero-length update is a nop, ALWAYS SUCCEED via early exit
313
     * NB: ONLY EVP_Cipher() / final produce or check the tag
314
     */
315
0
    if (inl == 0) {
316
0
        *outl = 0;
317
0
        return 1;
318
0
    }
319
320
0
    return chacha20_poly1305_cipher(vctx, out, outl, outsize, in, inl);
321
0
}
322
323
static int chacha20_poly1305_final(void *vctx, unsigned char *out, size_t *outl,
324
    size_t outsize)
325
0
{
326
0
    PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx;
327
0
    PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->base.hw;
328
329
0
    if (!ossl_prov_is_running())
330
0
        return 0;
331
332
    /* The tag must be set before actually decrypting data */
333
0
    if (!ctx->base.enc && ctx->tag_len == 0) {
334
0
        ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_SET);
335
0
        return 0;
336
0
    }
337
338
0
    if (hw->aead_cipher((PROV_CIPHER_CTX *)ctx, out, outl, NULL, 0) <= 0)
339
0
        return 0;
340
341
0
    *outl = 0;
342
343
    /* Don't reuse the IV */
344
0
    ctx->iv_state = IV_STATE_FINISHED;
345
0
    return 1;
346
0
}
347
348
/* ossl_chacha20_ossl_poly1305_functions */
349
const OSSL_DISPATCH ossl_chacha20_ossl_poly1305_functions[] = {
350
    { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))chacha20_poly1305_newctx },
351
    { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))chacha20_poly1305_freectx },
352
    { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))chacha20_poly1305_dupctx },
353
    { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))chacha20_poly1305_einit },
354
    { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))chacha20_poly1305_dinit },
355
    { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))chacha20_poly1305_update },
356
    { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))chacha20_poly1305_final },
357
    { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))chacha20_poly1305_cipher },
358
    { OSSL_FUNC_CIPHER_GET_PARAMS,
359
        (void (*)(void))chacha20_poly1305_get_params },
360
    { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,
361
        (void (*)(void))chacha20_poly1305_gettable_params },
362
    { OSSL_FUNC_CIPHER_GET_CTX_PARAMS,
363
        (void (*)(void))chacha20_poly1305_get_ctx_params },
364
    { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,
365
        (void (*)(void))chacha20_poly1305_gettable_ctx_params },
366
    { OSSL_FUNC_CIPHER_SET_CTX_PARAMS,
367
        (void (*)(void))chacha20_poly1305_set_ctx_params },
368
    { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,
369
        (void (*)(void))chacha20_poly1305_settable_ctx_params },
370
    OSSL_DISPATCH_END
371
};