summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas "Pixel" Noble <pixel@nobis-crew.org>2014-06-21 17:01:38 -0700
committerNicolas "Pixel" Noble <pixel@nobis-crew.org>2014-06-21 17:11:11 -0700
commitf35b19adcb83d5ff4114c2c56ac4ecdc91e6ccd6 (patch)
treed68b17a0d0822ece98932f0f67bf282646c89809
parentf7916cb7c797a26140e50a7bd07a5716c25bf85c (diff)
Optimizing cypher's process.
-rw-r--r--includes/BCypher.h42
1 files changed, 34 insertions, 8 deletions
diff --git a/includes/BCypher.h b/includes/BCypher.h
index ffb59b0..b84eeb1 100644
--- a/includes/BCypher.h
+++ b/includes/BCypher.h
@@ -46,27 +46,53 @@ class PRNG {
prng_state m_state;
};
-template<class PRNG>
+template<class PRNG, size_t BlockLen = 64>
class Cypher : public PRNG {
-public:
+ public:
void process(uint8_t * buf, size_t len) {
while (len) {
if (!m_len) {
m_ptr = m_buffer;
- m_len = sizeof(m_buffer);
- generate(m_buffer, sizeof(m_buffer));
+ m_len = BlockLen;
+ generate(m_buffer, BlockLen);
}
- size_t chunkLen = std::min(m_len, len);
+ ssize_t chunkLen = std::min(m_len, len);
m_len -= chunkLen;
len -= chunkLen;
+ if ((((uintptr_t) m_ptr) & 7) && (((uintptr_t) buf) & 7)) {
+ uint64_t * buf64 = (uint64_t *) buf;
+ uint64_t * ptr64 = (uint64_t *) m_ptr;
+ chunkLen -= 7;
+ while (chunkLen > 0) {
+ chunkLen -= 8;
+ *buf64++ ^= *ptr64++;
+ }
+ m_ptr = (uint8_t *) ptr64;
+ buf = (uint8_t *) buf64;
+ chunkLen += 7;
+ }
+
+ if ((((uintptr_t) m_ptr) & 3) && (((uintptr_t) buf) & 3)) {
+ uint64_t * buf32 = (uint32_t *) buf;
+ uint64_t * ptr32 = (uint32_t *) m_ptr;
+ chunkLen -= 3;
+ while (chunkLen > 0) {
+ chunkLen -= 4;
+ *buf32++ ^= *ptr32++;
+ }
+ m_ptr = (uint8_t *) ptr32;
+ buf = (uint8_t *) buf32;
+ chunkLen += 3;
+ }
+
while (chunkLen--)
*buf++ ^= *m_ptr++;
}
}
-private:
- uint8_t m_buffer[64], * m_ptr = m_buffer;
+ private:
+ uint8_t m_buffer[BlockLen], * m_ptr = m_buffer;
size_t m_len = 0;
};
@@ -76,4 +102,4 @@ typedef PRNG<&rc4_desc> RC4;
typedef PRNG<&sprng_desc> SPRNG;
typedef PRNG<&sober128_desc> Sober128;
-}
+};