sm2_crypt.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 <openssl/sm2.h>
  12. #include <openssl/evp.h>
  13. #include <openssl/bn.h>
  14. #include <openssl/asn1.h>
  15. #include <openssl/asn1t.h>
  16. #include <string.h>
  17. typedef struct SM2_Ciphertext_st SM2_Ciphertext;
  18. DECLARE_ASN1_FUNCTIONS(SM2_Ciphertext)
  19. struct SM2_Ciphertext_st {
  20. BIGNUM *C1x;
  21. BIGNUM *C1y;
  22. ASN1_OCTET_STRING *C3;
  23. ASN1_OCTET_STRING *C2;
  24. };
  25. ASN1_SEQUENCE(SM2_Ciphertext) = {
  26. ASN1_SIMPLE(SM2_Ciphertext, C1x, BIGNUM),
  27. ASN1_SIMPLE(SM2_Ciphertext, C1y, BIGNUM),
  28. ASN1_SIMPLE(SM2_Ciphertext, C3, ASN1_OCTET_STRING),
  29. ASN1_SIMPLE(SM2_Ciphertext, C2, ASN1_OCTET_STRING),
  30. } ASN1_SEQUENCE_END(SM2_Ciphertext)
  31. IMPLEMENT_ASN1_FUNCTIONS(SM2_Ciphertext)
  32. static size_t EC_field_size(const EC_GROUP *group)
  33. {
  34. /* Is there some simpler way to do this? */
  35. BIGNUM *p = BN_new();
  36. BIGNUM *a = BN_new();
  37. BIGNUM *b = BN_new();
  38. size_t field_size = 0;
  39. if (p == NULL || a == NULL || b == NULL)
  40. goto done;
  41. EC_GROUP_get_curve_GFp(group, p, a, b, NULL);
  42. field_size = (BN_num_bits(p) + 7) / 8;
  43. done:
  44. BN_free(p);
  45. BN_free(a);
  46. BN_free(b);
  47. return field_size;
  48. }
  49. size_t SM2_plaintext_size(const EC_KEY *key, const EVP_MD *digest, size_t msg_len)
  50. {
  51. const size_t field_size = EC_field_size(EC_KEY_get0_group(key));
  52. const size_t md_size = EVP_MD_size(digest);
  53. const size_t overhead = 10 + 2 * field_size + md_size;
  54. if(msg_len <= overhead)
  55. return 0;
  56. return msg_len - overhead;
  57. }
  58. size_t SM2_ciphertext_size(const EC_KEY *key, const EVP_MD *digest, size_t msg_len)
  59. {
  60. const size_t field_size = EC_field_size(EC_KEY_get0_group(key));
  61. const size_t md_size = EVP_MD_size(digest);
  62. return 10 + 2 * field_size + md_size + msg_len;
  63. }
  64. int SM2_encrypt(const EC_KEY *key,
  65. const EVP_MD *digest,
  66. const uint8_t *msg,
  67. size_t msg_len, uint8_t *ciphertext_buf, size_t *ciphertext_len)
  68. {
  69. int rc = 0;
  70. size_t i;
  71. BN_CTX *ctx = NULL;
  72. BIGNUM *k = NULL;
  73. BIGNUM *x1 = NULL;
  74. BIGNUM *y1 = NULL;
  75. BIGNUM *x2 = NULL;
  76. BIGNUM *y2 = NULL;
  77. EVP_MD_CTX *hash = EVP_MD_CTX_new();
  78. struct SM2_Ciphertext_st ctext_struct;
  79. const EC_GROUP *group = EC_KEY_get0_group(key);
  80. const BIGNUM *order = EC_GROUP_get0_order(group);
  81. const EC_POINT *P = EC_KEY_get0_public_key(key);
  82. EC_POINT *kG = NULL;
  83. EC_POINT *kP = NULL;
  84. uint8_t *msg_mask = NULL;
  85. uint8_t *x2y2 = NULL;
  86. uint8_t *C3 = NULL;
  87. const size_t field_size = EC_field_size(group);
  88. const size_t C3_size = EVP_MD_size(digest);
  89. if (field_size == 0 || C3_size == 0)
  90. goto done;
  91. kG = EC_POINT_new(group);
  92. kP = EC_POINT_new(group);
  93. if (kG == NULL || kP == NULL)
  94. goto done;
  95. ctx = BN_CTX_new();
  96. if (ctx == NULL)
  97. goto done;
  98. BN_CTX_start(ctx);
  99. k = BN_CTX_get(ctx);
  100. x1 = BN_CTX_get(ctx);
  101. x2 = BN_CTX_get(ctx);
  102. y1 = BN_CTX_get(ctx);
  103. y2 = BN_CTX_get(ctx);
  104. if (y2 == NULL)
  105. goto done;
  106. x2y2 = OPENSSL_zalloc(2 * field_size);
  107. C3 = OPENSSL_zalloc(C3_size);
  108. if (x2y2 == NULL || C3 == NULL)
  109. goto done;
  110. memset(ciphertext_buf, 0, *ciphertext_len);
  111. BN_priv_rand_range(k, order);
  112. if (EC_POINT_mul(group, kG, k, NULL, NULL, ctx) == 0)
  113. goto done;
  114. if (EC_POINT_get_affine_coordinates_GFp(group, kG, x1, y1, ctx) == 0)
  115. goto done;
  116. if (EC_POINT_mul(group, kP, NULL, P, k, ctx) == 0)
  117. goto done;
  118. if (EC_POINT_get_affine_coordinates_GFp(group, kP, x2, y2, ctx) == 0)
  119. goto done;
  120. BN_bn2binpad(x2, x2y2, field_size);
  121. BN_bn2binpad(y2, x2y2 + field_size, field_size);
  122. msg_mask = OPENSSL_zalloc(msg_len);
  123. if (msg_mask == NULL)
  124. goto done;
  125. /* X9.63 with no salt happens to match the KDF used in SM2 */
  126. if (ECDH_KDF_X9_62(msg_mask, msg_len, x2y2, 2 * field_size, NULL, 0, digest)
  127. == 0)
  128. goto done;
  129. for (i = 0; i != msg_len; ++i)
  130. msg_mask[i] ^= msg[i];
  131. if (EVP_DigestInit(hash, digest) == 0)
  132. goto done;
  133. if (EVP_DigestUpdate(hash, x2y2, field_size) == 0)
  134. goto done;
  135. if (EVP_DigestUpdate(hash, msg, msg_len) == 0)
  136. goto done;
  137. if (EVP_DigestUpdate(hash, x2y2 + field_size, field_size) == 0)
  138. goto done;
  139. if (EVP_DigestFinal(hash, C3, NULL) == 0)
  140. goto done;
  141. ctext_struct.C1x = x1;
  142. ctext_struct.C1y = y1;
  143. ctext_struct.C3 = ASN1_OCTET_STRING_new();
  144. ASN1_OCTET_STRING_set(ctext_struct.C3, C3, C3_size);
  145. ctext_struct.C2 = ASN1_OCTET_STRING_new();
  146. ASN1_OCTET_STRING_set(ctext_struct.C2, msg_mask, msg_len);
  147. *ciphertext_len = i2d_SM2_Ciphertext(&ctext_struct, &ciphertext_buf);
  148. ASN1_OCTET_STRING_free(ctext_struct.C2);
  149. ASN1_OCTET_STRING_free(ctext_struct.C3);
  150. rc = 1;
  151. done:
  152. OPENSSL_free(msg_mask);
  153. OPENSSL_free(x2y2);
  154. OPENSSL_free(C3);
  155. EVP_MD_CTX_free(hash);
  156. BN_CTX_free(ctx);
  157. EC_POINT_free(kG);
  158. EC_POINT_free(kP);
  159. return rc;
  160. }
  161. int SM2_decrypt(const EC_KEY *key,
  162. const EVP_MD *digest,
  163. const uint8_t *ciphertext,
  164. size_t ciphertext_len, uint8_t *ptext_buf, size_t *ptext_len)
  165. {
  166. int rc = 0;
  167. int i;
  168. BN_CTX *ctx = NULL;
  169. const EC_GROUP *group = EC_KEY_get0_group(key);
  170. EC_POINT *C1 = NULL;
  171. struct SM2_Ciphertext_st *sm2_ctext = NULL;
  172. BIGNUM *x2 = NULL;
  173. BIGNUM *y2 = NULL;
  174. uint8_t *x2y2 = NULL;
  175. uint8_t *computed_C3 = NULL;
  176. const size_t field_size = EC_field_size(group);
  177. const int hash_size = EVP_MD_size(digest);
  178. uint8_t *msg_mask = NULL;
  179. const uint8_t *C2 = NULL;
  180. const uint8_t *C3 = NULL;
  181. int msg_len = 0;
  182. EVP_MD_CTX *hash = NULL;
  183. if (field_size == 0 || hash_size == 0)
  184. goto done;
  185. memset(ptext_buf, 0xFF, *ptext_len);
  186. sm2_ctext = d2i_SM2_Ciphertext(NULL, &ciphertext, ciphertext_len);
  187. if (sm2_ctext == NULL)
  188. goto done;
  189. if (sm2_ctext->C3->length != hash_size)
  190. goto done;
  191. C2 = sm2_ctext->C2->data;
  192. C3 = sm2_ctext->C3->data;
  193. msg_len = sm2_ctext->C2->length;
  194. ctx = BN_CTX_new();
  195. if (ctx == NULL)
  196. goto done;
  197. BN_CTX_start(ctx);
  198. x2 = BN_CTX_get(ctx);
  199. y2 = BN_CTX_get(ctx);
  200. if(y2 == NULL)
  201. goto done;
  202. msg_mask = OPENSSL_zalloc(msg_len);
  203. x2y2 = OPENSSL_zalloc(2 * field_size);
  204. computed_C3 = OPENSSL_zalloc(hash_size);
  205. if(msg_mask == NULL || x2y2 == NULL || computed_C3 == NULL)
  206. goto done;
  207. C1 = EC_POINT_new(group);
  208. if (C1 == NULL)
  209. goto done;
  210. if (EC_POINT_set_affine_coordinates_GFp
  211. (group, C1, sm2_ctext->C1x, sm2_ctext->C1y, ctx) == 0)
  212. goto done;
  213. if (EC_POINT_mul(group, C1, NULL, C1, EC_KEY_get0_private_key(key), ctx) ==
  214. 0)
  215. goto done;
  216. if (EC_POINT_get_affine_coordinates_GFp(group, C1, x2, y2, ctx) == 0)
  217. goto done;
  218. BN_bn2binpad(x2, x2y2, field_size);
  219. BN_bn2binpad(y2, x2y2 + field_size, field_size);
  220. if (ECDH_KDF_X9_62(msg_mask, msg_len, x2y2, 2 * field_size, NULL, 0, digest)
  221. == 0)
  222. goto done;
  223. for (i = 0; i != msg_len; ++i)
  224. ptext_buf[i] = C2[i] ^ msg_mask[i];
  225. hash = EVP_MD_CTX_new();
  226. if (hash == NULL)
  227. goto done;
  228. if (EVP_DigestInit(hash, digest) == 0)
  229. goto done;
  230. if (EVP_DigestUpdate(hash, x2y2, field_size) == 0)
  231. goto done;
  232. if (EVP_DigestUpdate(hash, ptext_buf, msg_len) == 0)
  233. goto done;
  234. if (EVP_DigestUpdate(hash, x2y2 + field_size, field_size) == 0)
  235. goto done;
  236. if (EVP_DigestFinal(hash, computed_C3, NULL) == 0)
  237. goto done;
  238. if (memcmp(computed_C3, C3, hash_size) != 0)
  239. goto done;
  240. rc = 1;
  241. *ptext_len = msg_len;
  242. done:
  243. if (rc == 0)
  244. memset(ptext_buf, 0, *ptext_len);
  245. OPENSSL_free(msg_mask);
  246. OPENSSL_free(x2y2);
  247. OPENSSL_free(computed_C3);
  248. EC_POINT_free(C1);
  249. BN_CTX_free(ctx);
  250. SM2_Ciphertext_free(sm2_ctext);
  251. EVP_MD_CTX_free(hash);
  252. return rc;
  253. }