Coverage Report

Created: 2026-02-11 06:19

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