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.
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
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>
21 typedef struct SM2_Ciphertext_st SM2_Ciphertext;
22 DECLARE_ASN1_FUNCTIONS(SM2_Ciphertext)
24 struct SM2_Ciphertext_st {
27 ASN1_OCTET_STRING *C3;
28 ASN1_OCTET_STRING *C2;
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)
38 IMPLEMENT_ASN1_FUNCTIONS(SM2_Ciphertext)
40 static size_t EC_field_size(const EC_GROUP *group)
42 /* Is there some simpler way to do this? */
46 size_t field_size = 0;
48 if (p == NULL || a == NULL || b == NULL)
51 EC_GROUP_get_curve_GFp(group, p, a, b, NULL);
52 field_size = (BN_num_bits(p) + 7) / 8;
62 size_t SM2_plaintext_size(const EC_KEY *key, const EVP_MD *digest, size_t msg_len)
64 const size_t field_size = EC_field_size(EC_KEY_get0_group(key));
65 const size_t md_size = EVP_MD_size(digest);
67 const size_t overhead = 10 + 2 * field_size + md_size;
68 if(msg_len <= overhead)
71 return msg_len - overhead;
74 size_t SM2_ciphertext_size(const EC_KEY *key, const EVP_MD *digest, size_t msg_len)
76 const size_t field_size = EC_field_size(EC_KEY_get0_group(key));
77 const size_t md_size = EVP_MD_size(digest);
78 return 10 + 2 * field_size + md_size + msg_len;
81 int SM2_encrypt(const EC_KEY *key,
84 size_t msg_len, uint8_t *ciphertext_buf, size_t *ciphertext_len)
86 int rc = 0, ciphertext_leni;
94 EVP_MD_CTX *hash = EVP_MD_CTX_new();
95 struct SM2_Ciphertext_st ctext_struct;
96 const EC_GROUP *group = EC_KEY_get0_group(key);
97 const BIGNUM *order = EC_GROUP_get0_order(group);
98 const EC_POINT *P = EC_KEY_get0_public_key(key);
101 uint8_t *msg_mask = NULL;
102 uint8_t *x2y2 = NULL;
104 const size_t field_size = EC_field_size(group);
105 const size_t C3_size = EVP_MD_size(digest);
107 /* NULL these before any "goto done" */
108 ctext_struct.C2 = NULL;
109 ctext_struct.C3 = NULL;
117 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
121 kG = EC_POINT_new(group);
122 kP = EC_POINT_new(group);
124 if (kG == NULL || kP == NULL || ctx == NULL) {
125 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_MALLOC_FAILURE);
131 x1 = BN_CTX_get(ctx);
132 x2 = BN_CTX_get(ctx);
133 y1 = BN_CTX_get(ctx);
134 y2 = BN_CTX_get(ctx);
137 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_BN_LIB);
141 x2y2 = OPENSSL_zalloc(2 * field_size);
142 C3 = OPENSSL_zalloc(C3_size);
144 if (x2y2 == NULL || C3 == NULL) {
145 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_MALLOC_FAILURE);
149 memset(ciphertext_buf, 0, *ciphertext_len);
151 if (!BN_priv_rand_range(k, order)) {
152 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
156 if (!EC_POINT_mul(group, kG, k, NULL, NULL, ctx)
157 || !EC_POINT_get_affine_coordinates_GFp(group, kG, x1, y1, ctx)
158 || !EC_POINT_mul(group, kP, NULL, P, k, ctx)
159 || !EC_POINT_get_affine_coordinates_GFp(group, kP, x2, y2, ctx)) {
160 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_EC_LIB);
164 if (BN_bn2binpad(x2, x2y2, field_size) < 0
165 || BN_bn2binpad(y2, x2y2 + field_size, field_size) < 0) {
166 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
170 msg_mask = OPENSSL_zalloc(msg_len);
171 if (msg_mask == NULL) {
172 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_MALLOC_FAILURE);
176 /* X9.63 with no salt happens to match the KDF used in SM2 */
177 if (!ECDH_KDF_X9_62(msg_mask, msg_len, x2y2, 2 * field_size, NULL, 0,
179 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_EVP_LIB);
183 for (i = 0; i != msg_len; ++i)
184 msg_mask[i] ^= msg[i];
186 if (EVP_DigestInit(hash, digest) == 0
187 || EVP_DigestUpdate(hash, x2y2, field_size) == 0
188 || EVP_DigestUpdate(hash, msg, msg_len) == 0
189 || EVP_DigestUpdate(hash, x2y2 + field_size, field_size) == 0
190 || EVP_DigestFinal(hash, C3, NULL) == 0) {
191 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_EVP_LIB);
195 ctext_struct.C1x = x1;
196 ctext_struct.C1y = y1;
197 ctext_struct.C3 = ASN1_OCTET_STRING_new();
198 ctext_struct.C2 = ASN1_OCTET_STRING_new();
200 if (ctext_struct.C3 == NULL || ctext_struct.C2 == NULL) {
201 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_MALLOC_FAILURE);
204 if (!ASN1_OCTET_STRING_set(ctext_struct.C3, C3, C3_size)
205 || !ASN1_OCTET_STRING_set(ctext_struct.C2, msg_mask, msg_len)) {
206 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
210 ciphertext_leni = i2d_SM2_Ciphertext(&ctext_struct, &ciphertext_buf);
211 /* Ensure cast to size_t is safe */
212 if (ciphertext_leni < 0) {
213 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
216 *ciphertext_len = (size_t)ciphertext_leni;
221 ASN1_OCTET_STRING_free(ctext_struct.C2);
222 ASN1_OCTET_STRING_free(ctext_struct.C3);
223 OPENSSL_free(msg_mask);
226 EVP_MD_CTX_free(hash);
233 int SM2_decrypt(const EC_KEY *key,
234 const EVP_MD *digest,
235 const uint8_t *ciphertext,
236 size_t ciphertext_len, uint8_t *ptext_buf, size_t *ptext_len)
242 const EC_GROUP *group = EC_KEY_get0_group(key);
244 struct SM2_Ciphertext_st *sm2_ctext = NULL;
248 uint8_t *x2y2 = NULL;
249 uint8_t *computed_C3 = NULL;
251 const size_t field_size = EC_field_size(group);
252 const int hash_size = EVP_MD_size(digest);
254 uint8_t *msg_mask = NULL;
255 const uint8_t *C2 = NULL;
256 const uint8_t *C3 = NULL;
258 EVP_MD_CTX *hash = NULL;
260 if (field_size == 0 || hash_size == 0)
263 memset(ptext_buf, 0xFF, *ptext_len);
265 sm2_ctext = d2i_SM2_Ciphertext(NULL, &ciphertext, ciphertext_len);
267 if (sm2_ctext == NULL)
269 SM2err(SM2_F_SM2_DECRYPT, SM2_R_ASN1_ERROR);
273 if (sm2_ctext->C3->length != hash_size)
275 SM2err(SM2_F_SM2_DECRYPT, SM2_R_INVALID_ENCODING);
279 C2 = sm2_ctext->C2->data;
280 C3 = sm2_ctext->C3->data;
281 msg_len = sm2_ctext->C2->length;
286 SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);
291 x2 = BN_CTX_get(ctx);
292 y2 = BN_CTX_get(ctx);
296 SM2err(SM2_F_SM2_DECRYPT, ERR_R_BN_LIB);
300 msg_mask = OPENSSL_zalloc(msg_len);
301 x2y2 = OPENSSL_zalloc(2 * field_size);
302 computed_C3 = OPENSSL_zalloc(hash_size);
304 if(msg_mask == NULL || x2y2 == NULL || computed_C3 == NULL)
306 SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);
310 C1 = EC_POINT_new(group);
313 SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);
317 if (EC_POINT_set_affine_coordinates_GFp
318 (group, C1, sm2_ctext->C1x, sm2_ctext->C1y, ctx) == 0)
320 SM2err(SM2_F_SM2_DECRYPT, ERR_R_EC_LIB);
324 if (EC_POINT_mul(group, C1, NULL, C1, EC_KEY_get0_private_key(key), ctx) ==
327 SM2err(SM2_F_SM2_DECRYPT, ERR_R_EC_LIB);
331 if (EC_POINT_get_affine_coordinates_GFp(group, C1, x2, y2, ctx) == 0)
333 SM2err(SM2_F_SM2_DECRYPT, ERR_R_EC_LIB);
337 BN_bn2binpad(x2, x2y2, field_size);
338 BN_bn2binpad(y2, x2y2 + field_size, field_size);
340 if (ECDH_KDF_X9_62(msg_mask, msg_len, x2y2, 2 * field_size, NULL, 0, digest)
344 for (i = 0; i != msg_len; ++i)
345 ptext_buf[i] = C2[i] ^ msg_mask[i];
347 hash = EVP_MD_CTX_new();
351 SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);
355 if (EVP_DigestInit(hash, digest) == 0)
357 SM2err(SM2_F_SM2_DECRYPT, ERR_R_EVP_LIB);
361 if (EVP_DigestUpdate(hash, x2y2, field_size) == 0)
363 SM2err(SM2_F_SM2_DECRYPT, ERR_R_EVP_LIB);
367 if (EVP_DigestUpdate(hash, ptext_buf, msg_len) == 0)
369 SM2err(SM2_F_SM2_DECRYPT, ERR_R_EVP_LIB);
373 if (EVP_DigestUpdate(hash, x2y2 + field_size, field_size) == 0)
375 SM2err(SM2_F_SM2_DECRYPT, ERR_R_EVP_LIB);
379 if (EVP_DigestFinal(hash, computed_C3, NULL) == 0)
381 SM2err(SM2_F_SM2_DECRYPT, ERR_R_EVP_LIB);
385 if (memcmp(computed_C3, C3, hash_size) != 0)
389 *ptext_len = msg_len;
394 memset(ptext_buf, 0, *ptext_len);
396 OPENSSL_free(msg_mask);
398 OPENSSL_free(computed_C3);
401 SM2_Ciphertext_free(sm2_ctext);
402 EVP_MD_CTX_free(hash);