diff options
Diffstat (limited to 'src/stream')
-rw-r--r-- | src/stream/arc4/arc4.cpp | 5 | ||||
-rw-r--r-- | src/stream/arc4/arc4.h | 5 | ||||
-rw-r--r-- | src/stream/ctr/ctr.cpp | 45 | ||||
-rw-r--r-- | src/stream/ctr/ctr.h | 5 | ||||
-rw-r--r-- | src/stream/info.txt | 2 | ||||
-rw-r--r-- | src/stream/ofb/ofb.cpp | 6 | ||||
-rw-r--r-- | src/stream/ofb/ofb.h | 5 | ||||
-rw-r--r-- | src/stream/salsa20/salsa20.h | 10 | ||||
-rw-r--r-- | src/stream/stream_cipher.h | 18 | ||||
-rw-r--r-- | src/stream/turing/turing.cpp | 27 | ||||
-rw-r--r-- | src/stream/turing/turing.h | 11 | ||||
-rw-r--r-- | src/stream/wid_wake/wid_wake.h | 10 |
12 files changed, 75 insertions, 74 deletions
diff --git a/src/stream/arc4/arc4.cpp b/src/stream/arc4/arc4.cpp index 170235419..313e777a5 100644 --- a/src/stream/arc4/arc4.cpp +++ b/src/stream/arc4/arc4.cpp @@ -101,8 +101,9 @@ void ARC4::clear() /* * ARC4 Constructor */ -ARC4::ARC4(size_t s) : StreamCipher(1, 256), SKIP(s), - state(256), buffer(DEFAULT_BUFFERSIZE) +ARC4::ARC4(size_t s) : SKIP(s), + state(256), + buffer(DEFAULT_BUFFERSIZE) { clear(); } diff --git a/src/stream/arc4/arc4.h b/src/stream/arc4/arc4.h index 85ddb69b7..e3df97f83 100644 --- a/src/stream/arc4/arc4.h +++ b/src/stream/arc4/arc4.h @@ -26,6 +26,11 @@ class BOTAN_DLL ARC4 : public StreamCipher StreamCipher* clone() const { return new ARC4(SKIP); } + Key_Length_Specification key_spec() const + { + return Key_Length_Specification(1, 256); + } + /** * @param skip skip this many initial bytes in the keystream */ diff --git a/src/stream/ctr/ctr.cpp b/src/stream/ctr/ctr.cpp index dc2f334a8..0de0b7b84 100644 --- a/src/stream/ctr/ctr.cpp +++ b/src/stream/ctr/ctr.cpp @@ -1,6 +1,6 @@ /* -* CTR-BE Mode Cipher -* (C) 1999-2009 Jack Lloyd +* Counter mode +* (C) 1999-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ @@ -15,15 +15,11 @@ namespace Botan { */ CTR_BE::CTR_BE(BlockCipher* ciph) : - StreamCipher(ciph->MINIMUM_KEYLENGTH, - ciph->MAXIMUM_KEYLENGTH, - ciph->KEYLENGTH_MULTIPLE), - permutation(ciph) + permutation(ciph), + counter(256 * permutation->block_size()), + buffer(counter.size()), + position(0) { - position = 0; - - counter.resize(permutation->parallel_bytes()); - buffer.resize(counter.size()); } /* @@ -95,20 +91,18 @@ void CTR_BE::set_iv(const byte iv[], size_t iv_len) counter.copy(0, iv, iv_len); - const size_t PARALLEL_BLOCKS = counter.size() / BLOCK_SIZE; - - for(size_t i = 1; i != PARALLEL_BLOCKS; ++i) + for(size_t i = 1; i != 256; ++i) { counter.copy(i*BLOCK_SIZE, &counter[(i-1)*BLOCK_SIZE], BLOCK_SIZE); - for(s32bit j = BLOCK_SIZE - 1; j >= 0; --j) - if(++counter[i*BLOCK_SIZE+j]) + for(u32bit j = 0; j != BLOCK_SIZE; ++j) + if(++counter[i*BLOCK_SIZE + (BLOCK_SIZE-1-j)]) break; } - permutation->encrypt_n(&counter[0], &buffer[0], PARALLEL_BLOCKS); + permutation->encrypt_n(&counter[0], &buffer[0], 256); position = 0; } @@ -118,24 +112,15 @@ void CTR_BE::set_iv(const byte iv[], size_t iv_len) void CTR_BE::increment_counter() { const size_t BLOCK_SIZE = permutation->block_size(); - const size_t PARALLEL_BLOCKS = counter.size() / BLOCK_SIZE; - for(size_t i = 0; i != PARALLEL_BLOCKS; ++i) + for(size_t i = 0; i != 256; ++i) { - byte* this_ctr = &counter[i * BLOCK_SIZE]; - - byte last_byte = this_ctr[BLOCK_SIZE-1]; - last_byte += PARALLEL_BLOCKS; - - if(this_ctr[BLOCK_SIZE-1] > last_byte) - for(s32bit j = BLOCK_SIZE - 2; j >= 0; --j) - if(++this_ctr[j]) - break; - - this_ctr[BLOCK_SIZE-1] = last_byte; + for(u32bit j = 1; j != BLOCK_SIZE; ++j) + if(++counter[i*BLOCK_SIZE + (BLOCK_SIZE-1-j)]) + break; } - permutation->encrypt_n(&counter[0], &buffer[0], PARALLEL_BLOCKS); + permutation->encrypt_n(&counter[0], &buffer[0], 256); position = 0; } diff --git a/src/stream/ctr/ctr.h b/src/stream/ctr/ctr.h index e62ab2860..64b43b0f5 100644 --- a/src/stream/ctr/ctr.h +++ b/src/stream/ctr/ctr.h @@ -26,6 +26,11 @@ class BOTAN_DLL CTR_BE : public StreamCipher bool valid_iv_length(size_t iv_len) const { return (iv_len <= permutation->block_size()); } + Key_Length_Specification key_spec() const + { + return permutation->key_spec(); + } + std::string name() const; CTR_BE* clone() const diff --git a/src/stream/info.txt b/src/stream/info.txt index 68d6c46d6..c242b47e7 100644 --- a/src/stream/info.txt +++ b/src/stream/info.txt @@ -1,5 +1,5 @@ define STREAM_CIPHER <requires> -sym_algo +algo_base </requires> diff --git a/src/stream/ofb/ofb.cpp b/src/stream/ofb/ofb.cpp index 1f25c5c14..382a2b4dd 100644 --- a/src/stream/ofb/ofb.cpp +++ b/src/stream/ofb/ofb.cpp @@ -14,11 +14,7 @@ namespace Botan { /* * OFB Constructor */ -OFB::OFB(BlockCipher* ciph) : - StreamCipher(ciph->MINIMUM_KEYLENGTH, - ciph->MAXIMUM_KEYLENGTH, - ciph->KEYLENGTH_MULTIPLE), - permutation(ciph) +OFB::OFB(BlockCipher* ciph) : permutation(ciph) { position = 0; buffer.resize(permutation->block_size()); diff --git a/src/stream/ofb/ofb.h b/src/stream/ofb/ofb.h index 587a30bab..c4d8b2601 100644 --- a/src/stream/ofb/ofb.h +++ b/src/stream/ofb/ofb.h @@ -26,6 +26,11 @@ class BOTAN_DLL OFB : public StreamCipher bool valid_iv_length(size_t iv_len) const { return (iv_len <= permutation->block_size()); } + Key_Length_Specification key_spec() const + { + return permutation->key_spec(); + } + std::string name() const; OFB* clone() const diff --git a/src/stream/salsa20/salsa20.h b/src/stream/salsa20/salsa20.h index 213cb1117..d9645015f 100644 --- a/src/stream/salsa20/salsa20.h +++ b/src/stream/salsa20/salsa20.h @@ -25,14 +25,16 @@ class BOTAN_DLL Salsa20 : public StreamCipher bool valid_iv_length(size_t iv_len) const { return (iv_len == 8 || iv_len == 24); } + Key_Length_Specification key_spec() const + { + return Key_Length_Specification(16, 32, 16); + } + void clear(); std::string name() const; StreamCipher* clone() const { return new Salsa20; } - Salsa20() : StreamCipher(16, 32, 16), state(16), buffer(64) - { position = 0; } - - ~Salsa20() { clear(); } + Salsa20() : state(16), buffer(64), position(0) {} private: void key_schedule(const byte key[], size_t key_len); diff --git a/src/stream/stream_cipher.h b/src/stream/stream_cipher.h index 680d57f70..301e71f07 100644 --- a/src/stream/stream_cipher.h +++ b/src/stream/stream_cipher.h @@ -51,24 +51,6 @@ class BOTAN_DLL StreamCipher : public SymmetricAlgorithm * Get a new object representing the same algorithm as *this */ virtual StreamCipher* clone() const = 0; - - /** - * Zeroize internal state - */ - virtual void clear() = 0; - - /** - * StreamCipher constructor - * @param key_min the minimum key size - * @param key_max the maximum key size - * @param key_mod the modulo restriction on the key size - */ - StreamCipher(size_t key_min, - size_t key_max = 0, - size_t key_mod = 1) : - SymmetricAlgorithm(key_min, key_max, key_mod) {} - - virtual ~StreamCipher() {} }; } diff --git a/src/stream/turing/turing.cpp b/src/stream/turing/turing.cpp index 82e3aa2bb..619ef6682 100644 --- a/src/stream/turing/turing.cpp +++ b/src/stream/turing/turing.cpp @@ -210,13 +210,26 @@ void Turing::generate() */ u32bit Turing::fixedS(u32bit W) { - for(size_t i = 0; i != 4; ++i) - { - byte B = SBOX[get_byte(i, W)]; - W ^= rotate_left(Q_BOX[B], i*8); - W &= rotate_right(0x00FFFFFF, i*8); - W |= B << (24-i*8); - } + byte B = SBOX[get_byte(0, W)]; + W ^= Q_BOX[B]; + W &= 0x00FFFFFF; + W |= B << 24; + + B = SBOX[get_byte(1, W)]; + W ^= rotate_left(Q_BOX[B], 8); + W &= 0xFF00FFFF; + W |= B << 16; + + B = SBOX[get_byte(2, W)]; + W ^= rotate_left(Q_BOX[B], 16); + W &= 0xFFFF00FF; + W |= B << 8; + + B = SBOX[get_byte(3, W)]; + W ^= rotate_left(Q_BOX[B], 24); + W &= 0xFFFFFF00; + W |= B; + return W; } diff --git a/src/stream/turing/turing.h b/src/stream/turing/turing.h index adfabc0f1..aff314080 100644 --- a/src/stream/turing/turing.h +++ b/src/stream/turing/turing.h @@ -24,14 +24,17 @@ class BOTAN_DLL Turing : public StreamCipher bool valid_iv_length(size_t iv_len) const { return (iv_len % 4 == 0 && iv_len <= 16); } + Key_Length_Specification key_spec() const + { + return Key_Length_Specification(4, 32, 4); + } + void clear(); std::string name() const { return "Turing"; } StreamCipher* clone() const { return new Turing; } - Turing() : StreamCipher(4, 32, 4), - S0(256), S1(256), S2(256), S3(256), - R(17), buffer(340) - { position = 0; } + Turing() : S0(256), S1(256), S2(256), S3(256), + R(17), buffer(340), position(0) {} private: void key_schedule(const byte[], size_t); diff --git a/src/stream/wid_wake/wid_wake.h b/src/stream/wid_wake/wid_wake.h index 17e77d5b5..05842a574 100644 --- a/src/stream/wid_wake/wid_wake.h +++ b/src/stream/wid_wake/wid_wake.h @@ -27,14 +27,18 @@ class BOTAN_DLL WiderWake_41_BE : public StreamCipher bool valid_iv_length(size_t iv_len) const { return (iv_len == 8); } + Key_Length_Specification key_spec() const + { + return Key_Length_Specification(16); + } + void clear(); std::string name() const { return "WiderWake4+1-BE"; } StreamCipher* clone() const { return new WiderWake_41_BE; } - WiderWake_41_BE() : StreamCipher(16, 16, 1), - T(256), state(5), t_key(4), + WiderWake_41_BE() : T(256), state(5), t_key(4), buffer(DEFAULT_BUFFERSIZE), position(0) - { } + {} private: void key_schedule(const byte[], size_t); |