Coverage Report

Created: 2020-06-30 13:58

/src/botan/src/lib/block/shacal2/shacal2.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* SHACAL-2
3
* (C) 2017 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#include <botan/shacal2.h>
9
#include <botan/loadstor.h>
10
#include <botan/rotate.h>
11
#include <botan/cpuid.h>
12
13
namespace Botan {
14
15
namespace {
16
17
inline void SHACAL2_Fwd(uint32_t A, uint32_t B, uint32_t C, uint32_t& D,
18
                        uint32_t E, uint32_t F, uint32_t G, uint32_t& H,
19
                        uint32_t RK)
20
0
   {
21
0
   const uint32_t A_rho = rotr<2>(A) ^ rotr<13>(A) ^ rotr<22>(A);
22
0
   const uint32_t E_rho = rotr<6>(E) ^ rotr<11>(E) ^ rotr<25>(E);
23
0
24
0
   H += E_rho + ((E & F) ^ (~E & G)) + RK;
25
0
   D += H;
26
0
   H += A_rho + ((A & B) | ((A | B) & C));
27
0
   }
28
29
inline void SHACAL2_Rev(uint32_t A, uint32_t B, uint32_t C, uint32_t& D,
30
                        uint32_t E, uint32_t F, uint32_t G, uint32_t& H,
31
                        uint32_t RK)
32
0
   {
33
0
   const uint32_t A_rho = rotr<2>(A) ^ rotr<13>(A) ^ rotr<22>(A);
34
0
   const uint32_t E_rho = rotr<6>(E) ^ rotr<11>(E) ^ rotr<25>(E);
35
0
36
0
   H -= A_rho + ((A & B) | ((A | B) & C));
37
0
   D -= H;
38
0
   H -= E_rho + ((E & F) ^ (~E & G)) + RK;
39
0
   }
40
41
}
42
43
/*
44
* SHACAL2 Encryption
45
*/
46
void SHACAL2::encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const
47
0
   {
48
0
   verify_key_set(m_RK.empty() == false);
49
0
50
0
#if defined(BOTAN_HAS_SHACAL2_X86)
51
0
   if(CPUID::has_intel_sha())
52
0
      {
53
0
      return x86_encrypt_blocks(in, out, blocks);
54
0
      }
55
0
#endif
56
0
57
0
#if defined(BOTAN_HAS_SHACAL2_AVX2)
58
0
   if(CPUID::has_avx2())
59
0
      {
60
0
      while(blocks >= 8)
61
0
         {
62
0
         avx2_encrypt_8(in, out);
63
0
         in += 8*BLOCK_SIZE;
64
0
         out += 8*BLOCK_SIZE;
65
0
         blocks -= 8;
66
0
         }
67
0
      }
68
0
#endif
69
0
70
0
#if defined(BOTAN_HAS_SHACAL2_SIMD)
71
0
   if(CPUID::has_simd_32())
72
0
      {
73
0
      while(blocks >= 4)
74
0
         {
75
0
         simd_encrypt_4(in, out);
76
0
         in += 4*BLOCK_SIZE;
77
0
         out += 4*BLOCK_SIZE;
78
0
         blocks -= 4;
79
0
         }
80
0
      }
81
0
#endif
82
0
83
0
   for(size_t i = 0; i != blocks; ++i)
84
0
      {
85
0
      uint32_t A = load_be<uint32_t>(in, 0);
86
0
      uint32_t B = load_be<uint32_t>(in, 1);
87
0
      uint32_t C = load_be<uint32_t>(in, 2);
88
0
      uint32_t D = load_be<uint32_t>(in, 3);
89
0
      uint32_t E = load_be<uint32_t>(in, 4);
90
0
      uint32_t F = load_be<uint32_t>(in, 5);
91
0
      uint32_t G = load_be<uint32_t>(in, 6);
92
0
      uint32_t H = load_be<uint32_t>(in, 7);
93
0
94
0
      for(size_t r = 0; r != 64; r += 8)
95
0
         {
96
0
         SHACAL2_Fwd(A, B, C, D, E, F, G, H, m_RK[r+0]);
97
0
         SHACAL2_Fwd(H, A, B, C, D, E, F, G, m_RK[r+1]);
98
0
         SHACAL2_Fwd(G, H, A, B, C, D, E, F, m_RK[r+2]);
99
0
         SHACAL2_Fwd(F, G, H, A, B, C, D, E, m_RK[r+3]);
100
0
         SHACAL2_Fwd(E, F, G, H, A, B, C, D, m_RK[r+4]);
101
0
         SHACAL2_Fwd(D, E, F, G, H, A, B, C, m_RK[r+5]);
102
0
         SHACAL2_Fwd(C, D, E, F, G, H, A, B, m_RK[r+6]);
103
0
         SHACAL2_Fwd(B, C, D, E, F, G, H, A, m_RK[r+7]);
104
0
         }
105
0
106
0
      store_be(out, A, B, C, D, E, F, G, H);
107
0
108
0
      in += BLOCK_SIZE;
109
0
      out += BLOCK_SIZE;
110
0
      }
111
0
   }
112
113
/*
114
* SHACAL2 Encryption
115
*/
116
void SHACAL2::decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const
117
0
   {
118
0
   verify_key_set(m_RK.empty() == false);
119
0
120
0
#if defined(BOTAN_HAS_SHACAL2_AVX2)
121
0
   if(CPUID::has_avx2())
122
0
      {
123
0
      while(blocks >= 8)
124
0
         {
125
0
         avx2_decrypt_8(in, out);
126
0
         in += 8*BLOCK_SIZE;
127
0
         out += 8*BLOCK_SIZE;
128
0
         blocks -= 8;
129
0
         }
130
0
      }
131
0
#endif
132
0
133
0
#if defined(BOTAN_HAS_SHACAL2_SIMD)
134
0
   if(CPUID::has_simd_32())
135
0
      {
136
0
      while(blocks >= 4)
137
0
         {
138
0
         simd_decrypt_4(in, out);
139
0
         in += 4*BLOCK_SIZE;
140
0
         out += 4*BLOCK_SIZE;
141
0
         blocks -= 4;
142
0
         }
143
0
      }
144
0
#endif
145
0
146
0
   for(size_t i = 0; i != blocks; ++i)
147
0
      {
148
0
      uint32_t A = load_be<uint32_t>(in, 0);
149
0
      uint32_t B = load_be<uint32_t>(in, 1);
150
0
      uint32_t C = load_be<uint32_t>(in, 2);
151
0
      uint32_t D = load_be<uint32_t>(in, 3);
152
0
      uint32_t E = load_be<uint32_t>(in, 4);
153
0
      uint32_t F = load_be<uint32_t>(in, 5);
154
0
      uint32_t G = load_be<uint32_t>(in, 6);
155
0
      uint32_t H = load_be<uint32_t>(in, 7);
156
0
157
0
      for(size_t r = 0; r != 64; r += 8)
158
0
         {
159
0
         SHACAL2_Rev(B, C, D, E, F, G, H, A, m_RK[63-r]);
160
0
         SHACAL2_Rev(C, D, E, F, G, H, A, B, m_RK[62-r]);
161
0
         SHACAL2_Rev(D, E, F, G, H, A, B, C, m_RK[61-r]);
162
0
         SHACAL2_Rev(E, F, G, H, A, B, C, D, m_RK[60-r]);
163
0
         SHACAL2_Rev(F, G, H, A, B, C, D, E, m_RK[59-r]);
164
0
         SHACAL2_Rev(G, H, A, B, C, D, E, F, m_RK[58-r]);
165
0
         SHACAL2_Rev(H, A, B, C, D, E, F, G, m_RK[57-r]);
166
0
         SHACAL2_Rev(A, B, C, D, E, F, G, H, m_RK[56-r]);
167
0
         }
168
0
169
0
      store_be(out, A, B, C, D, E, F, G, H);
170
0
171
0
      in += BLOCK_SIZE;
172
0
      out += BLOCK_SIZE;
173
0
      }
174
0
   }
175
176
/*
177
* SHACAL2 Key Schedule
178
*/
179
void SHACAL2::key_schedule(const uint8_t key[], size_t len)
180
0
   {
181
0
   const uint32_t RC[64] = {
182
0
      0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
183
0
      0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
184
0
      0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
185
0
      0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
186
0
      0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
187
0
      0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
188
0
      0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
189
0
      0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
190
0
      0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
191
0
      0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
192
0
      0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
193
0
      0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
194
0
      0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
195
0
      0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
196
0
      0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
197
0
      0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2
198
0
   };
199
0
200
0
   if(m_RK.empty())
201
0
      m_RK.resize(64);
202
0
   else
203
0
      clear_mem(m_RK.data(), m_RK.size());
204
0
205
0
   load_be(m_RK.data(), key, len/4);
206
0
207
0
   for(size_t i = 16; i != 64; ++i)
208
0
      {
209
0
      const uint32_t sigma0_15 = rotr< 7>(m_RK[i-15]) ^ rotr<18>(m_RK[i-15]) ^ (m_RK[i-15] >> 3);
210
0
      const uint32_t sigma1_2  = rotr<17>(m_RK[i- 2]) ^ rotr<19>(m_RK[i- 2]) ^ (m_RK[i- 2] >> 10);
211
0
      m_RK[i] = m_RK[i-16] + sigma0_15 + m_RK[i-7] + sigma1_2;
212
0
      }
213
0
214
0
   for(size_t i = 0; i != 64; ++i)
215
0
      {
216
0
      m_RK[i] += RC[i];
217
0
      }
218
0
   }
219
220
size_t SHACAL2::parallelism() const
221
0
   {
222
0
#if defined(BOTAN_HAS_SHACAL2_X86)
223
0
   if(CPUID::has_intel_sha())
224
0
      {
225
0
      return 4;
226
0
      }
227
0
#endif
228
0
229
0
#if defined(BOTAN_HAS_SHACAL2_AVX2)
230
0
   if(CPUID::has_avx2())
231
0
      {
232
0
      return 8;
233
0
      }
234
0
#endif
235
0
236
0
#if defined(BOTAN_HAS_SHACAL2_SIMD)
237
0
   if(CPUID::has_simd_32())
238
0
      {
239
0
      return 4;
240
0
      }
241
0
#endif
242
0
243
0
   return 1;
244
0
   }
245
246
std::string SHACAL2::provider() const
247
0
   {
248
0
#if defined(BOTAN_HAS_SHACAL2_X86)
249
0
   if(CPUID::has_intel_sha())
250
0
      {
251
0
      return "intel_sha";
252
0
      }
253
0
#endif
254
0
255
0
#if defined(BOTAN_HAS_SHACAL2_AVX2)
256
0
   if(CPUID::has_avx2())
257
0
      {
258
0
      return "avx2";
259
0
      }
260
0
#endif
261
0
262
0
#if defined(BOTAN_HAS_SHACAL2_SIMD)
263
0
   if(CPUID::has_simd_32())
264
0
      {
265
0
      return "simd";
266
0
      }
267
0
#endif
268
0
269
0
   return "base";
270
0
   }
271
272
/*
273
* Clear memory of sensitive data
274
*/
275
void SHACAL2::clear()
276
0
   {
277
0
   zap(m_RK);
278
0
   }
279
280
}