sm2_crypt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * Copyright 2017-2021 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. #include "crypto/sm2.h"
  12. #include "crypto/sm2err.h"
  13. #include "crypto/ec.h" /* ecdh_KDF_X9_63() */
  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. typedef struct SM2_Ciphertext_st SM2_Ciphertext;
  21. DECLARE_ASN1_FUNCTIONS(SM2_Ciphertext)
  22. struct SM2_Ciphertext_st {
  23. BIGNUM *C1x;
  24. BIGNUM *C1y;
  25. ASN1_OCTET_STRING *C3;
  26. ASN1_OCTET_STRING *C2;
  27. };
  28. ASN1_SEQUENCE(SM2_Ciphertext) = {
  29. ASN1_SIMPLE(SM2_Ciphertext, C1x, BIGNUM),
  30. ASN1_SIMPLE(SM2_Ciphertext, C1y, BIGNUM),
  31. ASN1_SIMPLE(SM2_Ciphertext, C3, ASN1_OCTET_STRING),
  32. ASN1_SIMPLE(SM2_Ciphertext, C2, ASN1_OCTET_STRING),
  33. } ASN1_SEQUENCE_END(SM2_Ciphertext)
  34. IMPLEMENT_ASN1_FUNCTIONS(SM2_Ciphertext)
  35. static size_t ec_field_size(const EC_GROUP *group)
  36. {
  37. /* Is there some simpler way to do this? */
  38. BIGNUM *p = BN_new();
  39. BIGNUM *a = BN_new();
  40. BIGNUM *b = BN_new();
  41. size_t field_size = 0;
  42. if (p == NULL || a == NULL || b == NULL)
  43. goto done;
  44. if (!EC_GROUP_get_curve(group, p, a, b, NULL))
  45. goto done;
  46. field_size = (BN_num_bits(p) + 7) / 8;
  47. done:
  48. BN_free(p);
  49. BN_free(a);
  50. BN_free(b);
  51. return field_size;
  52. }
  53. int sm2_plaintext_size(const unsigned char *ct, size_t ct_size, size_t *pt_size)
  54. {
  55. struct SM2_Ciphertext_st *sm2_ctext = NULL;
  56. sm2_ctext = d2i_SM2_Ciphertext(NULL, &ct, ct_size);
  57. if (sm2_ctext == NULL) {
  58. SM2err(SM2_F_SM2_PLAINTEXT_SIZE, SM2_R_INVALID_ENCODING);
  59. return 0;
  60. }
  61. *pt_size = sm2_ctext->C2->length;
  62. SM2_Ciphertext_free(sm2_ctext);
  63. return 1;
  64. }
  65. int sm2_ciphertext_size(const EC_KEY *key, const EVP_MD *digest, size_t msg_len,
  66. size_t *ct_size)
  67. {
  68. const size_t field_size = ec_field_size(EC_KEY_get0_group(key));
  69. const int md_size = EVP_MD_size(digest);
  70. size_t sz;
  71. if (field_size == 0 || md_size < 0)
  72. return 0;
  73. /* Integer and string are simple type; set constructed = 0, means primitive and definite length encoding. */
  74. sz = 2 * ASN1_object_size(0, field_size + 1, V_ASN1_INTEGER)
  75. + ASN1_object_size(0, md_size, V_ASN1_OCTET_STRING)
  76. + ASN1_object_size(0, msg_len, V_ASN1_OCTET_STRING);
  77. /* Sequence is structured type; set constructed = 1, means constructed and definite length encoding. */
  78. *ct_size = ASN1_object_size(1, sz, V_ASN1_SEQUENCE);
  79. return 1;
  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. size_t field_size;
  105. const int C3_size = EVP_MD_size(digest);
  106. /* NULL these before any "goto done" */
  107. ctext_struct.C2 = NULL;
  108. ctext_struct.C3 = NULL;
  109. if (hash == NULL || C3_size <= 0) {
  110. SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
  111. goto done;
  112. }
  113. field_size = ec_field_size(group);
  114. if (field_size == 0) {
  115. SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
  116. goto done;
  117. }
  118. kG = EC_POINT_new(group);
  119. kP = EC_POINT_new(group);
  120. ctx = BN_CTX_new();
  121. if (kG == NULL || kP == NULL || ctx == NULL) {
  122. SM2err(SM2_F_SM2_ENCRYPT, ERR_R_MALLOC_FAILURE);
  123. goto done;
  124. }
  125. BN_CTX_start(ctx);
  126. k = BN_CTX_get(ctx);
  127. x1 = BN_CTX_get(ctx);
  128. x2 = BN_CTX_get(ctx);
  129. y1 = BN_CTX_get(ctx);
  130. y2 = BN_CTX_get(ctx);
  131. if (y2 == NULL) {
  132. SM2err(SM2_F_SM2_ENCRYPT, ERR_R_BN_LIB);
  133. goto done;
  134. }
  135. x2y2 = OPENSSL_zalloc(2 * field_size);
  136. C3 = OPENSSL_zalloc(C3_size);
  137. if (x2y2 == NULL || C3 == NULL) {
  138. SM2err(SM2_F_SM2_ENCRYPT, ERR_R_MALLOC_FAILURE);
  139. goto done;
  140. }
  141. memset(ciphertext_buf, 0, *ciphertext_len);
  142. if (!BN_priv_rand_range(k, order)) {
  143. SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
  144. goto done;
  145. }
  146. if (!EC_POINT_mul(group, kG, k, NULL, NULL, ctx)
  147. || !EC_POINT_get_affine_coordinates(group, kG, x1, y1, ctx)
  148. || !EC_POINT_mul(group, kP, NULL, P, k, ctx)
  149. || !EC_POINT_get_affine_coordinates(group, kP, x2, y2, ctx)) {
  150. SM2err(SM2_F_SM2_ENCRYPT, ERR_R_EC_LIB);
  151. goto done;
  152. }
  153. if (BN_bn2binpad(x2, x2y2, field_size) < 0
  154. || BN_bn2binpad(y2, x2y2 + field_size, field_size) < 0) {
  155. SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
  156. goto done;
  157. }
  158. msg_mask = OPENSSL_zalloc(msg_len);
  159. if (msg_mask == NULL) {
  160. SM2err(SM2_F_SM2_ENCRYPT, ERR_R_MALLOC_FAILURE);
  161. goto done;
  162. }
  163. /* X9.63 with no salt happens to match the KDF used in SM2 */
  164. if (!ecdh_KDF_X9_63(msg_mask, msg_len, x2y2, 2 * field_size, NULL, 0,
  165. digest)) {
  166. SM2err(SM2_F_SM2_ENCRYPT, ERR_R_EVP_LIB);
  167. goto done;
  168. }
  169. for (i = 0; i != msg_len; ++i)
  170. msg_mask[i] ^= msg[i];
  171. if (EVP_DigestInit(hash, digest) == 0
  172. || EVP_DigestUpdate(hash, x2y2, field_size) == 0
  173. || EVP_DigestUpdate(hash, msg, msg_len) == 0
  174. || EVP_DigestUpdate(hash, x2y2 + field_size, field_size) == 0
  175. || EVP_DigestFinal(hash, C3, NULL) == 0) {
  176. SM2err(SM2_F_SM2_ENCRYPT, ERR_R_EVP_LIB);
  177. goto done;
  178. }
  179. ctext_struct.C1x = x1;
  180. ctext_struct.C1y = y1;
  181. ctext_struct.C3 = ASN1_OCTET_STRING_new();
  182. ctext_struct.C2 = ASN1_OCTET_STRING_new();
  183. if (ctext_struct.C3 == NULL || ctext_struct.C2 == NULL) {
  184. SM2err(SM2_F_SM2_ENCRYPT, ERR_R_MALLOC_FAILURE);
  185. goto done;
  186. }
  187. if (!ASN1_OCTET_STRING_set(ctext_struct.C3, C3, C3_size)
  188. || !ASN1_OCTET_STRING_set(ctext_struct.C2, msg_mask, msg_len)) {
  189. SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
  190. goto done;
  191. }
  192. ciphertext_leni = i2d_SM2_Ciphertext(&ctext_struct, &ciphertext_buf);
  193. /* Ensure cast to size_t is safe */
  194. if (ciphertext_leni < 0) {
  195. SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
  196. goto done;
  197. }
  198. *ciphertext_len = (size_t)ciphertext_leni;
  199. rc = 1;
  200. done:
  201. ASN1_OCTET_STRING_free(ctext_struct.C2);
  202. ASN1_OCTET_STRING_free(ctext_struct.C3);
  203. OPENSSL_free(msg_mask);
  204. OPENSSL_free(x2y2);
  205. OPENSSL_free(C3);
  206. EVP_MD_CTX_free(hash);
  207. BN_CTX_free(ctx);
  208. EC_POINT_free(kG);
  209. EC_POINT_free(kP);
  210. return rc;
  211. }
  212. int sm2_decrypt(const EC_KEY *key,
  213. const EVP_MD *digest,
  214. const uint8_t *ciphertext,
  215. size_t ciphertext_len, uint8_t *ptext_buf, size_t *ptext_len)
  216. {
  217. int rc = 0;
  218. int i;
  219. BN_CTX *ctx = NULL;
  220. const EC_GROUP *group = EC_KEY_get0_group(key);
  221. EC_POINT *C1 = NULL;
  222. struct SM2_Ciphertext_st *sm2_ctext = NULL;
  223. BIGNUM *x2 = NULL;
  224. BIGNUM *y2 = NULL;
  225. uint8_t *x2y2 = NULL;
  226. uint8_t *computed_C3 = NULL;
  227. const size_t field_size = ec_field_size(group);
  228. 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;
  232. int msg_len = 0;
  233. EVP_MD_CTX *hash = NULL;
  234. if (field_size == 0 || hash_size <= 0)
  235. goto done;
  236. memset(ptext_buf, 0xFF, *ptext_len);
  237. sm2_ctext = d2i_SM2_Ciphertext(NULL, &ciphertext, ciphertext_len);
  238. if (sm2_ctext == NULL) {
  239. SM2err(SM2_F_SM2_DECRYPT, SM2_R_ASN1_ERROR);
  240. goto done;
  241. }
  242. if (sm2_ctext->C3->length != hash_size) {
  243. SM2err(SM2_F_SM2_DECRYPT, SM2_R_INVALID_ENCODING);
  244. goto done;
  245. }
  246. C2 = sm2_ctext->C2->data;
  247. C3 = sm2_ctext->C3->data;
  248. msg_len = sm2_ctext->C2->length;
  249. if (*ptext_len < (size_t)msg_len) {
  250. SM2err(SM2_F_SM2_DECRYPT, SM2_R_BUFFER_TOO_SMALL);
  251. goto done;
  252. }
  253. ctx = BN_CTX_new();
  254. if (ctx == NULL) {
  255. SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);
  256. goto done;
  257. }
  258. BN_CTX_start(ctx);
  259. x2 = BN_CTX_get(ctx);
  260. y2 = BN_CTX_get(ctx);
  261. if (y2 == NULL) {
  262. SM2err(SM2_F_SM2_DECRYPT, ERR_R_BN_LIB);
  263. goto done;
  264. }
  265. msg_mask = OPENSSL_zalloc(msg_len);
  266. x2y2 = OPENSSL_zalloc(2 * field_size);
  267. computed_C3 = OPENSSL_zalloc(hash_size);
  268. if (msg_mask == NULL || x2y2 == NULL || computed_C3 == NULL) {
  269. SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);
  270. goto done;
  271. }
  272. C1 = EC_POINT_new(group);
  273. if (C1 == NULL) {
  274. SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);
  275. goto done;
  276. }
  277. if (!EC_POINT_set_affine_coordinates(group, C1, sm2_ctext->C1x,
  278. sm2_ctext->C1y, ctx)
  279. || !EC_POINT_mul(group, C1, NULL, C1, EC_KEY_get0_private_key(key),
  280. ctx)
  281. || !EC_POINT_get_affine_coordinates(group, C1, x2, y2, ctx)) {
  282. SM2err(SM2_F_SM2_DECRYPT, ERR_R_EC_LIB);
  283. goto done;
  284. }
  285. if (BN_bn2binpad(x2, x2y2, field_size) < 0
  286. || BN_bn2binpad(y2, x2y2 + field_size, field_size) < 0
  287. || !ecdh_KDF_X9_63(msg_mask, msg_len, x2y2, 2 * field_size, NULL, 0,
  288. digest)) {
  289. SM2err(SM2_F_SM2_DECRYPT, ERR_R_INTERNAL_ERROR);
  290. goto done;
  291. }
  292. for (i = 0; i != msg_len; ++i)
  293. ptext_buf[i] = C2[i] ^ msg_mask[i];
  294. hash = EVP_MD_CTX_new();
  295. if (hash == NULL) {
  296. SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);
  297. goto done;
  298. }
  299. if (!EVP_DigestInit(hash, digest)
  300. || !EVP_DigestUpdate(hash, x2y2, field_size)
  301. || !EVP_DigestUpdate(hash, ptext_buf, msg_len)
  302. || !EVP_DigestUpdate(hash, x2y2 + field_size, field_size)
  303. || !EVP_DigestFinal(hash, computed_C3, NULL)) {
  304. SM2err(SM2_F_SM2_DECRYPT, ERR_R_EVP_LIB);
  305. goto done;
  306. }
  307. if (CRYPTO_memcmp(computed_C3, C3, hash_size) != 0) {
  308. SM2err(SM2_F_SM2_DECRYPT, SM2_R_INVALID_DIGEST);
  309. goto done;
  310. }
  311. rc = 1;
  312. *ptext_len = msg_len;
  313. done:
  314. if (rc == 0)
  315. memset(ptext_buf, 0, *ptext_len);
  316. OPENSSL_free(msg_mask);
  317. OPENSSL_free(x2y2);
  318. OPENSSL_free(computed_C3);
  319. EC_POINT_free(C1);
  320. BN_CTX_free(ctx);
  321. SM2_Ciphertext_free(sm2_ctext);
  322. EVP_MD_CTX_free(hash);
  323. return rc;
  324. }