Coverage Report

Created: 2025-06-13 06:58

/src/openssl32/providers/implementations/ciphers/cipher_sm4_xts.c
Line
Count
Source (jump to first uncovered line)
1
2
/*
3
 * Copyright 2022-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
/* 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
19
#define SM4_XTS_IV_BITS 128
20
19
#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
static const OSSL_PARAM sm4_xts_known_settable_ctx_params[] = {
193
    OSSL_PARAM_utf8_string(OSSL_CIPHER_PARAM_XTS_STANDARD, NULL, 0),
194
    OSSL_PARAM_END
195
};
196
197
static const OSSL_PARAM *sm4_xts_settable_ctx_params(ossl_unused void *cctx,
198
                                                     ossl_unused void *provctx)
199
1
{
200
1
    return sm4_xts_known_settable_ctx_params;
201
1
}
202
203
static int sm4_xts_set_ctx_params(void *vxctx, const OSSL_PARAM params[])
204
0
{
205
0
    PROV_SM4_XTS_CTX *xctx = (PROV_SM4_XTS_CTX *)vxctx;
206
0
    const OSSL_PARAM *p;
207
208
0
    if (params == NULL)
209
0
        return 1;
210
211
    /*-
212
     * Sets the XTS standard to use with SM4-XTS algorithm.
213
     *
214
     * Must be utf8 string "GB" or "IEEE",
215
     * "GB" means the GB/T 17964-2021 standard
216
     * "IEEE" means the IEEE Std 1619-2007 standard
217
     */
218
0
    p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_XTS_STANDARD);
219
220
0
    if (p != NULL) {
221
0
        const char *xts_standard = NULL;
222
223
0
        if (p->data_type != OSSL_PARAM_UTF8_STRING)
224
0
            return 0;
225
226
0
        if (!OSSL_PARAM_get_utf8_string_ptr(p, &xts_standard)) {
227
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
228
0
            return 0;
229
0
        }
230
0
        if (OPENSSL_strcasecmp(xts_standard, "GB") == 0) {
231
0
            xctx->xts_standard = 0;
232
0
        } else if (OPENSSL_strcasecmp(xts_standard, "IEEE") == 0) {
233
0
            xctx->xts_standard = 1;
234
0
        } else {
235
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
236
0
            return 0;
237
0
        }
238
0
    }
239
240
0
    return 1;
241
0
}
242
243
#define IMPLEMENT_cipher(lcmode, UCMODE, kbits, flags)                         \
244
static OSSL_FUNC_cipher_get_params_fn sm4_##kbits##_##lcmode##_get_params;     \
245
19
static int sm4_##kbits##_##lcmode##_get_params(OSSL_PARAM params[])            \
246
19
{                                                                              \
247
19
    return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,    \
248
19
                                          flags, 2 * kbits, SM4_XTS_BLOCK_BITS,\
249
19
                                          SM4_XTS_IV_BITS);                    \
250
19
}                                                                              \
251
static OSSL_FUNC_cipher_newctx_fn sm4_##kbits##_xts_newctx;                    \
252
0
static void *sm4_##kbits##_xts_newctx(void *provctx)                           \
253
0
{                                                                              \
254
0
    return sm4_xts_newctx(provctx, EVP_CIPH_##UCMODE##_MODE, flags, 2 * kbits, \
255
0
                          SM4_XTS_BLOCK_BITS, SM4_XTS_IV_BITS);                \
256
0
}                                                                              \
257
const OSSL_DISPATCH ossl_sm4##kbits##xts_functions[] = {                       \
258
    { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))sm4_##kbits##_xts_newctx },     \
259
    { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))sm4_xts_einit },          \
260
    { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))sm4_xts_dinit },          \
261
    { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))sm4_xts_stream_update },        \
262
    { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))sm4_xts_stream_final },          \
263
    { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))sm4_xts_cipher },               \
264
    { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))sm4_xts_freectx },             \
265
    { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))sm4_xts_dupctx },               \
266
    { OSSL_FUNC_CIPHER_GET_PARAMS,                                             \
267
      (void (*)(void))sm4_##kbits##_##lcmode##_get_params },                   \
268
    { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,                                        \
269
      (void (*)(void))ossl_cipher_generic_gettable_params },                   \
270
    { OSSL_FUNC_CIPHER_GET_CTX_PARAMS,                                         \
271
      (void (*)(void))ossl_cipher_generic_get_ctx_params },                    \
272
    { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,                                    \
273
      (void (*)(void))ossl_cipher_generic_gettable_ctx_params },               \
274
    { OSSL_FUNC_CIPHER_SET_CTX_PARAMS,                                         \
275
      (void (*)(void))sm4_xts_set_ctx_params },                                \
276
    { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,                                    \
277
     (void (*)(void))sm4_xts_settable_ctx_params },                            \
278
    OSSL_DISPATCH_END                                                          \
279
}
280
/* ossl_sm4128xts_functions */
281
IMPLEMENT_cipher(xts, XTS, 128, SM4_XTS_FLAGS);