diff options
author | lloyd <[email protected]> | 2010-10-13 16:33:20 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2010-10-13 16:33:20 +0000 |
commit | b502cefaf0f9396354d58c4c18a78ac7870f6168 (patch) | |
tree | 8e4d699bd47bcdecaa6c3b670e19743d52047bb8 | |
parent | fc4c8f57baa06cfc9073ce83a5e3d1547bea86c0 (diff) | |
parent | a142500346e9bef5c4b0905103eac9a494d6822e (diff) |
propagate from branch 'net.randombit.botan' (head cba32f885eb7889a9711cbee120df42839deb9d0)
to branch 'net.randombit.botan.c++0x' (head 7cb9cdfda0f3dedab24f1d3bc7e7ea9b22164234)
30 files changed, 164 insertions, 153 deletions
diff --git a/doc/examples/new_engine.cpp b/doc/examples/new_engine.cpp index 6c7bc340a..ed4abf4d2 100644 --- a/doc/examples/new_engine.cpp +++ b/doc/examples/new_engine.cpp @@ -23,16 +23,16 @@ class XOR_Cipher : public StreamCipher XOR_Cipher() : StreamCipher(1, 32) { mask_pos = 0; } private: - void cipher(const byte in[], byte out[], u32bit length) + void cipher(const byte in[], byte out[], size_t length) { - for(u32bit j = 0; j != length; j++) + for(size_t j = 0; j != length; j++) { out[j] = in[j] ^ mask[mask_pos]; mask_pos = (mask_pos + 1) % mask.size(); } } - void key_schedule(const byte key[], u32bit length) + void key_schedule(const byte key[], size_t length) { mask.set(key, length); } diff --git a/doc/examples/package.cpp b/doc/examples/package.cpp index b907ac3ae..38a2e1666 100644 --- a/doc/examples/package.cpp +++ b/doc/examples/package.cpp @@ -51,13 +51,13 @@ int main(int argc, char* argv[]) BlockCipher* cipher = new Serpent; std::vector<byte> input = slurp_file(argv[1]); - std::vector<byte> output(input.size() + cipher->BLOCK_SIZE); + std::vector<byte> output(input.size() + cipher->block_size()); aont_package(rng, new Serpent, &input[0], input.size(), &output[0]); - std::vector<byte> unpackage_output(output.size() - cipher->BLOCK_SIZE); + std::vector<byte> unpackage_output(output.size() - cipher->block_size()); aont_unpackage(new Serpent, &output[0], output.size(), diff --git a/doc/examples/tls_client.cpp b/doc/examples/tls_client.cpp index 26e93dd2f..c17ffe4da 100644 --- a/doc/examples/tls_client.cpp +++ b/doc/examples/tls_client.cpp @@ -37,7 +37,9 @@ int main(int argc, char* argv[]) std::auto_ptr<Botan::RandomNumberGenerator> rng( Botan::RandomNumberGenerator::make_rng()); - TLS_Client tls(*rng, sock); + TLS_Policy policy; + + TLS_Client tls(policy, *rng, sock); printf("Handshake extablished...\n"); diff --git a/doc/examples/tls_server.cpp b/doc/examples/tls_server.cpp index ff4265937..e45a24759 100644 --- a/doc/examples/tls_server.cpp +++ b/doc/examples/tls_server.cpp @@ -44,6 +44,8 @@ int main(int argc, char* argv[]) Unix_Server_Socket listener(port); + TLS_Policy policy; + while(true) { try { @@ -53,7 +55,7 @@ int main(int argc, char* argv[]) printf("Got new connection\n"); - TLS_Server tls(rng, *sock, cert, key); + TLS_Server tls(policy, rng, *sock, cert, key); std::string hostname = tls.requested_hostname(); diff --git a/doc/log.txt b/doc/log.txt index 15904cd00..382ccd8d4 100644 --- a/doc/log.txt +++ b/doc/log.txt @@ -6,10 +6,13 @@ - Add hex encoding/decoding functions that can be used without a Pipe - Add support for dynamic engine loading on Windows - Allow using PBKDF2 with empty passphrases + - Switch default PKCS #8 encryption algorithm from AES-128 to AES-256 + - Support use of HMAC(SHA-256) and CMAC(Blowfish) in passhash9 + - Use size_t instead of u32bit for length fields + - Replace BlockCipher::BLOCK_SIZE attribute with function block_size() + - Replace HashFunction::HASH_BLOCK_SIZE attribute with hash_block_size() - Changed semantics of MemoryRegion::resize and clear to match STL - Removed MemoryRegion::append, replaced by push_back and operator+= - - Support use of HMAC(SHA-256) and CMAC(Blowfish) in passhash9 - - Switch default PKCS #8 encryption algorithm from AES-128 to AES-256 - Improve support for Intel Atom processors - Fix compilation under Sun Studio diff --git a/src/block/aes/aes.cpp b/src/block/aes/aes.cpp index 1530af965..b317fa735 100644 --- a/src/block/aes/aes.cpp +++ b/src/block/aes/aes.cpp @@ -9,6 +9,9 @@ #include <botan/loadstor.h> #include <botan/rotate.h> +#include <assert.h> +#include <stdio.h> + namespace Botan { namespace { @@ -454,35 +457,25 @@ void AES::encrypt_n(const byte in[], byte out[], size_t blocks) const rotate_right(TE[get_byte(2, T1)], 16) ^ rotate_right(TE[get_byte(3, T2)], 24) ^ EK[7]; - for(u32bit j = 2; j != ROUNDS; j += 2) + for(u32bit r = 2*4; r < EK.size(); r += 2*4) { - const u32bit K0 = EK[4*j]; - const u32bit K1 = EK[4*j+1]; - const u32bit K2 = EK[4*j+2]; - const u32bit K3 = EK[4*j+3]; - T0 = TE0[get_byte(0, B0)] ^ TE1[get_byte(1, B1)] ^ - TE2[get_byte(2, B2)] ^ TE3[get_byte(3, B3)] ^ K0; + TE2[get_byte(2, B2)] ^ TE3[get_byte(3, B3)] ^ EK[r]; T1 = TE0[get_byte(0, B1)] ^ TE1[get_byte(1, B2)] ^ - TE2[get_byte(2, B3)] ^ TE3[get_byte(3, B0)] ^ K1; + TE2[get_byte(2, B3)] ^ TE3[get_byte(3, B0)] ^ EK[r+1]; T2 = TE0[get_byte(0, B2)] ^ TE1[get_byte(1, B3)] ^ - TE2[get_byte(2, B0)] ^ TE3[get_byte(3, B1)] ^ K2; + TE2[get_byte(2, B0)] ^ TE3[get_byte(3, B1)] ^ EK[r+2]; T3 = TE0[get_byte(0, B3)] ^ TE1[get_byte(1, B0)] ^ - TE2[get_byte(2, B1)] ^ TE3[get_byte(3, B2)] ^ K3; - - const u32bit K4 = EK[4*(j+1)+0]; - const u32bit K5 = EK[4*(j+1)+1]; - const u32bit K6 = EK[4*(j+1)+2]; - const u32bit K7 = EK[4*(j+1)+3]; + TE2[get_byte(2, B1)] ^ TE3[get_byte(3, B2)] ^ EK[r+3]; B0 = TE0[get_byte(0, T0)] ^ TE1[get_byte(1, T1)] ^ - TE2[get_byte(2, T2)] ^ TE3[get_byte(3, T3)] ^ K4; + TE2[get_byte(2, T2)] ^ TE3[get_byte(3, T3)] ^ EK[r+4]; B1 = TE0[get_byte(0, T1)] ^ TE1[get_byte(1, T2)] ^ - TE2[get_byte(2, T3)] ^ TE3[get_byte(3, T0)] ^ K5; + TE2[get_byte(2, T3)] ^ TE3[get_byte(3, T0)] ^ EK[r+5]; B2 = TE0[get_byte(0, T2)] ^ TE1[get_byte(1, T3)] ^ - TE2[get_byte(2, T0)] ^ TE3[get_byte(3, T1)] ^ K6; + TE2[get_byte(2, T0)] ^ TE3[get_byte(3, T1)] ^ EK[r+6]; B3 = TE0[get_byte(0, T3)] ^ TE1[get_byte(1, T0)] ^ - TE2[get_byte(2, T1)] ^ TE3[get_byte(3, T2)] ^ K7; + TE2[get_byte(2, T1)] ^ TE3[get_byte(3, T2)] ^ EK[r+7]; } /* @@ -563,35 +556,25 @@ void AES::decrypt_n(const byte in[], byte out[], size_t blocks) const rotate_right(TD[get_byte(2, T1)], 16) ^ rotate_right(TD[get_byte(3, T0)], 24) ^ DK[7]; - for(u32bit j = 2; j != ROUNDS; j += 2) + for(u32bit r = 2*4; r < DK.size(); r += 2*4) { - const u32bit K0 = DK[4*j+0]; - const u32bit K1 = DK[4*j+1]; - const u32bit K2 = DK[4*j+2]; - const u32bit K3 = DK[4*j+3]; - T0 = TD0[get_byte(0, B0)] ^ TD1[get_byte(1, B3)] ^ - TD2[get_byte(2, B2)] ^ TD3[get_byte(3, B1)] ^ K0; + TD2[get_byte(2, B2)] ^ TD3[get_byte(3, B1)] ^ DK[r]; T1 = TD0[get_byte(0, B1)] ^ TD1[get_byte(1, B0)] ^ - TD2[get_byte(2, B3)] ^ TD3[get_byte(3, B2)] ^ K1; + TD2[get_byte(2, B3)] ^ TD3[get_byte(3, B2)] ^ DK[r+1]; T2 = TD0[get_byte(0, B2)] ^ TD1[get_byte(1, B1)] ^ - TD2[get_byte(2, B0)] ^ TD3[get_byte(3, B3)] ^ K2; + TD2[get_byte(2, B0)] ^ TD3[get_byte(3, B3)] ^ DK[r+2]; T3 = TD0[get_byte(0, B3)] ^ TD1[get_byte(1, B2)] ^ - TD2[get_byte(2, B1)] ^ TD3[get_byte(3, B0)] ^ K3; - - const u32bit K4 = DK[4*(j+1)+0]; - const u32bit K5 = DK[4*(j+1)+1]; - const u32bit K6 = DK[4*(j+1)+2]; - const u32bit K7 = DK[4*(j+1)+3]; + TD2[get_byte(2, B1)] ^ TD3[get_byte(3, B0)] ^ DK[r+3]; B0 = TD0[get_byte(0, T0)] ^ TD1[get_byte(1, T3)] ^ - TD2[get_byte(2, T2)] ^ TD3[get_byte(3, T1)] ^ K4; + TD2[get_byte(2, T2)] ^ TD3[get_byte(3, T1)] ^ DK[r+4]; B1 = TD0[get_byte(0, T1)] ^ TD1[get_byte(1, T0)] ^ - TD2[get_byte(2, T3)] ^ TD3[get_byte(3, T2)] ^ K5; + TD2[get_byte(2, T3)] ^ TD3[get_byte(3, T2)] ^ DK[r+5]; B2 = TD0[get_byte(0, T2)] ^ TD1[get_byte(1, T1)] ^ - TD2[get_byte(2, T0)] ^ TD3[get_byte(3, T3)] ^ K6; + TD2[get_byte(2, T0)] ^ TD3[get_byte(3, T3)] ^ DK[r+6]; B3 = TD0[get_byte(0, T3)] ^ TD1[get_byte(1, T2)] ^ - TD2[get_byte(2, T1)] ^ TD3[get_byte(3, T0)] ^ K7; + TD2[get_byte(2, T1)] ^ TD3[get_byte(3, T0)] ^ DK[r+7]; } out[ 0] = SD[get_byte(0, B0)] ^ MD[0]; @@ -625,7 +608,7 @@ void AES::key_schedule(const byte key[], size_t length) 0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000, 0x20000000, 0x40000000, 0x80000000, 0x1B000000, 0x36000000 }; - ROUNDS = (length / 4) + 6; + const u32bit rounds = (length / 4) + 6; SecureVector<u32bit> XEK(64), XDK(64); @@ -633,7 +616,7 @@ void AES::key_schedule(const byte key[], size_t length) for(size_t i = 0; i != X; ++i) XEK[i] = load_be<u32bit>(key, i); - for(size_t i = X; i < 4*(ROUNDS+1); i += X) + for(size_t i = X; i < 4*(rounds+1); i += X) { XEK[i] = XEK[i-X] ^ S(rotate_left(XEK[i-1], 8)) ^ RC[(i-X)/X]; for(size_t j = 1; j != X; ++j) @@ -645,12 +628,12 @@ void AES::key_schedule(const byte key[], size_t length) } } - for(size_t i = 0; i != 4*(ROUNDS+1); i += 4) + for(size_t i = 0; i != 4*(rounds+1); i += 4) { - XDK[i ] = XEK[4*ROUNDS-i ]; - XDK[i+1] = XEK[4*ROUNDS-i+1]; - XDK[i+2] = XEK[4*ROUNDS-i+2]; - XDK[i+3] = XEK[4*ROUNDS-i+3]; + XDK[i ] = XEK[4*rounds-i ]; + XDK[i+1] = XEK[4*rounds-i+1]; + XDK[i+2] = XEK[4*rounds-i+2]; + XDK[i+3] = XEK[4*rounds-i+3]; } for(size_t i = 4; i != length + 24; ++i) @@ -661,12 +644,12 @@ void AES::key_schedule(const byte key[], size_t length) for(size_t i = 0; i != 4; ++i) { - store_be(XEK[i+4*ROUNDS], &ME[4*i]); + store_be(XEK[i+4*rounds], &ME[4*i]); store_be(XEK[i], &MD[4*i]); } - EK.copy(&XEK[0], length + 24); - DK.copy(&XDK[0], length + 24); + EK.set(&XEK[0], length + 24); + DK.set(&XDK[0], length + 24); } /* @@ -681,12 +664,20 @@ u32bit AES::S(u32bit input) /* * AES Constructor */ -AES::AES(u32bit key_size) : BlockCipher_Fixed_Block_Size(key_size), - EK(56), ME(16), DK(56), MD(16) +AES::AES() : BlockCipher_Fixed_Block_Size(16, 32, 8), + EK(0), ME(16), DK(0), MD(16) + { + } + +/* +* AES Constructor +*/ +AES::AES(size_t key_size) : BlockCipher_Fixed_Block_Size(key_size), + EK(key_size+24), ME(16), + DK(key_size+24), MD(16) { if(key_size != 16 && key_size != 24 && key_size != 32) throw Invalid_Key_Length(name(), key_size); - ROUNDS = (key_size / 4) + 6; } /* diff --git a/src/block/aes/aes.h b/src/block/aes/aes.h index 6fa0ccaff..d2e051f83 100644 --- a/src/block/aes/aes.h +++ b/src/block/aes/aes.h @@ -26,21 +26,17 @@ class BOTAN_DLL AES : public BlockCipher_Fixed_Block_Size<16> void clear(); BlockCipher* clone() const { return new AES; } - AES() : BlockCipher_Fixed_Block_Size(16, 32, 8), - EK(56), ME(16), DK(56), MD(16) - { ROUNDS = 14; } + AES(); /** * AES fixed to a particular key_size (16, 24, or 32 bytes) * @param key_size the chosen fixed key size */ - AES(u32bit key_size); + AES(size_t key_size); private: void key_schedule(const byte[], size_t); static u32bit S(u32bit); - u32bit ROUNDS; - SecureVector<u32bit> EK; SecureVector<byte> ME; diff --git a/src/block/block_cipher.h b/src/block/block_cipher.h index e522005b9..3e14e0739 100644 --- a/src/block/block_cipher.h +++ b/src/block/block_cipher.h @@ -25,9 +25,9 @@ class BOTAN_DLL BlockCipher : public SymmetricAlgorithm * @param key_max the maximum key size * @param key_mod the modulo restriction on the key size */ - BlockCipher(u32bit key_min, - u32bit key_max = 0, - u32bit key_mod = 1) : + BlockCipher(size_t key_min, + size_t key_max = 0, + size_t key_mod = 1) : SymmetricAlgorithm(key_min, key_max, key_mod) {} virtual ~BlockCipher() {} @@ -119,9 +119,9 @@ template<size_t N> class BlockCipher_Fixed_Block_Size : public BlockCipher { public: - BlockCipher_Fixed_Block_Size(u32bit kmin, - u32bit kmax = 0, - u32bit kmod = 1) : + BlockCipher_Fixed_Block_Size(size_t kmin, + size_t kmax = 0, + size_t kmod = 1) : BlockCipher(kmin, kmax, kmod) {} enum { BLOCK_SIZE = N }; diff --git a/src/block/des/des.cpp b/src/block/des/des.cpp index 15c771bda..739dfe87c 100644 --- a/src/block/des/des.cpp +++ b/src/block/des/des.cpp @@ -50,11 +50,11 @@ void des_key_schedule(u32bit round_key[32], const byte key[8]) ((key[3] & 0x10) >> 1) | ((key[2] & 0x10) >> 2) | ((key[1] & 0x10) >> 3) | ((key[0] & 0x10) >> 4); - for(u32bit j = 0; j != 16; ++j) + for(size_t i = 0; i != 16; ++i) { - C = ((C << ROT[j]) | (C >> (28-ROT[j]))) & 0x0FFFFFFF; - D = ((D << ROT[j]) | (D >> (28-ROT[j]))) & 0x0FFFFFFF; - round_key[2*j ] = ((C & 0x00000010) << 22) | ((C & 0x00000800) << 17) | + C = ((C << ROT[i]) | (C >> (28-ROT[i]))) & 0x0FFFFFFF; + D = ((D << ROT[i]) | (D >> (28-ROT[i]))) & 0x0FFFFFFF; + round_key[2*i ] = ((C & 0x00000010) << 22) | ((C & 0x00000800) << 17) | ((C & 0x00000020) << 16) | ((C & 0x00004004) << 15) | ((C & 0x00000200) << 11) | ((C & 0x00020000) << 10) | ((C & 0x01000000) >> 6) | ((C & 0x00100000) >> 4) | @@ -65,7 +65,7 @@ void des_key_schedule(u32bit round_key[32], const byte key[8]) ((D & 0x00000088) >> 3) | ((D & 0x00001000) >> 7) | ((D & 0x00080000) >> 9) | ((D & 0x02020000) >> 14) | ((D & 0x00400000) >> 21); - round_key[2*j+1] = ((C & 0x00000001) << 28) | ((C & 0x00000082) << 18) | + round_key[2*i+1] = ((C & 0x00000001) << 28) | ((C & 0x00000082) << 18) | ((C & 0x00002000) << 14) | ((C & 0x00000100) << 10) | ((C & 0x00001000) << 9) | ((C & 0x00040000) << 6) | ((C & 0x02400000) << 4) | ((C & 0x00008000) << 2) | @@ -85,20 +85,20 @@ void des_key_schedule(u32bit round_key[32], const byte key[8]) void des_encrypt(u32bit& L, u32bit& R, const u32bit round_key[32]) { - for(u32bit j = 0; j != 16; j += 2) + for(size_t i = 0; i != 16; i += 2) { u32bit T0, T1; - T0 = rotate_right(R, 4) ^ round_key[2*j]; - T1 = R ^ round_key[2*j + 1]; + T0 = rotate_right(R, 4) ^ round_key[2*i]; + T1 = R ^ round_key[2*i + 1]; L ^= DES_SPBOX1[get_byte(0, T0)] ^ DES_SPBOX2[get_byte(0, T1)] ^ DES_SPBOX3[get_byte(1, T0)] ^ DES_SPBOX4[get_byte(1, T1)] ^ DES_SPBOX5[get_byte(2, T0)] ^ DES_SPBOX6[get_byte(2, T1)] ^ DES_SPBOX7[get_byte(3, T0)] ^ DES_SPBOX8[get_byte(3, T1)]; - T0 = rotate_right(L, 4) ^ round_key[2*j + 2]; - T1 = L ^ round_key[2*j + 3]; + T0 = rotate_right(L, 4) ^ round_key[2*i + 2]; + T1 = L ^ round_key[2*i + 3]; R ^= DES_SPBOX1[get_byte(0, T0)] ^ DES_SPBOX2[get_byte(0, T1)] ^ DES_SPBOX3[get_byte(1, T0)] ^ DES_SPBOX4[get_byte(1, T1)] ^ @@ -113,20 +113,20 @@ void des_encrypt(u32bit& L, u32bit& R, void des_decrypt(u32bit& L, u32bit& R, const u32bit round_key[32]) { - for(u32bit j = 16; j != 0; j -= 2) + for(size_t i = 16; i != 0; i -= 2) { u32bit T0, T1; - T0 = rotate_right(R, 4) ^ round_key[2*j - 2]; - T1 = R ^ round_key[2*j - 1]; + T0 = rotate_right(R, 4) ^ round_key[2*i - 2]; + T1 = R ^ round_key[2*i - 1]; L ^= DES_SPBOX1[get_byte(0, T0)] ^ DES_SPBOX2[get_byte(0, T1)] ^ DES_SPBOX3[get_byte(1, T0)] ^ DES_SPBOX4[get_byte(1, T1)] ^ DES_SPBOX5[get_byte(2, T0)] ^ DES_SPBOX6[get_byte(2, T1)] ^ DES_SPBOX7[get_byte(3, T0)] ^ DES_SPBOX8[get_byte(3, T1)]; - T0 = rotate_right(L, 4) ^ round_key[2*j - 4]; - T1 = L ^ round_key[2*j - 3]; + T0 = rotate_right(L, 4) ^ round_key[2*i - 4]; + T1 = L ^ round_key[2*i - 3]; R ^= DES_SPBOX1[get_byte(0, T0)] ^ DES_SPBOX2[get_byte(0, T1)] ^ DES_SPBOX3[get_byte(1, T0)] ^ DES_SPBOX4[get_byte(1, T1)] ^ diff --git a/src/block/idea/idea.cpp b/src/block/idea/idea.cpp index be7680b2c..f9b6cc874 100644 --- a/src/block/idea/idea.cpp +++ b/src/block/idea/idea.cpp @@ -60,7 +60,7 @@ u16bit mul_inv(u16bit x) */ void idea_op(const byte in[], byte out[], size_t blocks, const u16bit K[52]) { - const u32bit BLOCK_SIZE = 8; + const size_t BLOCK_SIZE = 8; for(size_t i = 0; i != blocks; ++i) { diff --git a/src/block/rc5/rc5.cpp b/src/block/rc5/rc5.cpp index ebcbaf69f..d08b44425 100644 --- a/src/block/rc5/rc5.cpp +++ b/src/block/rc5/rc5.cpp @@ -18,20 +18,25 @@ namespace Botan { */ void RC5::encrypt_n(const byte in[], byte out[], size_t blocks) const { + const size_t rounds = (S.size() - 2) / 2; + for(size_t i = 0; i != blocks; ++i) { u32bit A = load_le<u32bit>(in, 0); u32bit B = load_le<u32bit>(in, 1); A += S[0]; B += S[1]; - for(size_t j = 0; j != ROUNDS; j += 4) + for(size_t j = 0; j != rounds; j += 4) { A = rotate_left(A ^ B, B % 32) + S[2*j+2]; B = rotate_left(B ^ A, A % 32) + S[2*j+3]; + A = rotate_left(A ^ B, B % 32) + S[2*j+4]; B = rotate_left(B ^ A, A % 32) + S[2*j+5]; + A = rotate_left(A ^ B, B % 32) + S[2*j+6]; B = rotate_left(B ^ A, A % 32) + S[2*j+7]; + A = rotate_left(A ^ B, B % 32) + S[2*j+8]; B = rotate_left(B ^ A, A % 32) + S[2*j+9]; } @@ -48,19 +53,24 @@ void RC5::encrypt_n(const byte in[], byte out[], size_t blocks) const */ void RC5::decrypt_n(const byte in[], byte out[], size_t blocks) const { + const size_t rounds = (S.size() - 2) / 2; + for(size_t i = 0; i != blocks; ++i) { u32bit A = load_le<u32bit>(in, 0); u32bit B = load_le<u32bit>(in, 1); - for(size_t j = ROUNDS; j != 0; j -= 4) + for(size_t j = rounds; j != 0; j -= 4) { B = rotate_right(B - S[2*j+1], A % 32) ^ A; A = rotate_right(A - S[2*j ], B % 32) ^ B; + B = rotate_right(B - S[2*j-1], A % 32) ^ A; A = rotate_right(A - S[2*j-2], B % 32) ^ B; + B = rotate_right(B - S[2*j-3], A % 32) ^ A; A = rotate_right(A - S[2*j-4], B % 32) ^ B; + B = rotate_right(B - S[2*j-5], A % 32) ^ A; A = rotate_right(A - S[2*j-6], B % 32) ^ B; } @@ -106,19 +116,19 @@ void RC5::key_schedule(const byte key[], size_t length) */ std::string RC5::name() const { - return "RC5(" + std::to_string(ROUNDS) + ")"; + return "RC5(" + std::to_string(get_rounds()) + ")"; } /* * RC5 Constructor */ -RC5::RC5(size_t r) : - BlockCipher_Fixed_Block_Size(1, 32), - ROUNDS(r) +RC5::RC5(size_t rounds) : BlockCipher_Fixed_Block_Size(1, 32) { - if(ROUNDS < 8 || ROUNDS > 32 || (ROUNDS % 4 != 0)) - throw Invalid_Argument(name() + ": Invalid number of rounds"); - S.resize(2*ROUNDS + 2); + if(rounds < 8 || rounds > 32 || (rounds % 4 != 0)) + throw Invalid_Argument("RC5: Invalid number of rounds " + + std::to_string(rounds)); + + S.resize(2*rounds + 2); } } diff --git a/src/block/rc5/rc5.h b/src/block/rc5/rc5.h index a9f3b5b0e..cb282af4e 100644 --- a/src/block/rc5/rc5.h +++ b/src/block/rc5/rc5.h @@ -23,7 +23,7 @@ class BOTAN_DLL RC5 : public BlockCipher_Fixed_Block_Size<8> void clear() { zeroise(S); } std::string name() const; - BlockCipher* clone() const { return new RC5(ROUNDS); } + BlockCipher* clone() const { return new RC5(get_rounds()); } /** * @param rounds the number of RC5 rounds to run. Must be between @@ -31,9 +31,11 @@ class BOTAN_DLL RC5 : public BlockCipher_Fixed_Block_Size<8> */ RC5(size_t rounds); private: + size_t get_rounds() const { return (S.size() - 2) / 2; } + void key_schedule(const byte[], size_t); + SecureVector<u32bit> S; - const size_t ROUNDS; }; } diff --git a/src/block/safer/safer_sk.cpp b/src/block/safer/safer_sk.cpp index 2e6c3a1d6..a91e5f687 100644 --- a/src/block/safer/safer_sk.cpp +++ b/src/block/safer/safer_sk.cpp @@ -17,12 +17,14 @@ namespace Botan { */ void SAFER_SK::encrypt_n(const byte in[], byte out[], size_t blocks) const { + const size_t rounds = get_rounds(); + for(size_t i = 0; i != blocks; ++i) { byte A = in[0], B = in[1], C = in[2], D = in[3], E = in[4], F = in[5], G = in[6], H = in[7], X, Y; - for(size_t j = 0; j != 16*ROUNDS; j += 16) + for(size_t j = 0; j != 16*rounds; j += 16) { A = EXP[A ^ EK[j ]]; B = LOG[B + EK[j+1]]; C = LOG[C + EK[j+2]]; D = EXP[D ^ EK[j+3]]; @@ -38,10 +40,10 @@ void SAFER_SK::encrypt_n(const byte in[], byte out[], size_t blocks) const A += B; F = C + G; E = C + F; C = X; G = Y; } - out[0] = A ^ EK[16*ROUNDS+0]; out[1] = B + EK[16*ROUNDS+1]; - out[2] = C + EK[16*ROUNDS+2]; out[3] = D ^ EK[16*ROUNDS+3]; - out[4] = E ^ EK[16*ROUNDS+4]; out[5] = F + EK[16*ROUNDS+5]; - out[6] = G + EK[16*ROUNDS+6]; out[7] = H ^ EK[16*ROUNDS+7]; + out[0] = A ^ EK[16*rounds+0]; out[1] = B + EK[16*rounds+1]; + out[2] = C + EK[16*rounds+2]; out[3] = D ^ EK[16*rounds+3]; + out[4] = E ^ EK[16*rounds+4]; out[5] = F + EK[16*rounds+5]; + out[6] = G + EK[16*rounds+6]; out[7] = H ^ EK[16*rounds+7]; in += BLOCK_SIZE; out += BLOCK_SIZE; @@ -53,16 +55,18 @@ void SAFER_SK::encrypt_n(const byte in[], byte out[], size_t blocks) const */ void SAFER_SK::decrypt_n(const byte in[], byte out[], size_t blocks) const { + const size_t rounds = get_rounds(); + for(size_t i = 0; i != blocks; ++i) { byte A = in[0], B = in[1], C = in[2], D = in[3], E = in[4], F = in[5], G = in[6], H = in[7]; - A ^= EK[16*ROUNDS+0]; B -= EK[16*ROUNDS+1]; C -= EK[16*ROUNDS+2]; - D ^= EK[16*ROUNDS+3]; E ^= EK[16*ROUNDS+4]; F -= EK[16*ROUNDS+5]; - G -= EK[16*ROUNDS+6]; H ^= EK[16*ROUNDS+7]; + A ^= EK[16*rounds+0]; B -= EK[16*rounds+1]; C -= EK[16*rounds+2]; + D ^= EK[16*rounds+3]; E ^= EK[16*rounds+4]; F -= EK[16*rounds+5]; + G -= EK[16*rounds+6]; H ^= EK[16*rounds+7]; - for(s32bit j = 16*(ROUNDS-1); j >= 0; j -= 16) + for(s32bit j = 16*(rounds-1); j >= 0; j -= 16) { byte T = E; E = B; B = C; C = T; T = F; F = D; D = G; G = T; A -= E; B -= F; C -= G; D -= H; E -= A; F -= B; G -= C; H -= D; @@ -99,7 +103,7 @@ void SAFER_SK::key_schedule(const byte key[], size_t) KB[17] ^= KB[i+9] = EK[i] = key[i+8]; } - for(size_t i = 0; i != ROUNDS; ++i) + for(size_t i = 0; i != get_rounds(); ++i) { for(size_t j = 0; j != 18; ++j) KB[j] = rotate_left(KB[j], 6); @@ -113,7 +117,7 @@ void SAFER_SK::key_schedule(const byte key[], size_t) */ std::string SAFER_SK::name() const { - return "SAFER-SK(" + std::to_string(ROUNDS) + ")"; + return "SAFER-SK(" + std::to_string(get_rounds()) + ")"; } /* @@ -121,18 +125,19 @@ std::string SAFER_SK::name() const */ BlockCipher* SAFER_SK::clone() const { - return new SAFER_SK(ROUNDS); + return new SAFER_SK(get_rounds()); } /* * SAFER-SK Constructor */ SAFER_SK::SAFER_SK(size_t rounds) : - BlockCipher_Fixed_Block_Size(16), - EK(16 * rounds + 8), ROUNDS(rounds) + BlockCipher_Fixed_Block_Size(16) { - if(ROUNDS > 13 || ROUNDS == 0) + if(rounds > 13 || rounds == 0) throw Invalid_Argument(name() + ": Invalid number of rounds"); + + EK.resize(16 * rounds + 8); } } diff --git a/src/block/safer/safer_sk.h b/src/block/safer/safer_sk.h index 5e8d32b0a..2fde757bd 100644 --- a/src/block/safer/safer_sk.h +++ b/src/block/safer/safer_sk.h @@ -31,6 +31,7 @@ class BOTAN_DLL SAFER_SK : public BlockCipher_Fixed_Block_Size<8> */ SAFER_SK(size_t rounds); private: + size_t get_rounds() const { return (EK.size() - 8) / 16; } void key_schedule(const byte[], size_t); static const byte EXP[256]; @@ -39,7 +40,6 @@ class BOTAN_DLL SAFER_SK : public BlockCipher_Fixed_Block_Size<8> static const byte KEY_INDEX[208]; SecureVector<byte> EK; - const size_t ROUNDS; }; } diff --git a/src/block/xtea_simd/xtea_simd.cpp b/src/block/xtea_simd/xtea_simd.cpp index 831cc0359..d684eca5a 100644 --- a/src/block/xtea_simd/xtea_simd.cpp +++ b/src/block/xtea_simd/xtea_simd.cpp @@ -22,7 +22,7 @@ void xtea_encrypt_8(const byte in[64], byte out[64], const u32bit EK[64]) SIMD_32::transpose(L0, R0, L1, R1); - for(u32bit i = 0; i != 32; i += 2) + for(size_t i = 0; i != 32; i += 2) { SIMD_32 K0(EK[2*i ]); SIMD_32 K1(EK[2*i+1]); @@ -59,7 +59,7 @@ void xtea_decrypt_8(const byte in[64], byte out[64], const u32bit EK[64]) SIMD_32::transpose(L0, R0, L1, R1); - for(u32bit i = 0; i != 32; i += 2) + for(size_t i = 0; i != 32; i += 2) { SIMD_32 K0(EK[63 - 2*i]); SIMD_32 K1(EK[62 - 2*i]); diff --git a/src/engine/core_engine/core_modes.cpp b/src/engine/core_engine/core_modes.cpp index 7bd981c21..7cf7cf460 100644 --- a/src/engine/core_engine/core_modes.cpp +++ b/src/engine/core_engine/core_modes.cpp @@ -140,7 +140,7 @@ Keyed_Filter* get_cipher_mode(const BlockCipher* block_cipher, if(mode.find("CFB") != std::string::npos || mode.find("EAX") != std::string::npos) { - u32bit bits = 0; + size_t bits = 0; std::vector<std::string> algo_info = parse_algorithm_name(mode); std::string mode_name = algo_info[0]; diff --git a/src/engine/core_engine/lookup_block.cpp b/src/engine/core_engine/lookup_block.cpp index cbfaf2972..77436c8c1 100644 --- a/src/engine/core_engine/lookup_block.cpp +++ b/src/engine/core_engine/lookup_block.cpp @@ -257,7 +257,7 @@ BlockCipher* Core_Engine::find_block_cipher(const SCAN_Name& request, #if defined(BOTAN_HAS_LION) if(request.algo_name() == "Lion" && request.arg_count_between(2, 3)) { - const u32bit block_size = request.arg_as_integer(2, 1024); + const size_t block_size = request.arg_as_integer(2, 1024); const HashFunction* hash = af.prototype_hash_function(request.arg(0)); diff --git a/src/engine/gnump/gmp_mem.cpp b/src/engine/gnump/gmp_mem.cpp index f3650e716..7cf11654d 100644 --- a/src/engine/gnump/gmp_mem.cpp +++ b/src/engine/gnump/gmp_mem.cpp @@ -17,7 +17,7 @@ namespace { * Allocator used by GNU MP */ Allocator* gmp_alloc = 0; -u32bit gmp_alloc_refcnt = 0; +size_t gmp_alloc_refcnt = 0; /* * Allocation Function for GNU MP diff --git a/src/engine/gnump/gmp_wrap.cpp b/src/engine/gnump/gmp_wrap.cpp index 39d107a78..107823ab3 100644 --- a/src/engine/gnump/gmp_wrap.cpp +++ b/src/engine/gnump/gmp_wrap.cpp @@ -32,7 +32,7 @@ GMP_MPZ::GMP_MPZ(const BigInt& in) /* * GMP_MPZ Constructor */ -GMP_MPZ::GMP_MPZ(const byte in[], u32bit length) +GMP_MPZ::GMP_MPZ(const byte in[], size_t length) { mpz_init(value); mpz_import(value, length, 1, 1, 0, 0, in); @@ -66,7 +66,7 @@ GMP_MPZ& GMP_MPZ::operator=(const GMP_MPZ& other) /* * Export the mpz_t as a bytestring */ -void GMP_MPZ::encode(byte out[], u32bit length) const +void GMP_MPZ::encode(byte out[], size_t length) const { size_t dummy = 0; mpz_export(out + (length - bytes()), &dummy, 1, 1, 0, 0, value); @@ -75,7 +75,7 @@ void GMP_MPZ::encode(byte out[], u32bit length) const /* * Return the number of significant bytes */ -u32bit GMP_MPZ::bytes() const +size_t GMP_MPZ::bytes() const { return ((mpz_sizeinbase(value, 2) + 7) / 8); } diff --git a/src/engine/gnump/gmp_wrap.h b/src/engine/gnump/gmp_wrap.h index 52d130d6b..fc7aa856e 100644 --- a/src/engine/gnump/gmp_wrap.h +++ b/src/engine/gnump/gmp_wrap.h @@ -22,8 +22,8 @@ class GMP_MPZ mpz_t value; BigInt to_bigint() const; - void encode(byte[], u32bit) const; - u32bit bytes() const; + void encode(byte[], size_t) const; + size_t bytes() const; SecureVector<byte> to_bytes() const { return BigInt::encode(to_bigint()); } @@ -32,7 +32,7 @@ class GMP_MPZ GMP_MPZ(const GMP_MPZ&); GMP_MPZ(const BigInt& = 0); - GMP_MPZ(const byte[], u32bit); + GMP_MPZ(const byte[], size_t); ~GMP_MPZ(); }; diff --git a/src/engine/openssl/bn_wrap.cpp b/src/engine/openssl/bn_wrap.cpp index 6f1b5ef25..779956824 100644 --- a/src/engine/openssl/bn_wrap.cpp +++ b/src/engine/openssl/bn_wrap.cpp @@ -23,7 +23,7 @@ OSSL_BN::OSSL_BN(const BigInt& in) /* * OSSL_BN Constructor */ -OSSL_BN::OSSL_BN(const byte in[], u32bit length) +OSSL_BN::OSSL_BN(const byte in[], size_t length) { value = BN_new(); BN_bin2bn(in, length, value); @@ -57,7 +57,7 @@ OSSL_BN& OSSL_BN::operator=(const OSSL_BN& other) /* * Export the BIGNUM as a bytestring */ -void OSSL_BN::encode(byte out[], u32bit length) const +void OSSL_BN::encode(byte out[], size_t length) const { BN_bn2bin(value, out + (length - bytes())); } @@ -65,7 +65,7 @@ void OSSL_BN::encode(byte out[], u32bit length) const /* * Return the number of significant bytes */ -u32bit OSSL_BN::bytes() const +size_t OSSL_BN::bytes() const { return BN_num_bytes(value); } diff --git a/src/engine/openssl/bn_wrap.h b/src/engine/openssl/bn_wrap.h index 372f5a329..c5c07a35c 100644 --- a/src/engine/openssl/bn_wrap.h +++ b/src/engine/openssl/bn_wrap.h @@ -22,8 +22,8 @@ class OSSL_BN BIGNUM* value; BigInt to_bigint() const; - void encode(byte[], u32bit) const; - u32bit bytes() const; + void encode(byte[], size_t) const; + size_t bytes() const; SecureVector<byte> to_bytes() const { return BigInt::encode(to_bigint()); } @@ -32,7 +32,7 @@ class OSSL_BN OSSL_BN(const OSSL_BN&); OSSL_BN(const BigInt& = 0); - OSSL_BN(const byte[], u32bit); + OSSL_BN(const byte[], size_t); ~OSSL_BN(); }; diff --git a/src/engine/openssl/ossl_bc.cpp b/src/engine/openssl/ossl_bc.cpp index 64b5fa3b4..74f0316dc 100644 --- a/src/engine/openssl/ossl_bc.cpp +++ b/src/engine/openssl/ossl_bc.cpp @@ -27,7 +27,7 @@ class EVP_BlockCipher : public BlockCipher EVP_BlockCipher(const EVP_CIPHER*, const std::string&); EVP_BlockCipher(const EVP_CIPHER*, const std::string&, - u32bit, u32bit, u32bit); + size_t, size_t, size_t); ~EVP_BlockCipher(); private: @@ -67,8 +67,8 @@ EVP_BlockCipher::EVP_BlockCipher(const EVP_CIPHER* algo, */ EVP_BlockCipher::EVP_BlockCipher(const EVP_CIPHER* algo, const std::string& algo_name, - u32bit key_min, u32bit key_max, - u32bit key_mod) : + size_t key_min, size_t key_max, + size_t key_mod) : BlockCipher(key_min, key_max, key_mod), block_sz(EVP_CIPHER_block_size(algo)), cipher_name(algo_name) diff --git a/src/hash/hash.h b/src/hash/hash.h index 95d12806f..881e23817 100644 --- a/src/hash/hash.h +++ b/src/hash/hash.h @@ -23,7 +23,7 @@ class BOTAN_DLL HashFunction : public BufferedComputation * @param hash_len the output length * @param block_len the internal block size (if applicable) */ - HashFunction(u32bit hash_len) : BufferedComputation(hash_len) {} + HashFunction(size_t hash_len) : BufferedComputation(hash_len) {} virtual ~HashFunction() {} diff --git a/src/hash/mdx_hash/mdx_hash.h b/src/hash/mdx_hash/mdx_hash.h index 5591f2b80..d1260180e 100644 --- a/src/hash/mdx_hash/mdx_hash.h +++ b/src/hash/mdx_hash/mdx_hash.h @@ -64,7 +64,7 @@ class BOTAN_DLL MDx_HashFunction : public HashFunction size_t position; const bool BIG_BYTE_ENDIAN, BIG_BIT_ENDIAN; - const u32bit COUNT_SIZE; + const size_t COUNT_SIZE; }; } diff --git a/src/hash/skein/skein_512.cpp b/src/hash/skein/skein_512.cpp index 0ff8e9605..b2316242a 100644 --- a/src/hash/skein/skein_512.cpp +++ b/src/hash/skein/skein_512.cpp @@ -133,14 +133,14 @@ void reset_tweak(MemoryRegion<u64bit>& T, void initial_block(MemoryRegion<u64bit>& H, MemoryRegion<u64bit>& T, - u32bit output_bits, + size_t output_bits, const std::string& personalization) { zeroise(H); // ASCII("SHA3") followed by version (0x0001) code byte config_str[32] = { 0x53, 0x48, 0x41, 0x33, 0x01, 0x00, 0 }; - store_le(output_bits, config_str + 8); + store_le(u32bit(output_bits), config_str + 8); reset_tweak(T, SKEIN_CONFIG, true); ubi_512(H, T, config_str, sizeof(config_str)); @@ -166,14 +166,14 @@ void initial_block(MemoryRegion<u64bit>& H, } -Skein_512::Skein_512(u32bit arg_output_bits, +Skein_512::Skein_512(size_t arg_output_bits, const std::string& arg_personalization) : HashFunction(arg_output_bits / 8), personalization(arg_personalization), output_bits(arg_output_bits), H(9), T(3), buffer(64), buf_pos(0) { - if(output_bits == 0 || output_bits % 8 != 0) + if(output_bits == 0 || output_bits % 8 != 0 || output_bits > 64*1024) throw Invalid_Argument("Bad output bits size for Skein-512"); initial_block(H, T, output_bits, personalization); diff --git a/src/hash/skein/skein_512.h b/src/hash/skein/skein_512.h index fce02c1f6..54cdd002c 100644 --- a/src/hash/skein/skein_512.h +++ b/src/hash/skein/skein_512.h @@ -25,7 +25,7 @@ class BOTAN_DLL Skein_512 : public HashFunction * @param personalization is a string that will paramaterize the * hash output */ - Skein_512(u32bit output_bits = 512, + Skein_512(size_t output_bits = 512, const std::string& personalization = ""); size_t hash_block_size() const { return 64; } diff --git a/src/mac/mac.h b/src/mac/mac.h index 1350c7d7a..b788e06c8 100644 --- a/src/mac/mac.h +++ b/src/mac/mac.h @@ -51,10 +51,10 @@ class BOTAN_DLL MessageAuthenticationCode : public BufferedComputation, * @param key_max the maximum key size * @param key_mod the modulo restriction on the key size */ - MessageAuthenticationCode(u32bit mac_len, - u32bit key_min, - u32bit key_max = 0, - u32bit key_mod = 1) : + MessageAuthenticationCode(size_t mac_len, + size_t key_min, + size_t key_max = 0, + size_t key_mod = 1) : BufferedComputation(mac_len), SymmetricAlgorithm(key_min, key_max, key_mod) {} diff --git a/src/utils/mem_ops.h b/src/utils/mem_ops.h index 503be90b3..fc59c90d6 100644 --- a/src/utils/mem_ops.h +++ b/src/utils/mem_ops.h @@ -19,7 +19,7 @@ namespace Botan { * @param in the source array * @param n the number of elements of in/out */ -template<typename T> inline void copy_mem(T* out, const T* in, u32bit n) +template<typename T> inline void copy_mem(T* out, const T* in, size_t n) { std::memmove(out, in, sizeof(T)*n); } @@ -29,7 +29,7 @@ template<typename T> inline void copy_mem(T* out, const T* in, u32bit n) * @param ptr a pointer to an array * @param n the number of Ts pointed to by ptr */ -template<typename T> inline void clear_mem(T* ptr, u32bit n) +template<typename T> inline void clear_mem(T* ptr, size_t n) { if(n) // avoid glibc warning if n == 0 std::memset(ptr, 0, sizeof(T)*n); @@ -42,7 +42,7 @@ template<typename T> inline void clear_mem(T* ptr, u32bit n) * @param val the value to set each byte to */ template<typename T> -inline void set_mem(T* ptr, u32bit n, byte val) +inline void set_mem(T* ptr, size_t n, byte val) { std::memset(ptr, val, sizeof(T)*n); } @@ -54,11 +54,11 @@ inline void set_mem(T* ptr, u32bit n, byte val) * @param n the number of Ts in p1 and p2 * @return true iff p1[i] == p2[i] forall i in [0...n) */ -template<typename T> inline bool same_mem(const T* p1, const T* p2, u32bit n) +template<typename T> inline bool same_mem(const T* p1, const T* p2, size_t n) { bool is_same = true; - for(u32bit i = 0; i != n; ++i) + for(size_t i = 0; i != n; ++i) is_same &= (p1[i] == p2[i]); return is_same; diff --git a/src/utils/rotate.h b/src/utils/rotate.h index c8f8d4a1a..5e3eef304 100644 --- a/src/utils/rotate.h +++ b/src/utils/rotate.h @@ -15,12 +15,12 @@ namespace Botan { /* * Word Rotation Functions */ -template<typename T> inline T rotate_left(T input, u32bit rot) +template<typename T> inline T rotate_left(T input, size_t rot) { return static_cast<T>((input << rot) | (input >> (8*sizeof(T)-rot)));; } -template<typename T> inline T rotate_right(T input, u32bit rot) +template<typename T> inline T rotate_right(T input, size_t rot) { return static_cast<T>((input >> rot) | (input << (8*sizeof(T)-rot))); } |