ab2d18ee2ec56494bba0dbb173f80083cc9cac94
[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 "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>
19 #include <string.h>
20
21 typedef struct SM2_Ciphertext_st SM2_Ciphertext;
22 DECLARE_ASN1_FUNCTIONS(SM2_Ciphertext)
23
24 struct SM2_Ciphertext_st {
25     BIGNUM *C1x;
26     BIGNUM *C1y;
27     ASN1_OCTET_STRING *C3;
28     ASN1_OCTET_STRING *C2;
29 };
30
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)
37
38 IMPLEMENT_ASN1_FUNCTIONS(SM2_Ciphertext)
39
40 static size_t EC_field_size(const EC_GROUP *group)
41 {
42     /* Is there some simpler way to do this? */
43     BIGNUM *p = BN_new();
44     BIGNUM *a = BN_new();
45     BIGNUM *b = BN_new();
46     size_t field_size = 0;
47
48     if (p == NULL || a == NULL || b == NULL)
49        goto done;
50
51     EC_GROUP_get_curve_GFp(group, p, a, b, NULL);
52     field_size = (BN_num_bits(p) + 7) / 8;
53
54  done:
55     BN_free(p);
56     BN_free(a);
57     BN_free(b);
58
59     return field_size;
60 }
61
62 size_t SM2_plaintext_size(const EC_KEY *key, const EVP_MD *digest, size_t msg_len)
63 {
64     const size_t field_size = EC_field_size(EC_KEY_get0_group(key));
65     const size_t md_size = EVP_MD_size(digest);
66
67     const size_t overhead = 10 + 2 * field_size + md_size;
68     if(msg_len <= overhead)
69        return 0;
70
71     return msg_len - overhead;
72 }
73
74 size_t SM2_ciphertext_size(const EC_KEY *key, const EVP_MD *digest, size_t msg_len)
75 {
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;
79 }
80
81 int SM2_encrypt(const EC_KEY *key,
82                 const EVP_MD *digest,
83                 const uint8_t *msg,
84                 size_t msg_len, uint8_t *ciphertext_buf, size_t *ciphertext_len)
85 {
86     int rc = 0, ciphertext_leni;
87     size_t i;
88     BN_CTX *ctx = NULL;
89     BIGNUM *k = NULL;
90     BIGNUM *x1 = NULL;
91     BIGNUM *y1 = NULL;
92     BIGNUM *x2 = NULL;
93     BIGNUM *y2 = NULL;
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);
99     EC_POINT *kG = NULL;
100     EC_POINT *kP = NULL;
101     uint8_t *msg_mask = NULL;
102     uint8_t *x2y2 = NULL;
103     uint8_t *C3 = NULL;
104     const size_t field_size = EC_field_size(group);
105     const size_t C3_size = EVP_MD_size(digest);
106
107     /* NULL these before any "goto done" */
108     ctext_struct.C2 = NULL;
109     ctext_struct.C3 = NULL;
110
111     if (hash == NULL
112             || group == NULL
113             || order == NULL
114             || P == NULL
115             || field_size == 0
116             || C3_size == 0) {
117         SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
118         goto done;
119     }
120
121     kG = EC_POINT_new(group);
122     kP = EC_POINT_new(group);
123     ctx = BN_CTX_new();
124     if (kG == NULL || kP == NULL || ctx == NULL) {
125         SM2err(SM2_F_SM2_ENCRYPT, ERR_R_MALLOC_FAILURE);
126         goto done;
127     }
128
129     BN_CTX_start(ctx);
130     k = BN_CTX_get(ctx);
131     x1 = BN_CTX_get(ctx);
132     x2 = BN_CTX_get(ctx);
133     y1 = BN_CTX_get(ctx);
134     y2 = BN_CTX_get(ctx);
135
136     if (y2 == NULL) {
137         SM2err(SM2_F_SM2_ENCRYPT, ERR_R_BN_LIB);
138         goto done;
139     }
140
141     x2y2 = OPENSSL_zalloc(2 * field_size);
142     C3 = OPENSSL_zalloc(C3_size);
143
144     if (x2y2 == NULL || C3 == NULL) {
145         SM2err(SM2_F_SM2_ENCRYPT, ERR_R_MALLOC_FAILURE);
146         goto done;
147     }
148
149     memset(ciphertext_buf, 0, *ciphertext_len);
150
151     if (!BN_priv_rand_range(k, order)) {
152         SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
153         goto done;
154     }
155
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);
161         goto done;
162     }
163
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);
167         goto done;
168     }
169
170     msg_mask = OPENSSL_zalloc(msg_len);
171     if (msg_mask == NULL) {
172        SM2err(SM2_F_SM2_ENCRYPT, ERR_R_MALLOC_FAILURE);
173        goto done;
174    }
175
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,
178                         digest)) {
179         SM2err(SM2_F_SM2_ENCRYPT, ERR_R_EVP_LIB);
180         goto done;
181     }
182
183     for (i = 0; i != msg_len; ++i)
184         msg_mask[i] ^= msg[i];
185
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);
192         goto done;
193     }
194
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();
199
200     if (ctext_struct.C3 == NULL || ctext_struct.C2 == NULL) {
201        SM2err(SM2_F_SM2_ENCRYPT, ERR_R_MALLOC_FAILURE);
202        goto done;
203     }
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);
207         goto done;
208     }
209
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);
214         goto done;
215     }
216     *ciphertext_len = (size_t)ciphertext_leni;
217
218     rc = 1;
219
220  done:
221     ASN1_OCTET_STRING_free(ctext_struct.C2);
222     ASN1_OCTET_STRING_free(ctext_struct.C3);
223     OPENSSL_free(msg_mask);
224     OPENSSL_free(x2y2);
225     OPENSSL_free(C3);
226     EVP_MD_CTX_free(hash);
227     BN_CTX_free(ctx);
228     EC_POINT_free(kG);
229     EC_POINT_free(kP);
230     return rc;
231 }
232
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)
237 {
238     int rc = 0;
239     int i;
240
241     BN_CTX *ctx = NULL;
242     const EC_GROUP *group = EC_KEY_get0_group(key);
243     EC_POINT *C1 = NULL;
244     struct SM2_Ciphertext_st *sm2_ctext = NULL;
245     BIGNUM *x2 = NULL;
246     BIGNUM *y2 = NULL;
247
248     uint8_t *x2y2 = NULL;
249     uint8_t *computed_C3 = NULL;
250
251     const size_t field_size = EC_field_size(group);
252     const int hash_size = EVP_MD_size(digest);
253
254     uint8_t *msg_mask = NULL;
255     const uint8_t *C2 = NULL;
256     const uint8_t *C3 = NULL;
257     int msg_len = 0;
258     EVP_MD_CTX *hash = NULL;
259
260     if (field_size == 0 || hash_size == 0)
261        goto done;
262
263     memset(ptext_buf, 0xFF, *ptext_len);
264
265     sm2_ctext = d2i_SM2_Ciphertext(NULL, &ciphertext, ciphertext_len);
266
267     if (sm2_ctext == NULL)
268         {
269         SM2err(SM2_F_SM2_DECRYPT, SM2_R_ASN1_ERROR);
270         goto done;
271         }
272
273     if (sm2_ctext->C3->length != hash_size)
274         {
275         SM2err(SM2_F_SM2_DECRYPT, SM2_R_INVALID_ENCODING);
276         goto done;
277         }
278
279     C2 = sm2_ctext->C2->data;
280     C3 = sm2_ctext->C3->data;
281     msg_len = sm2_ctext->C2->length;
282
283     ctx = BN_CTX_new();
284     if (ctx == NULL)
285         {
286         SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);
287         goto done;
288         }
289
290     BN_CTX_start(ctx);
291     x2 = BN_CTX_get(ctx);
292     y2 = BN_CTX_get(ctx);
293
294     if(y2 == NULL)
295         {
296         SM2err(SM2_F_SM2_DECRYPT, ERR_R_BN_LIB);
297         goto done;
298         }
299
300     msg_mask = OPENSSL_zalloc(msg_len);
301     x2y2 = OPENSSL_zalloc(2 * field_size);
302     computed_C3 = OPENSSL_zalloc(hash_size);
303
304     if(msg_mask == NULL || x2y2 == NULL || computed_C3 == NULL)
305         {
306         SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);
307         goto done;
308         }
309
310     C1 = EC_POINT_new(group);
311     if (C1 == NULL)
312         {
313         SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);
314         goto done;
315         }
316
317     if (EC_POINT_set_affine_coordinates_GFp
318         (group, C1, sm2_ctext->C1x, sm2_ctext->C1y, ctx) == 0)
319         {
320         SM2err(SM2_F_SM2_DECRYPT, ERR_R_EC_LIB);
321         goto done;
322         }
323
324     if (EC_POINT_mul(group, C1, NULL, C1, EC_KEY_get0_private_key(key), ctx) ==
325         0)
326         {
327         SM2err(SM2_F_SM2_DECRYPT, ERR_R_EC_LIB);
328         goto done;
329         }
330
331     if (EC_POINT_get_affine_coordinates_GFp(group, C1, x2, y2, ctx) == 0)
332         {
333         SM2err(SM2_F_SM2_DECRYPT, ERR_R_EC_LIB);
334         goto done;
335         }
336
337     BN_bn2binpad(x2, x2y2, field_size);
338     BN_bn2binpad(y2, x2y2 + field_size, field_size);
339
340     if (ECDH_KDF_X9_62(msg_mask, msg_len, x2y2, 2 * field_size, NULL, 0, digest)
341         == 0)
342         goto done;
343
344     for (i = 0; i != msg_len; ++i)
345         ptext_buf[i] = C2[i] ^ msg_mask[i];
346
347     hash = EVP_MD_CTX_new();
348
349     if (hash == NULL)
350         {
351         SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);
352         goto done;
353         }
354
355     if (EVP_DigestInit(hash, digest) == 0)
356         {
357         SM2err(SM2_F_SM2_DECRYPT, ERR_R_EVP_LIB);
358         goto done;
359         }
360
361     if (EVP_DigestUpdate(hash, x2y2, field_size) == 0)
362         {
363         SM2err(SM2_F_SM2_DECRYPT, ERR_R_EVP_LIB);
364         goto done;
365         }
366
367     if (EVP_DigestUpdate(hash, ptext_buf, msg_len) == 0)
368         {
369         SM2err(SM2_F_SM2_DECRYPT, ERR_R_EVP_LIB);
370         goto done;
371         }
372
373     if (EVP_DigestUpdate(hash, x2y2 + field_size, field_size) == 0)
374         {
375         SM2err(SM2_F_SM2_DECRYPT, ERR_R_EVP_LIB);
376         goto done;
377         }
378
379     if (EVP_DigestFinal(hash, computed_C3, NULL) == 0)
380         {
381         SM2err(SM2_F_SM2_DECRYPT, ERR_R_EVP_LIB);
382         goto done;
383         }
384
385     if (memcmp(computed_C3, C3, hash_size) != 0)
386         goto done;
387
388     rc = 1;
389     *ptext_len = msg_len;
390
391  done:
392
393     if (rc == 0)
394         memset(ptext_buf, 0, *ptext_len);
395
396     OPENSSL_free(msg_mask);
397     OPENSSL_free(x2y2);
398     OPENSSL_free(computed_C3);
399     EC_POINT_free(C1);
400     BN_CTX_free(ctx);
401     SM2_Ciphertext_free(sm2_ctext);
402     EVP_MD_CTX_free(hash);
403
404     return rc;
405 }