Coverage Report

Created: 2025-12-04 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/providers/implementations/ciphers/cipher_aes_xts.c
Line
Count
Source
1
2
/*
3
 * Copyright 2019-2024 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
140
#define AES_XTS_IV_BITS 128
25
140
#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
5
{
57
5
    if ((!ossl_aes_xts_allow_insecure_decrypt || enc)
58
5
            && CRYPTO_memcmp(key, key + bytes, bytes) == 0) {
59
2
        ERR_raise(ERR_LIB_PROV, PROV_R_XTS_DUPLICATED_KEYS);
60
2
        return 0;
61
2
    }
62
3
    return 1;
63
5
}
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
13
{
72
13
    PROV_AES_XTS_CTX *xctx = (PROV_AES_XTS_CTX *)vctx;
73
13
    PROV_CIPHER_CTX *ctx = &xctx->base;
74
75
13
    if (!ossl_prov_is_running())
76
0
        return 0;
77
78
13
    ctx->enc = enc;
79
80
13
    if (iv != NULL) {
81
0
        if (!ossl_cipher_generic_initiv(vctx, iv, ivlen))
82
0
            return 0;
83
0
    }
84
13
    if (key != NULL) {
85
5
        if (keylen != ctx->keylen) {
86
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
87
0
            return 0;
88
0
        }
89
5
        if (!aes_xts_check_keys_differ(key, keylen / 2, enc))
90
2
            return 0;
91
3
        if (!ctx->hw->init(ctx, key, keylen))
92
0
            return 0;
93
3
    }
94
11
    return aes_xts_set_ctx_params(ctx, params);
95
13
}
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
13
{
101
13
    return aes_xts_init(vctx, key, keylen, iv, ivlen, params, 1);
102
13
}
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
8
{
114
8
    PROV_AES_XTS_CTX *ctx;
115
116
8
    if (!ossl_prov_is_running())
117
0
        return NULL;
118
119
8
    ctx = OPENSSL_zalloc(sizeof(*ctx));
120
8
    if (ctx != NULL) {
121
8
        ossl_cipher_generic_initkey(&ctx->base, kbits, blkbits, ivbits, mode,
122
8
                                    flags, ossl_prov_cipher_hw_aes_xts(kbits),
123
8
                                    NULL);
124
8
    }
125
8
    return ctx;
126
8
}
127
128
static void aes_xts_freectx(void *vctx)
129
8
{
130
8
    PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
131
132
8
    ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
133
8
    OPENSSL_clear_free(ctx,  sizeof(*ctx));
134
8
}
135
136
static void *aes_xts_dupctx(void *vctx)
137
0
{
138
0
    PROV_AES_XTS_CTX *in = (PROV_AES_XTS_CTX *)vctx;
139
0
    PROV_AES_XTS_CTX *ret = NULL;
140
141
0
    if (!ossl_prov_is_running())
142
0
        return NULL;
143
144
0
    if (in->xts.key1 != NULL) {
145
0
        if (in->xts.key1 != &in->ks1)
146
0
            return NULL;
147
0
    }
148
0
    if (in->xts.key2 != NULL) {
149
0
        if (in->xts.key2 != &in->ks2)
150
0
            return NULL;
151
0
    }
152
0
    ret = OPENSSL_malloc(sizeof(*ret));
153
0
    if (ret == NULL)
154
0
        return NULL;
155
0
    in->base.hw->copyctx(&ret->base, &in->base);
156
0
    return ret;
157
0
}
158
159
static int aes_xts_cipher(void *vctx, unsigned char *out, size_t *outl,
160
                          size_t outsize, const unsigned char *in, size_t inl)
161
3
{
162
3
    PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
163
164
3
    if (!ossl_prov_is_running()
165
3
            || ctx->xts.key1 == NULL
166
3
            || ctx->xts.key2 == NULL
167
3
            || !ctx->base.iv_set
168
0
            || out == NULL
169
0
            || in == NULL
170
0
            || inl < AES_BLOCK_SIZE)
171
3
        return 0;
172
173
    /*
174
     * Impose a limit of 2^20 blocks per data unit as specified by
175
     * IEEE Std 1619-2018.  The earlier and obsolete IEEE Std 1619-2007
176
     * indicated that this was a SHOULD NOT rather than a MUST NOT.
177
     * NIST SP 800-38E mandates the same limit.
178
     */
179
0
    if (inl > XTS_MAX_BLOCKS_PER_DATA_UNIT * AES_BLOCK_SIZE) {
180
0
        ERR_raise(ERR_LIB_PROV, PROV_R_XTS_DATA_UNIT_IS_TOO_LARGE);
181
0
        return 0;
182
0
    }
183
184
0
    if (ctx->stream != NULL)
185
0
        (*ctx->stream)(in, out, inl, ctx->xts.key1, ctx->xts.key2, ctx->base.iv);
186
0
    else if (CRYPTO_xts128_encrypt(&ctx->xts, ctx->base.iv, in, out, inl,
187
0
                                   ctx->base.enc))
188
0
        return 0;
189
190
0
    *outl = inl;
191
0
    return 1;
192
0
}
193
194
static int aes_xts_stream_update(void *vctx, unsigned char *out, size_t *outl,
195
                                 size_t outsize, const unsigned char *in,
196
                                 size_t inl)
197
3
{
198
3
    PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
199
200
3
    if (outsize < inl) {
201
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
202
0
        return 0;
203
0
    }
204
205
3
    if (!aes_xts_cipher(ctx, out, outl, outsize, in, inl)) {
206
3
        ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
207
3
        return 0;
208
3
    }
209
210
0
    return 1;
211
3
}
212
213
static int aes_xts_stream_final(void *vctx, unsigned char *out, size_t *outl,
214
                                size_t outsize)
215
0
{
216
0
    if (!ossl_prov_is_running())
217
0
        return 0;
218
0
    *outl = 0;
219
0
    return 1;
220
0
}
221
222
static const OSSL_PARAM aes_xts_known_settable_ctx_params[] = {
223
    OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
224
    OSSL_PARAM_END
225
};
226
227
static const OSSL_PARAM *aes_xts_settable_ctx_params(ossl_unused void *cctx,
228
                                                     ossl_unused void *provctx)
229
7
{
230
7
    return aes_xts_known_settable_ctx_params;
231
7
}
232
233
static int aes_xts_set_ctx_params(void *vctx, const OSSL_PARAM params[])
234
6
{
235
6
    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
236
6
    const OSSL_PARAM *p;
237
238
6
    if (params == NULL)
239
4
        return 1;
240
241
2
    p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
242
2
    if (p != NULL) {
243
1
        size_t keylen;
244
245
1
        if (!OSSL_PARAM_get_size_t(p, &keylen)) {
246
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
247
0
            return 0;
248
0
        }
249
        /* The key length can not be modified for xts mode */
250
1
        if (keylen != ctx->keylen)
251
1
            return 0;
252
1
    }
253
254
1
    return 1;
255
2
}
256
257
#define IMPLEMENT_cipher(lcmode, UCMODE, kbits, flags)                         \
258
static OSSL_FUNC_cipher_get_params_fn aes_##kbits##_##lcmode##_get_params;     \
259
132
static int aes_##kbits##_##lcmode##_get_params(OSSL_PARAM params[])            \
260
132
{                                                                              \
261
132
    return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,    \
262
132
                                     flags, 2 * kbits, AES_XTS_BLOCK_BITS,     \
263
132
                                     AES_XTS_IV_BITS);                         \
264
132
}                                                                              \
cipher_aes_xts.c:aes_256_xts_get_params
Line
Count
Source
259
66
static int aes_##kbits##_##lcmode##_get_params(OSSL_PARAM params[])            \
260
66
{                                                                              \
261
66
    return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,    \
262
66
                                     flags, 2 * kbits, AES_XTS_BLOCK_BITS,     \
263
66
                                     AES_XTS_IV_BITS);                         \
264
66
}                                                                              \
cipher_aes_xts.c:aes_128_xts_get_params
Line
Count
Source
259
66
static int aes_##kbits##_##lcmode##_get_params(OSSL_PARAM params[])            \
260
66
{                                                                              \
261
66
    return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,    \
262
66
                                     flags, 2 * kbits, AES_XTS_BLOCK_BITS,     \
263
66
                                     AES_XTS_IV_BITS);                         \
264
66
}                                                                              \
265
static OSSL_FUNC_cipher_newctx_fn aes_##kbits##_xts_newctx;                    \
266
8
static void *aes_##kbits##_xts_newctx(void *provctx)                           \
267
8
{                                                                              \
268
8
    return aes_xts_newctx(provctx, EVP_CIPH_##UCMODE##_MODE, flags, 2 * kbits, \
269
8
                          AES_XTS_BLOCK_BITS, AES_XTS_IV_BITS);                \
270
8
}                                                                              \
cipher_aes_xts.c:aes_256_xts_newctx
Line
Count
Source
266
3
static void *aes_##kbits##_xts_newctx(void *provctx)                           \
267
3
{                                                                              \
268
3
    return aes_xts_newctx(provctx, EVP_CIPH_##UCMODE##_MODE, flags, 2 * kbits, \
269
3
                          AES_XTS_BLOCK_BITS, AES_XTS_IV_BITS);                \
270
3
}                                                                              \
cipher_aes_xts.c:aes_128_xts_newctx
Line
Count
Source
266
5
static void *aes_##kbits##_xts_newctx(void *provctx)                           \
267
5
{                                                                              \
268
5
    return aes_xts_newctx(provctx, EVP_CIPH_##UCMODE##_MODE, flags, 2 * kbits, \
269
5
                          AES_XTS_BLOCK_BITS, AES_XTS_IV_BITS);                \
270
5
}                                                                              \
271
const OSSL_DISPATCH ossl_aes##kbits##xts_functions[] = {                       \
272
    { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))aes_##kbits##_xts_newctx },     \
273
    { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))aes_xts_einit },          \
274
    { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))aes_xts_dinit },          \
275
    { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))aes_xts_stream_update },        \
276
    { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))aes_xts_stream_final },          \
277
    { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))aes_xts_cipher },               \
278
    { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))aes_xts_freectx },             \
279
    { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))aes_xts_dupctx },               \
280
    { OSSL_FUNC_CIPHER_GET_PARAMS,                                             \
281
      (void (*)(void))aes_##kbits##_##lcmode##_get_params },                   \
282
    { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,                                        \
283
      (void (*)(void))ossl_cipher_generic_gettable_params },                   \
284
    { OSSL_FUNC_CIPHER_GET_CTX_PARAMS,                                         \
285
      (void (*)(void))ossl_cipher_generic_get_ctx_params },                    \
286
    { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,                                    \
287
      (void (*)(void))ossl_cipher_generic_gettable_ctx_params },               \
288
    { OSSL_FUNC_CIPHER_SET_CTX_PARAMS,                                         \
289
      (void (*)(void))aes_xts_set_ctx_params },                                \
290
    { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,                                    \
291
     (void (*)(void))aes_xts_settable_ctx_params },                            \
292
    OSSL_DISPATCH_END                                                          \
293
}
294
295
IMPLEMENT_cipher(xts, XTS, 256, AES_XTS_FLAGS);
296
IMPLEMENT_cipher(xts, XTS, 128, AES_XTS_FLAGS);