Botan 3.6.1
Crypto and TLS for C&
shacal2_simd.cpp
Go to the documentation of this file.
1/*
2* SHACAL-2 using SIMD
3* (C) 2017 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/internal/shacal2.h>
9
10#include <botan/internal/simd_32.h>
11
12namespace Botan {
13
14namespace {
15
16inline void SHACAL2_Fwd(const SIMD_4x32& A,
17 const SIMD_4x32& B,
18 const SIMD_4x32& C,
19 SIMD_4x32& D,
20 const SIMD_4x32& E,
21 const SIMD_4x32& F,
22 const SIMD_4x32& G,
23 SIMD_4x32& H,
24 uint32_t RK) {
25 H += E.sigma1() + SIMD_4x32::choose(E, F, G) + SIMD_4x32::splat(RK);
26 D += H;
27 H += A.sigma0() + SIMD_4x32::majority(A, B, C);
28}
29
30inline void SHACAL2_Rev(const SIMD_4x32& A,
31 const SIMD_4x32& B,
32 const SIMD_4x32& C,
33 SIMD_4x32& D,
34 const SIMD_4x32& E,
35 const SIMD_4x32& F,
36 const SIMD_4x32& G,
37 SIMD_4x32& H,
38 uint32_t RK) {
39 H -= A.sigma0() + SIMD_4x32::majority(A, B, C);
40 D -= H;
41 H -= E.sigma1() + SIMD_4x32::choose(E, F, G) + SIMD_4x32::splat(RK);
42}
43
44} // namespace
45
46void SHACAL2::simd_encrypt_4(const uint8_t in[], uint8_t out[]) const {
47 SIMD_4x32 A = SIMD_4x32::load_be(in);
48 SIMD_4x32 E = SIMD_4x32::load_be(in + 16);
49 SIMD_4x32 B = SIMD_4x32::load_be(in + 32);
50 SIMD_4x32 F = SIMD_4x32::load_be(in + 48);
51
52 SIMD_4x32 C = SIMD_4x32::load_be(in + 64);
53 SIMD_4x32 G = SIMD_4x32::load_be(in + 80);
54 SIMD_4x32 D = SIMD_4x32::load_be(in + 96);
55 SIMD_4x32 H = SIMD_4x32::load_be(in + 112);
56
57 SIMD_4x32::transpose(A, B, C, D);
58 SIMD_4x32::transpose(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_4x32::transpose(A, B, C, D);
72 SIMD_4x32::transpose(E, F, G, H);
73
74 A.store_be(out);
75 E.store_be(out + 16);
76 B.store_be(out + 32);
77 F.store_be(out + 48);
78
79 C.store_be(out + 64);
80 G.store_be(out + 80);
81 D.store_be(out + 96);
82 H.store_be(out + 112);
83}
84
85void SHACAL2::simd_decrypt_4(const uint8_t in[], uint8_t out[]) const {
86 SIMD_4x32 A = SIMD_4x32::load_be(in);
87 SIMD_4x32 E = SIMD_4x32::load_be(in + 16);
88 SIMD_4x32 B = SIMD_4x32::load_be(in + 32);
89 SIMD_4x32 F = SIMD_4x32::load_be(in + 48);
90
91 SIMD_4x32 C = SIMD_4x32::load_be(in + 64);
92 SIMD_4x32 G = SIMD_4x32::load_be(in + 80);
93 SIMD_4x32 D = SIMD_4x32::load_be(in + 96);
94 SIMD_4x32 H = SIMD_4x32::load_be(in + 112);
95
96 SIMD_4x32::transpose(A, B, C, D);
97 SIMD_4x32::transpose(E, F, G, H);
98
99 for(size_t r = 0; r != 64; r += 8) {
100 SHACAL2_Rev(B, C, D, E, F, G, H, A, m_RK[63 - r]);
101 SHACAL2_Rev(C, D, E, F, G, H, A, B, m_RK[62 - r]);
102 SHACAL2_Rev(D, E, F, G, H, A, B, C, m_RK[61 - r]);
103 SHACAL2_Rev(E, F, G, H, A, B, C, D, m_RK[60 - r]);
104 SHACAL2_Rev(F, G, H, A, B, C, D, E, m_RK[59 - r]);
105 SHACAL2_Rev(G, H, A, B, C, D, E, F, m_RK[58 - r]);
106 SHACAL2_Rev(H, A, B, C, D, E, F, G, m_RK[57 - r]);
107 SHACAL2_Rev(A, B, C, D, E, F, G, H, m_RK[56 - r]);
108 }
109
110 SIMD_4x32::transpose(A, B, C, D);
111 SIMD_4x32::transpose(E, F, G, H);
112
113 A.store_be(out);
114 E.store_be(out + 16);
115 B.store_be(out + 32);
116 F.store_be(out + 48);
117
118 C.store_be(out + 64);
119 G.store_be(out + 80);
120 D.store_be(out + 96);
121 H.store_be(out + 112);
122}
123
124} // namespace Botan
static SIMD_4x32 load_be(const void *in) noexcept
Definition simd_32.h:175
static void transpose(SIMD_4x32 &B0, SIMD_4x32 &B1, SIMD_4x32 &B2, SIMD_4x32 &B3) noexcept
Definition simd_32.h:554
static SIMD_4x32 choose(const SIMD_4x32 &mask, const SIMD_4x32 &a, const SIMD_4x32 &b) noexcept
Definition simd_32.h:600
static SIMD_4x32 majority(const SIMD_4x32 &x, const SIMD_4x32 &y, const SIMD_4x32 &z) noexcept
Definition simd_32.h:610
static SIMD_4x32 splat(uint32_t B) noexcept
Definition simd_32.h:132