Crypto++  5.6.3
Free C++ class library of cryptographic schemes
seal.cpp
1 // seal.cpp - written and placed in the public domain by Wei Dai
2 // updated to SEAL 3.0 by Leonard Janke
3 
4 #include "pch.h"
5 
6 #include "seal.h"
7 #include "sha.h"
8 #include "misc.h"
9 #include "secblock.h"
10 
11 NAMESPACE_BEGIN(CryptoPP)
12 
13 #if !defined(NDEBUG) && !defined(CRYPTOPP_DOXYGEN_PROCESSING)
14 void SEAL_TestInstantiations()
15 {
17 }
18 #endif
19 
20 struct SEAL_Gamma
21 {
22  SEAL_Gamma(const byte *key)
23  : H(5), Z(5), D(16), lastIndex(0xffffffff)
24  {
25  GetUserKey(BIG_ENDIAN_ORDER, H.begin(), 5, key, 20);
26  memset(D, 0, 64);
27  }
28 
29  word32 Apply(word32 i);
30 
31  SecBlock<word32> H, Z, D;
32  word32 lastIndex;
33 };
34 
35 word32 SEAL_Gamma::Apply(word32 i)
36 {
37  word32 shaIndex = i/5;
38  if (shaIndex != lastIndex)
39  {
40  memcpy(Z, H, 20);
41  D[0] = shaIndex;
42  SHA::Transform(Z, D);
43  lastIndex = shaIndex;
44  }
45  return Z[i%5];
46 }
47 
48 template <class B>
49 void SEAL_Policy<B>::CipherSetKey(const NameValuePairs &params, const byte *key, size_t length)
50 {
51  CRYPTOPP_UNUSED(length);
52  m_insideCounter = m_outsideCounter = m_startCount = 0;
53 
54  unsigned int L = params.GetIntValueWithDefault("NumberOfOutputBitsPerPositionIndex", 32*1024);
55  m_iterationsPerCount = L / 8192;
56 
57  SEAL_Gamma gamma(key);
58  unsigned int i;
59 
60  for (i=0; i<512; i++)
61  m_T[i] = gamma.Apply(i);
62 
63  for (i=0; i<256; i++)
64  m_S[i] = gamma.Apply(0x1000+i);
65 
66  m_R.New(4*(L/8192));
67 
68  for (i=0; i<m_R.size(); i++)
69  m_R[i] = gamma.Apply(0x2000+i);
70 }
71 
72 template <class B>
73 void SEAL_Policy<B>::CipherResynchronize(byte *keystreamBuffer, const byte *IV, size_t length)
74 {
75  CRYPTOPP_UNUSED(keystreamBuffer), CRYPTOPP_UNUSED(IV), CRYPTOPP_UNUSED(length);
76  assert(length==4);
77 
78  m_outsideCounter = IV ? GetWord<word32>(false, BIG_ENDIAN_ORDER, IV) : 0;
79  m_startCount = m_outsideCounter;
80  m_insideCounter = 0;
81 }
82 
83 template <class B>
84 void SEAL_Policy<B>::SeekToIteration(lword iterationCount)
85 {
86  m_outsideCounter = m_startCount + (unsigned int)(iterationCount / m_iterationsPerCount);
87  m_insideCounter = (unsigned int)(iterationCount % m_iterationsPerCount);
88 }
89 
90 template <class B>
91 void SEAL_Policy<B>::OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount)
92 {
93  word32 a, b, c, d, n1, n2, n3, n4;
94  unsigned int p, q;
95 
96  for (size_t iteration = 0; iteration < iterationCount; ++iteration)
97  {
98 #define Ttab(x) *(word32 *)((byte *)m_T.begin()+x)
99 
100  a = m_outsideCounter ^ m_R[4*m_insideCounter];
101  b = rotrFixed(m_outsideCounter, 8U) ^ m_R[4*m_insideCounter+1];
102  c = rotrFixed(m_outsideCounter, 16U) ^ m_R[4*m_insideCounter+2];
103  d = rotrFixed(m_outsideCounter, 24U) ^ m_R[4*m_insideCounter+3];
104 
105  for (unsigned int j=0; j<2; j++)
106  {
107  p = a & 0x7fc;
108  b += Ttab(p);
109  a = rotrFixed(a, 9U);
110 
111  p = b & 0x7fc;
112  c += Ttab(p);
113  b = rotrFixed(b, 9U);
114 
115  p = c & 0x7fc;
116  d += Ttab(p);
117  c = rotrFixed(c, 9U);
118 
119  p = d & 0x7fc;
120  a += Ttab(p);
121  d = rotrFixed(d, 9U);
122  }
123 
124  n1 = d, n2 = b, n3 = a, n4 = c;
125 
126  p = a & 0x7fc;
127  b += Ttab(p);
128  a = rotrFixed(a, 9U);
129 
130  p = b & 0x7fc;
131  c += Ttab(p);
132  b = rotrFixed(b, 9U);
133 
134  p = c & 0x7fc;
135  d += Ttab(p);
136  c = rotrFixed(c, 9U);
137 
138  p = d & 0x7fc;
139  a += Ttab(p);
140  d = rotrFixed(d, 9U);
141 
142  // generate 8192 bits
143  for (unsigned int i=0; i<64; i++)
144  {
145  p = a & 0x7fc;
146  a = rotrFixed(a, 9U);
147  b += Ttab(p);
148  b ^= a;
149 
150  q = b & 0x7fc;
151  b = rotrFixed(b, 9U);
152  c ^= Ttab(q);
153  c += b;
154 
155  p = (p+c) & 0x7fc;
156  c = rotrFixed(c, 9U);
157  d += Ttab(p);
158  d ^= c;
159 
160  q = (q+d) & 0x7fc;
161  d = rotrFixed(d, 9U);
162  a ^= Ttab(q);
163  a += d;
164 
165  p = (p+a) & 0x7fc;
166  b ^= Ttab(p);
167  a = rotrFixed(a, 9U);
168 
169  q = (q+b) & 0x7fc;
170  c += Ttab(q);
171  b = rotrFixed(b, 9U);
172 
173  p = (p+c) & 0x7fc;
174  d ^= Ttab(p);
175  c = rotrFixed(c, 9U);
176 
177  q = (q+d) & 0x7fc;
178  d = rotrFixed(d, 9U);
179  a += Ttab(q);
180 
181 #define SEAL_OUTPUT(x) \
182  CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, B::ToEnum(), 0, b + m_S[4*i+0]);\
183  CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, B::ToEnum(), 1, c ^ m_S[4*i+1]);\
184  CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, B::ToEnum(), 2, d + m_S[4*i+2]);\
185  CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, B::ToEnum(), 3, a ^ m_S[4*i+3]);
186 
187  CRYPTOPP_KEYSTREAM_OUTPUT_SWITCH(SEAL_OUTPUT, 4*4);
188 
189  if (i & 1)
190  {
191  a += n3;
192  b += n4;
193  c ^= n3;
194  d ^= n4;
195  }
196  else
197  {
198  a += n1;
199  b += n2;
200  c ^= n1;
201  d ^= n2;
202  }
203  }
204 
205  if (++m_insideCounter == m_iterationsPerCount)
206  {
207  ++m_outsideCounter;
208  m_insideCounter = 0;
209  }
210  }
211 
212  a = b = c = d = n1 = n2 = n3 = n4 = 0;
213  p = q = 0;
214 }
215 
216 template class SEAL_Policy<BigEndian>;
217 template class SEAL_Policy<LittleEndian>;
218 
219 NAMESPACE_END
Utility functions for the Crypto++ library.
Classes and functions for secure memory allocations.
int GetIntValueWithDefault(const char *name, int defaultValue) const
Get a named value with type int, with default.
Definition: cryptlib.h:364
Classes for SEAL stream cipher.
Classes for SHA-1 and SHA-2 family of message digests.
iterator begin()
Provides an iterator pointing to the first element in the memory block.
Definition: secblock.h:484
const char * IV()
ConstByteArrayParameter, also accepts const byte * for backwards compatibility.
Definition: argnames.h:21
Crypto++ library namespace.
T rotrFixed(T x, unsigned int y)
Performs a right rotate.
Definition: misc.h:1189
Interface for retrieving values given their names.
Definition: cryptlib.h:261