Commit 5a2f0128 authored by g3gg0's avatar g3gg0
Browse files

io_crypt: disable scratch ram due to trouble when shooting series, some code cleanups

--HG--
branch : unified
parent 67b0caf753ab
......@@ -153,7 +153,7 @@ static uint32_t crypt_lfsr64_encrypt(crypt_cipher_t *cipher_ctx, uint8_t *dst, u
return in_length;
}
/* either the pointers are 64 bit aligned now or all data is encrypted already. do 64 bit writes if possible. */
/* the pointers are 64 bit aligned now. do 64 bit writes if possible. */
if((offset % 8) == 0)
{
/* processing loop for 64 bit writes at even file offsets. due to compiler optimizations this loop is faster */
......@@ -225,7 +225,7 @@ static uint32_t crypt_lfsr64_decrypt(crypt_cipher_t *ctx, uint8_t *dst, uint8_t
/* set encryption blocksize for this context */
static void crypt_lfsr64_set_blocksize(crypt_cipher_t *crypt_ctx, uint32_t size)
{
lfsr64_ctx_t *ctx = crypt_ctx->priv;
lfsr64_ctx_t *ctx = (lfsr64_ctx_t *)crypt_ctx->priv;
ctx->blocksize = size;
}
......@@ -245,8 +245,7 @@ static void crypt_lfsr64_reset(crypt_cipher_t *crypt_ctx)
{
lfsr64_ctx_t *ctx = crypt_ctx->priv;
/* to mark LFSR state as invalid */
ctx->current_block = 0xFFFFFFFF;
ctx->current_block = 0;
ctx->lfsr_state = 0;
ctx->blocksize = crypt_lfsr64_blocksize;
......
......@@ -532,6 +532,12 @@ static uint32_t crypt_rsa_encrypt(crypt_cipher_t *crypt_ctx, uint8_t *dst, uint8
uint32_t new_len = crypt_rsa_crypt(dst, src, length, &ctx->pub_key);
if(new_len > length)
{
trace_write(iocrypt_trace_ctx, "crypt_rsa_decrypt: buffer overflow %d vs. %d", new_len, length);
return 0;
}
return new_len;
}
......
......@@ -28,6 +28,7 @@ static iodev_handlers_t hook_iodev;
uint32_t iocrypt_trace_ctx = TRACE_ERROR;
static uint32_t iocrypt_scratch_used = 0;
static uint8_t *iocrypt_scratch = NULL;
static uint64_t iocrypt_key = 0;
......@@ -35,6 +36,7 @@ static crypt_cipher_t iocrypt_rsa_ctx;
static fd_map_t iocrypt_files[32];
static struct semaphore *iocrypt_password_sem = 0;
static struct semaphore *iocrypt_scratch_sem = 0;
static char iocrypt_ime_text[128];
/* those are model specific */
......@@ -375,7 +377,6 @@ static uint32_t hook_iodev_ReadFile(uint32_t fd, uint8_t *buf, uint32_t length)
/* when there is some encryption active, handle file offset */
if(iocrypt_files[fd].crypt_ctx.priv)
{
trace_write(iocrypt_trace_ctx, "iodev_ReadFile: apply offset %d", iocrypt_files[fd].header_size);
iodev_SetPosition(fd, iodev_GetPosition(fd) + iocrypt_files[fd].header_size);
}
......@@ -392,7 +393,6 @@ static uint32_t hook_iodev_ReadFile(uint32_t fd, uint8_t *buf, uint32_t length)
iodev_SetPosition(fd, fd_pos + ret);
}
trace_write(iocrypt_trace_ctx, "iodev_ReadFile: undo offset %d", iocrypt_files[fd].header_size);
iodev_SetPosition(fd, iodev_GetPosition(fd) - iocrypt_files[fd].header_size);
}
......@@ -428,9 +428,15 @@ static uint32_t hook_iodev_WriteFile(uint32_t fd, uint8_t *buf, uint32_t length)
/* if there is no buffer or the size is too big, do expensive in-place encryption */
if(iocrypt_scratch && (length <= CRYPT_SCRATCH_SIZE))
{
/* update buffer to write from */
work_ptr = &iocrypt_scratch[misalign];
trace_write(iocrypt_trace_ctx, " ->> double buffered");
take_semaphore(iocrypt_scratch_sem, 1000);
if(!iocrypt_scratch_used)
{
/* update buffer to write from */
work_ptr = &iocrypt_scratch[misalign];
trace_write(iocrypt_trace_ctx, " ->> double buffered");
iocrypt_scratch_used = 1;
}
give_semaphore(iocrypt_scratch_sem);
}
trace_write(iocrypt_trace_ctx, " ->> ENCRYPT");
......@@ -438,13 +444,11 @@ static uint32_t hook_iodev_WriteFile(uint32_t fd, uint8_t *buf, uint32_t length)
trace_write(iocrypt_trace_ctx, " ->> DONE");
/* when there is some encryption active, handle file offset */
trace_write(iocrypt_trace_ctx, "iodev_WriteFile: apply offset %d", iocrypt_files[fd].header_size);
iodev_SetPosition(fd, iodev_GetPosition(fd) + iocrypt_files[fd].header_size);
ret = orig_iodev->WriteFile(fd, work_ptr, length);
/* when there is some encryption active, handle file offset */
trace_write(iocrypt_trace_ctx, "iodev_WriteFile: undo offset %d", iocrypt_files[fd].header_size);
iodev_SetPosition(fd, iodev_GetPosition(fd) - iocrypt_files[fd].header_size);
trace_write(iocrypt_trace_ctx, "iodev_WriteFile post(0x%08X, 0x%08X) -> fd = %d, fd_pos = 0x%08X, fd_pos (now) = 0x%08X", buf, length, fd, fd_pos, iodev_GetPosition(fd));
......@@ -456,6 +460,12 @@ static uint32_t hook_iodev_WriteFile(uint32_t fd, uint8_t *buf, uint32_t length)
iocrypt_files[fd].crypt_ctx.decrypt(&iocrypt_files[fd].crypt_ctx, buf, buf, length, fd_pos);
trace_write(iocrypt_trace_ctx, " ->> DONE");
}
else
{
take_semaphore(iocrypt_scratch_sem, 1000);
iocrypt_scratch_used = 0;
give_semaphore(iocrypt_scratch_sem);
}
return ret;
}
......@@ -734,33 +744,51 @@ static struct menu_entry iocrypt_menus[] =
static unsigned int iocrypt_init()
{
/* for debugging */
if(1)
{
char filename[32];
snprintf(filename, sizeof(filename), "%sIO_CRYPT.TXT", module_card_drive);
iocrypt_trace_ctx = trace_start("debug", filename);
trace_set_flushrate(iocrypt_trace_ctx, 1000);
trace_format(iocrypt_trace_ctx, TRACE_FMT_TIME_REL | TRACE_FMT_COMMENT, ' ');
trace_write(iocrypt_trace_ctx, "io_crypt: Starting trace");
}
if(streq(camera_model_short, "600D"))
{
trace_write(iocrypt_trace_ctx, "io_crypt: Detected 600D");
iodev_table = 0x1E684;
iodev_ctx = 0x7EB08;
iodev_ctx_size = 0x18;
}
else if(streq(camera_model_short, "7D"))
{
trace_write(iocrypt_trace_ctx, "io_crypt: Detected 7D");
iodev_table = 0x2D3B8;
iodev_ctx = 0x85510;
iodev_ctx_size = 0x18;
}
else if(streq(camera_model_short, "60D"))
{
trace_write(iocrypt_trace_ctx, "io_crypt: Detected 60D");
iodev_table = 0x3E2BC;
iodev_ctx = 0x5CB38;
iodev_ctx_size = 0x18;
}
else if(streq(camera_model_short, "5D3"))
{
trace_write(iocrypt_trace_ctx, "io_crypt: Detected 5D3");
iodev_table = 0x44FA8;
iodev_ctx = 0x67140;
iodev_ctx_size = 0x20;
}
else if(streq(camera_model_short, "650D"))
{
/* not verified */
trace_write(iocrypt_trace_ctx, "io_crypt: Detected 650D");
iodev_table = 0x54060;
iodev_ctx = 0x7C278;
iodev_ctx_size = 0x20;
......@@ -772,24 +800,17 @@ static unsigned int iocrypt_init()
}
iocrypt_password_sem = create_named_semaphore("iocrypt_pw", 1);
/* for debugging */
if(1)
{
iocrypt_trace_ctx = trace_start("debug", "B:/IO_CRYPT.TXT");
trace_set_flushrate(iocrypt_trace_ctx, 1000);
trace_format(iocrypt_trace_ctx, TRACE_FMT_TIME_REL | TRACE_FMT_COMMENT, ' ');
trace_write(iocrypt_trace_ctx, "io_crypt: Starting trace");
}
iocrypt_scratch_sem = create_named_semaphore("iocrypt_scratch", 1);
/* ask for the initial password */
if(iocrypt_ask_pass && (iocrypt_mode == CRYPT_MODE_SYMMETRIC || iocrypt_mode == CRYPT_MODE_BACKGROUND_SYM))
{
trace_write(iocrypt_trace_ctx, "io_crypt: Asking for password");
iocrypt_enter_pw();
}
/* this memory is used for buffering encryption, so we dont have to undo the changes in memory */
iocrypt_scratch = shoot_malloc(CRYPT_SCRATCH_SIZE);
//iocrypt_scratch = shoot_malloc(CRYPT_SCRATCH_SIZE);
/* now patch the iodev handlers */
orig_iodev = (iodev_handlers_t *)MEM(iodev_table);
......
......@@ -84,13 +84,6 @@ int main(int argc, char *argv[])
return -1;
}
FILE *out_file = fopen(out_filename, "w");
if(!in_file)
{
printf("Could not open '%s'\n", out_filename);
return -1;
}
char *buffer = malloc(BLOCKSIZE);
......@@ -199,11 +192,15 @@ int main(int argc, char *argv[])
}
/* setup cipher with that hash */
uint32_t first = 1;
FILE *out_file = NULL;
uint32_t file_offset = 0;
crypt_cipher_t crypt_ctx;
crypt_lfsr64_init(&crypt_ctx, key);
crypt_ctx.set_blocksize(&crypt_ctx, lfsr_blocksize);
while(!feof(in_file))
{
int ret = fread(buffer, 1, BLOCKSIZE, in_file);
......@@ -211,6 +208,36 @@ int main(int argc, char *argv[])
if(ret > 0)
{
crypt_ctx.decrypt(&crypt_ctx, (uint8_t *)buffer, (uint8_t *)buffer, ret, file_offset);
/* try to detect file type */
if(first)
{
first = 0;
if(!memcmp(buffer, jpg_magic, 4))
{
printf("File type: JPEG (decrypted)\n");
}
else if(!memcmp(buffer, cr2_magic, 4))
{
printf("File type: CR2 (decrypted)\n");
}
else
{
printf("File type: unknown. invalid key?\n");
fclose(in_file);
free(out_filename);
return 0;
}
out_file = fopen(out_filename, "w");
if(!out_file)
{
printf("Could not open '%s'\n", out_filename);
return -1;
}
}
fwrite(buffer, 1, ret, out_file);
file_offset += ret;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment