Prepare new maemo release
[maemo-rb.git] / utils / nwztools / upgtools / fwp.c
blobc300f42f345e65ae48c93546a5f1a464c894383b
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2012 Amaury Pouly
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include <stdio.h>
22 #include "fwp.h"
23 #include "misc.h"
24 #include "mg.h"
25 #include <string.h>
27 int fwp_read(void *in, int size, void *out, uint8_t *key)
29 return mg_decrypt_fw(in, size, out, key);
32 int fwp_write(void *in, int size, void *out, uint8_t *key)
34 return mg_encrypt_fw(in, size, out, key);
37 static uint8_t g_key[NWZ_KEY_SIZE];
39 void fwp_setkey(char key[NWZ_KEY_SIZE])
41 memcpy(g_key, key, NWZ_KEY_SIZE);
44 int fwp_crypt(void *buf, int size, int mode)
46 while(size >= NWZ_KEY_SIZE)
48 if(mode)
49 mg_decrypt_pass(buf, NWZ_KEY_SIZE, buf, g_key);
50 else
51 mg_encrypt_pass(buf, NWZ_KEY_SIZE, buf, g_key);
52 buf += NWZ_KEY_SIZE;
53 size -= NWZ_KEY_SIZE;
55 if(size != 0)
57 cprintf(GREY, "Cannot fwp_crypt non-multiple of 8!\n");
58 return -1;
60 return 0;