sm2.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * Copyright 2014-2023 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. #ifndef GMSSL_SM2_H
  10. #define GMSSL_SM2_H
  11. #include <stdio.h>
  12. #include <stdint.h>
  13. #include <stdlib.h>
  14. #include <gmssl/sm3.h>
  15. #include <gmssl/api.h>
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. /*
  20. SM2 Public API
  21. SM2_DEFAULT_ID
  22. SM2_MAX_ID_LENGTH
  23. SM2_MAX_SIGNATURE_SIZE
  24. SM2_MAX_PLAINTEXT_SIZE
  25. SM2_MAX_CIPHERTEXT_SIZE
  26. SM2_KEY
  27. sm2_key_generate
  28. sm2_private_key_info_encrypt_to_der
  29. sm2_private_key_info_decrypt_from_der
  30. sm2_private_key_info_encrypt_to_pem
  31. sm2_private_key_info_decrypt_from_pem
  32. sm2_public_key_info_to_der
  33. sm2_public_key_info_from_der
  34. sm2_public_key_info_to_pem
  35. sm2_public_key_info_from_pem
  36. sm2_sign
  37. sm2_verify
  38. sm2_encrypt
  39. sm2_decrypt
  40. sm2_ecdh
  41. SM2_SIGN_CTX
  42. sm2_sign_init
  43. sm2_sign_update
  44. sm2_sign_finish
  45. sm2_verify_init
  46. sm2_verify_update
  47. sm2_verify_finish
  48. */
  49. typedef uint64_t SM2_BN[8];
  50. int sm2_bn_is_zero(const SM2_BN a);
  51. int sm2_bn_is_one(const SM2_BN a);
  52. int sm2_bn_is_odd(const SM2_BN a);
  53. int sm2_bn_cmp(const SM2_BN a, const SM2_BN b);
  54. int sm2_bn_from_hex(SM2_BN r, const char hex[64]);
  55. int sm2_bn_from_asn1_integer(SM2_BN r, const uint8_t *d, size_t dlen);
  56. int sm2_bn_equ_hex(const SM2_BN a, const char *hex);
  57. int sm2_bn_print(FILE *fp, int fmt, int ind, const char *label, const SM2_BN a);
  58. int sm2_bn_rshift(SM2_BN ret, const SM2_BN a, unsigned int nbits);
  59. void sm2_bn_to_bytes(const SM2_BN a, uint8_t out[32]);
  60. void sm2_bn_from_bytes(SM2_BN r, const uint8_t in[32]);
  61. void sm2_bn_to_hex(const SM2_BN a, char hex[64]);
  62. void sm2_bn_to_bits(const SM2_BN a, char bits[256]);
  63. void sm2_bn_set_word(SM2_BN r, uint32_t a);
  64. void sm2_bn_add(SM2_BN r, const SM2_BN a, const SM2_BN b);
  65. void sm2_bn_sub(SM2_BN ret, const SM2_BN a, const SM2_BN b);
  66. int sm2_bn_rand_range(SM2_BN r, const SM2_BN range);
  67. #define sm2_bn_init(r) memset((r),0,sizeof(SM2_BN))
  68. #define sm2_bn_set_zero(r) memset((r),0,sizeof(SM2_BN))
  69. #define sm2_bn_set_one(r) sm2_bn_set_word((r),1)
  70. #define sm2_bn_copy(r,a) memcpy((r),(a),sizeof(SM2_BN))
  71. #define sm2_bn_clean(r) memset((r),0,sizeof(SM2_BN))
  72. // GF(p)
  73. typedef SM2_BN SM2_Fp;
  74. void sm2_fp_add(SM2_Fp r, const SM2_Fp a, const SM2_Fp b);
  75. void sm2_fp_sub(SM2_Fp r, const SM2_Fp a, const SM2_Fp b);
  76. void sm2_fp_mul(SM2_Fp r, const SM2_Fp a, const SM2_Fp b);
  77. void sm2_fp_exp(SM2_Fp r, const SM2_Fp a, const SM2_Fp e);
  78. void sm2_fp_dbl(SM2_Fp r, const SM2_Fp a);
  79. void sm2_fp_tri(SM2_Fp r, const SM2_Fp a);
  80. void sm2_fp_div2(SM2_Fp r, const SM2_Fp a);
  81. void sm2_fp_neg(SM2_Fp r, const SM2_Fp a);
  82. void sm2_fp_sqr(SM2_Fp r, const SM2_Fp a);
  83. void sm2_fp_inv(SM2_Fp r, const SM2_Fp a);
  84. int sm2_fp_rand(SM2_Fp r);
  85. int sm2_fp_sqrt(SM2_Fp r, const SM2_Fp a);
  86. #define sm2_fp_init(r) sm2_bn_init(r)
  87. #define sm2_fp_set_zero(r) sm2_bn_set_zero(r)
  88. #define sm2_fp_set_one(r) sm2_bn_set_one(r)
  89. #define sm2_fp_copy(r,a) sm2_bn_copy(r,a)
  90. #define sm2_fp_clean(r) sm2_bn_clean(r)
  91. // GF(n)
  92. typedef SM2_BN SM2_Fn;
  93. void sm2_fn_add(SM2_Fn r, const SM2_Fn a, const SM2_Fn b);
  94. void sm2_fn_sub(SM2_Fn r, const SM2_Fn a, const SM2_Fn b);
  95. void sm2_fn_mul(SM2_Fn r, const SM2_Fn a, const SM2_Fn b);
  96. void sm2_fn_mul_word(SM2_Fn r, const SM2_Fn a, uint32_t b);
  97. void sm2_fn_exp(SM2_Fn r, const SM2_Fn a, const SM2_Fn e);
  98. void sm2_fn_neg(SM2_Fn r, const SM2_Fn a);
  99. void sm2_fn_sqr(SM2_Fn r, const SM2_Fn a);
  100. void sm2_fn_inv(SM2_Fn r, const SM2_Fn a);
  101. int sm2_fn_rand(SM2_Fn r);
  102. #define sm2_fn_init(r) sm2_bn_init(r)
  103. #define sm2_fn_set_zero(r) sm2_bn_set_zero(r)
  104. #define sm2_fn_set_one(r) sm2_bn_set_one(r)
  105. #define sm2_fn_copy(r,a) sm2_bn_copy(r,a)
  106. #define sm2_fn_clean(r) sm2_bn_clean(r)
  107. typedef struct {
  108. SM2_BN X;
  109. SM2_BN Y;
  110. SM2_BN Z;
  111. } SM2_JACOBIAN_POINT;
  112. void sm2_jacobian_point_init(SM2_JACOBIAN_POINT *R);
  113. void sm2_jacobian_point_set_xy(SM2_JACOBIAN_POINT *R, const SM2_BN x, const SM2_BN y);
  114. void sm2_jacobian_point_get_xy(const SM2_JACOBIAN_POINT *P, SM2_BN x, SM2_BN y);
  115. void sm2_jacobian_point_neg(SM2_JACOBIAN_POINT *R, const SM2_JACOBIAN_POINT *P);
  116. void sm2_jacobian_point_dbl(SM2_JACOBIAN_POINT *R, const SM2_JACOBIAN_POINT *P);
  117. void sm2_jacobian_point_add(SM2_JACOBIAN_POINT *R, const SM2_JACOBIAN_POINT *P, const SM2_JACOBIAN_POINT *Q);
  118. void sm2_jacobian_point_sub(SM2_JACOBIAN_POINT *R, const SM2_JACOBIAN_POINT *P, const SM2_JACOBIAN_POINT *Q);
  119. void sm2_jacobian_point_mul(SM2_JACOBIAN_POINT *R, const SM2_BN k, const SM2_JACOBIAN_POINT *P);
  120. void sm2_jacobian_point_to_bytes(const SM2_JACOBIAN_POINT *P, uint8_t out[64]);
  121. void sm2_jacobian_point_from_bytes(SM2_JACOBIAN_POINT *P, const uint8_t in[64]);
  122. void sm2_jacobian_point_mul_generator(SM2_JACOBIAN_POINT *R, const SM2_BN k);
  123. void sm2_jacobian_point_mul_sum(SM2_JACOBIAN_POINT *R, const SM2_BN t, const SM2_JACOBIAN_POINT *P, const SM2_BN s);
  124. void sm2_jacobian_point_from_hex(SM2_JACOBIAN_POINT *P, const char hex[64 * 2]); // for testing only
  125. int sm2_jacobian_point_is_at_infinity(const SM2_JACOBIAN_POINT *P);
  126. int sm2_jacobian_point_is_on_curve(const SM2_JACOBIAN_POINT *P);
  127. int sm2_jacobian_point_equ_hex(const SM2_JACOBIAN_POINT *P, const char hex[128]); // for testing only
  128. int sm2_jacobian_point_print(FILE *fp, int fmt, int ind, const char *label, const SM2_JACOBIAN_POINT *P);
  129. #define sm2_jacobian_point_set_infinity(R) sm2_jacobian_point_init(R)
  130. #define sm2_jacobian_point_copy(R, P) memcpy((R), (P), sizeof(SM2_JACOBIAN_POINT))
  131. typedef uint8_t sm2_bn_t[32];
  132. typedef struct {
  133. uint8_t x[32];
  134. uint8_t y[32];
  135. } SM2_POINT;
  136. #define sm2_point_init(P) memset((P),0,sizeof(SM2_POINT))
  137. #define sm2_point_set_infinity(P) sm2_point_init(P)
  138. int sm2_point_from_octets(SM2_POINT *P, const uint8_t *in, size_t inlen);
  139. void sm2_point_to_compressed_octets(const SM2_POINT *P, uint8_t out[33]);
  140. void sm2_point_to_uncompressed_octets(const SM2_POINT *P, uint8_t out[65]);
  141. int sm2_point_from_x(SM2_POINT *P, const uint8_t x[32], int y);
  142. int sm2_point_from_xy(SM2_POINT *P, const uint8_t x[32], const uint8_t y[32]);
  143. int sm2_point_is_on_curve(const SM2_POINT *P);
  144. int sm2_point_is_at_infinity(const SM2_POINT *P);
  145. int sm2_point_add(SM2_POINT *R, const SM2_POINT *P, const SM2_POINT *Q);
  146. int sm2_point_sub(SM2_POINT *R, const SM2_POINT *P, const SM2_POINT *Q);
  147. int sm2_point_neg(SM2_POINT *R, const SM2_POINT *P);
  148. int sm2_point_dbl(SM2_POINT *R, const SM2_POINT *P);
  149. int sm2_point_mul(SM2_POINT *R, const uint8_t k[32], const SM2_POINT *P);
  150. int sm2_point_mul_generator(SM2_POINT *R, const uint8_t k[32]);
  151. int sm2_point_mul_sum(SM2_POINT *R, const uint8_t k[32], const SM2_POINT *P, const uint8_t s[32]); // R = k * P + s * G
  152. /*
  153. RFC 5480 Elliptic Curve Cryptography Subject Public Key Information
  154. ECPoint ::= OCTET STRING
  155. */
  156. #define SM2_POINT_MAX_SIZE (2 + 65)
  157. int sm2_point_to_der(const SM2_POINT *P, uint8_t **out, size_t *outlen);
  158. int sm2_point_from_der(SM2_POINT *P, const uint8_t **in, size_t *inlen);
  159. int sm2_point_print(FILE *fp, int fmt, int ind, const char *label, const SM2_POINT *P);
  160. int sm2_point_from_hash(SM2_POINT *R, const uint8_t *data, size_t datalen);
  161. typedef struct {
  162. SM2_POINT public_key;
  163. uint8_t private_key[32];
  164. } SM2_KEY;
  165. _gmssl_export int sm2_key_generate(SM2_KEY *key);
  166. int sm2_key_set_private_key(SM2_KEY *key, const uint8_t private_key[32]); // key->public_key will be replaced
  167. int sm2_key_set_public_key(SM2_KEY *key, const SM2_POINT *public_key); // key->private_key will be cleared // FIXME: support octets as input?
  168. int sm2_key_print(FILE *fp, int fmt, int ind, const char *label, const SM2_KEY *key);
  169. int sm2_public_key_equ(const SM2_KEY *sm2_key, const SM2_KEY *pub_key);
  170. //int sm2_public_key_copy(SM2_KEY *sm2_key, const SM2_KEY *pub_key); // do we need this?
  171. int sm2_public_key_digest(const SM2_KEY *key, uint8_t dgst[32]);
  172. int sm2_public_key_print(FILE *fp, int fmt, int ind, const char *label, const SM2_KEY *pub_key);
  173. /*
  174. from RFC 5915
  175. ECPrivateKey ::= SEQUENCE {
  176. version INTEGER, -- value MUST be (1)
  177. privateKey OCTET STRING, -- big endian encoding of integer 这里不是以INTEGER编码的,因此长度固定
  178. parameters [0] EXPLICIT ECParameters OPTIONAL,
  179. -- ONLY namedCurve OID is permitted, by RFC 5480
  180. -- MUST always include this field, by RFC 5915
  181. publicKey [1] EXPLICIT BIT STRING OPTIONAL -- compressed_point
  182. -- SHOULD always include this field, by RFC 5915 }
  183. ECParameters ::= CHOICE { namedCurve OBJECT IDENTIFIER }
  184. */
  185. #define SM2_PRIVATE_KEY_DEFAULT_SIZE 120 // generated
  186. #define SM2_PRIVATE_KEY_BUF_SIZE 512 // MUST >= SM2_PRIVATE_KEY_DEFAULT_SIZE
  187. int sm2_private_key_to_der(const SM2_KEY *key, uint8_t **out, size_t *outlen);
  188. int sm2_private_key_from_der(SM2_KEY *key, const uint8_t **in, size_t *inlen);
  189. int sm2_private_key_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *d, size_t dlen);
  190. int sm2_private_key_to_pem(const SM2_KEY *key, FILE *fp);
  191. int sm2_private_key_from_pem(SM2_KEY *key, FILE *fp);
  192. /*
  193. AlgorithmIdentifier ::= {
  194. algorithm OBJECT IDENTIFIER { id-ecPublicKey },
  195. parameters OBJECT IDENTIFIER { id-sm2 } }
  196. */
  197. int sm2_public_key_algor_to_der(uint8_t **out, size_t *outlen);
  198. int sm2_public_key_algor_from_der(const uint8_t **in, size_t *inlen);
  199. /*
  200. SubjectPublicKeyInfo from RFC 5280
  201. SubjectPublicKeyInfo ::= SEQUENCE {
  202. algorithm AlgorithmIdentifier,
  203. subjectPublicKey BIT STRING -- uncompressed octets of ECPoint }
  204. */
  205. _gmssl_export int sm2_public_key_info_to_der(const SM2_KEY *a, uint8_t **out, size_t *outlen);
  206. _gmssl_export int sm2_public_key_info_from_der(SM2_KEY *a, const uint8_t **in, size_t *inlen);
  207. _gmssl_export int sm2_public_key_info_to_pem(const SM2_KEY *a, FILE *fp);
  208. _gmssl_export int sm2_public_key_info_from_pem(SM2_KEY *a, FILE *fp);
  209. /*
  210. PKCS #8 PrivateKeyInfo from RFC 5208
  211. PrivateKeyInfo ::= SEQUENCE {
  212. version Version { v1(0) },
  213. privateKeyAlgorithm AlgorithmIdentifier,
  214. privateKey OCTET STRING, -- DER-encoding of ECPrivateKey
  215. attributes [0] IMPLICIT SET OF Attribute OPTIONAL }
  216. */
  217. enum {
  218. PKCS8_private_key_info_version = 0,
  219. };
  220. int sm2_private_key_info_to_der(const SM2_KEY *key, uint8_t **out, size_t *outlen);
  221. int sm2_private_key_info_from_der(SM2_KEY *key, const uint8_t **attrs, size_t *attrslen, const uint8_t **in, size_t *inlen);
  222. int sm2_private_key_info_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *d, size_t dlen);
  223. int sm2_private_key_info_to_pem(const SM2_KEY *key, FILE *fp);
  224. // FIXME: #define default buffer size for sm2_private_key_info_from_pem
  225. int sm2_private_key_info_from_pem(SM2_KEY *key, FILE *fp);
  226. /*
  227. EncryptedPrivateKeyInfo ::= SEQUENCE {
  228. encryptionAlgorithm EncryptionAlgorithmIdentifier, -- id-PBES2
  229. encryptedData OCTET STRING }
  230. */
  231. _gmssl_export int sm2_private_key_info_encrypt_to_der(const SM2_KEY *key,
  232. const char *pass, uint8_t **out, size_t *outlen);
  233. _gmssl_export int sm2_private_key_info_decrypt_from_der(SM2_KEY *key, const uint8_t **attrs, size_t *attrs_len,
  234. const char *pass, const uint8_t **in, size_t *inlen);
  235. _gmssl_export int sm2_private_key_info_encrypt_to_pem(const SM2_KEY *key, const char *pass, FILE *fp);
  236. // FIXME: #define default buffer size
  237. _gmssl_export int sm2_private_key_info_decrypt_from_pem(SM2_KEY *key, const char *pass, FILE *fp);
  238. typedef struct {
  239. uint8_t r[32];
  240. uint8_t s[32];
  241. } SM2_SIGNATURE;
  242. int sm2_do_sign(const SM2_KEY *key, const uint8_t dgst[32], SM2_SIGNATURE *sig);
  243. int sm2_do_sign_fast(const SM2_Fn d, const uint8_t dgst[32], SM2_SIGNATURE *sig);
  244. int sm2_do_verify(const SM2_KEY *key, const uint8_t dgst[32], const SM2_SIGNATURE *sig);
  245. #define SM2_MIN_SIGNATURE_SIZE 8
  246. #define SM2_MAX_SIGNATURE_SIZE 72
  247. int sm2_signature_to_der(const SM2_SIGNATURE *sig, uint8_t **out, size_t *outlen);
  248. int sm2_signature_from_der(SM2_SIGNATURE *sig, const uint8_t **in, size_t *inlen);
  249. int sm2_signature_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *sig, size_t siglen);
  250. _gmssl_export int sm2_sign(const SM2_KEY *key, const uint8_t dgst[32], uint8_t *sig, size_t *siglen);
  251. _gmssl_export int sm2_verify(const SM2_KEY *key, const uint8_t dgst[32], const uint8_t *sig, size_t siglen);
  252. enum {
  253. SM2_signature_compact_size = 70,
  254. SM2_signature_typical_size = 71,
  255. SM2_signature_max_size = 72,
  256. };
  257. int sm2_sign_fixlen(const SM2_KEY *key, const uint8_t dgst[32], size_t siglen, uint8_t *sig);
  258. #define SM2_DEFAULT_ID "1234567812345678"
  259. #define SM2_DEFAULT_ID_LENGTH (sizeof(SM2_DEFAULT_ID) - 1) // LENGTH for string and SIZE for bytes
  260. #define SM2_DEFAULT_ID_BITS (SM2_DEFAULT_ID_LENGTH * 8)
  261. #define SM2_MAX_ID_BITS 65535
  262. #define SM2_MAX_ID_LENGTH (SM2_MAX_ID_BITS/8)
  263. int sm2_compute_z(uint8_t z[32], const SM2_POINT *pub, const char *id, size_t idlen);
  264. typedef struct {
  265. SM3_CTX sm3_ctx;
  266. SM2_KEY key;
  267. } SM2_SIGN_CTX;
  268. _gmssl_export int sm2_sign_init(SM2_SIGN_CTX *ctx, const SM2_KEY *key, const char *id, size_t idlen);
  269. _gmssl_export int sm2_sign_update(SM2_SIGN_CTX *ctx, const uint8_t *data, size_t datalen);
  270. _gmssl_export int sm2_sign_finish(SM2_SIGN_CTX *ctx, uint8_t *sig, size_t *siglen);
  271. int sm2_sign_finish_fixlen(SM2_SIGN_CTX *ctx, size_t siglen, uint8_t *sig);
  272. _gmssl_export int sm2_verify_init(SM2_SIGN_CTX *ctx, const SM2_KEY *key, const char *id, size_t idlen);
  273. _gmssl_export int sm2_verify_update(SM2_SIGN_CTX *ctx, const uint8_t *data, size_t datalen);
  274. _gmssl_export int sm2_verify_finish(SM2_SIGN_CTX *ctx, const uint8_t *sig, size_t siglen);
  275. /*
  276. SM2Cipher ::= SEQUENCE {
  277. XCoordinate INTEGER,
  278. YCoordinate INTEGER,
  279. HASH OCTET STRING SIZE(32),
  280. CipherText OCTET STRING }
  281. */
  282. #define SM2_MIN_PLAINTEXT_SIZE 1 // re-compute SM2_MIN_CIPHERTEXT_SIZE when modify
  283. #define SM2_MAX_PLAINTEXT_SIZE 255 // re-compute SM2_MAX_CIPHERTEXT_SIZE when modify
  284. typedef struct {
  285. SM2_POINT point;
  286. uint8_t hash[32];
  287. uint8_t ciphertext_size;
  288. uint8_t ciphertext[SM2_MAX_PLAINTEXT_SIZE];
  289. } SM2_CIPHERTEXT;
  290. int sm2_do_encrypt(const SM2_KEY *key, const uint8_t *in, size_t inlen, SM2_CIPHERTEXT *out);
  291. int sm2_do_decrypt(const SM2_KEY *key, const SM2_CIPHERTEXT *in, uint8_t *out, size_t *outlen);
  292. #define SM2_MIN_CIPHERTEXT_SIZE 45 // depends on SM2_MIN_PLAINTEXT_SIZE
  293. #define SM2_MAX_CIPHERTEXT_SIZE 366 // depends on SM2_MAX_PLAINTEXT_SIZE
  294. int sm2_ciphertext_to_der(const SM2_CIPHERTEXT *c, uint8_t **out, size_t *outlen);
  295. int sm2_ciphertext_from_der(SM2_CIPHERTEXT *c, const uint8_t **in, size_t *inlen);
  296. int sm2_ciphertext_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *a, size_t alen);
  297. _gmssl_export int sm2_encrypt(const SM2_KEY *key, const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
  298. _gmssl_export int sm2_decrypt(const SM2_KEY *key, const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
  299. enum {
  300. SM2_ciphertext_compact_point_size = 68,
  301. SM2_ciphertext_typical_point_size = 69,
  302. SM2_ciphertext_max_point_size = 70,
  303. };
  304. int sm2_do_encrypt_fixlen(const SM2_KEY *key, const uint8_t *in, size_t inlen, int point_size, SM2_CIPHERTEXT *out);
  305. int sm2_encrypt_fixlen(const SM2_KEY *key, const uint8_t *in, size_t inlen, int point_size, uint8_t *out, size_t *outlen);
  306. int sm2_do_ecdh(const SM2_KEY *key, const SM2_POINT *peer_public, SM2_POINT *out);
  307. _gmssl_export int sm2_ecdh(const SM2_KEY *key, const uint8_t *peer_public, size_t peer_public_len, SM2_POINT *out);
  308. #ifdef __cplusplus
  309. }
  310. #endif
  311. #endif