ZenUtils:

* add FRESC encryption
 * clean up code a bit


git-svn-id: svn://svn.rockbox.org/rockbox/trunk@18345 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Maurus Cuelenaere 2008-08-26 13:21:52 +00:00
parent af5f8665e8
commit bea2ab2398
3 changed files with 52 additions and 26 deletions

View file

@ -115,18 +115,18 @@ bool zen::crypt_firmware(const char* key, byte* buffer, size_t len)
return true;
#else
// Determine if the key length is dword aligned.
/* Determine if the key length is dword aligned. */
int keylen = strlen(key);
int keylen_rem = keylen % sizeof(dword);
// Determine how many times the key must be repeated to be dword aligned.
/* Determine how many times the key must be repeated to be dword aligned. */
int keycycle = keylen_rem ? (sizeof(dword) / keylen_rem) : 1;
int keyscount = (keylen * keycycle) / sizeof(dword);
// Allocate a buffer to hold the key as an array of dwords.
/* Allocate a buffer to hold the key as an array of dwords. */
dword* keys = new dword[keyscount];
// Copy the key into the key array, whilst mutating it.
/* Copy the key into the key array, whilst mutating it. */
for (int i = 0; i < keyscount; i++)
{
dword val;
@ -145,19 +145,19 @@ bool zen::crypt_firmware(const char* key, byte* buffer, size_t len)
keys[i] = (val - 0x01010101) | 0x80808080;
}
// Determine the number of dwords in the buffer.
/* Determine the number of dwords in the buffer. */
int len_div = len / sizeof(dword);
// Decrypt all dwords of the buffer.
/* Decrypt all dwords of the buffer. */
for (int i = 0; i < len_div; i++)
{
((dword*)buffer)[i] ^= keys[i % keyscount];
}
// Determine the remaining number of bytes in the buffer.
/* Determine the remaining number of bytes in the buffer. */
int len_rem = len % sizeof(dword);
// Decrypt the remaining number of bytes in the buffer.
/* Decrypt the remaining number of bytes in the buffer. */
for (int i = len_div * sizeof(dword); i < len; i++)
{
buffer[i] ^= ((key[i % keylen] - 0x01) | 0x80);

View file

@ -1,4 +1,6 @@
ADD_EXECUTABLE(zen_crypt main.cpp)
SET_SOURCE_FILES_PROPERTIES(main.cpp PROPERTIES COMPILE_FLAGS -Wno-multichar)
TARGET_LINK_LIBRARIES(zen_crypt shared)
TARGET_LINK_LIBRARIES(zen_crypt beecrypt)

View file

@ -46,12 +46,12 @@ enum mode_t
struct player_info_t
{
const char* name;
const char* null_key; // HMAC-SHA1 key
const char* fresc_key; // BlowFish key
const char* tl_key; // BlowFish key
const char* null_key; /* HMAC-SHA1 key */
const char* fresc_key; /* BlowFish key */
const char* tl_key; /* BlowFish key */
bool big_endian;
};
}; //namespace
}; /* namespace */
static const char VERSION[] = "0.2";
@ -210,7 +210,7 @@ bool sign(shared::bytes& data, player_info_t* pi, const std::string& file,
if (index)
{
if (verbose)
std::cout << "[*] Found NULL signature at: "
std::cout << "[*] Found NULL signature at: 0x"
<< std::hex << index << std::endl;
if (verbose)
@ -317,7 +317,7 @@ bool verify(shared::bytes& data, player_info_t* pi, bool verbose)
return false;
}
if (verbose)
std::cout << "[*] Found NULL signature at: "
std::cout << "[*] Found NULL signature at: 0x"
<< std::hex << index << std::endl;
if (verbose)
@ -382,8 +382,26 @@ bool encrypt(shared::bytes& data, int mode, player_info_t* pi,
}
else if (mode == mode_fresc)
{
std::cerr << "FRESC mode is not supported." << std::endl;
return false;
if (verbose)
std::cout << "[*] Encrypting input file..." << std::endl;
dword iv[2] = {shared::swap(data.size()), 0};
if (!zen::bf_cbc_encrypt((const byte*)pi->fresc_key,
strlen(pi->fresc_key)+1, &data[0],
data.size(), (const byte*)iv))
{
std::cerr << "Failed to encrypt the input file." << std::endl;
return false;
}
if (verbose)
std::cout << "[*] Writing file data..." << std::endl;
if (!shared::write_file(file, data, true))
{
std::cerr << "Failed to save the output file." << std::endl;
return false;
}
}
else if (mode == mode_tl)
{
@ -413,7 +431,7 @@ bool encrypt(shared::bytes& data, int mode, player_info_t* pi,
if (!zen::bf_cbc_encrypt((const byte*)pi->tl_key, strlen(pi->tl_key)+1,
&outbuf[0], len, (const byte*)iv))
{
std::cerr << "Failed to decrypt the input file." << std::endl;
std::cerr << "Failed to encrypt the input file." << std::endl;
return false;
}
@ -540,9 +558,11 @@ bool decrypt(shared::bytes& data, int mode, player_info_t* pi,
int process_arguments(int argc, char*argv[])
{
//--------------------------------------------------------------------
// Parse input variables.
//--------------------------------------------------------------------
/*
--------------------------------------------------------------------
Parse input variables.
--------------------------------------------------------------------
*/
GetPot cl(argc, argv);
if (cl.size() == 1 || cl.search(2, "-h", "--help"))
@ -636,9 +656,11 @@ int process_arguments(int argc, char*argv[])
pi->big_endian = big_endian;
//--------------------------------------------------------------------
// Read the input file.
//--------------------------------------------------------------------
/*
--------------------------------------------------------------------
Read the input file.
--------------------------------------------------------------------
*/
if (verbose)
std::cout << "[*] Reading input file..." << std::endl;
@ -651,9 +673,11 @@ int process_arguments(int argc, char*argv[])
}
//--------------------------------------------------------------------
// Process the input file.
//--------------------------------------------------------------------
/*
--------------------------------------------------------------------
Process the input file.
--------------------------------------------------------------------
*/
switch (command)
{