Coverage Report

Created: 2025-07-01 06:23

/src/irssi/subprojects/openssl-1.1.1l/crypto/sm2/sm2_crypt.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2017-2021 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 OpenSSL license (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
#include "crypto/sm2.h"
13
#include "crypto/sm2err.h"
14
#include "crypto/ec.h" /* ecdh_KDF_X9_63() */
15
#include <openssl/err.h>
16
#include <openssl/evp.h>
17
#include <openssl/bn.h>
18
#include <openssl/asn1.h>
19
#include <openssl/asn1t.h>
20
#include <string.h>
21
22
typedef struct SM2_Ciphertext_st SM2_Ciphertext;
23
DECLARE_ASN1_FUNCTIONS(SM2_Ciphertext)
24
25
struct SM2_Ciphertext_st {
26
    BIGNUM *C1x;
27
    BIGNUM *C1y;
28
    ASN1_OCTET_STRING *C3;
29
    ASN1_OCTET_STRING *C2;
30
};
31
32
ASN1_SEQUENCE(SM2_Ciphertext) = {
33
    ASN1_SIMPLE(SM2_Ciphertext, C1x, BIGNUM),
34
    ASN1_SIMPLE(SM2_Ciphertext, C1y, BIGNUM),
35
    ASN1_SIMPLE(SM2_Ciphertext, C3, ASN1_OCTET_STRING),
36
    ASN1_SIMPLE(SM2_Ciphertext, C2, ASN1_OCTET_STRING),
37
} ASN1_SEQUENCE_END(SM2_Ciphertext)
38
39
IMPLEMENT_ASN1_FUNCTIONS(SM2_Ciphertext)
40
41
static size_t ec_field_size(const EC_GROUP *group)
42
0
{
43
    /* Is there some simpler way to do this? */
44
0
    BIGNUM *p = BN_new();
45
0
    BIGNUM *a = BN_new();
46
0
    BIGNUM *b = BN_new();
47
0
    size_t field_size = 0;
48
49
0
    if (p == NULL || a == NULL || b == NULL)
50
0
       goto done;
51
52
0
    if (!EC_GROUP_get_curve(group, p, a, b, NULL))
53
0
        goto done;
54
0
    field_size = (BN_num_bits(p) + 7) / 8;
55
56
0
 done:
57
0
    BN_free(p);
58
0
    BN_free(a);
59
0
    BN_free(b);
60
61
0
    return field_size;
62
0
}
63
64
int sm2_plaintext_size(const unsigned char *ct, size_t ct_size, size_t *pt_size)
65
0
{
66
0
    struct SM2_Ciphertext_st *sm2_ctext = NULL;
67
68
0
    sm2_ctext = d2i_SM2_Ciphertext(NULL, &ct, ct_size);
69
70
0
    if (sm2_ctext == NULL) {
71
0
        SM2err(SM2_F_SM2_PLAINTEXT_SIZE, SM2_R_INVALID_ENCODING);
72
0
        return 0;
73
0
    }
74
75
0
    *pt_size = sm2_ctext->C2->length;
76
0
    SM2_Ciphertext_free(sm2_ctext);
77
78
0
    return 1;
79
0
}
80
81
int sm2_ciphertext_size(const EC_KEY *key, const EVP_MD *digest, size_t msg_len,
82
                        size_t *ct_size)
83
0
{
84
0
    const size_t field_size = ec_field_size(EC_KEY_get0_group(key));
85
0
    const int md_size = EVP_MD_size(digest);
86
0
    size_t sz;
87
88
0
    if (field_size == 0 || md_size < 0)
89
0
        return 0;
90
91
    /* Integer and string are simple type; set constructed = 0, means primitive and definite length encoding. */
92
0
    sz = 2 * ASN1_object_size(0, field_size + 1, V_ASN1_INTEGER)
93
0
         + ASN1_object_size(0, md_size, V_ASN1_OCTET_STRING)
94
0
         + ASN1_object_size(0, msg_len, V_ASN1_OCTET_STRING);
95
    /* Sequence is structured type; set constructed = 1, means constructed and definite length encoding. */
96
0
    *ct_size = ASN1_object_size(1, sz, V_ASN1_SEQUENCE);
97
98
0
    return 1;
99
0
}
100
101
int sm2_encrypt(const EC_KEY *key,
102
                const EVP_MD *digest,
103
                const uint8_t *msg,
104
                size_t msg_len, uint8_t *ciphertext_buf, size_t *ciphertext_len)
105
0
{
106
0
    int rc = 0, ciphertext_leni;
107
0
    size_t i;
108
0
    BN_CTX *ctx = NULL;
109
0
    BIGNUM *k = NULL;
110
0
    BIGNUM *x1 = NULL;
111
0
    BIGNUM *y1 = NULL;
112
0
    BIGNUM *x2 = NULL;
113
0
    BIGNUM *y2 = NULL;
114
0
    EVP_MD_CTX *hash = EVP_MD_CTX_new();
115
0
    struct SM2_Ciphertext_st ctext_struct;
116
0
    const EC_GROUP *group = EC_KEY_get0_group(key);
117
0
    const BIGNUM *order = EC_GROUP_get0_order(group);
118
0
    const EC_POINT *P = EC_KEY_get0_public_key(key);
119
0
    EC_POINT *kG = NULL;
120
0
    EC_POINT *kP = NULL;
121
0
    uint8_t *msg_mask = NULL;
122
0
    uint8_t *x2y2 = NULL;
123
0
    uint8_t *C3 = NULL;
124
0
    size_t field_size;
125
0
    const int C3_size = EVP_MD_size(digest);
126
127
    /* NULL these before any "goto done" */
128
0
    ctext_struct.C2 = NULL;
129
0
    ctext_struct.C3 = NULL;
130
131
0
    if (hash == NULL || C3_size <= 0) {
132
0
        SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
133
0
        goto done;
134
0
    }
135
136
0
    field_size = ec_field_size(group);
137
0
    if (field_size == 0) {
138
0
        SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
139
0
        goto done;
140
0
    }
141
142
0
    kG = EC_POINT_new(group);
143
0
    kP = EC_POINT_new(group);
144
0
    ctx = BN_CTX_new();
145
0
    if (kG == NULL || kP == NULL || ctx == NULL) {
146
0
        SM2err(SM2_F_SM2_ENCRYPT, ERR_R_MALLOC_FAILURE);
147
0
        goto done;
148
0
    }
149
150
0
    BN_CTX_start(ctx);
151
0
    k = BN_CTX_get(ctx);
152
0
    x1 = BN_CTX_get(ctx);
153
0
    x2 = BN_CTX_get(ctx);
154
0
    y1 = BN_CTX_get(ctx);
155
0
    y2 = BN_CTX_get(ctx);
156
157
0
    if (y2 == NULL) {
158
0
        SM2err(SM2_F_SM2_ENCRYPT, ERR_R_BN_LIB);
159
0
        goto done;
160
0
    }
161
162
0
    x2y2 = OPENSSL_zalloc(2 * field_size);
163
0
    C3 = OPENSSL_zalloc(C3_size);
164
165
0
    if (x2y2 == NULL || C3 == NULL) {
166
0
        SM2err(SM2_F_SM2_ENCRYPT, ERR_R_MALLOC_FAILURE);
167
0
        goto done;
168
0
    }
169
170
0
    memset(ciphertext_buf, 0, *ciphertext_len);
171
172
0
    if (!BN_priv_rand_range(k, order)) {
173
0
        SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
174
0
        goto done;
175
0
    }
176
177
0
    if (!EC_POINT_mul(group, kG, k, NULL, NULL, ctx)
178
0
            || !EC_POINT_get_affine_coordinates(group, kG, x1, y1, ctx)
179
0
            || !EC_POINT_mul(group, kP, NULL, P, k, ctx)
180
0
            || !EC_POINT_get_affine_coordinates(group, kP, x2, y2, ctx)) {
181
0
        SM2err(SM2_F_SM2_ENCRYPT, ERR_R_EC_LIB);
182
0
        goto done;
183
0
    }
184
185
0
    if (BN_bn2binpad(x2, x2y2, field_size) < 0
186
0
            || BN_bn2binpad(y2, x2y2 + field_size, field_size) < 0) {
187
0
        SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
188
0
        goto done;
189
0
    }
190
191
0
    msg_mask = OPENSSL_zalloc(msg_len);
192
0
    if (msg_mask == NULL) {
193
0
       SM2err(SM2_F_SM2_ENCRYPT, ERR_R_MALLOC_FAILURE);
194
0
       goto done;
195
0
   }
196
197
    /* X9.63 with no salt happens to match the KDF used in SM2 */
198
0
    if (!ecdh_KDF_X9_63(msg_mask, msg_len, x2y2, 2 * field_size, NULL, 0,
199
0
                        digest)) {
200
0
        SM2err(SM2_F_SM2_ENCRYPT, ERR_R_EVP_LIB);
201
0
        goto done;
202
0
    }
203
204
0
    for (i = 0; i != msg_len; ++i)
205
0
        msg_mask[i] ^= msg[i];
206
207
0
    if (EVP_DigestInit(hash, digest) == 0
208
0
            || EVP_DigestUpdate(hash, x2y2, field_size) == 0
209
0
            || EVP_DigestUpdate(hash, msg, msg_len) == 0
210
0
            || EVP_DigestUpdate(hash, x2y2 + field_size, field_size) == 0
211
0
            || EVP_DigestFinal(hash, C3, NULL) == 0) {
212
0
        SM2err(SM2_F_SM2_ENCRYPT, ERR_R_EVP_LIB);
213
0
        goto done;
214
0
    }
215
216
0
    ctext_struct.C1x = x1;
217
0
    ctext_struct.C1y = y1;
218
0
    ctext_struct.C3 = ASN1_OCTET_STRING_new();
219
0
    ctext_struct.C2 = ASN1_OCTET_STRING_new();
220
221
0
    if (ctext_struct.C3 == NULL || ctext_struct.C2 == NULL) {
222
0
       SM2err(SM2_F_SM2_ENCRYPT, ERR_R_MALLOC_FAILURE);
223
0
       goto done;
224
0
    }
225
0
    if (!ASN1_OCTET_STRING_set(ctext_struct.C3, C3, C3_size)
226
0
            || !ASN1_OCTET_STRING_set(ctext_struct.C2, msg_mask, msg_len)) {
227
0
        SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
228
0
        goto done;
229
0
    }
230
231
0
    ciphertext_leni = i2d_SM2_Ciphertext(&ctext_struct, &ciphertext_buf);
232
    /* Ensure cast to size_t is safe */
233
0
    if (ciphertext_leni < 0) {
234
0
        SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
235
0
        goto done;
236
0
    }
237
0
    *ciphertext_len = (size_t)ciphertext_leni;
238
239
0
    rc = 1;
240
241
0
 done:
242
0
    ASN1_OCTET_STRING_free(ctext_struct.C2);
243
0
    ASN1_OCTET_STRING_free(ctext_struct.C3);
244
0
    OPENSSL_free(msg_mask);
245
0
    OPENSSL_free(x2y2);
246
0
    OPENSSL_free(C3);
247
0
    EVP_MD_CTX_free(hash);
248
0
    BN_CTX_free(ctx);
249
0
    EC_POINT_free(kG);
250
0
    EC_POINT_free(kP);
251
0
    return rc;
252
0
}
253
254
int sm2_decrypt(const EC_KEY *key,
255
                const EVP_MD *digest,
256
                const uint8_t *ciphertext,
257
                size_t ciphertext_len, uint8_t *ptext_buf, size_t *ptext_len)
258
0
{
259
0
    int rc = 0;
260
0
    int i;
261
0
    BN_CTX *ctx = NULL;
262
0
    const EC_GROUP *group = EC_KEY_get0_group(key);
263
0
    EC_POINT *C1 = NULL;
264
0
    struct SM2_Ciphertext_st *sm2_ctext = NULL;
265
0
    BIGNUM *x2 = NULL;
266
0
    BIGNUM *y2 = NULL;
267
0
    uint8_t *x2y2 = NULL;
268
0
    uint8_t *computed_C3 = NULL;
269
0
    const size_t field_size = ec_field_size(group);
270
0
    const int hash_size = EVP_MD_size(digest);
271
0
    uint8_t *msg_mask = NULL;
272
0
    const uint8_t *C2 = NULL;
273
0
    const uint8_t *C3 = NULL;
274
0
    int msg_len = 0;
275
0
    EVP_MD_CTX *hash = NULL;
276
277
0
    if (field_size == 0 || hash_size <= 0)
278
0
       goto done;
279
280
0
    memset(ptext_buf, 0xFF, *ptext_len);
281
282
0
    sm2_ctext = d2i_SM2_Ciphertext(NULL, &ciphertext, ciphertext_len);
283
284
0
    if (sm2_ctext == NULL) {
285
0
        SM2err(SM2_F_SM2_DECRYPT, SM2_R_ASN1_ERROR);
286
0
        goto done;
287
0
    }
288
289
0
    if (sm2_ctext->C3->length != hash_size) {
290
0
        SM2err(SM2_F_SM2_DECRYPT, SM2_R_INVALID_ENCODING);
291
0
        goto done;
292
0
    }
293
294
0
    C2 = sm2_ctext->C2->data;
295
0
    C3 = sm2_ctext->C3->data;
296
0
    msg_len = sm2_ctext->C2->length;
297
0
    if (*ptext_len < (size_t)msg_len) {
298
0
        SM2err(SM2_F_SM2_DECRYPT, SM2_R_BUFFER_TOO_SMALL);
299
0
        goto done;
300
0
    }
301
302
0
    ctx = BN_CTX_new();
303
0
    if (ctx == NULL) {
304
0
        SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);
305
0
        goto done;
306
0
    }
307
308
0
    BN_CTX_start(ctx);
309
0
    x2 = BN_CTX_get(ctx);
310
0
    y2 = BN_CTX_get(ctx);
311
312
0
    if (y2 == NULL) {
313
0
        SM2err(SM2_F_SM2_DECRYPT, ERR_R_BN_LIB);
314
0
        goto done;
315
0
    }
316
317
0
    msg_mask = OPENSSL_zalloc(msg_len);
318
0
    x2y2 = OPENSSL_zalloc(2 * field_size);
319
0
    computed_C3 = OPENSSL_zalloc(hash_size);
320
321
0
    if (msg_mask == NULL || x2y2 == NULL || computed_C3 == NULL) {
322
0
        SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);
323
0
        goto done;
324
0
    }
325
326
0
    C1 = EC_POINT_new(group);
327
0
    if (C1 == NULL) {
328
0
        SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);
329
0
        goto done;
330
0
    }
331
332
0
    if (!EC_POINT_set_affine_coordinates(group, C1, sm2_ctext->C1x,
333
0
                                         sm2_ctext->C1y, ctx)
334
0
            || !EC_POINT_mul(group, C1, NULL, C1, EC_KEY_get0_private_key(key),
335
0
                             ctx)
336
0
            || !EC_POINT_get_affine_coordinates(group, C1, x2, y2, ctx)) {
337
0
        SM2err(SM2_F_SM2_DECRYPT, ERR_R_EC_LIB);
338
0
        goto done;
339
0
    }
340
341
0
    if (BN_bn2binpad(x2, x2y2, field_size) < 0
342
0
            || BN_bn2binpad(y2, x2y2 + field_size, field_size) < 0
343
0
            || !ecdh_KDF_X9_63(msg_mask, msg_len, x2y2, 2 * field_size, NULL, 0,
344
0
                               digest)) {
345
0
        SM2err(SM2_F_SM2_DECRYPT, ERR_R_INTERNAL_ERROR);
346
0
        goto done;
347
0
    }
348
349
0
    for (i = 0; i != msg_len; ++i)
350
0
        ptext_buf[i] = C2[i] ^ msg_mask[i];
351
352
0
    hash = EVP_MD_CTX_new();
353
0
    if (hash == NULL) {
354
0
        SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);
355
0
        goto done;
356
0
    }
357
358
0
    if (!EVP_DigestInit(hash, digest)
359
0
            || !EVP_DigestUpdate(hash, x2y2, field_size)
360
0
            || !EVP_DigestUpdate(hash, ptext_buf, msg_len)
361
0
            || !EVP_DigestUpdate(hash, x2y2 + field_size, field_size)
362
0
            || !EVP_DigestFinal(hash, computed_C3, NULL)) {
363
0
        SM2err(SM2_F_SM2_DECRYPT, ERR_R_EVP_LIB);
364
0
        goto done;
365
0
    }
366
367
0
    if (CRYPTO_memcmp(computed_C3, C3, hash_size) != 0) {
368
0
        SM2err(SM2_F_SM2_DECRYPT, SM2_R_INVALID_DIGEST);
369
0
        goto done;
370
0
    }
371
372
0
    rc = 1;
373
0
    *ptext_len = msg_len;
374
375
0
 done:
376
0
    if (rc == 0)
377
0
        memset(ptext_buf, 0, *ptext_len);
378
379
0
    OPENSSL_free(msg_mask);
380
0
    OPENSSL_free(x2y2);
381
0
    OPENSSL_free(computed_C3);
382
0
    EC_POINT_free(C1);
383
0
    BN_CTX_free(ctx);
384
0
    SM2_Ciphertext_free(sm2_ctext);
385
0
    EVP_MD_CTX_free(hash);
386
387
0
    return rc;
388
0
}