Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl40/providers/implementations/ciphers/cipher_aes_xts.c
Line
Count
Source
1
2
/*
3
 * Copyright 2019-2026 The OpenSSL Project Authors. All Rights Reserved.
4
 *
5
 * Licensed under the Apache License 2.0 (the "License").  You may not use
6
 * this file except in compliance with the License.  You can obtain a copy
7
 * in the file LICENSE in the source distribution or at
8
 * https://www.openssl.org/source/license.html
9
 */
10
11
/*
12
 * AES low level APIs are deprecated for public use, but still ok for internal
13
 * use where we're using them to implement the higher level EVP interface, as is
14
 * the case here.
15
 */
16
#include "internal/deprecated.h"
17
18
#include <openssl/proverr.h>
19
#include "cipher_aes_xts.h"
20
#include "prov/implementations.h"
21
#include "prov/providercommon.h"
22
#include "providers/implementations/ciphers/cipher_aes_xts.inc"
23
24
#define AES_XTS_FLAGS PROV_CIPHER_FLAG_CUSTOM_IV
25
221
#define AES_XTS_IV_BITS 128
26
221
#define AES_XTS_BLOCK_BITS 8
27
28
/* forward declarations */
29
static OSSL_FUNC_cipher_encrypt_init_fn aes_xts_einit;
30
static OSSL_FUNC_cipher_decrypt_init_fn aes_xts_dinit;
31
static OSSL_FUNC_cipher_update_fn aes_xts_stream_update;
32
static OSSL_FUNC_cipher_final_fn aes_xts_stream_final;
33
static OSSL_FUNC_cipher_cipher_fn aes_xts_cipher;
34
static OSSL_FUNC_cipher_freectx_fn aes_xts_freectx;
35
static OSSL_FUNC_cipher_dupctx_fn aes_xts_dupctx;
36
static OSSL_FUNC_cipher_set_ctx_params_fn aes_xts_set_ctx_params;
37
static OSSL_FUNC_cipher_settable_ctx_params_fn aes_xts_settable_ctx_params;
38
39
/*
40
 * Verify that the two keys are different.
41
 *
42
 * This addresses the vulnerability described in Rogaway's
43
 * September 2004 paper:
44
 *
45
 *      "Efficient Instantiations of Tweakable Blockciphers and
46
 *       Refinements to Modes OCB and PMAC".
47
 *      (http://web.cs.ucdavis.edu/~rogaway/papers/offsets.pdf)
48
 *
49
 * FIPS 140-2 IG A.9 XTS-AES Key Generation Requirements states
50
 * that:
51
 *      "The check for Key_1 != Key_2 shall be done at any place
52
 *       BEFORE using the keys in the XTS-AES algorithm to process
53
 *       data with them."
54
 */
55
static int aes_xts_check_keys_differ(const unsigned char *key, size_t bytes,
56
    int enc)
57
49
{
58
49
    if ((!ossl_aes_xts_allow_insecure_decrypt || enc)
59
49
        && CRYPTO_memcmp(key, key + bytes, bytes) == 0) {
60
2
        ERR_raise(ERR_LIB_PROV, PROV_R_XTS_DUPLICATED_KEYS);
61
2
        return 0;
62
2
    }
63
47
    return 1;
64
49
}
65
66
#ifdef AES_XTS_S390X
67
#include "cipher_aes_xts_s390x.inc"
68
#endif
69
70
/*-
71
 * Provider dispatch functions
72
 */
73
static int aes_xts_init(void *vctx, const unsigned char *key, size_t keylen,
74
    const unsigned char *iv, size_t ivlen,
75
    const OSSL_PARAM params[], int enc)
76
79
{
77
79
    PROV_AES_XTS_CTX *xctx = (PROV_AES_XTS_CTX *)vctx;
78
79
    PROV_CIPHER_CTX *ctx = &xctx->base;
79
80
79
    if (!ossl_prov_is_running())
81
0
        return 0;
82
83
79
    ctx->enc = enc;
84
85
79
    if (iv != NULL) {
86
21
        if (!ossl_cipher_generic_initiv(vctx, iv, ivlen))
87
0
            return 0;
88
21
    }
89
79
    if (key != NULL) {
90
49
        if (keylen != ctx->keylen) {
91
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
92
0
            return 0;
93
0
        }
94
49
        if (!aes_xts_check_keys_differ(key, keylen / 2, enc))
95
2
            return 0;
96
47
        if (!ctx->hw->init(ctx, key, keylen))
97
0
            return 0;
98
47
    }
99
77
    return aes_xts_set_ctx_params(ctx, params);
100
79
}
101
102
static int aes_xts_einit(void *vctx, const unsigned char *key, size_t keylen,
103
    const unsigned char *iv, size_t ivlen,
104
    const OSSL_PARAM params[])
105
79
{
106
#ifdef AES_XTS_S390X
107
    if (s390x_aes_xts_einit(vctx, key, keylen, iv, ivlen, params) == 1)
108
        return 1;
109
#endif
110
79
    return aes_xts_init(vctx, key, keylen, iv, ivlen, params, 1);
111
79
}
112
113
static int aes_xts_dinit(void *vctx, const unsigned char *key, size_t keylen,
114
    const unsigned char *iv, size_t ivlen,
115
    const OSSL_PARAM params[])
116
0
{
117
#ifdef AES_XTS_S390X
118
    if (s390x_aes_xts_dinit(vctx, key, keylen, iv, ivlen, params) == 1)
119
        return 1;
120
#endif
121
0
    return aes_xts_init(vctx, key, keylen, iv, ivlen, params, 0);
122
0
}
123
124
static void *aes_xts_newctx(void *provctx, unsigned int mode, uint64_t flags,
125
    size_t kbits, size_t blkbits, size_t ivbits)
126
51
{
127
51
    PROV_AES_XTS_CTX *ctx;
128
129
51
    CIPHER_PROV_CHECK(provctx, AES_128_XTS);
130
51
    ctx = OPENSSL_zalloc(sizeof(*ctx));
131
51
    if (ctx != NULL) {
132
51
        ossl_cipher_generic_initkey(&ctx->base, kbits, blkbits, ivbits, mode,
133
51
            flags, ossl_prov_cipher_hw_aes_xts(kbits),
134
51
            NULL);
135
51
    }
136
51
    return ctx;
137
51
}
138
139
static void aes_xts_freectx(void *vctx)
140
51
{
141
51
    PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
142
143
51
    ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
144
51
    OPENSSL_clear_free(ctx, sizeof(*ctx));
145
51
}
146
147
static void *aes_xts_dupctx(void *vctx)
148
0
{
149
0
    PROV_AES_XTS_CTX *in = (PROV_AES_XTS_CTX *)vctx;
150
0
    PROV_AES_XTS_CTX *ret = NULL;
151
152
0
    if (!ossl_prov_is_running())
153
0
        return NULL;
154
155
#ifdef AES_XTS_S390X
156
    if (in->plat.s390x.fc)
157
        return s390x_aes_xts_dupctx(vctx);
158
#endif
159
160
0
    if (in->xts.key1 != NULL) {
161
0
        if (in->xts.key1 != &in->ks1)
162
0
            return NULL;
163
0
    }
164
0
    if (in->xts.key2 != NULL) {
165
0
        if (in->xts.key2 != &in->ks2)
166
0
            return NULL;
167
0
    }
168
0
    ret = OPENSSL_malloc(sizeof(*ret));
169
0
    if (ret == NULL)
170
0
        return NULL;
171
0
    in->base.hw->copyctx(&ret->base, &in->base);
172
0
    return ret;
173
0
}
174
175
static int aes_xts_cipher(void *vctx, unsigned char *out, size_t *outl,
176
    size_t outsize, const unsigned char *in, size_t inl)
177
15
{
178
15
    PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
179
180
#ifdef AES_XTS_S390X
181
    if (ctx->plat.s390x.fc)
182
        return s390x_aes_xts_cipher(vctx, out, outl, outsize, in, inl);
183
#endif
184
185
15
    if (!ossl_prov_is_running()
186
15
        || ctx->xts.key1 == NULL
187
15
        || ctx->xts.key2 == NULL
188
15
        || !ctx->base.iv_set
189
0
        || out == NULL
190
0
        || in == NULL
191
0
        || inl < AES_BLOCK_SIZE)
192
15
        return 0;
193
194
    /*
195
     * Impose a limit of 2^20 blocks per data unit as specified by
196
     * IEEE Std 1619-2018.  The earlier and obsolete IEEE Std 1619-2007
197
     * indicated that this was a SHOULD NOT rather than a MUST NOT.
198
     * NIST SP 800-38E mandates the same limit.
199
     */
200
0
    if (inl > XTS_MAX_BLOCKS_PER_DATA_UNIT * AES_BLOCK_SIZE) {
201
0
        ERR_raise(ERR_LIB_PROV, PROV_R_XTS_DATA_UNIT_IS_TOO_LARGE);
202
0
        return 0;
203
0
    }
204
205
0
    if (ctx->stream != NULL)
206
0
        (*ctx->stream)(in, out, inl, ctx->xts.key1, ctx->xts.key2, ctx->base.iv);
207
0
    else if (CRYPTO_xts128_encrypt(&ctx->xts, ctx->base.iv, in, out, inl,
208
0
                 ctx->base.enc))
209
0
        return 0;
210
211
0
    *outl = inl;
212
0
    return 1;
213
0
}
214
215
static int aes_xts_stream_update(void *vctx, unsigned char *out, size_t *outl,
216
    size_t outsize, const unsigned char *in,
217
    size_t inl)
218
17
{
219
17
    PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
220
221
17
    if (outsize < inl) {
222
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
223
0
        return 0;
224
0
    }
225
226
17
    if (!aes_xts_cipher(ctx, out, outl, outsize, in, inl)) {
227
17
        ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
228
17
        return 0;
229
17
    }
230
231
0
    return 1;
232
17
}
233
234
static int aes_xts_stream_final(void *vctx, unsigned char *out, size_t *outl,
235
    size_t outsize)
236
0
{
237
0
    if (!ossl_prov_is_running())
238
0
        return 0;
239
0
    *outl = 0;
240
0
    return 1;
241
0
}
242
243
static const OSSL_PARAM *aes_xts_settable_ctx_params(ossl_unused void *cctx,
244
    ossl_unused void *provctx)
245
30
{
246
30
    return aes_xts_set_ctx_params_list;
247
30
}
248
249
static int aes_xts_set_ctx_params(void *vctx, const OSSL_PARAM params[])
250
78
{
251
78
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
252
78
    struct aes_xts_set_ctx_params_st p;
253
254
78
    if (ctx == NULL || !aes_xts_set_ctx_params_decoder(params, &p))
255
0
        return 0;
256
257
78
    if (p.keylen != NULL) {
258
22
        size_t keylen;
259
260
22
        if (!OSSL_PARAM_get_size_t(p.keylen, &keylen)) {
261
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
262
0
            return 0;
263
0
        }
264
        /* The key length can not be modified for xts mode */
265
22
        if (keylen != ctx->keylen)
266
20
            return 0;
267
22
    }
268
269
58
    return 1;
270
78
}
271
272
#define IMPLEMENT_cipher(lcmode, UCMODE, kbits, flags)                             \
273
    static OSSL_FUNC_cipher_get_params_fn aes_##kbits##_##lcmode##_get_params;     \
274
    static int aes_##kbits##_##lcmode##_get_params(OSSL_PARAM params[])            \
275
170
    {                                                                              \
276
170
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,    \
277
170
            flags, 2 * kbits, AES_XTS_BLOCK_BITS,                                  \
278
170
            AES_XTS_IV_BITS);                                                      \
279
170
    }                                                                              \
cipher_aes_xts.c:aes_256_xts_get_params
Line
Count
Source
275
85
    {                                                                              \
276
85
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,    \
277
85
            flags, 2 * kbits, AES_XTS_BLOCK_BITS,                                  \
278
85
            AES_XTS_IV_BITS);                                                      \
279
85
    }                                                                              \
cipher_aes_xts.c:aes_128_xts_get_params
Line
Count
Source
275
85
    {                                                                              \
276
85
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,    \
277
85
            flags, 2 * kbits, AES_XTS_BLOCK_BITS,                                  \
278
85
            AES_XTS_IV_BITS);                                                      \
279
85
    }                                                                              \
280
    static OSSL_FUNC_cipher_newctx_fn aes_##kbits##_xts_newctx;                    \
281
    static void *aes_##kbits##_xts_newctx(void *provctx)                           \
282
51
    {                                                                              \
283
51
        return aes_xts_newctx(provctx, EVP_CIPH_##UCMODE##_MODE, flags, 2 * kbits, \
284
51
            AES_XTS_BLOCK_BITS, AES_XTS_IV_BITS);                                  \
285
51
    }                                                                              \
cipher_aes_xts.c:aes_256_xts_newctx
Line
Count
Source
282
8
    {                                                                              \
283
8
        return aes_xts_newctx(provctx, EVP_CIPH_##UCMODE##_MODE, flags, 2 * kbits, \
284
8
            AES_XTS_BLOCK_BITS, AES_XTS_IV_BITS);                                  \
285
8
    }                                                                              \
cipher_aes_xts.c:aes_128_xts_newctx
Line
Count
Source
282
43
    {                                                                              \
283
43
        return aes_xts_newctx(provctx, EVP_CIPH_##UCMODE##_MODE, flags, 2 * kbits, \
284
43
            AES_XTS_BLOCK_BITS, AES_XTS_IV_BITS);                                  \
285
43
    }                                                                              \
286
    const OSSL_DISPATCH ossl_aes##kbits##xts_functions[] = {                       \
287
        { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))aes_##kbits##_xts_newctx },     \
288
        { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))aes_xts_einit },          \
289
        { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))aes_xts_dinit },          \
290
        { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))aes_xts_stream_update },        \
291
        { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))aes_xts_stream_final },          \
292
        { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))aes_xts_cipher },               \
293
        { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))aes_xts_freectx },             \
294
        { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))aes_xts_dupctx },               \
295
        { OSSL_FUNC_CIPHER_GET_PARAMS,                                             \
296
            (void (*)(void))aes_##kbits##_##lcmode##_get_params },                 \
297
        { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,                                        \
298
            (void (*)(void))ossl_cipher_generic_gettable_params },                 \
299
        { OSSL_FUNC_CIPHER_GET_CTX_PARAMS,                                         \
300
            (void (*)(void))ossl_cipher_generic_get_ctx_params },                  \
301
        { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,                                    \
302
            (void (*)(void))ossl_cipher_generic_gettable_ctx_params },             \
303
        { OSSL_FUNC_CIPHER_SET_CTX_PARAMS,                                         \
304
            (void (*)(void))aes_xts_set_ctx_params },                              \
305
        { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,                                    \
306
            (void (*)(void))aes_xts_settable_ctx_params },                         \
307
        OSSL_DISPATCH_END                                                          \
308
    }
309
310
IMPLEMENT_cipher(xts, XTS, 256, AES_XTS_FLAGS);
311
IMPLEMENT_cipher(xts, XTS, 128, AES_XTS_FLAGS);