AuthLicense.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include "md5.h"
  5. /*
  6. preHandleIndex:
  7. 0: |
  8. 1: &
  9. 2: ^
  10. 3: !^
  11. 4: +
  12. 5: -
  13. */
  14. int8_t preHandle(uint32_t *preGUIDptr, uint32_t key, uint8_t preHandleIndex)
  15. {
  16. uint8_t i;
  17. switch(preHandleIndex)
  18. {
  19. case 0: // |
  20. for(i=0;i<3;i++)
  21. {
  22. preGUIDptr[i] |= key;
  23. }
  24. break;
  25. case 1: // &
  26. for(i=0;i<3;i++)
  27. {
  28. preGUIDptr[i] &= key;
  29. }
  30. break;
  31. case 2: // ^
  32. for(i=0;i<3;i++)
  33. {
  34. preGUIDptr[i] ^= key;
  35. }
  36. break;
  37. case 3: // !^
  38. for(i=0;i<3;i++)
  39. {
  40. preGUIDptr[i] ^= key;
  41. preGUIDptr[i] = ~preGUIDptr[i];
  42. }
  43. break;
  44. case 4: // +
  45. for(i=0;i<3;i++)
  46. {
  47. preGUIDptr[i] += key;
  48. }
  49. break;
  50. case 5: // -
  51. for(i=0;i<3;i++)
  52. {
  53. preGUIDptr[i] -= key;
  54. }
  55. break;
  56. default:
  57. printf("´íÎóµ±Ç°Ñ¡ÔñµÄÔËË㷽ʽ²»¿ÉÓÃ\r\n");
  58. return -1;
  59. }
  60. return 1;
  61. }
  62. int8_t bitDiffuse(uint8_t decrypt[16], uint8_t License[256], uint8_t bitSelectIndex)
  63. {
  64. uint8_t i, j;
  65. //À©É¢µ½128×Ö½Ú
  66. switch (bitSelectIndex) {
  67. case 0:
  68. for(i=0;i<16;i++)
  69. {
  70. for(j=0;j<8;j++)
  71. {
  72. if((decrypt[i]&(0x01<<j)))
  73. License[i*8+j] |= 0x01;
  74. else
  75. License[i*8+j] &= ~0x01;
  76. }
  77. }
  78. break;
  79. case 1:
  80. for(i=0;i<16;i++)
  81. {
  82. for(j=0;j<8;j++)
  83. {
  84. if((decrypt[i]&(0x01<<j)))
  85. License[i*8+j] |= 0x02;
  86. else
  87. License[i*8+j] &= ~0x02;
  88. }
  89. }
  90. break;
  91. case 2:
  92. for(i=0;i<16;i++)
  93. {
  94. for(j=0;j<8;j++)
  95. {
  96. if((decrypt[i]&(0x01<<j)))
  97. License[i*8+j] |= 0x04;
  98. else
  99. License[i*8+j] &= ~0x04;
  100. }
  101. }
  102. break;
  103. case 3:
  104. for(i=0;i<16;i++)
  105. {
  106. for(j=0;j<8;j++)
  107. {
  108. if((decrypt[i]&(0x01<<j)))
  109. License[i*8+j] |= 0x08;
  110. else
  111. License[i*8+j] &= ~0x08;
  112. }
  113. }
  114. break;
  115. case 4:
  116. for(i=0;i<16;i++)
  117. {
  118. for(j=0;j<8;j++)
  119. {
  120. if((decrypt[i]&(0x01<<j)))
  121. License[i*8+j] |= 0x10;
  122. else
  123. License[i*8+j] &= ~0x10;
  124. }
  125. }
  126. break;
  127. case 5:
  128. for(i=0;i<16;i++)
  129. {
  130. for(j=0;j<8;j++)
  131. {
  132. if((decrypt[i]&(0x01<<j)))
  133. License[i*8+j] |= 0x20;
  134. else
  135. License[i*8+j] &= ~0x20;
  136. }
  137. }
  138. break;
  139. case 6:
  140. for(i=0;i<16;i++)
  141. {
  142. for(j=0;j<8;j++)
  143. {
  144. if((decrypt[i]&(0x01<<j)))
  145. License[i*8+j] |= 0x40;
  146. else
  147. License[i*8+j] &= ~0x40;
  148. }
  149. }
  150. break;
  151. case 7:
  152. for(i=0;i<16;i++)
  153. {
  154. for(j=0;j<8;j++)
  155. {
  156. if((decrypt[i]&(0x01<<j)))
  157. License[i*8+j] |= 0x80;
  158. else
  159. License[i*8+j] &= ~0x80;
  160. }
  161. }
  162. break;
  163. default:
  164. printf("´íÎóÎÞЧµÄÀ©É¢Î»Ñ¡Ôñ\r\n");
  165. return -1;
  166. }
  167. return 1;
  168. }
  169. int8_t Authorize(uint8_t decrypt[16], uint8_t License[128], uint8_t bitSelectIndex)
  170. {
  171. uint8_t i, j;
  172. //À©É¢µ½128×Ö½Ú
  173. switch (bitSelectIndex) {
  174. case 0:
  175. for(i=0;i<16;i++)
  176. {
  177. for(j=0;j<8;j++)
  178. {
  179. if ( ( License[i*8+j] & 0x01 ) == ( (decrypt[i] >> j) & 0x01 ) )
  180. {
  181. //printf("run\r\n");
  182. }
  183. else{
  184. return -1;
  185. //printf("bitDiffuse2 error\r\n");
  186. }
  187. }
  188. }
  189. //printf("bitDiffuse2 ok\r\n");
  190. break;
  191. case 1:
  192. for(i=0;i<16;i++)
  193. {
  194. for(j=0;j<8;j++)
  195. {
  196. if ( ( (License[i*8+j] >> 1) & 0x01 ) == ( (decrypt[i] >> j) & 0x01 ) )
  197. {
  198. //printf("run\r\n");
  199. }
  200. else{
  201. return -1;
  202. //printf("bitDiffuse2 error\r\n");
  203. }
  204. }
  205. }
  206. //printf("bitDiffuse2 ok\r\n");
  207. break;
  208. case 2:
  209. for(i=0;i<16;i++)
  210. {
  211. for(j=0;j<8;j++)
  212. {
  213. if ( ( (License[i*8+j] >> 2) & 0x01 ) == ( (decrypt[i] >> j) & 0x01 ) )
  214. {
  215. //printf("run\r\n");
  216. }
  217. else{
  218. return -1;
  219. //printf("bitDiffuse2 error\r\n");
  220. }
  221. }
  222. }
  223. //printf("bitDiffuse2 ok\r\n");
  224. break;
  225. case 3:
  226. for(i=0;i<16;i++)
  227. {
  228. for(j=0;j<8;j++)
  229. {
  230. if ( ( (License[i*8+j] >> 3) & 0x01 ) == ( (decrypt[i] >> j) & 0x01 ) )
  231. {
  232. //printf("run\r\n");
  233. }
  234. else{
  235. return -1;
  236. //printf("bitDiffuse2 error\r\n");
  237. }
  238. }
  239. }
  240. //printf("bitDiffuse2 ok\r\n");
  241. break;
  242. case 4:
  243. for(i=0;i<16;i++)
  244. {
  245. for(j=0;j<8;j++)
  246. {
  247. if ( ( (License[i*8+j] >> 4) & 0x01 ) == ( (decrypt[i] >> j) & 0x01 ) )
  248. {
  249. //printf("run\r\n");
  250. }
  251. else{
  252. return -1;
  253. //printf("bitDiffuse2 error\r\n");
  254. }
  255. }
  256. }
  257. //printf("bitDiffuse2 ok\r\n");
  258. break;
  259. case 5:
  260. for(i=0;i<16;i++)
  261. {
  262. for(j=0;j<8;j++)
  263. {
  264. if ( ( (License[i*8+j] >> 5) & 0x01 ) == ( (decrypt[i] >> j) & 0x01 ) )
  265. {
  266. //printf("run\r\n");
  267. }
  268. else{
  269. return -1;
  270. //printf("bitDiffuse2 error\r\n");
  271. }
  272. }
  273. }
  274. //printf("bitDiffuse2 ok\r\n");
  275. break;
  276. case 6:
  277. for(i=0;i<16;i++)
  278. {
  279. for(j=0;j<8;j++)
  280. {
  281. if ( ( (License[i*8+j] >> 6) & 0x01 ) == ( (decrypt[i] >> j) & 0x01 ) )
  282. {
  283. //printf("run\r\n");
  284. }
  285. else{
  286. return -1;
  287. //printf("bitDiffuse2 error\r\n");
  288. }
  289. }
  290. }
  291. //printf("bitDiffuse2 ok\r\n");
  292. break;
  293. case 7:
  294. for(i=0;i<16;i++)
  295. {
  296. for(j=0;j<8;j++)
  297. {
  298. if ( ( (License[i*8+j] >> 7) & 0x01 ) == ( (decrypt[i] >> j) & 0x01 ) )
  299. {
  300. //printf("run\r\n");
  301. }
  302. else{
  303. return -1;
  304. //printf("bitDiffuse2 error\r\n");
  305. }
  306. }
  307. }
  308. //printf("bitDiffuse2 ok\r\n");
  309. break;
  310. default:
  311. //printf("´íÎóÎÞЧµÄÀ©É¢Î»Ñ¡Ôñ\r\n");
  312. return -1;
  313. }
  314. return 1;
  315. }
  316. int8_t ExchangeCharToByte(uint8_t *LicenseKeySrc256, uint8_t *LicenseKeyDst128)
  317. {
  318. uint8_t i, index;
  319. uint8_t tmp;
  320. uint8_t tmp2;
  321. for(i=0;i<128;i++)
  322. {
  323. index = i*2;
  324. if(LicenseKeySrc256[index] >= '0' && LicenseKeySrc256[index] <= '9')
  325. {
  326. tmp = LicenseKeySrc256[index] - '0';
  327. }
  328. else if(LicenseKeySrc256[index] >= 'A' && LicenseKeySrc256[index] <= 'F')
  329. {
  330. tmp = LicenseKeySrc256[index] - 'A'+10;
  331. }
  332. else if(LicenseKeySrc256[index] >= 'a' && LicenseKeySrc256[index] <= 'f')
  333. {
  334. tmp = LicenseKeySrc256[index] - 'a'+10;
  335. }
  336. else
  337. {
  338. return -1;
  339. //printf("error\r\n");
  340. }
  341. if(LicenseKeySrc256[index+1] >= '0' && LicenseKeySrc256[index+1] <= '9')
  342. {
  343. tmp2 = LicenseKeySrc256[index+1] - '0';
  344. }
  345. else if(LicenseKeySrc256[index+1] >= 'A' && LicenseKeySrc256[index+1] <= 'F')
  346. {
  347. tmp2 = LicenseKeySrc256[index+1] - 'A'+10;
  348. }
  349. else if(LicenseKeySrc256[index+1] >= 'a' && LicenseKeySrc256[index+1] <= 'f')
  350. {
  351. tmp2 = LicenseKeySrc256[index+1] - 'a'+10;
  352. }
  353. else
  354. {
  355. return -1;
  356. //printf("error\r\n");
  357. }
  358. LicenseKeyDst128[i] = ((tmp&0x0f) << 4) | (tmp2&0x0f);
  359. }
  360. return 1;
  361. }
  362. int CheckLicense()
  363. {
  364. uint32_t *pUID = (uint32_t*)0x1FFF7A10;
  365. uint8_t GUID[12] = {0};
  366. uint32_t tmp = 0;
  367. uint8_t *pLicense = (uint8_t*)0x08060000;
  368. uint8_t LICENSE[128] = {0}; //从Flash中读出的带随机数填充的128字节License
  369. uint8_t CalLicense[16] = {0}; //计算得出的有效的16字节License
  370. MD5_CTX md5;
  371. int i;
  372. //获取GUID
  373. //printf("GUID: ");
  374. for(i=0;i<3;i++)
  375. {
  376. tmp = *pUID;
  377. GUID[4*i+0] = tmp&0xff;
  378. GUID[4*i+1] = (tmp>>8)&0xff;
  379. GUID[4*i+2] = (tmp>>16)&0xff;
  380. GUID[4*i+3] = (tmp>>24)&0xff;
  381. pUID++;
  382. //printf("%02x %02x %02x %02x ", GUID[4*i+0],GUID[4*i+1],GUID[4*i+2],GUID[4*i+3]);
  383. }
  384. //printf("\n");
  385. //获取License
  386. //printf("License: ");
  387. for(i=0;i<128;i++)
  388. {
  389. LICENSE[i] = *pLicense;
  390. pLicense++;
  391. //printf("%02x ", LICENSE[i]);
  392. }
  393. //printf("\n" );
  394. //根据GUID计算License
  395. preHandle((uint32_t*)GUID, 0x33445566, 2); //预处理
  396. MD5_Init(&md5);
  397. MD5_Update(&md5,GUID,12);
  398. MD5_Final(CalLicense, &md5);
  399. // printf("CalLicense: ");
  400. // for(i=0;i<16;i++)
  401. // {
  402. // printf("%02x ", CalLicense[i]);
  403. // }
  404. // printf("\n");
  405. //比对License
  406. if(Authorize(CalLicense, LICENSE, 0) == 1) //授权通过
  407. {
  408. printf("\r\nAlready Auth Success\r\n");
  409. return 0;
  410. }
  411. else //授权不通过
  412. {
  413. printf("\n\n====================================================\n");
  414. printf("\n\tLicense is invalid, Please authorize! \n");
  415. printf("\tGUID: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x \n",
  416. GUID[0], GUID[1], GUID[2], GUID[3], GUID[4], GUID[5], GUID[6], GUID[7],
  417. GUID[8], GUID[9], GUID[10], GUID[11] );
  418. printf("====================================================\n\n");
  419. return -1;
  420. }
  421. }