YAARX: Yet Another ARX Toolkit  0.1
 All Data Structures Files Functions Variables Macros Pages
speck.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012-2013 Luxembourg University,
3  * Laboratory of Algorithmics, Cryptology and Security (LACS).
4  *
5  * This file is part of the YAARX toolkit. YAARX stands for
6  * Yet Another ARX toolkit for analysis of ARX cryptographic algorithms.
7  *
8  * YAARX is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * YAARX is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with YAARX. If not, see <http://www.gnu.org/licenses/>.
20  */
27 #ifndef SPECK_H
28 #define SPECK_H
29 
30 #define SPECK_MAX_NKEY_WORDS 4
31 #define SPECK_KEY_LEN_BITS 128
32 #define SPECK_MAX_NROUNDS 34
33 #define SPECK_RIGHT_ROT_CONST 8
34 #define SPECK_LEFT_ROT_CONST 3
35 #define SPECK_RIGHT_ROT_CONST_16BITS 7
36 #define SPECK_LEFT_ROT_CONST_16BITS 2
37 //#define SPECK_P_THRES (1.0 / (double)(1UL << 3))// for WORD_SIZE 16
38 #define SPECK_P_THRES (1.0 / (double)(1UL << 16))//(1.0 / (double)(1UL << 5)) // (1.0 / (double)(1UL << 7)) <------ Speck32
39 #define SPECK_MAX_DIFF_CNT (1ULL << 22) //(1ULL << 22)//(1ULL << 16)
40 #define SPECK_NPAIRS (1ULL << 24)
41 #define SPECK_BEST_TRAIL_LOG2P -58//-40//-58
42 #define SPECK_MAX_HW 16//9//7//9//16//9//5//5//16//7//9//6//6//7//9//9//9//4
43 #define SPECK_CLUSTER_MAX_HW 9//9//7//9//7//9//7//9//7//9//9//5
44 #define SPECK_BACK_TO_HWAY 0
45 #define SPECK_GREEDY_SEARCH 0//1 <--------
46 #define SPECK_NDIFFS 2
47 #define SPECK_EPS (double)(1.0 / (double)(1ULL << 15)) // (double)(1ULL << 15))
48 #define SPECK_DEBUG 0
49 #define SPECK_TRAIL_LEN_MAX 14
50 #define SPECK_BEST_TRAILS_LATEX_FILE "speck-trails.tex"
51 #if (WORD_SIZE == 24)
52 #define SPECK_48 1 // apply special search only for the version SPECK48
53 #define SPECK_P_THRES (1.0 / (double)(1UL << 7))
54 #else
55 #define SPECK_48 0
56 #define SPECK_P_THRES (1.0 / (double)(1UL << 5))
57 #endif // #if (WORD_SIZE == 24)
58 #define SPECK_USE_PRECOMPUTED_BOUNDS 0//1 // use precomputed bounds
59 
60 #define SPECK_TRAIL_LEN 20
61 #define SPECK_LOG_FILE "speck.log"
62 
63 //#define SPECK_PDDT_MAX_HW 7
64 
66  : std::binary_function<std::array<differential_t, SPECK_NDIFFS>, std::array<differential_t, SPECK_NDIFFS>, bool>
67 {
68  bool operator()(std::array<differential_t, SPECK_NDIFFS> const& a,
69  std::array<differential_t, SPECK_NDIFFS> const& b) const
70  {
71  assert(a.size() == SPECK_NDIFFS);
72  assert(b.size() == SPECK_NDIFFS);
73 
74  bool b_equal = true;
75  uint32_t i = 0;
76  if(a.size() == b.size()) {
77  while((i != a.size()) && (i != b.size()) && (b_equal == true)) {
78  b_equal = ((a[i].dx == b[i].dx) && (a[i].dy == b[i].dy));
79  i++;
80  }
81  } else {
82  b_equal = false;
83  }
84 #if 1 // DEBUG
85  if(b_equal) {
86  assert(i == a.size());
87  assert(i == b.size());
88  };
89 #endif
90  // return boost::algorithm::iequals(x, y, std::locale());
91  return b_equal;
92  }
93 };
94 
96  : std::unary_function<std::array<differential_t, SPECK_NDIFFS>, std::size_t>
97 {
98  std::size_t operator()(std::array<differential_t, SPECK_NDIFFS> const& a) const
99  {
100  assert(a.size() == SPECK_NDIFFS);
101  std::size_t seed = 0;
102 
103  for(uint32_t i = 0; i < a.size(); i++) {
104  boost::hash_combine(seed, a[i].dx);
105  boost::hash_combine(seed, a[i].dy);
106  }
107  return seed;
108  }
109 };
110 
112  : std::binary_function<std::array<differential_t, NROUNDS>, std::array<differential_t, NROUNDS>, bool>
113 {
114  bool operator()(std::array<differential_t, NROUNDS> const& a,
115  std::array<differential_t, NROUNDS> const& b) const
116  {
117  assert(a.size() == NROUNDS);
118  assert(b.size() == NROUNDS);
119 
120  bool b_equal = true;
121  uint32_t i = 0;
122  if(a.size() == b.size()) {
123  while((i != a.size()) && (i != b.size()) && (b_equal == true)) {
124  b_equal = ((a[i].dx == b[i].dx) && (a[i].dy == b[i].dy));
125  i++;
126  }
127  } else {
128  b_equal = false;
129  }
130 #if 1 // DEBUG
131  if(b_equal) {
132  assert(i == a.size());
133  assert(i == b.size());
134  };
135 #endif
136  // return boost::algorithm::iequals(x, y, std::locale());
137  return b_equal;
138  }
139 };
140 
142  : std::unary_function<std::array<differential_t, NROUNDS>, std::size_t>
143 {
144  std::size_t operator()(std::array<differential_t, NROUNDS> const& a) const
145  {
146  assert(a.size() == NROUNDS);
147  std::size_t seed = 0;
148 
149  for(uint32_t i = 0; i < a.size(); i++) {
150  boost::hash_combine(seed, a[i].dx);
151  boost::hash_combine(seed, a[i].dy);
152  }
153  return seed;
154  }
155 };
156 
157 uint32_t speck_compute_nkeywords(uint32_t word_size, uint32_t key_size);
158 uint32_t speck_get_keysize(uint32_t word_size);
159 void speck_get_rot_const(uint32_t word_size, uint32_t* alpha, uint32_t* beta);
160 uint32_t speck_compute_nrounds(uint32_t word_size, uint32_t nkey_words);
161 void speck_key_expansion(WORD_T key[SPECK_MAX_NROUNDS], uint32_t nrounds, uint32_t nkey_words,
162  uint32_t alpha, uint32_t beta);
163 void speck_encrypt(WORD_T key[SPECK_MAX_NROUNDS], uint32_t nrounds,
164  uint32_t alpha, uint32_t beta,
165  WORD_T* x_in, WORD_T* y_in);
166 void speck_decrypt(WORD_T key[SPECK_MAX_NROUNDS], uint32_t nrounds,
167  uint32_t alpha, uint32_t beta,
168  WORD_T* x_in, WORD_T* y_in);
169 #endif // #ifndef SPECK_H
#define NROUNDS
Definition: common.hh:122
Definition: speck.hh:141
void speck_encrypt(WORD_T key[SPECK_MAX_NROUNDS], uint32_t nrounds, uint32_t alpha, uint32_t beta, WORD_T *x_in, WORD_T *y_in)
Definition: speck.cc:195
void speck_key_expansion(WORD_T key[SPECK_MAX_NROUNDS], uint32_t nrounds, uint32_t nkey_words, uint32_t alpha, uint32_t beta)
Definition: speck.cc:169
uint32_t speck_compute_nrounds(uint32_t word_size, uint32_t nkey_words)
Definition: speck.cc:112
Definition: speck.hh:65
uint32_t speck_get_keysize(uint32_t word_size)
Definition: speck.cc:66
uint32_t speck_compute_nkeywords(uint32_t word_size, uint32_t key_size)
Definition: speck.cc:40
void speck_get_rot_const(uint32_t word_size, uint32_t *alpha, uint32_t *beta)
Definition: speck.cc:95
Definition: speck.hh:95
Definition: speck.hh:111