diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/block/rc5/rc5.cpp | 28 | ||||
-rw-r--r-- | src/block/rc5/rc5.h | 6 | ||||
-rw-r--r-- | src/block/safer/safer_sk.cpp | 35 | ||||
-rw-r--r-- | src/block/safer/safer_sk.h | 2 |
4 files changed, 44 insertions, 27 deletions
diff --git a/src/block/rc5/rc5.cpp b/src/block/rc5/rc5.cpp index 86777f772..d9e74683c 100644 --- a/src/block/rc5/rc5.cpp +++ b/src/block/rc5/rc5.cpp @@ -18,20 +18,25 @@ namespace Botan { */ void RC5::encrypt_n(const byte in[], byte out[], size_t blocks) const { + const size_t rounds = (S.size() - 2) / 2; + for(size_t i = 0; i != blocks; ++i) { u32bit A = load_le<u32bit>(in, 0); u32bit B = load_le<u32bit>(in, 1); A += S[0]; B += S[1]; - for(size_t j = 0; j != ROUNDS; j += 4) + for(size_t j = 0; j != rounds; j += 4) { A = rotate_left(A ^ B, B % 32) + S[2*j+2]; B = rotate_left(B ^ A, A % 32) + S[2*j+3]; + A = rotate_left(A ^ B, B % 32) + S[2*j+4]; B = rotate_left(B ^ A, A % 32) + S[2*j+5]; + A = rotate_left(A ^ B, B % 32) + S[2*j+6]; B = rotate_left(B ^ A, A % 32) + S[2*j+7]; + A = rotate_left(A ^ B, B % 32) + S[2*j+8]; B = rotate_left(B ^ A, A % 32) + S[2*j+9]; } @@ -48,19 +53,24 @@ void RC5::encrypt_n(const byte in[], byte out[], size_t blocks) const */ void RC5::decrypt_n(const byte in[], byte out[], size_t blocks) const { + const size_t rounds = (S.size() - 2) / 2; + for(size_t i = 0; i != blocks; ++i) { u32bit A = load_le<u32bit>(in, 0); u32bit B = load_le<u32bit>(in, 1); - for(size_t j = ROUNDS; j != 0; j -= 4) + for(size_t j = rounds; j != 0; j -= 4) { B = rotate_right(B - S[2*j+1], A % 32) ^ A; A = rotate_right(A - S[2*j ], B % 32) ^ B; + B = rotate_right(B - S[2*j-1], A % 32) ^ A; A = rotate_right(A - S[2*j-2], B % 32) ^ B; + B = rotate_right(B - S[2*j-3], A % 32) ^ A; A = rotate_right(A - S[2*j-4], B % 32) ^ B; + B = rotate_right(B - S[2*j-5], A % 32) ^ A; A = rotate_right(A - S[2*j-6], B % 32) ^ B; } @@ -106,19 +116,19 @@ void RC5::key_schedule(const byte key[], size_t length) */ std::string RC5::name() const { - return "RC5(" + to_string(ROUNDS) + ")"; + return "RC5(" + to_string(get_rounds()) + ")"; } /* * RC5 Constructor */ -RC5::RC5(size_t r) : - BlockCipher_Fixed_Block_Size(1, 32), - ROUNDS(r) +RC5::RC5(size_t rounds) : BlockCipher_Fixed_Block_Size(1, 32) { - if(ROUNDS < 8 || ROUNDS > 32 || (ROUNDS % 4 != 0)) - throw Invalid_Argument(name() + ": Invalid number of rounds"); - S.resize(2*ROUNDS + 2); + if(rounds < 8 || rounds > 32 || (rounds % 4 != 0)) + throw Invalid_Argument("RC5: Invalid number of rounds " + + to_string(rounds)); + + S.resize(2*rounds + 2); } } diff --git a/src/block/rc5/rc5.h b/src/block/rc5/rc5.h index a9f3b5b0e..cb282af4e 100644 --- a/src/block/rc5/rc5.h +++ b/src/block/rc5/rc5.h @@ -23,7 +23,7 @@ class BOTAN_DLL RC5 : public BlockCipher_Fixed_Block_Size<8> void clear() { zeroise(S); } std::string name() const; - BlockCipher* clone() const { return new RC5(ROUNDS); } + BlockCipher* clone() const { return new RC5(get_rounds()); } /** * @param rounds the number of RC5 rounds to run. Must be between @@ -31,9 +31,11 @@ class BOTAN_DLL RC5 : public BlockCipher_Fixed_Block_Size<8> */ RC5(size_t rounds); private: + size_t get_rounds() const { return (S.size() - 2) / 2; } + void key_schedule(const byte[], size_t); + SecureVector<u32bit> S; - const size_t ROUNDS; }; } diff --git a/src/block/safer/safer_sk.cpp b/src/block/safer/safer_sk.cpp index cd9dddf34..941c4fed8 100644 --- a/src/block/safer/safer_sk.cpp +++ b/src/block/safer/safer_sk.cpp @@ -17,12 +17,14 @@ namespace Botan { */ void SAFER_SK::encrypt_n(const byte in[], byte out[], size_t blocks) const { + const size_t rounds = get_rounds(); + for(size_t i = 0; i != blocks; ++i) { byte A = in[0], B = in[1], C = in[2], D = in[3], E = in[4], F = in[5], G = in[6], H = in[7], X, Y; - for(size_t j = 0; j != 16*ROUNDS; j += 16) + for(size_t j = 0; j != 16*rounds; j += 16) { A = EXP[A ^ EK[j ]]; B = LOG[B + EK[j+1]]; C = LOG[C + EK[j+2]]; D = EXP[D ^ EK[j+3]]; @@ -38,10 +40,10 @@ void SAFER_SK::encrypt_n(const byte in[], byte out[], size_t blocks) const A += B; F = C + G; E = C + F; C = X; G = Y; } - out[0] = A ^ EK[16*ROUNDS+0]; out[1] = B + EK[16*ROUNDS+1]; - out[2] = C + EK[16*ROUNDS+2]; out[3] = D ^ EK[16*ROUNDS+3]; - out[4] = E ^ EK[16*ROUNDS+4]; out[5] = F + EK[16*ROUNDS+5]; - out[6] = G + EK[16*ROUNDS+6]; out[7] = H ^ EK[16*ROUNDS+7]; + out[0] = A ^ EK[16*rounds+0]; out[1] = B + EK[16*rounds+1]; + out[2] = C + EK[16*rounds+2]; out[3] = D ^ EK[16*rounds+3]; + out[4] = E ^ EK[16*rounds+4]; out[5] = F + EK[16*rounds+5]; + out[6] = G + EK[16*rounds+6]; out[7] = H ^ EK[16*rounds+7]; in += BLOCK_SIZE; out += BLOCK_SIZE; @@ -53,16 +55,18 @@ void SAFER_SK::encrypt_n(const byte in[], byte out[], size_t blocks) const */ void SAFER_SK::decrypt_n(const byte in[], byte out[], size_t blocks) const { + const size_t rounds = get_rounds(); + for(size_t i = 0; i != blocks; ++i) { byte A = in[0], B = in[1], C = in[2], D = in[3], E = in[4], F = in[5], G = in[6], H = in[7]; - A ^= EK[16*ROUNDS+0]; B -= EK[16*ROUNDS+1]; C -= EK[16*ROUNDS+2]; - D ^= EK[16*ROUNDS+3]; E ^= EK[16*ROUNDS+4]; F -= EK[16*ROUNDS+5]; - G -= EK[16*ROUNDS+6]; H ^= EK[16*ROUNDS+7]; + A ^= EK[16*rounds+0]; B -= EK[16*rounds+1]; C -= EK[16*rounds+2]; + D ^= EK[16*rounds+3]; E ^= EK[16*rounds+4]; F -= EK[16*rounds+5]; + G -= EK[16*rounds+6]; H ^= EK[16*rounds+7]; - for(s32bit j = 16*(ROUNDS-1); j >= 0; j -= 16) + for(s32bit j = 16*(rounds-1); j >= 0; j -= 16) { byte T = E; E = B; B = C; C = T; T = F; F = D; D = G; G = T; A -= E; B -= F; C -= G; D -= H; E -= A; F -= B; G -= C; H -= D; @@ -99,7 +103,7 @@ void SAFER_SK::key_schedule(const byte key[], size_t) KB[17] ^= KB[i+9] = EK[i] = key[i+8]; } - for(size_t i = 0; i != ROUNDS; ++i) + for(size_t i = 0; i != get_rounds(); ++i) { for(size_t j = 0; j != 18; ++j) KB[j] = rotate_left(KB[j], 6); @@ -113,7 +117,7 @@ void SAFER_SK::key_schedule(const byte key[], size_t) */ std::string SAFER_SK::name() const { - return "SAFER-SK(" + to_string(ROUNDS) + ")"; + return "SAFER-SK(" + to_string(get_rounds()) + ")"; } /* @@ -121,18 +125,19 @@ std::string SAFER_SK::name() const */ BlockCipher* SAFER_SK::clone() const { - return new SAFER_SK(ROUNDS); + return new SAFER_SK(get_rounds()); } /* * SAFER-SK Constructor */ SAFER_SK::SAFER_SK(size_t rounds) : - BlockCipher_Fixed_Block_Size(16), - EK(16 * rounds + 8), ROUNDS(rounds) + BlockCipher_Fixed_Block_Size(16) { - if(ROUNDS > 13 || ROUNDS == 0) + if(rounds > 13 || rounds == 0) throw Invalid_Argument(name() + ": Invalid number of rounds"); + + EK.resize(16 * rounds + 8); } } diff --git a/src/block/safer/safer_sk.h b/src/block/safer/safer_sk.h index 5e8d32b0a..2fde757bd 100644 --- a/src/block/safer/safer_sk.h +++ b/src/block/safer/safer_sk.h @@ -31,6 +31,7 @@ class BOTAN_DLL SAFER_SK : public BlockCipher_Fixed_Block_Size<8> */ SAFER_SK(size_t rounds); private: + size_t get_rounds() const { return (EK.size() - 8) / 16; } void key_schedule(const byte[], size_t); static const byte EXP[256]; @@ -39,7 +40,6 @@ class BOTAN_DLL SAFER_SK : public BlockCipher_Fixed_Block_Size<8> static const byte KEY_INDEX[208]; SecureVector<byte> EK; - const size_t ROUNDS; }; } |