summaryrefslogtreecommitdiffstats
path: root/utils/nwztools/upgtools/mg.cpp
blob: df2dbbfd4fa017344fc1b0d307e61bf7a37787b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2012 Amaury Pouly
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "mg.h"

/* on Windows, we use Cryptography API, on other platforms we rely on crypto++ */
#if defined(_WIN32) || defined(__WIN32__)
#include <windows.h>
#include <wincrypt.h>
#include <stdio.h>

static HCRYPTPROV g_hCryptProv;

typedef struct
{
    BLOBHEADER hdr;
    DWORD      dwKeySize;
    BYTE       rgbKeyData[];
} PLAINTEXTKEY;

static bool check_context()
{
    if(g_hCryptProv)
        return true;
    return CryptAcquireContext(&g_hCryptProv, NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES, CRYPT_VERIFYCONTEXT);
}

static HCRYPTKEY import_key(ALG_ID algid, DWORD mode, const uint8_t *key, int key_size, const uint8_t *iv)
{
    if(!check_context())
        return 0;
    DWORD blob_size = sizeof(PLAINTEXTKEY) + key_size;
    PLAINTEXTKEY *key_blob = (PLAINTEXTKEY *)malloc(blob_size);
    key_blob->hdr.bType = PLAINTEXTKEYBLOB;
    key_blob->hdr.bVersion = CUR_BLOB_VERSION;
    key_blob->hdr.reserved = 0;
    key_blob->hdr.aiKeyAlg = algid;
    key_blob->dwKeySize = key_size;
    memcpy(key_blob->rgbKeyData, key, key_size);
    HCRYPTKEY hKey;
    if(!CryptImportKey(g_hCryptProv, (const BYTE *)key_blob, blob_size, 0, 0, &hKey))
        return 0;
    CryptSetKeyParam(hKey, KP_MODE, (const BYTE *)&mode, 0);
    if(iv)
        CryptSetKeyParam(hKey, KP_IV, (const BYTE *)iv, 0);
    return hKey;
}

void MD5_CalculateDigest(void *digest, const void *input, size_t length)
{
    if(!check_context())
        return;
    HCRYPTHASH hHash;
    if(!CryptCreateHash(g_hCryptProv, CALG_MD5, 0, 0, &hHash))
        return;
    if(!CryptHashData(hHash, (const BYTE *)input, length, 0))
        return;
    DWORD dwSize = 16;
    if(!CryptGetHashParam(hHash, HP_HASHVAL, (BYTE *)digest, &dwSize, 0))
        return;
    CryptDestroyHash(hHash);
}

void mg_decrypt_fw(void *in, int size, void *out, uint8_t *key)
{
    if(!check_context() || (size % 8) != 0)
        abort(); /* size must be a multiple of 8 */
    HCRYPTKEY hKey = import_key(CALG_DES, CRYPT_MODE_ECB, key, 8, NULL);
    memcpy(out, in, size);
    DWORD dwSize = size;
    CryptDecrypt(hKey, 0, FALSE, 0, (BYTE *)out, &dwSize);
    CryptDestroyKey(hKey);
}

static HCRYPTKEY g_des_ecb_dec_key;

void des_ecb_dec_set_key(const uint8_t key[8])
{
    if(g_des_ecb_dec_key)
        CryptDestroyKey(g_des_ecb_dec_key);
    g_des_ecb_dec_key = import_key(CALG_DES, CRYPT_MODE_ECB, key, 8, NULL);
}

void des_ecb_dec(void *in, int size, void *out)
{
    if(size % 8)
        abort(); /* size must be a multiple of 8 */
    memcpy(out, in, size);
    DWORD dwSize = size;
    CryptDecrypt(g_des_ecb_dec_key, 0, FALSE, 0, (BYTE *)out, &dwSize);
}

static HCRYPTKEY g_des_ecb_enc_key;

void des_ecb_enc_set_key(const uint8_t key[8])
{
    if(g_des_ecb_enc_key)
        CryptDestroyKey(g_des_ecb_enc_key);
    g_des_ecb_enc_key = import_key(CALG_DES, CRYPT_MODE_ECB, key, 8, NULL);
}

void des_ecb_enc(void *in, int size, void *out)
{
    if(size % 8)
        abort(); /* size must be a multiple of 8 */
    memcpy(out, in, size);
    DWORD dwSize = size;
    CryptEncrypt(g_des_ecb_enc_key, 0, FALSE, 0, (BYTE *)out, &dwSize, dwSize);
}

static HCRYPTKEY g_aes_ecb_dec_key;

void aes_ecb_dec_set_key(const uint8_t key[16])
{
    if(g_aes_ecb_dec_key)
        CryptDestroyKey(g_aes_ecb_dec_key);
    g_aes_ecb_dec_key = import_key(CALG_AES_128, CRYPT_MODE_ECB, key, 16, NULL);
}

void aes_ecb_dec(void *in, int size, void *out)
{
    if(size % 16)
        abort(); /* size must be a multiple of 16 */
    memcpy(out, in, size);
    DWORD dwSize = size;
    CryptDecrypt(g_aes_ecb_dec_key, 0, FALSE, 0, (BYTE *)out, &dwSize);
}

static HCRYPTKEY g_aes_ecb_enc_key;

void aes_ecb_enc_set_key(const uint8_t key[16])
{
    if(g_aes_ecb_enc_key)
        CryptDestroyKey(g_aes_ecb_enc_key);
    g_aes_ecb_enc_key = import_key(CALG_AES_128, CRYPT_MODE_ECB, key, 16, NULL);
}

void aes_ecb_enc(void *in, int size, void *out)
{
    if(size % 16)
        abort(); /* size must be a multiple of 16 */
    memcpy(out, in, size);
    DWORD dwSize = size;
    CryptEncrypt(g_aes_ecb_enc_key, 0, FALSE, 0, (BYTE *)out, &dwSize, dwSize);
}

static HCRYPTKEY g_aes_cbc_dec_key;

void aes_cbc_dec_set_key_iv(const uint8_t key[16], const uint8_t iv[16])
{
    if(g_aes_cbc_dec_key)
        CryptDestroyKey(g_aes_cbc_dec_key);
    g_aes_cbc_dec_key = import_key(CALG_AES_128, CRYPT_MODE_CBC, key, 16, iv);
}

void aes_cbc_dec(void *in, int size, void *out)
{
    if(size % 16)
        abort(); /* size must be a multiple of 16 */
    memcpy(out, in, size);
    DWORD dwSize = size;
    CryptDecrypt(g_aes_cbc_dec_key, 0, FALSE, 0, (BYTE *)out, &dwSize);
}

static HCRYPTKEY g_aes_cbc_enc_key;

void aes_cbc_enc_set_key_iv(const uint8_t key[16], const uint8_t iv[16])
{
    if(g_aes_cbc_enc_key)
        CryptDestroyKey(g_aes_cbc_enc_key);
    g_aes_cbc_enc_key = import_key(CALG_AES_128, CRYPT_MODE_CBC, key, 16, iv);
}

void aes_cbc_enc(void *in, int size, void *out)
{
    if(size % 16)
        abort(); /* size must be a multiple of 16 */
    memcpy(out, in, size);
    DWORD dwSize = size;
    CryptEncrypt(g_aes_cbc_enc_key, 0, FALSE, 0, (BYTE *)out, &dwSize, dwSize);
}


#else /* Not on Windows */
/* MD5 is considered insecure by crypto++ */
#define CRYPTOPP_ENABLE_NAMESPACE_WEAK  1
#include <cryptopp/cryptlib.h>
#include <cryptopp/modes.h>
#include <cryptopp/des.h>
#include <cryptopp/aes.h>
#include <cryptopp/md5.h>

using namespace CryptoPP;
using namespace CryptoPP::Weak;

void MD5_CalculateDigest(void *digest, const void *input, size_t length)
{
    MD5().CalculateDigest((byte *)digest, (const byte *)input, length);
}

void mg_decrypt_fw(void *in, int size, void *out, uint8_t *key)
{
    ECB_Mode< DES >::Decryption dec;
    if(size % 8)
        abort(); /* size must be a multiple of 8 */
    dec.SetKey(key, 8);
    dec.ProcessData((byte*)out, (byte*)in, size);
}

static ECB_Mode< DES >::Decryption g_des_ecb_dec;

void des_ecb_dec_set_key(const uint8_t key[8])
{
    g_des_ecb_dec.SetKey(key, 8);
}

void des_ecb_dec(void *in, int size, void *out)
{
    if(size % 8)
        abort(); /* size must be a multiple of 8 */
    g_des_ecb_dec.ProcessData((byte*)out, (byte*)in, size);
}

static ECB_Mode< DES >::Encryption g_des_ecb_enc;

void des_ecb_enc_set_key(const uint8_t key[8])
{
    g_des_ecb_enc.SetKey(key, 8);
}

void des_ecb_enc(void *in, int size, void *out)
{
    if(size % 8)
        abort(); /* size must be a multiple of 8 */
    g_des_ecb_enc.ProcessData((byte*)out, (byte*)in, size);
}

static ECB_Mode< AES >::Decryption g_aes_ecb_dec;

void aes_ecb_dec_set_key(const uint8_t key[16])
{
    g_aes_ecb_dec.SetKey(key, 16);
}

void aes_ecb_dec(void *in, int size, void *out)
{
    if(size % 16)
        abort(); /* size must be a multiple of 16 */
    g_aes_ecb_dec.ProcessData((byte*)out, (byte*)in, size);
}

static ECB_Mode< AES >::Encryption g_aes_ecb_enc;

void aes_ecb_enc_set_key(const uint8_t key[16])
{
    g_aes_ecb_enc.SetKey(key, 16);
}

void aes_ecb_enc(void *in, int size, void *out)
{
    if(size % 16)
        abort(); /* size must be a multiple of 16 */
    g_aes_ecb_enc.ProcessData((byte*)out, (byte*)in, size);
}

static CBC_Mode< AES >::Decryption g_aes_cbc_dec;

void aes_cbc_dec_set_key_iv(const uint8_t key[16], const uint8_t iv[16])
{
    g_aes_cbc_dec.SetKeyWithIV(key, 16, iv);
}

void aes_cbc_dec(void *in, int size, void *out)
{
    if(size % 16)
        abort(); /* size must be a multiple of 16 */
    g_aes_cbc_dec.ProcessData((byte*)out, (byte*)in, size);
}

static CBC_Mode< AES >::Encryption g_aes_cbc_enc;

void aes_cbc_enc_set_key_iv(const uint8_t key[16], const uint8_t iv[16])
{
    g_aes_cbc_enc.SetKeyWithIV(key, 16, iv);
}

void aes_cbc_enc(void *in, int size, void *out)
{
    if(size % 16)
        abort(); /* size must be a multiple of 16 */
    g_aes_cbc_enc.ProcessData((byte*)out, (byte*)in, size);
}
#endif