sm2_crypt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * Copyright 2017-2024 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 Apache License 2.0 (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. * ECDSA low level APIs are deprecated for public use, but still ok for
  13. * internal use.
  14. */
  15. #include "internal/deprecated.h"
  16. #include "crypto/sm2.h"
  17. #include "crypto/sm2err.h"
  18. #include "crypto/ec.h" /* ossl_ecdh_kdf_X9_63() */
  19. #include <openssl/err.h>
  20. #include <openssl/evp.h>
  21. #include <openssl/bn.h>
  22. #include <openssl/asn1.h>
  23. #include <openssl/asn1t.h>
  24. #include <string.h>
  25. typedef struct SM2_Ciphertext_st SM2_Ciphertext;
  26. DECLARE_ASN1_FUNCTIONS(SM2_Ciphertext)
  27. struct SM2_Ciphertext_st {
  28. BIGNUM *C1x;
  29. BIGNUM *C1y;
  30. ASN1_OCTET_STRING *C3;
  31. ASN1_OCTET_STRING *C2;
  32. };
  33. ASN1_SEQUENCE(SM2_Ciphertext) = {
  34. ASN1_SIMPLE(SM2_Ciphertext, C1x, BIGNUM),
  35. ASN1_SIMPLE(SM2_Ciphertext, C1y, BIGNUM),
  36. ASN1_SIMPLE(SM2_Ciphertext, C3, ASN1_OCTET_STRING),
  37. ASN1_SIMPLE(SM2_Ciphertext, C2, ASN1_OCTET_STRING),
  38. } ASN1_SEQUENCE_END(SM2_Ciphertext)
  39. IMPLEMENT_ASN1_FUNCTIONS(SM2_Ciphertext)
  40. static size_t ec_field_size(const EC_GROUP *group)
  41. {
  42. const BIGNUM *p = EC_GROUP_get0_field(group);
  43. if (p == NULL)
  44. return 0;
  45. return BN_num_bytes(p);
  46. }
  47. static int is_all_zeros(const unsigned char *msg, size_t msglen)
  48. {
  49. unsigned char re = 0;
  50. size_t i;
  51. for (i = 0; i < msglen; i++) {
  52. re |= msg[i];
  53. }
  54. return re == 0 ? 1 : 0;
  55. }
  56. int ossl_sm2_plaintext_size(const unsigned char *ct, size_t ct_size,
  57. size_t *pt_size)
  58. {
  59. struct SM2_Ciphertext_st *sm2_ctext = NULL;
  60. sm2_ctext = d2i_SM2_Ciphertext(NULL, &ct, ct_size);
  61. if (sm2_ctext == NULL) {
  62. ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_ENCODING);
  63. return 0;
  64. }
  65. *pt_size = sm2_ctext->C2->length;
  66. SM2_Ciphertext_free(sm2_ctext);
  67. return 1;
  68. }
  69. int ossl_sm2_ciphertext_size(const EC_KEY *key, const EVP_MD *digest,
  70. size_t msg_len, size_t *ct_size)
  71. {
  72. const size_t field_size = ec_field_size(EC_KEY_get0_group(key));
  73. const int md_size = EVP_MD_get_size(digest);
  74. size_t sz;
  75. if (field_size == 0 || md_size < 0)
  76. return 0;
  77. /* Integer and string are simple type; set constructed = 0, means primitive and definite length encoding. */
  78. sz = 2 * ASN1_object_size(0, field_size + 1, V_ASN1_INTEGER)
  79. + ASN1_object_size(0, md_size, V_ASN1_OCTET_STRING)
  80. + ASN1_object_size(0, msg_len, V_ASN1_OCTET_STRING);
  81. /* Sequence is structured type; set constructed = 1, means constructed and definite length encoding. */
  82. *ct_size = ASN1_object_size(1, sz, V_ASN1_SEQUENCE);
  83. return 1;
  84. }
  85. int ossl_sm2_encrypt(const EC_KEY *key,
  86. const EVP_MD *digest,
  87. const uint8_t *msg, size_t msg_len,
  88. uint8_t *ciphertext_buf, size_t *ciphertext_len)
  89. {
  90. int rc = 0, ciphertext_leni;
  91. size_t i;
  92. BN_CTX *ctx = NULL;
  93. BIGNUM *k = NULL;
  94. BIGNUM *x1 = NULL;
  95. BIGNUM *y1 = NULL;
  96. BIGNUM *x2 = NULL;
  97. BIGNUM *y2 = NULL;
  98. EVP_MD_CTX *hash = EVP_MD_CTX_new();
  99. struct SM2_Ciphertext_st ctext_struct;
  100. const EC_GROUP *group = EC_KEY_get0_group(key);
  101. const BIGNUM *order = EC_GROUP_get0_order(group);
  102. const EC_POINT *P = EC_KEY_get0_public_key(key);
  103. EC_POINT *kG = NULL;
  104. EC_POINT *kP = NULL;
  105. uint8_t *msg_mask = NULL;
  106. uint8_t *x2y2 = NULL;
  107. uint8_t *C3 = NULL;
  108. size_t field_size;
  109. const int C3_size = EVP_MD_get_size(digest);
  110. EVP_MD *fetched_digest = NULL;
  111. OSSL_LIB_CTX *libctx = ossl_ec_key_get_libctx(key);
  112. const char *propq = ossl_ec_key_get0_propq(key);
  113. /* NULL these before any "goto done" */
  114. ctext_struct.C2 = NULL;
  115. ctext_struct.C3 = NULL;
  116. if (hash == NULL || C3_size <= 0) {
  117. ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
  118. goto done;
  119. }
  120. field_size = ec_field_size(group);
  121. if (field_size == 0) {
  122. ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
  123. goto done;
  124. }
  125. kG = EC_POINT_new(group);
  126. kP = EC_POINT_new(group);
  127. if (kG == NULL || kP == NULL) {
  128. ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
  129. goto done;
  130. }
  131. ctx = BN_CTX_new_ex(libctx);
  132. if (ctx == NULL) {
  133. ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
  134. goto done;
  135. }
  136. BN_CTX_start(ctx);
  137. k = BN_CTX_get(ctx);
  138. x1 = BN_CTX_get(ctx);
  139. x2 = BN_CTX_get(ctx);
  140. y1 = BN_CTX_get(ctx);
  141. y2 = BN_CTX_get(ctx);
  142. if (y2 == NULL) {
  143. ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
  144. goto done;
  145. }
  146. x2y2 = OPENSSL_zalloc(2 * field_size);
  147. C3 = OPENSSL_zalloc(C3_size);
  148. if (x2y2 == NULL || C3 == NULL)
  149. goto done;
  150. memset(ciphertext_buf, 0, *ciphertext_len);
  151. msg_mask = OPENSSL_zalloc(msg_len);
  152. if (msg_mask == NULL)
  153. goto done;
  154. again:
  155. if (!BN_priv_rand_range_ex(k, order, 0, ctx)) {
  156. ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
  157. goto done;
  158. }
  159. if (!EC_POINT_mul(group, kG, k, NULL, NULL, ctx)
  160. || !EC_POINT_get_affine_coordinates(group, kG, x1, y1, ctx)
  161. || !EC_POINT_mul(group, kP, NULL, P, k, ctx)
  162. || !EC_POINT_get_affine_coordinates(group, kP, x2, y2, ctx)) {
  163. ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
  164. goto done;
  165. }
  166. if (BN_bn2binpad(x2, x2y2, field_size) < 0
  167. || BN_bn2binpad(y2, x2y2 + field_size, field_size) < 0) {
  168. ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
  169. goto done;
  170. }
  171. /* X9.63 with no salt happens to match the KDF used in SM2 */
  172. if (!ossl_ecdh_kdf_X9_63(msg_mask, msg_len, x2y2, 2 * field_size, NULL, 0,
  173. digest, libctx, propq)) {
  174. ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
  175. goto done;
  176. }
  177. if (is_all_zeros(msg_mask, msg_len)) {
  178. memset(x2y2, 0, 2 * field_size);
  179. goto again;
  180. }
  181. for (i = 0; i != msg_len; ++i)
  182. msg_mask[i] ^= msg[i];
  183. fetched_digest = EVP_MD_fetch(libctx, EVP_MD_get0_name(digest), propq);
  184. if (fetched_digest == NULL) {
  185. ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
  186. goto done;
  187. }
  188. if (EVP_DigestInit(hash, fetched_digest) == 0
  189. || EVP_DigestUpdate(hash, x2y2, field_size) == 0
  190. || EVP_DigestUpdate(hash, msg, msg_len) == 0
  191. || EVP_DigestUpdate(hash, x2y2 + field_size, field_size) == 0
  192. || EVP_DigestFinal(hash, C3, NULL) == 0) {
  193. ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
  194. goto done;
  195. }
  196. ctext_struct.C1x = x1;
  197. ctext_struct.C1y = y1;
  198. ctext_struct.C3 = ASN1_OCTET_STRING_new();
  199. ctext_struct.C2 = ASN1_OCTET_STRING_new();
  200. if (ctext_struct.C3 == NULL || ctext_struct.C2 == NULL) {
  201. ERR_raise(ERR_LIB_SM2, ERR_R_ASN1_LIB);
  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. ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
  207. goto done;
  208. }
  209. ciphertext_leni = i2d_SM2_Ciphertext(&ctext_struct, &ciphertext_buf);
  210. /* Ensure cast to size_t is safe */
  211. if (ciphertext_leni < 0) {
  212. ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
  213. goto done;
  214. }
  215. *ciphertext_len = (size_t)ciphertext_leni;
  216. rc = 1;
  217. done:
  218. EVP_MD_free(fetched_digest);
  219. ASN1_OCTET_STRING_free(ctext_struct.C2);
  220. ASN1_OCTET_STRING_free(ctext_struct.C3);
  221. OPENSSL_free(msg_mask);
  222. OPENSSL_free(x2y2);
  223. OPENSSL_free(C3);
  224. EVP_MD_CTX_free(hash);
  225. BN_CTX_free(ctx);
  226. EC_POINT_free(kG);
  227. EC_POINT_free(kP);
  228. return rc;
  229. }
  230. int ossl_sm2_decrypt(const EC_KEY *key,
  231. const EVP_MD *digest,
  232. const uint8_t *ciphertext, size_t ciphertext_len,
  233. uint8_t *ptext_buf, size_t *ptext_len)
  234. {
  235. int rc = 0;
  236. int i;
  237. BN_CTX *ctx = NULL;
  238. const EC_GROUP *group = EC_KEY_get0_group(key);
  239. EC_POINT *C1 = NULL;
  240. struct SM2_Ciphertext_st *sm2_ctext = NULL;
  241. BIGNUM *x2 = NULL;
  242. BIGNUM *y2 = NULL;
  243. uint8_t *x2y2 = NULL;
  244. uint8_t *computed_C3 = NULL;
  245. const size_t field_size = ec_field_size(group);
  246. const int hash_size = EVP_MD_get_size(digest);
  247. uint8_t *msg_mask = NULL;
  248. const uint8_t *C2 = NULL;
  249. const uint8_t *C3 = NULL;
  250. int msg_len = 0;
  251. EVP_MD_CTX *hash = NULL;
  252. OSSL_LIB_CTX *libctx = ossl_ec_key_get_libctx(key);
  253. const char *propq = ossl_ec_key_get0_propq(key);
  254. if (field_size == 0 || hash_size <= 0)
  255. goto done;
  256. memset(ptext_buf, 0xFF, *ptext_len);
  257. sm2_ctext = d2i_SM2_Ciphertext(NULL, &ciphertext, ciphertext_len);
  258. if (sm2_ctext == NULL) {
  259. ERR_raise(ERR_LIB_SM2, SM2_R_ASN1_ERROR);
  260. goto done;
  261. }
  262. if (sm2_ctext->C3->length != hash_size) {
  263. ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_ENCODING);
  264. goto done;
  265. }
  266. C2 = sm2_ctext->C2->data;
  267. C3 = sm2_ctext->C3->data;
  268. msg_len = sm2_ctext->C2->length;
  269. if (*ptext_len < (size_t)msg_len) {
  270. ERR_raise(ERR_LIB_SM2, SM2_R_BUFFER_TOO_SMALL);
  271. goto done;
  272. }
  273. ctx = BN_CTX_new_ex(libctx);
  274. if (ctx == NULL) {
  275. ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
  276. goto done;
  277. }
  278. BN_CTX_start(ctx);
  279. x2 = BN_CTX_get(ctx);
  280. y2 = BN_CTX_get(ctx);
  281. if (y2 == NULL) {
  282. ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
  283. goto done;
  284. }
  285. msg_mask = OPENSSL_zalloc(msg_len);
  286. x2y2 = OPENSSL_zalloc(2 * field_size);
  287. computed_C3 = OPENSSL_zalloc(hash_size);
  288. if (msg_mask == NULL || x2y2 == NULL || computed_C3 == NULL)
  289. goto done;
  290. C1 = EC_POINT_new(group);
  291. if (C1 == NULL) {
  292. ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
  293. goto done;
  294. }
  295. if (!EC_POINT_set_affine_coordinates(group, C1, sm2_ctext->C1x,
  296. sm2_ctext->C1y, ctx)
  297. || !EC_POINT_mul(group, C1, NULL, C1, EC_KEY_get0_private_key(key),
  298. ctx)
  299. || !EC_POINT_get_affine_coordinates(group, C1, x2, y2, ctx)) {
  300. ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
  301. goto done;
  302. }
  303. if (BN_bn2binpad(x2, x2y2, field_size) < 0
  304. || BN_bn2binpad(y2, x2y2 + field_size, field_size) < 0
  305. || !ossl_ecdh_kdf_X9_63(msg_mask, msg_len, x2y2, 2 * field_size,
  306. NULL, 0, digest, libctx, propq)) {
  307. ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
  308. goto done;
  309. }
  310. if (is_all_zeros(msg_mask, msg_len)) {
  311. ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_ENCODING);
  312. goto done;
  313. }
  314. for (i = 0; i != msg_len; ++i)
  315. ptext_buf[i] = C2[i] ^ msg_mask[i];
  316. hash = EVP_MD_CTX_new();
  317. if (hash == NULL) {
  318. ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
  319. goto done;
  320. }
  321. if (!EVP_DigestInit(hash, digest)
  322. || !EVP_DigestUpdate(hash, x2y2, field_size)
  323. || !EVP_DigestUpdate(hash, ptext_buf, msg_len)
  324. || !EVP_DigestUpdate(hash, x2y2 + field_size, field_size)
  325. || !EVP_DigestFinal(hash, computed_C3, NULL)) {
  326. ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
  327. goto done;
  328. }
  329. if (CRYPTO_memcmp(computed_C3, C3, hash_size) != 0) {
  330. ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_DIGEST);
  331. goto done;
  332. }
  333. rc = 1;
  334. *ptext_len = msg_len;
  335. done:
  336. if (rc == 0)
  337. memset(ptext_buf, 0, *ptext_len);
  338. OPENSSL_free(msg_mask);
  339. OPENSSL_free(x2y2);
  340. OPENSSL_free(computed_C3);
  341. EC_POINT_free(C1);
  342. BN_CTX_free(ctx);
  343. SM2_Ciphertext_free(sm2_ctext);
  344. EVP_MD_CTX_free(hash);
  345. return rc;
  346. }