Botan 3.6.1
Crypto and TLS for C&
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/internal/shacal2.h>
8
9#include <botan/internal/simd_avx2.h>
10
11namespace Botan {
12
13namespace {
14
15void BOTAN_FORCE_INLINE BOTAN_AVX2_FN SHACAL2_Fwd(const SIMD_8x32& A,
16 const SIMD_8x32& B,
17 const SIMD_8x32& C,
18 SIMD_8x32& D,
19 const SIMD_8x32& E,
20 const SIMD_8x32& F,
21 const SIMD_8x32& G,
22 SIMD_8x32& H,
23 uint32_t RK) {
24 H += E.sigma1() + SIMD_8x32::choose(E, F, G) + SIMD_8x32::splat(RK);
25 D += H;
26 H += A.sigma0() + SIMD_8x32::majority(A, B, C);
27}
28
29void BOTAN_FORCE_INLINE BOTAN_AVX2_FN SHACAL2_Rev(const SIMD_8x32& A,
30 const SIMD_8x32& B,
31 const SIMD_8x32& C,
32 SIMD_8x32& D,
33 const SIMD_8x32& E,
34 const SIMD_8x32& F,
35 const SIMD_8x32& G,
36 SIMD_8x32& H,
37 uint32_t RK) {
38 H -= A.sigma0() + SIMD_8x32::majority(A, B, C);
39 D -= H;
40 H -= E.sigma1() + SIMD_8x32::choose(E, F, G) + SIMD_8x32::splat(RK);
41}
42
43} // namespace
44
45void BOTAN_AVX2_FN SHACAL2::avx2_encrypt_8(const uint8_t in[], uint8_t out[]) const {
46 SIMD_8x32::reset_registers();
47
48 SIMD_8x32 A = SIMD_8x32::load_be(in);
49 SIMD_8x32 B = SIMD_8x32::load_be(in + 32);
50 SIMD_8x32 C = SIMD_8x32::load_be(in + 64);
51 SIMD_8x32 D = SIMD_8x32::load_be(in + 96);
52
53 SIMD_8x32 E = SIMD_8x32::load_be(in + 128);
54 SIMD_8x32 F = SIMD_8x32::load_be(in + 160);
55 SIMD_8x32 G = SIMD_8x32::load_be(in + 192);
56 SIMD_8x32 H = SIMD_8x32::load_be(in + 224);
57
58 SIMD_8x32::transpose(A, B, C, D, E, F, G, H);
59
60 for(size_t r = 0; r != 64; r += 8) {
61 SHACAL2_Fwd(A, B, C, D, E, F, G, H, m_RK[r + 0]);
62 SHACAL2_Fwd(H, A, B, C, D, E, F, G, m_RK[r + 1]);
63 SHACAL2_Fwd(G, H, A, B, C, D, E, F, m_RK[r + 2]);
64 SHACAL2_Fwd(F, G, H, A, B, C, D, E, m_RK[r + 3]);
65 SHACAL2_Fwd(E, F, G, H, A, B, C, D, m_RK[r + 4]);
66 SHACAL2_Fwd(D, E, F, G, H, A, B, C, m_RK[r + 5]);
67 SHACAL2_Fwd(C, D, E, F, G, H, A, B, m_RK[r + 6]);
68 SHACAL2_Fwd(B, C, D, E, F, G, H, A, m_RK[r + 7]);
69 }
70
71 SIMD_8x32::transpose(A, B, C, D, E, F, G, H);
72
73 A.store_be(out);
74 B.store_be(out + 32);
75 C.store_be(out + 64);
76 D.store_be(out + 96);
77
78 E.store_be(out + 128);
79 F.store_be(out + 160);
80 G.store_be(out + 192);
81 H.store_be(out + 224);
82
83 SIMD_8x32::zero_registers();
84}
85
86BOTAN_AVX2_FN void SHACAL2::avx2_decrypt_8(const uint8_t in[], uint8_t out[]) const {
87 SIMD_8x32::reset_registers();
88
89 SIMD_8x32 A = SIMD_8x32::load_be(in);
90 SIMD_8x32 B = SIMD_8x32::load_be(in + 32);
91 SIMD_8x32 C = SIMD_8x32::load_be(in + 64);
92 SIMD_8x32 D = SIMD_8x32::load_be(in + 96);
93
94 SIMD_8x32 E = SIMD_8x32::load_be(in + 128);
95 SIMD_8x32 F = SIMD_8x32::load_be(in + 160);
96 SIMD_8x32 G = SIMD_8x32::load_be(in + 192);
97 SIMD_8x32 H = SIMD_8x32::load_be(in + 224);
98
99 SIMD_8x32::transpose(A, B, C, D, E, F, G, H);
100
101 for(size_t r = 0; r != 64; r += 8) {
102 SHACAL2_Rev(B, C, D, E, F, G, H, A, m_RK[63 - r]);
103 SHACAL2_Rev(C, D, E, F, G, H, A, B, m_RK[62 - r]);
104 SHACAL2_Rev(D, E, F, G, H, A, B, C, m_RK[61 - r]);
105 SHACAL2_Rev(E, F, G, H, A, B, C, D, m_RK[60 - r]);
106 SHACAL2_Rev(F, G, H, A, B, C, D, E, m_RK[59 - r]);
107 SHACAL2_Rev(G, H, A, B, C, D, E, F, m_RK[58 - r]);
108 SHACAL2_Rev(H, A, B, C, D, E, F, G, m_RK[57 - r]);
109 SHACAL2_Rev(A, B, C, D, E, F, G, H, m_RK[56 - r]);
110 }
111
112 SIMD_8x32::transpose(A, B, C, D, E, F, G, H);
113
114 A.store_be(out);
115 B.store_be(out + 32);
116 C.store_be(out + 64);
117 D.store_be(out + 96);
118
119 E.store_be(out + 128);
120 F.store_be(out + 160);
121 G.store_be(out + 192);
122 H.store_be(out + 224);
123
124 SIMD_8x32::zero_registers();
125}
126
127} // namespace Botan
#define BOTAN_FORCE_INLINE
Definition compiler.h:165
#define BOTAN_AVX2_FN
Definition simd_avx2.h:15