Coverage Report

Created: 2026-03-10 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/providers/implementations/ciphers/cipher_aes_xts.c
Line
Count
Source
1
2
/*
3
 * Copyright 2019-2023 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
72
#define AES_XTS_IV_BITS 128
26
72
#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
70
{
58
70
    if ((!ossl_aes_xts_allow_insecure_decrypt || enc)
59
35
        && CRYPTO_memcmp(key, key + bytes, bytes) == 0) {
60
0
        ERR_raise(ERR_LIB_PROV, PROV_R_XTS_DUPLICATED_KEYS);
61
0
        return 0;
62
0
    }
63
70
    return 1;
64
70
}
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
93
{
77
93
    PROV_AES_XTS_CTX *xctx = (PROV_AES_XTS_CTX *)vctx;
78
93
    PROV_CIPHER_CTX *ctx = &xctx->base;
79
80
93
    if (!ossl_prov_is_running())
81
0
        return 0;
82
83
93
    ctx->enc = enc;
84
85
93
    if (iv != NULL) {
86
23
        if (!ossl_cipher_generic_initiv(vctx, iv, ivlen))
87
0
            return 0;
88
23
    }
89
93
    if (key != NULL) {
90
70
        if (keylen != ctx->keylen) {
91
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
92
0
            return 0;
93
0
        }
94
70
        if (!aes_xts_check_keys_differ(key, keylen / 2, enc))
95
0
            return 0;
96
70
        if (!ctx->hw->init(ctx, key, keylen))
97
0
            return 0;
98
70
    }
99
93
    return aes_xts_set_ctx_params(ctx, params);
100
93
}
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
35
{
106
#ifdef AES_XTS_S390X
107
    if (s390x_aes_xts_einit(vctx, key, keylen, iv, ivlen, params) == 1)
108
        return 1;
109
#endif
110
35
    return aes_xts_init(vctx, key, keylen, iv, ivlen, params, 1);
111
35
}
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
58
{
117
#ifdef AES_XTS_S390X
118
    if (s390x_aes_xts_dinit(vctx, key, keylen, iv, ivlen, params) == 1)
119
        return 1;
120
#endif
121
58
    return aes_xts_init(vctx, key, keylen, iv, ivlen, params, 0);
122
58
}
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
70
{
127
70
    PROV_AES_XTS_CTX *ctx;
128
129
70
    CIPHER_PROV_CHECK(provctx, AES_128_XTS);
130
70
    ctx = OPENSSL_zalloc(sizeof(*ctx));
131
70
    if (ctx != NULL) {
132
70
        ossl_cipher_generic_initkey(&ctx->base, kbits, blkbits, ivbits, mode,
133
70
            flags, ossl_prov_cipher_hw_aes_xts(kbits),
134
70
            NULL);
135
70
    }
136
70
    return ctx;
137
70
}
138
139
static void aes_xts_freectx(void *vctx)
140
70
{
141
70
    PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
142
143
70
    ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
144
70
    OPENSSL_clear_free(ctx, sizeof(*ctx));
145
70
}
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
23
{
178
23
    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
23
    if (!ossl_prov_is_running()
186
23
        || ctx->xts.key1 == NULL
187
23
        || ctx->xts.key2 == NULL
188
23
        || !ctx->base.iv_set
189
23
        || out == NULL
190
23
        || in == NULL
191
23
        || inl < AES_BLOCK_SIZE)
192
0
        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
23
    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
23
    if (ctx->stream != NULL)
206
0
        (*ctx->stream)(in, out, inl, ctx->xts.key1, ctx->xts.key2, ctx->base.iv);
207
23
    else if (CRYPTO_xts128_encrypt(&ctx->xts, ctx->base.iv, in, out, inl,
208
23
                 ctx->base.enc))
209
0
        return 0;
210
211
23
    *outl = inl;
212
23
    return 1;
213
23
}
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
23
{
219
23
    PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
220
221
23
    if (outsize < inl) {
222
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
223
0
        return 0;
224
0
    }
225
226
23
    if (!aes_xts_cipher(ctx, out, outl, outsize, in, inl)) {
227
0
        ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
228
0
        return 0;
229
0
    }
230
231
23
    return 1;
232
23
}
233
234
static int aes_xts_stream_final(void *vctx, unsigned char *out, size_t *outl,
235
    size_t outsize)
236
23
{
237
23
    if (!ossl_prov_is_running())
238
0
        return 0;
239
23
    *outl = 0;
240
23
    return 1;
241
23
}
242
243
static const OSSL_PARAM *aes_xts_settable_ctx_params(ossl_unused void *cctx,
244
    ossl_unused void *provctx)
245
0
{
246
0
    return aes_xts_set_ctx_params_list;
247
0
}
248
249
static int aes_xts_set_ctx_params(void *vctx, const OSSL_PARAM params[])
250
186
{
251
186
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
252
186
    struct aes_xts_set_ctx_params_st p;
253
254
186
    if (ctx == NULL || !aes_xts_set_ctx_params_decoder(params, &p))
255
0
        return 0;
256
257
186
    if (p.keylen != NULL) {
258
0
        size_t keylen;
259
260
0
        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
0
        if (keylen != ctx->keylen)
266
0
            return 0;
267
0
    }
268
269
186
    return 1;
270
186
}
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
2
    {                                                                              \
276
2
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,    \
277
2
            flags, 2 * kbits, AES_XTS_BLOCK_BITS,                                  \
278
2
            AES_XTS_IV_BITS);                                                      \
279
2
    }                                                                              \
cipher_aes_xts.c:aes_256_xts_get_params
Line
Count
Source
275
1
    {                                                                              \
276
1
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,    \
277
1
            flags, 2 * kbits, AES_XTS_BLOCK_BITS,                                  \
278
1
            AES_XTS_IV_BITS);                                                      \
279
1
    }                                                                              \
cipher_aes_xts.c:aes_128_xts_get_params
Line
Count
Source
275
1
    {                                                                              \
276
1
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,    \
277
1
            flags, 2 * kbits, AES_XTS_BLOCK_BITS,                                  \
278
1
            AES_XTS_IV_BITS);                                                      \
279
1
    }                                                                              \
280
    static OSSL_FUNC_cipher_newctx_fn aes_##kbits##_xts_newctx;                    \
281
    static void *aes_##kbits##_xts_newctx(void *provctx)                           \
282
70
    {                                                                              \
283
70
        return aes_xts_newctx(provctx, EVP_CIPH_##UCMODE##_MODE, flags, 2 * kbits, \
284
70
            AES_XTS_BLOCK_BITS, AES_XTS_IV_BITS);                                  \
285
70
    }                                                                              \
Unexecuted instantiation: cipher_aes_xts.c:aes_256_xts_newctx
cipher_aes_xts.c:aes_128_xts_newctx
Line
Count
Source
282
70
    {                                                                              \
283
70
        return aes_xts_newctx(provctx, EVP_CIPH_##UCMODE##_MODE, flags, 2 * kbits, \
284
70
            AES_XTS_BLOCK_BITS, AES_XTS_IV_BITS);                                  \
285
70
    }                                                                              \
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);