Skip to content

Commit b91ce07

Browse files
committed
Revert BlowfishCompat changes (PR weidai11#877)
1 parent dd005c4 commit b91ce07

File tree

6 files changed

+40
-141
lines changed

6 files changed

+40
-141
lines changed

bfinit.cpp

+3-11
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,16 @@
33

44
NAMESPACE_BEGIN(CryptoPP)
55

6-
template<class Info, class ByteOrder>
7-
const word32 Blowfish_Base<Info, ByteOrder>::p_init[Info::ROUNDS+2] =
6+
const word32 Blowfish::Base::p_init[Blowfish::ROUNDS+2] =
87
{
98
608135816U, 2242054355U, 320440878U, 57701188U,
109
2752067618U, 698298832U, 137296536U, 3964562569U,
1110
1160258022U, 953160567U, 3193202383U, 887688300U,
1211
3232508343U, 3380367581U, 1065670069U, 3041331479U,
1312
2450970073U, 2306472731U
14-
};
13+
} ;
1514

16-
template<class Info, class ByteOrder>
17-
const word32 Blowfish_Base<Info, ByteOrder>::s_init[4*256] = {
15+
const word32 Blowfish::Base::s_init[4*256] = {
1816
3509652390U, 2564797868U, 805139163U, 3491422135U,
1917
3101798381U, 1780907670U, 3128725573U, 4046225305U,
2018
614570311U, 3012652279U, 134345442U, 2240740374U,
@@ -276,10 +274,4 @@ const word32 Blowfish_Base<Info, ByteOrder>::s_init[4*256] = {
276274
3075367218U, 3463963227U, 1469046755U, 985887462U
277275
};
278276

279-
template const word32 Blowfish_Base<Blowfish_Info, BigEndian>::p_init[Blowfish_Info::ROUNDS+2];
280-
template const word32 Blowfish_Base<Blowfish_Info, BigEndian>::s_init[4*256];
281-
282-
template const word32 Blowfish_Base<BlowfishCompat_Info, LittleEndian>::p_init[BlowfishCompat_Info::ROUNDS+2];
283-
template const word32 Blowfish_Base<BlowfishCompat_Info, LittleEndian>::s_init[4*256];
284-
285277
NAMESPACE_END

blowfish.cpp

+16-28
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,9 @@
66

77
NAMESPACE_BEGIN(CryptoPP)
88

9-
extern template const word32 Blowfish_Base<Blowfish_Info, BigEndian>::p_init[Blowfish_Info::ROUNDS+2];
10-
extern template const word32 Blowfish_Base<Blowfish_Info, BigEndian>::s_init[4*256];
11-
12-
extern template const word32 Blowfish_Base<BlowfishCompat_Info, LittleEndian>::p_init[BlowfishCompat_Info::ROUNDS+2];
13-
extern template const word32 Blowfish_Base<BlowfishCompat_Info, LittleEndian>::s_init[4*256];
14-
15-
template<class Info, class ByteOrder>
16-
void Blowfish_Base<Info, ByteOrder>::UncheckedSetKey(const byte *key_string, unsigned int keylength, const NameValuePairs &)
9+
void Blowfish::Base::UncheckedSetKey(const byte *key_string, unsigned int keylength, const NameValuePairs &)
1710
{
18-
this->AssertValidKeyLength(keylength);
11+
AssertValidKeyLength(keylength);
1912

2013
unsigned i, j=0, k;
2114
word32 data, dspace[2] = {0, 0};
@@ -24,7 +17,7 @@ void Blowfish_Base<Info, ByteOrder>::UncheckedSetKey(const byte *key_string, uns
2417
memcpy(sbox, s_init, sizeof(s_init));
2518

2619
// Xor key string into encryption key vector
27-
for (i=0 ; i<Info::ROUNDS+2 ; ++i)
20+
for (i=0 ; i<ROUNDS+2 ; ++i)
2821
{
2922
data = 0 ;
3023
for (k=0 ; k<4 ; ++k )
@@ -34,22 +27,21 @@ void Blowfish_Base<Info, ByteOrder>::UncheckedSetKey(const byte *key_string, uns
3427

3528
crypt_block(dspace, pbox);
3629

37-
for (i=0; i<Info::ROUNDS; i+=2)
30+
for (i=0; i<ROUNDS; i+=2)
3831
crypt_block(pbox+i, pbox+i+2);
3932

40-
crypt_block(pbox+Info::ROUNDS, sbox);
33+
crypt_block(pbox+ROUNDS, sbox);
4134

4235
for (i=0; i<4*256-2; i+=2)
4336
crypt_block(sbox+i, sbox+i+2);
4437

45-
if (!this->IsForwardTransformation())
46-
for (i=0; i<(Info::ROUNDS+2)/2; i++)
47-
std::swap(pbox[i], pbox[Info::ROUNDS+1-i]);
38+
if (!IsForwardTransformation())
39+
for (i=0; i<(ROUNDS+2)/2; i++)
40+
std::swap(pbox[i], pbox[ROUNDS+1-i]);
4841
}
4942

5043
// this version is only used to make pbox and sbox
51-
template<class Info, class ByteOrder>
52-
void Blowfish_Base<Info, ByteOrder>::crypt_block(const word32 in[2], word32 out[2]) const
44+
void Blowfish::Base::crypt_block(const word32 in[2], word32 out[2]) const
5345
{
5446
word32 left = in[0];
5547
word32 right = in[1];
@@ -59,7 +51,7 @@ void Blowfish_Base<Info, ByteOrder>::crypt_block(const word32 in[2], word32 out[
5951

6052
left ^= p[0];
6153

62-
for (unsigned i=0; i<Info::ROUNDS/2; i++)
54+
for (unsigned i=0; i<ROUNDS/2; i++)
6355
{
6456
right ^= (((s[GETBYTE(left,3)] + s[256+GETBYTE(left,2)])
6557
^ s[2*256+GETBYTE(left,1)]) + s[3*256+GETBYTE(left,0)])
@@ -70,16 +62,15 @@ void Blowfish_Base<Info, ByteOrder>::crypt_block(const word32 in[2], word32 out[
7062
^ p[2*i+2];
7163
}
7264

73-
right ^= p[Info::ROUNDS+1];
65+
right ^= p[ROUNDS+1];
7466

7567
out[0] = right;
7668
out[1] = left;
7769
}
7870

79-
template<class Info, class ByteOrder>
80-
void Blowfish_Base<Info, ByteOrder>::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
71+
void Blowfish::Base::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
8172
{
82-
typedef BlockGetAndPut<word32, ByteOrder> Block;
73+
typedef BlockGetAndPut<word32, BigEndian> Block;
8374

8475
word32 left, right;
8576
Block::Get(inBlock)(left)(right);
@@ -89,7 +80,7 @@ void Blowfish_Base<Info, ByteOrder>::ProcessAndXorBlock(const byte *inBlock, con
8980

9081
left ^= p[0];
9182

92-
for (unsigned i=0; i<Info::ROUNDS/2; i++)
83+
for (unsigned i=0; i<ROUNDS/2; i++)
9384
{
9485
right ^= (((s[GETBYTE(left,3)] + s[256+GETBYTE(left,2)])
9586
^ s[2*256+GETBYTE(left,1)]) + s[3*256+GETBYTE(left,0)])
@@ -100,12 +91,9 @@ void Blowfish_Base<Info, ByteOrder>::ProcessAndXorBlock(const byte *inBlock, con
10091
^ p[2*i+2];
10192
}
10293

103-
right ^= p[Info::ROUNDS+1];
94+
right ^= p[ROUNDS+1];
10495

105-
typename Block::Put(xorBlock, outBlock)(right)(left);
96+
Block::Put(xorBlock, outBlock)(right)(left);
10697
}
10798

108-
template class Blowfish_Base<Blowfish_Info, BigEndian>;
109-
template class Blowfish_Base<BlowfishCompat_Info, LittleEndian>;
110-
11199
NAMESPACE_END

blowfish.h

+21-37
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,6 @@
1111

1212
NAMESPACE_BEGIN(CryptoPP)
1313

14-
/// \brief Class specific implementation and overrides used to operate the cipher.
15-
/// \details Implementations and overrides in \p Base apply to both \p ENCRYPTION and \p DECRYPTION directions
16-
template<class Info, class ByteOrder>
17-
class CRYPTOPP_NO_VTABLE Blowfish_Base : public BlockCipherImpl<Info>
18-
{
19-
public:
20-
void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
21-
void UncheckedSetKey(const byte *key_string, unsigned int keylength, const NameValuePairs &params);
22-
23-
private:
24-
void crypt_block(const word32 in[2], word32 out[2]) const;
25-
26-
static const word32 p_init[Info::ROUNDS+2];
27-
static const word32 s_init[4*256];
28-
29-
FixedSizeSecBlock<word32, Info::ROUNDS+2> pbox;
30-
FixedSizeSecBlock<word32, 4*256> sbox;
31-
};
32-
3314
/// \brief Blowfish block cipher information
3415
struct Blowfish_Info : public FixedBlockSize<8>, public VariableKeyLength<16, 4, 56>, public FixedRounds<16>
3516
{
@@ -40,30 +21,33 @@ struct Blowfish_Info : public FixedBlockSize<8>, public VariableKeyLength<16, 4,
4021

4122
/// \brief Blowfish block cipher
4223
/// \since Crypto++ 1.0
43-
struct Blowfish : public Blowfish_Info, public BlockCipherDocumentation
24+
class Blowfish : public Blowfish_Info, public BlockCipherDocumentation
4425
{
45-
typedef BlockCipherFinal<ENCRYPTION, Blowfish_Base<Blowfish_Info, BigEndian> > Encryption;
46-
typedef BlockCipherFinal<DECRYPTION, Blowfish_Base<Blowfish_Info, BigEndian> > Decryption;
47-
};
26+
/// \brief Class specific implementation and overrides used to operate the cipher.
27+
/// \details Implementations and overrides in \p Base apply to both \p ENCRYPTION and \p DECRYPTION directions
28+
class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<Blowfish_Info>
29+
{
30+
public:
31+
void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
32+
void UncheckedSetKey(const byte *key_string, unsigned int keylength, const NameValuePairs &params);
4833

49-
typedef Blowfish::Encryption BlowfishEncryption;
50-
typedef Blowfish::Decryption BlowfishDecryption;
34+
private:
35+
void crypt_block(const word32 in[2], word32 out[2]) const;
5136

52-
/// \brief BlowfishCompat block cipher information
53-
struct BlowfishCompat_Info : public FixedBlockSize<8>, public VariableKeyLength<16, 4, 56>, public FixedRounds<16>
54-
{
55-
CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "BlowfishCompat";}
56-
};
37+
static const word32 p_init[ROUNDS+2];
38+
static const word32 s_init[4*256];
5739

58-
/// \brief BlowfishCompat block cipher
59-
struct BlowfishCompat : public BlowfishCompat_Info, public BlockCipherDocumentation
60-
{
61-
typedef BlockCipherFinal<ENCRYPTION, Blowfish_Base<BlowfishCompat_Info, LittleEndian> > Encryption;
62-
typedef BlockCipherFinal<DECRYPTION, Blowfish_Base<BlowfishCompat_Info, LittleEndian> > Decryption;
40+
FixedSizeSecBlock<word32, ROUNDS+2> pbox;
41+
FixedSizeSecBlock<word32, 4*256> sbox;
42+
};
43+
44+
public:
45+
typedef BlockCipherFinal<ENCRYPTION, Base> Encryption;
46+
typedef BlockCipherFinal<DECRYPTION, Base> Decryption;
6347
};
6448

65-
typedef BlowfishCompat::Encryption BlowfishCompatEncryption;
66-
typedef BlowfishCompat::Decryption BlowfishCompatDecryption;
49+
typedef Blowfish::Encryption BlowfishEncryption;
50+
typedef Blowfish::Decryption BlowfishDecryption;
6751

6852
NAMESPACE_END
6953

test.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,6 @@ bool Validate(int alg, bool thorough)
10251025
case 101: result = ValidateSIMECK(); break;
10261026
case 102: result = ValidateSIMON(); break;
10271027
case 103: result = ValidateSPECK(); break;
1028-
case 104: result = ValidateBlowfishCompat(); break;
10291028

10301029
case 110: result = ValidateSHA3(); break;
10311030
case 111: result = ValidateSHAKE(); break;

validat3.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ bool ValidateAll(bool thorough)
135135
pass=ValidateARC4() && pass;
136136
pass=ValidateRC5() && pass;
137137
pass=ValidateBlowfish() && pass;
138-
pass=ValidateBlowfishCompat() && pass;
139138
pass=ValidateThreeWay() && pass;
140139
pass=ValidateGOST() && pass;
141140
pass=ValidateSHARK() && pass;

validat4.cpp

-63
Original file line numberDiff line numberDiff line change
@@ -1126,69 +1126,6 @@ bool ValidateBlowfish()
11261126
return pass1 && pass2 && pass3;
11271127
}
11281128

1129-
bool ValidateBlowfishCompat()
1130-
{
1131-
std::cout << "\nBlowfishCompat validation suite running...\n\n";
1132-
bool pass1 = true, pass2 = true, pass3 = true, fail;
1133-
1134-
BlowfishCompatEncryption enc1; // 32 to 448-bits (4 to 56-bytes)
1135-
pass1 = enc1.StaticGetValidKeyLength(3) == 4 && pass1;
1136-
pass1 = enc1.StaticGetValidKeyLength(4) == 4 && pass1;
1137-
pass1 = enc1.StaticGetValidKeyLength(5) == 5 && pass1;
1138-
pass1 = enc1.StaticGetValidKeyLength(8) == 8 && pass1;
1139-
pass1 = enc1.StaticGetValidKeyLength(16) == 16 && pass1;
1140-
pass1 = enc1.StaticGetValidKeyLength(24) == 24 && pass1;
1141-
pass1 = enc1.StaticGetValidKeyLength(32) == 32 && pass1;
1142-
pass1 = enc1.StaticGetValidKeyLength(56) == 56 && pass1;
1143-
pass1 = enc1.StaticGetValidKeyLength(57) == 56 && pass1;
1144-
pass1 = enc1.StaticGetValidKeyLength(60) == 56 && pass1;
1145-
pass1 = enc1.StaticGetValidKeyLength(64) == 56 && pass1;
1146-
pass1 = enc1.StaticGetValidKeyLength(128) == 56 && pass1;
1147-
1148-
BlowfishCompatDecryption dec1; // 32 to 448-bits (4 to 56-bytes)
1149-
pass2 = dec1.StaticGetValidKeyLength(3) == 4 && pass2;
1150-
pass2 = dec1.StaticGetValidKeyLength(4) == 4 && pass2;
1151-
pass2 = dec1.StaticGetValidKeyLength(5) == 5 && pass2;
1152-
pass2 = dec1.StaticGetValidKeyLength(8) == 8 && pass2;
1153-
pass2 = dec1.StaticGetValidKeyLength(16) == 16 && pass2;
1154-
pass2 = dec1.StaticGetValidKeyLength(24) == 24 && pass2;
1155-
pass2 = dec1.StaticGetValidKeyLength(32) == 32 && pass2;
1156-
pass2 = dec1.StaticGetValidKeyLength(56) == 56 && pass2;
1157-
pass2 = dec1.StaticGetValidKeyLength(57) == 56 && pass2;
1158-
pass2 = dec1.StaticGetValidKeyLength(60) == 56 && pass2;
1159-
pass2 = dec1.StaticGetValidKeyLength(64) == 56 && pass2;
1160-
pass2 = dec1.StaticGetValidKeyLength(128) == 56 && pass2;
1161-
std::cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n";
1162-
1163-
HexEncoder output(new FileSink(std::cout));
1164-
const char *key[]={"abcdefghijklmnopqrstuvwxyz", "Who is John Galt?"};
1165-
byte *plain[]={(byte *)"BLOWFISH", (byte *)"\xfe\xdc\xba\x98\x76\x54\x32\x10"};
1166-
byte *cipher[]={(byte *)"\x82\x4d\x92\x0d\x00\x4d\x7e\xe3", (byte *)"\xd4\xf9\xb0\x06\xd3\x84\x92\x7e"};
1167-
byte out[8], outplain[8];
1168-
1169-
for (int i=0; i<2; i++)
1170-
{
1171-
ECB_Mode<BlowfishCompat>::Encryption enc2((byte *)key[i], strlen(key[i]));
1172-
enc2.ProcessData(out, plain[i], 8);
1173-
fail = memcmp(out, cipher[i], 8) != 0;
1174-
1175-
ECB_Mode<BlowfishCompat>::Decryption dec2((byte *)key[i], strlen(key[i]));
1176-
dec2.ProcessData(outplain, cipher[i], 8);
1177-
fail = fail || memcmp(outplain, plain[i], 8);
1178-
pass3 = pass3 && !fail;
1179-
1180-
std::cout << (fail ? "FAILED " : "passed ");
1181-
std::cout << '\"' << key[i] << '\"';
1182-
for (int j=0; j<(signed int)(30-strlen(key[i])); j++)
1183-
std::cout << ' ';
1184-
output.Put(outplain, 8);
1185-
std::cout << " ";
1186-
output.Put(out, 8);
1187-
std::cout << std::endl;
1188-
}
1189-
return pass1 && pass2 && pass3;
1190-
}
1191-
11921129
bool ValidateThreeWay()
11931130
{
11941131
std::cout << "\n3-WAY validation suite running...\n\n";

0 commit comments

Comments
 (0)