Coverage Report

Created: 2025-11-16 06:40

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