AuthLicense.c 10 KB

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