File: blowfish.cc

package info (click to toggle)
yapet 2.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,920 kB
  • sloc: cpp: 32,397; sh: 5,032; makefile: 880; ansic: 36; sed: 16
file content (92 lines) | stat: -rw-r--r-- 3,034 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "blowfish.hh"

#include <cppunit/CompilerOutputter.h>
#include <cppunit/TestCaller.h>
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>

#include "cryptoerror.hh"
#include "key448.hh"
#include "openssl.hh"

class BlowfishTest : public CppUnit::TestFixture {
   private:
    std::unique_ptr<yapet::Blowfish> blowfish;

   public:
    static CppUnit::TestSuite *suite() {
        CppUnit::TestSuite *suiteOfTests = new CppUnit::TestSuite("Blowfish");

        suiteOfTests->addTest(new CppUnit::TestCaller<BlowfishTest>(
            "should encrypt and decrypt", &BlowfishTest::encryptDecrypt));

        suiteOfTests->addTest(new CppUnit::TestCaller<BlowfishTest>(
            "should throw on decrypting corrupted data",
            &BlowfishTest::decryptCorruptData));

        suiteOfTests->addTest(new CppUnit::TestCaller<BlowfishTest>(
            "should throw on decrypting with wrong password",
            &BlowfishTest::decryptWithWrongPassword));

        suiteOfTests->addTest(new CppUnit::TestCaller<BlowfishTest>(
            "should throw on empty plain/cipher text",
            &BlowfishTest::throwOnEmptyPlainAndCipherText));

        return suiteOfTests;
    }

    void setUp() {
        std::shared_ptr<yapet::Key> key{new yapet::Key448{}};
        key->password(yapet::toSecureArray("test"));

        blowfish = std::unique_ptr<yapet::Blowfish>{new yapet::Blowfish{key}};
    }

    void encryptDecrypt() {
        auto plainText{yapet::toSecureArray("Encryption test")};
        auto cipherText = blowfish->encrypt(plainText);
        auto actual = blowfish->decrypt(cipherText);

        CPPUNIT_ASSERT(plainText == actual);
    }

    void decryptCorruptData() {
        auto plainText{yapet::toSecureArray("Encryption test")};
        auto cipherText = blowfish->encrypt(plainText);

        yapet::SecureArray corrupt{cipherText.size() - 2};
        corrupt << cipherText;

        CPPUNIT_ASSERT_THROW(blowfish->decrypt(corrupt),
                             yapet::EncryptionError);
    }

    void decryptWithWrongPassword() {
        auto plainText{yapet::toSecureArray("Encryption test")};
        auto cipherText = blowfish->encrypt(plainText);

        std::shared_ptr<yapet::Key> otherKey{new yapet::Key448{}};
        otherKey->password(yapet::toSecureArray("invalid"));

        auto otherBlowfish{
            std::unique_ptr<yapet::Blowfish>{new yapet::Blowfish{otherKey}}};

        CPPUNIT_ASSERT_THROW(otherBlowfish->decrypt(cipherText),
                             yapet::EncryptionError);
    }

    void throwOnEmptyPlainAndCipherText() {
        yapet::SecureArray empty{};

        CPPUNIT_ASSERT_THROW(blowfish->encrypt(empty), yapet::EncryptionError);
        CPPUNIT_ASSERT_THROW(blowfish->decrypt(empty), yapet::EncryptionError);
    }
};

int main() {
    yapet::OpenSSL::init();
    CppUnit::TextUi::TestRunner runner;
    runner.addTest(BlowfishTest::suite());
    return runner.run() ? 0 : 1;
}