* use Qt stuff for general sha1 & md5.

* replace apparently buggy round-up macro

git-svn-id: http://wiiqt.googlecode.com/svn/trunk@45 389f4c8b-5dfe-645f-db0e-df882bc27289
This commit is contained in:
giantpune@gmail.com 2011-01-04 00:07:21 +00:00
parent 3f1f5654ad
commit 930e92ca3c
10 changed files with 40 additions and 45 deletions

View File

@ -307,7 +307,7 @@ Boot2Info Blocks0to7::CheckHashes( Boot2Info info )
stuff += blocks.at( ret.secondBlock ); stuff += blocks.at( ret.secondBlock );
AesSetKey( ticket.DecryptedKey() ); AesSetKey( ticket.DecryptedKey() );
QByteArray decD = AesDecrypt( 0, stuff.mid( dataOff, RU( 0x40, t.Size( 0 ) ) ) ); QByteArray decD = AesDecrypt( 0, stuff.mid( dataOff, RU( t.Size( 0 ), 0x40 ) ) );
decD.resize( t.Size( 0 ) ); decD.resize( t.Size( 0 ) );
QByteArray realHash = GetSha1( decD ); QByteArray realHash = GetSha1( decD );
if( realHash != t.Hash( 0 ) ) if( realHash != t.Hash( 0 ) )

View File

@ -1602,7 +1602,7 @@ bool NandBin::CheckHmacData( quint16 entry )
if( !fst.size ) if( !fst.size )
return true; return true;
quint16 clCnt = ( RU( 0x4000, fst.size ) / 0x4000 ); quint16 clCnt = ( RU( fst.size, 0x4000 ) / 0x4000 );
//qDebug() << FstName( fst ) << "is" << hex << fst.size << "bytes (" << clCnt << ") clusters"; //qDebug() << FstName( fst ) << "is" << hex << fst.size << "bytes (" << clCnt << ") clusters";
quint16 fat = fst.sub; quint16 fat = fst.sub;

View File

@ -377,8 +377,9 @@ bool NandDump::InstallNusItem( const NusJob &job )
} }
quint32 cnt = qFromBigEndian( t.payload()->num_contents ); quint32 cnt = qFromBigEndian( t.payload()->num_contents );
if( cnt != (quint32)job.data.size() ) if( cnt != (quint32)job.data.size() - 2 )
{ {
qWarning() << "cnt != (quint32)job.data.size()";
AbortInstalling( job.tid ); AbortInstalling( job.tid );
return false; return false;
} }

View File

@ -1,6 +1,5 @@
#include "savedatabin.h" #include "savedatabin.h"
#include "aes.h" #include "aes.h"
#include "md5.h"
#include "ec.h" #include "ec.h"
SaveDataBin::SaveDataBin( QByteArray stuff ) SaveDataBin::SaveDataBin( QByteArray stuff )
@ -27,12 +26,7 @@ SaveDataBin::SaveDataBin( QByteArray stuff )
quint8 md5blanker[ 16 ] = MD5_BLANKER; quint8 md5blanker[ 16 ] = MD5_BLANKER;
QByteArray expected = header.mid( 0xe, 16 ); QByteArray expected = header.mid( 0xe, 16 );
QByteArray headerWithBlanker = header.left( 0xe ) + QByteArray( (const char*)&md5blanker, 16 ) + header.right( 0xf0a2 ); QByteArray headerWithBlanker = header.left( 0xe ) + QByteArray( (const char*)&md5blanker, 16 ) + header.right( 0xf0a2 );
//hexdump( headerWithBlanker.left( 0x50 ) ); QByteArray actual = GetMd5( headerWithBlanker );
MD5 hash;
hash.update( headerWithBlanker.data(), size );
hash.finalize();
QByteArray actual = QByteArray( (const char*)hash.hexdigestChar(), 16 );
if( actual != expected ) if( actual != expected )
{ {
qWarning() << "SaveDataBin::SaveDataBin -> md5 mismatch"; qWarning() << "SaveDataBin::SaveDataBin -> md5 mismatch";
@ -267,11 +261,7 @@ const QByteArray SaveDataBin::Data( const QByteArray &ngPriv, const QByteArray &
header2 += bnr; //add the banner.bin header2 += bnr; //add the banner.bin
header2 = PaddedByteArray( header2, 0xf0c0 ); //pad to 0xf0c0 header2 = PaddedByteArray( header2, 0xf0c0 ); //pad to 0xf0c0
MD5 hash; QByteArray actual = GetMd5( header2 );
hash.update( header2.data(), 0xf0c0 );
hash.finalize();
QByteArray actual = QByteArray( (const char*)hash.hexdigestChar(), 16 );
header += actual + QByteArray( 2, '\0' ) + bnr; header += actual + QByteArray( 2, '\0' ) + bnr;
header = PaddedByteArray( header, 0xf0c0 ); header = PaddedByteArray( header, 0xf0c0 );
@ -510,11 +500,7 @@ quint32 SaveDataBin::GetSize( QByteArray dataBin )
quint8 md5blanker[ 16 ] = MD5_BLANKER; quint8 md5blanker[ 16 ] = MD5_BLANKER;
QByteArray expected = header.mid( 0xe, 16 ); QByteArray expected = header.mid( 0xe, 16 );
QByteArray headerWithBlanker = header.left( 0xe ) + QByteArray( (const char*)&md5blanker, 16 ) + header.right( 0xf0a2 ); QByteArray headerWithBlanker = header.left( 0xe ) + QByteArray( (const char*)&md5blanker, 16 ) + header.right( 0xf0a2 );
MD5 hash; QByteArray actual = GetMd5( headerWithBlanker );
hash.update( headerWithBlanker.data(), size );
hash.finalize();
QByteArray actual = QByteArray( (const char*)hash.hexdigestChar(), 16 );
if( actual != expected ) if( actual != expected )
{ {
qWarning() << "SaveDataBin::GetSize -> md5 mismatch"; qWarning() << "SaveDataBin::GetSize -> md5 mismatch";

View File

@ -83,8 +83,8 @@ void hexdump12( const QByteArray &d, int from, int len )
QByteArray PaddedByteArray( const QByteArray &orig, quint32 padTo ) QByteArray PaddedByteArray( const QByteArray &orig, quint32 padTo )
{ {
//qDebug() << "need to pad from" << hex << orig.size() << "to nearest" << padTo;
QByteArray padding( RU( orig.size(), padTo ) - orig.size(), '\0' ); QByteArray padding( RU( orig.size(), padTo ) - orig.size(), '\0' );
//qDebug() << "padding with" << hex << RU( padTo, orig.size() ) << "bytes" <<
return orig + padding; return orig + padding;
} }
@ -121,7 +121,8 @@ void AesSetKey( const QByteArray &key )
QByteArray GetSha1( const QByteArray &stuff ) QByteArray GetSha1( const QByteArray &stuff )
{ {
SHA1Context sha; return QCryptographicHash::hash( stuff, QCryptographicHash::Sha1 );
/*SHA1Context sha;
SHA1Reset( &sha ); SHA1Reset( &sha );
SHA1Input( &sha, (const unsigned char*)stuff.data(), stuff.size() ); SHA1Input( &sha, (const unsigned char*)stuff.data(), stuff.size() );
if( !SHA1Result( &sha ) ) if( !SHA1Result( &sha ) )
@ -136,8 +137,14 @@ QByteArray GetSha1( const QByteArray &stuff )
quint32 part = qFromBigEndian( sha.Message_Digest[ i ] ); quint32 part = qFromBigEndian( sha.Message_Digest[ i ] );
memcpy( p + ( i * 4 ), &part, 4 ); memcpy( p + ( i * 4 ), &part, 4 );
} }
//hexdump( ret ); return ret;*/
return ret; }
QByteArray GetMd5( const QByteArray &stuff )
{
//qDebug() << "GetMd5" << hex << stuff.size();
return QCryptographicHash::hash( stuff, QCryptographicHash::Md5 );
} }
QByteArray ReadFile( const QString &path ) QByteArray ReadFile( const QString &path )

View File

@ -3,7 +3,8 @@
#include "includes.h" #include "includes.h"
//#include " //#include "
#define RU(x,n) (-(-(x) & -(n))) //round up //#define RU(x,n) (-(-(x) & -(n))) //round up
#define RU(N, S) ((((N) + (S) - 1) / (S)) * (S))
#define MIN( x, y ) ( ( x ) < ( y ) ? ( x ) : ( y ) ) #define MIN( x, y ) ( ( x ) < ( y ) ? ( x ) : ( y ) )
#define MAX( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) ) #define MAX( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) )
@ -59,6 +60,7 @@ QByteArray AesDecrypt( quint16 index, const QByteArray &source );
QByteArray AesEncrypt( quint16 index, const QByteArray &source ); QByteArray AesEncrypt( quint16 index, const QByteArray &source );
QByteArray GetSha1( const QByteArray &stuff ); QByteArray GetSha1( const QByteArray &stuff );
QByteArray GetMd5( const QByteArray &stuff );
bool IsValidSave( const SaveGame &save ); bool IsValidSave( const SaveGame &save );
const QByteArray DataFromSave( const SaveGame &save, const QString &name ); const QByteArray DataFromSave( const SaveGame &save, const QString &name );

View File

@ -66,7 +66,7 @@ Wad::Wad( const QByteArray &stuff )
//sanity check this thing //sanity check this thing
quint32 s = stuff.size(); quint32 s = stuff.size();
if( s < ( RU( 0x40, certSize ) + RU( 0x40, tikSize ) + RU( 0x40, tmdSize ) + RU( 0x40, appSize ) + RU( 0x40, footerSize ) ) ) if( s < ( RU( certSize, 0x40 ) + RU( tikSize, 0x40 ) + RU( tmdSize, 0x40 ) + RU( appSize, 0x40 ) + RU( footerSize, 0x40 ) ) )
{ {
Err( "Total size is less than the combined sizes of all the parts that it is supposed to contain" ); Err( "Total size is less than the combined sizes of all the parts that it is supposed to contain" );
return; return;
@ -74,11 +74,11 @@ Wad::Wad( const QByteArray &stuff )
quint32 pos = 0x40; quint32 pos = 0x40;
certData = stuff.mid( pos, certSize ); certData = stuff.mid( pos, certSize );
pos += RU( 0x40, certSize ); pos += RU( certSize, 0x4 );
tikData = stuff.mid( pos, tikSize ); tikData = stuff.mid( pos, tikSize );
pos += RU( 0x40, tikSize ); pos += RU( tikSize, 0x40 );
tmdData = stuff.mid( pos, tmdSize ); tmdData = stuff.mid( pos, tmdSize );
pos += RU( 0x40, tmdSize ); pos += RU( tmdSize, 0x40 );
Ticket ticket( tikData ); Ticket ticket( tikData );
Tmd t( tmdData ); Tmd t( tmdData );
@ -106,7 +106,7 @@ Wad::Wad( const QByteArray &stuff )
for( quint32 i = 0; i < cnt; i++ ) for( quint32 i = 0; i < cnt; i++ )
{ {
quint32 s = RU( 0x40, t.Size( i ) ); quint32 s = RU( t.Size( i ), 0x40 );
qDebug() << "content" << i << "is at" << hex << pos qDebug() << "content" << i << "is at" << hex << pos
<< "with size" << s; << "with size" << s;
QByteArray encData = stuff.mid( pos, s ); QByteArray encData = stuff.mid( pos, s );
@ -280,13 +280,13 @@ const QByteArray Wad::Data( quint32 magicWord, const QByteArray &footer )
quint16 cnt = t.Count(); quint16 cnt = t.Count();
for( quint16 i = 0; i < cnt; i++ ) for( quint16 i = 0; i < cnt; i++ )
{ {
quint32 s = RU( 0x40, partsEnc.at( i ).size() ); quint32 s = RU( partsEnc.at( i ).size(), 0x40 );
if( RU( 0x40, t.Size( i ) ) != s ) if( RU( t.Size( i ), 0x40 ) != s )
{ {
Err( QString( "Size of content %1 is bad ( %2, %3, %4 )" ) Err( QString( "Size of content %1 is bad ( %2, %3, %4 )" )
.arg( i ) .arg( i )
.arg( t.Size( i ), 0, 16 ) .arg( t.Size( i ), 0, 16 )
.arg( RU( 0x40, t.Size( i ) ), 0, 16 ) .arg( RU( t.Size( i ), 0x40 ), 0, 16 )
.arg( s, 0, 16 ) ); .arg( s, 0, 16 ) );
return QByteArray(); return QByteArray();
} }

View File

@ -140,7 +140,7 @@ void MainWindow::ReceiveTitleFromNus( NusJob job )
if( ok ) if( ok )
ShowMessage( tr( "Installed %1 title to nand" ).arg( title ) ); ShowMessage( tr( "Installed %1 title to nand" ).arg( title ) );
else else
ShowMessage( tr( "<b>Error %1 title to nand</b>" ).arg( title ) ); ShowMessage( tr( "<b>Error installing %1 title to nand</b>" ).arg( title ) );
} }
else if( ui->radioButton_wad->isChecked() ) else if( ui->radioButton_wad->isChecked() )
{ {

View File

@ -194,7 +194,7 @@ void MainWindow::ShowSneekSaveDetails( SaveListItem *item )
float mib = (float)size / 1048576.00f; float mib = (float)size / 1048576.00f;
sizeStr = tr( "%1 MiB" ).arg( mib, 3, 'f', 2 ); sizeStr = tr( "%1 MiB" ).arg( mib, 3, 'f', 2 );
} }
int blocks = RU( 0x20000, size) / 0x20000; int blocks = RU( size, 0x20000 ) / 0x20000;
QString si = QString( "%1 %2 (%3)").arg( blocks ).arg( blocks == 1 ? tr( "Block" ) : tr( "Blocks" ) ).arg( sizeStr ); QString si = QString( "%1 %2 (%3)").arg( blocks ).arg( blocks == 1 ? tr( "Block" ) : tr( "Blocks" ) ).arg( sizeStr );
ui->label_sneek_size->setText( si ); ui->label_sneek_size->setText( si );
@ -652,7 +652,7 @@ void MainWindow::on_comboBox_pcSelect_currentIndexChanged( int index )
float mib = (float)size / 1048576.00f; float mib = (float)size / 1048576.00f;
sizeStr = tr( "%1 MiB" ).arg( mib, 3, 'f', 2 ); sizeStr = tr( "%1 MiB" ).arg( mib, 3, 'f', 2 );
} }
int blocks = RU( 0x20000, size) / 0x20000; int blocks = RU( size, 0x20000 ) / 0x20000;
QString si = QString( "%1 %2 (%3)").arg( blocks ).arg( blocks == 1 ? tr( "Block" ) : tr( "Blocks" ) ).arg( sizeStr ); QString si = QString( "%1 %2 (%3)").arg( blocks ).arg( blocks == 1 ? tr( "Block" ) : tr( "Blocks" ) ).arg( sizeStr );
ui->label_pc_size->setText( si ); ui->label_pc_size->setText( si );

View File

@ -18,7 +18,6 @@ SOURCES += main.cpp\
../WiiQt/savebanner.cpp \ ../WiiQt/savebanner.cpp \
../WiiQt/aes.c \ ../WiiQt/aes.c \
../WiiQt/sha1.c \ ../WiiQt/sha1.c \
../WiiQt/md5.cpp \
../WiiQt/sharedcontentmap.cpp \ ../WiiQt/sharedcontentmap.cpp \
../WiiQt/tiktmd.cpp \ ../WiiQt/tiktmd.cpp \
../WiiQt/uidmap.cpp \ ../WiiQt/uidmap.cpp \