- uses: actions/checkout@v2
- name: Install deps
run: |
- sudo apt-get install g++ gcc libxxf86vm-dev libgl1-mesa-dev libbz2-dev libpng-dev libjpeg-dev zlib1g-dev -qyy
+ sudo apt-get install g++ gcc libxxf86vm-dev libgl1-mesa-dev libpng-dev libjpeg-dev zlib1g-dev -qyy
- name: Build
run: |
#ifdef NO_IRR_COMPILE_WITH_ZLIB_\r
#undef _IRR_COMPILE_WITH_ZLIB_\r
#endif\r
-//! Define _IRR_COMPILE_WITH_ZIP_ENCRYPTION_ if you want to read AES-encrypted ZIP archives\r
-#define _IRR_COMPILE_WITH_ZIP_ENCRYPTION_\r
-#ifdef NO_IRR_COMPILE_WITH_ZIP_ENCRYPTION_\r
-#undef _IRR_COMPILE_WITH_ZIP_ENCRYPTION_\r
-#endif\r
-//! Define _IRR_COMPILE_WITH_BZIP2_ if you want to support bzip2 compressed zip archives\r
-/** bzip2 is superior to the original zip file compression modes, but requires\r
-a certain amount of memory for decompression and adds several files to the\r
-library. */\r
-#define _IRR_COMPILE_WITH_BZIP2_\r
-#ifdef NO_IRR_COMPILE_WITH_BZIP2_\r
-#undef _IRR_COMPILE_WITH_BZIP2_\r
-#endif\r
-//! Define _IRR_COMPILE_WITH_LZMA_ if you want to use LZMA compressed zip files.\r
-/** LZMA is a very efficient compression code, known from 7zip. Irrlicht\r
-currently only supports zip archives, though. */\r
-#define _IRR_COMPILE_WITH_LZMA_\r
-#ifdef NO_IRR_COMPILE_WITH_LZMA_\r
-#undef _IRR_COMPILE_WITH_LZMA_\r
-#endif\r
#endif\r
\r
//! Define __IRR_COMPILE_WITH_MOUNT_ARCHIVE_LOADER_ if you want to mount folders as archives\r
\r
#include "os.h"\r
\r
-// This method is used for error output from bzip2.\r
-extern "C" void bz_internal_error(int errorCode)\r
-{\r
- irr::os::Printer::log("Error in bzip2 handling", irr::core::stringc(errorCode), irr::ELL_ERROR);\r
-}\r
\r
#ifdef __IRR_COMPILE_WITH_ZIP_ARCHIVE_LOADER_\r
\r
#include "IrrCompileConfig.h"\r
#ifdef _IRR_COMPILE_WITH_ZLIB_\r
#include <zlib.h> // use system lib\r
-\r
- #ifdef _IRR_COMPILE_WITH_ZIP_ENCRYPTION_\r
- #include "aesGladman/fileenc.h"\r
- #endif\r
- #ifdef _IRR_COMPILE_WITH_BZIP2_\r
- #include <bzlib.h>\r
- #endif\r
- #ifdef _IRR_COMPILE_WITH_LZMA_\r
- #include "lzma/LzmaDec.h"\r
- #endif\r
#endif\r
\r
namespace irr\r
delete [] tmp;\r
}\r
\r
-#ifdef _IRR_COMPILE_WITH_ZIP_ENCRYPTION_\r
- // AES encryption\r
- if ((entry.header.GeneralBitFlag & ZIP_FILE_ENCRYPTED) && (entry.header.CompressionMethod == 99))\r
- {\r
- s16 restSize = entry.header.ExtraFieldLength;\r
- SZipFileExtraHeader extraHeader;\r
- while (restSize)\r
- {\r
- File->read(&extraHeader, sizeof(extraHeader));\r
-#ifdef __BIG_ENDIAN__\r
- extraHeader.ID = os::Byteswap::byteswap(extraHeader.ID);\r
- extraHeader.Size = os::Byteswap::byteswap(extraHeader.Size);\r
-#endif\r
- restSize -= sizeof(extraHeader);\r
- if (extraHeader.ID==(s16)0x9901)\r
- {\r
- SZipFileAESExtraData data;\r
- File->read(&data, sizeof(data));\r
-#ifdef __BIG_ENDIAN__\r
- data.Version = os::Byteswap::byteswap(data.Version);\r
- data.CompressionMode = os::Byteswap::byteswap(data.CompressionMode);\r
-#endif\r
- restSize -= sizeof(data);\r
- if (data.Vendor[0]=='A' && data.Vendor[1]=='E')\r
- {\r
- // encode values into Sig\r
- // AE-Version | Strength | ActualMode\r
- entry.header.Sig =\r
- ((data.Version & 0xff) << 24) |\r
- (data.EncryptionStrength << 16) |\r
- (data.CompressionMode);\r
- File->seek(restSize, true);\r
- break;\r
- }\r
- }\r
- }\r
- }\r
- // move forward length of extra field.\r
- else\r
-#endif\r
if (entry.header.ExtraFieldLength)\r
File->seek(entry.header.ExtraFieldLength, true);\r
\r
return 0;\r
}\r
\r
-#ifdef _IRR_COMPILE_WITH_LZMA_\r
-//! Used for LZMA decompression. The lib has no default memory management\r
-namespace\r
-{\r
- void *SzAlloc(void *p, size_t size)\r
- {\r
- (void)p; // disable unused variable warnings\r
- return malloc(size);\r
- }\r
- void SzFree(void *p, void *address)\r
- {\r
- (void)p; // disable unused variable warnings\r
- free(address);\r
- }\r
- ISzAlloc lzmaAlloc = { SzAlloc, SzFree };\r
-}\r
-#endif\r
-\r
//! opens a file by index\r
IReadFile* CZipReader::createAndOpenFile(u32 index)\r
{\r
IReadFile* decrypted=0;\r
u8* decryptedBuf=0;\r
u32 decryptedSize=e.header.DataDescriptor.CompressedSize;\r
-#ifdef _IRR_COMPILE_WITH_ZIP_ENCRYPTION_\r
- if ((e.header.GeneralBitFlag & ZIP_FILE_ENCRYPTED) && (e.header.CompressionMethod == 99))\r
- {\r
- os::Printer::log("Reading encrypted file.");\r
- u8 salt[16]={0};\r
- const u16 saltSize = (((e.header.Sig & 0x00ff0000) >>16)+1)*4;\r
- File->seek(e.Offset);\r
- File->read(salt, saltSize);\r
- char pwVerification[2];\r
- char pwVerificationFile[2];\r
- File->read(pwVerification, 2);\r
- fcrypt_ctx zctx; // the encryption context\r
- int rc = fcrypt_init(\r
- (e.header.Sig & 0x00ff0000) >>16,\r
- (const unsigned char*)Password.c_str(), // the password\r
- Password.size(), // number of bytes in password\r
- salt, // the salt\r
- (unsigned char*)pwVerificationFile, // on return contains password verifier\r
- &zctx); // encryption context\r
- if (strncmp(pwVerificationFile, pwVerification, 2))\r
- {\r
- os::Printer::log("Wrong password");\r
- return 0;\r
- }\r
- decryptedSize= e.header.DataDescriptor.CompressedSize-saltSize-12;\r
- decryptedBuf= new u8[decryptedSize];\r
- u32 c = 0;\r
- while ((c+32768)<=decryptedSize)\r
- {\r
- File->read(decryptedBuf+c, 32768);\r
- fcrypt_decrypt(\r
- decryptedBuf+c, // pointer to the data to decrypt\r
- 32768, // how many bytes to decrypt\r
- &zctx); // decryption context\r
- c+=32768;\r
- }\r
- File->read(decryptedBuf+c, decryptedSize-c);\r
- fcrypt_decrypt(\r
- decryptedBuf+c, // pointer to the data to decrypt\r
- decryptedSize-c, // how many bytes to decrypt\r
- &zctx); // decryption context\r
-\r
- char fileMAC[10];\r
- char resMAC[10];\r
- rc = fcrypt_end(\r
- (unsigned char*)resMAC, // on return contains the authentication code\r
- &zctx); // encryption context\r
- if (rc != 10)\r
- {\r
- os::Printer::log("Error on encryption closing");\r
- delete [] decryptedBuf;\r
- return 0;\r
- }\r
- File->read(fileMAC, 10);\r
- if (strncmp(fileMAC, resMAC, 10))\r
- {\r
- os::Printer::log("Error on encryption check");\r
- delete [] decryptedBuf;\r
- return 0;\r
- }\r
- decrypted = FileSystem->createMemoryReadFile(decryptedBuf, decryptedSize, Files[index].FullName, true);\r
- actualCompressionMethod = (e.header.Sig & 0xffff);\r
-#if 0\r
- if ((e.header.Sig & 0xff000000)==0x01000000)\r
- {\r
- }\r
- else if ((e.header.Sig & 0xff000000)==0x02000000)\r
- {\r
- }\r
- else\r
- {\r
- os::Printer::log("Unknown encryption method");\r
- return 0;\r
- }\r
-#endif\r
- }\r
-#endif\r
switch(actualCompressionMethod)\r
{\r
case 0: // no compression\r
}\r
case 12:\r
{\r
- #ifdef _IRR_COMPILE_WITH_BZIP2_\r
-\r
- const u32 uncompressedSize = e.header.DataDescriptor.UncompressedSize;\r
- c8* pBuf = new c8[ uncompressedSize ];\r
- if (!pBuf)\r
- {\r
- swprintf_irr ( buf, 64, L"Not enough memory for decompressing %s", core::stringw(Files[index].FullName).c_str() );\r
- os::Printer::log( buf, ELL_ERROR);\r
- if (decrypted)\r
- decrypted->drop();\r
- return 0;\r
- }\r
-\r
- u8 *pcData = decryptedBuf;\r
- if (!pcData)\r
- {\r
- pcData = new u8[decryptedSize];\r
- if (!pcData)\r
- {\r
- swprintf_irr ( buf, 64, L"Not enough memory for decompressing %s", core::stringw(Files[index].FullName).c_str() );\r
- os::Printer::log( buf, ELL_ERROR);\r
- delete [] pBuf;\r
- return 0;\r
- }\r
-\r
- //memset(pcData, 0, decryptedSize);\r
- File->seek(e.Offset);\r
- File->read(pcData, decryptedSize);\r
- }\r
-\r
- bz_stream bz_ctx;\r
- memset(&bz_ctx, 0, sizeof(bz_ctx));\r
- /* use BZIP2's default memory allocation\r
- bz_ctx->bzalloc = NULL;\r
- bz_ctx->bzfree = NULL;\r
- bz_ctx->opaque = NULL;\r
- */\r
- int err = BZ2_bzDecompressInit(&bz_ctx, 0, 0); /* decompression */\r
- if(err != BZ_OK)\r
- {\r
- os::Printer::log("bzip2 decompression failed. File cannot be read.", ELL_ERROR);\r
- return 0;\r
- }\r
- bz_ctx.next_in = (char*)pcData;\r
- bz_ctx.avail_in = decryptedSize;\r
- /* pass all input to decompressor */\r
- bz_ctx.next_out = pBuf;\r
- bz_ctx.avail_out = uncompressedSize;\r
- err = BZ2_bzDecompress(&bz_ctx);\r
- err = BZ2_bzDecompressEnd(&bz_ctx);\r
-\r
- if (decrypted)\r
- decrypted->drop();\r
- else\r
- delete[] pcData;\r
-\r
- if (err != BZ_OK)\r
- {\r
- swprintf_irr ( buf, 64, L"Error decompressing %s", core::stringw(Files[index].FullName).c_str() );\r
- os::Printer::log( buf, ELL_ERROR);\r
- delete [] pBuf;\r
- return 0;\r
- }\r
- else\r
- return FileSystem->createMemoryReadFile(pBuf, uncompressedSize, Files[index].FullName, true);\r
-\r
- #else\r
os::Printer::log("bzip2 decompression not supported. File cannot be read.", ELL_ERROR);\r
return 0;\r
- #endif\r
}\r
case 14:\r
{\r
- #ifdef _IRR_COMPILE_WITH_LZMA_\r
-\r
- u32 uncompressedSize = e.header.DataDescriptor.UncompressedSize;\r
- c8* pBuf = new c8[ uncompressedSize ];\r
- if (!pBuf)\r
- {\r
- swprintf_irr ( buf, 64, L"Not enough memory for decompressing %s", core::stringw(Files[index].FullName).c_str() );\r
- os::Printer::log( buf, ELL_ERROR);\r
- if (decrypted)\r
- decrypted->drop();\r
- return 0;\r
- }\r
-\r
- u8 *pcData = decryptedBuf;\r
- if (!pcData)\r
- {\r
- pcData = new u8[decryptedSize];\r
- if (!pcData)\r
- {\r
- swprintf_irr ( buf, 64, L"Not enough memory for decompressing %s", core::stringw(Files[index].FullName).c_str() );\r
- os::Printer::log( buf, ELL_ERROR);\r
- delete [] pBuf;\r
- return 0;\r
- }\r
-\r
- //memset(pcData, 0, decryptedSize);\r
- File->seek(e.Offset);\r
- File->read(pcData, decryptedSize);\r
- }\r
-\r
- ELzmaStatus status;\r
- SizeT tmpDstSize = uncompressedSize;\r
- SizeT tmpSrcSize = decryptedSize;\r
-\r
- unsigned int propSize = (pcData[3]<<8)+pcData[2];\r
- int err = LzmaDecode((Byte*)pBuf, &tmpDstSize,\r
- pcData+4+propSize, &tmpSrcSize,\r
- pcData+4, propSize,\r
- e.header.GeneralBitFlag&0x1?LZMA_FINISH_END:LZMA_FINISH_ANY, &status,\r
- &lzmaAlloc);\r
- uncompressedSize = tmpDstSize; // may be different to expected value\r
-\r
- if (decrypted)\r
- decrypted->drop();\r
- else\r
- delete[] pcData;\r
-\r
- if (err != SZ_OK)\r
- {\r
- os::Printer::log( "Error decompressing", Files[index].FullName, ELL_ERROR);\r
- delete [] pBuf;\r
- return 0;\r
- }\r
- else\r
- return FileSystem->createMemoryReadFile(pBuf, uncompressedSize, Files[index].FullName, true);\r
-\r
- #else\r
os::Printer::log("lzma decompression not supported. File cannot be read.", ELL_ERROR);\r
return 0;\r
- #endif\r
}\r
case 99:\r
// If we come here with an encrypted file, decryption support is missing\r
CTRGouraudAlpha2.o CTRGouraudAlphaNoZ2.o CTRTextureDetailMap2.o CTRTextureGouraudAdd2.o CTRTextureGouraudAddNoZ2.o CTRTextureWire2.o \\r
CTRTextureLightMap2_Add.o CTRTextureLightMapGouraud2_M4.o IBurningShader.o CTRTextureBlend.o CTRTextureGouraudAlpha.o \\r
CTRTextureGouraudAlphaNoZ.o CDepthBuffer.o CBurningShader_Raster_Reference.o CTR_transparent_reflection_2_layer.o CTRGouraudNoZ2.o burning_shader_color.o\r
-IRRIOOBJ = CFileList.o CFileSystem.o CLimitReadFile.o CMemoryFile.o CReadFile.o CWriteFile.o CXMLReader.o CXMLWriter.o CWADReader.o CZipReader.o CPakReader.o CNPKReader.o CTarReader.o CMountPointReader.o irrXML.o CAttributes.o lzma/LzmaDec.o\r
+IRRIOOBJ = CFileList.o CFileSystem.o CLimitReadFile.o CMemoryFile.o CReadFile.o CWriteFile.o CXMLReader.o CXMLWriter.o CWADReader.o CZipReader.o CPakReader.o CNPKReader.o CTarReader.o CMountPointReader.o irrXML.o CAttributes.o\r
IRROTHEROBJ = CIrrDeviceSDL.o CIrrDeviceLinux.o CIrrDeviceConsole.o CIrrDeviceStub.o CIrrDeviceWin32.o CIrrDeviceFB.o CLogger.o COSOperator.o Irrlicht.o os.o leakHunter.o CProfiler.o utf8.o\r
IRRGUIOBJ = CGUIButton.o CGUICheckBox.o CGUIComboBox.o CGUIContextMenu.o CGUIEditBox.o CGUIEnvironment.o CGUIFileOpenDialog.o CGUIFont.o CGUIImage.o CGUIInOutFader.o CGUIListBox.o CGUIMenu.o CGUIMeshViewer.o CGUIMessageBox.o CGUIModalScreen.o CGUIScrollBar.o CGUISpinBox.o CGUISkin.o CGUIStaticText.o CGUITabControl.o CGUITable.o CGUIToolBar.o CGUIWindow.o CGUIColorSelectDialog.o CDefaultGUIElementFactory.o CGUISpriteBank.o CGUIImageList.o CGUITreeView.o CGUIProfiler.o\r
-LIBAESGM = aesGladman/aescrypt.o aesGladman/aeskey.o aesGladman/aestab.o aesGladman/fileenc.o aesGladman/hmac.o aesGladman/prng.o aesGladman/pwd2key.o aesGladman/sha1.o aesGladman/sha2.o\r
\r
# Next variable is for additional scene nodes etc. of customized Irrlicht versions\r
EXTRAOBJ =\r
LINKOBJ = $(IRRMESHOBJ) $(IRROBJ) $(IRRPARTICLEOBJ) $(IRRANIMOBJ) \\r
$(IRRVIDEOOBJ) $(IRRSWRENDEROBJ) $(IRRIOOBJ) $(IRROTHEROBJ) \\r
- $(IRRGUIOBJ) $(LIBAESGM) $(EXTRAOBJ)\r
+ $(IRRGUIOBJ) $(EXTRAOBJ)\r
\r
emscripten: EMSCRIPTEN=1\r
\r
+++ /dev/null
-A File Encryption Utility - VC++ 7.1 project Instructions\r
-\r
-1. Unzip the enclosed files into a suitable VC++ project directory.\r
-2. Obtain the bzip2 source code from http://sources.redhat.com/bzip2/\r
- and unzip the files into the bzip2 sub-directory.\r
-3. Compile the bzip2 project to give a static library\r
-4. Compile the encfile project.\r
-5. The executable encfile.exe is now ready for use:\r
-\r
- enfile password filename\r
- \r
- If the filename does not have the extension 'enc', it is assumed to \r
- be a normal file that will then be encrypted to a file with the same\r
- name but with an added extension 'enc'.\r
- \r
- If the filename has the extension 'enc' its is assumed to be an \r
- encrypted file that will be decrypted to a file with the same name\r
- but without the 'enc' extension.\r
-\r
-The default HASH function is SHA1, which is set up by defining USE_SHA1 in\r
-compiling the project. If USE_SHA256 is defined instead then SHA256 is used.\r
-\r
-Brian Gladman\r
- \r
- \r
+++ /dev/null
-/*\r
- ---------------------------------------------------------------------------\r
- Copyright (c) 2003, Dr Brian Gladman < >, Worcester, UK.\r
- All rights reserved.\r
-\r
- LICENSE TERMS\r
-\r
- The free distribution and use of this software in both source and binary\r
- form is allowed (with or without changes) provided that:\r
-\r
- 1. distributions of this source code include the above copyright\r
- notice, this list of conditions and the following disclaimer;\r
-\r
- 2. distributions in binary form include the above copyright\r
- notice, this list of conditions and the following disclaimer\r
- in the documentation and/or other associated materials;\r
-\r
- 3. the copyright holder's name is not used to endorse products\r
- built using this software without specific written permission.\r
-\r
- ALTERNATIVELY, provided that this notice is retained in full, this product\r
- may be distributed under the terms of the GNU General Public License (GPL),\r
- in which case the provisions of the GPL apply INSTEAD OF those given above.\r
-\r
- DISCLAIMER\r
-\r
- This software is provided 'as is' with no explicit or implied warranties\r
- in respect of its properties, including, but not limited to, correctness\r
- and/or fitness for purpose.\r
- ---------------------------------------------------------------------------\r
- Issue Date: 26/08/2003\r
-\r
- This file contains the definitions required to use AES in C. See aesopt.h\r
- for optimisation details.\r
-*/\r
-\r
-#ifndef _AES_H\r
-#define _AES_H\r
-\r
-#include "irrMath.h"\r
-\r
-#define AES_128 /* define if AES with 128 bit keys is needed */\r
-#define AES_192 /* define if AES with 192 bit keys is needed */\r
-#define AES_256 /* define if AES with 256 bit keys is needed */\r
-#define AES_VAR /* define if a variable key size is needed */\r
-\r
-/* The following must also be set in assembler files if being used */\r
-\r
-#define AES_ENCRYPT /* if support for encryption is needed */\r
-#define AES_DECRYPT /* if support for decryption is needed */\r
-#define AES_ERR_CHK /* for parameter checks & error return codes */\r
-\r
-typedef irr::u8 aes_08t;\r
-typedef irr::u32 aes_32t;\r
-\r
-#define AES_BLOCK_SIZE 16 /* the AES block size in bytes */\r
-#define N_COLS 4 /* the number of columns in the state */\r
-\r
-/* a maximum of 60 32-bit words are needed for the key schedule */\r
-#define KS_LENGTH 64\r
-\r
-#ifdef AES_ERR_CHK\r
-#define aes_ret int\r
-#define aes_good 0\r
-#define aes_error -1\r
-#else\r
-#define aes_ret void\r
-#endif\r
-\r
-#ifndef AES_DLL /* implement normal/DLL functions */\r
-#define aes_rval aes_ret\r
-#else\r
-#define aes_rval aes_ret __declspec(dllexport) _stdcall\r
-#endif\r
-\r
-/* This routine must be called before first use if non-static */\r
-/* tables are being used */\r
-\r
-void gen_tabs(void);\r
-\r
-/* The key length (klen) is input in bytes when it is in the range */\r
-/* 16 <= klen <= 32 or in bits when in the range 128 <= klen <= 256 */\r
-\r
-#ifdef AES_ENCRYPT\r
-\r
-typedef struct \r
-{\r
- aes_32t ks[KS_LENGTH];\r
-} aes_encrypt_ctx;\r
-\r
-#if defined(AES_128) || defined(AES_VAR)\r
-aes_rval aes_encrypt_key128(const void *in_key, aes_encrypt_ctx cx[1]);\r
-#endif\r
-\r
-#if defined(AES_192) || defined(AES_VAR)\r
-aes_rval aes_encrypt_key192(const void *in_key, aes_encrypt_ctx cx[1]);\r
-#endif\r
-\r
-#if defined(AES_256) || defined(AES_VAR)\r
-aes_rval aes_encrypt_key256(const void *in_key, aes_encrypt_ctx cx[1]);\r
-#endif\r
-\r
-#if defined(AES_VAR)\r
-aes_rval aes_encrypt_key(const void *in_key, int key_len, aes_encrypt_ctx cx[1]);\r
-#endif\r
-\r
-aes_rval aes_encrypt(const void *in_blk, void *out_blk, const aes_encrypt_ctx cx[1]);\r
-#endif\r
-\r
-#ifdef AES_DECRYPT\r
-\r
-typedef struct \r
-{\r
- aes_32t ks[KS_LENGTH];\r
-} aes_decrypt_ctx;\r
-\r
-#if defined(AES_128) || defined(AES_VAR)\r
-aes_rval aes_decrypt_key128(const void *in_key, aes_decrypt_ctx cx[1]);\r
-#endif\r
-\r
-#if defined(AES_192) || defined(AES_VAR)\r
-aes_rval aes_decrypt_key192(const void *in_key, aes_decrypt_ctx cx[1]);\r
-#endif\r
-\r
-#if defined(AES_256) || defined(AES_VAR)\r
-aes_rval aes_decrypt_key256(const void *in_key, aes_decrypt_ctx cx[1]);\r
-#endif\r
-\r
-#if defined(AES_VAR)\r
-aes_rval aes_decrypt_key(const void *in_key, int key_len, aes_decrypt_ctx cx[1]);\r
-#endif\r
-\r
-aes_rval aes_decrypt(const void *in_blk, void *out_blk, const aes_decrypt_ctx cx[1]);\r
-#endif\r
-\r
-#endif\r
-\r
+++ /dev/null
-/*\r
- ---------------------------------------------------------------------------\r
- Copyright (c) 2003, Dr Brian Gladman < >, Worcester, UK.\r
- All rights reserved.\r
-\r
- LICENSE TERMS\r
-\r
- The free distribution and use of this software in both source and binary\r
- form is allowed (with or without changes) provided that:\r
-\r
- 1. distributions of this source code include the above copyright\r
- notice, this list of conditions and the following disclaimer;\r
-\r
- 2. distributions in binary form include the above copyright\r
- notice, this list of conditions and the following disclaimer\r
- in the documentation and/or other associated materials;\r
-\r
- 3. the copyright holder's name is not used to endorse products\r
- built using this software without specific written permission.\r
-\r
- ALTERNATIVELY, provided that this notice is retained in full, this product\r
- may be distributed under the terms of the GNU General Public License (GPL),\r
- in which case the provisions of the GPL apply INSTEAD OF those given above.\r
-\r
- DISCLAIMER\r
-\r
- This software is provided 'as is' with no explicit or implied warranties\r
- in respect of its properties, including, but not limited to, correctness\r
- and/or fitness for purpose.\r
- ---------------------------------------------------------------------------\r
- Issue Date: 26/08/2003\r
-\r
- This file contains the code for implementing encryption and decryption\r
- for AES (Rijndael) for block and key sizes of 16, 24 and 32 bytes. It\r
- can optionally be replaced by code written in assembler using NASM. For\r
- further details see the file aesopt.h\r
-*/\r
-\r
-#include "aesopt.h"\r
-\r
-#define si(y,x,k,c) (s(y,c) = word_in(x, c) ^ (k)[c])\r
-#define so(y,x,c) word_out(y, c, s(x,c))\r
-\r
-#if defined(ARRAYS)\r
-#define locals(y,x) x[4],y[4]\r
-#else\r
-#define locals(y,x) x##0,x##1,x##2,x##3,y##0,y##1,y##2,y##3\r
-#endif\r
-\r
-#define l_copy(y, x) s(y,0) = s(x,0); s(y,1) = s(x,1); \\r
- s(y,2) = s(x,2); s(y,3) = s(x,3);\r
-#define state_in(y,x,k) si(y,x,k,0); si(y,x,k,1); si(y,x,k,2); si(y,x,k,3)\r
-#define state_out(y,x) so(y,x,0); so(y,x,1); so(y,x,2); so(y,x,3)\r
-#define round(rm,y,x,k) rm(y,x,k,0); rm(y,x,k,1); rm(y,x,k,2); rm(y,x,k,3)\r
-\r
-#if defined(ENCRYPTION) && !defined(AES_ASM)\r
-\r
-/* Visual C++ .Net v7.1 provides the fastest encryption code when using\r
- Pentium optimization with small code but this is poor for decryption\r
- so we need to control this with the following VC++ pragmas\r
-*/\r
-\r
-#if defined(_MSC_VER)\r
-#pragma optimize( "s", on )\r
-#endif\r
-\r
-/* Given the column (c) of the output state variable, the following\r
- macros give the input state variables which are needed in its\r
- computation for each row (r) of the state. All the alternative\r
- macros give the same end values but expand into different ways\r
- of calculating these values. In particular the complex macro\r
- used for dynamically variable block sizes is designed to expand\r
- to a compile time constant whenever possible but will expand to\r
- conditional clauses on some branches (I am grateful to Frank\r
- Yellin for this construction)\r
-*/\r
-\r
-#define fwd_var(x,r,c)\\r
- ( r == 0 ? ( c == 0 ? s(x,0) : c == 1 ? s(x,1) : c == 2 ? s(x,2) : s(x,3))\\r
- : r == 1 ? ( c == 0 ? s(x,1) : c == 1 ? s(x,2) : c == 2 ? s(x,3) : s(x,0))\\r
- : r == 2 ? ( c == 0 ? s(x,2) : c == 1 ? s(x,3) : c == 2 ? s(x,0) : s(x,1))\\r
- : ( c == 0 ? s(x,3) : c == 1 ? s(x,0) : c == 2 ? s(x,1) : s(x,2)))\r
-\r
-#if defined(FT4_SET)\r
-#undef dec_fmvars\r
-#define fwd_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(f,n),fwd_var,rf1,c))\r
-#elif defined(FT1_SET)\r
-#undef dec_fmvars\r
-#define fwd_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ one_table(x,upr,t_use(f,n),fwd_var,rf1,c))\r
-#else\r
-#define fwd_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ fwd_mcol(no_table(x,t_use(s,box),fwd_var,rf1,c)))\r
-#endif\r
-\r
-#if defined(FL4_SET)\r
-#define fwd_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(f,l),fwd_var,rf1,c))\r
-#elif defined(FL1_SET)\r
-#define fwd_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ one_table(x,ups,t_use(f,l),fwd_var,rf1,c))\r
-#else\r
-#define fwd_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ no_table(x,t_use(s,box),fwd_var,rf1,c))\r
-#endif\r
-\r
-aes_rval aes_encrypt(const void *in_blk, void *out_blk, const aes_encrypt_ctx cx[1])\r
-{ aes_32t locals(b0, b1);\r
- const aes_32t *kp = cx->ks;\r
-#ifdef dec_fmvars\r
- dec_fmvars; /* declare variables for fwd_mcol() if needed */\r
-#endif\r
-\r
- aes_32t nr = (kp[45] ^ kp[52] ^ kp[53] ? kp[52] : 14);\r
-\r
-#ifdef AES_ERR_CHK\r
- if( (nr != 10 || !(kp[0] | kp[3] | kp[4]))\r
- && (nr != 12 || !(kp[0] | kp[5] | kp[6]))\r
- && (nr != 14 || !(kp[0] | kp[7] | kp[8])) )\r
- return aes_error;\r
-#endif\r
-\r
- state_in(b0, in_blk, kp);\r
-\r
-#if (ENC_UNROLL == FULL)\r
-\r
- switch(nr)\r
- {\r
- case 14:\r
- round(fwd_rnd, b1, b0, kp + 1 * N_COLS);\r
- round(fwd_rnd, b0, b1, kp + 2 * N_COLS);\r
- kp += 2 * N_COLS;\r
- /* Falls through. */\r
- case 12:\r
- round(fwd_rnd, b1, b0, kp + 1 * N_COLS);\r
- round(fwd_rnd, b0, b1, kp + 2 * N_COLS);\r
- kp += 2 * N_COLS;\r
- /* Falls through. */\r
- case 10:\r
- round(fwd_rnd, b1, b0, kp + 1 * N_COLS);\r
- round(fwd_rnd, b0, b1, kp + 2 * N_COLS);\r
- round(fwd_rnd, b1, b0, kp + 3 * N_COLS);\r
- round(fwd_rnd, b0, b1, kp + 4 * N_COLS);\r
- round(fwd_rnd, b1, b0, kp + 5 * N_COLS);\r
- round(fwd_rnd, b0, b1, kp + 6 * N_COLS);\r
- round(fwd_rnd, b1, b0, kp + 7 * N_COLS);\r
- round(fwd_rnd, b0, b1, kp + 8 * N_COLS);\r
- round(fwd_rnd, b1, b0, kp + 9 * N_COLS);\r
- round(fwd_lrnd, b0, b1, kp +10 * N_COLS);\r
- }\r
-\r
-#else\r
-\r
-#if (ENC_UNROLL == PARTIAL)\r
- { aes_32t rnd;\r
- for(rnd = 0; rnd < (nr >> 1) - 1; ++rnd)\r
- {\r
- kp += N_COLS;\r
- round(fwd_rnd, b1, b0, kp);\r
- kp += N_COLS;\r
- round(fwd_rnd, b0, b1, kp);\r
- }\r
- kp += N_COLS;\r
- round(fwd_rnd, b1, b0, kp);\r
-#else\r
- { aes_32t rnd;\r
- for(rnd = 0; rnd < nr - 1; ++rnd)\r
- {\r
- kp += N_COLS;\r
- round(fwd_rnd, b1, b0, kp);\r
- l_copy(b0, b1);\r
- }\r
-#endif\r
- kp += N_COLS;\r
- round(fwd_lrnd, b0, b1, kp);\r
- }\r
-#endif\r
-\r
- state_out(out_blk, b0);\r
-#ifdef AES_ERR_CHK\r
- return aes_good;\r
-#endif\r
-}\r
-\r
-#endif\r
-\r
-#if defined(DECRYPTION) && !defined(AES_ASM)\r
-\r
-/* Visual C++ .Net v7.1 provides the fastest encryption code when using\r
- Pentium optimization with small code but this is poor for decryption\r
- so we need to control this with the following VC++ pragmas\r
-*/\r
-\r
-#if defined(_MSC_VER)\r
-#pragma optimize( "t", on )\r
-#endif\r
-\r
-/* Given the column (c) of the output state variable, the following\r
- macros give the input state variables which are needed in its\r
- computation for each row (r) of the state. All the alternative\r
- macros give the same end values but expand into different ways\r
- of calculating these values. In particular the complex macro\r
- used for dynamically variable block sizes is designed to expand\r
- to a compile time constant whenever possible but will expand to\r
- conditional clauses on some branches (I am grateful to Frank\r
- Yellin for this construction)\r
-*/\r
-\r
-#define inv_var(x,r,c)\\r
- ( r == 0 ? ( c == 0 ? s(x,0) : c == 1 ? s(x,1) : c == 2 ? s(x,2) : s(x,3))\\r
- : r == 1 ? ( c == 0 ? s(x,3) : c == 1 ? s(x,0) : c == 2 ? s(x,1) : s(x,2))\\r
- : r == 2 ? ( c == 0 ? s(x,2) : c == 1 ? s(x,3) : c == 2 ? s(x,0) : s(x,1))\\r
- : ( c == 0 ? s(x,1) : c == 1 ? s(x,2) : c == 2 ? s(x,3) : s(x,0)))\r
-\r
-#if defined(IT4_SET)\r
-#undef dec_imvars\r
-#define inv_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(i,n),inv_var,rf1,c))\r
-#elif defined(IT1_SET)\r
-#undef dec_imvars\r
-#define inv_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ one_table(x,upr,t_use(i,n),inv_var,rf1,c))\r
-#else\r
-#define inv_rnd(y,x,k,c) (s(y,c) = inv_mcol((k)[c] ^ no_table(x,t_use(i,box),inv_var,rf1,c)))\r
-#endif\r
-\r
-#if defined(IL4_SET)\r
-#define inv_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(i,l),inv_var,rf1,c))\r
-#elif defined(IL1_SET)\r
-#define inv_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ one_table(x,ups,t_use(i,l),inv_var,rf1,c))\r
-#else\r
-#define inv_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ no_table(x,t_use(i,box),inv_var,rf1,c))\r
-#endif\r
-\r
-aes_rval aes_decrypt(const void *in_blk, void *out_blk, const aes_decrypt_ctx cx[1])\r
-{ aes_32t locals(b0, b1);\r
-#ifdef dec_imvars\r
- dec_imvars; /* declare variables for inv_mcol() if needed */\r
-#endif\r
-\r
- aes_32t nr = (cx->ks[45] ^ cx->ks[52] ^ cx->ks[53] ? cx->ks[52] : 14);\r
- const aes_32t *kp = cx->ks + nr * N_COLS;\r
-\r
-#ifdef AES_ERR_CHK\r
- if( (nr != 10 || !(cx->ks[0] | cx->ks[3] | cx->ks[4]))\r
- && (nr != 12 || !(cx->ks[0] | cx->ks[5] | cx->ks[6]))\r
- && (nr != 14 || !(cx->ks[0] | cx->ks[7] | cx->ks[8])) )\r
- return aes_error;\r
-#endif\r
-\r
- state_in(b0, in_blk, kp);\r
-\r
-#if (DEC_UNROLL == FULL)\r
-\r
- switch(nr)\r
- {\r
- case 14:\r
- round(inv_rnd, b1, b0, kp - 1 * N_COLS);\r
- round(inv_rnd, b0, b1, kp - 2 * N_COLS);\r
- kp -= 2 * N_COLS;\r
- /* Falls through. */\r
- case 12:\r
- round(inv_rnd, b1, b0, kp - 1 * N_COLS);\r
- round(inv_rnd, b0, b1, kp - 2 * N_COLS);\r
- kp -= 2 * N_COLS;\r
- /* Falls through. */\r
- case 10:\r
- round(inv_rnd, b1, b0, kp - 1 * N_COLS);\r
- round(inv_rnd, b0, b1, kp - 2 * N_COLS);\r
- round(inv_rnd, b1, b0, kp - 3 * N_COLS);\r
- round(inv_rnd, b0, b1, kp - 4 * N_COLS);\r
- round(inv_rnd, b1, b0, kp - 5 * N_COLS);\r
- round(inv_rnd, b0, b1, kp - 6 * N_COLS);\r
- round(inv_rnd, b1, b0, kp - 7 * N_COLS);\r
- round(inv_rnd, b0, b1, kp - 8 * N_COLS);\r
- round(inv_rnd, b1, b0, kp - 9 * N_COLS);\r
- round(inv_lrnd, b0, b1, kp - 10 * N_COLS);\r
- }\r
-\r
-#else\r
-\r
-#if (DEC_UNROLL == PARTIAL)\r
- { aes_32t rnd;\r
- for(rnd = 0; rnd < (nr >> 1) - 1; ++rnd)\r
- {\r
- kp -= N_COLS;\r
- round(inv_rnd, b1, b0, kp);\r
- kp -= N_COLS;\r
- round(inv_rnd, b0, b1, kp);\r
- }\r
- kp -= N_COLS;\r
- round(inv_rnd, b1, b0, kp);\r
-#else\r
- { aes_32t rnd;\r
- for(rnd = 0; rnd < nr - 1; ++rnd)\r
- {\r
- kp -= N_COLS;\r
- round(inv_rnd, b1, b0, kp);\r
- l_copy(b0, b1);\r
- }\r
-#endif\r
- kp -= N_COLS;\r
- round(inv_lrnd, b0, b1, kp);\r
- }\r
-#endif\r
-\r
- state_out(out_blk, b0);\r
-#ifdef AES_ERR_CHK\r
- return aes_good;\r
-#endif\r
-}\r
-\r
-#endif\r
-\r
+++ /dev/null
-/*\r
- ---------------------------------------------------------------------------\r
- Copyright (c) 2003, Dr Brian Gladman < >, Worcester, UK.\r
- All rights reserved.\r
-\r
- LICENSE TERMS\r
-\r
- The free distribution and use of this software in both source and binary\r
- form is allowed (with or without changes) provided that:\r
-\r
- 1. distributions of this source code include the above copyright\r
- notice, this list of conditions and the following disclaimer;\r
-\r
- 2. distributions in binary form include the above copyright\r
- notice, this list of conditions and the following disclaimer\r
- in the documentation and/or other associated materials;\r
-\r
- 3. the copyright holder's name is not used to endorse products\r
- built using this software without specific written permission.\r
-\r
- ALTERNATIVELY, provided that this notice is retained in full, this product\r
- may be distributed under the terms of the GNU General Public License (GPL),\r
- in which case the provisions of the GPL apply INSTEAD OF those given above.\r
-\r
- DISCLAIMER\r
-\r
- This software is provided 'as is' with no explicit or implied warranties\r
- in respect of its properties, including, but not limited to, correctness\r
- and/or fitness for purpose.\r
- ---------------------------------------------------------------------------\r
- Issue Date: 26/08/2003\r
-\r
- This file contains the code for implementing the key schedule for AES\r
- (Rijndael) for block and key sizes of 16, 24, and 32 bytes. See aesopt.h\r
- for further details including optimisation.\r
-*/\r
-\r
-#include "aesopt.h"\r
-\r
-/* Initialise the key schedule from the user supplied key. The key\r
- length can be specified in bytes, with legal values of 16, 24\r
- and 32, or in bits, with legal values of 128, 192 and 256. These\r
- values correspond with Nk values of 4, 6 and 8 respectively.\r
-\r
- The following macros implement a single cycle in the key\r
- schedule generation process. The number of cycles needed\r
- for each cx->n_col and nk value is:\r
-\r
- nk = 4 5 6 7 8\r
- ------------------------------\r
- cx->n_col = 4 10 9 8 7 7\r
- cx->n_col = 5 14 11 10 9 9\r
- cx->n_col = 6 19 15 12 11 11\r
- cx->n_col = 7 21 19 16 13 14\r
- cx->n_col = 8 29 23 19 17 14\r
-*/\r
-\r
-#define ke4(k,i) \\r
-{ k[4*(i)+4] = ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; k[4*(i)+5] = ss[1] ^= ss[0]; \\r
- k[4*(i)+6] = ss[2] ^= ss[1]; k[4*(i)+7] = ss[3] ^= ss[2]; \\r
-}\r
-#define kel4(k,i) \\r
-{ k[4*(i)+4] = ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; k[4*(i)+5] = ss[1] ^= ss[0]; \\r
- k[4*(i)+6] = ss[2] ^= ss[1]; k[4*(i)+7] = ss[3] ^= ss[2]; \\r
-}\r
-\r
-#define ke6(k,i) \\r
-{ k[6*(i)+ 6] = ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; k[6*(i)+ 7] = ss[1] ^= ss[0]; \\r
- k[6*(i)+ 8] = ss[2] ^= ss[1]; k[6*(i)+ 9] = ss[3] ^= ss[2]; \\r
- k[6*(i)+10] = ss[4] ^= ss[3]; k[6*(i)+11] = ss[5] ^= ss[4]; \\r
-}\r
-#define kel6(k,i) \\r
-{ k[6*(i)+ 6] = ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; k[6*(i)+ 7] = ss[1] ^= ss[0]; \\r
- k[6*(i)+ 8] = ss[2] ^= ss[1]; k[6*(i)+ 9] = ss[3] ^= ss[2]; \\r
-}\r
-\r
-#define ke8(k,i) \\r
-{ k[8*(i)+ 8] = ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; k[8*(i)+ 9] = ss[1] ^= ss[0]; \\r
- k[8*(i)+10] = ss[2] ^= ss[1]; k[8*(i)+11] = ss[3] ^= ss[2]; \\r
- k[8*(i)+12] = ss[4] ^= ls_box(ss[3],0); k[8*(i)+13] = ss[5] ^= ss[4]; \\r
- k[8*(i)+14] = ss[6] ^= ss[5]; k[8*(i)+15] = ss[7] ^= ss[6]; \\r
-}\r
-#define kel8(k,i) \\r
-{ k[8*(i)+ 8] = ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; k[8*(i)+ 9] = ss[1] ^= ss[0]; \\r
- k[8*(i)+10] = ss[2] ^= ss[1]; k[8*(i)+11] = ss[3] ^= ss[2]; \\r
-}\r
-\r
-#if defined(ENCRYPTION_KEY_SCHEDULE)\r
-\r
-#if defined(AES_128) || defined(AES_VAR)\r
-\r
-aes_rval aes_encrypt_key128(const void *in_key, aes_encrypt_ctx cx[1])\r
-{ aes_32t ss[4];\r
-\r
- cx->ks[0] = ss[0] = word_in(in_key, 0);\r
- cx->ks[1] = ss[1] = word_in(in_key, 1);\r
- cx->ks[2] = ss[2] = word_in(in_key, 2);\r
- cx->ks[3] = ss[3] = word_in(in_key, 3);\r
-\r
-#if ENC_UNROLL == NONE\r
- { aes_32t i;\r
-\r
- for(i = 0; i < ((11 * N_COLS - 1) / 4); ++i)\r
- ke4(cx->ks, i);\r
- }\r
-#else\r
- ke4(cx->ks, 0); ke4(cx->ks, 1);\r
- ke4(cx->ks, 2); ke4(cx->ks, 3);\r
- ke4(cx->ks, 4); ke4(cx->ks, 5);\r
- ke4(cx->ks, 6); ke4(cx->ks, 7);\r
- ke4(cx->ks, 8); kel4(cx->ks, 9);\r
-#endif\r
-\r
- /* cx->ks[45] ^ cx->ks[52] ^ cx->ks[53] is zero for a 256 bit */\r
- /* key and must be non-zero for 128 and 192 bits keys */\r
- cx->ks[53] = cx->ks[45] = 0;\r
- cx->ks[52] = 10;\r
-#ifdef AES_ERR_CHK\r
- return aes_good;\r
-#endif\r
-}\r
-\r
-#endif\r
-\r
-#if defined(AES_192) || defined(AES_VAR)\r
-\r
-aes_rval aes_encrypt_key192(const void *in_key, aes_encrypt_ctx cx[1])\r
-{ aes_32t ss[6];\r
-\r
- cx->ks[0] = ss[0] = word_in(in_key, 0);\r
- cx->ks[1] = ss[1] = word_in(in_key, 1);\r
- cx->ks[2] = ss[2] = word_in(in_key, 2);\r
- cx->ks[3] = ss[3] = word_in(in_key, 3);\r
- cx->ks[4] = ss[4] = word_in(in_key, 4);\r
- cx->ks[5] = ss[5] = word_in(in_key, 5);\r
-\r
-#if ENC_UNROLL == NONE\r
- { aes_32t i;\r
-\r
- for(i = 0; i < (13 * N_COLS - 1) / 6; ++i)\r
- ke6(cx->ks, i);\r
- }\r
-#else\r
- ke6(cx->ks, 0); ke6(cx->ks, 1);\r
- ke6(cx->ks, 2); ke6(cx->ks, 3);\r
- ke6(cx->ks, 4); ke6(cx->ks, 5);\r
- ke6(cx->ks, 6); kel6(cx->ks, 7);\r
-#endif\r
-\r
- /* cx->ks[45] ^ cx->ks[52] ^ cx->ks[53] is zero for a 256 bit */\r
- /* key and must be non-zero for 128 and 192 bits keys */\r
- cx->ks[53] = cx->ks[45];\r
- cx->ks[52] = 12;\r
-#ifdef AES_ERR_CHK\r
- return aes_good;\r
-#endif\r
-}\r
-\r
-#endif\r
-\r
-#if defined(AES_256) || defined(AES_VAR)\r
-\r
-aes_rval aes_encrypt_key256(const void *in_key, aes_encrypt_ctx cx[1])\r
-{ aes_32t ss[8];\r
-\r
- cx->ks[0] = ss[0] = word_in(in_key, 0);\r
- cx->ks[1] = ss[1] = word_in(in_key, 1);\r
- cx->ks[2] = ss[2] = word_in(in_key, 2);\r
- cx->ks[3] = ss[3] = word_in(in_key, 3);\r
- cx->ks[4] = ss[4] = word_in(in_key, 4);\r
- cx->ks[5] = ss[5] = word_in(in_key, 5);\r
- cx->ks[6] = ss[6] = word_in(in_key, 6);\r
- cx->ks[7] = ss[7] = word_in(in_key, 7);\r
-\r
-#if ENC_UNROLL == NONE\r
- { aes_32t i;\r
-\r
- for(i = 0; i < (15 * N_COLS - 1) / 8; ++i)\r
- ke8(cx->ks, i);\r
- }\r
-#else\r
- ke8(cx->ks, 0); ke8(cx->ks, 1);\r
- ke8(cx->ks, 2); ke8(cx->ks, 3);\r
- ke8(cx->ks, 4); ke8(cx->ks, 5);\r
- kel8(cx->ks, 6);\r
-#endif\r
-#ifdef AES_ERR_CHK\r
- return aes_good;\r
-#endif\r
-}\r
-\r
-#endif\r
-\r
-#if defined(AES_VAR)\r
-\r
-aes_rval aes_encrypt_key(const void *in_key, int key_len, aes_encrypt_ctx cx[1])\r
-{\r
- switch(key_len)\r
- {\r
-#ifdef AES_ERR_CHK\r
- case 16: case 128: return aes_encrypt_key128(in_key, cx);\r
- case 24: case 192: return aes_encrypt_key192(in_key, cx);\r
- case 32: case 256: return aes_encrypt_key256(in_key, cx);\r
- default: return aes_error;\r
-#else\r
- case 16: case 128: aes_encrypt_key128(in_key, cx); return;\r
- case 24: case 192: aes_encrypt_key192(in_key, cx); return;\r
- case 32: case 256: aes_encrypt_key256(in_key, cx); return;\r
-#endif\r
- }\r
-}\r
-\r
-#endif\r
-\r
-#endif\r
-\r
-#if defined(DECRYPTION_KEY_SCHEDULE)\r
-\r
-#if DEC_ROUND == NO_TABLES\r
-#define ff(x) (x)\r
-#else\r
-#define ff(x) inv_mcol(x)\r
-#ifdef dec_imvars\r
-#define d_vars dec_imvars\r
-#endif\r
-#endif\r
-\r
-#if 1\r
-#define kdf4(k,i) \\r
-{ ss[0] = ss[0] ^ ss[2] ^ ss[1] ^ ss[3]; ss[1] = ss[1] ^ ss[3]; ss[2] = ss[2] ^ ss[3]; ss[3] = ss[3]; \\r
- ss[4] = ls_box(ss[(i+3) % 4], 3) ^ t_use(r,c)[i]; ss[i % 4] ^= ss[4]; \\r
- ss[4] ^= k[4*(i)]; k[4*(i)+4] = ff(ss[4]); ss[4] ^= k[4*(i)+1]; k[4*(i)+5] = ff(ss[4]); \\r
- ss[4] ^= k[4*(i)+2]; k[4*(i)+6] = ff(ss[4]); ss[4] ^= k[4*(i)+3]; k[4*(i)+7] = ff(ss[4]); \\r
-}\r
-#define kd4(k,i) \\r
-{ ss[4] = ls_box(ss[(i+3) % 4], 3) ^ t_use(r,c)[i]; ss[i % 4] ^= ss[4]; ss[4] = ff(ss[4]); \\r
- k[4*(i)+4] = ss[4] ^= k[4*(i)]; k[4*(i)+5] = ss[4] ^= k[4*(i)+1]; \\r
- k[4*(i)+6] = ss[4] ^= k[4*(i)+2]; k[4*(i)+7] = ss[4] ^= k[4*(i)+3]; \\r
-}\r
-#define kdl4(k,i) \\r
-{ ss[4] = ls_box(ss[(i+3) % 4], 3) ^ t_use(r,c)[i]; ss[i % 4] ^= ss[4]; \\r
- k[4*(i)+4] = (ss[0] ^= ss[1]) ^ ss[2] ^ ss[3]; k[4*(i)+5] = ss[1] ^ ss[3]; \\r
- k[4*(i)+6] = ss[0]; k[4*(i)+7] = ss[1]; \\r
-}\r
-#else\r
-#define kdf4(k,i) \\r
-{ ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; k[4*(i)+ 4] = ff(ss[0]); ss[1] ^= ss[0]; k[4*(i)+ 5] = ff(ss[1]); \\r
- ss[2] ^= ss[1]; k[4*(i)+ 6] = ff(ss[2]); ss[3] ^= ss[2]; k[4*(i)+ 7] = ff(ss[3]); \\r
-}\r
-#define kd4(k,i) \\r
-{ ss[4] = ls_box(ss[3],3) ^ t_use(r,c)[i]; \\r
- ss[0] ^= ss[4]; ss[4] = ff(ss[4]); k[4*(i)+ 4] = ss[4] ^= k[4*(i)]; \\r
- ss[1] ^= ss[0]; k[4*(i)+ 5] = ss[4] ^= k[4*(i)+ 1]; \\r
- ss[2] ^= ss[1]; k[4*(i)+ 6] = ss[4] ^= k[4*(i)+ 2]; \\r
- ss[3] ^= ss[2]; k[4*(i)+ 7] = ss[4] ^= k[4*(i)+ 3]; \\r
-}\r
-#define kdl4(k,i) \\r
-{ ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; k[4*(i)+ 4] = ss[0]; ss[1] ^= ss[0]; k[4*(i)+ 5] = ss[1]; \\r
- ss[2] ^= ss[1]; k[4*(i)+ 6] = ss[2]; ss[3] ^= ss[2]; k[4*(i)+ 7] = ss[3]; \\r
-}\r
-#endif\r
-\r
-#define kdf6(k,i) \\r
-{ ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; k[6*(i)+ 6] = ff(ss[0]); ss[1] ^= ss[0]; k[6*(i)+ 7] = ff(ss[1]); \\r
- ss[2] ^= ss[1]; k[6*(i)+ 8] = ff(ss[2]); ss[3] ^= ss[2]; k[6*(i)+ 9] = ff(ss[3]); \\r
- ss[4] ^= ss[3]; k[6*(i)+10] = ff(ss[4]); ss[5] ^= ss[4]; k[6*(i)+11] = ff(ss[5]); \\r
-}\r
-#define kd6(k,i) \\r
-{ ss[6] = ls_box(ss[5],3) ^ t_use(r,c)[i]; \\r
- ss[0] ^= ss[6]; ss[6] = ff(ss[6]); k[6*(i)+ 6] = ss[6] ^= k[6*(i)]; \\r
- ss[1] ^= ss[0]; k[6*(i)+ 7] = ss[6] ^= k[6*(i)+ 1]; \\r
- ss[2] ^= ss[1]; k[6*(i)+ 8] = ss[6] ^= k[6*(i)+ 2]; \\r
- ss[3] ^= ss[2]; k[6*(i)+ 9] = ss[6] ^= k[6*(i)+ 3]; \\r
- ss[4] ^= ss[3]; k[6*(i)+10] = ss[6] ^= k[6*(i)+ 4]; \\r
- ss[5] ^= ss[4]; k[6*(i)+11] = ss[6] ^= k[6*(i)+ 5]; \\r
-}\r
-#define kdl6(k,i) \\r
-{ ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; k[6*(i)+ 6] = ss[0]; ss[1] ^= ss[0]; k[6*(i)+ 7] = ss[1]; \\r
- ss[2] ^= ss[1]; k[6*(i)+ 8] = ss[2]; ss[3] ^= ss[2]; k[6*(i)+ 9] = ss[3]; \\r
-}\r
-\r
-#define kdf8(k,i) \\r
-{ ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; k[8*(i)+ 8] = ff(ss[0]); ss[1] ^= ss[0]; k[8*(i)+ 9] = ff(ss[1]); \\r
- ss[2] ^= ss[1]; k[8*(i)+10] = ff(ss[2]); ss[3] ^= ss[2]; k[8*(i)+11] = ff(ss[3]); \\r
- ss[4] ^= ls_box(ss[3],0); k[8*(i)+12] = ff(ss[4]); ss[5] ^= ss[4]; k[8*(i)+13] = ff(ss[5]); \\r
- ss[6] ^= ss[5]; k[8*(i)+14] = ff(ss[6]); ss[7] ^= ss[6]; k[8*(i)+15] = ff(ss[7]); \\r
-}\r
-#define kd8(k,i) \\r
-{ aes_32t g = ls_box(ss[7],3) ^ t_use(r,c)[i]; \\r
- ss[0] ^= g; g = ff(g); k[8*(i)+ 8] = g ^= k[8*(i)]; \\r
- ss[1] ^= ss[0]; k[8*(i)+ 9] = g ^= k[8*(i)+ 1]; \\r
- ss[2] ^= ss[1]; k[8*(i)+10] = g ^= k[8*(i)+ 2]; \\r
- ss[3] ^= ss[2]; k[8*(i)+11] = g ^= k[8*(i)+ 3]; \\r
- g = ls_box(ss[3],0); \\r
- ss[4] ^= g; g = ff(g); k[8*(i)+12] = g ^= k[8*(i)+ 4]; \\r
- ss[5] ^= ss[4]; k[8*(i)+13] = g ^= k[8*(i)+ 5]; \\r
- ss[6] ^= ss[5]; k[8*(i)+14] = g ^= k[8*(i)+ 6]; \\r
- ss[7] ^= ss[6]; k[8*(i)+15] = g ^= k[8*(i)+ 7]; \\r
-}\r
-#define kdl8(k,i) \\r
-{ ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; k[8*(i)+ 8] = ss[0]; ss[1] ^= ss[0]; k[8*(i)+ 9] = ss[1]; \\r
- ss[2] ^= ss[1]; k[8*(i)+10] = ss[2]; ss[3] ^= ss[2]; k[8*(i)+11] = ss[3]; \\r
-}\r
-\r
-#if defined(AES_128) || defined(AES_VAR)\r
-\r
-aes_rval aes_decrypt_key128(const void *in_key, aes_decrypt_ctx cx[1])\r
-{ aes_32t ss[5];\r
-#ifdef d_vars\r
- d_vars;\r
-#endif\r
- cx->ks[0] = ss[0] = word_in(in_key, 0);\r
- cx->ks[1] = ss[1] = word_in(in_key, 1);\r
- cx->ks[2] = ss[2] = word_in(in_key, 2);\r
- cx->ks[3] = ss[3] = word_in(in_key, 3);\r
-\r
-#if DEC_UNROLL == NONE\r
- { aes_32t i;\r
-\r
- for(i = 0; i < (11 * N_COLS - 1) / 4; ++i)\r
- ke4(cx->ks, i);\r
-#if !(DEC_ROUND == NO_TABLES)\r
- for(i = N_COLS; i < 10 * N_COLS; ++i)\r
- cx->ks[i] = inv_mcol(cx->ks[i]);\r
-#endif\r
- }\r
-#else\r
- kdf4(cx->ks, 0); kd4(cx->ks, 1);\r
- kd4(cx->ks, 2); kd4(cx->ks, 3);\r
- kd4(cx->ks, 4); kd4(cx->ks, 5);\r
- kd4(cx->ks, 6); kd4(cx->ks, 7);\r
- kd4(cx->ks, 8); kdl4(cx->ks, 9);\r
-#endif\r
-\r
- /* cx->ks[45] ^ cx->ks[52] ^ cx->ks[53] is zero for a 256 bit */\r
- /* key and must be non-zero for 128 and 192 bits keys */\r
- cx->ks[53] = cx->ks[45] = 0;\r
- cx->ks[52] = 10;\r
-#ifdef AES_ERR_CHK\r
- return aes_good;\r
-#endif\r
-}\r
-\r
-#endif\r
-\r
-#if defined(AES_192) || defined(AES_VAR)\r
-\r
-aes_rval aes_decrypt_key192(const void *in_key, aes_decrypt_ctx cx[1])\r
-{ aes_32t ss[7];\r
-#ifdef d_vars\r
- d_vars;\r
-#endif\r
- cx->ks[0] = ss[0] = word_in(in_key, 0);\r
- cx->ks[1] = ss[1] = word_in(in_key, 1);\r
- cx->ks[2] = ss[2] = word_in(in_key, 2);\r
- cx->ks[3] = ss[3] = word_in(in_key, 3);\r
-\r
-#if DEC_UNROLL == NONE\r
- cx->ks[4] = ss[4] = word_in(in_key, 4);\r
- cx->ks[5] = ss[5] = word_in(in_key, 5);\r
- { aes_32t i;\r
-\r
- for(i = 0; i < (13 * N_COLS - 1) / 6; ++i)\r
- ke6(cx->ks, i);\r
-#if !(DEC_ROUND == NO_TABLES)\r
- for(i = N_COLS; i < 12 * N_COLS; ++i)\r
- cx->ks[i] = inv_mcol(cx->ks[i]);\r
-#endif\r
- }\r
-#else\r
- ss[4] = word_in(in_key, 4);\r
- cx->ks[4] = ff(ss[4]);\r
- ss[5] = word_in(in_key, 5);\r
- cx->ks[5] = ff(ss[5]);\r
- kdf6(cx->ks, 0); kd6(cx->ks, 1);\r
- kd6(cx->ks, 2); kd6(cx->ks, 3);\r
- kd6(cx->ks, 4); kd6(cx->ks, 5);\r
- kd6(cx->ks, 6); kdl6(cx->ks, 7);\r
-#endif\r
-\r
- /* cx->ks[45] ^ cx->ks[52] ^ cx->ks[53] is zero for a 256 bit */\r
- /* key and must be non-zero for 128 and 192 bits keys */\r
- cx->ks[53] = cx->ks[45];\r
- cx->ks[52] = 12;\r
-#ifdef AES_ERR_CHK\r
- return aes_good;\r
-#endif\r
-}\r
-\r
-#endif\r
-\r
-#if defined(AES_256) || defined(AES_VAR)\r
-\r
-aes_rval aes_decrypt_key256(const void *in_key, aes_decrypt_ctx cx[1])\r
-{ aes_32t ss[8];\r
-#ifdef d_vars\r
- d_vars;\r
-#endif\r
- cx->ks[0] = ss[0] = word_in(in_key, 0);\r
- cx->ks[1] = ss[1] = word_in(in_key, 1);\r
- cx->ks[2] = ss[2] = word_in(in_key, 2);\r
- cx->ks[3] = ss[3] = word_in(in_key, 3);\r
-\r
-#if DEC_UNROLL == NONE\r
- cx->ks[4] = ss[4] = word_in(in_key, 4);\r
- cx->ks[5] = ss[5] = word_in(in_key, 5);\r
- cx->ks[6] = ss[6] = word_in(in_key, 6);\r
- cx->ks[7] = ss[7] = word_in(in_key, 7);\r
- { aes_32t i;\r
-\r
- for(i = 0; i < (15 * N_COLS - 1) / 8; ++i)\r
- ke8(cx->ks, i);\r
-#if !(DEC_ROUND == NO_TABLES)\r
- for(i = N_COLS; i < 14 * N_COLS; ++i)\r
- cx->ks[i] = inv_mcol(cx->ks[i]);\r
-#endif\r
- }\r
-#else\r
- ss[4] = word_in(in_key, 4);\r
- cx->ks[4] = ff(ss[4]);\r
- ss[5] = word_in(in_key, 5);\r
- cx->ks[5] = ff(ss[5]);\r
- ss[6] = word_in(in_key, 6);\r
- cx->ks[6] = ff(ss[6]);\r
- ss[7] = word_in(in_key, 7);\r
- cx->ks[7] = ff(ss[7]);\r
- kdf8(cx->ks, 0); kd8(cx->ks, 1);\r
- kd8(cx->ks, 2); kd8(cx->ks, 3);\r
- kd8(cx->ks, 4); kd8(cx->ks, 5);\r
- kdl8(cx->ks, 6);\r
-#endif\r
-#ifdef AES_ERR_CHK\r
- return aes_good;\r
-#endif\r
-}\r
-\r
-#endif\r
-\r
-#if defined(AES_VAR)\r
-\r
-aes_rval aes_decrypt_key(const void *in_key, int key_len, aes_decrypt_ctx cx[1])\r
-{\r
- switch(key_len)\r
- {\r
-#ifdef AES_ERR_CHK\r
- case 16: case 128: return aes_decrypt_key128(in_key, cx);\r
- case 24: case 192: return aes_decrypt_key192(in_key, cx);\r
- case 32: case 256: return aes_decrypt_key256(in_key, cx);\r
- default: return aes_error;\r
-#else\r
- case 16: case 128: aes_decrypt_key128(in_key, cx); return;\r
- case 24: case 192: aes_decrypt_key192(in_key, cx); return;\r
- case 32: case 256: aes_decrypt_key256(in_key, cx); return;\r
-#endif\r
- }\r
-}\r
-\r
-#endif\r
-\r
-#endif\r
-\r
+++ /dev/null
-/*\r
- ---------------------------------------------------------------------------\r
- Copyright (c) 2003, Dr Brian Gladman < >, Worcester, UK.\r
- All rights reserved.\r
-\r
- LICENSE TERMS\r
-\r
- The free distribution and use of this software in both source and binary\r
- form is allowed (with or without changes) provided that:\r
-\r
- 1. distributions of this source code include the above copyright\r
- notice, this list of conditions and the following disclaimer;\r
-\r
- 2. distributions in binary form include the above copyright\r
- notice, this list of conditions and the following disclaimer\r
- in the documentation and/or other associated materials;\r
-\r
- 3. the copyright holder's name is not used to endorse products\r
- built using this software without specific written permission.\r
-\r
- ALTERNATIVELY, provided that this notice is retained in full, this product\r
- may be distributed under the terms of the GNU General Public License (GPL),\r
- in which case the provisions of the GPL apply INSTEAD OF those given above.\r
-\r
- DISCLAIMER\r
-\r
- This software is provided 'as is' with no explicit or implied warranties\r
- in respect of its properties, including, but not limited to, correctness\r
- and/or fitness for purpose.\r
- ---------------------------------------------------------------------------\r
- Issue Date: 26/08/2003\r
-\r
- My thanks go to Dag Arne Osvik for devising the schemes used here for key\r
- length derivation from the form of the key schedule\r
-\r
- This file contains the compilation options for AES (Rijndael) and code\r
- that is common across encryption, key scheduling and table generation.\r
-\r
- OPERATION\r
-\r
- These source code files implement the AES algorithm Rijndael designed by\r
- Joan Daemen and Vincent Rijmen. This version is designed for the standard\r
- block size of 16 bytes and for key sizes of 128, 192 and 256 bits (16, 24\r
- and 32 bytes).\r
-\r
- This version is designed for flexibility and speed using operations on\r
- 32-bit words rather than operations on bytes. It can be compiled with\r
- either big or little endian internal byte order but is faster when the\r
- native byte order for the processor is used.\r
-\r
- THE CIPHER INTERFACE\r
-\r
- The cipher interface is implemented as an array of bytes in which lower\r
- AES bit sequence indexes map to higher numeric significance within bytes.\r
-\r
- aes_08t (an unsigned 8-bit type)\r
- aes_32t (an unsigned 32-bit type)\r
- struct aes_encrypt_ctx (structure for the cipher encryption context)\r
- struct aes_decrypt_ctx (structure for the cipher decryption context)\r
- aes_rval the function return type\r
-\r
- C subroutine calls:\r
-\r
- aes_rval aes_encrypt_key128(const void *in_key, aes_encrypt_ctx cx[1]);\r
- aes_rval aes_encrypt_key192(const void *in_key, aes_encrypt_ctx cx[1]);\r
- aes_rval aes_encrypt_key256(const void *in_key, aes_encrypt_ctx cx[1]);\r
- aes_rval aes_encrypt(const void *in_blk,\r
- void *out_blk, const aes_encrypt_ctx cx[1]);\r
-\r
- aes_rval aes_decrypt_key128(const void *in_key, aes_decrypt_ctx cx[1]);\r
- aes_rval aes_decrypt_key192(const void *in_key, aes_decrypt_ctx cx[1]);\r
- aes_rval aes_decrypt_key256(const void *in_key, aes_decrypt_ctx cx[1]);\r
- aes_rval aes_decrypt(const void *in_blk,\r
- void *out_blk, const aes_decrypt_ctx cx[1]);\r
-\r
- IMPORTANT NOTE: If you are using this C interface with dynamic tables make sure that\r
- you call genTabs() before AES is used so that the tables are initialised.\r
-\r
- C++ aes class subroutines:\r
-\r
- Class AESencrypt for encryption\r
-\r
- Construtors:\r
- AESencrypt(void)\r
- AESencrypt(const void *in_key) - 128 bit key\r
- Members:\r
- void key128(const void *in_key)\r
- void key192(const void *in_key)\r
- void key256(const void *in_key)\r
- void encrypt(const void *in_blk, void *out_blk) const\r
-\r
- Class AESdecrypt for encryption\r
- Construtors:\r
- AESdecrypt(void)\r
- AESdecrypt(const void *in_key) - 128 bit key\r
- Members:\r
- void key128(const void *in_key)\r
- void key192(const void *in_key)\r
- void key256(const void *in_key)\r
- void decrypt(const void *in_blk, void *out_blk) const\r
-\r
- COMPILATION\r
-\r
- The files used to provide AES (Rijndael) are\r
-\r
- a. aes.h for the definitions needed for use in C.\r
- b. aescpp.h for the definitions needed for use in C++.\r
- c. aesopt.h for setting compilation options (also includes common code).\r
- d. aescrypt.c for encryption and decrytpion, or\r
- e. aeskey.c for key scheduling.\r
- f. aestab.c for table loading or generation.\r
- g. aescrypt.asm for encryption and decryption using assembler code.\r
- h. aescrypt.mmx.asm for encryption and decryption using MMX assembler.\r
-\r
- To compile AES (Rijndael) for use in C code use aes.h and set the\r
- defines here for the facilities you need (key lengths, encryption\r
- and/or decryption). Do not define AES_DLL or AES_CPP. Set the options\r
- for optimisations and table sizes here.\r
-\r
- To compile AES (Rijndael) for use in in C++ code use aescpp.h but do\r
- not define AES_DLL\r
-\r
- To compile AES (Rijndael) in C as a Dynamic Link Library DLL) use\r
- aes.h and include the AES_DLL define.\r
-\r
- CONFIGURATION OPTIONS (here and in aes.h)\r
-\r
- a. set AES_DLL in aes.h if AES (Rijndael) is to be compiled as a DLL\r
- b. You may need to set PLATFORM_BYTE_ORDER to define the byte order.\r
- c. If you want the code to run in a specific internal byte order, then\r
- ALGORITHM_BYTE_ORDER must be set accordingly.\r
- d. set other configuration options decribed below.\r
-*/\r
-\r
-#ifndef _AESOPT_H\r
-#define _AESOPT_H\r
-\r
-#include "aes.h"\r
-\r
-/* CONFIGURATION - USE OF DEFINES\r
-\r
- Later in this section there are a number of defines that control the\r
- operation of the code. In each section, the purpose of each define is\r
- explained so that the relevant form can be included or excluded by\r
- setting either 1's or 0's respectively on the branches of the related\r
- #if clauses.\r
-*/\r
-\r
-/* BYTE ORDER IN 32-BIT WORDS\r
-\r
- To obtain the highest speed on processors with 32-bit words, this code\r
- needs to determine the byte order of the target machine. The following \r
- block of code is an attempt to capture the most obvious ways in which \r
- various environemnts define byte order. It may well fail, in which case \r
- the definitions will need to be set by editing at the points marked \r
- **** EDIT HERE IF NECESSARY **** below. My thanks to Peter Gutmann for \r
- some of these defines (from cryptlib).\r
-*/\r
-\r
-#define BRG_LITTLE_ENDIAN 1234 /* byte 0 is least significant (i386) */\r
-#define BRG_BIG_ENDIAN 4321 /* byte 0 is most significant (mc68k) */\r
-\r
-#ifdef __BIG_ENDIAN__\r
-#define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN\r
-#else\r
-#define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN\r
-#endif\r
-\r
-/* SOME LOCAL DEFINITIONS */\r
-\r
-#define NO_TABLES 0\r
-#define ONE_TABLE 1\r
-#define FOUR_TABLES 4\r
-#define NONE 0\r
-#define PARTIAL 1\r
-#define FULL 2\r
-\r
-#define aes_sw32 Byteswap::byteswap\r
-\r
-/* 1. FUNCTIONS REQUIRED\r
-\r
- This implementation provides subroutines for encryption, decryption\r
- and for setting the three key lengths (separately) for encryption\r
- and decryption. When the assembler code is not being used the following\r
- definition blocks allow the selection of the routines that are to be\r
- included in the compilation.\r
-*/\r
-#ifdef AES_ENCRYPT\r
-#define ENCRYPTION\r
-#define ENCRYPTION_KEY_SCHEDULE\r
-#endif\r
-\r
-#ifdef AES_DECRYPT\r
-#define DECRYPTION\r
-#define DECRYPTION_KEY_SCHEDULE\r
-#endif\r
-\r
-/* 2. ASSEMBLER SUPPORT\r
-\r
- This define (which can be on the command line) enables the use of the\r
- assembler code routines for encryption and decryption with the C code\r
- only providing key scheduling\r
-*/\r
-#if 0\r
-#define AES_ASM\r
-#endif\r
-\r
-/* 3. BYTE ORDER WITHIN 32 BIT WORDS\r
-\r
- The fundamental data processing units in Rijndael are 8-bit bytes. The\r
- input, output and key input are all enumerated arrays of bytes in which\r
- bytes are numbered starting at zero and increasing to one less than the\r
- number of bytes in the array in question. This enumeration is only used\r
- for naming bytes and does not imply any adjacency or order relationship\r
- from one byte to another. When these inputs and outputs are considered\r
- as bit sequences, bits 8*n to 8*n+7 of the bit sequence are mapped to\r
- byte[n] with bit 8n+i in the sequence mapped to bit 7-i within the byte.\r
- In this implementation bits are numbered from 0 to 7 starting at the\r
- numerically least significant end of each byte (bit n represents 2^n).\r
-\r
- However, Rijndael can be implemented more efficiently using 32-bit\r
- words by packing bytes into words so that bytes 4*n to 4*n+3 are placed\r
- into word[n]. While in principle these bytes can be assembled into words\r
- in any positions, this implementation only supports the two formats in\r
- which bytes in adjacent positions within words also have adjacent byte\r
- numbers. This order is called big-endian if the lowest numbered bytes\r
- in words have the highest numeric significance and little-endian if the\r
- opposite applies.\r
-\r
- This code can work in either order irrespective of the order used by the\r
- machine on which it runs. Normally the internal byte order will be set\r
- to the order of the processor on which the code is to be run but this\r
- define can be used to reverse this in special situations\r
-\r
- NOTE: Assembler code versions rely on PLATFORM_BYTE_ORDER being set\r
-*/\r
-#if 1 || defined(AES_ASM)\r
-#define ALGORITHM_BYTE_ORDER PLATFORM_BYTE_ORDER\r
-#elif 0\r
-#define ALGORITHM_BYTE_ORDER BRG_LITTLE_ENDIAN\r
-#elif 0\r
-#define ALGORITHM_BYTE_ORDER BRG_BIG_ENDIAN\r
-#else\r
-#error The algorithm byte order is not defined\r
-#endif\r
-\r
-/* 4. FAST INPUT/OUTPUT OPERATIONS.\r
-\r
- On some machines it is possible to improve speed by transferring the\r
- bytes in the input and output arrays to and from the internal 32-bit\r
- variables by addressing these arrays as if they are arrays of 32-bit\r
- words. On some machines this will always be possible but there may\r
- be a large performance penalty if the byte arrays are not aligned on\r
- the normal word boundaries. On other machines this technique will\r
- lead to memory access errors when such 32-bit word accesses are not\r
- properly aligned. The option SAFE_IO avoids such problems but will\r
- often be slower on those machines that support misaligned access\r
- (especially so if care is taken to align the input and output byte\r
- arrays on 32-bit word boundaries). If SAFE_IO is not defined it is\r
- assumed that access to byte arrays as if they are arrays of 32-bit\r
- words will not cause problems when such accesses are misaligned.\r
-*/\r
-#if 1 && !defined(_MSC_VER)\r
-#define SAFE_IO\r
-#endif\r
-\r
-/* 5. LOOP UNROLLING\r
-\r
- The code for encryption and decrytpion cycles through a number of rounds\r
- that can be implemented either in a loop or by expanding the code into a\r
- long sequence of instructions, the latter producing a larger program but\r
- one that will often be much faster. The latter is called loop unrolling.\r
- There are also potential speed advantages in expanding two iterations in\r
- a loop with half the number of iterations, which is called partial loop\r
- unrolling. The following options allow partial or full loop unrolling\r
- to be set independently for encryption and decryption\r
-*/\r
-#if 1\r
-#define ENC_UNROLL FULL\r
-#elif 0\r
-#define ENC_UNROLL PARTIAL\r
-#else\r
-#define ENC_UNROLL NONE\r
-#endif\r
-\r
-#if 1\r
-#define DEC_UNROLL FULL\r
-#elif 0\r
-#define DEC_UNROLL PARTIAL\r
-#else\r
-#define DEC_UNROLL NONE\r
-#endif\r
-\r
-/* 6. FAST FINITE FIELD OPERATIONS\r
-\r
- If this section is included, tables are used to provide faster finite\r
- field arithmetic (this has no effect if FIXED_TABLES is defined).\r
-*/\r
-#if 0\r
-#define FF_TABLES\r
-#endif\r
-\r
-/* 7. INTERNAL STATE VARIABLE FORMAT\r
-\r
- The internal state of Rijndael is stored in a number of local 32-bit\r
- word varaibles which can be defined either as an array or as individual\r
- names variables. Include this section if you want to store these local\r
- varaibles in arrays. Otherwise individual local variables will be used.\r
-*/\r
-#if 1\r
-#define ARRAYS\r
-#endif\r
-\r
-/* In this implementation the columns of the state array are each held in\r
- 32-bit words. The state array can be held in various ways: in an array\r
- of words, in a number of individual word variables or in a number of\r
- processor registers. The following define maps a variable name x and\r
- a column number c to the way the state array variable is to be held.\r
- The first define below maps the state into an array x[c] whereas the\r
- second form maps the state into a number of individual variables x0,\r
- x1, etc. Another form could map individual state colums to machine\r
- register names.\r
-*/\r
-\r
-#if defined(ARRAYS)\r
-#define s(x,c) x[c]\r
-#else\r
-#define s(x,c) x##c\r
-#endif\r
-\r
-/* 8. FIXED OR DYNAMIC TABLES\r
-\r
- When this section is included the tables used by the code are compiled\r
- statically into the binary file. Otherwise the subroutine gen_tabs()\r
- must be called to compute them before the code is first used.\r
-*/\r
-#if 1\r
-#define FIXED_TABLES\r
-#define DO_TABLES\r
-#endif\r
-\r
-/* 9. TABLE ALIGNMENT\r
-\r
- On some systems speed will be improved by aligning the AES large lookup\r
- tables on particular boundaries. This define should be set to a power of\r
- two giving the desired alignment. It can be left undefined if alignment \r
- is not needed. This option is specific to the Microsft VC++ compiler -\r
- it seems to sometimes cause trouble for the VC++ version 6 compiler.\r
-*/\r
-\r
-#if 0 && defined(_MSC_VER) && (_MSC_VER >= 1300)\r
-#define TABLE_ALIGN 64\r
-#endif\r
-\r
-/* 10. INTERNAL TABLE CONFIGURATION\r
-\r
- This cipher proceeds by repeating in a number of cycles known as 'rounds'\r
- which are implemented by a round function which can optionally be speeded\r
- up using tables. The basic tables are each 256 32-bit words, with either\r
- one or four tables being required for each round function depending on\r
- how much speed is required. The encryption and decryption round functions\r
- are different and the last encryption and decrytpion round functions are\r
- different again making four different round functions in all.\r
-\r
- This means that:\r
- 1. Normal encryption and decryption rounds can each use either 0, 1\r
- or 4 tables and table spaces of 0, 1024 or 4096 bytes each.\r
- 2. The last encryption and decryption rounds can also use either 0, 1\r
- or 4 tables and table spaces of 0, 1024 or 4096 bytes each.\r
-\r
- Include or exclude the appropriate definitions below to set the number\r
- of tables used by this implementation.\r
-*/\r
-\r
-#if 1 /* set tables for the normal encryption round */\r
-#define ENC_ROUND FOUR_TABLES\r
-#elif 0\r
-#define ENC_ROUND ONE_TABLE\r
-#else\r
-#define ENC_ROUND NO_TABLES\r
-#endif\r
-\r
-#if 1 /* set tables for the last encryption round */\r
-#define LAST_ENC_ROUND FOUR_TABLES\r
-#elif 0\r
-#define LAST_ENC_ROUND ONE_TABLE\r
-#else\r
-#define LAST_ENC_ROUND NO_TABLES\r
-#endif\r
-\r
-#if 1 /* set tables for the normal decryption round */\r
-#define DEC_ROUND FOUR_TABLES\r
-#elif 0\r
-#define DEC_ROUND ONE_TABLE\r
-#else\r
-#define DEC_ROUND NO_TABLES\r
-#endif\r
-\r
-#if 1 /* set tables for the last decryption round */\r
-#define LAST_DEC_ROUND FOUR_TABLES\r
-#elif 0\r
-#define LAST_DEC_ROUND ONE_TABLE\r
-#else\r
-#define LAST_DEC_ROUND NO_TABLES\r
-#endif\r
-\r
-/* The decryption key schedule can be speeded up with tables in the same\r
- way that the round functions can. Include or exclude the following\r
- defines to set this requirement.\r
-*/\r
-#if 1\r
-#define KEY_SCHED FOUR_TABLES\r
-#elif 0\r
-#define KEY_SCHED ONE_TABLE\r
-#else\r
-#define KEY_SCHED NO_TABLES\r
-#endif\r
-\r
-/* END OF CONFIGURATION OPTIONS */\r
-\r
-#define RC_LENGTH (5 * (AES_BLOCK_SIZE / 4 - 2))\r
-\r
-/* Disable or report errors on some combinations of options */\r
-\r
-#if ENC_ROUND == NO_TABLES && LAST_ENC_ROUND != NO_TABLES\r
-#undef LAST_ENC_ROUND\r
-#define LAST_ENC_ROUND NO_TABLES\r
-#elif ENC_ROUND == ONE_TABLE && LAST_ENC_ROUND == FOUR_TABLES\r
-#undef LAST_ENC_ROUND\r
-#define LAST_ENC_ROUND ONE_TABLE\r
-#endif\r
-\r
-#if ENC_ROUND == NO_TABLES && ENC_UNROLL != NONE\r
-#undef ENC_UNROLL\r
-#define ENC_UNROLL NONE\r
-#endif\r
-\r
-#if DEC_ROUND == NO_TABLES && LAST_DEC_ROUND != NO_TABLES\r
-#undef LAST_DEC_ROUND\r
-#define LAST_DEC_ROUND NO_TABLES\r
-#elif DEC_ROUND == ONE_TABLE && LAST_DEC_ROUND == FOUR_TABLES\r
-#undef LAST_DEC_ROUND\r
-#define LAST_DEC_ROUND ONE_TABLE\r
-#endif\r
-\r
-#if DEC_ROUND == NO_TABLES && DEC_UNROLL != NONE\r
-#undef DEC_UNROLL\r
-#define DEC_UNROLL NONE\r
-#endif\r
-\r
-/* upr(x,n): rotates bytes within words by n positions, moving bytes to\r
- higher index positions with wrap around into low positions\r
- ups(x,n): moves bytes by n positions to higher index positions in\r
- words but without wrap around\r
- bval(x,n): extracts a byte from a word\r
-\r
- NOTE: The definitions given here are intended only for use with\r
- unsigned variables and with shift counts that are compile\r
- time constants\r
-*/\r
-\r
-#if (ALGORITHM_BYTE_ORDER == BRG_LITTLE_ENDIAN)\r
-#define upr(x,n) (((aes_32t)(x) << (8 * (n))) | ((aes_32t)(x) >> (32 - 8 * (n))))\r
-#define ups(x,n) ((aes_32t) (x) << (8 * (n)))\r
-#define bval(x,n) ((aes_08t)((x) >> (8 * (n))))\r
-#define bytes2word(b0, b1, b2, b3) \\r
- (((aes_32t)(b3) << 24) | ((aes_32t)(b2) << 16) | ((aes_32t)(b1) << 8) | (b0))\r
-#endif\r
-\r
-#if (ALGORITHM_BYTE_ORDER == BRG_BIG_ENDIAN)\r
-#define upr(x,n) (((aes_32t)(x) >> (8 * (n))) | ((aes_32t)(x) << (32 - 8 * (n))))\r
-#define ups(x,n) ((aes_32t) (x) >> (8 * (n))))\r
-#define bval(x,n) ((aes_08t)((x) >> (24 - 8 * (n))))\r
-#define bytes2word(b0, b1, b2, b3) \\r
- (((aes_32t)(b0) << 24) | ((aes_32t)(b1) << 16) | ((aes_32t)(b2) << 8) | (b3))\r
-#endif\r
-\r
-#if defined(SAFE_IO)\r
-\r
-#define word_in(x,c) bytes2word(((aes_08t*)(x)+4*c)[0], ((aes_08t*)(x)+4*c)[1], \\r
- ((aes_08t*)(x)+4*c)[2], ((aes_08t*)(x)+4*c)[3])\r
-#define word_out(x,c,v) { ((aes_08t*)(x)+4*c)[0] = bval(v,0); ((aes_08t*)(x)+4*c)[1] = bval(v,1); \\r
- ((aes_08t*)(x)+4*c)[2] = bval(v,2); ((aes_08t*)(x)+4*c)[3] = bval(v,3); }\r
-\r
-#elif (ALGORITHM_BYTE_ORDER == PLATFORM_BYTE_ORDER)\r
-\r
-#define word_in(x,c) (*((aes_32t*)(x)+(c)))\r
-#define word_out(x,c,v) (*((aes_32t*)(x)+(c)) = (v))\r
-\r
-#else\r
-\r
-#define word_in(x,c) aes_sw32(*((aes_32t*)(x)+(c)))\r
-#define word_out(x,c,v) (*((aes_32t*)(x)+(c)) = aes_sw32(v))\r
-\r
-#endif\r
-\r
-/* the finite field modular polynomial and elements */\r
-\r
-#define WPOLY 0x011b\r
-#define BPOLY 0x1b\r
-\r
-/* multiply four bytes in GF(2^8) by 'x' {02} in parallel */\r
-\r
-#define m1 0x80808080\r
-#define m2 0x7f7f7f7f\r
-#define gf_mulx(x) ((((x) & m2) << 1) ^ ((((x) & m1) >> 7) * BPOLY))\r
-\r
-/* The following defines provide alternative definitions of gf_mulx that might\r
- give improved performance if a fast 32-bit multiply is not available. Note\r
- that a temporary variable u needs to be defined where gf_mulx is used.\r
-\r
-#define gf_mulx(x) (u = (x) & m1, u |= (u >> 1), ((x) & m2) << 1) ^ ((u >> 3) | (u >> 6))\r
-#define m4 (0x01010101 * BPOLY)\r
-#define gf_mulx(x) (u = (x) & m1, ((x) & m2) << 1) ^ ((u - (u >> 7)) & m4)\r
-*/\r
-\r
-/* Work out which tables are needed for the different options */\r
-\r
-#ifdef AES_ASM\r
-#ifdef ENC_ROUND\r
-#undef ENC_ROUND\r
-#endif\r
-#define ENC_ROUND FOUR_TABLES\r
-#ifdef LAST_ENC_ROUND\r
-#undef LAST_ENC_ROUND\r
-#endif\r
-#define LAST_ENC_ROUND FOUR_TABLES\r
-#ifdef DEC_ROUND\r
-#undef DEC_ROUND\r
-#endif\r
-#define DEC_ROUND FOUR_TABLES\r
-#ifdef LAST_DEC_ROUND\r
-#undef LAST_DEC_ROUND\r
-#endif\r
-#define LAST_DEC_ROUND FOUR_TABLES\r
-#ifdef KEY_SCHED\r
-#undef KEY_SCHED\r
-#define KEY_SCHED FOUR_TABLES\r
-#endif\r
-#endif\r
-\r
-#if defined(ENCRYPTION) || defined(AES_ASM)\r
-#if ENC_ROUND == ONE_TABLE\r
-#define FT1_SET\r
-#elif ENC_ROUND == FOUR_TABLES\r
-#define FT4_SET\r
-#else\r
-#define SBX_SET\r
-#endif\r
-#if LAST_ENC_ROUND == ONE_TABLE\r
-#define FL1_SET\r
-#elif LAST_ENC_ROUND == FOUR_TABLES\r
-#define FL4_SET\r
-#elif !defined(SBX_SET)\r
-#define SBX_SET\r
-#endif\r
-#endif\r
-\r
-#if defined(DECRYPTION) || defined(AES_ASM)\r
-#if DEC_ROUND == ONE_TABLE\r
-#define IT1_SET\r
-#elif DEC_ROUND == FOUR_TABLES\r
-#define IT4_SET\r
-#else\r
-#define ISB_SET\r
-#endif\r
-#if LAST_DEC_ROUND == ONE_TABLE\r
-#define IL1_SET\r
-#elif LAST_DEC_ROUND == FOUR_TABLES\r
-#define IL4_SET\r
-#elif !defined(ISB_SET)\r
-#define ISB_SET\r
-#endif\r
-#endif\r
-\r
-#if defined(ENCRYPTION_KEY_SCHEDULE) || defined(DECRYPTION_KEY_SCHEDULE)\r
-#if KEY_SCHED == ONE_TABLE\r
-#define LS1_SET\r
-#define IM1_SET\r
-#elif KEY_SCHED == FOUR_TABLES\r
-#define LS4_SET\r
-#define IM4_SET\r
-#elif !defined(SBX_SET)\r
-#define SBX_SET\r
-#endif\r
-#endif\r
-\r
-/* generic definitions of Rijndael macros that use tables */\r
-\r
-#define no_table(x,box,vf,rf,c) bytes2word( \\r
- box[bval(vf(x,0,c),rf(0,c))], \\r
- box[bval(vf(x,1,c),rf(1,c))], \\r
- box[bval(vf(x,2,c),rf(2,c))], \\r
- box[bval(vf(x,3,c),rf(3,c))])\r
-\r
-#define one_table(x,op,tab,vf,rf,c) \\r
- ( tab[bval(vf(x,0,c),rf(0,c))] \\r
- ^ op(tab[bval(vf(x,1,c),rf(1,c))],1) \\r
- ^ op(tab[bval(vf(x,2,c),rf(2,c))],2) \\r
- ^ op(tab[bval(vf(x,3,c),rf(3,c))],3))\r
-\r
-#define four_tables(x,tab,vf,rf,c) \\r
- ( tab[0][bval(vf(x,0,c),rf(0,c))] \\r
- ^ tab[1][bval(vf(x,1,c),rf(1,c))] \\r
- ^ tab[2][bval(vf(x,2,c),rf(2,c))] \\r
- ^ tab[3][bval(vf(x,3,c),rf(3,c))])\r
-\r
-#define vf1(x,r,c) (x)\r
-#define rf1(r,c) (r)\r
-#define rf2(r,c) ((8+r-c)&3)\r
-\r
-/* perform forward and inverse column mix operation on four bytes in long word x in */\r
-/* parallel. NOTE: x must be a simple variable, NOT an expression in these macros. */\r
-\r
-#if defined(FM4_SET) /* not currently used */\r
-#define fwd_mcol(x) four_tables(x,t_use(f,m),vf1,rf1,0)\r
-#elif defined(FM1_SET) /* not currently used */\r
-#define fwd_mcol(x) one_table(x,upr,t_use(f,m),vf1,rf1,0)\r
-#else\r
-#define dec_fmvars aes_32t g2\r
-#define fwd_mcol(x) (g2 = gf_mulx(x), g2 ^ upr((x) ^ g2, 3) ^ upr((x), 2) ^ upr((x), 1))\r
-#endif\r
-\r
-#if defined(IM4_SET)\r
-#define inv_mcol(x) four_tables(x,t_use(i,m),vf1,rf1,0)\r
-#elif defined(IM1_SET)\r
-#define inv_mcol(x) one_table(x,upr,t_use(i,m),vf1,rf1,0)\r
-#else\r
-#define dec_imvars aes_32t g2, g4, g9\r
-#define inv_mcol(x) (g2 = gf_mulx(x), g4 = gf_mulx(g2), g9 = (x) ^ gf_mulx(g4), g4 ^= g9, \\r
- (x) ^ g2 ^ g4 ^ upr(g2 ^ g9, 3) ^ upr(g4, 2) ^ upr(g9, 1))\r
-#endif\r
-\r
-#if defined(FL4_SET)\r
-#define ls_box(x,c) four_tables(x,t_use(f,l),vf1,rf2,c)\r
-#elif defined(LS4_SET)\r
-#define ls_box(x,c) four_tables(x,t_use(l,s),vf1,rf2,c)\r
-#elif defined(FL1_SET)\r
-#define ls_box(x,c) one_table(x,upr,t_use(f,l),vf1,rf2,c)\r
-#elif defined(LS1_SET)\r
-#define ls_box(x,c) one_table(x,upr,t_use(l,s),vf1,rf2,c)\r
-#else\r
-#define ls_box(x,c) no_table(x,t_use(s,box),vf1,rf2,c)\r
-#endif\r
-\r
-/* If there are no global variables, the definitions here can be\r
- used to put the AES tables in a structure so that a pointer \r
- can then be added to the AES context to pass them to the AES\r
- routines that need them. If this facility is used, the calling \r
- program has to ensure that this pointer is managed appropriately. \r
- In particular, the value of the t_dec(in,it) item in the table \r
- structure must be set to zero in order to ensure that the tables \r
- are initialised. In practice the three code sequences in aeskey.c \r
- that control the calls to gen_tabs() and the gen_tabs() routine \r
- itself will have to be changed for a specific implementation. If \r
- global variables are available it will generally be preferable to \r
- use them with the precomputed FIXED_TABLES option that uses static \r
- global tables.\r
-\r
- The following defines can be used to control the way the tables\r
- are defined, initialised and used in embedded environments that\r
- require special features for these purposes\r
-\r
- the 't_dec' construction is used to declare fixed table arrays\r
- the 't_set' construction is used to set fixed table values\r
- the 't_use' construction is used to access fixed table values\r
-\r
- 256 byte tables:\r
-\r
- t_xxx(s,box) => forward S box\r
- t_xxx(i,box) => inverse S box\r
-\r
- 256 32-bit word OR 4 x 256 32-bit word tables:\r
-\r
- t_xxx(f,n) => forward normal round\r
- t_xxx(f,l) => forward last round\r
- t_xxx(i,n) => inverse normal round\r
- t_xxx(i,l) => inverse last round\r
- t_xxx(l,s) => key schedule table\r
- t_xxx(i,m) => key schedule table\r
-\r
- Other variables and tables:\r
-\r
- t_xxx(r,c) => the rcon table\r
-*/\r
-\r
-#define t_dec(m,n) t_##m##n\r
-#define t_set(m,n) t_##m##n\r
-#define t_use(m,n) t_##m##n\r
-\r
-#if defined(DO_TABLES) /* declare and instantiate tables */\r
-\r
-/* finite field arithmetic operations for table generation */\r
-\r
-#if defined(FIXED_TABLES) || !defined(FF_TABLES)\r
-\r
-#define f2(x) ((x<<1) ^ (((x>>7) & 1) * WPOLY))\r
-#define f4(x) ((x<<2) ^ (((x>>6) & 1) * WPOLY) ^ (((x>>6) & 2) * WPOLY))\r
-#define f8(x) ((x<<3) ^ (((x>>5) & 1) * WPOLY) ^ (((x>>5) & 2) * WPOLY) \\r
- ^ (((x>>5) & 4) * WPOLY))\r
-#define f3(x) (f2(x) ^ x)\r
-#define f9(x) (f8(x) ^ x)\r
-#define fb(x) (f8(x) ^ f2(x) ^ x)\r
-#define fd(x) (f8(x) ^ f4(x) ^ x)\r
-#define fe(x) (f8(x) ^ f4(x) ^ f2(x))\r
-\r
-#else\r
-\r
-#define f2(x) ((x) ? pow[log[x] + 0x19] : 0)\r
-#define f3(x) ((x) ? pow[log[x] + 0x01] : 0)\r
-#define f9(x) ((x) ? pow[log[x] + 0xc7] : 0)\r
-#define fb(x) ((x) ? pow[log[x] + 0x68] : 0)\r
-#define fd(x) ((x) ? pow[log[x] + 0xee] : 0)\r
-#define fe(x) ((x) ? pow[log[x] + 0xdf] : 0)\r
-#define fi(x) ((x) ? pow[ 255 - log[x]] : 0)\r
-\r
-#endif\r
-\r
-#if defined(FIXED_TABLES) /* declare and set values for static tables */\r
-\r
-#define sb_data(w) \\r
- w(0x63), w(0x7c), w(0x77), w(0x7b), w(0xf2), w(0x6b), w(0x6f), w(0xc5),\\r
- w(0x30), w(0x01), w(0x67), w(0x2b), w(0xfe), w(0xd7), w(0xab), w(0x76),\\r
- w(0xca), w(0x82), w(0xc9), w(0x7d), w(0xfa), w(0x59), w(0x47), w(0xf0),\\r
- w(0xad), w(0xd4), w(0xa2), w(0xaf), w(0x9c), w(0xa4), w(0x72), w(0xc0),\\r
- w(0xb7), w(0xfd), w(0x93), w(0x26), w(0x36), w(0x3f), w(0xf7), w(0xcc),\\r
- w(0x34), w(0xa5), w(0xe5), w(0xf1), w(0x71), w(0xd8), w(0x31), w(0x15),\\r
- w(0x04), w(0xc7), w(0x23), w(0xc3), w(0x18), w(0x96), w(0x05), w(0x9a),\\r
- w(0x07), w(0x12), w(0x80), w(0xe2), w(0xeb), w(0x27), w(0xb2), w(0x75),\\r
- w(0x09), w(0x83), w(0x2c), w(0x1a), w(0x1b), w(0x6e), w(0x5a), w(0xa0),\\r
- w(0x52), w(0x3b), w(0xd6), w(0xb3), w(0x29), w(0xe3), w(0x2f), w(0x84),\\r
- w(0x53), w(0xd1), w(0x00), w(0xed), w(0x20), w(0xfc), w(0xb1), w(0x5b),\\r
- w(0x6a), w(0xcb), w(0xbe), w(0x39), w(0x4a), w(0x4c), w(0x58), w(0xcf),\\r
- w(0xd0), w(0xef), w(0xaa), w(0xfb), w(0x43), w(0x4d), w(0x33), w(0x85),\\r
- w(0x45), w(0xf9), w(0x02), w(0x7f), w(0x50), w(0x3c), w(0x9f), w(0xa8),\\r
- w(0x51), w(0xa3), w(0x40), w(0x8f), w(0x92), w(0x9d), w(0x38), w(0xf5),\\r
- w(0xbc), w(0xb6), w(0xda), w(0x21), w(0x10), w(0xff), w(0xf3), w(0xd2),\\r
- w(0xcd), w(0x0c), w(0x13), w(0xec), w(0x5f), w(0x97), w(0x44), w(0x17),\\r
- w(0xc4), w(0xa7), w(0x7e), w(0x3d), w(0x64), w(0x5d), w(0x19), w(0x73),\\r
- w(0x60), w(0x81), w(0x4f), w(0xdc), w(0x22), w(0x2a), w(0x90), w(0x88),\\r
- w(0x46), w(0xee), w(0xb8), w(0x14), w(0xde), w(0x5e), w(0x0b), w(0xdb),\\r
- w(0xe0), w(0x32), w(0x3a), w(0x0a), w(0x49), w(0x06), w(0x24), w(0x5c),\\r
- w(0xc2), w(0xd3), w(0xac), w(0x62), w(0x91), w(0x95), w(0xe4), w(0x79),\\r
- w(0xe7), w(0xc8), w(0x37), w(0x6d), w(0x8d), w(0xd5), w(0x4e), w(0xa9),\\r
- w(0x6c), w(0x56), w(0xf4), w(0xea), w(0x65), w(0x7a), w(0xae), w(0x08),\\r
- w(0xba), w(0x78), w(0x25), w(0x2e), w(0x1c), w(0xa6), w(0xb4), w(0xc6),\\r
- w(0xe8), w(0xdd), w(0x74), w(0x1f), w(0x4b), w(0xbd), w(0x8b), w(0x8a),\\r
- w(0x70), w(0x3e), w(0xb5), w(0x66), w(0x48), w(0x03), w(0xf6), w(0x0e),\\r
- w(0x61), w(0x35), w(0x57), w(0xb9), w(0x86), w(0xc1), w(0x1d), w(0x9e),\\r
- w(0xe1), w(0xf8), w(0x98), w(0x11), w(0x69), w(0xd9), w(0x8e), w(0x94),\\r
- w(0x9b), w(0x1e), w(0x87), w(0xe9), w(0xce), w(0x55), w(0x28), w(0xdf),\\r
- w(0x8c), w(0xa1), w(0x89), w(0x0d), w(0xbf), w(0xe6), w(0x42), w(0x68),\\r
- w(0x41), w(0x99), w(0x2d), w(0x0f), w(0xb0), w(0x54), w(0xbb), w(0x16)\r
-\r
-#define isb_data(w) \\r
- w(0x52), w(0x09), w(0x6a), w(0xd5), w(0x30), w(0x36), w(0xa5), w(0x38),\\r
- w(0xbf), w(0x40), w(0xa3), w(0x9e), w(0x81), w(0xf3), w(0xd7), w(0xfb),\\r
- w(0x7c), w(0xe3), w(0x39), w(0x82), w(0x9b), w(0x2f), w(0xff), w(0x87),\\r
- w(0x34), w(0x8e), w(0x43), w(0x44), w(0xc4), w(0xde), w(0xe9), w(0xcb),\\r
- w(0x54), w(0x7b), w(0x94), w(0x32), w(0xa6), w(0xc2), w(0x23), w(0x3d),\\r
- w(0xee), w(0x4c), w(0x95), w(0x0b), w(0x42), w(0xfa), w(0xc3), w(0x4e),\\r
- w(0x08), w(0x2e), w(0xa1), w(0x66), w(0x28), w(0xd9), w(0x24), w(0xb2),\\r
- w(0x76), w(0x5b), w(0xa2), w(0x49), w(0x6d), w(0x8b), w(0xd1), w(0x25),\\r
- w(0x72), w(0xf8), w(0xf6), w(0x64), w(0x86), w(0x68), w(0x98), w(0x16),\\r
- w(0xd4), w(0xa4), w(0x5c), w(0xcc), w(0x5d), w(0x65), w(0xb6), w(0x92),\\r
- w(0x6c), w(0x70), w(0x48), w(0x50), w(0xfd), w(0xed), w(0xb9), w(0xda),\\r
- w(0x5e), w(0x15), w(0x46), w(0x57), w(0xa7), w(0x8d), w(0x9d), w(0x84),\\r
- w(0x90), w(0xd8), w(0xab), w(0x00), w(0x8c), w(0xbc), w(0xd3), w(0x0a),\\r
- w(0xf7), w(0xe4), w(0x58), w(0x05), w(0xb8), w(0xb3), w(0x45), w(0x06),\\r
- w(0xd0), w(0x2c), w(0x1e), w(0x8f), w(0xca), w(0x3f), w(0x0f), w(0x02),\\r
- w(0xc1), w(0xaf), w(0xbd), w(0x03), w(0x01), w(0x13), w(0x8a), w(0x6b),\\r
- w(0x3a), w(0x91), w(0x11), w(0x41), w(0x4f), w(0x67), w(0xdc), w(0xea),\\r
- w(0x97), w(0xf2), w(0xcf), w(0xce), w(0xf0), w(0xb4), w(0xe6), w(0x73),\\r
- w(0x96), w(0xac), w(0x74), w(0x22), w(0xe7), w(0xad), w(0x35), w(0x85),\\r
- w(0xe2), w(0xf9), w(0x37), w(0xe8), w(0x1c), w(0x75), w(0xdf), w(0x6e),\\r
- w(0x47), w(0xf1), w(0x1a), w(0x71), w(0x1d), w(0x29), w(0xc5), w(0x89),\\r
- w(0x6f), w(0xb7), w(0x62), w(0x0e), w(0xaa), w(0x18), w(0xbe), w(0x1b),\\r
- w(0xfc), w(0x56), w(0x3e), w(0x4b), w(0xc6), w(0xd2), w(0x79), w(0x20),\\r
- w(0x9a), w(0xdb), w(0xc0), w(0xfe), w(0x78), w(0xcd), w(0x5a), w(0xf4),\\r
- w(0x1f), w(0xdd), w(0xa8), w(0x33), w(0x88), w(0x07), w(0xc7), w(0x31),\\r
- w(0xb1), w(0x12), w(0x10), w(0x59), w(0x27), w(0x80), w(0xec), w(0x5f),\\r
- w(0x60), w(0x51), w(0x7f), w(0xa9), w(0x19), w(0xb5), w(0x4a), w(0x0d),\\r
- w(0x2d), w(0xe5), w(0x7a), w(0x9f), w(0x93), w(0xc9), w(0x9c), w(0xef),\\r
- w(0xa0), w(0xe0), w(0x3b), w(0x4d), w(0xae), w(0x2a), w(0xf5), w(0xb0),\\r
- w(0xc8), w(0xeb), w(0xbb), w(0x3c), w(0x83), w(0x53), w(0x99), w(0x61),\\r
- w(0x17), w(0x2b), w(0x04), w(0x7e), w(0xba), w(0x77), w(0xd6), w(0x26),\\r
- w(0xe1), w(0x69), w(0x14), w(0x63), w(0x55), w(0x21), w(0x0c), w(0x7d),\r
-\r
-#define mm_data(w) \\r
- w(0x00), w(0x01), w(0x02), w(0x03), w(0x04), w(0x05), w(0x06), w(0x07),\\r
- w(0x08), w(0x09), w(0x0a), w(0x0b), w(0x0c), w(0x0d), w(0x0e), w(0x0f),\\r
- w(0x10), w(0x11), w(0x12), w(0x13), w(0x14), w(0x15), w(0x16), w(0x17),\\r
- w(0x18), w(0x19), w(0x1a), w(0x1b), w(0x1c), w(0x1d), w(0x1e), w(0x1f),\\r
- w(0x20), w(0x21), w(0x22), w(0x23), w(0x24), w(0x25), w(0x26), w(0x27),\\r
- w(0x28), w(0x29), w(0x2a), w(0x2b), w(0x2c), w(0x2d), w(0x2e), w(0x2f),\\r
- w(0x30), w(0x31), w(0x32), w(0x33), w(0x34), w(0x35), w(0x36), w(0x37),\\r
- w(0x38), w(0x39), w(0x3a), w(0x3b), w(0x3c), w(0x3d), w(0x3e), w(0x3f),\\r
- w(0x40), w(0x41), w(0x42), w(0x43), w(0x44), w(0x45), w(0x46), w(0x47),\\r
- w(0x48), w(0x49), w(0x4a), w(0x4b), w(0x4c), w(0x4d), w(0x4e), w(0x4f),\\r
- w(0x50), w(0x51), w(0x52), w(0x53), w(0x54), w(0x55), w(0x56), w(0x57),\\r
- w(0x58), w(0x59), w(0x5a), w(0x5b), w(0x5c), w(0x5d), w(0x5e), w(0x5f),\\r
- w(0x60), w(0x61), w(0x62), w(0x63), w(0x64), w(0x65), w(0x66), w(0x67),\\r
- w(0x68), w(0x69), w(0x6a), w(0x6b), w(0x6c), w(0x6d), w(0x6e), w(0x6f),\\r
- w(0x70), w(0x71), w(0x72), w(0x73), w(0x74), w(0x75), w(0x76), w(0x77),\\r
- w(0x78), w(0x79), w(0x7a), w(0x7b), w(0x7c), w(0x7d), w(0x7e), w(0x7f),\\r
- w(0x80), w(0x81), w(0x82), w(0x83), w(0x84), w(0x85), w(0x86), w(0x87),\\r
- w(0x88), w(0x89), w(0x8a), w(0x8b), w(0x8c), w(0x8d), w(0x8e), w(0x8f),\\r
- w(0x90), w(0x91), w(0x92), w(0x93), w(0x94), w(0x95), w(0x96), w(0x97),\\r
- w(0x98), w(0x99), w(0x9a), w(0x9b), w(0x9c), w(0x9d), w(0x9e), w(0x9f),\\r
- w(0xa0), w(0xa1), w(0xa2), w(0xa3), w(0xa4), w(0xa5), w(0xa6), w(0xa7),\\r
- w(0xa8), w(0xa9), w(0xaa), w(0xab), w(0xac), w(0xad), w(0xae), w(0xaf),\\r
- w(0xb0), w(0xb1), w(0xb2), w(0xb3), w(0xb4), w(0xb5), w(0xb6), w(0xb7),\\r
- w(0xb8), w(0xb9), w(0xba), w(0xbb), w(0xbc), w(0xbd), w(0xbe), w(0xbf),\\r
- w(0xc0), w(0xc1), w(0xc2), w(0xc3), w(0xc4), w(0xc5), w(0xc6), w(0xc7),\\r
- w(0xc8), w(0xc9), w(0xca), w(0xcb), w(0xcc), w(0xcd), w(0xce), w(0xcf),\\r
- w(0xd0), w(0xd1), w(0xd2), w(0xd3), w(0xd4), w(0xd5), w(0xd6), w(0xd7),\\r
- w(0xd8), w(0xd9), w(0xda), w(0xdb), w(0xdc), w(0xdd), w(0xde), w(0xdf),\\r
- w(0xe0), w(0xe1), w(0xe2), w(0xe3), w(0xe4), w(0xe5), w(0xe6), w(0xe7),\\r
- w(0xe8), w(0xe9), w(0xea), w(0xeb), w(0xec), w(0xed), w(0xee), w(0xef),\\r
- w(0xf0), w(0xf1), w(0xf2), w(0xf3), w(0xf4), w(0xf5), w(0xf6), w(0xf7),\\r
- w(0xf8), w(0xf9), w(0xfa), w(0xfb), w(0xfc), w(0xfd), w(0xfe), w(0xff)\r
-\r
-#define h0(x) (x)\r
-\r
-/* These defines are used to ensure tables are generated in the\r
- right format depending on the internal byte order required\r
-*/\r
-\r
-#define w0(p) bytes2word(p, 0, 0, 0)\r
-#define w1(p) bytes2word(0, p, 0, 0)\r
-#define w2(p) bytes2word(0, 0, p, 0)\r
-#define w3(p) bytes2word(0, 0, 0, p)\r
-\r
-#define u0(p) bytes2word(f2(p), p, p, f3(p))\r
-#define u1(p) bytes2word(f3(p), f2(p), p, p)\r
-#define u2(p) bytes2word(p, f3(p), f2(p), p)\r
-#define u3(p) bytes2word(p, p, f3(p), f2(p))\r
-\r
-#define v0(p) bytes2word(fe(p), f9(p), fd(p), fb(p))\r
-#define v1(p) bytes2word(fb(p), fe(p), f9(p), fd(p))\r
-#define v2(p) bytes2word(fd(p), fb(p), fe(p), f9(p))\r
-#define v3(p) bytes2word(f9(p), fd(p), fb(p), fe(p))\r
-\r
-const aes_32t t_dec(r,c)[RC_LENGTH] =\r
-{\r
- w0(0x01), w0(0x02), w0(0x04), w0(0x08), w0(0x10),\r
- w0(0x20), w0(0x40), w0(0x80), w0(0x1b), w0(0x36)\r
-};\r
-\r
-#if defined(__BORLANDC__)\r
- #define concat(s1, s2) s1##s2\r
- #define d_1(t,n,b,v) const t n[256] = { b(concat(v,0)) }\r
- #define d_4(t,n,b,v) const t n[4][256] = { { b(concat(v,0)) }, { b(concat(v,1)) }, { b(concat(v,2)) }, { b(concat(v,3)) } }\r
-#else\r
- #define d_1(t,n,b,v) const t n[256] = { b(v##0) }\r
- #define d_4(t,n,b,v) const t n[4][256] = { { b(v##0) }, { b(v##1) }, { b(v##2) }, { b(v##3) } }\r
-#endif\r
-\r
-#else /* declare and instantiate tables for dynamic value generation in in tab.c */\r
-\r
-aes_32t t_dec(r,c)[RC_LENGTH];\r
-\r
-#define d_1(t,n,b,v) t n[256]\r
-#define d_4(t,n,b,v) t n[4][256]\r
-\r
-#endif\r
-\r
-#else /* declare tables without instantiation */\r
-\r
-#if defined(FIXED_TABLES)\r
-\r
-extern const aes_32t t_dec(r,c)[RC_LENGTH];\r
-\r
-#if defined(_MSC_VER) && defined(TABLE_ALIGN)\r
-#define d_1(t,n,b,v) extern __declspec(align(TABLE_ALIGN)) const t n[256]\r
-#define d_4(t,n,b,v) extern __declspec(align(TABLE_ALIGN)) const t n[4][256]\r
-#else\r
-#define d_1(t,n,b,v) extern const t n[256]\r
-#define d_4(t,n,b,v) extern const t n[4][256]\r
-#endif\r
-#else\r
-\r
-extern aes_32t t_dec(r,c)[RC_LENGTH];\r
-\r
-#if defined(_MSC_VER) && defined(TABLE_ALIGN)\r
-#define d_1(t,n,b,v) extern __declspec(align(TABLE_ALIGN)) t n[256]\r
-#define d_4(t,n,b,v) extern __declspec(align(TABLE_ALIGN)) t n[4][256]\r
-#else\r
-#define d_1(t,n,b,v) extern t n[256]\r
-#define d_4(t,n,b,v) extern t n[4][256]\r
-#endif\r
-#endif\r
-\r
-#endif\r
-\r
-#ifdef SBX_SET\r
- d_1(aes_08t, t_dec(s,box), sb_data, h);\r
-#endif\r
-#ifdef ISB_SET\r
- d_1(aes_08t, t_dec(i,box), isb_data, h);\r
-#endif\r
-\r
-#ifdef FT1_SET\r
- d_1(aes_32t, t_dec(f,n), sb_data, u);\r
-#endif\r
-#ifdef FT4_SET\r
- d_4(aes_32t, t_dec(f,n), sb_data, u);\r
-#endif\r
-\r
-#ifdef FL1_SET\r
- d_1(aes_32t, t_dec(f,l), sb_data, w);\r
-#endif\r
-#ifdef FL4_SET\r
- d_4(aes_32t, t_dec(f,l), sb_data, w);\r
-#endif\r
-\r
-#ifdef IT1_SET\r
- d_1(aes_32t, t_dec(i,n), isb_data, v);\r
-#endif\r
-#ifdef IT4_SET\r
- d_4(aes_32t, t_dec(i,n), isb_data, v);\r
-#endif\r
-\r
-#ifdef IL1_SET\r
- d_1(aes_32t, t_dec(i,l), isb_data, w);\r
-#endif\r
-#ifdef IL4_SET\r
- d_4(aes_32t, t_dec(i,l), isb_data, w);\r
-#endif\r
-\r
-#ifdef LS1_SET\r
-#ifdef FL1_SET\r
-#undef LS1_SET\r
-#else\r
- d_1(aes_32t, t_dec(l,s), sb_data, w);\r
-#endif\r
-#endif\r
-\r
-#ifdef LS4_SET\r
-#ifdef FL4_SET\r
-#undef LS4_SET\r
-#else\r
- d_4(aes_32t, t_dec(l,s), sb_data, w);\r
-#endif\r
-#endif\r
-\r
-#ifdef IM1_SET\r
- d_1(aes_32t, t_dec(i,m), mm_data, v);\r
-#endif\r
-#ifdef IM4_SET\r
- d_4(aes_32t, t_dec(i,m), mm_data, v);\r
-#endif\r
-\r
-#endif\r
-\r
+++ /dev/null
-/*\r
- ---------------------------------------------------------------------------\r
- Copyright (c) 2003, Dr Brian Gladman < >, Worcester, UK.\r
- All rights reserved.\r
-\r
- LICENSE TERMS\r
-\r
- The free distribution and use of this software in both source and binary\r
- form is allowed (with or without changes) provided that:\r
-\r
- 1. distributions of this source code include the above copyright\r
- notice, this list of conditions and the following disclaimer;\r
-\r
- 2. distributions in binary form include the above copyright\r
- notice, this list of conditions and the following disclaimer\r
- in the documentation and/or other associated materials;\r
-\r
- 3. the copyright holder's name is not used to endorse products\r
- built using this software without specific written permission.\r
-\r
- ALTERNATIVELY, provided that this notice is retained in full, this product\r
- may be distributed under the terms of the GNU General Public License (GPL),\r
- in which case the provisions of the GPL apply INSTEAD OF those given above.\r
-\r
- DISCLAIMER\r
-\r
- This software is provided 'as is' with no explicit or implied warranties\r
- in respect of its properties, including, but not limited to, correctness\r
- and/or fitness for purpose.\r
- ---------------------------------------------------------------------------\r
- Issue Date: 26/08/2003\r
-\r
-*/\r
-\r
-#define DO_TABLES\r
-\r
-#include "aesopt.h"\r
-\r
-#if defined(FIXED_TABLES)\r
-\r
-/* implemented in case of wrong call for fixed tables */\r
-\r
-void gen_tabs(void)\r
-{\r
-}\r
-\r
-#else /* dynamic table generation */\r
-\r
-#if !defined(FF_TABLES)\r
-\r
-/* Generate the tables for the dynamic table option\r
-\r
- It will generally be sensible to use tables to compute finite\r
- field multiplies and inverses but where memory is scarse this\r
- code might sometimes be better. But it only has effect during\r
- initialisation so its pretty unimportant in overall terms.\r
-*/\r
-\r
-/* return 2 ^ (n - 1) where n is the bit number of the highest bit\r
- set in x with x in the range 1 < x < 0x00000200. This form is\r
- used so that locals within fi can be bytes rather than words\r
-*/\r
-\r
-static aes_08t hibit(const aes_32t x)\r
-{ aes_08t r = (aes_08t)((x >> 1) | (x >> 2));\r
-\r
- r |= (r >> 2);\r
- r |= (r >> 4);\r
- return (r + 1) >> 1;\r
-}\r
-\r
-/* return the inverse of the finite field element x */\r
-\r
-static aes_08t fi(const aes_08t x)\r
-{ aes_08t p1 = x, p2 = BPOLY, n1 = hibit(x), n2 = 0x80, v1 = 1, v2 = 0;\r
-\r
- if(x < 2) return x;\r
-\r
- for(;;)\r
- {\r
- if(!n1) return v1;\r
-\r
- while(n2 >= n1)\r
- {\r
- n2 /= n1; p2 ^= p1 * n2; v2 ^= v1 * n2; n2 = hibit(p2);\r
- }\r
-\r
- if(!n2) return v2;\r
-\r
- while(n1 >= n2)\r
- {\r
- n1 /= n2; p1 ^= p2 * n1; v1 ^= v2 * n1; n1 = hibit(p1);\r
- }\r
- }\r
-}\r
-\r
-#endif\r
-\r
-/* The forward and inverse affine transformations used in the S-box */\r
-\r
-#define fwd_affine(x) \\r
- (w = (aes_32t)x, w ^= (w<<1)^(w<<2)^(w<<3)^(w<<4), 0x63^(aes_08t)(w^(w>>8)))\r
-\r
-#define inv_affine(x) \\r
- (w = (aes_32t)x, w = (w<<1)^(w<<3)^(w<<6), 0x05^(aes_08t)(w^(w>>8)))\r
-\r
-static int init = 0;\r
-\r
-void gen_tabs(void)\r
-{ aes_32t i, w;\r
-\r
-#if defined(FF_TABLES)\r
-\r
- aes_08t pow[512], log[256];\r
-\r
- if(init) return;\r
- /* log and power tables for GF(2^8) finite field with\r
- WPOLY as modular polynomial - the simplest primitive\r
- root is 0x03, used here to generate the tables\r
- */\r
-\r
- i = 0; w = 1;\r
- do\r
- {\r
- pow[i] = (aes_08t)w;\r
- pow[i + 255] = (aes_08t)w;\r
- log[w] = (aes_08t)i++;\r
- w ^= (w << 1) ^ (w & 0x80 ? WPOLY : 0);\r
- }\r
- while (w != 1);\r
-\r
-#else\r
- if(init) return;\r
-#endif\r
-\r
- for(i = 0, w = 1; i < RC_LENGTH; ++i)\r
- {\r
- t_set(r,c)[i] = bytes2word(w, 0, 0, 0);\r
- w = f2(w);\r
- }\r
-\r
- for(i = 0; i < 256; ++i)\r
- { aes_08t b;\r
-\r
- b = fwd_affine(fi((aes_08t)i));\r
- w = bytes2word(f2(b), b, b, f3(b));\r
-\r
-#ifdef SBX_SET\r
- t_set(s,box)[i] = b;\r
-#endif\r
-\r
-#ifdef FT1_SET /* tables for a normal encryption round */\r
- t_set(f,n)[i] = w;\r
-#endif\r
-#ifdef FT4_SET\r
- t_set(f,n)[0][i] = w;\r
- t_set(f,n)[1][i] = upr(w,1);\r
- t_set(f,n)[2][i] = upr(w,2);\r
- t_set(f,n)[3][i] = upr(w,3);\r
-#endif\r
- w = bytes2word(b, 0, 0, 0);\r
-\r
-#ifdef FL1_SET /* tables for last encryption round (may also */\r
- t_set(f,l)[i] = w; /* be used in the key schedule) */\r
-#endif\r
-#ifdef FL4_SET\r
- t_set(f,l)[0][i] = w;\r
- t_set(f,l)[1][i] = upr(w,1);\r
- t_set(f,l)[2][i] = upr(w,2);\r
- t_set(f,l)[3][i] = upr(w,3);\r
-#endif\r
-\r
-#ifdef LS1_SET /* table for key schedule if t_set(f,l) above is */\r
- t_set(l,s)[i] = w; /* not of the required form */\r
-#endif\r
-#ifdef LS4_SET\r
- t_set(l,s)[0][i] = w;\r
- t_set(l,s)[1][i] = upr(w,1);\r
- t_set(l,s)[2][i] = upr(w,2);\r
- t_set(l,s)[3][i] = upr(w,3);\r
-#endif\r
-\r
- b = fi(inv_affine((aes_08t)i));\r
- w = bytes2word(fe(b), f9(b), fd(b), fb(b));\r
-\r
-#ifdef IM1_SET /* tables for the inverse mix column operation */\r
- t_set(i,m)[b] = w;\r
-#endif\r
-#ifdef IM4_SET\r
- t_set(i,m)[0][b] = w;\r
- t_set(i,m)[1][b] = upr(w,1);\r
- t_set(i,m)[2][b] = upr(w,2);\r
- t_set(i,m)[3][b] = upr(w,3);\r
-#endif\r
-\r
-#ifdef ISB_SET\r
- t_set(i,box)[i] = b;\r
-#endif\r
-#ifdef IT1_SET /* tables for a normal decryption round */\r
- t_set(i,n)[i] = w;\r
-#endif\r
-#ifdef IT4_SET\r
- t_set(i,n)[0][i] = w;\r
- t_set(i,n)[1][i] = upr(w,1);\r
- t_set(i,n)[2][i] = upr(w,2);\r
- t_set(i,n)[3][i] = upr(w,3);\r
-#endif\r
- w = bytes2word(b, 0, 0, 0);\r
-#ifdef IL1_SET /* tables for last decryption round */\r
- t_set(i,l)[i] = w;\r
-#endif\r
-#ifdef IL4_SET\r
- t_set(i,l)[0][i] = w;\r
- t_set(i,l)[1][i] = upr(w,1);\r
- t_set(i,l)[2][i] = upr(w,2);\r
- t_set(i,l)[3][i] = upr(w,3);\r
-#endif\r
- }\r
- init = 1;\r
-}\r
-\r
-#endif\r
-\r
+++ /dev/null
-/*\r
- ---------------------------------------------------------------------------\r
- Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK.\r
- All rights reserved.\r
-\r
- LICENSE TERMS\r
-\r
- The free distribution and use of this software in both source and binary\r
- form is allowed (with or without changes) provided that:\r
-\r
- 1. distributions of this source code include the above copyright\r
- notice, this list of conditions and the following disclaimer;\r
-\r
- 2. distributions in binary form include the above copyright\r
- notice, this list of conditions and the following disclaimer\r
- in the documentation and/or other associated materials;\r
-\r
- 3. the copyright holder's name is not used to endorse products\r
- built using this software without specific written permission.\r
-\r
- ALTERNATIVELY, provided that this notice is retained in full, this product\r
- may be distributed under the terms of the GNU General Public License (GPL),\r
- in which case the provisions of the GPL apply INSTEAD OF those given above.\r
-\r
- DISCLAIMER\r
-\r
- This software is provided 'as is' with no explicit or implied warranties\r
- in respect of its properties, including, but not limited to, correctness\r
- and/or fitness for purpose.\r
- -------------------------------------------------------------------------\r
- Issue Date: 26/08/2003\r
-\r
- This file implements password based file encryption and authentication \r
- using AES in CTR mode, HMAC-SHA1 authentication and RFC2898 password \r
- based key derivation.\r
-\r
-*/\r
-\r
-#include <memory.h>\r
-\r
-#include "fileenc.h"\r
-\r
-/* subroutine for data encryption/decryption */\r
-/* this could be speeded up a lot by aligning */\r
-/* buffers and using 32 bit operations */\r
-\r
-static void encr_data(unsigned char data[], unsigned long d_len, fcrypt_ctx cx[1])\r
-{\r
- unsigned long i = 0, pos = cx->encr_pos;\r
-\r
- while(i < d_len)\r
- {\r
- if(pos == BLOCK_SIZE)\r
- { unsigned int j = 0;\r
- /* increment encryption nonce */\r
- while(j < 8 && !++cx->nonce[j])\r
- ++j;\r
- /* encrypt the nonce to form next xor buffer */\r
- aes_encrypt(cx->nonce, cx->encr_bfr, cx->encr_ctx);\r
- pos = 0;\r
- }\r
-\r
- data[i++] ^= cx->encr_bfr[pos++];\r
- }\r
-\r
- cx->encr_pos = pos;\r
-}\r
-\r
-int fcrypt_init(\r
- int mode, /* the mode to be used (input) */\r
- const unsigned char pwd[], /* the user specified password (input) */\r
- unsigned int pwd_len, /* the length of the password (input) */\r
- const unsigned char salt[], /* the salt (input) */\r
-#ifdef PASSWORD_VERIFIER\r
- unsigned char pwd_ver[PWD_VER_LENGTH], /* 2 byte password verifier (output) */\r
-#endif\r
- fcrypt_ctx cx[1]) /* the file encryption context (output) */\r
-{\r
- unsigned char kbuf[2 * MAX_KEY_LENGTH + PWD_VER_LENGTH];\r
-\r
- if(pwd_len > MAX_PWD_LENGTH)\r
- return PASSWORD_TOO_LONG;\r
-\r
- if(mode < 1 || mode > 3)\r
- return BAD_MODE;\r
-\r
- cx->mode = mode;\r
- cx->pwd_len = pwd_len;\r
- /* initialise the encryption nonce and buffer pos */\r
- cx->encr_pos = BLOCK_SIZE;\r
-\r
- /* if we need a random component in the encryption */\r
- /* nonce, this is where it would have to be set */\r
- memset(cx->nonce, 0, BLOCK_SIZE * sizeof(unsigned char));\r
- /* initialise for authentication */\r
- hmac_sha_begin(cx->auth_ctx);\r
-\r
- /* derive the encryption and authetication keys and the password verifier */\r
- derive_key(pwd, pwd_len, salt, SALT_LENGTH(mode), KEYING_ITERATIONS,\r
- kbuf, 2 * KEY_LENGTH(mode) + PWD_VER_LENGTH);\r
- /* set the encryption key */\r
- aes_encrypt_key(kbuf, KEY_LENGTH(mode), cx->encr_ctx);\r
- /* set the authentication key */\r
- hmac_sha_key(kbuf + KEY_LENGTH(mode), KEY_LENGTH(mode), cx->auth_ctx);\r
-#ifdef PASSWORD_VERIFIER\r
- memcpy(pwd_ver, kbuf + 2 * KEY_LENGTH(mode), PWD_VER_LENGTH);\r
-#endif\r
- /* clear the buffer holding the derived key values */\r
- memset(kbuf, 0, 2 * KEY_LENGTH(mode) + PWD_VER_LENGTH);\r
-\r
- return GOOD_RETURN;\r
-}\r
-\r
-/* perform 'in place' encryption and authentication */\r
-\r
-void fcrypt_encrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1])\r
-{\r
- encr_data(data, data_len, cx);\r
- hmac_sha_data(data, data_len, cx->auth_ctx);\r
-}\r
-\r
-/* perform 'in place' authentication and decryption */\r
-\r
-void fcrypt_decrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1])\r
-{\r
- hmac_sha_data(data, data_len, cx->auth_ctx);\r
- encr_data(data, data_len, cx);\r
-}\r
-\r
-/* close encryption/decryption and return the MAC value */\r
-\r
-int fcrypt_end(unsigned char mac[], fcrypt_ctx cx[1])\r
-{\r
- hmac_sha_end(mac, MAC_LENGTH(cx->mode), cx->auth_ctx);\r
- memset(cx, 0, sizeof(fcrypt_ctx)); /* clear the encryption context */\r
- return MAC_LENGTH(cx->mode); /* return MAC length in bytes */\r
-}\r
-\r
+++ /dev/null
-/*\r
- ---------------------------------------------------------------------------\r
- Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK.\r
- All rights reserved.\r
-\r
- LICENSE TERMS\r
-\r
- The free distribution and use of this software in both source and binary\r
- form is allowed (with or without changes) provided that:\r
-\r
- 1. distributions of this source code include the above copyright\r
- notice, this list of conditions and the following disclaimer;\r
-\r
- 2. distributions in binary form include the above copyright\r
- notice, this list of conditions and the following disclaimer\r
- in the documentation and/or other associated materials;\r
-\r
- 3. the copyright holder's name is not used to endorse products\r
- built using this software without specific written permission.\r
-\r
- ALTERNATIVELY, provided that this notice is retained in full, this product\r
- may be distributed under the terms of the GNU General Public License (GPL),\r
- in which case the provisions of the GPL apply INSTEAD OF those given above.\r
-\r
- DISCLAIMER\r
-\r
- This software is provided 'as is' with no explicit or implied warranties\r
- in respect of its properties, including, but not limited to, correctness\r
- and/or fitness for purpose.\r
- ---------------------------------------------------------------------------\r
- Issue Date: 24/01/2003\r
-\r
- This file contains the header file for fileenc.c, which implements password\r
- based file encryption and authentication using AES in CTR mode, HMAC-SHA1 \r
- authentication and RFC2898 password based key derivation.\r
-*/\r
-\r
-#ifndef _FENC_H\r
-#define _FENC_H\r
-\r
-#include "aes.h"\r
-#include "hmac.h"\r
-#include "pwd2key.h"\r
-\r
-#define BLOCK_SIZE AES_BLOCK_SIZE\r
-#define PASSWORD_VERIFIER\r
-\r
-#define MAX_KEY_LENGTH 32\r
-#define MAX_PWD_LENGTH 128\r
-#define MAX_SALT_LENGTH 16\r
-#define KEYING_ITERATIONS 1000\r
-\r
-#ifdef PASSWORD_VERIFIER\r
-#define PWD_VER_LENGTH 2\r
-#else\r
-#define PWD_VER_LENGTH 0\r
-#endif\r
-\r
-#define GOOD_RETURN 0\r
-#define PASSWORD_TOO_LONG -100\r
-#define BAD_MODE -101\r
-\r
-/*\r
- Field lengths (in bytes) versus File Encryption Mode (0 < mode < 4)\r
-\r
- Mode Key Salt MAC Overhead\r
- 1 16 8 10 18\r
- 2 24 12 10 22\r
- 3 32 16 10 26\r
-\r
- The following macros assume that the mode value is correct.\r
-*/\r
-\r
-#define KEY_LENGTH(mode) (8 * (mode & 3) + 8)\r
-#define SALT_LENGTH(mode) (4 * (mode & 3) + 4)\r
-#define MAC_LENGTH(mode) (10)\r
-\r
-/* the context for file encryption */\r
-\r
-typedef struct\r
-{ unsigned char nonce[BLOCK_SIZE]; /* the CTR nonce */\r
- unsigned char encr_bfr[BLOCK_SIZE]; /* encrypt buffer */\r
- aes_encrypt_ctx encr_ctx[1]; /* encryption context */\r
- hmac_ctx auth_ctx[1]; /* authentication context */\r
- unsigned int encr_pos; /* block position (enc) */\r
- unsigned int pwd_len; /* password length */\r
- unsigned int mode; /* File encryption mode */\r
-} fcrypt_ctx;\r
-\r
-/* initialise file encryption or decryption */\r
-\r
-int fcrypt_init(\r
- int mode, /* the mode to be used (input) */\r
- const unsigned char pwd[], /* the user specified password (input) */\r
- unsigned int pwd_len, /* the length of the password (input) */\r
- const unsigned char salt[], /* the salt (input) */\r
-#ifdef PASSWORD_VERIFIER\r
- unsigned char pwd_ver[PWD_VER_LENGTH], /* 2 byte password verifier (output) */\r
-#endif\r
- fcrypt_ctx cx[1]); /* the file encryption context (output) */\r
-\r
-/* perform 'in place' encryption or decryption and authentication */\r
-\r
-void fcrypt_encrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1]);\r
-void fcrypt_decrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1]);\r
-\r
-/* close encryption/decryption and return the MAC value */\r
-/* the return value is the length of the MAC */\r
-\r
-int fcrypt_end(unsigned char mac[], /* the MAC value (output) */\r
- fcrypt_ctx cx[1]); /* the context (input) */\r
-\r
-#endif\r
-\r
+++ /dev/null
-/*\r
- ---------------------------------------------------------------------------\r
- Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK.\r
- All rights reserved.\r
-\r
- LICENSE TERMS\r
-\r
- The free distribution and use of this software in both source and binary\r
- form is allowed (with or without changes) provided that:\r
-\r
- 1. distributions of this source code include the above copyright\r
- notice, this list of conditions and the following disclaimer;\r
-\r
- 2. distributions in binary form include the above copyright\r
- notice, this list of conditions and the following disclaimer\r
- in the documentation and/or other associated materials;\r
-\r
- 3. the copyright holder's name is not used to endorse products\r
- built using this software without specific written permission.\r
-\r
- ALTERNATIVELY, provided that this notice is retained in full, this product\r
- may be distributed under the terms of the GNU General Public License (GPL),\r
- in which case the provisions of the GPL apply INSTEAD OF those given above.\r
-\r
- DISCLAIMER\r
-\r
- This software is provided 'as is' with no explicit or implied warranties\r
- in respect of its properties, including, but not limited to, correctness\r
- and/or fitness for purpose.\r
- ---------------------------------------------------------------------------\r
- Issue Date: 26/08/2003\r
- Includes a bugfix from Dr Brian Gladman made on 16/04/2012 for compiling on 64-bit\r
-\r
- This is an implementation of HMAC, the FIPS standard keyed hash function\r
-*/\r
-\r
-#include <string.h>\r
-#include "hmac.h"\r
-\r
-#define HMAC_IPAD (0x36 * (((unsigned long)-1) / 0xff))\r
-#define HMAC_OPAD (0x5c * (((unsigned long)-1) / 0xff))\r
-\r
-/* initialise the HMAC context to zero */\r
-void hmac_sha_begin(hmac_ctx cx[1])\r
-{\r
- memset(cx, 0, sizeof(hmac_ctx));\r
-}\r
-\r
-/* input the HMAC key (can be called multiple times) */\r
-int hmac_sha_key(const unsigned char key[], unsigned long key_len, hmac_ctx cx[1])\r
-{\r
- if(cx->klen == HMAC_IN_DATA) /* error if further key input */\r
- return HMAC_BAD_MODE; /* is attempted in data mode */\r
-\r
- if(cx->klen + key_len > HMAC_HASH_INPUT_SIZE) /* if the key has to be hashed */\r
- {\r
- if(cx->klen <= HMAC_HASH_INPUT_SIZE) /* if the hash has not yet been */\r
- { /* started, initialise it and */\r
- sha_begin(cx->ctx); /* hash stored key characters */\r
- sha_hash(cx->key, cx->klen, cx->ctx);\r
- }\r
-\r
- sha_hash(key, key_len, cx->ctx); /* hash long key data into hash */\r
- }\r
- else /* otherwise store key data */\r
- memcpy(cx->key + cx->klen, key, key_len);\r
-\r
- cx->klen += key_len; /* update the key length count */\r
- return HMAC_OK;\r
-}\r
-\r
-/* input the HMAC data (can be called multiple times) - */\r
-/* note that this call terminates the key input phase */\r
-void hmac_sha_data(const unsigned char data[], unsigned long data_len, hmac_ctx cx[1])\r
-{ unsigned int i;\r
-\r
- if(cx->klen != HMAC_IN_DATA) /* if not yet in data phase */\r
- {\r
- if(cx->klen > HMAC_HASH_INPUT_SIZE) /* if key is being hashed */\r
- { /* complete the hash and */\r
- sha_end(cx->key, cx->ctx); /* store the result as the */\r
- cx->klen = HMAC_HASH_OUTPUT_SIZE; /* key and set new length */\r
- }\r
-\r
- /* pad the key if necessary */\r
- memset(cx->key + cx->klen, 0, HMAC_HASH_INPUT_SIZE - cx->klen);\r
-\r
- /* xor ipad into key value */\r
- for(i = 0; i < HMAC_HASH_INPUT_SIZE / sizeof(unsigned long); ++i)\r
- ((unsigned long*)cx->key)[i] ^= HMAC_IPAD;\r
-\r
- /* and start hash operation */\r
- sha_begin(cx->ctx);\r
- sha_hash(cx->key, HMAC_HASH_INPUT_SIZE, cx->ctx);\r
-\r
- /* mark as now in data mode */\r
- cx->klen = HMAC_IN_DATA;\r
- }\r
-\r
- /* hash the data (if any) */\r
- if(data_len)\r
- sha_hash(data, data_len, cx->ctx);\r
-}\r
-\r
-/* compute and output the MAC value */\r
-void hmac_sha_end(unsigned char mac[], unsigned long mac_len, hmac_ctx cx[1])\r
-{ unsigned char dig[HMAC_HASH_OUTPUT_SIZE];\r
- unsigned int i;\r
-\r
- /* if no data has been entered perform a null data phase */\r
- if(cx->klen != HMAC_IN_DATA)\r
- hmac_sha_data((const unsigned char*)0, 0, cx);\r
-\r
- sha_end(dig, cx->ctx); /* complete the inner hash */\r
-\r
- /* set outer key value using opad and removing ipad */\r
- for(i = 0; i < HMAC_HASH_INPUT_SIZE / sizeof(unsigned long); ++i)\r
- ((unsigned long*)cx->key)[i] ^= HMAC_OPAD ^ HMAC_IPAD;\r
-\r
- /* perform the outer hash operation */\r
- sha_begin(cx->ctx);\r
- sha_hash(cx->key, HMAC_HASH_INPUT_SIZE, cx->ctx);\r
- sha_hash(dig, HMAC_HASH_OUTPUT_SIZE, cx->ctx);\r
- sha_end(dig, cx->ctx);\r
-\r
- /* output the hash value */\r
- for(i = 0; i < mac_len; ++i)\r
- mac[i] = dig[i];\r
-}\r
-\r
-/* 'do it all in one go' subroutine */\r
-void hmac_sha(const unsigned char key[], unsigned long key_len,\r
- const unsigned char data[], unsigned long data_len,\r
- unsigned char mac[], unsigned long mac_len)\r
-{ hmac_ctx cx[1];\r
-\r
- hmac_sha_begin(cx);\r
- hmac_sha_key(key, key_len, cx);\r
- hmac_sha_data(data, data_len, cx);\r
- hmac_sha_end(mac, mac_len, cx);\r
-}\r
-\r
+++ /dev/null
-/*\r
- ---------------------------------------------------------------------------\r
- Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK.\r
- All rights reserved.\r
-\r
- LICENSE TERMS\r
-\r
- The free distribution and use of this software in both source and binary\r
- form is allowed (with or without changes) provided that:\r
-\r
- 1. distributions of this source code include the above copyright\r
- notice, this list of conditions and the following disclaimer;\r
-\r
- 2. distributions in binary form include the above copyright\r
- notice, this list of conditions and the following disclaimer\r
- in the documentation and/or other associated materials;\r
-\r
- 3. the copyright holder's name is not used to endorse products\r
- built using this software without specific written permission.\r
-\r
- ALTERNATIVELY, provided that this notice is retained in full, this product\r
- may be distributed under the terms of the GNU General Public License (GPL),\r
- in which case the provisions of the GPL apply INSTEAD OF those given above.\r
-\r
- DISCLAIMER\r
-\r
- This software is provided 'as is' with no explicit or implied warranties\r
- in respect of its properties, including, but not limited to, correctness\r
- and/or fitness for purpose.\r
- ---------------------------------------------------------------------------\r
- Issue Date: 26/08/2003\r
- Includes a bugfix from Dr Brian Gladman made on 16/04/2012 for compiling on 64-bit\r
-\r
- This is an implementation of HMAC, the FIPS standard keyed hash function\r
-*/\r
-\r
-#ifndef _HMAC_H\r
-#define _HMAC_H\r
-\r
-#include <memory.h>\r
-\r
-#define USE_SHA1 // Irrlicht only cares about SHA1 for now\r
-#if !defined(USE_SHA1) && !defined(USE_SHA256)\r
-#error define USE_SHA1 or USE_SHA256 to set the HMAC hash algorithm\r
-#endif\r
-\r
-#ifdef USE_SHA1\r
-\r
-#include "sha1.h"\r
-\r
-#define HMAC_HASH_INPUT_SIZE SHA1_BLOCK_SIZE\r
-#define HMAC_HASH_OUTPUT_SIZE SHA1_DIGEST_SIZE\r
-#define sha_ctx sha1_ctx\r
-#define sha_begin sha1_begin\r
-#define sha_hash sha1_hash\r
-#define sha_end sha1_end\r
-\r
-#endif\r
-\r
-#ifdef USE_SHA256\r
-\r
-#include "sha2.h"\r
-\r
-#define HMAC_HASH_INPUT_SIZE SHA256_BLOCK_SIZE\r
-#define HMAC_HASH_OUTPUT_SIZE SHA256_DIGEST_SIZE\r
-#define sha_ctx sha256_ctx\r
-#define sha_begin sha256_begin\r
-#define sha_hash sha256_hash\r
-#define sha_end sha256_end\r
-\r
-#endif\r
-\r
-#define HMAC_OK 0\r
-#define HMAC_BAD_MODE -1\r
-#define HMAC_IN_DATA 0xffffffff\r
-\r
-typedef struct\r
-{ unsigned char key[HMAC_HASH_INPUT_SIZE];\r
- sha_ctx ctx[1];\r
- unsigned long klen;\r
-} hmac_ctx;\r
-\r
-void hmac_sha_begin(hmac_ctx cx[1]);\r
-\r
-int hmac_sha_key(const unsigned char key[], unsigned long key_len, hmac_ctx cx[1]);\r
-\r
-void hmac_sha_data(const unsigned char data[], unsigned long data_len, hmac_ctx cx[1]);\r
-\r
-void hmac_sha_end(unsigned char mac[], unsigned long mac_len, hmac_ctx cx[1]);\r
-\r
-void hmac_sha(const unsigned char key[], unsigned long key_len,\r
- const unsigned char data[], unsigned long data_len,\r
- unsigned char mac[], unsigned long mac_len);\r
-\r
-#endif\r
+++ /dev/null
-/*\r
- ---------------------------------------------------------------------------\r
- Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK.\r
- All rights reserved.\r
-\r
- LICENSE TERMS\r
-\r
- The free distribution and use of this software in both source and binary\r
- form is allowed (with or without changes) provided that:\r
-\r
- 1. distributions of this source code include the above copyright\r
- notice, this list of conditions and the following disclaimer;\r
-\r
- 2. distributions in binary form include the above copyright\r
- notice, this list of conditions and the following disclaimer\r
- in the documentation and/or other associated materials;\r
-\r
- 3. the copyright holder's name is not used to endorse products\r
- built using this software without specific written permission.\r
-\r
- ALTERNATIVELY, provided that this notice is retained in full, this product\r
- may be distributed under the terms of the GNU General Public License (GPL),\r
- in which case the provisions of the GPL apply INSTEAD OF those given above.\r
-\r
- DISCLAIMER\r
-\r
- This software is provided 'as is' with no explicit or implied warranties\r
- in respect of its properties, including, but not limited to, correctness\r
- and/or fitness for purpose.\r
- ---------------------------------------------------------------------------\r
- Issue Date: 24/01/2003\r
-\r
- This file implements a random data pool based on the use of an external\r
- entropy function. It is based on the ideas advocated by Peter Gutmann in\r
- his work on pseudo random sequence generators. It is not a 'paranoid'\r
- random sequence generator and no attempt is made to protect the pool\r
- from prying eyes either by memory locking or by techniques to obscure\r
- its location in memory.\r
-*/\r
-\r
-#include <memory.h>\r
-#include <string.h>\r
-#include "prng.h"\r
-\r
-/* mix a random data pool using the SHA1 compression function (as */\r
-/* suggested by Peter Gutmann in his paper on random pools) */\r
-\r
-static void prng_mix(unsigned char buf[])\r
-{ unsigned int i, len;\r
- sha1_ctx ctx[1];\r
-\r
- /*lint -e{663} unusual array to pointer conversion */\r
- for(i = 0; i < PRNG_POOL_SIZE; i += SHA1_DIGEST_SIZE)\r
- {\r
- /* copy digest size pool block into SHA1 hash block */\r
- memcpy(ctx->hash, buf + (i ? i : PRNG_POOL_SIZE)\r
- - SHA1_DIGEST_SIZE, SHA1_DIGEST_SIZE);\r
-\r
- /* copy data from pool into the SHA1 data buffer */\r
- len = PRNG_POOL_SIZE - i;\r
- memcpy(ctx->wbuf, buf + i, (len > SHA1_BLOCK_SIZE ? SHA1_BLOCK_SIZE : len));\r
-\r
- if(len < SHA1_BLOCK_SIZE)\r
- memcpy(((char*)ctx->wbuf) + len, buf, SHA1_BLOCK_SIZE - len);\r
-\r
- /* compress using the SHA1 compression function */\r
- sha1_compile(ctx);\r
-\r
- /* put digest size block back into the random pool */\r
- memcpy(buf + i, ctx->hash, SHA1_DIGEST_SIZE);\r
- }\r
-}\r
-\r
-/* refresh the output buffer and update the random pool by adding */\r
-/* entropy and remixing */\r
-\r
-static void update_pool(prng_ctx ctx[1])\r
-{ unsigned int i = 0;\r
-\r
- /* transfer random pool data to the output buffer */\r
- memcpy(ctx->obuf, ctx->rbuf, PRNG_POOL_SIZE);\r
-\r
- /* enter entropy data into the pool */\r
- while(i < PRNG_POOL_SIZE)\r
- i += ctx->entropy(ctx->rbuf + i, PRNG_POOL_SIZE - i);\r
-\r
- /* invert and xor the original pool data into the pool */\r
- for(i = 0; i < PRNG_POOL_SIZE; ++i)\r
- ctx->rbuf[i] ^= ~ctx->obuf[i];\r
-\r
- /* mix the pool and the output buffer */\r
- prng_mix(ctx->rbuf);\r
- prng_mix(ctx->obuf);\r
-}\r
-\r
-void prng_init(prng_entropy_fn fun, prng_ctx ctx[1])\r
-{ int i;\r
-\r
- /* clear the buffers and the counter in the context */\r
- memset(ctx, 0, sizeof(prng_ctx));\r
-\r
- /* set the pointer to the entropy collection function */\r
- ctx->entropy = fun;\r
-\r
- /* initialise the random data pool */\r
- update_pool(ctx);\r
-\r
- /* mix the pool a minimum number of times */\r
- for(i = 0; i < PRNG_MIN_MIX; ++i)\r
- prng_mix(ctx->rbuf);\r
-\r
- /* update the pool to prime the pool output buffer */\r
- update_pool(ctx);\r
-}\r
-\r
-/* provide random bytes from the random data pool */\r
-\r
-void prng_rand(unsigned char data[], unsigned int data_len, prng_ctx ctx[1])\r
-{ unsigned char *rp = data;\r
- unsigned int len, pos = ctx->pos;\r
-\r
- while(data_len)\r
- {\r
- /* transfer 'data_len' bytes (or the number of bytes remaining */\r
- /* the pool output buffer if less) into the output */\r
- len = (data_len < PRNG_POOL_SIZE - pos ? data_len : PRNG_POOL_SIZE - pos);\r
- memcpy(rp, ctx->obuf + pos, len);\r
- rp += len; /* update ouput buffer position pointer */\r
- pos += len; /* update pool output buffer pointer */\r
- data_len -= len; /* update the remaining data count */\r
-\r
- /* refresh the random pool if necessary */\r
- if(pos == PRNG_POOL_SIZE)\r
- {\r
- update_pool(ctx); pos = 0;\r
- }\r
- }\r
-\r
- ctx->pos = pos;\r
-}\r
-\r
-void prng_end(prng_ctx ctx[1])\r
-{\r
- /* ensure the data in the context is destroyed */\r
- memset(ctx, 0, sizeof(prng_ctx));\r
-}\r
-\r
+++ /dev/null
-/*\r
- ---------------------------------------------------------------------------\r
- Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK.\r
- All rights reserved.\r
-\r
- LICENSE TERMS\r
-\r
- The free distribution and use of this software in both source and binary\r
- form is allowed (with or without changes) provided that:\r
-\r
- 1. distributions of this source code include the above copyright\r
- notice, this list of conditions and the following disclaimer;\r
-\r
- 2. distributions in binary form include the above copyright\r
- notice, this list of conditions and the following disclaimer\r
- in the documentation and/or other associated materials;\r
-\r
- 3. the copyright holder's name is not used to endorse products\r
- built using this software without specific written permission.\r
-\r
- ALTERNATIVELY, provided that this notice is retained in full, this product\r
- may be distributed under the terms of the GNU General Public License (GPL),\r
- in which case the provisions of the GPL apply INSTEAD OF those given above.\r
-\r
- DISCLAIMER\r
-\r
- This software is provided 'as is' with no explicit or implied warranties\r
- in respect of its properties, including, but not limited to, correctness\r
- and/or fitness for purpose.\r
- ---------------------------------------------------------------------------\r
- Issue Date: 24/01/2003\r
-\r
- This is the header file for an implementation of a random data pool based on\r
- the use of an external entropy function (inspired by Peter Gutmann's work).\r
-*/\r
-\r
-#ifndef _PRNG_H\r
-#define _PRNG_H\r
-\r
-#include "sha1.h"\r
-\r
-#define PRNG_POOL_LEN 256 /* minimum random pool size */\r
-#define PRNG_MIN_MIX 20 /* min initial pool mixing iterations */\r
-\r
-/* ensure that pool length is a multiple of the SHA1 digest size */\r
-\r
-#define PRNG_POOL_SIZE (SHA1_DIGEST_SIZE * (1 + (PRNG_POOL_LEN - 1) / SHA1_DIGEST_SIZE))\r
-\r
-/* A function for providing entropy is a parameter in the prng_init() */\r
-/* call. This function has the following form and returns a maximum */\r
-/* of 'len' bytes of pseudo random data in the buffer 'buf'. It can */\r
-/* return less than 'len' bytes but will be repeatedly called for more */\r
-/* data in this case. */\r
-\r
-typedef int (*prng_entropy_fn)(unsigned char buf[], unsigned int len);\r
-\r
-typedef struct\r
-{ unsigned char rbuf[PRNG_POOL_SIZE]; /* the random pool */\r
- unsigned char obuf[PRNG_POOL_SIZE]; /* pool output buffer */\r
- unsigned int pos; /* output buffer position */\r
- prng_entropy_fn entropy; /* entropy function pointer */\r
-} prng_ctx;\r
-\r
-/* initialise the random stream generator */\r
-void prng_init(prng_entropy_fn fun, prng_ctx ctx[1]);\r
-\r
-/* obtain random bytes from the generator */\r
-void prng_rand(unsigned char data[], unsigned int data_len, prng_ctx ctx[1]);\r
-\r
-/* close the random stream generator */\r
-void prng_end(prng_ctx ctx[1]);\r
-\r
-#endif\r
-\r
+++ /dev/null
-/*\r
- ---------------------------------------------------------------------------\r
- Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK.\r
- All rights reserved.\r
-\r
- LICENSE TERMS\r
-\r
- The free distribution and use of this software in both source and binary\r
- form is allowed (with or without changes) provided that:\r
-\r
- 1. distributions of this source code include the above copyright\r
- notice, this list of conditions and the following disclaimer;\r
-\r
- 2. distributions in binary form include the above copyright\r
- notice, this list of conditions and the following disclaimer\r
- in the documentation and/or other associated materials;\r
-\r
- 3. the copyright holder's name is not used to endorse products\r
- built using this software without specific written permission.\r
-\r
- ALTERNATIVELY, provided that this notice is retained in full, this product\r
- may be distributed under the terms of the GNU General Public License (GPL),\r
- in which case the provisions of the GPL apply INSTEAD OF those given above.\r
-\r
- DISCLAIMER\r
-\r
- This software is provided 'as is' with no explicit or implied warranties\r
- in respect of its properties, including, but not limited to, correctness\r
- and/or fitness for purpose.\r
- ---------------------------------------------------------------------------\r
- Issue Date: 26/08/2003\r
-\r
- This is an implementation of RFC2898, which specifies key derivation from\r
- a password and a salt value.\r
-*/\r
-\r
-#include <memory.h>\r
-#include <string.h>\r
-#include "hmac.h"\r
-\r
-void derive_key(const unsigned char pwd[], /* the PASSWORD */\r
- unsigned int pwd_len, /* and its length */\r
- const unsigned char salt[], /* the SALT and its */\r
- unsigned int salt_len, /* length */\r
- unsigned int iter, /* the number of iterations */\r
- unsigned char key[], /* space for the output key */\r
- unsigned int key_len)/* and its required length */\r
-{\r
- unsigned int i, j, k, n_blk;\r
- unsigned char uu[HMAC_HASH_OUTPUT_SIZE], ux[HMAC_HASH_OUTPUT_SIZE];\r
- hmac_ctx c1[1], c2[1], c3[1];\r
-\r
- /* set HMAC context (c1) for password */\r
- hmac_sha_begin(c1);\r
- hmac_sha_key(pwd, pwd_len, c1);\r
-\r
- /* set HMAC context (c2) for password and salt */\r
- memcpy(c2, c1, sizeof(hmac_ctx));\r
- hmac_sha_data(salt, salt_len, c2);\r
-\r
- /* find the number of SHA blocks in the key */\r
- n_blk = 1 + (key_len - 1) / HMAC_HASH_OUTPUT_SIZE;\r
-\r
- for(i = 0; i < n_blk; ++i) /* for each block in key */\r
- {\r
- /* ux[] holds the running xor value */\r
- memset(ux, 0, HMAC_HASH_OUTPUT_SIZE);\r
-\r
- /* set HMAC context (c3) for password and salt */\r
- memcpy(c3, c2, sizeof(hmac_ctx));\r
-\r
- /* enter additional data for 1st block into uu */\r
- uu[0] = (unsigned char)((i + 1) >> 24);\r
- uu[1] = (unsigned char)((i + 1) >> 16);\r
- uu[2] = (unsigned char)((i + 1) >> 8);\r
- uu[3] = (unsigned char)(i + 1);\r
-\r
- /* this is the key mixing iteration */\r
- for(j = 0, k = 4; j < iter; ++j)\r
- {\r
- /* add previous round data to HMAC */\r
- hmac_sha_data(uu, k, c3);\r
-\r
- /* obtain HMAC for uu[] */\r
- hmac_sha_end(uu, HMAC_HASH_OUTPUT_SIZE, c3);\r
-\r
- /* xor into the running xor block */\r
- for(k = 0; k < HMAC_HASH_OUTPUT_SIZE; ++k)\r
- ux[k] ^= uu[k];\r
-\r
- /* set HMAC context (c3) for password */\r
- memcpy(c3, c1, sizeof(hmac_ctx));\r
- }\r
-\r
- /* compile key blocks into the key output */\r
- j = 0; k = i * HMAC_HASH_OUTPUT_SIZE;\r
- while(j < HMAC_HASH_OUTPUT_SIZE && k < key_len)\r
- key[k++] = ux[j++];\r
- }\r
-}\r
-\r
-#ifdef TEST\r
-\r
-#include <stdio.h>\r
-\r
-struct\r
-{ unsigned int pwd_len;\r
- unsigned int salt_len;\r
- unsigned int it_count;\r
- unsigned char *pwd;\r
- unsigned char salt[32];\r
- unsigned char key[32];\r
-} tests[] =\r
-{\r
- { 8, 4, 5, (unsigned char*)"password",\r
- {\r
- 0x12, 0x34, 0x56, 0x78\r
- },\r
- {\r
- 0x5c, 0x75, 0xce, 0xf0, 0x1a, 0x96, 0x0d, 0xf7,\r
- 0x4c, 0xb6, 0xb4, 0x9b, 0x9e, 0x38, 0xe6, 0xb5\r
- }\r
- },\r
- { 8, 8, 5, (unsigned char*)"password",\r
- {\r
- 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12\r
- },\r
- {\r
- 0xd1, 0xda, 0xa7, 0x86, 0x15, 0xf2, 0x87, 0xe6,\r
- 0xa1, 0xc8, 0xb1, 0x20, 0xd7, 0x06, 0x2a, 0x49\r
- }\r
- },\r
- { 8, 21, 1, (unsigned char*)"password",\r
- {\r
- "ATHENA.MIT.EDUraeburn"\r
- },\r
- {\r
- 0xcd, 0xed, 0xb5, 0x28, 0x1b, 0xb2, 0xf8, 0x01,\r
- 0x56, 0x5a, 0x11, 0x22, 0xb2, 0x56, 0x35, 0x15\r
- }\r
- },\r
- { 8, 21, 2, (unsigned char*)"password",\r
- {\r
- "ATHENA.MIT.EDUraeburn"\r
- },\r
- {\r
- 0x01, 0xdb, 0xee, 0x7f, 0x4a, 0x9e, 0x24, 0x3e,\r
- 0x98, 0x8b, 0x62, 0xc7, 0x3c, 0xda, 0x93, 0x5d\r
- }\r
- },\r
- { 8, 21, 1200, (unsigned char*)"password",\r
- {\r
- "ATHENA.MIT.EDUraeburn"\r
- },\r
- {\r
- 0x5c, 0x08, 0xeb, 0x61, 0xfd, 0xf7, 0x1e, 0x4e,\r
- 0x4e, 0xc3, 0xcf, 0x6b, 0xa1, 0xf5, 0x51, 0x2b\r
- }\r
- }\r
-};\r
-\r
-int main()\r
-{ unsigned int i, j, key_len = 256;\r
- unsigned char key[256];\r
-\r
- printf("\nTest of RFC2898 Password Based Key Derivation");\r
- for(i = 0; i < 5; ++i)\r
- {\r
- derive_key(tests[i].pwd, tests[i].pwd_len, tests[i].salt,\r
- tests[i].salt_len, tests[i].it_count, key, key_len);\r
-\r
- printf("\ntest %i: ", i + 1);\r
- printf("key %s", memcmp(tests[i].key, key, 16) ? "is bad" : "is good");\r
- for(j = 0; j < key_len && j < 64; j += 4)\r
- {\r
- if(j % 16 == 0)\r
- printf("\n");\r
- printf("0x%02x%02x%02x%02x ", key[j], key[j + 1], key[j + 2], key[j + 3]);\r
- }\r
- printf(j < key_len ? " ... \n" : "\n");\r
- }\r
- printf("\n");\r
- return 0;\r
-}\r
-\r
-#endif\r
-\r
+++ /dev/null
-/*\r
- ---------------------------------------------------------------------------\r
- Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK.\r
- All rights reserved.\r
-\r
- LICENSE TERMS\r
-\r
- The free distribution and use of this software in both source and binary \r
- form is allowed (with or without changes) provided that:\r
-\r
- 1. distributions of this source code include the above copyright \r
- notice, this list of conditions and the following disclaimer;\r
-\r
- 2. distributions in binary form include the above copyright\r
- notice, this list of conditions and the following disclaimer\r
- in the documentation and/or other associated materials;\r
-\r
- 3. the copyright holder's name is not used to endorse products \r
- built using this software without specific written permission. \r
-\r
- ALTERNATIVELY, provided that this notice is retained in full, this product\r
- may be distributed under the terms of the GNU General Public License (GPL),\r
- in which case the provisions of the GPL apply INSTEAD OF those given above.\r
- \r
- DISCLAIMER\r
-\r
- This software is provided 'as is' with no explicit or implied warranties\r
- in respect of its properties, including, but not limited to, correctness \r
- and/or fitness for purpose.\r
- ---------------------------------------------------------------------------\r
- Issue Date: 26/08/2003\r
-\r
- This is an implementation of RFC2898, which specifies key derivation from\r
- a password and a salt value.\r
-*/\r
-\r
-#ifndef PWD2KEY_H\r
-#define PWD2KEY_H\r
-\r
-void derive_key(\r
- const unsigned char pwd[], /* the PASSWORD, and */\r
- unsigned int pwd_len, /* its length */ \r
- const unsigned char salt[], /* the SALT and its */\r
- unsigned int salt_len, /* length */\r
- unsigned int iter, /* the number of iterations */\r
- unsigned char key[], /* space for the output key */\r
- unsigned int key_len); /* and its required length */\r
-\r
-#endif\r
-\r
+++ /dev/null
-/*\r
- ---------------------------------------------------------------------------\r
- Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK.\r
- All rights reserved.\r
-\r
- LICENSE TERMS\r
-\r
- The free distribution and use of this software in both source and binary \r
- form is allowed (with or without changes) provided that:\r
-\r
- 1. distributions of this source code include the above copyright \r
- notice, this list of conditions and the following disclaimer;\r
-\r
- 2. distributions in binary form include the above copyright\r
- notice, this list of conditions and the following disclaimer\r
- in the documentation and/or other associated materials;\r
-\r
- 3. the copyright holder's name is not used to endorse products \r
- built using this software without specific written permission. \r
-\r
- ALTERNATIVELY, provided that this notice is retained in full, this product\r
- may be distributed under the terms of the GNU General Public License (GPL),\r
- in which case the provisions of the GPL apply INSTEAD OF those given above.\r
- \r
- DISCLAIMER\r
-\r
- This software is provided 'as is' with no explicit or implied warranties\r
- in respect of its properties, including, but not limited to, correctness \r
- and/or fitness for purpose.\r
- ---------------------------------------------------------------------------\r
- Issue Date: 26/08/2003\r
-\r
- This is a byte oriented version of SHA1 that operates on arrays of bytes\r
- stored in memory. It runs at 22 cycles per byte on a Pentium P4 processor\r
-*/\r
-\r
-#include <string.h> /* for memcpy() etc. */\r
-#include <stdlib.h> /* for _lrotl with VC++ */\r
-\r
-#include "sha1.h"\r
-#include "../os.h"\r
-\r
-/*\r
- To obtain the highest speed on processors with 32-bit words, this code \r
- needs to determine the order in which bytes are packed into such words.\r
- The following block of code is an attempt to capture the most obvious \r
- ways in which various environemnts specify their endian definitions. \r
- It may well fail, in which case the definitions will need to be set by \r
- editing at the points marked **** EDIT HERE IF NECESSARY **** below.\r
-*/\r
-\r
-/* BYTE ORDER IN 32-BIT WORDS\r
-\r
- To obtain the highest speed on processors with 32-bit words, this code\r
- needs to determine the byte order of the target machine. The following \r
- block of code is an attempt to capture the most obvious ways in which \r
- various environemnts define byte order. It may well fail, in which case \r
- the definitions will need to be set by editing at the points marked \r
- **** EDIT HERE IF NECESSARY **** below. My thanks to Peter Gutmann for \r
- some of these defines (from cryptlib).\r
-*/\r
-\r
-#define BRG_LITTLE_ENDIAN 1234 /* byte 0 is least significant (i386) */\r
-#define BRG_BIG_ENDIAN 4321 /* byte 0 is most significant (mc68k) */\r
-\r
-#ifdef __BIG_ENDIAN__\r
-#define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN\r
-#else\r
-#define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN\r
-#endif\r
-\r
-#define rotl32(x,n) (((x) << n) | ((x) >> (32 - n)))\r
-\r
-#if (PLATFORM_BYTE_ORDER == BRG_BIG_ENDIAN)\r
-#define swap_b32(x) (x)\r
-#else\r
-#define swap_b32(x) irr::os::Byteswap::byteswap(x)\r
-#endif\r
-\r
-#define SHA1_MASK (SHA1_BLOCK_SIZE - 1)\r
-\r
-#if 1\r
-\r
-#define ch(x,y,z) (((x) & (y)) ^ (~(x) & (z)))\r
-#define parity(x,y,z) ((x) ^ (y) ^ (z))\r
-#define maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))\r
-\r
-#else /* Discovered Rich Schroeppel and Colin Plumb */\r
-\r
-#define ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))\r
-#define parity(x,y,z) ((x) ^ (y) ^ (z))\r
-#define maj(x,y,z) (((x) & (y)) | ((z) & ((x) ^ (y))))\r
-\r
-#endif\r
-\r
-/* A normal version as set out in the FIPS */\r
-\r
-#define rnd(f,k) \\r
- t = a; a = rotl32(a,5) + f(b,c,d) + e + k + w[i]; \\r
- e = d; d = c; c = rotl32(b, 30); b = t\r
-\r
-void sha1_compile(sha1_ctx ctx[1])\r
-{ sha1_32t w[80], i, a, b, c, d, e, t;\r
-\r
- /* note that words are compiled from the buffer into 32-bit */\r
- /* words in big-endian order so an order reversal is needed */\r
- /* here on little endian machines */\r
- for(i = 0; i < SHA1_BLOCK_SIZE / 4; ++i)\r
- w[i] = swap_b32(ctx->wbuf[i]);\r
-\r
- for(i = SHA1_BLOCK_SIZE / 4; i < 80; ++i)\r
- w[i] = rotl32(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1);\r
-\r
- a = ctx->hash[0];\r
- b = ctx->hash[1];\r
- c = ctx->hash[2];\r
- d = ctx->hash[3];\r
- e = ctx->hash[4];\r
-\r
- for(i = 0; i < 20; ++i)\r
- {\r
- rnd(ch, 0x5a827999); \r
- }\r
-\r
- for(i = 20; i < 40; ++i)\r
- {\r
- rnd(parity, 0x6ed9eba1);\r
- }\r
-\r
- for(i = 40; i < 60; ++i)\r
- {\r
- rnd(maj, 0x8f1bbcdc);\r
- }\r
-\r
- for(i = 60; i < 80; ++i)\r
- {\r
- rnd(parity, 0xca62c1d6);\r
- }\r
-\r
- ctx->hash[0] += a; \r
- ctx->hash[1] += b; \r
- ctx->hash[2] += c; \r
- ctx->hash[3] += d; \r
- ctx->hash[4] += e;\r
-}\r
-\r
-void sha1_begin(sha1_ctx ctx[1])\r
-{\r
- ctx->count[0] = ctx->count[1] = 0;\r
- ctx->hash[0] = 0x67452301;\r
- ctx->hash[1] = 0xefcdab89;\r
- ctx->hash[2] = 0x98badcfe;\r
- ctx->hash[3] = 0x10325476;\r
- ctx->hash[4] = 0xc3d2e1f0;\r
-}\r
-\r
-/* SHA1 hash data in an array of bytes into hash buffer and */\r
-/* call the hash_compile function as required. */\r
-\r
-void sha1_hash(const unsigned char data[], unsigned long len, sha1_ctx ctx[1])\r
-{ sha1_32t pos = (sha1_32t)(ctx->count[0] & SHA1_MASK), \r
- space = SHA1_BLOCK_SIZE - pos;\r
- const unsigned char *sp = data;\r
-\r
- if((ctx->count[0] += len) < len)\r
- ++(ctx->count[1]);\r
-\r
- while(len >= space) /* tranfer whole blocks if possible */\r
- {\r
- memcpy(((unsigned char*)ctx->wbuf) + pos, sp, space);\r
- sp += space; len -= space; space = SHA1_BLOCK_SIZE; pos = 0; \r
- sha1_compile(ctx);\r
- }\r
-\r
- /*lint -e{803} conceivable data overrun */\r
- memcpy(((unsigned char*)ctx->wbuf) + pos, sp, len);\r
-}\r
-\r
-/* SHA1 final padding and digest calculation */\r
-\r
-#if (PLATFORM_BYTE_ORDER == BRG_LITTLE_ENDIAN)\r
-static sha1_32t mask[4] = \r
- { 0x00000000, 0x000000ff, 0x0000ffff, 0x00ffffff };\r
-static sha1_32t bits[4] = \r
- { 0x00000080, 0x00008000, 0x00800000, 0x80000000 };\r
-#else\r
-static sha1_32t mask[4] = \r
- { 0x00000000, 0xff000000, 0xffff0000, 0xffffff00 };\r
-static sha1_32t bits[4] = \r
- { 0x80000000, 0x00800000, 0x00008000, 0x00000080 };\r
-#endif\r
-\r
-void sha1_end(unsigned char hval[], sha1_ctx ctx[1])\r
-{ sha1_32t i = (sha1_32t)(ctx->count[0] & SHA1_MASK);\r
-\r
- /* mask out the rest of any partial 32-bit word and then set */\r
- /* the next byte to 0x80. On big-endian machines any bytes in */\r
- /* the buffer will be at the top end of 32 bit words, on little */\r
- /* endian machines they will be at the bottom. Hence the AND */\r
- /* and OR masks above are reversed for little endian systems */\r
- /* Note that we can always add the first padding byte at this */\r
- /* point because the buffer always has at least one empty slot */ \r
- ctx->wbuf[i >> 2] = (ctx->wbuf[i >> 2] & mask[i & 3]) | bits[i & 3];\r
-\r
- /* we need 9 or more empty positions, one for the padding byte */\r
- /* (above) and eight for the length count. If there is not */\r
- /* enough space pad and empty the buffer */\r
- if(i > SHA1_BLOCK_SIZE - 9)\r
- {\r
- if(i < 60) ctx->wbuf[15] = 0;\r
- sha1_compile(ctx);\r
- i = 0;\r
- }\r
- else /* compute a word index for the empty buffer positions */\r
- i = (i >> 2) + 1;\r
-\r
- while(i < 14) /* and zero pad all but last two positions */ \r
- ctx->wbuf[i++] = 0;\r
- \r
- /* assemble the eight byte counter in in big-endian format */\r
- ctx->wbuf[14] = swap_b32((ctx->count[1] << 3) | (ctx->count[0] >> 29));\r
- ctx->wbuf[15] = swap_b32(ctx->count[0] << 3);\r
-\r
- sha1_compile(ctx);\r
-\r
- /* extract the hash value as bytes in case the hash buffer is */\r
- /* misaligned for 32-bit words */\r
- for(i = 0; i < SHA1_DIGEST_SIZE; ++i)\r
- hval[i] = (unsigned char)(ctx->hash[i >> 2] >> (8 * (~i & 3)));\r
-}\r
-\r
-void sha1(unsigned char hval[], const unsigned char data[], unsigned long len)\r
-{ sha1_ctx cx[1];\r
-\r
- sha1_begin(cx); sha1_hash(data, len, cx); sha1_end(hval, cx);\r
-}\r
-\r
+++ /dev/null
-/*\r
- ---------------------------------------------------------------------------\r
- Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK.\r
- All rights reserved.\r
-\r
- LICENSE TERMS\r
-\r
- The free distribution and use of this software in both source and binary \r
- form is allowed (with or without changes) provided that:\r
-\r
- 1. distributions of this source code include the above copyright \r
- notice, this list of conditions and the following disclaimer;\r
-\r
- 2. distributions in binary form include the above copyright\r
- notice, this list of conditions and the following disclaimer\r
- in the documentation and/or other associated materials;\r
-\r
- 3. the copyright holder's name is not used to endorse products \r
- built using this software without specific written permission. \r
-\r
- ALTERNATIVELY, provided that this notice is retained in full, this product\r
- may be distributed under the terms of the GNU General Public License (GPL),\r
- in which case the provisions of the GPL apply INSTEAD OF those given above.\r
- \r
- DISCLAIMER\r
-\r
- This software is provided 'as is' with no explicit or implied warranties\r
- in respect of its properties, including, but not limited to, correctness \r
- and/or fitness for purpose.\r
- ---------------------------------------------------------------------------\r
- Issue Date: 26/08/2003\r
-*/\r
-\r
-#ifndef _SHA1_H\r
-#define _SHA1_H\r
-\r
-#include <limits.h>\r
-\r
-#define SHA1_BLOCK_SIZE 64\r
-#define SHA1_DIGEST_SIZE 20\r
-\r
-/* define an unsigned 32-bit type */\r
-\r
-#if UINT_MAX == 0xffffffff\r
- typedef unsigned int sha1_32t;\r
-#elif ULONG_MAX == 0xffffffff\r
- typedef unsigned long sha1_32t;\r
-#else\r
-#error Please define sha1_32t as an unsigned 32 bit type in sha2.h\r
-#endif\r
-\r
-/* type to hold the SHA256 context */\r
-\r
-typedef struct\r
-{ sha1_32t count[2];\r
- sha1_32t hash[5];\r
- sha1_32t wbuf[16];\r
-} sha1_ctx;\r
-\r
-void sha1_compile(sha1_ctx ctx[1]);\r
-\r
-void sha1_begin(sha1_ctx ctx[1]);\r
-void sha1_hash(const unsigned char data[], unsigned long len, sha1_ctx ctx[1]);\r
-void sha1_end(unsigned char hval[], sha1_ctx ctx[1]);\r
-void sha1(unsigned char hval[], const unsigned char data[], unsigned long len);\r
-\r
-#endif\r
-\r
+++ /dev/null
-/*\r
- ---------------------------------------------------------------------------\r
- Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK.\r
- All rights reserved.\r
-\r
- LICENSE TERMS\r
-\r
- The free distribution and use of this software in both source and binary\r
- form is allowed (with or without changes) provided that:\r
-\r
- 1. distributions of this source code include the above copyright\r
- notice, this list of conditions and the following disclaimer;\r
-\r
- 2. distributions in binary form include the above copyright\r
- notice, this list of conditions and the following disclaimer\r
- in the documentation and/or other associated materials;\r
-\r
- 3. the copyright holder's name is not used to endorse products\r
- built using this software without specific written permission.\r
-\r
- ALTERNATIVELY, provided that this notice is retained in full, this product\r
- may be distributed under the terms of the GNU General Public License (GPL),\r
- in which case the provisions of the GPL apply INSTEAD OF those given above.\r
-\r
- DISCLAIMER\r
-\r
- This software is provided 'as is' with no explicit or implied warranties\r
- in respect of its properties, including, but not limited to, correctness\r
- and/or fitness for purpose.\r
- ---------------------------------------------------------------------------\r
- Issue Date: 26/08/2003\r
-\r
- This is a byte oriented version of SHA2 that operates on arrays of bytes\r
- stored in memory. This code implements sha256, sha384 and sha512 but the\r
- latter two functions rely on efficient 64-bit integer operations that\r
- may not be very efficient on 32-bit machines\r
-\r
- The sha256 functions use a type 'sha256_ctx' to hold details of the\r
- current hash state and uses the following three calls:\r
-\r
- void sha256_begin(sha256_ctx ctx[1])\r
- void sha256_hash(const unsigned char data[],\r
- unsigned long len, sha256_ctx ctx[1])\r
- void sha256_end(unsigned char hval[], sha256_ctx ctx[1])\r
-\r
- The first subroutine initialises a hash computation by setting up the\r
- context in the sha256_ctx context. The second subroutine hashes 8-bit\r
- bytes from array data[] into the hash state withinh sha256_ctx context,\r
- the number of bytes to be hashed being given by the the unsigned long\r
- integer len. The third subroutine completes the hash calculation and\r
- places the resulting digest value in the array of 8-bit bytes hval[].\r
-\r
- The sha384 and sha512 functions are similar and use the interfaces:\r
-\r
- void sha384_begin(sha384_ctx ctx[1]);\r
- void sha384_hash(const unsigned char data[],\r
- unsigned long len, sha384_ctx ctx[1]);\r
- void sha384_end(unsigned char hval[], sha384_ctx ctx[1]);\r
-\r
- void sha512_begin(sha512_ctx ctx[1]);\r
- void sha512_hash(const unsigned char data[],\r
- unsigned long len, sha512_ctx ctx[1]);\r
- void sha512_end(unsigned char hval[], sha512_ctx ctx[1]);\r
-\r
- In addition there is a function sha2 that can be used to call all these\r
- functions using a call with a hash length parameter as follows:\r
-\r
- int sha2_begin(unsigned long len, sha2_ctx ctx[1]);\r
- void sha2_hash(const unsigned char data[],\r
- unsigned long len, sha2_ctx ctx[1]);\r
- void sha2_end(unsigned char hval[], sha2_ctx ctx[1]);\r
-\r
- My thanks to Erik Andersen <andersen@codepoet.org> for testing this code\r
- on big-endian systems and for his assistance with corrections\r
-*/\r
-\r
-/* define the hash functions that you need */\r
-\r
-#define SHA_2 /* for dynamic hash length */\r
-#define SHA_256\r
-#define SHA_384\r
-#define SHA_512\r
-\r
-#include <string.h> /* for memcpy() etc. */\r
-#include <stdlib.h> /* for _lrotr with VC++ */\r
-\r
-#include "sha2.h"\r
-#include "../os.h"\r
-\r
-/* BYTE ORDER IN 32-BIT WORDS\r
-\r
- To obtain the highest speed on processors with 32-bit words, this code\r
- needs to determine the byte order of the target machine. The following\r
- block of code is an attempt to capture the most obvious ways in which\r
- various environemnts define byte order. It may well fail, in which case\r
- the definitions will need to be set by editing at the points marked\r
- **** EDIT HERE IF NECESSARY **** below. My thanks to Peter Gutmann for\r
- some of these defines (from cryptlib).\r
-*/\r
-\r
-#define BRG_LITTLE_ENDIAN 1234 /* byte 0 is least significant (i386) */\r
-#define BRG_BIG_ENDIAN 4321 /* byte 0 is most significant (mc68k) */\r
-\r
-#ifdef __BIG_ENDIAN__\r
-#define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN\r
-#else\r
-#define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN\r
-#endif\r
-\r
-#ifdef _MSC_VER\r
-#pragma intrinsic(memcpy)\r
-#endif\r
-\r
-#define rotr32(x,n) (((x) >> n) | ((x) << (32 - n)))\r
-\r
-#if !defined(bswap_32)\r
-#define bswap_32(x) irr::os::Byteswap::byteswap(x)\r
-#endif\r
-\r
-#if (PLATFORM_BYTE_ORDER == BRG_LITTLE_ENDIAN)\r
-#define SWAP_BYTES\r
-#else\r
-#undef SWAP_BYTES\r
-#endif\r
-\r
-#if defined(SHA_2) || defined(SHA_256)\r
-\r
-#define SHA256_MASK (SHA256_BLOCK_SIZE - 1)\r
-\r
-#if defined(SWAP_BYTES)\r
-#define bsw_32(p,n) { int _i = (n); while(_i--) p[_i] = bswap_32(p[_i]); }\r
-#else\r
-#define bsw_32(p,n)\r
-#endif\r
-\r
-/* SHA256 mixing function definitions */\r
-\r
-#if 0\r
-\r
-#define ch(x,y,z) (((x) & (y)) ^ (~(x) & (z)))\r
-#define maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))\r
-\r
-#else /* Thanks to Rich Schroeppel and Colin Plumb for the following */\r
-\r
-#define ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))\r
-#define maj(x,y,z) (((x) & (y)) | ((z) & ((x) ^ (y))))\r
-\r
-#endif\r
-\r
-#define s256_0(x) (rotr32((x), 2) ^ rotr32((x), 13) ^ rotr32((x), 22))\r
-#define s256_1(x) (rotr32((x), 6) ^ rotr32((x), 11) ^ rotr32((x), 25))\r
-#define g256_0(x) (rotr32((x), 7) ^ rotr32((x), 18) ^ ((x) >> 3))\r
-#define g256_1(x) (rotr32((x), 17) ^ rotr32((x), 19) ^ ((x) >> 10))\r
-\r
-/* rotated SHA256 round definition. Rather than swapping variables as in */\r
-/* FIPS-180, different variables are 'rotated' on each round, returning */\r
-/* to their starting positions every eight rounds */\r
-\r
-#define h2(i) p[i & 15] += \\r
- g256_1(p[(i + 14) & 15]) + p[(i + 9) & 15] + g256_0(p[(i + 1) & 15])\r
-\r
-#define h2_cycle(i,j) \\r
- v[(7 - i) & 7] += (j ? h2(i) : p[i & 15]) + k256[i + j] \\r
- + s256_1(v[(4 - i) & 7]) + ch(v[(4 - i) & 7], v[(5 - i) & 7], v[(6 - i) & 7]); \\r
- v[(3 - i) & 7] += v[(7 - i) & 7]; \\r
- v[(7 - i) & 7] += s256_0(v[(0 - i) & 7]) + maj(v[(0 - i) & 7], v[(1 - i) & 7], v[(2 - i) & 7])\r
-\r
-/* SHA256 mixing data */\r
-\r
-const sha2_32t k256[64] =\r
-{ n_u32(428a2f98), n_u32(71374491), n_u32(b5c0fbcf), n_u32(e9b5dba5),\r
- n_u32(3956c25b), n_u32(59f111f1), n_u32(923f82a4), n_u32(ab1c5ed5),\r
- n_u32(d807aa98), n_u32(12835b01), n_u32(243185be), n_u32(550c7dc3),\r
- n_u32(72be5d74), n_u32(80deb1fe), n_u32(9bdc06a7), n_u32(c19bf174),\r
- n_u32(e49b69c1), n_u32(efbe4786), n_u32(0fc19dc6), n_u32(240ca1cc),\r
- n_u32(2de92c6f), n_u32(4a7484aa), n_u32(5cb0a9dc), n_u32(76f988da),\r
- n_u32(983e5152), n_u32(a831c66d), n_u32(b00327c8), n_u32(bf597fc7),\r
- n_u32(c6e00bf3), n_u32(d5a79147), n_u32(06ca6351), n_u32(14292967),\r
- n_u32(27b70a85), n_u32(2e1b2138), n_u32(4d2c6dfc), n_u32(53380d13),\r
- n_u32(650a7354), n_u32(766a0abb), n_u32(81c2c92e), n_u32(92722c85),\r
- n_u32(a2bfe8a1), n_u32(a81a664b), n_u32(c24b8b70), n_u32(c76c51a3),\r
- n_u32(d192e819), n_u32(d6990624), n_u32(f40e3585), n_u32(106aa070),\r
- n_u32(19a4c116), n_u32(1e376c08), n_u32(2748774c), n_u32(34b0bcb5),\r
- n_u32(391c0cb3), n_u32(4ed8aa4a), n_u32(5b9cca4f), n_u32(682e6ff3),\r
- n_u32(748f82ee), n_u32(78a5636f), n_u32(84c87814), n_u32(8cc70208),\r
- n_u32(90befffa), n_u32(a4506ceb), n_u32(bef9a3f7), n_u32(c67178f2),\r
-};\r
-\r
-/* SHA256 initialisation data */\r
-\r
-const sha2_32t i256[8] =\r
-{\r
- n_u32(6a09e667), n_u32(bb67ae85), n_u32(3c6ef372), n_u32(a54ff53a),\r
- n_u32(510e527f), n_u32(9b05688c), n_u32(1f83d9ab), n_u32(5be0cd19)\r
-};\r
-\r
-sha2_void sha256_begin(sha256_ctx ctx[1])\r
-{\r
- ctx->count[0] = ctx->count[1] = 0;\r
- memcpy(ctx->hash, i256, 8 * sizeof(sha2_32t));\r
-}\r
-\r
-/* Compile 64 bytes of hash data into SHA256 digest value */\r
-/* NOTE: this routine assumes that the byte order in the */\r
-/* ctx->wbuf[] at this point is in such an order that low */\r
-/* address bytes in the ORIGINAL byte stream placed in this */\r
-/* buffer will now go to the high end of words on BOTH big */\r
-/* and little endian systems */\r
-\r
-sha2_void sha256_compile(sha256_ctx ctx[1])\r
-{ sha2_32t v[8], j, *p = ctx->wbuf;\r
-\r
- memcpy(v, ctx->hash, 8 * sizeof(sha2_32t));\r
-\r
- for(j = 0; j < 64; j += 16)\r
- {\r
- h2_cycle( 0, j); h2_cycle( 1, j); h2_cycle( 2, j); h2_cycle( 3, j);\r
- h2_cycle( 4, j); h2_cycle( 5, j); h2_cycle( 6, j); h2_cycle( 7, j);\r
- h2_cycle( 8, j); h2_cycle( 9, j); h2_cycle(10, j); h2_cycle(11, j);\r
- h2_cycle(12, j); h2_cycle(13, j); h2_cycle(14, j); h2_cycle(15, j);\r
- }\r
-\r
- ctx->hash[0] += v[0]; ctx->hash[1] += v[1]; ctx->hash[2] += v[2]; ctx->hash[3] += v[3];\r
- ctx->hash[4] += v[4]; ctx->hash[5] += v[5]; ctx->hash[6] += v[6]; ctx->hash[7] += v[7];\r
-}\r
-\r
-/* SHA256 hash data in an array of bytes into hash buffer */\r
-/* and call the hash_compile function as required. */\r
-\r
-sha2_void sha256_hash(const unsigned char data[], unsigned long len, sha256_ctx ctx[1])\r
-{ sha2_32t pos = (sha2_32t)(ctx->count[0] & SHA256_MASK),\r
- space = SHA256_BLOCK_SIZE - pos;\r
- const unsigned char *sp = data;\r
-\r
- if((ctx->count[0] += len) < len)\r
- ++(ctx->count[1]);\r
-\r
- while(len >= space) /* tranfer whole blocks while possible */\r
- {\r
- memcpy(((unsigned char*)ctx->wbuf) + pos, sp, space);\r
- sp += space; len -= space; space = SHA256_BLOCK_SIZE; pos = 0;\r
- bsw_32(ctx->wbuf, SHA256_BLOCK_SIZE >> 2)\r
- sha256_compile(ctx);\r
- }\r
-\r
- memcpy(((unsigned char*)ctx->wbuf) + pos, sp, len);\r
-}\r
-\r
-/* SHA256 Final padding and digest calculation */\r
-\r
-static sha2_32t m1[4] =\r
-{\r
- n_u32(00000000), n_u32(ff000000), n_u32(ffff0000), n_u32(ffffff00)\r
-};\r
-\r
-static sha2_32t b1[4] =\r
-{\r
- n_u32(80000000), n_u32(00800000), n_u32(00008000), n_u32(00000080)\r
-};\r
-\r
-sha2_void sha256_end(unsigned char hval[], sha256_ctx ctx[1])\r
-{ sha2_32t i = (sha2_32t)(ctx->count[0] & SHA256_MASK);\r
-\r
- bsw_32(ctx->wbuf, (i + 3) >> 2)\r
- /* bytes in the buffer are now in an order in which references */\r
- /* to 32-bit words will put bytes with lower addresses into the */\r
- /* top of 32 bit words on BOTH big and little endian machines */\r
-\r
- /* we now need to mask valid bytes and add the padding which is */\r
- /* a single 1 bit and as many zero bits as necessary. */\r
- ctx->wbuf[i >> 2] = (ctx->wbuf[i >> 2] & m1[i & 3]) | b1[i & 3];\r
-\r
- /* we need 9 or more empty positions, one for the padding byte */\r
- /* (above) and eight for the length count. If there is not */\r
- /* enough space pad and empty the buffer */\r
- if(i > SHA256_BLOCK_SIZE - 9)\r
- {\r
- if(i < 60) ctx->wbuf[15] = 0;\r
- sha256_compile(ctx);\r
- i = 0;\r
- }\r
- else /* compute a word index for the empty buffer positions */\r
- i = (i >> 2) + 1;\r
-\r
- while(i < 14) /* and zero pad all but last two positions */\r
- ctx->wbuf[i++] = 0;\r
-\r
- /* the following 32-bit length fields are assembled in the */\r
- /* wrong byte order on little endian machines but this is */\r
- /* corrected later since they are only ever used as 32-bit */\r
- /* word values. */\r
-\r
- ctx->wbuf[14] = (ctx->count[1] << 3) | (ctx->count[0] >> 29);\r
- ctx->wbuf[15] = ctx->count[0] << 3;\r
-\r
- sha256_compile(ctx);\r
-\r
- /* extract the hash value as bytes in case the hash buffer is */\r
- /* mislaigned for 32-bit words */\r
- for(i = 0; i < SHA256_DIGEST_SIZE; ++i)\r
- hval[i] = (unsigned char)(ctx->hash[i >> 2] >> (8 * (~i & 3)));\r
-}\r
-\r
-sha2_void sha256(unsigned char hval[], const unsigned char data[], unsigned long len)\r
-{ sha256_ctx cx[1];\r
-\r
- sha256_begin(cx); sha256_hash(data, len, cx); sha256_end(hval, cx);\r
-}\r
-\r
-#endif\r
-\r
-#if defined(SHA_2) || defined(SHA_384) || defined(SHA_512)\r
-\r
-#define SHA512_MASK (SHA512_BLOCK_SIZE - 1)\r
-\r
-#define rotr64(x,n) (((x) >> n) | ((x) << (64 - n)))\r
-\r
-#if !defined(bswap_64)\r
-#define bswap_64(x) ((((sha2_64t)(bswap_32((sha2_32t)(x)))) << 32) | (bswap_32((sha2_32t)((x) >> 32))))\r
-#endif\r
-\r
-#if defined(SWAP_BYTES)\r
-#define bsw_64(p,n) { int _i = (n); while(_i--) p[_i] = bswap_64(p[_i]); }\r
-#else\r
-#define bsw_64(p,n)\r
-#endif\r
-\r
-/* SHA512 mixing function definitions */\r
-\r
-#define s512_0(x) (rotr64((x), 28) ^ rotr64((x), 34) ^ rotr64((x), 39))\r
-#define s512_1(x) (rotr64((x), 14) ^ rotr64((x), 18) ^ rotr64((x), 41))\r
-#define g512_0(x) (rotr64((x), 1) ^ rotr64((x), 8) ^ ((x) >> 7))\r
-#define g512_1(x) (rotr64((x), 19) ^ rotr64((x), 61) ^ ((x) >> 6))\r
-\r
-/* rotated SHA512 round definition. Rather than swapping variables as in */\r
-/* FIPS-180, different variables are 'rotated' on each round, returning */\r
-/* to their starting positions every eight rounds */\r
-\r
-#define h5(i) ctx->wbuf[i & 15] += \\r
- g512_1(ctx->wbuf[(i + 14) & 15]) + ctx->wbuf[(i + 9) & 15] + g512_0(ctx->wbuf[(i + 1) & 15])\r
-\r
-#define h5_cycle(i,j) \\r
- v[(7 - i) & 7] += (j ? h5(i) : ctx->wbuf[i & 15]) + k512[i + j] \\r
- + s512_1(v[(4 - i) & 7]) + ch(v[(4 - i) & 7], v[(5 - i) & 7], v[(6 - i) & 7]); \\r
- v[(3 - i) & 7] += v[(7 - i) & 7]; \\r
- v[(7 - i) & 7] += s512_0(v[(0 - i) & 7]) + maj(v[(0 - i) & 7], v[(1 - i) & 7], v[(2 - i) & 7])\r
-\r
-/* SHA384/SHA512 mixing data */\r
-\r
-const sha2_64t k512[80] =\r
-{\r
- n_u64(428a2f98d728ae22), n_u64(7137449123ef65cd),\r
- n_u64(b5c0fbcfec4d3b2f), n_u64(e9b5dba58189dbbc),\r
- n_u64(3956c25bf348b538), n_u64(59f111f1b605d019),\r
- n_u64(923f82a4af194f9b), n_u64(ab1c5ed5da6d8118),\r
- n_u64(d807aa98a3030242), n_u64(12835b0145706fbe),\r
- n_u64(243185be4ee4b28c), n_u64(550c7dc3d5ffb4e2),\r
- n_u64(72be5d74f27b896f), n_u64(80deb1fe3b1696b1),\r
- n_u64(9bdc06a725c71235), n_u64(c19bf174cf692694),\r
- n_u64(e49b69c19ef14ad2), n_u64(efbe4786384f25e3),\r
- n_u64(0fc19dc68b8cd5b5), n_u64(240ca1cc77ac9c65),\r
- n_u64(2de92c6f592b0275), n_u64(4a7484aa6ea6e483),\r
- n_u64(5cb0a9dcbd41fbd4), n_u64(76f988da831153b5),\r
- n_u64(983e5152ee66dfab), n_u64(a831c66d2db43210),\r
- n_u64(b00327c898fb213f), n_u64(bf597fc7beef0ee4),\r
- n_u64(c6e00bf33da88fc2), n_u64(d5a79147930aa725),\r
- n_u64(06ca6351e003826f), n_u64(142929670a0e6e70),\r
- n_u64(27b70a8546d22ffc), n_u64(2e1b21385c26c926),\r
- n_u64(4d2c6dfc5ac42aed), n_u64(53380d139d95b3df),\r
- n_u64(650a73548baf63de), n_u64(766a0abb3c77b2a8),\r
- n_u64(81c2c92e47edaee6), n_u64(92722c851482353b),\r
- n_u64(a2bfe8a14cf10364), n_u64(a81a664bbc423001),\r
- n_u64(c24b8b70d0f89791), n_u64(c76c51a30654be30),\r
- n_u64(d192e819d6ef5218), n_u64(d69906245565a910),\r
- n_u64(f40e35855771202a), n_u64(106aa07032bbd1b8),\r
- n_u64(19a4c116b8d2d0c8), n_u64(1e376c085141ab53),\r
- n_u64(2748774cdf8eeb99), n_u64(34b0bcb5e19b48a8),\r
- n_u64(391c0cb3c5c95a63), n_u64(4ed8aa4ae3418acb),\r
- n_u64(5b9cca4f7763e373), n_u64(682e6ff3d6b2b8a3),\r
- n_u64(748f82ee5defb2fc), n_u64(78a5636f43172f60),\r
- n_u64(84c87814a1f0ab72), n_u64(8cc702081a6439ec),\r
- n_u64(90befffa23631e28), n_u64(a4506cebde82bde9),\r
- n_u64(bef9a3f7b2c67915), n_u64(c67178f2e372532b),\r
- n_u64(ca273eceea26619c), n_u64(d186b8c721c0c207),\r
- n_u64(eada7dd6cde0eb1e), n_u64(f57d4f7fee6ed178),\r
- n_u64(06f067aa72176fba), n_u64(0a637dc5a2c898a6),\r
- n_u64(113f9804bef90dae), n_u64(1b710b35131c471b),\r
- n_u64(28db77f523047d84), n_u64(32caab7b40c72493),\r
- n_u64(3c9ebe0a15c9bebc), n_u64(431d67c49c100d4c),\r
- n_u64(4cc5d4becb3e42b6), n_u64(597f299cfc657e2a),\r
- n_u64(5fcb6fab3ad6faec), n_u64(6c44198c4a475817)\r
-};\r
-\r
-/* Compile 64 bytes of hash data into SHA384/SHA512 digest value */\r
-\r
-sha2_void sha512_compile(sha512_ctx ctx[1])\r
-{ sha2_64t v[8];\r
- sha2_32t j;\r
-\r
- memcpy(v, ctx->hash, 8 * sizeof(sha2_64t));\r
-\r
- for(j = 0; j < 80; j += 16)\r
- {\r
- h5_cycle( 0, j); h5_cycle( 1, j); h5_cycle( 2, j); h5_cycle( 3, j);\r
- h5_cycle( 4, j); h5_cycle( 5, j); h5_cycle( 6, j); h5_cycle( 7, j);\r
- h5_cycle( 8, j); h5_cycle( 9, j); h5_cycle(10, j); h5_cycle(11, j);\r
- h5_cycle(12, j); h5_cycle(13, j); h5_cycle(14, j); h5_cycle(15, j);\r
- }\r
-\r
- ctx->hash[0] += v[0]; ctx->hash[1] += v[1]; ctx->hash[2] += v[2]; ctx->hash[3] += v[3];\r
- ctx->hash[4] += v[4]; ctx->hash[5] += v[5]; ctx->hash[6] += v[6]; ctx->hash[7] += v[7];\r
-}\r
-\r
-/* Compile 128 bytes of hash data into SHA256 digest value */\r
-/* NOTE: this routine assumes that the byte order in the */\r
-/* ctx->wbuf[] at this point is in such an order that low */\r
-/* address bytes in the ORIGINAL byte stream placed in this */\r
-/* buffer will now go to the high end of words on BOTH big */\r
-/* and little endian systems */\r
-\r
-sha2_void sha512_hash(const unsigned char data[], unsigned long len, sha512_ctx ctx[1])\r
-{ sha2_32t pos = (sha2_32t)(ctx->count[0] & SHA512_MASK),\r
- space = SHA512_BLOCK_SIZE - pos;\r
- const unsigned char *sp = data;\r
-\r
- if((ctx->count[0] += len) < len)\r
- ++(ctx->count[1]);\r
-\r
- while(len >= space) /* tranfer whole blocks while possible */\r
- {\r
- memcpy(((unsigned char*)ctx->wbuf) + pos, sp, space);\r
- sp += space; len -= space; space = SHA512_BLOCK_SIZE; pos = 0;\r
- bsw_64(ctx->wbuf, SHA512_BLOCK_SIZE >> 3);\r
- sha512_compile(ctx);\r
- }\r
-\r
- memcpy(((unsigned char*)ctx->wbuf) + pos, sp, len);\r
-}\r
-\r
-/* SHA384/512 Final padding and digest calculation */\r
-\r
-static sha2_64t m2[8] =\r
-{\r
- n_u64(0000000000000000), n_u64(ff00000000000000),\r
- n_u64(ffff000000000000), n_u64(ffffff0000000000),\r
- n_u64(ffffffff00000000), n_u64(ffffffffff000000),\r
- n_u64(ffffffffffff0000), n_u64(ffffffffffffff00)\r
-};\r
-\r
-static sha2_64t b2[8] =\r
-{\r
- n_u64(8000000000000000), n_u64(0080000000000000),\r
- n_u64(0000800000000000), n_u64(0000008000000000),\r
- n_u64(0000000080000000), n_u64(0000000000800000),\r
- n_u64(0000000000008000), n_u64(0000000000000080)\r
-};\r
-\r
-static void sha_end(unsigned char hval[], sha512_ctx ctx[1], const unsigned int hlen)\r
-{ sha2_32t i = (sha2_32t)(ctx->count[0] & SHA512_MASK);\r
-\r
- bsw_64(ctx->wbuf, (i + 7) >> 3);\r
-\r
- /* bytes in the buffer are now in an order in which references */\r
- /* to 64-bit words will put bytes with lower addresses into the */\r
- /* top of 64 bit words on BOTH big and little endian machines */\r
-\r
- /* we now need to mask valid bytes and add the padding which is */\r
- /* a single 1 bit and as many zero bits as necessary. */\r
- ctx->wbuf[i >> 3] = (ctx->wbuf[i >> 3] & m2[i & 7]) | b2[i & 7];\r
-\r
- /* we need 17 or more empty byte positions, one for the padding */\r
- /* byte (above) and sixteen for the length count. If there is */\r
- /* not enough space pad and empty the buffer */\r
- if(i > SHA512_BLOCK_SIZE - 17)\r
- {\r
- if(i < 120) ctx->wbuf[15] = 0;\r
- sha512_compile(ctx);\r
- i = 0;\r
- }\r
- else\r
- i = (i >> 3) + 1;\r
-\r
- while(i < 14)\r
- ctx->wbuf[i++] = 0;\r
-\r
- /* the following 64-bit length fields are assembled in the */\r
- /* wrong byte order on little endian machines but this is */\r
- /* corrected later since they are only ever used as 64-bit */\r
- /* word values. */\r
-\r
- ctx->wbuf[14] = (ctx->count[1] << 3) | (ctx->count[0] >> 61);\r
- ctx->wbuf[15] = ctx->count[0] << 3;\r
-\r
- sha512_compile(ctx);\r
-\r
- /* extract the hash value as bytes in case the hash buffer is */\r
- /* misaligned for 32-bit words */\r
- for(i = 0; i < hlen; ++i)\r
- hval[i] = (unsigned char)(ctx->hash[i >> 3] >> (8 * (~i & 7)));\r
-}\r
-\r
-#endif\r
-\r
-#if defined(SHA_2) || defined(SHA_384)\r
-\r
-/* SHA384 initialisation data */\r
-\r
-const sha2_64t i384[80] =\r
-{\r
- n_u64(cbbb9d5dc1059ed8), n_u64(629a292a367cd507),\r
- n_u64(9159015a3070dd17), n_u64(152fecd8f70e5939),\r
- n_u64(67332667ffc00b31), n_u64(8eb44a8768581511),\r
- n_u64(db0c2e0d64f98fa7), n_u64(47b5481dbefa4fa4)\r
-};\r
-\r
-sha2_void sha384_begin(sha384_ctx ctx[1])\r
-{\r
- ctx->count[0] = ctx->count[1] = 0;\r
- memcpy(ctx->hash, i384, 8 * sizeof(sha2_64t));\r
-}\r
-\r
-sha2_void sha384_end(unsigned char hval[], sha384_ctx ctx[1])\r
-{\r
- sha_end(hval, ctx, SHA384_DIGEST_SIZE);\r
-}\r
-\r
-sha2_void sha384(unsigned char hval[], const unsigned char data[], unsigned long len)\r
-{ sha384_ctx cx[1];\r
-\r
- sha384_begin(cx); sha384_hash(data, len, cx); sha384_end(hval, cx);\r
-}\r
-\r
-#endif\r
-\r
-#if defined(SHA_2) || defined(SHA_512)\r
-\r
-/* SHA512 initialisation data */\r
-\r
-const sha2_64t i512[80] =\r
-{\r
- n_u64(6a09e667f3bcc908), n_u64(bb67ae8584caa73b),\r
- n_u64(3c6ef372fe94f82b), n_u64(a54ff53a5f1d36f1),\r
- n_u64(510e527fade682d1), n_u64(9b05688c2b3e6c1f),\r
- n_u64(1f83d9abfb41bd6b), n_u64(5be0cd19137e2179)\r
-};\r
-\r
-sha2_void sha512_begin(sha512_ctx ctx[1])\r
-{\r
- ctx->count[0] = ctx->count[1] = 0;\r
- memcpy(ctx->hash, i512, 8 * sizeof(sha2_64t));\r
-}\r
-\r
-sha2_void sha512_end(unsigned char hval[], sha512_ctx ctx[1])\r
-{\r
- sha_end(hval, ctx, SHA512_DIGEST_SIZE);\r
-}\r
-\r
-sha2_void sha512(unsigned char hval[], const unsigned char data[], unsigned long len)\r
-{ sha512_ctx cx[1];\r
-\r
- sha512_begin(cx); sha512_hash(data, len, cx); sha512_end(hval, cx);\r
-}\r
-\r
-#endif\r
-\r
-#if defined(SHA_2)\r
-\r
-#define CTX_256(x) ((x)->uu->ctx256)\r
-#define CTX_384(x) ((x)->uu->ctx512)\r
-#define CTX_512(x) ((x)->uu->ctx512)\r
-\r
-/* SHA2 initialisation */\r
-\r
-sha2_int sha2_begin(unsigned long len, sha2_ctx ctx[1])\r
-{ unsigned long l = len;\r
- switch(len)\r
- {\r
- case 256: l = len >> 3;\r
- /* Falls through. */\r
- case 32: CTX_256(ctx)->count[0] = CTX_256(ctx)->count[1] = 0;\r
- memcpy(CTX_256(ctx)->hash, i256, 32); break;\r
- case 384: l = len >> 3;\r
- /* Falls through. */\r
- case 48: CTX_384(ctx)->count[0] = CTX_384(ctx)->count[1] = 0;\r
- memcpy(CTX_384(ctx)->hash, i384, 64); break;\r
- case 512: l = len >> 3;\r
- /* Falls through. */\r
- case 64: CTX_512(ctx)->count[0] = CTX_512(ctx)->count[1] = 0;\r
- memcpy(CTX_512(ctx)->hash, i512, 64); break;\r
- default: return SHA2_BAD;\r
- }\r
-\r
- ctx->sha2_len = l; return SHA2_GOOD;\r
-}\r
-\r
-sha2_void sha2_hash(const unsigned char data[], unsigned long len, sha2_ctx ctx[1])\r
-{\r
- switch(ctx->sha2_len)\r
- {\r
- case 32: sha256_hash(data, len, CTX_256(ctx)); return;\r
- case 48: sha384_hash(data, len, CTX_384(ctx)); return;\r
- case 64: sha512_hash(data, len, CTX_512(ctx)); return;\r
- }\r
-}\r
-\r
-sha2_void sha2_end(unsigned char hval[], sha2_ctx ctx[1])\r
-{\r
- switch(ctx->sha2_len)\r
- {\r
- case 32: sha256_end(hval, CTX_256(ctx)); return;\r
- case 48: sha_end(hval, CTX_384(ctx), SHA384_DIGEST_SIZE); return;\r
- case 64: sha_end(hval, CTX_512(ctx), SHA512_DIGEST_SIZE); return;\r
- }\r
-}\r
-\r
-sha2_int sha2(unsigned char hval[], unsigned long size,\r
- const unsigned char data[], unsigned long len)\r
-{ sha2_ctx cx[1];\r
-\r
- if(sha2_begin(size, cx) == SHA2_GOOD)\r
- {\r
- sha2_hash(data, len, cx); sha2_end(hval, cx); return SHA2_GOOD;\r
- }\r
- else\r
- return SHA2_BAD;\r
-}\r
-\r
-#endif\r
-\r
+++ /dev/null
-/*\r
- ---------------------------------------------------------------------------\r
- Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK.\r
- All rights reserved.\r
-\r
- LICENSE TERMS\r
-\r
- The free distribution and use of this software in both source and binary\r
- form is allowed (with or without changes) provided that:\r
-\r
- 1. distributions of this source code include the above copyright\r
- notice, this list of conditions and the following disclaimer;\r
-\r
- 2. distributions in binary form include the above copyright\r
- notice, this list of conditions and the following disclaimer\r
- in the documentation and/or other associated materials;\r
-\r
- 3. the copyright holder's name is not used to endorse products\r
- built using this software without specific written permission.\r
-\r
- ALTERNATIVELY, provided that this notice is retained in full, this product\r
- may be distributed under the terms of the GNU General Public License (GPL),\r
- in which case the provisions of the GPL apply INSTEAD OF those given above.\r
-\r
- DISCLAIMER\r
-\r
- This software is provided 'as is' with no explicit or implied warranties\r
- in respect of its properties, including, but not limited to, correctness\r
- and/or fitness for purpose.\r
- ---------------------------------------------------------------------------\r
- Issue Date: 26/08/2003\r
-*/\r
-\r
-#ifndef _SHA2_H\r
-#define _SHA2_H\r
-\r
-#include "irrMath.h"\r
-\r
-/* Defines for suffixes to 32 and 64 bit unsigned numeric values */\r
-\r
-#define sfx_lo(x,y) x##y\r
-#define sfx_hi(x,y) sfx_lo(x,y)\r
-#define n_u32(p) sfx_hi(0x##p,s_u32)\r
-#define n_u64(p) sfx_hi(0x##p,s_u64)\r
-\r
-/* define an unsigned 32-bit type */\r
-\r
-#if UINT_MAX == 0xffffffff\r
- typedef unsigned int sha2_32t;\r
- #define s_u32 u\r
-#elif ULONG_MAX == 0xffffffff\r
- typedef unsigned long sha2_32t;\r
- #define s_u32 ul\r
-#else\r
-#error Please define sha2_32t as an unsigned 32 bit type in sha2.h\r
-#endif\r
-\r
-/* define an unsigned 64-bit type */\r
-\r
-#if defined(_MSC_VER) || defined(__BORLANDC__)\r
-#if (_MSC_VER < 1300) || (__BORLANDC__ < 0x582)\r
- typedef unsigned __int64 sha2_64t;\r
- #define s_u64 ui64\r
-#elif ULONG_MAX == 0xffffffffffffffff\r
- typedef unsigned long sha2_64t;\r
- #define s_u64 ul\r
-#elif ULONG_MAX == 0xffffffff\r
- typedef unsigned long long sha2_64t; /* a somewhat dangerous guess */\r
- #define s_u64 ull\r
-#else\r
-#error Please define sha2_64t as an unsigned 64 bit type in sha2.h\r
-#endif\r
-#else\r
-#ifdef _IRR_SOLARIS_PLATFORM_\r
-#include <sys/int_types.h>\r
-#else\r
-#include <stdint.h>\r
-#endif\r
- typedef uint64_t sha2_64t;\r
-#if __WORDSIZE==64\r
-#define s_u64 ul\r
-#else\r
-#define s_u64 ull\r
-#endif\r
-#endif\r
-\r
-#define SHA256_DIGEST_SIZE 32\r
-#define SHA384_DIGEST_SIZE 48\r
-#define SHA512_DIGEST_SIZE 64\r
-\r
-#define SHA256_BLOCK_SIZE 64\r
-#define SHA384_BLOCK_SIZE 128\r
-#define SHA512_BLOCK_SIZE 128\r
-\r
-#define SHA2_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE\r
-\r
-#define SHA2_GOOD 0\r
-#define SHA2_BAD 1\r
-\r
-/* type to hold the SHA256 context */\r
-\r
-typedef struct\r
-{ sha2_32t count[2];\r
- sha2_32t hash[8];\r
- sha2_32t wbuf[16];\r
-} sha256_ctx;\r
-\r
-/* type to hold the SHA384/512 context */\r
-\r
-typedef struct\r
-{ sha2_64t count[2];\r
- sha2_64t hash[8];\r
- sha2_64t wbuf[16];\r
-} sha512_ctx;\r
-\r
-typedef sha512_ctx sha384_ctx;\r
-\r
-/* type to hold a SHA2 context (256/384/512) */\r
-\r
-typedef struct\r
-{ union\r
- { sha256_ctx ctx256[1];\r
- sha512_ctx ctx512[1];\r
- } uu[1];\r
- sha2_32t sha2_len;\r
-} sha2_ctx;\r
-\r
-#ifndef SHA2_DLL /* implement normal or DLL functions */\r
-#define sha2_void void\r
-#define sha2_int int\r
-#else\r
-#define sha2_void void __declspec(dllexport) _stdcall\r
-#define sha2_int int __declspec(dllexport) _stdcall\r
-#endif\r
-\r
-sha2_void sha256_compile(sha256_ctx ctx[1]);\r
-sha2_void sha512_compile(sha512_ctx ctx[1]);\r
-\r
-sha2_void sha256_begin(sha256_ctx ctx[1]);\r
-sha2_void sha256_hash(const unsigned char data[], unsigned long len, sha256_ctx ctx[1]);\r
-sha2_void sha256_end(unsigned char hval[], sha256_ctx ctx[1]);\r
-sha2_void sha256(unsigned char hval[], const unsigned char data[], unsigned long len);\r
-\r
-sha2_void sha384_begin(sha384_ctx ctx[1]);\r
-#define sha384_hash sha512_hash\r
-sha2_void sha384_end(unsigned char hval[], sha384_ctx ctx[1]);\r
-sha2_void sha384(unsigned char hval[], const unsigned char data[], unsigned long len);\r
-\r
-sha2_void sha512_begin(sha512_ctx ctx[1]);\r
-sha2_void sha512_hash(const unsigned char data[], unsigned long len, sha512_ctx ctx[1]);\r
-sha2_void sha512_end(unsigned char hval[], sha512_ctx ctx[1]);\r
-sha2_void sha512(unsigned char hval[], const unsigned char data[], unsigned long len);\r
-\r
-sha2_int sha2_begin(unsigned long size, sha2_ctx ctx[1]);\r
-sha2_void sha2_hash(const unsigned char data[], unsigned long len, sha2_ctx ctx[1]);\r
-sha2_void sha2_end(unsigned char hval[], sha2_ctx ctx[1]);\r
-sha2_int sha2(unsigned char hval[], unsigned long size, const unsigned char data[], unsigned long len);\r
-\r
-#endif\r
-\r
+++ /dev/null
-/* LzmaDec.c -- LZMA Decoder\r
-2009-09-20 : Igor Pavlov : Public domain */\r
-\r
-#include "LzmaDec.h"\r
-\r
-#include <string.h>\r
-\r
-#define kNumTopBits 24\r
-#define kTopValue ((UInt32)1 << kNumTopBits)\r
-\r
-#define kNumBitModelTotalBits 11\r
-#define kBitModelTotal (1 << kNumBitModelTotalBits)\r
-#define kNumMoveBits 5\r
-\r
-#define RC_INIT_SIZE 5\r
-\r
-#define NORMALIZE if (range < kTopValue) { range <<= 8; code = (code << 8) | (*buf++); }\r
-\r
-#define IF_BIT_0(p) ttt = *(p); NORMALIZE; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)\r
-#define UPDATE_0(p) range = bound; *(p) = (CLzmaProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits));\r
-#define UPDATE_1(p) range -= bound; code -= bound; *(p) = (CLzmaProb)(ttt - (ttt >> kNumMoveBits));\r
-#define GET_BIT2(p, i, A0, A1) IF_BIT_0(p) \\r
- { UPDATE_0(p); i = (i + i); A0; } else \\r
- { UPDATE_1(p); i = (i + i) + 1; A1; }\r
-#define GET_BIT(p, i) GET_BIT2(p, i, ; , ;)\r
-\r
-#define TREE_GET_BIT(probs, i) { GET_BIT((probs + i), i); }\r
-#define TREE_DECODE(probs, limit, i) \\r
- { i = 1; do { TREE_GET_BIT(probs, i); } while (i < limit); i -= limit; }\r
-\r
-/* #define _LZMA_SIZE_OPT */\r
-\r
-#ifdef _LZMA_SIZE_OPT\r
-#define TREE_6_DECODE(probs, i) TREE_DECODE(probs, (1 << 6), i)\r
-#else\r
-#define TREE_6_DECODE(probs, i) \\r
- { i = 1; \\r
- TREE_GET_BIT(probs, i); \\r
- TREE_GET_BIT(probs, i); \\r
- TREE_GET_BIT(probs, i); \\r
- TREE_GET_BIT(probs, i); \\r
- TREE_GET_BIT(probs, i); \\r
- TREE_GET_BIT(probs, i); \\r
- i -= 0x40; }\r
-#endif\r
-\r
-#define NORMALIZE_CHECK if (range < kTopValue) { if (buf >= bufLimit) return DUMMY_ERROR; range <<= 8; code = (code << 8) | (*buf++); }\r
-\r
-#define IF_BIT_0_CHECK(p) ttt = *(p); NORMALIZE_CHECK; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)\r
-#define UPDATE_0_CHECK range = bound;\r
-#define UPDATE_1_CHECK range -= bound; code -= bound;\r
-#define GET_BIT2_CHECK(p, i, A0, A1) IF_BIT_0_CHECK(p) \\r
- { UPDATE_0_CHECK; i = (i + i); A0; } else \\r
- { UPDATE_1_CHECK; i = (i + i) + 1; A1; }\r
-#define GET_BIT_CHECK(p, i) GET_BIT2_CHECK(p, i, ; , ;)\r
-#define TREE_DECODE_CHECK(probs, limit, i) \\r
- { i = 1; do { GET_BIT_CHECK(probs + i, i) } while (i < limit); i -= limit; }\r
-\r
-\r
-#define kNumPosBitsMax 4\r
-#define kNumPosStatesMax (1 << kNumPosBitsMax)\r
-\r
-#define kLenNumLowBits 3\r
-#define kLenNumLowSymbols (1 << kLenNumLowBits)\r
-#define kLenNumMidBits 3\r
-#define kLenNumMidSymbols (1 << kLenNumMidBits)\r
-#define kLenNumHighBits 8\r
-#define kLenNumHighSymbols (1 << kLenNumHighBits)\r
-\r
-#define LenChoice 0\r
-#define LenChoice2 (LenChoice + 1)\r
-#define LenLow (LenChoice2 + 1)\r
-#define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits))\r
-#define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits))\r
-#define kNumLenProbs (LenHigh + kLenNumHighSymbols)\r
-\r
-\r
-#define kNumStates 12\r
-#define kNumLitStates 7\r
-\r
-#define kStartPosModelIndex 4\r
-#define kEndPosModelIndex 14\r
-#define kNumFullDistances (1 << (kEndPosModelIndex >> 1))\r
-\r
-#define kNumPosSlotBits 6\r
-#define kNumLenToPosStates 4\r
-\r
-#define kNumAlignBits 4\r
-#define kAlignTableSize (1 << kNumAlignBits)\r
-\r
-#define kMatchMinLen 2\r
-#define kMatchSpecLenStart (kMatchMinLen + kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)\r
-\r
-#define IsMatch 0\r
-#define IsRep (IsMatch + (kNumStates << kNumPosBitsMax))\r
-#define IsRepG0 (IsRep + kNumStates)\r
-#define IsRepG1 (IsRepG0 + kNumStates)\r
-#define IsRepG2 (IsRepG1 + kNumStates)\r
-#define IsRep0Long (IsRepG2 + kNumStates)\r
-#define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax))\r
-#define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))\r
-#define Align (SpecPos + kNumFullDistances - kEndPosModelIndex)\r
-#define LenCoder (Align + kAlignTableSize)\r
-#define RepLenCoder (LenCoder + kNumLenProbs)\r
-#define Literal (RepLenCoder + kNumLenProbs)\r
-\r
-#define LZMA_BASE_SIZE 1846\r
-#define LZMA_LIT_SIZE 768\r
-\r
-#define LzmaProps_GetNumProbs(p) ((UInt32)LZMA_BASE_SIZE + (LZMA_LIT_SIZE << ((p)->lc + (p)->lp)))\r
-\r
-#if Literal != LZMA_BASE_SIZE\r
-StopCompilingDueBUG\r
-#endif\r
-\r
-#define LZMA_DIC_MIN (1 << 12)\r
-\r
-/* First LZMA-symbol is always decoded.\r
-And it decodes new LZMA-symbols while (buf < bufLimit), but "buf" is without last normalization\r
-Out:\r
- Result:\r
- SZ_OK - OK\r
- SZ_ERROR_DATA - Error\r
- p->remainLen:\r
- < kMatchSpecLenStart : normal remain\r
- = kMatchSpecLenStart : finished\r
- = kMatchSpecLenStart + 1 : Flush marker\r
- = kMatchSpecLenStart + 2 : State Init Marker\r
-*/\r
-\r
-static int MY_FAST_CALL LzmaDec_DecodeReal(CLzmaDec *p, SizeT limit, const Byte *bufLimit)\r
-{\r
- CLzmaProb *probs = p->probs;\r
-\r
- unsigned state = p->state;\r
- UInt32 rep0 = p->reps[0], rep1 = p->reps[1], rep2 = p->reps[2], rep3 = p->reps[3];\r
- unsigned pbMask = ((unsigned)1 << (p->prop.pb)) - 1;\r
- unsigned lpMask = ((unsigned)1 << (p->prop.lp)) - 1;\r
- unsigned lc = p->prop.lc;\r
-\r
- Byte *dic = p->dic;\r
- SizeT dicBufSize = p->dicBufSize;\r
- SizeT dicPos = p->dicPos;\r
- \r
- UInt32 processedPos = p->processedPos;\r
- UInt32 checkDicSize = p->checkDicSize;\r
- unsigned len = 0;\r
-\r
- const Byte *buf = p->buf;\r
- UInt32 range = p->range;\r
- UInt32 code = p->code;\r
-\r
- do\r
- {\r
- CLzmaProb *prob;\r
- UInt32 bound;\r
- unsigned ttt;\r
- unsigned posState = processedPos & pbMask;\r
-\r
- prob = probs + IsMatch + (state << kNumPosBitsMax) + posState;\r
- IF_BIT_0(prob)\r
- {\r
- unsigned symbol;\r
- UPDATE_0(prob);\r
- prob = probs + Literal;\r
- if (checkDicSize != 0 || processedPos != 0)\r
- prob += (LZMA_LIT_SIZE * (((processedPos & lpMask) << lc) +\r
- (dic[(dicPos == 0 ? dicBufSize : dicPos) - 1] >> (8 - lc))));\r
-\r
- if (state < kNumLitStates)\r
- {\r
- state -= (state < 4) ? state : 3;\r
- symbol = 1;\r
- do { GET_BIT(prob + symbol, symbol) } while (symbol < 0x100);\r
- }\r
- else\r
- {\r
- unsigned matchByte = p->dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];\r
- unsigned offs = 0x100;\r
- state -= (state < 10) ? 3 : 6;\r
- symbol = 1;\r
- do\r
- {\r
- unsigned bit;\r
- CLzmaProb *probLit;\r
- matchByte <<= 1;\r
- bit = (matchByte & offs);\r
- probLit = prob + offs + bit + symbol;\r
- GET_BIT2(probLit, symbol, offs &= ~bit, offs &= bit)\r
- }\r
- while (symbol < 0x100);\r
- }\r
- dic[dicPos++] = (Byte)symbol;\r
- processedPos++;\r
- continue;\r
- }\r
- else\r
- {\r
- UPDATE_1(prob);\r
- prob = probs + IsRep + state;\r
- IF_BIT_0(prob)\r
- {\r
- UPDATE_0(prob);\r
- state += kNumStates;\r
- prob = probs + LenCoder;\r
- }\r
- else\r
- {\r
- UPDATE_1(prob);\r
- if (checkDicSize == 0 && processedPos == 0)\r
- return SZ_ERROR_DATA;\r
- prob = probs + IsRepG0 + state;\r
- IF_BIT_0(prob)\r
- {\r
- UPDATE_0(prob);\r
- prob = probs + IsRep0Long + (state << kNumPosBitsMax) + posState;\r
- IF_BIT_0(prob)\r
- {\r
- UPDATE_0(prob);\r
- dic[dicPos] = dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];\r
- dicPos++;\r
- processedPos++;\r
- state = state < kNumLitStates ? 9 : 11;\r
- continue;\r
- }\r
- UPDATE_1(prob);\r
- }\r
- else\r
- {\r
- UInt32 distance;\r
- UPDATE_1(prob);\r
- prob = probs + IsRepG1 + state;\r
- IF_BIT_0(prob)\r
- {\r
- UPDATE_0(prob);\r
- distance = rep1;\r
- }\r
- else\r
- {\r
- UPDATE_1(prob);\r
- prob = probs + IsRepG2 + state;\r
- IF_BIT_0(prob)\r
- {\r
- UPDATE_0(prob);\r
- distance = rep2;\r
- }\r
- else\r
- {\r
- UPDATE_1(prob);\r
- distance = rep3;\r
- rep3 = rep2;\r
- }\r
- rep2 = rep1;\r
- }\r
- rep1 = rep0;\r
- rep0 = distance;\r
- }\r
- state = state < kNumLitStates ? 8 : 11;\r
- prob = probs + RepLenCoder;\r
- }\r
- {\r
- unsigned limit, offset;\r
- CLzmaProb *probLen = prob + LenChoice;\r
- IF_BIT_0(probLen)\r
- {\r
- UPDATE_0(probLen);\r
- probLen = prob + LenLow + (posState << kLenNumLowBits);\r
- offset = 0;\r
- limit = (1 << kLenNumLowBits);\r
- }\r
- else\r
- {\r
- UPDATE_1(probLen);\r
- probLen = prob + LenChoice2;\r
- IF_BIT_0(probLen)\r
- {\r
- UPDATE_0(probLen);\r
- probLen = prob + LenMid + (posState << kLenNumMidBits);\r
- offset = kLenNumLowSymbols;\r
- limit = (1 << kLenNumMidBits);\r
- }\r
- else\r
- {\r
- UPDATE_1(probLen);\r
- probLen = prob + LenHigh;\r
- offset = kLenNumLowSymbols + kLenNumMidSymbols;\r
- limit = (1 << kLenNumHighBits);\r
- }\r
- }\r
- TREE_DECODE(probLen, limit, len);\r
- len += offset;\r
- }\r
-\r
- if (state >= kNumStates)\r
- {\r
- UInt32 distance;\r
- prob = probs + PosSlot +\r
- ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) << kNumPosSlotBits);\r
- TREE_6_DECODE(prob, distance);\r
- if (distance >= kStartPosModelIndex)\r
- {\r
- unsigned posSlot = (unsigned)distance;\r
- int numDirectBits = (int)(((distance >> 1) - 1));\r
- distance = (2 | (distance & 1));\r
- if (posSlot < kEndPosModelIndex)\r
- {\r
- distance <<= numDirectBits;\r
- prob = probs + SpecPos + distance - posSlot - 1;\r
- {\r
- UInt32 mask = 1;\r
- unsigned i = 1;\r
- do\r
- {\r
- GET_BIT2(prob + i, i, ; , distance |= mask);\r
- mask <<= 1;\r
- }\r
- while (--numDirectBits != 0);\r
- }\r
- }\r
- else\r
- {\r
- numDirectBits -= kNumAlignBits;\r
- do\r
- {\r
- NORMALIZE\r
- range >>= 1;\r
- \r
- {\r
- UInt32 t;\r
- code -= range;\r
- t = (0 - ((UInt32)code >> 31)); /* (UInt32)((Int32)code >> 31) */\r
- distance = (distance << 1) + (t + 1);\r
- code += range & t;\r
- }\r
- /*\r
- distance <<= 1;\r
- if (code >= range)\r
- {\r
- code -= range;\r
- distance |= 1;\r
- }\r
- */\r
- }\r
- while (--numDirectBits != 0);\r
- prob = probs + Align;\r
- distance <<= kNumAlignBits;\r
- {\r
- unsigned i = 1;\r
- GET_BIT2(prob + i, i, ; , distance |= 1);\r
- GET_BIT2(prob + i, i, ; , distance |= 2);\r
- GET_BIT2(prob + i, i, ; , distance |= 4);\r
- GET_BIT2(prob + i, i, ; , distance |= 8);\r
- }\r
- if (distance == (UInt32)0xFFFFFFFF)\r
- {\r
- len += kMatchSpecLenStart;\r
- state -= kNumStates;\r
- break;\r
- }\r
- }\r
- }\r
- rep3 = rep2;\r
- rep2 = rep1;\r
- rep1 = rep0;\r
- rep0 = distance + 1;\r
- if (checkDicSize == 0)\r
- {\r
- if (distance >= processedPos)\r
- return SZ_ERROR_DATA;\r
- }\r
- else if (distance >= checkDicSize)\r
- return SZ_ERROR_DATA;\r
- state = (state < kNumStates + kNumLitStates) ? kNumLitStates : kNumLitStates + 3;\r
- }\r
-\r
- len += kMatchMinLen;\r
-\r
- if (limit == dicPos)\r
- return SZ_ERROR_DATA;\r
- {\r
- SizeT rem = limit - dicPos;\r
- unsigned curLen = ((rem < len) ? (unsigned)rem : len);\r
- SizeT pos = (dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0);\r
-\r
- processedPos += curLen;\r
-\r
- len -= curLen;\r
- if (pos + curLen <= dicBufSize)\r
- {\r
- Byte *dest = dic + dicPos;\r
- ptrdiff_t src = (ptrdiff_t)pos - (ptrdiff_t)dicPos;\r
- const Byte *lim = dest + curLen;\r
- dicPos += curLen;\r
- do\r
- *(dest) = (Byte)*(dest + src);\r
- while (++dest != lim);\r
- }\r
- else\r
- {\r
- do\r
- {\r
- dic[dicPos++] = dic[pos];\r
- if (++pos == dicBufSize)\r
- pos = 0;\r
- }\r
- while (--curLen != 0);\r
- }\r
- }\r
- }\r
- }\r
- while (dicPos < limit && buf < bufLimit);\r
- NORMALIZE;\r
- p->buf = buf;\r
- p->range = range;\r
- p->code = code;\r
- p->remainLen = len;\r
- p->dicPos = dicPos;\r
- p->processedPos = processedPos;\r
- p->reps[0] = rep0;\r
- p->reps[1] = rep1;\r
- p->reps[2] = rep2;\r
- p->reps[3] = rep3;\r
- p->state = state;\r
-\r
- return SZ_OK;\r
-}\r
-\r
-static void MY_FAST_CALL LzmaDec_WriteRem(CLzmaDec *p, SizeT limit)\r
-{\r
- if (p->remainLen != 0 && p->remainLen < kMatchSpecLenStart)\r
- {\r
- Byte *dic = p->dic;\r
- SizeT dicPos = p->dicPos;\r
- SizeT dicBufSize = p->dicBufSize;\r
- unsigned len = p->remainLen;\r
- UInt32 rep0 = p->reps[0];\r
- if (limit - dicPos < len)\r
- len = (unsigned)(limit - dicPos);\r
-\r
- if (p->checkDicSize == 0 && p->prop.dicSize - p->processedPos <= len)\r
- p->checkDicSize = p->prop.dicSize;\r
-\r
- p->processedPos += len;\r
- p->remainLen -= len;\r
- while (len-- != 0)\r
- {\r
- dic[dicPos] = dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];\r
- dicPos++;\r
- }\r
- p->dicPos = dicPos;\r
- }\r
-}\r
-\r
-static int MY_FAST_CALL LzmaDec_DecodeReal2(CLzmaDec *p, SizeT limit, const Byte *bufLimit)\r
-{\r
- do\r
- {\r
- SizeT limit2 = limit;\r
- if (p->checkDicSize == 0)\r
- {\r
- UInt32 rem = p->prop.dicSize - p->processedPos;\r
- if (limit - p->dicPos > rem)\r
- limit2 = p->dicPos + rem;\r
- }\r
- RINOK(LzmaDec_DecodeReal(p, limit2, bufLimit));\r
- if (p->processedPos >= p->prop.dicSize)\r
- p->checkDicSize = p->prop.dicSize;\r
- LzmaDec_WriteRem(p, limit);\r
- }\r
- while (p->dicPos < limit && p->buf < bufLimit && p->remainLen < kMatchSpecLenStart);\r
-\r
- if (p->remainLen > kMatchSpecLenStart)\r
- {\r
- p->remainLen = kMatchSpecLenStart;\r
- }\r
- return 0;\r
-}\r
-\r
-typedef enum\r
-{\r
- DUMMY_ERROR, /* unexpected end of input stream */\r
- DUMMY_LIT,\r
- DUMMY_MATCH,\r
- DUMMY_REP\r
-} ELzmaDummy;\r
-\r
-static ELzmaDummy LzmaDec_TryDummy(const CLzmaDec *p, const Byte *buf, SizeT inSize)\r
-{\r
- UInt32 range = p->range;\r
- UInt32 code = p->code;\r
- const Byte *bufLimit = buf + inSize;\r
- CLzmaProb *probs = p->probs;\r
- unsigned state = p->state;\r
- ELzmaDummy res;\r
-\r
- {\r
- CLzmaProb *prob;\r
- UInt32 bound;\r
- unsigned ttt;\r
- unsigned posState = (p->processedPos) & ((1 << p->prop.pb) - 1);\r
-\r
- prob = probs + IsMatch + (state << kNumPosBitsMax) + posState;\r
- IF_BIT_0_CHECK(prob)\r
- {\r
- UPDATE_0_CHECK\r
-\r
- /* if (bufLimit - buf >= 7) return DUMMY_LIT; */\r
-\r
- prob = probs + Literal;\r
- if (p->checkDicSize != 0 || p->processedPos != 0)\r
- prob += (LZMA_LIT_SIZE *\r
- ((((p->processedPos) & ((1 << (p->prop.lp)) - 1)) << p->prop.lc) +\r
- (p->dic[(p->dicPos == 0 ? p->dicBufSize : p->dicPos) - 1] >> (8 - p->prop.lc))));\r
-\r
- if (state < kNumLitStates)\r
- {\r
- unsigned symbol = 1;\r
- do { GET_BIT_CHECK(prob + symbol, symbol) } while (symbol < 0x100);\r
- }\r
- else\r
- {\r
- unsigned matchByte = p->dic[p->dicPos - p->reps[0] +\r
- ((p->dicPos < p->reps[0]) ? p->dicBufSize : 0)];\r
- unsigned offs = 0x100;\r
- unsigned symbol = 1;\r
- do\r
- {\r
- unsigned bit;\r
- CLzmaProb *probLit;\r
- matchByte <<= 1;\r
- bit = (matchByte & offs);\r
- probLit = prob + offs + bit + symbol;\r
- GET_BIT2_CHECK(probLit, symbol, offs &= ~bit, offs &= bit)\r
- }\r
- while (symbol < 0x100);\r
- }\r
- res = DUMMY_LIT;\r
- }\r
- else\r
- {\r
- unsigned len;\r
- UPDATE_1_CHECK;\r
-\r
- prob = probs + IsRep + state;\r
- IF_BIT_0_CHECK(prob)\r
- {\r
- UPDATE_0_CHECK;\r
- state = 0;\r
- prob = probs + LenCoder;\r
- res = DUMMY_MATCH;\r
- }\r
- else\r
- {\r
- UPDATE_1_CHECK;\r
- res = DUMMY_REP;\r
- prob = probs + IsRepG0 + state;\r
- IF_BIT_0_CHECK(prob)\r
- {\r
- UPDATE_0_CHECK;\r
- prob = probs + IsRep0Long + (state << kNumPosBitsMax) + posState;\r
- IF_BIT_0_CHECK(prob)\r
- {\r
- UPDATE_0_CHECK;\r
- NORMALIZE_CHECK;\r
- return DUMMY_REP;\r
- }\r
- else\r
- {\r
- UPDATE_1_CHECK;\r
- }\r
- }\r
- else\r
- {\r
- UPDATE_1_CHECK;\r
- prob = probs + IsRepG1 + state;\r
- IF_BIT_0_CHECK(prob)\r
- {\r
- UPDATE_0_CHECK;\r
- }\r
- else\r
- {\r
- UPDATE_1_CHECK;\r
- prob = probs + IsRepG2 + state;\r
- IF_BIT_0_CHECK(prob)\r
- {\r
- UPDATE_0_CHECK;\r
- }\r
- else\r
- {\r
- UPDATE_1_CHECK;\r
- }\r
- }\r
- }\r
- state = kNumStates;\r
- prob = probs + RepLenCoder;\r
- }\r
- {\r
- unsigned limit, offset;\r
- CLzmaProb *probLen = prob + LenChoice;\r
- IF_BIT_0_CHECK(probLen)\r
- {\r
- UPDATE_0_CHECK;\r
- probLen = prob + LenLow + (posState << kLenNumLowBits);\r
- offset = 0;\r
- limit = 1 << kLenNumLowBits;\r
- }\r
- else\r
- {\r
- UPDATE_1_CHECK;\r
- probLen = prob + LenChoice2;\r
- IF_BIT_0_CHECK(probLen)\r
- {\r
- UPDATE_0_CHECK;\r
- probLen = prob + LenMid + (posState << kLenNumMidBits);\r
- offset = kLenNumLowSymbols;\r
- limit = 1 << kLenNumMidBits;\r
- }\r
- else\r
- {\r
- UPDATE_1_CHECK;\r
- probLen = prob + LenHigh;\r
- offset = kLenNumLowSymbols + kLenNumMidSymbols;\r
- limit = 1 << kLenNumHighBits;\r
- }\r
- }\r
- TREE_DECODE_CHECK(probLen, limit, len);\r
- len += offset;\r
- }\r
-\r
- if (state < 4)\r
- {\r
- unsigned posSlot;\r
- prob = probs + PosSlot +\r
- ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) <<\r
- kNumPosSlotBits);\r
- TREE_DECODE_CHECK(prob, 1 << kNumPosSlotBits, posSlot);\r
- if (posSlot >= kStartPosModelIndex)\r
- {\r
- int numDirectBits = ((posSlot >> 1) - 1);\r
-\r
- /* if (bufLimit - buf >= 8) return DUMMY_MATCH; */\r
-\r
- if (posSlot < kEndPosModelIndex)\r
- {\r
- prob = probs + SpecPos + ((2 | (posSlot & 1)) << numDirectBits) - posSlot - 1;\r
- }\r
- else\r
- {\r
- numDirectBits -= kNumAlignBits;\r
- do\r
- {\r
- NORMALIZE_CHECK\r
- range >>= 1;\r
- code -= range & (((code - range) >> 31) - 1);\r
- /* if (code >= range) code -= range; */\r
- }\r
- while (--numDirectBits != 0);\r
- prob = probs + Align;\r
- numDirectBits = kNumAlignBits;\r
- }\r
- {\r
- unsigned i = 1;\r
- do\r
- {\r
- GET_BIT_CHECK(prob + i, i);\r
- }\r
- while (--numDirectBits != 0);\r
- }\r
- }\r
- }\r
- }\r
- }\r
- NORMALIZE_CHECK;\r
- return res;\r
-}\r
-\r
-\r
-static void LzmaDec_InitRc(CLzmaDec *p, const Byte *data)\r
-{\r
- p->code = ((UInt32)data[1] << 24) | ((UInt32)data[2] << 16) | ((UInt32)data[3] << 8) | ((UInt32)data[4]);\r
- p->range = 0xFFFFFFFF;\r
- p->needFlush = 0;\r
-}\r
-\r
-void LzmaDec_InitDicAndState(CLzmaDec *p, Bool initDic, Bool initState)\r
-{\r
- p->needFlush = 1;\r
- p->remainLen = 0;\r
- p->tempBufSize = 0;\r
-\r
- if (initDic)\r
- {\r
- p->processedPos = 0;\r
- p->checkDicSize = 0;\r
- p->needInitState = 1;\r
- }\r
- if (initState)\r
- p->needInitState = 1;\r
-}\r
-\r
-void LzmaDec_Init(CLzmaDec *p)\r
-{\r
- p->dicPos = 0;\r
- LzmaDec_InitDicAndState(p, True, True);\r
-}\r
-\r
-static void LzmaDec_InitStateReal(CLzmaDec *p)\r
-{\r
- UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (p->prop.lc + p->prop.lp));\r
- UInt32 i;\r
- CLzmaProb *probs = p->probs;\r
- for (i = 0; i < numProbs; i++)\r
- probs[i] = kBitModelTotal >> 1;\r
- p->reps[0] = p->reps[1] = p->reps[2] = p->reps[3] = 1;\r
- p->state = 0;\r
- p->needInitState = 0;\r
-}\r
-\r
-SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit, const Byte *src, SizeT *srcLen,\r
- ELzmaFinishMode finishMode, ELzmaStatus *status)\r
-{\r
- SizeT inSize = *srcLen;\r
- (*srcLen) = 0;\r
- LzmaDec_WriteRem(p, dicLimit);\r
- \r
- *status = LZMA_STATUS_NOT_SPECIFIED;\r
-\r
- while (p->remainLen != kMatchSpecLenStart)\r
- {\r
- int checkEndMarkNow;\r
-\r
- if (p->needFlush != 0)\r
- {\r
- for (; inSize > 0 && p->tempBufSize < RC_INIT_SIZE; (*srcLen)++, inSize--)\r
- p->tempBuf[p->tempBufSize++] = *src++;\r
- if (p->tempBufSize < RC_INIT_SIZE)\r
- {\r
- *status = LZMA_STATUS_NEEDS_MORE_INPUT;\r
- return SZ_OK;\r
- }\r
- if (p->tempBuf[0] != 0)\r
- return SZ_ERROR_DATA;\r
-\r
- LzmaDec_InitRc(p, p->tempBuf);\r
- p->tempBufSize = 0;\r
- }\r
-\r
- checkEndMarkNow = 0;\r
- if (p->dicPos >= dicLimit)\r
- {\r
- if (p->remainLen == 0 && p->code == 0)\r
- {\r
- *status = LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK;\r
- return SZ_OK;\r
- }\r
- if (finishMode == LZMA_FINISH_ANY)\r
- {\r
- *status = LZMA_STATUS_NOT_FINISHED;\r
- return SZ_OK;\r
- }\r
- if (p->remainLen != 0)\r
- {\r
- *status = LZMA_STATUS_NOT_FINISHED;\r
- return SZ_ERROR_DATA;\r
- }\r
- checkEndMarkNow = 1;\r
- }\r
-\r
- if (p->needInitState)\r
- LzmaDec_InitStateReal(p);\r
- \r
- if (p->tempBufSize == 0)\r
- {\r
- SizeT processed;\r
- const Byte *bufLimit;\r
- if (inSize < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow)\r
- {\r
- int dummyRes = LzmaDec_TryDummy(p, src, inSize);\r
- if (dummyRes == DUMMY_ERROR)\r
- {\r
- memcpy(p->tempBuf, src, inSize);\r
- p->tempBufSize = (unsigned)inSize;\r
- (*srcLen) += inSize;\r
- *status = LZMA_STATUS_NEEDS_MORE_INPUT;\r
- return SZ_OK;\r
- }\r
- if (checkEndMarkNow && dummyRes != DUMMY_MATCH)\r
- {\r
- *status = LZMA_STATUS_NOT_FINISHED;\r
- return SZ_ERROR_DATA;\r
- }\r
- bufLimit = src;\r
- }\r
- else\r
- bufLimit = src + inSize - LZMA_REQUIRED_INPUT_MAX;\r
- p->buf = src;\r
- if (LzmaDec_DecodeReal2(p, dicLimit, bufLimit) != 0)\r
- return SZ_ERROR_DATA;\r
- processed = (SizeT)(p->buf - src);\r
- (*srcLen) += processed;\r
- src += processed;\r
- inSize -= processed;\r
- }\r
- else\r
- {\r
- unsigned rem = p->tempBufSize, lookAhead = 0;\r
- while (rem < LZMA_REQUIRED_INPUT_MAX && lookAhead < inSize)\r
- p->tempBuf[rem++] = src[lookAhead++];\r
- p->tempBufSize = rem;\r
- if (rem < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow)\r
- {\r
- int dummyRes = LzmaDec_TryDummy(p, p->tempBuf, rem);\r
- if (dummyRes == DUMMY_ERROR)\r
- {\r
- (*srcLen) += lookAhead;\r
- *status = LZMA_STATUS_NEEDS_MORE_INPUT;\r
- return SZ_OK;\r
- }\r
- if (checkEndMarkNow && dummyRes != DUMMY_MATCH)\r
- {\r
- *status = LZMA_STATUS_NOT_FINISHED;\r
- return SZ_ERROR_DATA;\r
- }\r
- }\r
- p->buf = p->tempBuf;\r
- if (LzmaDec_DecodeReal2(p, dicLimit, p->buf) != 0)\r
- return SZ_ERROR_DATA;\r
- lookAhead -= (rem - (unsigned)(p->buf - p->tempBuf));\r
- (*srcLen) += lookAhead;\r
- src += lookAhead;\r
- inSize -= lookAhead;\r
- p->tempBufSize = 0;\r
- }\r
- }\r
- if (p->code == 0)\r
- *status = LZMA_STATUS_FINISHED_WITH_MARK;\r
- return (p->code == 0) ? SZ_OK : SZ_ERROR_DATA;\r
-}\r
-\r
-SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status)\r
-{\r
- SizeT outSize = *destLen;\r
- SizeT inSize = *srcLen;\r
- *srcLen = *destLen = 0;\r
- for (;;)\r
- {\r
- SizeT inSizeCur = inSize, outSizeCur, dicPos;\r
- ELzmaFinishMode curFinishMode;\r
- SRes res;\r
- if (p->dicPos == p->dicBufSize)\r
- p->dicPos = 0;\r
- dicPos = p->dicPos;\r
- if (outSize > p->dicBufSize - dicPos)\r
- {\r
- outSizeCur = p->dicBufSize;\r
- curFinishMode = LZMA_FINISH_ANY;\r
- }\r
- else\r
- {\r
- outSizeCur = dicPos + outSize;\r
- curFinishMode = finishMode;\r
- }\r
-\r
- res = LzmaDec_DecodeToDic(p, outSizeCur, src, &inSizeCur, curFinishMode, status);\r
- src += inSizeCur;\r
- inSize -= inSizeCur;\r
- *srcLen += inSizeCur;\r
- outSizeCur = p->dicPos - dicPos;\r
- memcpy(dest, p->dic + dicPos, outSizeCur);\r
- dest += outSizeCur;\r
- outSize -= outSizeCur;\r
- *destLen += outSizeCur;\r
- if (res != 0)\r
- return res;\r
- if (outSizeCur == 0 || outSize == 0)\r
- return SZ_OK;\r
- }\r
-}\r
-\r
-void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc)\r
-{\r
- alloc->Free(alloc, p->probs);\r
- p->probs = 0;\r
-}\r
-\r
-static void LzmaDec_FreeDict(CLzmaDec *p, ISzAlloc *alloc)\r
-{\r
- alloc->Free(alloc, p->dic);\r
- p->dic = 0;\r
-}\r
-\r
-void LzmaDec_Free(CLzmaDec *p, ISzAlloc *alloc)\r
-{\r
- LzmaDec_FreeProbs(p, alloc);\r
- LzmaDec_FreeDict(p, alloc);\r
-}\r
-\r
-SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size)\r
-{\r
- UInt32 dicSize;\r
- Byte d;\r
- \r
- if (size < LZMA_PROPS_SIZE)\r
- return SZ_ERROR_UNSUPPORTED;\r
- else\r
- dicSize = data[1] | ((UInt32)data[2] << 8) | ((UInt32)data[3] << 16) | ((UInt32)data[4] << 24);\r
- \r
- if (dicSize < LZMA_DIC_MIN)\r
- dicSize = LZMA_DIC_MIN;\r
- p->dicSize = dicSize;\r
-\r
- d = data[0];\r
- if (d >= (9 * 5 * 5))\r
- return SZ_ERROR_UNSUPPORTED;\r
-\r
- p->lc = d % 9;\r
- d /= 9;\r
- p->pb = d / 5;\r
- p->lp = d % 5;\r
-\r
- return SZ_OK;\r
-}\r
-\r
-static SRes LzmaDec_AllocateProbs2(CLzmaDec *p, const CLzmaProps *propNew, ISzAlloc *alloc)\r
-{\r
- UInt32 numProbs = LzmaProps_GetNumProbs(propNew);\r
- if (p->probs == 0 || numProbs != p->numProbs)\r
- {\r
- LzmaDec_FreeProbs(p, alloc);\r
- p->probs = (CLzmaProb *)alloc->Alloc(alloc, numProbs * sizeof(CLzmaProb));\r
- p->numProbs = numProbs;\r
- if (p->probs == 0)\r
- return SZ_ERROR_MEM;\r
- }\r
- return SZ_OK;\r
-}\r
-\r
-SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc)\r
-{\r
- CLzmaProps propNew;\r
- RINOK(LzmaProps_Decode(&propNew, props, propsSize));\r
- RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc));\r
- p->prop = propNew;\r
- return SZ_OK;\r
-}\r
-\r
-SRes LzmaDec_Allocate(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc)\r
-{\r
- CLzmaProps propNew;\r
- SizeT dicBufSize;\r
- RINOK(LzmaProps_Decode(&propNew, props, propsSize));\r
- RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc));\r
- dicBufSize = propNew.dicSize;\r
- if (p->dic == 0 || dicBufSize != p->dicBufSize)\r
- {\r
- LzmaDec_FreeDict(p, alloc);\r
- p->dic = (Byte *)alloc->Alloc(alloc, dicBufSize);\r
- if (p->dic == 0)\r
- {\r
- LzmaDec_FreeProbs(p, alloc);\r
- return SZ_ERROR_MEM;\r
- }\r
- }\r
- p->dicBufSize = dicBufSize;\r
- p->prop = propNew;\r
- return SZ_OK;\r
-}\r
-\r
-SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,\r
- const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode,\r
- ELzmaStatus *status, ISzAlloc *alloc)\r
-{\r
- CLzmaDec p;\r
- SRes res;\r
- SizeT inSize = *srcLen;\r
- SizeT outSize = *destLen;\r
- *srcLen = *destLen = 0;\r
- if (inSize < RC_INIT_SIZE)\r
- return SZ_ERROR_INPUT_EOF;\r
-\r
- LzmaDec_Construct(&p);\r
- res = LzmaDec_AllocateProbs(&p, propData, propSize, alloc);\r
- if (res != 0)\r
- return res;\r
- p.dic = dest;\r
- p.dicBufSize = outSize;\r
-\r
- LzmaDec_Init(&p);\r
- \r
- *srcLen = inSize;\r
- res = LzmaDec_DecodeToDic(&p, outSize, src, srcLen, finishMode, status);\r
-\r
- if (res == SZ_OK && *status == LZMA_STATUS_NEEDS_MORE_INPUT)\r
- res = SZ_ERROR_INPUT_EOF;\r
-\r
- (*destLen) = p.dicPos;\r
- LzmaDec_FreeProbs(&p, alloc);\r
- return res;\r
-}\r
+++ /dev/null
-/* LzmaDec.h -- LZMA Decoder\r
-2009-02-07 : Igor Pavlov : Public domain */\r
-\r
-#ifndef __LZMA_DEC_H\r
-#define __LZMA_DEC_H\r
-\r
-#include "Types.h"\r
-\r
-#ifdef __cplusplus\r
-extern "C" {\r
-#endif\r
-\r
-/* #define _LZMA_PROB32 */\r
-/* _LZMA_PROB32 can increase the speed on some CPUs,\r
- but memory usage for CLzmaDec::probs will be doubled in that case */\r
-\r
-#ifdef _LZMA_PROB32\r
-#define CLzmaProb UInt32\r
-#else\r
-#define CLzmaProb UInt16\r
-#endif\r
-\r
-\r
-/* ---------- LZMA Properties ---------- */\r
-\r
-#define LZMA_PROPS_SIZE 5\r
-\r
-typedef struct _CLzmaProps\r
-{\r
- unsigned lc, lp, pb;\r
- UInt32 dicSize;\r
-} CLzmaProps;\r
-\r
-/* LzmaProps_Decode - decodes properties\r
-Returns:\r
- SZ_OK\r
- SZ_ERROR_UNSUPPORTED - Unsupported properties\r
-*/\r
-\r
-SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size);\r
-\r
-\r
-/* ---------- LZMA Decoder state ---------- */\r
-\r
-/* LZMA_REQUIRED_INPUT_MAX = number of required input bytes for worst case.\r
- Num bits = log2((2^11 / 31) ^ 22) + 26 < 134 + 26 = 160; */\r
-\r
-#define LZMA_REQUIRED_INPUT_MAX 20\r
-\r
-typedef struct\r
-{\r
- CLzmaProps prop;\r
- CLzmaProb *probs;\r
- Byte *dic;\r
- const Byte *buf;\r
- UInt32 range, code;\r
- SizeT dicPos;\r
- SizeT dicBufSize;\r
- UInt32 processedPos;\r
- UInt32 checkDicSize;\r
- unsigned state;\r
- UInt32 reps[4];\r
- unsigned remainLen;\r
- int needFlush;\r
- int needInitState;\r
- UInt32 numProbs;\r
- unsigned tempBufSize;\r
- Byte tempBuf[LZMA_REQUIRED_INPUT_MAX];\r
-} CLzmaDec;\r
-\r
-#define LzmaDec_Construct(p) { (p)->dic = 0; (p)->probs = 0; }\r
-\r
-void LzmaDec_Init(CLzmaDec *p);\r
-\r
-/* There are two types of LZMA streams:\r
- 0) Stream with end mark. That end mark adds about 6 bytes to compressed size.\r
- 1) Stream without end mark. You must know exact uncompressed size to decompress such stream. */\r
-\r
-typedef enum\r
-{\r
- LZMA_FINISH_ANY, /* finish at any point */\r
- LZMA_FINISH_END /* block must be finished at the end */\r
-} ELzmaFinishMode;\r
-\r
-/* ELzmaFinishMode has meaning only if the decoding reaches output limit !!!\r
-\r
- You must use LZMA_FINISH_END, when you know that current output buffer\r
- covers last bytes of block. In other cases you must use LZMA_FINISH_ANY.\r
-\r
- If LZMA decoder sees end marker before reaching output limit, it returns SZ_OK,\r
- and output value of destLen will be less than output buffer size limit.\r
- You can check status result also.\r
-\r
- You can use multiple checks to test data integrity after full decompression:\r
- 1) Check Result and "status" variable.\r
- 2) Check that output(destLen) = uncompressedSize, if you know real uncompressedSize.\r
- 3) Check that output(srcLen) = compressedSize, if you know real compressedSize.\r
- You must use correct finish mode in that case. */\r
-\r
-typedef enum\r
-{\r
- LZMA_STATUS_NOT_SPECIFIED, /* use main error code instead */\r
- LZMA_STATUS_FINISHED_WITH_MARK, /* stream was finished with end mark. */\r
- LZMA_STATUS_NOT_FINISHED, /* stream was not finished */\r
- LZMA_STATUS_NEEDS_MORE_INPUT, /* you must provide more input bytes */\r
- LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK /* there is probability that stream was finished without end mark */\r
-} ELzmaStatus;\r
-\r
-/* ELzmaStatus is used only as output value for function call */\r
-\r
-\r
-/* ---------- Interfaces ---------- */\r
-\r
-/* There are 3 levels of interfaces:\r
- 1) Dictionary Interface\r
- 2) Buffer Interface\r
- 3) One Call Interface\r
- You can select any of these interfaces, but don't mix functions from different\r
- groups for same object. */\r
-\r
-\r
-/* There are two variants to allocate state for Dictionary Interface:\r
- 1) LzmaDec_Allocate / LzmaDec_Free\r
- 2) LzmaDec_AllocateProbs / LzmaDec_FreeProbs\r
- You can use variant 2, if you set dictionary buffer manually.\r
- For Buffer Interface you must always use variant 1.\r
-\r
-LzmaDec_Allocate* can return:\r
- SZ_OK\r
- SZ_ERROR_MEM - Memory allocation error\r
- SZ_ERROR_UNSUPPORTED - Unsupported properties\r
-*/\r
- \r
-SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc);\r
-void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc);\r
-\r
-SRes LzmaDec_Allocate(CLzmaDec *state, const Byte *prop, unsigned propsSize, ISzAlloc *alloc);\r
-void LzmaDec_Free(CLzmaDec *state, ISzAlloc *alloc);\r
-\r
-/* ---------- Dictionary Interface ---------- */\r
-\r
-/* You can use it, if you want to eliminate the overhead for data copying from\r
- dictionary to some other external buffer.\r
- You must work with CLzmaDec variables directly in this interface.\r
-\r
- STEPS:\r
- LzmaDec_Constr()\r
- LzmaDec_Allocate()\r
- for (each new stream)\r
- {\r
- LzmaDec_Init()\r
- while (it needs more decompression)\r
- {\r
- LzmaDec_DecodeToDic()\r
- use data from CLzmaDec::dic and update CLzmaDec::dicPos\r
- }\r
- }\r
- LzmaDec_Free()\r
-*/\r
-\r
-/* LzmaDec_DecodeToDic\r
- \r
- The decoding to internal dictionary buffer (CLzmaDec::dic).\r
- You must manually update CLzmaDec::dicPos, if it reaches CLzmaDec::dicBufSize !!!\r
-\r
-finishMode:\r
- It has meaning only if the decoding reaches output limit (dicLimit).\r
- LZMA_FINISH_ANY - Decode just dicLimit bytes.\r
- LZMA_FINISH_END - Stream must be finished after dicLimit.\r
-\r
-Returns:\r
- SZ_OK\r
- status:\r
- LZMA_STATUS_FINISHED_WITH_MARK\r
- LZMA_STATUS_NOT_FINISHED\r
- LZMA_STATUS_NEEDS_MORE_INPUT\r
- LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK\r
- SZ_ERROR_DATA - Data error\r
-*/\r
-\r
-SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit,\r
- const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status);\r
-\r
-\r
-/* ---------- Buffer Interface ---------- */\r
-\r
-/* It's zlib-like interface.\r
- See LzmaDec_DecodeToDic description for information about STEPS and return results,\r
- but you must use LzmaDec_DecodeToBuf instead of LzmaDec_DecodeToDic and you don't need\r
- to work with CLzmaDec variables manually.\r
-\r
-finishMode:\r
- It has meaning only if the decoding reaches output limit (*destLen).\r
- LZMA_FINISH_ANY - Decode just destLen bytes.\r
- LZMA_FINISH_END - Stream must be finished after (*destLen).\r
-*/\r
-\r
-SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen,\r
- const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status);\r
-\r
-\r
-/* ---------- One Call Interface ---------- */\r
-\r
-/* LzmaDecode\r
-\r
-finishMode:\r
- It has meaning only if the decoding reaches output limit (*destLen).\r
- LZMA_FINISH_ANY - Decode just destLen bytes.\r
- LZMA_FINISH_END - Stream must be finished after (*destLen).\r
-\r
-Returns:\r
- SZ_OK\r
- status:\r
- LZMA_STATUS_FINISHED_WITH_MARK\r
- LZMA_STATUS_NOT_FINISHED\r
- LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK\r
- SZ_ERROR_DATA - Data error\r
- SZ_ERROR_MEM - Memory allocation error\r
- SZ_ERROR_UNSUPPORTED - Unsupported properties\r
- SZ_ERROR_INPUT_EOF - It needs more bytes in input buffer (src).\r
-*/\r
-\r
-SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,\r
- const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode,\r
- ELzmaStatus *status, ISzAlloc *alloc);\r
-\r
-#ifdef __cplusplus\r
-}\r
-#endif\r
-\r
-#endif\r
+++ /dev/null
-/* Types.h -- Basic types\r
-2010-10-09 : Igor Pavlov : Public domain */\r
-\r
-#ifndef __7Z_TYPES_H\r
-#define __7Z_TYPES_H\r
-\r
-#include <stddef.h>\r
-\r
-#ifdef _WIN32\r
-#include <windows.h>\r
-#endif\r
-\r
-#ifndef EXTERN_C_BEGIN\r
-#ifdef __cplusplus\r
-#define EXTERN_C_BEGIN extern "C" {\r
-#define EXTERN_C_END }\r
-#else\r
-#define EXTERN_C_BEGIN\r
-#define EXTERN_C_END\r
-#endif\r
-#endif\r
-\r
-EXTERN_C_BEGIN\r
-\r
-#define SZ_OK 0\r
-\r
-#define SZ_ERROR_DATA 1\r
-#define SZ_ERROR_MEM 2\r
-#define SZ_ERROR_CRC 3\r
-#define SZ_ERROR_UNSUPPORTED 4\r
-#define SZ_ERROR_PARAM 5\r
-#define SZ_ERROR_INPUT_EOF 6\r
-#define SZ_ERROR_OUTPUT_EOF 7\r
-#define SZ_ERROR_READ 8\r
-#define SZ_ERROR_WRITE 9\r
-#define SZ_ERROR_PROGRESS 10\r
-#define SZ_ERROR_FAIL 11\r
-#define SZ_ERROR_THREAD 12\r
-\r
-#define SZ_ERROR_ARCHIVE 16\r
-#define SZ_ERROR_NO_ARCHIVE 17\r
-\r
-typedef int SRes;\r
-\r
-#ifdef _WIN32\r
-typedef DWORD WRes;\r
-#else\r
-typedef int WRes;\r
-#endif\r
-\r
-#ifndef RINOK\r
-#define RINOK(x) { int __result__ = (x); if (__result__ != 0) return __result__; }\r
-#endif\r
-\r
-typedef unsigned char Byte;\r
-typedef short Int16;\r
-typedef unsigned short UInt16;\r
-\r
-#ifdef _LZMA_UINT32_IS_ULONG\r
-typedef long Int32;\r
-typedef unsigned long UInt32;\r
-#else\r
-typedef int Int32;\r
-typedef unsigned int UInt32;\r
-#endif\r
-\r
-#ifdef _SZ_NO_INT_64\r
-\r
-/* define _SZ_NO_INT_64, if your compiler doesn't support 64-bit integers.\r
- NOTES: Some code will work incorrectly in that case! */\r
-\r
-typedef long Int64;\r
-typedef unsigned long UInt64;\r
-\r
-#else\r
-\r
-#if defined(_MSC_VER) || defined(__BORLANDC__)\r
-typedef __int64 Int64;\r
-typedef unsigned __int64 UInt64;\r
-#define UINT64_CONST(n) n\r
-#else\r
-typedef long long int Int64;\r
-typedef unsigned long long int UInt64;\r
-#define UINT64_CONST(n) n ## ULL\r
-#endif\r
-\r
-#endif\r
-\r
-#ifdef _LZMA_NO_SYSTEM_SIZE_T\r
-typedef UInt32 SizeT;\r
-#else\r
-typedef size_t SizeT;\r
-#endif\r
-\r
-typedef int Bool;\r
-#define True 1\r
-#define False 0\r
-\r
-\r
-#ifdef _WIN32\r
-#define MY_STD_CALL __stdcall\r
-#else\r
-#define MY_STD_CALL\r
-#endif\r
-\r
-#ifdef _MSC_VER\r
-\r
-#if _MSC_VER >= 1300\r
-#define MY_NO_INLINE __declspec(noinline)\r
-#else\r
-#define MY_NO_INLINE\r
-#endif\r
-\r
-#define MY_CDECL __cdecl\r
-#define MY_FAST_CALL __fastcall\r
-\r
-#else\r
-\r
-#define MY_CDECL\r
-#define MY_FAST_CALL\r
-\r
-#endif\r
-\r
-\r
-/* The following interfaces use first parameter as pointer to structure */\r
-\r
-typedef struct\r
-{\r
- Byte (*Read)(void *p); /* reads one byte, returns 0 in case of EOF or error */\r
-} IByteIn;\r
-\r
-typedef struct\r
-{\r
- void (*Write)(void *p, Byte b);\r
-} IByteOut;\r
-\r
-typedef struct\r
-{\r
- SRes (*Read)(void *p, void *buf, size_t *size);\r
- /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream.\r
- (output(*size) < input(*size)) is allowed */\r
-} ISeqInStream;\r
-\r
-/* it can return SZ_ERROR_INPUT_EOF */\r
-SRes SeqInStream_Read(ISeqInStream *stream, void *buf, size_t size);\r
-SRes SeqInStream_Read2(ISeqInStream *stream, void *buf, size_t size, SRes errorType);\r
-SRes SeqInStream_ReadByte(ISeqInStream *stream, Byte *buf);\r
-\r
-typedef struct\r
-{\r
- size_t (*Write)(void *p, const void *buf, size_t size);\r
- /* Returns: result - the number of actually written bytes.\r
- (result < size) means error */\r
-} ISeqOutStream;\r
-\r
-typedef enum\r
-{\r
- SZ_SEEK_SET = 0,\r
- SZ_SEEK_CUR = 1,\r
- SZ_SEEK_END = 2\r
-} ESzSeek;\r
-\r
-typedef struct\r
-{\r
- SRes (*Read)(void *p, void *buf, size_t *size); /* same as ISeqInStream::Read */\r
- SRes (*Seek)(void *p, Int64 *pos, ESzSeek origin);\r
-} ISeekInStream;\r
-\r
-typedef struct\r
-{\r
- SRes (*Look)(void *p, const void **buf, size_t *size);\r
- /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream.\r
- (output(*size) > input(*size)) is not allowed\r
- (output(*size) < input(*size)) is allowed */\r
- SRes (*Skip)(void *p, size_t offset);\r
- /* offset must be <= output(*size) of Look */\r
-\r
- SRes (*Read)(void *p, void *buf, size_t *size);\r
- /* reads directly (without buffer). It's same as ISeqInStream::Read */\r
- SRes (*Seek)(void *p, Int64 *pos, ESzSeek origin);\r
-} ILookInStream;\r
-\r
-SRes LookInStream_LookRead(ILookInStream *stream, void *buf, size_t *size);\r
-SRes LookInStream_SeekTo(ILookInStream *stream, UInt64 offset);\r
-\r
-/* reads via ILookInStream::Read */\r
-SRes LookInStream_Read2(ILookInStream *stream, void *buf, size_t size, SRes errorType);\r
-SRes LookInStream_Read(ILookInStream *stream, void *buf, size_t size);\r
-\r
-#define LookToRead_BUF_SIZE (1 << 14)\r
-\r
-typedef struct\r
-{\r
- ILookInStream s;\r
- ISeekInStream *realStream;\r
- size_t pos;\r
- size_t size;\r
- Byte buf[LookToRead_BUF_SIZE];\r
-} CLookToRead;\r
-\r
-void LookToRead_CreateVTable(CLookToRead *p, int lookahead);\r
-void LookToRead_Init(CLookToRead *p);\r
-\r
-typedef struct\r
-{\r
- ISeqInStream s;\r
- ILookInStream *realStream;\r
-} CSecToLook;\r
-\r
-void SecToLook_CreateVTable(CSecToLook *p);\r
-\r
-typedef struct\r
-{\r
- ISeqInStream s;\r
- ILookInStream *realStream;\r
-} CSecToRead;\r
-\r
-void SecToRead_CreateVTable(CSecToRead *p);\r
-\r
-typedef struct\r
-{\r
- SRes (*Progress)(void *p, UInt64 inSize, UInt64 outSize);\r
- /* Returns: result. (result != SZ_OK) means break.\r
- Value (UInt64)(Int64)-1 for size means unknown value. */\r
-} ICompressProgress;\r
-\r
-typedef struct\r
-{\r
- void *(*Alloc)(void *p, size_t size);\r
- void (*Free)(void *p, void *address); /* address can be 0 */\r
-} ISzAlloc;\r
-\r
-#define IAlloc_Alloc(p, size) (p)->Alloc((p), size)\r
-#define IAlloc_Free(p, a) (p)->Free((p), a)\r
-\r
-#ifdef _WIN32\r
-\r
-#define CHAR_PATH_SEPARATOR '\\'\r
-#define WCHAR_PATH_SEPARATOR L'\\'\r
-#define STRING_PATH_SEPARATOR "\\"\r
-#define WSTRING_PATH_SEPARATOR L"\\"\r
-\r
-#else\r
-\r
-#define CHAR_PATH_SEPARATOR '/'\r
-#define WCHAR_PATH_SEPARATOR L'/'\r
-#define STRING_PATH_SEPARATOR "/"\r
-#define WSTRING_PATH_SEPARATOR L"/"\r
-\r
-#endif\r
-\r
-EXTERN_C_END\r
-\r
-#endif\r