]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/sm2/sm2_crypt.c
Following the license change, modify the boilerplates in crypto/smN/
[thirdparty/openssl.git] / crypto / sm2 / sm2_crypt.c
CommitLineData
3d328a44 1/*
b0edda11 2 * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
3d328a44
JL
3 * Copyright 2017 Ribose Inc. All Rights Reserved.
4 * Ported from Ribose contributions from Botan.
5 *
f9f859ad 6 * Licensed under the Apache License 2.0 (the "License"). You may not use
3d328a44
JL
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
e425f90f 12#include "internal/sm2.h"
2398404e 13#include "internal/sm2err.h"
ffd89124 14#include "internal/ec_int.h" /* ecdh_KDF_X9_63() */
2398404e 15#include <openssl/err.h>
3d328a44
JL
16#include <openssl/evp.h>
17#include <openssl/bn.h>
18#include <openssl/asn1.h>
19#include <openssl/asn1t.h>
20#include <string.h>
21
22typedef struct SM2_Ciphertext_st SM2_Ciphertext;
23DECLARE_ASN1_FUNCTIONS(SM2_Ciphertext)
24
25struct SM2_Ciphertext_st {
26 BIGNUM *C1x;
27 BIGNUM *C1y;
28 ASN1_OCTET_STRING *C3;
29 ASN1_OCTET_STRING *C2;
30};
31
32ASN1_SEQUENCE(SM2_Ciphertext) = {
33 ASN1_SIMPLE(SM2_Ciphertext, C1x, BIGNUM),
34 ASN1_SIMPLE(SM2_Ciphertext, C1y, BIGNUM),
35 ASN1_SIMPLE(SM2_Ciphertext, C3, ASN1_OCTET_STRING),
36 ASN1_SIMPLE(SM2_Ciphertext, C2, ASN1_OCTET_STRING),
37} ASN1_SEQUENCE_END(SM2_Ciphertext)
38
39IMPLEMENT_ASN1_FUNCTIONS(SM2_Ciphertext)
40
2167239a 41static size_t ec_field_size(const EC_GROUP *group)
3d328a44
JL
42{
43 /* Is there some simpler way to do this? */
44 BIGNUM *p = BN_new();
45 BIGNUM *a = BN_new();
46 BIGNUM *b = BN_new();
47 size_t field_size = 0;
48
49 if (p == NULL || a == NULL || b == NULL)
50 goto done;
51
9cc570d4 52 if (!EC_GROUP_get_curve(group, p, a, b, NULL))
bdd92f4d 53 goto done;
3d328a44
JL
54 field_size = (BN_num_bits(p) + 7) / 8;
55
56 done:
57 BN_free(p);
58 BN_free(a);
59 BN_free(b);
60
61 return field_size;
62}
63
2167239a 64int sm2_plaintext_size(const EC_KEY *key, const EVP_MD *digest, size_t msg_len,
e14d6cf6 65 size_t *pt_size)
4e664750 66{
2167239a 67 const size_t field_size = ec_field_size(EC_KEY_get0_group(key));
e14d6cf6
MC
68 const int md_size = EVP_MD_size(digest);
69 size_t overhead;
4e664750 70
e14d6cf6 71 if (md_size < 0) {
245be530 72 SM2err(SM2_F_SM2_PLAINTEXT_SIZE, SM2_R_INVALID_DIGEST);
e14d6cf6
MC
73 return 0;
74 }
75 if (field_size == 0) {
245be530 76 SM2err(SM2_F_SM2_PLAINTEXT_SIZE, SM2_R_INVALID_FIELD);
e14d6cf6
MC
77 return 0;
78 }
4e664750 79
e14d6cf6 80 overhead = 10 + 2 * field_size + (size_t)md_size;
245be530
MC
81 if (msg_len <= overhead) {
82 SM2err(SM2_F_SM2_PLAINTEXT_SIZE, SM2_R_INVALID_ENCODING);
e14d6cf6
MC
83 return 0;
84 }
85
86 *pt_size = msg_len - overhead;
87 return 1;
4e664750
JL
88}
89
2167239a 90int sm2_ciphertext_size(const EC_KEY *key, const EVP_MD *digest, size_t msg_len,
e14d6cf6 91 size_t *ct_size)
3d328a44 92{
2167239a 93 const size_t field_size = ec_field_size(EC_KEY_get0_group(key));
e14d6cf6 94 const int md_size = EVP_MD_size(digest);
307a494e 95 size_t sz;
e14d6cf6
MC
96
97 if (field_size == 0 || md_size < 0)
98 return 0;
99
307a494e 100 /* Integer and string are simple type; set constructed = 0, means primitive and definite length encoding. */
101 sz = 2 * ASN1_object_size(0, field_size + 1, V_ASN1_INTEGER)
102 + ASN1_object_size(0, md_size, V_ASN1_OCTET_STRING)
103 + ASN1_object_size(0, msg_len, V_ASN1_OCTET_STRING);
104 /* Sequence is structured type; set constructed = 1, means constructed and definite length encoding. */
105 *ct_size = ASN1_object_size(1, sz, V_ASN1_SEQUENCE);
106
e14d6cf6 107 return 1;
3d328a44
JL
108}
109
2167239a 110int sm2_encrypt(const EC_KEY *key,
3d328a44
JL
111 const EVP_MD *digest,
112 const uint8_t *msg,
113 size_t msg_len, uint8_t *ciphertext_buf, size_t *ciphertext_len)
114{
44d3845d 115 int rc = 0, ciphertext_leni;
3d328a44
JL
116 size_t i;
117 BN_CTX *ctx = NULL;
118 BIGNUM *k = NULL;
119 BIGNUM *x1 = NULL;
120 BIGNUM *y1 = NULL;
121 BIGNUM *x2 = NULL;
122 BIGNUM *y2 = NULL;
3d328a44 123 EVP_MD_CTX *hash = EVP_MD_CTX_new();
3d328a44
JL
124 struct SM2_Ciphertext_st ctext_struct;
125 const EC_GROUP *group = EC_KEY_get0_group(key);
126 const BIGNUM *order = EC_GROUP_get0_order(group);
127 const EC_POINT *P = EC_KEY_get0_public_key(key);
128 EC_POINT *kG = NULL;
129 EC_POINT *kP = NULL;
130 uint8_t *msg_mask = NULL;
3d328a44
JL
131 uint8_t *x2y2 = NULL;
132 uint8_t *C3 = NULL;
1829ff4b
MC
133 size_t field_size;
134 const int C3_size = EVP_MD_size(digest);
3d328a44 135
44d3845d
MC
136 /* NULL these before any "goto done" */
137 ctext_struct.C2 = NULL;
138 ctext_struct.C3 = NULL;
139
1829ff4b
MC
140 if (hash == NULL || C3_size <= 0) {
141 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
142 goto done;
143 }
144
145 field_size = ec_field_size(group);
146 if (field_size == 0) {
44d3845d
MC
147 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
148 goto done;
149 }
3d328a44
JL
150
151 kG = EC_POINT_new(group);
152 kP = EC_POINT_new(group);
3d328a44 153 ctx = BN_CTX_new();
44d3845d
MC
154 if (kG == NULL || kP == NULL || ctx == NULL) {
155 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_MALLOC_FAILURE);
156 goto done;
157 }
3d328a44
JL
158
159 BN_CTX_start(ctx);
160 k = BN_CTX_get(ctx);
161 x1 = BN_CTX_get(ctx);
162 x2 = BN_CTX_get(ctx);
163 y1 = BN_CTX_get(ctx);
164 y2 = BN_CTX_get(ctx);
165
44d3845d 166 if (y2 == NULL) {
2398404e
JL
167 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_BN_LIB);
168 goto done;
44d3845d 169 }
3d328a44
JL
170
171 x2y2 = OPENSSL_zalloc(2 * field_size);
172 C3 = OPENSSL_zalloc(C3_size);
173
44d3845d 174 if (x2y2 == NULL || C3 == NULL) {
2398404e
JL
175 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_MALLOC_FAILURE);
176 goto done;
44d3845d 177 }
3d328a44
JL
178
179 memset(ciphertext_buf, 0, *ciphertext_len);
180
44d3845d
MC
181 if (!BN_priv_rand_range(k, order)) {
182 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
3d328a44 183 goto done;
44d3845d 184 }
3d328a44 185
44d3845d 186 if (!EC_POINT_mul(group, kG, k, NULL, NULL, ctx)
9cc570d4 187 || !EC_POINT_get_affine_coordinates(group, kG, x1, y1, ctx)
44d3845d 188 || !EC_POINT_mul(group, kP, NULL, P, k, ctx)
9cc570d4 189 || !EC_POINT_get_affine_coordinates(group, kP, x2, y2, ctx)) {
2398404e 190 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_EC_LIB);
3d328a44 191 goto done;
44d3845d 192 }
3d328a44 193
44d3845d
MC
194 if (BN_bn2binpad(x2, x2y2, field_size) < 0
195 || BN_bn2binpad(y2, x2y2 + field_size, field_size) < 0) {
196 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
3d328a44 197 goto done;
44d3845d 198 }
3d328a44
JL
199
200 msg_mask = OPENSSL_zalloc(msg_len);
44d3845d 201 if (msg_mask == NULL) {
2398404e 202 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_MALLOC_FAILURE);
3d328a44 203 goto done;
44d3845d 204 }
3d328a44
JL
205
206 /* X9.63 with no salt happens to match the KDF used in SM2 */
ffd89124 207 if (!ecdh_KDF_X9_63(msg_mask, msg_len, x2y2, 2 * field_size, NULL, 0,
44d3845d
MC
208 digest)) {
209 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_EVP_LIB);
3d328a44 210 goto done;
44d3845d 211 }
3d328a44
JL
212
213 for (i = 0; i != msg_len; ++i)
214 msg_mask[i] ^= msg[i];
215
44d3845d
MC
216 if (EVP_DigestInit(hash, digest) == 0
217 || EVP_DigestUpdate(hash, x2y2, field_size) == 0
218 || EVP_DigestUpdate(hash, msg, msg_len) == 0
219 || EVP_DigestUpdate(hash, x2y2 + field_size, field_size) == 0
220 || EVP_DigestFinal(hash, C3, NULL) == 0) {
2398404e 221 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_EVP_LIB);
3d328a44 222 goto done;
44d3845d 223 }
3d328a44
JL
224
225 ctext_struct.C1x = x1;
226 ctext_struct.C1y = y1;
227 ctext_struct.C3 = ASN1_OCTET_STRING_new();
3d328a44 228 ctext_struct.C2 = ASN1_OCTET_STRING_new();
3d328a44 229
44d3845d
MC
230 if (ctext_struct.C3 == NULL || ctext_struct.C2 == NULL) {
231 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_MALLOC_FAILURE);
232 goto done;
233 }
234 if (!ASN1_OCTET_STRING_set(ctext_struct.C3, C3, C3_size)
235 || !ASN1_OCTET_STRING_set(ctext_struct.C2, msg_mask, msg_len)) {
236 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
237 goto done;
238 }
3d328a44 239
44d3845d
MC
240 ciphertext_leni = i2d_SM2_Ciphertext(&ctext_struct, &ciphertext_buf);
241 /* Ensure cast to size_t is safe */
242 if (ciphertext_leni < 0) {
243 SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
244 goto done;
245 }
246 *ciphertext_len = (size_t)ciphertext_leni;
3d328a44
JL
247
248 rc = 1;
249
250 done:
44d3845d
MC
251 ASN1_OCTET_STRING_free(ctext_struct.C2);
252 ASN1_OCTET_STRING_free(ctext_struct.C3);
3d328a44
JL
253 OPENSSL_free(msg_mask);
254 OPENSSL_free(x2y2);
255 OPENSSL_free(C3);
256 EVP_MD_CTX_free(hash);
257 BN_CTX_free(ctx);
258 EC_POINT_free(kG);
259 EC_POINT_free(kP);
260 return rc;
261}
262
2167239a 263int sm2_decrypt(const EC_KEY *key,
3d328a44
JL
264 const EVP_MD *digest,
265 const uint8_t *ciphertext,
266 size_t ciphertext_len, uint8_t *ptext_buf, size_t *ptext_len)
267{
268 int rc = 0;
269 int i;
3d328a44
JL
270 BN_CTX *ctx = NULL;
271 const EC_GROUP *group = EC_KEY_get0_group(key);
272 EC_POINT *C1 = NULL;
273 struct SM2_Ciphertext_st *sm2_ctext = NULL;
274 BIGNUM *x2 = NULL;
275 BIGNUM *y2 = NULL;
3d328a44
JL
276 uint8_t *x2y2 = NULL;
277 uint8_t *computed_C3 = NULL;
2167239a 278 const size_t field_size = ec_field_size(group);
3d328a44 279 const int hash_size = EVP_MD_size(digest);
3d328a44
JL
280 uint8_t *msg_mask = NULL;
281 const uint8_t *C2 = NULL;
282 const uint8_t *C3 = NULL;
283 int msg_len = 0;
284 EVP_MD_CTX *hash = NULL;
285
1829ff4b 286 if (field_size == 0 || hash_size <= 0)
3d328a44
JL
287 goto done;
288
289 memset(ptext_buf, 0xFF, *ptext_len);
290
291 sm2_ctext = d2i_SM2_Ciphertext(NULL, &ciphertext, ciphertext_len);
292
245be530 293 if (sm2_ctext == NULL) {
2398404e 294 SM2err(SM2_F_SM2_DECRYPT, SM2_R_ASN1_ERROR);
3d328a44 295 goto done;
245be530 296 }
3d328a44 297
245be530 298 if (sm2_ctext->C3->length != hash_size) {
2398404e 299 SM2err(SM2_F_SM2_DECRYPT, SM2_R_INVALID_ENCODING);
3d328a44 300 goto done;
245be530 301 }
3d328a44
JL
302
303 C2 = sm2_ctext->C2->data;
304 C3 = sm2_ctext->C3->data;
305 msg_len = sm2_ctext->C2->length;
306
307 ctx = BN_CTX_new();
245be530 308 if (ctx == NULL) {
2398404e
JL
309 SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);
310 goto done;
245be530 311 }
3d328a44
JL
312
313 BN_CTX_start(ctx);
314 x2 = BN_CTX_get(ctx);
315 y2 = BN_CTX_get(ctx);
316
245be530 317 if (y2 == NULL) {
2398404e
JL
318 SM2err(SM2_F_SM2_DECRYPT, ERR_R_BN_LIB);
319 goto done;
245be530 320 }
3d328a44
JL
321
322 msg_mask = OPENSSL_zalloc(msg_len);
323 x2y2 = OPENSSL_zalloc(2 * field_size);
324 computed_C3 = OPENSSL_zalloc(hash_size);
325
245be530 326 if (msg_mask == NULL || x2y2 == NULL || computed_C3 == NULL) {
2398404e
JL
327 SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);
328 goto done;
245be530 329 }
3d328a44
JL
330
331 C1 = EC_POINT_new(group);
245be530 332 if (C1 == NULL) {
2398404e 333 SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);
3d328a44 334 goto done;
245be530 335 }
3d328a44 336
9cc570d4
MC
337 if (!EC_POINT_set_affine_coordinates(group, C1, sm2_ctext->C1x,
338 sm2_ctext->C1y, ctx)
245be530
MC
339 || !EC_POINT_mul(group, C1, NULL, C1, EC_KEY_get0_private_key(key),
340 ctx)
9cc570d4 341 || !EC_POINT_get_affine_coordinates(group, C1, x2, y2, ctx)) {
2398404e 342 SM2err(SM2_F_SM2_DECRYPT, ERR_R_EC_LIB);
3d328a44 343 goto done;
245be530 344 }
3d328a44 345
245be530
MC
346 if (BN_bn2binpad(x2, x2y2, field_size) < 0
347 || BN_bn2binpad(y2, x2y2 + field_size, field_size) < 0
ffd89124 348 || !ecdh_KDF_X9_63(msg_mask, msg_len, x2y2, 2 * field_size, NULL, 0,
245be530
MC
349 digest)) {
350 SM2err(SM2_F_SM2_DECRYPT, ERR_R_INTERNAL_ERROR);
3d328a44 351 goto done;
245be530 352 }
3d328a44
JL
353
354 for (i = 0; i != msg_len; ++i)
355 ptext_buf[i] = C2[i] ^ msg_mask[i];
356
357 hash = EVP_MD_CTX_new();
245be530 358 if (hash == NULL) {
2398404e
JL
359 SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);
360 goto done;
245be530 361 }
3d328a44 362
245be530
MC
363 if (!EVP_DigestInit(hash, digest)
364 || !EVP_DigestUpdate(hash, x2y2, field_size)
365 || !EVP_DigestUpdate(hash, ptext_buf, msg_len)
366 || !EVP_DigestUpdate(hash, x2y2 + field_size, field_size)
367 || !EVP_DigestFinal(hash, computed_C3, NULL)) {
2398404e 368 SM2err(SM2_F_SM2_DECRYPT, ERR_R_EVP_LIB);
3d328a44 369 goto done;
245be530 370 }
3d328a44 371
245be530
MC
372 if (CRYPTO_memcmp(computed_C3, C3, hash_size) != 0) {
373 SM2err(SM2_F_SM2_DECRYPT, SM2_R_INVALID_DIGEST);
3d328a44 374 goto done;
245be530 375 }
3d328a44
JL
376
377 rc = 1;
dceb99a5 378 *ptext_len = msg_len;
3d328a44
JL
379
380 done:
3d328a44
JL
381 if (rc == 0)
382 memset(ptext_buf, 0, *ptext_len);
383
384 OPENSSL_free(msg_mask);
385 OPENSSL_free(x2y2);
386 OPENSSL_free(computed_C3);
387 EC_POINT_free(C1);
388 BN_CTX_free(ctx);
389 SM2_Ciphertext_free(sm2_ctext);
390 EVP_MD_CTX_free(hash);
391
392 return rc;
393}