Botan  2.4.0 Crypto and TLS for C++11
threefish.cpp
Go to the documentation of this file.
1 /*
2 * Threefish-512
3 * (C) 2013,2014,2016 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7
8 #include <botan/threefish.h>
10 #include <botan/cpuid.h>
11
12 namespace Botan {
13
14 #define THREEFISH_ROUND(X0,X1,X2,X3,X4,X5,X6,X7,ROT1,ROT2,ROT3,ROT4) \
15  do { \
16  X0 += X4; \
17  X1 += X5; \
18  X2 += X6; \
19  X3 += X7; \
20  X4 = rotl<ROT1>(X4); \
21  X5 = rotl<ROT2>(X5); \
22  X6 = rotl<ROT3>(X6); \
23  X7 = rotl<ROT4>(X7); \
24  X4 ^= X0; \
25  X5 ^= X1; \
26  X6 ^= X2; \
27  X7 ^= X3; \
28  } while(0)
29
30 #define THREEFISH_INJECT_KEY(r) \
31  do { \
32  X0 += m_K[(r ) % 9]; \
33  X1 += m_K[(r+1) % 9]; \
34  X2 += m_K[(r+2) % 9]; \
35  X3 += m_K[(r+3) % 9]; \
36  X4 += m_K[(r+4) % 9]; \
37  X5 += m_K[(r+5) % 9] + m_T[(r ) % 3]; \
38  X6 += m_K[(r+6) % 9] + m_T[(r+1) % 3]; \
39  X7 += m_K[(r+7) % 9] + (r); \
40  } while(0)
41
42 #define THREEFISH_ENC_8_ROUNDS(R1,R2) \
43  do { \
44  THREEFISH_ROUND(X0,X2,X4,X6, X1,X3,X5,X7, 46,36,19,37); \
45  THREEFISH_ROUND(X2,X4,X6,X0, X1,X7,X5,X3, 33,27,14,42); \
46  THREEFISH_ROUND(X4,X6,X0,X2, X1,X3,X5,X7, 17,49,36,39); \
47  THREEFISH_ROUND(X6,X0,X2,X4, X1,X7,X5,X3, 44, 9,54,56); \
48  THREEFISH_INJECT_KEY(R1); \
49  \
50  THREEFISH_ROUND(X0,X2,X4,X6, X1,X3,X5,X7, 39,30,34,24); \
51  THREEFISH_ROUND(X2,X4,X6,X0, X1,X7,X5,X3, 13,50,10,17); \
52  THREEFISH_ROUND(X4,X6,X0,X2, X1,X3,X5,X7, 25,29,39,43); \
53  THREEFISH_ROUND(X6,X0,X2,X4, X1,X7,X5,X3, 8,35,56,22); \
54  THREEFISH_INJECT_KEY(R2); \
55  } while(0)
56
57 void Threefish_512::skein_feedfwd(const secure_vector<uint64_t>& M,
58  const secure_vector<uint64_t>& T)
59  {
60  BOTAN_ASSERT(m_K.size() == 9, "Key was set");
61  BOTAN_ASSERT(M.size() == 8, "Single block");
62
63  m_T[0] = T[0];
64  m_T[1] = T[1];
65  m_T[2] = T[0] ^ T[1];
66
67  uint64_t X0 = M[0];
68  uint64_t X1 = M[1];
69  uint64_t X2 = M[2];
70  uint64_t X3 = M[3];
71  uint64_t X4 = M[4];
72  uint64_t X5 = M[5];
73  uint64_t X6 = M[6];
74  uint64_t X7 = M[7];
75
77
87
88  m_K[0] = M[0] ^ X0;
89  m_K[1] = M[1] ^ X1;
90  m_K[2] = M[2] ^ X2;
91  m_K[3] = M[3] ^ X3;
92  m_K[4] = M[4] ^ X4;
93  m_K[5] = M[5] ^ X5;
94  m_K[6] = M[6] ^ X6;
95  m_K[7] = M[7] ^ X7;
96
97  m_K[8] = m_K[0] ^ m_K[1] ^ m_K[2] ^ m_K[3] ^
98  m_K[4] ^ m_K[5] ^ m_K[6] ^ m_K[7] ^ 0x1BD11BDAA9FC1A22;
99  }
100
102  {
103 #if defined(BOTAN_HAS_THREEFISH_512_AVX2)
104  if(CPUID::has_avx2())
105  {
106  return 2;
107  }
108 #endif
109
110  return 1;
111  }
112
113 std::string Threefish_512::provider() const
114  {
115 #if defined(BOTAN_HAS_THREEFISH_512_AVX2)
116  if(CPUID::has_avx2())
117  {
118  return "avx2";
119  }
120 #endif
121
122  return "base";
123  }
124
125 void Threefish_512::encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const
126  {
127  verify_key_set(m_K.empty() == false);
128
129 #if defined(BOTAN_HAS_THREEFISH_512_AVX2)
130  if(CPUID::has_avx2())
131  {
132  return avx2_encrypt_n(in, out, blocks);
133  }
134 #endif
135
136  BOTAN_PARALLEL_SIMD_FOR(size_t i = 0; i < blocks; ++i)
137  {
138  uint64_t X0, X1, X2, X3, X4, X5, X6, X7;
139  load_le(in + BLOCK_SIZE*i, X0, X1, X2, X3, X4, X5, X6, X7);
140
142
148  THREEFISH_ENC_8_ROUNDS(11,12);
149  THREEFISH_ENC_8_ROUNDS(13,14);
150  THREEFISH_ENC_8_ROUNDS(15,16);
151  THREEFISH_ENC_8_ROUNDS(17,18);
152
153  store_le(out + BLOCK_SIZE*i, X0, X1, X2, X3, X4, X5, X6, X7);
154  }
155  }
156
157 #undef THREEFISH_ENC_8_ROUNDS
158 #undef THREEFISH_INJECT_KEY
159 #undef THREEFISH_ROUND
160
161 void Threefish_512::decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const
162  {
163  verify_key_set(m_K.empty() == false);
164
165 #if defined(BOTAN_HAS_THREEFISH_512_AVX2)
166  if(CPUID::has_avx2())
167  {
168  return avx2_decrypt_n(in, out, blocks);
169  }
170 #endif
171
172 #define THREEFISH_ROUND(X0,X1,X2,X3,X4,X5,X6,X7,ROT1,ROT2,ROT3,ROT4) \
173  do { \
174  X4 ^= X0; \
175  X5 ^= X1; \
176  X6 ^= X2; \
177  X7 ^= X3; \
178  X4 = rotr<ROT1>(X4); \
179  X5 = rotr<ROT2>(X5); \
180  X6 = rotr<ROT3>(X6); \
181  X7 = rotr<ROT4>(X7); \
182  X0 -= X4; \
183  X1 -= X5; \
184  X2 -= X6; \
185  X3 -= X7; \
186  } while(0)
187
188 #define THREEFISH_INJECT_KEY(r) \
189  do { \
190  X0 -= m_K[(r ) % 9]; \
191  X1 -= m_K[(r+1) % 9]; \
192  X2 -= m_K[(r+2) % 9]; \
193  X3 -= m_K[(r+3) % 9]; \
194  X4 -= m_K[(r+4) % 9]; \
195  X5 -= m_K[(r+5) % 9] + m_T[(r ) % 3]; \
196  X6 -= m_K[(r+6) % 9] + m_T[(r+1) % 3]; \
197  X7 -= m_K[(r+7) % 9] + (r); \
198  } while(0)
199
200 #define THREEFISH_DEC_8_ROUNDS(R1,R2) \
201  do { \
202  THREEFISH_ROUND(X6,X0,X2,X4, X1,X7,X5,X3, 8,35,56,22); \
203  THREEFISH_ROUND(X4,X6,X0,X2, X1,X3,X5,X7, 25,29,39,43); \
204  THREEFISH_ROUND(X2,X4,X6,X0, X1,X7,X5,X3, 13,50,10,17); \
205  THREEFISH_ROUND(X0,X2,X4,X6, X1,X3,X5,X7, 39,30,34,24); \
206  THREEFISH_INJECT_KEY(R1); \
207  \
208  THREEFISH_ROUND(X6,X0,X2,X4, X1,X7,X5,X3, 44, 9,54,56); \
209  THREEFISH_ROUND(X4,X6,X0,X2, X1,X3,X5,X7, 17,49,36,39); \
210  THREEFISH_ROUND(X2,X4,X6,X0, X1,X7,X5,X3, 33,27,14,42); \
211  THREEFISH_ROUND(X0,X2,X4,X6, X1,X3,X5,X7, 46,36,19,37); \
212  THREEFISH_INJECT_KEY(R2); \
213  } while(0)
214
215  BOTAN_PARALLEL_SIMD_FOR(size_t i = 0; i < blocks; ++i)
216  {
217  uint64_t X0, X1, X2, X3, X4, X5, X6, X7;
218  load_le(in + BLOCK_SIZE*i, X0, X1, X2, X3, X4, X5, X6, X7);
219
221
222  THREEFISH_DEC_8_ROUNDS(17,16);
223  THREEFISH_DEC_8_ROUNDS(15,14);
224  THREEFISH_DEC_8_ROUNDS(13,12);
225  THREEFISH_DEC_8_ROUNDS(11,10);
231
232  store_le(out + BLOCK_SIZE*i, X0, X1, X2, X3, X4, X5, X6, X7);
233  }
234
235 #undef THREEFISH_DEC_8_ROUNDS
236 #undef THREEFISH_INJECT_KEY
237 #undef THREEFISH_ROUND
238  }
239
240 void Threefish_512::set_tweak(const uint8_t tweak[], size_t len)
241  {
242  if(len != 16)
243  throw Exception("Threefish-512 requires 128 bit tweak");
244  m_T.resize(3);
247  m_T[2] = m_T[0] ^ m_T[1];
248  }
249
250 void Threefish_512::key_schedule(const uint8_t key[], size_t)
251  {
252  // todo: define key schedule for smaller keys
253  m_K.resize(9);
254
255  for(size_t i = 0; i != 8; ++i)
257
258  m_K[8] = m_K[0] ^ m_K[1] ^ m_K[2] ^ m_K[3] ^
259  m_K[4] ^ m_K[5] ^ m_K[6] ^ m_K[7] ^ 0x1BD11BDAA9FC1A22;
260
261  // Reset tweak to all zeros on key reset
262  m_T.resize(3);
263  zeroise(m_T);
264  }
265
267  {
268  zap(m_K);
269  zap(m_T);
270  }
271
272 }
std::string provider() const override
Definition: threefish.cpp:113
void verify_key_set(bool cond) const
Definition: sym_algo.h:95
void zap(std::vector< T, Alloc > &vec)
Definition: secmem.h:191
#define THREEFISH_INJECT_KEY(r)
Definition: threefish.cpp:30
void encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override
Definition: threefish.cpp:125
Definition: assert.h:29
void decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override
Definition: threefish.cpp:161
uint64_t load_le< uint64_t >(const uint8_t in[], size_t off)
T load_le(const uint8_t in[], size_t off)
Definition: alg_id.cpp:13
size_t parallelism() const override
Definition: threefish.cpp:101
#define THREEFISH_DEC_8_ROUNDS(R1, R2)
void set_tweak(const uint8_t tweak[], size_t len)
Definition: threefish.cpp:240
fe T
Definition: ge.cpp:37
#define BOTAN_PARALLEL_SIMD_FOR
Definition: compiler.h:189
#define THREEFISH_ENC_8_ROUNDS(R1, R2)
Definition: threefish.cpp:42
void store_le(uint16_t in, uint8_t out[2])