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.
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
12 #include "internal/sm2.h"
13 #include <openssl/evp.h>
14 #include <openssl/bn.h>
15 #include <openssl/asn1.h>
16 #include <openssl/asn1t.h>
19 typedef struct SM2_Ciphertext_st SM2_Ciphertext
;
20 DECLARE_ASN1_FUNCTIONS(SM2_Ciphertext
)
22 struct SM2_Ciphertext_st
{
25 ASN1_OCTET_STRING
*C3
;
26 ASN1_OCTET_STRING
*C2
;
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
)
36 IMPLEMENT_ASN1_FUNCTIONS(SM2_Ciphertext
)
38 static size_t EC_field_size(const EC_GROUP
*group
)
40 /* Is there some simpler way to do this? */
44 size_t field_size
= 0;
46 if (p
== NULL
|| a
== NULL
|| b
== NULL
)
49 EC_GROUP_get_curve_GFp(group
, p
, a
, b
, NULL
);
50 field_size
= (BN_num_bits(p
) + 7) / 8;
60 size_t SM2_plaintext_size(const EC_KEY
*key
, const EVP_MD
*digest
, size_t msg_len
)
62 const size_t field_size
= EC_field_size(EC_KEY_get0_group(key
));
63 const size_t md_size
= EVP_MD_size(digest
);
65 const size_t overhead
= 10 + 2 * field_size
+ md_size
;
66 if(msg_len
<= overhead
)
69 return msg_len
- overhead
;
72 size_t SM2_ciphertext_size(const EC_KEY
*key
, const EVP_MD
*digest
, size_t msg_len
)
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
;
79 int SM2_encrypt(const EC_KEY
*key
,
82 size_t msg_len
, uint8_t *ciphertext_buf
, size_t *ciphertext_len
)
93 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
);
101 uint8_t *msg_mask
= NULL
;
103 uint8_t *x2y2
= NULL
;
106 const size_t field_size
= EC_field_size(group
);
107 const size_t C3_size
= EVP_MD_size(digest
);
109 if (field_size
== 0 || C3_size
== 0)
112 kG
= EC_POINT_new(group
);
113 kP
= EC_POINT_new(group
);
114 if (kG
== NULL
|| kP
== NULL
)
123 x1
= BN_CTX_get(ctx
);
124 x2
= BN_CTX_get(ctx
);
125 y1
= BN_CTX_get(ctx
);
126 y2
= BN_CTX_get(ctx
);
131 x2y2
= OPENSSL_zalloc(2 * field_size
);
132 C3
= OPENSSL_zalloc(C3_size
);
134 if (x2y2
== NULL
|| C3
== NULL
)
137 memset(ciphertext_buf
, 0, *ciphertext_len
);
139 BN_priv_rand_range(k
, order
);
141 if (EC_POINT_mul(group
, kG
, k
, NULL
, NULL
, ctx
) == 0)
144 if (EC_POINT_get_affine_coordinates_GFp(group
, kG
, x1
, y1
, ctx
) == 0)
147 if (EC_POINT_mul(group
, kP
, NULL
, P
, k
, ctx
) == 0)
150 if (EC_POINT_get_affine_coordinates_GFp(group
, kP
, x2
, y2
, ctx
) == 0)
153 BN_bn2binpad(x2
, x2y2
, field_size
);
154 BN_bn2binpad(y2
, x2y2
+ field_size
, field_size
);
156 msg_mask
= OPENSSL_zalloc(msg_len
);
157 if (msg_mask
== NULL
)
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
)
165 for (i
= 0; i
!= msg_len
; ++i
)
166 msg_mask
[i
] ^= msg
[i
];
168 if (EVP_DigestInit(hash
, digest
) == 0)
171 if (EVP_DigestUpdate(hash
, x2y2
, field_size
) == 0)
174 if (EVP_DigestUpdate(hash
, msg
, msg_len
) == 0)
177 if (EVP_DigestUpdate(hash
, x2y2
+ field_size
, field_size
) == 0)
180 if (EVP_DigestFinal(hash
, C3
, NULL
) == 0)
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
);
190 *ciphertext_len
= i2d_SM2_Ciphertext(&ctext_struct
, &ciphertext_buf
);
192 ASN1_OCTET_STRING_free(ctext_struct
.C2
);
193 ASN1_OCTET_STRING_free(ctext_struct
.C3
);
198 OPENSSL_free(msg_mask
);
201 EVP_MD_CTX_free(hash
);
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
)
217 const EC_GROUP
*group
= EC_KEY_get0_group(key
);
219 struct SM2_Ciphertext_st
*sm2_ctext
= NULL
;
223 uint8_t *x2y2
= NULL
;
224 uint8_t *computed_C3
= NULL
;
226 const size_t field_size
= EC_field_size(group
);
227 const int hash_size
= EVP_MD_size(digest
);
229 uint8_t *msg_mask
= NULL
;
230 const uint8_t *C2
= NULL
;
231 const uint8_t *C3
= NULL
;
233 EVP_MD_CTX
*hash
= NULL
;
235 if (field_size
== 0 || hash_size
== 0)
238 memset(ptext_buf
, 0xFF, *ptext_len
);
240 sm2_ctext
= d2i_SM2_Ciphertext(NULL
, &ciphertext
, ciphertext_len
);
242 if (sm2_ctext
== NULL
)
245 if (sm2_ctext
->C3
->length
!= hash_size
)
248 C2
= sm2_ctext
->C2
->data
;
249 C3
= sm2_ctext
->C3
->data
;
250 msg_len
= sm2_ctext
->C2
->length
;
257 x2
= BN_CTX_get(ctx
);
258 y2
= BN_CTX_get(ctx
);
263 msg_mask
= OPENSSL_zalloc(msg_len
);
264 x2y2
= OPENSSL_zalloc(2 * field_size
);
265 computed_C3
= OPENSSL_zalloc(hash_size
);
267 if(msg_mask
== NULL
|| x2y2
== NULL
|| computed_C3
== NULL
)
270 C1
= EC_POINT_new(group
);
274 if (EC_POINT_set_affine_coordinates_GFp
275 (group
, C1
, sm2_ctext
->C1x
, sm2_ctext
->C1y
, ctx
) == 0)
278 if (EC_POINT_mul(group
, C1
, NULL
, C1
, EC_KEY_get0_private_key(key
), ctx
) ==
282 if (EC_POINT_get_affine_coordinates_GFp(group
, C1
, x2
, y2
, ctx
) == 0)
285 BN_bn2binpad(x2
, x2y2
, field_size
);
286 BN_bn2binpad(y2
, x2y2
+ field_size
, field_size
);
288 if (ECDH_KDF_X9_62(msg_mask
, msg_len
, x2y2
, 2 * field_size
, NULL
, 0, digest
)
292 for (i
= 0; i
!= msg_len
; ++i
)
293 ptext_buf
[i
] = C2
[i
] ^ msg_mask
[i
];
295 hash
= EVP_MD_CTX_new();
300 if (EVP_DigestInit(hash
, digest
) == 0)
303 if (EVP_DigestUpdate(hash
, x2y2
, field_size
) == 0)
306 if (EVP_DigestUpdate(hash
, ptext_buf
, msg_len
) == 0)
309 if (EVP_DigestUpdate(hash
, x2y2
+ field_size
, field_size
) == 0)
312 if (EVP_DigestFinal(hash
, computed_C3
, NULL
) == 0)
315 if (memcmp(computed_C3
, C3
, hash_size
) != 0)
319 *ptext_len
= msg_len
;
324 memset(ptext_buf
, 0, *ptext_len
);
326 OPENSSL_free(msg_mask
);
328 OPENSSL_free(computed_C3
);
331 SM2_Ciphertext_free(sm2_ctext
);
332 EVP_MD_CTX_free(hash
);