sm2_crypt.c 12 KB

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