sm2_crypt.c 11 KB

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