Fabcoin Core  0.16.2
P2P Digital Currency
serpent.cpp
Go to the documentation of this file.
1 // serpent.cpp - written and placed in the public domain by Wei Dai
2 
3 #include "pch.h"
4 
5 #include "serpent.h"
6 #include "secblock.h"
7 #include "misc.h"
8 
9 #include "serpentp.h"
10 
12 
13 void Serpent_KeySchedule(word32 *k, unsigned int rounds, const byte *userKey, size_t keylen)
14 {
16  GetUserKey(LITTLE_ENDIAN_ORDER, k0.begin(), 8, userKey, keylen);
17  if (keylen < 32)
18  k0[keylen/4] |= word32(1) << ((keylen%4)*8);
19 
20  word32 t = k0[7];
21  unsigned int i;
22  for (i = 0; i < 8; ++i)
23  k[i] = k0[i] = t = rotlFixed(k0[i] ^ k0[(i+3)%8] ^ k0[(i+5)%8] ^ t ^ 0x9e3779b9 ^ i, 11);
24  for (i = 8; i < 4*(rounds+1); ++i)
25  k[i] = t = rotlFixed(k[i-8] ^ k[i-5] ^ k[i-3] ^ t ^ 0x9e3779b9 ^ i, 11);
26  k -= 20;
27 
28  word32 a,b,c,d,e;
29  for (i=0; i<rounds/8; i++)
30  {
35  k += 8*4;
40  }
42 }
43 
44 void Serpent::Base::UncheckedSetKey(const byte *userKey, unsigned int keylen, const NameValuePairs &)
45 {
46  AssertValidKeyLength(keylen);
47  Serpent_KeySchedule(m_key, 32, userKey, keylen);
48 }
49 
51 
52 void Serpent::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
53 {
54  word32 a, b, c, d, e;
55 
56  Block::Get(inBlock)(a)(b)(c)(d);
57 
58  const word32 *k = m_key;
59  unsigned int i=1;
60 
61  do
62  {
70  afterS6(KX); afterS6(S7);
71 
72  if (i == 4)
73  break;
74 
75  ++i;
76  c = b;
77  b = e;
78  e = d;
79  d = a;
80  a = e;
81  k += 32;
82  beforeS0(LT);
83  }
84  while (true);
85 
86  afterS7(KX);
87 
88  Block::Put(xorBlock, outBlock)(d)(e)(b)(a);
89 }
90 
91 void Serpent::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
92 {
93  word32 a, b, c, d, e;
94 
95  Block::Get(inBlock)(a)(b)(c)(d);
96 
97  const word32 *k = m_key + 96;
98  unsigned int i=4;
99 
100  beforeI7(KX);
101  goto start;
102 
103  do
104  {
105  c = b;
106  b = d;
107  d = e;
108  k -= 32;
109  beforeI7(ILT);
110 start:
111  beforeI7(I7); afterI7(KX);
119  }
120  while (--i != 0);
121 
122  Block::Put(xorBlock, outBlock)(a)(d)(b)(e);
123 }
124 
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
#define I4(i, r0, r1, r2, r3, r4)
Definition: serpentp.h:253
Utility functions for the Crypto++ library.
PutBlock< T, B, PA > Put
Definition: misc.h:2237
#define afterS7(f)
Definition: serpentp.h:35
T rotlFixed(T x, unsigned int y)
Performs a left rotate.
Definition: misc.h:1263
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
static GetBlock< T, B, GA > Get(const void *block)
Definition: misc.h:2236
#define afterI4(f)
Definition: serpentp.h:42
void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params)
Sets the key for this object without performing parameter validation.
Definition: serpent.cpp:44
#define S0(x)
Definition: sha256.c:67
#define c(i)
#define beforeI7(f)
Definition: serpentp.h:38
#define SK(r, a, b, c, d, e)
Definition: serpentp.h:426
#define afterI1(f)
Definition: serpentp.h:45
#define afterS2(f)
Definition: serpentp.h:30
Access a block of memory.
Definition: misc.h:2233
#define S4(i, r0, r1, r2, r3, r4)
Definition: serpentp.h:229
#define I3(i, r0, r1, r2, r3, r4)
Definition: serpentp.h:207
byte order is little-endian
Definition: cryptlib.h:126
#define LT(i, a, b, c, d, e)
Definition: serpentp.h:6
#define afterI0(f)
Definition: serpentp.h:46
Classes and functions for secure memory allocations.
#define S1(x)
Definition: sha256.c:68
#define afterS6(f)
Definition: serpentp.h:34
#define a(i)
#define afterS1(f)
Definition: serpentp.h:29
#define S3(i, r0, r1, r2, r3, r4)
Definition: serpentp.h:184
#define S6(i, r0, r1, r2, r3, r4)
Definition: serpentp.h:323
#define afterI7(f)
Definition: serpentp.h:39
#define KX(r, a, b, c, d, e)
Definition: serpentp.h:414
#define k0
Definition: ripemd.cpp:18
void AssertValidKeyLength(size_t length) const
Validates the key length.
Definition: cryptlib.h:725
#define b(i, j)
BlockGetAndPut< word32, LittleEndian > Block
Definition: serpent.cpp:50
#define beforeS0(f)
Definition: serpentp.h:27
#define ILT(i, a, b, c, d, e)
Definition: serpentp.h:15
#define afterI2(f)
Definition: serpentp.h:44
#define afterI5(f)
Definition: serpentp.h:41
iterator begin()
Provides an iterator pointing to the first element in the memory block.
Definition: secblock.h:499
#define afterS3(f)
Definition: serpentp.h:31
#define I2(i, r0, r1, r2, r3, r4)
Definition: serpentp.h:161
void Serpent_KeySchedule(word32 *k, unsigned int rounds, const byte *userKey, size_t keylen)
Definition: serpent.cpp:13
#define LK(r, a, b, c, d, e)
Definition: serpentp.h:420
#define afterI3(f)
Definition: serpentp.h:43
#define I7(i, r0, r1, r2, r3, r4)
Definition: serpentp.h:390
#define I1(i, r0, r1, r2, r3, r4)
Definition: serpentp.h:118
#define I0(i, r0, r1, r2, r3, r4)
Definition: serpentp.h:73
#define afterS4(f)
Definition: serpentp.h:32
#define NAMESPACE_END
Definition: config.h:201
#define e(i)
Definition: sha.cpp:733
Classes for the Serpent block cipher.
#define d(i)
Definition: sha.cpp:732
#define afterS0(f)
Definition: serpentp.h:28
unsigned int word32
Definition: config.h:231
#define I6(i, r0, r1, r2, r3, r4)
Definition: serpentp.h:345
#define afterS5(f)
Definition: serpentp.h:33
#define S5(i, r0, r1, r2, r3, r4)
Definition: serpentp.h:277
void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
Encrypt or decrypt a block.
Definition: serpent.cpp:52
#define I5(i, r0, r1, r2, r3, r4)
Definition: serpentp.h:300
#define S7(i, r0, r1, r2, r3, r4)
Definition: serpentp.h:366
void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
Encrypt or decrypt a block.
Definition: serpent.cpp:91
#define afterI6(f)
Definition: serpentp.h:40
#define S2(i, r0, r1, r2, r3, r4)
Definition: serpentp.h:141
Interface for retrieving values given their names.
Definition: cryptlib.h:279
FixedSizeSecBlock< word32, 33 *4 > m_key
Definition: serpent.h:32