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