2 * Copyright 2017 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 <openssl/sm2.h>
13 #include <openssl/evp.h>
14 #include <openssl/bn.h>
15 #include <openssl/asn1.h>
16 #include <openssl/asn1t.h>
19 typedef struct SM2_Ciphertext_st SM2_Ciphertext;
20 DECLARE_ASN1_FUNCTIONS(SM2_Ciphertext)
22 struct SM2_Ciphertext_st {
25 ASN1_OCTET_STRING *C3;
26 ASN1_OCTET_STRING *C2;
29 ASN1_SEQUENCE(SM2_Ciphertext) = {
30 ASN1_SIMPLE(SM2_Ciphertext, C1x, BIGNUM),
31 ASN1_SIMPLE(SM2_Ciphertext, C1y, BIGNUM),
32 ASN1_SIMPLE(SM2_Ciphertext, C3, ASN1_OCTET_STRING),
33 ASN1_SIMPLE(SM2_Ciphertext, C2, ASN1_OCTET_STRING),
34 } ASN1_SEQUENCE_END(SM2_Ciphertext)
36 IMPLEMENT_ASN1_FUNCTIONS(SM2_Ciphertext)
38 static size_t EC_field_size(const EC_GROUP *group)
40 /* Is there some simpler way to do this? */
44 size_t field_size = 0;
46 if (p == NULL || a == NULL || b == NULL)
49 EC_GROUP_get_curve_GFp(group, p, a, b, NULL);
50 field_size = (BN_num_bits(p) + 7) / 8;
60 size_t SM2_ciphertext_size(const EC_KEY *key, const EVP_MD *digest, size_t msg_len)
62 return 10 + 2 * EC_field_size(EC_KEY_get0_group(key)) +
63 EVP_MD_size(digest) + msg_len;
66 int SM2_encrypt(const EC_KEY *key,
69 size_t msg_len, uint8_t *ciphertext_buf, size_t *ciphertext_len)
80 EVP_MD_CTX *hash = EVP_MD_CTX_new();
82 struct SM2_Ciphertext_st ctext_struct;
83 const EC_GROUP *group = EC_KEY_get0_group(key);
84 const BIGNUM *order = EC_GROUP_get0_order(group);
85 const EC_POINT *P = EC_KEY_get0_public_key(key);
88 uint8_t *msg_mask = NULL;
93 const size_t field_size = EC_field_size(group);
94 const size_t C3_size = EVP_MD_size(digest);
96 if (field_size == 0 || C3_size == 0)
99 kG = EC_POINT_new(group);
100 kP = EC_POINT_new(group);
101 if (kG == NULL || kP == NULL)
110 x1 = BN_CTX_get(ctx);
111 x2 = BN_CTX_get(ctx);
112 y1 = BN_CTX_get(ctx);
113 y2 = BN_CTX_get(ctx);
118 x2y2 = OPENSSL_zalloc(2 * field_size);
119 C3 = OPENSSL_zalloc(C3_size);
121 if (x2y2 == NULL || C3 == NULL)
124 memset(ciphertext_buf, 0, *ciphertext_len);
126 BN_priv_rand_range(k, order);
128 if (EC_POINT_mul(group, kG, k, NULL, NULL, ctx) == 0)
131 if (EC_POINT_get_affine_coordinates_GFp(group, kG, x1, y1, ctx) == 0)
134 if (EC_POINT_mul(group, kP, NULL, P, k, ctx) == 0)
137 if (EC_POINT_get_affine_coordinates_GFp(group, kP, x2, y2, ctx) == 0)
140 BN_bn2binpad(x2, x2y2, field_size);
141 BN_bn2binpad(y2, x2y2 + field_size, field_size);
143 msg_mask = OPENSSL_zalloc(msg_len);
144 if (msg_mask == NULL)
147 /* X9.63 with no salt happens to match the KDF used in SM2 */
148 if (ECDH_KDF_X9_62(msg_mask, msg_len, x2y2, 2 * field_size, NULL, 0, digest)
152 for (i = 0; i != msg_len; ++i)
153 msg_mask[i] ^= msg[i];
155 if (EVP_DigestInit(hash, digest) == 0)
158 if (EVP_DigestUpdate(hash, x2y2, field_size) == 0)
161 if (EVP_DigestUpdate(hash, msg, msg_len) == 0)
164 if (EVP_DigestUpdate(hash, x2y2 + field_size, field_size) == 0)
167 if (EVP_DigestFinal(hash, C3, NULL) == 0)
170 ctext_struct.C1x = x1;
171 ctext_struct.C1y = y1;
172 ctext_struct.C3 = ASN1_OCTET_STRING_new();
173 ASN1_OCTET_STRING_set(ctext_struct.C3, C3, C3_size);
174 ctext_struct.C2 = ASN1_OCTET_STRING_new();
175 ASN1_OCTET_STRING_set(ctext_struct.C2, msg_mask, msg_len);
177 *ciphertext_len = i2d_SM2_Ciphertext(&ctext_struct, &ciphertext_buf);
179 ASN1_OCTET_STRING_free(ctext_struct.C2);
180 ASN1_OCTET_STRING_free(ctext_struct.C3);
185 OPENSSL_free(msg_mask);
188 EVP_MD_CTX_free(hash);
195 int SM2_decrypt(const EC_KEY *key,
196 const EVP_MD *digest,
197 const uint8_t *ciphertext,
198 size_t ciphertext_len, uint8_t *ptext_buf, size_t *ptext_len)
204 const EC_GROUP *group = EC_KEY_get0_group(key);
206 struct SM2_Ciphertext_st *sm2_ctext = NULL;
210 uint8_t *x2y2 = NULL;
211 uint8_t *computed_C3 = NULL;
213 const size_t field_size = EC_field_size(group);
214 const int hash_size = EVP_MD_size(digest);
216 uint8_t *msg_mask = NULL;
217 const uint8_t *C2 = NULL;
218 const uint8_t *C3 = NULL;
220 EVP_MD_CTX *hash = NULL;
222 if (field_size == 0 || hash_size == 0)
225 memset(ptext_buf, 0xFF, *ptext_len);
227 sm2_ctext = d2i_SM2_Ciphertext(NULL, &ciphertext, ciphertext_len);
229 if (sm2_ctext == NULL)
232 if (sm2_ctext->C3->length != hash_size)
235 C2 = sm2_ctext->C2->data;
236 C3 = sm2_ctext->C3->data;
237 msg_len = sm2_ctext->C2->length;
244 x2 = BN_CTX_get(ctx);
245 y2 = BN_CTX_get(ctx);
250 msg_mask = OPENSSL_zalloc(msg_len);
251 x2y2 = OPENSSL_zalloc(2 * field_size);
252 computed_C3 = OPENSSL_zalloc(hash_size);
254 if(msg_mask == NULL || x2y2 == NULL || computed_C3 == NULL)
257 C1 = EC_POINT_new(group);
261 if (EC_POINT_set_affine_coordinates_GFp
262 (group, C1, sm2_ctext->C1x, sm2_ctext->C1y, ctx) == 0)
265 if (EC_POINT_mul(group, C1, NULL, C1, EC_KEY_get0_private_key(key), ctx) ==
269 if (EC_POINT_get_affine_coordinates_GFp(group, C1, x2, y2, ctx) == 0)
272 BN_bn2binpad(x2, x2y2, field_size);
273 BN_bn2binpad(y2, x2y2 + field_size, field_size);
275 if (ECDH_KDF_X9_62(msg_mask, msg_len, x2y2, 2 * field_size, NULL, 0, digest)
279 for (i = 0; i != msg_len; ++i)
280 ptext_buf[i] = C2[i] ^ msg_mask[i];
282 hash = EVP_MD_CTX_new();
287 if (EVP_DigestInit(hash, digest) == 0)
290 if (EVP_DigestUpdate(hash, x2y2, field_size) == 0)
293 if (EVP_DigestUpdate(hash, ptext_buf, msg_len) == 0)
296 if (EVP_DigestUpdate(hash, x2y2 + field_size, field_size) == 0)
299 if (EVP_DigestFinal(hash, computed_C3, NULL) == 0)
302 if (memcmp(computed_C3, C3, hash_size) != 0)
306 *ptext_len = msg_len;
311 memset(ptext_buf, 0, *ptext_len);
313 OPENSSL_free(msg_mask);
315 OPENSSL_free(computed_C3);
318 SM2_Ciphertext_free(sm2_ctext);
319 EVP_MD_CTX_free(hash);