summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAmaury Pouly <amaury.pouly@gmail.com>2017-01-04 16:55:53 +0100
committerAmaury Pouly <amaury.pouly@gmail.com>2017-01-04 17:05:15 +0100
commitdbeb6db1b55a50dedf17e7d78ddb6fe9eebc2a63 (patch)
tree29118847ebd2328095bb9f31fe7208c0a4bb6052
parent92ecbd5fb8a7c8e939b1b4dde82cc6c9ba9d41af (diff)
downloadrockbox-dbeb6db1b55a50dedf17e7d78ddb6fe9eebc2a63.tar.gz
rockbox-dbeb6db1b55a50dedf17e7d78ddb6fe9eebc2a63.zip
nwztools: cleanup crypto, switch MD5 to Crypto++
We already use Crypto++ for DES anyway, and using OpenSSL is not great because of its incompatible licence. Change-Id: I78771b84c1708795a0c0c30afa5bdfe4885dea4e
-rw-r--r--utils/nwztools/upgtools/Makefile8
-rw-r--r--utils/nwztools/upgtools/fwp.c16
-rw-r--r--utils/nwztools/upgtools/fwp.h8
-rw-r--r--utils/nwztools/upgtools/md5.cpp31
-rw-r--r--utils/nwztools/upgtools/md5.h37
-rw-r--r--utils/nwztools/upgtools/mg.cpp26
-rw-r--r--utils/nwztools/upgtools/mg.h11
-rw-r--r--utils/nwztools/upgtools/upg.c22
-rw-r--r--utils/nwztools/upgtools/upgtool.c2
9 files changed, 108 insertions, 53 deletions
diff --git a/utils/nwztools/upgtools/Makefile b/utils/nwztools/upgtools/Makefile
index 1030b1b849..57525c1770 100644
--- a/utils/nwztools/upgtools/Makefile
+++ b/utils/nwztools/upgtools/Makefile
@@ -3,9 +3,9 @@ CC=gcc
3CXX=g++ 3CXX=g++
4LD=g++ 4LD=g++
5PROFILE= 5PROFILE=
6CFLAGS=-g $(PROFILE) -std=c99 -W -Wall $(DEFINES) `pkg-config --cflags openssl` `pkg-config --cflags libcrypto++` 6CFLAGS=-g $(PROFILE) -std=c99 -W -Wall $(DEFINES) `pkg-config --cflags libcrypto++`
7CXXFLAGS=-g $(PROFILE) -W -Wall $(DEFINES) `pkg-config --cflags openssl` `pkg-config --cflags libcrypto++` 7CXXFLAGS=-g $(PROFILE) -W -Wall $(DEFINES) `pkg-config --cflags libcrypto++`
8LDFLAGS=$(PROFILE) `pkg-config --libs openssl` `pkg-config --libs libcrypto++` -lcrypt -lpthread 8LDFLAGS=$(PROFILE) `pkg-config --libs libcrypto++` -lpthread
9BINS=upgtool 9BINS=upgtool
10 10
11all: $(BINS) 11all: $(BINS)
@@ -16,7 +16,7 @@ all: $(BINS)
16%.o: %.cpp 16%.o: %.cpp
17 $(CXX) $(CXXFLAGS) -c -o $@ $< 17 $(CXX) $(CXXFLAGS) -c -o $@ $<
18 18
19upgtool: upgtool.o upg.o misc.o fwp.o mg.o keysig_search.o 19upgtool: upgtool.o upg.o misc.o fwp.o mg.o keysig_search.o md5.o
20 $(LD) -o $@ $^ $(LDFLAGS) 20 $(LD) -o $@ $^ $(LDFLAGS)
21 21
22clean: 22clean:
diff --git a/utils/nwztools/upgtools/fwp.c b/utils/nwztools/upgtools/fwp.c
index 34c55f6e5a..7d8f8002a8 100644
--- a/utils/nwztools/upgtools/fwp.c
+++ b/utils/nwztools/upgtools/fwp.c
@@ -18,21 +18,20 @@
18 * KIND, either express or implied. 18 * KIND, either express or implied.
19 * 19 *
20 ****************************************************************************/ 20 ****************************************************************************/
21#include <stdio.h> 21#include <string.h>
22#include <stdlib.h> 22#include <stdlib.h>
23#include "fwp.h" 23#include "fwp.h"
24#include "misc.h" 24#include "misc.h"
25#include "mg.h" 25#include "mg.h"
26#include <string.h>
27 26
28int fwp_read(void *in, int size, void *out, uint8_t *key) 27void fwp_read(void *in, int size, void *out, uint8_t *key)
29{ 28{
30 return mg_decrypt_fw(in, size, out, key); 29 mg_decrypt_fw(in, size, out, key);
31} 30}
32 31
33int fwp_write(void *in, int size, void *out, uint8_t *key) 32void fwp_write(void *in, int size, void *out, uint8_t *key)
34{ 33{
35 return mg_encrypt_fw(in, size, out, key); 34 mg_encrypt_fw(in, size, out, key);
36} 35}
37 36
38static uint8_t g_key[NWZ_KEY_SIZE]; 37static uint8_t g_key[NWZ_KEY_SIZE];
@@ -42,7 +41,7 @@ void fwp_setkey(char key[NWZ_KEY_SIZE])
42 memcpy(g_key, key, NWZ_KEY_SIZE); 41 memcpy(g_key, key, NWZ_KEY_SIZE);
43} 42}
44 43
45int fwp_crypt(void *buf, int size, int mode) 44void fwp_crypt(void *buf, int size, int mode)
46{ 45{
47 while(size >= NWZ_KEY_SIZE) 46 while(size >= NWZ_KEY_SIZE)
48 { 47 {
@@ -54,6 +53,5 @@ int fwp_crypt(void *buf, int size, int mode)
54 size -= NWZ_KEY_SIZE; 53 size -= NWZ_KEY_SIZE;
55 } 54 }
56 if(size != 0) 55 if(size != 0)
57 abort(); 56 abort(); /* size is not a multiple of 8 */
58 return 0;
59} 57}
diff --git a/utils/nwztools/upgtools/fwp.h b/utils/nwztools/upgtools/fwp.h
index 0d928fbec1..32fe260090 100644
--- a/utils/nwztools/upgtools/fwp.h
+++ b/utils/nwztools/upgtools/fwp.h
@@ -33,11 +33,13 @@ extern "C" {
33#define NWZ_SIG_SIZE 8 33#define NWZ_SIG_SIZE 8
34#define NWZ_EXPKEY_SIZE (NWZ_KEY_SIZE * NWZ_KEY_SIZE) 34#define NWZ_EXPKEY_SIZE (NWZ_KEY_SIZE * NWZ_KEY_SIZE)
35#define NWZ_DES_BLOCK 8 35#define NWZ_DES_BLOCK 8
36#define NWZ_MD5_SIZE 16
36 37
37int fwp_read(void *in, int size, void *out, uint8_t *key); 38/* size must be a multiple of 8 */
38int fwp_write(void *in, int size, void *out, uint8_t *key); 39void fwp_read(void *in, int size, void *out, uint8_t *key);
40void fwp_write(void *in, int size, void *out, uint8_t *key);
39void fwp_setkey(char key[8]); 41void fwp_setkey(char key[8]);
40int fwp_crypt(void *buf, int size, int mode); 42void fwp_crypt(void *buf, int size, int mode);
41 43
42#ifdef __cplusplus 44#ifdef __cplusplus
43} 45}
diff --git a/utils/nwztools/upgtools/md5.cpp b/utils/nwztools/upgtools/md5.cpp
new file mode 100644
index 0000000000..3b0c2358e4
--- /dev/null
+++ b/utils/nwztools/upgtools/md5.cpp
@@ -0,0 +1,31 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2012 Amaury Pouly
11 *
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.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21#include "md5.h"
22/* MD5 is considered insecure by crypto++ */
23#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
24#include <crypto++/md5.h>
25
26using namespace CryptoPP::Weak;
27
28void MD5_CalculateDigest(void *digest, const void *input, size_t length)
29{
30 MD5().CalculateDigest((byte *)digest, (const byte *)input, length);
31}
diff --git a/utils/nwztools/upgtools/md5.h b/utils/nwztools/upgtools/md5.h
new file mode 100644
index 0000000000..1e4b57ab0e
--- /dev/null
+++ b/utils/nwztools/upgtools/md5.h
@@ -0,0 +1,37 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2016 Amaury Pouly
11 *
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.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21#ifndef __md5_h__
22#define __md5_h__
23
24#include <stddef.h>
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30/* Compute the MD5 digest of a buffer */
31void MD5_CalculateDigest(void *digest, const void *input, size_t length);
32
33#ifdef __cplusplus
34}
35#endif
36
37#endif /* __md5_h__ */
diff --git a/utils/nwztools/upgtools/mg.cpp b/utils/nwztools/upgtools/mg.cpp
index f02b67375a..79039702db 100644
--- a/utils/nwztools/upgtools/mg.cpp
+++ b/utils/nwztools/upgtools/mg.cpp
@@ -28,43 +28,41 @@
28using namespace CryptoPP; 28using namespace CryptoPP;
29namespace 29namespace
30{ 30{
31 inline int dec_des_ecb(void *in, int size, void *out, uint8_t *key) 31 inline void dec_des_ecb(void *in, int size, void *out, uint8_t *key)
32 { 32 {
33 ECB_Mode< DES >::Decryption dec; 33 ECB_Mode< DES >::Decryption dec;
34 if(size % 8) 34 if(size % 8)
35 return 42; 35 abort(); /* size must be a multiple of 8 */
36 dec.SetKey(key, 8); 36 dec.SetKey(key, 8);
37 dec.ProcessData((byte*)out, (byte*)in, size); 37 dec.ProcessData((byte*)out, (byte*)in, size);
38 return 0;
39 } 38 }
40 39
41 inline int enc_des_ecb(void *in, int size, void *out, uint8_t *key) 40 inline void enc_des_ecb(void *in, int size, void *out, uint8_t *key)
42 { 41 {
43 ECB_Mode< DES >::Encryption enc; 42 ECB_Mode< DES >::Encryption enc;
44 if(size % 8) 43 if(size % 8)
45 return 42; 44 abort(); /* size must be a multiple of 8 */
46 enc.SetKey(key, 8); 45 enc.SetKey(key, 8);
47 enc.ProcessData((byte*)out, (byte*)in, size); 46 enc.ProcessData((byte*)out, (byte*)in, size);
48 return 0;
49 } 47 }
50} 48}
51 49
52int mg_decrypt_fw(void *in, int size, void *out, uint8_t *key) 50void mg_decrypt_fw(void *in, int size, void *out, uint8_t *key)
53{ 51{
54 return dec_des_ecb(in, size, out, key); 52 dec_des_ecb(in, size, out, key);
55} 53}
56 54
57int mg_encrypt_fw(void *in, int size, void *out, uint8_t *key) 55void mg_encrypt_fw(void *in, int size, void *out, uint8_t *key)
58{ 56{
59 return enc_des_ecb(in, size, out, key); 57 enc_des_ecb(in, size, out, key);
60} 58}
61 59
62int mg_decrypt_pass(void *in, int size, void *out, uint8_t *key) 60void mg_decrypt_pass(void *in, int size, void *out, uint8_t *key)
63{ 61{
64 return dec_des_ecb(in, size, out, key); 62 dec_des_ecb(in, size, out, key);
65} 63}
66 64
67int mg_encrypt_pass(void *in, int size, void *out, uint8_t *key) 65void mg_encrypt_pass(void *in, int size, void *out, uint8_t *key)
68{ 66{
69 return enc_des_ecb(in, size, out, key); 67 enc_des_ecb(in, size, out, key);
70} 68}
diff --git a/utils/nwztools/upgtools/mg.h b/utils/nwztools/upgtools/mg.h
index a0c1f2ef65..ef8dcd5ecb 100644
--- a/utils/nwztools/upgtools/mg.h
+++ b/utils/nwztools/upgtools/mg.h
@@ -26,12 +26,13 @@
26#ifdef __cplusplus 26#ifdef __cplusplus
27extern "C" { 27extern "C" {
28#endif 28#endif
29int mg_decrypt_fw(void *in, int size, void *out, uint8_t *key); 29/* size must be a multiple of 8 */
30int mg_encrypt_fw(void *in, int size, void *out, uint8_t *key); 30void mg_decrypt_fw(void *in, int size, void *out, uint8_t *key);
31int mg_decrypt_pass(void *in, int size, void *out, uint8_t *key); 31void mg_encrypt_fw(void *in, int size, void *out, uint8_t *key);
32int mg_encrypt_pass(void *in, int size, void *out, uint8_t *key); 32void mg_decrypt_pass(void *in, int size, void *out, uint8_t *key);
33void mg_encrypt_pass(void *in, int size, void *out, uint8_t *key);
33#ifdef __cplusplus 34#ifdef __cplusplus
34} 35}
35#endif 36#endif
36 37
37#endif /* __mg_h__ */ \ No newline at end of file 38#endif /* __mg_h__ */
diff --git a/utils/nwztools/upgtools/upg.c b/utils/nwztools/upgtools/upg.c
index 44d3eca789..8a6a9f0754 100644
--- a/utils/nwztools/upgtools/upg.c
+++ b/utils/nwztools/upgtools/upg.c
@@ -22,7 +22,7 @@
22#include <stdlib.h> 22#include <stdlib.h>
23#include <string.h> 23#include <string.h>
24#include <ctype.h> 24#include <ctype.h>
25#include <openssl/md5.h> 25#include "md5.h"
26 26
27struct nwz_model_t g_model_list[] = 27struct nwz_model_t g_model_list[] =
28{ 28{
@@ -97,19 +97,14 @@ struct upg_file_t *upg_read_memory(void *buf, size_t size, char key[NWZ_KEY_SIZE
97 struct upg_md5_t *md5 = buf; 97 struct upg_md5_t *md5 = buf;
98 cprintf(BLUE, "Preliminary\n"); 98 cprintf(BLUE, "Preliminary\n");
99 cprintf(GREEN, " MD5: "); 99 cprintf(GREEN, " MD5: ");
100 for(int i = 0; i < MD5_DIGEST_LENGTH; i++) 100 for(int i = 0; i < NWZ_MD5_SIZE; i++)
101 cprintf(YELLOW, "%02x", md5->md5[i]); 101 cprintf(YELLOW, "%02x", md5->md5[i]);
102 cprintf(OFF, " "); 102 cprintf(OFF, " ");
103 103
104 /* check MD5 */ 104 /* check MD5 */
105 uint8_t actual_md5[MD5_DIGEST_LENGTH]; 105 uint8_t actual_md5[NWZ_MD5_SIZE];
106 { 106 MD5_CalculateDigest(actual_md5, (md5 + 1), size - sizeof(struct upg_header_t));
107 MD5_CTX c; 107 if(memcmp(actual_md5, md5->md5, NWZ_MD5_SIZE) != 0)
108 MD5_Init(&c);
109 MD5_Update(&c, md5 + 1, size - sizeof(struct upg_header_t));
110 MD5_Final(actual_md5, &c);
111 }
112 if(memcmp(actual_md5, md5->md5, MD5_DIGEST_LENGTH) != 0)
113 { 108 {
114 cprintf(RED, "Mismatch\n"); 109 cprintf(RED, "Mismatch\n");
115 err_printf(GREY, "MD5 Mismatch\n"); 110 err_printf(GREY, "MD5 Mismatch\n");
@@ -223,12 +218,7 @@ void *upg_write_memory(struct upg_file_t *file, char key[NWZ_KEY_SIZE],
223 /* encrypt everything and hash everything */ 218 /* encrypt everything and hash everything */
224 fwp_write(hdr, tot_size - sizeof(*md5), hdr, (void *)key); 219 fwp_write(hdr, tot_size - sizeof(*md5), hdr, (void *)key);
225 /* write final MD5 */ 220 /* write final MD5 */
226 { 221 MD5_CalculateDigest(md5->md5, (void *)hdr, tot_size - sizeof(*md5));
227 MD5_CTX c;
228 MD5_Init(&c);
229 MD5_Update(&c, (void *)hdr, tot_size - sizeof(*md5));
230 MD5_Final(md5->md5, &c);
231 }
232 *out_size = tot_size; 222 *out_size = tot_size;
233 return buf; 223 return buf;
234} 224}
diff --git a/utils/nwztools/upgtools/upgtool.c b/utils/nwztools/upgtools/upgtool.c
index 0de46a4260..a1dce84870 100644
--- a/utils/nwztools/upgtools/upgtool.c
+++ b/utils/nwztools/upgtools/upgtool.c
@@ -29,7 +29,6 @@
29#include "misc.h" 29#include "misc.h"
30#include "elf.h" 30#include "elf.h"
31#include <sys/stat.h> 31#include <sys/stat.h>
32#include <openssl/md5.h>
33#include "crypt.h" 32#include "crypt.h"
34#include "fwp.h" 33#include "fwp.h"
35#include "keysig_search.h" 34#include "keysig_search.h"
@@ -482,4 +481,3 @@ int main(int argc, char **argv)
482 481
483 return ret; 482 return ret;
484} 483}
485