Coverage Report

Created: 2025-11-07 06:58

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
152
    (ossl_unused void *cctx, ossl_unused void *provctx)
153
1
{
154
1
    return chacha20_poly1305_get_ctx_params_list;
155
1
}
156
157
static const OSSL_PARAM *chacha20_poly1305_settable_ctx_params(
158
        ossl_unused void *cctx, ossl_unused void *provctx
159
    )
160
0
{
161
0
    return chacha20_poly1305_set_ctx_params_list;
162
0
}
163
164
static int chacha20_poly1305_set_ctx_params(void *vctx,
165
                                            const OSSL_PARAM params[])
166
0
{
167
0
    size_t len;
168
0
    PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx;
169
0
    PROV_CIPHER_HW_CHACHA20_POLY1305 *hw =
170
0
        (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->base.hw;
171
0
    struct chacha20_poly1305_set_ctx_params_st p;
172
173
0
    if (!chacha20_poly1305_set_ctx_params_decoder(params, &p))
174
0
        return 0;
175
176
0
    if (p.keylen != NULL) {
177
0
        if (!OSSL_PARAM_get_size_t(p.keylen, &len)) {
178
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
179
0
            return 0;
180
0
        }
181
0
        if (len != CHACHA20_POLY1305_KEYLEN) {
182
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
183
0
            return 0;
184
0
        }
185
0
    }
186
187
0
    if (p.ivlen != NULL) {
188
0
        if (!OSSL_PARAM_get_size_t(p.ivlen, &len)) {
189
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
190
0
            return 0;
191
0
        }
192
0
        if (len != CHACHA20_POLY1305_MAX_IVLEN) {
193
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
194
0
            return 0;
195
0
        }
196
0
    }
197
198
0
    if (p.tag != NULL) {
199
0
        if (p.tag->data_type != OSSL_PARAM_OCTET_STRING) {
200
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
201
0
            return 0;
202
0
        }
203
0
        if (p.tag->data_size == 0 || p.tag->data_size > POLY1305_BLOCK_SIZE) {
204
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG_LENGTH);
205
0
            return 0;
206
0
        }
207
0
        if (p.tag->data != NULL) {
208
0
            if (ctx->base.enc) {
209
0
                ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_NEEDED);
210
0
                return 0;
211
0
            }
212
0
            memcpy(ctx->tag, p.tag->data, p.tag->data_size);
213
0
        }
214
0
        ctx->tag_len = p.tag->data_size;
215
0
    }
216
217
0
    if (p.aad != NULL) {
218
0
        if (p.aad->data_type != OSSL_PARAM_OCTET_STRING) {
219
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
220
0
            return 0;
221
0
        }
222
0
        len = hw->tls_init(&ctx->base, p.aad->data, p.aad->data_size);
223
0
        if (len == 0) {
224
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DATA);
225
0
            return 0;
226
0
        }
227
0
        ctx->tls_aad_pad_sz = len;
228
0
    }
229
230
0
    if (p.fixed != NULL) {
231
0
        if (p.fixed->data_type != OSSL_PARAM_OCTET_STRING) {
232
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
233
0
            return 0;
234
0
        }
235
0
        if (hw->tls_iv_set_fixed(&ctx->base, p.fixed->data,
236
0
                                 p.fixed->data_size) == 0) {
237
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
238
0
            return 0;
239
0
        }
240
0
    }
241
0
    return 1;
242
0
}
243
244
static int chacha20_poly1305_einit(void *vctx, const unsigned char *key,
245
                                  size_t keylen, const unsigned char *iv,
246
                                  size_t ivlen, const OSSL_PARAM params[])
247
0
{
248
0
    int ret;
249
250
    /* The generic function checks for ossl_prov_is_running() */
251
0
    ret = ossl_cipher_generic_einit(vctx, key, keylen, iv, ivlen, NULL);
252
0
    if (ret && iv != NULL) {
253
0
        PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
254
0
        PROV_CIPHER_HW_CHACHA20_POLY1305 *hw =
255
0
            (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw;
256
257
0
        hw->initiv(ctx);
258
0
    }
259
0
    if (ret && !chacha20_poly1305_set_ctx_params(vctx, params))
260
0
        ret = 0;
261
0
    return ret;
262
0
}
263
264
static int chacha20_poly1305_dinit(void *vctx, const unsigned char *key,
265
                                  size_t keylen, const unsigned char *iv,
266
                                  size_t ivlen, const OSSL_PARAM params[])
267
0
{
268
0
    int ret;
269
270
    /* The generic function checks for ossl_prov_is_running() */
271
0
    ret = ossl_cipher_generic_dinit(vctx, key, keylen, iv, ivlen, NULL);
272
0
    if (ret && iv != NULL) {
273
0
        PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
274
0
        PROV_CIPHER_HW_CHACHA20_POLY1305 *hw =
275
0
            (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw;
276
277
0
        hw->initiv(ctx);
278
0
    }
279
0
    if (ret && !chacha20_poly1305_set_ctx_params(vctx, params))
280
0
        ret = 0;
281
0
    return ret;
282
0
}
283
284
static int chacha20_poly1305_cipher(void *vctx, unsigned char *out,
285
                                    size_t *outl, size_t outsize,
286
                                    const unsigned char *in, size_t inl)
287
0
{
288
0
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
289
0
    PROV_CIPHER_HW_CHACHA20_POLY1305 *hw =
290
0
        (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw;
291
292
0
    if (!ossl_prov_is_running())
293
0
        return 0;
294
295
0
    if (inl == 0) {
296
0
        *outl = 0;
297
0
        return 1;
298
0
    }
299
300
0
    if (outsize < inl) {
301
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
302
0
        return 0;
303
0
    }
304
305
0
    if (!hw->aead_cipher(ctx, out, outl, in, inl))
306
0
        return 0;
307
308
0
    return 1;
309
0
}
310
311
static int chacha20_poly1305_final(void *vctx, unsigned char *out, size_t *outl,
312
                                   size_t outsize)
313
0
{
314
0
    PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx;
315
0
    PROV_CIPHER_HW_CHACHA20_POLY1305 *hw =
316
0
        (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->base.hw;
317
318
0
    if (!ossl_prov_is_running())
319
0
        return 0;
320
321
    /* The tag must be set before actually decrypting data */
322
0
    if (!ctx->base.enc && ctx->tag_len == 0) {
323
0
        ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_SET);
324
0
        return 0;
325
0
    }
326
327
0
    if (hw->aead_cipher((PROV_CIPHER_CTX *)ctx, out, outl, NULL, 0) <= 0)
328
0
        return 0;
329
330
0
    *outl = 0;
331
0
    return 1;
332
0
}
333
334
/* ossl_chacha20_ossl_poly1305_functions */
335
const OSSL_DISPATCH ossl_chacha20_ossl_poly1305_functions[] = {
336
    { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))chacha20_poly1305_newctx },
337
    { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))chacha20_poly1305_freectx },
338
    { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))chacha20_poly1305_dupctx },
339
    { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))chacha20_poly1305_einit },
340
    { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))chacha20_poly1305_dinit },
341
    { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))chacha20_poly1305_update },
342
    { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))chacha20_poly1305_final },
343
    { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))chacha20_poly1305_cipher },
344
    { OSSL_FUNC_CIPHER_GET_PARAMS,
345
        (void (*)(void))chacha20_poly1305_get_params },
346
    { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,
347
        (void (*)(void))chacha20_poly1305_gettable_params },
348
    { OSSL_FUNC_CIPHER_GET_CTX_PARAMS,
349
         (void (*)(void))chacha20_poly1305_get_ctx_params },
350
    { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,
351
        (void (*)(void))chacha20_poly1305_gettable_ctx_params },
352
    { OSSL_FUNC_CIPHER_SET_CTX_PARAMS,
353
        (void (*)(void))chacha20_poly1305_set_ctx_params },
354
    { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,
355
        (void (*)(void))chacha20_poly1305_settable_ctx_params },
356
    OSSL_DISPATCH_END
357
};