1 // validat4.cpp - originally written and placed in the public domain by Wei Dai
2 // CryptoPP::Test namespace added by JW in February 2017.
3 // Source files split in July 2018 to expedite compiles.
4
5 #include "pch.h"
6
7 #define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
8
9 #include "cryptlib.h"
10 #include "cpu.h"
11 #include "validate.h"
12
13 #include "hex.h"
14 #include "base32.h"
15 #include "base64.h"
16
17 #include "rc2.h"
18 #include "aes.h"
19 #include "des.h"
20 #include "rc5.h"
21 #include "rc6.h"
22 #include "3way.h"
23 #include "aria.h"
24 #include "cast.h"
25 #include "mars.h"
26 #include "idea.h"
27 #include "gost.h"
28 #include "seal.h"
29 #include "seed.h"
30 #include "safer.h"
31 #include "shark.h"
32 #include "square.h"
33 #include "serpent.h"
34 #include "shacal2.h"
35 #include "twofish.h"
36 #include "blowfish.h"
37 #include "camellia.h"
38 #include "skipjack.h"
39
40 #include "arc4.h"
41 #include "salsa.h"
42 #include "chacha.h"
43 #include "rabbit.h"
44 #include "sosemanuk.h"
45
46 #include "modes.h"
47 #include "cmac.h"
48 #include "dmac.h"
49 #include "hmac.h"
50 #include "vmac.h"
51 #include "ttmac.h"
52
53 #include "drbg.h"
54
55 #include <iostream>
56 #include <iomanip>
57 #include <sstream>
58
59 // Aggressive stack checking with VS2005 SP1 and above.
60 #if (_MSC_FULL_VER >= 140050727)
61 # pragma strict_gs_check (on)
62 #endif
63
64 #if CRYPTOPP_MSC_VERSION
65 # pragma warning(disable: 4505 4355)
66 #endif
67
68 NAMESPACE_BEGIN(CryptoPP)
NAMESPACE_BEGIN(Test)69 NAMESPACE_BEGIN(Test)
70
71
72 bool ValidateHmacDRBG()
73 {
74 std::cout << "\nTesting NIST HMAC DRBGs...\n\n";
75 bool pass=true, fail;
76
77 // # CAVS 14.3
78 // # DRBG800-90A information for "drbg_pr"
79 // # Generated on Tue Apr 02 15:32:12 2013
80
81 {
82 // [SHA-1], [PredictionResistance = False], [EntropyInputLen = 128], [NonceLen = 64]
83 // [PersonalizationStringLen = 0], [AdditionalInputLen = 0], [ReturnedBitsLen = 640]
84 const byte entropy1[] = "\x79\x34\x9b\xbf\x7c\xdd\xa5\x79\x95\x57\x86\x66\x21\xc9\x13\x83";
85 const byte entropy2[] = "\xc7\x21\x5b\x5b\x96\xc4\x8e\x9b\x33\x8c\x74\xe3\xe9\x9d\xfe\xdf";
86 const byte nonce[] = "\x11\x46\x73\x3a\xbf\x8c\x35\xc8";
87
88 HMAC_DRBG<SHA1, 128/8, 440/8> drbg(entropy1, 16, nonce, 8);
89 drbg.IncorporateEntropy(entropy2, 16);
90
91 SecByteBlock result(80);
92 drbg.GenerateBlock(result, result.size());
93 drbg.GenerateBlock(result, result.size());
94
95 const byte expected[] = "\xc6\xa1\x6a\xb8\xd4\x20\x70\x6f\x0f\x34\xab\x7f\xec\x5a\xdc\xa9\xd8\xca\x3a\x13"
96 "\x3e\x15\x9c\xa6\xac\x43\xc6\xf8\xa2\xbe\x22\x83\x4a\x4c\x0a\x0a\xff\xb1\x0d\x71"
97 "\x94\xf1\xc1\xa5\xcf\x73\x22\xec\x1a\xe0\x96\x4e\xd4\xbf\x12\x27\x46\xe0\x87\xfd"
98 "\xb5\xb3\xe9\x1b\x34\x93\xd5\xbb\x98\xfa\xed\x49\xe8\x5f\x13\x0f\xc8\xa4\x59\xb7";
99
100 fail = !!memcmp(result, expected, 640/8);
101 pass = !fail && pass;
102
103 std::cout << (fail ? "FAILED " : "passed ") << "HMAC_DRBG SHA1/128/440 (COUNT=0, E=16, N=8)\n";
104 }
105
106 {
107 // [SHA-1], [PredictionResistance = False], [EntropyInputLen = 128], [NonceLen = 64]
108 // [PersonalizationStringLen = 0], [AdditionalInputLen = 0], [ReturnedBitsLen = 640]
109 const byte entropy1[] = "\xee\x57\xfc\x23\x60\x0f\xb9\x02\x9a\x9e\xc6\xc8\x2e\x7b\x51\xe4";
110 const byte entropy2[] = "\x84\x1d\x27\x6c\xa9\x51\x90\x61\xd9\x2d\x7d\xdf\xa6\x62\x8c\xa3";
111 const byte nonce[] = "\x3e\x97\x21\xe4\x39\x3e\xf9\xad";
112
113 HMAC_DRBG<SHA1, 128/8, 440/8> drbg(entropy1, 16, nonce, 8);
114 drbg.IncorporateEntropy(entropy2, 16);
115
116 SecByteBlock result(80);
117 drbg.GenerateBlock(result, result.size());
118 drbg.GenerateBlock(result, result.size());
119
120 const byte expected[] = "\xee\x26\xa5\xc8\xef\x08\xa1\xca\x8f\x14\x15\x4d\x67\xc8\x8f\x5e\x7e\xd8\x21\x9d"
121 "\x93\x1b\x98\x42\xac\x00\x39\xf2\x14\x55\x39\xf2\x14\x2b\x44\x11\x7a\x99\x8c\x22"
122 "\xf5\x90\xf6\xc9\xb3\x8b\x46\x5b\x78\x3e\xcf\xf1\x3a\x77\x50\x20\x1f\x7e\xcf\x1b"
123 "\x8a\xb3\x93\x60\x4c\x73\xb2\x38\x93\x36\x60\x9a\xf3\x44\x0c\xde\x43\x29\x8b\x84";
124
125 fail = !!memcmp(result, expected, 640/8);
126 pass = !fail && pass;
127
128 std::cout << (fail ? "FAILED " : "passed ") << "HMAC_DRBG SHA1/128/440 (COUNT=1, E=16, N=8)\n";
129 }
130
131 // *****************************************************
132
133 {
134 // [SHA-1], [PredictionResistance = False], [EntropyInputLen = 128], [NonceLen = 64]
135 // [PersonalizationStringLen = 0], [AdditionalInputLen = 16], [ReturnedBitsLen = 640]
136 const byte entropy1[] = "\x7d\x70\x52\xa7\x76\xfd\x2f\xb3\xd7\x19\x1f\x73\x33\x04\xee\x8b";
137 const byte entropy2[] = "\x49\x04\x7e\x87\x9d\x61\x09\x55\xee\xd9\x16\xe4\x06\x0e\x00\xc9";
138 const byte nonce[] = "\xbe\x4a\x0c\xee\xdc\xa8\x02\x07";
139 const byte additional1[] = "\xfd\x8b\xb3\x3a\xab\x2f\x6c\xdf\xbc\x54\x18\x11\x86\x1d\x51\x8d";
140 const byte additional2[] = "\x99\xaf\xe3\x47\x54\x04\x61\xdd\xf6\xab\xeb\x49\x1e\x07\x15\xb4";
141 const byte additional3[] = "\x02\xf7\x73\x48\x2d\xd7\xae\x66\xf7\x6e\x38\x15\x98\xa6\x4e\xf0";
142
143 HMAC_DRBG<SHA1, 128/8, 440/8> drbg(entropy1, 16, nonce, 8);
144 drbg.IncorporateEntropy(entropy2, 16, additional1, 16);
145
146 SecByteBlock result(80);
147 drbg.GenerateBlock(additional2, 16, result, result.size());
148 drbg.GenerateBlock(additional3, 16, result, result.size());
149
150 const byte expected[] = "\xa7\x36\x34\x38\x44\xfc\x92\x51\x13\x91\xdb\x0a\xdd\xd9\x06\x4d\xbe\xe2\x4c\x89"
151 "\x76\xaa\x25\x9a\x9e\x3b\x63\x68\xaa\x6d\xe4\xc9\xbf\x3a\x0e\xff\xcd\xa9\xcb\x0e"
152 "\x9d\xc3\x36\x52\xab\x58\xec\xb7\x65\x0e\xd8\x04\x67\xf7\x6a\x84\x9f\xb1\xcf\xc1"
153 "\xed\x0a\x09\xf7\x15\x50\x86\x06\x4d\xb3\x24\xb1\xe1\x24\xf3\xfc\x9e\x61\x4f\xcb";
154
155 fail = !!memcmp(result, expected, 640/8);
156 pass = !fail && pass;
157
158 std::cout << (fail ? "FAILED " : "passed ") << "HMAC_DRBG SHA1/128/440 (COUNT=0, E=16, N=8, A=16)\n";
159 }
160
161 {
162 // [SHA-1], [PredictionResistance = False], [EntropyInputLen = 128], [NonceLen = 64]
163 // [PersonalizationStringLen = 0], [AdditionalInputLen = 16], [ReturnedBitsLen = 640]
164 const byte entropy1[] = "\x29\xc6\x2a\xfa\x3c\x52\x20\x8a\x3f\xde\xcb\x43\xfa\x61\x3f\x15";
165 const byte entropy2[] = "\xbd\x87\xbe\x99\xd1\x84\x16\x54\x12\x31\x41\x40\xd4\x02\x71\x41";
166 const byte nonce[] = "\x6c\x9e\xb5\x9a\xc3\xc2\xd4\x8b";
167 const byte additional1[] = "\x43\x3d\xda\xf2\x59\xd1\x4b\xcf\x89\x76\x30\xcc\xaa\x27\x33\x8c";
168 const byte additional2[] = "\x14\x11\x46\xd4\x04\xf2\x84\xc2\xd0\x2b\x6a\x10\x15\x6e\x33\x82";
169 const byte additional3[] = "\xed\xc3\x43\xdb\xff\xe7\x1a\xb4\x11\x4a\xc3\x63\x9d\x44\x5b\x65";
170
171 HMAC_DRBG<SHA1, 128/8, 440/8> drbg(entropy1, 16, nonce, 8);
172 drbg.IncorporateEntropy(entropy2, 16, additional1, 16);
173
174 SecByteBlock result(80);
175 drbg.GenerateBlock(additional2, 16, result, result.size());
176 drbg.GenerateBlock(additional3, 16, result, result.size());
177
178 const byte expected[] = "\x8c\x73\x0f\x05\x26\x69\x4d\x5a\x9a\x45\xdb\xab\x05\x7a\x19\x75\x35\x7d\x65\xaf"
179 "\xd3\xef\xf3\x03\x32\x0b\xd1\x40\x61\xf9\xad\x38\x75\x91\x02\xb6\xc6\x01\x16\xf6"
180 "\xdb\x7a\x6e\x8e\x7a\xb9\x4c\x05\x50\x0b\x4d\x1e\x35\x7d\xf8\xe9\x57\xac\x89\x37"
181 "\xb0\x5f\xb3\xd0\x80\xa0\xf9\x06\x74\xd4\x4d\xe1\xbd\x6f\x94\xd2\x95\xc4\x51\x9d";
182
183 fail = !!memcmp(result, expected, 640/8);
184 pass = !fail && pass;
185
186 std::cout << (fail ? "FAILED " : "passed ") << "HMAC_DRBG SHA1/128/440 (COUNT=1, E=16, N=8, A=16)\n";
187 }
188
189 return pass;
190 }
191
192 class CipherFactory
193 {
194 public:
195 virtual unsigned int BlockSize() const =0;
196 virtual unsigned int KeyLength() const =0;
197
198 virtual BlockTransformation* NewEncryption(const byte *keyStr) const =0;
199 virtual BlockTransformation* NewDecryption(const byte *keyStr) const =0;
200 };
201
202 template <class E, class D> class FixedRoundsCipherFactory : public CipherFactory
203 {
204 public:
FixedRoundsCipherFactory(unsigned int keylen=0)205 FixedRoundsCipherFactory(unsigned int keylen=0) :
206 m_keylen(keylen ? keylen : static_cast<unsigned int>(E::DEFAULT_KEYLENGTH)) {}
207
BlockSize() const208 unsigned int BlockSize() const {return E::BLOCKSIZE;}
KeyLength() const209 unsigned int KeyLength() const {return m_keylen;}
210
NewEncryption(const byte * keyStr) const211 BlockTransformation* NewEncryption(const byte *keyStr) const
212 {return new E(keyStr, m_keylen);}
NewDecryption(const byte * keyStr) const213 BlockTransformation* NewDecryption(const byte *keyStr) const
214 {return new D(keyStr, m_keylen);}
215
216 unsigned int m_keylen;
217 };
218
219 template <class E, class D> class VariableRoundsCipherFactory : public CipherFactory
220 {
221 public:
VariableRoundsCipherFactory(unsigned int keylen=0,unsigned int rounds=0)222 VariableRoundsCipherFactory(unsigned int keylen=0, unsigned int rounds=0) :
223 m_keylen(keylen ? keylen : static_cast<unsigned int>(E::DEFAULT_KEYLENGTH)),
224 m_rounds(rounds ? rounds : static_cast<unsigned int>(E::DEFAULT_ROUNDS)) {}
225
BlockSize() const226 unsigned int BlockSize() const {return static_cast<unsigned int>(E::BLOCKSIZE);}
KeyLength() const227 unsigned int KeyLength() const {return m_keylen;}
228
NewEncryption(const byte * keyStr) const229 BlockTransformation* NewEncryption(const byte *keyStr) const
230 {return new E(keyStr, m_keylen, m_rounds);}
NewDecryption(const byte * keyStr) const231 BlockTransformation* NewDecryption(const byte *keyStr) const
232 {return new D(keyStr, m_keylen, m_rounds);}
233
234 unsigned int m_keylen, m_rounds;
235 };
236
BlockTransformationTest(const CipherFactory & cg,BufferedTransformation & valdata,unsigned int tuples=0xffff)237 bool BlockTransformationTest(const CipherFactory &cg, BufferedTransformation &valdata, unsigned int tuples = 0xffff)
238 {
239 HexEncoder output(new FileSink(std::cout));
240 SecByteBlock plain(cg.BlockSize()), cipher(cg.BlockSize()), out(cg.BlockSize()), outplain(cg.BlockSize());
241 SecByteBlock key(cg.KeyLength());
242 bool pass=true, fail;
243
244 while (valdata.MaxRetrievable() && tuples--)
245 {
246 (void)valdata.Get(key, cg.KeyLength());
247 (void)valdata.Get(plain, cg.BlockSize());
248 (void)valdata.Get(cipher, cg.BlockSize());
249
250 member_ptr<BlockTransformation> transE(cg.NewEncryption(key));
251 transE->ProcessBlock(plain, out);
252 fail = memcmp(out, cipher, cg.BlockSize()) != 0;
253
254 member_ptr<BlockTransformation> transD(cg.NewDecryption(key));
255 transD->ProcessBlock(out, outplain);
256 fail=fail || memcmp(outplain, plain, cg.BlockSize());
257
258 pass = pass && !fail;
259
260 std::cout << (fail ? "FAILED " : "passed ");
261 output.Put(key, cg.KeyLength());
262 std::cout << " ";
263 output.Put(outplain, cg.BlockSize());
264 std::cout << " ";
265 output.Put(out, cg.BlockSize());
266 std::cout << std::endl;
267 }
268 return pass;
269 }
270
271 class FilterTester : public Unflushable<Sink>
272 {
273 public:
FilterTester(const byte * validOutput,size_t outputLen)274 FilterTester(const byte *validOutput, size_t outputLen)
275 : validOutput(validOutput), outputLen(outputLen), counter(0), fail(false) {}
PutByte(byte inByte)276 void PutByte(byte inByte)
277 {
278 if (counter >= outputLen || validOutput[counter] != inByte)
279 {
280 std::cerr << "incorrect output " << counter << ", " << (word16)validOutput[counter] << ", " << (word16)inByte << "\n";
281 fail = true;
282 CRYPTOPP_ASSERT(false);
283 }
284 counter++;
285 }
Put2(const byte * inString,size_t length,int messageEnd,bool blocking)286 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
287 {
288 CRYPTOPP_UNUSED(messageEnd), CRYPTOPP_UNUSED(blocking);
289
290 while (length--)
291 FilterTester::PutByte(*inString++);
292
293 if (messageEnd)
294 if (counter != outputLen)
295 {
296 fail = true;
297 CRYPTOPP_ASSERT(false);
298 }
299
300 return 0;
301 }
GetResult()302 bool GetResult()
303 {
304 return !fail;
305 }
306
307 const byte *validOutput;
308 size_t outputLen, counter;
309 bool fail;
310 };
311
TestFilter(BufferedTransformation & bt,const byte * in,size_t inLen,const byte * out,size_t outLen)312 bool TestFilter(BufferedTransformation &bt, const byte *in, size_t inLen, const byte *out, size_t outLen)
313 {
314 FilterTester *ft;
315 bt.Attach(ft = new FilterTester(out, outLen));
316
317 while (inLen)
318 {
319 size_t randomLen = GlobalRNG().GenerateWord32(0, (word32)inLen);
320 bt.Put(in, randomLen);
321 in += randomLen;
322 inLen -= randomLen;
323 }
324 bt.MessageEnd();
325 return ft->GetResult();
326 }
327
ValidateDES()328 bool ValidateDES()
329 {
330 std::cout << "\nDES validation suite running...\n\n";
331
332 FileSource valdata(DataDir("TestData/descert.dat").c_str(), true, new HexDecoder);
333 bool pass = BlockTransformationTest(FixedRoundsCipherFactory<DESEncryption, DESDecryption>(), valdata);
334
335 std::cout << "\nTesting EDE2, EDE3, and XEX3 variants...\n\n";
336
337 FileSource valdata1(DataDir("TestData/3desval.dat").c_str(), true, new HexDecoder);
338 pass = BlockTransformationTest(FixedRoundsCipherFactory<DES_EDE2_Encryption, DES_EDE2_Decryption>(), valdata1, 1) && pass;
339 pass = BlockTransformationTest(FixedRoundsCipherFactory<DES_EDE3_Encryption, DES_EDE3_Decryption>(), valdata1, 1) && pass;
340 pass = BlockTransformationTest(FixedRoundsCipherFactory<DES_XEX3_Encryption, DES_XEX3_Decryption>(), valdata1, 1) && pass;
341
342 return pass;
343 }
344
TestModeIV(SymmetricCipher & e,SymmetricCipher & d)345 bool TestModeIV(SymmetricCipher &e, SymmetricCipher &d)
346 {
347 SecByteBlock lastIV, iv(e.IVSize());
348 StreamTransformationFilter filter(e, new StreamTransformationFilter(d));
349
350 // Enterprise Analysis finding on the stack based array
351 const int BUF_SIZE=20480U;
352 AlignedSecByteBlock plaintext(BUF_SIZE);
353
354 for (unsigned int i=1; i<BUF_SIZE; i*=2)
355 {
356 e.GetNextIV(GlobalRNG(), iv);
357 if (iv == lastIV)
358 return false;
359 else
360 lastIV = iv;
361
362 e.Resynchronize(iv);
363 d.Resynchronize(iv);
364
365 unsigned int length = STDMAX(GlobalRNG().GenerateWord32(0, i), (word32)e.MinLastBlockSize());
366 GlobalRNG().GenerateBlock(plaintext, length);
367
368 if (!TestFilter(filter, plaintext, length, plaintext, length))
369 return false;
370 }
371
372 return true;
373 }
374
ValidateCipherModes()375 bool ValidateCipherModes()
376 {
377 std::cout << "\nTesting DES modes...\n\n";
378 const byte key[] = {0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef};
379 const byte iv[] = {0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef};
380 const byte plain[] = { // "Now is the time for all " without tailing 0
381 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
382 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
383 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20};
384 DESEncryption desE(key);
385 DESDecryption desD(key);
386 bool pass=true, fail;
387
388 {
389 // from FIPS 81
390 const byte encrypted[] = {
391 0x3f, 0xa4, 0x0e, 0x8a, 0x98, 0x4d, 0x48, 0x15,
392 0x6a, 0x27, 0x17, 0x87, 0xab, 0x88, 0x83, 0xf9,
393 0x89, 0x3d, 0x51, 0xec, 0x4b, 0x56, 0x3b, 0x53};
394
395 ECB_Mode_ExternalCipher::Encryption modeE(desE);
396 fail = !TestFilter(StreamTransformationFilter(modeE, NULLPTR, StreamTransformationFilter::NO_PADDING).Ref(),
397 plain, sizeof(plain), encrypted, sizeof(encrypted));
398 pass = pass && !fail;
399 std::cout << (fail ? "FAILED " : "passed ") << "ECB encryption" << std::endl;
400
401 ECB_Mode_ExternalCipher::Decryption modeD(desD);
402 fail = !TestFilter(StreamTransformationFilter(modeD, NULLPTR, StreamTransformationFilter::NO_PADDING).Ref(),
403 encrypted, sizeof(encrypted), plain, sizeof(plain));
404 pass = pass && !fail;
405 std::cout << (fail ? "FAILED " : "passed ") << "ECB decryption" << std::endl;
406 }
407 {
408 // from FIPS 81
409 const byte encrypted[] = {
410 0xE5, 0xC7, 0xCD, 0xDE, 0x87, 0x2B, 0xF2, 0x7C,
411 0x43, 0xE9, 0x34, 0x00, 0x8C, 0x38, 0x9C, 0x0F,
412 0x68, 0x37, 0x88, 0x49, 0x9A, 0x7C, 0x05, 0xF6};
413
414 CBC_Mode_ExternalCipher::Encryption modeE(desE, iv);
415 fail = !TestFilter(StreamTransformationFilter(modeE, NULLPTR, StreamTransformationFilter::NO_PADDING).Ref(),
416 plain, sizeof(plain), encrypted, sizeof(encrypted));
417 pass = pass && !fail;
418 std::cout << (fail ? "FAILED " : "passed ") << "CBC encryption with no padding" << std::endl;
419
420 CBC_Mode_ExternalCipher::Decryption modeD(desD, iv);
421 fail = !TestFilter(StreamTransformationFilter(modeD, NULLPTR, StreamTransformationFilter::NO_PADDING).Ref(),
422 encrypted, sizeof(encrypted), plain, sizeof(plain));
423 pass = pass && !fail;
424 std::cout << (fail ? "FAILED " : "passed ") << "CBC decryption with no padding" << std::endl;
425
426 fail = !TestModeIV(modeE, modeD);
427 pass = pass && !fail;
428 std::cout << (fail ? "FAILED " : "passed ") << "CBC mode IV generation" << std::endl;
429 }
430 {
431 // generated with Crypto++, matches FIPS 81
432 // but has extra 8 bytes as result of padding
433 const byte encrypted[] = {
434 0xE5, 0xC7, 0xCD, 0xDE, 0x87, 0x2B, 0xF2, 0x7C,
435 0x43, 0xE9, 0x34, 0x00, 0x8C, 0x38, 0x9C, 0x0F,
436 0x68, 0x37, 0x88, 0x49, 0x9A, 0x7C, 0x05, 0xF6,
437 0x62, 0xC1, 0x6A, 0x27, 0xE4, 0xFC, 0xF2, 0x77};
438
439 CBC_Mode_ExternalCipher::Encryption modeE(desE, iv);
440 fail = !TestFilter(StreamTransformationFilter(modeE).Ref(),
441 plain, sizeof(plain), encrypted, sizeof(encrypted));
442 pass = pass && !fail;
443 std::cout << (fail ? "FAILED " : "passed ") << "CBC encryption with PKCS #7 padding" << std::endl;
444
445 CBC_Mode_ExternalCipher::Decryption modeD(desD, iv);
446 fail = !TestFilter(StreamTransformationFilter(modeD).Ref(),
447 encrypted, sizeof(encrypted), plain, sizeof(plain));
448 pass = pass && !fail;
449 std::cout << (fail ? "FAILED " : "passed ") << "CBC decryption with PKCS #7 padding" << std::endl;
450 }
451 {
452 // generated with Crypto++ 5.2, matches FIPS 81
453 // but has extra 8 bytes as result of padding
454 const byte encrypted[] = {
455 0xE5, 0xC7, 0xCD, 0xDE, 0x87, 0x2B, 0xF2, 0x7C,
456 0x43, 0xE9, 0x34, 0x00, 0x8C, 0x38, 0x9C, 0x0F,
457 0x68, 0x37, 0x88, 0x49, 0x9A, 0x7C, 0x05, 0xF6,
458 0xcf, 0xb7, 0xc7, 0x64, 0x0e, 0x7c, 0xd9, 0xa7};
459
460 CBC_Mode_ExternalCipher::Encryption modeE(desE, iv);
461 fail = !TestFilter(StreamTransformationFilter(modeE, NULLPTR, StreamTransformationFilter::ONE_AND_ZEROS_PADDING).Ref(),
462 plain, sizeof(plain), encrypted, sizeof(encrypted));
463 pass = pass && !fail;
464 std::cout << (fail ? "FAILED " : "passed ") << "CBC encryption with one-and-zeros padding" << std::endl;
465
466 CBC_Mode_ExternalCipher::Decryption modeD(desD, iv);
467 fail = !TestFilter(StreamTransformationFilter(modeD, NULLPTR, StreamTransformationFilter::ONE_AND_ZEROS_PADDING).Ref(),
468 encrypted, sizeof(encrypted), plain, sizeof(plain));
469 pass = pass && !fail;
470 std::cout << (fail ? "FAILED " : "passed ") << "CBC decryption with one-and-zeros padding" << std::endl;
471 }
472 {
473 const byte plain_1[] = {'a', 0, 0, 0, 0, 0, 0, 0};
474 // generated with Crypto++
475 const byte encrypted[] = {
476 0x9B, 0x47, 0x57, 0x59, 0xD6, 0x9C, 0xF6, 0xD0};
477
478 CBC_Mode_ExternalCipher::Encryption modeE(desE, iv);
479 fail = !TestFilter(StreamTransformationFilter(modeE, NULLPTR, StreamTransformationFilter::ZEROS_PADDING).Ref(),
480 plain_1, 1, encrypted, sizeof(encrypted));
481 pass = pass && !fail;
482 std::cout << (fail ? "FAILED " : "passed ") << "CBC encryption with zeros padding" << std::endl;
483
484 CBC_Mode_ExternalCipher::Decryption modeD(desD, iv);
485 fail = !TestFilter(StreamTransformationFilter(modeD, NULLPTR, StreamTransformationFilter::ZEROS_PADDING).Ref(),
486 encrypted, sizeof(encrypted), plain_1, sizeof(plain_1));
487 pass = pass && !fail;
488 std::cout << (fail ? "FAILED " : "passed ") << "CBC decryption with zeros padding" << std::endl;
489 }
490 {
491 // generated with Crypto++, matches FIPS 81
492 // but with last two blocks swapped as result of CTS
493 const byte encrypted[] = {
494 0xE5, 0xC7, 0xCD, 0xDE, 0x87, 0x2B, 0xF2, 0x7C,
495 0x68, 0x37, 0x88, 0x49, 0x9A, 0x7C, 0x05, 0xF6,
496 0x43, 0xE9, 0x34, 0x00, 0x8C, 0x38, 0x9C, 0x0F};
497
498 CBC_CTS_Mode_ExternalCipher::Encryption modeE(desE, iv);
499 fail = !TestFilter(StreamTransformationFilter(modeE).Ref(),
500 plain, sizeof(plain), encrypted, sizeof(encrypted));
501 pass = pass && !fail;
502 std::cout << (fail ? "FAILED " : "passed ") << "CBC encryption with ciphertext stealing (CTS)" << std::endl;
503
504 CBC_CTS_Mode_ExternalCipher::Decryption modeD(desD, iv);
505 fail = !TestFilter(StreamTransformationFilter(modeD).Ref(),
506 encrypted, sizeof(encrypted), plain, sizeof(plain));
507 pass = pass && !fail;
508 std::cout << (fail ? "FAILED " : "passed ") << "CBC decryption with ciphertext stealing (CTS)" << std::endl;
509
510 fail = !TestModeIV(modeE, modeD);
511 pass = pass && !fail;
512 std::cout << (fail ? "FAILED " : "passed ") << "CBC CTS IV generation" << std::endl;
513 }
514 {
515 // generated with Crypto++
516 const byte decryptionIV[] = {0x4D, 0xD0, 0xAC, 0x8F, 0x47, 0xCF, 0x79, 0xCE};
517 const byte encrypted[] = {0x12, 0x34, 0x56};
518
519 byte stolenIV[8];
520
521 CBC_CTS_Mode_ExternalCipher::Encryption modeE(desE, iv);
522 modeE.SetStolenIV(stolenIV);
523 fail = !TestFilter(StreamTransformationFilter(modeE).Ref(),
524 plain, 3, encrypted, sizeof(encrypted));
525 fail = memcmp(stolenIV, decryptionIV, 8) != 0 || fail;
526 pass = pass && !fail;
527 std::cout << (fail ? "FAILED " : "passed ") << "CBC encryption with ciphertext and IV stealing" << std::endl;
528
529 CBC_CTS_Mode_ExternalCipher::Decryption modeD(desD, stolenIV);
530 fail = !TestFilter(StreamTransformationFilter(modeD).Ref(),
531 encrypted, sizeof(encrypted), plain, 3);
532 pass = pass && !fail;
533 std::cout << (fail ? "FAILED " : "passed ") << "CBC decryption with ciphertext and IV stealing" << std::endl;
534 }
535 {
536 const byte encrypted[] = { // from FIPS 81
537 0xF3,0x09,0x62,0x49,0xC7,0xF4,0x6E,0x51,
538 0xA6,0x9E,0x83,0x9B,0x1A,0x92,0xF7,0x84,
539 0x03,0x46,0x71,0x33,0x89,0x8E,0xA6,0x22};
540
541 CFB_Mode_ExternalCipher::Encryption modeE(desE, iv);
542 fail = !TestFilter(StreamTransformationFilter(modeE).Ref(),
543 plain, sizeof(plain), encrypted, sizeof(encrypted));
544 pass = pass && !fail;
545 std::cout << (fail ? "FAILED " : "passed ") << "CFB encryption" << std::endl;
546
547 CFB_Mode_ExternalCipher::Decryption modeD(desE, iv);
548 fail = !TestFilter(StreamTransformationFilter(modeD).Ref(),
549 encrypted, sizeof(encrypted), plain, sizeof(plain));
550 pass = pass && !fail;
551 std::cout << (fail ? "FAILED " : "passed ") << "CFB decryption" << std::endl;
552
553 fail = !TestModeIV(modeE, modeD);
554 pass = pass && !fail;
555 std::cout << (fail ? "FAILED " : "passed ") << "CFB mode IV generation" << std::endl;
556 }
557 {
558 const byte plain_2[] = { // "Now is the." without tailing 0
559 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,0x68,0x65};
560 const byte encrypted[] = { // from FIPS 81
561 0xf3,0x1f,0xda,0x07,0x01,0x14,0x62,0xee,0x18,0x7f};
562
563 CFB_Mode_ExternalCipher::Encryption modeE(desE, iv, 1);
564 fail = !TestFilter(StreamTransformationFilter(modeE).Ref(),
565 plain_2, sizeof(plain_2), encrypted, sizeof(encrypted));
566 pass = pass && !fail;
567 std::cout << (fail ? "FAILED " : "passed ") << "CFB (8-bit feedback) encryption" << std::endl;
568
569 CFB_Mode_ExternalCipher::Decryption modeD(desE, iv, 1);
570 fail = !TestFilter(StreamTransformationFilter(modeD).Ref(),
571 encrypted, sizeof(encrypted), plain_2, sizeof(plain_2));
572 pass = pass && !fail;
573 std::cout << (fail ? "FAILED " : "passed ") << "CFB (8-bit feedback) decryption" << std::endl;
574
575 fail = !TestModeIV(modeE, modeD);
576 pass = pass && !fail;
577 std::cout << (fail ? "FAILED " : "passed ") << "CFB (8-bit feedback) IV generation" << std::endl;
578 }
579 {
580 const byte encrypted[] = { // from Eric Young's libdes
581 0xf3,0x09,0x62,0x49,0xc7,0xf4,0x6e,0x51,
582 0x35,0xf2,0x4a,0x24,0x2e,0xeb,0x3d,0x3f,
583 0x3d,0x6d,0x5b,0xe3,0x25,0x5a,0xf8,0xc3};
584
585 OFB_Mode_ExternalCipher::Encryption modeE(desE, iv);
586 fail = !TestFilter(StreamTransformationFilter(modeE).Ref(),
587 plain, sizeof(plain), encrypted, sizeof(encrypted));
588 pass = pass && !fail;
589 std::cout << (fail ? "FAILED " : "passed ") << "OFB encryption" << std::endl;
590
591 OFB_Mode_ExternalCipher::Decryption modeD(desE, iv);
592 fail = !TestFilter(StreamTransformationFilter(modeD).Ref(),
593 encrypted, sizeof(encrypted), plain, sizeof(plain));
594 pass = pass && !fail;
595 std::cout << (fail ? "FAILED " : "passed ") << "OFB decryption" << std::endl;
596
597 fail = !TestModeIV(modeE, modeD);
598 pass = pass && !fail;
599 std::cout << (fail ? "FAILED " : "passed ") << "OFB IV generation" << std::endl;
600 }
601 {
602 const byte encrypted[] = { // generated with Crypto++
603 0xF3, 0x09, 0x62, 0x49, 0xC7, 0xF4, 0x6E, 0x51,
604 0x16, 0x3A, 0x8C, 0xA0, 0xFF, 0xC9, 0x4C, 0x27,
605 0xFA, 0x2F, 0x80, 0xF4, 0x80, 0xB8, 0x6F, 0x75};
606
607 CTR_Mode_ExternalCipher::Encryption modeE(desE, iv);
608 fail = !TestFilter(StreamTransformationFilter(modeE).Ref(),
609 plain, sizeof(plain), encrypted, sizeof(encrypted));
610 pass = pass && !fail;
611 std::cout << (fail ? "FAILED " : "passed ") << "Counter Mode encryption" << std::endl;
612
613 CTR_Mode_ExternalCipher::Decryption modeD(desE, iv);
614 fail = !TestFilter(StreamTransformationFilter(modeD).Ref(),
615 encrypted, sizeof(encrypted), plain, sizeof(plain));
616 pass = pass && !fail;
617 std::cout << (fail ? "FAILED " : "passed ") << "Counter Mode decryption" << std::endl;
618
619 fail = !TestModeIV(modeE, modeD);
620 pass = pass && !fail;
621 std::cout << (fail ? "FAILED " : "passed ") << "Counter Mode IV generation" << std::endl;
622 }
623 {
624 const byte plain_3[] = { // "7654321 Now is the time for "
625 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x20,
626 0x4e, 0x6f, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74,
627 0x68, 0x65, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x20,
628 0x66, 0x6f, 0x72, 0x20};
629 const byte mac1[] = { // from FIPS 113
630 0xf1, 0xd3, 0x0f, 0x68, 0x49, 0x31, 0x2c, 0xa4};
631 const byte mac2[] = { // generated with Crypto++
632 0x35, 0x80, 0xC5, 0xC4, 0x6B, 0x81, 0x24, 0xE2};
633
634 CBC_MAC<DES> cbcmac(key);
635 HashFilter cbcmacFilter(cbcmac);
636 fail = !TestFilter(cbcmacFilter, plain_3, sizeof(plain_3), mac1, sizeof(mac1));
637 pass = pass && !fail;
638 std::cout << (fail ? "FAILED " : "passed ") << "CBC MAC" << std::endl;
639
640 DMAC<DES> dmac(key);
641 HashFilter dmacFilter(dmac);
642 fail = !TestFilter(dmacFilter, plain_3, sizeof(plain_3), mac2, sizeof(mac2));
643 pass = pass && !fail;
644 std::cout << (fail ? "FAILED " : "passed ") << "DMAC" << std::endl;
645 }
646
647 return pass;
648 }
649
ValidateIDEA()650 bool ValidateIDEA()
651 {
652 std::cout << "\nIDEA validation suite running...\n\n";
653
654 FileSource valdata(DataDir("TestData/ideaval.dat").c_str(), true, new HexDecoder);
655 return BlockTransformationTest(FixedRoundsCipherFactory<IDEAEncryption, IDEADecryption>(), valdata);
656 }
657
ValidateSAFER()658 bool ValidateSAFER()
659 {
660 std::cout << "\nSAFER validation suite running...\n\n";
661
662 FileSource valdata(DataDir("TestData/saferval.dat").c_str(), true, new HexDecoder);
663 bool pass = true;
664 pass = BlockTransformationTest(VariableRoundsCipherFactory<SAFER_K_Encryption, SAFER_K_Decryption>(8,6), valdata, 4) && pass;
665 pass = BlockTransformationTest(VariableRoundsCipherFactory<SAFER_K_Encryption, SAFER_K_Decryption>(16,12), valdata, 4) && pass;
666 pass = BlockTransformationTest(VariableRoundsCipherFactory<SAFER_SK_Encryption, SAFER_SK_Decryption>(8,6), valdata, 4) && pass;
667 pass = BlockTransformationTest(VariableRoundsCipherFactory<SAFER_SK_Encryption, SAFER_SK_Decryption>(16,10), valdata, 4) && pass;
668 return pass;
669 }
670
ValidateRC2()671 bool ValidateRC2()
672 {
673 std::cout << "\nRC2 validation suite running...\n\n";
674
675 FileSource valdata(DataDir("TestData/rc2val.dat").c_str(), true, new HexDecoder);
676 HexEncoder output(new FileSink(std::cout));
677 SecByteBlock plain(RC2Encryption::BLOCKSIZE), cipher(RC2Encryption::BLOCKSIZE), out(RC2Encryption::BLOCKSIZE), outplain(RC2Encryption::BLOCKSIZE);
678 SecByteBlock key(128);
679 bool pass=true, fail;
680
681 while (valdata.MaxRetrievable())
682 {
683 byte keyLen, effectiveLen;
684
685 (void)valdata.Get(keyLen);
686 (void)valdata.Get(effectiveLen);
687 (void)valdata.Get(key, keyLen);
688 (void)valdata.Get(plain, RC2Encryption::BLOCKSIZE);
689 (void)valdata.Get(cipher, RC2Encryption::BLOCKSIZE);
690
691 member_ptr<BlockTransformation> transE(new RC2Encryption(key, keyLen, effectiveLen));
692 transE->ProcessBlock(plain, out);
693 fail = memcmp(out, cipher, RC2Encryption::BLOCKSIZE) != 0;
694
695 member_ptr<BlockTransformation> transD(new RC2Decryption(key, keyLen, effectiveLen));
696 transD->ProcessBlock(out, outplain);
697 fail=fail || memcmp(outplain, plain, RC2Encryption::BLOCKSIZE);
698
699 pass = pass && !fail;
700
701 std::cout << (fail ? "FAILED " : "passed ");
702 output.Put(key, keyLen);
703 std::cout << " ";
704 output.Put(outplain, RC2Encryption::BLOCKSIZE);
705 std::cout << " ";
706 output.Put(out, RC2Encryption::BLOCKSIZE);
707 std::cout << std::endl;
708 }
709 return pass;
710 }
711
ValidateARC4()712 bool ValidateARC4()
713 {
714 unsigned char Key0[] = {0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef };
715 unsigned char Input0[]={0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef};
716 unsigned char Output0[] = {0x75,0xb7,0x87,0x80,0x99,0xe0,0xc5,0x96};
717
718 unsigned char Key1[]={0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef};
719 unsigned char Input1[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
720 unsigned char Output1[]={0x74,0x94,0xc2,0xe7,0x10,0x4b,0x08,0x79};
721
722 unsigned char Key2[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
723 unsigned char Input2[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
724 unsigned char Output2[]={0xde,0x18,0x89,0x41,0xa3,0x37,0x5d,0x3a};
725
726 unsigned char Key3[]={0xef,0x01,0x23,0x45};
727 unsigned char Input3[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
728 unsigned char Output3[]={0xd6,0xa1,0x41,0xa7,0xec,0x3c,0x38,0xdf,0xbd,0x61};
729
730 unsigned char Key4[]={ 0x01,0x23,0x45,0x67,0x89,0xab, 0xcd,0xef };
731 unsigned char Input4[] =
732 {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
733 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
734 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
735 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
736 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
737 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
738 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
739 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
740 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
741 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
742 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
743 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
744 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
745 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
746 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
747 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
748 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
749 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
750 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
751 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
752 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
753 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
754 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
755 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
756 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
757 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
758 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
759 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
760 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
761 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
762 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
763 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
764 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
765 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
766 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
767 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
768 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
769 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
770 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
771 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
772 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
773 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
774 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
775 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
776 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
777 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
778 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
779 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
780 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
781 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
782 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
783 0x01};
784 unsigned char Output4[]= {
785 0x75,0x95,0xc3,0xe6,0x11,0x4a,0x09,0x78,0x0c,0x4a,0xd4,
786 0x52,0x33,0x8e,0x1f,0xfd,0x9a,0x1b,0xe9,0x49,0x8f,
787 0x81,0x3d,0x76,0x53,0x34,0x49,0xb6,0x77,0x8d,0xca,
788 0xd8,0xc7,0x8a,0x8d,0x2b,0xa9,0xac,0x66,0x08,0x5d,
789 0x0e,0x53,0xd5,0x9c,0x26,0xc2,0xd1,0xc4,0x90,0xc1,
790 0xeb,0xbe,0x0c,0xe6,0x6d,0x1b,0x6b,0x1b,0x13,0xb6,
791 0xb9,0x19,0xb8,0x47,0xc2,0x5a,0x91,0x44,0x7a,0x95,
792 0xe7,0x5e,0x4e,0xf1,0x67,0x79,0xcd,0xe8,0xbf,0x0a,
793 0x95,0x85,0x0e,0x32,0xaf,0x96,0x89,0x44,0x4f,0xd3,
794 0x77,0x10,0x8f,0x98,0xfd,0xcb,0xd4,0xe7,0x26,0x56,
795 0x75,0x00,0x99,0x0b,0xcc,0x7e,0x0c,0xa3,0xc4,0xaa,
796 0xa3,0x04,0xa3,0x87,0xd2,0x0f,0x3b,0x8f,0xbb,0xcd,
797 0x42,0xa1,0xbd,0x31,0x1d,0x7a,0x43,0x03,0xdd,0xa5,
798 0xab,0x07,0x88,0x96,0xae,0x80,0xc1,0x8b,0x0a,0xf6,
799 0x6d,0xff,0x31,0x96,0x16,0xeb,0x78,0x4e,0x49,0x5a,
800 0xd2,0xce,0x90,0xd7,0xf7,0x72,0xa8,0x17,0x47,0xb6,
801 0x5f,0x62,0x09,0x3b,0x1e,0x0d,0xb9,0xe5,0xba,0x53,
802 0x2f,0xaf,0xec,0x47,0x50,0x83,0x23,0xe6,0x71,0x32,
803 0x7d,0xf9,0x44,0x44,0x32,0xcb,0x73,0x67,0xce,0xc8,
804 0x2f,0x5d,0x44,0xc0,0xd0,0x0b,0x67,0xd6,0x50,0xa0,
805 0x75,0xcd,0x4b,0x70,0xde,0xdd,0x77,0xeb,0x9b,0x10,
806 0x23,0x1b,0x6b,0x5b,0x74,0x13,0x47,0x39,0x6d,0x62,
807 0x89,0x74,0x21,0xd4,0x3d,0xf9,0xb4,0x2e,0x44,0x6e,
808 0x35,0x8e,0x9c,0x11,0xa9,0xb2,0x18,0x4e,0xcb,0xef,
809 0x0c,0xd8,0xe7,0xa8,0x77,0xef,0x96,0x8f,0x13,0x90,
810 0xec,0x9b,0x3d,0x35,0xa5,0x58,0x5c,0xb0,0x09,0x29,
811 0x0e,0x2f,0xcd,0xe7,0xb5,0xec,0x66,0xd9,0x08,0x4b,
812 0xe4,0x40,0x55,0xa6,0x19,0xd9,0xdd,0x7f,0xc3,0x16,
813 0x6f,0x94,0x87,0xf7,0xcb,0x27,0x29,0x12,0x42,0x64,
814 0x45,0x99,0x85,0x14,0xc1,0x5d,0x53,0xa1,0x8c,0x86,
815 0x4c,0xe3,0xa2,0xb7,0x55,0x57,0x93,0x98,0x81,0x26,
816 0x52,0x0e,0xac,0xf2,0xe3,0x06,0x6e,0x23,0x0c,0x91,
817 0xbe,0xe4,0xdd,0x53,0x04,0xf5,0xfd,0x04,0x05,0xb3,
818 0x5b,0xd9,0x9c,0x73,0x13,0x5d,0x3d,0x9b,0xc3,0x35,
819 0xee,0x04,0x9e,0xf6,0x9b,0x38,0x67,0xbf,0x2d,0x7b,
820 0xd1,0xea,0xa5,0x95,0xd8,0xbf,0xc0,0x06,0x6f,0xf8,
821 0xd3,0x15,0x09,0xeb,0x0c,0x6c,0xaa,0x00,0x6c,0x80,
822 0x7a,0x62,0x3e,0xf8,0x4c,0x3d,0x33,0xc1,0x95,0xd2,
823 0x3e,0xe3,0x20,0xc4,0x0d,0xe0,0x55,0x81,0x57,0xc8,
824 0x22,0xd4,0xb8,0xc5,0x69,0xd8,0x49,0xae,0xd5,0x9d,
825 0x4e,0x0f,0xd7,0xf3,0x79,0x58,0x6b,0x4b,0x7f,0xf6,
826 0x84,0xed,0x6a,0x18,0x9f,0x74,0x86,0xd4,0x9b,0x9c,
827 0x4b,0xad,0x9b,0xa2,0x4b,0x96,0xab,0xf9,0x24,0x37,
828 0x2c,0x8a,0x8f,0xff,0xb1,0x0d,0x55,0x35,0x49,0x00,
829 0xa7,0x7a,0x3d,0xb5,0xf2,0x05,0xe1,0xb9,0x9f,0xcd,
830 0x86,0x60,0x86,0x3a,0x15,0x9a,0xd4,0xab,0xe4,0x0f,
831 0xa4,0x89,0x34,0x16,0x3d,0xdd,0xe5,0x42,0xa6,0x58,
832 0x55,0x40,0xfd,0x68,0x3c,0xbf,0xd8,0xc0,0x0f,0x12,
833 0x12,0x9a,0x28,0x4d,0xea,0xcc,0x4c,0xde,0xfe,0x58,
834 0xbe,0x71,0x37,0x54,0x1c,0x04,0x71,0x26,0xc8,0xd4,
835 0x9e,0x27,0x55,0xab,0x18,0x1a,0xb7,0xe9,0x40,0xb0,
836 0xc0};
837
838 member_ptr<Weak::ARC4> arc4;
839 bool pass=true, fail;
840 unsigned int i;
841
842 std::cout << "\nARC4 validation suite running...\n\n";
843
844 arc4.reset(new Weak::ARC4(Key0, sizeof(Key0)));
845 arc4->ProcessString(Input0, sizeof(Input0));
846 fail = memcmp(Input0, Output0, sizeof(Input0)) != 0;
847 std::cout << (fail ? "FAILED" : "passed") << " Test 0" << std::endl;
848 pass = pass && !fail;
849
850 arc4.reset(new Weak::ARC4(Key1, sizeof(Key1)));
851 arc4->ProcessString(Key1, Input1, sizeof(Key1));
852 fail = memcmp(Output1, Key1, sizeof(Key1)) != 0;
853 std::cout << (fail ? "FAILED" : "passed") << " Test 1" << std::endl;
854 pass = pass && !fail;
855
856 arc4.reset(new Weak::ARC4(Key2, sizeof(Key2)));
857 for (i=0, fail=false; i<sizeof(Input2); i++)
858 if (arc4->ProcessByte(Input2[i]) != Output2[i])
859 fail = true;
860 std::cout << (fail ? "FAILED" : "passed") << " Test 2" << std::endl;
861 pass = pass && !fail;
862
863 arc4.reset(new Weak::ARC4(Key3, sizeof(Key3)));
864 for (i=0, fail=false; i<sizeof(Input3); i++)
865 if (arc4->ProcessByte(Input3[i]) != Output3[i])
866 fail = true;
867 std::cout << (fail ? "FAILED" : "passed") << " Test 3" << std::endl;
868 pass = pass && !fail;
869
870 arc4.reset(new Weak::ARC4(Key4, sizeof(Key4)));
871 for (i=0, fail=false; i<sizeof(Input4); i++)
872 if (arc4->ProcessByte(Input4[i]) != Output4[i])
873 fail = true;
874 std::cout << (fail ? "FAILED" : "passed") << " Test 4" << std::endl;
875 pass = pass && !fail;
876
877 return pass;
878 }
879
ValidateRC5()880 bool ValidateRC5()
881 {
882 std::cout << "\nRC5 validation suite running...\n\n";
883 bool pass1 = true, pass2 = true;
884
885 RC5Encryption enc; // 0 to 2040-bits (255-bytes)
886 pass1 = RC5Encryption::DEFAULT_KEYLENGTH == 16 && pass1;
887 pass1 = enc.StaticGetValidKeyLength(0) == 0 && pass1;
888 pass1 = enc.StaticGetValidKeyLength(254) == 254 && pass1;
889 pass1 = enc.StaticGetValidKeyLength(255) == 255 && pass1;
890 pass1 = enc.StaticGetValidKeyLength(256) == 255 && pass1;
891 pass1 = enc.StaticGetValidKeyLength(0) == enc.MinKeyLength() && pass1;
892 pass1 = enc.StaticGetValidKeyLength(SIZE_MAX) == enc.MaxKeyLength() && pass1;
893
894 RC5Decryption dec;
895 pass2 = RC5Decryption::DEFAULT_KEYLENGTH == 16 && pass2;
896 pass2 = dec.StaticGetValidKeyLength(0) == 0 && pass2;
897 pass2 = dec.StaticGetValidKeyLength(254) == 254 && pass2;
898 pass2 = dec.StaticGetValidKeyLength(255) == 255 && pass2;
899 pass2 = dec.StaticGetValidKeyLength(256) == 255 && pass2;
900 pass2 = dec.StaticGetValidKeyLength(0) == dec.MinKeyLength() && pass2;
901 pass2 = dec.StaticGetValidKeyLength(SIZE_MAX) == dec.MaxKeyLength() && pass2;
902 std::cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n";
903
904 FileSource valdata(DataDir("TestData/rc5val.dat").c_str(), true, new HexDecoder);
905 return BlockTransformationTest(VariableRoundsCipherFactory<RC5Encryption, RC5Decryption>(16, 12), valdata) && pass1 && pass2;
906 }
907
ValidateRC6()908 bool ValidateRC6()
909 {
910 std::cout << "\nRC6 validation suite running...\n\n";
911 bool pass1 = true, pass2 = true, pass3 = true;
912
913 RC6Encryption enc;
914 pass1 = enc.StaticGetValidKeyLength(8) == 16 && pass1;
915 pass1 = enc.StaticGetValidKeyLength(16) == 16 && pass1;
916 pass1 = enc.StaticGetValidKeyLength(24) == 24 && pass1;
917 pass1 = enc.StaticGetValidKeyLength(32) == 32 && pass1;
918 pass1 = enc.StaticGetValidKeyLength(64) == 32 && pass1;
919 pass1 = enc.StaticGetValidKeyLength(128) == 32 && pass1;
920 pass1 = enc.StaticGetValidKeyLength(0) == enc.MinKeyLength() && pass1;
921 pass1 = enc.StaticGetValidKeyLength(SIZE_MAX) == enc.MaxKeyLength() && pass1;
922
923 RC6Decryption dec;
924 pass2 = dec.StaticGetValidKeyLength(8) == 16 && pass2;
925 pass2 = dec.StaticGetValidKeyLength(16) == 16 && pass2;
926 pass2 = dec.StaticGetValidKeyLength(24) == 24 && pass2;
927 pass2 = dec.StaticGetValidKeyLength(32) == 32 && pass2;
928 pass2 = dec.StaticGetValidKeyLength(64) == 32 && pass2;
929 pass2 = dec.StaticGetValidKeyLength(128) == 32 && pass2;
930 pass2 = dec.StaticGetValidKeyLength(0) == dec.MinKeyLength() && pass2;
931 pass2 = dec.StaticGetValidKeyLength(SIZE_MAX) == dec.MaxKeyLength() && pass2;
932 std::cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n";
933
934 FileSource valdata(DataDir("TestData/rc6val.dat").c_str(), true, new HexDecoder);
935 pass3 = BlockTransformationTest(FixedRoundsCipherFactory<RC6Encryption, RC6Decryption>(16), valdata, 2) && pass3;
936 pass3 = BlockTransformationTest(FixedRoundsCipherFactory<RC6Encryption, RC6Decryption>(24), valdata, 2) && pass3;
937 pass3 = BlockTransformationTest(FixedRoundsCipherFactory<RC6Encryption, RC6Decryption>(32), valdata, 2) && pass3;
938 return pass1 && pass2 && pass3;
939 }
940
ValidateMARS()941 bool ValidateMARS()
942 {
943 std::cout << "\nMARS validation suite running...\n\n";
944 bool pass1 = true, pass2 = true, pass3 = true;
945
946 MARSEncryption enc;
947 pass1 = enc.StaticGetValidKeyLength(8) == 16 && pass1;
948 pass1 = enc.StaticGetValidKeyLength(16) == 16 && pass1;
949 pass1 = enc.StaticGetValidKeyLength(24) == 24 && pass1;
950 pass1 = enc.StaticGetValidKeyLength(32) == 32 && pass1;
951 pass1 = enc.StaticGetValidKeyLength(64) == 56 && pass1;
952 pass1 = enc.StaticGetValidKeyLength(128) == 56 && pass1;
953 pass1 = enc.StaticGetValidKeyLength(0) == enc.MinKeyLength() && pass1;
954 pass1 = enc.StaticGetValidKeyLength(SIZE_MAX) == enc.MaxKeyLength() && pass1;
955
956 MARSDecryption dec;
957 pass2 = dec.StaticGetValidKeyLength(8) == 16 && pass2;
958 pass2 = dec.StaticGetValidKeyLength(16) == 16 && pass2;
959 pass2 = dec.StaticGetValidKeyLength(24) == 24 && pass2;
960 pass2 = dec.StaticGetValidKeyLength(32) == 32 && pass2;
961 pass2 = dec.StaticGetValidKeyLength(64) == 56 && pass2;
962 pass2 = dec.StaticGetValidKeyLength(128) == 56 && pass2;
963 pass2 = dec.StaticGetValidKeyLength(0) == dec.MinKeyLength() && pass2;
964 pass2 = dec.StaticGetValidKeyLength(SIZE_MAX) == dec.MaxKeyLength() && pass2;
965 std::cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n";
966
967 FileSource valdata(DataDir("TestData/marsval.dat").c_str(), true, new HexDecoder);
968 pass3 = BlockTransformationTest(FixedRoundsCipherFactory<MARSEncryption, MARSDecryption>(16), valdata, 4) && pass3;
969 pass3 = BlockTransformationTest(FixedRoundsCipherFactory<MARSEncryption, MARSDecryption>(24), valdata, 3) && pass3;
970 pass3 = BlockTransformationTest(FixedRoundsCipherFactory<MARSEncryption, MARSDecryption>(32), valdata, 2) && pass3;
971 return pass1 && pass2 && pass3;
972 }
973
ValidateRijndael()974 bool ValidateRijndael()
975 {
976 std::cout << "\nRijndael (AES) validation suite running...\n\n";
977 bool pass1 = true, pass2 = true, pass3 = true;
978
979 RijndaelEncryption enc;
980 pass1 = enc.StaticGetValidKeyLength(8) == 16 && pass1;
981 pass1 = enc.StaticGetValidKeyLength(16) == 16 && pass1;
982 pass1 = enc.StaticGetValidKeyLength(24) == 24 && pass1;
983 pass1 = enc.StaticGetValidKeyLength(32) == 32 && pass1;
984 pass1 = enc.StaticGetValidKeyLength(64) == 32 && pass1;
985 pass1 = enc.StaticGetValidKeyLength(128) == 32 && pass1;
986 pass1 = enc.StaticGetValidKeyLength(0) == enc.MinKeyLength() && pass1;
987 pass1 = enc.StaticGetValidKeyLength(SIZE_MAX) == enc.MaxKeyLength() && pass1;
988
989 RijndaelDecryption dec;
990 pass2 = dec.StaticGetValidKeyLength(8) == 16 && pass2;
991 pass2 = dec.StaticGetValidKeyLength(16) == 16 && pass2;
992 pass2 = dec.StaticGetValidKeyLength(24) == 24 && pass2;
993 pass2 = dec.StaticGetValidKeyLength(32) == 32 && pass2;
994 pass2 = dec.StaticGetValidKeyLength(64) == 32 && pass2;
995 pass2 = dec.StaticGetValidKeyLength(128) == 32 && pass2;
996 pass2 = dec.StaticGetValidKeyLength(0) == dec.MinKeyLength() && pass2;
997 pass2 = dec.StaticGetValidKeyLength(SIZE_MAX) == dec.MaxKeyLength() && pass2;
998 std::cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n";
999
1000 FileSource valdata(DataDir("TestData/rijndael.dat").c_str(), true, new HexDecoder);
1001 pass3 = BlockTransformationTest(FixedRoundsCipherFactory<RijndaelEncryption, RijndaelDecryption>(16), valdata, 4) && pass3;
1002 pass3 = BlockTransformationTest(FixedRoundsCipherFactory<RijndaelEncryption, RijndaelDecryption>(24), valdata, 3) && pass3;
1003 pass3 = BlockTransformationTest(FixedRoundsCipherFactory<RijndaelEncryption, RijndaelDecryption>(32), valdata, 2) && pass3;
1004 pass3 = RunTestDataFile("TestVectors/aes.txt") && pass3;
1005 return pass1 && pass2 && pass3;
1006 }
1007
ValidateTwofish()1008 bool ValidateTwofish()
1009 {
1010 std::cout << "\nTwofish validation suite running...\n\n";
1011 bool pass1 = true, pass2 = true, pass3 = true;
1012
1013 TwofishEncryption enc;
1014 pass1 = enc.StaticGetValidKeyLength(8) == 16 && pass1;
1015 pass1 = enc.StaticGetValidKeyLength(16) == 16 && pass1;
1016 pass1 = enc.StaticGetValidKeyLength(24) == 24 && pass1;
1017 pass1 = enc.StaticGetValidKeyLength(32) == 32 && pass1;
1018 pass1 = enc.StaticGetValidKeyLength(64) == 32 && pass1;
1019 pass1 = enc.StaticGetValidKeyLength(128) == 32 && pass1;
1020
1021 TwofishDecryption dec;
1022 pass2 = dec.StaticGetValidKeyLength(8) == 16 && pass2;
1023 pass2 = dec.StaticGetValidKeyLength(16) == 16 && pass2;
1024 pass2 = dec.StaticGetValidKeyLength(24) == 24 && pass2;
1025 pass2 = dec.StaticGetValidKeyLength(32) == 32 && pass2;
1026 pass2 = dec.StaticGetValidKeyLength(64) == 32 && pass2;
1027 pass2 = dec.StaticGetValidKeyLength(128) == 32 && pass2;
1028 std::cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n";
1029
1030 FileSource valdata(DataDir("TestData/twofishv.dat").c_str(), true, new HexDecoder);
1031 pass3 = BlockTransformationTest(FixedRoundsCipherFactory<TwofishEncryption, TwofishDecryption>(16), valdata, 4) && pass3;
1032 pass3 = BlockTransformationTest(FixedRoundsCipherFactory<TwofishEncryption, TwofishDecryption>(24), valdata, 3) && pass3;
1033 pass3 = BlockTransformationTest(FixedRoundsCipherFactory<TwofishEncryption, TwofishDecryption>(32), valdata, 2) && pass3;
1034 return pass1 && pass2 && pass3;
1035 }
1036
ValidateSerpent()1037 bool ValidateSerpent()
1038 {
1039 std::cout << "\nSerpent validation suite running...\n\n";
1040 bool pass1 = true, pass2 = true, pass3 = true;
1041
1042 SerpentEncryption enc;
1043 pass1 = enc.StaticGetValidKeyLength(8) == 16 && pass1;
1044 pass1 = enc.StaticGetValidKeyLength(16) == 16 && pass1;
1045 pass1 = enc.StaticGetValidKeyLength(24) == 24 && pass1;
1046 pass1 = enc.StaticGetValidKeyLength(32) == 32 && pass1;
1047 pass1 = enc.StaticGetValidKeyLength(64) == 32 && pass1;
1048 pass1 = enc.StaticGetValidKeyLength(128) == 32 && pass1;
1049
1050 SerpentDecryption dec;
1051 pass2 = dec.StaticGetValidKeyLength(8) == 16 && pass2;
1052 pass2 = dec.StaticGetValidKeyLength(16) == 16 && pass2;
1053 pass2 = dec.StaticGetValidKeyLength(24) == 24 && pass2;
1054 pass2 = dec.StaticGetValidKeyLength(32) == 32 && pass2;
1055 pass2 = dec.StaticGetValidKeyLength(64) == 32 && pass2;
1056 pass2 = dec.StaticGetValidKeyLength(128) == 32 && pass2;
1057 std::cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n";
1058
1059 FileSource valdata(DataDir("TestData/serpentv.dat").c_str(), true, new HexDecoder);
1060 pass3 = BlockTransformationTest(FixedRoundsCipherFactory<SerpentEncryption, SerpentDecryption>(16), valdata, 5) && pass3;
1061 pass3 = BlockTransformationTest(FixedRoundsCipherFactory<SerpentEncryption, SerpentDecryption>(24), valdata, 4) && pass3;
1062 pass3 = BlockTransformationTest(FixedRoundsCipherFactory<SerpentEncryption, SerpentDecryption>(32), valdata, 3) && pass3;
1063 return pass1 && pass2 && pass3;
1064 }
1065
ValidateBlowfish()1066 bool ValidateBlowfish()
1067 {
1068 std::cout << "\nBlowfish validation suite running...\n\n";
1069 bool pass1 = true, pass2 = true, pass3 = true, fail;
1070
1071 BlowfishEncryption enc1; // 32 to 448-bits (4 to 56-bytes)
1072 pass1 = enc1.StaticGetValidKeyLength(3) == 4 && pass1;
1073 pass1 = enc1.StaticGetValidKeyLength(4) == 4 && pass1;
1074 pass1 = enc1.StaticGetValidKeyLength(5) == 5 && pass1;
1075 pass1 = enc1.StaticGetValidKeyLength(8) == 8 && pass1;
1076 pass1 = enc1.StaticGetValidKeyLength(16) == 16 && pass1;
1077 pass1 = enc1.StaticGetValidKeyLength(24) == 24 && pass1;
1078 pass1 = enc1.StaticGetValidKeyLength(32) == 32 && pass1;
1079 pass1 = enc1.StaticGetValidKeyLength(56) == 56 && pass1;
1080 pass1 = enc1.StaticGetValidKeyLength(57) == 56 && pass1;
1081 pass1 = enc1.StaticGetValidKeyLength(60) == 56 && pass1;
1082 pass1 = enc1.StaticGetValidKeyLength(64) == 56 && pass1;
1083 pass1 = enc1.StaticGetValidKeyLength(128) == 56 && pass1;
1084
1085 BlowfishDecryption dec1; // 32 to 448-bits (4 to 56-bytes)
1086 pass2 = dec1.StaticGetValidKeyLength(3) == 4 && pass2;
1087 pass2 = dec1.StaticGetValidKeyLength(4) == 4 && pass2;
1088 pass2 = dec1.StaticGetValidKeyLength(5) == 5 && pass2;
1089 pass2 = dec1.StaticGetValidKeyLength(8) == 8 && pass2;
1090 pass2 = dec1.StaticGetValidKeyLength(16) == 16 && pass2;
1091 pass2 = dec1.StaticGetValidKeyLength(24) == 24 && pass2;
1092 pass2 = dec1.StaticGetValidKeyLength(32) == 32 && pass2;
1093 pass2 = dec1.StaticGetValidKeyLength(56) == 56 && pass2;
1094 pass2 = dec1.StaticGetValidKeyLength(57) == 56 && pass2;
1095 pass2 = dec1.StaticGetValidKeyLength(60) == 56 && pass2;
1096 pass2 = dec1.StaticGetValidKeyLength(64) == 56 && pass2;
1097 pass2 = dec1.StaticGetValidKeyLength(128) == 56 && pass2;
1098 std::cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n";
1099
1100 HexEncoder output(new FileSink(std::cout));
1101 const char *key[]={"abcdefghijklmnopqrstuvwxyz", "Who is John Galt?"};
1102 byte *plain[]={(byte *)"BLOWFISH", (byte *)"\xfe\xdc\xba\x98\x76\x54\x32\x10"};
1103 byte *cipher[]={(byte *)"\x32\x4e\xd0\xfe\xf4\x13\xa2\x03", (byte *)"\xcc\x91\x73\x2b\x80\x22\xf6\x84"};
1104 byte out[8], outplain[8];
1105
1106 for (int i=0; i<2; i++)
1107 {
1108 ECB_Mode<Blowfish>::Encryption enc2((byte *)key[i], strlen(key[i]));
1109 enc2.ProcessData(out, plain[i], 8);
1110 fail = memcmp(out, cipher[i], 8) != 0;
1111
1112 ECB_Mode<Blowfish>::Decryption dec2((byte *)key[i], strlen(key[i]));
1113 dec2.ProcessData(outplain, cipher[i], 8);
1114 fail = fail || memcmp(outplain, plain[i], 8);
1115 pass3 = pass3 && !fail;
1116
1117 std::cout << (fail ? "FAILED " : "passed ");
1118 std::cout << '\"' << key[i] << '\"';
1119 for (int j=0; j<(signed int)(30-strlen(key[i])); j++)
1120 std::cout << ' ';
1121 output.Put(outplain, 8);
1122 std::cout << " ";
1123 output.Put(out, 8);
1124 std::cout << std::endl;
1125 }
1126 return pass1 && pass2 && pass3;
1127 }
1128
ValidateThreeWay()1129 bool ValidateThreeWay()
1130 {
1131 std::cout << "\n3-WAY validation suite running...\n\n";
1132 bool pass1 = true, pass2 = true;
1133
1134 ThreeWayEncryption enc; // 96-bit only
1135 pass1 = ThreeWayEncryption::KEYLENGTH == 12 && pass1;
1136 pass1 = enc.StaticGetValidKeyLength(8) == 12 && pass1;
1137 pass1 = enc.StaticGetValidKeyLength(12) == 12 && pass1;
1138 pass1 = enc.StaticGetValidKeyLength(16) == 12 && pass1;
1139
1140 ThreeWayDecryption dec; // 96-bit only
1141 pass2 = ThreeWayDecryption::KEYLENGTH == 12 && pass2;
1142 pass2 = dec.StaticGetValidKeyLength(8) == 12 && pass2;
1143 pass2 = dec.StaticGetValidKeyLength(12) == 12 && pass2;
1144 pass2 = dec.StaticGetValidKeyLength(16) == 12 && pass2;
1145 std::cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n";
1146
1147 FileSource valdata(DataDir("TestData/3wayval.dat").c_str(), true, new HexDecoder);
1148 return BlockTransformationTest(FixedRoundsCipherFactory<ThreeWayEncryption, ThreeWayDecryption>(), valdata) && pass1 && pass2;
1149 }
1150
ValidateGOST()1151 bool ValidateGOST()
1152 {
1153 std::cout << "\nGOST validation suite running...\n\n";
1154 bool pass1 = true, pass2 = true;
1155
1156 GOSTEncryption enc; // 256-bit only
1157 pass1 = GOSTEncryption::KEYLENGTH == 32 && pass1;
1158 pass1 = enc.StaticGetValidKeyLength(16) == 32 && pass1;
1159 pass1 = enc.StaticGetValidKeyLength(24) == 32 && pass1;
1160 pass1 = enc.StaticGetValidKeyLength(32) == 32 && pass1;
1161 pass1 = enc.StaticGetValidKeyLength(40) == 32 && pass1;
1162 pass1 = enc.StaticGetValidKeyLength(64) == 32 && pass1;
1163
1164 GOSTDecryption dec; // 256-bit only
1165 pass2 = GOSTDecryption::KEYLENGTH == 32 && pass2;
1166 pass2 = dec.StaticGetValidKeyLength(16) == 32 && pass2;
1167 pass2 = dec.StaticGetValidKeyLength(24) == 32 && pass2;
1168 pass2 = dec.StaticGetValidKeyLength(32) == 32 && pass2;
1169 pass2 = dec.StaticGetValidKeyLength(40) == 32 && pass2;
1170 pass2 = dec.StaticGetValidKeyLength(64) == 32 && pass2;
1171 std::cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n";
1172
1173 FileSource valdata(DataDir("TestData/gostval.dat").c_str(), true, new HexDecoder);
1174 return BlockTransformationTest(FixedRoundsCipherFactory<GOSTEncryption, GOSTDecryption>(), valdata) && pass1 && pass2;
1175 }
1176
ValidateSHARK()1177 bool ValidateSHARK()
1178 {
1179 std::cout << "\nSHARK validation suite running...\n\n";
1180 bool pass1 = true, pass2 = true;
1181
1182 SHARKEncryption enc; // 128-bit only
1183 pass1 = SHARKEncryption::KEYLENGTH == 16 && pass1;
1184 pass1 = enc.StaticGetValidKeyLength(8) == 16 && pass1;
1185 pass1 = enc.StaticGetValidKeyLength(15) == 16 && pass1;
1186 pass1 = enc.StaticGetValidKeyLength(16) == 16 && pass1;
1187 pass1 = enc.StaticGetValidKeyLength(17) == 16 && pass1;
1188 pass1 = enc.StaticGetValidKeyLength(32) == 16 && pass1;
1189
1190 SHARKDecryption dec; // 128-bit only
1191 pass2 = SHARKDecryption::KEYLENGTH == 16 && pass2;
1192 pass2 = dec.StaticGetValidKeyLength(8) == 16 && pass2;
1193 pass2 = dec.StaticGetValidKeyLength(15) == 16 && pass2;
1194 pass2 = dec.StaticGetValidKeyLength(16) == 16 && pass2;
1195 pass2 = dec.StaticGetValidKeyLength(17) == 16 && pass2;
1196 pass2 = dec.StaticGetValidKeyLength(32) == 16 && pass2;
1197 std::cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n";
1198
1199 FileSource valdata(DataDir("TestData/sharkval.dat").c_str(), true, new HexDecoder);
1200 return BlockTransformationTest(FixedRoundsCipherFactory<SHARKEncryption, SHARKDecryption>(), valdata) && pass1 && pass2;
1201 }
1202
ValidateCAST()1203 bool ValidateCAST()
1204 {
1205 std::cout << "\nCAST-128 validation suite running...\n\n";
1206 bool pass1 = true, pass2 = true, pass3 = true;
1207
1208 CAST128Encryption enc1; // 40 to 128-bits (5 to 16-bytes)
1209 pass1 = CAST128Encryption::DEFAULT_KEYLENGTH == 16 && pass1;
1210 pass1 = enc1.StaticGetValidKeyLength(4) == 5 && pass1;
1211 pass1 = enc1.StaticGetValidKeyLength(5) == 5 && pass1;
1212 pass1 = enc1.StaticGetValidKeyLength(15) == 15 && pass1;
1213 pass1 = enc1.StaticGetValidKeyLength(16) == 16 && pass1;
1214 pass1 = enc1.StaticGetValidKeyLength(17) == 16 && pass1;
1215
1216 CAST128Decryption dec1; // 40 to 128-bits (5 to 16-bytes)
1217 pass2 = CAST128Decryption::DEFAULT_KEYLENGTH == 16 && pass2;
1218 pass2 = dec1.StaticGetValidKeyLength(4) == 5 && pass2;
1219 pass2 = dec1.StaticGetValidKeyLength(5) == 5 && pass2;
1220 pass2 = dec1.StaticGetValidKeyLength(15) == 15 && pass2;
1221 pass2 = dec1.StaticGetValidKeyLength(16) == 16 && pass2;
1222 pass2 = dec1.StaticGetValidKeyLength(17) == 16 && pass2;
1223 std::cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n";
1224
1225 FileSource val128(DataDir("TestData/cast128v.dat").c_str(), true, new HexDecoder);
1226 pass3 = BlockTransformationTest(FixedRoundsCipherFactory<CAST128Encryption, CAST128Decryption>(16), val128, 1) && pass3;
1227 pass3 = BlockTransformationTest(FixedRoundsCipherFactory<CAST128Encryption, CAST128Decryption>(10), val128, 1) && pass3;
1228 pass3 = BlockTransformationTest(FixedRoundsCipherFactory<CAST128Encryption, CAST128Decryption>(5), val128, 1) && pass3;
1229
1230 std::cout << "\nCAST-256 validation suite running...\n\n";
1231 bool pass4 = true, pass5 = true, pass6 = true;
1232
1233 CAST256Encryption enc2; // 128, 160, 192, 224, or 256-bits (16 to 32-bytes, step 4)
1234 pass1 = CAST128Encryption::DEFAULT_KEYLENGTH == 16 && pass1;
1235 pass4 = enc2.StaticGetValidKeyLength(15) == 16 && pass4;
1236 pass4 = enc2.StaticGetValidKeyLength(16) == 16 && pass4;
1237 pass4 = enc2.StaticGetValidKeyLength(17) == 20 && pass4;
1238 pass4 = enc2.StaticGetValidKeyLength(20) == 20 && pass4;
1239 pass4 = enc2.StaticGetValidKeyLength(24) == 24 && pass4;
1240 pass4 = enc2.StaticGetValidKeyLength(28) == 28 && pass4;
1241 pass4 = enc2.StaticGetValidKeyLength(31) == 32 && pass4;
1242 pass4 = enc2.StaticGetValidKeyLength(32) == 32 && pass4;
1243 pass4 = enc2.StaticGetValidKeyLength(33) == 32 && pass4;
1244
1245 CAST256Decryption dec2; // 128, 160, 192, 224, or 256-bits (16 to 32-bytes, step 4)
1246 pass2 = CAST256Decryption::DEFAULT_KEYLENGTH == 16 && pass2;
1247 pass5 = dec2.StaticGetValidKeyLength(15) == 16 && pass5;
1248 pass5 = dec2.StaticGetValidKeyLength(16) == 16 && pass5;
1249 pass5 = dec2.StaticGetValidKeyLength(17) == 20 && pass5;
1250 pass5 = dec2.StaticGetValidKeyLength(20) == 20 && pass5;
1251 pass5 = dec2.StaticGetValidKeyLength(24) == 24 && pass5;
1252 pass5 = dec2.StaticGetValidKeyLength(28) == 28 && pass5;
1253 pass5 = dec2.StaticGetValidKeyLength(31) == 32 && pass5;
1254 pass5 = dec2.StaticGetValidKeyLength(32) == 32 && pass5;
1255 pass5 = dec2.StaticGetValidKeyLength(33) == 32 && pass5;
1256 std::cout << (pass4 && pass5 ? "passed:" : "FAILED:") << " Algorithm key lengths\n";
1257
1258 FileSource val256(DataDir("TestData/cast256v.dat").c_str(), true, new HexDecoder);
1259 pass6 = BlockTransformationTest(FixedRoundsCipherFactory<CAST256Encryption, CAST256Decryption>(16), val256, 1) && pass6;
1260 pass6 = BlockTransformationTest(FixedRoundsCipherFactory<CAST256Encryption, CAST256Decryption>(24), val256, 1) && pass6;
1261 pass6 = BlockTransformationTest(FixedRoundsCipherFactory<CAST256Encryption, CAST256Decryption>(32), val256, 1) && pass6;
1262
1263 return pass1 && pass2 && pass3 && pass4 && pass5 && pass6;
1264 }
1265
ValidateSquare()1266 bool ValidateSquare()
1267 {
1268 std::cout << "\nSquare validation suite running...\n\n";
1269 bool pass1 = true, pass2 = true;
1270
1271 SquareEncryption enc; // 128-bits only
1272 pass1 = enc.StaticGetValidKeyLength(8) == 16 && pass1;
1273 pass1 = enc.StaticGetValidKeyLength(15) == 16 && pass1;
1274 pass1 = enc.StaticGetValidKeyLength(16) == 16 && pass1;
1275 pass1 = enc.StaticGetValidKeyLength(17) == 16 && pass1;
1276
1277 SquareDecryption dec; // 128-bits only
1278 pass2 = dec.StaticGetValidKeyLength(8) == 16 && pass2;
1279 pass2 = dec.StaticGetValidKeyLength(15) == 16 && pass2;
1280 pass2 = dec.StaticGetValidKeyLength(16) == 16 && pass2;
1281 pass2 = dec.StaticGetValidKeyLength(17) == 16 && pass2;
1282 std::cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n";
1283
1284 FileSource valdata(DataDir("TestData/squareva.dat").c_str(), true, new HexDecoder);
1285 return BlockTransformationTest(FixedRoundsCipherFactory<SquareEncryption, SquareDecryption>(), valdata) && pass1 && pass2;
1286 }
1287
ValidateSKIPJACK()1288 bool ValidateSKIPJACK()
1289 {
1290 std::cout << "\nSKIPJACK validation suite running...\n\n";
1291 bool pass1 = true, pass2 = true;
1292
1293 SKIPJACKEncryption enc; // 80-bits only
1294 pass1 = enc.StaticGetValidKeyLength(8) == 10 && pass1;
1295 pass1 = enc.StaticGetValidKeyLength(9) == 10 && pass1;
1296 pass1 = enc.StaticGetValidKeyLength(10) == 10 && pass1;
1297 pass1 = enc.StaticGetValidKeyLength(16) == 10 && pass1;
1298
1299 SKIPJACKDecryption dec; // 80-bits only
1300 pass2 = dec.StaticGetValidKeyLength(8) == 10 && pass2;
1301 pass2 = dec.StaticGetValidKeyLength(9) == 10 && pass2;
1302 pass2 = dec.StaticGetValidKeyLength(10) == 10 && pass2;
1303 pass2 = dec.StaticGetValidKeyLength(16) == 10 && pass2;
1304 std::cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n";
1305
1306 FileSource valdata(DataDir("TestData/skipjack.dat").c_str(), true, new HexDecoder);
1307 return BlockTransformationTest(FixedRoundsCipherFactory<SKIPJACKEncryption, SKIPJACKDecryption>(), valdata) && pass1 && pass2;
1308 }
1309
ValidateSEAL()1310 bool ValidateSEAL()
1311 {
1312 const byte input[] = {0x37,0xa0,0x05,0x95,0x9b,0x84,0xc4,0x9c,0xa4,0xbe,0x1e,0x05,0x06,0x73,0x53,0x0f,0x5f,0xb0,0x97,0xfd,0xf6,0xa1,0x3f,0xbd,0x6c,0x2c,0xde,0xcd,0x81,0xfd,0xee,0x7c};
1313 const byte key[] = {0x67, 0x45, 0x23, 0x01, 0xef, 0xcd, 0xab, 0x89, 0x98, 0xba, 0xdc, 0xfe, 0x10, 0x32, 0x54, 0x76, 0xc3, 0xd2, 0xe1, 0xf0};
1314 const byte iv[] = {0x01, 0x35, 0x77, 0xaf};
1315 byte output[32];
1316
1317 std::cout << "\nSEAL validation suite running...\n\n";
1318
1319 SEAL<>::Encryption seal(key, sizeof(key), iv);
1320 unsigned int size = sizeof(input);
1321 bool pass = true;
1322
1323 memset(output, 1, size);
1324 seal.ProcessString(output, input, size);
1325 for (unsigned int i=0; i<size; i++)
1326 if (output[i] != 0)
1327 pass = false;
1328
1329 seal.Seek(1);
1330 output[1] = seal.ProcessByte(output[1]);
1331 seal.ProcessString(output+2, size-2);
1332 pass = pass && memcmp(output+1, input+1, size-1) == 0;
1333
1334 std::cout << (pass ? "passed" : "FAILED") << std::endl;
1335 return pass;
1336 }
1337
ValidateBaseCode()1338 bool ValidateBaseCode()
1339 {
1340 bool pass = true, fail;
1341 byte data[255];
1342 for (unsigned int i=0; i<255; i++)
1343 data[i] = byte(i);
1344
1345 const char hexEncoded[] =
1346 "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F2021222324252627"
1347 "28292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F"
1348 "505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F7071727374757677"
1349 "78797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9F"
1350 "A0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7"
1351 "C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFE0E1E2E3E4E5E6E7E8E9EAEBECEDEEEF"
1352 "F0F1F2F3F4F5F6F7F8F9FAFBFCFDFE";
1353 const char base32Encoded[] =
1354 "AAASEA2EAWDAQCAJBIFS2DIQB6IBCESVCSKTNF22DEPBYHA7D2RUAIJCENUCKJTHFAWUWK3NFWZC8NBT"
1355 "GI3VIPJYG66DUQT5HS8V6R4AIFBEGTCFI3DWSUKKJPGE4VURKBIXEW4WKXMFQYC3MJPX2ZK8M7SGC2VD"
1356 "NTUYN35IPFXGY5DPP3ZZA6MUQP4HK7VZRB6ZW856RX9H9AEBSKB2JBNGS8EIVCWMTUG27D6SUGJJHFEX"
1357 "U4M3TGN4VQQJ5HW9WCS4FI7EWYVKRKFJXKX43MPQX82MDNXVYU45PP72ZG7MZRF7Z496BSQC2RCNMTYH"
1358 "3DE6XU8N3ZHN9WGT4MJ7JXQY49NPVYY55VQ77Z9A6HTQH3HF65V8T4RK7RYQ55ZR8D29F69W8Z5RR8H3"
1359 "9M7939R8";
1360 const char base64AndHexEncoded[] =
1361 "41414543417751464267634943516F4C4441304F4478415245684D554652595847426B6147787764"
1362 "486838674953496A4A43556D4A7967704B6973734C5334764D4445794D7A51310A4E6A63344F546F"
1363 "375044302B50304242516B4E4552555A4853456C4B5330784E546B395155564A5456465657563168"
1364 "5A576C746358563566594746695932526C5A6D646F615770720A6247317562334278636E4E306458"
1365 "5A3365486C3665337839666E2B4167594B44684957476834694A696F754D6A5936506B4A47536B35"
1366 "53566C7065596D5A71626E4A32656E3643680A6F714F6B7061616E714B6D717136797472712B7773"
1367 "624B7A744C573274376935757275387662362F774D484377385446787366497963724C7A4D334F7A"
1368 "39445230745055316462580A324E6E6132397A6433742F6734654C6A354F586D352B6A7036757673"
1369 "3765377638504879382F5431397666342B6672372F50332B0A";
1370 const char base64URLAndHexEncoded[] =
1371 "41414543417751464267634943516F4C4441304F4478415245684D554652595847426B6147787764"
1372 "486838674953496A4A43556D4A7967704B6973734C5334764D4445794D7A51314E6A63344F546F37"
1373 "5044302D50304242516B4E4552555A4853456C4B5330784E546B395155564A54564656575631685A"
1374 "576C746358563566594746695932526C5A6D646F615770726247317562334278636E4E3064585A33"
1375 "65486C3665337839666E2D4167594B44684957476834694A696F754D6A5936506B4A47536B355356"
1376 "6C7065596D5A71626E4A32656E3643686F714F6B7061616E714B6D717136797472712D7773624B7A"
1377 "744C573274376935757275387662365F774D484377385446787366497963724C7A4D334F7A394452"
1378 "3074505531646258324E6E6132397A6433745F6734654C6A354F586D352D6A703675767337653776"
1379 "38504879385F5431397666342D6672375F50332D";
1380
1381 std::cout << "\nBase64, Base64URL, Base32 and Base16 coding validation suite running...\n\n";
1382
1383 fail = !TestFilter(HexEncoder().Ref(), data, 255, (const byte *)hexEncoded, strlen(hexEncoded));
1384 try {HexEncoder().IsolatedInitialize(g_nullNameValuePairs);}
1385 catch (const Exception&) {fail=true;}
1386 std::cout << (fail ? "FAILED:" : "passed:");
1387 std::cout << " Hex Encoding\n";
1388 pass = pass && !fail;
1389
1390 fail = !TestFilter(HexDecoder().Ref(), (const byte *)hexEncoded, strlen(hexEncoded), data, 255);
1391 try {HexDecoder().IsolatedInitialize(g_nullNameValuePairs);}
1392 catch (const Exception&) {fail=true;}
1393 std::cout << (fail ? "FAILED:" : "passed:");
1394 std::cout << " Hex Decoding\n";
1395 pass = pass && !fail;
1396
1397 fail = !TestFilter(Base32Encoder().Ref(), data, 255, (const byte *)base32Encoded, strlen(base32Encoded));
1398 try {Base32Encoder().IsolatedInitialize(g_nullNameValuePairs);}
1399 catch (const Exception&) {fail=true;}
1400 std::cout << (fail ? "FAILED:" : "passed:");
1401 std::cout << " Base32 Encoding\n";
1402 pass = pass && !fail;
1403
1404 fail = !TestFilter(Base32Decoder().Ref(), (const byte *)base32Encoded, strlen(base32Encoded), data, 255);
1405 try {Base32Decoder().IsolatedInitialize(g_nullNameValuePairs);}
1406 catch (const Exception&) {fail=true;}
1407 std::cout << (fail ? "FAILED:" : "passed:");
1408 std::cout << " Base32 Decoding\n";
1409 pass = pass && !fail;
1410
1411 fail = !TestFilter(Base64Encoder(new HexEncoder).Ref(), data, 255, (const byte *)base64AndHexEncoded, strlen(base64AndHexEncoded));
1412 try {Base64Encoder().IsolatedInitialize(g_nullNameValuePairs);}
1413 catch (const Exception&) {fail=true;}
1414 std::cout << (fail ? "FAILED:" : "passed:");
1415 std::cout << " Base64 Encoding\n";
1416 pass = pass && !fail;
1417
1418 fail = !TestFilter(HexDecoder(new Base64Decoder).Ref(), (const byte *)base64AndHexEncoded, strlen(base64AndHexEncoded), data, 255);
1419 try {Base64Decoder().IsolatedInitialize(g_nullNameValuePairs);}
1420 catch (const Exception&) {fail=true;}
1421 std::cout << (fail ? "FAILED:" : "passed:");
1422 std::cout << " Base64 Decoding\n";
1423 pass = pass && !fail;
1424
1425 fail = !TestFilter(Base64URLEncoder(new HexEncoder).Ref(), data, 255, (const byte *)base64URLAndHexEncoded, strlen(base64URLAndHexEncoded));
1426 try {Base64URLEncoder().IsolatedInitialize(g_nullNameValuePairs);}
1427 catch (const Exception&) {fail=true;}
1428 std::cout << (fail ? "FAILED:" : "passed:");
1429 std::cout << " Base64 URL Encoding\n";
1430 pass = pass && !fail;
1431
1432 fail = !TestFilter(HexDecoder(new Base64URLDecoder).Ref(), (const byte *)base64URLAndHexEncoded, strlen(base64URLAndHexEncoded), data, 255);
1433 try {Base64URLDecoder().IsolatedInitialize(g_nullNameValuePairs);}
1434 catch (const Exception&) {fail=true;}
1435 std::cout << (fail ? "FAILED:" : "passed:");
1436 std::cout << " Base64 URL Decoding\n";
1437 pass = pass && !fail;
1438
1439 return pass;
1440 }
1441
1442 class MyEncoder : public SimpleProxyFilter
1443 {
1444 public:
1445 MyEncoder(BufferedTransformation *attachment = NULLPTR);
1446 void IsolatedInitialize(const NameValuePairs ¶ms);
1447 };
1448
MyEncoder(BufferedTransformation * attachment)1449 MyEncoder::MyEncoder(BufferedTransformation *attachment)
1450 : SimpleProxyFilter(new BaseN_Encoder(new Grouper), attachment)
1451 {
1452 IsolatedInitialize(MakeParameters(Name::InsertLineBreaks(), true)(Name::MaxLineLength(), 72));
1453 }
1454
IsolatedInitialize(const NameValuePairs & parameters)1455 void MyEncoder::IsolatedInitialize(const NameValuePairs ¶meters)
1456 {
1457 bool insertLineBreaks = parameters.GetValueWithDefault(Name::InsertLineBreaks(), true);
1458 int maxLineLength = parameters.GetIntValueWithDefault(Name::MaxLineLength(), 72);
1459
1460 const byte padding = '=';
1461 const char *lineBreak = insertLineBreaks ? "\n" : "";
1462
1463 char stars[64];
1464 memset(stars, '*', 64);
1465
1466 m_filter->Initialize(CombinedNameValuePairs(
1467 parameters,
1468 MakeParameters(Name::EncodingLookupArray(), (const byte *)&stars[0], false)
1469 (Name::PaddingByte(), padding)
1470 (Name::GroupSize(), insertLineBreaks ? maxLineLength : 0)
1471 (Name::Separator(), ConstByteArrayParameter(lineBreak))
1472 (Name::Terminator(), ConstByteArrayParameter(lineBreak))
1473 (Name::Log2Base(), 6, true)));
1474 }
1475
1476 class MyDecoder : public BaseN_Decoder
1477 {
1478 public:
1479 MyDecoder(BufferedTransformation *attachment = NULLPTR);
1480 void IsolatedInitialize(const NameValuePairs ¶ms);
1481 static const int * CRYPTOPP_API GetDecodingLookupArray();
1482 };
1483
MyDecoder(BufferedTransformation * attachment)1484 MyDecoder::MyDecoder(BufferedTransformation *attachment)
1485 : BaseN_Decoder(GetDecodingLookupArray(), 6, attachment)
1486 {
1487 }
1488
IsolatedInitialize(const NameValuePairs & parameters)1489 void MyDecoder::IsolatedInitialize(const NameValuePairs ¶meters)
1490 {
1491 BaseN_Decoder::IsolatedInitialize(CombinedNameValuePairs(
1492 parameters,
1493 MakeParameters(Name::DecodingLookupArray(), GetDecodingLookupArray(), false)(Name::Log2Base(), 6, true)));
1494 }
1495
1496 struct MyDecoderAlphabet
1497 {
MyDecoderAlphabetMyDecoderAlphabet1498 MyDecoderAlphabet() {
1499 std::fill(tab, tab+COUNTOF(tab), '*');
1500 }
1501 byte tab[64];
1502 };
1503
1504 struct MyDecoderArray
1505 {
MyDecoderArrayMyDecoderArray1506 MyDecoderArray() {
1507 std::fill(tab, tab+COUNTOF(tab), -1);
1508 }
1509 int tab[256];
1510 };
1511
GetDecodingLookupArray()1512 const int * MyDecoder::GetDecodingLookupArray()
1513 {
1514 static bool s_initialized = false;
1515 static MyDecoderAlphabet s_alpha;
1516 static MyDecoderArray s_array;
1517
1518 MEMORY_BARRIER();
1519 if (!s_initialized)
1520 {
1521 InitializeDecodingLookupArray(s_array.tab, s_alpha.tab, COUNTOF(s_alpha.tab), false);
1522 s_initialized = true;
1523 MEMORY_BARRIER();
1524 }
1525 return s_array.tab;
1526 }
1527
ValidateEncoder()1528 bool ValidateEncoder()
1529 {
1530 // The default encoder and decoder alphabet are bogus. They are a
1531 // string of '*'. To round trip a string both IsolatedInitialize
1532 // must be called and work correctly.
1533 std::cout << "\nCustom encoder validation running...\n\n";
1534 bool pass = true;
1535
1536 int lookup[256];
1537 const char alphabet[64+1] =
1538 "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz01234576789*";
1539 const char expected[] =
1540 "ILcBMSgriDicmKmTi2oENCsuJTufN0yWjL1HnS8xKdaiOkeZK3gKock1ktmlo1q4LlsNPrAyGrG0gjO2gzQ5FQ==";
1541
1542 MyEncoder encoder;
1543 std::string str1;
1544
1545 AlgorithmParameters eparams = MakeParameters(Name::EncodingLookupArray(),(const byte*)alphabet)
1546 (Name::InsertLineBreaks(), false);
1547 encoder.IsolatedInitialize(eparams);
1548
1549 encoder.Detach(new StringSink(str1));
1550 encoder.Put((const byte*) alphabet, 64);
1551 encoder.MessageEnd();
1552
1553 MyDecoder decoder;
1554 std::string str2;
1555
1556 MyDecoder::InitializeDecodingLookupArray(lookup, (const byte*) alphabet, 64, false);
1557 AlgorithmParameters dparams = MakeParameters(Name::DecodingLookupArray(),(const int*)lookup);
1558 decoder.IsolatedInitialize(dparams);
1559
1560 decoder.Detach(new StringSink(str2));
1561 decoder.Put(ConstBytePtr(str1), BytePtrSize(str1));
1562 decoder.MessageEnd();
1563
1564 pass = (str1 == std::string(expected)) && pass;
1565 pass = (str2 == std::string(alphabet, 64)) && pass;
1566
1567 std::cout << (pass ? "passed:" : "FAILED:");
1568 std::cout << " Encode and decode\n";
1569
1570 // Try forcing an empty message. This is the Monero bug
1571 // at https://github.com/weidai11/cryptopp/issues/562.
1572 {
1573 MyDecoder decoder2;
1574 SecByteBlock empty;
1575
1576 AlgorithmParameters dparams2 = MakeParameters(Name::DecodingLookupArray(),(const int*)lookup);
1577 decoder2.IsolatedInitialize(dparams2);
1578
1579 decoder2.Detach(new Redirector(TheBitBucket()));
1580 decoder2.Put(empty.BytePtr(), empty.SizeInBytes());
1581 decoder2.MessageEnd();
1582
1583 // Tame the optimizer
1584 volatile lword size = decoder2.MaxRetrievable();
1585 lword shadow = size;
1586 CRYPTOPP_UNUSED(shadow);
1587 }
1588
1589 std::cout << "passed: 0-length message\n";
1590
1591 return pass;
1592 }
1593
ValidateSHACAL2()1594 bool ValidateSHACAL2()
1595 {
1596 std::cout << "\nSHACAL-2 validation suite running...\n\n";
1597 bool pass1 = true, pass2 = true, pass3 = true;
1598
1599 SHACAL2Encryption enc; // 128 to 512-bits (16 to 64-bytes)
1600 pass1 = enc.StaticGetValidKeyLength(8) == 16 && pass1;
1601 pass1 = enc.StaticGetValidKeyLength(15) == 16 && pass1;
1602 pass1 = enc.StaticGetValidKeyLength(16) == 16 && pass1;
1603 pass1 = enc.StaticGetValidKeyLength(64) == 64 && pass1;
1604 pass1 = enc.StaticGetValidKeyLength(65) == 64 && pass1;
1605 pass1 = enc.StaticGetValidKeyLength(128) == 64 && pass1;
1606 pass1 = enc.StaticGetValidKeyLength(0) == enc.MinKeyLength() && pass1;
1607 pass1 = enc.StaticGetValidKeyLength(SIZE_MAX) == enc.MaxKeyLength() && pass1;
1608
1609 SHACAL2Decryption dec; // 128 to 512-bits (16 to 64-bytes)
1610 pass2 = dec.StaticGetValidKeyLength(8) == 16 && pass2;
1611 pass2 = dec.StaticGetValidKeyLength(15) == 16 && pass2;
1612 pass2 = dec.StaticGetValidKeyLength(16) == 16 && pass2;
1613 pass2 = dec.StaticGetValidKeyLength(64) == 64 && pass2;
1614 pass2 = dec.StaticGetValidKeyLength(65) == 64 && pass2;
1615 pass2 = dec.StaticGetValidKeyLength(128) == 64 && pass2;
1616 pass2 = dec.StaticGetValidKeyLength(0) == dec.MinKeyLength() && pass2;
1617 pass2 = dec.StaticGetValidKeyLength(SIZE_MAX) == dec.MaxKeyLength() && pass2;
1618 std::cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n";
1619
1620 FileSource valdata(DataDir("TestData/shacal2v.dat").c_str(), true, new HexDecoder);
1621 pass3 = BlockTransformationTest(FixedRoundsCipherFactory<SHACAL2Encryption, SHACAL2Decryption>(16), valdata, 4) && pass3;
1622 pass3 = BlockTransformationTest(FixedRoundsCipherFactory<SHACAL2Encryption, SHACAL2Decryption>(64), valdata, 10) && pass3;
1623 return pass1 && pass2 && pass3;
1624 }
1625
ValidateARIA()1626 bool ValidateARIA()
1627 {
1628 std::cout << "\nARIA validation suite running...\n\n";
1629 bool pass1 = true, pass2 = true, pass3 = true;
1630
1631 ARIAEncryption enc;
1632 pass1 = enc.StaticGetValidKeyLength(8) == 16 && pass1;
1633 pass1 = enc.StaticGetValidKeyLength(16) == 16 && pass1;
1634 pass1 = enc.StaticGetValidKeyLength(24) == 24 && pass1;
1635 pass1 = enc.StaticGetValidKeyLength(32) == 32 && pass1;
1636 pass1 = enc.StaticGetValidKeyLength(64) == 32 && pass1;
1637 pass1 = enc.StaticGetValidKeyLength(128) == 32 && pass1;
1638 pass1 = enc.StaticGetValidKeyLength(0) == enc.MinKeyLength() && pass1;
1639 pass1 = enc.StaticGetValidKeyLength(SIZE_MAX) == enc.MaxKeyLength() && pass1;
1640
1641 ARIADecryption dec;
1642 pass2 = dec.StaticGetValidKeyLength(8) == 16 && pass2;
1643 pass2 = dec.StaticGetValidKeyLength(16) == 16 && pass2;
1644 pass2 = dec.StaticGetValidKeyLength(24) == 24 && pass2;
1645 pass2 = dec.StaticGetValidKeyLength(32) == 32 && pass2;
1646 pass2 = dec.StaticGetValidKeyLength(64) == 32 && pass2;
1647 pass2 = dec.StaticGetValidKeyLength(128) == 32 && pass2;
1648 pass2 = dec.StaticGetValidKeyLength(0) == dec.MinKeyLength() && pass2;
1649 pass2 = dec.StaticGetValidKeyLength(SIZE_MAX) == dec.MaxKeyLength() && pass2;
1650 std::cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n";
1651
1652 FileSource valdata(DataDir("TestData/aria.dat").c_str(), true, new HexDecoder);
1653 pass3 = BlockTransformationTest(FixedRoundsCipherFactory<ARIAEncryption, ARIADecryption>(16), valdata, 15) && pass3;
1654 pass3 = BlockTransformationTest(FixedRoundsCipherFactory<ARIAEncryption, ARIADecryption>(24), valdata, 15) && pass3;
1655 pass3 = BlockTransformationTest(FixedRoundsCipherFactory<ARIAEncryption, ARIADecryption>(32), valdata, 15) && pass3;
1656 return pass1 && pass2 && pass3;
1657 }
1658
ValidateSIMECK()1659 bool ValidateSIMECK()
1660 {
1661 std::cout << "\nSIMECK validation suite running...\n";
1662
1663 return RunTestDataFile("TestVectors/simeck.txt");
1664 }
1665
ValidateCHAM()1666 bool ValidateCHAM()
1667 {
1668 std::cout << "\nCHAM validation suite running...\n";
1669
1670 return RunTestDataFile("TestVectors/cham.txt");
1671 }
1672
ValidateHIGHT()1673 bool ValidateHIGHT()
1674 {
1675 std::cout << "\nHIGHT validation suite running...\n";
1676
1677 return RunTestDataFile("TestVectors/hight.txt");
1678 }
1679
ValidateLEA()1680 bool ValidateLEA()
1681 {
1682 std::cout << "\nLEA validation suite running...\n";
1683
1684 return RunTestDataFile("TestVectors/lea.txt");
1685 }
1686
ValidateSIMON()1687 bool ValidateSIMON()
1688 {
1689 std::cout << "\nSIMON validation suite running...\n";
1690
1691 return RunTestDataFile("TestVectors/simon.txt");
1692 }
1693
ValidateSPECK()1694 bool ValidateSPECK()
1695 {
1696 std::cout << "\nSPECK validation suite running...\n";
1697
1698 return RunTestDataFile("TestVectors/speck.txt");
1699 }
1700
ValidateCamellia()1701 bool ValidateCamellia()
1702 {
1703 std::cout << "\nCamellia validation suite running...\n\n";
1704 bool pass1 = true, pass2 = true, pass3 = true;
1705
1706 CamelliaEncryption enc;
1707 pass1 = enc.StaticGetValidKeyLength(8) == 16 && pass1;
1708 pass1 = enc.StaticGetValidKeyLength(16) == 16 && pass1;
1709 pass1 = enc.StaticGetValidKeyLength(24) == 24 && pass1;
1710 pass1 = enc.StaticGetValidKeyLength(32) == 32 && pass1;
1711 pass1 = enc.StaticGetValidKeyLength(64) == 32 && pass1;
1712 pass1 = enc.StaticGetValidKeyLength(128) == 32 && pass1;
1713 pass1 = enc.StaticGetValidKeyLength(0) == enc.MinKeyLength() && pass1;
1714 pass1 = enc.StaticGetValidKeyLength(SIZE_MAX) == enc.MaxKeyLength() && pass1;
1715
1716 CamelliaDecryption dec;
1717 pass2 = dec.StaticGetValidKeyLength(8) == 16 && pass2;
1718 pass2 = dec.StaticGetValidKeyLength(16) == 16 && pass2;
1719 pass2 = dec.StaticGetValidKeyLength(24) == 24 && pass2;
1720 pass2 = dec.StaticGetValidKeyLength(32) == 32 && pass2;
1721 pass2 = dec.StaticGetValidKeyLength(64) == 32 && pass2;
1722 pass2 = dec.StaticGetValidKeyLength(128) == 32 && pass2;
1723 pass2 = dec.StaticGetValidKeyLength(0) == dec.MinKeyLength() && pass2;
1724 pass2 = dec.StaticGetValidKeyLength(SIZE_MAX) == dec.MaxKeyLength() && pass2;
1725 std::cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n";
1726
1727 FileSource valdata(DataDir("TestData/camellia.dat").c_str(), true, new HexDecoder);
1728 pass3 = BlockTransformationTest(FixedRoundsCipherFactory<CamelliaEncryption, CamelliaDecryption>(16), valdata, 15) && pass3;
1729 pass3 = BlockTransformationTest(FixedRoundsCipherFactory<CamelliaEncryption, CamelliaDecryption>(24), valdata, 15) && pass3;
1730 pass3 = BlockTransformationTest(FixedRoundsCipherFactory<CamelliaEncryption, CamelliaDecryption>(32), valdata, 15) && pass3;
1731 return pass1 && pass2 && pass3;
1732 }
1733
ValidateSalsa()1734 bool ValidateSalsa()
1735 {
1736 std::cout << "\nSalsa validation suite running...\n";
1737
1738 return RunTestDataFile("TestVectors/salsa.txt");
1739 }
1740
ValidateChaCha()1741 bool ValidateChaCha()
1742 {
1743 std::cout << "\nChaCha validation suite running...\n";
1744
1745 return RunTestDataFile("TestVectors/chacha.txt");
1746 }
1747
ValidateChaChaTLS()1748 bool ValidateChaChaTLS()
1749 {
1750 std::cout << "\nChaCha-TLS validation suite running...\n";
1751
1752 return RunTestDataFile("TestVectors/chacha_tls.txt");
1753 }
1754
ValidateSosemanuk()1755 bool ValidateSosemanuk()
1756 {
1757 std::cout << "\nSosemanuk validation suite running...\n";
1758 return RunTestDataFile("TestVectors/sosemanuk.txt");
1759 }
1760
ValidateRabbit()1761 bool ValidateRabbit()
1762 {
1763 std::cout << "\nRabbit validation suite running...\n";
1764 return RunTestDataFile("TestVectors/rabbit.txt");
1765 }
1766
ValidateHC128()1767 bool ValidateHC128()
1768 {
1769 std::cout << "\nHC-128 validation suite running...\n";
1770 return RunTestDataFile("TestVectors/hc128.txt");
1771 }
1772
ValidateHC256()1773 bool ValidateHC256()
1774 {
1775 std::cout << "\nHC-256 validation suite running...\n";
1776 return RunTestDataFile("TestVectors/hc256.txt");
1777 }
1778
ValidateVMAC()1779 bool ValidateVMAC()
1780 {
1781 std::cout << "\nVMAC validation suite running...\n";
1782 return RunTestDataFile("TestVectors/vmac.txt");
1783 }
1784
ValidateCCM()1785 bool ValidateCCM()
1786 {
1787 std::cout << "\nAES/CCM validation suite running...\n";
1788 return RunTestDataFile("TestVectors/ccm.txt");
1789 }
1790
ValidateGCM()1791 bool ValidateGCM()
1792 {
1793 std::cout << "\nAES/GCM validation suite running...\n";
1794 std::cout << "\n2K tables:";
1795 bool pass = RunTestDataFile("TestVectors/gcm.txt", MakeParameters(Name::TableSize(), (int)2048));
1796 std::cout << "\n64K tables:";
1797 return RunTestDataFile("TestVectors/gcm.txt", MakeParameters(Name::TableSize(), (int)64*1024)) && pass;
1798 }
1799
ValidateXTS()1800 bool ValidateXTS()
1801 {
1802 std::cout << "\nAES/XTS validation suite running...\n";
1803 return RunTestDataFile("TestVectors/xts.txt");
1804 }
1805
ValidateCMAC()1806 bool ValidateCMAC()
1807 {
1808 std::cout << "\nCMAC validation suite running...\n";
1809 return RunTestDataFile("TestVectors/cmac.txt");
1810 }
1811
1812 NAMESPACE_END // Test
1813 NAMESPACE_END // CryptoPP
1814