7e7be9d04a5cad37cd76cce664624771102b22e9
[openssl.git] / crypto / sm2 / sm2_crypt.c
1 /*
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.
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 <openssl/sm2.h>
13 #include <openssl/evp.h>
14 #include <openssl/bn.h>
15 #include <openssl/asn1.h>
16 #include <openssl/asn1t.h>
17 #include <string.h>
18
19 typedef struct SM2_Ciphertext_st SM2_Ciphertext;
20 DECLARE_ASN1_FUNCTIONS(SM2_Ciphertext)
21
22 struct SM2_Ciphertext_st {
23     BIGNUM *C1x;
24     BIGNUM *C1y;
25     ASN1_OCTET_STRING *C3;
26     ASN1_OCTET_STRING *C2;
27 };
28
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)
35
36 IMPLEMENT_ASN1_FUNCTIONS(SM2_Ciphertext)
37
38 static size_t EC_field_size(const EC_GROUP *group)
39 {
40     /* Is there some simpler way to do this? */
41     BIGNUM *p = BN_new();
42     BIGNUM *a = BN_new();
43     BIGNUM *b = BN_new();
44     size_t field_size = 0;
45
46     if (p == NULL || a == NULL || b == NULL)
47        goto done;
48
49     EC_GROUP_get_curve_GFp(group, p, a, b, NULL);
50     field_size = (BN_num_bits(p) + 7) / 8;
51
52  done:
53     BN_free(p);
54     BN_free(a);
55     BN_free(b);
56
57     return field_size;
58 }
59
60 size_t SM2_ciphertext_size(const EC_KEY *key, const EVP_MD *digest, size_t msg_len)
61 {
62     return 10 + 2 * EC_field_size(EC_KEY_get0_group(key)) +
63         EVP_MD_size(digest) + msg_len;
64 }
65
66 int SM2_encrypt(const EC_KEY *key,
67                 const EVP_MD *digest,
68                 const uint8_t *msg,
69                 size_t msg_len, uint8_t *ciphertext_buf, size_t *ciphertext_len)
70 {
71     int rc = 0;
72     size_t i;
73     BN_CTX *ctx = NULL;
74     BIGNUM *k = NULL;
75     BIGNUM *x1 = NULL;
76     BIGNUM *y1 = NULL;
77     BIGNUM *x2 = NULL;
78     BIGNUM *y2 = NULL;
79
80     EVP_MD_CTX *hash = EVP_MD_CTX_new();
81
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);
86     EC_POINT *kG = NULL;
87     EC_POINT *kP = NULL;
88     uint8_t *msg_mask = NULL;
89
90     uint8_t *x2y2 = NULL;
91     uint8_t *C3 = NULL;
92
93     const size_t field_size = EC_field_size(group);
94     const size_t C3_size = EVP_MD_size(digest);
95
96     if (field_size == 0 || C3_size == 0)
97        goto done;
98
99     kG = EC_POINT_new(group);
100     kP = EC_POINT_new(group);
101     if (kG == NULL || kP == NULL)
102        goto done;
103
104     ctx = BN_CTX_new();
105     if (ctx == NULL)
106        goto done;
107
108     BN_CTX_start(ctx);
109     k = BN_CTX_get(ctx);
110     x1 = BN_CTX_get(ctx);
111     x2 = BN_CTX_get(ctx);
112     y1 = BN_CTX_get(ctx);
113     y2 = BN_CTX_get(ctx);
114
115     if (y2 == NULL)
116        goto done;
117
118     x2y2 = OPENSSL_zalloc(2 * field_size);
119     C3 = OPENSSL_zalloc(C3_size);
120
121     if (x2y2 == NULL || C3 == NULL)
122        goto done;
123
124     memset(ciphertext_buf, 0, *ciphertext_len);
125
126     BN_priv_rand_range(k, order);
127
128     if (EC_POINT_mul(group, kG, k, NULL, NULL, ctx) == 0)
129         goto done;
130
131     if (EC_POINT_get_affine_coordinates_GFp(group, kG, x1, y1, ctx) == 0)
132         goto done;
133
134     if (EC_POINT_mul(group, kP, NULL, P, k, ctx) == 0)
135         goto done;
136
137     if (EC_POINT_get_affine_coordinates_GFp(group, kP, x2, y2, ctx) == 0)
138         goto done;
139
140     BN_bn2binpad(x2, x2y2, field_size);
141     BN_bn2binpad(y2, x2y2 + field_size, field_size);
142
143     msg_mask = OPENSSL_zalloc(msg_len);
144     if (msg_mask == NULL)
145        goto done;
146
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)
149         == 0)
150         goto done;
151
152     for (i = 0; i != msg_len; ++i)
153         msg_mask[i] ^= msg[i];
154
155     if (EVP_DigestInit(hash, digest) == 0)
156         goto done;
157
158     if (EVP_DigestUpdate(hash, x2y2, field_size) == 0)
159         goto done;
160
161     if (EVP_DigestUpdate(hash, msg, msg_len) == 0)
162         goto done;
163
164     if (EVP_DigestUpdate(hash, x2y2 + field_size, field_size) == 0)
165         goto done;
166
167     if (EVP_DigestFinal(hash, C3, NULL) == 0)
168         goto done;
169
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);
176
177     *ciphertext_len = i2d_SM2_Ciphertext(&ctext_struct, &ciphertext_buf);
178
179     ASN1_OCTET_STRING_free(ctext_struct.C2);
180     ASN1_OCTET_STRING_free(ctext_struct.C3);
181
182     rc = 1;
183
184  done:
185     OPENSSL_free(msg_mask);
186     OPENSSL_free(x2y2);
187     OPENSSL_free(C3);
188     EVP_MD_CTX_free(hash);
189     BN_CTX_free(ctx);
190     EC_POINT_free(kG);
191     EC_POINT_free(kP);
192     return rc;
193 }
194
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)
199 {
200     int rc = 0;
201     int i;
202
203     BN_CTX *ctx = NULL;
204     const EC_GROUP *group = EC_KEY_get0_group(key);
205     EC_POINT *C1 = NULL;
206     struct SM2_Ciphertext_st *sm2_ctext = NULL;
207     BIGNUM *x2 = NULL;
208     BIGNUM *y2 = NULL;
209
210     uint8_t *x2y2 = NULL;
211     uint8_t *computed_C3 = NULL;
212
213     const size_t field_size = EC_field_size(group);
214     const int hash_size = EVP_MD_size(digest);
215
216     uint8_t *msg_mask = NULL;
217     const uint8_t *C2 = NULL;
218     const uint8_t *C3 = NULL;
219     int msg_len = 0;
220     EVP_MD_CTX *hash = NULL;
221
222     if (field_size == 0 || hash_size == 0)
223        goto done;
224
225     memset(ptext_buf, 0xFF, *ptext_len);
226
227     sm2_ctext = d2i_SM2_Ciphertext(NULL, &ciphertext, ciphertext_len);
228
229     if (sm2_ctext == NULL)
230         goto done;
231
232     if (sm2_ctext->C3->length != hash_size)
233         goto done;
234
235     C2 = sm2_ctext->C2->data;
236     C3 = sm2_ctext->C3->data;
237     msg_len = sm2_ctext->C2->length;
238
239     ctx = BN_CTX_new();
240     if (ctx == NULL)
241        goto done;
242
243     BN_CTX_start(ctx);
244     x2 = BN_CTX_get(ctx);
245     y2 = BN_CTX_get(ctx);
246
247     if(y2 == NULL)
248        goto done;
249
250     msg_mask = OPENSSL_zalloc(msg_len);
251     x2y2 = OPENSSL_zalloc(2 * field_size);
252     computed_C3 = OPENSSL_zalloc(hash_size);
253
254     if(msg_mask == NULL || x2y2 == NULL || computed_C3 == NULL)
255        goto done;
256
257     C1 = EC_POINT_new(group);
258     if (C1 == NULL)
259         goto done;
260
261     if (EC_POINT_set_affine_coordinates_GFp
262         (group, C1, sm2_ctext->C1x, sm2_ctext->C1y, ctx) == 0)
263         goto done;
264
265     if (EC_POINT_mul(group, C1, NULL, C1, EC_KEY_get0_private_key(key), ctx) ==
266         0)
267         goto done;
268
269     if (EC_POINT_get_affine_coordinates_GFp(group, C1, x2, y2, ctx) == 0)
270         goto done;
271
272     BN_bn2binpad(x2, x2y2, field_size);
273     BN_bn2binpad(y2, x2y2 + field_size, field_size);
274
275     if (ECDH_KDF_X9_62(msg_mask, msg_len, x2y2, 2 * field_size, NULL, 0, digest)
276         == 0)
277         goto done;
278
279     for (i = 0; i != msg_len; ++i)
280         ptext_buf[i] = C2[i] ^ msg_mask[i];
281
282     hash = EVP_MD_CTX_new();
283
284     if (hash == NULL)
285        goto done;
286
287     if (EVP_DigestInit(hash, digest) == 0)
288         goto done;
289
290     if (EVP_DigestUpdate(hash, x2y2, field_size) == 0)
291         goto done;
292
293     if (EVP_DigestUpdate(hash, ptext_buf, msg_len) == 0)
294         goto done;
295
296     if (EVP_DigestUpdate(hash, x2y2 + field_size, field_size) == 0)
297         goto done;
298
299     if (EVP_DigestFinal(hash, computed_C3, NULL) == 0)
300         goto done;
301
302     if (memcmp(computed_C3, C3, hash_size) != 0)
303         goto done;
304
305     rc = 1;
306     *ptext_len = msg_len;
307
308  done:
309
310     if (rc == 0)
311         memset(ptext_buf, 0, *ptext_len);
312
313     OPENSSL_free(msg_mask);
314     OPENSSL_free(x2y2);
315     OPENSSL_free(computed_C3);
316     EC_POINT_free(C1);
317     BN_CTX_free(ctx);
318     SM2_Ciphertext_free(sm2_ctext);
319     EVP_MD_CTX_free(hash);
320
321     return rc;
322 }