io_decrypt.c 5.98 KB
Newer Older
1
2
3
4
5

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
6
#include <inttypes.h>
7
8
9

#include "io_crypt.h"
#include "crypt_lfsr64.h"
10
#include "crypt_rsa.h"
11
12
13
14
15
16
#include "hash_password.h"


#define BLOCKSIZE (8 * 1024)


17
18
19
20
21
22
static const uint8_t cr2_magic[] = "\x49\x49\x2A\x00";
static const uint8_t jpg_magic[] = "\xff\xd8\xff\xe1";
static const uint8_t rsa_magic[] = "\xff\xd8\xff\xd8";
static const uint8_t lfsr_magic[] = "\xff\xd8\xff\x8d";


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
static uint32_t lfsr113[] = { 0x00009821, 0x00098722, 0x00986332, 0x961FEFA7 };

void rand_fill(uint32_t *buffer, uint32_t length)
{
    for(uint32_t pos = 0; pos < length; pos++)
    {
        lfsr113[0] = ((lfsr113[0] & 0xFFFFFFFE) << 18) ^ (((lfsr113[0] <<  6) ^ lfsr113[0]) >> 13);
        lfsr113[1] = ((lfsr113[1] & 0xFFFFFFF8) <<  2) ^ (((lfsr113[1] <<  2) ^ lfsr113[1]) >> 27);
        lfsr113[2] = ((lfsr113[2] & 0xFFFFFFF0) <<  7) ^ (((lfsr113[2] << 13) ^ lfsr113[2]) >> 21);
        lfsr113[3] = ((lfsr113[3] & 0xFFFFFF80) << 13) ^ (((lfsr113[3] <<  3) ^ lfsr113[3]) >> 12);

        buffer[pos] = lfsr113[0] ^ lfsr113[1] ^ lfsr113[2] ^ lfsr113[3];
    }
}

void rand_seed(uint32_t seed)
{
    uint32_t tmp = 0;

    /* apply seed to internal states and do a few rounds to equally distribute seed bits */
    for(int loops = 0; loops < 128; loops++)
    {
        lfsr113[loops%4] ^= seed;
        rand_fill(&tmp, 1);
    }
}

50
static crypt_cipher_t iocrypt_rsa_ctx;
51
52
int main(int argc, char *argv[])
{
53
    if(argc < 3)
54
    {
55
        printf("Usage: '%s <infile> <outfile> [password]\n", argv[0]);
56
57
58
        return -1;
    }
    
59
60
61
    uint64_t key = 0;
    uint32_t lfsr_blocksize = 0x00020000;
    
62
63
64
    char *in_filename = argv[1];
    char *out_filename = argv[2];
    
65
66
67
68
69
70
    /* password is optional */
    if(argc >= 4)
    {
        /* hash the password */
        hash_password(argv[3], &key);
    }
71
72
73
74
75
76
    
    /* open files */
    FILE *in_file = fopen(in_filename, "rb");
    if(!in_file)
    {
        printf("Could not open '%s'\n", in_filename);
77
        return -1;
78
79
80
81
82
83
    } 
    
    FILE *out_file = fopen(out_filename, "w");
    if(!in_file)
    {
        printf("Could not open '%s'\n", out_filename);
84
        return -1;
85
86
87
    }
    
    char *buffer = malloc(BLOCKSIZE);
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
    
    
    /* try to detect file type */
    if(fread(buffer, 1, 4, in_file) != 4)
    {
        printf("Could not read '%s'\n", in_filename);
        return -1;
    }
    
    if(!memcmp(buffer, jpg_magic, 4))
    {
        printf("File type: JPEG (plain)\n");
        return 0;
    }
    else if(!memcmp(buffer, cr2_magic, 4))
    {
        printf("File type: CR2 (plain)\n");
        return 0;
    }
    else if(!memcmp(buffer, lfsr_magic, 4))
    {
        printf("File type: LFSR64\n");
        
        if(!key)
        {
            printf("Error: Please specify a password\n");
            return -2;
        }
        if(fread(&lfsr_blocksize, 1, sizeof(uint32_t), in_file) != sizeof(uint32_t))
        {
            printf("Could not read '%s'\n", in_filename);
            return -1;
        }
        
        fseek(in_file, 0x200, SEEK_SET);
    }
    else if(!memcmp(buffer, rsa_magic, 4))
    {
        printf("File type: RSA+LFSR64\n");
        
        crypt_rsa_init(&iocrypt_rsa_ctx);

        uint32_t encrypted_size = 0;
        
        if(fread(&encrypted_size, 1, sizeof(uint32_t), in_file) != sizeof(uint32_t))
        {
            printf("Could not read '%s'\n", in_filename);
            return -1;
        }
        
        if(fread(&lfsr_blocksize, 1, sizeof(uint32_t), in_file) != sizeof(uint32_t))
        {
            printf("Could not read '%s'\n", in_filename);
            return -1;
        }

        if(!encrypted_size || encrypted_size > 32768 * 4)
        {
            printf("encrypted_size: %d\n", encrypted_size);
            return -1;
        }
        
        if(!lfsr_blocksize || lfsr_blocksize > 0x10000000)
        {
            printf("lfsr_blocksize: %d\n", lfsr_blocksize);
            return -1;
        }
        
        char *encrypted = malloc(encrypted_size);
        if(fread(encrypted, 1, encrypted_size, in_file) != encrypted_size)
        {
            printf("Could not read '%s'\n", in_filename);
            return -1;
        }
        uint32_t decrypted_size = iocrypt_rsa_ctx.decrypt(&iocrypt_rsa_ctx, encrypted, encrypted, encrypted_size, 0);

        if(!decrypted_size || decrypted_size > encrypted_size)
        {
            printf("decrypted_size: %d. maybe key mismatch?\n", decrypted_size);
            return -1;
        }
        
        /* that decrypted data is the file crypt key */
        memcpy(&key, encrypted, sizeof(uint64_t));
        
        free(encrypted);
    
        uint32_t used_header = 4 + sizeof(uint32_t) + encrypted_size;
        uint32_t aligned_header = (used_header + 0x1FF) & ~0x1FF;
        
        /* now skip that header and continue with LFSR113 decryption */
        fseek(in_file, aligned_header, SEEK_SET);
    }
    else
    {
        if(key)
        {
            printf("File type: unknown. assuming LFSR64\n");
        }
        else
        {
            printf("File type: unknown. assuming LFSR64. Please specify a password\n");
            return -2;
        }
    }
    
    /* setup cipher with that hash */
195
    uint32_t file_offset = 0;
196
197
198
    crypt_cipher_t crypt_ctx;
    crypt_lfsr64_init(&crypt_ctx, key);
    crypt_ctx.set_blocksize(&crypt_ctx, lfsr_blocksize);
199
200
201
202
203
204
205
    
    while(!feof(in_file))
    {
        int ret = fread(buffer, 1, BLOCKSIZE, in_file);
        
        if(ret > 0)
        {
206
            crypt_ctx.decrypt(&crypt_ctx, buffer, buffer, ret, file_offset);
207
208
209
            fwrite(buffer, 1, ret, out_file);
            file_offset += ret;
        }
210
211
212
213
214
        if(ret < 0)
        {
            printf("Could not read '%s'\n", in_filename);
            break;
        }
215
216
217
218
219
    }
    
    fclose(in_file);
    fclose(out_file);
}