Coverage Report

Created: 2025-06-13 06:58

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