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