8000 Replace various function-like macros with inline functions by randombit · Pull Request #2913 · randombit/botan · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Replace various function-like macros with inline functions #2913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
839 changes: 408 additions & 431 deletions src/lib/block/aes/aes_ni/aes_ni.cpp

Large diffs are not rendered by default.

53 changes: 10 additions & 43 deletions src/lib/block/serpent/serpent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,48 +16,13 @@

namespace Botan {

namespace {

/*
* Serpent's Linear Transform
*/
inline void transform(uint32_t& B0, uint32_t& B1, uint32_t& B2, uint32_t& B3)
{
B0 = rotl<13>(B0); B2 = rotl<3>(B2);
B1 ^= B0 ^ B2; B3 ^= B2 ^ (B0 << 3);
B1 = rotl<1>(B1); B3 = rotl<7>(B3);
B0 ^= B1 ^ B3; B2 ^= B3 ^ (B1 << 7);
B0 = rotl<5>(B0); B2 = rotl<22>(B2);
}

/*
* Serpent's Inverse Linear Transform
*/
inline void i_transform(uint32_t& B0, uint32_t& B1, uint32_t& B2, uint32_t& B3)
{
B2 = rotr<22>(B2); B0 = rotr<5>(B0);
B2 ^= B3 ^ (B1 << 7); B0 ^= B1 ^ B3;
B3 = rotr<7>(B3); B1 = rotr<1>(B1);
B3 ^= B2 ^ (B0 << 3); B1 ^= B0 ^ B2;
B2 = rotr<3>(B2); B0 = rotr<13>(B0);
}

}

/*
* XOR a key block with a data block
*/
#define key_xor(round, B0, B1, B2, B3) \
B0 ^= m_round_key[4*round ]; \
B1 ^= m_round_key[4*round+1]; \
B2 ^= m_round_key[4*round+2]; \
B3 ^= m_round_key[4*round+3];

/*
* Serpent Encryption
*/
void Serpent::encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const
{
using namespace Botan::Serpent_F;

verify_key_set(m_round_key.empty() == false);

#if defined(BOTAN_HAS_SERPENT_AVX2)
Expand Down Expand Up @@ -86,6 +51,8 @@ void Serpent::encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const
}
#endif

const Key_Inserter key_xor(m_round_key.data());

BOTAN_PARALLEL_SIMD_FOR(size_t i = 0; i < blocks; ++i)
{
uint32_t B0, B1, B2, B3;
Expand Down Expand Up @@ -133,6 +100,8 @@ void Serpent::encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const
*/
void Serpent::decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const
{
using namespace Botan::Serpent_F;

verify_key_set(m_round_key.empty() == false);

#if defined(BOTAN_HAS_SERPENT_AVX2)
Expand Down Expand Up @@ -161,6 +130,8 @@ void Serpent::decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const
}
#endif

const Key_Inserter key_xor(m_round_key.data());

BOTAN_PARALLEL_SIMD_FOR(size_t i = 0; i < blocks; ++i)
{
uint32_t B0, B1, B2, B3;
Expand Down Expand Up @@ -203,15 +174,13 @@ void Serpent::decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const
}
}

#undef key_xor
#undef transform
#undef i_transform

/*
* Serpent Key Schedule
*/
void Serpent::key_schedule(const uint8_t key[], size_t length)
{
using namespace Botan::Serpent_F;

const uint32_t PHI = 0x9E3779B9;

secure_vector<uint32_t> W(140);
Expand Down Expand Up @@ -294,6 +263,4 @@ std::string Serpent::provider() const
return "base";
}

#undef key_xor

}
30 changes: 18 additions & 12 deletions src/lib/block/serpent/serpent_avx2/serpent_avx2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,17 @@
*/

#include <botan/internal/serpent.h>
#include <botan/internal/serpent_sbox.h>
#include <botan/internal/simd_avx2.h>
#include <botan/internal/serpent_sbox.h>

namespace Botan {

#if defined(__GNUG__)

#define key_xor(round, B0, B1, B2, B3) \
do { \
B0 ^= SIMD_8x32::splat(m_round_key[4*round ]); \
B1 ^= SIMD_8x32::splat(m_round_key[4*round+1]); \
B2 ^= SIMD_8x32::splat(m_round_key[4*round+2]); \
B3 ^= SIMD_8x32::splat(m_round_key[4*round+3]); \
} while(0)
// These macros are redundant with the versions in serpent_sbox.h
// but unfortunately removing them seems to trigger a bug in GCC
// when building in amalgamation mode

/*
* Serpent's linear transformations
*/
#define transform(B0, B1, B2, B3) \
do { \
B0 = B0.rotl<13>(); \
Expand Down Expand Up @@ -50,9 +44,13 @@ namespace Botan {
B0 = B0.rotr<13>(); \
} while(0)

#endif

BOTAN_FUNC_ISA("avx2")
void Serpent::avx2_encrypt_8(const uint8_t in[128], uint8_t out[128]) const
{
using namespace Botan::Serpent_F;

SIMD_8x32::reset_registers();

SIMD_8x32 B0 = SIMD_8x32::load_le(in);
Expand All @@ -62,6 +60,8 @@ void Serpent::avx2_encrypt_8(const uint8_t in[128], uint8_t out[128]) const

SIMD_8x32::transpose(B0, B1, B2, B3);

const Key_Inserter key_xor(m_round_key.data());

key_xor( 0,B0,B1,B2,B3); SBoxE0(B0,B1,B2,B3); transform(B0,B1,B2,B3);
key_xor( 1,B0,B1,B2,B3); SBoxE1(B0,B1,B2,B3); transform(B0,B1,B2,B3);
key_xor( 2,B0,B1,B2,B3); SBoxE2(B0,B1,B2,B3); transform(B0,B1,B2,B3);
Expand All @@ -70,6 +70,7 @@ void Serpent::avx2_encrypt_8(const uint8_t in[128], uint8_t out[128]) const
key_xor( 5,B0,B1,B2,B3); SBoxE5(B0,B1,B2,B3); transform(B0,B1,B2,B3);
key_xor( 6,B0,B1,B2,B3); SBoxE6(B0,B1,B2,B3); transform(B0,B1,B2,B3);
key_xor( 7,B0,B1,B2,B3); SBoxE7(B0,B1,B2,B3); transform(B0,B1,B2,B3);

key_xor( 8,B0,B1,B2,B3); SBoxE0(B0,B1,B2,B3); transform(B0,B1,B2,B3);
key_xor( 9,B0,B1,B2,B3); SBoxE1(B0,B1,B2,B3); transform(B0,B1,B2,B3);
key_xor(10,B0,B1,B2,B3); SBoxE2(B0,B1,B2,B3); transform(B0,B1,B2,B3);
Expand All @@ -78,6 +79,7 @@ void Serpent::avx2_encrypt_8(const uint8_t in[128], uint8_t out[128]) const
key_xor(13,B0,B1,B2,B3); SBoxE5(B0,B1,B2,B3); transform(B0,B1,B2,B3);
key_xor(14,B0,B1,B2,B3); SBoxE6(B0,B1,B2,B3); transform(B0,B1,B2,B3);
key_xor(15,B0,B1,B2,B3); SBoxE7(B0,B1,B2,B3); transform(B0,B1,B2,B3);

key_xor(16,B0,B1,B2,B3); SBoxE0(B0,B1,B2,B3); transform(B0,B1,B2,B3);
key_xor(17,B0,B1,B2,B3); SBoxE1(B0,B1,B2,B3); transform(B0,B1,B2,B3);
key_xor(18,B0,B1,B2,B3); SBoxE2(B0,B1,B2,B3); transform(B0,B1,B2,B3);
Expand All @@ -86,6 +88,7 @@ void Serpent::avx2_encrypt_8(const uint8_t in[128], uint8_t out[128]) const
key_xor(21,B0,B1,B2,B3); SBoxE5(B0,B1,B2,B3); transform(B0,B1,B2,B3);
key_xor(22,B0,B1,B2,B3); SBoxE6(B0,B1,B2,B3); transform(B0,B1,B2,B3);
key_xor(23,B0,B1,B2,B3); SBoxE7(B0,B1,B2,B3); transform(B0,B1,B2,B3);

key_xor(24,B0,B1,B2,B3); SBoxE0(B0,B1,B2,B3); transform(B0,B1,B2,B3);
key_xor(25,B0,B1,B2,B3); SBoxE1(B0,B1,B2,B3); transform(B0,B1,B2,B3);
key_xor(26,B0,B1,B2,B3); SBoxE2(B0,B1,B2,B3); transform(B0,B1,B2,B3);
Expand All @@ -107,6 +110,8 @@ void Serpent::avx2_encrypt_8(const uint8_t in[128], uint8_t out[128]) const
BOTAN_FUNC_ISA("avx2")
void Serpent::avx2_decrypt_8(const uint8_t in[128], uint8_t out[128]) const
{
using namespace Botan::Serpent_F;

SIMD_8x32::reset_registers();

SIMD_8x32 B0 = SIMD_8x32::load_le(in);
Expand All @@ -116,6 +121,8 @@ void Serpent::avx2_decrypt_8(const uint8_t in[128], uint8_t out[128]) const

SIMD_8x32::transpose(B0, B1, B2, B3);

const Key_Inserter key_xor(m_round_key.data());

key_xor(32,B0,B1,B2,B3); SBoxD7(B0,B1,B2,B3); key_xor(31,B0,B1,B2,B3);
i_transform(B0,B1,B2,B3); SBoxD6(B0,B1,B2,B3); key_xor(30,B0,B1,B2,B3);
i_transform(B0,B1,B2,B3); SBoxD5(B0,B1,B2,B3); key_xor(29,B0,B1,B2,B3);
Expand Down Expand Up @@ -162,7 +169,6 @@ void Serpent::avx2_decrypt_8(const uint8_t in[128], uint8_t out[128]) const
SIMD_8x32::zero_registers();
}

#undef key_xor
#undef transform
#undef i_transform

Expand Down
69 changes: 66 additions & 3 deletions src/lib/block/serpent/serpent_sbox.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/*
* Serpent SBox Expressions
* (C) 1999-2007,2013 Jack Lloyd
*
* The sbox expressions used here were discovered by Dag Arne Osvik and
Expand All @@ -8,11 +7,13 @@
* Botan is released under the Simplified BSD License (see license.txt)
*/

#ifndef BOTAN_SERPENT_SBOX_H_
#define BOTAN_SERPENT_SBOX_H_
#ifndef BOTAN_SERPENT_FUNCS_H_
#define BOTAN_SERPENT_FUNCS_H_

#include <botan/build.h>

namespace Botan::Serpent_F {

template<typename T>
BOTAN_FORCE_INLINE void SBoxE0(T& a, T& b, T& c, T& d)
{
Expand Down Expand Up @@ -443,4 +444,66 @@ BOTAN_FORCE_INLINE void SBoxD7(T& a, T& b, T& c, T& d)
d = t0;
}

template<size_t S>
BOTAN_FORCE_INLINE uint32_t shl(uint32_t v)
{
return v << S;
}

/*
* Serpent's Linear Transform
*/
template<typename T>
BOTAN_FORCE_INLINE void transform(T& B0, T& B1, T& B2, T& B3)
{
B0 = rotl<13>(B0);
B2 = rotl<3>(B2);
B1 ^= B0 ^ B2;
B3 ^= B2 ^ shl<3>(B0);
B1 = rotl<1>(B1);
B3 = rotl<7>(B3);
B0 ^= B1 ^ B3;
B2 ^= B3 ^ shl<7>(B1);
B0 = rotl<5>(B0);
B2 = rotl<22>(B2);
}

/*
* Serpent's Inverse Linear Transform
*/
template<typename T>
BOTAN_FORCE_INLINE void i_transform(T& B0, T& B1, T& B2, T& B3)
{
B2 = rotr<22>(B2);
B0 = rotr<5>(B0);
B2 ^= B3 ^ shl<7>(B1);
B0 ^= B1 ^ B3;
B3 = rotr<7>(B3);
B1 = rotr<1>(B1);
B3 ^= B2 ^ shl<3>(B0);
B1 ^= B0 ^ B2;
B2 = rotr<3>(B2);
B0 = rotr<13>(B0);
}

class Key_Inserter
{
public:
Key_Inserter(const uint32_t* RK) : m_RK(RK) {}

template<typename T>
inline void operator()(size_t R, T& B0, T& B1, T& B2, T& B3) const
{
B0 ^= m_RK[4*R ];
B1 ^= m_RK[4*R+1];
B2 ^= m_RK[4*R+2];
B3 ^= m_RK[4*R+3];
}

private:
const uint32_t* m_RK;
};

}

#endif
53 changes: 9 additions & 44 deletions src/lib/block/serpent/serpent_simd/serpent_simd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,62 +6,27 @@
*/

#include <botan/internal/serpent.h>
#include <botan/internal/serpent_sbox.h>
#include <botan/internal/simd_32.h>
#include <botan/internal/serpent_sbox.h>

namespace Botan {

#define key_xor(round, B0, B1, B2, B3) \
do { \
B0 ^= SIMD_4x32::splat(m_round_key[4*round ]); \
B1 ^= SIMD_4x32::splat(m_round_key[4*round+1]); \
B2 ^= SIMD_4x32::splat(m_round_key[4*round+2]); \
B3 ^= SIMD_4x32::splat(m_round_key[4*round+3]); \
} while(0)

/*
* Serpent's linear transformations
*/
#define transform(B0, B1, B2, B3) \
do { \
B0 = B0.rotl<13>(); \
B2 = B2.rotl<3>(); \
B1 ^= B0 ^ B2; \
B3 ^= B2 ^ B0.shl<3>(); \
B1 = B1.rotl<1>(); \
B3 = B3.rotl<7>(); \
B0 ^= B1 ^ B3; \
B2 ^= B3 ^ B1.shl<7>(); \
B0 = B0.rotl<5>(); \
B2 = B2.rotl<22>(); \
} while(0)

#define i_transform(B0, B1, B2, B3) \
do { \
B2 = B2.rotr<22>(); \
B0 = B0.rotr<5>(); \
B2 ^= B3 ^ B1.shl<7>(); \
B0 ^= B1 ^ B3; \
B3 = B3.rotr<7>(); \
B1 = B1.rotr<1>(); \
B3 ^= B2 ^ B0.shl<3>(); \
B1 ^= B0 ^ B2; \
B2 = B2.rotr<3>(); \
B0 = B0.rotr<13>(); \
} while(0)

/*
* SIMD Serpent Encryption of 4 blocks in parallel
*/
void Serpent::simd_encrypt_4(const uint8_t in[64], uint8_t out[64]) const
{
using namespace Botan::Serpent_F;

SIMD_4x32 B0 = SIMD_4x32::load_le(in);
SIMD_4x32 B1 = SIMD_4x32::load_le(in + 16);
SIMD_4x32 B2 = SIMD_4x32::load_le(in + 32);
SIMD_4x32 B3 = SIMD_4x32::load_le(in + 48);

SIMD_4x32::transpose(B0, B1, B2, B3);

const Key_Inserter key_xor(m_round_key.data());

key_xor( 0,B0,B1,B2,B3); SBoxE0(B0,B1,B2,B3); transform(B0,B1,B2,B3);
key_xor( 1,B0,B1,B2,B3); SBoxE1(B0,B1,B2,B3); transform(B0,B1,B2,B3);
key_xor( 2,B0,B1,B2,B3); SBoxE2(B0,B1,B2,B3); transform(B0,B1,B2,B3);
Expand Down Expand Up @@ -111,13 +76,17 @@ void Serpent::simd_encrypt_4(const uint8_t in[64], uint8_t out[64]) const
*/
void Serpent::simd_decrypt_4(const uint8_t in[64], uint8_t out[64]) const
{
using namespace Botan::Serpent_F;

SIMD_4x32 B0 = SIMD_4x32::load_le(in);
SIMD_4x32 B1 = SIMD_4x32::load_le(in + 16);
SIMD_4x32 B2 = SIMD_4x32::load_le(in + 32);
SIMD_4x32 B3 = SIMD_4x32::load_le(in + 48);

SIMD_4x32::transpose(B0, B1, B2, B3);

const Key_Inserter key_xor(m_round_key.data());

key_xor(32,B0,B1,B2,B3); SBoxD7(B0,B1,B2,B3); key_xor(31,B0,B1,B2,B3);
i_transform(B0,B1,B2,B3); SBoxD6(B0,B1,B2,B3); key_xor(30,B0,B1,B2,B3);
i_transform(B0,B1,B2,B3); SBoxD5(B0,B1,B2,B3); key_xor(29,B0,B1,B2,B3);
Expand Down Expand Up @@ -162,8 +131,4 @@ void Serpent::simd_decrypt_4(const uint8_t in[64], uint8_t out[64]) const
B3.store_le(out + 48);
}

#undef key_xor
#undef transform
#undef i_transform

}
Loading
0