Coverage Report

Created: 2025-10-12 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/providers/implementations/ciphers/cipher_sm4_xts.c
Line
Count
Source
1
/*
2
 * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
11
/* Dispatch functions for SM4 XTS mode */
12
13
#include <openssl/proverr.h>
14
#include "cipher_sm4_xts.h"
15
#include "prov/implementations.h"
16
#include "prov/providercommon.h"
17
18
#define SM4_XTS_FLAGS PROV_CIPHER_FLAG_CUSTOM_IV
19
1
#define SM4_XTS_IV_BITS 128
20
1
#define SM4_XTS_BLOCK_BITS 8
21
22
/* forward declarations */
23
static OSSL_FUNC_cipher_encrypt_init_fn sm4_xts_einit;
24
static OSSL_FUNC_cipher_decrypt_init_fn sm4_xts_dinit;
25
static OSSL_FUNC_cipher_update_fn sm4_xts_stream_update;
26
static OSSL_FUNC_cipher_final_fn sm4_xts_stream_final;
27
static OSSL_FUNC_cipher_cipher_fn sm4_xts_cipher;
28
static OSSL_FUNC_cipher_freectx_fn sm4_xts_freectx;
29
static OSSL_FUNC_cipher_dupctx_fn sm4_xts_dupctx;
30
static OSSL_FUNC_cipher_set_ctx_params_fn sm4_xts_set_ctx_params;
31
static OSSL_FUNC_cipher_settable_ctx_params_fn sm4_xts_settable_ctx_params;
32
33
/*-
34
 * Provider dispatch functions
35
 */
36
static int sm4_xts_init(void *vctx, const unsigned char *key, size_t keylen,
37
                        const unsigned char *iv, size_t ivlen,
38
                        const OSSL_PARAM params[], int enc)
39
0
{
40
0
    PROV_SM4_XTS_CTX *xctx = (PROV_SM4_XTS_CTX *)vctx;
41
0
    PROV_CIPHER_CTX *ctx = &xctx->base;
42
43
0
    if (!ossl_prov_is_running())
44
0
        return 0;
45
46
0
    ctx->enc = enc;
47
48
0
    if (iv != NULL) {
49
0
        if (!ossl_cipher_generic_initiv(vctx, iv, ivlen))
50
0
            return 0;
51
0
    }
52
0
    if (key != NULL) {
53
0
        if (keylen != ctx->keylen) {
54
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
55
0
            return 0;
56
0
        }
57
0
        if (!ctx->hw->init(ctx, key, keylen))
58
0
            return 0;
59
0
    }
60
0
    return sm4_xts_set_ctx_params(xctx, params);
61
0
}
62
63
static int sm4_xts_einit(void *vctx, const unsigned char *key, size_t keylen,
64
                         const unsigned char *iv, size_t ivlen,
65
                         const OSSL_PARAM params[])
66
0
{
67
0
    return sm4_xts_init(vctx, key, keylen, iv, ivlen, params, 1);
68
0
}
69
70
static int sm4_xts_dinit(void *vctx, const unsigned char *key, size_t keylen,
71
                         const unsigned char *iv, size_t ivlen,
72
                         const OSSL_PARAM params[])
73
0
{
74
0
    return sm4_xts_init(vctx, key, keylen, iv, ivlen, params, 0);
75
0
}
76
77
static void *sm4_xts_newctx(void *provctx, unsigned int mode, uint64_t flags,
78
                            size_t kbits, size_t blkbits, size_t ivbits)
79
0
{
80
0
    PROV_SM4_XTS_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
81
82
0
    if (ctx != NULL) {
83
0
        ossl_cipher_generic_initkey(&ctx->base, kbits, blkbits, ivbits, mode,
84
0
                                    flags, ossl_prov_cipher_hw_sm4_xts(kbits),
85
0
                                    NULL);
86
0
    }
87
0
    return ctx;
88
0
}
89
90
static void sm4_xts_freectx(void *vctx)
91
0
{
92
0
    PROV_SM4_XTS_CTX *ctx = (PROV_SM4_XTS_CTX *)vctx;
93
94
0
    ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
95
0
    OPENSSL_clear_free(ctx, sizeof(*ctx));
96
0
}
97
98
static void *sm4_xts_dupctx(void *vctx)
99
0
{
100
0
    PROV_SM4_XTS_CTX *in = (PROV_SM4_XTS_CTX *)vctx;
101
0
    PROV_SM4_XTS_CTX *ret = NULL;
102
103
0
    if (!ossl_prov_is_running())
104
0
        return NULL;
105
106
0
    if (in->xts.key1 != NULL) {
107
0
        if (in->xts.key1 != &in->ks1)
108
0
            return NULL;
109
0
    }
110
0
    if (in->xts.key2 != NULL) {
111
0
        if (in->xts.key2 != &in->ks2)
112
0
            return NULL;
113
0
    }
114
0
    ret = OPENSSL_malloc(sizeof(*ret));
115
0
    if (ret == NULL)
116
0
        return NULL;
117
0
    in->base.hw->copyctx(&ret->base, &in->base);
118
0
    return ret;
119
0
}
120
121
static int sm4_xts_cipher(void *vctx, unsigned char *out, size_t *outl,
122
                          size_t outsize, const unsigned char *in, size_t inl)
123
0
{
124
0
    PROV_SM4_XTS_CTX *ctx = (PROV_SM4_XTS_CTX *)vctx;
125
126
0
    if (!ossl_prov_is_running()
127
0
            || ctx->xts.key1 == NULL
128
0
            || ctx->xts.key2 == NULL
129
0
            || !ctx->base.iv_set
130
0
            || out == NULL
131
0
            || in == NULL
132
0
            || inl < SM4_BLOCK_SIZE)
133
0
        return 0;
134
135
    /*
136
     * Impose a limit of 2^20 blocks per data unit as specified by
137
     * IEEE Std 1619-2018.  The earlier and obsolete IEEE Std 1619-2007
138
     * indicated that this was a SHOULD NOT rather than a MUST NOT.
139
     * NIST SP 800-38E mandates the same limit.
140
     */
141
0
    if (inl > XTS_MAX_BLOCKS_PER_DATA_UNIT * SM4_BLOCK_SIZE) {
142
0
        ERR_raise(ERR_LIB_PROV, PROV_R_XTS_DATA_UNIT_IS_TOO_LARGE);
143
0
        return 0;
144
0
    }
145
0
    if (ctx->xts_standard) {
146
0
        if (ctx->stream != NULL)
147
0
            (*ctx->stream)(in, out, inl, ctx->xts.key1, ctx->xts.key2,
148
0
                           ctx->base.iv, ctx->base.enc);
149
0
        else if (CRYPTO_xts128_encrypt(&ctx->xts, ctx->base.iv, in, out, inl,
150
0
                                       ctx->base.enc))
151
0
            return 0;
152
0
    } else {
153
0
        if (ctx->stream_gb != NULL)
154
0
            (*ctx->stream_gb)(in, out, inl, ctx->xts.key1, ctx->xts.key2,
155
0
                              ctx->base.iv, ctx->base.enc);
156
0
        else if (ossl_crypto_xts128gb_encrypt(&ctx->xts, ctx->base.iv, in, out,
157
0
                                              inl, ctx->base.enc))
158
0
            return 0;
159
0
    }
160
0
    *outl = inl;
161
0
    return 1;
162
0
}
163
164
static int sm4_xts_stream_update(void *vctx, unsigned char *out, size_t *outl,
165
                                 size_t outsize, const unsigned char *in,
166
                                 size_t inl)
167
0
{
168
0
    PROV_SM4_XTS_CTX *ctx = (PROV_SM4_XTS_CTX *)vctx;
169
170
0
    if (outsize < inl) {
171
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
172
0
        return 0;
173
0
    }
174
175
0
    if (!sm4_xts_cipher(ctx, out, outl, outsize, in, inl)) {
176
0
        ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
177
0
        return 0;
178
0
    }
179
180
0
    return 1;
181
0
}
182
183
static int sm4_xts_stream_final(void *vctx, unsigned char *out, size_t *outl,
184
                                size_t outsize)
185
0
{
186
0
    if (!ossl_prov_is_running())
187
0
        return 0;
188
0
    *outl = 0;
189
0
    return 1;
190
0
}
191
192
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
193
#ifndef sm4_xts_set_ctx_params_list
194
static const OSSL_PARAM sm4_xts_set_ctx_params_list[] = {
195
    OSSL_PARAM_utf8_string(OSSL_CIPHER_PARAM_XTS_STANDARD, NULL, 0),
196
    OSSL_PARAM_END
197
};
198
#endif
199
200
#ifndef sm4_xts_set_ctx_params_st
201
struct sm4_xts_set_ctx_params_st {
202
    OSSL_PARAM *std;
203
};
204
#endif
205
206
#ifndef sm4_xts_set_ctx_params_decoder
207
static int sm4_xts_set_ctx_params_decoder
208
    (const OSSL_PARAM *p, struct sm4_xts_set_ctx_params_st *r)
209
0
{
210
0
    const char *s;
211
212
0
    memset(r, 0, sizeof(*r));
213
0
    if (p != NULL)
214
0
        for (; (s = p->key) != NULL; p++)
215
0
            if (ossl_likely(strcmp("xts_standard", s + 0) == 0)) {
216
                /* OSSL_CIPHER_PARAM_XTS_STANDARD */
217
0
                if (ossl_unlikely(r->std != NULL)) {
218
0
                    ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
219
0
                                   "param %s is repeated", s);
220
0
                    return 0;
221
0
                }
222
0
                r->std = (OSSL_PARAM *)p;
223
0
            }
224
0
    return 1;
225
0
}
226
#endif
227
/* End of machine generated */
228
229
static const OSSL_PARAM *sm4_xts_settable_ctx_params(ossl_unused void *cctx,
230
                                                     ossl_unused void *provctx)
231
0
{
232
0
    return sm4_xts_set_ctx_params_list;
233
0
}
234
235
static int sm4_xts_set_ctx_params(void *vxctx, const OSSL_PARAM params[])
236
0
{
237
0
    PROV_SM4_XTS_CTX *xctx = (PROV_SM4_XTS_CTX *)vxctx;
238
0
    struct sm4_xts_set_ctx_params_st p;
239
240
0
    if (xctx == NULL || !sm4_xts_set_ctx_params_decoder(params, &p))
241
0
        return 0;
242
243
    /*-
244
     * Sets the XTS standard to use with SM4-XTS algorithm.
245
     *
246
     * Must be utf8 string "GB" or "IEEE",
247
     * "GB" means the GB/T 17964-2021 standard
248
     * "IEEE" means the IEEE Std 1619-2007 standard
249
     */
250
0
    if (p.std != NULL) {
251
0
        const char *xts_standard = NULL;
252
253
0
        if (p.std->data_type != OSSL_PARAM_UTF8_STRING)
254
0
            return 0;
255
256
0
        if (!OSSL_PARAM_get_utf8_string_ptr(p.std, &xts_standard)) {
257
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
258
0
            return 0;
259
0
        }
260
0
        if (OPENSSL_strcasecmp(xts_standard, "GB") == 0) {
261
0
            xctx->xts_standard = 0;
262
0
        } else if (OPENSSL_strcasecmp(xts_standard, "IEEE") == 0) {
263
0
            xctx->xts_standard = 1;
264
0
        } else {
265
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
266
0
            return 0;
267
0
        }
268
0
    }
269
270
0
    return 1;
271
0
}
272
273
#define IMPLEMENT_cipher(lcmode, UCMODE, kbits, flags)                         \
274
static OSSL_FUNC_cipher_get_params_fn sm4_##kbits##_##lcmode##_get_params;     \
275
1
static int sm4_##kbits##_##lcmode##_get_params(OSSL_PARAM params[])            \
276
1
{                                                                              \
277
1
    return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,    \
278
1
                                          flags, 2 * kbits, SM4_XTS_BLOCK_BITS,\
279
1
                                          SM4_XTS_IV_BITS);                    \
280
1
}                                                                              \
281
static OSSL_FUNC_cipher_newctx_fn sm4_##kbits##_xts_newctx;                    \
282
0
static void *sm4_##kbits##_xts_newctx(void *provctx)                           \
283
0
{                                                                              \
284
0
    return sm4_xts_newctx(provctx, EVP_CIPH_##UCMODE##_MODE, flags, 2 * kbits, \
285
0
                          SM4_XTS_BLOCK_BITS, SM4_XTS_IV_BITS);                \
286
0
}                                                                              \
287
const OSSL_DISPATCH ossl_sm4##kbits##xts_functions[] = {                       \
288
    { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))sm4_##kbits##_xts_newctx },     \
289
    { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))sm4_xts_einit },          \
290
    { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))sm4_xts_dinit },          \
291
    { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))sm4_xts_stream_update },        \
292
    { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))sm4_xts_stream_final },          \
293
    { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))sm4_xts_cipher },               \
294
    { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))sm4_xts_freectx },             \
295
    { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))sm4_xts_dupctx },               \
296
    { OSSL_FUNC_CIPHER_GET_PARAMS,                                             \
297
      (void (*)(void))sm4_##kbits##_##lcmode##_get_params },                   \
298
    { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,                                        \
299
      (void (*)(void))ossl_cipher_generic_gettable_params },                   \
300
    { OSSL_FUNC_CIPHER_GET_CTX_PARAMS,                                         \
301
      (void (*)(void))ossl_cipher_generic_get_ctx_params },                    \
302
    { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,                                    \
303
      (void (*)(void))ossl_cipher_generic_gettable_ctx_params },               \
304
    { OSSL_FUNC_CIPHER_SET_CTX_PARAMS,                                         \
305
      (void (*)(void))sm4_xts_set_ctx_params },                                \
306
    { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,                                    \
307
     (void (*)(void))sm4_xts_settable_ctx_params },                            \
308
    OSSL_DISPATCH_END                                                          \
309
}
310
/* ossl_sm4128xts_functions */
311
IMPLEMENT_cipher(xts, XTS, 128, SM4_XTS_FLAGS);