mirror of
https://github.com/libAthena/athena.git
synced 2025-12-09 13:38:03 +00:00
Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
602f6b6e15 | ||
|
|
78286b0f67 | ||
|
|
c6a6d3b9c4 | ||
|
|
6405bffdd2 | ||
|
|
7442d618e7 | ||
|
|
f3ba8819a4 | ||
|
|
24fedff58e | ||
|
|
87306a18d8 | ||
|
|
09e3d33ff2 | ||
|
|
11331e068e | ||
|
|
7ec5a5971a | ||
|
|
7ef451c86a | ||
|
|
ece50c6968 | ||
| 4bc6004a6a |
@@ -6,7 +6,7 @@ project(Athena)
|
|||||||
##################
|
##################
|
||||||
|
|
||||||
set(ATHENA_MAJOR_VERSION 2)
|
set(ATHENA_MAJOR_VERSION 2)
|
||||||
set(ATHENA_MINOR_VERSION 0)
|
set(ATHENA_MINOR_VERSION 1)
|
||||||
set(ATHENA_PATCH_VERSION 0)
|
set(ATHENA_PATCH_VERSION 0)
|
||||||
set(ATHENA_VERSION
|
set(ATHENA_VERSION
|
||||||
${ATHENA_MAJOR_VERSION}.${ATHENA_MINOR_VERSION}.${ATHENA_PATCH_VERSION})
|
${ATHENA_MAJOR_VERSION}.${ATHENA_MINOR_VERSION}.${ATHENA_PATCH_VERSION})
|
||||||
@@ -218,7 +218,7 @@ install(EXPORT AthenaTargets DESTINATION ${INSTALL_CMAKE_DIR} COMPONENT Athena)
|
|||||||
# atdna import #
|
# atdna import #
|
||||||
################
|
################
|
||||||
|
|
||||||
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/atdna/")
|
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/atdna/CMakeLists.txt")
|
||||||
add_subdirectory(atdna)
|
add_subdirectory(atdna)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
|||||||
4
PKGBUILD
4
PKGBUILD
@@ -1,7 +1,7 @@
|
|||||||
# PKGBUILD for libAthena
|
# PKGBUILD for libAthena
|
||||||
_pkgname=libathena
|
_pkgname=libathena
|
||||||
pkgname=$_pkgname-git
|
pkgname=$_pkgname-git
|
||||||
pkgver=1.1.0.71.ge45679f
|
pkgver=2.0.0.9.gf3ba881
|
||||||
pkgrel=1
|
pkgrel=1
|
||||||
pkgdesc="Basic cross platform IO library"
|
pkgdesc="Basic cross platform IO library"
|
||||||
arch=('i686' 'x86_64')
|
arch=('i686' 'x86_64')
|
||||||
@@ -21,7 +21,7 @@ build() {
|
|||||||
cd "$srcdir/$_pkgname"
|
cd "$srcdir/$_pkgname"
|
||||||
mkdir -p build
|
mkdir -p build
|
||||||
cd build
|
cd build
|
||||||
cmake -DCMAKE_INSTALL_PREFIX="$pkgdir/usr" ..
|
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$pkgdir/usr" ..
|
||||||
make
|
make
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,20 @@ namespace Athena
|
|||||||
namespace io
|
namespace io
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/* forward-declaration dance for recursively-derived types */
|
||||||
|
|
||||||
|
template <size_t sizeVar, Endian VE>
|
||||||
|
struct Buffer;
|
||||||
|
|
||||||
|
template <atInt32 sizeVar, Endian VE>
|
||||||
|
struct String;
|
||||||
|
|
||||||
|
template <atInt32 sizeVar, Endian VE>
|
||||||
|
struct WString;
|
||||||
|
|
||||||
|
template <atInt32 sizeVar, Endian VE>
|
||||||
|
struct WStringAsString;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Base DNA class used against 'atdna'
|
* @brief Base DNA class used against 'atdna'
|
||||||
*
|
*
|
||||||
@@ -38,12 +52,33 @@ struct DNA
|
|||||||
template <typename T, size_t cntVar, Endian VE = DNAE>
|
template <typename T, size_t cntVar, Endian VE = DNAE>
|
||||||
using Vector = std::vector<T>;
|
using Vector = std::vector<T>;
|
||||||
|
|
||||||
struct Delete {};
|
|
||||||
|
|
||||||
template <size_t sizeVar>
|
template <size_t sizeVar>
|
||||||
struct Buffer : public DNA, public std::unique_ptr<atUint8[]>
|
using Buffer = struct Buffer<sizeVar, DNAE>;
|
||||||
|
|
||||||
|
template <atInt32 sizeVar = -1>
|
||||||
|
using String = struct String<sizeVar, DNAE>;
|
||||||
|
|
||||||
|
template <atInt32 sizeVar = -1, Endian VE = DNAE>
|
||||||
|
using WString = struct WString<sizeVar, VE>;
|
||||||
|
|
||||||
|
template <atInt32 sizeVar = -1>
|
||||||
|
using WStringAsString = struct WStringAsString<sizeVar, DNAE>;
|
||||||
|
|
||||||
|
template <off_t offset, SeekOrigin direction>
|
||||||
|
struct Seek {};
|
||||||
|
|
||||||
|
template <size_t align>
|
||||||
|
struct Align {};
|
||||||
|
|
||||||
|
struct Delete {};
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Concrete DNA types */
|
||||||
|
|
||||||
|
template <size_t sizeVar, Endian VE>
|
||||||
|
struct Buffer : public DNA<VE>, public std::unique_ptr<atUint8[]>
|
||||||
{
|
{
|
||||||
Delete expl;
|
typename DNA<VE>::Delete expl;
|
||||||
inline void read(IStreamReader& reader)
|
inline void read(IStreamReader& reader)
|
||||||
{
|
{
|
||||||
reset(new atUint8[sizeVar]);
|
reset(new atUint8[sizeVar]);
|
||||||
@@ -55,10 +90,10 @@ struct DNA
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <atInt32 sizeVar = -1>
|
template <atInt32 sizeVar, Endian VE>
|
||||||
struct String : public DNA, public std::string
|
struct String : public DNA<VE>, public std::string
|
||||||
{
|
{
|
||||||
Delete expl;
|
typename DNA<VE>::Delete expl;
|
||||||
inline void read(IStreamReader& reader)
|
inline void read(IStreamReader& reader)
|
||||||
{*this = reader.readString(sizeVar);}
|
{*this = reader.readString(sizeVar);}
|
||||||
inline void write(IStreamWriter& writer) const
|
inline void write(IStreamWriter& writer) const
|
||||||
@@ -69,10 +104,10 @@ struct DNA
|
|||||||
{this->swap(__str); return *this;}
|
{this->swap(__str); return *this;}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <atInt32 sizeVar = -1, Endian VE = DNAE>
|
template <atInt32 sizeVar, Endian VE>
|
||||||
struct WString : public DNA, public std::wstring
|
struct WString : public DNA<VE>, public std::wstring
|
||||||
{
|
{
|
||||||
Delete expl;
|
typename DNA<VE>::Delete expl;
|
||||||
inline void read(IStreamReader& reader)
|
inline void read(IStreamReader& reader)
|
||||||
{
|
{
|
||||||
reader.setEndian(VE);
|
reader.setEndian(VE);
|
||||||
@@ -89,28 +124,20 @@ struct DNA
|
|||||||
{this->swap(__str); return *this;}
|
{this->swap(__str); return *this;}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <atInt32 sizeVar = -1>
|
template <atInt32 sizeVar, Endian VE>
|
||||||
struct UTF8 : public DNA, public std::string
|
struct WStringAsString : public DNA<VE>, public std::string
|
||||||
{
|
{
|
||||||
Delete expl;
|
typename DNA<VE>::Delete expl;
|
||||||
inline void read(IStreamReader& reader)
|
inline void read(IStreamReader& reader)
|
||||||
{*this = reader.readUnicode(sizeVar);}
|
{*this = reader.readWStringAsString(sizeVar);}
|
||||||
inline void write(IStreamWriter& writer) const
|
inline void write(IStreamWriter& writer) const
|
||||||
{writer.writeUnicode(*this, sizeVar);}
|
{writer.writeStringAsWString(*this, sizeVar);}
|
||||||
inline std::string& operator=(const std::string& __str)
|
inline std::string& operator=(const std::string& __str)
|
||||||
{return this->assign(__str);}
|
{return this->assign(__str);}
|
||||||
inline std::string& operator=(std::string&& __str)
|
inline std::string& operator=(std::string&& __str)
|
||||||
{this->swap(__str); return *this;}
|
{this->swap(__str); return *this;}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <off_t offset, SeekOrigin direction>
|
|
||||||
struct Seek {};
|
|
||||||
|
|
||||||
template <size_t align>
|
|
||||||
struct Align {};
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
/** Macro to automatically declare read/write methods in subclasses */
|
/** Macro to automatically declare read/write methods in subclasses */
|
||||||
#define DECL_DNA \
|
#define DECL_DNA \
|
||||||
void read(Athena::io::IStreamReader&); \
|
void read(Athena::io::IStreamReader&); \
|
||||||
|
|||||||
@@ -14,16 +14,16 @@ class IStreamReader : public IStream
|
|||||||
public:
|
public:
|
||||||
virtual ~IStreamReader() {}
|
virtual ~IStreamReader() {}
|
||||||
|
|
||||||
/*! \brief Sets the Endianss of the stream
|
/*! \brief Sets the Endianness of the stream
|
||||||
*
|
*
|
||||||
* \param endian The Endianess to set \sa Endian
|
* \param endian The Endianness to set \sa Endian
|
||||||
*/
|
*/
|
||||||
inline void setEndian(Endian endian)
|
inline void setEndian(Endian endian)
|
||||||
{m_endian = endian;}
|
{m_endian = endian;}
|
||||||
|
|
||||||
/*! \brief Returns the current Endianness of the stream
|
/*! \brief Returns the current Endianness of the stream
|
||||||
*
|
*
|
||||||
* \return Endian The current Stream Endianess
|
* \return Endian The current Stream Endianness
|
||||||
*/
|
*/
|
||||||
inline Endian endian() const
|
inline Endian endian() const
|
||||||
{return m_endian;}
|
{return m_endian;}
|
||||||
@@ -293,13 +293,13 @@ public:
|
|||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*! \brief Reads a Unicode string and advances the position in the file
|
/*! \brief Reads a wide-char string, converts to UTF8 and advances the position in the file
|
||||||
*
|
*
|
||||||
* \param fixedLen If non-negative, this is a fixed-length string read
|
* \param fixedLen If non-negative, this is a fixed-length string read
|
||||||
* \return std::string The value at the current address
|
* \return std::string The value at the current address
|
||||||
* \throw IOException when address is out of range
|
* \throw IOException when address is out of range
|
||||||
*/
|
*/
|
||||||
inline std::string readUnicode(atInt32 fixedLen = -1)
|
inline std::string readWStringAsString(atInt32 fixedLen = -1)
|
||||||
{
|
{
|
||||||
std::wstring tmp;
|
std::wstring tmp;
|
||||||
atUint16 chr = readUint16();
|
atUint16 chr = readUint16();
|
||||||
|
|||||||
@@ -13,16 +13,16 @@ class IStreamWriter : public IStream
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual ~IStreamWriter() {}
|
virtual ~IStreamWriter() {}
|
||||||
/*! \brief Sets the Endianss of the stream
|
/*! \brief Sets the Endianness of the stream
|
||||||
*
|
*
|
||||||
* \param endian The Endianess to set \sa Endian
|
* \param endian The Endianness to set \sa Endian
|
||||||
*/
|
*/
|
||||||
inline void setEndian(Endian endian)
|
inline void setEndian(Endian endian)
|
||||||
{m_endian = endian;}
|
{m_endian = endian;}
|
||||||
|
|
||||||
/*! \brief Returns the current Endianness of the stream
|
/*! \brief Returns the current Endianness of the stream
|
||||||
*
|
*
|
||||||
* \return Endian The current Stream Endianess
|
* \return Endian The current Stream Endianness
|
||||||
*/
|
*/
|
||||||
inline Endian endian() const
|
inline Endian endian() const
|
||||||
{return m_endian;}
|
{return m_endian;}
|
||||||
@@ -273,14 +273,14 @@ public:
|
|||||||
writeUBytes((atUint8*)&vec, 16);
|
writeUBytes((atUint8*)&vec, 16);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*! \brief Writes an unicode string to the buffer and advances the buffer.
|
/*! \brief Converts a UTF8 string to a wide-char string in the buffer and advances the buffer.
|
||||||
* It also swaps the bytes depending on the platform and Stream settings.
|
* It also swaps the bytes depending on the platform and Stream settings.
|
||||||
*
|
*
|
||||||
* \sa Endian
|
* \sa Endian
|
||||||
* \param str The string to write to the buffer
|
* \param str The string to write to the buffer
|
||||||
* \param fixedLen If not -1, the number of characters to zero-fill string to
|
* \param fixedLen If not -1, the number of characters to zero-fill string to
|
||||||
*/
|
*/
|
||||||
inline void writeUnicode(const std::string& str, atInt32 fixedLen = -1)
|
inline void writeStringAsWString(const std::string& str, atInt32 fixedLen = -1)
|
||||||
{
|
{
|
||||||
std::string tmpStr = "\xEF\xBB\xBF" + str;
|
std::string tmpStr = "\xEF\xBB\xBF" + str;
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ namespace Athena
|
|||||||
namespace utility
|
namespace utility
|
||||||
{
|
{
|
||||||
inline bool isEmpty(atInt8* buf, atUint32 size) {return !memcmp(buf, buf + 1, size - 1);}
|
inline bool isEmpty(atInt8* buf, atUint32 size) {return !memcmp(buf, buf + 1, size - 1);}
|
||||||
bool isSystemBigEndian();
|
inline bool isSystemBigEndian() {return (*(atUint16*)"\xFE\xFF" == 0xFEFF);}
|
||||||
|
|
||||||
inline atInt16 swap16(atInt16 val)
|
inline atInt16 swap16(atInt16 val)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ namespace Athena
|
|||||||
class IAES
|
class IAES
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
virtual ~IAES() {}
|
||||||
virtual void encrypt(const uint8_t* iv, const uint8_t* inbuf, uint8_t* outbuf, uint64_t len)=0;
|
virtual void encrypt(const uint8_t* iv, const uint8_t* inbuf, uint8_t* outbuf, uint64_t len)=0;
|
||||||
virtual void decrypt(const uint8_t* iv, const uint8_t* inbuf, uint8_t* outbuf, uint64_t len)=0;
|
virtual void decrypt(const uint8_t* iv, const uint8_t* inbuf, uint8_t* outbuf, uint64_t len)=0;
|
||||||
virtual void setKey(const uint8_t* key)=0;
|
virtual void setKey(const uint8_t* key)=0;
|
||||||
|
|||||||
@@ -20,8 +20,8 @@ FileReader::FileReader(const std::string& filename, atInt32 cacheSize)
|
|||||||
m_cacheData(nullptr),
|
m_cacheData(nullptr),
|
||||||
m_offset(0)
|
m_offset(0)
|
||||||
{
|
{
|
||||||
setCacheSize(cacheSize);
|
|
||||||
open();
|
open();
|
||||||
|
setCacheSize(cacheSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
FileReader::~FileReader()
|
FileReader::~FileReader()
|
||||||
|
|||||||
@@ -23,12 +23,6 @@ namespace Athena
|
|||||||
namespace utility
|
namespace utility
|
||||||
{
|
{
|
||||||
|
|
||||||
bool isSystemBigEndian()
|
|
||||||
{
|
|
||||||
static const atUint8* test = (atUint8*)"\xFE\xFF";
|
|
||||||
return (*(atUint16*)test == 0xFEFF);
|
|
||||||
}
|
|
||||||
|
|
||||||
void fillRandom(atUint8* rndArea, atUint64 count)
|
void fillRandom(atUint8* rndArea, atUint64 count)
|
||||||
{
|
{
|
||||||
for (atUint64 i = 0; i < count; i++)
|
for (atUint64 i = 0; i < count; i++)
|
||||||
|
|||||||
@@ -198,12 +198,12 @@ WiiBanner* WiiSaveReader::readBanner()
|
|||||||
animSpeed = base::readUint16();
|
animSpeed = base::readUint16();
|
||||||
base::seek(22);
|
base::seek(22);
|
||||||
|
|
||||||
gameTitle = base::readUnicode();
|
gameTitle = base::readWStringAsString();
|
||||||
|
|
||||||
if (base::position() != 0x0080)
|
if (base::position() != 0x0080)
|
||||||
base::seek(0x0080, SeekOrigin::Begin);
|
base::seek(0x0080, SeekOrigin::Begin);
|
||||||
|
|
||||||
subTitle = base::readUnicode();
|
subTitle = base::readWStringAsString();
|
||||||
|
|
||||||
if (base::position() != 0x00C0)
|
if (base::position() != 0x00C0)
|
||||||
base::seek(0x00C0, SeekOrigin::Begin);
|
base::seek(0x00C0, SeekOrigin::Begin);
|
||||||
|
|||||||
@@ -110,12 +110,12 @@ void WiiSaveWriter::writeBanner(WiiBanner* banner)
|
|||||||
base::writeInt16(banner->animationSpeed());
|
base::writeInt16(banner->animationSpeed());
|
||||||
base::seek(22);
|
base::seek(22);
|
||||||
|
|
||||||
base::writeUnicode(banner->title());
|
base::writeStringAsWString(banner->title());
|
||||||
|
|
||||||
if (base::position() != 0x0080)
|
if (base::position() != 0x0080)
|
||||||
base::seek(0x0080, SeekOrigin::Begin);
|
base::seek(0x0080, SeekOrigin::Begin);
|
||||||
|
|
||||||
base::writeUnicode(banner->subtitle());
|
base::writeStringAsWString(banner->subtitle());
|
||||||
|
|
||||||
if (base::position() != 0x00C0)
|
if (base::position() != 0x00C0)
|
||||||
base::seek(0x00C0, SeekOrigin::Begin);
|
base::seek(0x00C0, SeekOrigin::Begin);
|
||||||
|
|||||||
100
src/aes.cpp
100
src/aes.cpp
@@ -20,8 +20,6 @@ namespace Athena
|
|||||||
#define ROTL16(x) (((x)<<16)|((x)>>16))
|
#define ROTL16(x) (((x)<<16)|((x)>>16))
|
||||||
#define ROTL24(x) (((x)<<24)|((x)>>8))
|
#define ROTL24(x) (((x)<<24)|((x)>>8))
|
||||||
|
|
||||||
/* Fixed Data */
|
|
||||||
|
|
||||||
static const uint8_t InCo[4] = {0xB, 0xD, 0x9, 0xE}; /* Inverse Coefficients */
|
static const uint8_t InCo[4] = {0xB, 0xD, 0x9, 0xE}; /* Inverse Coefficients */
|
||||||
|
|
||||||
static inline uint32_t pack(const uint8_t* b)
|
static inline uint32_t pack(const uint8_t* b)
|
||||||
@@ -51,9 +49,8 @@ static inline uint8_t xtime(uint8_t a)
|
|||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
|
|
||||||
class SoftwareAES : public IAES
|
static const struct SoftwareAESTables
|
||||||
{
|
{
|
||||||
protected:
|
|
||||||
uint8_t fbsub[256];
|
uint8_t fbsub[256];
|
||||||
uint8_t rbsub[256];
|
uint8_t rbsub[256];
|
||||||
uint8_t ptab[256], ltab[256];
|
uint8_t ptab[256], ltab[256];
|
||||||
@@ -61,38 +58,14 @@ protected:
|
|||||||
uint32_t rtable[256];
|
uint32_t rtable[256];
|
||||||
uint32_t rco[30];
|
uint32_t rco[30];
|
||||||
|
|
||||||
/* Parameter-dependent data */
|
uint8_t bmul(uint8_t x, uint8_t y) const
|
||||||
|
|
||||||
int Nk, Nb, Nr;
|
|
||||||
uint8_t fi[24], ri[24];
|
|
||||||
uint32_t fkey[120];
|
|
||||||
uint32_t rkey[120];
|
|
||||||
|
|
||||||
|
|
||||||
uint8_t bmul(uint8_t x, uint8_t y);
|
|
||||||
uint32_t SubByte(uint32_t a);
|
|
||||||
uint8_t product(uint32_t x, uint32_t y);
|
|
||||||
uint32_t InvMixCol(uint32_t x);
|
|
||||||
uint8_t ByteSub(uint8_t x);
|
|
||||||
void gentables(void);
|
|
||||||
void gkey(int nb, int nk, const uint8_t* key);
|
|
||||||
void _encrypt(uint8_t* buff);
|
|
||||||
void _decrypt(uint8_t* buff);
|
|
||||||
|
|
||||||
public:
|
|
||||||
void encrypt(const uint8_t* iv, const uint8_t* inbuf, uint8_t* outbuf, uint64_t len);
|
|
||||||
void decrypt(const uint8_t* iv, const uint8_t* inbuf, uint8_t* outbuf, uint64_t len);
|
|
||||||
void setKey(const uint8_t* key);
|
|
||||||
};
|
|
||||||
|
|
||||||
uint8_t SoftwareAES::bmul(uint8_t x, uint8_t y)
|
|
||||||
{
|
{
|
||||||
/* x.y= AntiLog(Log(x) + Log(y)) */
|
/* x.y= AntiLog(Log(x) + Log(y)) */
|
||||||
if (x && y) return ptab[(ltab[x] + ltab[y]) % 255];
|
if (x && y) return ptab[(ltab[x] + ltab[y]) % 255];
|
||||||
else return 0;
|
else return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t SoftwareAES::SubByte(uint32_t a)
|
uint32_t SubByte(uint32_t a) const
|
||||||
{
|
{
|
||||||
uint8_t b[4];
|
uint8_t b[4];
|
||||||
unpack(a, b);
|
unpack(a, b);
|
||||||
@@ -103,7 +76,7 @@ uint32_t SoftwareAES::SubByte(uint32_t a)
|
|||||||
return pack(b);
|
return pack(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t SoftwareAES::product(uint32_t x, uint32_t y)
|
uint8_t product(uint32_t x, uint32_t y) const
|
||||||
{
|
{
|
||||||
/* dot product of two 4-byte arrays */
|
/* dot product of two 4-byte arrays */
|
||||||
uint8_t xb[4], yb[4];
|
uint8_t xb[4], yb[4];
|
||||||
@@ -112,7 +85,7 @@ uint8_t SoftwareAES::product(uint32_t x, uint32_t y)
|
|||||||
return bmul(xb[0], yb[0])^bmul(xb[1], yb[1])^bmul(xb[2], yb[2])^bmul(xb[3], yb[3]);
|
return bmul(xb[0], yb[0])^bmul(xb[1], yb[1])^bmul(xb[2], yb[2])^bmul(xb[3], yb[3]);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t SoftwareAES::InvMixCol(uint32_t x)
|
uint32_t InvMixCol(uint32_t x) const
|
||||||
{
|
{
|
||||||
/* matrix Multiplication */
|
/* matrix Multiplication */
|
||||||
uint32_t y, m;
|
uint32_t y, m;
|
||||||
@@ -130,7 +103,7 @@ uint32_t SoftwareAES::InvMixCol(uint32_t x)
|
|||||||
return y;
|
return y;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t SoftwareAES::ByteSub(uint8_t x)
|
uint8_t ByteSub(uint8_t x) const
|
||||||
{
|
{
|
||||||
uint8_t y = ptab[255 - ltab[x]]; /* multiplicative inverse */
|
uint8_t y = ptab[255 - ltab[x]]; /* multiplicative inverse */
|
||||||
x = y;
|
x = y;
|
||||||
@@ -146,7 +119,7 @@ uint8_t SoftwareAES::ByteSub(uint8_t x)
|
|||||||
return y;
|
return y;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SoftwareAES::gentables(void)
|
SoftwareAESTables()
|
||||||
{
|
{
|
||||||
/* generate tables */
|
/* generate tables */
|
||||||
int i;
|
int i;
|
||||||
@@ -202,6 +175,26 @@ void SoftwareAES::gentables(void)
|
|||||||
rtable[i] = pack(b);
|
rtable[i] = pack(b);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} AEStb;
|
||||||
|
|
||||||
|
class SoftwareAES : public IAES
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
/* Parameter-dependent data */
|
||||||
|
int Nk, Nb, Nr;
|
||||||
|
uint8_t fi[24], ri[24];
|
||||||
|
uint32_t fkey[120];
|
||||||
|
uint32_t rkey[120];
|
||||||
|
|
||||||
|
void gkey(int nb, int nk, const uint8_t* key);
|
||||||
|
void _encrypt(uint8_t* buff);
|
||||||
|
void _decrypt(uint8_t* buff);
|
||||||
|
|
||||||
|
public:
|
||||||
|
void encrypt(const uint8_t* iv, const uint8_t* inbuf, uint8_t* outbuf, uint64_t len);
|
||||||
|
void decrypt(const uint8_t* iv, const uint8_t* inbuf, uint8_t* outbuf, uint64_t len);
|
||||||
|
void setKey(const uint8_t* key);
|
||||||
|
};
|
||||||
|
|
||||||
void SoftwareAES::gkey(int nb, int nk, const uint8_t* key)
|
void SoftwareAES::gkey(int nb, int nk, const uint8_t* key)
|
||||||
{
|
{
|
||||||
@@ -247,7 +240,7 @@ void SoftwareAES::gkey(int nb, int nk, const uint8_t* key)
|
|||||||
|
|
||||||
for (j = Nk, k = 0; j < N; j += Nk, k++)
|
for (j = Nk, k = 0; j < N; j += Nk, k++)
|
||||||
{
|
{
|
||||||
fkey[j] = fkey[j - Nk] ^ SubByte(ROTL24(fkey[j - 1]))^rco[k];
|
fkey[j] = fkey[j - Nk] ^ AEStb.SubByte(ROTL24(fkey[j - 1]))^AEStb.rco[k];
|
||||||
|
|
||||||
if (Nk <= 6)
|
if (Nk <= 6)
|
||||||
{
|
{
|
||||||
@@ -259,7 +252,7 @@ void SoftwareAES::gkey(int nb, int nk, const uint8_t* key)
|
|||||||
for (i = 1; i < 4 && (i + j) < N; i++)
|
for (i = 1; i < 4 && (i + j) < N; i++)
|
||||||
fkey[i + j] = fkey[i + j - Nk] ^ fkey[i + j - 1];
|
fkey[i + j] = fkey[i + j - Nk] ^ fkey[i + j - 1];
|
||||||
|
|
||||||
if ((j + 4) < N) fkey[j + 4] = fkey[j + 4 - Nk] ^ SubByte(fkey[j + 3]);
|
if ((j + 4) < N) fkey[j + 4] = fkey[j + 4 - Nk] ^ AEStb.SubByte(fkey[j + 3]);
|
||||||
|
|
||||||
for (i = 5; i < Nk && (i + j) < N; i++)
|
for (i = 5; i < Nk && (i + j) < N; i++)
|
||||||
fkey[i + j] = fkey[i + j - Nk] ^ fkey[i + j - 1];
|
fkey[i + j] = fkey[i + j - Nk] ^ fkey[i + j - 1];
|
||||||
@@ -275,7 +268,7 @@ void SoftwareAES::gkey(int nb, int nk, const uint8_t* key)
|
|||||||
{
|
{
|
||||||
k = N - Nb - i;
|
k = N - Nb - i;
|
||||||
|
|
||||||
for (j = 0; j < Nb; j++) rkey[k + j] = InvMixCol(fkey[i + j]);
|
for (j = 0; j < Nb; j++) rkey[k + j] = AEStb.InvMixCol(fkey[i + j]);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (j = N - Nb; j < N; j++) rkey[j - N + Nb] = fkey[j];
|
for (j = N - Nb; j < N; j++) rkey[j - N + Nb] = fkey[j];
|
||||||
@@ -313,10 +306,10 @@ void SoftwareAES::_encrypt(uint8_t* buff)
|
|||||||
{
|
{
|
||||||
/* deal with each 32-bit element of the State */
|
/* deal with each 32-bit element of the State */
|
||||||
/* This is the time-critical bit */
|
/* This is the time-critical bit */
|
||||||
y[j] = fkey[k++] ^ ftable[(uint8_t)x[j]] ^
|
y[j] = fkey[k++] ^ AEStb.ftable[(uint8_t)x[j]] ^
|
||||||
ROTL8(ftable[(uint8_t)(x[fi[m]] >> 8)])^
|
ROTL8(AEStb.ftable[(uint8_t)(x[fi[m]] >> 8)])^
|
||||||
ROTL16(ftable[(uint8_t)(x[fi[m + 1]] >> 16)])^
|
ROTL16(AEStb.ftable[(uint8_t)(x[fi[m + 1]] >> 16)])^
|
||||||
ROTL24(ftable[(uint8_t)(x[fi[m + 2]] >> 24)]);
|
ROTL24(AEStb.ftable[(uint8_t)(x[fi[m + 2]] >> 24)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
t = x;
|
t = x;
|
||||||
@@ -327,10 +320,10 @@ void SoftwareAES::_encrypt(uint8_t* buff)
|
|||||||
/* Last Round - unroll if possible */
|
/* Last Round - unroll if possible */
|
||||||
for (m = j = 0; j < Nb; j++, m += 3)
|
for (m = j = 0; j < Nb; j++, m += 3)
|
||||||
{
|
{
|
||||||
y[j] = fkey[k++] ^ (uint32_t)fbsub[(uint8_t)x[j]] ^
|
y[j] = fkey[k++] ^ (uint32_t)AEStb.fbsub[(uint8_t)x[j]] ^
|
||||||
ROTL8((uint32_t)fbsub[(uint8_t)(x[fi[m]] >> 8)])^
|
ROTL8((uint32_t)AEStb.fbsub[(uint8_t)(x[fi[m]] >> 8)])^
|
||||||
ROTL16((uint32_t)fbsub[(uint8_t)(x[fi[m + 1]] >> 16)])^
|
ROTL16((uint32_t)AEStb.fbsub[(uint8_t)(x[fi[m + 1]] >> 16)])^
|
||||||
ROTL24((uint32_t)fbsub[(uint8_t)(x[fi[m + 2]] >> 24)]);
|
ROTL24((uint32_t)AEStb.fbsub[(uint8_t)(x[fi[m + 2]] >> 24)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = j = 0; i < Nb; i++, j += 4)
|
for (i = j = 0; i < Nb; i++, j += 4)
|
||||||
@@ -368,10 +361,10 @@ void SoftwareAES::_decrypt(uint8_t* buff)
|
|||||||
for (m = j = 0; j < Nb; j++, m += 3)
|
for (m = j = 0; j < Nb; j++, m += 3)
|
||||||
{
|
{
|
||||||
/* This is the time-critical bit */
|
/* This is the time-critical bit */
|
||||||
y[j] = rkey[k++] ^ rtable[(uint8_t)x[j]] ^
|
y[j] = rkey[k++] ^ AEStb.rtable[(uint8_t)x[j]] ^
|
||||||
ROTL8(rtable[(uint8_t)(x[ri[m]] >> 8)])^
|
ROTL8(AEStb.rtable[(uint8_t)(x[ri[m]] >> 8)])^
|
||||||
ROTL16(rtable[(uint8_t)(x[ri[m + 1]] >> 16)])^
|
ROTL16(AEStb.rtable[(uint8_t)(x[ri[m + 1]] >> 16)])^
|
||||||
ROTL24(rtable[(uint8_t)(x[ri[m + 2]] >> 24)]);
|
ROTL24(AEStb.rtable[(uint8_t)(x[ri[m + 2]] >> 24)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
t = x;
|
t = x;
|
||||||
@@ -382,10 +375,10 @@ void SoftwareAES::_decrypt(uint8_t* buff)
|
|||||||
/* Last Round - unroll if possible */
|
/* Last Round - unroll if possible */
|
||||||
for (m = j = 0; j < Nb; j++, m += 3)
|
for (m = j = 0; j < Nb; j++, m += 3)
|
||||||
{
|
{
|
||||||
y[j] = rkey[k++] ^ (uint32_t)rbsub[(uint8_t)x[j]] ^
|
y[j] = rkey[k++] ^ (uint32_t)AEStb.rbsub[(uint8_t)x[j]] ^
|
||||||
ROTL8((uint32_t)rbsub[(uint8_t)(x[ri[m]] >> 8)])^
|
ROTL8((uint32_t)AEStb.rbsub[(uint8_t)(x[ri[m]] >> 8)])^
|
||||||
ROTL16((uint32_t)rbsub[(uint8_t)(x[ri[m + 1]] >> 16)])^
|
ROTL16((uint32_t)AEStb.rbsub[(uint8_t)(x[ri[m + 1]] >> 16)])^
|
||||||
ROTL24((uint32_t)rbsub[(uint8_t)(x[ri[m + 2]] >> 24)]);
|
ROTL24((uint32_t)AEStb.rbsub[(uint8_t)(x[ri[m + 2]] >> 24)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = j = 0; i < Nb; i++, j += 4)
|
for (i = j = 0; i < Nb; i++, j += 4)
|
||||||
@@ -399,7 +392,6 @@ void SoftwareAES::_decrypt(uint8_t* buff)
|
|||||||
|
|
||||||
void SoftwareAES::setKey(const uint8_t* key)
|
void SoftwareAES::setKey(const uint8_t* key)
|
||||||
{
|
{
|
||||||
gentables();
|
|
||||||
gkey(4, 4, key);
|
gkey(4, 4, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user