~zooko/cryptopp/trunk

« back to all changes in this revision

Viewing changes to validat1.cpp

  • Committer: weidai
  • Date: 2007-05-04 15:04:58 UTC
  • Revision ID: svn-v4:57ff6487-cd31-0410-9ec3-f628ee90f5f0:trunk/c5:328
reduce risk of random number reuse after VM rollback

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 
3
3
#include "pch.h"
4
4
 
 
5
#define CRYPTOPP_ENABLE_NAMESPACE_WEAK
5
6
#include "files.h"
6
7
#include "hex.h"
7
8
#include "base32.h"
33
34
#include "camellia.h"
34
35
#include "osrng.h"
35
36
#include "zdeflate.h"
 
37
#include "cpu.h"
36
38
 
37
39
#include <stdlib.h>
38
40
#include <time.h>
56
58
        pass=ValidateMD5() && pass;
57
59
        pass=ValidateSHA() && pass;
58
60
        pass=ValidateSHA2() && pass;
59
 
        pass=ValidateHAVAL() && pass;
60
61
        pass=ValidateTiger() && pass;
61
62
        pass=ValidateRIPEMD() && pass;
62
63
        pass=ValidatePanama() && pass;
63
64
        pass=ValidateWhirlpool() && pass;
64
65
 
65
 
        pass=ValidateMD5MAC() && pass;
66
66
        pass=ValidateHMAC() && pass;
67
 
        pass=ValidateXMACC() && pass;
68
67
        pass=ValidateTTMAC() && pass;
69
68
 
70
69
        pass=ValidatePBKDF() && pass;
92
91
        pass=ValidateSHACAL2() && pass;
93
92
        pass=ValidateCamellia() && pass;
94
93
        pass=ValidateSalsa() && pass;
 
94
        pass=ValidateSosemanuk() && pass;
95
95
 
96
96
        pass=ValidateBBS() && pass;
97
97
        pass=ValidateDH() && pass;
189
189
                pass = false;
190
190
        }
191
191
        cout << "sizeof(word64) == " << sizeof(word64) << endl;
192
 
#elif CRYPTOPP_NATIVE_DWORD_AVAILABLE
 
192
#elif defined(CRYPTOPP_NATIVE_DWORD_AVAILABLE)
193
193
        if (sizeof(dword) >= 8)
194
194
        {
195
195
                cout << "FAILED:  sizeof(dword) >= 8, but WORD64_AVAILABLE not defined" << endl;
216
216
#endif
217
217
        cout << endl;
218
218
 
 
219
        bool hasMMX = HasMMX();
 
220
        bool hasSSE2 = HasSSE2();
 
221
        bool hasSSSE3 = HasSSSE3();
 
222
        bool isP4 = IsP4();
 
223
        int cacheLineSize = GetCacheLineSize();
 
224
 
 
225
        if ((isP4 && (!hasMMX || !hasSSE2)) || (hasSSE2 && !hasMMX) || (cacheLineSize < 16 || cacheLineSize > 256 || !IsPowerOf2(cacheLineSize)))
 
226
        {
 
227
                cout << "FAILED:  ";
 
228
                pass = false;
 
229
        }
 
230
        else
 
231
                cout << "passed:  ";
 
232
 
 
233
        cout << "hasMMX == " << hasMMX << ", hasSSE2 == " << hasSSE2 << ", hasSSSE3 == " << hasSSSE3 << ", isP4 == " << isP4 << ", cacheLineSize == " << cacheLineSize;
 
234
 
219
235
        if (!pass)
220
236
        {
221
237
                cout << "Some critical setting in config.h is in error.  Please fix it and recompile." << endl;
260
276
                        cout << "passed:";
261
277
                cout << "  it took " << long(t1) << " seconds to generate " << total << " bytes" << endl;
262
278
 
 
279
#if 0   // disable this part. it's causing an unpredictable pause during the validation testing
263
280
                if (t1 < 2)
264
281
                {
265
282
                        // that was fast, are we really blocking?
280
297
                                total += 1;
281
298
                                length += 1;
282
299
                        }
283
 
                        // turn off this test because it fails on several systems, including Darwin
284
 
                        // they don't block, or gather entropy too fast?
285
 
                        if (false) // (length > 1024)
 
300
                        if (length > 1024)
286
301
                        {
287
302
                                cout << "FAILED:";
288
303
                                pass = false;
291
306
                                cout << "passed:";
292
307
                        cout << "  it generated " << length << " bytes in " << long(time(NULL) - t) << " seconds" << endl;
293
308
                }
 
309
#endif
294
310
 
295
311
                test.AttachedTransformation()->MessageEnd();
296
312
 
976
992
        0xc0};
977
993
 
978
994
        // VC60 workaround: auto_ptr lacks reset()
979
 
        member_ptr<ARC4> arc4;
 
995
        member_ptr<Weak::ARC4> arc4;
980
996
        bool pass=true, fail;
981
997
        int i;
982
998
 
983
999
        cout << "\nARC4 validation suite running...\n\n";
984
1000
 
985
 
        arc4.reset(new ARC4(Key0, sizeof(Key0)));
 
1001
        arc4.reset(new Weak::ARC4(Key0, sizeof(Key0)));
986
1002
        arc4->ProcessString(Input0, sizeof(Input0));
987
1003
        fail = memcmp(Input0, Output0, sizeof(Input0)) != 0;
988
1004
        cout << (fail ? "FAILED" : "passed") << "    Test 0" << endl;
989
1005
        pass = pass && !fail;
990
1006
 
991
 
        arc4.reset(new ARC4(Key1, sizeof(Key1)));
 
1007
        arc4.reset(new Weak::ARC4(Key1, sizeof(Key1)));
992
1008
        arc4->ProcessString(Key1, Input1, sizeof(Key1));
993
1009
        fail = memcmp(Output1, Key1, sizeof(Key1)) != 0;
994
1010
        cout << (fail ? "FAILED" : "passed") << "    Test 1" << endl;
995
1011
        pass = pass && !fail;
996
1012
 
997
 
        arc4.reset(new ARC4(Key2, sizeof(Key2)));
 
1013
        arc4.reset(new Weak::ARC4(Key2, sizeof(Key2)));
998
1014
        for (i=0, fail=false; i<sizeof(Input2); i++)
999
1015
                if (arc4->ProcessByte(Input2[i]) != Output2[i])
1000
1016
                        fail = true;
1001
1017
        cout << (fail ? "FAILED" : "passed") << "    Test 2" << endl;
1002
1018
        pass = pass && !fail;
1003
1019
 
1004
 
        arc4.reset(new ARC4(Key3, sizeof(Key3)));
 
1020
        arc4.reset(new Weak::ARC4(Key3, sizeof(Key3)));
1005
1021
        for (i=0, fail=false; i<sizeof(Input3); i++)
1006
1022
                if (arc4->ProcessByte(Input3[i]) != Output3[i])
1007
1023
                        fail = true;
1008
1024
        cout << (fail ? "FAILED" : "passed") << "    Test 3" << endl;
1009
1025
        pass = pass && !fail;
1010
1026
 
1011
 
        arc4.reset(new ARC4(Key4, sizeof(Key4)));
 
1027
        arc4.reset(new Weak::ARC4(Key4, sizeof(Key4)));
1012
1028
        for (i=0, fail=false; i<sizeof(Input4); i++)
1013
1029
                if (arc4->ProcessByte(Input4[i]) != Output4[i])
1014
1030
                        fail = true;
1296
1312
{
1297
1313
        cout << "\nCamellia validation suite running...\n\n";
1298
1314
 
1299
 
#ifdef WORD64_AVAILABLE
1300
1315
        bool pass = true;
1301
1316
        FileSource valdata("camellia.dat", true, new HexDecoder);
1302
1317
        pass = BlockTransformationTest(FixedRoundsCipherFactory<CamelliaEncryption, CamelliaDecryption>(16), valdata, 15) && pass;
1303
1318
        pass = BlockTransformationTest(FixedRoundsCipherFactory<CamelliaEncryption, CamelliaDecryption>(24), valdata, 15) && pass;
1304
1319
        pass = BlockTransformationTest(FixedRoundsCipherFactory<CamelliaEncryption, CamelliaDecryption>(32), valdata, 15) && pass;
1305
1320
        return pass;
1306
 
#else
1307
 
        cout << "word64 not available, skipping Camellia validation." << endl;
1308
 
        return true;
1309
 
#endif
1310
1321
}
1311
1322
 
1312
1323
bool ValidateSalsa()
1315
1326
 
1316
1327
        return RunTestDataFile("TestVectors/salsa.txt");
1317
1328
}
 
1329
 
 
1330
bool ValidateSosemanuk()
 
1331
{
 
1332
        cout << "\nSosemanuk validation suite running...\n";
 
1333
 
 
1334
        return RunTestDataFile("TestVectors/sosemanuk.txt");
 
1335
}