MCPcopy Create free account
hub / github.com/Tripwire/tripwire-open-source / TestKeyFile

Function TestKeyFile

src/twtest/keyfile_t.cpp:46–135  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

44////////////////////////////////////////////////////////////////////////////////
45
46void TestKeyFile()
47{
48 cDebug d("TestKeyFile");
49
50 // test keyfile exceptions all work correctly
51 d.TraceDebug("Testing expception strings, IGNORE THESE ERRORS...\n");
52 cTWUtil::PrintErrorMsg(eKeyFile(_T("Testing only, ignore this.")));
53 cTWUtil::PrintErrorMsg(eKeyFileInvalidFmt(_T("Testing only, ignore this.")));
54 cTWUtil::PrintErrorMsg(eKeyFileBadPassphrase(_T("Testing only, ignore this.")));
55 cTWUtil::PrintErrorMsg(eKeyFileArchive(_T("Testing only, ignore this.")));
56 cTWUtil::PrintErrorMsg(eKeyFileUninitialized(_T("Testing only, ignore this.")));
57
58 cKeyFile keyfile;
59
60 d.TraceDebug("Generating keys...\n");
61 std::string s = "haybaby";
62 keyfile.GenerateKeys(cElGamalSig::KEY1024, (int8*)s.data(), 7);
63
64 char plaintext[9000];
65 char ciphertext[9000];
66 memset(plaintext, 42, 9000);
67
68 // encrypt
69 d.TraceDebug("Encrypting...\n");
70 {
71 cElGamalSig elGamal(*keyfile.GetPublicKey());
72
73 TEST(elGamal.GetBlockSizePlain() < 9000);
74 TEST(elGamal.GetBlockSizeCipher() < 9000);
75 std::string s = "haybaby";
76 cPrivateKeyProxy key;
77 TEST(key.AquireKey(keyfile, (int8*)s.data(), 7));
78 elGamal.SetSigning(key.GetKey());
79 elGamal.ProcessBlock(plaintext, ciphertext);
80 }
81
82 // decrypt
83 d.TraceDebug("Decrypting...\n");
84 {
85 cElGamalSig elGamal(*keyfile.GetPublicKey());
86 char recovered_text[9000];
87
88 elGamal.SetVerifying(keyfile.GetPublicKey());
89 elGamal.ProcessBlock(ciphertext, recovered_text);
90
91 TEST(memcmp(recovered_text, plaintext, elGamal.GetBlockSizePlain()) == 0);
92 }
93
94 // save to and read from memory
95 d.TraceDebug("Read/Write to memory...\n");
96 {
97 int8* pMem = new int8[keyfile.GetWriteLen()];
98 keyfile.WriteMem(pMem);
99
100 cKeyFile keyfile2;
101 TEST(!keyfile2.KeysLoaded());
102 keyfile2.ReadMem(pMem);
103 TEST(keyfile2.KeysLoaded());

Callers

nothing calls this directly

Calls 15

TwTestPathFunction · 0.85
dataMethod · 0.80
GetPublicKeyMethod · 0.80
GetBlockSizePlainMethod · 0.80
GetBlockSizeCipherMethod · 0.80
AquireKeyMethod · 0.80
SetSigningMethod · 0.80
SetVerifyingMethod · 0.80
WriteMemMethod · 0.80
KeysLoadedMethod · 0.80
ReadMemMethod · 0.80
WriteFileMethod · 0.80

Tested by

no test coverage detected