aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/block/gost_28147/gost_28147.cpp
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2017-10-11 17:02:20 -0400
committerJack Lloyd <[email protected]>2017-10-12 11:13:11 -0400
commit175f09ffd806f2f19cd509017a67ae1384f29ae1 (patch)
tree6194884467e4720dd79797cd106a45d60211f35f /src/lib/block/gost_28147/gost_28147.cpp
parent40b3f979723b2b3dfb5c44047d7f786a73fd7f6f (diff)
Add compile-time rotation functions
The problem with asm rol/ror is the compiler can't schedule effectively. But we only need asm in the case when the rotation is variable, so distinguish the two cases. If a compile time constant, then static_assert that the rotation is in the correct range and do the straightforward expression knowing the compiler will probably do the right thing. Otherwise do a tricky expression that both GCC and Clang happen to have recognize. Avoid the reduction case; instead require that the rotation be in range (this reverts 2b37c13dcf). Remove the asm rotations (making this branch illnamed), because now both Clang and GCC will create a roll without any extra help. Remove the reduction/mask by the word size for the variable case. The compiler can't optimize that it out well, but it's easy to ensure it is valid in the callers, especially now that the variable input cases are easy to grep for.
Diffstat (limited to 'src/lib/block/gost_28147/gost_28147.cpp')
-rw-r--r--src/lib/block/gost_28147/gost_28147.cpp25
1 files changed, 16 insertions, 9 deletions
diff --git a/src/lib/block/gost_28147/gost_28147.cpp b/src/lib/block/gost_28147/gost_28147.cpp
index f73ac5910..ffe9b5d66 100644
--- a/src/lib/block/gost_28147/gost_28147.cpp
+++ b/src/lib/block/gost_28147/gost_28147.cpp
@@ -12,11 +12,17 @@ namespace Botan {
uint8_t GOST_28147_89_Params::sbox_entry(size_t row, size_t col) const
{
- uint8_t x = m_sboxes[4 * col + (row / 2)];
-
+ const uint8_t x = m_sboxes[4 * col + (row / 2)];
return (row % 2 == 0) ? (x >> 4) : (x & 0x0F);
}
+uint8_t GOST_28147_89_Params::sbox_pair(size_t row, size_t col) const
+ {
+ const uint8_t x = m_sboxes[4 * (col % 16) + row];
+ const uint8_t y = m_sboxes[4 * (col / 16) + row];
+ return (x >> 4) | (y << 4);
+ }
+
GOST_28147_89_Params::GOST_28147_89_Params(const std::string& n) : m_name(n)
{
// Encoded in the packed fromat from RFC 4357
@@ -53,13 +59,14 @@ GOST_28147_89_Params::GOST_28147_89_Params(const std::string& n) : m_name(n)
GOST_28147_89::GOST_28147_89(const GOST_28147_89_Params& param) : m_SBOX(1024)
{
// Convert the parallel 4x4 sboxes into larger word-based sboxes
- for(size_t i = 0; i != 4; ++i)
- for(size_t j = 0; j != 256; ++j)
- {
- const uint32_t T = (param.sbox_entry(2*i , j % 16)) |
- (param.sbox_entry(2*i+1, j / 16) << 4);
- m_SBOX[256*i+j] = rotate_left(T, (11+8*i) % 32);
- }
+
+ for(size_t i = 0; i != 256; ++i)
+ {
+ m_SBOX[i ] = rotl<11, uint32_t>(param.sbox_pair(0, i));
+ m_SBOX[i+256] = rotl<19, uint32_t>(param.sbox_pair(1, i));
+ m_SBOX[i+512] = rotl<27, uint32_t>(param.sbox_pair(2, i));
+ m_SBOX[i+768] = rotl< 3, uint32_t>(param.sbox_pair(3, i));
+ }
}
std::string GOST_28147_89::name() const