sm2_crypt.c 11 KB

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