Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/sm2/sm2_crypt.c

raw
1 /*
2 * Copyright 2017-2026 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 int 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
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
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, (long)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
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 int field_size = ec_field_size(EC_KEY_get0_group(key));
91 const int md_size = EVP_MD_get_size(digest);
92 int sz;
93
94 if (field_size == 0 || md_size <= 0 || msg_len > INT_MAX / 2)
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, (int)msg_len, V_ASN1_OCTET_STRING);
101 /* Sequence is structured type; set constructed = 1, means constructed and definite length encoding. */
102 *ct_size = (size_t)ASN1_object_size(1, sz, V_ASN1_SEQUENCE);
103
104 return 1;
105 }
106
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 int 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 (msg_len > INT_MAX / 2) {
141 ERR_raise(ERR_LIB_SM2, ERR_R_PASSED_INVALID_ARGUMENT);
142 goto done;
143 }
144
145 if (hash == NULL || C3_size <= 0) {
146 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
147 goto done;
148 }
149
150 field_size = ec_field_size(group);
151 if (field_size == 0) {
152 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
153 goto done;
154 }
155
156 kG = EC_POINT_new(group);
157 kP = EC_POINT_new(group);
158 if (kG == NULL || kP == NULL) {
159 ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
160 goto done;
161 }
162 ctx = BN_CTX_new_ex(libctx);
163 if (ctx == NULL) {
164 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
165 goto done;
166 }
167
168 BN_CTX_start(ctx);
169 k = BN_CTX_get(ctx);
170 x1 = BN_CTX_get(ctx);
171 x2 = BN_CTX_get(ctx);
172 y1 = BN_CTX_get(ctx);
173 y2 = BN_CTX_get(ctx);
174
175 if (y2 == NULL) {
176 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
177 goto done;
178 }
179
180 x2y2 = OPENSSL_calloc(2, field_size);
181 C3 = OPENSSL_zalloc(C3_size);
182
183 if (x2y2 == NULL || C3 == NULL)
184 goto done;
185
186 memset(ciphertext_buf, 0, *ciphertext_len);
187
188 msg_mask = OPENSSL_zalloc(msg_len);
189 if (msg_mask == NULL)
190 goto done;
191
192 again:
193 if (!BN_priv_rand_range_ex(k, order, 0, ctx)) {
194 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
195 goto done;
196 }
197
198 if (!EC_POINT_mul(group, kG, k, NULL, NULL, ctx)
199 || !EC_POINT_get_affine_coordinates(group, kG, x1, y1, ctx)
200 || !EC_POINT_mul(group, kP, NULL, P, k, ctx)
201 || !EC_POINT_get_affine_coordinates(group, kP, x2, y2, ctx)) {
202 ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
203 goto done;
204 }
205
206 if (BN_bn2binpad(x2, x2y2, field_size) < 0
207 || BN_bn2binpad(y2, x2y2 + field_size, field_size) < 0) {
208 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
209 goto done;
210 }
211
212 /* X9.63 with no salt happens to match the KDF used in SM2 */
213 if (!ossl_ecdh_kdf_X9_63(msg_mask, msg_len, x2y2, 2 * field_size, NULL, 0,
214 digest, libctx, propq)) {
215 ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
216 goto done;
217 }
218
219 if (is_all_zeros(msg_mask, msg_len)) {
220 memset(x2y2, 0, 2 * field_size);
221 goto again;
222 }
223
224 for (i = 0; i != msg_len; ++i)
225 msg_mask[i] ^= msg[i];
226
227 fetched_digest = EVP_MD_fetch(libctx, EVP_MD_get0_name(digest), propq);
228 if (fetched_digest == NULL) {
229 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
230 goto done;
231 }
232 if (EVP_DigestInit(hash, fetched_digest) == 0
233 || EVP_DigestUpdate(hash, x2y2, field_size) == 0
234 || EVP_DigestUpdate(hash, msg, msg_len) == 0
235 || EVP_DigestUpdate(hash, x2y2 + field_size, field_size) == 0
236 || EVP_DigestFinal(hash, C3, NULL) == 0) {
237 ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
238 goto done;
239 }
240
241 ctext_struct.C1x = x1;
242 ctext_struct.C1y = y1;
243 ctext_struct.C3 = ASN1_OCTET_STRING_new();
244 ctext_struct.C2 = ASN1_OCTET_STRING_new();
245
246 if (ctext_struct.C3 == NULL || ctext_struct.C2 == NULL) {
247 ERR_raise(ERR_LIB_SM2, ERR_R_ASN1_LIB);
248 goto done;
249 }
250 if (!ASN1_OCTET_STRING_set(ctext_struct.C3, C3, C3_size)
251 || !ASN1_OCTET_STRING_set(ctext_struct.C2, msg_mask, (int)msg_len)) {
252 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
253 goto done;
254 }
255
256 ciphertext_leni = i2d_SM2_Ciphertext(&ctext_struct, NULL);
257 /* Ensure cast to size_t is safe */
258 if (ciphertext_leni < 0) {
259 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
260 goto done;
261 }
262
263 if (*ciphertext_len < (size_t)ciphertext_leni) {
264 ERR_raise(ERR_LIB_SM2, SM2_R_BUFFER_TOO_SMALL);
265 goto done;
266 }
267
268 ciphertext_leni = i2d_SM2_Ciphertext(&ctext_struct, &ciphertext_buf);
269 if (ciphertext_leni < 0) {
270 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
271 goto done;
272 }
273 *ciphertext_len = (size_t)ciphertext_leni;
274
275 rc = 1;
276
277 done:
278 EVP_MD_free(fetched_digest);
279 ASN1_OCTET_STRING_free(ctext_struct.C2);
280 ASN1_OCTET_STRING_free(ctext_struct.C3);
281 OPENSSL_free(msg_mask);
282 OPENSSL_free(x2y2);
283 OPENSSL_free(C3);
284 EVP_MD_CTX_free(hash);
285 BN_CTX_end(ctx);
286 BN_CTX_free(ctx);
287 EC_POINT_free(kG);
288 EC_POINT_free(kP);
289 return rc;
290 }
291
292 int ossl_sm2_decrypt(const EC_KEY *key,
293 const EVP_MD *digest,
294 const uint8_t *ciphertext, size_t ciphertext_len,
295 uint8_t *ptext_buf, size_t *ptext_len)
296 {
297 int rc = 0;
298 int i;
299 BN_CTX *ctx = NULL;
300 const EC_GROUP *group = EC_KEY_get0_group(key);
301 EC_POINT *C1 = NULL;
302 struct SM2_Ciphertext_st *sm2_ctext = NULL;
303 BIGNUM *x2 = NULL;
304 BIGNUM *y2 = NULL;
305 uint8_t *x2y2 = NULL;
306 uint8_t *computed_C3 = NULL;
307 const int field_size = ec_field_size(group);
308 const int hash_size = EVP_MD_get_size(digest);
309 uint8_t *msg_mask = NULL;
310 const uint8_t *C2 = NULL;
311 const uint8_t *C3 = NULL;
312 int msg_len = 0;
313 EVP_MD_CTX *hash = NULL;
314 OSSL_LIB_CTX *libctx = ossl_ec_key_get_libctx(key);
315 const char *propq = ossl_ec_key_get0_propq(key);
316
317 if (field_size == 0 || hash_size <= 0 || ciphertext_len > LONG_MAX)
318 goto done;
319
320 memset(ptext_buf, 0xFF, *ptext_len);
321
322 sm2_ctext = d2i_SM2_Ciphertext(NULL, &ciphertext, (long)ciphertext_len);
323
324 if (sm2_ctext == NULL) {
325 ERR_raise(ERR_LIB_SM2, SM2_R_ASN1_ERROR);
326 goto done;
327 }
328
329 if (sm2_ctext->C3->length != hash_size) {
330 ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_ENCODING);
331 goto done;
332 }
333
334 C2 = sm2_ctext->C2->data;
335 C3 = sm2_ctext->C3->data;
336 msg_len = sm2_ctext->C2->length;
337 if (*ptext_len < (size_t)msg_len) {
338 ERR_raise(ERR_LIB_SM2, SM2_R_BUFFER_TOO_SMALL);
339 goto done;
340 }
341
342 ctx = BN_CTX_new_ex(libctx);
343 if (ctx == NULL) {
344 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
345 goto done;
346 }
347
348 BN_CTX_start(ctx);
349 x2 = BN_CTX_get(ctx);
350 y2 = BN_CTX_get(ctx);
351
352 if (y2 == NULL) {
353 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
354 goto done;
355 }
356
357 msg_mask = OPENSSL_zalloc(msg_len);
358 x2y2 = OPENSSL_calloc(2, field_size);
359 computed_C3 = OPENSSL_zalloc(hash_size);
360
361 if (msg_mask == NULL || x2y2 == NULL || computed_C3 == NULL)
362 goto done;
363
364 C1 = EC_POINT_new(group);
365 if (C1 == NULL) {
366 ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
367 goto done;
368 }
369
370 if (!EC_POINT_set_affine_coordinates(group, C1, sm2_ctext->C1x,
371 sm2_ctext->C1y, ctx)
372 || !EC_POINT_mul(group, C1, NULL, C1, EC_KEY_get0_private_key(key),
373 ctx)
374 || !EC_POINT_get_affine_coordinates(group, C1, x2, y2, ctx)) {
375 ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
376 goto done;
377 }
378
379 if (BN_bn2binpad(x2, x2y2, field_size) < 0
380 || BN_bn2binpad(y2, x2y2 + field_size, field_size) < 0
381 || !ossl_ecdh_kdf_X9_63(msg_mask, msg_len, x2y2, 2 * field_size,
382 NULL, 0, digest, libctx, propq)) {
383 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
384 goto done;
385 }
386
387 if (is_all_zeros(msg_mask, msg_len)) {
388 ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_ENCODING);
389 goto done;
390 }
391
392 for (i = 0; i != msg_len; ++i)
393 ptext_buf[i] = C2[i] ^ msg_mask[i];
394
395 hash = EVP_MD_CTX_new();
396 if (hash == NULL) {
397 ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
398 goto done;
399 }
400
401 if (!EVP_DigestInit(hash, digest)
402 || !EVP_DigestUpdate(hash, x2y2, field_size)
403 || !EVP_DigestUpdate(hash, ptext_buf, msg_len)
404 || !EVP_DigestUpdate(hash, x2y2 + field_size, field_size)
405 || !EVP_DigestFinal(hash, computed_C3, NULL)) {
406 ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
407 goto done;
408 }
409
410 if (CRYPTO_memcmp(computed_C3, C3, hash_size) != 0) {
411 ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_DIGEST);
412 goto done;
413 }
414
415 rc = 1;
416 *ptext_len = msg_len;
417
418 done:
419 if (rc == 0)
420 memset(ptext_buf, 0, *ptext_len);
421
422 OPENSSL_free(msg_mask);
423 OPENSSL_free(x2y2);
424 OPENSSL_free(computed_C3);
425 EC_POINT_free(C1);
426 BN_CTX_end(ctx);
427 BN_CTX_free(ctx);
428 SM2_Ciphertext_free(sm2_ctext);
429 EVP_MD_CTX_free(hash);
430
431 return rc;
432 }