Fabcoin Core  0.16.2
P2P Digital Currency
3way.cpp
Go to the documentation of this file.
1 // 3way.cpp - modifed by Wei Dai from Joan Daemen's 3way.c
2 // The original code and all modifications are in the public domain.
3 
4 #include "pch.h"
5 #include "3way.h"
6 #include "misc.h"
7 
9 
10 #if defined(CRYPTOPP_DEBUG) && !defined(CRYPTOPP_DOXYGEN_PROCESSING)
11 void ThreeWay_TestInstantiations()
12 {
15 }
16 #endif
17 
18 namespace
19 {
20  const word32 START_E = 0x0b0b; // round constant of first encryption round
21  const word32 START_D = 0xb1b1; // round constant of first decryption round
22 }
23 
24 static inline word32 reverseBits(word32 a)
25 {
26  a = ((a & 0xAAAAAAAA) >> 1) | ((a & 0x55555555) << 1);
27  a = ((a & 0xCCCCCCCC) >> 2) | ((a & 0x33333333) << 2);
28  return ((a & 0xF0F0F0F0) >> 4) | ((a & 0x0F0F0F0F) << 4);
29 }
30 
31 #define mu(a0, a1, a2) \
32 { \
33  a1 = reverseBits(a1); \
34  word32 t = reverseBits(a0); \
35  a0 = reverseBits(a2); \
36  a2 = t; \
37 }
38 
39 #define pi_gamma_pi(a0, a1, a2) \
40 { \
41  word32 b0, b2; \
42  b2 = rotlFixed(a2, 1U); \
43  b0 = rotlFixed(a0, 22U); \
44  a0 = rotlFixed(b0 ^ (a1|(~b2)), 1U); \
45  a2 = rotlFixed(b2 ^ (b0|(~a1)), 22U);\
46  a1 ^= (b2|(~b0)); \
47 }
48 
49 // thanks to Paulo Barreto for this optimized theta()
50 #define theta(a0, a1, a2) \
51 { \
52  word32 b0, b1, c; \
53  c = a0 ^ a1 ^ a2; \
54  c = rotlFixed(c, 16U) ^ rotlFixed(c, 8U); \
55  b0 = (a0 << 24) ^ (a2 >> 8) ^ (a1 << 8) ^ (a0 >> 24); \
56  b1 = (a1 << 24) ^ (a0 >> 8) ^ (a2 << 8) ^ (a1 >> 24); \
57  a0 ^= c ^ b0; \
58  a1 ^= c ^ b1; \
59  a2 ^= c ^ (b0 >> 16) ^ (b1 << 16); \
60 }
61 
62 #define rho(a0, a1, a2) \
63 { \
64  theta(a0, a1, a2); \
65  pi_gamma_pi(a0, a1, a2); \
66 }
67 
68 void ThreeWay::Base::UncheckedSetKey(const byte *uk, unsigned int length, const NameValuePairs &params)
69 {
70  AssertValidKeyLength(length);
71 
72  m_rounds = GetRoundsAndThrowIfInvalid(params, this);
73 
74  for (unsigned int i=0; i<3; i++)
75  m_k[i] = (word32)uk[4*i+3] | ((word32)uk[4*i+2]<<8) | ((word32)uk[4*i+1]<<16) | ((word32)uk[4*i]<<24);
76 
78  {
79  theta(m_k[0], m_k[1], m_k[2]);
80  mu(m_k[0], m_k[1], m_k[2]);
81  m_k[0] = ByteReverse(m_k[0]);
82  m_k[1] = ByteReverse(m_k[1]);
83  m_k[2] = ByteReverse(m_k[2]);
84  }
85 }
86 
87 void ThreeWay::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
88 {
90 
91  word32 a0, a1, a2;
92  Block::Get(inBlock)(a0)(a1)(a2);
93 
94  word32 rc = START_E;
95 
96  for(unsigned i=0; i<m_rounds; i++)
97  {
98  a0 ^= m_k[0] ^ (rc<<16);
99  a1 ^= m_k[1];
100  a2 ^= m_k[2] ^ rc;
101  rho(a0, a1, a2);
102 
103  rc <<= 1;
104  if (rc&0x10000) rc ^= 0x11011;
105  }
106  a0 ^= m_k[0] ^ (rc<<16);
107  a1 ^= m_k[1];
108  a2 ^= m_k[2] ^ rc;
109  theta(a0, a1, a2);
110 
111  Block::Put(xorBlock, outBlock)(a0)(a1)(a2);
112 }
113 
114 void ThreeWay::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
115 {
117 
118  word32 a0, a1, a2;
119  Block::Get(inBlock)(a0)(a1)(a2);
120 
121  word32 rc = START_D;
122 
123  mu(a0, a1, a2);
124  for(unsigned i=0; i<m_rounds; i++)
125  {
126  a0 ^= m_k[0] ^ (rc<<16);
127  a1 ^= m_k[1];
128  a2 ^= m_k[2] ^ rc;
129  rho(a0, a1, a2);
130 
131  rc <<= 1;
132  if (rc&0x10000) rc ^= 0x11011;
133  }
134  a0 ^= m_k[0] ^ (rc<<16);
135  a1 ^= m_k[1];
136  a2 ^= m_k[2] ^ rc;
137  theta(a0, a1, a2);
138  mu(a0, a1, a2);
139 
140  Block::Put(xorBlock, outBlock)(a0)(a1)(a2);
141 }
142 
uint8_t byte
Definition: Common.h:57
Utility functions for the Crypto++ library.
PutBlock< T, B, PA > Put
Definition: misc.h:2237
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
static GetBlock< T, B, GA > Get(const void *block)
Definition: misc.h:2236
#define theta(a0, a1, a2)
Definition: 3way.cpp:50
unsigned int m_rounds
Definition: 3way.h:36
#define a(i)
#define a1
void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
Sets the key for this object without performing parameter validation.
Definition: 3way.cpp:68
virtual bool IsForwardTransformation() const =0
Determines if the cipher is being operated in its forward direction.
unsigned int GetRoundsAndThrowIfInvalid(const NameValuePairs &param, const Algorithm *alg)
Validates the number of rounds for an algorithm.
Definition: seckey.h:110
BlockGetAndPut< word32, BigEndian > Block
Definition: cast.cpp:34
void AssertValidKeyLength(size_t length) const
Validates the key length.
Definition: cryptlib.h:725
void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
Encrypt or decrypt a block.
Definition: 3way.cpp:87
#define a0
#define rho(a0, a1, a2)
Definition: 3way.cpp:62
void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
Encrypt or decrypt a block.
Definition: 3way.cpp:114
#define NAMESPACE_END
Definition: config.h:201
Classes for the 3-Way block cipher.
#define mu(a0, a1, a2)
Definition: 3way.cpp:31
FixedSizeSecBlock< word32, 3 > m_k
Definition: 3way.h:37
#define a2
unsigned int word32
Definition: config.h:231
byte ByteReverse(byte value)
Reverses bytes in a 8-bit value.
Definition: misc.h:1663
Interface for retrieving values given their names.
Definition: cryptlib.h:279