Functions.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #include "Functions.hpp"
  2. #include <iostream>
  3. #include <string>
  4. #include <stdlib.h>
  5. #include <gmssl/sm2.h>
  6. #include <gmssl/sm3.h>
  7. #include <gmssl/sm4.h>
  8. #include <gmssl/error.h>
  9. #include <gmssl/rand.h>
  10. #include "CommonFunction.hpp"
  11. /**
  12. * @brief 将给定的 uint8_t 类型数据转换为十六进制字符串形式
  13. * @param data 待转换的数据数组
  14. * @param length 数据数组的长度
  15. * @param hex_string 转换后的十六进制字符串应存储的数组
  16. */
  17. void Functions::uint8_to_hex_string(const uint8_t* data, size_t length, char* hex_string) {
  18. for (size_t i = 0; i < length; ++i) {
  19. snprintf(hex_string + 2 * i, 3, "%02x", data[i]);
  20. }
  21. }
  22. void Functions::uint8_to_hex_hi_string(const uint8_t* data, size_t length, char* hex_string)
  23. {
  24. for (size_t i = 0; i < length; ++i) {
  25. snprintf(hex_string + 2 * i, 3, "%02X", data[i]);
  26. }
  27. }
  28. void Functions::hex_to_bytes(const char* hex_string, size_t length, uint8_t* data, size_t* data_len) {
  29. *data_len = length / 2;
  30. for (size_t i = 0; i < *data_len; ++i) {
  31. char hex[3];
  32. hex[0] = hex_string[i * 2];
  33. hex[1] = hex_string[i * 2 + 1];
  34. hex[2] = '\0';
  35. int byteValue = 0;
  36. #if MG_OS__WIN_AVAIL
  37. sscanf_s(hex, "%x", &byteValue);
  38. #else
  39. sscanf(hex, "%x", &byteValue);
  40. #endif
  41. data[i] = (uint8_t)byteValue;
  42. }
  43. }
  44. /**
  45. * @brief 用于对给定字符串进行sm3签名的函数
  46. *
  47. * @param buf 待签名的字符串
  48. * @param sign 签名算法
  49. */
  50. void Functions::sm3_sign(const uint8_t* buf, int len, char* sign) {
  51. SM3_CTX sm3_ctx;
  52. uint8_t dgst[32];
  53. sm3_init(&sm3_ctx);
  54. sm3_update(&sm3_ctx, buf, len);
  55. sm3_finish(&sm3_ctx, dgst);
  56. uint8_to_hex_hi_string(dgst, 32, sign);
  57. }
  58. void Functions::util_sm2_encrypt(const char* plain_text, const char* public_key_str, char* encrypted_text) {
  59. size_t pubkey_len;
  60. SM2_POINT sm2_point ;
  61. SM2_KEY pub_key;
  62. uint8_t pub_bytes[128];
  63. size_t len;
  64. uint8_t encrypt_bytes[255];
  65. SM2_CIPHERTEXT sm2_ciphertext;
  66. // char result[SM2_MAX_CIPHERTEXT_SIZE * 2 + 1];
  67. // unsigned char ciphertext[SM2_MAX_CIPHERTEXT_SIZE];
  68. //
  69. hex_to_bytes(public_key_str, strlen(public_key_str), pub_bytes, &pubkey_len);
  70. sm2_point_from_octets(&sm2_point, pub_bytes, pubkey_len);
  71. memcpy(&pub_key, &sm2_point, sizeof(SM2_POINT));
  72. // sm2_encrypt(&pub_key, (uint8_t *)plain_text, strlen(plain_text), (uint8_t*)encrypted_text, &len);
  73. sm2_do_encrypt(&pub_key, (uint8_t*)plain_text, strlen(plain_text), &sm2_ciphertext);
  74. size_t out_len = 32 * 3 + int(sm2_ciphertext.ciphertext_size) + 1;
  75. uint8_t c1c3c2[5120];
  76. c1c3c2[0] = 0x04; // for bc_lib
  77. memcpy(c1c3c2 + 1, sm2_ciphertext.point.x, 32);
  78. memcpy(c1c3c2 + 1 + 32, sm2_ciphertext.point.y, 32);
  79. memcpy(c1c3c2 + 1 + 32 + 32, sm2_ciphertext.hash, 32);
  80. memcpy(c1c3c2 + 1 + 32 + 32 + 32, sm2_ciphertext.ciphertext, sm2_ciphertext.ciphertext_size);
  81. uint8_to_hex_string(c1c3c2, out_len, encrypted_text);
  82. // base64_encode(c1c3c2, out_len, encrypted_text);
  83. // util_sm2_decrypt(encrypted_text, "12a3d7d7c8c6d2b5946f61cede593db9287cc10a51280433d15ede2cffd78ee6", x);
  84. }
  85. void Functions::util_sm2_decrypt(const char* plain_text, const char* private_key, char* decrypt_text) {
  86. size_t prikey_len;
  87. SM2_POINT sm2_point;
  88. SM2_KEY pri_key;
  89. uint8_t pri_bytes[128];
  90. size_t len;
  91. uint8_t encrypt_bytes[255];
  92. uint8_t buf[1024];
  93. size_t buf_len;
  94. // char result[SM2_MAX_CIPHERTEXT_SIZE * 2 + 1];
  95. // unsigned char ciphertext[SM2_MAX_CIPHERTEXT_SIZE];
  96. //
  97. hex_to_bytes(plain_text, strlen(plain_text), buf, &prikey_len);
  98. hex_to_bytes(private_key, strlen(private_key), pri_bytes, &buf_len);
  99. sm2_point_from_octets(&sm2_point, pri_bytes, prikey_len);
  100. memcpy(&pri_key, &sm2_point, sizeof(SM2_POINT));
  101. sm2_decrypt(&pri_key, buf, buf_len, (uint8_t*)encrypt_bytes, &len);
  102. uint8_to_hex_string(encrypt_bytes, len, decrypt_text);
  103. }
  104. void Functions::util_sm4_encrypt(const char* plain_text, const char* key_screct, char* encrypted_text) {
  105. SM4_KEY key;
  106. uint8_t ebytes[16] = {0};
  107. uint8_t bs[16];
  108. size_t bs_len = 0;
  109. hex_to_bytes(key_screct, strlen(key_screct) - 1, bs, &bs_len);
  110. sm4_set_encrypt_key(&key, bs);
  111. /* test encrypt once */
  112. auto i = 0;
  113. auto len = strlen(plain_text);
  114. auto blockCount = len / 16;
  115. if (len % 16 != 0) {
  116. blockCount++;
  117. }
  118. //uint8_t *buf = malloc(blockCount*16)
  119. uint8_t buf[4096] = {0};
  120. for (i = 0; i < len; i += 16) {
  121. sm4_encrypt(&key, (uint8_t*)(plain_text + i), (uint8_t*)(buf + i));
  122. }
  123. // 解密试一下
  124. // 转为base64字符串
  125. base64_encode((uint8_t*)buf, blockCount*16, encrypted_text);
  126. char deBuf[4096] = {'\0'};
  127. util_sm4_decrypt(encrypted_text, key_screct, deBuf);
  128. // 解密试一下
  129. printf("hello");
  130. /*
  131. uint8_t iv[16] = {};
  132. SM4_KEY sm4_key;
  133. sm4_set_encrypt_key(&sm4_key, key_screct);
  134. sm4_cbc_encrypt(&sm4_key, iv, (uint8_t*)plain_text, strlen(plain_text) / SM4_BLOCK_SIZE, (uint8_t*)encrypted_text);
  135. int i = 0;
  136. for (i = 0; i < strlen(encrypted_text); i++) {
  137. printf("%02X", encrypted_text[i]);
  138. }
  139. */
  140. }
  141. void Functions::util_sm4_decrypt(const char* plain_text, const char* key_screct, char* decrypted_text) {
  142. SM4_KEY key;
  143. uint8_t ebytes[16] = { 0 };
  144. uint8_t bs[16];
  145. size_t bs_len = 0;
  146. hex_to_bytes(key_screct, strlen(key_screct) - 1, bs, &bs_len);
  147. sm4_set_encrypt_key(&key, bs);
  148. // base64解码
  149. size_t outLen;
  150. uint8_t deBuf[4096] = { '\0' };
  151. base64_decode(plain_text, strlen(plain_text), &outLen, deBuf);
  152. /* test encrypt once */
  153. auto i = 0;
  154. auto len = outLen;
  155. auto blockCount = len / 16;
  156. if (len % 16 != 0) {
  157. blockCount++;
  158. }
  159. //uint8_t *buf = malloc(blockCount*16)
  160. for (i = 0; i < len; i += 16) {
  161. sm4_decrypt(&key, (uint8_t*)(deBuf + i), (uint8_t*)(decrypted_text + i));
  162. }
  163. }
  164. void Functions::util_rand_bytes(uint8_t* buf, size_t buflen)
  165. {
  166. rand_bytes(buf, buflen);
  167. }
  168. static const char base64_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  169. void Functions::base64_encode(const unsigned char* data, size_t input_length, char* encoded_data){
  170. size_t output_length = 4 * ((input_length + 2) / 3);
  171. if (encoded_data == NULL) return ;
  172. for (size_t i = 0, j = 0; i < input_length;) {
  173. uint32_t octet_a = i < input_length ? data[i++] : 0;
  174. uint32_t octet_b = i < input_length ? data[i++] : 0;
  175. uint32_t octet_c = i < input_length ? data[i++] : 0;
  176. uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
  177. encoded_data[j++] = base64_chars[(triple >> 3 * 6) & 0x3F];
  178. encoded_data[j++] = base64_chars[(triple >> 2 * 6) & 0x3F];
  179. encoded_data[j++] = base64_chars[(triple >> 1 * 6) & 0x3F];
  180. encoded_data[j++] = base64_chars[(triple >> 0 * 6) & 0x3F];
  181. }
  182. // 添加Base64填充字符
  183. for (size_t i = 0; i < (3 - input_length % 3) % 3; i++) {
  184. encoded_data[output_length - 1 - i] = '=';
  185. }
  186. encoded_data[output_length] = '\0';
  187. }
  188. // Base64解码
  189. void Functions::base64_decode(const char* data, size_t input_length, size_t* output_length, uint8_t* decoded_data) {
  190. if (input_length % 4 != 0) return ;
  191. *output_length = input_length / 4 * 3;
  192. if (data[input_length - 1] == '=') (*output_length)--;
  193. if (data[input_length - 2] == '=') (*output_length)--;
  194. if (decoded_data == NULL) return ;
  195. for (size_t i = 0, j = 0; i < input_length;) {
  196. uint32_t sextet_a = data[i] == '=' ? 0 & i++ : base64_chars[(unsigned char)data[i++]];
  197. uint32_t sextet_b = data[i] == '=' ? 0 & i++ : base64_chars[(unsigned char)data[i++]];
  198. uint32_t sextet_c = data[i] == '=' ? 0 & i++ : base64_chars[(unsigned char)data[i++]];
  199. uint32_t sextet_d = data[i] == '=' ? 0 & i++ : base64_chars[(unsigned char)data[i++]];
  200. uint32_t triple = (sextet_a << 3 * 6) + (sextet_b << 2 * 6) + (sextet_c << 1 * 6) + sextet_d;
  201. if (j < *output_length) decoded_data[j++] = (triple >> 2 * 8) & 0xFF;
  202. if (j < *output_length) decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
  203. if (j < *output_length) decoded_data[j++] = triple & 0xFF;
  204. }
  205. }