Coverage Report

Created: 2020-11-21 08:34

/src/botan/src/lib/hash/sha3/sha3_bmi2/sha3_bmi2.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* SHA-3
3
* (C) 2019 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#include <botan/internal/sha3.h>
9
#include <botan/internal/rotate.h>
10
11
namespace Botan {
12
13
namespace {
14
15
inline void SHA3_BMI2_round(uint64_t T[25], const uint64_t A[25], uint64_t RC)
16
0
   {
17
0
   const uint64_t C0 = A[0] ^ A[5] ^ A[10] ^ A[15] ^ A[20];
18
0
   const uint64_t C1 = A[1] ^ A[6] ^ A[11] ^ A[16] ^ A[21];
19
0
   const uint64_t C2 = A[2] ^ A[7] ^ A[12] ^ A[17] ^ A[22];
20
0
   const uint64_t C3 = A[3] ^ A[8] ^ A[13] ^ A[18] ^ A[23];
21
0
   const uint64_t C4 = A[4] ^ A[9] ^ A[14] ^ A[19] ^ A[24];
22
23
0
   const uint64_t D0 = rotl<1>(C0) ^ C3;
24
0
   const uint64_t D1 = rotl<1>(C1) ^ C4;
25
0
   const uint64_t D2 = rotl<1>(C2) ^ C0;
26
0
   const uint64_t D3 = rotl<1>(C3) ^ C1;
27
0
   const uint64_t D4 = rotl<1>(C4) ^ C2;
28
29
0
   const uint64_t B00 =          A[ 0] ^ D1;
30
0
   const uint64_t B01 = rotl<44>(A[ 6] ^ D2);
31
0
   const uint64_t B02 = rotl<43>(A[12] ^ D3);
32
0
   const uint64_t B03 = rotl<21>(A[18] ^ D4);
33
0
   const uint64_t B04 = rotl<14>(A[24] ^ D0);
34
0
   T[ 0] = B00 ^ (~B01 & B02) ^ RC;
35
0
   T[ 1] = B01 ^ (~B02 & B03);
36
0
   T[ 2] = B02 ^ (~B03 & B04);
37
0
   T[ 3] = B03 ^ (~B04 & B00);
38
0
   T[ 4] = B04 ^ (~B00 & B01);
39
40
0
   const uint64_t B05 = rotl<28>(A[ 3] ^ D4);
41
0
   const uint64_t B06 = rotl<20>(A[ 9] ^ D0);
42
0
   const uint64_t B07 = rotl< 3>(A[10] ^ D1);
43
0
   const uint64_t B08 = rotl<45>(A[16] ^ D2);
44
0
   const uint64_t B09 = rotl<61>(A[22] ^ D3);
45
0
   T[ 5] = B05 ^ (~B06 & B07);
46
0
   T[ 6] = B06 ^ (~B07 & B08);
47
0
   T[ 7] = B07 ^ (~B08 & B09);
48
0
   T[ 8] = B08 ^ (~B09 & B05);
49
0
   T[ 9] = B09 ^ (~B05 & B06);
50
51
0
   const uint64_t B10 = rotl< 1>(A[ 1] ^ D2);
52
0
   const uint64_t B11 = rotl< 6>(A[ 7] ^ D3);
53
0
   const uint64_t B12 = rotl<25>(A[13] ^ D4);
54
0
   const uint64_t B13 = rotl< 8>(A[19] ^ D0);
55
0
   const uint64_t B14 = rotl<18>(A[20] ^ D1);
56
0
   T[10] = B10 ^ (~B11 & B12);
57
0
   T[11] = B11 ^ (~B12 & B13);
58
0
   T[12] = B12 ^ (~B13 & B14);
59
0
   T[13] = B13 ^ (~B14 & B10);
60
0
   T[14] = B14 ^ (~B10 & B11);
61
62
0
   const uint64_t B15 = rotl<27>(A[ 4] ^ D0);
63
0
   const uint64_t B16 = rotl<36>(A[ 5] ^ D1);
64
0
   const uint64_t B17 = rotl<10>(A[11] ^ D2);
65
0
   const uint64_t B18 = rotl<15>(A[17] ^ D3);
66
0
   const uint64_t B19 = rotl<56>(A[23] ^ D4);
67
0
   T[15] = B15 ^ (~B16 & B17);
68
0
   T[16] = B16 ^ (~B17 & B18);
69
0
   T[17] = B17 ^ (~B18 & B19);
70
0
   T[18] = B18 ^ (~B19 & B15);
71
0
   T[19] = B19 ^ (~B15 & B16);
72
73
0
   const uint64_t B20 = rotl<62>(A[ 2] ^ D3);
74
0
   const uint64_t B21 = rotl<55>(A[ 8] ^ D4);
75
0
   const uint64_t B22 = rotl<39>(A[14] ^ D0);
76
0
   const uint64_t B23 = rotl<41>(A[15] ^ D1);
77
0
   const uint64_t B24 = rotl< 2>(A[21] ^ D2);
78
0
   T[20] = B20 ^ (~B21 & B22);
79
0
   T[21] = B21 ^ (~B22 & B23);
80
0
   T[22] = B22 ^ (~B23 & B24);
81
0
   T[23] = B23 ^ (~B24 & B20);
82
0
   T[24] = B24 ^ (~B20 & B21);
83
0
   }
84
85
}
86
87
void SHA_3::permute_bmi2(uint64_t A[25])
88
0
   {
89
0
   static const uint64_t RC[24] = {
90
0
      0x0000000000000001, 0x0000000000008082, 0x800000000000808A,
91
0
      0x8000000080008000, 0x000000000000808B, 0x0000000080000001,
92
0
      0x8000000080008081, 0x8000000000008009, 0x000000000000008A,
93
0
      0x0000000000000088, 0x0000000080008009, 0x000000008000000A,
94
0
      0x000000008000808B, 0x800000000000008B, 0x8000000000008089,
95
0
      0x8000000000008003, 0x8000000000008002, 0x8000000000000080,
96
0
      0x000000000000800A, 0x800000008000000A, 0x8000000080008081,
97
0
      0x8000000000008080, 0x0000000080000001, 0x8000000080008008
98
0
   };
99
100
0
   uint64_t T[25];
101
102
0
   for(size_t i = 0; i != 24; i += 2)
103
0
      {
104
0
      SHA3_BMI2_round(T, A, RC[i+0]);
105
0
      SHA3_BMI2_round(A, T, RC[i+1]);
106
0
      }
107
0
   }
108
109
}