aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar David Leverton <levertond@googlemail.com> 2008-02-05 18:56:29 +0000
committerAvatar David Leverton <levertond@googlemail.com> 2008-02-05 18:56:29 +0000
commita93af732eb92c8cd2eed6659bfb7d0c714f43560 (patch)
tree8610e1b14ab594600ed4caf1e1cd012ade70071a
parent4e0f2bd85350518e2ab26d0266f1851a2a629c5d (diff)
downloadpaludis-a93af732eb92c8cd2eed6659bfb7d0c714f43560.tar.gz
paludis-a93af732eb92c8cd2eed6659bfb7d0c714f43560.tar.xz
Tweak.
-rw-r--r--paludis/util/sha1.cc97
-rw-r--r--paludis/util/sha1.hh3
2 files changed, 49 insertions, 51 deletions
diff --git a/paludis/util/sha1.cc b/paludis/util/sha1.cc
index d519f26..b36be17 100644
--- a/paludis/util/sha1.cc
+++ b/paludis/util/sha1.cc
@@ -55,46 +55,76 @@ namespace
}
#endif
- inline uint32_t s(int n, uint32_t x)
+ template <int n_>
+ inline uint32_t s(uint32_t x)
{
- return (x << n) | (x >> (32 - n));
+ return (x << n_) | (x >> (32 - n_));
}
- inline uint32_t f_0_19(uint32_t b, uint32_t c, uint32_t d)
+ template <int> uint32_t f(uint32_t, uint32_t, uint32_t);
+ template <int> uint32_t k();
+
+ template<> inline uint32_t f<0>(uint32_t b, uint32_t c, uint32_t d)
{
return (b & c) | (~b & d);
}
+ template<> inline uint32_t k<0>()
+ {
+ return 0x5A827999U;
+ }
- inline uint32_t f_20_39(uint32_t b, uint32_t c, uint32_t d)
+ template<> inline uint32_t f<20>(uint32_t b, uint32_t c, uint32_t d)
{
return b ^ c ^ d;
}
+ template<> inline uint32_t k<20>()
+ {
+ return 0x6ED9EBA1U;
+ }
- inline uint32_t f_40_59(uint32_t b, uint32_t c, uint32_t d)
+ template<> inline uint32_t f<40>(uint32_t b, uint32_t c, uint32_t d)
{
return (b & c) | (b & d) | (c & d);
}
+ template<> inline uint32_t k<40>()
+ {
+ return 0x8F1BBCDCU;
+ }
- inline uint32_t f_60_79(uint32_t b, uint32_t c, uint32_t d)
+ template<> inline uint32_t f<60>(uint32_t b, uint32_t c, uint32_t d)
{
return b ^ c ^ d;
}
+ template<> inline uint32_t k<60>()
+ {
+ return 0xCA62C1D6U;
+ }
+}
- const uint32_t k_0_19 = 0x5A827999U;
- const uint32_t k_20_39 = 0x6ED9EBA1U;
- const uint32_t k_40_59 = 0x8F1BBCDCU;
- const uint32_t k_60_79 = 0xCA62C1D6U;
+template <int t_>
+inline void
+SHA1::process_chunk(uint32_t * w, uint32_t & a, uint32_t & b, uint32_t & c, uint32_t & d, uint32_t & e)
+{
+ for (int t = t_; t < t_ + 20; ++t)
+ {
+ uint32_t temp = s<5>(a) + f<t_>(b, c, d) + e + w[t] + k<t_>();
+ e = d;
+ d = c;
+ c = s<30>(b);
+ b = a;
+ a = temp;
+ }
}
void
SHA1::process_block(uint32_t * w)
{
- uint32_t a, b, c, d, e, temp;
+ uint32_t a, b, c, d, e;
std::transform(&w[0], &w[16], &w[0], from_bigendian);
for (int t = 16; t < 80; ++t)
- w[t] = s(1, w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16]);
+ w[t] = s<1>(w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16]);
a = h0;
b = h1;
@@ -102,45 +132,10 @@ SHA1::process_block(uint32_t * w)
d = h3;
e = h4;
- for (int t = 0; t < 20; ++t)
- {
- temp = s(5, a) + f_0_19(b, c, d) + e + w[t] + k_0_19;
- e = d;
- d = c;
- c = s(30, b);
- b = a;
- a = temp;
- }
-
- for (int t = 20; t < 40; ++t)
- {
- temp = s(5, a) + f_20_39(b, c, d) + e + w[t] + k_20_39;
- e = d;
- d = c;
- c = s(30, b);
- b = a;
- a = temp;
- }
-
- for (int t = 40; t < 60; ++t)
- {
- temp = s(5, a) + f_40_59(b, c, d) + e + w[t] + k_40_59;
- e = d;
- d = c;
- c = s(30, b);
- b = a;
- a = temp;
- }
-
- for (int t = 60; t < 80; ++t)
- {
- temp = s(5, a) + f_60_79(b, c, d) + e + w[t] + k_60_79;
- e = d;
- d = c;
- c = s(30, b);
- b = a;
- a = temp;
- }
+ process_chunk<0>(w, a, b, c, d, e);
+ process_chunk<20>(w, a, b, c, d, e);
+ process_chunk<40>(w, a, b, c, d, e);
+ process_chunk<60>(w, a, b, c, d, e);
h0 += a;
h1 += b;
diff --git a/paludis/util/sha1.hh b/paludis/util/sha1.hh
index f1fc702..9c384f6 100644
--- a/paludis/util/sha1.hh
+++ b/paludis/util/sha1.hh
@@ -49,6 +49,9 @@ namespace paludis
uint32_t h0, h1, h2, h3, h4;
void PALUDIS_HIDDEN process_block(uint32_t *);
+ template <int>
+ inline void PALUDIS_HIDDEN process_chunk(
+ uint32_t *, uint32_t &, uint32_t &, uint32_t &, uint32_t &, uint32_t &);
public:
/**