main.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. #include <stdlib.h>
  2. #include <time.h>
  3. #include <cstring>
  4. #include <stdio.h>
  5. #include <cstdint>
  6. #include <fcntl.h>
  7. #include <unistd.h>
  8. #include <string>
  9. #include "lzo/minilzo.h"
  10. #include "zstd/zstd.h"
  11. #include "ucl/ucl.h"
  12. #include "lz4/lz4.h"
  13. #include "cipher/aes.h"
  14. #include "cipher/des.h"
  15. #include "cipher/des3.h"
  16. #include "cipher/rc4.h"
  17. #include "cipher/rsa.h"
  18. #include "cipher/ecc.h"
  19. using namespace std;
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. #include "lzma/FileIO.h"
  24. #include "lzma/TinyLzmaCompress.h"
  25. #include "lzma/TinyLzmaDecompress.h"
  26. #ifdef __cplusplus
  27. }
  28. #endif
  29. #define HEAP_ALLOC(var, size) \
  30. lzo_align_t __LZO_MMODEL var [ ((size) + (sizeof(lzo_align_t) - 1)) / sizeof(lzo_align_t) ]
  31. // 定义各个字段的偏移量
  32. #define SYMMETRIC_ENC_TYP_OFFSET 0
  33. #define ASYMMETRIC_ENC_TYP_OFFSET 1
  34. #define COMP_TYP_OFFSET 2
  35. #define INITIAL_FILE_SIZE_OFFSET 3
  36. #define COMPRESSED_FILE_SIZE_OFFSET 7
  37. #define ENCRYPTED_FILE_SIZE_OFFSET 11
  38. #define ENCRYPTED_KEY_SIZE_OFFSET 15
  39. #define PROTECT_TYPE_OFFSET 19
  40. #define MAC_NUMS_OFFSET 20
  41. #define RUN_ADD_OFFSET 21
  42. /**
  43. *
  44. * 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  45. */
  46. static HEAP_ALLOC(wrkmem, LZO1X_1_MEM_COMPRESS);
  47. const int MAGIC_LENGTH = 32;
  48. static int get_random_bytes(unsigned char *buf, size_t len) {
  49. // FILE *f;
  50. // f = fopen("/dev/urandom", "r");
  51. // fread(buf, len, 1, f);
  52. // fclose(f);
  53. srand((unsigned)time(NULL));
  54. for(int i = 0; i < len; i++) {
  55. buf[i] = rand() % 255;
  56. }
  57. return 0;
  58. }
  59. int hexStringToByteArray(const char *hexString, unsigned char *byteArray, int byteArraySize) {
  60. int count = 0;
  61. const char *pos = hexString;
  62. // 转换16进制字符串为字节数组
  63. while (count < byteArraySize && sscanf(pos, "%2hhx", &byteArray[count]) == 1) {
  64. pos += 2;
  65. count++;
  66. }
  67. return count; // 返回转换的字节数
  68. }
  69. int main(int argc, char *argv[]) {
  70. FILE *input_file;
  71. char *input_file_path = argv[1];
  72. uint8_t symmetric_enc_typ = (uint8_t) atoi(argv[2]);
  73. uint8_t asymmetric_enc_typ = (uint8_t) atoi(argv[3]);
  74. uint8_t comp_typ = (uint8_t) atoi(argv[4]);
  75. uint8_t cp_symmetric_enc_typ = symmetric_enc_typ;
  76. uint8_t cp_asymmetric_enc_typ = asymmetric_enc_typ;
  77. uint8_t cp_comp_typ = comp_typ;
  78. char *output_file_path = argv[5];
  79. // 保护模式
  80. uint8_t protect_type = (uint8_t) atoi(argv[6]);
  81. char *puf_path = argv[7];
  82. uint8_t mac_enable = (uint8_t) atoi(argv[8]);
  83. char *mac_path = argv[9];
  84. // 运行地址 0xb0000000
  85. char *str_run_add = argv[10];
  86. // printf("str_run_add %s\n", str_run_add);
  87. unsigned int run_add = strtoull(str_run_add, NULL, 16);
  88. printf("input_file_path %s\n", input_file_path);
  89. printf("symmetric_enc_typ %u\n", symmetric_enc_typ);
  90. printf("asymmetric_enc_typ %u\n", asymmetric_enc_typ);
  91. printf("comp_typ %u\n", comp_typ);
  92. printf("output_file_path %s\n", output_file_path);
  93. printf("protect type %u\n", protect_type);
  94. printf("puf_path %s\n", puf_path);
  95. printf("mac_enable %u\n", mac_enable);
  96. printf("mac_path %s\n", mac_path);
  97. input_file = fopen(input_file_path, "rb");
  98. if (input_file == NULL) {
  99. printf("unable to open file.\n");
  100. return 1;
  101. }
  102. // 获取文件大小
  103. fseek(input_file, 0, SEEK_END);
  104. uint32_t initial_file_size = ftell(input_file);
  105. printf("initial file size:%u\n", initial_file_size);
  106. rewind(input_file);
  107. /**
  108. * DES: 64bit 1 key_length 8
  109. * AES: 128bit 2 key_length 32
  110. * 3DES: 64bit 3 key_length 24
  111. * RC4: 256bit 4 key_length 32
  112. */
  113. int symmetric_map[5] = {0, 4, 1, 3, 2};
  114. symmetric_enc_typ = symmetric_map[symmetric_enc_typ];
  115. uint8_t input_length, key_length;
  116. if (symmetric_enc_typ == 1) input_length = 8, key_length = 8;
  117. else if (symmetric_enc_typ == 2) input_length = 16, key_length = 32;
  118. else if (symmetric_enc_typ == 3) input_length = 8, key_length = 24;
  119. else if (symmetric_enc_typ == 4) input_length = 1, key_length = 32;
  120. else input_length = 0, key_length = 0;
  121. // 分配足够的内存来存储文件内容
  122. uint8_t *byte_array = (uint8_t *) malloc(initial_file_size * sizeof(uint8_t));
  123. if (byte_array == NULL) {
  124. printf("unable to allocate memory.\n");
  125. return 0;
  126. }
  127. // 读取文件内容到字节数组
  128. int num_elements = fread(byte_array, sizeof(uint8_t), initial_file_size, input_file);
  129. if (num_elements != initial_file_size) {
  130. printf("an error occurred while reading the file.\n");
  131. return 0;
  132. }
  133. fclose(input_file);
  134. const int serial_length = 39;
  135. unsigned char puf_key[serial_length];
  136. memset(puf_key, 0, serial_length);
  137. unsigned char puf_value[serial_length];
  138. memset(puf_value, 0, serial_length);
  139. if (protect_type == 1) {
  140. printf("[STATE] node:1 ; message:PUF交互\n");
  141. FILE *file = fopen(puf_path, "r");
  142. if (file == NULL) {
  143. perror("Failed to open file");
  144. return EXIT_FAILURE;
  145. }
  146. char line1[256];
  147. char line2[256];
  148. char line3[256];
  149. // 读取第一行
  150. if (fgets(line1, sizeof(line1), file) == NULL) {
  151. perror("Failed to read line 1");
  152. fclose(file);
  153. return EXIT_FAILURE;
  154. }
  155. // 读取第二行
  156. if (fgets(line2, sizeof(line2), file) == NULL) {
  157. perror("Failed to read line 2");
  158. fclose(file);
  159. return EXIT_FAILURE;
  160. }
  161. // 读取第三行
  162. if (fgets(line3, sizeof(line3), file) == NULL) {
  163. perror("Failed to read line 3");
  164. fclose(file);
  165. return EXIT_FAILURE;
  166. }
  167. // 打印读取的内容
  168. printf("Line 1: %s", line1);
  169. printf("Line 2: %s", line2);
  170. printf("Line 3: %s\n", line3);
  171. // 关闭文件
  172. fclose(file);
  173. // 转换字符串为字节数组
  174. int convertedCount2 = hexStringToByteArray(line2, puf_key, serial_length);
  175. int convertedCount3 = hexStringToByteArray(line3, puf_value, serial_length);
  176. for (int i = 0; i < 39; i++) {
  177. printf("0x%02x,", puf_value[i]);
  178. }
  179. printf("\n");
  180. // 检查转换的结果
  181. if (convertedCount2 != serial_length || convertedCount3 != serial_length) {
  182. printf("Conversion error\n");
  183. return EXIT_FAILURE;
  184. }
  185. }
  186. printf("[STATE] node:2 ; message:获取密钥\n");
  187. uint8_t module_key[32] = {0};
  188. if (protect_type == 1)
  189. memcpy(module_key, puf_value + 4, 32);
  190. else
  191. get_random_bytes(module_key, 32);
  192. printf("module key:\n");
  193. for (int i = 0; i < 32; i++) {
  194. printf("%02x ", module_key[i]);
  195. }
  196. printf("\n");
  197. uint8_t mac_nums = 0;
  198. unsigned char mac_array[10][18];
  199. memset(mac_array, 0, 180);
  200. if (mac_enable == 1) {
  201. // 从本地文件中读取MAC地址
  202. int mac_fd = open(mac_path, O_RDONLY);
  203. if (mac_fd <= 0) {
  204. printf("mac_fd : %d\n", mac_fd);
  205. printf("本地未找到MAC地址列表文件\n");
  206. return -1;
  207. }
  208. while (mac_nums < 10 && (read(mac_fd, mac_array[mac_nums], 18) > 0)) {
  209. mac_nums++;
  210. }
  211. close(mac_fd);
  212. } else {
  213. mac_array[0][0] = 'P';
  214. }
  215. int comp_map[6] = {0, 3, 1, 2, 4, 5};
  216. comp_typ = comp_map[comp_typ];
  217. unsigned long compressed_file_size;
  218. uint8_t *compress_out = (uint8_t *) calloc(initial_file_size, sizeof(uint8_t));
  219. int r = -1;
  220. if (comp_typ == 1) {
  221. printf("compress with lzo...\n");
  222. r = lzo1x_1_compress(byte_array, initial_file_size, compress_out, reinterpret_cast<lzo_uint *>(&compressed_file_size), wrkmem);
  223. } else if (comp_typ == 2) {
  224. printf("compress with ucl...\n");
  225. int level = 2;
  226. r = ucl_nrv2b_99_compress(byte_array, initial_file_size, compress_out,
  227. reinterpret_cast<ucl_uint *>(&compressed_file_size), NULL, level, NULL, NULL);
  228. compressed_file_size = reinterpret_cast<unsigned long>(compressed_file_size);
  229. } else if (comp_typ == 3) {
  230. printf("compress with lzma...\n");
  231. compressed_file_size = initial_file_size;
  232. r = tinyLzmaCompress(byte_array, initial_file_size, compress_out,
  233. reinterpret_cast<size_t *>(&compressed_file_size));
  234. compressed_file_size = reinterpret_cast<unsigned long>(compressed_file_size);
  235. } else if (comp_typ == 4) {
  236. printf("compress with zstd...\n");
  237. compressed_file_size = ZSTD_compressBound(initial_file_size);
  238. printf("%d\n", compressed_file_size);
  239. compressed_file_size = ZSTD_compress(compress_out, compressed_file_size, byte_array, initial_file_size, 2);
  240. } else if (comp_typ == 5) {
  241. printf("compress with lz4...\n");
  242. int max_dst_size = LZ4_compressBound(initial_file_size);
  243. compressed_file_size = LZ4_compress_default(byte_array, compress_out, initial_file_size, max_dst_size);
  244. }
  245. if (r == 0 || r == -1) {
  246. printf("compressed %u bytes into %u bytes\n", initial_file_size, compressed_file_size);
  247. printf("[STATE] node: 6 ; message:整体压缩\n");
  248. } else {
  249. /* this should NEVER happen */
  250. printf("internal error - compression failed: %d\n", r);
  251. return 2;
  252. }
  253. /* check for an incompressible block */
  254. if ((unsigned int) compressed_file_size >= initial_file_size) {
  255. printf("This block contains incompressible data.\n");
  256. return 0;
  257. }
  258. uint8_t *new_compress_out = (uint8_t *) realloc(compress_out, compressed_file_size);
  259. if (new_compress_out != NULL) {
  260. compress_out = new_compress_out;
  261. }
  262. uint8_t key[key_length];
  263. for (int i = 0; i < key_length; i++) key[i] = module_key[i];
  264. printf("key:\n");
  265. for (int i = 0; i < key_length; i++) {
  266. printf("%02x ", key[i]);
  267. }
  268. printf("\n");
  269. uint32_t encrypted_file_size = (compressed_file_size + input_length - 1) / input_length * input_length;
  270. printf("encrypted_file_size %u\n", encrypted_file_size);
  271. printf("compress bytes:\n");
  272. for (int i = 0; i < 8; i++) {
  273. printf("%02x ", compress_out[i]);
  274. }
  275. printf("\n");
  276. uint8_t *buff = (uint8_t *) malloc(encrypted_file_size * sizeof(uint8_t));
  277. if (symmetric_enc_typ == 1) {
  278. printf("enc_typ = 1\n");
  279. DesContext des_context;
  280. desInit(&des_context, key, key_length);
  281. for (int i = 0; i < encrypted_file_size; i += input_length)
  282. desEncryptBlock(&des_context, compress_out + i, buff + i);
  283. }
  284. if (symmetric_enc_typ == 2) {
  285. printf("enc_typ = 2\n");
  286. AesContext aes_context;
  287. aesInit(&aes_context, key, key_length);
  288. for (int i = 0; i < encrypted_file_size; i += input_length)
  289. aesEncryptBlock(&aes_context, compress_out + i, buff + i);
  290. }
  291. if (symmetric_enc_typ == 3) {
  292. printf("enc_typ = 3\n");
  293. Des3Context des3_context;
  294. des3Init(&des3_context, key, key_length);
  295. for (int i = 0; i < encrypted_file_size; i += input_length)
  296. des3EncryptBlock(&des3_context, compress_out + i, buff + i);
  297. }
  298. if (symmetric_enc_typ == 4) {
  299. printf("enc_typ = 4\n");
  300. rc4_encryption(compress_out, encrypted_file_size, key, key_length);
  301. memcpy(buff, compress_out, encrypted_file_size);
  302. }
  303. printf("final bytes:\n");
  304. for (int i = 0; i < 8; i++) {
  305. printf("%02x ", buff[i]);
  306. }
  307. printf("\n");
  308. for (int i = encrypted_file_size - 8; i < encrypted_file_size; i++) {
  309. printf("%02x ", buff[i]);
  310. }
  311. printf("\n");
  312. printf("[STATE] node: 4 ; message:整体加密\n");
  313. // uint8_t *tmp_encrypted = NULL;
  314. uint8_t *encrypted = NULL;
  315. // uint32_t encrypted_key_size = sizeof key * sizeof(long long);
  316. uint32_t encrypted_key_size = key_length;
  317. // 如果是软硬结合,加密的key为PUF激励长度39字节
  318. if (protect_type == 1) encrypted_key_size = 39;
  319. /**
  320. * rsa key
  321. */
  322. struct public_key_class pub[1];
  323. struct private_key_class priv[1];
  324. /**
  325. * ecc key
  326. */
  327. uint8_t pub_key[ECC_KEYSIZE];
  328. uint8_t prv_key[ECC_KEYSIZE];
  329. if (asymmetric_enc_typ != 0) {
  330. if (asymmetric_enc_typ == 5) {
  331. rsa_gen_keys(pub, priv);
  332. encrypted_key_size *= 8;
  333. printf("Private Key:\n Modulus: %lld\n Exponent: %lld\n", (long long) priv->modulus, (long long) priv->exponent);
  334. printf("Public Key:\n Modulus: %lld\n Exponent: %lld\n", (long long) pub->modulus, (long long) pub->exponent);
  335. // 1是软硬结合,加密的是激励
  336. // 0是软加密,加密的是秘钥
  337. if (protect_type == 1) encrypted = rsa_encrypt(puf_key, 39, pub);
  338. else if (protect_type == 0) encrypted = rsa_encrypt(key, key_length, pub);
  339. // encrypted = (uint8_t *) tmp_encrypted;
  340. if (encrypted == NULL) {
  341. fprintf(stderr, "error in encryption!\n");
  342. return 1;
  343. }
  344. } else if (asymmetric_enc_typ == 6) {
  345. printf("enc with ecc\n");
  346. ecc_init_keys(pub_key, prv_key);
  347. printf("ecc init end\n");
  348. // 1是软硬结合,加密的是激励,申请的空间是39
  349. // 0是软加密,加密的是秘钥,申请的空间是sizeof key
  350. if (protect_type == 0) {
  351. encrypted = (uint8_t *) realloc(encrypted, sizeof key);
  352. ecc_encrypt(pub_key, key, key_length, encrypted, &encrypted_key_size);
  353. } else if (protect_type == 1) {
  354. encrypted = (uint8_t *) realloc(encrypted, 39);
  355. ecc_encrypt(pub_key, puf_key, 39, encrypted, &encrypted_key_size);
  356. }
  357. printf("encrypted_key_size is %u with ecc\n", encrypted_key_size);
  358. }
  359. }
  360. printf("enc key\n");
  361. for (int i = 0; i < encrypted_key_size; i++) {
  362. printf("0x%02x, ", encrypted[i]);
  363. }
  364. printf("\n");
  365. /**
  366. * enc_typ 1B
  367. * comp_typ 1B
  368. * initial_file_size 4B
  369. * compressed_file_size 4B
  370. * encrypted_file_size 4B
  371. */
  372. printf("uint8_t symmetric_enc_typ: %u\n", symmetric_enc_typ);
  373. printf("uint8_t asymmetric_enc_typ: %u\n", asymmetric_enc_typ);
  374. printf("uint8_t comp_typ: %u\n", comp_typ);
  375. printf("uint32_t file_size: %u\n", initial_file_size);
  376. printf("uint32_t compressed_file_size: %u\n", compressed_file_size);
  377. printf("uint32_t encrypted_file_size: %u\n", encrypted_file_size);
  378. printf("uint8_t input_length: %u\n", input_length);
  379. printf("uint8_t key_length: %u\n", key_length);
  380. uint8_t magic[MAGIC_LENGTH];
  381. // 将各个字段的值存入 magic 数组
  382. magic[SYMMETRIC_ENC_TYP_OFFSET] = symmetric_enc_typ;
  383. magic[ASYMMETRIC_ENC_TYP_OFFSET] = asymmetric_enc_typ;
  384. magic[COMP_TYP_OFFSET] = comp_typ;
  385. memcpy(magic + INITIAL_FILE_SIZE_OFFSET, &initial_file_size, sizeof(uint32_t));
  386. memcpy(magic + COMPRESSED_FILE_SIZE_OFFSET, &compressed_file_size, sizeof(uint32_t));
  387. memcpy(magic + ENCRYPTED_FILE_SIZE_OFFSET, &encrypted_file_size, sizeof(uint32_t));
  388. memcpy(magic + ENCRYPTED_KEY_SIZE_OFFSET, &encrypted_key_size, sizeof(uint32_t));
  389. // 保护类型
  390. memcpy(magic + PROTECT_TYPE_OFFSET, &protect_type, sizeof(uint8_t));
  391. // MAC地址绑定的数量
  392. memcpy(magic + MAC_NUMS_OFFSET, &mac_nums, sizeof(uint8_t));
  393. // 运行地址
  394. printf("str_run_add %x\n", run_add);
  395. memcpy(magic + RUN_ADD_OFFSET, &run_add, 4);
  396. FILE *p = fopen(output_file_path, "wb");
  397. if (p == NULL) {
  398. printf("open filed\n");
  399. }
  400. fwrite(magic, sizeof(uint8_t), MAGIC_LENGTH, p);
  401. fwrite(buff, sizeof(uint8_t), encrypted_file_size, p);
  402. fwrite(encrypted, sizeof(uint8_t), encrypted_key_size, p);
  403. if (asymmetric_enc_typ == 5) {
  404. fwrite(pub, sizeof(struct public_key_class), 1, p);
  405. fwrite(priv, sizeof(struct private_key_class), 1, p);
  406. } else if (asymmetric_enc_typ == 6) {
  407. fwrite(pub_key, sizeof(uint8_t), ECC_KEYSIZE, p);
  408. fwrite(prv_key, sizeof(uint8_t), ECC_KEYSIZE, p);
  409. }
  410. for (int i = 0; i < mac_nums; i++) {
  411. for (int j = 0; j < 18; j++) {
  412. printf("%02x ", mac_array[i][j]);
  413. }
  414. printf("\n");
  415. }
  416. for (int i = 0; i < mac_nums; i++) fwrite(mac_array[i], sizeof(uint8_t), 18, p);
  417. printf("[STATE] node: 7 ; message:加壳\n");
  418. fclose(p);
  419. free(byte_array);
  420. free(buff);
  421. printf("[STATE] node: 8 ; message:成功\n");
  422. printf("成功\n");
  423. return 0;
  424. }
  425. /**
  426. * magic
  427. * encrypted_file
  428. * enc_key/enc serial_challenge
  429. * pub_key
  430. * prv_key
  431. * mac_info[0-10]
  432. */