Skip to content

Instantly share code, notes, and snippets.

@denise-amiga
Created June 12, 2020 13:23
Show Gist options
  • Save denise-amiga/a36dc54f874ed5c3ca242c599e0ee209 to your computer and use it in GitHub Desktop.
Save denise-amiga/a36dc54f874ed5c3ca242c599e0ee209 to your computer and use it in GitHub Desktop.
asus encrypt dsl-n66u
//encrypt_asus
//Ren.B
#define ALIGN_SIZE 1024
#define DEFAULT_HEADER "DSL-N66U"
//must be the same structure in /apps/private/cfg_manager/utility.c
typedef struct romfile_header_s
{
char productName[16];
char keyWord[16]; //encryptRomfile
unsigned long fileLength;
unsigned int rand;
}romfile_header_t;
//must be the same keyword in /apps/private/cfg_manager/utility.c
#define KEYWORD "EnCrYpTRomFIle"
/**
** src : source file path, ex: /tmp/var/romfile.cfg
** dst : destination file path, ex: /tmp/var/romfile_encrypt.cfg
** productName : the encrypted romfile header, string type.
** Encrypt the src file to the dst file, the src file will not be changed.
**/
int encryptRomfile(char *src, char *dst, char *productName)
{
FILE *fp = NULL;
unsigned long count, filelen, i;
unsigned int rand = 0;
char *buffer = NULL;
int srcFD = -1;
int ret = 0;
romfile_header_t rhdr;
unlink(dst);
if ((fp = fopen(dst, "w")) == NULL) return -1;
if( ( srcFD = open(src, O_RDONLY) ) < 0 )
{
return -2;
}
count = readFileSize(src);
buffer = (char *) calloc( count, sizeof(char));
ret = read(srcFD, buffer, count);
close(srcFD);
if( ret < 0 )
{
free(buffer);
fclose(fp);
return -3;
}
memset(&rhdr, 0, sizeof(rhdr));
strcpy(rhdr.keyWord, KEYWORD);
rand = (get_rand() % 15) + 15; //rand will be 15~29
rhdr.rand = rand;
//currently ROMFILE_PADDING is not needed. We keep it just in case in the future.
#if defined(ROMFILE_PADDING)
filelen = count + (ALIGN_SIZE - (count%ALIGN_SIZE));
#else
filelen = count;
#endif
rhdr.fileLength = filelen;
if((!productName)||( strlen(productName) <= 0 ))
{
strncpy(rhdr.productName, DEFAULT_HEADER, sizeof(rhdr.productName) );
}
else
{
strncpy(rhdr.productName, productName, sizeof(rhdr.productName) );
rhdr.productName[sizeof(rhdr.productName)-1] = '\0';
}
//write header
fwrite(&rhdr, 1, sizeof(rhdr), fp);
//encrypt data
for (i = 0; i < count; i++)
{{
if (buffer[i] == 0x0)
buffer[i] = 0xfd + get_rand() % 3;
else
buffer[i] = (0xff - buffer[i] + rand) & 0xff;
}}
//write data
fwrite(buffer, 1, count, fp);
#if defined(ROMFILE_PADDING)
//paddings
for (i = count; i < filelen; i++)
{
temp = 0xfd + get_rand() % 3;
fwrite(&temp, 1, 1, fp);
}
#endif
fclose(fp);
free(buffer);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment