Coverage Report

Created: 2026-03-09 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/sm2/sm2_crypt.c
Line
Count
Source
1
/*
2
 * Copyright 2017-2025 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright 2017 Ribose Inc. All Rights Reserved.
4
 * Ported from Ribose contributions from Botan.
5
 *
6
 * Licensed under the Apache License 2.0 (the "License").  You may not use
7
 * this file except in compliance with the License.  You can obtain a copy
8
 * in the file LICENSE in the source distribution or at
9
 * https://www.openssl.org/source/license.html
10
 */
11
12
/*
13
 * ECDSA low level APIs are deprecated for public use, but still ok for
14
 * internal use.
15
 */
16
#include "internal/deprecated.h"
17
18
#include "crypto/sm2.h"
19
#include "crypto/sm2err.h"
20
#include "crypto/ec.h" /* ossl_ecdh_kdf_X9_63() */
21
#include <openssl/err.h>
22
#include <openssl/evp.h>
23
#include <openssl/bn.h>
24
#include <openssl/asn1.h>
25
#include <openssl/asn1t.h>
26
#include <string.h>
27
28
#include <crypto/asn1.h>
29
30
typedef struct SM2_Ciphertext_st SM2_Ciphertext;
31
DECLARE_ASN1_FUNCTIONS(SM2_Ciphertext)
32
33
struct SM2_Ciphertext_st {
34
    BIGNUM *C1x;
35
    BIGNUM *C1y;
36
    ASN1_OCTET_STRING *C3;
37
    ASN1_OCTET_STRING *C2;
38
};
39
40
ASN1_SEQUENCE(SM2_Ciphertext) = {
41
    ASN1_SIMPLE(SM2_Ciphertext, C1x, BIGNUM),
42
    ASN1_SIMPLE(SM2_Ciphertext, C1y, BIGNUM),
43
    ASN1_SIMPLE(SM2_Ciphertext, C3, ASN1_OCTET_STRING),
44
    ASN1_SIMPLE(SM2_Ciphertext, C2, ASN1_OCTET_STRING),
45
0
} ASN1_SEQUENCE_END(SM2_Ciphertext)
46
0
47
0
IMPLEMENT_ASN1_FUNCTIONS(SM2_Ciphertext)
48
0
49
0
static int ec_field_size(const EC_GROUP *group)
50
0
{
51
0
    const BIGNUM *p = EC_GROUP_get0_field(group);
52
53
0
    if (p == NULL)
54
0
        return 0;
55
56
0
    return BN_num_bytes(p);
57
0
}
58
59
static int is_all_zeros(const unsigned char *msg, size_t msglen)
60
0
{
61
0
    unsigned char re = 0;
62
0
    size_t i;
63
64
0
    for (i = 0; i < msglen; i++) {
65
0
        re |= msg[i];
66
0
    }
67
68
0
    return re == 0 ? 1 : 0;
69
0
}
70
71
int ossl_sm2_plaintext_size(const unsigned char *ct, size_t ct_size,
72
    size_t *pt_size)
73
0
{
74
0
    struct SM2_Ciphertext_st *sm2_ctext = NULL;
75
76
0
    sm2_ctext = d2i_SM2_Ciphertext(NULL, &ct, (long)ct_size);
77
78
0
    if (sm2_ctext == NULL) {
79
0
        ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_ENCODING);
80
0
        return 0;
81
0
    }
82
83
0
    *pt_size = sm2_ctext->C2->length;
84
0
    SM2_Ciphertext_free(sm2_ctext);
85
86
0
    return 1;
87
0
}
88
89
int ossl_sm2_ciphertext_size(const EC_KEY *key, const EVP_MD *digest,
90
    size_t msg_len, size_t *ct_size)
91
0
{
92
0
    const int field_size = ec_field_size(EC_KEY_get0_group(key));
93
0
    const int md_size = EVP_MD_get_size(digest);
94
0
    int sz;
95
96
0
    if (field_size == 0 || md_size <= 0 || msg_len > INT_MAX / 2)
97
0
        return 0;
98
99
    /* Integer and string are simple type; set constructed = 0, means primitive and definite length encoding. */
100
0
    sz = 2 * ASN1_object_size(0, field_size + 1, V_ASN1_INTEGER)
101
0
        + ASN1_object_size(0, md_size, V_ASN1_OCTET_STRING)
102
0
        + ASN1_object_size(0, (int)msg_len, V_ASN1_OCTET_STRING);
103
    /* Sequence is structured type; set constructed = 1, means constructed and definite length encoding. */
104
0
    *ct_size = (size_t)ASN1_object_size(1, sz, V_ASN1_SEQUENCE);
105
106
0
    return 1;
107
0
}
108
109
int ossl_sm2_encrypt(const EC_KEY *key,
110
    const EVP_MD *digest,
111
    const uint8_t *msg, size_t msg_len,
112
    uint8_t *ciphertext_buf, size_t *ciphertext_len)
113
0
{
114
0
    int rc = 0, ciphertext_leni;
115
0
    size_t i;
116
0
    BN_CTX *ctx = NULL;
117
0
    BIGNUM *k = NULL;
118
0
    BIGNUM *x1 = NULL;
119
0
    BIGNUM *y1 = NULL;
120
0
    BIGNUM *x2 = NULL;
121
0
    BIGNUM *y2 = NULL;
122
0
    EVP_MD_CTX *hash = EVP_MD_CTX_new();
123
0
    struct SM2_Ciphertext_st ctext_struct;
124
0
    const EC_GROUP *group = EC_KEY_get0_group(key);
125
0
    const BIGNUM *order = EC_GROUP_get0_order(group);
126
0
    const EC_POINT *P = EC_KEY_get0_public_key(key);
127
0
    EC_POINT *kG = NULL;
128
0
    EC_POINT *kP = NULL;
129
0
    uint8_t *msg_mask = NULL;
130
0
    uint8_t *x2y2 = NULL;
131
0
    uint8_t *C3 = NULL;
132
0
    int field_size;
133
0
    const int C3_size = EVP_MD_get_size(digest);
134
0
    EVP_MD *fetched_digest = NULL;
135
0
    OSSL_LIB_CTX *libctx = ossl_ec_key_get_libctx(key);
136
0
    const char *propq = ossl_ec_key_get0_propq(key);
137
138
    /* NULL these before any "goto done" */
139
0
    ctext_struct.C2 = NULL;
140
0
    ctext_struct.C3 = NULL;
141
142
0
    if (msg_len > INT_MAX / 2) {
143
0
        ERR_raise(ERR_LIB_SM2, ERR_R_PASSED_INVALID_ARGUMENT);
144
0
        goto done;
145
0
    }
146
147
0
    if (hash == NULL || C3_size <= 0) {
148
0
        ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
149
0
        goto done;
150
0
    }
151
152
0
    field_size = ec_field_size(group);
153
0
    if (field_size == 0) {
154
0
        ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
155
0
        goto done;
156
0
    }
157
158
0
    kG = EC_POINT_new(group);
159
0
    kP = EC_POINT_new(group);
160
0
    if (kG == NULL || kP == NULL) {
161
0
        ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
162
0
        goto done;
163
0
    }
164
0
    ctx = BN_CTX_new_ex(libctx);
165
0
    if (ctx == NULL) {
166
0
        ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
167
0
        goto done;
168
0
    }
169
170
0
    BN_CTX_start(ctx);
171
0
    k = BN_CTX_get(ctx);
172
0
    x1 = BN_CTX_get(ctx);
173
0
    x2 = BN_CTX_get(ctx);
174
0
    y1 = BN_CTX_get(ctx);
175
0
    y2 = BN_CTX_get(ctx);
176
177
0
    if (y2 == NULL) {
178
0
        ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
179
0
        goto done;
180
0
    }
181
182
0
    x2y2 = OPENSSL_calloc(2, field_size);
183
0
    C3 = OPENSSL_zalloc(C3_size);
184
185
0
    if (x2y2 == NULL || C3 == NULL)
186
0
        goto done;
187
188
0
    memset(ciphertext_buf, 0, *ciphertext_len);
189
190
0
    msg_mask = OPENSSL_zalloc(msg_len);
191
0
    if (msg_mask == NULL)
192
0
        goto done;
193
194
0
again:
195
0
    if (!BN_priv_rand_range_ex(k, order, 0, ctx)) {
196
0
        ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
197
0
        goto done;
198
0
    }
199
200
0
    if (!EC_POINT_mul(group, kG, k, NULL, NULL, ctx)
201
0
        || !EC_POINT_get_affine_coordinates(group, kG, x1, y1, ctx)
202
0
        || !EC_POINT_mul(group, kP, NULL, P, k, ctx)
203
0
        || !EC_POINT_get_affine_coordinates(group, kP, x2, y2, ctx)) {
204
0
        ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
205
0
        goto done;
206
0
    }
207
208
0
    if (BN_bn2binpad(x2, x2y2, field_size) < 0
209
0
        || BN_bn2binpad(y2, x2y2 + field_size, field_size) < 0) {
210
0
        ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
211
0
        goto done;
212
0
    }
213
214
    /* X9.63 with no salt happens to match the KDF used in SM2 */
215
0
    if (!ossl_ecdh_kdf_X9_63(msg_mask, msg_len, x2y2, 2 * field_size, NULL, 0,
216
0
            digest, libctx, propq)) {
217
0
        ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
218
0
        goto done;
219
0
    }
220
221
0
    if (is_all_zeros(msg_mask, msg_len)) {
222
0
        memset(x2y2, 0, 2 * field_size);
223
0
        goto again;
224
0
    }
225
226
0
    for (i = 0; i != msg_len; ++i)
227
0
        msg_mask[i] ^= msg[i];
228
229
0
    fetched_digest = EVP_MD_fetch(libctx, EVP_MD_get0_name(digest), propq);
230
0
    if (fetched_digest == NULL) {
231
0
        ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
232
0
        goto done;
233
0
    }
234
0
    if (EVP_DigestInit(hash, fetched_digest) == 0
235
0
        || EVP_DigestUpdate(hash, x2y2, field_size) == 0
236
0
        || EVP_DigestUpdate(hash, msg, msg_len) == 0
237
0
        || EVP_DigestUpdate(hash, x2y2 + field_size, field_size) == 0
238
0
        || EVP_DigestFinal(hash, C3, NULL) == 0) {
239
0
        ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
240
0
        goto done;
241
0
    }
242
243
0
    ctext_struct.C1x = x1;
244
0
    ctext_struct.C1y = y1;
245
0
    ctext_struct.C3 = ASN1_OCTET_STRING_new();
246
0
    ctext_struct.C2 = ASN1_OCTET_STRING_new();
247
248
0
    if (ctext_struct.C3 == NULL || ctext_struct.C2 == NULL) {
249
0
        ERR_raise(ERR_LIB_SM2, ERR_R_ASN1_LIB);
250
0
        goto done;
251
0
    }
252
0
    if (!ASN1_OCTET_STRING_set(ctext_struct.C3, C3, C3_size)
253
0
        || !ASN1_OCTET_STRING_set(ctext_struct.C2, msg_mask, (int)msg_len)) {
254
0
        ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
255
0
        goto done;
256
0
    }
257
258
0
    ciphertext_leni = i2d_SM2_Ciphertext(&ctext_struct, &ciphertext_buf);
259
    /* Ensure cast to size_t is safe */
260
0
    if (ciphertext_leni < 0) {
261
0
        ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
262
0
        goto done;
263
0
    }
264
0
    *ciphertext_len = (size_t)ciphertext_leni;
265
266
0
    rc = 1;
267
268
0
done:
269
0
    EVP_MD_free(fetched_digest);
270
0
    ASN1_OCTET_STRING_free(ctext_struct.C2);
271
0
    ASN1_OCTET_STRING_free(ctext_struct.C3);
272
0
    OPENSSL_free(msg_mask);
273
0
    OPENSSL_free(x2y2);
274
0
    OPENSSL_free(C3);
275
0
    EVP_MD_CTX_free(hash);
276
0
    BN_CTX_free(ctx);
277
0
    EC_POINT_free(kG);
278
0
    EC_POINT_free(kP);
279
0
    return rc;
280
0
}
281
282
int ossl_sm2_decrypt(const EC_KEY *key,
283
    const EVP_MD *digest,
284
    const uint8_t *ciphertext, size_t ciphertext_len,
285
    uint8_t *ptext_buf, size_t *ptext_len)
286
0
{
287
0
    int rc = 0;
288
0
    int i;
289
0
    BN_CTX *ctx = NULL;
290
0
    const EC_GROUP *group = EC_KEY_get0_group(key);
291
0
    EC_POINT *C1 = NULL;
292
0
    struct SM2_Ciphertext_st *sm2_ctext = NULL;
293
0
    BIGNUM *x2 = NULL;
294
0
    BIGNUM *y2 = NULL;
295
0
    uint8_t *x2y2 = NULL;
296
0
    uint8_t *computed_C3 = NULL;
297
0
    const int field_size = ec_field_size(group);
298
0
    const int hash_size = EVP_MD_get_size(digest);
299
0
    uint8_t *msg_mask = NULL;
300
0
    const uint8_t *C2 = NULL;
301
0
    const uint8_t *C3 = NULL;
302
0
    int msg_len = 0;
303
0
    EVP_MD_CTX *hash = NULL;
304
0
    OSSL_LIB_CTX *libctx = ossl_ec_key_get_libctx(key);
305
0
    const char *propq = ossl_ec_key_get0_propq(key);
306
307
0
    if (field_size == 0 || hash_size <= 0 || ciphertext_len > LONG_MAX)
308
0
        goto done;
309
310
0
    memset(ptext_buf, 0xFF, *ptext_len);
311
312
0
    sm2_ctext = d2i_SM2_Ciphertext(NULL, &ciphertext, (long)ciphertext_len);
313
314
0
    if (sm2_ctext == NULL) {
315
0
        ERR_raise(ERR_LIB_SM2, SM2_R_ASN1_ERROR);
316
0
        goto done;
317
0
    }
318
319
0
    if (sm2_ctext->C3->length != hash_size) {
320
0
        ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_ENCODING);
321
0
        goto done;
322
0
    }
323
324
0
    C2 = sm2_ctext->C2->data;
325
0
    C3 = sm2_ctext->C3->data;
326
0
    msg_len = sm2_ctext->C2->length;
327
0
    if (*ptext_len < (size_t)msg_len) {
328
0
        ERR_raise(ERR_LIB_SM2, SM2_R_BUFFER_TOO_SMALL);
329
0
        goto done;
330
0
    }
331
332
0
    ctx = BN_CTX_new_ex(libctx);
333
0
    if (ctx == NULL) {
334
0
        ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
335
0
        goto done;
336
0
    }
337
338
0
    BN_CTX_start(ctx);
339
0
    x2 = BN_CTX_get(ctx);
340
0
    y2 = BN_CTX_get(ctx);
341
342
0
    if (y2 == NULL) {
343
0
        ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
344
0
        goto done;
345
0
    }
346
347
0
    msg_mask = OPENSSL_zalloc(msg_len);
348
0
    x2y2 = OPENSSL_calloc(2, field_size);
349
0
    computed_C3 = OPENSSL_zalloc(hash_size);
350
351
0
    if (msg_mask == NULL || x2y2 == NULL || computed_C3 == NULL)
352
0
        goto done;
353
354
0
    C1 = EC_POINT_new(group);
355
0
    if (C1 == NULL) {
356
0
        ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
357
0
        goto done;
358
0
    }
359
360
0
    if (!EC_POINT_set_affine_coordinates(group, C1, sm2_ctext->C1x,
361
0
            sm2_ctext->C1y, ctx)
362
0
        || !EC_POINT_mul(group, C1, NULL, C1, EC_KEY_get0_private_key(key),
363
0
            ctx)
364
0
        || !EC_POINT_get_affine_coordinates(group, C1, x2, y2, ctx)) {
365
0
        ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
366
0
        goto done;
367
0
    }
368
369
0
    if (BN_bn2binpad(x2, x2y2, field_size) < 0
370
0
        || BN_bn2binpad(y2, x2y2 + field_size, field_size) < 0
371
0
        || !ossl_ecdh_kdf_X9_63(msg_mask, msg_len, x2y2, 2 * field_size,
372
0
            NULL, 0, digest, libctx, propq)) {
373
0
        ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
374
0
        goto done;
375
0
    }
376
377
0
    if (is_all_zeros(msg_mask, msg_len)) {
378
0
        ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_ENCODING);
379
0
        goto done;
380
0
    }
381
382
0
    for (i = 0; i != msg_len; ++i)
383
0
        ptext_buf[i] = C2[i] ^ msg_mask[i];
384
385
0
    hash = EVP_MD_CTX_new();
386
0
    if (hash == NULL) {
387
0
        ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
388
0
        goto done;
389
0
    }
390
391
0
    if (!EVP_DigestInit(hash, digest)
392
0
        || !EVP_DigestUpdate(hash, x2y2, field_size)
393
0
        || !EVP_DigestUpdate(hash, ptext_buf, msg_len)
394
0
        || !EVP_DigestUpdate(hash, x2y2 + field_size, field_size)
395
0
        || !EVP_DigestFinal(hash, computed_C3, NULL)) {
396
0
        ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
397
0
        goto done;
398
0
    }
399
400
0
    if (CRYPTO_memcmp(computed_C3, C3, hash_size) != 0) {
401
0
        ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_DIGEST);
402
0
        goto done;
403
0
    }
404
405
0
    rc = 1;
406
0
    *ptext_len = msg_len;
407
408
0
done:
409
0
    if (rc == 0)
410
0
        memset(ptext_buf, 0, *ptext_len);
411
412
0
    OPENSSL_free(msg_mask);
413
0
    OPENSSL_free(x2y2);
414
0
    OPENSSL_free(computed_C3);
415
0
    EC_POINT_free(C1);
416
0
    BN_CTX_free(ctx);
417
0
    SM2_Ciphertext_free(sm2_ctext);
418
0
    EVP_MD_CTX_free(hash);
419
420
0
    return rc;
421
0
}