Coverage Report

Created: 2023-06-08 06:41

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