Coverage Report

Created: 2025-10-12 06:56

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
/*
12
 * This file uses the low level AES functions (which are deprecated for
13
 * non-internal use) in order to implement provider AES ciphers.
14
 */
15
#include "internal/deprecated.h"
16
17
#include <openssl/proverr.h>
18
#include "cipher_aes.h"
19
#include "prov/providercommon.h"
20
#include "prov/implementations.h"
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
48
} PROV_AES_WRAP_CTX;
49
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
    ctx->enc = enc;
112
0
    if (ctx->pad)
113
0
        wctx->wrapfn = enc ? CRYPTO_128_wrap_pad : CRYPTO_128_unwrap_pad;
114
0
    else
115
0
        wctx->wrapfn = enc ? CRYPTO_128_wrap : CRYPTO_128_unwrap;
116
117
0
    if (iv != NULL) {
118
0
        if (!ossl_cipher_generic_initiv(ctx, iv, ivlen))
119
0
            return 0;
120
0
    }
121
0
    if (key != NULL) {
122
0
        int use_forward_transform;
123
124
0
        if (keylen != ctx->keylen) {
125
0
           ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
126
0
           return 0;
127
0
        }
128
        /*
129
         * See SP800-38F : Section 5.1
130
         * The forward and inverse transformations for the AES block
131
         * cipher—called “cipher” and “inverse  cipher” are informally known as
132
         * the AES encryption and AES decryption functions, respectively.
133
         * If the designated cipher function for a key-wrap algorithm is chosen
134
         * to be the AES decryption function, then CIPH-1K will be the AES
135
         * encryption function.
136
         */
137
0
        if (ctx->inverse_cipher == 0)
138
0
            use_forward_transform = ctx->enc;
139
0
        else
140
0
            use_forward_transform = !ctx->enc;
141
0
        if (use_forward_transform) {
142
0
            AES_set_encrypt_key(key, (int)(keylen * 8), &wctx->ks.ks);
143
0
            ctx->block = (block128_f)AES_encrypt;
144
0
        } else {
145
0
            AES_set_decrypt_key(key, (int)(keylen * 8), &wctx->ks.ks);
146
0
            ctx->block = (block128_f)AES_decrypt;
147
0
        }
148
0
    }
149
0
    return aes_wrap_set_ctx_params(ctx, params);
150
0
}
151
152
static int aes_wrap_einit(void *ctx, const unsigned char *key, size_t keylen,
153
                          const unsigned char *iv, size_t ivlen,
154
                          const OSSL_PARAM params[])
155
0
{
156
0
    return aes_wrap_init(ctx, key, keylen, iv, ivlen, params, 1);
157
0
}
158
159
static int aes_wrap_dinit(void *ctx, const unsigned char *key, size_t keylen,
160
                          const unsigned char *iv, size_t ivlen,
161
                          const OSSL_PARAM params[])
162
0
{
163
0
    return aes_wrap_init(ctx, key, keylen, iv, ivlen, params, 0);
164
0
}
165
166
static int aes_wrap_cipher_internal(void *vctx, unsigned char *out,
167
                                    const unsigned char *in, size_t inlen)
168
0
{
169
0
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
170
0
    PROV_AES_WRAP_CTX *wctx = (PROV_AES_WRAP_CTX *)vctx;
171
0
    size_t rv;
172
0
    int pad = ctx->pad;
173
174
    /* No final operation so always return zero length */
175
0
    if (in == NULL)
176
0
        return 0;
177
178
    /* Input length must always be non-zero */
179
0
    if (inlen == 0 || inlen > INT_MAX) {
180
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_INPUT_LENGTH);
181
0
        return -1;
182
0
    }
183
184
    /* If decrypting need at least 16 bytes and multiple of 8 */
185
0
    if (!ctx->enc && (inlen < 16 || inlen & 0x7)) {
186
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_INPUT_LENGTH);
187
0
        return -1;
188
0
    }
189
190
    /* If not padding input must be multiple of 8 */
191
0
    if (!pad && inlen & 0x7) {
192
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_INPUT_LENGTH);
193
0
        return -1;
194
0
    }
195
196
0
    if (out == NULL) {
197
0
        if (ctx->enc) {
198
            /* If padding round up to multiple of 8 */
199
0
            if (pad)
200
0
                inlen = (inlen + 7) / 8 * 8;
201
            /* 8 byte prefix */
202
0
            return (int)(inlen + 8);
203
0
        } else {
204
            /*
205
             * If not padding output will be exactly 8 bytes smaller than
206
             * input. If padding it will be at least 8 bytes smaller but we
207
             * don't know how much.
208
             */
209
0
            return (int)(inlen - 8);
210
0
        }
211
0
    }
212
213
0
    rv = wctx->wrapfn(&wctx->ks.ks, ctx->iv_set ? ctx->iv : NULL, out, in,
214
0
                      inlen, ctx->block);
215
0
    if (!rv) {
216
0
        ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
217
0
        return -1;
218
0
    }
219
0
    if (rv > INT_MAX) {
220
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_OUTPUT_LENGTH);
221
0
        return -1;
222
0
    }
223
0
    return (int)rv;
224
0
}
225
226
static int aes_wrap_final(void *vctx, unsigned char *out, size_t *outl,
227
                          size_t outsize)
228
0
{
229
0
    if (!ossl_prov_is_running())
230
0
        return 0;
231
232
0
    *outl = 0;
233
0
    return 1;
234
0
}
235
236
static int aes_wrap_cipher(void *vctx,
237
                           unsigned char *out, size_t *outl, size_t outsize,
238
                           const unsigned char *in, size_t inl)
239
0
{
240
0
    PROV_AES_WRAP_CTX *ctx = (PROV_AES_WRAP_CTX *)vctx;
241
0
    size_t len;
242
243
0
    if (!ossl_prov_is_running())
244
0
        return 0;
245
246
0
    if (inl == 0) {
247
0
        *outl = 0;
248
0
        return 1;
249
0
    }
250
251
0
    if (outsize < inl) {
252
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
253
0
        return 0;
254
0
    }
255
256
0
    len = aes_wrap_cipher_internal(ctx, out, in, inl);
257
0
    if (len <= 0)
258
0
        return 0;
259
260
0
    *outl = len;
261
0
    return 1;
262
0
}
263
264
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
265
#ifndef aes_wrap_set_ctx_params_list
266
static const OSSL_PARAM aes_wrap_set_ctx_params_list[] = {
267
    OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
268
    OSSL_PARAM_END
269
};
270
#endif
271
272
#ifndef aes_wrap_set_ctx_params_st
273
struct aes_wrap_set_ctx_params_st {
274
    OSSL_PARAM *keylen;
275
};
276
#endif
277
278
#ifndef aes_wrap_set_ctx_params_decoder
279
static int aes_wrap_set_ctx_params_decoder
280
    (const OSSL_PARAM *p, struct aes_wrap_set_ctx_params_st *r)
281
0
{
282
0
    const char *s;
283
284
0
    memset(r, 0, sizeof(*r));
285
0
    if (p != NULL)
286
0
        for (; (s = p->key) != NULL; p++)
287
0
            if (ossl_likely(strcmp("keylen", s + 0) == 0)) {
288
                /* OSSL_CIPHER_PARAM_KEYLEN */
289
0
                if (ossl_unlikely(r->keylen != NULL)) {
290
0
                    ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
291
0
                                   "param %s is repeated", s);
292
0
                    return 0;
293
0
                }
294
0
                r->keylen = (OSSL_PARAM *)p;
295
0
            }
296
0
    return 1;
297
0
}
298
#endif
299
/* End of machine generated */
300
301
static const OSSL_PARAM *aes_wrap_settable_ctx_params(ossl_unused void *cctx,
302
                                                      ossl_unused void *provctx)
303
0
{
304
0
    return aes_wrap_set_ctx_params_list;
305
0
}
306
307
static int aes_wrap_set_ctx_params(void *vctx, const OSSL_PARAM params[])
308
0
{
309
0
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
310
0
    struct aes_wrap_set_ctx_params_st p;
311
0
    size_t keylen = 0;
312
313
0
    if (ctx == NULL || !aes_wrap_set_ctx_params_decoder(params, &p))
314
0
        return 0;
315
316
0
    if (p.keylen != NULL) {
317
0
        if (!OSSL_PARAM_get_size_t(p.keylen, &keylen)) {
318
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
319
0
            return 0;
320
0
        }
321
0
        if (ctx->keylen != keylen) {
322
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
323
0
            return 0;
324
0
        }
325
0
    }
326
0
    return 1;
327
0
}
328
329
#define IMPLEMENT_cipher(mode, fname, UCMODE, flags, kbits, blkbits, ivbits)   \
330
    static OSSL_FUNC_cipher_get_params_fn aes_##kbits##_##fname##_get_params;  \
331
    static int aes_##kbits##_##fname##_get_params(OSSL_PARAM params[])         \
332
12
    {                                                                          \
333
12
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,\
334
12
                                              flags, kbits, blkbits, ivbits);  \
335
12
    }                                                                          \
cipher_aes_wrp.c:aes_256_wrap_get_params
Line
Count
Source
332
1
    {                                                                          \
333
1
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,\
334
1
                                              flags, kbits, blkbits, ivbits);  \
335
1
    }                                                                          \
cipher_aes_wrp.c:aes_192_wrap_get_params
Line
Count
Source
332
1
    {                                                                          \
333
1
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,\
334
1
                                              flags, kbits, blkbits, ivbits);  \
335
1
    }                                                                          \
cipher_aes_wrp.c:aes_128_wrap_get_params
Line
Count
Source
332
1
    {                                                                          \
333
1
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,\
334
1
                                              flags, kbits, blkbits, ivbits);  \
335
1
    }                                                                          \
cipher_aes_wrp.c:aes_256_wrappad_get_params
Line
Count
Source
332
1
    {                                                                          \
333
1
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,\
334
1
                                              flags, kbits, blkbits, ivbits);  \
335
1
    }                                                                          \
cipher_aes_wrp.c:aes_192_wrappad_get_params
Line
Count
Source
332
1
    {                                                                          \
333
1
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,\
334
1
                                              flags, kbits, blkbits, ivbits);  \
335
1
    }                                                                          \
cipher_aes_wrp.c:aes_128_wrappad_get_params
Line
Count
Source
332
1
    {                                                                          \
333
1
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,\
334
1
                                              flags, kbits, blkbits, ivbits);  \
335
1
    }                                                                          \
cipher_aes_wrp.c:aes_256_wrapinv_get_params
Line
Count
Source
332
1
    {                                                                          \
333
1
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,\
334
1
                                              flags, kbits, blkbits, ivbits);  \
335
1
    }                                                                          \
cipher_aes_wrp.c:aes_192_wrapinv_get_params
Line
Count
Source
332
1
    {                                                                          \
333
1
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,\
334
1
                                              flags, kbits, blkbits, ivbits);  \
335
1
    }                                                                          \
cipher_aes_wrp.c:aes_128_wrapinv_get_params
Line
Count
Source
332
1
    {                                                                          \
333
1
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,\
334
1
                                              flags, kbits, blkbits, ivbits);  \
335
1
    }                                                                          \
cipher_aes_wrp.c:aes_256_wrappadinv_get_params
Line
Count
Source
332
1
    {                                                                          \
333
1
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,\
334
1
                                              flags, kbits, blkbits, ivbits);  \
335
1
    }                                                                          \
cipher_aes_wrp.c:aes_192_wrappadinv_get_params
Line
Count
Source
332
1
    {                                                                          \
333
1
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,\
334
1
                                              flags, kbits, blkbits, ivbits);  \
335
1
    }                                                                          \
cipher_aes_wrp.c:aes_128_wrappadinv_get_params
Line
Count
Source
332
1
    {                                                                          \
333
1
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,\
334
1
                                              flags, kbits, blkbits, ivbits);  \
335
1
    }                                                                          \
336
    static OSSL_FUNC_cipher_newctx_fn aes_##kbits##fname##_newctx;             \
337
    static void *aes_##kbits##fname##_newctx(void *provctx)                    \
338
0
    {                                                                          \
339
0
        return aes_##mode##_newctx(kbits, blkbits, ivbits,                     \
340
0
                                   EVP_CIPH_##UCMODE##_MODE, flags);           \
341
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
342
    const OSSL_DISPATCH ossl_##aes##kbits##fname##_functions[] = {             \
343
        { OSSL_FUNC_CIPHER_NEWCTX,                                             \
344
            (void (*)(void))aes_##kbits##fname##_newctx },                     \
345
        { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))aes_##mode##_einit }, \
346
        { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))aes_##mode##_dinit }, \
347
        { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))aes_##mode##_cipher },      \
348
        { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))aes_##mode##_final },        \
349
        { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))aes_##mode##_freectx },    \
350
        { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))aes_##mode##_dupctx },      \
351
        { OSSL_FUNC_CIPHER_GET_PARAMS,                                         \
352
            (void (*)(void))aes_##kbits##_##fname##_get_params },              \
353
        { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,                                    \
354
            (void (*)(void))ossl_cipher_generic_gettable_params },             \
355
        { OSSL_FUNC_CIPHER_GET_CTX_PARAMS,                                     \
356
            (void (*)(void))ossl_cipher_generic_get_ctx_params },              \
357
        { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,                                \
358
            (void (*)(void))ossl_cipher_generic_gettable_ctx_params },         \
359
        { OSSL_FUNC_CIPHER_SET_CTX_PARAMS,                                     \
360
            (void (*)(void))aes_wrap_set_ctx_params },                         \
361
        { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,                                \
362
            (void (*)(void))aes_wrap_settable_ctx_params },                    \
363
        OSSL_DISPATCH_END                                                      \
364
    }
365
366
IMPLEMENT_cipher(wrap, wrap, WRAP, WRAP_FLAGS, 256, 64, AES_WRAP_NOPAD_IVLEN * 8);
367
IMPLEMENT_cipher(wrap, wrap, WRAP, WRAP_FLAGS, 192, 64, AES_WRAP_NOPAD_IVLEN * 8);
368
IMPLEMENT_cipher(wrap, wrap, WRAP, WRAP_FLAGS, 128, 64, AES_WRAP_NOPAD_IVLEN * 8);
369
IMPLEMENT_cipher(wrap, wrappad, WRAP, WRAP_FLAGS, 256, 64, AES_WRAP_PAD_IVLEN * 8);
370
IMPLEMENT_cipher(wrap, wrappad, WRAP, WRAP_FLAGS, 192, 64, AES_WRAP_PAD_IVLEN * 8);
371
IMPLEMENT_cipher(wrap, wrappad, WRAP, WRAP_FLAGS, 128, 64, AES_WRAP_PAD_IVLEN * 8);
372
373
IMPLEMENT_cipher(wrap, wrapinv, WRAP, WRAP_FLAGS_INV, 256, 64, AES_WRAP_NOPAD_IVLEN * 8);
374
IMPLEMENT_cipher(wrap, wrapinv, WRAP, WRAP_FLAGS_INV, 192, 64, AES_WRAP_NOPAD_IVLEN * 8);
375
IMPLEMENT_cipher(wrap, wrapinv, WRAP, WRAP_FLAGS_INV, 128, 64, AES_WRAP_NOPAD_IVLEN * 8);
376
IMPLEMENT_cipher(wrap, wrappadinv, WRAP, WRAP_FLAGS_INV, 256, 64, AES_WRAP_PAD_IVLEN * 8);
377
IMPLEMENT_cipher(wrap, wrappadinv, WRAP, WRAP_FLAGS_INV, 192, 64, AES_WRAP_PAD_IVLEN * 8);
378
IMPLEMENT_cipher(wrap, wrappadinv, WRAP, WRAP_FLAGS_INV, 128, 64, AES_WRAP_PAD_IVLEN * 8);