]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/sm2/sm2_crypt.c
Update copyright year
[thirdparty/openssl.git] / crypto / sm2 / sm2_crypt.c
CommitLineData
3d328a44 1/*
33388b44 2 * Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved.
3d328a44
JL
3 * Copyright 2017 Ribose Inc. All Rights Reserved.
4 * Ported from Ribose contributions from Botan.
5 *
f9f859ad 6 * Licensed under the Apache License 2.0 (the "License"). You may not use
3d328a44
JL
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
579422c8
P
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
25f2138b
DMSP
18#include "crypto/sm2.h"
19#include "crypto/sm2err.h"
20#include "crypto/ec.h" /* ecdh_KDF_X9_63() */
2398404e 21#include <openssl/err.h>
3d328a44
JL
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
28typedef struct SM2_Ciphertext_st SM2_Ciphertext;
29DECLARE_ASN1_FUNCTIONS(SM2_Ciphertext)
30
31struct SM2_Ciphertext_st {
32 BIGNUM *C1x;
33 BIGNUM *C1y;
34 ASN1_OCTET_STRING *C3;
35 ASN1_OCTET_STRING *C2;
36};
37
38ASN1_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
45IMPLEMENT_ASN1_FUNCTIONS(SM2_Ciphertext)
46
2167239a 47static size_t ec_field_size(const EC_GROUP *group)
3d328a44
JL
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
9cc570d4 58 if (!EC_GROUP_get_curve(group, p, a, b, NULL))
bdd92f4d 59 goto done;
3d328a44
JL
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
2167239a 70int sm2_plaintext_size(const EC_KEY *key, const EVP_MD *digest, size_t msg_len,
e14d6cf6 71 size_t *pt_size)
4e664750 72{
2167239a 73 const size_t field_size = ec_field_size(EC_KEY_get0_group(key));
e14d6cf6
MC
74 const int md_size = EVP_MD_size(digest);
75 size_t overhead;
4e664750 76
e14d6cf6 77 if (md_size < 0) {
245be530 78 SM2err(SM2_F_SM2_PLAINTEXT_SIZE, SM2_R_INVALID_DIGEST);
e14d6cf6
MC
79 return 0;
80 }
81 if (field_size == 0) {
245be530 82 SM2err(SM2_F_SM2_PLAINTEXT_SIZE, SM2_R_INVALID_FIELD);
e14d6cf6
MC
83 return 0;
84 }
4e664750 85
e14d6cf6 86 overhead = 10 + 2 * field_size + (size_t)md_size;
245be530
MC
87 if (msg_len <= overhead) {
88 SM2err(SM2_F_SM2_PLAINTEXT_SIZE, SM2_R_INVALID_ENCODING);
e14d6cf6
MC
89 return 0;
90 }
91
92 *pt_size = msg_len - overhead;
93 return 1;
4e664750
JL
94}
95
2167239a 96int sm2_ciphertext_size(const EC_KEY *key, const EVP_MD *digest, size_t msg_len,
e14d6cf6 97 size_t *ct_size)
3d328a44 98{
2167239a 99 const size_t field_size = ec_field_size(EC_KEY_get0_group(key));
e14d6cf6 100 const int md_size = EVP_MD_size(digest);
307a494e 101 size_t sz;
e14d6cf6
MC
102
103 if (field_size == 0 || md_size < 0)
104 return 0;
105
307a494e 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
e14d6cf6 113 return 1;
3d328a44
JL
114}
115
2167239a 116int sm2_encrypt(const EC_KEY *key,
3d328a44
JL
117 const EVP_MD *digest,
118 const uint8_t *msg,
119 size_t msg_len, uint8_t *ciphertext_buf, size_t *ciphertext_len)
120{
44d3845d 121 int rc = 0, ciphertext_leni;
3d328a44
JL
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;
3d328a44 129 EVP_MD_CTX *hash = EVP_MD_CTX_new();
3d328a44
JL
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;
3d328a44
JL
137 uint8_t *x2y2 = NULL;
138 uint8_t *C3 = NULL;
1829ff4b
MC
139 size_t field_size;
140 const int C3_size = EVP_MD_size(digest);
3d328a44 141
44d3845d
MC
142 /* NULL these before any "goto done" */
143 ctext_struct.C2 = NULL;
144 ctext_struct.C3 = NULL;
145
1829ff4b
MC
146 if (hash == NULL || C3_size <= 0) {
147 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
148 goto done;
149 }
150
151 field_size = ec_field_size(group);
152 if (field_size == 0) {
44d3845d
MC
153 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
154 goto done;
155 }
3d328a44
JL
156
157 kG = EC_POINT_new(group);
158 kP = EC_POINT_new(group);
3d328a44 159 ctx = BN_CTX_new();
44d3845d
MC
160 if (kG == NULL || kP == NULL || ctx == NULL) {
161 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_MALLOC_FAILURE);
162 goto done;
163 }
3d328a44
JL
164
165 BN_CTX_start(ctx);
166 k = BN_CTX_get(ctx);
167 x1 = BN_CTX_get(ctx);
168 x2 = BN_CTX_get(ctx);
169 y1 = BN_CTX_get(ctx);
170 y2 = BN_CTX_get(ctx);
171
44d3845d 172 if (y2 == NULL) {
2398404e
JL
173 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_BN_LIB);
174 goto done;
44d3845d 175 }
3d328a44
JL
176
177 x2y2 = OPENSSL_zalloc(2 * field_size);
178 C3 = OPENSSL_zalloc(C3_size);
179
44d3845d 180 if (x2y2 == NULL || C3 == NULL) {
2398404e
JL
181 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_MALLOC_FAILURE);
182 goto done;
44d3845d 183 }
3d328a44
JL
184
185 memset(ciphertext_buf, 0, *ciphertext_len);
186
44d3845d
MC
187 if (!BN_priv_rand_range(k, order)) {
188 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
3d328a44 189 goto done;
44d3845d 190 }
3d328a44 191
44d3845d 192 if (!EC_POINT_mul(group, kG, k, NULL, NULL, ctx)
9cc570d4 193 || !EC_POINT_get_affine_coordinates(group, kG, x1, y1, ctx)
44d3845d 194 || !EC_POINT_mul(group, kP, NULL, P, k, ctx)
9cc570d4 195 || !EC_POINT_get_affine_coordinates(group, kP, x2, y2, ctx)) {
2398404e 196 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_EC_LIB);
3d328a44 197 goto done;
44d3845d 198 }
3d328a44 199
44d3845d
MC
200 if (BN_bn2binpad(x2, x2y2, field_size) < 0
201 || BN_bn2binpad(y2, x2y2 + field_size, field_size) < 0) {
202 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
3d328a44 203 goto done;
44d3845d 204 }
3d328a44
JL
205
206 msg_mask = OPENSSL_zalloc(msg_len);
44d3845d 207 if (msg_mask == NULL) {
2398404e 208 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_MALLOC_FAILURE);
3d328a44 209 goto done;
44d3845d 210 }
3d328a44
JL
211
212 /* X9.63 with no salt happens to match the KDF used in SM2 */
ffd89124 213 if (!ecdh_KDF_X9_63(msg_mask, msg_len, x2y2, 2 * field_size, NULL, 0,
44d3845d
MC
214 digest)) {
215 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_EVP_LIB);
3d328a44 216 goto done;
44d3845d 217 }
3d328a44
JL
218
219 for (i = 0; i != msg_len; ++i)
220 msg_mask[i] ^= msg[i];
221
44d3845d
MC
222 if (EVP_DigestInit(hash, digest) == 0
223 || EVP_DigestUpdate(hash, x2y2, field_size) == 0
224 || EVP_DigestUpdate(hash, msg, msg_len) == 0
225 || EVP_DigestUpdate(hash, x2y2 + field_size, field_size) == 0
226 || EVP_DigestFinal(hash, C3, NULL) == 0) {
2398404e 227 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_EVP_LIB);
3d328a44 228 goto done;
44d3845d 229 }
3d328a44
JL
230
231 ctext_struct.C1x = x1;
232 ctext_struct.C1y = y1;
233 ctext_struct.C3 = ASN1_OCTET_STRING_new();
3d328a44 234 ctext_struct.C2 = ASN1_OCTET_STRING_new();
3d328a44 235
44d3845d
MC
236 if (ctext_struct.C3 == NULL || ctext_struct.C2 == NULL) {
237 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_MALLOC_FAILURE);
238 goto done;
239 }
240 if (!ASN1_OCTET_STRING_set(ctext_struct.C3, C3, C3_size)
241 || !ASN1_OCTET_STRING_set(ctext_struct.C2, msg_mask, msg_len)) {
242 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
243 goto done;
244 }
3d328a44 245
44d3845d
MC
246 ciphertext_leni = i2d_SM2_Ciphertext(&ctext_struct, &ciphertext_buf);
247 /* Ensure cast to size_t is safe */
248 if (ciphertext_leni < 0) {
249 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
250 goto done;
251 }
252 *ciphertext_len = (size_t)ciphertext_leni;
3d328a44
JL
253
254 rc = 1;
255
256 done:
44d3845d
MC
257 ASN1_OCTET_STRING_free(ctext_struct.C2);
258 ASN1_OCTET_STRING_free(ctext_struct.C3);
3d328a44
JL
259 OPENSSL_free(msg_mask);
260 OPENSSL_free(x2y2);
261 OPENSSL_free(C3);
262 EVP_MD_CTX_free(hash);
263 BN_CTX_free(ctx);
264 EC_POINT_free(kG);
265 EC_POINT_free(kP);
266 return rc;
267}
268
2167239a 269int sm2_decrypt(const EC_KEY *key,
3d328a44
JL
270 const EVP_MD *digest,
271 const uint8_t *ciphertext,
272 size_t ciphertext_len, uint8_t *ptext_buf, size_t *ptext_len)
273{
274 int rc = 0;
275 int i;
3d328a44
JL
276 BN_CTX *ctx = NULL;
277 const EC_GROUP *group = EC_KEY_get0_group(key);
278 EC_POINT *C1 = NULL;
279 struct SM2_Ciphertext_st *sm2_ctext = NULL;
280 BIGNUM *x2 = NULL;
281 BIGNUM *y2 = NULL;
3d328a44
JL
282 uint8_t *x2y2 = NULL;
283 uint8_t *computed_C3 = NULL;
2167239a 284 const size_t field_size = ec_field_size(group);
3d328a44 285 const int hash_size = EVP_MD_size(digest);
3d328a44
JL
286 uint8_t *msg_mask = NULL;
287 const uint8_t *C2 = NULL;
288 const uint8_t *C3 = NULL;
289 int msg_len = 0;
290 EVP_MD_CTX *hash = NULL;
291
1829ff4b 292 if (field_size == 0 || hash_size <= 0)
3d328a44
JL
293 goto done;
294
295 memset(ptext_buf, 0xFF, *ptext_len);
296
297 sm2_ctext = d2i_SM2_Ciphertext(NULL, &ciphertext, ciphertext_len);
298
245be530 299 if (sm2_ctext == NULL) {
2398404e 300 SM2err(SM2_F_SM2_DECRYPT, SM2_R_ASN1_ERROR);
3d328a44 301 goto done;
245be530 302 }
3d328a44 303
245be530 304 if (sm2_ctext->C3->length != hash_size) {
2398404e 305 SM2err(SM2_F_SM2_DECRYPT, SM2_R_INVALID_ENCODING);
3d328a44 306 goto done;
245be530 307 }
3d328a44
JL
308
309 C2 = sm2_ctext->C2->data;
310 C3 = sm2_ctext->C3->data;
311 msg_len = sm2_ctext->C2->length;
312
313 ctx = BN_CTX_new();
245be530 314 if (ctx == NULL) {
2398404e
JL
315 SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);
316 goto done;
245be530 317 }
3d328a44
JL
318
319 BN_CTX_start(ctx);
320 x2 = BN_CTX_get(ctx);
321 y2 = BN_CTX_get(ctx);
322
245be530 323 if (y2 == NULL) {
2398404e
JL
324 SM2err(SM2_F_SM2_DECRYPT, ERR_R_BN_LIB);
325 goto done;
245be530 326 }
3d328a44
JL
327
328 msg_mask = OPENSSL_zalloc(msg_len);
329 x2y2 = OPENSSL_zalloc(2 * field_size);
330 computed_C3 = OPENSSL_zalloc(hash_size);
331
245be530 332 if (msg_mask == NULL || x2y2 == NULL || computed_C3 == NULL) {
2398404e
JL
333 SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);
334 goto done;
245be530 335 }
3d328a44
JL
336
337 C1 = EC_POINT_new(group);
245be530 338 if (C1 == NULL) {
2398404e 339 SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);
3d328a44 340 goto done;
245be530 341 }
3d328a44 342
9cc570d4
MC
343 if (!EC_POINT_set_affine_coordinates(group, C1, sm2_ctext->C1x,
344 sm2_ctext->C1y, ctx)
245be530
MC
345 || !EC_POINT_mul(group, C1, NULL, C1, EC_KEY_get0_private_key(key),
346 ctx)
9cc570d4 347 || !EC_POINT_get_affine_coordinates(group, C1, x2, y2, ctx)) {
2398404e 348 SM2err(SM2_F_SM2_DECRYPT, ERR_R_EC_LIB);
3d328a44 349 goto done;
245be530 350 }
3d328a44 351
245be530
MC
352 if (BN_bn2binpad(x2, x2y2, field_size) < 0
353 || BN_bn2binpad(y2, x2y2 + field_size, field_size) < 0
ffd89124 354 || !ecdh_KDF_X9_63(msg_mask, msg_len, x2y2, 2 * field_size, NULL, 0,
245be530
MC
355 digest)) {
356 SM2err(SM2_F_SM2_DECRYPT, ERR_R_INTERNAL_ERROR);
3d328a44 357 goto done;
245be530 358 }
3d328a44
JL
359
360 for (i = 0; i != msg_len; ++i)
361 ptext_buf[i] = C2[i] ^ msg_mask[i];
362
363 hash = EVP_MD_CTX_new();
245be530 364 if (hash == NULL) {
2398404e
JL
365 SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);
366 goto done;
245be530 367 }
3d328a44 368
245be530
MC
369 if (!EVP_DigestInit(hash, digest)
370 || !EVP_DigestUpdate(hash, x2y2, field_size)
371 || !EVP_DigestUpdate(hash, ptext_buf, msg_len)
372 || !EVP_DigestUpdate(hash, x2y2 + field_size, field_size)
373 || !EVP_DigestFinal(hash, computed_C3, NULL)) {
2398404e 374 SM2err(SM2_F_SM2_DECRYPT, ERR_R_EVP_LIB);
3d328a44 375 goto done;
245be530 376 }
3d328a44 377
245be530
MC
378 if (CRYPTO_memcmp(computed_C3, C3, hash_size) != 0) {
379 SM2err(SM2_F_SM2_DECRYPT, SM2_R_INVALID_DIGEST);
3d328a44 380 goto done;
245be530 381 }
3d328a44
JL
382
383 rc = 1;
dceb99a5 384 *ptext_len = msg_len;
3d328a44
JL
385
386 done:
3d328a44
JL
387 if (rc == 0)
388 memset(ptext_buf, 0, *ptext_len);
389
390 OPENSSL_free(msg_mask);
391 OPENSSL_free(x2y2);
392 OPENSSL_free(computed_C3);
393 EC_POINT_free(C1);
394 BN_CTX_free(ctx);
395 SM2_Ciphertext_free(sm2_ctext);
396 EVP_MD_CTX_free(hash);
397
398 return rc;
399}