aboutsummaryrefslogtreecommitdiffstats
path: root/src/block/cast
diff options
context:
space:
mode:
Diffstat (limited to 'src/block/cast')
-rw-r--r--src/block/cast/cast128.cpp37
-rw-r--r--src/block/cast/cast128.h15
-rw-r--r--src/block/cast/cast256.cpp31
-rw-r--r--src/block/cast/cast256.h14
4 files changed, 50 insertions, 47 deletions
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];