/* --------------------------------------------------------------------------- Copyright (c) 1999, Dr Brian Gladman, Worcester, UK. All rights reserved. LICENSE TERMS The free distribution and use of this software is allowed (with or without changes) provided that: 1. source code distributions include the above copyright notice, this list of conditions and the following disclaimer; 2. binary distributions include the above copyright notice, this list of conditions and the following disclaimer in their documentation; 3. the name of the copyright holder is not used to endorse products built using this software without specific written permission. DISCLAIMER This software is provided 'as is' with no explicit or implied warranties in respect of its properties, including, but not limited to, correctness and/or fitness for purpose. --------------------------------------------------------------------------- My thanks to Doug Whiting and Niels Ferguson for comments that led to improvements in this implementation. Issue Date: 14th January 1999 */ /* Adapted for TrueCrypt */ #ifdef TC_WINDOWS_BOOT #pragma optimize ("tl", on) #endif #include "Twofish.h" #include "Common/Endian.h" #define Q_TABLES #define M_TABLE #if !defined (TC_MINIMIZE_CODE_SIZE) || defined (TC_WINDOWS_BOOT_TWOFISH) # define MK_TABLE # define ONE_STEP #endif /* finite field arithmetic for GF(2**8) with the modular */ /* polynomial x^8 + x^6 + x^5 + x^3 + 1 (0x169) */ #define G_M 0x0169 static u1byte tab_5b[4] = { 0, G_M >> 2, G_M >> 1, (G_M >> 1) ^ (G_M >> 2) }; static u1byte tab_ef[4] = { 0, (G_M >> 1) ^ (G_M >> 2), G_M >> 1, G_M >> 2 }; #define ffm_01(x) (x) #define ffm_5b(x) ((x) ^ ((x) >> 2) ^ tab_5b[(x) & 3]) #define ffm_ef(x) ((x) ^ ((x) >> 1) ^ ((x) >> 2) ^ tab_ef[(x) & 3]) static u1byte ror4[16] = { 0, 8, 1, 9, 2, 10, 3, 11, 4, 12, 5, 13, 6, 14, 7, 15 }; static u1byte ashx[16] = { 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12, 5, 14, 7 }; static u1byte qt0[2][16] = { { 8, 1, 7, 13, 6, 15, 3, 2, 0, 11, 5, 9, 14, 12, 10, 4 }, { 2, 8, 11, 13, 15, 7, 6, 14, 3, 1, 9, 4, 0, 10, 12, 5 } }; static u1byte qt1[2][16] = { { 14, 12, 11, 8, 1, 2, 3, 5, 15, 4, 10, 6, 7, 0, 9, 13 }, { 1, 14, 2, 11, 4, 12, 3, 7, 6, 13, 10, 5, 15, 9, 0, 8 } }; static u1byte qt2[2][16] = { { 11, 10, 5, 14, 6, 13, 9, 0, 12, 8, 15, 3, 2, 4, 7, 1 }, { 4, 12, 7, 5, 1, 6, 9, 10, 0, 14, 13, 8, 2, 11, 3, 15 } }; static u1byte qt3[2][16] = { { 13, 7, 15, 4, 1, 2, 6, 14, 9, 11, 3, 0, 8, 5, 12, 10 }, { 11, 9, 5, 1, 12, 3, 13, 14, 6, 4, 7, 15, 2, 0, 8, 10 } }; static u1byte qp(const u4byte n, const u1byte x) { u1byte a0, a1, a2, a3, a4, b0, b1, b2, b3, b4; a0 = x >> 4; b0 = x & 15; a1 = a0 ^ b0; b1 = ror4[b0] ^ ashx[a0]; a2 = qt0[n][a1]; b2 = qt1[n][b1]; a3 = a2 ^ b2; b3 = ror4[b2] ^ ashx[a2]; a4 = qt2[n][a3]; b4 = qt3[n][b3]; return (b4 << 4) | a4; }; #ifdef Q_TABLES static u4byte qt_gen = 0; static u1byte q_tab[2][256]; #define q(n,x) q_tab[n][x] static void gen_qtab(void) { u4byte i; for(i = 0; i < 256; ++i) { q(0,i) = qp(0, (u1byte)i); q(1,i) = qp(1, (u1byte)i); } }; #else #define q(n,x) qp(n, x) #endif #ifdef M_TABLE static u4byte mt_gen = 0; static u4byte m_tab[4][256]; static void gen_mtab(void) { u4byte i, f01, f5b, fef; for(i = 0; i < 256; ++i) { f01 = q(1,i); f5b = ffm_5b(f01); fef = ffm_ef(f01); m_tab[0][i] = f01 + (f5b << 8) + (fef << 16) + (fef << 24); m_tab[2][i] = f5b + (fef << 8) + (f01 << 16) + (fef << 24); f01 = q(0,i); f5b = ffm_5b(f01); fef = ffm_ef(f01); m_tab[1][i] = fef + (fef << 8) + (f5b << 16) + (f01 << 24); m_tab[3][i] = f5b + (f01 << 8) + (fef << 16) + (f5b << 24); } }; #define mds(n,x) m_tab[n][x] #else #define fm_00 ffm_01 #define fm_10 ffm_5b #define fm_20 ffm_ef #define fm_30 ffm_ef #define q_0(x) q(1,x) #define fm_01 ffm_ef #define fm_11 ffm_ef #define fm_21 ffm_5b #define fm_31 ffm_01 #define q_1(x) q(0,x) #define fm_02 ffm_5b #define fm_12 ffm_ef #define fm_22 ffm_01 #define fm_32 ffm_ef #define q_2(x) q(1,x) #define fm_03 ffm_5b #define fm_13 ffm_01 #define fm_23 ffm_ef #define fm_33 ffm_5b #define q_3(x) q(0,x) #define f_0(n,x) ((u4byte)fm_0##n(x)) #define f_1(n,x) ((u4byte)fm_1##n(x) << 8) #define f_2(n,x) ((u4byte)fm_2##n(x) << 16) #define f_3(n,x) ((u4byte)fm_3##n(x) << 24) #define mds(n,x) f_0(n,q_##n(x)) ^ f_1(n,q_##n(x)) ^ f_2(n,q_##n(x)) ^ f_3(n,q_##n(x)) #endif static u4byte h_fun(TwofishInstance *instance, const u4byte x, const u4byte key[]) { u4byte b0, b1, b2, b3; #ifndef M_TABLE u4byte m5b_b0, m5b_b1, m5b_b2, m5b_b3; u4byte mef_b0, mef_b1, mef_b2, mef_b3; #endif b0 = extract_byte(x, 0); b1 = extract_byte(x, 1); b2 = extract_byte(x, 2); b3 = extract_byte(x, 3); switch(instance->k_len) { case 4: b0 = q(1, (u1byte) b0) ^ extract_byte(key[3],0); b1 = q(0, (u1byte) b1) ^ extract_byte(key[3],1); b2 = q(0, (u1byte) b2) ^ extract_byte(key[3],2); b3 = q(1, (u1byte) b3) ^ extract_byte(key[3],3); case 3: b0 = q(1, (u1byte) b0) ^ extract_byte(key[2],0); b1 = q(1, (u1byte) b1) ^ extract_byte(key[2],1); b2 = q(0, (u1byte) b2) ^ extract_byte(key[2],2); b3 = q(0, (u1byte) b3) ^ extract_byte(key[2],3); case 2: b0 = q(0, (u1byte) (q(0, (u1byte) b0) ^ extract_byte(key[1],0))) ^ extract_byte(key[0],0); b1 = q(0, (u1byte) (q(1, (u1byte) b1) ^ extract_byte(key[1],1))) ^ extract_byte(key[0],1); b2 = q(1, (u1byte) (q(0, (u1byte) b2) ^ extract_byte(key[1],2))) ^ extract_byte(key[0],2); b3 = q(1, (u1byte) (q(1, (u1byte) b3) ^ extract_byte(key[1],3))) ^ extract_byte(key[0],3); } #ifdef M_TABLE return mds(0, b0) ^ mds(1, b1) ^ mds(2, b2) ^ mds(3, b3); #else b0 = q(
/*
Derived from source code of TrueCrypt 7.1a, which is
Copyright (c) 2008-2012 TrueCrypt Developers Association and which is governed
by the TrueCrypt License 3.0.
Modifications and additions to the original source code (contained in this file)
and all other portions of this file are Copyright (c) 2013-2017 IDRIX
and are governed by the Apache License 2.0 the full text of which is
contained in the file License.txt included in VeraCrypt binary and source
code distribution packages.
*/
#ifndef TC_HEADER_Platform_Serializable
#define TC_HEADER_Platform_Serializable
#include <stdexcept>
#include "PlatformBase.h"
#include "ForEach.h"
#include "Serializer.h"
#include "SerializerFactory.h"
namespace VeraCrypt
{
class Serializable
{
public:
virtual ~Serializable () { }
virtual void Deserialize (shared_ptr <Stream> stream) = 0;
static string DeserializeHeader (shared_ptr <Stream> stream);
static Serializable *DeserializeNew (shared_ptr <Stream> stream);
template <class T>
static shared_ptr <T> DeserializeNew (shared_ptr <Stream> stream)
{
shared_ptr <T> p (dynamic_cast <T *> (DeserializeNew (stream)));
if (!p)
throw std::runtime_error (SRC_POS);
return p;
}
template <class T>
static void DeserializeList (shared_ptr <Stream> stream, list < shared_ptr <T> > &dataList)
{
if (DeserializeHeader (stream) != string ("list<") + SerializerFactory::GetName (typeid (T)) + ">")
throw std::runtime_error (SRC_POS);
Serializer sr (stream);
uint64 listSize;
sr.Deserialize ("ListSize", listSize);
for (size_t i = 0; i < listSize; i++)
{
shared_ptr <T> p (dynamic_cast <T *> (DeserializeNew (stream)));
if (!p)
throw std::runtime_error (SRC_POS);
dataList.push_back (p);
}
}
virtual void Serialize (shared_ptr <Stream> stream) const;
template <class T>
static void SerializeList (shared_ptr <Stream> stream, const list < shared_ptr <T> > &dataList)
{
Serializer sr (stream);
SerializeHeader (sr, string ("list<") + SerializerFactory::GetName (typeid (T)) + ">");
sr.Serialize ("ListSize", (uint64) dataList.size());
foreach_ref (const T &item, dataList)
item.Serialize (stream);
}
static void SerializeHeader (Serializer &serializer, const string &name);
protected:
Serializable () { }
};
}
#define TC_SERIALIZABLE(TYPE) \
static Serializable *GetNewSerializable () { return new TYPE(); } \
virtual void Deserialize (shared_ptr <Stream> stream); \
virtual void Serialize (shared_ptr <Stream> stream) const
#endif // TC_HEADER_Platform_Serializable