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_aes_siv.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 AES SIV mode */
11
12
/*
13
 * This file uses the low level AES functions (which are deprecated for
14
 * non-internal use) in order to implement provider AES ciphers.
15
 */
16
#include "internal/deprecated.h"
17
18
#include <openssl/proverr.h>
19
#include "cipher_aes_siv.h"
20
#include "prov/implementations.h"
21
#include "prov/providercommon.h"
22
#include "prov/ciphercommon_aead.h"
23
#include "prov/provider_ctx.h"
24
#include "providers/implementations/ciphers/cipher_aes_siv.inc"
25
26
#define siv_stream_update siv_cipher
27
#define SIV_FLAGS AEAD_FLAGS
28
29
static OSSL_FUNC_cipher_set_ctx_params_fn aes_siv_set_ctx_params;
30
31
static void *aes_siv_newctx(void *provctx, size_t keybits, unsigned int mode,
32
    uint64_t flags)
33
0
{
34
0
    PROV_AES_SIV_CTX *ctx;
35
36
0
    if (!ossl_prov_is_running())
37
0
        return NULL;
38
39
0
    ctx = OPENSSL_zalloc(sizeof(*ctx));
40
0
    if (ctx != NULL) {
41
0
        ctx->taglen = SIV_LEN;
42
0
        ctx->mode = mode;
43
0
        ctx->keylen = keybits / 8;
44
0
        ctx->hw = ossl_prov_cipher_hw_aes_siv(keybits);
45
0
        ctx->libctx = PROV_LIBCTX_OF(provctx);
46
0
    }
47
0
    return ctx;
48
0
}
49
50
static void aes_siv_freectx(void *vctx)
51
0
{
52
0
    PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
53
54
0
    if (ctx != NULL) {
55
0
        ctx->hw->cleanup(ctx);
56
0
        OPENSSL_clear_free(ctx, sizeof(*ctx));
57
0
    }
58
0
}
59
60
static void *siv_dupctx(void *vctx)
61
0
{
62
0
    PROV_AES_SIV_CTX *in = (PROV_AES_SIV_CTX *)vctx;
63
0
    PROV_AES_SIV_CTX *ret;
64
65
0
    if (!ossl_prov_is_running())
66
0
        return NULL;
67
68
0
    ret = OPENSSL_malloc(sizeof(*ret));
69
0
    if (ret == NULL)
70
0
        return NULL;
71
0
    if (!in->hw->dupctx(in, ret)) {
72
0
        OPENSSL_free(ret);
73
0
        ret = NULL;
74
0
    }
75
0
    return ret;
76
0
}
77
78
static int siv_init(void *vctx, const unsigned char *key, size_t keylen,
79
    const unsigned char *iv, size_t ivlen,
80
    const OSSL_PARAM params[], int enc)
81
0
{
82
0
    PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
83
84
0
    if (!ossl_prov_is_running())
85
0
        return 0;
86
87
0
    ctx->enc = enc;
88
89
0
    if (key != NULL) {
90
0
        if (keylen != ctx->keylen) {
91
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
92
0
            return 0;
93
0
        }
94
0
        if (!ctx->hw->initkey(ctx, key, ctx->keylen))
95
0
            return 0;
96
0
    }
97
0
    return aes_siv_set_ctx_params(ctx, params);
98
0
}
99
100
static int siv_einit(void *vctx, const unsigned char *key, size_t keylen,
101
    const unsigned char *iv, size_t ivlen,
102
    const OSSL_PARAM params[])
103
0
{
104
0
    return siv_init(vctx, key, keylen, iv, ivlen, params, 1);
105
0
}
106
107
static int siv_dinit(void *vctx, const unsigned char *key, size_t keylen,
108
    const unsigned char *iv, size_t ivlen,
109
    const OSSL_PARAM params[])
110
0
{
111
0
    return siv_init(vctx, key, keylen, iv, ivlen, params, 0);
112
0
}
113
114
static int siv_cipher(void *vctx, unsigned char *out, size_t *outl,
115
    size_t outsize, const unsigned char *in, size_t inl)
116
0
{
117
0
    PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
118
119
0
    if (!ossl_prov_is_running())
120
0
        return 0;
121
122
0
    if (out != NULL && outsize < inl) {
123
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
124
0
        return 0;
125
0
    }
126
127
0
    if (ctx->hw->cipher(ctx, out, in, inl) <= 0)
128
0
        return 0;
129
130
0
    if (outl != NULL)
131
0
        *outl = inl;
132
0
    return 1;
133
0
}
134
135
static int siv_stream_final(void *vctx, unsigned char *out, size_t *outl,
136
    size_t outsize)
137
0
{
138
0
    PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
139
140
0
    if (!ossl_prov_is_running())
141
0
        return 0;
142
143
0
    if (!ctx->hw->cipher(vctx, out, NULL, 0))
144
0
        return 0;
145
146
0
    if (outl != NULL)
147
0
        *outl = 0;
148
0
    return 1;
149
0
}
150
151
static int aes_siv_get_ctx_params(void *vctx, OSSL_PARAM params[])
152
0
{
153
0
    PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
154
0
    SIV128_CONTEXT *sctx;
155
0
    struct aes_siv_get_ctx_params_st p;
156
157
0
    if (ctx == NULL || !aes_siv_get_ctx_params_decoder(params, &p))
158
0
        return 0;
159
160
0
    sctx = &ctx->siv;
161
162
0
    if (p.tag != NULL) {
163
0
        if (!ctx->enc
164
0
            || p.tag->data_type != OSSL_PARAM_OCTET_STRING
165
0
            || p.tag->data_size != ctx->taglen
166
0
            || !OSSL_PARAM_set_octet_string(p.tag, &sctx->tag.byte,
167
0
                ctx->taglen)) {
168
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
169
0
            return 0;
170
0
        }
171
0
    }
172
173
0
    if (p.taglen != NULL && !OSSL_PARAM_set_size_t(p.taglen, ctx->taglen)) {
174
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
175
0
        return 0;
176
0
    }
177
178
0
    if (p.keylen != NULL && !OSSL_PARAM_set_size_t(p.keylen, ctx->keylen)) {
179
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
180
0
        return 0;
181
0
    }
182
0
    return 1;
183
0
}
184
185
static const OSSL_PARAM *aes_siv_gettable_ctx_params(ossl_unused void *cctx,
186
    ossl_unused void *provctx)
187
3
{
188
3
    return aes_siv_get_ctx_params_list;
189
3
}
190
191
static int aes_siv_set_ctx_params(void *vctx, const OSSL_PARAM params[])
192
0
{
193
0
    PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
194
0
    struct aes_siv_set_ctx_params_st p;
195
0
    unsigned int speed = 0;
196
197
0
    if (ctx == NULL || !aes_siv_set_ctx_params_decoder(params, &p))
198
0
        return 0;
199
200
0
    if (p.tag != NULL) {
201
0
        if (ctx->enc)
202
0
            return 1;
203
0
        if (p.tag->data_type != OSSL_PARAM_OCTET_STRING
204
0
            || !ctx->hw->settag(ctx, p.tag->data, p.tag->data_size)) {
205
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
206
0
            return 0;
207
0
        }
208
0
    }
209
210
0
    if (p.speed != NULL) {
211
0
        if (!OSSL_PARAM_get_uint(p.speed, &speed)) {
212
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
213
0
            return 0;
214
0
        }
215
0
        ctx->hw->setspeed(ctx, (int)speed);
216
0
    }
217
218
0
    if (p.keylen != NULL) {
219
0
        size_t keylen;
220
221
0
        if (!OSSL_PARAM_get_size_t(p.keylen, &keylen)) {
222
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
223
0
            return 0;
224
0
        }
225
        /* The key length can not be modified */
226
0
        if (keylen != ctx->keylen)
227
0
            return 0;
228
0
    }
229
0
    return 1;
230
0
}
231
232
static const OSSL_PARAM *aes_siv_settable_ctx_params(ossl_unused void *cctx,
233
    ossl_unused void *provctx)
234
0
{
235
0
    return aes_siv_set_ctx_params_list;
236
0
}
237
238
#define IMPLEMENT_cipher(alg, lc, UCMODE, flags, kbits, blkbits, ivbits)         \
239
    static OSSL_FUNC_cipher_newctx_fn alg##kbits##lc##_newctx;                   \
240
    static OSSL_FUNC_cipher_freectx_fn alg##_##lc##_freectx;                     \
241
    static OSSL_FUNC_cipher_dupctx_fn lc##_dupctx;                               \
242
    static OSSL_FUNC_cipher_encrypt_init_fn lc##_einit;                          \
243
    static OSSL_FUNC_cipher_decrypt_init_fn lc##_dinit;                          \
244
    static OSSL_FUNC_cipher_update_fn lc##_stream_update;                        \
245
    static OSSL_FUNC_cipher_final_fn lc##_stream_final;                          \
246
    static OSSL_FUNC_cipher_cipher_fn lc##_cipher;                               \
247
    static OSSL_FUNC_cipher_get_params_fn alg##_##kbits##_##lc##_get_params;     \
248
    static OSSL_FUNC_cipher_get_ctx_params_fn alg##_##lc##_get_ctx_params;       \
249
    static OSSL_FUNC_cipher_gettable_ctx_params_fn                               \
250
        alg##_##lc##_gettable_ctx_params;                                        \
251
    static OSSL_FUNC_cipher_set_ctx_params_fn alg##_##lc##_set_ctx_params;       \
252
    static OSSL_FUNC_cipher_settable_ctx_params_fn                               \
253
        alg##_##lc##_settable_ctx_params;                                        \
254
    static int alg##_##kbits##_##lc##_get_params(OSSL_PARAM params[])            \
255
3
    {                                                                            \
256
3
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,  \
257
3
            flags, 2 * kbits, blkbits, ivbits);                                  \
258
3
    }                                                                            \
cipher_aes_siv.c:aes_128_siv_get_params
Line
Count
Source
255
1
    {                                                                            \
256
1
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,  \
257
1
            flags, 2 * kbits, blkbits, ivbits);                                  \
258
1
    }                                                                            \
cipher_aes_siv.c:aes_192_siv_get_params
Line
Count
Source
255
1
    {                                                                            \
256
1
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,  \
257
1
            flags, 2 * kbits, blkbits, ivbits);                                  \
258
1
    }                                                                            \
cipher_aes_siv.c:aes_256_siv_get_params
Line
Count
Source
255
1
    {                                                                            \
256
1
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,  \
257
1
            flags, 2 * kbits, blkbits, ivbits);                                  \
258
1
    }                                                                            \
259
    static void *alg##kbits##lc##_newctx(void *provctx)                          \
260
0
    {                                                                            \
261
0
        return alg##_##lc##_newctx(provctx, 2 * kbits, EVP_CIPH_##UCMODE##_MODE, \
262
0
            flags);                                                              \
263
0
    }                                                                            \
Unexecuted instantiation: cipher_aes_siv.c:aes128siv_newctx
Unexecuted instantiation: cipher_aes_siv.c:aes192siv_newctx
Unexecuted instantiation: cipher_aes_siv.c:aes256siv_newctx
264
    const OSSL_DISPATCH ossl_##alg##kbits##lc##_functions[] = {                  \
265
        { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))alg##kbits##lc##_newctx },    \
266
        { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))alg##_##lc##_freectx },      \
267
        { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))lc##_dupctx },                \
268
        { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))lc##_einit },           \
269
        { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))lc##_dinit },           \
270
        { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))lc##_stream_update },         \
271
        { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))lc##_stream_final },           \
272
        { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))lc##_cipher },                \
273
        { OSSL_FUNC_CIPHER_GET_PARAMS,                                           \
274
            (void (*)(void))alg##_##kbits##_##lc##_get_params },                 \
275
        { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,                                      \
276
            (void (*)(void))ossl_cipher_generic_gettable_params },               \
277
        { OSSL_FUNC_CIPHER_GET_CTX_PARAMS,                                       \
278
            (void (*)(void))alg##_##lc##_get_ctx_params },                       \
279
        { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,                                  \
280
            (void (*)(void))alg##_##lc##_gettable_ctx_params },                  \
281
        { OSSL_FUNC_CIPHER_SET_CTX_PARAMS,                                       \
282
            (void (*)(void))alg##_##lc##_set_ctx_params },                       \
283
        { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,                                  \
284
            (void (*)(void))alg##_##lc##_settable_ctx_params },                  \
285
        OSSL_DISPATCH_END                                                        \
286
    };
287
288
IMPLEMENT_cipher(aes, siv, SIV, SIV_FLAGS, 128, 8, 0)
289
IMPLEMENT_cipher(aes, siv, SIV, SIV_FLAGS, 192, 8, 0)
290
IMPLEMENT_cipher(aes, siv, SIV, SIV_FLAGS, 256, 8, 0)