Fabcoin Core  0.16.2
P2P Digital Currency
rc5.cpp
Go to the documentation of this file.
1 // rc5.cpp - written and placed in the public domain by Wei Dai
2 
3 #include "pch.h"
4 #include "rc5.h"
5 #include "misc.h"
6 #include "secblock.h"
7 
9 
10 void RC5::Base::UncheckedSetKey(const byte *k, unsigned int keylen, const NameValuePairs &params)
11 {
12  AssertValidKeyLength(keylen);
13 
14  r = GetRoundsAndThrowIfInvalid(params, this);
15  sTable.New(2*(r+1));
16 
17  static const RC5_WORD MAGIC_P = 0xb7e15163L; // magic constant P for wordsize
18  static const RC5_WORD MAGIC_Q = 0x9e3779b9L; // magic constant Q for wordsize
19  static const int U=sizeof(RC5_WORD);
20 
21  const unsigned int c = STDMAX((keylen+U-1)/U, 1U); // RC6 paper says c=1 if keylen==0
22  SecBlock<RC5_WORD> l(c);
23 
24  GetUserKey(LITTLE_ENDIAN_ORDER, l.begin(), c, k, keylen);
25 
26  sTable[0] = MAGIC_P;
27  for (unsigned j=1; j<sTable.size();j++)
28  sTable[j] = sTable[j-1] + MAGIC_Q;
29 
30  RC5_WORD a=0, b=0;
31  const unsigned n = 3*STDMAX((unsigned int)sTable.size(), c);
32 
33  for (unsigned h=0; h < n; h++)
34  {
35  a = sTable[h % sTable.size()] = rotlFixed((sTable[h % sTable.size()] + a + b), 3);
36  b = l[h % c] = rotlMod((l[h % c] + a + b), (a+b));
37  }
38 }
39 
41 
42 void RC5::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
43 {
44  const RC5_WORD *sptr = sTable;
45  RC5_WORD a, b;
46 
47  Block::Get(inBlock)(a)(b);
48  a += sptr[0];
49  b += sptr[1];
50  sptr += 2;
51 
52  for(unsigned i=0; i<r; i++)
53  {
54  a = rotlMod(a^b,b) + sptr[2*i+0];
55  b = rotlMod(a^b,a) + sptr[2*i+1];
56  }
57 
58  Block::Put(xorBlock, outBlock)(a)(b);
59 }
60 
61 void RC5::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
62 {
63  const RC5_WORD *sptr = sTable.end();
64  RC5_WORD a, b;
65 
66  Block::Get(inBlock)(a)(b);
67 
68  for (unsigned i=0; i<r; i++)
69  {
70  sptr-=2;
71  b = rotrMod(b-sptr[1], a) ^ a;
72  a = rotrMod(a-sptr[0], b) ^ b;
73  }
74  b -= sTable[1];
75  a -= sTable[0];
76 
77  Block::Put(xorBlock, outBlock)(a)(b);
78 }
79 
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
SecBlock< RC5_WORD > sTable
Definition: rc5.h:36
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
Classes for the RC5 block cipher.
static GetBlock< T, B, GA > Get(const void *block)
Definition: misc.h:2236
BlockGetAndPut< RC5::RC5_WORD, LittleEndian > Block
Definition: rc5.cpp:40
#define c(i)
void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
Encrypt or decrypt a block.
Definition: rc5.cpp:61
Access a block of memory.
Definition: misc.h:2233
RC5 block cipher.
Definition: rc5.h:27
byte order is little-endian
Definition: cryptlib.h:126
Classes and functions for secure memory allocations.
#define a(i)
unsigned int r
Definition: rc5.h:35
#define b(i, j)
iterator begin()
Provides an iterator pointing to the first element in the memory block.
Definition: secblock.h:499
Definition: rc5.h:29
void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
Encrypt or decrypt a block.
Definition: rc5.cpp:42
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
word32 RC5_WORD
Definition: rc5.h:20
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