Use the new non-curve type specific EC functions internally
[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     if (!EC_GROUP_get_curve(group, p, a, b, NULL))
52         goto done;
53     field_size = (BN_num_bits(p) + 7) / 8;
54
55  done:
56     BN_free(p);
57     BN_free(a);
58     BN_free(b);
59
60     return field_size;
61 }
62
63 int sm2_plaintext_size(const EC_KEY *key, const EVP_MD *digest, size_t msg_len,
64                        size_t *pt_size)
65 {
66     const size_t field_size = ec_field_size(EC_KEY_get0_group(key));
67     const int md_size = EVP_MD_size(digest);
68     size_t overhead;
69
70     if (md_size < 0) {
71         SM2err(SM2_F_SM2_PLAINTEXT_SIZE, SM2_R_INVALID_DIGEST);
72         return 0;
73     }
74     if (field_size == 0) {
75         SM2err(SM2_F_SM2_PLAINTEXT_SIZE, SM2_R_INVALID_FIELD);
76         return 0;
77     }
78
79     overhead = 10 + 2 * field_size + (size_t)md_size;
80     if (msg_len <= overhead) {
81         SM2err(SM2_F_SM2_PLAINTEXT_SIZE, SM2_R_INVALID_ENCODING);
82         return 0;
83     }
84
85     *pt_size = msg_len - overhead;
86     return 1;
87 }
88
89 int sm2_ciphertext_size(const EC_KEY *key, const EVP_MD *digest, size_t msg_len,
90                         size_t *ct_size)
91 {
92     const size_t field_size = ec_field_size(EC_KEY_get0_group(key));
93     const int md_size = EVP_MD_size(digest);
94
95     if (field_size == 0 || md_size < 0)
96         return 0;
97
98     *ct_size = 12 + 2 * field_size + (size_t)md_size + msg_len;
99     return 1;
100 }
101
102 int sm2_encrypt(const EC_KEY *key,
103                 const EVP_MD *digest,
104                 const uint8_t *msg,
105                 size_t msg_len, uint8_t *ciphertext_buf, size_t *ciphertext_len)
106 {
107     int rc = 0, ciphertext_leni;
108     size_t i;
109     BN_CTX *ctx = NULL;
110     BIGNUM *k = NULL;
111     BIGNUM *x1 = NULL;
112     BIGNUM *y1 = NULL;
113     BIGNUM *x2 = NULL;
114     BIGNUM *y2 = NULL;
115     EVP_MD_CTX *hash = EVP_MD_CTX_new();
116     struct SM2_Ciphertext_st ctext_struct;
117     const EC_GROUP *group = EC_KEY_get0_group(key);
118     const BIGNUM *order = EC_GROUP_get0_order(group);
119     const EC_POINT *P = EC_KEY_get0_public_key(key);
120     EC_POINT *kG = NULL;
121     EC_POINT *kP = NULL;
122     uint8_t *msg_mask = NULL;
123     uint8_t *x2y2 = NULL;
124     uint8_t *C3 = NULL;
125     size_t field_size;
126     const int C3_size = EVP_MD_size(digest);
127
128     /* NULL these before any "goto done" */
129     ctext_struct.C2 = NULL;
130     ctext_struct.C3 = NULL;
131
132     if (hash == NULL || C3_size <= 0) {
133         SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
134         goto done;
135     }
136
137     field_size = ec_field_size(group);
138     if (field_size == 0) {
139         SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
140         goto done;
141     }
142
143     kG = EC_POINT_new(group);
144     kP = EC_POINT_new(group);
145     ctx = BN_CTX_new();
146     if (kG == NULL || kP == NULL || ctx == NULL) {
147         SM2err(SM2_F_SM2_ENCRYPT, ERR_R_MALLOC_FAILURE);
148         goto done;
149     }
150
151     BN_CTX_start(ctx);
152     k = BN_CTX_get(ctx);
153     x1 = BN_CTX_get(ctx);
154     x2 = BN_CTX_get(ctx);
155     y1 = BN_CTX_get(ctx);
156     y2 = BN_CTX_get(ctx);
157
158     if (y2 == NULL) {
159         SM2err(SM2_F_SM2_ENCRYPT, ERR_R_BN_LIB);
160         goto done;
161     }
162
163     x2y2 = OPENSSL_zalloc(2 * field_size);
164     C3 = OPENSSL_zalloc(C3_size);
165
166     if (x2y2 == NULL || C3 == NULL) {
167         SM2err(SM2_F_SM2_ENCRYPT, ERR_R_MALLOC_FAILURE);
168         goto done;
169     }
170
171     memset(ciphertext_buf, 0, *ciphertext_len);
172
173     if (!BN_priv_rand_range(k, order)) {
174         SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
175         goto done;
176     }
177
178     if (!EC_POINT_mul(group, kG, k, NULL, NULL, ctx)
179             || !EC_POINT_get_affine_coordinates(group, kG, x1, y1, ctx)
180             || !EC_POINT_mul(group, kP, NULL, P, k, ctx)
181             || !EC_POINT_get_affine_coordinates(group, kP, x2, y2, ctx)) {
182         SM2err(SM2_F_SM2_ENCRYPT, ERR_R_EC_LIB);
183         goto done;
184     }
185
186     if (BN_bn2binpad(x2, x2y2, field_size) < 0
187             || BN_bn2binpad(y2, x2y2 + field_size, field_size) < 0) {
188         SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
189         goto done;
190     }
191
192     msg_mask = OPENSSL_zalloc(msg_len);
193     if (msg_mask == NULL) {
194        SM2err(SM2_F_SM2_ENCRYPT, ERR_R_MALLOC_FAILURE);
195        goto done;
196    }
197
198     /* X9.63 with no salt happens to match the KDF used in SM2 */
199     if (!ECDH_KDF_X9_62(msg_mask, msg_len, x2y2, 2 * field_size, NULL, 0,
200                         digest)) {
201         SM2err(SM2_F_SM2_ENCRYPT, ERR_R_EVP_LIB);
202         goto done;
203     }
204
205     for (i = 0; i != msg_len; ++i)
206         msg_mask[i] ^= msg[i];
207
208     if (EVP_DigestInit(hash, digest) == 0
209             || EVP_DigestUpdate(hash, x2y2, field_size) == 0
210             || EVP_DigestUpdate(hash, msg, msg_len) == 0
211             || EVP_DigestUpdate(hash, x2y2 + field_size, field_size) == 0
212             || EVP_DigestFinal(hash, C3, NULL) == 0) {
213         SM2err(SM2_F_SM2_ENCRYPT, ERR_R_EVP_LIB);
214         goto done;
215     }
216
217     ctext_struct.C1x = x1;
218     ctext_struct.C1y = y1;
219     ctext_struct.C3 = ASN1_OCTET_STRING_new();
220     ctext_struct.C2 = ASN1_OCTET_STRING_new();
221
222     if (ctext_struct.C3 == NULL || ctext_struct.C2 == NULL) {
223        SM2err(SM2_F_SM2_ENCRYPT, ERR_R_MALLOC_FAILURE);
224        goto done;
225     }
226     if (!ASN1_OCTET_STRING_set(ctext_struct.C3, C3, C3_size)
227             || !ASN1_OCTET_STRING_set(ctext_struct.C2, msg_mask, msg_len)) {
228         SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
229         goto done;
230     }
231
232     ciphertext_leni = i2d_SM2_Ciphertext(&ctext_struct, &ciphertext_buf);
233     /* Ensure cast to size_t is safe */
234     if (ciphertext_leni < 0) {
235         SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
236         goto done;
237     }
238     *ciphertext_len = (size_t)ciphertext_leni;
239
240     rc = 1;
241
242  done:
243     ASN1_OCTET_STRING_free(ctext_struct.C2);
244     ASN1_OCTET_STRING_free(ctext_struct.C3);
245     OPENSSL_free(msg_mask);
246     OPENSSL_free(x2y2);
247     OPENSSL_free(C3);
248     EVP_MD_CTX_free(hash);
249     BN_CTX_free(ctx);
250     EC_POINT_free(kG);
251     EC_POINT_free(kP);
252     return rc;
253 }
254
255 int sm2_decrypt(const EC_KEY *key,
256                 const EVP_MD *digest,
257                 const uint8_t *ciphertext,
258                 size_t ciphertext_len, uint8_t *ptext_buf, size_t *ptext_len)
259 {
260     int rc = 0;
261     int i;
262     BN_CTX *ctx = NULL;
263     const EC_GROUP *group = EC_KEY_get0_group(key);
264     EC_POINT *C1 = NULL;
265     struct SM2_Ciphertext_st *sm2_ctext = NULL;
266     BIGNUM *x2 = NULL;
267     BIGNUM *y2 = NULL;
268     uint8_t *x2y2 = NULL;
269     uint8_t *computed_C3 = NULL;
270     const size_t field_size = ec_field_size(group);
271     const int hash_size = EVP_MD_size(digest);
272     uint8_t *msg_mask = NULL;
273     const uint8_t *C2 = NULL;
274     const uint8_t *C3 = NULL;
275     int msg_len = 0;
276     EVP_MD_CTX *hash = NULL;
277
278     if (field_size == 0 || hash_size <= 0)
279        goto done;
280
281     memset(ptext_buf, 0xFF, *ptext_len);
282
283     sm2_ctext = d2i_SM2_Ciphertext(NULL, &ciphertext, ciphertext_len);
284
285     if (sm2_ctext == NULL) {
286         SM2err(SM2_F_SM2_DECRYPT, SM2_R_ASN1_ERROR);
287         goto done;
288     }
289
290     if (sm2_ctext->C3->length != hash_size) {
291         SM2err(SM2_F_SM2_DECRYPT, SM2_R_INVALID_ENCODING);
292         goto done;
293     }
294
295     C2 = sm2_ctext->C2->data;
296     C3 = sm2_ctext->C3->data;
297     msg_len = sm2_ctext->C2->length;
298
299     ctx = BN_CTX_new();
300     if (ctx == NULL) {
301         SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);
302         goto done;
303     }
304
305     BN_CTX_start(ctx);
306     x2 = BN_CTX_get(ctx);
307     y2 = BN_CTX_get(ctx);
308
309     if (y2 == NULL) {
310         SM2err(SM2_F_SM2_DECRYPT, ERR_R_BN_LIB);
311         goto done;
312     }
313
314     msg_mask = OPENSSL_zalloc(msg_len);
315     x2y2 = OPENSSL_zalloc(2 * field_size);
316     computed_C3 = OPENSSL_zalloc(hash_size);
317
318     if (msg_mask == NULL || x2y2 == NULL || computed_C3 == NULL) {
319         SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);
320         goto done;
321     }
322
323     C1 = EC_POINT_new(group);
324     if (C1 == NULL) {
325         SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);
326         goto done;
327     }
328
329     if (!EC_POINT_set_affine_coordinates(group, C1, sm2_ctext->C1x,
330                                          sm2_ctext->C1y, ctx)
331             || !EC_POINT_mul(group, C1, NULL, C1, EC_KEY_get0_private_key(key),
332                              ctx)
333             || !EC_POINT_get_affine_coordinates(group, C1, x2, y2, ctx)) {
334         SM2err(SM2_F_SM2_DECRYPT, ERR_R_EC_LIB);
335         goto done;
336     }
337
338     if (BN_bn2binpad(x2, x2y2, field_size) < 0
339             || BN_bn2binpad(y2, x2y2 + field_size, field_size) < 0
340             || !ECDH_KDF_X9_62(msg_mask, msg_len, x2y2, 2 * field_size, NULL, 0,
341                                digest)) {
342         SM2err(SM2_F_SM2_DECRYPT, ERR_R_INTERNAL_ERROR);
343         goto done;
344     }
345
346     for (i = 0; i != msg_len; ++i)
347         ptext_buf[i] = C2[i] ^ msg_mask[i];
348
349     hash = EVP_MD_CTX_new();
350     if (hash == NULL) {
351         SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);
352         goto done;
353     }
354
355     if (!EVP_DigestInit(hash, digest)
356             || !EVP_DigestUpdate(hash, x2y2, field_size)
357             || !EVP_DigestUpdate(hash, ptext_buf, msg_len)
358             || !EVP_DigestUpdate(hash, x2y2 + field_size, field_size)
359             || !EVP_DigestFinal(hash, computed_C3, NULL)) {
360         SM2err(SM2_F_SM2_DECRYPT, ERR_R_EVP_LIB);
361         goto done;
362     }
363
364     if (CRYPTO_memcmp(computed_C3, C3, hash_size) != 0) {
365         SM2err(SM2_F_SM2_DECRYPT, SM2_R_INVALID_DIGEST);
366         goto done;
367     }
368
369     rc = 1;
370     *ptext_len = msg_len;
371
372  done:
373     if (rc == 0)
374         memset(ptext_buf, 0, *ptext_len);
375
376     OPENSSL_free(msg_mask);
377     OPENSSL_free(x2y2);
378     OPENSSL_free(computed_C3);
379     EC_POINT_free(C1);
380     BN_CTX_free(ctx);
381     SM2_Ciphertext_free(sm2_ctext);
382     EVP_MD_CTX_free(hash);
383
384     return rc;
385 }