forked from len0rd/rockbox
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
This commit is contained in:
parent
92ecbd5fb8
commit
dbeb6db1b5
9 changed files with 108 additions and 53 deletions
|
@ -3,9 +3,9 @@ CC=gcc
|
|||
CXX=g++
|
||||
LD=g++
|
||||
PROFILE=
|
||||
CFLAGS=-g $(PROFILE) -std=c99 -W -Wall $(DEFINES) `pkg-config --cflags openssl` `pkg-config --cflags libcrypto++`
|
||||
CXXFLAGS=-g $(PROFILE) -W -Wall $(DEFINES) `pkg-config --cflags openssl` `pkg-config --cflags libcrypto++`
|
||||
LDFLAGS=$(PROFILE) `pkg-config --libs openssl` `pkg-config --libs libcrypto++` -lcrypt -lpthread
|
||||
CFLAGS=-g $(PROFILE) -std=c99 -W -Wall $(DEFINES) `pkg-config --cflags libcrypto++`
|
||||
CXXFLAGS=-g $(PROFILE) -W -Wall $(DEFINES) `pkg-config --cflags libcrypto++`
|
||||
LDFLAGS=$(PROFILE) `pkg-config --libs libcrypto++` -lpthread
|
||||
BINS=upgtool
|
||||
|
||||
all: $(BINS)
|
||||
|
@ -16,7 +16,7 @@ all: $(BINS)
|
|||
%.o: %.cpp
|
||||
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
||||
|
||||
upgtool: upgtool.o upg.o misc.o fwp.o mg.o keysig_search.o
|
||||
upgtool: upgtool.o upg.o misc.o fwp.o mg.o keysig_search.o md5.o
|
||||
$(LD) -o $@ $^ $(LDFLAGS)
|
||||
|
||||
clean:
|
||||
|
|
|
@ -18,21 +18,20 @@
|
|||
* KIND, either express or implied.
|
||||
*
|
||||
****************************************************************************/
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "fwp.h"
|
||||
#include "misc.h"
|
||||
#include "mg.h"
|
||||
#include <string.h>
|
||||
|
||||
int fwp_read(void *in, int size, void *out, uint8_t *key)
|
||||
void fwp_read(void *in, int size, void *out, uint8_t *key)
|
||||
{
|
||||
return mg_decrypt_fw(in, size, out, key);
|
||||
mg_decrypt_fw(in, size, out, key);
|
||||
}
|
||||
|
||||
int fwp_write(void *in, int size, void *out, uint8_t *key)
|
||||
void fwp_write(void *in, int size, void *out, uint8_t *key)
|
||||
{
|
||||
return mg_encrypt_fw(in, size, out, key);
|
||||
mg_encrypt_fw(in, size, out, key);
|
||||
}
|
||||
|
||||
static uint8_t g_key[NWZ_KEY_SIZE];
|
||||
|
@ -42,7 +41,7 @@ void fwp_setkey(char key[NWZ_KEY_SIZE])
|
|||
memcpy(g_key, key, NWZ_KEY_SIZE);
|
||||
}
|
||||
|
||||
int fwp_crypt(void *buf, int size, int mode)
|
||||
void fwp_crypt(void *buf, int size, int mode)
|
||||
{
|
||||
while(size >= NWZ_KEY_SIZE)
|
||||
{
|
||||
|
@ -54,6 +53,5 @@ int fwp_crypt(void *buf, int size, int mode)
|
|||
size -= NWZ_KEY_SIZE;
|
||||
}
|
||||
if(size != 0)
|
||||
abort();
|
||||
return 0;
|
||||
abort(); /* size is not a multiple of 8 */
|
||||
}
|
||||
|
|
|
@ -33,11 +33,13 @@ extern "C" {
|
|||
#define NWZ_SIG_SIZE 8
|
||||
#define NWZ_EXPKEY_SIZE (NWZ_KEY_SIZE * NWZ_KEY_SIZE)
|
||||
#define NWZ_DES_BLOCK 8
|
||||
#define NWZ_MD5_SIZE 16
|
||||
|
||||
int fwp_read(void *in, int size, void *out, uint8_t *key);
|
||||
int fwp_write(void *in, int size, void *out, uint8_t *key);
|
||||
/* size must be a multiple of 8 */
|
||||
void fwp_read(void *in, int size, void *out, uint8_t *key);
|
||||
void fwp_write(void *in, int size, void *out, uint8_t *key);
|
||||
void fwp_setkey(char key[8]);
|
||||
int fwp_crypt(void *buf, int size, int mode);
|
||||
void fwp_crypt(void *buf, int size, int mode);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
31
utils/nwztools/upgtools/md5.cpp
Normal file
31
utils/nwztools/upgtools/md5.cpp
Normal file
|
@ -0,0 +1,31 @@
|
|||
/***************************************************************************
|
||||
* __________ __ ___.
|
||||
* 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 "md5.h"
|
||||
/* MD5 is considered insecure by crypto++ */
|
||||
#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
|
||||
#include <crypto++/md5.h>
|
||||
|
||||
using namespace CryptoPP::Weak;
|
||||
|
||||
void MD5_CalculateDigest(void *digest, const void *input, size_t length)
|
||||
{
|
||||
MD5().CalculateDigest((byte *)digest, (const byte *)input, length);
|
||||
}
|
37
utils/nwztools/upgtools/md5.h
Normal file
37
utils/nwztools/upgtools/md5.h
Normal file
|
@ -0,0 +1,37 @@
|
|||
/***************************************************************************
|
||||
* __________ __ ___.
|
||||
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
||||
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
||||
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
||||
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
||||
* \/ \/ \/ \/ \/
|
||||
* $Id$
|
||||
*
|
||||
* Copyright (C) 2016 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
#ifndef __md5_h__
|
||||
#define __md5_h__
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Compute the MD5 digest of a buffer */
|
||||
void MD5_CalculateDigest(void *digest, const void *input, size_t length);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __md5_h__ */
|
|
@ -28,43 +28,41 @@
|
|||
using namespace CryptoPP;
|
||||
namespace
|
||||
{
|
||||
inline int dec_des_ecb(void *in, int size, void *out, uint8_t *key)
|
||||
inline void dec_des_ecb(void *in, int size, void *out, uint8_t *key)
|
||||
{
|
||||
ECB_Mode< DES >::Decryption dec;
|
||||
if(size % 8)
|
||||
return 42;
|
||||
abort(); /* size must be a multiple of 8 */
|
||||
dec.SetKey(key, 8);
|
||||
dec.ProcessData((byte*)out, (byte*)in, size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline int enc_des_ecb(void *in, int size, void *out, uint8_t *key)
|
||||
inline void enc_des_ecb(void *in, int size, void *out, uint8_t *key)
|
||||
{
|
||||
ECB_Mode< DES >::Encryption enc;
|
||||
if(size % 8)
|
||||
return 42;
|
||||
abort(); /* size must be a multiple of 8 */
|
||||
enc.SetKey(key, 8);
|
||||
enc.ProcessData((byte*)out, (byte*)in, size);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int mg_decrypt_fw(void *in, int size, void *out, uint8_t *key)
|
||||
void mg_decrypt_fw(void *in, int size, void *out, uint8_t *key)
|
||||
{
|
||||
return dec_des_ecb(in, size, out, key);
|
||||
dec_des_ecb(in, size, out, key);
|
||||
}
|
||||
|
||||
int mg_encrypt_fw(void *in, int size, void *out, uint8_t *key)
|
||||
void mg_encrypt_fw(void *in, int size, void *out, uint8_t *key)
|
||||
{
|
||||
return enc_des_ecb(in, size, out, key);
|
||||
enc_des_ecb(in, size, out, key);
|
||||
}
|
||||
|
||||
int mg_decrypt_pass(void *in, int size, void *out, uint8_t *key)
|
||||
void mg_decrypt_pass(void *in, int size, void *out, uint8_t *key)
|
||||
{
|
||||
return dec_des_ecb(in, size, out, key);
|
||||
dec_des_ecb(in, size, out, key);
|
||||
}
|
||||
|
||||
int mg_encrypt_pass(void *in, int size, void *out, uint8_t *key)
|
||||
void mg_encrypt_pass(void *in, int size, void *out, uint8_t *key)
|
||||
{
|
||||
return enc_des_ecb(in, size, out, key);
|
||||
enc_des_ecb(in, size, out, key);
|
||||
}
|
||||
|
|
|
@ -26,12 +26,13 @@
|
|||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
int mg_decrypt_fw(void *in, int size, void *out, uint8_t *key);
|
||||
int mg_encrypt_fw(void *in, int size, void *out, uint8_t *key);
|
||||
int mg_decrypt_pass(void *in, int size, void *out, uint8_t *key);
|
||||
int mg_encrypt_pass(void *in, int size, void *out, uint8_t *key);
|
||||
/* size must be a multiple of 8 */
|
||||
void mg_decrypt_fw(void *in, int size, void *out, uint8_t *key);
|
||||
void mg_encrypt_fw(void *in, int size, void *out, uint8_t *key);
|
||||
void mg_decrypt_pass(void *in, int size, void *out, uint8_t *key);
|
||||
void mg_encrypt_pass(void *in, int size, void *out, uint8_t *key);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __mg_h__ */
|
||||
#endif /* __mg_h__ */
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <openssl/md5.h>
|
||||
#include "md5.h"
|
||||
|
||||
struct nwz_model_t g_model_list[] =
|
||||
{
|
||||
|
@ -97,19 +97,14 @@ struct upg_file_t *upg_read_memory(void *buf, size_t size, char key[NWZ_KEY_SIZE
|
|||
struct upg_md5_t *md5 = buf;
|
||||
cprintf(BLUE, "Preliminary\n");
|
||||
cprintf(GREEN, " MD5: ");
|
||||
for(int i = 0; i < MD5_DIGEST_LENGTH; i++)
|
||||
for(int i = 0; i < NWZ_MD5_SIZE; i++)
|
||||
cprintf(YELLOW, "%02x", md5->md5[i]);
|
||||
cprintf(OFF, " ");
|
||||
|
||||
/* check MD5 */
|
||||
uint8_t actual_md5[MD5_DIGEST_LENGTH];
|
||||
{
|
||||
MD5_CTX c;
|
||||
MD5_Init(&c);
|
||||
MD5_Update(&c, md5 + 1, size - sizeof(struct upg_header_t));
|
||||
MD5_Final(actual_md5, &c);
|
||||
}
|
||||
if(memcmp(actual_md5, md5->md5, MD5_DIGEST_LENGTH) != 0)
|
||||
uint8_t actual_md5[NWZ_MD5_SIZE];
|
||||
MD5_CalculateDigest(actual_md5, (md5 + 1), size - sizeof(struct upg_header_t));
|
||||
if(memcmp(actual_md5, md5->md5, NWZ_MD5_SIZE) != 0)
|
||||
{
|
||||
cprintf(RED, "Mismatch\n");
|
||||
err_printf(GREY, "MD5 Mismatch\n");
|
||||
|
@ -223,12 +218,7 @@ void *upg_write_memory(struct upg_file_t *file, char key[NWZ_KEY_SIZE],
|
|||
/* encrypt everything and hash everything */
|
||||
fwp_write(hdr, tot_size - sizeof(*md5), hdr, (void *)key);
|
||||
/* write final MD5 */
|
||||
{
|
||||
MD5_CTX c;
|
||||
MD5_Init(&c);
|
||||
MD5_Update(&c, (void *)hdr, tot_size - sizeof(*md5));
|
||||
MD5_Final(md5->md5, &c);
|
||||
}
|
||||
MD5_CalculateDigest(md5->md5, (void *)hdr, tot_size - sizeof(*md5));
|
||||
*out_size = tot_size;
|
||||
return buf;
|
||||
}
|
||||
|
|
|
@ -29,7 +29,6 @@
|
|||
#include "misc.h"
|
||||
#include "elf.h"
|
||||
#include <sys/stat.h>
|
||||
#include <openssl/md5.h>
|
||||
#include "crypt.h"
|
||||
#include "fwp.h"
|
||||
#include "keysig_search.h"
|
||||
|
@ -482,4 +481,3 @@ int main(int argc, char **argv)
|
|||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue