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