Update copyright year
[openssl.git] / crypto / sm2 / sm2_crypt.c
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 <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_plaintext_size(const EC_KEY *key, const EVP_MD *digest, size_t msg_len)
61 {
62     const size_t field_size = EC_field_size(EC_KEY_get0_group(key));
63     const size_t md_size = EVP_MD_size(digest);
64
65     const size_t overhead = 10 + 2 * field_size + md_size;
66     if(msg_len <= overhead)
67        return 0;
68
69     return msg_len - overhead;
70 }
71
72 size_t SM2_ciphertext_size(const EC_KEY *key, const EVP_MD *digest, size_t msg_len)
73 {
74     const size_t field_size = EC_field_size(EC_KEY_get0_group(key));
75     const size_t md_size = EVP_MD_size(digest);
76     return 10 + 2 * field_size + md_size + msg_len;
77 }
78
79 int SM2_encrypt(const EC_KEY *key,
80                 const EVP_MD *digest,
81                 const uint8_t *msg,
82                 size_t msg_len, uint8_t *ciphertext_buf, size_t *ciphertext_len)
83 {
84     int rc = 0;
85     size_t i;
86     BN_CTX *ctx = NULL;
87     BIGNUM *k = NULL;
88     BIGNUM *x1 = NULL;
89     BIGNUM *y1 = NULL;
90     BIGNUM *x2 = NULL;
91     BIGNUM *y2 = NULL;
92
93     EVP_MD_CTX *hash = EVP_MD_CTX_new();
94
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);
99     EC_POINT *kG = NULL;
100     EC_POINT *kP = NULL;
101     uint8_t *msg_mask = NULL;
102
103     uint8_t *x2y2 = NULL;
104     uint8_t *C3 = NULL;
105
106     const size_t field_size = EC_field_size(group);
107     const size_t C3_size = EVP_MD_size(digest);
108
109     if (field_size == 0 || C3_size == 0)
110        goto done;
111
112     kG = EC_POINT_new(group);
113     kP = EC_POINT_new(group);
114     if (kG == NULL || kP == NULL)
115        goto done;
116
117     ctx = BN_CTX_new();
118     if (ctx == NULL)
119        goto done;
120
121     BN_CTX_start(ctx);
122     k = BN_CTX_get(ctx);
123     x1 = BN_CTX_get(ctx);
124     x2 = BN_CTX_get(ctx);
125     y1 = BN_CTX_get(ctx);
126     y2 = BN_CTX_get(ctx);
127
128     if (y2 == NULL)
129        goto done;
130
131     x2y2 = OPENSSL_zalloc(2 * field_size);
132     C3 = OPENSSL_zalloc(C3_size);
133
134     if (x2y2 == NULL || C3 == NULL)
135        goto done;
136
137     memset(ciphertext_buf, 0, *ciphertext_len);
138
139     BN_priv_rand_range(k, order);
140
141     if (EC_POINT_mul(group, kG, k, NULL, NULL, ctx) == 0)
142         goto done;
143
144     if (EC_POINT_get_affine_coordinates_GFp(group, kG, x1, y1, ctx) == 0)
145         goto done;
146
147     if (EC_POINT_mul(group, kP, NULL, P, k, ctx) == 0)
148         goto done;
149
150     if (EC_POINT_get_affine_coordinates_GFp(group, kP, x2, y2, ctx) == 0)
151         goto done;
152
153     BN_bn2binpad(x2, x2y2, field_size);
154     BN_bn2binpad(y2, x2y2 + field_size, field_size);
155
156     msg_mask = OPENSSL_zalloc(msg_len);
157     if (msg_mask == NULL)
158        goto done;
159
160     /* X9.63 with no salt happens to match the KDF used in SM2 */
161     if (ECDH_KDF_X9_62(msg_mask, msg_len, x2y2, 2 * field_size, NULL, 0, digest)
162         == 0)
163         goto done;
164
165     for (i = 0; i != msg_len; ++i)
166         msg_mask[i] ^= msg[i];
167
168     if (EVP_DigestInit(hash, digest) == 0)
169         goto done;
170
171     if (EVP_DigestUpdate(hash, x2y2, field_size) == 0)
172         goto done;
173
174     if (EVP_DigestUpdate(hash, msg, msg_len) == 0)
175         goto done;
176
177     if (EVP_DigestUpdate(hash, x2y2 + field_size, field_size) == 0)
178         goto done;
179
180     if (EVP_DigestFinal(hash, C3, NULL) == 0)
181         goto done;
182
183     ctext_struct.C1x = x1;
184     ctext_struct.C1y = y1;
185     ctext_struct.C3 = ASN1_OCTET_STRING_new();
186     ASN1_OCTET_STRING_set(ctext_struct.C3, C3, C3_size);
187     ctext_struct.C2 = ASN1_OCTET_STRING_new();
188     ASN1_OCTET_STRING_set(ctext_struct.C2, msg_mask, msg_len);
189
190     *ciphertext_len = i2d_SM2_Ciphertext(&ctext_struct, &ciphertext_buf);
191
192     ASN1_OCTET_STRING_free(ctext_struct.C2);
193     ASN1_OCTET_STRING_free(ctext_struct.C3);
194
195     rc = 1;
196
197  done:
198     OPENSSL_free(msg_mask);
199     OPENSSL_free(x2y2);
200     OPENSSL_free(C3);
201     EVP_MD_CTX_free(hash);
202     BN_CTX_free(ctx);
203     EC_POINT_free(kG);
204     EC_POINT_free(kP);
205     return rc;
206 }
207
208 int SM2_decrypt(const EC_KEY *key,
209                 const EVP_MD *digest,
210                 const uint8_t *ciphertext,
211                 size_t ciphertext_len, uint8_t *ptext_buf, size_t *ptext_len)
212 {
213     int rc = 0;
214     int i;
215
216     BN_CTX *ctx = NULL;
217     const EC_GROUP *group = EC_KEY_get0_group(key);
218     EC_POINT *C1 = NULL;
219     struct SM2_Ciphertext_st *sm2_ctext = NULL;
220     BIGNUM *x2 = NULL;
221     BIGNUM *y2 = NULL;
222
223     uint8_t *x2y2 = NULL;
224     uint8_t *computed_C3 = NULL;
225
226     const size_t field_size = EC_field_size(group);
227     const int hash_size = EVP_MD_size(digest);
228
229     uint8_t *msg_mask = NULL;
230     const uint8_t *C2 = NULL;
231     const uint8_t *C3 = NULL;
232     int msg_len = 0;
233     EVP_MD_CTX *hash = NULL;
234
235     if (field_size == 0 || hash_size == 0)
236        goto done;
237
238     memset(ptext_buf, 0xFF, *ptext_len);
239
240     sm2_ctext = d2i_SM2_Ciphertext(NULL, &ciphertext, ciphertext_len);
241
242     if (sm2_ctext == NULL)
243         goto done;
244
245     if (sm2_ctext->C3->length != hash_size)
246         goto done;
247
248     C2 = sm2_ctext->C2->data;
249     C3 = sm2_ctext->C3->data;
250     msg_len = sm2_ctext->C2->length;
251
252     ctx = BN_CTX_new();
253     if (ctx == NULL)
254        goto done;
255
256     BN_CTX_start(ctx);
257     x2 = BN_CTX_get(ctx);
258     y2 = BN_CTX_get(ctx);
259
260     if(y2 == NULL)
261        goto done;
262
263     msg_mask = OPENSSL_zalloc(msg_len);
264     x2y2 = OPENSSL_zalloc(2 * field_size);
265     computed_C3 = OPENSSL_zalloc(hash_size);
266
267     if(msg_mask == NULL || x2y2 == NULL || computed_C3 == NULL)
268        goto done;
269
270     C1 = EC_POINT_new(group);
271     if (C1 == NULL)
272         goto done;
273
274     if (EC_POINT_set_affine_coordinates_GFp
275         (group, C1, sm2_ctext->C1x, sm2_ctext->C1y, ctx) == 0)
276         goto done;
277
278     if (EC_POINT_mul(group, C1, NULL, C1, EC_KEY_get0_private_key(key), ctx) ==
279         0)
280         goto done;
281
282     if (EC_POINT_get_affine_coordinates_GFp(group, C1, x2, y2, ctx) == 0)
283         goto done;
284
285     BN_bn2binpad(x2, x2y2, field_size);
286     BN_bn2binpad(y2, x2y2 + field_size, field_size);
287
288     if (ECDH_KDF_X9_62(msg_mask, msg_len, x2y2, 2 * field_size, NULL, 0, digest)
289         == 0)
290         goto done;
291
292     for (i = 0; i != msg_len; ++i)
293         ptext_buf[i] = C2[i] ^ msg_mask[i];
294
295     hash = EVP_MD_CTX_new();
296
297     if (hash == NULL)
298        goto done;
299
300     if (EVP_DigestInit(hash, digest) == 0)
301         goto done;
302
303     if (EVP_DigestUpdate(hash, x2y2, field_size) == 0)
304         goto done;
305
306     if (EVP_DigestUpdate(hash, ptext_buf, msg_len) == 0)
307         goto done;
308
309     if (EVP_DigestUpdate(hash, x2y2 + field_size, field_size) == 0)
310         goto done;
311
312     if (EVP_DigestFinal(hash, computed_C3, NULL) == 0)
313         goto done;
314
315     if (memcmp(computed_C3, C3, hash_size) != 0)
316         goto done;
317
318     rc = 1;
319     *ptext_len = msg_len;
320
321  done:
322
323     if (rc == 0)
324         memset(ptext_buf, 0, *ptext_len);
325
326     OPENSSL_free(msg_mask);
327     OPENSSL_free(x2y2);
328     OPENSSL_free(computed_C3);
329     EC_POINT_free(C1);
330     BN_CTX_free(ctx);
331     SM2_Ciphertext_free(sm2_ctext);
332     EVP_MD_CTX_free(hash);
333
334     return rc;
335 }