File: rsaunittest.cpp

package info (click to toggle)
qca2 2.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,672 kB
  • sloc: cpp: 58,980; ansic: 832; perl: 133; sh: 79; makefile: 22
file content (194 lines) | stat: -rw-r--r-- 7,978 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
 * Copyright (C)  2005-2006  Brad Hards <bradh@frogmouth.net>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <QtCrypto>
#include <QtTest/QtTest>

#ifdef QT_STATICPLUGIN
#include "import_plugins.h"
#endif

class RSAUnitTest : public QObject
{
    Q_OBJECT

private Q_SLOTS:
    void initTestCase();
    void cleanupTestCase();
    void testrsa();
    void testAsymmetricEncryption();

private:
    QCA::Initializer *m_init;
};

void RSAUnitTest::initTestCase()
{
    m_init = new QCA::Initializer;
}

void RSAUnitTest::cleanupTestCase()
{
    delete m_init;
}

void RSAUnitTest::testrsa()
{
    QStringList providersToTest;
    providersToTest.append(QStringLiteral("qca-ossl"));
    // providersToTest.append("qca-gcrypt");

    foreach (const QString provider, providersToTest) {
        if (!QCA::isSupported("pkey", provider) || !QCA::PKey::supportedTypes(provider).contains(QCA::PKey::RSA) ||
            !QCA::PKey::supportedIOTypes(provider).contains(QCA::PKey::RSA))
            QWARN((QStringLiteral("RSA not supported for ") + provider).toLocal8Bit().constData());
        else {
            QCA::KeyGenerator keygen;
            QCOMPARE(keygen.isBusy(), false);
            QCOMPARE(keygen.blockingEnabled(), true);

            QList<int> keySizes;
            keySizes << 512 << 1024 << 768 << 2048;
            foreach (int keysize, keySizes) {
                QCA::PrivateKey rsaKey = keygen.createRSA(keysize, 65537, provider);
                QCOMPARE(rsaKey.isNull(), false);
                QCOMPARE(rsaKey.isRSA(), true);
                QCOMPARE(rsaKey.isDSA(), false);
                QCOMPARE(rsaKey.isDH(), false);
                QCOMPARE(rsaKey.isPrivate(), true);
                QCOMPARE(rsaKey.isPublic(), false);
                QCOMPARE(rsaKey.canSign(), true);
                QCOMPARE(rsaKey.canDecrypt(), true);
                QCOMPARE(rsaKey.canEncrypt(), true);

                QCA::RSAPrivateKey rsaPrivKey = rsaKey.toRSA();
                QCOMPARE(rsaPrivKey.bitSize(), keysize);

                QString rsaPEM = rsaKey.toPEM();
                QCOMPARE(rsaPEM.isEmpty(), false);

                QCA::ConvertResult checkResult;
                QCA::PrivateKey    fromPEMkey = QCA::PrivateKey::fromPEM(rsaPEM, QCA::SecureArray(), &checkResult);
                QCOMPARE(checkResult, QCA::ConvertGood);
                QCOMPARE(fromPEMkey.isNull(), false);
                QCOMPARE(fromPEMkey.isRSA(), true);
                QCOMPARE(fromPEMkey.isDSA(), false);
                QCOMPARE(fromPEMkey.isDH(), false);
                QCOMPARE(fromPEMkey.isPrivate(), true);
                QCOMPARE(fromPEMkey.isPublic(), false);
                QCOMPARE(rsaKey == fromPEMkey, true);

                QCA::SecureArray rsaDER = rsaKey.toDER(QCA::SecureArray("foo"));
                QCOMPARE(rsaDER.isEmpty(), false);

                QCA::PrivateKey fromDERkey = QCA::PrivateKey::fromDER(rsaDER, QCA::SecureArray("foo"), &checkResult);
                QCOMPARE(checkResult, QCA::ConvertGood);
                QCOMPARE(fromDERkey.isNull(), false);
                QCOMPARE(fromDERkey.isRSA(), true);
                QCOMPARE(fromDERkey.isDSA(), false);
                QCOMPARE(fromDERkey.isDH(), false);
                QCOMPARE(fromDERkey.isPrivate(), true);
                QCOMPARE(fromDERkey.isPublic(), false);
                QCOMPARE(rsaKey == fromDERkey, true);

                // same test, without passphrase
                rsaDER = rsaKey.toDER();
                QCOMPARE(rsaDER.isEmpty(), false);

                fromDERkey = QCA::PrivateKey::fromDER(rsaDER, QCA::SecureArray(), &checkResult);
                QCOMPARE(checkResult, QCA::ConvertGood);
                QCOMPARE(fromDERkey.isNull(), false);
                QCOMPARE(fromDERkey.isRSA(), true);
                QCOMPARE(fromDERkey.isDSA(), false);
                QCOMPARE(fromDERkey.isDH(), false);
                QCOMPARE(fromDERkey.isPrivate(), true);
                QCOMPARE(fromDERkey.isPublic(), false);
                QCOMPARE(rsaKey == fromDERkey, true);

                QCA::PublicKey pubKey = rsaKey.toPublicKey();
                QCOMPARE(pubKey.isNull(), false);
                QCOMPARE(pubKey.isRSA(), true);
                QCOMPARE(pubKey.isDSA(), false);
                QCOMPARE(pubKey.isDH(), false);
                QCOMPARE(pubKey.isPrivate(), false);
                QCOMPARE(pubKey.isPublic(), true);

                QCA::RSAPublicKey RSApubKey = pubKey.toRSA();
                QCOMPARE(RSApubKey.e(), QCA::BigInteger(65537));
                QCOMPARE(RSApubKey.isNull(), false);
                QCOMPARE(RSApubKey.isRSA(), true);
                QCOMPARE(RSApubKey.isDSA(), false);
                QCOMPARE(RSApubKey.isDH(), false);
                QCOMPARE(RSApubKey.isPrivate(), false);
                QCOMPARE(RSApubKey.isPublic(), true);
            }
        }
    }
}

void RSAUnitTest::testAsymmetricEncryption()
{
    if (!QCA::isSupported("pkey", QStringLiteral("qca-ossl")) ||
        !QCA::PKey::supportedTypes(QStringLiteral("qca-ossl")).contains(QCA::PKey::RSA) ||
        !QCA::PKey::supportedIOTypes(QStringLiteral("qca-ossl")).contains(QCA::PKey::RSA)) {
        QWARN("RSA not supported");
        QSKIP("RSA not supported. skipping");
    }
    QCA::RSAPrivateKey rsaPrivKey1 = QCA::KeyGenerator().createRSA(512, 65537, QStringLiteral("qca-ossl")).toRSA();
    QCA::RSAPublicKey  rsaPubKey1  = rsaPrivKey1.toPublicKey().toRSA();

    QCA::RSAPrivateKey rsaPrivKey2 = QCA::KeyGenerator().createRSA(512, 65537, QStringLiteral("qca-ossl")).toRSA();
    // QCA::RSAPublicKey rsaPubKey2 = rsaPrivKey2.toPublicKey().toRSA();

    const QCA::SecureArray clearText = "Hello World !";
    QCA::SecureArray       testText;
    QCA::SecureArray       cipherText;

    // Test keys #1: Enc with public, dec with private
    QVERIFY(rsaPubKey1.maximumEncryptSize(QCA::EME_PKCS1v15) >= clearText.size());
    cipherText = rsaPubKey1.encrypt(clearText, QCA::EME_PKCS1v15);
    QVERIFY(rsaPrivKey1.decrypt(cipherText, &testText, QCA::EME_PKCS1v15));
    QCOMPARE(clearText, testText);
    testText.clear();
    // ---

    // Test keys #2 to decipher key #1
    QVERIFY(!rsaPrivKey2.decrypt(cipherText, &testText, QCA::EME_PKCS1v15));
    QVERIFY(testText.isEmpty());
    // ---

    // Test keys #2: Enc with private, dec with public
    cipherText.clear();
    QVERIFY(rsaPrivKey1.maximumEncryptSize(QCA::EME_PKCS1v15) >= clearText.size());
    cipherText = rsaPrivKey1.encrypt(clearText, QCA::EME_PKCS1v15);
    QVERIFY(rsaPubKey1.decrypt(cipherText, &testText, QCA::EME_PKCS1v15));
    QCOMPARE(clearText, testText);
    testText.clear();
    // ---
}

QTEST_MAIN(RSAUnitTest)

#include "rsaunittest.moc"