Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/providers/implementations/ciphers/cipher_aes_wrp.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
/*
11
 * This file uses the low level AES functions (which are deprecated for
12
 * non-internal use) in order to implement provider AES ciphers.
13
 */
14
#include "internal/deprecated.h"
15
16
#include <openssl/proverr.h>
17
#include "cipher_aes.h"
18
#include "prov/providercommon.h"
19
#include "prov/implementations.h"
20
#include "providers/implementations/ciphers/cipher_aes_wrp.inc"
21
22
/* AES wrap with padding has IV length of 4, without padding 8 */
23
43
#define AES_WRAP_PAD_IVLEN 4
24
#define AES_WRAP_NOPAD_IVLEN 8
25
26
#define WRAP_FLAGS (PROV_CIPHER_FLAG_CUSTOM_IV)
27
#define WRAP_FLAGS_INV (WRAP_FLAGS | PROV_CIPHER_FLAG_INVERSE_CIPHER)
28
29
typedef size_t (*aeswrap_fn)(void *key, const unsigned char *iv,
30
    unsigned char *out, const unsigned char *in,
31
    size_t inlen, block128_f block);
32
33
static OSSL_FUNC_cipher_encrypt_init_fn aes_wrap_einit;
34
static OSSL_FUNC_cipher_decrypt_init_fn aes_wrap_dinit;
35
static OSSL_FUNC_cipher_update_fn aes_wrap_cipher;
36
static OSSL_FUNC_cipher_final_fn aes_wrap_final;
37
static OSSL_FUNC_cipher_freectx_fn aes_wrap_freectx;
38
static OSSL_FUNC_cipher_set_ctx_params_fn aes_wrap_set_ctx_params;
39
40
typedef struct prov_aes_wrap_ctx_st {
41
    PROV_CIPHER_CTX base;
42
    union {
43
        OSSL_UNION_ALIGN;
44
        AES_KEY ks;
45
    } ks;
46
    aeswrap_fn wrapfn;
47
    int updated;
48
49
} PROV_AES_WRAP_CTX;
50
51
static void *aes_wrap_newctx(void *provctx, size_t kbits, size_t blkbits,
52
    size_t ivbits, unsigned int mode, uint64_t flags)
53
43
{
54
43
    PROV_AES_WRAP_CTX *wctx;
55
43
    PROV_CIPHER_CTX *ctx;
56
57
43
    CIPHER_PROV_CHECK(provctx, AES_128_WRP);
58
43
    wctx = OPENSSL_zalloc(sizeof(*wctx));
59
43
    ctx = (PROV_CIPHER_CTX *)wctx;
60
43
    if (ctx != NULL) {
61
43
        ossl_cipher_generic_initkey(ctx, kbits, blkbits, ivbits, mode, flags,
62
43
            NULL, NULL);
63
43
        ctx->pad = (ctx->ivlen == AES_WRAP_PAD_IVLEN);
64
43
    }
65
43
    return wctx;
66
43
}
67
68
static void *aes_wrap_dupctx(void *wctx)
69
0
{
70
0
    PROV_AES_WRAP_CTX *ctx = wctx;
71
0
    PROV_AES_WRAP_CTX *dctx = wctx;
72
73
0
    if (!ossl_prov_is_running())
74
0
        return NULL;
75
76
0
    if (ctx == NULL)
77
0
        return NULL;
78
0
    dctx = OPENSSL_memdup(ctx, sizeof(*ctx));
79
80
0
    if (dctx != NULL && dctx->base.tlsmac != NULL && dctx->base.alloced) {
81
0
        dctx->base.tlsmac = OPENSSL_memdup(dctx->base.tlsmac,
82
0
            dctx->base.tlsmacsize);
83
0
        if (dctx->base.tlsmac == NULL) {
84
0
            OPENSSL_free(dctx);
85
0
            dctx = NULL;
86
0
        }
87
0
    }
88
0
    return dctx;
89
0
}
90
91
static void aes_wrap_freectx(void *vctx)
92
43
{
93
43
    PROV_AES_WRAP_CTX *wctx = (PROV_AES_WRAP_CTX *)vctx;
94
95
43
    ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
96
43
    OPENSSL_clear_free(wctx, sizeof(*wctx));
97
43
}
98
99
static int aes_wrap_init(void *vctx, const unsigned char *key,
100
    size_t keylen, const unsigned char *iv,
101
    size_t ivlen, const OSSL_PARAM params[], int enc)
102
68
{
103
68
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
104
68
    PROV_AES_WRAP_CTX *wctx = (PROV_AES_WRAP_CTX *)vctx;
105
106
68
    if (!ossl_prov_is_running())
107
0
        return 0;
108
109
68
    wctx->updated = 0;
110
68
    ctx->enc = enc;
111
68
    if (ctx->pad)
112
18
        wctx->wrapfn = enc ? CRYPTO_128_wrap_pad : CRYPTO_128_unwrap_pad;
113
50
    else
114
50
        wctx->wrapfn = enc ? CRYPTO_128_wrap : CRYPTO_128_unwrap;
115
116
68
    if (iv != NULL) {
117
0
        if (!ossl_cipher_generic_initiv(ctx, iv, ivlen))
118
0
            return 0;
119
0
    }
120
68
    if (key != NULL) {
121
25
        int use_forward_transform;
122
123
25
        if (keylen != ctx->keylen) {
124
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
125
0
            return 0;
126
0
        }
127
        /*
128
         * See SP800-38F : Section 5.1
129
         * The forward and inverse transformations for the AES block
130
         * cipher—called “cipher” and “inverse  cipher” are informally known as
131
         * the AES encryption and AES decryption functions, respectively.
132
         * If the designated cipher function for a key-wrap algorithm is chosen
133
         * to be the AES decryption function, then CIPH-1K will be the AES
134
         * encryption function.
135
         */
136
25
        if (ctx->inverse_cipher == 0)
137
25
            use_forward_transform = ctx->enc;
138
0
        else
139
0
            use_forward_transform = !ctx->enc;
140
25
        if (use_forward_transform) {
141
3
            AES_set_encrypt_key(key, (int)(keylen * 8), &wctx->ks.ks);
142
3
            ctx->block = (block128_f)AES_encrypt;
143
22
        } else {
144
22
            AES_set_decrypt_key(key, (int)(keylen * 8), &wctx->ks.ks);
145
22
            ctx->block = (block128_f)AES_decrypt;
146
22
        }
147
25
    }
148
68
    return aes_wrap_set_ctx_params(ctx, params);
149
68
}
150
151
static int aes_wrap_einit(void *ctx, const unsigned char *key, size_t keylen,
152
    const unsigned char *iv, size_t ivlen,
153
    const OSSL_PARAM params[])
154
10
{
155
10
    return aes_wrap_init(ctx, key, keylen, iv, ivlen, params, 1);
156
10
}
157
158
static int aes_wrap_dinit(void *ctx, const unsigned char *key, size_t keylen,
159
    const unsigned char *iv, size_t ivlen,
160
    const OSSL_PARAM params[])
161
58
{
162
58
    return aes_wrap_init(ctx, key, keylen, iv, ivlen, params, 0);
163
58
}
164
165
static int aes_wrap_cipher_internal(void *vctx, unsigned char *out,
166
    const unsigned char *in, size_t inlen)
167
22
{
168
22
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
169
22
    PROV_AES_WRAP_CTX *wctx = (PROV_AES_WRAP_CTX *)vctx;
170
22
    size_t rv;
171
22
    int pad = ctx->pad;
172
173
    /* No final operation so always return zero length */
174
22
    if (in == NULL)
175
0
        return 0;
176
177
    /* Input length must always be non-zero */
178
22
    if (inlen == 0 || inlen > INT_MAX) {
179
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_INPUT_LENGTH);
180
0
        return -1;
181
0
    }
182
183
    /* If decrypting need at least 16 bytes and multiple of 8 */
184
22
    if (!ctx->enc && (inlen < 16 || inlen & 0x7)) {
185
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_INPUT_LENGTH);
186
0
        return -1;
187
0
    }
188
189
    /* If not padding input must be multiple of 8 */
190
22
    if (!pad && inlen & 0x7) {
191
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_INPUT_LENGTH);
192
0
        return -1;
193
0
    }
194
195
22
    if (out == NULL) {
196
0
        if (ctx->enc) {
197
            /* If padding round up to multiple of 8 */
198
0
            if (pad)
199
0
                inlen = (inlen + 7) / 8 * 8;
200
            /* 8 byte prefix */
201
0
            return (int)(inlen + 8);
202
0
        } else {
203
            /*
204
             * If not padding output will be exactly 8 bytes smaller than
205
             * input. If padding it will be at least 8 bytes smaller but we
206
             * don't know how much.
207
             */
208
0
            return (int)(inlen - 8);
209
0
        }
210
0
    }
211
212
    /*
213
     * Multiple calls to update are not allowed, since the algorithm
214
     * relies on all fields being present.
215
     */
216
22
    if (wctx->updated) {
217
0
        ERR_raise(ERR_LIB_PROV, EVP_R_UPDATE_ERROR);
218
0
        return -1;
219
0
    }
220
22
    wctx->updated = 1;
221
222
22
    rv = wctx->wrapfn(&wctx->ks.ks, ctx->iv_set ? ctx->iv : NULL, out, in,
223
22
        inlen, ctx->block);
224
22
    if (!rv) {
225
22
        ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
226
22
        return -1;
227
22
    }
228
0
    if (rv > INT_MAX) {
229
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_OUTPUT_LENGTH);
230
0
        return -1;
231
0
    }
232
0
    return (int)rv;
233
0
}
234
235
static int aes_wrap_final(void *vctx, unsigned char *out, size_t *outl,
236
    size_t outsize)
237
0
{
238
0
    if (!ossl_prov_is_running())
239
0
        return 0;
240
241
0
    *outl = 0;
242
0
    return 1;
243
0
}
244
245
static int aes_wrap_cipher(void *vctx,
246
    unsigned char *out, size_t *outl, size_t outsize,
247
    const unsigned char *in, size_t inl)
248
25
{
249
25
    PROV_AES_WRAP_CTX *ctx = (PROV_AES_WRAP_CTX *)vctx;
250
25
    int len;
251
252
25
    if (!ossl_prov_is_running())
253
0
        return 0;
254
255
25
    if (inl == 0) {
256
0
        *outl = 0;
257
0
        return 1;
258
0
    }
259
260
25
    if (outsize < inl) {
261
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
262
0
        return 0;
263
0
    }
264
265
25
    len = aes_wrap_cipher_internal(ctx, out, in, inl);
266
25
    if (len <= 0)
267
22
        return 0;
268
269
3
    *outl = (size_t)len;
270
3
    return 1;
271
25
}
272
273
static const OSSL_PARAM *aes_wrap_settable_ctx_params(ossl_unused void *cctx,
274
    ossl_unused void *provctx)
275
3
{
276
3
    return aes_wrap_set_ctx_params_list;
277
3
}
278
279
static int aes_wrap_set_ctx_params(void *vctx, const OSSL_PARAM params[])
280
62
{
281
62
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
282
62
    struct aes_wrap_set_ctx_params_st p;
283
62
    size_t keylen = 0;
284
285
62
    if (ctx == NULL || !aes_wrap_set_ctx_params_decoder(params, &p))
286
0
        return 0;
287
288
62
    if (p.keylen != NULL) {
289
2
        if (!OSSL_PARAM_get_size_t(p.keylen, &keylen)) {
290
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
291
0
            return 0;
292
0
        }
293
2
        if (ctx->keylen != keylen) {
294
2
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
295
2
            return 0;
296
2
        }
297
2
    }
298
60
    return 1;
299
62
}
300
301
#define IMPLEMENT_cipher(mode, fname, UCMODE, flags, kbits, blkbits, ivbits)    \
302
    static OSSL_FUNC_cipher_get_params_fn aes_##kbits##_##fname##_get_params;   \
303
    static int aes_##kbits##_##fname##_get_params(OSSL_PARAM params[])          \
304
804
    {                                                                           \
305
804
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
306
804
            flags, kbits, blkbits, ivbits);                                     \
307
804
    }                                                                           \
cipher_aes_wrp.c:aes_256_wrap_get_params
Line
Count
Source
304
67
    {                                                                           \
305
67
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
306
67
            flags, kbits, blkbits, ivbits);                                     \
307
67
    }                                                                           \
cipher_aes_wrp.c:aes_192_wrap_get_params
Line
Count
Source
304
67
    {                                                                           \
305
67
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
306
67
            flags, kbits, blkbits, ivbits);                                     \
307
67
    }                                                                           \
cipher_aes_wrp.c:aes_128_wrap_get_params
Line
Count
Source
304
67
    {                                                                           \
305
67
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
306
67
            flags, kbits, blkbits, ivbits);                                     \
307
67
    }                                                                           \
cipher_aes_wrp.c:aes_256_wrappad_get_params
Line
Count
Source
304
67
    {                                                                           \
305
67
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
306
67
            flags, kbits, blkbits, ivbits);                                     \
307
67
    }                                                                           \
cipher_aes_wrp.c:aes_192_wrappad_get_params
Line
Count
Source
304
67
    {                                                                           \
305
67
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
306
67
            flags, kbits, blkbits, ivbits);                                     \
307
67
    }                                                                           \
cipher_aes_wrp.c:aes_128_wrappad_get_params
Line
Count
Source
304
67
    {                                                                           \
305
67
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
306
67
            flags, kbits, blkbits, ivbits);                                     \
307
67
    }                                                                           \
cipher_aes_wrp.c:aes_256_wrapinv_get_params
Line
Count
Source
304
67
    {                                                                           \
305
67
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
306
67
            flags, kbits, blkbits, ivbits);                                     \
307
67
    }                                                                           \
cipher_aes_wrp.c:aes_192_wrapinv_get_params
Line
Count
Source
304
67
    {                                                                           \
305
67
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
306
67
            flags, kbits, blkbits, ivbits);                                     \
307
67
    }                                                                           \
cipher_aes_wrp.c:aes_128_wrapinv_get_params
Line
Count
Source
304
67
    {                                                                           \
305
67
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
306
67
            flags, kbits, blkbits, ivbits);                                     \
307
67
    }                                                                           \
cipher_aes_wrp.c:aes_256_wrappadinv_get_params
Line
Count
Source
304
67
    {                                                                           \
305
67
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
306
67
            flags, kbits, blkbits, ivbits);                                     \
307
67
    }                                                                           \
cipher_aes_wrp.c:aes_192_wrappadinv_get_params
Line
Count
Source
304
67
    {                                                                           \
305
67
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
306
67
            flags, kbits, blkbits, ivbits);                                     \
307
67
    }                                                                           \
cipher_aes_wrp.c:aes_128_wrappadinv_get_params
Line
Count
Source
304
67
    {                                                                           \
305
67
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
306
67
            flags, kbits, blkbits, ivbits);                                     \
307
67
    }                                                                           \
308
    static OSSL_FUNC_cipher_newctx_fn aes_##kbits##fname##_newctx;              \
309
    static void *aes_##kbits##fname##_newctx(void *provctx)                     \
310
43
    {                                                                           \
311
43
        return aes_##mode##_newctx(provctx, kbits, blkbits, ivbits,             \
312
43
            EVP_CIPH_##UCMODE##_MODE, flags);                                   \
313
43
    }                                                                           \
cipher_aes_wrp.c:aes_256wrap_newctx
Line
Count
Source
310
11
    {                                                                           \
311
11
        return aes_##mode##_newctx(provctx, kbits, blkbits, ivbits,             \
312
11
            EVP_CIPH_##UCMODE##_MODE, flags);                                   \
313
11
    }                                                                           \
cipher_aes_wrp.c:aes_192wrap_newctx
Line
Count
Source
310
10
    {                                                                           \
311
10
        return aes_##mode##_newctx(provctx, kbits, blkbits, ivbits,             \
312
10
            EVP_CIPH_##UCMODE##_MODE, flags);                                   \
313
10
    }                                                                           \
cipher_aes_wrp.c:aes_128wrap_newctx
Line
Count
Source
310
10
    {                                                                           \
311
10
        return aes_##mode##_newctx(provctx, kbits, blkbits, ivbits,             \
312
10
            EVP_CIPH_##UCMODE##_MODE, flags);                                   \
313
10
    }                                                                           \
cipher_aes_wrp.c:aes_256wrappad_newctx
Line
Count
Source
310
6
    {                                                                           \
311
6
        return aes_##mode##_newctx(provctx, kbits, blkbits, ivbits,             \
312
6
            EVP_CIPH_##UCMODE##_MODE, flags);                                   \
313
6
    }                                                                           \
cipher_aes_wrp.c:aes_192wrappad_newctx
Line
Count
Source
310
2
    {                                                                           \
311
2
        return aes_##mode##_newctx(provctx, kbits, blkbits, ivbits,             \
312
2
            EVP_CIPH_##UCMODE##_MODE, flags);                                   \
313
2
    }                                                                           \
cipher_aes_wrp.c:aes_128wrappad_newctx
Line
Count
Source
310
4
    {                                                                           \
311
4
        return aes_##mode##_newctx(provctx, kbits, blkbits, ivbits,             \
312
4
            EVP_CIPH_##UCMODE##_MODE, flags);                                   \
313
4
    }                                                                           \
Unexecuted instantiation: cipher_aes_wrp.c:aes_256wrapinv_newctx
Unexecuted instantiation: cipher_aes_wrp.c:aes_192wrapinv_newctx
Unexecuted instantiation: cipher_aes_wrp.c:aes_128wrapinv_newctx
Unexecuted instantiation: cipher_aes_wrp.c:aes_256wrappadinv_newctx
Unexecuted instantiation: cipher_aes_wrp.c:aes_192wrappadinv_newctx
Unexecuted instantiation: cipher_aes_wrp.c:aes_128wrappadinv_newctx
314
    const OSSL_DISPATCH ossl_##aes##kbits##fname##_functions[] = {              \
315
        { OSSL_FUNC_CIPHER_NEWCTX,                                              \
316
            (void (*)(void))aes_##kbits##fname##_newctx },                      \
317
        { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))aes_##mode##_einit },  \
318
        { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))aes_##mode##_dinit },  \
319
        { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))aes_##mode##_cipher },       \
320
        { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))aes_##mode##_final },         \
321
        { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))aes_##mode##_freectx },     \
322
        { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))aes_##mode##_dupctx },       \
323
        { OSSL_FUNC_CIPHER_GET_PARAMS,                                          \
324
            (void (*)(void))aes_##kbits##_##fname##_get_params },               \
325
        { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,                                     \
326
            (void (*)(void))ossl_cipher_generic_gettable_params },              \
327
        { OSSL_FUNC_CIPHER_GET_CTX_PARAMS,                                      \
328
            (void (*)(void))ossl_cipher_generic_get_ctx_params },               \
329
        { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,                                 \
330
            (void (*)(void))ossl_cipher_generic_gettable_ctx_params },          \
331
        { OSSL_FUNC_CIPHER_SET_CTX_PARAMS,                                      \
332
            (void (*)(void))aes_wrap_set_ctx_params },                          \
333
        { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,                                 \
334
            (void (*)(void))aes_wrap_settable_ctx_params },                     \
335
        OSSL_DISPATCH_END                                                       \
336
    }
337
338
IMPLEMENT_cipher(wrap, wrap, WRAP, WRAP_FLAGS, 256, 64, AES_WRAP_NOPAD_IVLEN * 8);
339
IMPLEMENT_cipher(wrap, wrap, WRAP, WRAP_FLAGS, 192, 64, AES_WRAP_NOPAD_IVLEN * 8);
340
IMPLEMENT_cipher(wrap, wrap, WRAP, WRAP_FLAGS, 128, 64, AES_WRAP_NOPAD_IVLEN * 8);
341
IMPLEMENT_cipher(wrap, wrappad, WRAP, WRAP_FLAGS, 256, 64, AES_WRAP_PAD_IVLEN * 8);
342
IMPLEMENT_cipher(wrap, wrappad, WRAP, WRAP_FLAGS, 192, 64, AES_WRAP_PAD_IVLEN * 8);
343
IMPLEMENT_cipher(wrap, wrappad, WRAP, WRAP_FLAGS, 128, 64, AES_WRAP_PAD_IVLEN * 8);
344
345
IMPLEMENT_cipher(wrap, wrapinv, WRAP, WRAP_FLAGS_INV, 256, 64, AES_WRAP_NOPAD_IVLEN * 8);
346
IMPLEMENT_cipher(wrap, wrapinv, WRAP, WRAP_FLAGS_INV, 192, 64, AES_WRAP_NOPAD_IVLEN * 8);
347
IMPLEMENT_cipher(wrap, wrapinv, WRAP, WRAP_FLAGS_INV, 128, 64, AES_WRAP_NOPAD_IVLEN * 8);
348
IMPLEMENT_cipher(wrap, wrappadinv, WRAP, WRAP_FLAGS_INV, 256, 64, AES_WRAP_PAD_IVLEN * 8);
349
IMPLEMENT_cipher(wrap, wrappadinv, WRAP, WRAP_FLAGS_INV, 192, 64, AES_WRAP_PAD_IVLEN * 8);
350
IMPLEMENT_cipher(wrap, wrappadinv, WRAP, WRAP_FLAGS_INV, 128, 64, AES_WRAP_PAD_IVLEN * 8);