Coverage Report

Created: 2025-12-10 06:24

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
70
#define AES_XTS_IV_BITS 128
26
70
#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
68
{
58
68
    if ((!ossl_aes_xts_allow_insecure_decrypt || enc)
59
34
        && 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
68
    return 1;
64
68
}
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
91
{
77
91
    PROV_AES_XTS_CTX *xctx = (PROV_AES_XTS_CTX *)vctx;
78
91
    PROV_CIPHER_CTX *ctx = &xctx->base;
79
80
91
    if (!ossl_prov_is_running())
81
0
        return 0;
82
83
91
    ctx->enc = enc;
84
85
91
    if (iv != NULL) {
86
23
        if (!ossl_cipher_generic_initiv(vctx, iv, ivlen))
87
0
            return 0;
88
23
    }
89
91
    if (key != NULL) {
90
68
        if (keylen != ctx->keylen) {
91
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
92
0
            return 0;
93
0
        }
94
68
        if (!aes_xts_check_keys_differ(key, keylen / 2, enc))
95
0
            return 0;
96
68
        if (!ctx->hw->init(ctx, key, keylen))
97
0
            return 0;
98
68
    }
99
91
    return aes_xts_set_ctx_params(ctx, params);
100
91
}
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
34
{
106
#ifdef AES_XTS_S390X
107
    if (s390x_aes_xts_einit(vctx, key, keylen, iv, ivlen, params) == 1)
108
        return 1;
109
#endif
110
34
    return aes_xts_init(vctx, key, keylen, iv, ivlen, params, 1);
111
34
}
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
57
{
117
#ifdef AES_XTS_S390X
118
    if (s390x_aes_xts_dinit(vctx, key, keylen, iv, ivlen, params) == 1)
119
        return 1;
120
#endif
121
57
    return aes_xts_init(vctx, key, keylen, iv, ivlen, params, 0);
122
57
}
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
68
{
127
68
    PROV_AES_XTS_CTX *ctx;
128
129
68
    if (!ossl_prov_is_running())
130
0
        return NULL;
131
132
68
    ctx = OPENSSL_zalloc(sizeof(*ctx));
133
68
    if (ctx != NULL) {
134
68
        ossl_cipher_generic_initkey(&ctx->base, kbits, blkbits, ivbits, mode,
135
68
            flags, ossl_prov_cipher_hw_aes_xts(kbits),
136
68
            NULL);
137
68
    }
138
68
    return ctx;
139
68
}
140
141
static void aes_xts_freectx(void *vctx)
142
68
{
143
68
    PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
144
145
68
    ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
146
68
    OPENSSL_clear_free(ctx, sizeof(*ctx));
147
68
}
148
149
static void *aes_xts_dupctx(void *vctx)
150
0
{
151
0
    PROV_AES_XTS_CTX *in = (PROV_AES_XTS_CTX *)vctx;
152
0
    PROV_AES_XTS_CTX *ret = NULL;
153
154
0
    if (!ossl_prov_is_running())
155
0
        return NULL;
156
157
#ifdef AES_XTS_S390X
158
    if (in->plat.s390x.fc)
159
        return s390x_aes_xts_dupctx(vctx);
160
#endif
161
162
0
    if (in->xts.key1 != NULL) {
163
0
        if (in->xts.key1 != &in->ks1)
164
0
            return NULL;
165
0
    }
166
0
    if (in->xts.key2 != NULL) {
167
0
        if (in->xts.key2 != &in->ks2)
168
0
            return NULL;
169
0
    }
170
0
    ret = OPENSSL_malloc(sizeof(*ret));
171
0
    if (ret == NULL)
172
0
        return NULL;
173
0
    in->base.hw->copyctx(&ret->base, &in->base);
174
0
    return ret;
175
0
}
176
177
static int aes_xts_cipher(void *vctx, unsigned char *out, size_t *outl,
178
    size_t outsize, const unsigned char *in, size_t inl)
179
23
{
180
23
    PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
181
182
#ifdef AES_XTS_S390X
183
    if (ctx->plat.s390x.fc)
184
        return s390x_aes_xts_cipher(vctx, out, outl, outsize, in, inl);
185
#endif
186
187
23
    if (!ossl_prov_is_running()
188
23
        || ctx->xts.key1 == NULL
189
23
        || ctx->xts.key2 == NULL
190
23
        || !ctx->base.iv_set
191
23
        || out == NULL
192
23
        || in == NULL
193
23
        || inl < AES_BLOCK_SIZE)
194
0
        return 0;
195
196
    /*
197
     * Impose a limit of 2^20 blocks per data unit as specified by
198
     * IEEE Std 1619-2018.  The earlier and obsolete IEEE Std 1619-2007
199
     * indicated that this was a SHOULD NOT rather than a MUST NOT.
200
     * NIST SP 800-38E mandates the same limit.
201
     */
202
23
    if (inl > XTS_MAX_BLOCKS_PER_DATA_UNIT * AES_BLOCK_SIZE) {
203
0
        ERR_raise(ERR_LIB_PROV, PROV_R_XTS_DATA_UNIT_IS_TOO_LARGE);
204
0
        return 0;
205
0
    }
206
207
23
    if (ctx->stream != NULL)
208
0
        (*ctx->stream)(in, out, inl, ctx->xts.key1, ctx->xts.key2, ctx->base.iv);
209
23
    else if (CRYPTO_xts128_encrypt(&ctx->xts, ctx->base.iv, in, out, inl,
210
23
                 ctx->base.enc))
211
0
        return 0;
212
213
23
    *outl = inl;
214
23
    return 1;
215
23
}
216
217
static int aes_xts_stream_update(void *vctx, unsigned char *out, size_t *outl,
218
    size_t outsize, const unsigned char *in,
219
    size_t inl)
220
23
{
221
23
    PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
222
223
23
    if (outsize < inl) {
224
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
225
0
        return 0;
226
0
    }
227
228
23
    if (!aes_xts_cipher(ctx, out, outl, outsize, in, inl)) {
229
0
        ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
230
0
        return 0;
231
0
    }
232
233
23
    return 1;
234
23
}
235
236
static int aes_xts_stream_final(void *vctx, unsigned char *out, size_t *outl,
237
    size_t outsize)
238
23
{
239
23
    if (!ossl_prov_is_running())
240
0
        return 0;
241
23
    *outl = 0;
242
23
    return 1;
243
23
}
244
245
static const OSSL_PARAM *aes_xts_settable_ctx_params(ossl_unused void *cctx,
246
    ossl_unused void *provctx)
247
0
{
248
0
    return aes_xts_set_ctx_params_list;
249
0
}
250
251
static int aes_xts_set_ctx_params(void *vctx, const OSSL_PARAM params[])
252
182
{
253
182
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
254
182
    struct aes_xts_set_ctx_params_st p;
255
256
182
    if (ctx == NULL || !aes_xts_set_ctx_params_decoder(params, &p))
257
0
        return 0;
258
259
182
    if (p.keylen != NULL) {
260
0
        size_t keylen;
261
262
0
        if (!OSSL_PARAM_get_size_t(p.keylen, &keylen)) {
263
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
264
0
            return 0;
265
0
        }
266
        /* The key length can not be modified for xts mode */
267
0
        if (keylen != ctx->keylen)
268
0
            return 0;
269
0
    }
270
271
182
    return 1;
272
182
}
273
274
#define IMPLEMENT_cipher(lcmode, UCMODE, kbits, flags)                             \
275
    static OSSL_FUNC_cipher_get_params_fn aes_##kbits##_##lcmode##_get_params;     \
276
    static int aes_##kbits##_##lcmode##_get_params(OSSL_PARAM params[])            \
277
2
    {                                                                              \
278
2
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,    \
279
2
            flags, 2 * kbits, AES_XTS_BLOCK_BITS,                                  \
280
2
            AES_XTS_IV_BITS);                                                      \
281
2
    }                                                                              \
cipher_aes_xts.c:aes_256_xts_get_params
Line
Count
Source
277
1
    {                                                                              \
278
1
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,    \
279
1
            flags, 2 * kbits, AES_XTS_BLOCK_BITS,                                  \
280
1
            AES_XTS_IV_BITS);                                                      \
281
1
    }                                                                              \
cipher_aes_xts.c:aes_128_xts_get_params
Line
Count
Source
277
1
    {                                                                              \
278
1
        return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,    \
279
1
            flags, 2 * kbits, AES_XTS_BLOCK_BITS,                                  \
280
1
            AES_XTS_IV_BITS);                                                      \
281
1
    }                                                                              \
282
    static OSSL_FUNC_cipher_newctx_fn aes_##kbits##_xts_newctx;                    \
283
    static void *aes_##kbits##_xts_newctx(void *provctx)                           \
284
68
    {                                                                              \
285
68
        return aes_xts_newctx(provctx, EVP_CIPH_##UCMODE##_MODE, flags, 2 * kbits, \
286
68
            AES_XTS_BLOCK_BITS, AES_XTS_IV_BITS);                                  \
287
68
    }                                                                              \
Unexecuted instantiation: cipher_aes_xts.c:aes_256_xts_newctx
cipher_aes_xts.c:aes_128_xts_newctx
Line
Count
Source
284
68
    {                                                                              \
285
68
        return aes_xts_newctx(provctx, EVP_CIPH_##UCMODE##_MODE, flags, 2 * kbits, \
286
68
            AES_XTS_BLOCK_BITS, AES_XTS_IV_BITS);                                  \
287
68
    }                                                                              \
288
    const OSSL_DISPATCH ossl_aes##kbits##xts_functions[] = {                       \
289
        { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))aes_##kbits##_xts_newctx },     \
290
        { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))aes_xts_einit },          \
291
        { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))aes_xts_dinit },          \
292
        { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))aes_xts_stream_update },        \
293
        { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))aes_xts_stream_final },          \
294
        { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))aes_xts_cipher },               \
295
        { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))aes_xts_freectx },             \
296
        { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))aes_xts_dupctx },               \
297
        { OSSL_FUNC_CIPHER_GET_PARAMS,                                             \
298
            (void (*)(void))aes_##kbits##_##lcmode##_get_params },                 \
299
        { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,                                        \
300
            (void (*)(void))ossl_cipher_generic_gettable_params },                 \
301
        { OSSL_FUNC_CIPHER_GET_CTX_PARAMS,                                         \
302
            (void (*)(void))ossl_cipher_generic_get_ctx_params },                  \
303
        { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,                                    \
304
            (void (*)(void))ossl_cipher_generic_gettable_ctx_params },             \
305
        { OSSL_FUNC_CIPHER_SET_CTX_PARAMS,                                         \
306
            (void (*)(void))aes_xts_set_ctx_params },                              \
307
        { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,                                    \
308
            (void (*)(void))aes_xts_settable_ctx_params },                         \
309
        OSSL_DISPATCH_END                                                          \
310
    }
311
312
IMPLEMENT_cipher(xts, XTS, 256, AES_XTS_FLAGS);
313
IMPLEMENT_cipher(xts, XTS, 128, AES_XTS_FLAGS);