123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- #include "Functions.hpp"
- #include <iostream>
- #include <string>
- #include <stdlib.h>
- #include <gmssl/sm2.h>
- #include <gmssl/sm3.h>
- #include <gmssl/sm4.h>
- #include <gmssl/error.h>
- #include <gmssl/rand.h>
- #include "CommonFunction.hpp"
- /**
- * @brief 将给定的 uint8_t 类型数据转换为十六进制字符串形式
- * @param data 待转换的数据数组
- * @param length 数据数组的长度
- * @param hex_string 转换后的十六进制字符串应存储的数组
- */
- void Functions::uint8_to_hex_string(const uint8_t* data, size_t length, char* hex_string) {
- for (size_t i = 0; i < length; ++i) {
- snprintf(hex_string + 2 * i, 3, "%02x", data[i]);
- }
- }
- void Functions::uint8_to_hex_hi_string(const uint8_t* data, size_t length, char* hex_string)
- {
- for (size_t i = 0; i < length; ++i) {
- snprintf(hex_string + 2 * i, 3, "%02X", data[i]);
- }
- }
- void Functions::hex_to_bytes(const char* hex_string, size_t length, uint8_t* data, size_t* data_len) {
- *data_len = length / 2;
- for (size_t i = 0; i < *data_len; ++i) {
- char hex[3];
- hex[0] = hex_string[i * 2];
- hex[1] = hex_string[i * 2 + 1];
- hex[2] = '\0';
- int byteValue = 0;
- #if MG_OS__WIN_AVAIL
- sscanf_s(hex, "%x", &byteValue);
- #else
- sscanf(hex, "%x", &byteValue);
- #endif
- data[i] = (uint8_t)byteValue;
- }
- }
- /**
- * @brief 用于对给定字符串进行sm3签名的函数
- *
- * @param buf 待签名的字符串
- * @param sign 签名算法
- */
- void Functions::sm3_sign(const uint8_t* buf, int len, char* sign) {
- SM3_CTX sm3_ctx;
- uint8_t dgst[32];
- sm3_init(&sm3_ctx);
- sm3_update(&sm3_ctx, buf, len);
- sm3_finish(&sm3_ctx, dgst);
- uint8_to_hex_hi_string(dgst, 32, sign);
- }
- void Functions::util_sm2_encrypt(const char* plain_text, const char* public_key_str, char* encrypted_text) {
- size_t pubkey_len;
- SM2_POINT sm2_point ;
- SM2_KEY pub_key;
- uint8_t pub_bytes[128];
- size_t len;
- uint8_t encrypt_bytes[255];
- SM2_CIPHERTEXT sm2_ciphertext;
- // char result[SM2_MAX_CIPHERTEXT_SIZE * 2 + 1];
- // unsigned char ciphertext[SM2_MAX_CIPHERTEXT_SIZE];
- //
- hex_to_bytes(public_key_str, strlen(public_key_str), pub_bytes, &pubkey_len);
- sm2_point_from_octets(&sm2_point, pub_bytes, pubkey_len);
- memcpy(&pub_key, &sm2_point, sizeof(SM2_POINT));
- // sm2_encrypt(&pub_key, (uint8_t *)plain_text, strlen(plain_text), (uint8_t*)encrypted_text, &len);
- sm2_do_encrypt(&pub_key, (uint8_t*)plain_text, strlen(plain_text), &sm2_ciphertext);
- size_t out_len = 32 * 3 + int(sm2_ciphertext.ciphertext_size) + 1;
- uint8_t c1c3c2[5120];
- c1c3c2[0] = 0x04; // for bc_lib
- memcpy(c1c3c2 + 1, sm2_ciphertext.point.x, 32);
- memcpy(c1c3c2 + 1 + 32, sm2_ciphertext.point.y, 32);
- memcpy(c1c3c2 + 1 + 32 + 32, sm2_ciphertext.hash, 32);
- memcpy(c1c3c2 + 1 + 32 + 32 + 32, sm2_ciphertext.ciphertext, sm2_ciphertext.ciphertext_size);
- uint8_to_hex_string(c1c3c2, out_len, encrypted_text);
- // base64_encode(c1c3c2, out_len, encrypted_text);
- // util_sm2_decrypt(encrypted_text, "12a3d7d7c8c6d2b5946f61cede593db9287cc10a51280433d15ede2cffd78ee6", x);
- }
- void Functions::util_sm2_decrypt(const char* plain_text, const char* private_key, char* decrypt_text) {
- size_t prikey_len;
- SM2_POINT sm2_point;
- SM2_KEY pri_key;
- uint8_t pri_bytes[128];
- size_t len;
- uint8_t encrypt_bytes[255];
- uint8_t buf[1024];
- size_t buf_len;
- // char result[SM2_MAX_CIPHERTEXT_SIZE * 2 + 1];
- // unsigned char ciphertext[SM2_MAX_CIPHERTEXT_SIZE];
- //
- hex_to_bytes(plain_text, strlen(plain_text), buf, &prikey_len);
- hex_to_bytes(private_key, strlen(private_key), pri_bytes, &buf_len);
- sm2_point_from_octets(&sm2_point, pri_bytes, prikey_len);
- memcpy(&pri_key, &sm2_point, sizeof(SM2_POINT));
- sm2_decrypt(&pri_key, buf, buf_len, (uint8_t*)encrypt_bytes, &len);
- uint8_to_hex_string(encrypt_bytes, len, decrypt_text);
- }
- void Functions::util_sm4_encrypt(const char* plain_text, const char* key_screct, char* encrypted_text) {
- SM4_KEY key;
- uint8_t ebytes[16] = {0};
- uint8_t bs[16];
- size_t bs_len = 0;
- hex_to_bytes(key_screct, strlen(key_screct) - 1, bs, &bs_len);
- sm4_set_encrypt_key(&key, bs);
- /* test encrypt once */
- auto i = 0;
- auto len = strlen(plain_text);
- auto blockCount = len / 16;
- if (len % 16 != 0) {
- blockCount++;
- }
- //uint8_t *buf = malloc(blockCount*16)
- uint8_t buf[4096] = {0};
- for (i = 0; i < len; i += 16) {
- sm4_encrypt(&key, (uint8_t*)(plain_text + i), (uint8_t*)(buf + i));
- }
- // 解密试一下
- // 转为base64字符串
- base64_encode((uint8_t*)buf, blockCount*16, encrypted_text);
- char deBuf[4096] = {'\0'};
- util_sm4_decrypt(encrypted_text, key_screct, deBuf);
- // 解密试一下
- printf("hello");
-
-
-
- /*
- uint8_t iv[16] = {};
- SM4_KEY sm4_key;
- sm4_set_encrypt_key(&sm4_key, key_screct);
- sm4_cbc_encrypt(&sm4_key, iv, (uint8_t*)plain_text, strlen(plain_text) / SM4_BLOCK_SIZE, (uint8_t*)encrypted_text);
- int i = 0;
- for (i = 0; i < strlen(encrypted_text); i++) {
- printf("%02X", encrypted_text[i]);
- }
- */
- }
- void Functions::util_sm4_decrypt(const char* plain_text, const char* key_screct, char* decrypted_text) {
-
- SM4_KEY key;
- uint8_t ebytes[16] = { 0 };
- uint8_t bs[16];
- size_t bs_len = 0;
- hex_to_bytes(key_screct, strlen(key_screct) - 1, bs, &bs_len);
- sm4_set_encrypt_key(&key, bs);
- // base64解码
- size_t outLen;
- uint8_t deBuf[4096] = { '\0' };
- base64_decode(plain_text, strlen(plain_text), &outLen, deBuf);
- /* test encrypt once */
- auto i = 0;
- auto len = outLen;
- auto blockCount = len / 16;
- if (len % 16 != 0) {
- blockCount++;
- }
- //uint8_t *buf = malloc(blockCount*16)
- for (i = 0; i < len; i += 16) {
- sm4_decrypt(&key, (uint8_t*)(deBuf + i), (uint8_t*)(decrypted_text + i));
- }
-
- }
- void Functions::util_rand_bytes(uint8_t* buf, size_t buflen)
- {
- rand_bytes(buf, buflen);
- }
- static const char base64_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
- void Functions::base64_encode(const unsigned char* data, size_t input_length, char* encoded_data){
- size_t output_length = 4 * ((input_length + 2) / 3);
- if (encoded_data == NULL) return ;
- for (size_t i = 0, j = 0; i < input_length;) {
- uint32_t octet_a = i < input_length ? data[i++] : 0;
- uint32_t octet_b = i < input_length ? data[i++] : 0;
- uint32_t octet_c = i < input_length ? data[i++] : 0;
- uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
- encoded_data[j++] = base64_chars[(triple >> 3 * 6) & 0x3F];
- encoded_data[j++] = base64_chars[(triple >> 2 * 6) & 0x3F];
- encoded_data[j++] = base64_chars[(triple >> 1 * 6) & 0x3F];
- encoded_data[j++] = base64_chars[(triple >> 0 * 6) & 0x3F];
- }
- // 添加Base64填充字符
- for (size_t i = 0; i < (3 - input_length % 3) % 3; i++) {
- encoded_data[output_length - 1 - i] = '=';
- }
- encoded_data[output_length] = '\0';
- }
- // Base64解码
- void Functions::base64_decode(const char* data, size_t input_length, size_t* output_length, uint8_t* decoded_data) {
- if (input_length % 4 != 0) return ;
- *output_length = input_length / 4 * 3;
- if (data[input_length - 1] == '=') (*output_length)--;
- if (data[input_length - 2] == '=') (*output_length)--;
- if (decoded_data == NULL) return ;
- for (size_t i = 0, j = 0; i < input_length;) {
- uint32_t sextet_a = data[i] == '=' ? 0 & i++ : base64_chars[(unsigned char)data[i++]];
- uint32_t sextet_b = data[i] == '=' ? 0 & i++ : base64_chars[(unsigned char)data[i++]];
- uint32_t sextet_c = data[i] == '=' ? 0 & i++ : base64_chars[(unsigned char)data[i++]];
- uint32_t sextet_d = data[i] == '=' ? 0 & i++ : base64_chars[(unsigned char)data[i++]];
- uint32_t triple = (sextet_a << 3 * 6) + (sextet_b << 2 * 6) + (sextet_c << 1 * 6) + sextet_d;
- if (j < *output_length) decoded_data[j++] = (triple >> 2 * 8) & 0xFF;
- if (j < *output_length) decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
- if (j < *output_length) decoded_data[j++] = triple & 0xFF;
- }
- }
|