123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443 |
- #include <stdio.h>
- #include <stdint.h>
- #include <stdio.h>
- #include "md5.h"
- /*
- preHandleIndex:
- 0: |
- 1: &
- 2: ^
- 3: !^
- 4: +
- 5: -
- */
- int8_t preHandle(uint32_t *preGUIDptr, uint32_t key, uint8_t preHandleIndex)
- {
- uint8_t i;
- switch(preHandleIndex)
- {
- case 0: // |
- for(i=0;i<3;i++)
- {
- preGUIDptr[i] |= key;
- }
- break;
- case 1: // &
- for(i=0;i<3;i++)
- {
- preGUIDptr[i] &= key;
- }
- break;
- case 2: // ^
- for(i=0;i<3;i++)
- {
- preGUIDptr[i] ^= key;
- }
- break;
- case 3: // !^
- for(i=0;i<3;i++)
- {
- preGUIDptr[i] ^= key;
- preGUIDptr[i] = ~preGUIDptr[i];
- }
- break;
- case 4: // +
- for(i=0;i<3;i++)
- {
- preGUIDptr[i] += key;
- }
- break;
- case 5: // -
- for(i=0;i<3;i++)
- {
- preGUIDptr[i] -= key;
- }
- break;
- default:
- printf("´íÎóµ±Ç°Ñ¡ÔñµÄÔËË㷽ʽ²»¿ÉÓÃ\r\n");
- return -1;
- }
- return 1;
- }
- int8_t bitDiffuse(uint8_t decrypt[16], uint8_t License[256], uint8_t bitSelectIndex)
- {
- uint8_t i, j;
- //À©É¢µ½128×Ö½Ú
- switch (bitSelectIndex) {
- case 0:
- for(i=0;i<16;i++)
- {
- for(j=0;j<8;j++)
- {
- if((decrypt[i]&(0x01<<j)))
- License[i*8+j] |= 0x01;
- else
- License[i*8+j] &= ~0x01;
- }
- }
- break;
- case 1:
- for(i=0;i<16;i++)
- {
- for(j=0;j<8;j++)
- {
- if((decrypt[i]&(0x01<<j)))
- License[i*8+j] |= 0x02;
- else
- License[i*8+j] &= ~0x02;
- }
- }
- break;
- case 2:
- for(i=0;i<16;i++)
- {
- for(j=0;j<8;j++)
- {
- if((decrypt[i]&(0x01<<j)))
- License[i*8+j] |= 0x04;
- else
- License[i*8+j] &= ~0x04;
- }
- }
- break;
- case 3:
- for(i=0;i<16;i++)
- {
- for(j=0;j<8;j++)
- {
- if((decrypt[i]&(0x01<<j)))
- License[i*8+j] |= 0x08;
- else
- License[i*8+j] &= ~0x08;
- }
- }
- break;
- case 4:
- for(i=0;i<16;i++)
- {
- for(j=0;j<8;j++)
- {
- if((decrypt[i]&(0x01<<j)))
- License[i*8+j] |= 0x10;
- else
- License[i*8+j] &= ~0x10;
- }
- }
- break;
- case 5:
- for(i=0;i<16;i++)
- {
- for(j=0;j<8;j++)
- {
- if((decrypt[i]&(0x01<<j)))
- License[i*8+j] |= 0x20;
- else
- License[i*8+j] &= ~0x20;
- }
- }
- break;
- case 6:
- for(i=0;i<16;i++)
- {
- for(j=0;j<8;j++)
- {
- if((decrypt[i]&(0x01<<j)))
- License[i*8+j] |= 0x40;
- else
- License[i*8+j] &= ~0x40;
- }
- }
- break;
- case 7:
- for(i=0;i<16;i++)
- {
- for(j=0;j<8;j++)
- {
- if((decrypt[i]&(0x01<<j)))
- License[i*8+j] |= 0x80;
- else
- License[i*8+j] &= ~0x80;
- }
- }
- break;
- default:
- printf("´íÎóÎÞЧµÄÀ©É¢Î»Ñ¡Ôñ\r\n");
- return -1;
- }
- return 1;
- }
- int8_t Authorize(uint8_t decrypt[16], uint8_t License[128], uint8_t bitSelectIndex)
- {
- uint8_t i, j;
- //À©É¢µ½128×Ö½Ú
- switch (bitSelectIndex) {
- case 0:
- for(i=0;i<16;i++)
- {
- for(j=0;j<8;j++)
- {
- if ( ( License[i*8+j] & 0x01 ) == ( (decrypt[i] >> j) & 0x01 ) )
- {
- //printf("run\r\n");
- }
- else{
- return -1;
- //printf("bitDiffuse2 error\r\n");
- }
- }
- }
- //printf("bitDiffuse2 ok\r\n");
- break;
- case 1:
- for(i=0;i<16;i++)
- {
- for(j=0;j<8;j++)
- {
-
- if ( ( (License[i*8+j] >> 1) & 0x01 ) == ( (decrypt[i] >> j) & 0x01 ) )
- {
- //printf("run\r\n");
- }
- else{
- return -1;
- //printf("bitDiffuse2 error\r\n");
- }
- }
- }
- //printf("bitDiffuse2 ok\r\n");
- break;
- case 2:
- for(i=0;i<16;i++)
- {
- for(j=0;j<8;j++)
- {
- if ( ( (License[i*8+j] >> 2) & 0x01 ) == ( (decrypt[i] >> j) & 0x01 ) )
- {
- //printf("run\r\n");
- }
- else{
- return -1;
- //printf("bitDiffuse2 error\r\n");
- }
- }
- }
- //printf("bitDiffuse2 ok\r\n");
- break;
- case 3:
- for(i=0;i<16;i++)
- {
- for(j=0;j<8;j++)
- {
- if ( ( (License[i*8+j] >> 3) & 0x01 ) == ( (decrypt[i] >> j) & 0x01 ) )
- {
- //printf("run\r\n");
- }
- else{
- return -1;
- //printf("bitDiffuse2 error\r\n");
- }
- }
- }
- //printf("bitDiffuse2 ok\r\n");
- break;
- case 4:
- for(i=0;i<16;i++)
- {
- for(j=0;j<8;j++)
- {
- if ( ( (License[i*8+j] >> 4) & 0x01 ) == ( (decrypt[i] >> j) & 0x01 ) )
- {
- //printf("run\r\n");
- }
- else{
- return -1;
- //printf("bitDiffuse2 error\r\n");
- }
- }
- }
- //printf("bitDiffuse2 ok\r\n");
- break;
- case 5:
- for(i=0;i<16;i++)
- {
- for(j=0;j<8;j++)
- {
- if ( ( (License[i*8+j] >> 5) & 0x01 ) == ( (decrypt[i] >> j) & 0x01 ) )
- {
- //printf("run\r\n");
- }
- else{
- return -1;
- //printf("bitDiffuse2 error\r\n");
- }
- }
- }
- //printf("bitDiffuse2 ok\r\n");
- break;
- case 6:
- for(i=0;i<16;i++)
- {
- for(j=0;j<8;j++)
- {
- if ( ( (License[i*8+j] >> 6) & 0x01 ) == ( (decrypt[i] >> j) & 0x01 ) )
- {
- //printf("run\r\n");
- }
- else{
- return -1;
- //printf("bitDiffuse2 error\r\n");
- }
- }
- }
- //printf("bitDiffuse2 ok\r\n");
- break;
- case 7:
- for(i=0;i<16;i++)
- {
- for(j=0;j<8;j++)
- {
- if ( ( (License[i*8+j] >> 7) & 0x01 ) == ( (decrypt[i] >> j) & 0x01 ) )
- {
- //printf("run\r\n");
- }
- else{
- return -1;
- //printf("bitDiffuse2 error\r\n");
- }
- }
- }
- //printf("bitDiffuse2 ok\r\n");
- break;
- default:
- //printf("´íÎóÎÞЧµÄÀ©É¢Î»Ñ¡Ôñ\r\n");
- return -1;
- }
- return 1;
- }
- int8_t ExchangeCharToByte(uint8_t *LicenseKeySrc256, uint8_t *LicenseKeyDst128)
- {
- uint8_t i, index;
- uint8_t tmp;
- uint8_t tmp2;
- for(i=0;i<128;i++)
- {
- index = i*2;
- if(LicenseKeySrc256[index] >= '0' && LicenseKeySrc256[index] <= '9')
- {
- tmp = LicenseKeySrc256[index] - '0';
- }
- else if(LicenseKeySrc256[index] >= 'A' && LicenseKeySrc256[index] <= 'F')
- {
- tmp = LicenseKeySrc256[index] - 'A'+10;
- }
- else if(LicenseKeySrc256[index] >= 'a' && LicenseKeySrc256[index] <= 'f')
- {
- tmp = LicenseKeySrc256[index] - 'a'+10;
- }
- else
- {
- return -1;
- //printf("error\r\n");
- }
-
- if(LicenseKeySrc256[index+1] >= '0' && LicenseKeySrc256[index+1] <= '9')
- {
- tmp2 = LicenseKeySrc256[index+1] - '0';
- }
- else if(LicenseKeySrc256[index+1] >= 'A' && LicenseKeySrc256[index+1] <= 'F')
- {
- tmp2 = LicenseKeySrc256[index+1] - 'A'+10;
- }
- else if(LicenseKeySrc256[index+1] >= 'a' && LicenseKeySrc256[index+1] <= 'f')
- {
- tmp2 = LicenseKeySrc256[index+1] - 'a'+10;
- }
- else
- {
- return -1;
- //printf("error\r\n");
- }
- LicenseKeyDst128[i] = ((tmp&0x0f) << 4) | (tmp2&0x0f);
- }
- return 1;
- }
- int CheckLicense()
- {
- uint32_t *pUID = (uint32_t*)0x1FFF7A10;
- uint8_t GUID[12] = {0};
- uint32_t tmp = 0;
- uint8_t *pLicense = (uint8_t*)0x08060000;
- uint8_t LICENSE[128] = {0xE9,0x5F,0x60,0x69,0x92,0x01,0xDD,0x0F,0x98,0x42,0x57,0x15,
- 0x6B,0x64,0x69,0xBE,0x03,0x0A,0x6A,0x17,0x25,0x7C,0xDC,0xAC,
- 0xDF,0x10,0x04,0xBB,0x5C,0xE9,0xDA,0x70,0xD7,0xD9,0x22,0x8C,
- 0xE9,0xAA,0xD7,0x2F,0x09,0x86,0xCE,0xE2,0x52,0xAD,0x9B,0x08,
- 0x86,0x65,0xAF,0x52,0x55,0x38,0xC2,0xA9,0xD6,0x60,0x7B,0x36,
- 0x5F,0x7F,0x41,0xBD,0x73,0x8C,0x7B,0x2A,0x14,0x12,0xDA,0x72,
- 0x49,0x9C,0x11,0x96,0xD1,0x6B,0xA9,0x01,0x3B,0x6B,0x2E,0x1D,
- 0x1F,0x67,0x97,0x21,0xA0,0x77,0xDD,0x29,0x3D,0xC1,0xE7,0x8C,
- 0xC2,0x5F,0xB2,0x6B,0xA1,0x9F,0xAE,0x84,0x5F,0x6C,0x64,0x52,
- 0x71,0x0B,0x59,0x51,0x29,0x07,0x32,0x9B,0x0B,0x6D,0x1E,0xB9,
- 0x47,0x3B,0x73,0xBA,0x7F,0x14,0x95,0x87}; //从Flash中读出的带随机数填充的128字节License
- uint8_t CalLicense[16] = {0}; //计算得出的有效的16字节License
- MD5_CTX md5;
- int i;
- //获取GUID
- printf("GUID: ");
- for(i=0;i<3;i++)
- {
- tmp = *pUID;
- GUID[4*i+0] = tmp&0xff;
- GUID[4*i+1] = (tmp>>8)&0xff;
- GUID[4*i+2] = (tmp>>16)&0xff;
- GUID[4*i+3] = (tmp>>24)&0xff;
- pUID++;
- printf("%02x %02x %02x %02x ", GUID[4*i+0],GUID[4*i+1],GUID[4*i+2],GUID[4*i+3]);
- }
- printf("\n");
- // //获取License
- // printf("License: ");
- // for(i=0;i<128;i++)
- // {
- // LICENSE[i] = *pLicense;
- // pLicense++;
- // printf("%02x ", LICENSE[i]);
- // }
- // printf("\n" );
- //根据GUID计算License
- preHandle((uint32_t*)GUID, 0x33445566, 2); //预处理
- MD5_Init(&md5);
- MD5_Update(&md5,GUID,12);
- MD5_Final(CalLicense, &md5);
- printf("CalLicense: ");
- for(i=0;i<16;i++)
- {
- printf("%02x ", CalLicense[i]);
- }
- printf("\n");
- //比对License
- if(Authorize(CalLicense, LICENSE, 0) == 1) //授权通过
- {
- printf("\r\nAlready Auth Success\r\n");
- return 0;
- }
- else //授权不通过
- {
- printf("\n\n====================================================\n");
- printf("\n\tLicense is invalid, Please authorize! \n");
- printf("\n====================================================\n\n");
- return -1;
- }
- }
|