Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

shacal2_avx2.cpp
Go to the documentation of this file.
1 /*
2 * (C) 2018 Jack Lloyd
3 *
4 * Botan is released under the Simplified BSD License (see license.txt)
5 */
6
7 #include <botan/shacal2.h>
8 #include <botan/internal/simd_avx2.h>
9
10 namespace Botan {
11
12 namespace {
13
14 inline
15 void SHACAL2_Fwd(const SIMD_8x32& A, const SIMD_8x32& B, const SIMD_8x32& C, SIMD_8x32& D,
16 const SIMD_8x32& E, const SIMD_8x32& F, const SIMD_8x32& G, SIMD_8x32& H,
17 uint32_t RK)
18 {
19 H += E.rho<6,11,25>() + ((E & F) ^ (~E & G)) + SIMD_8x32::splat(RK);
20 D += H;
21 H += A.rho<2,13,22>() + ((A & B) | ((A | B) & C));
22 }
23
24 inline
25 void SHACAL2_Rev(const SIMD_8x32& A, const SIMD_8x32& B, const SIMD_8x32& C, SIMD_8x32& D,
26 const SIMD_8x32& E, const SIMD_8x32& F, const SIMD_8x32& G, SIMD_8x32& H,
27 uint32_t RK)
28 {
29 H -= A.rho<2,13,22>() + ((A & B) | ((A | B) & C));
30 D -= H;
31 H -= E.rho<6,11,25>() + ((E & F) ^ (~E & G)) + SIMD_8x32::splat(RK);
32 }
33
34 }
35
36 void SHACAL2::avx2_encrypt_8(const uint8_t in[], uint8_t out[]) const
37 {
38 SIMD_8x32::reset_registers();
39
40 SIMD_8x32 A = SIMD_8x32::load_be(in);
41 SIMD_8x32 B = SIMD_8x32::load_be(in+32);
42 SIMD_8x32 C = SIMD_8x32::load_be(in+64);
43 SIMD_8x32 D = SIMD_8x32::load_be(in+96);
44
45 SIMD_8x32 E = SIMD_8x32::load_be(in+128);
46 SIMD_8x32 F = SIMD_8x32::load_be(in+160);
47 SIMD_8x32 G = SIMD_8x32::load_be(in+192);
48 SIMD_8x32 H = SIMD_8x32::load_be(in+224);
49
50 SIMD_8x32::transpose(A, B, C, D, E, F, G, H);
51
52 for(size_t r = 0; r != 64; r += 8)
53 {
54 SHACAL2_Fwd(A, B, C, D, E, F, G, H, m_RK[r+0]);
55 SHACAL2_Fwd(H, A, B, C, D, E, F, G, m_RK[r+1]);
56 SHACAL2_Fwd(G, H, A, B, C, D, E, F, m_RK[r+2]);
57 SHACAL2_Fwd(F, G, H, A, B, C, D, E, m_RK[r+3]);
58 SHACAL2_Fwd(E, F, G, H, A, B, C, D, m_RK[r+4]);
59 SHACAL2_Fwd(D, E, F, G, H, A, B, C, m_RK[r+5]);
60 SHACAL2_Fwd(C, D, E, F, G, H, A, B, m_RK[r+6]);
61 SHACAL2_Fwd(B, C, D, E, F, G, H, A, m_RK[r+7]);
62 }
63
64 SIMD_8x32::transpose(A, B, C, D, E, F, G, H);
65
66 A.store_be(out);
67 B.store_be(out+32);
68 C.store_be(out+64);
69 D.store_be(out+96);
70
71 E.store_be(out+128);
72 F.store_be(out+160);
73 G.store_be(out+192);
74 H.store_be(out+224);
75
76 SIMD_8x32::zero_registers();
77 }
78
79 void SHACAL2::avx2_decrypt_8(const uint8_t in[], uint8_t out[]) const
80 {
81 SIMD_8x32::reset_registers();
82
83 SIMD_8x32 A = SIMD_8x32::load_be(in);
84 SIMD_8x32 B = SIMD_8x32::load_be(in+32);
85 SIMD_8x32 C = SIMD_8x32::load_be(in+64);
86 SIMD_8x32 D = SIMD_8x32::load_be(in+96);
87
88 SIMD_8x32 E = SIMD_8x32::load_be(in+128);
89 SIMD_8x32 F = SIMD_8x32::load_be(in+160);
90 SIMD_8x32 G = SIMD_8x32::load_be(in+192);
91 SIMD_8x32 H = SIMD_8x32::load_be(in+224);
92
93 SIMD_8x32::transpose(A, B, C, D, E, F, G, H);
94
95 for(size_t r = 0; r != 64; r += 8)
96 {
97 SHACAL2_Rev(B, C, D, E, F, G, H, A, m_RK[63-r]);
98 SHACAL2_Rev(C, D, E, F, G, H, A, B, m_RK[62-r]);
99 SHACAL2_Rev(D, E, F, G, H, A, B, C, m_RK[61-r]);
100 SHACAL2_Rev(E, F, G, H, A, B, C, D, m_RK[60-r]);
101 SHACAL2_Rev(F, G, H, A, B, C, D, E, m_RK[59-r]);
102 SHACAL2_Rev(G, H, A, B, C, D, E, F, m_RK[58-r]);
103 SHACAL2_Rev(H, A, B, C, D, E, F, G, m_RK[57-r]);
104 SHACAL2_Rev(A, B, C, D, E, F, G, H, m_RK[56-r]);
105 }
106
107 SIMD_8x32::transpose(A, B, C, D, E, F, G, H);
108
109 A.store_be(out);
110 B.store_be(out+32);
111 C.store_be(out+64);
112 D.store_be(out+96);
113
114 E.store_be(out+128);
115 F.store_be(out+160);
116 G.store_be(out+192);
117 H.store_be(out+224);
118
119 SIMD_8x32::zero_registers();
120 }
121
122 }
Botan::SIMD_8x32::splat
static SIMD_8x32 splat(uint32_t B)
Definition: simd_avx2.h:45
Botan::SIMD_8x32::reset_registers
static void reset_registers()
Definition: simd_avx2.h:258
Botan
Definition: alg_id.cpp:13
Botan::SIMD_8x32::transpose
static void transpose(SIMD_8x32 &B0, SIMD_8x32 &B1, SIMD_8x32 &B2, SIMD_8x32 &B3)
Definition: simd_avx2.h:228
Botan::SIMD_8x32::load_be
static SIMD_8x32 load_be(const uint8_t *in)
Definition: simd_avx2.h:57
Botan::SIMD_8x32::zero_registers
static void zero_registers()
Definition: simd_avx2.h:264