diff options
Diffstat (limited to 'src/block')
69 files changed, 1102 insertions, 1026 deletions
diff --git a/src/block/aes/aes.cpp b/src/block/aes/aes.cpp index 8783f13a0..88439cf98 100644 --- a/src/block/aes/aes.cpp +++ b/src/block/aes/aes.cpp @@ -412,14 +412,14 @@ const u32bit TD[1024] = { /* * AES Encryption */ -void AES::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void AES::encrypt_n(const byte in[], byte out[], size_t blocks) const { const u32bit* TE0 = TE; const u32bit* TE1 = TE + 256; const u32bit* TE2 = TE + 512; const u32bit* TE3 = TE + 768; - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { u32bit T0 = load_be<u32bit>(in, 0) ^ EK[0]; u32bit T1 = load_be<u32bit>(in, 1) ^ EK[1]; @@ -521,22 +521,22 @@ void AES::encrypt_n(const byte in[], byte out[], u32bit blocks) const out[14] = SE[get_byte(2, B1)] ^ ME[14]; out[15] = SE[get_byte(3, B2)] ^ ME[15]; - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * AES Decryption */ -void AES::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void AES::decrypt_n(const byte in[], byte out[], size_t blocks) const { const u32bit* TD0 = TD; const u32bit* TD1 = TD + 256; const u32bit* TD2 = TD + 512; const u32bit* TD3 = TD + 768; - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { u32bit T0 = load_be<u32bit>(in, 0) ^ DK[0]; u32bit T1 = load_be<u32bit>(in, 1) ^ DK[1]; @@ -611,15 +611,15 @@ void AES::decrypt_n(const byte in[], byte out[], u32bit blocks) const out[14] = SD[get_byte(2, B1)] ^ MD[14]; out[15] = SD[get_byte(3, B0)] ^ MD[15]; - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * AES Key Schedule */ -void AES::key_schedule(const byte key[], u32bit length) +void AES::key_schedule(const byte key[], size_t length) { static const u32bit RC[10] = { 0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000, 0x20000000, @@ -627,46 +627,46 @@ void AES::key_schedule(const byte key[], u32bit length) ROUNDS = (length / 4) + 6; - SecureVector<u32bit, 64> XEK, XDK; + SecureVector<u32bit> XEK(64), XDK(64); - const u32bit X = length / 4; - for(u32bit j = 0; j != X; ++j) - XEK[j] = load_be<u32bit>(key, j); + const size_t X = length / 4; + for(size_t i = 0; i != X; ++i) + XEK[i] = load_be<u32bit>(key, i); - for(u32bit j = X; j < 4*(ROUNDS+1); j += X) + for(size_t i = X; i < 4*(ROUNDS+1); i += X) { - XEK[j] = XEK[j-X] ^ S(rotate_left(XEK[j-1], 8)) ^ RC[(j-X)/X]; - for(u32bit k = 1; k != X; ++k) + XEK[i] = XEK[i-X] ^ S(rotate_left(XEK[i-1], 8)) ^ RC[(i-X)/X]; + for(size_t j = 1; j != X; ++j) { - if(X == 8 && k == 4) - XEK[j+k] = XEK[j+k-X] ^ S(XEK[j+k-1]); + if(X == 8 && j == 4) + XEK[i+j] = XEK[i+j-X] ^ S(XEK[i+j-1]); else - XEK[j+k] = XEK[j+k-X] ^ XEK[j+k-1]; + XEK[i+j] = XEK[i+j-X] ^ XEK[i+j-1]; } } - for(u32bit j = 0; j != 4*(ROUNDS+1); j += 4) + for(size_t i = 0; i != 4*(ROUNDS+1); i += 4) { - XDK[j ] = XEK[4*ROUNDS-j ]; - XDK[j+1] = XEK[4*ROUNDS-j+1]; - XDK[j+2] = XEK[4*ROUNDS-j+2]; - XDK[j+3] = XEK[4*ROUNDS-j+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(u32bit j = 4; j != length + 24; ++j) - XDK[j] = TD[SE[get_byte(0, XDK[j])] + 0] ^ - TD[SE[get_byte(1, XDK[j])] + 256] ^ - TD[SE[get_byte(2, XDK[j])] + 512] ^ - TD[SE[get_byte(3, XDK[j])] + 768]; + for(size_t i = 4; i != length + 24; ++i) + XDK[i] = TD[SE[get_byte(0, XDK[i])] + 0] ^ + TD[SE[get_byte(1, XDK[i])] + 256] ^ + TD[SE[get_byte(2, XDK[i])] + 512] ^ + TD[SE[get_byte(3, XDK[i])] + 768]; - for(u32bit j = 0; j != 4; ++j) + for(size_t i = 0; i != 4; ++i) { - store_be(XEK[j+4*ROUNDS], ME + 4*j); - store_be(XEK[j], MD + 4*j); + store_be(XEK[i+4*ROUNDS], &ME[4*i]); + store_be(XEK[i], &MD[4*i]); } - EK.copy(XEK, length + 24); - DK.copy(XDK, length + 24); + EK.copy(&XEK[0], length + 24); + DK.copy(&XDK[0], length + 24); } /* @@ -681,7 +681,8 @@ u32bit AES::S(u32bit input) /* * AES Constructor */ -AES::AES(u32bit key_size) : BlockCipher(16, key_size) +AES::AES(u32bit key_size) : BlockCipher(16, key_size), + EK(56), ME(16), DK(56), MD(16) { if(key_size != 16 && key_size != 24 && key_size != 32) throw Invalid_Key_Length(name(), key_size); @@ -693,10 +694,10 @@ AES::AES(u32bit key_size) : BlockCipher(16, key_size) */ void AES::clear() { - EK.clear(); - DK.clear(); - ME.clear(); - MD.clear(); + zeroise(EK); + zeroise(DK); + zeroise(ME); + zeroise(MD); } } diff --git a/src/block/aes/aes.h b/src/block/aes/aes.h index 8770bdb35..d62413f5b 100644 --- a/src/block/aes/aes.h +++ b/src/block/aes/aes.h @@ -18,14 +18,15 @@ namespace Botan { class BOTAN_DLL AES : public BlockCipher { public: - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; void clear(); std::string name() const { return "AES"; } BlockCipher* clone() const { return new AES; } - AES() : BlockCipher(16, 16, 32, 8) { ROUNDS = 14; } + AES() : BlockCipher(16, 16, 32, 8), EK(56), ME(16), DK(56), MD(16) + { ROUNDS = 14; } /** * AES fixed to a particular key_size (16, 24, or 32 bytes) @@ -33,16 +34,16 @@ class BOTAN_DLL AES : public BlockCipher */ AES(u32bit key_size); private: - void key_schedule(const byte[], u32bit); + void key_schedule(const byte[], size_t); static u32bit S(u32bit); u32bit ROUNDS; - SecureVector<u32bit, 56> EK; - SecureVector<byte, 16> ME; + SecureVector<u32bit> EK; + SecureVector<byte> ME; - SecureVector<u32bit, 56> DK; - SecureVector<byte, 16> MD; + SecureVector<u32bit > DK; + SecureVector<byte> MD; }; /** diff --git a/src/block/aes_intel/aes_intel.cpp b/src/block/aes_intel/aes_intel.cpp index 211bb3b47..a2e660f2c 100644 --- a/src/block/aes_intel/aes_intel.cpp +++ b/src/block/aes_intel/aes_intel.cpp @@ -103,7 +103,7 @@ __m128i aes_256_key_expansion(__m128i key, __m128i key2) /* * AES-128 Encryption */ -void AES_128_Intel::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void AES_128_Intel::encrypt_n(const byte in[], byte out[], size_t blocks) const { const __m128i* in_mm = (const __m128i*)in; __m128i* out_mm = (__m128i*)out; @@ -155,7 +155,7 @@ void AES_128_Intel::encrypt_n(const byte in[], byte out[], u32bit blocks) const out_mm += 4; } - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { __m128i B = _mm_loadu_si128(in_mm + i); @@ -179,7 +179,7 @@ void AES_128_Intel::encrypt_n(const byte in[], byte out[], u32bit blocks) const /* * AES-128 Decryption */ -void AES_128_Intel::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void AES_128_Intel::decrypt_n(const byte in[], byte out[], size_t blocks) const { const __m128i* in_mm = (const __m128i*)in; __m128i* out_mm = (__m128i*)out; @@ -231,7 +231,7 @@ void AES_128_Intel::decrypt_n(const byte in[], byte out[], u32bit blocks) const out_mm += 4; } - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { __m128i B = _mm_loadu_si128(in_mm + i); @@ -255,7 +255,7 @@ void AES_128_Intel::decrypt_n(const byte in[], byte out[], u32bit blocks) const /* * AES-128 Key Schedule */ -void AES_128_Intel::key_schedule(const byte key[], u32bit) +void AES_128_Intel::key_schedule(const byte key[], size_t) { #define AES_128_key_exp(K, RCON) \ aes_128_key_expansion(K, _mm_aeskeygenassist_si128(K, RCON)) @@ -306,14 +306,14 @@ void AES_128_Intel::key_schedule(const byte key[], u32bit) */ void AES_128_Intel::clear() { - EK.clear(); - DK.clear(); + zeroise(EK); + zeroise(DK); } /* * AES-192 Encryption */ -void AES_192_Intel::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void AES_192_Intel::encrypt_n(const byte in[], byte out[], size_t blocks) const { const __m128i* in_mm = (const __m128i*)in; __m128i* out_mm = (__m128i*)out; @@ -369,7 +369,7 @@ void AES_192_Intel::encrypt_n(const byte in[], byte out[], u32bit blocks) const out_mm += 4; } - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { __m128i B = _mm_loadu_si128(in_mm + i); @@ -395,7 +395,7 @@ void AES_192_Intel::encrypt_n(const byte in[], byte out[], u32bit blocks) const /* * AES-192 Decryption */ -void AES_192_Intel::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void AES_192_Intel::decrypt_n(const byte in[], byte out[], size_t blocks) const { const __m128i* in_mm = (const __m128i*)in; __m128i* out_mm = (__m128i*)out; @@ -451,7 +451,7 @@ void AES_192_Intel::decrypt_n(const byte in[], byte out[], u32bit blocks) const out_mm += 4; } - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { __m128i B = _mm_loadu_si128(in_mm + i); @@ -477,7 +477,7 @@ void AES_192_Intel::decrypt_n(const byte in[], byte out[], u32bit blocks) const /* * AES-192 Key Schedule */ -void AES_192_Intel::key_schedule(const byte key[], u32bit) +void AES_192_Intel::key_schedule(const byte key[], size_t) { __m128i K0 = _mm_loadu_si128((const __m128i*)(key)); __m128i K1 = _mm_loadu_si128((const __m128i*)(key + 8)); @@ -522,14 +522,14 @@ void AES_192_Intel::key_schedule(const byte key[], u32bit) */ void AES_192_Intel::clear() { - EK.clear(); - DK.clear(); + zeroise(EK); + zeroise(DK); } /* * AES-256 Encryption */ -void AES_256_Intel::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void AES_256_Intel::encrypt_n(const byte in[], byte out[], size_t blocks) const { const __m128i* in_mm = (const __m128i*)in; __m128i* out_mm = (__m128i*)out; @@ -589,7 +589,7 @@ void AES_256_Intel::encrypt_n(const byte in[], byte out[], u32bit blocks) const out_mm += 4; } - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { __m128i B = _mm_loadu_si128(in_mm + i); @@ -617,7 +617,7 @@ void AES_256_Intel::encrypt_n(const byte in[], byte out[], u32bit blocks) const /* * AES-256 Decryption */ -void AES_256_Intel::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void AES_256_Intel::decrypt_n(const byte in[], byte out[], size_t blocks) const { const __m128i* in_mm = (const __m128i*)in; __m128i* out_mm = (__m128i*)out; @@ -677,7 +677,7 @@ void AES_256_Intel::decrypt_n(const byte in[], byte out[], u32bit blocks) const out_mm += 4; } - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { __m128i B = _mm_loadu_si128(in_mm + i); @@ -705,7 +705,7 @@ void AES_256_Intel::decrypt_n(const byte in[], byte out[], u32bit blocks) const /* * AES-256 Key Schedule */ -void AES_256_Intel::key_schedule(const byte key[], u32bit) +void AES_256_Intel::key_schedule(const byte key[], size_t) { __m128i K0 = _mm_loadu_si128((const __m128i*)(key)); __m128i K1 = _mm_loadu_si128((const __m128i*)(key + 16)); @@ -772,8 +772,8 @@ void AES_256_Intel::key_schedule(const byte key[], u32bit) */ void AES_256_Intel::clear() { - EK.clear(); - DK.clear(); + zeroise(EK); + zeroise(DK); } } diff --git a/src/block/aes_intel/aes_intel.h b/src/block/aes_intel/aes_intel.h index 592fb7faa..1d8a68389 100644 --- a/src/block/aes_intel/aes_intel.h +++ b/src/block/aes_intel/aes_intel.h @@ -18,10 +18,10 @@ namespace Botan { class BOTAN_DLL AES_128_Intel : public BlockCipher { public: - u32bit parallelism() const { return 4; } + size_t parallelism() const { return 4; } - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; void clear(); std::string name() const { return "AES-128"; } @@ -29,7 +29,7 @@ class BOTAN_DLL AES_128_Intel : public BlockCipher AES_128_Intel() : BlockCipher(16, 16) { } private: - void key_schedule(const byte[], u32bit); + void key_schedule(const byte[], size_t); SecureVector<u32bit, 44> EK, DK; }; @@ -40,10 +40,10 @@ class BOTAN_DLL AES_128_Intel : public BlockCipher class BOTAN_DLL AES_192_Intel : public BlockCipher { public: - u32bit parallelism() const { return 4; } + size_t parallelism() const { return 4; } - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; void clear(); std::string name() const { return "AES-192"; } @@ -51,7 +51,7 @@ class BOTAN_DLL AES_192_Intel : public BlockCipher AES_192_Intel() : BlockCipher(16, 24) { } private: - void key_schedule(const byte[], u32bit); + void key_schedule(const byte[], size_t); SecureVector<u32bit, 52> EK, DK; }; @@ -62,10 +62,10 @@ class BOTAN_DLL AES_192_Intel : public BlockCipher class BOTAN_DLL AES_256_Intel : public BlockCipher { public: - u32bit parallelism() const { return 4; } + size_t parallelism() const { return 4; } - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; void clear(); std::string name() const { return "AES-256"; } @@ -73,7 +73,7 @@ class BOTAN_DLL AES_256_Intel : public BlockCipher AES_256_Intel() : BlockCipher(16, 32) { } private: - void key_schedule(const byte[], u32bit); + void key_schedule(const byte[], size_t); SecureVector<u32bit, 60> EK, DK; }; diff --git a/src/block/aes_ssse3/aes_ssse3.cpp b/src/block/aes_ssse3/aes_ssse3.cpp index 07e36e25e..c5869f899 100644 --- a/src/block/aes_ssse3/aes_ssse3.cpp +++ b/src/block/aes_ssse3/aes_ssse3.cpp @@ -171,7 +171,7 @@ __m128i aes_schedule_round(__m128i* rcon, __m128i input1, __m128i input2) smeared); } -__m128i aes_ssse3_encrypt(__m128i B, const __m128i* keys, u32bit rounds) +__m128i aes_ssse3_encrypt(__m128i B, const __m128i* keys, size_t rounds) { const __m128i sb2u = _mm_set_epi32( 0x5EB7E955, 0xBC982FCD, 0xE27A93C6, 0x0B712400); @@ -197,7 +197,7 @@ __m128i aes_ssse3_encrypt(__m128i B, const __m128i* keys, u32bit rounds) 4)), _mm_loadu_si128(keys)); - for(u32bit r = 1; ; ++r) + for(size_t r = 1; ; ++r) { const __m128i K = _mm_loadu_si128(keys + r); @@ -240,7 +240,7 @@ __m128i aes_ssse3_encrypt(__m128i B, const __m128i* keys, u32bit rounds) } } -__m128i aes_ssse3_decrypt(__m128i B, const __m128i* keys, u32bit rounds) +__m128i aes_ssse3_decrypt(__m128i B, const __m128i* keys, size_t rounds) { const __m128i k_dipt1 = _mm_set_epi32( 0x154A411E, 0x114E451A, 0x0F505B04, 0x0B545F00); @@ -278,7 +278,7 @@ __m128i aes_ssse3_decrypt(__m128i B, const __m128i* keys, u32bit rounds) B = mm_xor3(t, _mm_loadu_si128(keys), _mm_shuffle_epi8(k_dipt1, _mm_and_si128(B, low_nibs))); - for(u32bit r = 1; ; ++r) + for(size_t r = 1; ; ++r) { const __m128i K = _mm_loadu_si128(keys + r); @@ -337,14 +337,14 @@ __m128i aes_ssse3_decrypt(__m128i B, const __m128i* keys, u32bit rounds) /* * AES-128 Encryption */ -void AES_128_SSSE3::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void AES_128_SSSE3::encrypt_n(const byte in[], byte out[], size_t blocks) const { const __m128i* in_mm = (const __m128i*)in; __m128i* out_mm = (__m128i*)out; const __m128i* keys = (const __m128i*)&EK[0]; - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { __m128i B = _mm_loadu_si128(in_mm + i); _mm_storeu_si128(out_mm + i, aes_ssse3_encrypt(B, keys, 10)); @@ -354,14 +354,14 @@ void AES_128_SSSE3::encrypt_n(const byte in[], byte out[], u32bit blocks) const /* * AES-128 Decryption */ -void AES_128_SSSE3::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void AES_128_SSSE3::decrypt_n(const byte in[], byte out[], size_t blocks) const { const __m128i* in_mm = (const __m128i*)in; __m128i* out_mm = (__m128i*)out; const __m128i* keys = (const __m128i*)&DK[0]; - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { __m128i B = _mm_loadu_si128(in_mm + i); _mm_storeu_si128(out_mm + i, aes_ssse3_decrypt(B, keys, 10)); @@ -371,7 +371,7 @@ void AES_128_SSSE3::decrypt_n(const byte in[], byte out[], u32bit blocks) const /* * AES-128 Key Schedule */ -void AES_128_SSSE3::key_schedule(const byte keyb[], u32bit) +void AES_128_SSSE3::key_schedule(const byte keyb[], size_t) { __m128i rcon = _mm_set_epi32(0x702A9808, 0x4D7C7D81, 0x1F8391B9, 0xAF9DEEB6); @@ -387,7 +387,7 @@ void AES_128_SSSE3::key_schedule(const byte keyb[], u32bit) _mm_storeu_si128(EK_mm, key); - for(u32bit i = 1; i != 10; ++i) + for(size_t i = 1; i != 10; ++i) { key = aes_schedule_round(&rcon, key, key); @@ -406,14 +406,14 @@ void AES_128_SSSE3::key_schedule(const byte keyb[], u32bit) /* * AES-192 Encryption */ -void AES_192_SSSE3::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void AES_192_SSSE3::encrypt_n(const byte in[], byte out[], size_t blocks) const { const __m128i* in_mm = (const __m128i*)in; __m128i* out_mm = (__m128i*)out; const __m128i* keys = (const __m128i*)&EK[0]; - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { __m128i B = _mm_loadu_si128(in_mm + i); _mm_storeu_si128(out_mm + i, aes_ssse3_encrypt(B, keys, 12)); @@ -423,14 +423,14 @@ void AES_192_SSSE3::encrypt_n(const byte in[], byte out[], u32bit blocks) const /* * AES-192 Decryption */ -void AES_192_SSSE3::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void AES_192_SSSE3::decrypt_n(const byte in[], byte out[], size_t blocks) const { const __m128i* in_mm = (const __m128i*)in; __m128i* out_mm = (__m128i*)out; const __m128i* keys = (const __m128i*)&DK[0]; - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { __m128i B = _mm_loadu_si128(in_mm + i); _mm_storeu_si128(out_mm + i, aes_ssse3_decrypt(B, keys, 12)); @@ -440,7 +440,7 @@ void AES_192_SSSE3::decrypt_n(const byte in[], byte out[], u32bit blocks) const /* * AES-192 Key Schedule */ -void AES_192_SSSE3::key_schedule(const byte keyb[], u32bit) +void AES_192_SSSE3::key_schedule(const byte keyb[], size_t) { __m128i rcon = _mm_set_epi32(0x702A9808, 0x4D7C7D81, 0x1F8391B9, 0xAF9DEEB6); @@ -461,7 +461,7 @@ void AES_192_SSSE3::key_schedule(const byte keyb[], u32bit) // key2 with 8 high bytes masked off __m128i t = _mm_slli_si128(_mm_srli_si128(key2, 8), 8); - for(u32bit i = 0; i != 4; ++i) + for(size_t i = 0; i != 4; ++i) { key2 = aes_schedule_round(&rcon, key2, key1); @@ -505,14 +505,14 @@ void AES_192_SSSE3::key_schedule(const byte keyb[], u32bit) /* * AES-256 Encryption */ -void AES_256_SSSE3::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void AES_256_SSSE3::encrypt_n(const byte in[], byte out[], size_t blocks) const { const __m128i* in_mm = (const __m128i*)in; __m128i* out_mm = (__m128i*)out; const __m128i* keys = (const __m128i*)&EK[0]; - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { __m128i B = _mm_loadu_si128(in_mm + i); _mm_storeu_si128(out_mm + i, aes_ssse3_encrypt(B, keys, 14)); @@ -522,14 +522,14 @@ void AES_256_SSSE3::encrypt_n(const byte in[], byte out[], u32bit blocks) const /* * AES-256 Decryption */ -void AES_256_SSSE3::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void AES_256_SSSE3::decrypt_n(const byte in[], byte out[], size_t blocks) const { const __m128i* in_mm = (const __m128i*)in; __m128i* out_mm = (__m128i*)out; const __m128i* keys = (const __m128i*)&DK[0]; - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { __m128i B = _mm_loadu_si128(in_mm + i); _mm_storeu_si128(out_mm + i, aes_ssse3_decrypt(B, keys, 14)); @@ -539,7 +539,7 @@ void AES_256_SSSE3::decrypt_n(const byte in[], byte out[], u32bit blocks) const /* * AES-256 Key Schedule */ -void AES_256_SSSE3::key_schedule(const byte keyb[], u32bit) +void AES_256_SSSE3::key_schedule(const byte keyb[], size_t) { __m128i rcon = _mm_set_epi32(0x702A9808, 0x4D7C7D81, 0x1F8391B9, 0xAF9DEEB6); @@ -560,7 +560,7 @@ void AES_256_SSSE3::key_schedule(const byte keyb[], u32bit) _mm_storeu_si128(DK_mm + 13, aes_schedule_mangle_dec(key2, 1)); - for(u32bit i = 2; i != 14; i += 2) + for(size_t i = 2; i != 14; i += 2) { __m128i k_t = key2; key1 = key2 = aes_schedule_round(&rcon, key2, key1); diff --git a/src/block/aes_ssse3/aes_ssse3.h b/src/block/aes_ssse3/aes_ssse3.h index 8087b58a0..0cdb5f4de 100644 --- a/src/block/aes_ssse3/aes_ssse3.h +++ b/src/block/aes_ssse3/aes_ssse3.h @@ -18,18 +18,18 @@ namespace Botan { class BOTAN_DLL AES_128_SSSE3 : public BlockCipher { public: - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; - void clear() { EK.clear(); DK.clear(); } + void clear() { zeroise(EK); zeroise(DK); } std::string name() const { return "AES-128"; } BlockCipher* clone() const { return new AES_128_SSSE3; } - AES_128_SSSE3() : BlockCipher(16, 16) {} + AES_128_SSSE3() : BlockCipher(16, 16), EK(44), DK(44) {} private: - void key_schedule(const byte[], u32bit); + void key_schedule(const byte[], size_t); - SecureVector<u32bit, 44> EK, DK; + SecureVector<u32bit> EK, DK; }; /** @@ -38,18 +38,18 @@ class BOTAN_DLL AES_128_SSSE3 : public BlockCipher class BOTAN_DLL AES_192_SSSE3 : public BlockCipher { public: - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; - void clear() { EK.clear(); DK.clear(); } + void clear() { zeroise(EK); zeroise(DK); } std::string name() const { return "AES-192"; } BlockCipher* clone() const { return new AES_192_SSSE3; } - AES_192_SSSE3() : BlockCipher(16, 24) {} + AES_192_SSSE3() : BlockCipher(16, 24), EK(52), DK(52) {} private: - void key_schedule(const byte[], u32bit); + void key_schedule(const byte[], size_t); - SecureVector<u32bit, 52> EK, DK; + SecureVector<u32bit> EK, DK; }; /** @@ -58,18 +58,18 @@ class BOTAN_DLL AES_192_SSSE3 : public BlockCipher class BOTAN_DLL AES_256_SSSE3 : public BlockCipher { public: - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; - void clear() { EK.clear(); DK.clear(); } + void clear() { zeroise(EK); zeroise(DK); } std::string name() const { return "AES-256"; } BlockCipher* clone() const { return new AES_256_SSSE3; } - AES_256_SSSE3() : BlockCipher(16, 32) {} + AES_256_SSSE3() : BlockCipher(16, 32), EK(60), DK(60) {} private: - void key_schedule(const byte[], u32bit); + void key_schedule(const byte[], size_t); - SecureVector<u32bit, 60> EK, DK; + SecureVector<u32bit> EK, DK; }; } diff --git a/src/block/aes_ssse3/info.txt b/src/block/aes_ssse3/info.txt index 7cb6ceb08..b1af2e143 100644 --- a/src/block/aes_ssse3/info.txt +++ b/src/block/aes_ssse3/info.txt @@ -13,4 +13,5 @@ simd_engine gcc clang msvc +sunstudio </cc> diff --git a/src/block/block_cipher.h b/src/block/block_cipher.h index c1b58996e..5f5e5e530 100644 --- a/src/block/block_cipher.h +++ b/src/block/block_cipher.h @@ -40,24 +40,29 @@ class BOTAN_DLL BlockCipher : public SymmetricAlgorithm const u32bit BLOCK_SIZE; /** + * @return block size of this algorithm + */ + size_t block_size() const { return BLOCK_SIZE; } + + /** * @return native parallelism of this cipher in blocks */ - virtual u32bit parallelism() const { return 1; } + virtual size_t parallelism() const { return 1; } /** * @return prefererred parallelism of this cipher in bytes */ - u32bit parallel_bytes() const + size_t parallel_bytes() const { - return parallelism() * BLOCK_SIZE * BOTAN_BLOCK_CIPHER_PAR_MULT; + return parallelism() * block_size() * BOTAN_BLOCK_CIPHER_PAR_MULT; } /** * Encrypt a block. * @param in The plaintext block to be encrypted as a byte array. - * Must be of length BLOCK_SIZE. + * Must be of length block_size(). * @param out The byte array designated to hold the encrypted block. - * Must be of length BLOCK_SIZE. + * Must be of length block_size(). */ void encrypt(const byte in[], byte out[]) const { encrypt_n(in, out, 1); } @@ -65,9 +70,9 @@ class BOTAN_DLL BlockCipher : public SymmetricAlgorithm /** * Decrypt a block. * @param in The ciphertext block to be decypted as a byte array. - * Must be of length BLOCK_SIZE. + * Must be of length block_size(). * @param out The byte array designated to hold the decrypted block. - * Must be of length BLOCK_SIZE. + * Must be of length block_size(). */ void decrypt(const byte in[], byte out[]) const { decrypt_n(in, out, 1); } @@ -75,7 +80,7 @@ class BOTAN_DLL BlockCipher : public SymmetricAlgorithm /** * Encrypt a block. * @param block the plaintext block to be encrypted - * Must be of length BLOCK_SIZE. Will hold the result when the function + * Must be of length block_size(). Will hold the result when the function * has finished. */ void encrypt(byte block[]) const { encrypt_n(block, block, 1); } @@ -83,28 +88,28 @@ class BOTAN_DLL BlockCipher : public SymmetricAlgorithm /** * Decrypt a block. * @param block the ciphertext block to be decrypted - * Must be of length BLOCK_SIZE. Will hold the result when the function + * Must be of length block_size(). Will hold the result when the function * has finished. */ void decrypt(byte block[]) const { decrypt_n(block, block, 1); } /** * Encrypt one or more blocks - * @param in the input buffer (multiple of BLOCK_SIZE) + * @param in the input buffer (multiple of block_size()) * @param out the output buffer (same size as in) * @param blocks the number of blocks to process */ virtual void encrypt_n(const byte in[], byte out[], - u32bit blocks) const = 0; + size_t blocks) const = 0; /** * Decrypt one or more blocks - * @param in the input buffer (multiple of BLOCK_SIZE) + * @param in the input buffer (multiple of block_size()) * @param out the output buffer (same size as in) * @param blocks the number of blocks to process */ virtual void decrypt_n(const byte in[], byte out[], - u32bit blocks) const = 0; + size_t blocks) const = 0; /** * Get a new object representing the same algorithm as *this diff --git a/src/block/blowfish/blowfish.cpp b/src/block/blowfish/blowfish.cpp index d0b182a84..f77c65d4d 100644 --- a/src/block/blowfish/blowfish.cpp +++ b/src/block/blowfish/blowfish.cpp @@ -13,19 +13,19 @@ namespace Botan { /* * Blowfish Encryption */ -void Blowfish::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void Blowfish::encrypt_n(const byte in[], byte out[], size_t blocks) const { - const u32bit* S1 = S + 0; - const u32bit* S2 = S + 256; - const u32bit* S3 = S + 512; - const u32bit* S4 = S + 768; + const u32bit* S1 = &S[0]; + const u32bit* S2 = &S[256]; + const u32bit* S3 = &S[512]; + const u32bit* S4 = &S[768]; - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { u32bit L = load_be<u32bit>(in, 0); u32bit R = load_be<u32bit>(in, 1); - for(u32bit j = 0; j != 16; j += 2) + for(size_t j = 0; j != 16; j += 2) { L ^= P[j]; R ^= ((S1[get_byte(0, L)] + S2[get_byte(1, L)]) ^ @@ -40,27 +40,27 @@ void Blowfish::encrypt_n(const byte in[], byte out[], u32bit blocks) const store_be(out, R, L); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * Blowfish Decryption */ -void Blowfish::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void Blowfish::decrypt_n(const byte in[], byte out[], size_t blocks) const { - const u32bit* S1 = S + 0; - const u32bit* S2 = S + 256; - const u32bit* S3 = S + 512; - const u32bit* S4 = S + 768; + const u32bit* S1 = &S[0]; + const u32bit* S2 = &S[256]; + const u32bit* S3 = &S[512]; + const u32bit* S4 = &S[768]; - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { u32bit L = load_be<u32bit>(in, 0); u32bit R = load_be<u32bit>(in, 1); - for(u32bit j = 17; j != 1; j -= 2) + for(size_t j = 17; j != 1; j -= 2) { L ^= P[j]; R ^= ((S1[get_byte(0, L)] + S2[get_byte(1, L)]) ^ @@ -75,53 +75,54 @@ void Blowfish::decrypt_n(const byte in[], byte out[], u32bit blocks) const store_be(out, R, L); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * Blowfish Key Schedule */ -void Blowfish::key_schedule(const byte key[], u32bit length) +void Blowfish::key_schedule(const byte key[], size_t length) { clear(); - for(u32bit j = 0, k = 0; j != 18; ++j, k += 4) - P[j] ^= make_u32bit(key[(k ) % length], key[(k+1) % length], - key[(k+2) % length], key[(k+3) % length]); + for(size_t i = 0, j = 0; i != 18; ++i, j += 4) + P[i] ^= make_u32bit(key[(j ) % length], key[(j+1) % length], + key[(j+2) % length], key[(j+3) % length]); u32bit L = 0, R = 0; - generate_sbox(P, 18, L, R); - generate_sbox(S, 1024, L, R); + generate_sbox(P, L, R); + generate_sbox(S, L, R); } /* * Generate one of the Sboxes */ -void Blowfish::generate_sbox(u32bit Box[], u32bit size, +void Blowfish::generate_sbox(MemoryRegion<u32bit>& box, u32bit& L, u32bit& R) const { - const u32bit* S1 = S + 0; - const u32bit* S2 = S + 256; - const u32bit* S3 = S + 512; - const u32bit* S4 = S + 768; + const u32bit* S1 = &S[0]; + const u32bit* S2 = &S[256]; + const u32bit* S3 = &S[512]; + const u32bit* S4 = &S[768]; - for(u32bit j = 0; j != size; j += 2) + for(size_t i = 0; i != box.size(); i += 2) { - for(u32bit k = 0; k != 16; k += 2) + for(size_t j = 0; j != 16; j += 2) { - L ^= P[k]; + L ^= P[j]; R ^= ((S1[get_byte(0, L)] + S2[get_byte(1, L)]) ^ S3[get_byte(2, L)]) + S4[get_byte(3, L)]; - R ^= P[k+1]; + R ^= P[j+1]; L ^= ((S1[get_byte(0, R)] + S2[get_byte(1, R)]) ^ S3[get_byte(2, R)]) + S4[get_byte(3, R)]; } u32bit T = R; R = L ^ P[16]; L = T ^ P[17]; - Box[j] = L; Box[j+1] = R; + box[i] = L; + box[i+1] = R; } } @@ -130,8 +131,8 @@ void Blowfish::generate_sbox(u32bit Box[], u32bit size, */ void Blowfish::clear() { - P.copy(P_INIT, 18); - S.copy(S_INIT, 1024); + std::copy(P_INIT, P_INIT + 18, P.begin()); + std::copy(S_INIT, S_INIT + 1024, S.begin()); } } diff --git a/src/block/blowfish/blowfish.h b/src/block/blowfish/blowfish.h index a178ec488..4d39e9e58 100644 --- a/src/block/blowfish/blowfish.h +++ b/src/block/blowfish/blowfish.h @@ -18,23 +18,24 @@ namespace Botan { class BOTAN_DLL Blowfish : public BlockCipher { public: - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; void clear(); std::string name() const { return "Blowfish"; } BlockCipher* clone() const { return new Blowfish; } - Blowfish() : BlockCipher(8, 1, 56) {} + Blowfish() : BlockCipher(8, 1, 56), S(1024), P(18) {} private: - void key_schedule(const byte[], u32bit); - void generate_sbox(u32bit[], u32bit, u32bit&, u32bit&) const; + void key_schedule(const byte[], size_t); + void generate_sbox(MemoryRegion<u32bit>& box, + u32bit& L, u32bit& R) const; static const u32bit P_INIT[18]; static const u32bit S_INIT[1024]; - SecureVector<u32bit, 1024> S; - SecureVector<u32bit, 18> P; + SecureVector<u32bit> S; + SecureVector<u32bit> P; }; } diff --git a/src/block/cascade/cascade.cpp b/src/block/cascade/cascade.cpp index f72ef7b76..225b7fd6e 100644 --- a/src/block/cascade/cascade.cpp +++ b/src/block/cascade/cascade.cpp @@ -10,26 +10,26 @@ namespace Botan { void Cascade_Cipher::encrypt_n(const byte in[], byte out[], - u32bit blocks) const + size_t blocks) const { - u32bit c1_blocks = blocks * (BLOCK_SIZE / cipher1->BLOCK_SIZE); - u32bit c2_blocks = blocks * (BLOCK_SIZE / cipher2->BLOCK_SIZE); + size_t c1_blocks = blocks * (block_size() / cipher1->block_size()); + size_t c2_blocks = blocks * (block_size() / cipher2->block_size()); cipher1->encrypt_n(in, out, c1_blocks); cipher2->encrypt_n(out, out, c2_blocks); } void Cascade_Cipher::decrypt_n(const byte in[], byte out[], - u32bit blocks) const + size_t blocks) const { - u32bit c1_blocks = blocks * (BLOCK_SIZE / cipher1->BLOCK_SIZE); - u32bit c2_blocks = blocks * (BLOCK_SIZE / cipher2->BLOCK_SIZE); + size_t c1_blocks = blocks * (block_size() / cipher1->block_size()); + size_t c2_blocks = blocks * (block_size() / cipher2->block_size()); cipher2->decrypt_n(in, out, c2_blocks); cipher1->decrypt_n(out, out, c1_blocks); } -void Cascade_Cipher::key_schedule(const byte key[], u32bit) +void Cascade_Cipher::key_schedule(const byte key[], size_t) { const byte* key2 = key + cipher1->MAXIMUM_KEYLENGTH; @@ -56,11 +56,11 @@ BlockCipher* Cascade_Cipher::clone() const namespace { -u32bit euclids_algorithm(u32bit a, u32bit b) +size_t euclids_algorithm(size_t a, size_t b) { while(b != 0) // gcd { - u32bit t = b; + size_t t = b; b = a % b; a = t; } @@ -68,12 +68,12 @@ u32bit euclids_algorithm(u32bit a, u32bit b) return a; } -u32bit block_size_for_cascade(u32bit bs, u32bit bs2) +size_t block_size_for_cascade(size_t bs, size_t bs2) { if(bs == bs2) return bs; - u32bit gcd = euclids_algorithm(bs, bs2); + size_t gcd = euclids_algorithm(bs, bs2); return (bs * bs2) / gcd; } @@ -81,11 +81,11 @@ u32bit block_size_for_cascade(u32bit bs, u32bit bs2) } Cascade_Cipher::Cascade_Cipher(BlockCipher* c1, BlockCipher* c2) : - BlockCipher(block_size_for_cascade(c1->BLOCK_SIZE, c2->BLOCK_SIZE), + BlockCipher(block_size_for_cascade(c1->block_size(), c2->block_size()), c1->MAXIMUM_KEYLENGTH + c2->MAXIMUM_KEYLENGTH), cipher1(c1), cipher2(c2) { - if(BLOCK_SIZE % c1->BLOCK_SIZE || BLOCK_SIZE % c2->BLOCK_SIZE) + if(block_size() % c1->block_size() || block_size() % c2->block_size()) throw Internal_Error("Failure in " + name() + " constructor"); } diff --git a/src/block/cascade/cascade.h b/src/block/cascade/cascade.h index abd9b015d..5e1989cb6 100644 --- a/src/block/cascade/cascade.h +++ b/src/block/cascade/cascade.h @@ -18,8 +18,8 @@ namespace Botan { class BOTAN_DLL Cascade_Cipher : public BlockCipher { public: - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; void clear(); std::string name() const; @@ -34,7 +34,7 @@ class BOTAN_DLL Cascade_Cipher : public BlockCipher ~Cascade_Cipher(); private: - void key_schedule(const byte[], u32bit); + void key_schedule(const byte[], size_t); BlockCipher* cipher1; BlockCipher* cipher2; diff --git a/src/block/cast/cast128.cpp b/src/block/cast/cast128.cpp index cabde4b4f..092fc201e 100644 --- a/src/block/cast/cast128.cpp +++ b/src/block/cast/cast128.cpp @@ -48,9 +48,9 @@ inline void R3(u32bit& L, u32bit R, u32bit MK, u32bit RK) /* * CAST-128 Encryption */ -void CAST_128::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void CAST_128::encrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { u32bit L = load_be<u32bit>(in, 0); u32bit R = load_be<u32bit>(in, 1); @@ -74,17 +74,17 @@ void CAST_128::encrypt_n(const byte in[], byte out[], u32bit blocks) const store_be(out, R, L); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * CAST-128 Decryption */ -void CAST_128::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void CAST_128::decrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { u32bit L = load_be<u32bit>(in, 0); u32bit R = load_be<u32bit>(in, 1); @@ -108,44 +108,45 @@ void CAST_128::decrypt_n(const byte in[], byte out[], u32bit blocks) const store_be(out, R, L); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * CAST-128 Key Schedule */ -void CAST_128::key_schedule(const byte key[], u32bit length) +void CAST_128::key_schedule(const byte key[], size_t length) { clear(); - SecureVector<u32bit, 4> X; - for(u32bit j = 0; j != length; ++j) + SecureVector<u32bit> X(4); + for(size_t j = 0; j != length; ++j) X[j/4] = (X[j/4] << 8) + key[j]; - key_schedule(MK, X); - key_schedule(RK, X); + cast_ks(MK, X); + cast_ks(RK, X); - for(u32bit j = 0; j != 16; ++j) + for(size_t j = 0; j != 16; ++j) RK[j] %= 32; } /* * S-Box Based Key Expansion */ -void CAST_128::key_schedule(u32bit K[16], u32bit X[4]) +void CAST_128::cast_ks(MemoryRegion<u32bit>& K, + MemoryRegion<u32bit>& X) { class ByteReader { public: - byte operator()(u32bit i) { return (X[i/4] >> (8*(3 - (i%4)))); } + byte operator()(size_t i) { return (X[i/4] >> (8*(3 - (i%4)))); } ByteReader(const u32bit* x) : X(x) {} private: const u32bit* X; }; - SecureVector<u32bit, 4> Z; - ByteReader x(X), z(Z); + SecureVector<u32bit> Z(4); + ByteReader x(&X[0]), z(&Z[0]); Z[0] = X[0] ^ S5[x(13)] ^ S6[x(15)] ^ S7[x(12)] ^ S8[x(14)] ^ S7[x( 8)]; Z[1] = X[2] ^ S5[z( 0)] ^ S6[z( 2)] ^ S7[z( 1)] ^ S8[z( 3)] ^ S8[x(10)]; diff --git a/src/block/cast/cast128.h b/src/block/cast/cast128.h index 967e91938..edccf04b3 100644 --- a/src/block/cast/cast128.h +++ b/src/block/cast/cast128.h @@ -18,25 +18,26 @@ namespace Botan { class BOTAN_DLL CAST_128 : public BlockCipher { public: - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; - void clear() { MK.clear(); RK.clear(); } + void clear() { zeroise(MK); zeroise(RK); } std::string name() const { return "CAST-128"; } BlockCipher* clone() const { return new CAST_128; } - CAST_128() : BlockCipher(8, 11, 16) {} + CAST_128() : BlockCipher(8, 11, 16), MK(16), RK(16) {} private: - void key_schedule(const byte[], u32bit); + void key_schedule(const byte[], size_t); - static void key_schedule(u32bit[16], u32bit[4]); + static void cast_ks(MemoryRegion<u32bit>& ks, + MemoryRegion<u32bit>& user_key); static const u32bit S5[256]; static const u32bit S6[256]; static const u32bit S7[256]; static const u32bit S8[256]; - SecureVector<u32bit, 16> MK, RK; + SecureVector<u32bit> MK, RK; }; extern const u32bit CAST_SBOX1[256]; diff --git a/src/block/cast/cast256.cpp b/src/block/cast/cast256.cpp index 8aaf8009f..1b41cd2af 100644 --- a/src/block/cast/cast256.cpp +++ b/src/block/cast/cast256.cpp @@ -48,9 +48,9 @@ void round3(u32bit& out, u32bit in, u32bit mask, u32bit rot) /* * CAST-256 Encryption */ -void CAST_256::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void CAST_256::encrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { u32bit A = load_be<u32bit>(in, 0); u32bit B = load_be<u32bit>(in, 1); @@ -84,17 +84,17 @@ void CAST_256::encrypt_n(const byte in[], byte out[], u32bit blocks) const store_be(out, A, B, C, D); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * CAST-256 Decryption */ -void CAST_256::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void CAST_256::decrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { u32bit A = load_be<u32bit>(in, 0); u32bit B = load_be<u32bit>(in, 1); @@ -128,23 +128,24 @@ void CAST_256::decrypt_n(const byte in[], byte out[], u32bit blocks) const store_be(out, A, B, C, D); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * CAST-256 Key Schedule */ -void CAST_256::key_schedule(const byte key[], u32bit length) +void CAST_256::key_schedule(const byte key[], size_t length) { - SecureVector<u32bit, 8> TMP; - for(u32bit j = 0; j != length; ++j) - TMP[j/4] = (TMP[j/4] << 8) + key[j]; + SecureVector<u32bit> K(8); + for(size_t j = 0; j != length; ++j) + K[j/4] = (K[j/4] << 8) + key[j]; - u32bit A = TMP[0], B = TMP[1], C = TMP[2], D = TMP[3], - E = TMP[4], F = TMP[5], G = TMP[6], H = TMP[7]; - for(u32bit j = 0; j != 48; j += 4) + u32bit A = K[0], B = K[1], C = K[2], D = K[3], + E = K[4], F = K[5], G = K[6], H = K[7]; + + for(size_t j = 0; j != 48; j += 4) { round1(G, H, KEY_MASK[4*j+ 0], KEY_ROT[(4*j+ 0) % 32]); round2(F, G, KEY_MASK[4*j+ 1], KEY_ROT[(4*j+ 1) % 32]); diff --git a/src/block/cast/cast256.h b/src/block/cast/cast256.h index c4a305671..74e38face 100644 --- a/src/block/cast/cast256.h +++ b/src/block/cast/cast256.h @@ -18,22 +18,22 @@ namespace Botan { class BOTAN_DLL CAST_256 : public BlockCipher { public: - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; - void clear() { MK.clear(); RK.clear(); } + void clear() { zeroise(MK); zeroise(RK); } std::string name() const { return "CAST-256"; } BlockCipher* clone() const { return new CAST_256; } - CAST_256() : BlockCipher(16, 4, 32, 4) {} + CAST_256() : BlockCipher(16, 4, 32, 4), MK(48), RK(48) {} private: - void key_schedule(const byte[], u32bit); + void key_schedule(const byte[], size_t); static const u32bit KEY_MASK[192]; static const byte KEY_ROT[32]; - SecureVector<u32bit, 48> MK; - SecureVector<byte, 48> RK; + SecureVector<u32bit> MK; + SecureVector<byte> RK; }; extern const u32bit CAST_SBOX1[256]; diff --git a/src/block/des/des.cpp b/src/block/des/des.cpp index bbe564827..7c61df3db 100644 --- a/src/block/des/des.cpp +++ b/src/block/des/des.cpp @@ -140,9 +140,9 @@ void des_decrypt(u32bit& L, u32bit& R, /* * DES Encryption */ -void DES::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void DES::encrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { u64bit T = (DES_IPTAB1[in[0]] ) | (DES_IPTAB1[in[1]] << 1) | (DES_IPTAB1[in[2]] << 2) | (DES_IPTAB1[in[3]] << 3) | @@ -152,7 +152,7 @@ void DES::encrypt_n(const byte in[], byte out[], u32bit blocks) const u32bit L = static_cast<u32bit>(T >> 32); u32bit R = static_cast<u32bit>(T); - des_encrypt(L, R, round_key); + des_encrypt(L, R, &round_key[0]); T = (DES_FPTAB1[get_byte(0, L)] << 5) | (DES_FPTAB1[get_byte(1, L)] << 3) | (DES_FPTAB1[get_byte(2, L)] << 1) | (DES_FPTAB2[get_byte(3, L)] << 1) | @@ -162,17 +162,17 @@ void DES::encrypt_n(const byte in[], byte out[], u32bit blocks) const store_be(T, out); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * DES Decryption */ -void DES::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void DES::decrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { u64bit T = (DES_IPTAB1[in[0]] ) | (DES_IPTAB1[in[1]] << 1) | (DES_IPTAB1[in[2]] << 2) | (DES_IPTAB1[in[3]] << 3) | @@ -182,7 +182,7 @@ void DES::decrypt_n(const byte in[], byte out[], u32bit blocks) const u32bit L = static_cast<u32bit>(T >> 32); u32bit R = static_cast<u32bit>(T); - des_decrypt(L, R, round_key); + des_decrypt(L, R, &round_key[0]); T = (DES_FPTAB1[get_byte(0, L)] << 5) | (DES_FPTAB1[get_byte(1, L)] << 3) | (DES_FPTAB1[get_byte(2, L)] << 1) | (DES_FPTAB2[get_byte(3, L)] << 1) | @@ -193,25 +193,25 @@ void DES::decrypt_n(const byte in[], byte out[], u32bit blocks) const store_be(T, out); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * DES Key Schedule */ -void DES::key_schedule(const byte key[], u32bit) +void DES::key_schedule(const byte key[], size_t) { - des_key_schedule(round_key.begin(), key); + des_key_schedule(&round_key[0], key); } /* * TripleDES Encryption */ -void TripleDES::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void TripleDES::encrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { u64bit T = (DES_IPTAB1[in[0]] ) | (DES_IPTAB1[in[1]] << 1) | (DES_IPTAB1[in[2]] << 2) | (DES_IPTAB1[in[3]] << 3) | @@ -221,9 +221,9 @@ void TripleDES::encrypt_n(const byte in[], byte out[], u32bit blocks) const u32bit L = static_cast<u32bit>(T >> 32); u32bit R = static_cast<u32bit>(T); - des_encrypt(L, R, round_key); - des_decrypt(R, L, round_key + 32); - des_encrypt(L, R, round_key + 64); + des_encrypt(L, R, &round_key[0]); + des_decrypt(R, L, &round_key[32]); + des_encrypt(L, R, &round_key[64]); T = (DES_FPTAB1[get_byte(0, L)] << 5) | (DES_FPTAB1[get_byte(1, L)] << 3) | (DES_FPTAB1[get_byte(2, L)] << 1) | (DES_FPTAB2[get_byte(3, L)] << 1) | @@ -234,17 +234,17 @@ void TripleDES::encrypt_n(const byte in[], byte out[], u32bit blocks) const store_be(T, out); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * TripleDES Decryption */ -void TripleDES::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void TripleDES::decrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { u64bit T = (DES_IPTAB1[in[0]] ) | (DES_IPTAB1[in[1]] << 1) | (DES_IPTAB1[in[2]] << 2) | (DES_IPTAB1[in[3]] << 3) | @@ -254,9 +254,9 @@ void TripleDES::decrypt_n(const byte in[], byte out[], u32bit blocks) const u32bit L = static_cast<u32bit>(T >> 32); u32bit R = static_cast<u32bit>(T); - des_decrypt(L, R, round_key + 64); - des_encrypt(R, L, round_key + 32); - des_decrypt(L, R, round_key); + des_decrypt(L, R, &round_key[64]); + des_encrypt(R, L, &round_key[32]); + des_decrypt(L, R, &round_key[0]); T = (DES_FPTAB1[get_byte(0, L)] << 5) | (DES_FPTAB1[get_byte(1, L)] << 3) | (DES_FPTAB1[get_byte(2, L)] << 1) | (DES_FPTAB2[get_byte(3, L)] << 1) | @@ -267,15 +267,15 @@ void TripleDES::decrypt_n(const byte in[], byte out[], u32bit blocks) const store_be(T, out); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * TripleDES Key Schedule */ -void TripleDES::key_schedule(const byte key[], u32bit length) +void TripleDES::key_schedule(const byte key[], size_t length) { des_key_schedule(&round_key[0], key); des_key_schedule(&round_key[32], key + 8); @@ -283,7 +283,7 @@ void TripleDES::key_schedule(const byte key[], u32bit length) if(length == 24) des_key_schedule(&round_key[64], key + 16); else - copy_mem(&round_key[64], round_key.begin(), 32); + copy_mem(&round_key[64], &round_key[0], 32); } } diff --git a/src/block/des/des.h b/src/block/des/des.h index 1ae806850..03641ba40 100644 --- a/src/block/des/des.h +++ b/src/block/des/des.h @@ -18,18 +18,18 @@ namespace Botan { class BOTAN_DLL DES : public BlockCipher { public: - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; - void clear() { round_key.clear(); } + void clear() { zeroise(round_key); } std::string name() const { return "DES"; } BlockCipher* clone() const { return new DES; } - DES() : BlockCipher(8, 8) {} + DES() : BlockCipher(8, 8), round_key(32) {} private: - void key_schedule(const byte[], u32bit); + void key_schedule(const byte[], size_t); - SecureVector<u32bit, 32> round_key; + SecureVector<u32bit> round_key; }; /** @@ -38,18 +38,18 @@ class BOTAN_DLL DES : public BlockCipher class BOTAN_DLL TripleDES : public BlockCipher { public: - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; - void clear() { round_key.clear(); } + void clear() { zeroise(round_key); } std::string name() const { return "TripleDES"; } BlockCipher* clone() const { return new TripleDES; } - TripleDES() : BlockCipher(8, 16, 24, 8) {} + TripleDES() : BlockCipher(8, 16, 24, 8), round_key(96) {} private: - void key_schedule(const byte[], u32bit); + void key_schedule(const byte[], size_t); - SecureVector<u32bit, 96> round_key; + SecureVector<u32bit> round_key; }; /* diff --git a/src/block/des/desx.cpp b/src/block/des/desx.cpp index d19d7da8a..c4dacdfdd 100644 --- a/src/block/des/desx.cpp +++ b/src/block/des/desx.cpp @@ -13,39 +13,39 @@ namespace Botan { /* * DESX Encryption */ -void DESX::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void DESX::encrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { - xor_buf(out, in, K1.begin(), BLOCK_SIZE); + xor_buf(out, in, &K1[0], block_size()); des.encrypt(out); - xor_buf(out, K2.begin(), BLOCK_SIZE); + xor_buf(out, &K2[0], block_size()); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * DESX Decryption */ -void DESX::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void DESX::decrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { - xor_buf(out, in, K2.begin(), BLOCK_SIZE); + xor_buf(out, in, &K2[0], block_size()); des.decrypt(out); - xor_buf(out, K1.begin(), BLOCK_SIZE); + xor_buf(out, &K1[0], block_size()); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * DESX Key Schedule */ -void DESX::key_schedule(const byte key[], u32bit) +void DESX::key_schedule(const byte key[], size_t) { K1.copy(key, 8); des.set_key(key + 8, 8); diff --git a/src/block/des/desx.h b/src/block/des/desx.h index 45a9d8479..b61ea3cf9 100644 --- a/src/block/des/desx.h +++ b/src/block/des/desx.h @@ -18,17 +18,17 @@ namespace Botan { class BOTAN_DLL DESX : public BlockCipher { public: - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; - void clear() { des.clear(); K1.clear(); K2.clear(); } + void clear() { des.clear(); zeroise(K1); zeroise(K2); } std::string name() const { return "DESX"; } BlockCipher* clone() const { return new DESX; } - DESX() : BlockCipher(8, 24) {} + DESX() : BlockCipher(8, 24), K1(8), K2(8) {} private: - void key_schedule(const byte[], u32bit); - SecureVector<byte, 8> K1, K2; + void key_schedule(const byte[], size_t); + SecureVector<byte> K1, K2; DES des; }; diff --git a/src/block/gost_28147/gost_28147.cpp b/src/block/gost_28147/gost_28147.cpp index 2dfce0473..ddf26b3d0 100644 --- a/src/block/gost_28147/gost_28147.cpp +++ b/src/block/gost_28147/gost_28147.cpp @@ -11,7 +11,7 @@ namespace Botan { -byte GOST_28147_89_Params::sbox_entry(u32bit row, u32bit col) const +byte GOST_28147_89_Params::sbox_entry(size_t row, size_t col) const { byte x = sboxes[4 * col + (row / 2)]; @@ -52,7 +52,7 @@ GOST_28147_89_Params::GOST_28147_89_Params(const std::string& n) : name(n) * GOST Constructor */ GOST_28147_89::GOST_28147_89(const GOST_28147_89_Params& param) : - BlockCipher(8, 32) + BlockCipher(8, 32), SBOX(1024), EK(8) { // Convert the parallel 4x4 sboxes into larger word-based sboxes for(size_t i = 0; i != 4; ++i) @@ -85,11 +85,12 @@ GOST_28147_89::GOST_28147_89(const GOST_28147_89_Params& param) : /* * GOST Encryption */ -void GOST_28147_89::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void GOST_28147_89::encrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { - u32bit N1 = load_le<u32bit>(in, 0), N2 = load_le<u32bit>(in, 1); + u32bit N1 = load_le<u32bit>(in, 0); + u32bit N2 = load_le<u32bit>(in, 1); for(size_t j = 0; j != 3; ++j) { @@ -106,26 +107,27 @@ void GOST_28147_89::encrypt_n(const byte in[], byte out[], u32bit blocks) const store_le(out, N2, N1); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * GOST Decryption */ -void GOST_28147_89::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void GOST_28147_89::decrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { - u32bit N1 = load_le<u32bit>(in, 0), N2 = load_le<u32bit>(in, 1); + u32bit N1 = load_le<u32bit>(in, 0); + u32bit N2 = load_le<u32bit>(in, 1); GOST_2ROUND(N1, N2, 0, 1); GOST_2ROUND(N1, N2, 2, 3); GOST_2ROUND(N1, N2, 4, 5); GOST_2ROUND(N1, N2, 6, 7); - for(size_t i = 0; i != 3; ++i) + for(size_t j = 0; j != 3; ++j) { GOST_2ROUND(N1, N2, 7, 6); GOST_2ROUND(N1, N2, 5, 4); @@ -134,18 +136,18 @@ void GOST_28147_89::decrypt_n(const byte in[], byte out[], u32bit blocks) const } store_le(out, N2, N1); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * GOST Key Schedule */ -void GOST_28147_89::key_schedule(const byte key[], u32bit) +void GOST_28147_89::key_schedule(const byte key[], size_t) { - for(u32bit j = 0; j != 8; ++j) - EK[j] = load_le<u32bit>(key, j); + for(size_t i = 0; i != 8; ++i) + EK[i] = load_le<u32bit>(key, i); } } diff --git a/src/block/gost_28147/gost_28147.h b/src/block/gost_28147/gost_28147.h index ec23466f4..d06b63228 100644 --- a/src/block/gost_28147/gost_28147.h +++ b/src/block/gost_28147/gost_28147.h @@ -26,7 +26,7 @@ class BOTAN_DLL GOST_28147_89_Params * @param col the column * @return sbox entry at this row/column */ - byte sbox_entry(u32bit row, u32bit col) const; + byte sbox_entry(size_t row, size_t col) const; /** * @return name of this parameter set @@ -52,10 +52,10 @@ class BOTAN_DLL GOST_28147_89_Params class BOTAN_DLL GOST_28147_89 : public BlockCipher { public: - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; - void clear() { EK.clear(); } + void clear() { zeroise(EK); } std::string name() const { return "GOST-28147-89"; } BlockCipher* clone() const { return new GOST_28147_89(SBOX); } @@ -65,13 +65,13 @@ class BOTAN_DLL GOST_28147_89 : public BlockCipher */ GOST_28147_89(const GOST_28147_89_Params& params); private: - GOST_28147_89(const SecureVector<u32bit, 1024>& other_SBOX) : - BlockCipher(8, 32), SBOX(other_SBOX) {} + GOST_28147_89(const SecureVector<u32bit>& other_SBOX) : + BlockCipher(8, 32), SBOX(other_SBOX), EK(8) {} - void key_schedule(const byte[], u32bit); + void key_schedule(const byte[], size_t); - SecureVector<u32bit, 1024> SBOX; - SecureVector<u32bit, 8> EK; + SecureVector<u32bit> SBOX; + SecureVector<u32bit> EK; }; } diff --git a/src/block/idea/idea.cpp b/src/block/idea/idea.cpp index 0c5dfed42..be7680b2c 100644 --- a/src/block/idea/idea.cpp +++ b/src/block/idea/idea.cpp @@ -33,46 +33,43 @@ inline u16bit mul(u16bit x, u16bit y) /* * Find multiplicative inverses modulo 65537 +* +* 65537 is prime; thus Fermat's little theorem tells us that +* x^65537 == x modulo 65537, which means +* x^(65537-2) == x^-1 modulo 65537 since +* x^(65537-2) * x == 1 mod 65537 +* +* Do the exponentiation with a basic square and multiply: all bits are +* of exponent are 1 so we always multiply */ u16bit mul_inv(u16bit x) { - if(x <= 1) - return x; - - u16bit t0 = static_cast<u16bit>(65537 / x), t1 = 1; - u16bit y = static_cast<u16bit>(65537 % x); + u16bit y = x; - while(y != 1) + for(size_t i = 0; i != 15; ++i) { - u16bit q = x / y; - x %= y; - t1 += q * t0; - - if(x == 1) - return t1; - - q = y / x; - y %= x; - t0 += q * t1; + y = mul(y, y); // square + y = mul(y, x); } - return (1 - t0); + + return y; } /** * IDEA is involutional, depending only on the key schedule */ -void idea_op(const byte in[], byte out[], u32bit blocks, const u16bit K[52]) +void idea_op(const byte in[], byte out[], size_t blocks, const u16bit K[52]) { const u32bit BLOCK_SIZE = 8; - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { u16bit X1 = load_be<u16bit>(in, 0); u16bit X2 = load_be<u16bit>(in, 1); u16bit X3 = load_be<u16bit>(in, 2); u16bit X4 = load_be<u16bit>(in, 3); - for(u32bit j = 0; j != 8; ++j) + for(size_t j = 0; j != 8; ++j) { X1 = mul(X1, K[6*j+0]); X2 += K[6*j+1]; @@ -109,32 +106,32 @@ void idea_op(const byte in[], byte out[], u32bit blocks, const u16bit K[52]) /* * IDEA Encryption */ -void IDEA::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void IDEA::encrypt_n(const byte in[], byte out[], size_t blocks) const { - idea_op(in, out, blocks, EK); + idea_op(in, out, blocks, &EK[0]); } /* * IDEA Decryption */ -void IDEA::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void IDEA::decrypt_n(const byte in[], byte out[], size_t blocks) const { - idea_op(in, out, blocks, DK); + idea_op(in, out, blocks, &DK[0]); } /* * IDEA Key Schedule */ -void IDEA::key_schedule(const byte key[], u32bit) +void IDEA::key_schedule(const byte key[], size_t) { - for(u32bit j = 0; j != 8; ++j) - EK[j] = load_be<u16bit>(key, j); + for(size_t i = 0; i != 8; ++i) + EK[i] = load_be<u16bit>(key, i); - for(u32bit j = 1, k = 8, offset = 0; k != 52; j %= 8, ++j, ++k) + for(size_t i = 1, j = 8, offset = 0; j != 52; i %= 8, ++i, ++j) { - EK[j+7+offset] = static_cast<u16bit>((EK[(j % 8) + offset] << 9) | - (EK[((j+1) % 8) + offset] >> 7)); - offset += (j == 8) ? 8 : 0; + EK[i+7+offset] = static_cast<u16bit>((EK[(i % 8) + offset] << 9) | + (EK[((i+1) % 8) + offset] >> 7)); + offset += (i == 8) ? 8 : 0; } DK[51] = mul_inv(EK[3]); @@ -142,14 +139,14 @@ void IDEA::key_schedule(const byte key[], u32bit) DK[49] = -EK[1]; DK[48] = mul_inv(EK[0]); - for(u32bit j = 1, k = 4, counter = 47; j != 8; ++j, k += 6) + for(size_t i = 1, j = 4, counter = 47; i != 8; ++i, j += 6) { - DK[counter--] = EK[k+1]; - DK[counter--] = EK[k]; - DK[counter--] = mul_inv(EK[k+5]); - DK[counter--] = -EK[k+3]; - DK[counter--] = -EK[k+4]; - DK[counter--] = mul_inv(EK[k+2]); + DK[counter--] = EK[j+1]; + DK[counter--] = EK[j]; + DK[counter--] = mul_inv(EK[j+5]); + DK[counter--] = -EK[j+3]; + DK[counter--] = -EK[j+4]; + DK[counter--] = mul_inv(EK[j+2]); } DK[5] = EK[47]; diff --git a/src/block/idea/idea.h b/src/block/idea/idea.h index aed3be3ea..c0af38ad6 100644 --- a/src/block/idea/idea.h +++ b/src/block/idea/idea.h @@ -18,28 +18,28 @@ namespace Botan { class BOTAN_DLL IDEA : public BlockCipher { public: - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; - void clear() { EK.clear(); DK.clear(); } + void clear() { zeroise(EK); zeroise(DK); } std::string name() const { return "IDEA"; } BlockCipher* clone() const { return new IDEA; } - IDEA() : BlockCipher(8, 16) {} + IDEA() : BlockCipher(8, 16), EK(52), DK(52) {} protected: /** * @return const reference to encryption subkeys */ - const SecureVector<u16bit, 52>& get_EK() const { return EK; } + const SecureVector<u16bit>& get_EK() const { return EK; } /** * @return const reference to decryption subkeys */ - const SecureVector<u16bit, 52>& get_DK() const { return DK; } + const SecureVector<u16bit>& get_DK() const { return DK; } private: - void key_schedule(const byte[], u32bit); - SecureVector<u16bit, 52> EK, DK; + void key_schedule(const byte[], size_t); + SecureVector<u16bit> EK, DK; }; } diff --git a/src/block/idea_sse2/idea_sse2.cpp b/src/block/idea_sse2/idea_sse2.cpp index a7ded37e1..8c7bd2a2c 100644 --- a/src/block/idea_sse2/idea_sse2.cpp +++ b/src/block/idea_sse2/idea_sse2.cpp @@ -144,7 +144,7 @@ void idea_op_8(const byte in[64], byte out[64], const u16bit EK[52]) B2 = _mm_or_si128(_mm_slli_epi16(B2, 8), _mm_srli_epi16(B2, 8)); B3 = _mm_or_si128(_mm_slli_epi16(B3, 8), _mm_srli_epi16(B3, 8)); - for(u32bit i = 0; i != 8; ++i) + for(size_t i = 0; i != 8; ++i) { B0 = mul(B0, EK[6*i+0]); B1 = _mm_add_epi16(B1, _mm_set1_epi16(EK[6*i+1])); @@ -194,13 +194,15 @@ void idea_op_8(const byte in[64], byte out[64], const u16bit EK[52]) /* * IDEA Encryption */ -void IDEA_SSE2::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void IDEA_SSE2::encrypt_n(const byte in[], byte out[], size_t blocks) const { + const u16bit* KS = &this->get_EK()[0]; + while(blocks >= 8) { - idea_op_8(in, out, this->get_EK()); - in += 8 * BLOCK_SIZE; - out += 8 * BLOCK_SIZE; + idea_op_8(in, out, KS); + in += 8 * block_size(); + out += 8 * block_size(); blocks -= 8; } @@ -211,13 +213,15 @@ void IDEA_SSE2::encrypt_n(const byte in[], byte out[], u32bit blocks) const /* * IDEA Decryption */ -void IDEA_SSE2::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void IDEA_SSE2::decrypt_n(const byte in[], byte out[], size_t blocks) const { + const u16bit* KS = &this->get_DK()[0]; + while(blocks >= 8) { - idea_op_8(in, out, this->get_DK()); - in += 8 * BLOCK_SIZE; - out += 8 * BLOCK_SIZE; + idea_op_8(in, out, KS); + in += 8 * block_size(); + out += 8 * block_size(); blocks -= 8; } diff --git a/src/block/idea_sse2/idea_sse2.h b/src/block/idea_sse2/idea_sse2.h index b00e0f400..8e475568e 100644 --- a/src/block/idea_sse2/idea_sse2.h +++ b/src/block/idea_sse2/idea_sse2.h @@ -18,10 +18,10 @@ namespace Botan { class BOTAN_DLL IDEA_SSE2 : public IDEA { public: - u32bit parallelism() const { return 8; } + size_t parallelism() const { return 8; } - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; BlockCipher* clone() const { return new IDEA_SSE2; } }; diff --git a/src/block/kasumi/kasumi.cpp b/src/block/kasumi/kasumi.cpp index d7f981b20..1a217a9c7 100644 --- a/src/block/kasumi/kasumi.cpp +++ b/src/block/kasumi/kasumi.cpp @@ -109,18 +109,18 @@ u16bit FI(u16bit I, u16bit K) /* * KASUMI Encryption */ -void KASUMI::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void KASUMI::encrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { u16bit B0 = load_be<u16bit>(in, 0); u16bit B1 = load_be<u16bit>(in, 1); u16bit B2 = load_be<u16bit>(in, 2); u16bit B3 = load_be<u16bit>(in, 3); - for(u32bit j = 0; j != 8; j += 2) + for(size_t j = 0; j != 8; j += 2) { - const u16bit* K = EK + 8*j; + const u16bit* K = &EK[8*j]; u16bit R = B1 ^ (rotate_left(B0, 1) & K[0]); u16bit L = B0 ^ (rotate_left(R, 1) | K[1]); @@ -145,26 +145,26 @@ void KASUMI::encrypt_n(const byte in[], byte out[], u32bit blocks) const store_be(out, B0, B1, B2, B3); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * KASUMI Decryption */ -void KASUMI::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void KASUMI::decrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { u16bit B0 = load_be<u16bit>(in, 0); u16bit B1 = load_be<u16bit>(in, 1); u16bit B2 = load_be<u16bit>(in, 2); u16bit B3 = load_be<u16bit>(in, 3); - for(u32bit j = 0; j != 8; j += 2) + for(size_t j = 0; j != 8; j += 2) { - const u16bit* K = EK + 8*(6-j); + const u16bit* K = &EK[8*(6-j)]; u16bit L = B2, R = B3; @@ -191,36 +191,36 @@ void KASUMI::decrypt_n(const byte in[], byte out[], u32bit blocks) const store_be(out, B0, B1, B2, B3); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * KASUMI Key Schedule */ -void KASUMI::key_schedule(const byte key[], u32bit) +void KASUMI::key_schedule(const byte key[], size_t) { static const u16bit RC[] = { 0x0123, 0x4567, 0x89AB, 0xCDEF, 0xFEDC, 0xBA98, 0x7654, 0x3210 }; - SecureVector<u16bit, 16> K; - for(u32bit j = 0; j != 8; ++j) + SecureVector<u16bit> K(16); + for(size_t i = 0; i != 8; ++i) { - K[j] = load_be<u16bit>(key, j); - K[j+8] = K[j] ^ RC[j]; + K[i] = load_be<u16bit>(key, i); + K[i+8] = K[i] ^ RC[i]; } - for(u32bit j = 0; j != 8; ++j) + for(size_t i = 0; i != 8; ++i) { - EK[8*j ] = rotate_left(K[(j+0) % 8 ], 2); - EK[8*j+1] = rotate_left(K[(j+2) % 8 + 8], 1); - EK[8*j+2] = rotate_left(K[(j+1) % 8 ], 5); - EK[8*j+3] = K[(j+4) % 8 + 8]; - EK[8*j+4] = rotate_left(K[(j+5) % 8 ], 8); - EK[8*j+5] = K[(j+3) % 8 + 8]; - EK[8*j+6] = rotate_left(K[(j+6) % 8 ], 13); - EK[8*j+7] = K[(j+7) % 8 + 8]; + EK[8*i ] = rotate_left(K[(i+0) % 8 ], 2); + EK[8*i+1] = rotate_left(K[(i+2) % 8 + 8], 1); + EK[8*i+2] = rotate_left(K[(i+1) % 8 ], 5); + EK[8*i+3] = K[(i+4) % 8 + 8]; + EK[8*i+4] = rotate_left(K[(i+5) % 8 ], 8); + EK[8*i+5] = K[(i+3) % 8 + 8]; + EK[8*i+6] = rotate_left(K[(i+6) % 8 ], 13); + EK[8*i+7] = K[(i+7) % 8 + 8]; } } diff --git a/src/block/kasumi/kasumi.h b/src/block/kasumi/kasumi.h index fda348ef3..c6b3c4351 100644 --- a/src/block/kasumi/kasumi.h +++ b/src/block/kasumi/kasumi.h @@ -18,18 +18,18 @@ namespace Botan { class BOTAN_DLL KASUMI : public BlockCipher { public: - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; - void clear() { EK.clear(); } + void clear() { zeroise(EK); } std::string name() const { return "KASUMI"; } BlockCipher* clone() const { return new KASUMI; } - KASUMI() : BlockCipher(8, 16) {} + KASUMI() : BlockCipher(8, 16), EK(64) {} private: - void key_schedule(const byte[], u32bit); + void key_schedule(const byte[], size_t); - SecureVector<u16bit, 64> EK; + SecureVector<u16bit> EK; }; } diff --git a/src/block/lion/lion.cpp b/src/block/lion/lion.cpp index cfb1406d7..9214044f6 100644 --- a/src/block/lion/lion.cpp +++ b/src/block/lion/lion.cpp @@ -14,13 +14,14 @@ namespace Botan { /* * Lion Encryption */ -void Lion::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void Lion::encrypt_n(const byte in[], byte out[], size_t blocks) const { - SecureVector<byte> buffer(LEFT_SIZE); + SecureVector<byte> buffer_vec(LEFT_SIZE); + byte* buffer = &buffer_vec[0]; - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { - xor_buf(buffer, in, key1, LEFT_SIZE); + xor_buf(buffer, in, &key1[0], LEFT_SIZE); cipher->set_key(buffer, LEFT_SIZE); cipher->cipher(in + LEFT_SIZE, out + LEFT_SIZE, RIGHT_SIZE); @@ -28,25 +29,26 @@ void Lion::encrypt_n(const byte in[], byte out[], u32bit blocks) const hash->final(buffer); xor_buf(out, in, buffer, LEFT_SIZE); - xor_buf(buffer, out, key2, LEFT_SIZE); + xor_buf(buffer, out, &key2[0], LEFT_SIZE); cipher->set_key(buffer, LEFT_SIZE); cipher->cipher1(out + LEFT_SIZE, RIGHT_SIZE); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * Lion Decryption */ -void Lion::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void Lion::decrypt_n(const byte in[], byte out[], size_t blocks) const { - SecureVector<byte> buffer(LEFT_SIZE); + SecureVector<byte> buffer_vec(LEFT_SIZE); + byte* buffer = &buffer_vec[0]; - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { - xor_buf(buffer, in, key2, LEFT_SIZE); + xor_buf(buffer, in, &key2[0], LEFT_SIZE); cipher->set_key(buffer, LEFT_SIZE); cipher->cipher(in + LEFT_SIZE, out + LEFT_SIZE, RIGHT_SIZE); @@ -54,19 +56,19 @@ void Lion::decrypt_n(const byte in[], byte out[], u32bit blocks) const hash->final(buffer); xor_buf(out, in, buffer, LEFT_SIZE); - xor_buf(buffer, out, key1, LEFT_SIZE); + xor_buf(buffer, out, &key1[0], LEFT_SIZE); cipher->set_key(buffer, LEFT_SIZE); cipher->cipher1(out + LEFT_SIZE, RIGHT_SIZE); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * Lion Key Schedule */ -void Lion::key_schedule(const byte key[], u32bit length) +void Lion::key_schedule(const byte key[], size_t length) { clear(); @@ -81,7 +83,7 @@ std::string Lion::name() const { return "Lion(" + hash->name() + "," + cipher->name() + "," + - std::to_string(BLOCK_SIZE) + ")"; + std::to_string(block_size()) + ")"; } /* @@ -89,7 +91,7 @@ std::string Lion::name() const */ BlockCipher* Lion::clone() const { - return new Lion(hash->clone(), cipher->clone(), BLOCK_SIZE); + return new Lion(hash->clone(), cipher->clone(), block_size()); } /* @@ -99,22 +101,22 @@ void Lion::clear() { hash->clear(); cipher->clear(); - key1.clear(); - key2.clear(); + zeroise(key1); + zeroise(key2); } /* * Lion Constructor */ -Lion::Lion(HashFunction* hash_in, StreamCipher* sc_in, u32bit block_len) : - BlockCipher(std::max<u32bit>(2*hash_in->OUTPUT_LENGTH + 1, block_len), - 2, 2*hash_in->OUTPUT_LENGTH, 2), - LEFT_SIZE(hash_in->OUTPUT_LENGTH), - RIGHT_SIZE(BLOCK_SIZE - LEFT_SIZE), +Lion::Lion(HashFunction* hash_in, StreamCipher* sc_in, size_t block_len) : + BlockCipher(std::max<size_t>(2*hash_in->output_length() + 1, block_len), + 2, 2*hash_in->output_length(), 2), + LEFT_SIZE(hash_in->output_length()), + RIGHT_SIZE(block_size() - LEFT_SIZE), hash(hash_in), cipher(sc_in) { - if(2*LEFT_SIZE + 1 > BLOCK_SIZE) + if(2*LEFT_SIZE + 1 > block_size()) throw Invalid_Argument(name() + ": Chosen block size is too small"); if(!cipher->valid_keylength(LEFT_SIZE)) diff --git a/src/block/lion/lion.h b/src/block/lion/lion.h index bba4e6f30..9beb68ca6 100644 --- a/src/block/lion/lion.h +++ b/src/block/lion/lion.h @@ -25,8 +25,8 @@ namespace Botan { class BOTAN_DLL Lion : public BlockCipher { public: - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; void clear(); std::string name() const; @@ -39,13 +39,13 @@ class BOTAN_DLL Lion : public BlockCipher */ Lion(HashFunction* hash, StreamCipher* cipher, - u32bit block_size); + size_t block_size); ~Lion() { delete hash; delete cipher; } private: - void key_schedule(const byte[], u32bit); + void key_schedule(const byte[], size_t); - const u32bit LEFT_SIZE, RIGHT_SIZE; + const size_t LEFT_SIZE, RIGHT_SIZE; HashFunction* hash; StreamCipher* cipher; diff --git a/src/block/lubyrack/lubyrack.cpp b/src/block/lubyrack/lubyrack.cpp index bdb26837e..aa33c6bc4 100644 --- a/src/block/lubyrack/lubyrack.cpp +++ b/src/block/lubyrack/lubyrack.cpp @@ -13,13 +13,15 @@ namespace Botan { /* * Luby-Rackoff Encryption */ -void LubyRackoff::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void LubyRackoff::encrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) - { - const u32bit len = hash->OUTPUT_LENGTH; + const size_t len = hash->output_length(); + + SecureVector<byte> buffer_vec(len); + byte* buffer = &buffer_vec[0]; - SecureVector<byte> buffer(len); + for(size_t i = 0; i != blocks; ++i) + { hash->update(K1); hash->update(in, len); hash->final(buffer); @@ -40,21 +42,23 @@ void LubyRackoff::encrypt_n(const byte in[], byte out[], u32bit blocks) const hash->final(buffer); xor_buf(out, buffer, len); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * Luby-Rackoff Decryption */ -void LubyRackoff::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void LubyRackoff::decrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) - { - const u32bit len = hash->OUTPUT_LENGTH; + const size_t len = hash->output_length(); + + SecureVector<byte> buffer_vec(len); + byte* buffer = &buffer_vec[0]; - SecureVector<byte> buffer(len); + for(size_t i = 0; i != blocks; ++i) + { hash->update(K2); hash->update(in + len, len); hash->final(buffer); @@ -75,15 +79,15 @@ void LubyRackoff::decrypt_n(const byte in[], byte out[], u32bit blocks) const hash->final(buffer); xor_buf(out + len, buffer, len); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * Luby-Rackoff Key Schedule */ -void LubyRackoff::key_schedule(const byte key[], u32bit length) +void LubyRackoff::key_schedule(const byte key[], size_t length) { K1.set(key, length / 2); K2.set(key + length / 2, length / 2); @@ -94,8 +98,8 @@ void LubyRackoff::key_schedule(const byte key[], u32bit length) */ void LubyRackoff::clear() { - K1.clear(); - K2.clear(); + zeroise(K1); + zeroise(K2); hash->clear(); } @@ -119,7 +123,7 @@ std::string LubyRackoff::name() const * Luby-Rackoff Constructor */ LubyRackoff::LubyRackoff(HashFunction* h) : - BlockCipher(2 * (h ? h->OUTPUT_LENGTH: 0), + BlockCipher(2 * (h ? h->output_length(): 0), 2, 32, 2), hash(h) { diff --git a/src/block/lubyrack/lubyrack.h b/src/block/lubyrack/lubyrack.h index a69d2302f..4567215e1 100644 --- a/src/block/lubyrack/lubyrack.h +++ b/src/block/lubyrack/lubyrack.h @@ -19,8 +19,8 @@ namespace Botan { class BOTAN_DLL LubyRackoff : public BlockCipher { public: - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; void clear(); std::string name() const; @@ -32,7 +32,7 @@ class BOTAN_DLL LubyRackoff : public BlockCipher LubyRackoff(HashFunction* hash); ~LubyRackoff() { delete hash; } private: - void key_schedule(const byte[], u32bit); + void key_schedule(const byte[], size_t); HashFunction* hash; SecureVector<byte> K1, K2; diff --git a/src/block/mars/mars.cpp b/src/block/mars/mars.cpp index 57a224fac..5864ac49b 100644 --- a/src/block/mars/mars.cpp +++ b/src/block/mars/mars.cpp @@ -149,7 +149,7 @@ inline void decrypt_round(u32bit& A, u32bit& B, u32bit& C, u32bit& D, */ void forward_mix(u32bit& A, u32bit& B, u32bit& C, u32bit& D) { - for(u32bit j = 0; j != 2; ++j) + for(size_t j = 0; j != 2; ++j) { B ^= SBOX[get_byte(3, A)]; B += SBOX[get_byte(2, A) + 256]; C += SBOX[get_byte(1, A)]; D ^= SBOX[get_byte(0, A) + 256]; @@ -174,7 +174,7 @@ void forward_mix(u32bit& A, u32bit& B, u32bit& C, u32bit& D) */ void reverse_mix(u32bit& A, u32bit& B, u32bit& C, u32bit& D) { - for(u32bit j = 0; j != 2; ++j) + for(size_t j = 0; j != 2; ++j) { B ^= SBOX[get_byte(3, A) + 256]; C -= SBOX[get_byte(0, A)]; D -= SBOX[get_byte(1, A) + 256]; D ^= SBOX[get_byte(2, A)]; @@ -202,7 +202,7 @@ u32bit gen_mask(u32bit input) { u32bit mask = 0; - for(u32bit j = 2; j != 31; ++j) + for(size_t j = 2; j != 31; ++j) { u32bit region = (input >> (j-1)) & 0x07; @@ -232,9 +232,9 @@ u32bit gen_mask(u32bit input) /* * MARS Encryption */ -void MARS::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void MARS::encrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { u32bit A = load_le<u32bit>(in, 0) + EK[0]; u32bit B = load_le<u32bit>(in, 1) + EK[1]; @@ -267,17 +267,17 @@ void MARS::encrypt_n(const byte in[], byte out[], u32bit blocks) const store_le(out, A, B, C, D); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * MARS Decryption */ -void MARS::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void MARS::decrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { u32bit A = load_le<u32bit>(in, 3) + EK[39]; u32bit B = load_le<u32bit>(in, 2) + EK[38]; @@ -310,40 +310,41 @@ void MARS::decrypt_n(const byte in[], byte out[], u32bit blocks) const store_le(out, D, C, B, A); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * MARS Key Schedule */ -void MARS::key_schedule(const byte key[], u32bit length) +void MARS::key_schedule(const byte key[], size_t length) { - SecureVector<u32bit, 15> T; - for(u32bit j = 0; j != length / 4; ++j) - T[j] = load_le<u32bit>(key, j); + SecureVector<u32bit> T(15); + for(size_t i = 0; i != length / 4; ++i) + T[i] = load_le<u32bit>(key, i); + T[length / 4] = length / 4; - for(u32bit j = 0; j != 4; ++j) + for(u32bit i = 0; i != 4; ++i) { - T[ 0] ^= rotate_left(T[ 8] ^ T[13], 3) ^ (j ); - T[ 1] ^= rotate_left(T[ 9] ^ T[14], 3) ^ (j + 4); - T[ 2] ^= rotate_left(T[10] ^ T[ 0], 3) ^ (j + 8); - T[ 3] ^= rotate_left(T[11] ^ T[ 1], 3) ^ (j + 12); - T[ 4] ^= rotate_left(T[12] ^ T[ 2], 3) ^ (j + 16); - T[ 5] ^= rotate_left(T[13] ^ T[ 3], 3) ^ (j + 20); - T[ 6] ^= rotate_left(T[14] ^ T[ 4], 3) ^ (j + 24); - T[ 7] ^= rotate_left(T[ 0] ^ T[ 5], 3) ^ (j + 28); - T[ 8] ^= rotate_left(T[ 1] ^ T[ 6], 3) ^ (j + 32); - T[ 9] ^= rotate_left(T[ 2] ^ T[ 7], 3) ^ (j + 36); - T[10] ^= rotate_left(T[ 3] ^ T[ 8], 3) ^ (j + 40); - T[11] ^= rotate_left(T[ 4] ^ T[ 9], 3) ^ (j + 44); - T[12] ^= rotate_left(T[ 5] ^ T[10], 3) ^ (j + 48); - T[13] ^= rotate_left(T[ 6] ^ T[11], 3) ^ (j + 52); - T[14] ^= rotate_left(T[ 7] ^ T[12], 3) ^ (j + 56); - - for(u32bit k = 0; k != 4; ++k) + T[ 0] ^= rotate_left(T[ 8] ^ T[13], 3) ^ (i ); + T[ 1] ^= rotate_left(T[ 9] ^ T[14], 3) ^ (i + 4); + T[ 2] ^= rotate_left(T[10] ^ T[ 0], 3) ^ (i + 8); + T[ 3] ^= rotate_left(T[11] ^ T[ 1], 3) ^ (i + 12); + T[ 4] ^= rotate_left(T[12] ^ T[ 2], 3) ^ (i + 16); + T[ 5] ^= rotate_left(T[13] ^ T[ 3], 3) ^ (i + 20); + T[ 6] ^= rotate_left(T[14] ^ T[ 4], 3) ^ (i + 24); + T[ 7] ^= rotate_left(T[ 0] ^ T[ 5], 3) ^ (i + 28); + T[ 8] ^= rotate_left(T[ 1] ^ T[ 6], 3) ^ (i + 32); + T[ 9] ^= rotate_left(T[ 2] ^ T[ 7], 3) ^ (i + 36); + T[10] ^= rotate_left(T[ 3] ^ T[ 8], 3) ^ (i + 40); + T[11] ^= rotate_left(T[ 4] ^ T[ 9], 3) ^ (i + 44); + T[12] ^= rotate_left(T[ 5] ^ T[10], 3) ^ (i + 48); + T[13] ^= rotate_left(T[ 6] ^ T[11], 3) ^ (i + 52); + T[14] ^= rotate_left(T[ 7] ^ T[12], 3) ^ (i + 56); + + for(size_t j = 0; j != 4; ++j) { T[ 0] = rotate_left(T[ 0] + SBOX[T[14] % 512], 9); T[ 1] = rotate_left(T[ 1] + SBOX[T[ 0] % 512], 9); @@ -362,17 +363,23 @@ void MARS::key_schedule(const byte key[], u32bit length) T[14] = rotate_left(T[14] + SBOX[T[13] % 512], 9); } - EK[10*j + 0] = T[ 0]; EK[10*j + 1] = T[ 4]; EK[10*j + 2] = T[ 8]; - EK[10*j + 3] = T[12]; EK[10*j + 4] = T[ 1]; EK[10*j + 5] = T[ 5]; - EK[10*j + 6] = T[ 9]; EK[10*j + 7] = T[13]; EK[10*j + 8] = T[ 2]; - EK[10*j + 9] = T[ 6]; + EK[10*i + 0] = T[ 0]; + EK[10*i + 1] = T[ 4]; + EK[10*i + 2] = T[ 8]; + EK[10*i + 3] = T[12]; + EK[10*i + 4] = T[ 1]; + EK[10*i + 5] = T[ 5]; + EK[10*i + 6] = T[ 9]; + EK[10*i + 7] = T[13]; + EK[10*i + 8] = T[ 2]; + EK[10*i + 9] = T[ 6]; } - for(u32bit j = 5; j != 37; j += 2) + for(size_t i = 5; i != 37; i += 2) { - u32bit key3 = EK[j] & 3; - EK[j] |= 3; - EK[j] ^= rotate_left(SBOX[265 + key3], EK[j-1] % 32) & gen_mask(EK[j]); + u32bit key3 = EK[i] & 3; + EK[i] |= 3; + EK[i] ^= rotate_left(SBOX[265 + key3], EK[i-1] % 32) & gen_mask(EK[i]); } } diff --git a/src/block/mars/mars.h b/src/block/mars/mars.h index f455ec5ca..a61f475f2 100644 --- a/src/block/mars/mars.h +++ b/src/block/mars/mars.h @@ -18,18 +18,18 @@ namespace Botan { class BOTAN_DLL MARS : public BlockCipher { public: - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; - void clear() { EK.clear(); } + void clear() { zeroise(EK); } std::string name() const { return "MARS"; } BlockCipher* clone() const { return new MARS; } - MARS() : BlockCipher(16, 16, 32, 4) {} + MARS() : BlockCipher(16, 16, 32, 4), EK(40) {} private: - void key_schedule(const byte[], u32bit); + void key_schedule(const byte[], size_t); - SecureVector<u32bit, 40> EK; + SecureVector<u32bit> EK; }; } diff --git a/src/block/misty1/misty1.cpp b/src/block/misty1/misty1.cpp index bc85d71f9..b87f513a0 100644 --- a/src/block/misty1/misty1.cpp +++ b/src/block/misty1/misty1.cpp @@ -102,18 +102,18 @@ u16bit FI(u16bit input, u16bit key7, u16bit key9) /* * MISTY1 Encryption */ -void MISTY1::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void MISTY1::encrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { u16bit B0 = load_be<u16bit>(in, 0); u16bit B1 = load_be<u16bit>(in, 1); u16bit B2 = load_be<u16bit>(in, 2); u16bit B3 = load_be<u16bit>(in, 3); - for(u32bit j = 0; j != 12; j += 3) + for(size_t j = 0; j != 12; j += 3) { - const u16bit* RK = EK + 8 * j; + const u16bit* RK = &EK[8 * j]; B1 ^= B0 & RK[0]; B0 ^= B1 | RK[1]; @@ -144,26 +144,26 @@ void MISTY1::encrypt_n(const byte in[], byte out[], u32bit blocks) const store_be(out, B2, B3, B0, B1); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * MISTY1 Decryption */ -void MISTY1::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void MISTY1::decrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { u16bit B0 = load_be<u16bit>(in, 2); u16bit B1 = load_be<u16bit>(in, 3); u16bit B2 = load_be<u16bit>(in, 0); u16bit B3 = load_be<u16bit>(in, 1); - for(u32bit j = 0; j != 12; j += 3) + for(size_t j = 0; j != 12; j += 3) { - const u16bit* RK = DK + 8 * j; + const u16bit* RK = &DK[8 * j]; B2 ^= B3 | RK[0]; B3 ^= B2 & RK[1]; @@ -194,25 +194,25 @@ void MISTY1::decrypt_n(const byte in[], byte out[], u32bit blocks) const store_be(out, B0, B1, B2, B3); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * MISTY1 Key Schedule */ -void MISTY1::key_schedule(const byte key[], u32bit length) +void MISTY1::key_schedule(const byte key[], size_t length) { - SecureVector<u16bit, 32> KS; - for(u32bit j = 0; j != length / 2; ++j) - KS[j] = load_be<u16bit>(key, j); + SecureVector<u16bit> KS(32); + for(size_t i = 0; i != length / 2; ++i) + KS[i] = load_be<u16bit>(key, i); - for(u32bit j = 0; j != 8; ++j) + for(size_t i = 0; i != 8; ++i) { - KS[j+ 8] = FI(KS[j], KS[(j+1) % 8] >> 9, KS[(j+1) % 8] & 0x1FF); - KS[j+16] = KS[j+8] >> 9; - KS[j+24] = KS[j+8] & 0x1FF; + KS[i+ 8] = FI(KS[i], KS[(i+1) % 8] >> 9, KS[(i+1) % 8] & 0x1FF); + KS[i+16] = KS[i+8] >> 9; + KS[i+24] = KS[i+8] & 0x1FF; } /* @@ -241,17 +241,17 @@ void MISTY1::key_schedule(const byte key[], u32bit length) 0x1C, 0x05, 0x00, 0x15, 0x1D, 0x02, 0x11, 0x19, 0x07, 0x13, 0x1B, 0x04, 0x04, 0x0A, 0x0E, 0x00 }; - for(u32bit j = 0; j != 100; ++j) + for(size_t i = 0; i != 100; ++i) { - EK[j] = KS[EK_ORDER[j]]; - DK[j] = KS[DK_ORDER[j]]; + EK[i] = KS[EK_ORDER[i]]; + DK[i] = KS[DK_ORDER[i]]; } } /* * MISTY1 Constructor */ -MISTY1::MISTY1(u32bit rounds) : BlockCipher(8, 16) +MISTY1::MISTY1(size_t rounds) : BlockCipher(8, 16), EK(100), DK(100) { if(rounds != 8) throw Invalid_Argument("MISTY1: Invalid number of rounds: " diff --git a/src/block/misty1/misty1.h b/src/block/misty1/misty1.h index a9bc12c7b..318e63b7d 100644 --- a/src/block/misty1/misty1.h +++ b/src/block/misty1/misty1.h @@ -18,10 +18,10 @@ namespace Botan { class BOTAN_DLL MISTY1 : public BlockCipher { public: - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; - void clear() { EK.clear(); DK.clear(); } + void clear() { zeroise(EK); zeroise(DK); } std::string name() const { return "MISTY1"; } BlockCipher* clone() const { return new MISTY1; } @@ -29,11 +29,11 @@ class BOTAN_DLL MISTY1 : public BlockCipher * @param rounds the number of rounds. Must be 8 with the current * implementation */ - MISTY1(u32bit rounds = 8); + MISTY1(size_t rounds = 8); private: - void key_schedule(const byte[], u32bit); + void key_schedule(const byte[], size_t); - SecureVector<u16bit, 100> EK, DK; + SecureVector<u16bit> EK, DK; }; } diff --git a/src/block/noekeon/noekeon.cpp b/src/block/noekeon/noekeon.cpp index 0bfce1882..c29fed93e 100644 --- a/src/block/noekeon/noekeon.cpp +++ b/src/block/noekeon/noekeon.cpp @@ -84,19 +84,19 @@ const byte Noekeon::RC[] = { /* * Noekeon Encryption */ -void Noekeon::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void Noekeon::encrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { u32bit A0 = load_be<u32bit>(in, 0); u32bit A1 = load_be<u32bit>(in, 1); u32bit A2 = load_be<u32bit>(in, 2); u32bit A3 = load_be<u32bit>(in, 3); - for(u32bit j = 0; j != 16; ++j) + for(size_t j = 0; j != 16; ++j) { A0 ^= RC[j]; - theta(A0, A1, A2, A3, EK); + theta(A0, A1, A2, A3, &EK[0]); A1 = rotate_left(A1, 1); A2 = rotate_left(A2, 5); @@ -110,30 +110,30 @@ void Noekeon::encrypt_n(const byte in[], byte out[], u32bit blocks) const } A0 ^= RC[16]; - theta(A0, A1, A2, A3, EK); + theta(A0, A1, A2, A3, &EK[0]); store_be(out, A0, A1, A2, A3); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * Noekeon Encryption */ -void Noekeon::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void Noekeon::decrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { u32bit A0 = load_be<u32bit>(in, 0); u32bit A1 = load_be<u32bit>(in, 1); u32bit A2 = load_be<u32bit>(in, 2); u32bit A3 = load_be<u32bit>(in, 3); - for(u32bit j = 16; j != 0; --j) + for(size_t j = 16; j != 0; --j) { - theta(A0, A1, A2, A3, DK); + theta(A0, A1, A2, A3, &DK[0]); A0 ^= RC[j]; A1 = rotate_left(A1, 1); @@ -147,29 +147,29 @@ void Noekeon::decrypt_n(const byte in[], byte out[], u32bit blocks) const A3 = rotate_right(A3, 2); } - theta(A0, A1, A2, A3, DK); + theta(A0, A1, A2, A3, &DK[0]); A0 ^= RC[0]; store_be(out, A0, A1, A2, A3); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * Noekeon Key Schedule */ -void Noekeon::key_schedule(const byte key[], u32bit) +void Noekeon::key_schedule(const byte key[], size_t) { u32bit A0 = load_be<u32bit>(key, 0); u32bit A1 = load_be<u32bit>(key, 1); u32bit A2 = load_be<u32bit>(key, 2); u32bit A3 = load_be<u32bit>(key, 3); - for(u32bit j = 0; j != 16; ++j) + for(size_t i = 0; i != 16; ++i) { - A0 ^= RC[j]; + A0 ^= RC[i]; theta(A0, A1, A2, A3); A1 = rotate_left(A1, 1); @@ -203,8 +203,8 @@ void Noekeon::key_schedule(const byte key[], u32bit) */ void Noekeon::clear() { - EK.clear(); - DK.clear(); + zeroise(EK); + zeroise(DK); } } diff --git a/src/block/noekeon/noekeon.h b/src/block/noekeon/noekeon.h index 2e524f8b8..593afa634 100644 --- a/src/block/noekeon/noekeon.h +++ b/src/block/noekeon/noekeon.h @@ -18,14 +18,14 @@ namespace Botan { class BOTAN_DLL Noekeon : public BlockCipher { public: - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; void clear(); std::string name() const { return "Noekeon"; } BlockCipher* clone() const { return new Noekeon; } - Noekeon() : BlockCipher(16, 16) {} + Noekeon() : BlockCipher(16, 16), EK(4), DK(4) {} protected: /** * The Noekeon round constants @@ -35,16 +35,16 @@ class BOTAN_DLL Noekeon : public BlockCipher /** * @return const reference to encryption subkeys */ - const SecureVector<u32bit, 4>& get_EK() const { return EK; } + const SecureVector<u32bit>& get_EK() const { return EK; } /** * @return const reference to decryption subkeys */ - const SecureVector<u32bit, 4>& get_DK() const { return DK; } + const SecureVector<u32bit>& get_DK() const { return DK; } private: - void key_schedule(const byte[], u32bit); - SecureVector<u32bit, 4> EK, DK; + void key_schedule(const byte[], size_t); + SecureVector<u32bit> EK, DK; }; } diff --git a/src/block/noekeon_simd/noekeon_simd.cpp b/src/block/noekeon_simd/noekeon_simd.cpp index be7ca86da..97158593a 100644 --- a/src/block/noekeon_simd/noekeon_simd.cpp +++ b/src/block/noekeon_simd/noekeon_simd.cpp @@ -53,9 +53,9 @@ namespace Botan { /* * Noekeon Encryption */ -void Noekeon_SIMD::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void Noekeon_SIMD::encrypt_n(const byte in[], byte out[], size_t blocks) const { - const SecureVector<u32bit, 4>& EK = this->get_EK(); + const SecureVector<u32bit>& EK = this->get_EK(); SIMD_32 K0 = SIMD_32(EK[0]); SIMD_32 K1 = SIMD_32(EK[1]); @@ -71,7 +71,7 @@ void Noekeon_SIMD::encrypt_n(const byte in[], byte out[], u32bit blocks) const SIMD_32::transpose(A0, A1, A2, A3); - for(u32bit i = 0; i != 16; ++i) + for(size_t i = 0; i != 16; ++i) { A0 ^= SIMD_32(RC[i]); @@ -110,9 +110,9 @@ void Noekeon_SIMD::encrypt_n(const byte in[], byte out[], u32bit blocks) const /* * Noekeon Encryption */ -void Noekeon_SIMD::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void Noekeon_SIMD::decrypt_n(const byte in[], byte out[], size_t blocks) const { - const SecureVector<u32bit, 4>& DK = this->get_DK(); + const SecureVector<u32bit>& DK = this->get_DK(); SIMD_32 K0 = SIMD_32(DK[0]); SIMD_32 K1 = SIMD_32(DK[1]); @@ -128,7 +128,7 @@ void Noekeon_SIMD::decrypt_n(const byte in[], byte out[], u32bit blocks) const SIMD_32::transpose(A0, A1, A2, A3); - for(u32bit i = 0; i != 16; ++i) + for(size_t i = 0; i != 16; ++i) { NOK_SIMD_THETA(A0, A1, A2, A3, K0, K1, K2, K3); diff --git a/src/block/noekeon_simd/noekeon_simd.h b/src/block/noekeon_simd/noekeon_simd.h index 507f17e21..5cc2d8b09 100644 --- a/src/block/noekeon_simd/noekeon_simd.h +++ b/src/block/noekeon_simd/noekeon_simd.h @@ -18,10 +18,10 @@ namespace Botan { class BOTAN_DLL Noekeon_SIMD : public Noekeon { public: - u32bit parallelism() const { return 4; } + size_t parallelism() const { return 4; } - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; BlockCipher* clone() const { return new Noekeon_SIMD; } }; diff --git a/src/block/rc2/rc2.cpp b/src/block/rc2/rc2.cpp index 3114c6055..5c7cb1ead 100644 --- a/src/block/rc2/rc2.cpp +++ b/src/block/rc2/rc2.cpp @@ -14,16 +14,16 @@ namespace Botan { /* * RC2 Encryption */ -void RC2::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void RC2::encrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { u16bit R0 = load_le<u16bit>(in, 0); u16bit R1 = load_le<u16bit>(in, 1); u16bit R2 = load_le<u16bit>(in, 2); u16bit R3 = load_le<u16bit>(in, 3); - for(u32bit j = 0; j != 16; ++j) + for(size_t j = 0; j != 16; ++j) { R0 += (R1 & ~R3) + (R2 & R3) + K[4*j]; R0 = rotate_left(R0, 1); @@ -48,24 +48,24 @@ void RC2::encrypt_n(const byte in[], byte out[], u32bit blocks) const store_le(out, R0, R1, R2, R3); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * RC2 Decryption */ -void RC2::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void RC2::decrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { u16bit R0 = load_le<u16bit>(in, 0); u16bit R1 = load_le<u16bit>(in, 1); u16bit R2 = load_le<u16bit>(in, 2); u16bit R3 = load_le<u16bit>(in, 3); - for(u32bit j = 0; j != 16; ++j) + for(size_t j = 0; j != 16; ++j) { R3 = rotate_right(R3, 5); R3 -= (R0 & ~R2) + (R1 & R2) + K[63 - (4*j + 0)]; @@ -90,15 +90,15 @@ void RC2::decrypt_n(const byte in[], byte out[], u32bit blocks) const store_le(out, R0, R1, R2, R3); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * RC2 Key Schedule */ -void RC2::key_schedule(const byte key[], u32bit length) +void RC2::key_schedule(const byte key[], size_t length) { static const byte TABLE[256] = { 0xD9, 0x78, 0xF9, 0xC4, 0x19, 0xDD, 0xB5, 0xED, 0x28, 0xE9, 0xFD, 0x79, @@ -124,23 +124,24 @@ void RC2::key_schedule(const byte key[], u32bit length) 0xC5, 0xF3, 0xDB, 0x47, 0xE5, 0xA5, 0x9C, 0x77, 0x0A, 0xA6, 0x20, 0x68, 0xFE, 0x7F, 0xC1, 0xAD }; - SecureVector<byte, 128> L; + SecureVector<byte> L(128); L.copy(key, length); - for(u32bit j = length; j != 128; ++j) - L[j] = TABLE[(L[j-1] + L[j-length]) % 256]; + for(size_t i = length; i != 128; ++i) + L[i] = TABLE[(L[i-1] + L[i-length]) % 256]; + L[128-length] = TABLE[L[128-length]]; - for(s32bit j = 127-length; j >= 0; --j) - L[j] = TABLE[L[j+1] ^ L[j+length]]; - for(u32bit j = 0; j != 64; ++j) - K[j] = load_le<u16bit>(L, j); + for(s32bit i = 127-length; i >= 0; --i) + L[i] = TABLE[L[i+1] ^ L[i+length]]; + + load_le<u16bit>(&K[0], &L[0], 64); } /* * Return the code of the effective key bits */ -byte RC2::EKB_code(u32bit ekb) +byte RC2::EKB_code(size_t ekb) { const byte EKB[256] = { 0xBD, 0x56, 0xEA, 0xF2, 0xA2, 0xF1, 0xAC, 0x2A, 0xB0, 0x93, 0xD1, 0x9C, diff --git a/src/block/rc2/rc2.h b/src/block/rc2/rc2.h index c16680347..4addf22ed 100644 --- a/src/block/rc2/rc2.h +++ b/src/block/rc2/rc2.h @@ -18,25 +18,25 @@ namespace Botan { class BOTAN_DLL RC2 : public BlockCipher { public: - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; /** * Return the code of the effective key bits * @param bits key length * @return EKB code */ - static byte EKB_code(u32bit bits); + static byte EKB_code(size_t bits); - void clear() { K.clear(); } + void clear() { zeroise(K); } std::string name() const { return "RC2"; } BlockCipher* clone() const { return new RC2; } - RC2() : BlockCipher(8, 1, 32) {} + RC2() : BlockCipher(8, 1, 32), K(64) {} private: - void key_schedule(const byte[], u32bit); + void key_schedule(const byte[], size_t); - SecureVector<u16bit, 64> K; + SecureVector<u16bit> K; }; } diff --git a/src/block/rc5/rc5.cpp b/src/block/rc5/rc5.cpp index 6c712db9a..36d4cdd3a 100644 --- a/src/block/rc5/rc5.cpp +++ b/src/block/rc5/rc5.cpp @@ -16,14 +16,15 @@ namespace Botan { /* * RC5 Encryption */ -void RC5::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void RC5::encrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { - u32bit A = load_le<u32bit>(in, 0), B = load_le<u32bit>(in, 1); + u32bit A = load_le<u32bit>(in, 0); + u32bit B = load_le<u32bit>(in, 1); A += S[0]; B += S[1]; - for(u32bit 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]; @@ -37,21 +38,22 @@ void RC5::encrypt_n(const byte in[], byte out[], u32bit blocks) const store_le(out, A, B); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * RC5 Decryption */ -void RC5::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void RC5::decrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { - u32bit A = load_le<u32bit>(in, 0), B = load_le<u32bit>(in, 1); + u32bit A = load_le<u32bit>(in, 0); + u32bit B = load_le<u32bit>(in, 1); - for(u32bit 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; @@ -66,31 +68,36 @@ void RC5::decrypt_n(const byte in[], byte out[], u32bit blocks) const store_le(out, A, B); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * RC5 Key Schedule */ -void RC5::key_schedule(const byte key[], u32bit length) +void RC5::key_schedule(const byte key[], size_t length) { - const u32bit WORD_KEYLENGTH = (((length - 1) / 4) + 1), - MIX_ROUNDS = 3*std::max(WORD_KEYLENGTH, S.size()); + const size_t WORD_KEYLENGTH = (((length - 1) / 4) + 1); + const size_t MIX_ROUNDS = 3 * std::max(WORD_KEYLENGTH, S.size()); + S[0] = 0xB7E15163; - for(u32bit j = 1; j != S.size(); ++j) - S[j] = S[j-1] + 0x9E3779B9; + for(size_t i = 1; i != S.size(); ++i) + S[i] = S[i-1] + 0x9E3779B9; + + SecureVector<u32bit> K(8); + + for(s32bit i = length-1; i >= 0; --i) + K[i/4] = (K[i/4] << 8) + key[i]; + + u32bit A = 0, B = 0; - SecureVector<u32bit, 8> K; - for(s32bit j = length-1; j >= 0; --j) - K[j/4] = (K[j/4] << 8) + key[j]; - for(u32bit j = 0, A = 0, B = 0; j != MIX_ROUNDS; ++j) + for(size_t i = 0; i != MIX_ROUNDS; ++i) { - A = rotate_left(S[j % S.size()] + A + B, 3); - B = rotate_left(K[j % WORD_KEYLENGTH] + A + B, (A + B) % 32); - S[j % S.size()] = A; - K[j % WORD_KEYLENGTH] = B; + A = rotate_left(S[i % S.size()] + A + B, 3); + B = rotate_left(K[i % WORD_KEYLENGTH] + A + B, (A + B) % 32); + S[i % S.size()] = A; + K[i % WORD_KEYLENGTH] = B; } } @@ -105,7 +112,7 @@ std::string RC5::name() const /* * RC5 Constructor */ -RC5::RC5(u32bit r) : BlockCipher(8, 1, 32), ROUNDS(r) +RC5::RC5(size_t r) : BlockCipher(8, 1, 32), ROUNDS(r) { if(ROUNDS < 8 || ROUNDS > 32 || (ROUNDS % 4 != 0)) throw Invalid_Argument(name() + ": Invalid number of rounds"); diff --git a/src/block/rc5/rc5.h b/src/block/rc5/rc5.h index 385c6b2b1..11a62badb 100644 --- a/src/block/rc5/rc5.h +++ b/src/block/rc5/rc5.h @@ -18,10 +18,10 @@ namespace Botan { class BOTAN_DLL RC5 : public BlockCipher { public: - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; - void clear() { S.clear(); } + void clear() { zeroise(S); } std::string name() const; BlockCipher* clone() const { return new RC5(ROUNDS); } @@ -29,11 +29,11 @@ class BOTAN_DLL RC5 : public BlockCipher * @param rounds the number of RC5 rounds to run. Must be between * 8 and 32 and a multiple of 4. */ - RC5(u32bit rounds); + RC5(size_t rounds); private: - void key_schedule(const byte[], u32bit); + void key_schedule(const byte[], size_t); SecureVector<u32bit> S; - const u32bit ROUNDS; + const size_t ROUNDS; }; } diff --git a/src/block/rc6/rc6.cpp b/src/block/rc6/rc6.cpp index ff846f006..df87acbb1 100644 --- a/src/block/rc6/rc6.cpp +++ b/src/block/rc6/rc6.cpp @@ -15,9 +15,9 @@ namespace Botan { /* * RC6 Encryption */ -void RC6::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void RC6::encrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { u32bit A = load_le<u32bit>(in, 0); u32bit B = load_le<u32bit>(in, 1); @@ -26,7 +26,7 @@ void RC6::encrypt_n(const byte in[], byte out[], u32bit blocks) const B += S[0]; D += S[1]; - for(u32bit j = 0; j != 20; j += 4) + for(size_t j = 0; j != 20; j += 4) { u32bit T1, T2; @@ -55,17 +55,17 @@ void RC6::encrypt_n(const byte in[], byte out[], u32bit blocks) const store_le(out, A, B, C, D); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * RC6 Decryption */ -void RC6::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void RC6::decrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { u32bit A = load_le<u32bit>(in, 0); u32bit B = load_le<u32bit>(in, 1); @@ -74,7 +74,7 @@ void RC6::decrypt_n(const byte in[], byte out[], u32bit blocks) const C -= S[43]; A -= S[42]; - for(u32bit j = 0; j != 20; j += 4) + for(size_t j = 0; j != 20; j += 4) { u32bit T1, T2; @@ -103,31 +103,35 @@ void RC6::decrypt_n(const byte in[], byte out[], u32bit blocks) const store_le(out, A, B, C, D); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * RC6 Key Schedule */ -void RC6::key_schedule(const byte key[], u32bit length) +void RC6::key_schedule(const byte key[], size_t length) { - const u32bit WORD_KEYLENGTH = (((length - 1) / 4) + 1), - MIX_ROUNDS = 3*std::max(WORD_KEYLENGTH, S.size()); + const size_t WORD_KEYLENGTH = (((length - 1) / 4) + 1); + const size_t MIX_ROUNDS = 3 * std::max(WORD_KEYLENGTH, S.size()); + S[0] = 0xB7E15163; - for(u32bit j = 1; j != S.size(); ++j) - S[j] = S[j-1] + 0x9E3779B9; + for(size_t i = 1; i != S.size(); ++i) + S[i] = S[i-1] + 0x9E3779B9; + + SecureVector<u32bit> K(8); + + for(s32bit i = length-1; i >= 0; --i) + K[i/4] = (K[i/4] << 8) + key[i]; - SecureVector<u32bit, 8> K; - for(s32bit j = length-1; j >= 0; --j) - K[j/4] = (K[j/4] << 8) + key[j]; - for(u32bit j = 0, A = 0, B = 0; j != MIX_ROUNDS; ++j) + u32bit A = 0, B = 0; + for(size_t i = 0; i != MIX_ROUNDS; ++i) { - A = rotate_left(S[j % S.size()] + A + B, 3); - B = rotate_left(K[j % WORD_KEYLENGTH] + A + B, (A + B) % 32); - S[j % S.size()] = A; - K[j % WORD_KEYLENGTH] = B; + A = rotate_left(S[i % S.size()] + A + B, 3); + B = rotate_left(K[i % WORD_KEYLENGTH] + A + B, (A + B) % 32); + S[i % S.size()] = A; + K[i % WORD_KEYLENGTH] = B; } } diff --git a/src/block/rc6/rc6.h b/src/block/rc6/rc6.h index 9b2d587fa..307834a8c 100644 --- a/src/block/rc6/rc6.h +++ b/src/block/rc6/rc6.h @@ -18,18 +18,18 @@ namespace Botan { class BOTAN_DLL RC6 : public BlockCipher { public: - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; - void clear() { S.clear(); } + void clear() { zeroise(S); } std::string name() const { return "RC6"; } BlockCipher* clone() const { return new RC6; } - RC6() : BlockCipher(16, 1, 32) {} + RC6() : BlockCipher(16, 1, 32), S(44) {} private: - void key_schedule(const byte[], u32bit); + void key_schedule(const byte[], size_t); - SecureVector<u32bit, 44> S; + SecureVector<u32bit> S; }; } diff --git a/src/block/safer/safer_sk.cpp b/src/block/safer/safer_sk.cpp index 84ad9523b..5edd9245b 100644 --- a/src/block/safer/safer_sk.cpp +++ b/src/block/safer/safer_sk.cpp @@ -15,14 +15,14 @@ namespace Botan { /* * SAFER-SK Encryption */ -void SAFER_SK::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void SAFER_SK::encrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + 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(u32bit 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]]; @@ -43,17 +43,17 @@ void SAFER_SK::encrypt_n(const byte in[], byte out[], u32bit blocks) const 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; + in += block_size(); + out += block_size(); } } /* * SAFER-SK Decryption */ -void SAFER_SK::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void SAFER_SK::decrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + 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]; @@ -81,29 +81,30 @@ void SAFER_SK::decrypt_n(const byte in[], byte out[], u32bit blocks) const out[0] = A; out[1] = B; out[2] = C; out[3] = D; out[4] = E; out[5] = F; out[6] = G; out[7] = H; - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * SAFER-SK Key Schedule */ -void SAFER_SK::key_schedule(const byte key[], u32bit) +void SAFER_SK::key_schedule(const byte key[], size_t) { - SecureVector<byte, 18> KB; + SecureVector<byte> KB(18); - for(u32bit j = 0; j != 8; ++j) + for(size_t i = 0; i != 8; ++i) { - KB[ 8] ^= KB[j] = rotate_left(key[j], 5); - KB[17] ^= KB[j+9] = EK[j] = key[j+8]; + KB[ 8] ^= KB[i] = rotate_left(key[i], 5); + KB[17] ^= KB[i+9] = EK[i] = key[i+8]; } - for(u32bit j = 0; j != ROUNDS; ++j) + + for(size_t i = 0; i != ROUNDS; ++i) { - for(u32bit k = 0; k != 18; ++k) - KB[k] = rotate_left(KB[k], 6); - for(u32bit k = 0; k != 16; ++k) - EK[16*j+k+8] = KB[KEY_INDEX[16*j+k]] + BIAS[16*j+k]; + for(size_t j = 0; j != 18; ++j) + KB[j] = rotate_left(KB[j], 6); + for(size_t j = 0; j != 16; ++j) + EK[16*i+j+8] = KB[KEY_INDEX[16*i+j]] + BIAS[16*i+j]; } } @@ -126,7 +127,7 @@ BlockCipher* SAFER_SK::clone() const /* * SAFER-SK Constructor */ -SAFER_SK::SAFER_SK(u32bit rounds) : BlockCipher(8, 16), +SAFER_SK::SAFER_SK(size_t rounds) : BlockCipher(8, 16), EK(16 * rounds + 8), ROUNDS(rounds) { if(ROUNDS > 13 || ROUNDS == 0) diff --git a/src/block/safer/safer_sk.h b/src/block/safer/safer_sk.h index c93797602..a64d09fb7 100644 --- a/src/block/safer/safer_sk.h +++ b/src/block/safer/safer_sk.h @@ -18,10 +18,10 @@ namespace Botan { class BOTAN_DLL SAFER_SK : public BlockCipher { public: - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; - void clear() { EK.clear(); } + void clear() { zeroise(EK); } std::string name() const; BlockCipher* clone() const; @@ -29,9 +29,9 @@ class BOTAN_DLL SAFER_SK : public BlockCipher * @param rounds the number of rounds to use - must be between 1 * and 13 */ - SAFER_SK(u32bit rounds); + SAFER_SK(size_t rounds); private: - void key_schedule(const byte[], u32bit); + void key_schedule(const byte[], size_t); static const byte EXP[256]; static const byte LOG[512]; @@ -39,7 +39,7 @@ class BOTAN_DLL SAFER_SK : public BlockCipher static const byte KEY_INDEX[208]; SecureVector<byte> EK; - const u32bit ROUNDS; + const size_t ROUNDS; }; } diff --git a/src/block/seed/seed.cpp b/src/block/seed/seed.cpp index 651233bdb..015d2d48d 100644 --- a/src/block/seed/seed.cpp +++ b/src/block/seed/seed.cpp @@ -22,9 +22,9 @@ u32bit SEED::G_FUNC::operator()(u32bit X) const /* * SEED Encryption */ -void SEED::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void SEED::encrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { u32bit B0 = load_be<u32bit>(in, 0); u32bit B1 = load_be<u32bit>(in, 1); @@ -33,7 +33,7 @@ void SEED::encrypt_n(const byte in[], byte out[], u32bit blocks) const G_FUNC G; - for(u32bit j = 0; j != 16; j += 2) + for(size_t j = 0; j != 16; j += 2) { u32bit T0, T1; @@ -54,17 +54,17 @@ void SEED::encrypt_n(const byte in[], byte out[], u32bit blocks) const store_be(out, B2, B3, B0, B1); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * SEED Decryption */ -void SEED::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void SEED::decrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { u32bit B0 = load_be<u32bit>(in, 0); u32bit B1 = load_be<u32bit>(in, 1); @@ -73,7 +73,7 @@ void SEED::decrypt_n(const byte in[], byte out[], u32bit blocks) const G_FUNC G; - for(u32bit j = 0; j != 16; j += 2) + for(size_t j = 0; j != 16; j += 2) { u32bit T0, T1; @@ -94,15 +94,15 @@ void SEED::decrypt_n(const byte in[], byte out[], u32bit blocks) const store_be(out, B2, B3, B0, B1); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * SEED Key Schedule */ -void SEED::key_schedule(const byte key[], u32bit) +void SEED::key_schedule(const byte key[], size_t) { const u32bit RC[16] = { 0x9E3779B9, 0x3C6EF373, 0x78DDE6E6, 0xF1BBCDCC, @@ -111,24 +111,24 @@ void SEED::key_schedule(const byte key[], u32bit) 0x779B99E3, 0xEF3733C6, 0xDE6E678D, 0xBCDCCF1B }; - SecureVector<u32bit, 4> WK; + SecureVector<u32bit> WK(4); - for(u32bit j = 0; j != 4; ++j) - WK[j] = load_be<u32bit>(key, j); + for(size_t i = 0; i != 4; ++i) + WK[i] = load_be<u32bit>(key, i); G_FUNC G; - for(u32bit j = 0; j != 16; j += 2) + for(size_t i = 0; i != 16; i += 2) { - K[2*j ] = G(WK[0] + WK[2] - RC[j]); - K[2*j+1] = G(WK[1] - WK[3] + RC[j]) ^ K[2*j]; + K[2*i ] = G(WK[0] + WK[2] - RC[i]); + K[2*i+1] = G(WK[1] - WK[3] + RC[i]) ^ K[2*i]; byte T = get_byte(3, WK[0]); WK[0] = (WK[0] >> 8) | (get_byte(3, WK[1]) << 24); WK[1] = (WK[1] >> 8) | (T << 24); - K[2*j+2] = G(WK[0] + WK[2] - RC[j+1]); - K[2*j+3] = G(WK[1] - WK[3] + RC[j+1]) ^ K[2*j+2]; + K[2*i+2] = G(WK[0] + WK[2] - RC[i+1]); + K[2*i+3] = G(WK[1] - WK[3] + RC[i+1]) ^ K[2*i+2]; T = get_byte(0, WK[3]); WK[3] = (WK[3] << 8) | get_byte(0, WK[2]); diff --git a/src/block/seed/seed.h b/src/block/seed/seed.h index 0c80199ad..48fefc9b0 100644 --- a/src/block/seed/seed.h +++ b/src/block/seed/seed.h @@ -18,16 +18,16 @@ namespace Botan { class BOTAN_DLL SEED : public BlockCipher { public: - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; - void clear() { K.clear(); } + void clear() { zeroise(K); } std::string name() const { return "SEED"; } BlockCipher* clone() const { return new SEED; } - SEED() : BlockCipher(16, 16) {} + SEED() : BlockCipher(16, 16), K(32) {} private: - void key_schedule(const byte[], u32bit); + void key_schedule(const byte[], size_t); class G_FUNC { @@ -37,7 +37,7 @@ class BOTAN_DLL SEED : public BlockCipher static const u32bit S0[256], S1[256], S2[256], S3[256]; }; - SecureVector<u32bit, 32> K; + SecureVector<u32bit> K; }; } diff --git a/src/block/serpent/serpent.cpp b/src/block/serpent/serpent.cpp index b93326e58..ec37a9e97 100644 --- a/src/block/serpent/serpent.cpp +++ b/src/block/serpent/serpent.cpp @@ -243,9 +243,9 @@ inline void i_transform(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) /* * Serpent Encryption */ -void Serpent::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void Serpent::encrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { u32bit B0 = load_le<u32bit>(in, 0); u32bit B1 = load_le<u32bit>(in, 1); @@ -287,17 +287,17 @@ void Serpent::encrypt_n(const byte in[], byte out[], u32bit blocks) const store_le(out, B0, B1, B2, B3); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * Serpent Decryption */ -void Serpent::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void Serpent::decrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { u32bit B0 = load_le<u32bit>(in, 0); u32bit B1 = load_le<u32bit>(in, 1); @@ -339,8 +339,8 @@ void Serpent::decrypt_n(const byte in[], byte out[], u32bit blocks) const store_le(out, B0, B1, B2, B3); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } @@ -351,17 +351,22 @@ void Serpent::decrypt_n(const byte in[], byte out[], u32bit blocks) const /* * Serpent Key Schedule */ -void Serpent::key_schedule(const byte key[], u32bit length) +void Serpent::key_schedule(const byte key[], size_t length) { const u32bit PHI = 0x9E3779B9; - SecureVector<u32bit, 140> W; - for(u32bit j = 0; j != length / 4; ++j) - W[j] = load_le<u32bit>(key, j); + SecureVector<u32bit> W(140); + for(size_t i = 0; i != length / 4; ++i) + W[i] = load_le<u32bit>(key, i); W[length / 4] |= u32bit(1) << ((length%4)*8); - for(u32bit j = 8; j != 140; ++j) - W[j] = rotate_left(W[j-8] ^ W[j-5] ^ W[j-3] ^ W[j-1] ^ PHI ^ (j-8), 11); + + for(size_t i = 8; i != 140; ++i) + { + u32bit wi = W[i-8] ^ W[i-5] ^ W[i-3] ^ W[i-1] ^ PHI ^ u32bit(i-8); + W[i] = rotate_left(wi, 11); + } + SBoxE4(W[ 8],W[ 9],W[ 10],W[ 11]); SBoxE3(W[ 12],W[ 13],W[ 14],W[ 15]); SBoxE2(W[ 16],W[ 17],W[ 18],W[ 19]); SBoxE1(W[ 20],W[ 21],W[ 22],W[ 23]); SBoxE8(W[ 24],W[ 25],W[ 26],W[ 27]); SBoxE7(W[ 28],W[ 29],W[ 30],W[ 31]); @@ -379,7 +384,7 @@ void Serpent::key_schedule(const byte key[], u32bit length) SBoxE8(W[120],W[121],W[122],W[123]); SBoxE7(W[124],W[125],W[126],W[127]); SBoxE6(W[128],W[129],W[130],W[131]); SBoxE5(W[132],W[133],W[134],W[135]); SBoxE4(W[136],W[137],W[138],W[139]); - round_key.copy(W + 8, 132); + round_key.copy(&W[8], 132); } } diff --git a/src/block/serpent/serpent.h b/src/block/serpent/serpent.h index dc81d4178..515a90407 100644 --- a/src/block/serpent/serpent.h +++ b/src/block/serpent/serpent.h @@ -18,19 +18,19 @@ namespace Botan { class BOTAN_DLL Serpent : public BlockCipher { public: - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; - void clear() { round_key.clear(); } + void clear() { zeroise(round_key); } std::string name() const { return "Serpent"; } BlockCipher* clone() const { return new Serpent; } - Serpent() : BlockCipher(16, 16, 32, 8) {} + Serpent() : BlockCipher(16, 16, 32, 8), round_key(132) {} protected: /** * For use by subclasses using SIMD, asm, etc * @return const reference to the key schedule */ - const SecureVector<u32bit, 132>& get_round_keys() const + const SecureVector<u32bit>& get_round_keys() const { return round_key; } /** @@ -41,8 +41,8 @@ class BOTAN_DLL Serpent : public BlockCipher { round_key.set(ks, 132); } private: - void key_schedule(const byte key[], u32bit length); - SecureVector<u32bit, 132> round_key; + void key_schedule(const byte key[], size_t length); + SecureVector<u32bit> round_key; }; } diff --git a/src/block/serpent_ia32/info.txt b/src/block/serpent_ia32/info.txt index c5f0946b8..3a1dd5919 100644 --- a/src/block/serpent_ia32/info.txt +++ b/src/block/serpent_ia32/info.txt @@ -2,6 +2,10 @@ define SERPENT_IA32 load_on asm_ok +<arch> +ia32 +</arch> + <requires> asm_ia32 serpent diff --git a/src/block/serpent_ia32/serp_ia32.cpp b/src/block/serpent_ia32/serp_ia32.cpp index ecdfec9b1..76814647c 100644 --- a/src/block/serpent_ia32/serp_ia32.cpp +++ b/src/block/serpent_ia32/serp_ia32.cpp @@ -44,37 +44,37 @@ void botan_serpent_ia32_key_schedule(u32bit ks[140]); /* * Serpent Encryption */ -void Serpent_IA32::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void Serpent_IA32::encrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { botan_serpent_ia32_encrypt(in, out, this->get_round_keys()); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * Serpent Decryption */ -void Serpent_IA32::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void Serpent_IA32::decrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { botan_serpent_ia32_decrypt(in, out, this->get_round_keys()); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * Serpent Key Schedule */ -void Serpent_IA32::key_schedule(const byte key[], u32bit length) +void Serpent_IA32::key_schedule(const byte key[], size_t length) { - SecureVector<u32bit, 140> W; - for(u32bit j = 0; j != length / 4; ++j) - W[j] = load_le<u32bit>(key, j); + SecureVector<u32bit> W(140); + for(size_t i = 0; i != length / 4; ++i) + W[i] = load_le<u32bit>(key, i); W[length / 4] |= u32bit(1) << ((length%4)*8); botan_serpent_ia32_key_schedule(W); diff --git a/src/block/serpent_ia32/serp_ia32.h b/src/block/serpent_ia32/serp_ia32.h index 229a2042b..d7b5bedc7 100644 --- a/src/block/serpent_ia32/serp_ia32.h +++ b/src/block/serpent_ia32/serp_ia32.h @@ -18,12 +18,12 @@ namespace Botan { class BOTAN_DLL Serpent_IA32 : public Serpent { public: - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; BlockCipher* clone() const { return new Serpent_IA32; } private: - void key_schedule(const byte[], u32bit); + void key_schedule(const byte[], size_t); }; } diff --git a/src/block/serpent_simd/serp_simd.cpp b/src/block/serpent_simd/serp_simd.cpp index c64514de1..aef37cb99 100644 --- a/src/block/serpent_simd/serp_simd.cpp +++ b/src/block/serpent_simd/serp_simd.cpp @@ -178,13 +178,15 @@ void serpent_decrypt_4(const byte in[64], /* * Serpent Encryption */ -void Serpent_SIMD::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void Serpent_SIMD::encrypt_n(const byte in[], byte out[], size_t blocks) const { + const u32bit* KS = &(this->get_round_keys()[0]); + while(blocks >= 4) { - serpent_encrypt_4(in, out, this->get_round_keys()); - in += 4 * BLOCK_SIZE; - out += 4 * BLOCK_SIZE; + serpent_encrypt_4(in, out, KS); + in += 4 * block_size(); + out += 4 * block_size(); blocks -= 4; } @@ -195,13 +197,15 @@ void Serpent_SIMD::encrypt_n(const byte in[], byte out[], u32bit blocks) const /* * Serpent Decryption */ -void Serpent_SIMD::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void Serpent_SIMD::decrypt_n(const byte in[], byte out[], size_t blocks) const { + const u32bit* KS = &(this->get_round_keys()[0]); + while(blocks >= 4) { - serpent_decrypt_4(in, out, this->get_round_keys()); - in += 4 * BLOCK_SIZE; - out += 4 * BLOCK_SIZE; + serpent_decrypt_4(in, out, KS); + in += 4 * block_size(); + out += 4 * block_size(); blocks -= 4; } diff --git a/src/block/serpent_simd/serp_simd.h b/src/block/serpent_simd/serp_simd.h index f0a11fc93..b3c0b06c8 100644 --- a/src/block/serpent_simd/serp_simd.h +++ b/src/block/serpent_simd/serp_simd.h @@ -18,10 +18,10 @@ namespace Botan { class BOTAN_DLL Serpent_SIMD : public Serpent { public: - u32bit parallelism() const { return 4; } + size_t parallelism() const { return 4; } - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; BlockCipher* clone() const { return new Serpent_SIMD; } }; diff --git a/src/block/skipjack/skipjack.cpp b/src/block/skipjack/skipjack.cpp index b23d1e160..7f25cc90a 100644 --- a/src/block/skipjack/skipjack.cpp +++ b/src/block/skipjack/skipjack.cpp @@ -15,7 +15,7 @@ namespace { /* * Skipjack Stepping Rule 'A' */ -void step_A(u16bit& W1, u16bit& W4, u32bit round, const byte FTAB[]) +void step_A(u16bit& W1, u16bit& W4, size_t round, const byte FTAB[]) { byte G1 = get_byte(0, W1), G2 = get_byte(1, W1), G3; @@ -31,7 +31,7 @@ void step_A(u16bit& W1, u16bit& W4, u32bit round, const byte FTAB[]) /* * Skipjack Stepping Rule 'B' */ -void step_B(u16bit& W1, u16bit& W2, u32bit round, const byte FTAB[]) +void step_B(u16bit& W1, u16bit& W2, size_t round, const byte FTAB[]) { W2 ^= W1 ^ round; byte G1 = get_byte(0, W1), G2 = get_byte(1, W1), G3; @@ -45,7 +45,7 @@ void step_B(u16bit& W1, u16bit& W2, u32bit round, const byte FTAB[]) /* * Skipjack Invserse Stepping Rule 'A' */ -void step_Ai(u16bit& W1, u16bit& W2, u32bit round, const byte FTAB[]) +void step_Ai(u16bit& W1, u16bit& W2, size_t round, const byte FTAB[]) { W1 ^= W2 ^ round; byte G1 = get_byte(1, W2), G2 = get_byte(0, W2), G3; @@ -59,7 +59,7 @@ void step_Ai(u16bit& W1, u16bit& W2, u32bit round, const byte FTAB[]) /* * Skipjack Invserse Stepping Rule 'B' */ -void step_Bi(u16bit& W2, u16bit& W3, u32bit round, const byte FTAB[]) +void step_Bi(u16bit& W2, u16bit& W3, size_t round, const byte FTAB[]) { byte G1 = get_byte(1, W2), G2 = get_byte(0, W2), G3; G3 = FTAB[((4 * round - 1) % 10)*256 + G2] ^ G1; @@ -75,85 +75,89 @@ void step_Bi(u16bit& W2, u16bit& W3, u32bit round, const byte FTAB[]) /* * Skipjack Encryption */ -void Skipjack::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void Skipjack::encrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + const byte* ftab = &FTAB[0]; + + for(size_t i = 0; i != blocks; ++i) { u16bit W1 = load_le<u16bit>(in, 3); u16bit W2 = load_le<u16bit>(in, 2); u16bit W3 = load_le<u16bit>(in, 1); u16bit W4 = load_le<u16bit>(in, 0); - step_A(W1, W4, 1, FTAB); step_A(W4, W3, 2, FTAB); - step_A(W3, W2, 3, FTAB); step_A(W2, W1, 4, FTAB); - step_A(W1, W4, 5, FTAB); step_A(W4, W3, 6, FTAB); - step_A(W3, W2, 7, FTAB); step_A(W2, W1, 8, FTAB); + step_A(W1, W4, 1, ftab); step_A(W4, W3, 2, ftab); + step_A(W3, W2, 3, ftab); step_A(W2, W1, 4, ftab); + step_A(W1, W4, 5, ftab); step_A(W4, W3, 6, ftab); + step_A(W3, W2, 7, ftab); step_A(W2, W1, 8, ftab); - step_B(W1, W2, 9, FTAB); step_B(W4, W1, 10, FTAB); - step_B(W3, W4, 11, FTAB); step_B(W2, W3, 12, FTAB); - step_B(W1, W2, 13, FTAB); step_B(W4, W1, 14, FTAB); - step_B(W3, W4, 15, FTAB); step_B(W2, W3, 16, FTAB); + step_B(W1, W2, 9, ftab); step_B(W4, W1, 10, ftab); + step_B(W3, W4, 11, ftab); step_B(W2, W3, 12, ftab); + step_B(W1, W2, 13, ftab); step_B(W4, W1, 14, ftab); + step_B(W3, W4, 15, ftab); step_B(W2, W3, 16, ftab); - step_A(W1, W4, 17, FTAB); step_A(W4, W3, 18, FTAB); - step_A(W3, W2, 19, FTAB); step_A(W2, W1, 20, FTAB); - step_A(W1, W4, 21, FTAB); step_A(W4, W3, 22, FTAB); - step_A(W3, W2, 23, FTAB); step_A(W2, W1, 24, FTAB); + step_A(W1, W4, 17, ftab); step_A(W4, W3, 18, ftab); + step_A(W3, W2, 19, ftab); step_A(W2, W1, 20, ftab); + step_A(W1, W4, 21, ftab); step_A(W4, W3, 22, ftab); + step_A(W3, W2, 23, ftab); step_A(W2, W1, 24, ftab); - step_B(W1, W2, 25, FTAB); step_B(W4, W1, 26, FTAB); - step_B(W3, W4, 27, FTAB); step_B(W2, W3, 28, FTAB); - step_B(W1, W2, 29, FTAB); step_B(W4, W1, 30, FTAB); - step_B(W3, W4, 31, FTAB); step_B(W2, W3, 32, FTAB); + step_B(W1, W2, 25, ftab); step_B(W4, W1, 26, ftab); + step_B(W3, W4, 27, ftab); step_B(W2, W3, 28, ftab); + step_B(W1, W2, 29, ftab); step_B(W4, W1, 30, ftab); + step_B(W3, W4, 31, ftab); step_B(W2, W3, 32, ftab); store_le(out, W4, W3, W2, W1); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * Skipjack Decryption */ -void Skipjack::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void Skipjack::decrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + const byte* ftab = &FTAB[0]; + + for(size_t i = 0; i != blocks; ++i) { u16bit W1 = load_le<u16bit>(in, 3); u16bit W2 = load_le<u16bit>(in, 2); u16bit W3 = load_le<u16bit>(in, 1); u16bit W4 = load_le<u16bit>(in, 0); - step_Bi(W2, W3, 32, FTAB); step_Bi(W3, W4, 31, FTAB); - step_Bi(W4, W1, 30, FTAB); step_Bi(W1, W2, 29, FTAB); - step_Bi(W2, W3, 28, FTAB); step_Bi(W3, W4, 27, FTAB); - step_Bi(W4, W1, 26, FTAB); step_Bi(W1, W2, 25, FTAB); + step_Bi(W2, W3, 32, ftab); step_Bi(W3, W4, 31, ftab); + step_Bi(W4, W1, 30, ftab); step_Bi(W1, W2, 29, ftab); + step_Bi(W2, W3, 28, ftab); step_Bi(W3, W4, 27, ftab); + step_Bi(W4, W1, 26, ftab); step_Bi(W1, W2, 25, ftab); - step_Ai(W1, W2, 24, FTAB); step_Ai(W2, W3, 23, FTAB); - step_Ai(W3, W4, 22, FTAB); step_Ai(W4, W1, 21, FTAB); - step_Ai(W1, W2, 20, FTAB); step_Ai(W2, W3, 19, FTAB); - step_Ai(W3, W4, 18, FTAB); step_Ai(W4, W1, 17, FTAB); + step_Ai(W1, W2, 24, ftab); step_Ai(W2, W3, 23, ftab); + step_Ai(W3, W4, 22, ftab); step_Ai(W4, W1, 21, ftab); + step_Ai(W1, W2, 20, ftab); step_Ai(W2, W3, 19, ftab); + step_Ai(W3, W4, 18, ftab); step_Ai(W4, W1, 17, ftab); - step_Bi(W2, W3, 16, FTAB); step_Bi(W3, W4, 15, FTAB); - step_Bi(W4, W1, 14, FTAB); step_Bi(W1, W2, 13, FTAB); - step_Bi(W2, W3, 12, FTAB); step_Bi(W3, W4, 11, FTAB); - step_Bi(W4, W1, 10, FTAB); step_Bi(W1, W2, 9, FTAB); + step_Bi(W2, W3, 16, ftab); step_Bi(W3, W4, 15, ftab); + step_Bi(W4, W1, 14, ftab); step_Bi(W1, W2, 13, ftab); + step_Bi(W2, W3, 12, ftab); step_Bi(W3, W4, 11, ftab); + step_Bi(W4, W1, 10, ftab); step_Bi(W1, W2, 9, ftab); - step_Ai(W1, W2, 8, FTAB); step_Ai(W2, W3, 7, FTAB); - step_Ai(W3, W4, 6, FTAB); step_Ai(W4, W1, 5, FTAB); - step_Ai(W1, W2, 4, FTAB); step_Ai(W2, W3, 3, FTAB); - step_Ai(W3, W4, 2, FTAB); step_Ai(W4, W1, 1, FTAB); + step_Ai(W1, W2, 8, ftab); step_Ai(W2, W3, 7, ftab); + step_Ai(W3, W4, 6, ftab); step_Ai(W4, W1, 5, ftab); + step_Ai(W1, W2, 4, ftab); step_Ai(W2, W3, 3, ftab); + step_Ai(W3, W4, 2, ftab); step_Ai(W4, W1, 1, ftab); store_le(out, W4, W3, W2, W1); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * Skipjack Key Schedule */ -void Skipjack::key_schedule(const byte key[], u32bit) +void Skipjack::key_schedule(const byte key[], size_t) { static const byte F[256] = { 0xA3, 0xD7, 0x09, 0x83, 0xF8, 0x48, 0xF6, 0xF4, 0xB3, 0x21, 0x15, 0x78, @@ -179,8 +183,8 @@ void Skipjack::key_schedule(const byte key[], u32bit) 0x5E, 0x6C, 0xA9, 0x13, 0x57, 0x25, 0xB5, 0xE3, 0xBD, 0xA8, 0x3A, 0x01, 0x05, 0x59, 0x2A, 0x46 }; - for(u32bit i = 0; i != 10; ++i) - for(u32bit j = 0; j != 256; ++j) + for(size_t i = 0; i != 10; ++i) + for(size_t j = 0; j != 256; ++j) FTAB[256*i+j] = F[j ^ key[9-i]]; } @@ -189,7 +193,7 @@ void Skipjack::key_schedule(const byte key[], u32bit) */ void Skipjack::clear() { - FTAB.clear(); + zeroise(FTAB); } } diff --git a/src/block/skipjack/skipjack.h b/src/block/skipjack/skipjack.h index 29978efc7..dff85df6c 100644 --- a/src/block/skipjack/skipjack.h +++ b/src/block/skipjack/skipjack.h @@ -18,18 +18,18 @@ namespace Botan { class BOTAN_DLL Skipjack : public BlockCipher { public: - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; void clear(); std::string name() const { return "Skipjack"; } BlockCipher* clone() const { return new Skipjack; } - Skipjack() : BlockCipher(8, 10) {} + Skipjack() : BlockCipher(8, 10), FTAB(2560) {} private: - void key_schedule(const byte[], u32bit); + void key_schedule(const byte[], size_t); - SecureVector<byte, 2560> FTAB; + SecureVector<byte> FTAB; }; } diff --git a/src/block/square/square.cpp b/src/block/square/square.cpp index adcf18611..ba86dd931 100644 --- a/src/block/square/square.cpp +++ b/src/block/square/square.cpp @@ -14,9 +14,9 @@ namespace Botan { /* * Square Encryption */ -void Square::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void Square::encrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { u32bit B0, B1, B2, B3; @@ -29,7 +29,7 @@ void Square::encrypt_n(const byte in[], byte out[], u32bit blocks) const B3 = TE0[in[ 3] ^ ME[ 3]] ^ TE1[in[ 7] ^ ME[ 7]] ^ TE2[in[11] ^ ME[11]] ^ TE3[in[15] ^ ME[15]] ^ EK[3]; - for(u32bit j = 1; j != 7; j += 2) + for(size_t j = 1; j != 7; j += 2) { u32bit T0, T1, T2, T3; T0 = TE0[get_byte(0, B0)] ^ TE1[get_byte(0, B1)] ^ @@ -68,17 +68,17 @@ void Square::encrypt_n(const byte in[], byte out[], u32bit blocks) const out[14] = SE[get_byte(3, B2)] ^ ME[30]; out[15] = SE[get_byte(3, B3)] ^ ME[31]; - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * Square Decryption */ -void Square::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void Square::decrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { u32bit B0, B1, B2, B3; @@ -91,7 +91,7 @@ void Square::decrypt_n(const byte in[], byte out[], u32bit blocks) const B3 = TD0[in[ 3] ^ MD[ 3]] ^ TD1[in[ 7] ^ MD[ 7]] ^ TD2[in[11] ^ MD[11]] ^ TD3[in[15] ^ MD[15]] ^ DK[3]; - for(u32bit j = 1; j != 7; j += 2) + for(size_t j = 1; j != 7; j += 2) { u32bit T0, T1, T2, T3; T0 = TD0[get_byte(0, B0)] ^ TD1[get_byte(0, B1)] ^ @@ -130,35 +130,36 @@ void Square::decrypt_n(const byte in[], byte out[], u32bit blocks) const out[14] = SD[get_byte(3, B2)] ^ MD[30]; out[15] = SD[get_byte(3, B3)] ^ MD[31]; - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * Square Key Schedule */ -void Square::key_schedule(const byte key[], u32bit) +void Square::key_schedule(const byte key[], size_t) { - SecureVector<u32bit, 36> XEK, XDK; + SecureVector<u32bit> XEK(36), XDK(36); - for(u32bit i = 0; i != 4; ++i) + for(size_t i = 0; i != 4; ++i) XEK[i] = load_be<u32bit>(key, i); - for(u32bit i = 0; i != 8; ++i) + for(size_t i = 0; i != 8; ++i) { XEK[4*i+4] = XEK[4*i ] ^ rotate_left(XEK[4*i+3], 8) ^ (0x01000000 << i); XEK[4*i+5] = XEK[4*i+1] ^ XEK[4*i+4]; XEK[4*i+6] = XEK[4*i+2] ^ XEK[4*i+5]; XEK[4*i+7] = XEK[4*i+3] ^ XEK[4*i+6]; - XDK.copy(28 - 4*i, XEK + 4*(i+1), 4); + for(size_t j = 0; j != 4; ++j) + XDK[28 - 4*i + j] = XEK[4*(i+1)+j]; - transform(XEK + 4*i); + transform(&XEK[4*i]); } - for(u32bit i = 0; i != 4; ++i) - for(u32bit j = 0; j != 4; ++j) + for(size_t i = 0; i != 4; ++i) + for(size_t j = 0; j != 4; ++j) { ME[4*i+j ] = get_byte(j, XEK[i ]); ME[4*i+j+16] = get_byte(j, XEK[i+32]); @@ -166,8 +167,8 @@ void Square::key_schedule(const byte key[], u32bit) MD[4*i+j+16] = get_byte(j, XEK[i ]); } - EK.copy(XEK + 4, 28); - DK.copy(XDK + 4, 28); + EK.copy(&XEK[4], 28); + DK.copy(&XDK[4], 28); } /* @@ -181,14 +182,14 @@ void Square::transform(u32bit round_key[4]) { 1, 3, 2, 1 }, { 1, 1, 3, 2 } }; - for(u32bit i = 0; i != 4; ++i) + for(size_t i = 0; i != 4; ++i) { byte A[4] = { 0 }, B[4] = { 0 }; store_be(round_key[i], A); - for(u32bit j = 0; j != 4; ++j) - for(u32bit k = 0; k != 4; ++k) + for(size_t j = 0; j != 4; ++j) + for(size_t k = 0; k != 4; ++k) { const byte a = A[k]; const byte b = G[k][j]; @@ -206,10 +207,10 @@ void Square::transform(u32bit round_key[4]) */ void Square::clear() { - EK.clear(); - DK.clear(); - ME.clear(); - MD.clear(); + zeroise(EK); + zeroise(DK); + zeroise(ME); + zeroise(MD); } } diff --git a/src/block/square/square.h b/src/block/square/square.h index a17771f11..0a134bcb5 100644 --- a/src/block/square/square.h +++ b/src/block/square/square.h @@ -18,16 +18,16 @@ namespace Botan { class BOTAN_DLL Square : public BlockCipher { public: - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; void clear(); std::string name() const { return "Square"; } BlockCipher* clone() const { return new Square; } - Square() : BlockCipher(16, 16) {} + Square() : BlockCipher(16, 16), EK(28), DK(28), ME(32), MD(32) {} private: - void key_schedule(const byte[], u32bit); + void key_schedule(const byte[], size_t); static void transform(u32bit[4]); @@ -45,8 +45,8 @@ class BOTAN_DLL Square : public BlockCipher static const u32bit TD2[256]; static const u32bit TD3[256]; - SecureVector<u32bit, 28> EK, DK; - SecureVector<byte, 32> ME, MD; + SecureVector<u32bit> EK, DK; + SecureVector<byte> ME, MD; }; } diff --git a/src/block/tea/tea.cpp b/src/block/tea/tea.cpp index de30858da..328786a14 100644 --- a/src/block/tea/tea.cpp +++ b/src/block/tea/tea.cpp @@ -13,14 +13,15 @@ namespace Botan { /* * TEA Encryption */ -void TEA::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void TEA::encrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { - u32bit L = load_be<u32bit>(in, 0), R = load_be<u32bit>(in, 1); + u32bit L = load_be<u32bit>(in, 0); + u32bit R = load_be<u32bit>(in, 1); u32bit S = 0; - for(u32bit j = 0; j != 32; ++j) + for(size_t j = 0; j != 32; ++j) { S += 0x9E3779B9; L += ((R << 4) + K[0]) ^ (R + S) ^ ((R >> 5) + K[1]); @@ -29,22 +30,23 @@ void TEA::encrypt_n(const byte in[], byte out[], u32bit blocks) const store_be(out, L, R); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * TEA Decryption */ -void TEA::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void TEA::decrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { - u32bit L = load_be<u32bit>(in, 0), R = load_be<u32bit>(in, 1); + u32bit L = load_be<u32bit>(in, 0); + u32bit R = load_be<u32bit>(in, 1); u32bit S = 0xC6EF3720; - for(u32bit j = 0; j != 32; ++j) + for(size_t j = 0; j != 32; ++j) { R -= ((L << 4) + K[2]) ^ (L + S) ^ ((L >> 5) + K[3]); L -= ((R << 4) + K[0]) ^ (R + S) ^ ((R >> 5) + K[1]); @@ -53,18 +55,18 @@ void TEA::decrypt_n(const byte in[], byte out[], u32bit blocks) const store_be(out, L, R); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * TEA Key Schedule */ -void TEA::key_schedule(const byte key[], u32bit) +void TEA::key_schedule(const byte key[], size_t) { - for(u32bit j = 0; j != 4; ++j) - K[j] = load_be<u32bit>(key, j); + for(size_t i = 0; i != 4; ++i) + K[i] = load_be<u32bit>(key, i); } } diff --git a/src/block/tea/tea.h b/src/block/tea/tea.h index 128f42080..eeab13cbc 100644 --- a/src/block/tea/tea.h +++ b/src/block/tea/tea.h @@ -18,17 +18,17 @@ namespace Botan { class BOTAN_DLL TEA : public BlockCipher { public: - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; - void clear() { K.clear(); } + void clear() { zeroise(K); } std::string name() const { return "TEA"; } BlockCipher* clone() const { return new TEA; } - TEA() : BlockCipher(8, 16) {} + TEA() : BlockCipher(8, 16), K(4) {} private: - void key_schedule(const byte[], u32bit); - SecureVector<u32bit, 4> K; + void key_schedule(const byte[], size_t); + SecureVector<u32bit> K; }; } diff --git a/src/block/twofish/twofish.cpp b/src/block/twofish/twofish.cpp index a183821b2..a573c2ec8 100644 --- a/src/block/twofish/twofish.cpp +++ b/src/block/twofish/twofish.cpp @@ -14,182 +14,194 @@ namespace Botan { /* * Twofish Encryption */ -void Twofish::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void Twofish::encrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { - u32bit A = load_le<u32bit>(in, 0) ^ round_key[0]; - u32bit B = load_le<u32bit>(in, 1) ^ round_key[1]; - u32bit C = load_le<u32bit>(in, 2) ^ round_key[2]; - u32bit D = load_le<u32bit>(in, 3) ^ round_key[3]; + u32bit A = load_le<u32bit>(in, 0) ^ RK[0]; + u32bit B = load_le<u32bit>(in, 1) ^ RK[1]; + u32bit C = load_le<u32bit>(in, 2) ^ RK[2]; + u32bit D = load_le<u32bit>(in, 3) ^ RK[3]; - for(u32bit j = 0; j != 16; j += 2) + for(size_t j = 0; j != 16; j += 2) { u32bit X, Y; - X = SBox0[get_byte(3, A)] ^ SBox1[get_byte(2, A)] ^ - SBox2[get_byte(1, A)] ^ SBox3[get_byte(0, A)]; - Y = SBox0[get_byte(0, B)] ^ SBox1[get_byte(3, B)] ^ - SBox2[get_byte(2, B)] ^ SBox3[get_byte(1, B)]; + X = SB[ get_byte(3, A)] ^ SB[256+get_byte(2, A)] ^ + SB[512+get_byte(1, A)] ^ SB[768+get_byte(0, A)]; + Y = SB[ get_byte(0, B)] ^ SB[256+get_byte(3, B)] ^ + SB[512+get_byte(2, B)] ^ SB[768+get_byte(1, B)]; X += Y; - Y += X + round_key[2*j + 9]; - X += round_key[2*j + 8]; + Y += X + RK[2*j + 9]; + X += RK[2*j + 8]; C = rotate_right(C ^ X, 1); D = rotate_left(D, 1) ^ Y; - X = SBox0[get_byte(3, C)] ^ SBox1[get_byte(2, C)] ^ - SBox2[get_byte(1, C)] ^ SBox3[get_byte(0, C)]; - Y = SBox0[get_byte(0, D)] ^ SBox1[get_byte(3, D)] ^ - SBox2[get_byte(2, D)] ^ SBox3[get_byte(1, D)]; + X = SB[ get_byte(3, C)] ^ SB[256+get_byte(2, C)] ^ + SB[512+get_byte(1, C)] ^ SB[768+get_byte(0, C)]; + Y = SB[ get_byte(0, D)] ^ SB[256+get_byte(3, D)] ^ + SB[512+get_byte(2, D)] ^ SB[768+get_byte(1, D)]; X += Y; - Y += X + round_key[2*j + 11]; - X += round_key[2*j + 10]; + Y += X + RK[2*j + 11]; + X += RK[2*j + 10]; A = rotate_right(A ^ X, 1); B = rotate_left(B, 1) ^ Y; } - C ^= round_key[4]; - D ^= round_key[5]; - A ^= round_key[6]; - B ^= round_key[7]; + C ^= RK[4]; + D ^= RK[5]; + A ^= RK[6]; + B ^= RK[7]; store_le(out, C, D, A, B); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * Twofish Decryption */ -void Twofish::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void Twofish::decrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { - u32bit A = load_le<u32bit>(in, 0) ^ round_key[4]; - u32bit B = load_le<u32bit>(in, 1) ^ round_key[5]; - u32bit C = load_le<u32bit>(in, 2) ^ round_key[6]; - u32bit D = load_le<u32bit>(in, 3) ^ round_key[7]; + u32bit A = load_le<u32bit>(in, 0) ^ RK[4]; + u32bit B = load_le<u32bit>(in, 1) ^ RK[5]; + u32bit C = load_le<u32bit>(in, 2) ^ RK[6]; + u32bit D = load_le<u32bit>(in, 3) ^ RK[7]; - for(u32bit j = 0; j != 16; j += 2) + for(size_t j = 0; j != 16; j += 2) { u32bit X, Y; - X = SBox0[get_byte(3, A)] ^ SBox1[get_byte(2, A)] ^ - SBox2[get_byte(1, A)] ^ SBox3[get_byte(0, A)]; - Y = SBox0[get_byte(0, B)] ^ SBox1[get_byte(3, B)] ^ - SBox2[get_byte(2, B)] ^ SBox3[get_byte(1, B)]; + X = SB[ get_byte(3, A)] ^ SB[256+get_byte(2, A)] ^ + SB[512+get_byte(1, A)] ^ SB[768+get_byte(0, A)]; + Y = SB[ get_byte(0, B)] ^ SB[256+get_byte(3, B)] ^ + SB[512+get_byte(2, B)] ^ SB[768+get_byte(1, B)]; X += Y; - Y += X + round_key[39 - 2*j]; - X += round_key[38 - 2*j]; + Y += X + RK[39 - 2*j]; + X += RK[38 - 2*j]; C = rotate_left(C, 1) ^ X; D = rotate_right(D ^ Y, 1); - X = SBox0[get_byte(3, C)] ^ SBox1[get_byte(2, C)] ^ - SBox2[get_byte(1, C)] ^ SBox3[get_byte(0, C)]; - Y = SBox0[get_byte(0, D)] ^ SBox1[get_byte(3, D)] ^ - SBox2[get_byte(2, D)] ^ SBox3[get_byte(1, D)]; + X = SB[ get_byte(3, C)] ^ SB[256+get_byte(2, C)] ^ + SB[512+get_byte(1, C)] ^ SB[768+get_byte(0, C)]; + Y = SB[ get_byte(0, D)] ^ SB[256+get_byte(3, D)] ^ + SB[512+get_byte(2, D)] ^ SB[768+get_byte(1, D)]; X += Y; - Y += X + round_key[37 - 2*j]; - X += round_key[36 - 2*j]; + Y += X + RK[37 - 2*j]; + X += RK[36 - 2*j]; A = rotate_left(A, 1) ^ X; B = rotate_right(B ^ Y, 1); } - C ^= round_key[0]; - D ^= round_key[1]; - A ^= round_key[2]; - B ^= round_key[3]; + C ^= RK[0]; + D ^= RK[1]; + A ^= RK[2]; + B ^= RK[3]; store_le(out, C, D, A, B); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * Twofish Key Schedule */ -void Twofish::key_schedule(const byte key[], u32bit length) +void Twofish::key_schedule(const byte key[], size_t length) { - SecureVector<byte, 16> S; + SecureVector<byte> S(16); - for(u32bit j = 0; j != length; ++j) - rs_mul(S + 4*(j/8), key[j], j); + for(size_t i = 0; i != length; ++i) + rs_mul(&S[4*(i/8)], key[i], i); if(length == 16) { - for(u32bit j = 0; j != 256; ++j) + for(size_t i = 0; i != 256; ++i) { - SBox0[j] = MDS0[Q0[Q0[j]^S[ 0]]^S[ 4]]; - SBox1[j] = MDS1[Q0[Q1[j]^S[ 1]]^S[ 5]]; - SBox2[j] = MDS2[Q1[Q0[j]^S[ 2]]^S[ 6]]; - SBox3[j] = MDS3[Q1[Q1[j]^S[ 3]]^S[ 7]]; + SB[ i] = MDS0[Q0[Q0[i]^S[ 0]]^S[ 4]]; + SB[256+i] = MDS1[Q0[Q1[i]^S[ 1]]^S[ 5]]; + SB[512+i] = MDS2[Q1[Q0[i]^S[ 2]]^S[ 6]]; + SB[768+i] = MDS3[Q1[Q1[i]^S[ 3]]^S[ 7]]; } - for(u32bit j = 0; j != 40; j += 2) + + for(size_t i = 0; i != 40; i += 2) { - u32bit X = MDS0[Q0[Q0[j ]^key[ 8]]^key[ 0]] ^ - MDS1[Q0[Q1[j ]^key[ 9]]^key[ 1]] ^ - MDS2[Q1[Q0[j ]^key[10]]^key[ 2]] ^ - MDS3[Q1[Q1[j ]^key[11]]^key[ 3]]; - u32bit Y = MDS0[Q0[Q0[j+1]^key[12]]^key[ 4]] ^ - MDS1[Q0[Q1[j+1]^key[13]]^key[ 5]] ^ - MDS2[Q1[Q0[j+1]^key[14]]^key[ 6]] ^ - MDS3[Q1[Q1[j+1]^key[15]]^key[ 7]]; - Y = rotate_left(Y, 8); X += Y; Y += X; - round_key[j] = X; round_key[j+1] = rotate_left(Y, 9); + u32bit X = MDS0[Q0[Q0[i ]^key[ 8]]^key[ 0]] ^ + MDS1[Q0[Q1[i ]^key[ 9]]^key[ 1]] ^ + MDS2[Q1[Q0[i ]^key[10]]^key[ 2]] ^ + MDS3[Q1[Q1[i ]^key[11]]^key[ 3]]; + u32bit Y = MDS0[Q0[Q0[i+1]^key[12]]^key[ 4]] ^ + MDS1[Q0[Q1[i+1]^key[13]]^key[ 5]] ^ + MDS2[Q1[Q0[i+1]^key[14]]^key[ 6]] ^ + MDS3[Q1[Q1[i+1]^key[15]]^key[ 7]]; + Y = rotate_left(Y, 8); + X += Y; Y += X; + + RK[i] = X; + RK[i+1] = rotate_left(Y, 9); } } else if(length == 24) { - for(u32bit j = 0; j != 256; ++j) + for(size_t i = 0; i != 256; ++i) { - SBox0[j] = MDS0[Q0[Q0[Q1[j]^S[ 0]]^S[ 4]]^S[ 8]]; - SBox1[j] = MDS1[Q0[Q1[Q1[j]^S[ 1]]^S[ 5]]^S[ 9]]; - SBox2[j] = MDS2[Q1[Q0[Q0[j]^S[ 2]]^S[ 6]]^S[10]]; - SBox3[j] = MDS3[Q1[Q1[Q0[j]^S[ 3]]^S[ 7]]^S[11]]; + SB[ i] = MDS0[Q0[Q0[Q1[i]^S[ 0]]^S[ 4]]^S[ 8]]; + SB[256+i] = MDS1[Q0[Q1[Q1[i]^S[ 1]]^S[ 5]]^S[ 9]]; + SB[512+i] = MDS2[Q1[Q0[Q0[i]^S[ 2]]^S[ 6]]^S[10]]; + SB[768+i] = MDS3[Q1[Q1[Q0[i]^S[ 3]]^S[ 7]]^S[11]]; } - for(u32bit j = 0; j != 40; j += 2) + + for(size_t i = 0; i != 40; i += 2) { - u32bit X = MDS0[Q0[Q0[Q1[j ]^key[16]]^key[ 8]]^key[ 0]] ^ - MDS1[Q0[Q1[Q1[j ]^key[17]]^key[ 9]]^key[ 1]] ^ - MDS2[Q1[Q0[Q0[j ]^key[18]]^key[10]]^key[ 2]] ^ - MDS3[Q1[Q1[Q0[j ]^key[19]]^key[11]]^key[ 3]]; - u32bit Y = MDS0[Q0[Q0[Q1[j+1]^key[20]]^key[12]]^key[ 4]] ^ - MDS1[Q0[Q1[Q1[j+1]^key[21]]^key[13]]^key[ 5]] ^ - MDS2[Q1[Q0[Q0[j+1]^key[22]]^key[14]]^key[ 6]] ^ - MDS3[Q1[Q1[Q0[j+1]^key[23]]^key[15]]^key[ 7]]; - Y = rotate_left(Y, 8); X += Y; Y += X; - round_key[j] = X; round_key[j+1] = rotate_left(Y, 9); + u32bit X = MDS0[Q0[Q0[Q1[i ]^key[16]]^key[ 8]]^key[ 0]] ^ + MDS1[Q0[Q1[Q1[i ]^key[17]]^key[ 9]]^key[ 1]] ^ + MDS2[Q1[Q0[Q0[i ]^key[18]]^key[10]]^key[ 2]] ^ + MDS3[Q1[Q1[Q0[i ]^key[19]]^key[11]]^key[ 3]]; + u32bit Y = MDS0[Q0[Q0[Q1[i+1]^key[20]]^key[12]]^key[ 4]] ^ + MDS1[Q0[Q1[Q1[i+1]^key[21]]^key[13]]^key[ 5]] ^ + MDS2[Q1[Q0[Q0[i+1]^key[22]]^key[14]]^key[ 6]] ^ + MDS3[Q1[Q1[Q0[i+1]^key[23]]^key[15]]^key[ 7]]; + Y = rotate_left(Y, 8); + X += Y; Y += X; + + RK[i] = X; + RK[i+1] = rotate_left(Y, 9); } } else if(length == 32) { - for(u32bit j = 0; j != 256; ++j) + for(size_t i = 0; i != 256; ++i) { - SBox0[j] = MDS0[Q0[Q0[Q1[Q1[j]^S[ 0]]^S[ 4]]^S[ 8]]^S[12]]; - SBox1[j] = MDS1[Q0[Q1[Q1[Q0[j]^S[ 1]]^S[ 5]]^S[ 9]]^S[13]]; - SBox2[j] = MDS2[Q1[Q0[Q0[Q0[j]^S[ 2]]^S[ 6]]^S[10]]^S[14]]; - SBox3[j] = MDS3[Q1[Q1[Q0[Q1[j]^S[ 3]]^S[ 7]]^S[11]]^S[15]]; + SB[ i] = MDS0[Q0[Q0[Q1[Q1[i]^S[ 0]]^S[ 4]]^S[ 8]]^S[12]]; + SB[256+i] = MDS1[Q0[Q1[Q1[Q0[i]^S[ 1]]^S[ 5]]^S[ 9]]^S[13]]; + SB[512+i] = MDS2[Q1[Q0[Q0[Q0[i]^S[ 2]]^S[ 6]]^S[10]]^S[14]]; + SB[768+i] = MDS3[Q1[Q1[Q0[Q1[i]^S[ 3]]^S[ 7]]^S[11]]^S[15]]; } - for(u32bit j = 0; j != 40; j += 2) + + for(size_t i = 0; i != 40; i += 2) { - u32bit X = MDS0[Q0[Q0[Q1[Q1[j ]^key[24]]^key[16]]^key[ 8]]^key[ 0]] ^ - MDS1[Q0[Q1[Q1[Q0[j ]^key[25]]^key[17]]^key[ 9]]^key[ 1]] ^ - MDS2[Q1[Q0[Q0[Q0[j ]^key[26]]^key[18]]^key[10]]^key[ 2]] ^ - MDS3[Q1[Q1[Q0[Q1[j ]^key[27]]^key[19]]^key[11]]^key[ 3]]; - u32bit Y = MDS0[Q0[Q0[Q1[Q1[j+1]^key[28]]^key[20]]^key[12]]^key[ 4]] ^ - MDS1[Q0[Q1[Q1[Q0[j+1]^key[29]]^key[21]]^key[13]]^key[ 5]] ^ - MDS2[Q1[Q0[Q0[Q0[j+1]^key[30]]^key[22]]^key[14]]^key[ 6]] ^ - MDS3[Q1[Q1[Q0[Q1[j+1]^key[31]]^key[23]]^key[15]]^key[ 7]]; - Y = rotate_left(Y, 8); X += Y; Y += X; - round_key[j] = X; round_key[j+1] = rotate_left(Y, 9); + u32bit X = MDS0[Q0[Q0[Q1[Q1[i ]^key[24]]^key[16]]^key[ 8]]^key[ 0]] ^ + MDS1[Q0[Q1[Q1[Q0[i ]^key[25]]^key[17]]^key[ 9]]^key[ 1]] ^ + MDS2[Q1[Q0[Q0[Q0[i ]^key[26]]^key[18]]^key[10]]^key[ 2]] ^ + MDS3[Q1[Q1[Q0[Q1[i ]^key[27]]^key[19]]^key[11]]^key[ 3]]; + u32bit Y = MDS0[Q0[Q0[Q1[Q1[i+1]^key[28]]^key[20]]^key[12]]^key[ 4]] ^ + MDS1[Q0[Q1[Q1[Q0[i+1]^key[29]]^key[21]]^key[13]]^key[ 5]] ^ + MDS2[Q1[Q0[Q0[Q0[i+1]^key[30]]^key[22]]^key[14]]^key[ 6]] ^ + MDS3[Q1[Q1[Q0[Q1[i+1]^key[31]]^key[23]]^key[15]]^key[ 7]]; + Y = rotate_left(Y, 8); + X += Y; Y += X; + + RK[i] = X; + RK[i+1] = rotate_left(Y, 9); } } } @@ -197,7 +209,7 @@ void Twofish::key_schedule(const byte key[], u32bit length) /* * Do one column of the RS matrix multiplcation */ -void Twofish::rs_mul(byte S[4], byte key, u32bit offset) +void Twofish::rs_mul(byte S[4], byte key, size_t offset) { if(key) { @@ -220,11 +232,8 @@ void Twofish::rs_mul(byte S[4], byte key, u32bit offset) */ void Twofish::clear() { - SBox0.clear(); - SBox1.clear(); - SBox2.clear(); - SBox3.clear(); - round_key.clear(); + zeroise(SB); + zeroise(RK); } } diff --git a/src/block/twofish/twofish.h b/src/block/twofish/twofish.h index 3191dc963..38263af98 100644 --- a/src/block/twofish/twofish.h +++ b/src/block/twofish/twofish.h @@ -18,18 +18,18 @@ namespace Botan { class BOTAN_DLL Twofish : public BlockCipher { public: - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; void clear(); std::string name() const { return "Twofish"; } BlockCipher* clone() const { return new Twofish; } - Twofish() : BlockCipher(16, 16, 32, 8) {} + Twofish() : BlockCipher(16, 16, 32, 8), SB(1024), RK(40) {} private: - void key_schedule(const byte[], u32bit); + void key_schedule(const byte[], size_t); - static void rs_mul(byte[4], byte, u32bit); + static void rs_mul(byte[4], byte, size_t); static const u32bit MDS0[256]; static const u32bit MDS1[256]; @@ -41,8 +41,7 @@ class BOTAN_DLL Twofish : public BlockCipher static const byte EXP_TO_POLY[255]; static const byte POLY_TO_EXP[255]; - SecureVector<u32bit, 256> SBox0, SBox1, SBox2, SBox3; - SecureVector<u32bit, 40> round_key; + SecureVector<u32bit> SB, RK; }; } diff --git a/src/block/xtea/xtea.cpp b/src/block/xtea/xtea.cpp index bb1a30374..ba07ba57c 100644 --- a/src/block/xtea/xtea.cpp +++ b/src/block/xtea/xtea.cpp @@ -17,7 +17,7 @@ void xtea_encrypt_4(const byte in[32], byte out[32], const u32bit EK[64]) u32bit L0, R0, L1, R1, L2, R2, L3, R3; load_be(in, L0, R0, L1, R1, L2, R2, L3, R3); - for(u32bit i = 0; i != 32; ++i) + for(size_t i = 0; i != 32; ++i) { L0 += (((R0 << 4) ^ (R0 >> 5)) + R0) ^ EK[2*i]; L1 += (((R1 << 4) ^ (R1 >> 5)) + R1) ^ EK[2*i]; @@ -38,7 +38,7 @@ void xtea_decrypt_4(const byte in[32], byte out[32], const u32bit EK[64]) u32bit L0, R0, L1, R1, L2, R2, L3, R3; load_be(in, L0, R0, L1, R1, L2, R2, L3, R3); - for(u32bit i = 0; i != 32; ++i) + for(size_t i = 0; i != 32; ++i) { R0 -= (((L0 << 4) ^ (L0 >> 5)) + L0) ^ EK[63 - 2*i]; R1 -= (((L1 << 4) ^ (L1 >> 5)) + L1) ^ EK[63 - 2*i]; @@ -59,21 +59,22 @@ void xtea_decrypt_4(const byte in[32], byte out[32], const u32bit EK[64]) /* * XTEA Encryption */ -void XTEA::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void XTEA::encrypt_n(const byte in[], byte out[], size_t blocks) const { while(blocks >= 4) { - xtea_encrypt_4(in, out, this->EK); - in += 4 * BLOCK_SIZE; - out += 4 * BLOCK_SIZE; + xtea_encrypt_4(in, out, &(this->EK[0])); + in += 4 * block_size(); + out += 4 * block_size(); blocks -= 4; } - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { - u32bit L = load_be<u32bit>(in, 0), R = load_be<u32bit>(in, 1); + u32bit L = load_be<u32bit>(in, 0); + u32bit R = load_be<u32bit>(in, 1); - for(u32bit j = 0; j != 32; ++j) + for(size_t j = 0; j != 32; ++j) { L += (((R << 4) ^ (R >> 5)) + R) ^ EK[2*j]; R += (((L << 4) ^ (L >> 5)) + L) ^ EK[2*j+1]; @@ -81,29 +82,30 @@ void XTEA::encrypt_n(const byte in[], byte out[], u32bit blocks) const store_be(out, L, R); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * XTEA Decryption */ -void XTEA::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void XTEA::decrypt_n(const byte in[], byte out[], size_t blocks) const { while(blocks >= 4) { - xtea_decrypt_4(in, out, this->EK); - in += 4 * BLOCK_SIZE; - out += 4 * BLOCK_SIZE; + xtea_decrypt_4(in, out, &(this->EK[0])); + in += 4 * block_size(); + out += 4 * block_size(); blocks -= 4; } - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { - u32bit L = load_be<u32bit>(in, 0), R = load_be<u32bit>(in, 1); + u32bit L = load_be<u32bit>(in, 0); + u32bit R = load_be<u32bit>(in, 1); - for(u32bit j = 0; j != 32; ++j) + for(size_t j = 0; j != 32; ++j) { R -= (((L << 4) ^ (L >> 5)) + L) ^ EK[63 - 2*j]; L -= (((R << 4) ^ (R >> 5)) + R) ^ EK[62 - 2*j]; @@ -111,22 +113,22 @@ void XTEA::decrypt_n(const byte in[], byte out[], u32bit blocks) const store_be(out, L, R); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * XTEA Key Schedule */ -void XTEA::key_schedule(const byte key[], u32bit) +void XTEA::key_schedule(const byte key[], size_t) { - SecureVector<u32bit, 4> UK; - for(u32bit i = 0; i != 4; ++i) + SecureVector<u32bit> UK(4); + for(size_t i = 0; i != 4; ++i) UK[i] = load_be<u32bit>(key, i); u32bit D = 0; - for(u32bit i = 0; i != 64; i += 2) + for(size_t i = 0; i != 64; i += 2) { EK[i ] = D + UK[D % 4]; D += 0x9E3779B9; diff --git a/src/block/xtea/xtea.h b/src/block/xtea/xtea.h index d15108939..c870f588a 100644 --- a/src/block/xtea/xtea.h +++ b/src/block/xtea/xtea.h @@ -18,23 +18,23 @@ namespace Botan { class BOTAN_DLL XTEA : public BlockCipher { public: - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; - void clear() { EK.clear(); } + void clear() { zeroise(EK); } std::string name() const { return "XTEA"; } BlockCipher* clone() const { return new XTEA; } - XTEA() : BlockCipher(8, 16) {} + XTEA() : BlockCipher(8, 16), EK(64) {} protected: /** * @return const reference to the key schedule */ - const SecureVector<u32bit, 64>& get_EK() const { return EK; } + const SecureVector<u32bit>& get_EK() const { return EK; } private: - void key_schedule(const byte[], u32bit); - SecureVector<u32bit, 64> EK; + void key_schedule(const byte[], size_t); + SecureVector<u32bit> EK; }; } diff --git a/src/block/xtea_simd/xtea_simd.cpp b/src/block/xtea_simd/xtea_simd.cpp index 794533d5e..5b73c7bb9 100644 --- a/src/block/xtea_simd/xtea_simd.cpp +++ b/src/block/xtea_simd/xtea_simd.cpp @@ -92,13 +92,15 @@ void xtea_decrypt_8(const byte in[64], byte out[64], const u32bit EK[64]) /* * XTEA Encryption */ -void XTEA_SIMD::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void XTEA_SIMD::encrypt_n(const byte in[], byte out[], size_t blocks) const { + const u32bit* KS = &(this->get_EK()[0]); + while(blocks >= 8) { - xtea_encrypt_8(in, out, this->get_EK()); - in += 8 * BLOCK_SIZE; - out += 8 * BLOCK_SIZE; + xtea_encrypt_8(in, out, KS); + in += 8 * block_size(); + out += 8 * block_size(); blocks -= 8; } @@ -109,13 +111,15 @@ void XTEA_SIMD::encrypt_n(const byte in[], byte out[], u32bit blocks) const /* * XTEA Decryption */ -void XTEA_SIMD::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void XTEA_SIMD::decrypt_n(const byte in[], byte out[], size_t blocks) const { + const u32bit* KS = &(this->get_EK()[0]); + while(blocks >= 8) { - xtea_decrypt_8(in, out, this->get_EK()); - in += 8 * BLOCK_SIZE; - out += 8 * BLOCK_SIZE; + xtea_decrypt_8(in, out, KS); + in += 8 * block_size(); + out += 8 * block_size(); blocks -= 8; } diff --git a/src/block/xtea_simd/xtea_simd.h b/src/block/xtea_simd/xtea_simd.h index 87eeb433b..ecfdf90a5 100644 --- a/src/block/xtea_simd/xtea_simd.h +++ b/src/block/xtea_simd/xtea_simd.h @@ -18,10 +18,10 @@ namespace Botan { class BOTAN_DLL XTEA_SIMD : public XTEA { public: - u32bit parallelism() const { return 8; } + size_t parallelism() const { return 8; } - void encrypt_n(const byte in[], byte out[], u32bit blocks) const; - void decrypt_n(const byte in[], byte out[], u32bit blocks) const; + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; BlockCipher* clone() const { return new XTEA_SIMD; } }; |