sm9.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*
  2. * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the License); you may
  5. * not use this file except in compliance with the License.
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <stdint.h>
  13. #include <gmssl/sm3.h>
  14. #include <gmssl/sm2.h>
  15. #ifndef GMSSL_SM9_H
  16. #define GMSSL_SM9_H
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /*
  21. SM9 Public API
  22. SM9_SIGNATURE_SIZE
  23. SM9_MAX_PLAINTEXT_SIZE
  24. SM9_MAX_CIPHERTEXT_SIZE
  25. SM9_SIGN_MASTER_KEY
  26. sm9_sign_master_key_generate
  27. sm9_sign_master_key_extract_key
  28. sm9_sign_master_key_info_encrypt_to_der
  29. sm9_sign_master_key_info_decrypt_from_der
  30. sm9_sign_master_key_info_encrypt_to_pem
  31. sm9_sign_master_key_info_decrypt_from_pem
  32. sm9_sign_master_public_key_to_der
  33. sm9_sign_master_public_key_from_der
  34. sm9_sign_master_public_key_to_pem
  35. sm9_sign_master_public_key_from_pem
  36. SM9_SIGN_KEY
  37. sm9_sign_key_info_encrypt_to_der
  38. sm9_sign_key_info_decrypt_from_der
  39. sm9_sign_key_info_encrypt_to_pem
  40. sm9_sign_key_info_decrypt_from_pem
  41. SM9_SIGN_CTX
  42. sm9_sign_init
  43. sm9_sign_update
  44. sm9_sign_finish
  45. sm9_verify_init
  46. sm9_verify_update
  47. sm9_verify_finish
  48. SM9_ENC_MASTER_KEY
  49. sm9_enc_master_key_generate
  50. sm9_enc_master_key_extract_key
  51. sm9_enc_master_key_info_encrypt_to_der
  52. sm9_enc_master_key_info_decrypt_from_der
  53. sm9_enc_master_key_info_encrypt_to_pem
  54. sm9_enc_master_key_info_decrypt_from_pem
  55. sm9_enc_master_public_key_to_der
  56. sm9_enc_master_public_key_from_der
  57. sm9_enc_master_public_key_to_pem
  58. sm9_enc_master_public_key_from_pem
  59. SM9_ENC_KEY
  60. sm9_enc_key_info_encrypt_to_der
  61. sm9_enc_key_info_decrypt_from_der
  62. sm9_enc_key_info_encrypt_to_pem
  63. sm9_enc_key_info_decrypt_from_pem
  64. sm9_encrypt
  65. sm9_decrypt
  66. */
  67. #define SM9_HEX_SEP '\n'
  68. typedef uint64_t sm9_bn_t[8];
  69. #define sm9_bn_init(r) sm9_bn_set_zero(r)
  70. #define sm9_bn_clean(r) sm9_bn_set_zero(r)
  71. void sm9_bn_set_zero(sm9_bn_t r);
  72. void sm9_bn_set_one(sm9_bn_t r);
  73. int sm9_bn_is_zero(const sm9_bn_t a);
  74. int sm9_bn_is_one(const sm9_bn_t a);
  75. void sm9_bn_set_word(sm9_bn_t r, uint32_t a);
  76. void sm9_bn_copy(sm9_bn_t r, const sm9_bn_t a);
  77. int sm9_bn_rand_range(sm9_bn_t r, const sm9_bn_t range);
  78. int sm9_bn_equ(const sm9_bn_t a, const sm9_bn_t b);
  79. int sm9_bn_cmp(const sm9_bn_t a, const sm9_bn_t b);
  80. void sm9_bn_add(sm9_bn_t r, const sm9_bn_t a, const sm9_bn_t b);
  81. void sm9_bn_sub(sm9_bn_t ret, const sm9_bn_t a, const sm9_bn_t b);
  82. void sm9_bn_to_bits(const sm9_bn_t a, char bits[256]);
  83. void sm9_bn_to_bytes(const sm9_bn_t a, uint8_t out[32]);
  84. void sm9_bn_from_bytes(sm9_bn_t r, const uint8_t in[32]);
  85. void sm9_bn_to_hex(const sm9_bn_t a, char hex[64]);
  86. int sm9_bn_from_hex(sm9_bn_t r, const char hex[64]);
  87. int sm9_bn_print(FILE *fp, int fmt, int ind, const char *label, const sm9_bn_t a);
  88. void sm9_print_bn(const char *prefix, const sm9_bn_t a); // 标准打印格式
  89. typedef sm9_bn_t sm9_fp_t;
  90. #define sm9_fp_init(r) sm9_fp_set_zero(r)
  91. #define sm9_fp_clean(f) sm9_fp_set_zero(r)
  92. #define sm9_fp_set_zero(r) sm9_bn_set_zero(r)
  93. #define sm9_fp_set_one(r) sm9_bn_set_one(r)
  94. #define sm9_fp_copy(r,a) sm9_bn_copy((r),(a))
  95. #define sm9_fp_rand(r) sm9_bn_rand_range((r), SM9_P)
  96. #define sm9_fp_is_zero(a) sm9_bn_is_zero(a)
  97. #define sm9_fp_is_one(a) sm9_bn_is_one(a)
  98. #define sm9_fp_equ(a,b) sm9_bn_equ((a),(b))
  99. #define sm9_fp_to_bytes(a,buf) sm9_bn_to_bytes((a),(buf))
  100. #define sm9_fp_to_hex(a,s) sm9_bn_to_hex((a),(s))
  101. #define sm9_fp_print(fp,fmt,ind,label,a) sm9_bn_print(fp,fmt,ind,label,a)
  102. void sm9_fp_add(sm9_fp_t r, const sm9_fp_t a, const sm9_fp_t b);
  103. void sm9_fp_sub(sm9_fp_t r, const sm9_fp_t a, const sm9_fp_t b);
  104. void sm9_fp_dbl(sm9_fp_t r, const sm9_fp_t a);
  105. void sm9_fp_tri(sm9_fp_t r, const sm9_fp_t a);
  106. void sm9_fp_neg(sm9_fp_t r, const sm9_fp_t a);
  107. void sm9_fp_mul(sm9_fp_t r, const sm9_fp_t a, const sm9_fp_t b);
  108. void sm9_fp_sqr(sm9_fp_t r, const sm9_fp_t a);
  109. void sm9_fp_pow(sm9_fp_t r, const sm9_fp_t a, const sm9_bn_t e);
  110. void sm9_fp_inv(sm9_fp_t r, const sm9_fp_t a);
  111. void sm9_fp_div2(sm9_fp_t r, const sm9_fp_t a);
  112. int sm9_fp_from_bytes(sm9_fp_t r, const uint8_t buf[32]);
  113. int sm9_fp_from_hex(sm9_fp_t r, const char hex[64]);
  114. typedef sm9_bn_t sm9_fn_t;
  115. #define sm9_fn_init(r) sm9_fn_set_zero(r)
  116. #define sm9_fn_clean(f) sm9_fn_set_zero(r)
  117. #define sm9_fn_set_zero(r) sm9_bn_set_zero(r)
  118. #define sm9_fn_set_one(r) sm9_bn_set_one(r)
  119. #define sm9_fn_copy(r,a) sm9_bn_copy((r),(a))
  120. #define sm9_fn_rand(r) sm9_bn_rand_range((r), SM9_N)
  121. #define sm9_fn_is_zero(a) sm9_bn_is_zero(a)
  122. #define sm9_fn_is_one(a) sm9_bn_is_one(a)
  123. #define sm9_fn_equ(a,b) sm9_bn_equ((a),(b))
  124. #define sm9_fn_to_bytes(a,out) sm9_bn_to_bytes((a),(out))
  125. #define sm9_fn_to_hex(a,s) sm9_bn_to_hex((a),(s))
  126. #define sm9_fn_print(fp,fmt,ind,label,a) sm9_bn_print(fp,fmt,ind,label,a)
  127. void sm9_fn_add(sm9_fn_t r, const sm9_fn_t a, const sm9_fn_t b);
  128. void sm9_fn_sub(sm9_fn_t r, const sm9_fn_t a, const sm9_fn_t b);
  129. void sm9_fn_mul(sm9_fn_t r, const sm9_fn_t a, const sm9_fn_t b);
  130. void sm9_fn_pow(sm9_fn_t r, const sm9_fn_t a, const sm9_bn_t e);
  131. void sm9_fn_inv(sm9_fn_t r, const sm9_fn_t a);
  132. void sm9_fn_from_hash(sm9_fn_t h, const uint8_t Ha[40]);
  133. int sm9_fn_from_bytes(sm9_fn_t a, const uint8_t in[32]);
  134. int sm9_fn_from_hex(sm9_fn_t r, const char hex[64]);
  135. typedef uint64_t sm9_barrett_bn_t[9];
  136. int sm9_barrett_bn_cmp(const sm9_barrett_bn_t a, const sm9_barrett_bn_t b);
  137. void sm9_barrett_bn_add(sm9_barrett_bn_t r, const sm9_barrett_bn_t a, const sm9_barrett_bn_t b);
  138. void sm9_barrett_bn_sub(sm9_barrett_bn_t ret, const sm9_barrett_bn_t a, const sm9_barrett_bn_t b);
  139. typedef sm9_fp_t sm9_fp2_t[2];
  140. extern const sm9_fp2_t SM9_FP2_ZERO;
  141. extern const sm9_fp2_t SM9_FP2_ONE;
  142. extern const sm9_fp2_t SM9_FP2_U;
  143. #define sm9_fp2_init(a) sm9_fp2_set_zero(a)
  144. #define sm9_fp2_clean(a) sm9_fp2_set_zero(a)
  145. #define sm9_fp2_set_zero(a) sm9_fp2_copy((a), SM9_FP2_ZERO)
  146. #define sm9_fp2_set_one(a) sm9_fp2_copy((a), SM9_FP2_ONE)
  147. #define sm9_fp2_set_u(a) sm9_fp2_copy((a), SM9_FP2_U)
  148. #define sm9_fp2_is_zero(a) sm9_fp2_equ((a), SM9_FP2_ZERO)
  149. #define sm9_fp2_is_one(a) sm9_fp2_equ((a), SM9_FP2_ONE)
  150. void sm9_fp2_set_fp(sm9_fp2_t r, const sm9_fp_t a);
  151. void sm9_fp2_set(sm9_fp2_t r, const sm9_fp_t a0, const sm9_fp_t a1);
  152. void sm9_fp2_copy(sm9_fp2_t r, const sm9_fp2_t a);
  153. int sm9_fp2_rand(sm9_fp2_t r);
  154. int sm9_fp2_equ(const sm9_fp2_t a, const sm9_fp2_t b);
  155. void sm9_fp2_add(sm9_fp2_t r, const sm9_fp2_t a, const sm9_fp2_t b);
  156. void sm9_fp2_dbl(sm9_fp2_t r, const sm9_fp2_t a);
  157. void sm9_fp2_tri(sm9_fp2_t r, const sm9_fp2_t a);
  158. void sm9_fp2_sub(sm9_fp2_t r, const sm9_fp2_t a, const sm9_fp2_t b);
  159. void sm9_fp2_neg(sm9_fp2_t r, const sm9_fp2_t a);
  160. void sm9_fp2_mul(sm9_fp2_t r, const sm9_fp2_t a, const sm9_fp2_t b);
  161. void sm9_fp2_mul_u(sm9_fp2_t r, const sm9_fp2_t a, const sm9_fp2_t b);
  162. void sm9_fp2_mul_fp(sm9_fp2_t r, const sm9_fp2_t a, const sm9_fp_t k);
  163. void sm9_fp2_sqr(sm9_fp2_t r, const sm9_fp2_t a);
  164. void sm9_fp2_sqr_u(sm9_fp2_t r, const sm9_fp2_t a);
  165. void sm9_fp2_inv(sm9_fp2_t r, const sm9_fp2_t a);
  166. void sm9_fp2_div(sm9_fp2_t r, const sm9_fp2_t a, const sm9_fp2_t b);
  167. void sm9_fp2_div2(sm9_fp2_t r, const sm9_fp2_t a);
  168. void sm9_fp2_to_hex(const sm9_fp2_t a, char hex[129]);
  169. int sm9_fp2_from_hex(sm9_fp2_t r, const char hex[129]);
  170. int sm9_fp2_print(FILE *fp, int fmt, int ind, const char *label, const sm9_fp2_t a);
  171. typedef sm9_fp2_t sm9_fp4_t[2];
  172. extern const sm9_fp4_t SM9_FP4_ZERO;
  173. extern const sm9_fp4_t SM9_FP4_ONE;
  174. extern const sm9_fp4_t SM9_FP4_U;
  175. extern const sm9_fp4_t SM9_FP4_V;
  176. #define sm9_fp4_init(a) sm9_fp4_set_zero(a)
  177. #define sm9_fp4_clean(a) sm9_fp4_set_zero(a)
  178. #define sm9_fp4_set_zero(a) sm9_fp4_copy((a), SM9_FP4_ZERO)
  179. #define sm9_fp4_set_one(a) sm9_fp4_copy((a), SM9_FP4_ONE)
  180. #define sm9_fp4_is_zero(a) sm9_fp4_equ((a), SM9_FP4_ZERO)
  181. #define sm9_fp4_is_one(a) sm9_fp4_equ((a), SM9_FP4_ONE)
  182. void sm9_fp4_set_u(sm9_fp4_t r);
  183. void sm9_fp4_set_v(sm9_fp4_t r);
  184. void sm9_fp4_set_fp(sm9_fp4_t r, const sm9_fp_t a);
  185. void sm9_fp4_set_fp2(sm9_fp4_t r, const sm9_fp2_t a);
  186. void sm9_fp4_set(sm9_fp4_t r, const sm9_fp2_t a0, const sm9_fp2_t a1);
  187. void sm9_fp4_copy(sm9_fp4_t r, const sm9_fp4_t a);
  188. int sm9_fp4_rand(sm9_fp4_t r);
  189. int sm9_fp4_equ(const sm9_fp4_t a, const sm9_fp4_t b);
  190. void sm9_fp4_add(sm9_fp4_t r, const sm9_fp4_t a, const sm9_fp4_t b);
  191. void sm9_fp4_dbl(sm9_fp4_t r, const sm9_fp4_t a);
  192. void sm9_fp4_sub(sm9_fp4_t r, const sm9_fp4_t a, const sm9_fp4_t b);
  193. void sm9_fp4_neg(sm9_fp4_t r, const sm9_fp4_t a);
  194. void sm9_fp4_mul(sm9_fp4_t r, const sm9_fp4_t a, const sm9_fp4_t b);
  195. void sm9_fp4_mul_fp(sm9_fp4_t r, const sm9_fp4_t a, const sm9_fp_t k);
  196. void sm9_fp4_mul_fp2(sm9_fp4_t r, const sm9_fp4_t a, const sm9_fp2_t b0);
  197. void sm9_fp4_mul_v(sm9_fp4_t r, const sm9_fp4_t a, const sm9_fp4_t b);
  198. void sm9_fp4_sqr(sm9_fp4_t r, const sm9_fp4_t a);
  199. void sm9_fp4_sqr_v(sm9_fp4_t r, const sm9_fp4_t a);
  200. void sm9_fp4_inv(sm9_fp4_t r, const sm9_fp4_t a);
  201. void sm9_fp4_to_bytes(const sm9_fp4_t a, uint8_t buf[128]);
  202. int sm9_fp4_from_bytes(sm9_fp4_t r, const uint8_t buf[128]);
  203. void sm9_fp4_to_hex(const sm9_fp4_t a, char hex[259]);
  204. int sm9_fp4_from_hex(sm9_fp4_t r, const char hex[259]);
  205. typedef sm9_fp4_t sm9_fp12_t[3];
  206. #define sm9_fp12_init(r) sm9_fp12_set_zero(a)
  207. #define sm9_fp12_clean(r) sm9_fp12_set_zero(a)
  208. void sm9_fp12_set_zero(sm9_fp12_t r);
  209. void sm9_fp12_set_one(sm9_fp12_t r);
  210. void sm9_fp12_set_u(sm9_fp12_t r);
  211. void sm9_fp12_set_v(sm9_fp12_t r);
  212. void sm9_fp12_set_w(sm9_fp12_t r);
  213. void sm9_fp12_set_w_sqr(sm9_fp12_t r);
  214. void sm9_fp12_set_fp(sm9_fp12_t r, const sm9_fp_t a);
  215. void sm9_fp12_set_fp2(sm9_fp12_t r, const sm9_fp2_t a);
  216. void sm9_fp12_set_fp4(sm9_fp12_t r, const sm9_fp4_t a);
  217. void sm9_fp12_set(sm9_fp12_t r, const sm9_fp4_t a0, const sm9_fp4_t a1, const sm9_fp4_t a2);
  218. void sm9_fp12_copy(sm9_fp12_t r, const sm9_fp12_t a);
  219. int sm9_fp12_rand(sm9_fp12_t r);
  220. int sm9_fp12_is_one(const sm9_fp12_t a);
  221. int sm9_fp12_is_zero(const sm9_fp12_t a);
  222. int sm9_fp12_equ(const sm9_fp12_t a, const sm9_fp12_t b);
  223. void sm9_fp12_add(sm9_fp12_t r, const sm9_fp12_t a, const sm9_fp12_t b);
  224. void sm9_fp12_dbl(sm9_fp12_t r, const sm9_fp12_t a);
  225. void sm9_fp12_tri(sm9_fp12_t r, const sm9_fp12_t a);
  226. void sm9_fp12_sub(sm9_fp12_t r, const sm9_fp12_t a, const sm9_fp12_t b);
  227. void sm9_fp12_neg(sm9_fp12_t r, const sm9_fp12_t a);
  228. void sm9_fp12_mul(sm9_fp12_t r, const sm9_fp12_t a, const sm9_fp12_t b);
  229. void sm9_fp12_sqr(sm9_fp12_t r, const sm9_fp12_t a);
  230. void sm9_fp12_inv(sm9_fp12_t r, const sm9_fp12_t a);
  231. void sm9_fp12_pow(sm9_fp12_t r, const sm9_fp12_t a, const sm9_bn_t k);
  232. void sm9_fp12_to_bytes(const sm9_fp12_t a, uint8_t buf[32 * 12]);
  233. int sm9_fp12_from_bytes(sm9_fp12_t r, const uint8_t in[32 * 12]);
  234. void sm9_fp12_to_hex(const sm9_fp12_t a, char hex[65 * 12]);
  235. int sm9_fp12_from_hex(sm9_fp12_t r, const char hex[65 * 12]); // 这个明显是不对的
  236. void sm9_fp12_print(const char *prefix, const sm9_fp12_t a);
  237. void sm9_fp2_conjugate(sm9_fp2_t r, const sm9_fp2_t a);
  238. void sm9_fp2_frobenius(sm9_fp2_t r, const sm9_fp2_t a);
  239. void sm9_fp4_frobenius(sm9_fp4_t r, const sm9_fp4_t a);
  240. void sm9_fp4_conjugate(sm9_fp4_t r, const sm9_fp4_t a);
  241. void sm9_fp4_frobenius2(sm9_fp4_t r, const sm9_fp4_t a);
  242. void sm9_fp4_frobenius3(sm9_fp4_t r, const sm9_fp4_t a);
  243. void sm9_fp12_frobenius(sm9_fp12_t r, const sm9_fp12_t x);
  244. void sm9_fp12_frobenius2(sm9_fp12_t r, const sm9_fp12_t x);
  245. void sm9_fp12_frobenius3(sm9_fp12_t r, const sm9_fp12_t x);
  246. void sm9_fp12_frobenius6(sm9_fp12_t r, const sm9_fp12_t x);
  247. typedef struct {
  248. sm9_fp_t X;
  249. sm9_fp_t Y;
  250. sm9_fp_t Z;
  251. } SM9_POINT;
  252. #define sm9_point_init(R) sm9_point_set_infinity(R)
  253. #define sm9_point_clean(R) sm9_point_set_infinity(R)
  254. void sm9_point_set_infinity(SM9_POINT *R);
  255. void sm9_point_copy(SM9_POINT *R, const SM9_POINT *P);
  256. void sm9_point_get_xy(const SM9_POINT *P, sm9_fp_t x, sm9_fp_t y);
  257. int sm9_point_is_at_infinity(const SM9_POINT *P);
  258. int sm9_point_equ(const SM9_POINT *P, const SM9_POINT *Q);
  259. int sm9_point_is_on_curve(const SM9_POINT *P);
  260. void sm9_point_dbl(SM9_POINT *R, const SM9_POINT *P);
  261. void sm9_point_add(SM9_POINT *R, const SM9_POINT *P, const SM9_POINT *Q);
  262. void sm9_point_neg(SM9_POINT *R, const SM9_POINT *P);
  263. void sm9_point_sub(SM9_POINT *R, const SM9_POINT *P, const SM9_POINT *Q);
  264. void sm9_point_mul(SM9_POINT *R, const sm9_bn_t k, const SM9_POINT *P);
  265. void sm9_point_mul_generator(SM9_POINT *R, const sm9_bn_t k);
  266. void sm9_point_from_hex(SM9_POINT *R, const char hex[65 * 2]);
  267. int sm9_point_to_uncompressed_octets(const SM9_POINT *P, uint8_t octets[65]);
  268. int sm9_point_from_uncompressed_octets(SM9_POINT *P, const uint8_t octets[65]);
  269. int sm9_point_print(FILE *fp, int fmt, int ind, const char *label, const SM9_POINT *P);
  270. typedef struct {
  271. sm9_fp2_t X;
  272. sm9_fp2_t Y;
  273. sm9_fp2_t Z;
  274. } SM9_TWIST_POINT;
  275. #define sm9_twist_point_copy(R, P) memcpy((R), (P), sizeof(SM9_TWIST_POINT))
  276. int sm9_twist_point_to_uncompressed_octets(const SM9_TWIST_POINT *P, uint8_t octets[129]);
  277. int sm9_twist_point_from_uncompressed_octets(SM9_TWIST_POINT *P, const uint8_t octets[129]);
  278. void sm9_twist_point_from_hex(SM9_TWIST_POINT *R, const char hex[65 * 4]);
  279. int sm9_twist_point_is_at_infinity(const SM9_TWIST_POINT *P);
  280. void sm9_twist_point_set_infinity(SM9_TWIST_POINT *R);
  281. void sm9_twist_point_get_xy(const SM9_TWIST_POINT *P, sm9_fp2_t x, sm9_fp2_t y);
  282. int sm9_twist_point_equ(const SM9_TWIST_POINT *P, const SM9_TWIST_POINT *Q);
  283. int sm9_twist_point_is_on_curve(const SM9_TWIST_POINT *P);
  284. void sm9_twist_point_neg(SM9_TWIST_POINT *R, const SM9_TWIST_POINT *P);
  285. void sm9_twist_point_dbl(SM9_TWIST_POINT *R, const SM9_TWIST_POINT *P);
  286. void sm9_twist_point_add(SM9_TWIST_POINT *R, const SM9_TWIST_POINT *P, const SM9_TWIST_POINT *Q);
  287. void sm9_twist_point_sub(SM9_TWIST_POINT *R, const SM9_TWIST_POINT *P, const SM9_TWIST_POINT *Q);
  288. void sm9_twist_point_add_full(SM9_TWIST_POINT *R, const SM9_TWIST_POINT *P, const SM9_TWIST_POINT *Q);
  289. void sm9_twist_point_mul(SM9_TWIST_POINT *R, const sm9_bn_t k, const SM9_TWIST_POINT *P);
  290. void sm9_twist_point_mul_generator(SM9_TWIST_POINT *R, const sm9_bn_t k);
  291. int sm9_twist_point_print(FILE *fp, int fmt, int ind, const char *label, const SM9_TWIST_POINT *P);
  292. void sm9_eval_g_tangent(sm9_fp12_t num, sm9_fp12_t den, const SM9_TWIST_POINT *P, const SM9_POINT *Q);
  293. void sm9_eval_g_line(sm9_fp12_t num, sm9_fp12_t den, const SM9_TWIST_POINT *T, const SM9_TWIST_POINT *P, const SM9_POINT *Q);
  294. void sm9_twist_point_pi1(SM9_TWIST_POINT *R, const SM9_TWIST_POINT *P);
  295. void sm9_twist_point_pi2(SM9_TWIST_POINT *R, const SM9_TWIST_POINT *P);
  296. void sm9_twist_point_neg_pi2(SM9_TWIST_POINT *R, const SM9_TWIST_POINT *P);
  297. void sm9_final_exponent_hard_part(sm9_fp12_t r, const sm9_fp12_t f);
  298. void sm9_final_exponent(sm9_fp12_t r, const sm9_fp12_t f);
  299. void sm9_pairing(sm9_fp12_t r, const SM9_TWIST_POINT *Q, const SM9_POINT *P);
  300. /* private key extract algorithms */
  301. #define SM9_HID_SIGN 0x01
  302. #define SM9_HID_EXCH 0x02
  303. #define SM9_HID_ENC 0x03
  304. #define SM9_HASH1_PREFIX 0x01
  305. #define SM9_HASH2_PREFIX 0x02
  306. int sm9_hash1(sm9_bn_t h1, const char *id, size_t idlen, uint8_t hid);
  307. const char *sm9_oid_name(int oid);
  308. int sm9_oid_from_name(const char *name);
  309. int sm9_oid_to_der(int oid, uint8_t **out, size_t *outlen);
  310. int sm9_oid_from_der(int *oid, const uint8_t **in, size_t *inlen);
  311. int sm9_algor_to_der(int alg, int params, uint8_t **out, size_t *outlen);
  312. int sm9_algor_from_der(int *alg, int *params, const uint8_t **in, size_t *inlen);
  313. #define PEM_SM9_SIGN_MASTER_KEY "ENCRYPTED SM9 SIGN MASTER KEY"
  314. #define PEM_SM9_SIGN_MASTER_PUBLIC_KEY "SM9 SIGN MASTER PUBLIC KEY"
  315. #define PEM_SM9_SIGN_PRIVATE_KEY "ENCRYPTED SM9 SIGN PRIVATE KEY"
  316. #define PEM_SM9_ENC_MASTER_KEY "ENCRYPTED SM9 ENC MASTER KEY"
  317. #define PEM_SM9_ENC_MASTER_PUBLIC_KEY "SM9 ENC MASTER PUBLIC KEY"
  318. #define PEM_SM9_ENC_PRIVATE_KEY "ENCRYPTED SM9 ENC PRIVATE KEY"
  319. #define SM9_MAX_ID_SIZE (SM2_MAX_ID_SIZE)
  320. /*
  321. SM9SignMasterKey ::= SEQUENCE {
  322. ks INTEGER,
  323. Ppubs BIT STRING -- uncompressed octets of twisted point }
  324. SM9SignMasterPublicKey ::= SEQUENCE {
  325. Ppubs BIT STRING -- uncompressed octets of twisted point }
  326. SM9SignPrivateKey ::= SEQUENCE {
  327. ds BIT STRING, -- uncompressed octets of ECPoint
  328. Ppubs BIT STRING -- uncompressed octets of twisted point }
  329. */
  330. typedef struct {
  331. SM9_TWIST_POINT Ppubs; // Ppubs = ks * P2
  332. sm9_fn_t ks;
  333. } SM9_SIGN_MASTER_KEY;
  334. typedef struct {
  335. SM9_TWIST_POINT Ppubs;
  336. SM9_POINT ds;
  337. } SM9_SIGN_KEY;
  338. int sm9_sign_master_key_generate(SM9_SIGN_MASTER_KEY *master);
  339. int sm9_sign_master_key_extract_key(SM9_SIGN_MASTER_KEY *master, const char *id, size_t idlen, SM9_SIGN_KEY *key);
  340. // algorthm,parameters = sm9,sm9sign
  341. #define SM9_SIGN_MASTER_KEY_MAX_SIZE 171
  342. int sm9_sign_master_key_to_der(const SM9_SIGN_MASTER_KEY *msk, uint8_t **out, size_t *outlen);
  343. int sm9_sign_master_key_from_der(SM9_SIGN_MASTER_KEY *msk, const uint8_t **in, size_t *inlen);
  344. int sm9_sign_master_key_info_encrypt_to_der(const SM9_SIGN_MASTER_KEY *msk, const char *pass, uint8_t **out, size_t *outlen);
  345. int sm9_sign_master_key_info_decrypt_from_der(SM9_SIGN_MASTER_KEY *msk, const char *pass, const uint8_t **in, size_t *inlen);
  346. int sm9_sign_master_key_info_encrypt_to_pem(const SM9_SIGN_MASTER_KEY *msk, const char *pass, FILE *fp);
  347. int sm9_sign_master_key_info_decrypt_from_pem(SM9_SIGN_MASTER_KEY *msk, const char *pass, FILE *fp);
  348. int sm9_sign_master_key_print(FILE *fp, int fmt, int ind, const char *label, const SM9_SIGN_MASTER_KEY *msk);
  349. #define SM9_SIGN_MASTER_PUBLIC_KEY_SIZE 136
  350. int sm9_sign_master_public_key_to_der(const SM9_SIGN_MASTER_KEY *mpk, uint8_t **out, size_t *outlen);
  351. int sm9_sign_master_public_key_from_der(SM9_SIGN_MASTER_KEY *mpk, const uint8_t **in, size_t *inlen);
  352. int sm9_sign_master_public_key_to_pem(const SM9_SIGN_MASTER_KEY *mpk, FILE *fp);
  353. int sm9_sign_master_public_key_from_pem(SM9_SIGN_MASTER_KEY *mpk, FILE *fp);
  354. int sm9_sign_master_public_key_print(FILE *fp, int fmt, int ind, const char *label, const SM9_SIGN_MASTER_KEY *mpk);
  355. // algorithm,parameters = sm9sign,<null>
  356. #define SM9_SIGN_KEY_SIZE 204
  357. int sm9_sign_key_to_der(const SM9_SIGN_KEY *key, uint8_t **out, size_t *outlen);
  358. int sm9_sign_key_from_der(SM9_SIGN_KEY *key, const uint8_t **in, size_t *inlen);
  359. int sm9_sign_key_info_encrypt_to_der(const SM9_SIGN_KEY *key, const char *pass, uint8_t **out, size_t *outlen);
  360. int sm9_sign_key_info_decrypt_from_der(SM9_SIGN_KEY *key, const char *pass, const uint8_t **in, size_t *inlen);
  361. int sm9_sign_key_info_encrypt_to_pem(const SM9_SIGN_KEY *key, const char *pass, FILE *fp);
  362. int sm9_sign_key_info_decrypt_from_pem(SM9_SIGN_KEY *key, const char *pass, FILE *fp);
  363. int sm9_sign_key_print(FILE *fp, int fmt, int ind, const char *label, const SM9_SIGN_KEY *key);
  364. /*
  365. from GM/T 0080-2020 SM9 Cryptographic Alagorithm Application Specification
  366. SM9Signature ::= SEQUENCE {
  367. h OCTET STRING,
  368. S BIT STRING -- uncompressed octets of ECPoint }
  369. */
  370. typedef struct {
  371. sm9_fn_t h;
  372. SM9_POINT S;
  373. } SM9_SIGNATURE;
  374. int sm9_do_sign(const SM9_SIGN_KEY *key, const SM3_CTX *sm3_ctx, SM9_SIGNATURE *sig);
  375. int sm9_do_verify(const SM9_SIGN_MASTER_KEY *mpk, const char *id, size_t idlen, const SM3_CTX *sm3_ctx, const SM9_SIGNATURE *sig);
  376. #define SM9_SIGNATURE_SIZE 104
  377. int sm9_signature_to_der(const SM9_SIGNATURE *sig, uint8_t **out, size_t *outlen);
  378. int sm9_signature_from_der(SM9_SIGNATURE *sig, const uint8_t **in, size_t *inlen);
  379. int sm9_signature_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *sig, size_t siglen);
  380. typedef struct {
  381. SM3_CTX sm3_ctx;
  382. } SM9_SIGN_CTX;
  383. int sm9_sign_init(SM9_SIGN_CTX *ctx);
  384. int sm9_sign_update(SM9_SIGN_CTX *ctx, const uint8_t *data, size_t datalen);
  385. int sm9_sign_finish(SM9_SIGN_CTX *ctx, const SM9_SIGN_KEY *key, uint8_t *sig, size_t *siglen);
  386. int sm9_verify_init(SM9_SIGN_CTX *ctx);
  387. int sm9_verify_update(SM9_SIGN_CTX *ctx, const uint8_t *data, size_t datalen);
  388. int sm9_verify_finish(SM9_SIGN_CTX *ctx, const uint8_t *sig, size_t siglen,
  389. const SM9_SIGN_MASTER_KEY *mpk, const char *id, size_t idlen);
  390. /*
  391. SM9EncMasterKey ::= SEQUENCE {
  392. de INTEGER,
  393. Ppube BIT STRING -- uncompressed octets of ECPoint }
  394. SM9EncMasterPublicKey ::= SEQUENCE {
  395. Ppube BIT STRING -- uncompressed octets of ECPoint }
  396. SM9EncPrivateKey ::= SEQUENCE {
  397. de BIT STRING, -- uncompressed octets of twisted point
  398. Ppube BIT STRING -- uncompressed octets of ECPoint }
  399. */
  400. typedef struct {
  401. SM9_POINT Ppube; // Ppube = ke * P1
  402. sm9_fn_t ke;
  403. } SM9_ENC_MASTER_KEY;
  404. typedef struct {
  405. SM9_POINT Ppube;
  406. SM9_TWIST_POINT de;
  407. } SM9_ENC_KEY;
  408. int sm9_enc_master_key_generate(SM9_ENC_MASTER_KEY *master);
  409. int sm9_enc_master_key_extract_key(SM9_ENC_MASTER_KEY *master, const char *id, size_t idlen, SM9_ENC_KEY *key);
  410. // algorithm,parameters = sm9,sm9encrypt
  411. #define SM9_ENC_MASTER_KEY_MAX_SIZE 105
  412. int sm9_enc_master_key_to_der(const SM9_ENC_MASTER_KEY *msk, uint8_t **out, size_t *outlen);
  413. int sm9_enc_master_key_from_der(SM9_ENC_MASTER_KEY *msk, const uint8_t **in, size_t *inlen);
  414. int sm9_enc_master_key_info_encrypt_to_der(const SM9_ENC_MASTER_KEY *msk, const char *pass, uint8_t **out, size_t *outlen);
  415. int sm9_enc_master_key_info_decrypt_from_der(SM9_ENC_MASTER_KEY *msk, const char *pass, const uint8_t **in, size_t *inlen);
  416. int sm9_enc_master_key_info_encrypt_to_pem(const SM9_ENC_MASTER_KEY *msk, const char *pass, FILE *fp);
  417. int sm9_enc_master_key_info_decrypt_from_pem(SM9_ENC_MASTER_KEY *msk, const char *pass, FILE *fp);
  418. int sm9_enc_master_key_print(FILE *fp, int fmt, int ind, const char *label, const SM9_ENC_MASTER_KEY *msk);
  419. #define SM9_ENC_MASTER_PUBLIC_KEY_SIZE 70
  420. int sm9_enc_master_public_key_to_der(const SM9_ENC_MASTER_KEY *mpk, uint8_t **out, size_t *outlen);
  421. int sm9_enc_master_public_key_from_der(SM9_ENC_MASTER_KEY *mpk, const uint8_t **in, size_t *inlen);
  422. int sm9_enc_master_public_key_to_pem(const SM9_ENC_MASTER_KEY *mpk, FILE *fp);
  423. int sm9_enc_master_public_key_from_pem(SM9_ENC_MASTER_KEY *mpk, FILE *fp);
  424. int sm9_enc_master_public_key_print(FILE *fp, int fmt, int ind, const char *label, const SM9_ENC_MASTER_KEY *mpk);
  425. // algorithm,parameters = sm9encrypt,<null>
  426. #define SM9_ENC_KEY_SIZE 204
  427. int sm9_enc_key_to_der(const SM9_ENC_KEY *key, uint8_t **out, size_t *outlen);
  428. int sm9_enc_key_from_der(SM9_ENC_KEY *key, const uint8_t **in, size_t *inlen);
  429. int sm9_enc_key_info_encrypt_to_der(const SM9_ENC_KEY *key, const char *pass, uint8_t **out, size_t *outlen);
  430. int sm9_enc_key_info_decrypt_from_der(SM9_ENC_KEY *key, const char *pass, const uint8_t **in, size_t *inlen);
  431. int sm9_enc_key_info_encrypt_to_pem(const SM9_ENC_KEY *key, const char *pass, FILE *fp);
  432. int sm9_enc_key_info_decrypt_from_pem(SM9_ENC_KEY *key, const char *pass, FILE *fp);
  433. int sm9_enc_key_print(FILE *fp, int fmt, int ind, const char *label, const SM9_ENC_KEY *key);
  434. #define SM9_MAX_PRIVATE_KEY_SIZE (SM9_SIGN_KEY_SIZE) // MAX(SIGN_MASTER_KEY, SIGN_KEY, ENC_MASTER_KEY, ENC_KEY)
  435. #define SM9_MAX_PRIVATE_KEY_INFO_SIZE 512
  436. #define SM9_MAX_ENCED_PRIVATE_KEY_INFO_SIZE 1024
  437. /*
  438. from GM/T 0080-2020 SM9 Cryptographic Alagorithm Application Specification
  439. SM9Cipher ::= SEQUENCE {
  440. EnType INTEGER, -- 0 for XOR
  441. C1 BIT STRING, -- uncompressed octets of ECPoint
  442. C3 OCTET STRING, -- 32 bytes HMAC-SM3 tag
  443. CipherText OCTET STRING }
  444. */
  445. int sm9_kem_encrypt(const SM9_ENC_MASTER_KEY *mpk, const char *id, size_t idlen, size_t klen, uint8_t *kbuf, SM9_POINT *C);
  446. int sm9_kem_decrypt(const SM9_ENC_KEY *key, const char *id, size_t idlen, const SM9_POINT *C, size_t klen, uint8_t *kbuf);
  447. int sm9_do_encrypt(const SM9_ENC_MASTER_KEY *mpk, const char *id, size_t idlen,
  448. const uint8_t *in, size_t inlen, SM9_POINT *C1, uint8_t *c2, uint8_t c3[SM3_HMAC_SIZE]);
  449. int sm9_do_decrypt(const SM9_ENC_KEY *key, const char *id, size_t idlen,
  450. const SM9_POINT *C1, const uint8_t *c2, size_t c2len, const uint8_t c3[SM3_HMAC_SIZE], uint8_t *out);
  451. #define SM9_MAX_PLAINTEXT_SIZE 255
  452. #define SM9_MAX_CIPHERTEXT_SIZE 367 // calculated in test_sm9_ciphertext()
  453. int sm9_ciphertext_to_der(const SM9_POINT *C1, const uint8_t *c2, size_t c2len,
  454. const uint8_t c3[SM3_HMAC_SIZE], uint8_t **out, size_t *outlen);
  455. int sm9_ciphertext_from_der(SM9_POINT *C1, const uint8_t **c2, size_t *c2len,
  456. const uint8_t **c3, const uint8_t **in, size_t *inlen);
  457. int sm9_ciphertext_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *a, size_t alen);
  458. int sm9_encrypt(const SM9_ENC_MASTER_KEY *mpk, const char *id, size_t idlen,
  459. const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
  460. int sm9_decrypt(const SM9_ENC_KEY *key, const char *id, size_t idlen,
  461. const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
  462. #ifdef __cplusplus
  463. }
  464. #endif
  465. #endif