Fabcoin Core  0.16.2
P2P Digital Currency
rc6.cpp
Go to the documentation of this file.
1 // rc6.cpp - written and placed in the public domain by Sean Woods
2 // based on Wei Dai's RC5 code.
3 
4 #include "pch.h"
5 #include "rc6.h"
6 #include "misc.h"
7 #include "secblock.h"
8 
10 
11 void RC6::Base::UncheckedSetKey(const byte *k, unsigned int keylen, const NameValuePairs &params)
12 {
13  AssertValidKeyLength(keylen);
14 
15  r = GetRoundsAndThrowIfInvalid(params, this);
16  sTable.New(2*(r+2));
17 
18  static const RC6_WORD MAGIC_P = 0xb7e15163L; // magic constant P for wordsize
19  static const RC6_WORD MAGIC_Q = 0x9e3779b9L; // magic constant Q for wordsize
20  static const int U=sizeof(RC6_WORD);
21 
22  const unsigned int c = STDMAX((keylen+U-1)/U, 1U); // RC6 paper says c=1 if keylen==0
23  SecBlock<RC6_WORD> l(c);
24 
25  GetUserKey(LITTLE_ENDIAN_ORDER, l.begin(), c, k, keylen);
26 
27  sTable[0] = MAGIC_P;
28  for (unsigned j=1; j<sTable.size();j++)
29  sTable[j] = sTable[j-1] + MAGIC_Q;
30 
31  RC6_WORD a=0, b=0;
32  const unsigned n = 3*STDMAX((unsigned int)sTable.size(), c);
33 
34  for (unsigned h=0; h < n; h++)
35  {
36  a = sTable[h % sTable.size()] = rotlFixed((sTable[h % sTable.size()] + a + b), 3);
37  b = l[h % c] = rotlMod((l[h % c] + a + b), (a+b));
38  }
39 }
40 
42 
43 void RC6::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
44 {
45  const RC6_WORD *sptr = sTable;
46  RC6_WORD a, b, c, d, t, u;
47 
48  Block::Get(inBlock)(a)(b)(c)(d);
49  b += sptr[0];
50  d += sptr[1];
51  sptr += 2;
52 
53  for(unsigned i=0; i<r; i++)
54  {
55  t = rotlFixed(b*(2*b+1), 5);
56  u = rotlFixed(d*(2*d+1), 5);
57  a = rotlMod(a^t,u) + sptr[0];
58  c = rotlMod(c^u,t) + sptr[1];
59  t = a; a = b; b = c; c = d; d = t;
60  sptr += 2;
61  }
62 
63  a += sptr[0];
64  c += sptr[1];
65 
66  Block::Put(xorBlock, outBlock)(a)(b)(c)(d);
67 }
68 
69 void RC6::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
70 {
71  const RC6_WORD *sptr = sTable.end();
72  RC6_WORD a, b, c, d, t, u;
73 
74  Block::Get(inBlock)(a)(b)(c)(d);
75 
76  sptr -= 2;
77  c -= sptr[1];
78  a -= sptr[0];
79 
80  for (unsigned i=0; i < r; i++)
81  {
82  sptr -= 2;
83  t = a; a = d; d = c; c = b; b = t;
84  u = rotlFixed(d*(2*d+1), 5);
85  t = rotlFixed(b*(2*b+1), 5);
86  c = rotrMod(c-sptr[1], t) ^ u;
87  a = rotrMod(a-sptr[0], u) ^ t;
88  }
89 
90  sptr -= 2;
91  d -= sTable[1];
92  b -= sTable[0];
93 
94  Block::Put(xorBlock, outBlock)(a)(b)(c)(d);
95 }
96 
iterator end()
Provides an iterator pointing beyond the last element in the memory block.
Definition: secblock.h:507
void GetUserKey(ByteOrder order, T *out, size_t outlen, const byte *in, size_t inlen)
Definition: misc.h:1879
uint8_t byte
Definition: Common.h:57
Utility functions for the Crypto++ library.
PutBlock< T, B, PA > Put
Definition: misc.h:2237
RC6 block cipher.
Definition: rc6.h:25
T rotlFixed(T x, unsigned int y)
Performs a left rotate.
Definition: misc.h:1263
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
#define h(i)
Definition: sha.cpp:736
BlockGetAndPut< RC6::RC6_WORD, LittleEndian > Block
Definition: rc6.cpp:41
SecBlock< RC6_WORD > sTable
Definition: rc6.h:34
static GetBlock< T, B, GA > Get(const void *block)
Definition: misc.h:2236
void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
Encrypt or decrypt a block.
Definition: rc6.cpp:69
#define c(i)
unsigned int r
Definition: rc6.h:33
Access a block of memory.
Definition: misc.h:2233
Classes for the RC6 block cipher.
byte order is little-endian
Definition: cryptlib.h:126
Classes and functions for secure memory allocations.
#define a(i)
word32 RC6_WORD
Definition: rc6.h:19
#define b(i, j)
iterator begin()
Provides an iterator pointing to the first element in the memory block.
Definition: secblock.h:499
Definition: rc6.h:27
const T & STDMAX(const T &a, const T &b)
Replacement function for std::max.
Definition: misc.h:487
#define NAMESPACE_END
Definition: config.h:201
#define d(i)
Definition: sha.cpp:732
void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
Encrypt or decrypt a block.
Definition: rc6.cpp:43
T rotlMod(T x, unsigned int y)
Performs a left rotate.
Definition: misc.h:1340
T rotrMod(T x, unsigned int y)
Performs a right rotate.
Definition: misc.h:1354
Interface for retrieving values given their names.
Definition: cryptlib.h:279