Adds experimental data.bin export of wii saves,
use at your own risk as it has been tested only to the point that a real wii recognizes the save on an sd card Please post test results here or on issue 1945 removed the automated backup when importing from a save.bin fixed display of pokemon titles in memcardmanager git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@4829 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
parent
2db709aeb6
commit
b186f0821e
|
@ -98,6 +98,11 @@ bool CVolumeWiiCrypted::Read(u64 _ReadOffset, u64 _Length, u8* _pBuffer) const
|
|||
return(true);
|
||||
}
|
||||
|
||||
bool CVolumeWiiCrypted::GetTitleID(u8* _pBuffer) const
|
||||
{
|
||||
return RAWRead((u64)0x0F8001DC, 8, _pBuffer);
|
||||
}
|
||||
|
||||
std::string CVolumeWiiCrypted::GetUniqueID() const
|
||||
{
|
||||
if (m_pReader == NULL)
|
||||
|
|
|
@ -33,6 +33,7 @@ public:
|
|||
~CVolumeWiiCrypted();
|
||||
bool Read(u64 _Offset, u64 _Length, u8* _pBuffer) const;
|
||||
bool RAWRead(u64 _Offset, u64 _Length, u8* _pBuffer) const;
|
||||
bool GetTitleID(u8* _pBuffer) const;
|
||||
std::string GetUniqueID() const;
|
||||
std::string GetMakerID() const;
|
||||
std::string GetName() const;
|
||||
|
|
|
@ -1542,6 +1542,38 @@
|
|||
>
|
||||
</File>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="Crypto"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\Src\MemoryCards\Crypto\bn.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\Src\MemoryCards\Crypto\ec.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\Src\MemoryCards\Crypto\md5.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\Src\MemoryCards\Crypto\md5.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\Src\MemoryCards\Crypto\sha1.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\Src\MemoryCards\Crypto\sha1.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\Src\MemoryCards\Crypto\tools.h"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
<File
|
||||
RelativePath=".\src\main.cpp"
|
||||
>
|
||||
|
|
|
@ -49,7 +49,6 @@ Core::GetWindowHandle().
|
|||
#include "ConfigMain.h"
|
||||
#include "PluginManager.h"
|
||||
#include "MemcardManager.h"
|
||||
#include "MemoryCards/WiiSaveCrypted.h"
|
||||
#include "CheatsWindow.h"
|
||||
#include "LuaWindow.h"
|
||||
#include "AboutDolphin.h"
|
||||
|
@ -814,8 +813,8 @@ void CFrame::OnImportSave(wxCommandEvent& WXUNUSED (event))
|
|||
|
||||
if (!path.IsEmpty())
|
||||
{
|
||||
CWiiSaveCrypted saveFile(path.ToUTF8().data());
|
||||
saveFile.Extract();
|
||||
CWiiSaveCrypted* saveFile = new CWiiSaveCrypted(path.ToUTF8().data());
|
||||
delete saveFile;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -106,6 +106,7 @@ BEGIN_EVENT_TABLE(CGameListCtrl, wxListCtrl)
|
|||
EVT_MENU(IDM_PROPERTIES, CGameListCtrl::OnProperties)
|
||||
EVT_MENU(IDM_OPENCONTAININGFOLDER, CGameListCtrl::OnOpenContainingFolder)
|
||||
EVT_MENU(IDM_OPENSAVEFOLDER, CGameListCtrl::OnOpenSaveFolder)
|
||||
EVT_MENU(IDM_EXPORTSAVE, CGameListCtrl::OnExportSave)
|
||||
EVT_MENU(IDM_SETDEFAULTGCM, CGameListCtrl::OnSetDefaultGCM)
|
||||
EVT_MENU(IDM_COMPRESSGCM, CGameListCtrl::OnCompressGCM)
|
||||
EVT_MENU(IDM_MULTICOMPRESSGCM, CGameListCtrl::OnMultiCompressGCM)
|
||||
|
@ -803,8 +804,10 @@ void CGameListCtrl::OnRightClick(wxMouseEvent& event)
|
|||
popupMenu->AppendSeparator();
|
||||
|
||||
if (selected_iso->GetPlatform() != GameListItem::GAMECUBE_DISC)
|
||||
{
|
||||
popupMenu->Append(IDM_OPENSAVEFOLDER, _("Open Wii &save folder"));
|
||||
|
||||
popupMenu->Append(IDM_EXPORTSAVE, _("Export Wii save (Experimental)"));
|
||||
}
|
||||
popupMenu->Append(IDM_OPENCONTAININGFOLDER, _("Open &containing folder"));
|
||||
popupMenu->AppendCheckItem(IDM_SETDEFAULTGCM, _("Set as &default ISO"));
|
||||
|
||||
|
@ -883,6 +886,24 @@ void CGameListCtrl::OnOpenSaveFolder(wxCommandEvent& WXUNUSED (event))
|
|||
WxUtils::Explore(iso->GetWiiFSPath().c_str());
|
||||
}
|
||||
|
||||
void CGameListCtrl::OnExportSave(wxCommandEvent& WXUNUSED (event))
|
||||
{
|
||||
const GameListItem *iso = GetSelectedISO();
|
||||
if (!iso)
|
||||
return;
|
||||
u64 title;
|
||||
DiscIO::IVolume *Iso = DiscIO::CreateVolumeFromFilename(iso->GetFileName());
|
||||
if (Iso)
|
||||
{
|
||||
if (Iso->GetTitleID((u8*)&title))
|
||||
{
|
||||
title = Common::swap64(title);
|
||||
CWiiSaveCrypted* exportSave = new CWiiSaveCrypted("", title);
|
||||
delete exportSave;
|
||||
}
|
||||
delete Iso;
|
||||
}
|
||||
}
|
||||
|
||||
// =======================================================
|
||||
// Save this file as the default file
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
#include <wx/tipwin.h>
|
||||
|
||||
#include "ISOFile.h"
|
||||
|
||||
#include "MemoryCards/WiiSaveCrypted.h"
|
||||
|
||||
class wxEmuStateTip : public wxTipWindow
|
||||
{
|
||||
|
@ -98,6 +98,7 @@ private:
|
|||
void OnProperties(wxCommandEvent& event);
|
||||
void OnOpenContainingFolder(wxCommandEvent& event);
|
||||
void OnOpenSaveFolder(wxCommandEvent& event);
|
||||
void OnExportSave(wxCommandEvent& event);
|
||||
void OnSetDefaultGCM(wxCommandEvent& event);
|
||||
void OnDeleteGCM(wxCommandEvent& event);
|
||||
void OnCompressGCM(wxCommandEvent& event);
|
||||
|
|
|
@ -224,6 +224,7 @@ enum
|
|||
IDM_NOTIFYMAPLOADED,
|
||||
IDM_OPENCONTAININGFOLDER,
|
||||
IDM_OPENSAVEFOLDER,
|
||||
IDM_EXPORTSAVE,
|
||||
IDM_IMPORTSAVE,
|
||||
IDM_SETDEFAULTGCM,
|
||||
IDM_DELETEGCM,
|
||||
|
|
|
@ -24,7 +24,6 @@
|
|||
#include "ISOFile.h"
|
||||
#include "StringUtil.h"
|
||||
|
||||
#include "VolumeCreator.h"
|
||||
#include "Filesystem.h"
|
||||
#include "BannerLoader.h"
|
||||
#include "FileSearch.h"
|
||||
|
@ -216,6 +215,7 @@ const std::string GameListItem::GetWiiFSPath() const
|
|||
{
|
||||
DiscIO::IVolume *Iso = DiscIO::CreateVolumeFromFilename(m_FileName);
|
||||
|
||||
std::string ret("NULL");
|
||||
if (Iso != NULL)
|
||||
{
|
||||
if (DiscIO::IsVolumeWiiDisc(Iso) || DiscIO::IsVolumeWadFile(Iso))
|
||||
|
@ -223,11 +223,7 @@ const std::string GameListItem::GetWiiFSPath() const
|
|||
char Path[250];
|
||||
u64 Title;
|
||||
|
||||
if (DiscIO::IsVolumeWiiDisc(Iso))
|
||||
Iso->RAWRead((u64)0x0F8001DC, 8, (u8*)&Title);
|
||||
else
|
||||
Iso->GetTitleID((u8*)&Title);
|
||||
|
||||
Iso->GetTitleID((u8*)&Title);
|
||||
Title = Common::swap64(Title);
|
||||
|
||||
sprintf(Path, FULL_WII_USER_DIR "title/%08x/%08x/data/", (u32)(Title>>32), (u32)Title);
|
||||
|
@ -235,10 +231,11 @@ const std::string GameListItem::GetWiiFSPath() const
|
|||
if (!File::Exists(Path))
|
||||
File::CreateFullPath(Path);
|
||||
|
||||
return std::string(wxGetCwd().mb_str()) + std::string(Path).substr(strlen(ROOT_DIR));
|
||||
ret = std::string(wxGetCwd().mb_str()) + std::string(Path).substr(strlen(ROOT_DIR));
|
||||
}
|
||||
delete Iso;
|
||||
}
|
||||
|
||||
return "NULL";
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
#define __ISOFILE_H_
|
||||
|
||||
#include "Volume.h"
|
||||
#include "VolumeCreator.h"
|
||||
|
||||
class PointerWrap;
|
||||
class GameListItem
|
||||
|
|
|
@ -757,9 +757,10 @@ bool CMemcardManager::ReloadMemcard(const char *fileName, int card)
|
|||
if (!memoryCard[card]->DEntry_Comment1(j, title)) title[0]=0;
|
||||
if (!memoryCard[card]->DEntry_Comment2(j, comment)) comment[0]=0;
|
||||
|
||||
bool ascii = memoryCard[card]->IsAsciiEncoding();
|
||||
wxCSConv SJISConv(wxT("SHIFT_JIS"));
|
||||
wxTitle = wxString(title, SJISConv);
|
||||
wxComment = wxString(comment, SJISConv);
|
||||
wxTitle = wxString(title, ascii ? *wxConvCurrent : SJISConv);
|
||||
wxComment = wxString(comment, ascii ? *wxConvCurrent : SJISConv);
|
||||
|
||||
m_MemcardList[card]->SetItem(index, COLUMN_TITLE, wxTitle);
|
||||
m_MemcardList[card]->SetItem(index, COLUMN_COMMENT, wxComment);
|
||||
|
|
|
@ -0,0 +1,123 @@
|
|||
// Copyright 2007,2008 Segher Boessenkool <segher@kernel.crashing.org>
|
||||
// Licensed under the terms of the GNU GPL, version 2
|
||||
// http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "Common.h"
|
||||
#include "tools.h"
|
||||
|
||||
/*static void bn_print(char *name, u8 *a, u32 n)
|
||||
{
|
||||
u32 i;
|
||||
|
||||
printf("%s = ", name);
|
||||
|
||||
for (i = 0; i < n; i++)
|
||||
printf("%02x", a[i]);
|
||||
|
||||
printf("\n");
|
||||
}*/
|
||||
|
||||
static void bn_zero(u8 *d, u32 n)
|
||||
{
|
||||
memset(d, 0, n);
|
||||
}
|
||||
|
||||
static void bn_copy(u8 *d, u8 *a, u32 n)
|
||||
{
|
||||
memcpy(d, a, n);
|
||||
}
|
||||
|
||||
int bn_compare(u8 *a, u8 *b, u32 n)
|
||||
{
|
||||
u32 i;
|
||||
|
||||
for (i = 0; i < n; i++) {
|
||||
if (a[i] < b[i])
|
||||
return -1;
|
||||
if (a[i] > b[i])
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void bn_sub_modulus(u8 *a, u8 *N, u32 n)
|
||||
{
|
||||
u32 i;
|
||||
u32 dig;
|
||||
u8 c;
|
||||
|
||||
c = 0;
|
||||
for (i = n - 1; i < n; i--) {
|
||||
dig = N[i] + c;
|
||||
c = (a[i] < dig);
|
||||
a[i] -= dig;
|
||||
}
|
||||
}
|
||||
|
||||
void bn_add(u8 *d, u8 *a, u8 *b, u8 *N, u32 n)
|
||||
{
|
||||
u32 i;
|
||||
u32 dig;
|
||||
u8 c;
|
||||
|
||||
c = 0;
|
||||
for (i = n - 1; i < n; i--) {
|
||||
dig = a[i] + b[i] + c;
|
||||
c = (dig >= 0x100);
|
||||
d[i] = dig;
|
||||
}
|
||||
|
||||
if (c)
|
||||
bn_sub_modulus(d, N, n);
|
||||
|
||||
if (bn_compare(d, N, n) >= 0)
|
||||
bn_sub_modulus(d, N, n);
|
||||
}
|
||||
|
||||
void bn_mul(u8 *d, u8 *a, u8 *b, u8 *N, u32 n)
|
||||
{
|
||||
u32 i;
|
||||
u8 mask;
|
||||
|
||||
bn_zero(d, n);
|
||||
|
||||
for (i = 0; i < n; i++)
|
||||
for (mask = 0x80; mask != 0; mask >>= 1) {
|
||||
bn_add(d, d, d, N, n);
|
||||
if ((a[i] & mask) != 0)
|
||||
bn_add(d, d, b, N, n);
|
||||
}
|
||||
}
|
||||
|
||||
void bn_exp(u8 *d, u8 *a, u8 *N, u32 n, u8 *e, u32 en)
|
||||
{
|
||||
u8 t[512];
|
||||
u32 i;
|
||||
u8 mask;
|
||||
|
||||
bn_zero(d, n);
|
||||
d[n-1] = 1;
|
||||
for (i = 0; i < en; i++)
|
||||
for (mask = 0x80; mask != 0; mask >>= 1) {
|
||||
bn_mul(t, d, d, N, n);
|
||||
if ((e[i] & mask) != 0)
|
||||
bn_mul(d, t, a, N, n);
|
||||
else
|
||||
bn_copy(d, t, n);
|
||||
}
|
||||
}
|
||||
|
||||
// only for prime N -- stupid but lazy, see if I care
|
||||
void bn_inv(u8 *d, u8 *a, u8 *N, u32 n)
|
||||
{
|
||||
u8 t[512], s[512];
|
||||
|
||||
bn_copy(t, N, n);
|
||||
bn_zero(s, n);
|
||||
s[n-1] = 2;
|
||||
bn_sub_modulus(t, s, n);
|
||||
bn_exp(d, a, N, n, t, n);
|
||||
}
|
|
@ -0,0 +1,397 @@
|
|||
// Copyright 2007,2008 Segher Boessenkool <segher@kernel.crashing.org>
|
||||
// Licensed under the terms of the GNU GPL, version 2
|
||||
// http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "Common.h"
|
||||
#include "tools.h"
|
||||
#include "time.h"
|
||||
#include "stdlib.h"
|
||||
// y**2 + x*y = x**3 + x + b
|
||||
static u8 ec_b[30] =
|
||||
{0x00,0x66,0x64,0x7e,0xde,0x6c,0x33,0x2c,0x7f,0x8c,0x09,0x23,0xbb,0x58,0x21
|
||||
,0x3b,0x33,0x3b,0x20,0xe9,0xce,0x42,0x81,0xfe,0x11,0x5f,0x7d,0x8f,0x90,0xad};
|
||||
|
||||
// order of the addition group of points
|
||||
static u8 ec_N[30] =
|
||||
{0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
|
||||
,0x13,0xe9,0x74,0xe7,0x2f,0x8a,0x69,0x22,0x03,0x1d,0x26,0x03,0xcf,0xe0,0xd7};
|
||||
|
||||
// base point
|
||||
static u8 ec_G[60] =
|
||||
{0x00,0xfa,0xc9,0xdf,0xcb,0xac,0x83,0x13,0xbb,0x21,0x39,0xf1,0xbb,0x75,0x5f
|
||||
,0xef,0x65,0xbc,0x39,0x1f,0x8b,0x36,0xf8,0xf8,0xeb,0x73,0x71,0xfd,0x55,0x8b
|
||||
,0x01,0x00,0x6a,0x08,0xa4,0x19,0x03,0x35,0x06,0x78,0xe5,0x85,0x28,0xbe,0xbf
|
||||
,0x8a,0x0b,0xef,0xf8,0x67,0xa7,0xca,0x36,0x71,0x6f,0x7e,0x01,0xf8,0x10,0x52};
|
||||
|
||||
/*static void elt_print(char *name, u8 *a)
|
||||
{
|
||||
u32 i;
|
||||
|
||||
printf("%s = ", name);
|
||||
|
||||
for (i = 0; i < 30; i++)
|
||||
printf("%02x", a[i]);
|
||||
|
||||
printf("\n");
|
||||
}*/
|
||||
|
||||
static void elt_copy(u8 *d, u8 *a)
|
||||
{
|
||||
memcpy(d, a, 30);
|
||||
}
|
||||
|
||||
static void elt_zero(u8 *d)
|
||||
{
|
||||
memset(d, 0, 30);
|
||||
}
|
||||
|
||||
static int elt_is_zero(u8 *d)
|
||||
{
|
||||
u32 i;
|
||||
|
||||
for (i = 0; i < 30; i++)
|
||||
if (d[i] != 0)
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void elt_add(u8 *d, u8 *a, u8 *b)
|
||||
{
|
||||
u32 i;
|
||||
|
||||
for (i = 0; i < 30; i++)
|
||||
d[i] = a[i] ^ b[i];
|
||||
}
|
||||
|
||||
static void elt_mul_x(u8 *d, u8 *a)
|
||||
{
|
||||
u8 carry, x, y;
|
||||
u32 i;
|
||||
|
||||
carry = a[0] & 1;
|
||||
|
||||
x = 0;
|
||||
for (i = 0; i < 29; i++) {
|
||||
y = a[i + 1];
|
||||
d[i] = x ^ (y >> 7);
|
||||
x = y << 1;
|
||||
}
|
||||
d[29] = x ^ carry;
|
||||
|
||||
d[20] ^= carry << 2;
|
||||
}
|
||||
|
||||
static void elt_mul(u8 *d, u8 *a, u8 *b)
|
||||
{
|
||||
u32 i, n;
|
||||
u8 mask;
|
||||
|
||||
elt_zero(d);
|
||||
|
||||
i = 0;
|
||||
mask = 1;
|
||||
for (n = 0; n < 233; n++) {
|
||||
elt_mul_x(d, d);
|
||||
|
||||
if ((a[i] & mask) != 0)
|
||||
elt_add(d, d, b);
|
||||
|
||||
mask >>= 1;
|
||||
if (mask == 0) {
|
||||
mask = 0x80;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static const u8 square[16] =
|
||||
{0x00,0x01,0x04,0x05,0x10,0x11,0x14,0x15,0x40,0x41,0x44,0x45,0x50,0x51,0x54,0x55};
|
||||
|
||||
static void elt_square_to_wide(u8 *d, u8 *a)
|
||||
{
|
||||
u32 i;
|
||||
|
||||
for (i = 0; i < 30; i++) {
|
||||
d[2*i] = square[a[i] >> 4];
|
||||
d[2*i + 1] = square[a[i] & 15];
|
||||
}
|
||||
}
|
||||
|
||||
static void wide_reduce(u8 *d)
|
||||
{
|
||||
u32 i;
|
||||
u8 x;
|
||||
|
||||
for (i = 0; i < 30; i++) {
|
||||
x = d[i];
|
||||
|
||||
d[i + 19] ^= x >> 7;
|
||||
d[i + 20] ^= x << 1;
|
||||
|
||||
d[i + 29] ^= x >> 1;
|
||||
d[i + 30] ^= x << 7;
|
||||
}
|
||||
|
||||
x = d[30] & ~1;
|
||||
|
||||
d[49] ^= x >> 7;
|
||||
d[50] ^= x << 1;
|
||||
|
||||
d[59] ^= x >> 1;
|
||||
|
||||
d[30] &= 1;
|
||||
}
|
||||
|
||||
static void elt_square(u8 *d, u8 *a)
|
||||
{
|
||||
u8 wide[60];
|
||||
|
||||
elt_square_to_wide(wide, a);
|
||||
wide_reduce(wide);
|
||||
|
||||
elt_copy(d, wide + 30);
|
||||
}
|
||||
|
||||
static void itoh_tsujii(u8 *d, u8 *a, u8 *b, u32 j)
|
||||
{
|
||||
u8 t[30];
|
||||
|
||||
elt_copy(t, a);
|
||||
while (j--) {
|
||||
elt_square(d, t);
|
||||
elt_copy(t, d);
|
||||
}
|
||||
|
||||
elt_mul(d, t, b);
|
||||
}
|
||||
|
||||
static void elt_inv(u8 *d, u8 *a)
|
||||
{
|
||||
u8 t[30];
|
||||
u8 s[30];
|
||||
|
||||
itoh_tsujii(t, a, a, 1);
|
||||
itoh_tsujii(s, t, a, 1);
|
||||
itoh_tsujii(t, s, s, 3);
|
||||
itoh_tsujii(s, t, a, 1);
|
||||
itoh_tsujii(t, s, s, 7);
|
||||
itoh_tsujii(s, t, t, 14);
|
||||
itoh_tsujii(t, s, a, 1);
|
||||
itoh_tsujii(s, t, t, 29);
|
||||
itoh_tsujii(t, s, s, 58);
|
||||
itoh_tsujii(s, t, t, 116);
|
||||
elt_square(d, s);
|
||||
}
|
||||
|
||||
/*static int point_is_on_curve(u8 *p)
|
||||
{
|
||||
u8 s[30], t[30];
|
||||
u8 *x, *y;
|
||||
|
||||
x = p;
|
||||
y = p + 30;
|
||||
|
||||
elt_square(t, x);
|
||||
elt_mul(s, t, x);
|
||||
|
||||
elt_add(s, s, t);
|
||||
|
||||
elt_square(t, y);
|
||||
elt_add(s, s, t);
|
||||
|
||||
elt_mul(t, x, y);
|
||||
elt_add(s, s, t);
|
||||
|
||||
elt_add(s, s, ec_b);
|
||||
|
||||
return elt_is_zero(s);
|
||||
}
|
||||
*/
|
||||
static int point_is_zero(u8 *p)
|
||||
{
|
||||
return elt_is_zero(p) && elt_is_zero(p + 30);
|
||||
}
|
||||
|
||||
static void point_double(u8 *r, u8 *p)
|
||||
{
|
||||
u8 s[30], t[30];
|
||||
u8 *px, *py, *rx, *ry;
|
||||
|
||||
px = p;
|
||||
py = p + 30;
|
||||
rx = r;
|
||||
ry = r + 30;
|
||||
|
||||
if (elt_is_zero(px)) {
|
||||
elt_zero(rx);
|
||||
elt_zero(ry);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
elt_inv(t, px);
|
||||
elt_mul(s, py, t);
|
||||
elt_add(s, s, px);
|
||||
|
||||
elt_square(t, px);
|
||||
|
||||
elt_square(rx, s);
|
||||
elt_add(rx, rx, s);
|
||||
rx[29] ^= 1;
|
||||
|
||||
elt_mul(ry, s, rx);
|
||||
elt_add(ry, ry, rx);
|
||||
elt_add(ry, ry, t);
|
||||
}
|
||||
|
||||
static void point_add(u8 *r, u8 *p, u8 *q)
|
||||
{
|
||||
u8 s[30], t[30], u[30];
|
||||
u8 *px, *py, *qx, *qy, *rx, *ry;
|
||||
|
||||
px = p;
|
||||
py = p + 30;
|
||||
qx = q;
|
||||
qy = q + 30;
|
||||
rx = r;
|
||||
ry = r + 30;
|
||||
|
||||
if (point_is_zero(p)) {
|
||||
elt_copy(rx, qx);
|
||||
elt_copy(ry, qy);
|
||||
return;
|
||||
}
|
||||
|
||||
if (point_is_zero(q)) {
|
||||
elt_copy(rx, px);
|
||||
elt_copy(ry, py);
|
||||
return;
|
||||
}
|
||||
|
||||
elt_add(u, px, qx);
|
||||
|
||||
if (elt_is_zero(u)) {
|
||||
elt_add(u, py, qy);
|
||||
if (elt_is_zero(u))
|
||||
point_double(r, p);
|
||||
else {
|
||||
elt_zero(rx);
|
||||
elt_zero(ry);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
elt_inv(t, u);
|
||||
elt_add(u, py, qy);
|
||||
elt_mul(s, t, u);
|
||||
|
||||
elt_square(t, s);
|
||||
elt_add(t, t, s);
|
||||
elt_add(t, t, qx);
|
||||
t[29] ^= 1;
|
||||
|
||||
elt_mul(u, s, t);
|
||||
elt_add(s, u, py);
|
||||
elt_add(rx, t, px);
|
||||
elt_add(ry, s, rx);
|
||||
}
|
||||
|
||||
static void point_mul(u8 *d, u8 *a, u8 *b) // a is bignum
|
||||
{
|
||||
u32 i;
|
||||
u8 mask;
|
||||
|
||||
elt_zero(d);
|
||||
elt_zero(d + 30);
|
||||
|
||||
for (i = 0; i < 30; i++)
|
||||
for (mask = 0x80; mask != 0; mask >>= 1) {
|
||||
point_double(d, d);
|
||||
if ((a[i] & mask) != 0)
|
||||
point_add(d, d, b);
|
||||
}
|
||||
}
|
||||
|
||||
void silly_random(u8 * rndArea, u8 count)
|
||||
{
|
||||
u16 i;
|
||||
srand((unsigned) (time(NULL)));
|
||||
|
||||
for(i=0;i<count;i++)
|
||||
{
|
||||
rndArea[i]=rand();
|
||||
}
|
||||
}
|
||||
|
||||
void generate_ecdsa(u8 *R, u8 *S, u8 *k, u8 *hash)
|
||||
{
|
||||
u8 e[30];
|
||||
u8 kk[30];
|
||||
u8 m[30];
|
||||
u8 minv[30];
|
||||
u8 mG[60];
|
||||
//FILE *fp;
|
||||
|
||||
elt_zero(e);
|
||||
memcpy(e + 10, hash, 20);
|
||||
|
||||
//Changing random number generator to a lame one...
|
||||
silly_random(m, sizeof(m));
|
||||
//fp = fopen("/dev/random", "rb");
|
||||
//if (fread(m, sizeof m, 1, fp) != 1)
|
||||
// fatal("reading random");
|
||||
//fclose(fp);
|
||||
m[0] = 0;
|
||||
|
||||
// R = (mG).x
|
||||
|
||||
point_mul(mG, m, ec_G);
|
||||
elt_copy(R, mG);
|
||||
if (bn_compare(R, ec_N, 30) >= 0)
|
||||
bn_sub_modulus(R, ec_N, 30);
|
||||
|
||||
// S = m**-1*(e + Rk) (mod N)
|
||||
|
||||
elt_copy(kk, k);
|
||||
if (bn_compare(kk, ec_N, 30) >= 0)
|
||||
bn_sub_modulus(kk, ec_N, 30);
|
||||
bn_mul(S, R, kk, ec_N, 30);
|
||||
bn_add(kk, S, e, ec_N, 30);
|
||||
bn_inv(minv, m, ec_N, 30);
|
||||
bn_mul(S, minv, kk, ec_N, 30);
|
||||
}
|
||||
|
||||
int check_ecdsa(u8 *Q, u8 *R, u8 *S, u8 *hash)
|
||||
{
|
||||
u8 Sinv[30];
|
||||
u8 e[30];
|
||||
u8 w1[30], w2[30];
|
||||
u8 r1[60], r2[60];
|
||||
|
||||
bn_inv(Sinv, S, ec_N, 30);
|
||||
|
||||
elt_zero(e);
|
||||
memcpy(e + 10, hash, 20);
|
||||
|
||||
bn_mul(w1, e, Sinv, ec_N, 30);
|
||||
bn_mul(w2, R, Sinv, ec_N, 30);
|
||||
|
||||
point_mul(r1, w1, ec_G);
|
||||
point_mul(r2, w2, Q);
|
||||
|
||||
point_add(r1, r1, r2);
|
||||
|
||||
if (bn_compare(r1, ec_N, 30) >= 0)
|
||||
bn_sub_modulus(r1, ec_N, 30);
|
||||
|
||||
return (bn_compare(r1, R, 30) == 0);
|
||||
}
|
||||
|
||||
void ec_priv_to_pub(u8 *k, u8 *Q)
|
||||
{
|
||||
point_mul(Q, k, ec_G);
|
||||
}
|
|
@ -0,0 +1,570 @@
|
|||
/*
|
||||
* RFC 1321 compliant MD5 implementation
|
||||
*
|
||||
* Copyright (C) 2006-2009, Paul Bakker <polarssl_maintainer at polarssl.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Joined copyright on original XySSL code with: Christophe Devine
|
||||
*
|
||||
* 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 program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
/*
|
||||
* The MD5 algorithm was designed by Ron Rivest in 1991.
|
||||
*
|
||||
* http://www.ietf.org/rfc/rfc1321.txt
|
||||
*/
|
||||
|
||||
//#include "polarssl/config.h"
|
||||
|
||||
#if defined(POLARSSL_MD5_C)
|
||||
|
||||
#include "polarssl/md5.h"
|
||||
#else
|
||||
#include "md5.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
/*
|
||||
* 32-bit integer manipulation macros (little endian)
|
||||
*/
|
||||
#ifndef GET_ULONG_LE
|
||||
#define GET_ULONG_LE(n,b,i) \
|
||||
{ \
|
||||
(n) = ( (unsigned long) (b)[(i) ] ) \
|
||||
| ( (unsigned long) (b)[(i) + 1] << 8 ) \
|
||||
| ( (unsigned long) (b)[(i) + 2] << 16 ) \
|
||||
| ( (unsigned long) (b)[(i) + 3] << 24 ); \
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef PUT_ULONG_LE
|
||||
#define PUT_ULONG_LE(n,b,i) \
|
||||
{ \
|
||||
(b)[(i) ] = (unsigned char) ( (n) ); \
|
||||
(b)[(i) + 1] = (unsigned char) ( (n) >> 8 ); \
|
||||
(b)[(i) + 2] = (unsigned char) ( (n) >> 16 ); \
|
||||
(b)[(i) + 3] = (unsigned char) ( (n) >> 24 ); \
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* MD5 context setup
|
||||
*/
|
||||
void md5_starts( md5_context *ctx )
|
||||
{
|
||||
ctx->total[0] = 0;
|
||||
ctx->total[1] = 0;
|
||||
|
||||
ctx->state[0] = 0x67452301;
|
||||
ctx->state[1] = 0xEFCDAB89;
|
||||
ctx->state[2] = 0x98BADCFE;
|
||||
ctx->state[3] = 0x10325476;
|
||||
}
|
||||
|
||||
static void md5_process( md5_context *ctx, unsigned char data[64] )
|
||||
{
|
||||
unsigned long X[16], A, B, C, D;
|
||||
|
||||
GET_ULONG_LE( X[ 0], data, 0 );
|
||||
GET_ULONG_LE( X[ 1], data, 4 );
|
||||
GET_ULONG_LE( X[ 2], data, 8 );
|
||||
GET_ULONG_LE( X[ 3], data, 12 );
|
||||
GET_ULONG_LE( X[ 4], data, 16 );
|
||||
GET_ULONG_LE( X[ 5], data, 20 );
|
||||
GET_ULONG_LE( X[ 6], data, 24 );
|
||||
GET_ULONG_LE( X[ 7], data, 28 );
|
||||
GET_ULONG_LE( X[ 8], data, 32 );
|
||||
GET_ULONG_LE( X[ 9], data, 36 );
|
||||
GET_ULONG_LE( X[10], data, 40 );
|
||||
GET_ULONG_LE( X[11], data, 44 );
|
||||
GET_ULONG_LE( X[12], data, 48 );
|
||||
GET_ULONG_LE( X[13], data, 52 );
|
||||
GET_ULONG_LE( X[14], data, 56 );
|
||||
GET_ULONG_LE( X[15], data, 60 );
|
||||
|
||||
#define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
|
||||
|
||||
#define P(a,b,c,d,k,s,t) \
|
||||
{ \
|
||||
a += F(b,c,d) + X[k] + t; a = S(a,s) + b; \
|
||||
}
|
||||
|
||||
A = ctx->state[0];
|
||||
B = ctx->state[1];
|
||||
C = ctx->state[2];
|
||||
D = ctx->state[3];
|
||||
|
||||
#define F(x,y,z) (z ^ (x & (y ^ z)))
|
||||
|
||||
P( A, B, C, D, 0, 7, 0xD76AA478 );
|
||||
P( D, A, B, C, 1, 12, 0xE8C7B756 );
|
||||
P( C, D, A, B, 2, 17, 0x242070DB );
|
||||
P( B, C, D, A, 3, 22, 0xC1BDCEEE );
|
||||
P( A, B, C, D, 4, 7, 0xF57C0FAF );
|
||||
P( D, A, B, C, 5, 12, 0x4787C62A );
|
||||
P( C, D, A, B, 6, 17, 0xA8304613 );
|
||||
P( B, C, D, A, 7, 22, 0xFD469501 );
|
||||
P( A, B, C, D, 8, 7, 0x698098D8 );
|
||||
P( D, A, B, C, 9, 12, 0x8B44F7AF );
|
||||
P( C, D, A, B, 10, 17, 0xFFFF5BB1 );
|
||||
P( B, C, D, A, 11, 22, 0x895CD7BE );
|
||||
P( A, B, C, D, 12, 7, 0x6B901122 );
|
||||
P( D, A, B, C, 13, 12, 0xFD987193 );
|
||||
P( C, D, A, B, 14, 17, 0xA679438E );
|
||||
P( B, C, D, A, 15, 22, 0x49B40821 );
|
||||
|
||||
#undef F
|
||||
|
||||
#define F(x,y,z) (y ^ (z & (x ^ y)))
|
||||
|
||||
P( A, B, C, D, 1, 5, 0xF61E2562 );
|
||||
P( D, A, B, C, 6, 9, 0xC040B340 );
|
||||
P( C, D, A, B, 11, 14, 0x265E5A51 );
|
||||
P( B, C, D, A, 0, 20, 0xE9B6C7AA );
|
||||
P( A, B, C, D, 5, 5, 0xD62F105D );
|
||||
P( D, A, B, C, 10, 9, 0x02441453 );
|
||||
P( C, D, A, B, 15, 14, 0xD8A1E681 );
|
||||
P( B, C, D, A, 4, 20, 0xE7D3FBC8 );
|
||||
P( A, B, C, D, 9, 5, 0x21E1CDE6 );
|
||||
P( D, A, B, C, 14, 9, 0xC33707D6 );
|
||||
P( C, D, A, B, 3, 14, 0xF4D50D87 );
|
||||
P( B, C, D, A, 8, 20, 0x455A14ED );
|
||||
P( A, B, C, D, 13, 5, 0xA9E3E905 );
|
||||
P( D, A, B, C, 2, 9, 0xFCEFA3F8 );
|
||||
P( C, D, A, B, 7, 14, 0x676F02D9 );
|
||||
P( B, C, D, A, 12, 20, 0x8D2A4C8A );
|
||||
|
||||
#undef F
|
||||
|
||||
#define F(x,y,z) (x ^ y ^ z)
|
||||
|
||||
P( A, B, C, D, 5, 4, 0xFFFA3942 );
|
||||
P( D, A, B, C, 8, 11, 0x8771F681 );
|
||||
P( C, D, A, B, 11, 16, 0x6D9D6122 );
|
||||
P( B, C, D, A, 14, 23, 0xFDE5380C );
|
||||
P( A, B, C, D, 1, 4, 0xA4BEEA44 );
|
||||
P( D, A, B, C, 4, 11, 0x4BDECFA9 );
|
||||
P( C, D, A, B, 7, 16, 0xF6BB4B60 );
|
||||
P( B, C, D, A, 10, 23, 0xBEBFBC70 );
|
||||
P( A, B, C, D, 13, 4, 0x289B7EC6 );
|
||||
P( D, A, B, C, 0, 11, 0xEAA127FA );
|
||||
P( C, D, A, B, 3, 16, 0xD4EF3085 );
|
||||
P( B, C, D, A, 6, 23, 0x04881D05 );
|
||||
P( A, B, C, D, 9, 4, 0xD9D4D039 );
|
||||
P( D, A, B, C, 12, 11, 0xE6DB99E5 );
|
||||
P( C, D, A, B, 15, 16, 0x1FA27CF8 );
|
||||
P( B, C, D, A, 2, 23, 0xC4AC5665 );
|
||||
|
||||
#undef F
|
||||
|
||||
#define F(x,y,z) (y ^ (x | ~z))
|
||||
|
||||
P( A, B, C, D, 0, 6, 0xF4292244 );
|
||||
P( D, A, B, C, 7, 10, 0x432AFF97 );
|
||||
P( C, D, A, B, 14, 15, 0xAB9423A7 );
|
||||
P( B, C, D, A, 5, 21, 0xFC93A039 );
|
||||
P( A, B, C, D, 12, 6, 0x655B59C3 );
|
||||
P( D, A, B, C, 3, 10, 0x8F0CCC92 );
|
||||
P( C, D, A, B, 10, 15, 0xFFEFF47D );
|
||||
P( B, C, D, A, 1, 21, 0x85845DD1 );
|
||||
P( A, B, C, D, 8, 6, 0x6FA87E4F );
|
||||
P( D, A, B, C, 15, 10, 0xFE2CE6E0 );
|
||||
P( C, D, A, B, 6, 15, 0xA3014314 );
|
||||
P( B, C, D, A, 13, 21, 0x4E0811A1 );
|
||||
P( A, B, C, D, 4, 6, 0xF7537E82 );
|
||||
P( D, A, B, C, 11, 10, 0xBD3AF235 );
|
||||
P( C, D, A, B, 2, 15, 0x2AD7D2BB );
|
||||
P( B, C, D, A, 9, 21, 0xEB86D391 );
|
||||
|
||||
#undef F
|
||||
|
||||
ctx->state[0] += A;
|
||||
ctx->state[1] += B;
|
||||
ctx->state[2] += C;
|
||||
ctx->state[3] += D;
|
||||
}
|
||||
|
||||
/*
|
||||
* MD5 process buffer
|
||||
*/
|
||||
void md5_update( md5_context *ctx, unsigned char *input, int ilen )
|
||||
{
|
||||
int fill;
|
||||
unsigned long left;
|
||||
|
||||
if( ilen <= 0 )
|
||||
return;
|
||||
|
||||
left = ctx->total[0] & 0x3F;
|
||||
fill = 64 - left;
|
||||
|
||||
ctx->total[0] += ilen;
|
||||
ctx->total[0] &= 0xFFFFFFFF;
|
||||
|
||||
if( ctx->total[0] < (unsigned long) ilen )
|
||||
ctx->total[1]++;
|
||||
|
||||
if( left && ilen >= fill )
|
||||
{
|
||||
memcpy( (void *) (ctx->buffer + left),
|
||||
(void *) input, fill );
|
||||
md5_process( ctx, ctx->buffer );
|
||||
input += fill;
|
||||
ilen -= fill;
|
||||
left = 0;
|
||||
}
|
||||
|
||||
while( ilen >= 64 )
|
||||
{
|
||||
md5_process( ctx, input );
|
||||
input += 64;
|
||||
ilen -= 64;
|
||||
}
|
||||
|
||||
if( ilen > 0 )
|
||||
{
|
||||
memcpy( (void *) (ctx->buffer + left),
|
||||
(void *) input, ilen );
|
||||
}
|
||||
}
|
||||
|
||||
static const unsigned char md5_padding[64] =
|
||||
{
|
||||
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||
};
|
||||
|
||||
/*
|
||||
* MD5 final digest
|
||||
*/
|
||||
void md5_finish( md5_context *ctx, unsigned char output[16] )
|
||||
{
|
||||
unsigned long last, padn;
|
||||
unsigned long high, low;
|
||||
unsigned char msglen[8];
|
||||
|
||||
high = ( ctx->total[0] >> 29 )
|
||||
| ( ctx->total[1] << 3 );
|
||||
low = ( ctx->total[0] << 3 );
|
||||
|
||||
PUT_ULONG_LE( low, msglen, 0 );
|
||||
PUT_ULONG_LE( high, msglen, 4 );
|
||||
|
||||
last = ctx->total[0] & 0x3F;
|
||||
padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
|
||||
|
||||
md5_update( ctx, (unsigned char *) md5_padding, padn );
|
||||
md5_update( ctx, msglen, 8 );
|
||||
|
||||
PUT_ULONG_LE( ctx->state[0], output, 0 );
|
||||
PUT_ULONG_LE( ctx->state[1], output, 4 );
|
||||
PUT_ULONG_LE( ctx->state[2], output, 8 );
|
||||
PUT_ULONG_LE( ctx->state[3], output, 12 );
|
||||
}
|
||||
|
||||
/*
|
||||
* output = MD5( input buffer )
|
||||
*/
|
||||
void md5( unsigned char *input, int ilen, unsigned char output[16] )
|
||||
{
|
||||
md5_context ctx;
|
||||
|
||||
md5_starts( &ctx );
|
||||
md5_update( &ctx, input, ilen );
|
||||
md5_finish( &ctx, output );
|
||||
|
||||
memset( &ctx, 0, sizeof( md5_context ) );
|
||||
}
|
||||
|
||||
/*
|
||||
* output = MD5( file contents )
|
||||
*/
|
||||
int md5_file( char *path, unsigned char output[16] )
|
||||
{
|
||||
FILE *f;
|
||||
size_t n;
|
||||
md5_context ctx;
|
||||
unsigned char buf[1024];
|
||||
|
||||
if( ( f = fopen( path, "rb" ) ) == NULL )
|
||||
return( 1 );
|
||||
|
||||
md5_starts( &ctx );
|
||||
|
||||
while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
|
||||
md5_update( &ctx, buf, (int) n );
|
||||
|
||||
md5_finish( &ctx, output );
|
||||
|
||||
memset( &ctx, 0, sizeof( md5_context ) );
|
||||
|
||||
if( ferror( f ) != 0 )
|
||||
{
|
||||
fclose( f );
|
||||
return( 2 );
|
||||
}
|
||||
|
||||
fclose( f );
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
/*
|
||||
* MD5 HMAC context setup
|
||||
*/
|
||||
void md5_hmac_starts( md5_context *ctx, unsigned char *key, int keylen )
|
||||
{
|
||||
int i;
|
||||
unsigned char sum[16];
|
||||
|
||||
if( keylen > 64 )
|
||||
{
|
||||
md5( key, keylen, sum );
|
||||
keylen = 16;
|
||||
key = sum;
|
||||
}
|
||||
|
||||
memset( ctx->ipad, 0x36, 64 );
|
||||
memset( ctx->opad, 0x5C, 64 );
|
||||
|
||||
for( i = 0; i < keylen; i++ )
|
||||
{
|
||||
ctx->ipad[i] = (unsigned char)( ctx->ipad[i] ^ key[i] );
|
||||
ctx->opad[i] = (unsigned char)( ctx->opad[i] ^ key[i] );
|
||||
}
|
||||
|
||||
md5_starts( ctx );
|
||||
md5_update( ctx, ctx->ipad, 64 );
|
||||
|
||||
memset( sum, 0, sizeof( sum ) );
|
||||
}
|
||||
|
||||
/*
|
||||
* MD5 HMAC process buffer
|
||||
*/
|
||||
void md5_hmac_update( md5_context *ctx, unsigned char *input, int ilen )
|
||||
{
|
||||
md5_update( ctx, input, ilen );
|
||||
}
|
||||
|
||||
/*
|
||||
* MD5 HMAC final digest
|
||||
*/
|
||||
void md5_hmac_finish( md5_context *ctx, unsigned char output[16] )
|
||||
{
|
||||
unsigned char tmpbuf[16];
|
||||
|
||||
md5_finish( ctx, tmpbuf );
|
||||
md5_starts( ctx );
|
||||
md5_update( ctx, ctx->opad, 64 );
|
||||
md5_update( ctx, tmpbuf, 16 );
|
||||
md5_finish( ctx, output );
|
||||
|
||||
memset( tmpbuf, 0, sizeof( tmpbuf ) );
|
||||
}
|
||||
|
||||
/*
|
||||
* output = HMAC-MD5( hmac key, input buffer )
|
||||
*/
|
||||
void md5_hmac( unsigned char *key, int keylen, unsigned char *input, int ilen,
|
||||
unsigned char output[16] )
|
||||
{
|
||||
md5_context ctx;
|
||||
|
||||
md5_hmac_starts( &ctx, key, keylen );
|
||||
md5_hmac_update( &ctx, input, ilen );
|
||||
md5_hmac_finish( &ctx, output );
|
||||
|
||||
memset( &ctx, 0, sizeof( md5_context ) );
|
||||
}
|
||||
|
||||
#if defined(POLARSSL_SELF_TEST)
|
||||
/*
|
||||
* RFC 1321 test vectors
|
||||
*/
|
||||
static unsigned char md5_test_buf[7][81] =
|
||||
{
|
||||
{ "" },
|
||||
{ "a" },
|
||||
{ "abc" },
|
||||
{ "message digest" },
|
||||
{ "abcdefghijklmnopqrstuvwxyz" },
|
||||
{ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" },
|
||||
{ "12345678901234567890123456789012345678901234567890123456789012" \
|
||||
"345678901234567890" }
|
||||
};
|
||||
|
||||
static const int md5_test_buflen[7] =
|
||||
{
|
||||
0, 1, 3, 14, 26, 62, 80
|
||||
};
|
||||
|
||||
static const unsigned char md5_test_sum[7][16] =
|
||||
{
|
||||
{ 0xD4, 0x1D, 0x8C, 0xD9, 0x8F, 0x00, 0xB2, 0x04,
|
||||
0xE9, 0x80, 0x09, 0x98, 0xEC, 0xF8, 0x42, 0x7E },
|
||||
{ 0x0C, 0xC1, 0x75, 0xB9, 0xC0, 0xF1, 0xB6, 0xA8,
|
||||
0x31, 0xC3, 0x99, 0xE2, 0x69, 0x77, 0x26, 0x61 },
|
||||
{ 0x90, 0x01, 0x50, 0x98, 0x3C, 0xD2, 0x4F, 0xB0,
|
||||
0xD6, 0x96, 0x3F, 0x7D, 0x28, 0xE1, 0x7F, 0x72 },
|
||||
{ 0xF9, 0x6B, 0x69, 0x7D, 0x7C, 0xB7, 0x93, 0x8D,
|
||||
0x52, 0x5A, 0x2F, 0x31, 0xAA, 0xF1, 0x61, 0xD0 },
|
||||
{ 0xC3, 0xFC, 0xD3, 0xD7, 0x61, 0x92, 0xE4, 0x00,
|
||||
0x7D, 0xFB, 0x49, 0x6C, 0xCA, 0x67, 0xE1, 0x3B },
|
||||
{ 0xD1, 0x74, 0xAB, 0x98, 0xD2, 0x77, 0xD9, 0xF5,
|
||||
0xA5, 0x61, 0x1C, 0x2C, 0x9F, 0x41, 0x9D, 0x9F },
|
||||
{ 0x57, 0xED, 0xF4, 0xA2, 0x2B, 0xE3, 0xC9, 0x55,
|
||||
0xAC, 0x49, 0xDA, 0x2E, 0x21, 0x07, 0xB6, 0x7A }
|
||||
};
|
||||
|
||||
/*
|
||||
* RFC 2202 test vectors
|
||||
*/
|
||||
static unsigned char md5_hmac_test_key[7][26] =
|
||||
{
|
||||
{ "\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B" },
|
||||
{ "Jefe" },
|
||||
{ "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" },
|
||||
{ "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10"
|
||||
"\x11\x12\x13\x14\x15\x16\x17\x18\x19" },
|
||||
{ "\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C" },
|
||||
{ "" }, /* 0xAA 80 times */
|
||||
{ "" }
|
||||
};
|
||||
|
||||
static const int md5_hmac_test_keylen[7] =
|
||||
{
|
||||
16, 4, 16, 25, 16, 80, 80
|
||||
};
|
||||
|
||||
static unsigned char md5_hmac_test_buf[7][74] =
|
||||
{
|
||||
{ "Hi There" },
|
||||
{ "what do ya want for nothing?" },
|
||||
{ "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
|
||||
"\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
|
||||
"\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
|
||||
"\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
|
||||
"\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" },
|
||||
{ "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
|
||||
"\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
|
||||
"\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
|
||||
"\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
|
||||
"\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" },
|
||||
{ "Test With Truncation" },
|
||||
{ "Test Using Larger Than Block-Size Key - Hash Key First" },
|
||||
{ "Test Using Larger Than Block-Size Key and Larger"
|
||||
" Than One Block-Size Data" }
|
||||
};
|
||||
|
||||
static const int md5_hmac_test_buflen[7] =
|
||||
{
|
||||
8, 28, 50, 50, 20, 54, 73
|
||||
};
|
||||
|
||||
static const unsigned char md5_hmac_test_sum[7][16] =
|
||||
{
|
||||
{ 0x92, 0x94, 0x72, 0x7A, 0x36, 0x38, 0xBB, 0x1C,
|
||||
0x13, 0xF4, 0x8E, 0xF8, 0x15, 0x8B, 0xFC, 0x9D },
|
||||
{ 0x75, 0x0C, 0x78, 0x3E, 0x6A, 0xB0, 0xB5, 0x03,
|
||||
0xEA, 0xA8, 0x6E, 0x31, 0x0A, 0x5D, 0xB7, 0x38 },
|
||||
{ 0x56, 0xBE, 0x34, 0x52, 0x1D, 0x14, 0x4C, 0x88,
|
||||
0xDB, 0xB8, 0xC7, 0x33, 0xF0, 0xE8, 0xB3, 0xF6 },
|
||||
{ 0x69, 0x7E, 0xAF, 0x0A, 0xCA, 0x3A, 0x3A, 0xEA,
|
||||
0x3A, 0x75, 0x16, 0x47, 0x46, 0xFF, 0xAA, 0x79 },
|
||||
{ 0x56, 0x46, 0x1E, 0xF2, 0x34, 0x2E, 0xDC, 0x00,
|
||||
0xF9, 0xBA, 0xB9, 0x95 },
|
||||
{ 0x6B, 0x1A, 0xB7, 0xFE, 0x4B, 0xD7, 0xBF, 0x8F,
|
||||
0x0B, 0x62, 0xE6, 0xCE, 0x61, 0xB9, 0xD0, 0xCD },
|
||||
{ 0x6F, 0x63, 0x0F, 0xAD, 0x67, 0xCD, 0xA0, 0xEE,
|
||||
0x1F, 0xB1, 0xF5, 0x62, 0xDB, 0x3A, 0xA5, 0x3E }
|
||||
};
|
||||
|
||||
/*
|
||||
* Checkup routine
|
||||
*/
|
||||
int md5_self_test( int verbose )
|
||||
{
|
||||
int i, buflen;
|
||||
unsigned char buf[1024];
|
||||
unsigned char md5sum[16];
|
||||
md5_context ctx;
|
||||
|
||||
for( i = 0; i < 7; i++ )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
printf( " MD5 test #%d: ", i + 1 );
|
||||
|
||||
md5( md5_test_buf[i], md5_test_buflen[i], md5sum );
|
||||
|
||||
if( memcmp( md5sum, md5_test_sum[i], 16 ) != 0 )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
printf( "failed\n" );
|
||||
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
if( verbose != 0 )
|
||||
printf( "passed\n" );
|
||||
}
|
||||
|
||||
if( verbose != 0 )
|
||||
printf( "\n" );
|
||||
|
||||
for( i = 0; i < 7; i++ )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
printf( " HMAC-MD5 test #%d: ", i + 1 );
|
||||
|
||||
if( i == 5 || i == 6 )
|
||||
{
|
||||
memset( buf, '\xAA', buflen = 80 );
|
||||
md5_hmac_starts( &ctx, buf, buflen );
|
||||
}
|
||||
else
|
||||
md5_hmac_starts( &ctx, md5_hmac_test_key[i],
|
||||
md5_hmac_test_keylen[i] );
|
||||
|
||||
md5_hmac_update( &ctx, md5_hmac_test_buf[i],
|
||||
md5_hmac_test_buflen[i] );
|
||||
|
||||
md5_hmac_finish( &ctx, md5sum );
|
||||
|
||||
buflen = ( i == 4 ) ? 12 : 16;
|
||||
|
||||
if( memcmp( md5sum, md5_hmac_test_sum[i], buflen ) != 0 )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
printf( "failed\n" );
|
||||
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
if( verbose != 0 )
|
||||
printf( "passed\n" );
|
||||
}
|
||||
|
||||
if( verbose != 0 )
|
||||
printf( "\n" );
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1,138 @@
|
|||
/**
|
||||
* \file md5.h
|
||||
*
|
||||
* Copyright (C) 2006-2009, Paul Bakker <polarssl_maintainer at polarssl.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Joined copyright on original XySSL code with: Christophe Devine
|
||||
*
|
||||
* 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 program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
#ifndef POLARSSL_MD5_H
|
||||
#define POLARSSL_MD5_H
|
||||
|
||||
/**
|
||||
* \brief MD5 context structure
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
unsigned long total[2]; /*!< number of bytes processed */
|
||||
unsigned long state[4]; /*!< intermediate digest state */
|
||||
unsigned char buffer[64]; /*!< data block being processed */
|
||||
|
||||
unsigned char ipad[64]; /*!< HMAC: inner padding */
|
||||
unsigned char opad[64]; /*!< HMAC: outer padding */
|
||||
}
|
||||
md5_context;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \brief MD5 context setup
|
||||
*
|
||||
* \param ctx context to be initialized
|
||||
*/
|
||||
void md5_starts( md5_context *ctx );
|
||||
|
||||
/**
|
||||
* \brief MD5 process buffer
|
||||
*
|
||||
* \param ctx MD5 context
|
||||
* \param input buffer holding the data
|
||||
* \param ilen length of the input data
|
||||
*/
|
||||
void md5_update( md5_context *ctx, unsigned char *input, int ilen );
|
||||
|
||||
/**
|
||||
* \brief MD5 final digest
|
||||
*
|
||||
* \param ctx MD5 context
|
||||
* \param output MD5 checksum result
|
||||
*/
|
||||
void md5_finish( md5_context *ctx, unsigned char output[16] );
|
||||
|
||||
/**
|
||||
* \brief Output = MD5( input buffer )
|
||||
*
|
||||
* \param input buffer holding the data
|
||||
* \param ilen length of the input data
|
||||
* \param output MD5 checksum result
|
||||
*/
|
||||
void md5( unsigned char *input, int ilen, unsigned char output[16] );
|
||||
|
||||
/**
|
||||
* \brief Output = MD5( file contents )
|
||||
*
|
||||
* \param path input file name
|
||||
* \param output MD5 checksum result
|
||||
*
|
||||
* \return 0 if successful, 1 if fopen failed,
|
||||
* or 2 if fread failed
|
||||
*/
|
||||
int md5_file( char *path, unsigned char output[16] );
|
||||
|
||||
/**
|
||||
* \brief MD5 HMAC context setup
|
||||
*
|
||||
* \param ctx HMAC context to be initialized
|
||||
* \param key HMAC secret key
|
||||
* \param keylen length of the HMAC key
|
||||
*/
|
||||
void md5_hmac_starts( md5_context *ctx, unsigned char *key, int keylen );
|
||||
|
||||
/**
|
||||
* \brief MD5 HMAC process buffer
|
||||
*
|
||||
* \param ctx HMAC context
|
||||
* \param input buffer holding the data
|
||||
* \param ilen length of the input data
|
||||
*/
|
||||
void md5_hmac_update( md5_context *ctx, unsigned char *input, int ilen );
|
||||
|
||||
/**
|
||||
* \brief MD5 HMAC final digest
|
||||
*
|
||||
* \param ctx HMAC context
|
||||
* \param output MD5 HMAC checksum result
|
||||
*/
|
||||
void md5_hmac_finish( md5_context *ctx, unsigned char output[16] );
|
||||
|
||||
/**
|
||||
* \brief Output = HMAC-MD5( hmac key, input buffer )
|
||||
*
|
||||
* \param key HMAC secret key
|
||||
* \param keylen length of the HMAC key
|
||||
* \param input buffer holding the data
|
||||
* \param ilen length of the input data
|
||||
* \param output HMAC-MD5 result
|
||||
*/
|
||||
void md5_hmac( unsigned char *key, int keylen,
|
||||
unsigned char *input, int ilen,
|
||||
unsigned char output[16] );
|
||||
|
||||
/**
|
||||
* \brief Checkup routine
|
||||
*
|
||||
* \return 0 if successful, or 1 if the test failed
|
||||
*/
|
||||
int md5_self_test( int verbose );
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* md5.h */
|
|
@ -0,0 +1,611 @@
|
|||
/*
|
||||
* FIPS-180-1 compliant SHA-1 implementation
|
||||
*
|
||||
* Copyright (C) 2006-2009, Paul Bakker <polarssl_maintainer at polarssl.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Joined copyright on original XySSL code with: Christophe Devine
|
||||
*
|
||||
* 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 program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
/*
|
||||
* The SHA-1 standard was published by NIST in 1993.
|
||||
*
|
||||
* http://www.itl.nist.gov/fipspubs/fip180-1.htm
|
||||
*/
|
||||
/*
|
||||
#include "polarssl/config.h"
|
||||
|
||||
#if defined(POLARSSL_SHA1_C)
|
||||
|
||||
#include "polarssl/sha1.h"
|
||||
*/
|
||||
#include "sha1.h"
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
/*
|
||||
* 32-bit integer manipulation macros (big endian)
|
||||
*/
|
||||
#ifndef GET_ULONG_BE
|
||||
#define GET_ULONG_BE(n,b,i) \
|
||||
{ \
|
||||
(n) = ( (unsigned long) (b)[(i) ] << 24 ) \
|
||||
| ( (unsigned long) (b)[(i) + 1] << 16 ) \
|
||||
| ( (unsigned long) (b)[(i) + 2] << 8 ) \
|
||||
| ( (unsigned long) (b)[(i) + 3] ); \
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef PUT_ULONG_BE
|
||||
#define PUT_ULONG_BE(n,b,i) \
|
||||
{ \
|
||||
(b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
|
||||
(b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
|
||||
(b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
|
||||
(b)[(i) + 3] = (unsigned char) ( (n) ); \
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* SHA-1 context setup
|
||||
*/
|
||||
void sha1_starts( sha1_context *ctx )
|
||||
{
|
||||
ctx->total[0] = 0;
|
||||
ctx->total[1] = 0;
|
||||
|
||||
ctx->state[0] = 0x67452301;
|
||||
ctx->state[1] = 0xEFCDAB89;
|
||||
ctx->state[2] = 0x98BADCFE;
|
||||
ctx->state[3] = 0x10325476;
|
||||
ctx->state[4] = 0xC3D2E1F0;
|
||||
}
|
||||
|
||||
static void sha1_process( sha1_context *ctx, unsigned char data[64] )
|
||||
{
|
||||
unsigned long temp, W[16], A, B, C, D, E;
|
||||
|
||||
GET_ULONG_BE( W[ 0], data, 0 );
|
||||
GET_ULONG_BE( W[ 1], data, 4 );
|
||||
GET_ULONG_BE( W[ 2], data, 8 );
|
||||
GET_ULONG_BE( W[ 3], data, 12 );
|
||||
GET_ULONG_BE( W[ 4], data, 16 );
|
||||
GET_ULONG_BE( W[ 5], data, 20 );
|
||||
GET_ULONG_BE( W[ 6], data, 24 );
|
||||
GET_ULONG_BE( W[ 7], data, 28 );
|
||||
GET_ULONG_BE( W[ 8], data, 32 );
|
||||
GET_ULONG_BE( W[ 9], data, 36 );
|
||||
GET_ULONG_BE( W[10], data, 40 );
|
||||
GET_ULONG_BE( W[11], data, 44 );
|
||||
GET_ULONG_BE( W[12], data, 48 );
|
||||
GET_ULONG_BE( W[13], data, 52 );
|
||||
GET_ULONG_BE( W[14], data, 56 );
|
||||
GET_ULONG_BE( W[15], data, 60 );
|
||||
|
||||
#define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
|
||||
|
||||
#define R(t) \
|
||||
( \
|
||||
temp = W[(t - 3) & 0x0F] ^ W[(t - 8) & 0x0F] ^ \
|
||||
W[(t - 14) & 0x0F] ^ W[ t & 0x0F], \
|
||||
( W[t & 0x0F] = S(temp,1) ) \
|
||||
)
|
||||
|
||||
#define P(a,b,c,d,e,x) \
|
||||
{ \
|
||||
e += S(a,5) + F(b,c,d) + K + x; b = S(b,30); \
|
||||
}
|
||||
|
||||
A = ctx->state[0];
|
||||
B = ctx->state[1];
|
||||
C = ctx->state[2];
|
||||
D = ctx->state[3];
|
||||
E = ctx->state[4];
|
||||
|
||||
#define F(x,y,z) (z ^ (x & (y ^ z)))
|
||||
#define K 0x5A827999
|
||||
|
||||
P( A, B, C, D, E, W[0] );
|
||||
P( E, A, B, C, D, W[1] );
|
||||
P( D, E, A, B, C, W[2] );
|
||||
P( C, D, E, A, B, W[3] );
|
||||
P( B, C, D, E, A, W[4] );
|
||||
P( A, B, C, D, E, W[5] );
|
||||
P( E, A, B, C, D, W[6] );
|
||||
P( D, E, A, B, C, W[7] );
|
||||
P( C, D, E, A, B, W[8] );
|
||||
P( B, C, D, E, A, W[9] );
|
||||
P( A, B, C, D, E, W[10] );
|
||||
P( E, A, B, C, D, W[11] );
|
||||
P( D, E, A, B, C, W[12] );
|
||||
P( C, D, E, A, B, W[13] );
|
||||
P( B, C, D, E, A, W[14] );
|
||||
P( A, B, C, D, E, W[15] );
|
||||
P( E, A, B, C, D, R(16) );
|
||||
P( D, E, A, B, C, R(17) );
|
||||
P( C, D, E, A, B, R(18) );
|
||||
P( B, C, D, E, A, R(19) );
|
||||
|
||||
#undef K
|
||||
#undef F
|
||||
|
||||
#define F(x,y,z) (x ^ y ^ z)
|
||||
#define K 0x6ED9EBA1
|
||||
|
||||
P( A, B, C, D, E, R(20) );
|
||||
P( E, A, B, C, D, R(21) );
|
||||
P( D, E, A, B, C, R(22) );
|
||||
P( C, D, E, A, B, R(23) );
|
||||
P( B, C, D, E, A, R(24) );
|
||||
P( A, B, C, D, E, R(25) );
|
||||
P( E, A, B, C, D, R(26) );
|
||||
P( D, E, A, B, C, R(27) );
|
||||
P( C, D, E, A, B, R(28) );
|
||||
P( B, C, D, E, A, R(29) );
|
||||
P( A, B, C, D, E, R(30) );
|
||||
P( E, A, B, C, D, R(31) );
|
||||
P( D, E, A, B, C, R(32) );
|
||||
P( C, D, E, A, B, R(33) );
|
||||
P( B, C, D, E, A, R(34) );
|
||||
P( A, B, C, D, E, R(35) );
|
||||
P( E, A, B, C, D, R(36) );
|
||||
P( D, E, A, B, C, R(37) );
|
||||
P( C, D, E, A, B, R(38) );
|
||||
P( B, C, D, E, A, R(39) );
|
||||
|
||||
#undef K
|
||||
#undef F
|
||||
|
||||
#define F(x,y,z) ((x & y) | (z & (x | y)))
|
||||
#define K 0x8F1BBCDC
|
||||
|
||||
P( A, B, C, D, E, R(40) );
|
||||
P( E, A, B, C, D, R(41) );
|
||||
P( D, E, A, B, C, R(42) );
|
||||
P( C, D, E, A, B, R(43) );
|
||||
P( B, C, D, E, A, R(44) );
|
||||
P( A, B, C, D, E, R(45) );
|
||||
P( E, A, B, C, D, R(46) );
|
||||
P( D, E, A, B, C, R(47) );
|
||||
P( C, D, E, A, B, R(48) );
|
||||
P( B, C, D, E, A, R(49) );
|
||||
P( A, B, C, D, E, R(50) );
|
||||
P( E, A, B, C, D, R(51) );
|
||||
P( D, E, A, B, C, R(52) );
|
||||
P( C, D, E, A, B, R(53) );
|
||||
P( B, C, D, E, A, R(54) );
|
||||
P( A, B, C, D, E, R(55) );
|
||||
P( E, A, B, C, D, R(56) );
|
||||
P( D, E, A, B, C, R(57) );
|
||||
P( C, D, E, A, B, R(58) );
|
||||
P( B, C, D, E, A, R(59) );
|
||||
|
||||
#undef K
|
||||
#undef F
|
||||
|
||||
#define F(x,y,z) (x ^ y ^ z)
|
||||
#define K 0xCA62C1D6
|
||||
|
||||
P( A, B, C, D, E, R(60) );
|
||||
P( E, A, B, C, D, R(61) );
|
||||
P( D, E, A, B, C, R(62) );
|
||||
P( C, D, E, A, B, R(63) );
|
||||
P( B, C, D, E, A, R(64) );
|
||||
P( A, B, C, D, E, R(65) );
|
||||
P( E, A, B, C, D, R(66) );
|
||||
P( D, E, A, B, C, R(67) );
|
||||
P( C, D, E, A, B, R(68) );
|
||||
P( B, C, D, E, A, R(69) );
|
||||
P( A, B, C, D, E, R(70) );
|
||||
P( E, A, B, C, D, R(71) );
|
||||
P( D, E, A, B, C, R(72) );
|
||||
P( C, D, E, A, B, R(73) );
|
||||
P( B, C, D, E, A, R(74) );
|
||||
P( A, B, C, D, E, R(75) );
|
||||
P( E, A, B, C, D, R(76) );
|
||||
P( D, E, A, B, C, R(77) );
|
||||
P( C, D, E, A, B, R(78) );
|
||||
P( B, C, D, E, A, R(79) );
|
||||
|
||||
#undef K
|
||||
#undef F
|
||||
|
||||
ctx->state[0] += A;
|
||||
ctx->state[1] += B;
|
||||
ctx->state[2] += C;
|
||||
ctx->state[3] += D;
|
||||
ctx->state[4] += E;
|
||||
}
|
||||
|
||||
/*
|
||||
* SHA-1 process buffer
|
||||
*/
|
||||
void sha1_update( sha1_context *ctx, unsigned char *input, int ilen )
|
||||
{
|
||||
int fill;
|
||||
unsigned long left;
|
||||
|
||||
if( ilen <= 0 )
|
||||
return;
|
||||
|
||||
left = ctx->total[0] & 0x3F;
|
||||
fill = 64 - left;
|
||||
|
||||
ctx->total[0] += ilen;
|
||||
ctx->total[0] &= 0xFFFFFFFF;
|
||||
|
||||
if( ctx->total[0] < (unsigned long) ilen )
|
||||
ctx->total[1]++;
|
||||
|
||||
if( left && ilen >= fill )
|
||||
{
|
||||
memcpy( (void *) (ctx->buffer + left),
|
||||
(void *) input, fill );
|
||||
sha1_process( ctx, ctx->buffer );
|
||||
input += fill;
|
||||
ilen -= fill;
|
||||
left = 0;
|
||||
}
|
||||
|
||||
while( ilen >= 64 )
|
||||
{
|
||||
sha1_process( ctx, input );
|
||||
input += 64;
|
||||
ilen -= 64;
|
||||
}
|
||||
|
||||
if( ilen > 0 )
|
||||
{
|
||||
memcpy( (void *) (ctx->buffer + left),
|
||||
(void *) input, ilen );
|
||||
}
|
||||
}
|
||||
|
||||
static const unsigned char sha1_padding[64] =
|
||||
{
|
||||
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||
};
|
||||
|
||||
/*
|
||||
* SHA-1 final digest
|
||||
*/
|
||||
void sha1_finish( sha1_context *ctx, unsigned char output[20] )
|
||||
{
|
||||
unsigned long last, padn;
|
||||
unsigned long high, low;
|
||||
unsigned char msglen[8];
|
||||
|
||||
high = ( ctx->total[0] >> 29 )
|
||||
| ( ctx->total[1] << 3 );
|
||||
low = ( ctx->total[0] << 3 );
|
||||
|
||||
PUT_ULONG_BE( high, msglen, 0 );
|
||||
PUT_ULONG_BE( low, msglen, 4 );
|
||||
|
||||
last = ctx->total[0] & 0x3F;
|
||||
padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
|
||||
|
||||
sha1_update( ctx, (unsigned char *) sha1_padding, padn );
|
||||
sha1_update( ctx, msglen, 8 );
|
||||
|
||||
PUT_ULONG_BE( ctx->state[0], output, 0 );
|
||||
PUT_ULONG_BE( ctx->state[1], output, 4 );
|
||||
PUT_ULONG_BE( ctx->state[2], output, 8 );
|
||||
PUT_ULONG_BE( ctx->state[3], output, 12 );
|
||||
PUT_ULONG_BE( ctx->state[4], output, 16 );
|
||||
}
|
||||
|
||||
/*
|
||||
* output = SHA-1( input buffer )
|
||||
*/
|
||||
void sha1( unsigned char *input, int ilen, unsigned char output[20] )
|
||||
{
|
||||
sha1_context ctx;
|
||||
|
||||
sha1_starts( &ctx );
|
||||
sha1_update( &ctx, input, ilen );
|
||||
sha1_finish( &ctx, output );
|
||||
|
||||
memset( &ctx, 0, sizeof( sha1_context ) );
|
||||
}
|
||||
|
||||
/*
|
||||
* output = SHA-1( file contents )
|
||||
*/
|
||||
int sha1_file( char *path, unsigned char output[20] )
|
||||
{
|
||||
FILE *f;
|
||||
size_t n;
|
||||
sha1_context ctx;
|
||||
unsigned char buf[1024];
|
||||
|
||||
if( ( f = fopen( path, "rb" ) ) == NULL )
|
||||
return( 1 );
|
||||
|
||||
sha1_starts( &ctx );
|
||||
|
||||
while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
|
||||
sha1_update( &ctx, buf, (int) n );
|
||||
|
||||
sha1_finish( &ctx, output );
|
||||
|
||||
memset( &ctx, 0, sizeof( sha1_context ) );
|
||||
|
||||
if( ferror( f ) != 0 )
|
||||
{
|
||||
fclose( f );
|
||||
return( 2 );
|
||||
}
|
||||
|
||||
fclose( f );
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
/*
|
||||
* SHA-1 HMAC context setup
|
||||
*/
|
||||
void sha1_hmac_starts( sha1_context *ctx, unsigned char *key, int keylen )
|
||||
{
|
||||
int i;
|
||||
unsigned char sum[20];
|
||||
|
||||
if( keylen > 64 )
|
||||
{
|
||||
sha1( key, keylen, sum );
|
||||
keylen = 20;
|
||||
key = sum;
|
||||
}
|
||||
|
||||
memset( ctx->ipad, 0x36, 64 );
|
||||
memset( ctx->opad, 0x5C, 64 );
|
||||
|
||||
for( i = 0; i < keylen; i++ )
|
||||
{
|
||||
ctx->ipad[i] = (unsigned char)( ctx->ipad[i] ^ key[i] );
|
||||
ctx->opad[i] = (unsigned char)( ctx->opad[i] ^ key[i] );
|
||||
}
|
||||
|
||||
sha1_starts( ctx );
|
||||
sha1_update( ctx, ctx->ipad, 64 );
|
||||
|
||||
memset( sum, 0, sizeof( sum ) );
|
||||
}
|
||||
|
||||
/*
|
||||
* SHA-1 HMAC process buffer
|
||||
*/
|
||||
void sha1_hmac_update( sha1_context *ctx, unsigned char *input, int ilen )
|
||||
{
|
||||
sha1_update( ctx, input, ilen );
|
||||
}
|
||||
|
||||
/*
|
||||
* SHA-1 HMAC final digest
|
||||
*/
|
||||
void sha1_hmac_finish( sha1_context *ctx, unsigned char output[20] )
|
||||
{
|
||||
unsigned char tmpbuf[20];
|
||||
|
||||
sha1_finish( ctx, tmpbuf );
|
||||
sha1_starts( ctx );
|
||||
sha1_update( ctx, ctx->opad, 64 );
|
||||
sha1_update( ctx, tmpbuf, 20 );
|
||||
sha1_finish( ctx, output );
|
||||
|
||||
memset( tmpbuf, 0, sizeof( tmpbuf ) );
|
||||
}
|
||||
|
||||
/*
|
||||
* output = HMAC-SHA-1( hmac key, input buffer )
|
||||
*/
|
||||
void sha1_hmac( unsigned char *key, int keylen,
|
||||
unsigned char *input, int ilen,
|
||||
unsigned char output[20] )
|
||||
{
|
||||
sha1_context ctx;
|
||||
|
||||
sha1_hmac_starts( &ctx, key, keylen );
|
||||
sha1_hmac_update( &ctx, input, ilen );
|
||||
sha1_hmac_finish( &ctx, output );
|
||||
|
||||
memset( &ctx, 0, sizeof( sha1_context ) );
|
||||
}
|
||||
|
||||
#if defined(POLARSSL_SELF_TEST)
|
||||
/*
|
||||
* FIPS-180-1 test vectors
|
||||
*/
|
||||
static unsigned char sha1_test_buf[3][57] =
|
||||
{
|
||||
{ "abc" },
|
||||
{ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
|
||||
{ "" }
|
||||
};
|
||||
|
||||
static const int sha1_test_buflen[3] =
|
||||
{
|
||||
3, 56, 1000
|
||||
};
|
||||
|
||||
static const unsigned char sha1_test_sum[3][20] =
|
||||
{
|
||||
{ 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
|
||||
0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
|
||||
{ 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
|
||||
0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
|
||||
{ 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
|
||||
0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
|
||||
};
|
||||
|
||||
/*
|
||||
* RFC 2202 test vectors
|
||||
*/
|
||||
static unsigned char sha1_hmac_test_key[7][26] =
|
||||
{
|
||||
{ "\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B"
|
||||
"\x0B\x0B\x0B\x0B" },
|
||||
{ "Jefe" },
|
||||
{ "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
|
||||
"\xAA\xAA\xAA\xAA" },
|
||||
{ "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10"
|
||||
"\x11\x12\x13\x14\x15\x16\x17\x18\x19" },
|
||||
{ "\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C"
|
||||
"\x0C\x0C\x0C\x0C" },
|
||||
{ "" }, /* 0xAA 80 times */
|
||||
{ "" }
|
||||
};
|
||||
|
||||
static const int sha1_hmac_test_keylen[7] =
|
||||
{
|
||||
20, 4, 20, 25, 20, 80, 80
|
||||
};
|
||||
|
||||
static unsigned char sha1_hmac_test_buf[7][74] =
|
||||
{
|
||||
{ "Hi There" },
|
||||
{ "what do ya want for nothing?" },
|
||||
{ "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
|
||||
"\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
|
||||
"\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
|
||||
"\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
|
||||
"\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" },
|
||||
{ "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
|
||||
"\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
|
||||
"\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
|
||||
"\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
|
||||
"\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" },
|
||||
{ "Test With Truncation" },
|
||||
{ "Test Using Larger Than Block-Size Key - Hash Key First" },
|
||||
{ "Test Using Larger Than Block-Size Key and Larger"
|
||||
" Than One Block-Size Data" }
|
||||
};
|
||||
|
||||
static const int sha1_hmac_test_buflen[7] =
|
||||
{
|
||||
8, 28, 50, 50, 20, 54, 73
|
||||
};
|
||||
|
||||
static const unsigned char sha1_hmac_test_sum[7][20] =
|
||||
{
|
||||
{ 0xB6, 0x17, 0x31, 0x86, 0x55, 0x05, 0x72, 0x64, 0xE2, 0x8B,
|
||||
0xC0, 0xB6, 0xFB, 0x37, 0x8C, 0x8E, 0xF1, 0x46, 0xBE, 0x00 },
|
||||
{ 0xEF, 0xFC, 0xDF, 0x6A, 0xE5, 0xEB, 0x2F, 0xA2, 0xD2, 0x74,
|
||||
0x16, 0xD5, 0xF1, 0x84, 0xDF, 0x9C, 0x25, 0x9A, 0x7C, 0x79 },
|
||||
{ 0x12, 0x5D, 0x73, 0x42, 0xB9, 0xAC, 0x11, 0xCD, 0x91, 0xA3,
|
||||
0x9A, 0xF4, 0x8A, 0xA1, 0x7B, 0x4F, 0x63, 0xF1, 0x75, 0xD3 },
|
||||
{ 0x4C, 0x90, 0x07, 0xF4, 0x02, 0x62, 0x50, 0xC6, 0xBC, 0x84,
|
||||
0x14, 0xF9, 0xBF, 0x50, 0xC8, 0x6C, 0x2D, 0x72, 0x35, 0xDA },
|
||||
{ 0x4C, 0x1A, 0x03, 0x42, 0x4B, 0x55, 0xE0, 0x7F, 0xE7, 0xF2,
|
||||
0x7B, 0xE1 },
|
||||
{ 0xAA, 0x4A, 0xE5, 0xE1, 0x52, 0x72, 0xD0, 0x0E, 0x95, 0x70,
|
||||
0x56, 0x37, 0xCE, 0x8A, 0x3B, 0x55, 0xED, 0x40, 0x21, 0x12 },
|
||||
{ 0xE8, 0xE9, 0x9D, 0x0F, 0x45, 0x23, 0x7D, 0x78, 0x6D, 0x6B,
|
||||
0xBA, 0xA7, 0x96, 0x5C, 0x78, 0x08, 0xBB, 0xFF, 0x1A, 0x91 }
|
||||
};
|
||||
|
||||
/*
|
||||
* Checkup routine
|
||||
*/
|
||||
int sha1_self_test( int verbose )
|
||||
{
|
||||
int i, j, buflen;
|
||||
unsigned char buf[1024];
|
||||
unsigned char sha1sum[20];
|
||||
sha1_context ctx;
|
||||
|
||||
/*
|
||||
* SHA-1
|
||||
*/
|
||||
for( i = 0; i < 3; i++ )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
printf( " SHA-1 test #%d: ", i + 1 );
|
||||
|
||||
sha1_starts( &ctx );
|
||||
|
||||
if( i == 2 )
|
||||
{
|
||||
memset( buf, 'a', buflen = 1000 );
|
||||
|
||||
for( j = 0; j < 1000; j++ )
|
||||
sha1_update( &ctx, buf, buflen );
|
||||
}
|
||||
else
|
||||
sha1_update( &ctx, sha1_test_buf[i],
|
||||
sha1_test_buflen[i] );
|
||||
|
||||
sha1_finish( &ctx, sha1sum );
|
||||
|
||||
if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
printf( "failed\n" );
|
||||
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
if( verbose != 0 )
|
||||
printf( "passed\n" );
|
||||
}
|
||||
|
||||
if( verbose != 0 )
|
||||
printf( "\n" );
|
||||
|
||||
for( i = 0; i < 7; i++ )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
printf( " HMAC-SHA-1 test #%d: ", i + 1 );
|
||||
|
||||
if( i == 5 || i == 6 )
|
||||
{
|
||||
memset( buf, '\xAA', buflen = 80 );
|
||||
sha1_hmac_starts( &ctx, buf, buflen );
|
||||
}
|
||||
else
|
||||
sha1_hmac_starts( &ctx, sha1_hmac_test_key[i],
|
||||
sha1_hmac_test_keylen[i] );
|
||||
|
||||
sha1_hmac_update( &ctx, sha1_hmac_test_buf[i],
|
||||
sha1_hmac_test_buflen[i] );
|
||||
|
||||
sha1_hmac_finish( &ctx, sha1sum );
|
||||
|
||||
buflen = ( i == 4 ) ? 12 : 20;
|
||||
|
||||
if( memcmp( sha1sum, sha1_hmac_test_sum[i], buflen ) != 0 )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
printf( "failed\n" );
|
||||
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
if( verbose != 0 )
|
||||
printf( "passed\n" );
|
||||
}
|
||||
|
||||
if( verbose != 0 )
|
||||
printf( "\n" );
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
//#endif
|
|
@ -0,0 +1,138 @@
|
|||
/**
|
||||
* \file sha1.h
|
||||
*
|
||||
* Copyright (C) 2006-2009, Paul Bakker <polarssl_maintainer at polarssl.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Joined copyright on original XySSL code with: Christophe Devine
|
||||
*
|
||||
* 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 program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
#ifndef POLARSSL_SHA1_H
|
||||
#define POLARSSL_SHA1_H
|
||||
|
||||
/**
|
||||
* \brief SHA-1 context structure
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
unsigned long total[2]; /*!< number of bytes processed */
|
||||
unsigned long state[5]; /*!< intermediate digest state */
|
||||
unsigned char buffer[64]; /*!< data block being processed */
|
||||
|
||||
unsigned char ipad[64]; /*!< HMAC: inner padding */
|
||||
unsigned char opad[64]; /*!< HMAC: outer padding */
|
||||
}
|
||||
sha1_context;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \brief SHA-1 context setup
|
||||
*
|
||||
* \param ctx context to be initialized
|
||||
*/
|
||||
void sha1_starts( sha1_context *ctx );
|
||||
|
||||
/**
|
||||
* \brief SHA-1 process buffer
|
||||
*
|
||||
* \param ctx SHA-1 context
|
||||
* \param input buffer holding the data
|
||||
* \param ilen length of the input data
|
||||
*/
|
||||
void sha1_update( sha1_context *ctx, unsigned char *input, int ilen );
|
||||
|
||||
/**
|
||||
* \brief SHA-1 final digest
|
||||
*
|
||||
* \param ctx SHA-1 context
|
||||
* \param output SHA-1 checksum result
|
||||
*/
|
||||
void sha1_finish( sha1_context *ctx, unsigned char output[20] );
|
||||
|
||||
/**
|
||||
* \brief Output = SHA-1( input buffer )
|
||||
*
|
||||
* \param input buffer holding the data
|
||||
* \param ilen length of the input data
|
||||
* \param output SHA-1 checksum result
|
||||
*/
|
||||
void sha1( unsigned char *input, int ilen, unsigned char output[20] );
|
||||
|
||||
/**
|
||||
* \brief Output = SHA-1( file contents )
|
||||
*
|
||||
* \param path input file name
|
||||
* \param output SHA-1 checksum result
|
||||
*
|
||||
* \return 0 if successful, 1 if fopen failed,
|
||||
* or 2 if fread failed
|
||||
*/
|
||||
int sha1_file( char *path, unsigned char output[20] );
|
||||
|
||||
/**
|
||||
* \brief SHA-1 HMAC context setup
|
||||
*
|
||||
* \param ctx HMAC context to be initialized
|
||||
* \param key HMAC secret key
|
||||
* \param keylen length of the HMAC key
|
||||
*/
|
||||
void sha1_hmac_starts( sha1_context *ctx, unsigned char *key, int keylen );
|
||||
|
||||
/**
|
||||
* \brief SHA-1 HMAC process buffer
|
||||
*
|
||||
* \param ctx HMAC context
|
||||
* \param input buffer holding the data
|
||||
* \param ilen length of the input data
|
||||
*/
|
||||
void sha1_hmac_update( sha1_context *ctx, unsigned char *input, int ilen );
|
||||
|
||||
/**
|
||||
* \brief SHA-1 HMAC final digest
|
||||
*
|
||||
* \param ctx HMAC context
|
||||
* \param output SHA-1 HMAC checksum result
|
||||
*/
|
||||
void sha1_hmac_finish( sha1_context *ctx, unsigned char output[20] );
|
||||
|
||||
/**
|
||||
* \brief Output = HMAC-SHA-1( hmac key, input buffer )
|
||||
*
|
||||
* \param key HMAC secret key
|
||||
* \param keylen length of the HMAC key
|
||||
* \param input buffer holding the data
|
||||
* \param ilen length of the input data
|
||||
* \param output HMAC-SHA-1 result
|
||||
*/
|
||||
void sha1_hmac( unsigned char *key, int keylen,
|
||||
unsigned char *input, int ilen,
|
||||
unsigned char output[20] );
|
||||
|
||||
/**
|
||||
* \brief Checkup routine
|
||||
*
|
||||
* \return 0 if successful, or 1 if the test failed
|
||||
*/
|
||||
int sha1_self_test( int verbose );
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* sha1.h */
|
|
@ -0,0 +1,21 @@
|
|||
// Copyright 2007,2008 Segher Boessenkool <segher@kernel.crashing.org>
|
||||
// Licensed under the terms of the GNU GPL, version 2
|
||||
// http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
|
||||
|
||||
#ifndef _TOOLS_H
|
||||
#define _TOOLS_H
|
||||
#include "sha1.h"
|
||||
|
||||
// bignum
|
||||
int bn_compare(u8 *a, u8 *b, u32 n);
|
||||
void bn_sub_modulus(u8 *a, u8 *N, u32 n);
|
||||
void bn_add(u8 *d, u8 *a, u8 *b, u8 *N, u32 n);
|
||||
void bn_mul(u8 *d, u8 *a, u8 *b, u8 *N, u32 n);
|
||||
void bn_inv(u8 *d, u8 *a, u8 *N, u32 n); // only for prime N
|
||||
void bn_exp(u8 *d, u8 *a, u8 *N, u32 n, u8 *e, u32 en);
|
||||
|
||||
void generate_ecdsa(u8 *R, u8 *S, u8 *k, u8 *hash);
|
||||
|
||||
void ec_priv_to_pub(u8 *k, u8 *Q);
|
||||
|
||||
#endif
|
|
@ -15,47 +15,90 @@
|
|||
// Official SVN repository and contact information can be found at
|
||||
// http://code.google.com/p/dolphin-emu/
|
||||
|
||||
// Based off of tachtig http://git.infradead.org/?p=users/segher/wii.git
|
||||
// Based off of tachtig/twintig http://git.infradead.org/?p=users/segher/wii.git
|
||||
|
||||
|
||||
#include "WiiSaveCrypted.h"
|
||||
#include "FileUtil.h"
|
||||
|
||||
#include "FileUtil.h"
|
||||
#include "MathUtil.h"
|
||||
|
||||
u8 SD_IV[0x10] = {0x21, 0x67, 0x12, 0xE6, 0xAA, 0x1F, 0x68, 0x9F,
|
||||
0x95, 0xC5, 0xA2, 0x23, 0x24, 0xDC, 0x6A, 0x98};
|
||||
|
||||
CWiiSaveCrypted::CWiiSaveCrypted(const char* FileName)
|
||||
const u8 SDKey[16] = {0xAB, 0x01, 0xB9, 0xD8, 0xE1, 0x62, 0x2B, 0x08,
|
||||
0xAF, 0xBA, 0xD8, 0x4D, 0xBF, 0xC2, 0xA5, 0x5D};
|
||||
|
||||
u8 MD5_BLANKER[0x10] = {0x0E, 0x65, 0x37, 0x81, 0x99, 0xBE, 0x45, 0x17,
|
||||
0xAB, 0x06, 0xEC, 0x22, 0x45, 0x1A, 0x57, 0x93};
|
||||
CWiiSaveCrypted::CWiiSaveCrypted(const char* FileName, u64 title)
|
||||
: _saveGameTitle(title)
|
||||
{
|
||||
_filename = std::string(FileName);
|
||||
strcpy(pathData_bin, FileName);
|
||||
if (!title)
|
||||
{
|
||||
AES_set_decrypt_key(SDKey, 128, &m_AES_KEY);
|
||||
|
||||
const u8 SDKey[16] = {0xAB, 0x01, 0xB9, 0xD8, 0xE1, 0x62, 0x2B, 0x08,
|
||||
0xAF, 0xBA, 0xD8, 0x4D, 0xBF, 0xC2, 0xA5, 0x5D};
|
||||
AES_set_decrypt_key(SDKey, 128, &m_AES_KEY);
|
||||
b_valid = true;
|
||||
ReadHDR();
|
||||
ReadBKHDR();
|
||||
|
||||
do
|
||||
{
|
||||
b_valid = true;
|
||||
ReadHDR();
|
||||
ReadBKHDR();
|
||||
ImportWiiSaveFiles();
|
||||
// TODO: check_sig()
|
||||
if (b_valid)
|
||||
{
|
||||
SuccessAlert("Successfully imported save files");
|
||||
b_tryAgain = false;
|
||||
}
|
||||
else b_tryAgain = AskYesNo("Import failed, try again?");
|
||||
}while(b_tryAgain);
|
||||
}
|
||||
else
|
||||
{
|
||||
AES_set_encrypt_key(SDKey, 128, &m_AES_KEY);
|
||||
|
||||
if (getPaths(true))
|
||||
{
|
||||
do
|
||||
{
|
||||
b_valid = true;
|
||||
WriteHDR();
|
||||
WriteBKHDR();
|
||||
ExportWiiSaveFiles();
|
||||
do_sig();
|
||||
if (b_valid)
|
||||
{
|
||||
SuccessAlert("Successfully exported file to %s", pathData_bin);
|
||||
b_tryAgain = false;
|
||||
}
|
||||
else b_tryAgain = AskYesNo("Export failed, try again?");
|
||||
}while(b_tryAgain);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CWiiSaveCrypted::ReadHDR()
|
||||
{
|
||||
saveFileP = fopen(_filename.c_str(), "rb");
|
||||
if (!saveFileP)
|
||||
fpData_bin = fopen(pathData_bin, "rb");
|
||||
if (!fpData_bin)
|
||||
{
|
||||
PanicAlert("Cannot open %s", _filename.c_str());
|
||||
PanicAlert("Cannot open %s", pathData_bin);
|
||||
b_valid = false;
|
||||
return;
|
||||
}
|
||||
Data_Bin_HDR * tmpHDR = new Data_Bin_HDR;
|
||||
|
||||
if (fread(tmpHDR, HDR_SZ, 1, saveFileP) != 1)
|
||||
if (fread(&_encryptedHeader, HEADER_SZ, 1, fpData_bin) != 1)
|
||||
{
|
||||
PanicAlert("failed to read header");
|
||||
b_valid = false;
|
||||
return;
|
||||
}
|
||||
AES_cbc_encrypt((const u8*)tmpHDR, (u8*)&hdr, HDR_SZ, &m_AES_KEY, SD_IV, AES_DECRYPT);
|
||||
delete tmpHDR;
|
||||
_bannerSize = Common::swap32(hdr.BannerSize);
|
||||
fclose(fpData_bin);
|
||||
|
||||
|
||||
AES_cbc_encrypt((const u8*)&_encryptedHeader, (u8*)&_header, HEADER_SZ, &m_AES_KEY, SD_IV, AES_DECRYPT);
|
||||
_bannerSize = Common::swap32(_header.hdr.BannerSize);
|
||||
if ((_bannerSize < FULL_BNR_MIN) || (_bannerSize > FULL_BNR_MAX) ||
|
||||
(((_bannerSize - BNR_SZ) % ICON_SZ) != 0))
|
||||
{
|
||||
|
@ -63,151 +106,224 @@ void CWiiSaveCrypted::ReadHDR()
|
|||
b_valid = false;
|
||||
return;
|
||||
}
|
||||
_saveGameTitle = Common::swap64(hdr.SaveGameTitle);
|
||||
fclose(saveFileP);
|
||||
_saveGameTitle = Common::swap64(_header.hdr.SaveGameTitle);
|
||||
|
||||
memcpy(md5_file, _header.hdr.Md5, 0x10);
|
||||
memcpy(_header.hdr.Md5, MD5_BLANKER, 0x10);
|
||||
md5((u8*)&_header, HEADER_SZ, md5_calc);
|
||||
if (memcmp(md5_file, md5_calc, 0x10))
|
||||
{
|
||||
PanicAlert("MD5 mismatch\n %016llx%016llx != %016llx%016llx", Common::swap64(md5_file),Common::swap64(md5_file+8), Common::swap64(md5_calc), Common::swap64(md5_calc+8));
|
||||
b_valid= false;
|
||||
}
|
||||
|
||||
if (!getPaths())
|
||||
{
|
||||
b_valid = false;
|
||||
return;
|
||||
}
|
||||
if (!File::Exists(pathBanner_bin) || AskYesNo("%s already exists, overwrite?", pathBanner_bin))
|
||||
{
|
||||
INFO_LOG(CONSOLE, "creating file %s", pathBanner_bin);
|
||||
fpBanner_bin = fopen(pathBanner_bin, "wb");
|
||||
if (fpBanner_bin)
|
||||
{
|
||||
fwrite(_header.BNR, _bannerSize, 1, fpBanner_bin);
|
||||
fclose(fpBanner_bin);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CWiiSaveCrypted::WriteHDR()
|
||||
{
|
||||
if (!b_valid) return;
|
||||
memset(&_header, 0, HEADER_SZ);
|
||||
|
||||
_header.hdr.BannerSize = Common::swap32(File::GetSize(pathBanner_bin));
|
||||
|
||||
_header.hdr.SaveGameTitle = Common::swap64(_saveGameTitle);
|
||||
memcpy(_header.hdr.Md5, MD5_BLANKER, 0x10);
|
||||
_header.hdr.Permissions = 0x3C;//0x35;
|
||||
|
||||
fpBanner_bin = fopen(pathBanner_bin, "rb");
|
||||
if (fpBanner_bin)
|
||||
{
|
||||
if (fread(_header.BNR, Common::swap32(_header.hdr.BannerSize), 1, fpBanner_bin) != 1)
|
||||
{
|
||||
PanicAlert("Failed to read banner.bin");
|
||||
b_valid = false;
|
||||
return;
|
||||
}
|
||||
fclose(fpBanner_bin);
|
||||
}
|
||||
|
||||
md5((u8*)&_header, HEADER_SZ, md5_calc);
|
||||
memcpy(_header.hdr.Md5, md5_calc, 0x10);
|
||||
|
||||
AES_cbc_encrypt((const unsigned char *)&_header, (u8*)&_encryptedHeader, HEADER_SZ, &m_AES_KEY, SD_IV, AES_ENCRYPT);
|
||||
fpData_bin = fopen(pathData_bin, "wb");
|
||||
if (fpData_bin)
|
||||
{
|
||||
if (fwrite(&_encryptedHeader, HEADER_SZ, 1, fpData_bin) != 1)
|
||||
{
|
||||
PanicAlert("Failed to write header for %s", pathData_bin);
|
||||
b_valid = false;
|
||||
}
|
||||
fclose(fpData_bin);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void CWiiSaveCrypted::ReadBKHDR()
|
||||
{
|
||||
if (!b_valid) return;
|
||||
saveFileP = fopen(_filename.c_str(), "rb");
|
||||
if (!saveFileP)
|
||||
|
||||
fpData_bin = fopen(pathData_bin, "rb");
|
||||
if (!fpData_bin)
|
||||
{
|
||||
PanicAlert("Cannot open %s", _filename.c_str());
|
||||
PanicAlert("Cannot open %s", pathData_bin);
|
||||
b_valid = false;
|
||||
return;
|
||||
}
|
||||
fseek(saveFileP, HDR_SZ + FULL_BNR_MAX/*_bannerSize*/, SEEK_SET);
|
||||
|
||||
if (fread(&bkhdr, BK_SZ, 1, saveFileP) != 1)
|
||||
fseek(fpData_bin, HEADER_SZ, SEEK_SET);
|
||||
if (fread(&bkhdr, BK_SZ, 1, fpData_bin) != 1)
|
||||
{
|
||||
PanicAlert("failed to read bk header");
|
||||
b_valid = false;
|
||||
return;
|
||||
}
|
||||
fclose(fpData_bin);
|
||||
|
||||
if (Common::swap64((u8*)&bkhdr) != 0x00000070426b0001ULL)
|
||||
if (bkhdr.size != Common::swap32(BK_LISTED_SZ) ||
|
||||
bkhdr.magic != Common::swap32(BK_HDR_MAGIC))
|
||||
{
|
||||
PanicAlert("Invalid Size or Magic word %016llx", bkhdr);
|
||||
PanicAlert("Invalid Size(%x) or Magic word (%x)", bkhdr.size, bkhdr.magic);
|
||||
b_valid = false;
|
||||
return;
|
||||
}
|
||||
if (_saveGameTitle != Common::swap64(bkhdr.SaveGameTitle))
|
||||
WARN_LOG(CONSOLE, "encrypted title (%x) does not match unencrypted title (%x)", _saveGameTitle, Common::swap64(bkhdr.SaveGameTitle));
|
||||
|
||||
|
||||
_numberOfFiles = Common::swap32(bkhdr.numberOfFiles);
|
||||
_sizeOfFiles = Common::swap32(bkhdr.sizeOfFiles);
|
||||
_totalSize = Common::swap32(bkhdr.totalSize);
|
||||
fclose(saveFileP);
|
||||
|
||||
if (_sizeOfFiles + FULL_CERT_SZ != _totalSize)
|
||||
WARN_LOG(CONSOLE, "Size(%x) + cert(%x) does not equal totalsize(%x)", _sizeOfFiles, FULL_CERT_SZ, _totalSize);
|
||||
if (_saveGameTitle != Common::swap64(bkhdr.SaveGameTitle))
|
||||
WARN_LOG(CONSOLE, "encrypted title (%x) does not match unencrypted title (%x)", _saveGameTitle, Common::swap64(bkhdr.SaveGameTitle));
|
||||
|
||||
|
||||
}
|
||||
|
||||
void CWiiSaveCrypted::Extract()
|
||||
void CWiiSaveCrypted::WriteBKHDR()
|
||||
{
|
||||
if (!b_valid) return;
|
||||
_numberOfFiles = 0;
|
||||
_sizeOfFiles = 0;
|
||||
|
||||
ScanForFiles(pathSavedir, FilesList, &_numberOfFiles, &_sizeOfFiles);
|
||||
memset(&bkhdr, 0, BK_SZ);
|
||||
bkhdr.size = Common::swap32(BK_LISTED_SZ);
|
||||
bkhdr.magic = Common::swap32(BK_HDR_MAGIC);
|
||||
//customize this
|
||||
bkhdr.NGid = Common::swap32(1);
|
||||
//
|
||||
bkhdr.numberOfFiles = Common::swap32(_numberOfFiles);
|
||||
bkhdr.sizeOfFiles = Common::swap32(_sizeOfFiles);
|
||||
bkhdr.totalSize = Common::swap32(_sizeOfFiles + FULL_CERT_SZ);
|
||||
bkhdr.SaveGameTitle = Common::swap64(_saveGameTitle);
|
||||
// customize this
|
||||
const u8 MAC[6] = {0};
|
||||
//
|
||||
memcpy(bkhdr.MACaddress, MAC, 6);
|
||||
|
||||
fpData_bin = fopen(pathData_bin, "ab");
|
||||
if(fpData_bin)
|
||||
{
|
||||
if (fwrite(&bkhdr, BK_SZ, 1, fpData_bin) != 1)
|
||||
{
|
||||
PanicAlert("Failed to write bkhdr");
|
||||
b_valid = false;
|
||||
}
|
||||
fclose(fpData_bin);
|
||||
}
|
||||
}
|
||||
|
||||
void CWiiSaveCrypted::ImportWiiSaveFiles()
|
||||
{
|
||||
if (!b_valid) return;
|
||||
|
||||
saveFileP = fopen(_filename.c_str(), "rb");
|
||||
if (!saveFileP)
|
||||
fpData_bin = fopen(pathData_bin, "rb");
|
||||
if (!fpData_bin)
|
||||
{
|
||||
PanicAlert("Cannot open %s", _filename.c_str());
|
||||
PanicAlert("Cannot open %s", pathData_bin);
|
||||
b_valid = false;
|
||||
return;
|
||||
}
|
||||
|
||||
sprintf(dir, FULL_WII_USER_DIR "title/%08x/%08x/data/", (u32)(_saveGameTitle>>32), (u32)_saveGameTitle);
|
||||
|
||||
if (!AskYesNo("Warning! it is advised to backup all files in the folder:\n%s\nDo you wish to continue?", dir))
|
||||
return;
|
||||
|
||||
INFO_LOG(CONSOLE, "%s", dir);
|
||||
File::CreateFullPath(dir);
|
||||
|
||||
fseek(saveFileP, HDR_SZ, SEEK_SET);
|
||||
|
||||
_encryptedData = new u8[_bannerSize];
|
||||
_data = new u8[_bannerSize];
|
||||
if (fread(_encryptedData, _bannerSize, 1, saveFileP) != 1)
|
||||
{
|
||||
PanicAlert("failed to read banner");
|
||||
b_valid = false;
|
||||
return;
|
||||
}
|
||||
|
||||
AES_cbc_encrypt((const u8*)_encryptedData, (u8*)_data, _bannerSize, &m_AES_KEY, SD_IV, AES_DECRYPT);
|
||||
delete []_encryptedData;
|
||||
|
||||
sprintf(path, "%sbanner.bin", dir);
|
||||
|
||||
// remove after code is more thoroughly tested
|
||||
sprintf(tmpPath, "%s.bak", path);
|
||||
File::Copy(path, tmpPath);
|
||||
//
|
||||
|
||||
if (!File::Exists(path) || AskYesNo("%s already exists, overwrite?", path))
|
||||
{
|
||||
INFO_LOG(CONSOLE, "creating file %s", path);
|
||||
outFileP = fopen(path, "wb");
|
||||
if (outFileP)
|
||||
{
|
||||
fwrite(_data, _bannerSize, 1, outFileP);
|
||||
fclose(outFileP);
|
||||
}
|
||||
}
|
||||
delete []_data;
|
||||
|
||||
int lastpos = HDR_SZ + FULL_BNR_MAX /*_bannerSize */+ BK_SZ;
|
||||
|
||||
|
||||
int lastpos = HEADER_SZ + BK_SZ;
|
||||
|
||||
|
||||
FileHDR _tmpFileHDR;
|
||||
|
||||
for(u32 i = 0; i < _numberOfFiles; i++)
|
||||
{
|
||||
fseek(saveFileP, lastpos, SEEK_SET);
|
||||
fseek(fpData_bin, lastpos, SEEK_SET);
|
||||
memset(&_tmpFileHDR, 0, FILE_HDR_SZ);
|
||||
memset(IV, 0, 0x10);
|
||||
u32 roundedsize;
|
||||
|
||||
fread(&_tmpFileHDR, FILE_HDR_SZ, 1, saveFileP);
|
||||
_fileSize = 0;
|
||||
|
||||
if (fread(&_tmpFileHDR, FILE_HDR_SZ, 1, fpData_bin) != 1)
|
||||
{
|
||||
PanicAlert("Failed to write header for file %d", i);
|
||||
b_valid = false;
|
||||
}
|
||||
|
||||
lastpos += FILE_HDR_SZ;
|
||||
if(Common::swap32(_tmpFileHDR.magic) != 0x03adf17e)
|
||||
if(Common::swap32(_tmpFileHDR.magic) != FILE_HDR_MAGIC)
|
||||
{
|
||||
PanicAlert("Bad File Header");
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
sprintf(path, "%s%s", dir, _tmpFileHDR.name);
|
||||
sprintf(pathRawSave, "%s%s", pathSavedir, _tmpFileHDR.name);
|
||||
|
||||
if (_tmpFileHDR.type == 2)
|
||||
{
|
||||
PanicAlert("savegame with a dir, report me :p, %s", path);
|
||||
// we should prolly write any future files to this new dir
|
||||
// but tachtig doesnt do this...
|
||||
File::CreateFullPath(path);
|
||||
PanicAlert("savegame with a dir, report me :p, %s", pathRawSave);
|
||||
// TODO: tachtig doesnt write future files to this dir
|
||||
// for now, put the rest of the files in the new folder
|
||||
File::CreateFullPath(pathRawSave);
|
||||
strcpy(pathSavedir, pathRawSave);
|
||||
}
|
||||
else
|
||||
{
|
||||
roundedsize = (Common::swap32(_tmpFileHDR.size));// + 63) & ~63; // rounding makes corrupted files for NSMBwii
|
||||
lastpos += roundedsize;
|
||||
_encryptedData = new u8[roundedsize];
|
||||
_data = new u8[roundedsize];
|
||||
fread(_encryptedData, roundedsize, 1, saveFileP);
|
||||
memcpy(IV, _tmpFileHDR.IV, 0x10);
|
||||
AES_cbc_encrypt((const unsigned char *)_encryptedData, _data, roundedsize, &m_AES_KEY, IV, AES_DECRYPT);
|
||||
delete []_encryptedData;
|
||||
|
||||
// remove after code is more thoroughly tested
|
||||
sprintf(tmpPath, "%s.bak", path);
|
||||
File::Copy(path, tmpPath);
|
||||
//
|
||||
if (!File::Exists(path) || AskYesNo("%s already exists, overwrite?", path))
|
||||
_fileSize = Common::swap32(_tmpFileHDR.size);
|
||||
lastpos += ROUND_UP(_fileSize, BLOCK_SZ);
|
||||
_encryptedData = new u8[_fileSize];
|
||||
_data = new u8[_fileSize];
|
||||
if (fread(_encryptedData, _fileSize, 1, fpData_bin) != 1)
|
||||
{
|
||||
INFO_LOG(CONSOLE, "creating file %s", path);
|
||||
PanicAlert("Failed to read data from file %d", i);
|
||||
b_valid = false;
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
memcpy(IV, _tmpFileHDR.IV, 0x10);
|
||||
AES_cbc_encrypt((const unsigned char *)_encryptedData, _data, _fileSize, &m_AES_KEY, IV, AES_DECRYPT);
|
||||
delete []_encryptedData;
|
||||
|
||||
outFileP = fopen(path, "wb");
|
||||
if (outFileP)
|
||||
if (!File::Exists(pathRawSave) || AskYesNo("%s already exists, overwrite?", pathRawSave))
|
||||
{
|
||||
INFO_LOG(CONSOLE, "creating file %s", pathRawSave);
|
||||
|
||||
fpRawSaveFile = fopen(pathRawSave, "wb");
|
||||
if (fpRawSaveFile)
|
||||
{
|
||||
fwrite(_data, roundedsize, 1, outFileP);
|
||||
fclose(outFileP);
|
||||
fwrite(_data, _fileSize, 1, fpRawSaveFile);
|
||||
fclose(fpRawSaveFile);
|
||||
}
|
||||
}
|
||||
delete []_data;
|
||||
|
@ -215,7 +331,245 @@ void CWiiSaveCrypted::Extract()
|
|||
|
||||
}
|
||||
}
|
||||
fclose(saveFileP);
|
||||
fclose(fpData_bin);
|
||||
}
|
||||
|
||||
void CWiiSaveCrypted::ExportWiiSaveFiles()
|
||||
{
|
||||
if (!b_valid) return;
|
||||
|
||||
u8 *__ENCdata,
|
||||
*__data;
|
||||
|
||||
for(u32 i = 0; i < _numberOfFiles; i++)
|
||||
{
|
||||
FileHDR tmpFileHDR;
|
||||
std::string __name, __ext;
|
||||
memset(&tmpFileHDR, 0, FILE_HDR_SZ);
|
||||
|
||||
_fileSize = File::GetSize(FilesList.at(i).c_str());
|
||||
_roundedfileSize = ROUND_UP(_fileSize, BLOCK_SZ);
|
||||
|
||||
tmpFileHDR.magic = Common::swap32(FILE_HDR_MAGIC);
|
||||
tmpFileHDR.size = Common::swap32(_fileSize);
|
||||
tmpFileHDR.Permissions = 0x3C;
|
||||
tmpFileHDR.type = File::IsDirectory(FilesList.at(i).c_str()) ? 2 : 1;
|
||||
|
||||
SplitPath(FilesList.at(i), NULL, &__name, &__ext);
|
||||
__name += __ext;
|
||||
|
||||
if (__name.length() > 0x44)
|
||||
{
|
||||
PanicAlert("% is too long for the filename, max chars is 45", __name.c_str());
|
||||
b_valid = false;
|
||||
return;
|
||||
}
|
||||
strncpy((char *)tmpFileHDR.name, __name.c_str(), 0x44);
|
||||
|
||||
fpData_bin = fopen(pathData_bin, "ab");
|
||||
if (fpData_bin)
|
||||
{
|
||||
fwrite(&tmpFileHDR, FILE_HDR_SZ, 1, fpData_bin);
|
||||
fclose(fpData_bin);
|
||||
}
|
||||
|
||||
if (tmpFileHDR.type == 1)
|
||||
{
|
||||
if (_fileSize == 0)
|
||||
{
|
||||
PanicAlert("%s is a 0 byte file", FilesList.at(i).c_str());
|
||||
b_valid = false;
|
||||
return;
|
||||
}
|
||||
fpRawSaveFile = fopen(FilesList.at(i).c_str(), "rb");
|
||||
if (!fpRawSaveFile)
|
||||
{
|
||||
PanicAlert("%s failed to open", FilesList.at(i).c_str());
|
||||
b_valid = false;
|
||||
}
|
||||
__data = new u8[_roundedfileSize];
|
||||
__ENCdata = new u8[_roundedfileSize];
|
||||
memset(__data, 0, _roundedfileSize);
|
||||
if (fread(__data, _fileSize, 1, fpRawSaveFile) != 1)
|
||||
{
|
||||
PanicAlert("failed to read data from file: %s", FilesList.at(i).c_str());
|
||||
b_valid = false;
|
||||
}
|
||||
fclose(fpRawSaveFile);
|
||||
|
||||
AES_cbc_encrypt((const u8*)__data, __ENCdata, _roundedfileSize, &m_AES_KEY, tmpFileHDR.IV, AES_ENCRYPT);
|
||||
fpData_bin = fopen(pathData_bin, "ab");
|
||||
if (fpData_bin)
|
||||
{
|
||||
fwrite(__ENCdata, _roundedfileSize, 1, fpData_bin);
|
||||
fclose(fpData_bin);
|
||||
}
|
||||
delete [] __data;
|
||||
delete [] __ENCdata;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CWiiSaveCrypted::do_sig()
|
||||
{
|
||||
if (!b_valid) return;
|
||||
u8 sig[0x40];
|
||||
u8 ng_cert[0x180];
|
||||
u8 ap_cert[0x180];
|
||||
u8 hash[0x14];
|
||||
u8 ap_priv[30];
|
||||
u8 ap_sig[60];
|
||||
char signer[64];
|
||||
char name[64];
|
||||
u8 *data;
|
||||
u32 data_size;
|
||||
//allow customization
|
||||
u32 ng_id = Common::swap32(1);
|
||||
u32 ng_key_id = Common::swap32(2);
|
||||
u8 ng_sig[0x3C] = {0};
|
||||
u8 ng_priv[0x1E] = {0};
|
||||
//allow customization
|
||||
|
||||
sprintf(signer, "Root-CA00000001-MS00000002");
|
||||
sprintf(name, "NG%08x", ng_id);
|
||||
make_ec_cert(ng_cert, ng_sig, signer, name, ng_priv, ng_key_id);
|
||||
|
||||
|
||||
memset(ap_priv, 0, sizeof ap_priv);
|
||||
ap_priv[10] = 1;
|
||||
|
||||
memset(ap_sig, 81, sizeof ap_sig); // temp
|
||||
|
||||
sprintf(signer, "Root-CA00000001-MS00000002-NG%08x", ng_id);
|
||||
sprintf(name, "AP%08x%08x", 1, 2);
|
||||
make_ec_cert(ap_cert, ap_sig, signer, name, ap_priv, 0);
|
||||
|
||||
sha1(ap_cert + 0x80, 0x100, hash);
|
||||
generate_ecdsa(ap_sig, ap_sig + 30, ng_priv, hash);
|
||||
make_ec_cert(ap_cert, ap_sig, signer, name, ap_priv, 0);
|
||||
|
||||
data_size = Common::swap32(bkhdr.sizeOfFiles) + 0x80;
|
||||
|
||||
fpData_bin = fopen(pathData_bin, "rb");
|
||||
if (!fpData_bin)
|
||||
{
|
||||
b_valid = false;
|
||||
return;
|
||||
}
|
||||
data = new u8[data_size];
|
||||
|
||||
fseek(fpData_bin, 0xf0c0, SEEK_SET);
|
||||
if (fread(data, data_size, 1, fpData_bin) != 1)
|
||||
PanicAlert("read data for sig check");
|
||||
sha1(data, data_size, hash);
|
||||
sha1(hash, 20, hash);
|
||||
fclose(fpData_bin);
|
||||
delete []data;
|
||||
fpData_bin = fopen(pathData_bin, "ab");
|
||||
if (!fpData_bin)
|
||||
{
|
||||
b_valid = false;
|
||||
return;
|
||||
}
|
||||
generate_ecdsa(sig, sig + 30, ap_priv, hash);
|
||||
*(u32*)(sig + 60) = Common::swap32(0x2f536969);
|
||||
|
||||
|
||||
|
||||
if (fwrite(sig, sizeof sig, 1, fpData_bin) != 1)
|
||||
PanicAlert("write sig");
|
||||
if (fwrite(ng_cert, sizeof ng_cert, 1, fpData_bin) != 1)
|
||||
PanicAlert("write NG cert");
|
||||
if (fwrite(ap_cert, sizeof ap_cert, 1, fpData_bin) != 1)
|
||||
PanicAlert("write AP cert");
|
||||
fclose(fpData_bin);
|
||||
}
|
||||
|
||||
|
||||
void CWiiSaveCrypted::make_ec_cert(u8 *cert, u8 *sig, char *signer, char *name, u8 *priv, u32 key_id)
|
||||
{
|
||||
memset(cert, 0, 0x180);
|
||||
*(u32*)cert = Common::swap32(0x10002);
|
||||
|
||||
memcpy(cert + 4, sig, 60);
|
||||
strcpy((char*)cert + 0x80, signer);
|
||||
*(u32*)(cert + 0xc0) = Common::swap32(2);
|
||||
strcpy((char*)cert + 0xc4, name);
|
||||
*(u32*)(cert + 0x104) = Common::swap32(key_id);
|
||||
ec_priv_to_pub(priv, cert + 0x108);
|
||||
}
|
||||
|
||||
bool CWiiSaveCrypted::getPaths(bool _export)
|
||||
{
|
||||
if (_saveGameTitle)
|
||||
{
|
||||
sprintf(pathSavedir, FULL_WII_USER_DIR "title/%08x/%08x/data/", (u32)(_saveGameTitle>>32), (u32)_saveGameTitle);
|
||||
sprintf(pathBanner_bin, "%sbanner.bin", pathSavedir);
|
||||
sprintf(_saveGameString, "%c%c%c%c",
|
||||
(u8)(_saveGameTitle >> 24) & 0xFF, (u8)(_saveGameTitle >> 16) & 0xFF,
|
||||
(u8)(_saveGameTitle >> 8) & 0xFF, (u8)_saveGameTitle & 0xFF);
|
||||
}
|
||||
if (_export)
|
||||
{
|
||||
if(!File::IsDirectory(pathSavedir))
|
||||
{
|
||||
b_valid = false;
|
||||
PanicAlert("No save folder found for title %s", _saveGameString);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!File::Exists(pathBanner_bin))
|
||||
{
|
||||
b_valid = false;
|
||||
PanicAlert("No banner file found for title %s", _saveGameString);
|
||||
return false;
|
||||
}
|
||||
if (strlen(pathData_bin) == 0)
|
||||
strcpy(pathData_bin, ".");// If no path was passed, use current dir
|
||||
sprintf(pathData_bin, "%s/private/wii/title/%s/data.bin", pathData_bin, _saveGameString);
|
||||
File::CreateFullPath(pathData_bin);
|
||||
}
|
||||
else
|
||||
{
|
||||
File::CreateFullPath(pathSavedir);
|
||||
if (!AskYesNo("Warning! it is advised to backup all files in the folder:\n%s\nDo you wish to continue?", pathSavedir))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void CWiiSaveCrypted::ScanForFiles(std::string savDir, std::vector<std::string>&FilesList, u32 *_numFiles, u32 *_sizeFiles)
|
||||
{
|
||||
std::vector<std::string> Directories;
|
||||
*_numFiles = *_sizeFiles = 0;
|
||||
|
||||
Directories.push_back(savDir);
|
||||
for (u32 i = 0; i < Directories.size(); i++)
|
||||
{
|
||||
if (i) FilesList.push_back(Directories.at(i));//add dir to fst
|
||||
|
||||
File::FSTEntry FST_Temp;
|
||||
File::ScanDirectoryTree(Directories.at(i).c_str(), FST_Temp);
|
||||
for (u32 j = 0; j < FST_Temp.children.size(); j++)
|
||||
{
|
||||
if (strncmp(FST_Temp.children.at(j).virtualName.c_str(), "banner.bin", 10) != 0 &&
|
||||
strncmp(FST_Temp.children.at(j).virtualName.c_str(), "nocopy", 6) != 0)
|
||||
{
|
||||
(*_numFiles)++;
|
||||
*_sizeFiles += FILE_HDR_SZ + ROUND_UP(FST_Temp.children.at(j).size, BLOCK_SZ);
|
||||
|
||||
if (FST_Temp.children.at(j).isDirectory)
|
||||
{
|
||||
Directories.push_back(FST_Temp.children.at(j).physicalName);
|
||||
}
|
||||
else
|
||||
{
|
||||
FilesList.push_back(FST_Temp.children.at(j).physicalName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CWiiSaveCrypted::~CWiiSaveCrypted()
|
||||
|
|
|
@ -20,6 +20,8 @@
|
|||
|
||||
#include "AES/aes.h"
|
||||
#include "StringUtil.h"
|
||||
#include "Crypto/tools.h"
|
||||
#include "Crypto/md5.h"
|
||||
|
||||
// --- this is used for encrypted Wii save files
|
||||
|
||||
|
@ -28,48 +30,78 @@
|
|||
class CWiiSaveCrypted
|
||||
{
|
||||
public:
|
||||
CWiiSaveCrypted(const char* FileName);
|
||||
CWiiSaveCrypted(const char* FileName, u64 title = 0);
|
||||
~CWiiSaveCrypted();
|
||||
void ReadHDR();
|
||||
void ReadBKHDR();
|
||||
void Extract();
|
||||
void WriteHDR();
|
||||
void WriteBKHDR();
|
||||
void Extract(){;}
|
||||
void ImportWiiSaveFiles();
|
||||
void ExportWiiSaveFiles(); // To data.bin
|
||||
void do_sig();
|
||||
void make_ec_cert(u8 *cert, u8 *sig, char *signer, char *name, u8 *priv,
|
||||
u32 key_id);
|
||||
bool getPaths(bool _export = false);
|
||||
void ScanForFiles(std::string savDir, std::vector<std::string>&FilesList, u32 *_numFiles, u32 *_sizeFiles);
|
||||
|
||||
private:
|
||||
FILE *saveFileP,
|
||||
*outFileP;
|
||||
AES_KEY m_AES_KEY;
|
||||
std::vector<std::string> FilesList;
|
||||
|
||||
FILE *fpData_bin,
|
||||
*fpBanner_bin,
|
||||
*fpRawSaveFile;
|
||||
|
||||
char pathData_bin[2048],
|
||||
pathSavedir[2048],
|
||||
pathBanner_bin[2048], //should always be FULL_WII_USER_DIR "title/%08x/%08x/data/"
|
||||
pathRawSave[2048],
|
||||
_saveGameString[5];
|
||||
|
||||
u8 IV[0x10],
|
||||
*_encryptedData,
|
||||
*_data;
|
||||
*_data,
|
||||
md5_file[16],
|
||||
md5_calc[16];
|
||||
|
||||
char dir[1024],
|
||||
path[1024],
|
||||
tmpPath[1024];
|
||||
|
||||
u32 _bannerSize,
|
||||
_numberOfFiles,
|
||||
_sizeOfFiles,
|
||||
_totalSize;
|
||||
_totalSize,
|
||||
_fileSize,
|
||||
_roundedfileSize;
|
||||
|
||||
u64 _saveGameTitle;
|
||||
|
||||
std::string _filename;
|
||||
|
||||
bool b_valid;
|
||||
bool b_valid,
|
||||
b_tryAgain;
|
||||
|
||||
enum
|
||||
{
|
||||
BLOCK_SZ = 0x40,
|
||||
HDR_SZ = 0x20,
|
||||
BK_SZ = 0x80,
|
||||
FILE_HDR_SZ = 0x80,
|
||||
ICON_SZ = 0x1200,
|
||||
BNR_SZ = 0x60a0,
|
||||
FULL_BNR_MIN = 0x72a0, // BNR_SZ + 1*ICON_SZ
|
||||
FULL_BNR_MAX = 0xF0A0, // BNR_SZ + 8*ICON_SZ
|
||||
FULL_BNR_MIN = 0x72a0, // BNR_SZ + 1*ICON_SZ
|
||||
FULL_BNR_MAX = 0xF0A0, // BNR_SZ + 8*ICON_SZ
|
||||
HEADER_SZ = 0xF0C0, // HDR_SZ + FULL_BNR_MAX
|
||||
BK_LISTED_SZ = 0x70, // Size before rounding to nearest block
|
||||
BK_SZ = 0x80,
|
||||
FILE_HDR_SZ = 0x80,
|
||||
|
||||
SIG_SZ = 0x40,
|
||||
NG_CERT_SZ = 0x180,
|
||||
AP_CERT_SZ = 0x180,
|
||||
FULL_CERT_SZ = 0x3C0, // SIG_SZ + NG_CERT_SZ + AP_CERT_SZ + 0x80?
|
||||
|
||||
|
||||
BK_HDR_MAGIC = 0x426B0001,
|
||||
FILE_HDR_MAGIC = 0x03adf17e
|
||||
};
|
||||
|
||||
#pragma pack(push,1)
|
||||
|
||||
struct Data_Bin_HDR // encrypted
|
||||
{
|
||||
u64 SaveGameTitle;
|
||||
|
@ -78,13 +110,20 @@ private:
|
|||
u8 unk1; // maybe permissions is a be16
|
||||
u8 Md5[0x10]; // md5 of plaintext header with md5 blanker applied
|
||||
u16 unk2;
|
||||
}hdr;
|
||||
};
|
||||
|
||||
struct HEADER
|
||||
{
|
||||
Data_Bin_HDR hdr;
|
||||
u8 BNR[FULL_BNR_MAX];
|
||||
}_header, _encryptedHeader;
|
||||
|
||||
struct BK_Header // Not encrypted
|
||||
{
|
||||
u32 size; // 0x00000070
|
||||
u16 magic; // 'Bk'
|
||||
u16 magic2; // or version (0x0001)
|
||||
u32 size; // 0x00000070
|
||||
// u16 magic; // 'Bk'
|
||||
// u16 magic2; // or version (0x0001)
|
||||
u32 magic; // 0x426B0001
|
||||
u32 NGid;
|
||||
u32 numberOfFiles;
|
||||
u32 sizeOfFiles;
|
||||
|
@ -93,8 +132,8 @@ private:
|
|||
u32 totalSize;
|
||||
u8 unk3[64];
|
||||
u64 SaveGameTitle;
|
||||
u64 MACaddress;
|
||||
u8 padding[0x10];
|
||||
u8 MACaddress[6];
|
||||
u8 padding[0x12];
|
||||
}bkhdr;
|
||||
|
||||
struct FileHDR // encrypted
|
||||
|
|
|
@ -37,6 +37,10 @@ if wxenv['HAVE_WX']:
|
|||
'stdafx.cpp',
|
||||
'WxUtils.cpp',
|
||||
'MemoryCards/WiiSaveCrypted.cpp',
|
||||
'MemoryCards/Crypto/bn.cpp'
|
||||
'MemoryCards/Crypto/ec.cpp'
|
||||
'MemoryCards/Crypto/md5.cpp'
|
||||
'MemoryCards/Crypto/sha1.cpp'
|
||||
]
|
||||
|
||||
CPPDEFINES = [
|
||||
|
|
Loading…
Reference in New Issue