From 930e92ca3c65692f85b94249c6a56233acae9f1d Mon Sep 17 00:00:00 2001 From: "giantpune@gmail.com" Date: Tue, 4 Jan 2011 00:07:21 +0000 Subject: [PATCH] * 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 --- WiiQt/blocks0to7.cpp | 2 +- WiiQt/nandbin.cpp | 2 +- WiiQt/nanddump.cpp | 3 ++- WiiQt/savedatabin.cpp | 30 ++++++++---------------------- WiiQt/tools.cpp | 19 +++++++++++++------ WiiQt/tools.h | 4 +++- WiiQt/wad.cpp | 16 ++++++++-------- nand_dump/mainwindow.cpp | 2 +- saveToy/mainwindow.cpp | 4 ++-- saveToy/saveToy.pro | 3 +-- 10 files changed, 40 insertions(+), 45 deletions(-) diff --git a/WiiQt/blocks0to7.cpp b/WiiQt/blocks0to7.cpp index b6bf0e0..bc649fd 100644 --- a/WiiQt/blocks0to7.cpp +++ b/WiiQt/blocks0to7.cpp @@ -307,7 +307,7 @@ Boot2Info Blocks0to7::CheckHashes( Boot2Info info ) stuff += blocks.at( ret.secondBlock ); 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 ) ); QByteArray realHash = GetSha1( decD ); if( realHash != t.Hash( 0 ) ) diff --git a/WiiQt/nandbin.cpp b/WiiQt/nandbin.cpp index 491f064..2b9bc1c 100755 --- a/WiiQt/nandbin.cpp +++ b/WiiQt/nandbin.cpp @@ -1602,7 +1602,7 @@ bool NandBin::CheckHmacData( quint16 entry ) if( !fst.size ) 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"; quint16 fat = fst.sub; diff --git a/WiiQt/nanddump.cpp b/WiiQt/nanddump.cpp index a99a29c..a119e1f 100644 --- a/WiiQt/nanddump.cpp +++ b/WiiQt/nanddump.cpp @@ -377,8 +377,9 @@ bool NandDump::InstallNusItem( const NusJob &job ) } 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 ); return false; } diff --git a/WiiQt/savedatabin.cpp b/WiiQt/savedatabin.cpp index 7db4fa5..aacd780 100644 --- a/WiiQt/savedatabin.cpp +++ b/WiiQt/savedatabin.cpp @@ -1,6 +1,5 @@ #include "savedatabin.h" #include "aes.h" -#include "md5.h" #include "ec.h" SaveDataBin::SaveDataBin( QByteArray stuff ) @@ -26,13 +25,8 @@ SaveDataBin::SaveDataBin( QByteArray stuff ) //check MD5 quint8 md5blanker[ 16 ] = MD5_BLANKER; QByteArray expected = header.mid( 0xe, 16 ); - QByteArray headerWithBlanker = header.left( 0xe ) + QByteArray( (const char*)&md5blanker, 16 ) + header.right( 0xf0a2 ); - //hexdump( headerWithBlanker.left( 0x50 ) ); - MD5 hash; - hash.update( headerWithBlanker.data(), size ); - hash.finalize(); - - QByteArray actual = QByteArray( (const char*)hash.hexdigestChar(), 16 ); + QByteArray headerWithBlanker = header.left( 0xe ) + QByteArray( (const char*)&md5blanker, 16 ) + header.right( 0xf0a2 ); + QByteArray actual = GetMd5( headerWithBlanker ); if( actual != expected ) { qWarning() << "SaveDataBin::SaveDataBin -> md5 mismatch"; @@ -260,19 +254,15 @@ const QByteArray SaveDataBin::Data( const QByteArray &ngPriv, const QByteArray & b.write( (const char*)&tmp8, 1 ); tmp8 = 0; //nocopy or some shit like that? b.write( (const char*)&tmp8, 1 ); - b.close(); + b.close(); QByteArray header2 = header + QByteArray( (const char*)&md5blanker, 16 ) + QByteArray( 2, '\0' ); //md5 blanker + padding to 0x20 - header2 += bnr; //add the banner.bin - header2 = PaddedByteArray( header2, 0xf0c0 ); //pad to 0xf0c0 + header2 += bnr; //add the banner.bin + header2 = PaddedByteArray( header2, 0xf0c0 ); //pad to 0xf0c0 - MD5 hash; - hash.update( header2.data(), 0xf0c0 ); - hash.finalize(); - - QByteArray actual = QByteArray( (const char*)hash.hexdigestChar(), 16 ); - header += actual + QByteArray( 2, '\0' ) + bnr; + QByteArray actual = GetMd5( header2 ); + header += actual + QByteArray( 2, '\0' ) + bnr; header = PaddedByteArray( header, 0xf0c0 ); quint8 iv[ 16 ] = SD_IV; @@ -510,11 +500,7 @@ quint32 SaveDataBin::GetSize( QByteArray dataBin ) quint8 md5blanker[ 16 ] = MD5_BLANKER; QByteArray expected = header.mid( 0xe, 16 ); QByteArray headerWithBlanker = header.left( 0xe ) + QByteArray( (const char*)&md5blanker, 16 ) + header.right( 0xf0a2 ); - MD5 hash; - hash.update( headerWithBlanker.data(), size ); - hash.finalize(); - - QByteArray actual = QByteArray( (const char*)hash.hexdigestChar(), 16 ); + QByteArray actual = GetMd5( headerWithBlanker ); if( actual != expected ) { qWarning() << "SaveDataBin::GetSize -> md5 mismatch"; diff --git a/WiiQt/tools.cpp b/WiiQt/tools.cpp index 6c5eff7..f539e74 100644 --- a/WiiQt/tools.cpp +++ b/WiiQt/tools.cpp @@ -83,8 +83,8 @@ void hexdump12( const QByteArray &d, int from, int len ) QByteArray PaddedByteArray( const QByteArray &orig, quint32 padTo ) { - QByteArray padding( RU( orig.size(), padTo ) - orig.size(), '\0' ); - //qDebug() << "padding with" << hex << RU( padTo, orig.size() ) << "bytes" << + //qDebug() << "need to pad from" << hex << orig.size() << "to nearest" << padTo; + QByteArray padding( RU( orig.size(), padTo ) - orig.size(), '\0' ); return orig + padding; } @@ -121,7 +121,8 @@ void AesSetKey( const QByteArray &key ) QByteArray GetSha1( const QByteArray &stuff ) { - SHA1Context sha; + return QCryptographicHash::hash( stuff, QCryptographicHash::Sha1 ); + /*SHA1Context sha; SHA1Reset( &sha ); SHA1Input( &sha, (const unsigned char*)stuff.data(), stuff.size() ); if( !SHA1Result( &sha ) ) @@ -135,9 +136,15 @@ QByteArray GetSha1( const QByteArray &stuff ) { quint32 part = qFromBigEndian( sha.Message_Digest[ i ] ); 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 ) diff --git a/WiiQt/tools.h b/WiiQt/tools.h index 6d78dae..86eb818 100644 --- a/WiiQt/tools.h +++ b/WiiQt/tools.h @@ -3,7 +3,8 @@ #include "includes.h" //#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 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 GetSha1( const QByteArray &stuff ); +QByteArray GetMd5( const QByteArray &stuff ); bool IsValidSave( const SaveGame &save ); const QByteArray DataFromSave( const SaveGame &save, const QString &name ); diff --git a/WiiQt/wad.cpp b/WiiQt/wad.cpp index f662316..3f97e38 100644 --- a/WiiQt/wad.cpp +++ b/WiiQt/wad.cpp @@ -66,7 +66,7 @@ Wad::Wad( const QByteArray &stuff ) //sanity check this thing 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" ); return; @@ -74,11 +74,11 @@ Wad::Wad( const QByteArray &stuff ) quint32 pos = 0x40; certData = stuff.mid( pos, certSize ); - pos += RU( 0x40, certSize ); + pos += RU( certSize, 0x4 ); tikData = stuff.mid( pos, tikSize ); - pos += RU( 0x40, tikSize ); + pos += RU( tikSize, 0x40 ); tmdData = stuff.mid( pos, tmdSize ); - pos += RU( 0x40, tmdSize ); + pos += RU( tmdSize, 0x40 ); Ticket ticket( tikData ); Tmd t( tmdData ); @@ -106,7 +106,7 @@ Wad::Wad( const QByteArray &stuff ) 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 << "with size" << s; QByteArray encData = stuff.mid( pos, s ); @@ -280,13 +280,13 @@ const QByteArray Wad::Data( quint32 magicWord, const QByteArray &footer ) quint16 cnt = t.Count(); for( quint16 i = 0; i < cnt; i++ ) { - quint32 s = RU( 0x40, partsEnc.at( i ).size() ); - if( RU( 0x40, t.Size( i ) ) != s ) + quint32 s = RU( partsEnc.at( i ).size(), 0x40 ); + if( RU( t.Size( i ), 0x40 ) != s ) { Err( QString( "Size of content %1 is bad ( %2, %3, %4 )" ) .arg( i ) .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 ) ); return QByteArray(); } diff --git a/nand_dump/mainwindow.cpp b/nand_dump/mainwindow.cpp index 306f672..918af73 100644 --- a/nand_dump/mainwindow.cpp +++ b/nand_dump/mainwindow.cpp @@ -140,7 +140,7 @@ void MainWindow::ReceiveTitleFromNus( NusJob job ) if( ok ) ShowMessage( tr( "Installed %1 title to nand" ).arg( title ) ); else - ShowMessage( tr( "Error %1 title to nand" ).arg( title ) ); + ShowMessage( tr( "Error installing %1 title to nand" ).arg( title ) ); } else if( ui->radioButton_wad->isChecked() ) { diff --git a/saveToy/mainwindow.cpp b/saveToy/mainwindow.cpp index 1563581..849c9f8 100644 --- a/saveToy/mainwindow.cpp +++ b/saveToy/mainwindow.cpp @@ -194,7 +194,7 @@ void MainWindow::ShowSneekSaveDetails( SaveListItem *item ) float mib = (float)size / 1048576.00f; 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 ); ui->label_sneek_size->setText( si ); @@ -652,7 +652,7 @@ void MainWindow::on_comboBox_pcSelect_currentIndexChanged( int index ) float mib = (float)size / 1048576.00f; 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 ); ui->label_pc_size->setText( si ); diff --git a/saveToy/saveToy.pro b/saveToy/saveToy.pro index 51a77c4..6693e93 100644 --- a/saveToy/saveToy.pro +++ b/saveToy/saveToy.pro @@ -17,8 +17,7 @@ SOURCES += main.cpp\ ../WiiQt/tools.cpp \ ../WiiQt/savebanner.cpp \ ../WiiQt/aes.c \ - ../WiiQt/sha1.c \ - ../WiiQt/md5.cpp \ + ../WiiQt/sha1.c \ ../WiiQt/sharedcontentmap.cpp \ ../WiiQt/tiktmd.cpp \ ../WiiQt/uidmap.cpp \