| 44 | //////////////////////////////////////////////////////////////////////////////// |
| 45 | |
| 46 | void 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()); |
nothing calls this directly
no test coverage detected