sm2_crypt.c 11 KB

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