Coverage Report

Created: 2025-11-07 06:58

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