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